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

Work together with nvim-cmp #2

Closed
bellini666 opened this issue Feb 11, 2022 · 19 comments
Closed

Work together with nvim-cmp #2

bellini666 opened this issue Feb 11, 2022 · 19 comments

Comments

@bellini666
Copy link

Hi,

First of all, nice plugin!

Using this plugin I can no longer confirm a completion using nvim-cmp with <cr>.

Also, it would be nice if this plugin can work together with it by automatically adding (|) when completing a function, just like https://github.com/windwp/nvim-autopairs does when you configure cmp to use it.

@ZhiyuanLck
Copy link
Owner

ZhiyuanLck commented Feb 11, 2022

For nvim-cmp, maybe you can try the following config:

['<CR>'] = function()
  cmp.mapping.confirm({ select = true })()
  require('pairs'):type_enter()
end

If this not work, please inform me.

Cause I am not familiar with nvim-cmp,you can try yourself first with its interface. I will add the description about the interfaces of smart-pairs later in the readme. They are simple:

  • require('pairs'):typeset_left(left)
  • require('pairs'):typeset_right(right)
  • require('pairs'):typeset_del()
  • require('pairs'):typeset_enter()
  • require('pairs'):typeset_space()

Integration with completion plugin will be considered later.

@karlc1
Copy link

karlc1 commented Feb 11, 2022

I have this same issue, and your fix does not work. This really makes this otherwise nice plugin unusable for me.
If you allow to pass a function to enable_enter, maybe something like this could work:

enable_enter = function()
    return require("cmp").visible() == false
end

This would support other cases where you don't want the behaviour to trigger.

@ZhiyuanLck
Copy link
Owner

@karlc1 you are right. There should be three things to do in the next commit

  1. Make all enable options be a boolean value or a function that return a boolean value.
  2. Add custom fallback function when enable option is set to false
  3. Add extra before and after hook

@ZhiyuanLck
Copy link
Owner

ZhiyuanLck commented Feb 12, 2022

If enable_cond and enable_fallback options can be used to work with nvim-cmp, I'm glad to post your config in readme to help the others.

@ZhiyuanLck
Copy link
Owner

@bellini666 @karlc1 A minial workaround config about pairs completion after function may be

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

cmp.event:on('confirm_done', function(event)
  local kind = event.entry:get_completion_item().kind
  if kind == k.Method or kind == k.Function then
    require('pairs.bracket').type_left('(')
  end
end)

But I do not check if it works.

@bellini666
Copy link
Author

Hey @ZhiyuanLck ,

I could not make that with enable_cond, but I ended up doing the following:

use({
  "ZhiyuanLck/smart-pairs",
  config = function()
    require("pairs"):setup({
      enter = {
        enable_mapping = false,
      },
    })
  end,
}),

Then on cmp setup:

local cmp = require("cmp")
local kind = cmp.lsp.CompletionItemKind

cmp.setup({
  mapping = {
    ["<CR>"] = cmp.mapping(function(fallback)
      if not cmp.confirm({ select = false }) then
        require("pairs.enter").type()
      end
    end),
  },
})
cmp.event:on("confirm_done", function(event)
  local item = event.entry:get_completion_item()
  local parensDisabled = item.data and item.data.funcParensDisabled or false
  if not parensDisabled and (item.kind == kind.Method or item.kind == kind.Function) then
    require("pairs.bracket").type_left("(")
  end
end)

(note: the parensDisabled avoids adding "(" after completing an import of a function)

With that config, smart-pairs and cmp are working together nicely!

@ZhiyuanLck
Copy link
Owner

Glad to see your solution! I'll close this issue and link your comment as a temporary workaround.

@marcelarie
Copy link

idk if I'm doing something wrong but with the workaround does not work for me. When I want to select the first match from cmp it just enters.

@ajitid
Copy link

ajitid commented Feb 17, 2022

@marcelarie under <CR> mapping, try select = true

@roginfarrer
Copy link

Does anyone get errors with this solution in markdown files?

E5108: Error executing lua ...im/site/pack/packer/start/smart-pairs/lua/pairs/init.lua:395: bad argument #2 to 'triplet' (number expected, got string)
stack traceback:
	[C]: in function 'triplet'
	...im/site/pack/packer/start/smart-pairs/lua/pairs/init.lua:395: in function 'has_right_start'
	...m/site/pack/packer/start/smart-pairs/lua/pairs/enter.lua:27: in function 'type_aux'
	...m/site/pack/packer/start/smart-pairs/lua/pairs/enter.lua:61: in function 'type'
	/Users/rfarrer/.config/nvim/lua/user/plugins/cmp.lua:38: in function 'on_keymap'
	...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:144: in function 'callback'
	...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:112: in function <...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:108>
	...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:242: in function <...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:241>

@marcelarie
Copy link

marcelarie commented Feb 17, 2022

@marcelarie under <CR> mapping, try select = true

I will 👍🏽

Does anyone get errors with this solution in markdown files?

Me too.

@ZhiyuanLck
Copy link
Owner

@roginfarrer it is a small bug, please creat a new issue in the next time

@roginfarrer
Copy link

I will open a new issue now, I thought it helpful to consolidate concerns with cmp to one place. Thanks!

@marcelarie
Copy link

@marcelarie under <CR> mapping, try select = true

Do you mean like this?

		["<CR>"] = cmp.mapping(function(fallback)
			if not cmp.mapping.confirm({ select = true }) then
				require("pairs.enter").type()
			end
		end),

@ajitid
Copy link

ajitid commented Feb 18, 2022

@bellini666 — which langserver sends item.data.funcParensDisabled? I tried two (tsserver and gopls) and they don't send it.

@marcelarie — assuming what you want is typing few chars and hit enter (rather than manually selecting the first item using <c-n>), then yes, using select=true would help.

@marcelarie
Copy link

@ajitid yes I tried that but it does nothing, can't select the first item or add a new line on enter with this config.

@ajitid
Copy link

ajitid commented Feb 18, 2022

@marcelarie it should be cmp.confirm not cmp.mapping.confim

@marcelarie
Copy link

idk how I did not see that. thanks @ajitid !!

@bellini666
Copy link
Author

@bellini666 — which langserver sends item.data.funcParensDisabled? I tried two (tsserver and gopls) and they don't send it.

Pyright sends it. Don't know if it is something specific or part of the language server spec (thought it was)

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

No branches or pull requests

6 participants