nvim-autopairs icon indicating copy to clipboard operation
nvim-autopairs copied to clipboard

feat(cpp <> pairs) auto add <> for templates,includes and templated types

Open Sam-programs opened this issue 1 year ago • 9 comments

this issue #405 inspired me to make this pr this pr adds cpp_pairs to the completion pairs which does

press < 
template|    -> template<|>
#include|    -> #include <|> // extra space for include
std::vector| -> std::vector<|>

cpp_sort_cmp is there to make sure templated classes are picked over constructors for templated classes without this we wouldn't detect templated classes

Sam-programs avatar Oct 14 '23 10:10 Sam-programs

windpw this uses set_current_line i could migrate it to only use keys but that would make the code much less readable

Sam-programs avatar Oct 15 '23 11:10 Sam-programs

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 19 '23 10:12 stale[bot]

Awesome feature, I have a question on whether it would be possible to have both autopair's default confirm_done callback which completes function braces callback with your cpp_pairs callback which completees generics. And would it be possible to have cpp_pairs work with other languages that have generics as well?

kuator avatar Jan 14 '24 11:01 kuator

I have a question on whether it would be possible to have both autopair's default confirm_done callback which completes function braces callback with your cpp_pairs callback which completees generic

I think that would make the code a bit messy, Because it would be language-specific code in the general confirm_done callback.

And would it be possible to have cpp_pairs work with other languages that have generics as well?

I think it would have to be manually added for each language. templates and #include have a hardcoded check for cpp.

Sam-programs avatar Jan 14 '24 11:01 Sam-programs

I have a question on whether it would be possible to have both autopair's default confirm_done callback which completes function braces callback with your cpp_pairs callback which completees generic

I think that would make the code a bit messy, Because it would be language-specific code in the general confirm_done callback.

And would it be possible to have cpp_pairs work with other languages that have generics as well?

I think it would have to be manually added for each language. templates and #include have a hardcoded check for cpp.

Thank you for the quick answer!

kuator avatar Jan 14 '24 13:01 kuator

I have a question on whether it would be possible to have both autopair's default confirm_done callback which completes function braces callback with your cpp_pairs callback which completees generic

I think that would make the code a bit messy, Because it would be language-specific code in the general confirm_done callback.

And would it be possible to have cpp_pairs work with other languages that have generics as well?

I think it would have to be manually added for each language. templates and #include have a hardcoded check for cpp.

So I decided to keep it stupid and wrote my simple callback:

  local function on_confirm_done(evt)
    local entry = evt.entry
    local item = entry:get_completion_item()
    local types = require("cmp.types")

    if evt.commit_character then
      return
    end

    if item.label:find('<>') and vim.bo.filetype == "cs" and entry:get_kind() == types.lsp.CompletionItemKind.Class then
      local keys = vim.api.nvim_replace_termcodes('<><left>', false, false, true)
      vim.api.nvim_feedkeys(keys, "i", true)
      return
    end

    if
        vim.bo.filetype == "cs"
        and (
          entry:get_kind() == types.lsp.CompletionItemKind.Function
          or entry:get_kind() == types.lsp.CompletionItemKind.Method
          or entry:get_kind() == types.lsp.CompletionItemKind.Class
        )
    then
      local keys = vim.api.nvim_replace_termcodes('()<left>', false, false, true)
      vim.api.nvim_feedkeys(keys, "i", true)
      return
    end

  end

Now I can ditch nvim-autopairs as this was the only reason for me to use in the first place.

kuator avatar Jan 14 '24 15:01 kuator

@kuator feedkey's 'i' mode still uses mappings because there is no 'n' flag, That might cause issues.

   {escape_ks}  If true, escape K_SPECIAL bytes in `keys`. This should be
                false if you already used nvim_replace_termcodes(), and
                true otherwise.

Also the last parameter should be false, That shouldn't matter thought.

Now I can ditch nvim-autopairs as this was the only reason for me to use in the first place.

You don't manually write pairs anywhere even for []? I am just curious.

Sam-programs avatar Jan 14 '24 16:01 Sam-programs

@kuator feedkey's 'i' mode still uses mappings because there is no 'n' flag, That might cause issues.

   {escape_ks}  If true, escape K_SPECIAL bytes in `keys`. This should be
                false if you already used nvim_replace_termcodes(), and
                true otherwise.

Also the last parameter should be false, That shouldn't matter thought.

Now I can ditch nvim-autopairs as this was the only reason for me to use in the first place.

You don't manually write pairs anywhere even for []? I am just curious.

I tried to use auto-pair plugins multiple times throughout my vim journey starting from https://github.com/Raimondi/delimitMate and now nvim-autopairs and the behaviour ends up being more annoying than useful. It's simpler for me type the pairs myself. What I also did is I set up the luasnip snippets. One example is qq which converts into "" or bb which translates into (). Have already deleted nvim-autopairs, now there's no unexpected behaviour going when I type.

kuator avatar Jan 14 '24 16:01 kuator

@kuator feedkey's 'i' mode still uses mappings because there is no 'n' flag, That might cause issues.

   {escape_ks}  If true, escape K_SPECIAL bytes in `keys`. This should be
                false if you already used nvim_replace_termcodes(), and
                true otherwise.

Also the last parameter should be false, That shouldn't matter thought.

Now I can ditch nvim-autopairs as this was the only reason for me to use in the first place.

You don't manually write pairs anywhere even for []? I am just curious.

@kuator feedkey's 'i' mode still uses mappings because there is no 'n' flag, That might cause issues.

   {escape_ks}  If true, escape K_SPECIAL bytes in `keys`. This should be
                false if you already used nvim_replace_termcodes(), and
                true otherwise.

Also the last parameter should be false, That shouldn't matter thought.

Now I can ditch nvim-autopairs as this was the only reason for me to use in the first place.

You don't manually write pairs anywhere even for []? I am just curious.

Honestly, I have no idea how feedkeys works. I did a bit more research and now instead call luasnip:

  local function on_confirm_done(evt)
    local entry = evt.entry
    local item = entry:get_completion_item()
    local types = require("cmp.types")

    if evt.commit_character then
      return
    end

    if item.label:find('<>') and vim.bo.filetype == "cs" and entry:get_kind() == types.lsp.CompletionItemKind.Class then
      local ls = require('luasnip')
      ls.snip_expand(ls.s("trig", { ls.t"<", ls.i(1, ""), ls.t">" }) )
      return
    end

    if
        vim.bo.filetype == "cs"
        and (
          entry:get_kind() == types.lsp.CompletionItemKind.Function
          or entry:get_kind() == types.lsp.CompletionItemKind.Method
          or entry:get_kind() == types.lsp.CompletionItemKind.Class
        )
    then
      local ls = require('luasnip')
      ls.snip_expand(ls.s("trig", { ls.t"(", ls.i(1, ""), ls.t")" }) )

      return
    end

  end

kuator avatar Jan 14 '24 16:01 kuator