Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 9 commits into from
Closed

Conversation

Sam-programs
Copy link
Contributor

@Sam-programs Sam-programs commented Oct 14, 2023

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
Copy link
Contributor Author

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

Copy link

stale bot commented Dec 19, 2023

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 stale bot added the wontfix This will not be worked on label Dec 19, 2023
@kuator
Copy link

kuator commented Jan 14, 2024

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?

@stale stale bot removed the wontfix This will not be worked on label Jan 14, 2024
@Sam-programs
Copy link
Contributor Author

Sam-programs commented Jan 14, 2024

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.

@kuator
Copy link

kuator commented Jan 14, 2024

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
Copy link

kuator commented Jan 14, 2024

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.

@Sam-programs
Copy link
Contributor Author

Sam-programs commented Jan 14, 2024

@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
Copy link

kuator commented Jan 14, 2024

@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
Copy link

kuator commented Jan 14, 2024

@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

@Sam-programs Sam-programs closed this by deleting the head repository Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants