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

Integration with nvim-cmp #5

Open
xulongwu4 opened this issue Feb 17, 2023 · 21 comments
Open

Integration with nvim-cmp #5

xulongwu4 opened this issue Feb 17, 2023 · 21 comments
Labels
low priority Not planed for the near future

Comments

@xulongwu4
Copy link

Hi, lovely plugin. Do you support integration with nvim-cmp just like nvim-autopairs does?

https://github.com/hrsh7th/nvim-cmp/wiki/Advanced-techniques#add-parentheses-after-selecting-function-or-method-item

@altermo

This comment was marked as off-topic.

@altermo altermo closed this as completed Feb 18, 2023
@xulongwu4
Copy link
Author

xulongwu4 commented Feb 18, 2023

Hi again, thanks for the help! I might have missed something, but I couldn't make it work with your code snippet. In case my request didn't go through: I was asking whether it is possible to add a pair of parenthesis to a cmp completion item which is a function. This was accomplishable by nvim-autopairs as shown in the link I provided above. I am not good at reading lua code or nvim apis, but I didn't see the part related to check whether the completion item is a function and adding the parenthesis etc. in the cmpnewline function. I would appreciate your help again!

@altermo altermo reopened this Feb 18, 2023
@xulongwu4
Copy link
Author

This is a gif showing the effect that I want to achieve:

nvim-autopair

@altermo
Copy link
Owner

altermo commented Feb 18, 2023

Currently that specific feature is not possible.
I may decide to add the feature in the future.

@altermo altermo added the low priority Not planed for the near future label May 30, 2023
@XXiaoA
Copy link

XXiaoA commented Sep 17, 2023

Looking forward to this useful feature!

@GersiD
Copy link

GersiD commented Sep 27, 2023

This feature is indeed very useful, looking forward to it 😄

@weilbith
Copy link

Sorry, currently don't find the resources, but doesn't nvim-cmp support inserting parenthesis after the completion by itself?

@GersiD
Copy link

GersiD commented Sep 27, 2023

If it does then I can't figure out how to. I just switched from LazyVim to Kickstart and I tried getting the function completion to work like lazy had.and lazy doesn't use the cmp:on_event trick but I have no idea how it gets function completion to work otherwise.

@weilbith
Copy link

Okay, it seems like this is a feature of the LSP completion where some servers provide this feature by using snippets. Sorry.

@GersiD
Copy link

GersiD commented Sep 27, 2023

Yes sir. This is the last reason I still use nvim-autopairs.

@hungnguyen1503
Copy link

Hello, I'm still looking forward to this useful feature

@GersiD
Copy link

GersiD commented Sep 30, 2023

Very wholesome, very useful feature. In the meantime try and configure the LSP you use to send snippets for completion. This varies per LSP. That's how I got around it so far. Clangd, jdtls, luals, and pyright all support it. So thats all I need for now.

@altermo
Copy link
Owner

altermo commented Sep 30, 2023

If anyone wants this feature before I implement it:

local cmp=require('cmp')
local Kind=cmp.lsp.CompletionItemKind
cmp.event:on(
  'confirm_done',
  function (evt)
    if vim.tbl_contains({Kind.Function,Kind.Method},evt.entry:get_completion_item().kind) then
      vim.api.nvim_feedkeys('()'..vim.api.nvim_replace_termcodes('<Left>',true,true,true),'n',false)
    end
  end)

@weilbith
Copy link

I was actually thinking about exactly such a solution. Probably just need to make it a liiiittle more advanced, so it does not interfere with server which do actually send additional snippets to add them too.

@mawkler
Copy link

mawkler commented Oct 3, 2023

@altermo This works great, but I found that it doesn't work on JavaScript/TypeScript arrow functions:

const foo = (bar, baz) => bar + baz;

I realize that this is trickier, but is this something that would be possible to support?

@altermo
Copy link
Owner

altermo commented Oct 3, 2023

How would it work exactly?
What would the trigger be for the auto parentheses?

@mawkler
Copy link

mawkler commented Oct 6, 2023

As I was typing out a response to this I realize that it's probably too difficult :)

@mawkler
Copy link

mawkler commented Oct 20, 2023

I modified @altermo's temporary solution to exclude lua_ls and rust_analyzer, since they already create pairs themselves.

The pcall is to catch any null pointer exception when doing event.entry.source.source.client.config.name.

local cmp = require('cmp')
local ind = cmp.lsp.CompletionItemKind

local function ls_name_from_event(event)
  return event.entry.source.source.client.config.name
end

-- Add parenthesis on completion confirmation
cmp.event:on('confirm_done', function(event)
  local ok, ls_name = pcall(ls_name_from_event, event)
  if ok and vim.tbl_contains({ 'rust_analyzer', 'lua_ls' }, ls_name) then
    return
  end

  local completion_kind = event.entry:get_completion_item().kind
  if vim.tbl_contains({ ind.Function, ind.Method }, completion_kind) then
    local left = vim.api.nvim_replace_termcodes('<Left>', true, true, true)
    vim.api.nvim_feedkeys('()' .. left, 'n', false)
  end
end)

@hungnguyen1503
Copy link

Hi @mawkler, thanks a lot , I have used your config with pylsp very well

@Andrew15-5
Copy link

Is there an official response why something like this isn't implemented in cmp-nvim directly? Or at least a snippet like this for those that don't want to rely on per-autopair plugin implementation. I don't think that any autopair plugin actually needs to insert that parenthesis pair in a special way. Unless it does?

P.S. I started searching here after I stumbled upon hrsh7th/nvim-cmp#1672. I also had to include tinymist in the list.

@altermo
Copy link
Owner

altermo commented May 24, 2024

It will take some time until I come up with a good design, so in the meantime, I created an experimental version.

To use it:

require'ultimate-autopair'.init({
  require'ultimate-autopair'.extend_default{
      --Normal config goes here
  },
  {profile=require'ultimate-autopair.experimental.cmpair'.init},
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
low priority Not planed for the near future
Projects
None yet
Development

No branches or pull requests

8 participants