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

fix: make sure nvim-cmp keymaps are registered #2087

Closed

Conversation

tronikelis
Copy link

Context

When editing files with the https://github.com/microsoft/compose-language-service LSP, the popup would always appear on new lines like so:

Completion appears on fully whitespace line

As you can see, this interferes with my fallback Tab keymap which indents the code and its very annoying

2024-11-09.17-20-25.mp4

This is with keyword_length = 1

The underlying issue

I think the underlying issue is:

  1. docker compose LSP acts differently (this does not happen on other lsps)
  2. nvim-cmp, even with keyword_length = 1 still shows the menu when there are no keywords (fully whitespace line), unless this is the expected behavior

Workaround

I started using this config to workaround the issue

enabled = function()
    return #vim.trim(vim.api.nvim_get_current_line()) ~= 0
end,

But then I saw that sometimes my completion selection just stopped working altogether like so:

Completion stops
2024-11-09.17-08-10.mp4

This PR

This pr makes the workaround actually work

With this PR
2024-11-09.17-07-44.mp4

@tronikelis tronikelis force-pushed the fix/make_sure_nvim_is_initialized branch from e255ad7 to 7491c46 Compare November 9, 2024 19:16
@tronikelis tronikelis changed the title fix: make sure nvim keymaps are registered fix: make sure nvim-cmp keymaps are registered Nov 10, 2024
@hrsh7th
Copy link
Owner

hrsh7th commented Nov 21, 2024

IMO, you can use get_trigger_characters option.

check out it.

require('cmp').setup({
  ...
  completion = {
    get_trigger_characters = function(trigger_characters)
      return vim.iter(trigger_characters):filter(function(char)
        return not chat:match('^%s*$')
      end)
    end
  }
  ...
})

@tronikelis
Copy link
Author

IMO, you can use get_trigger_characters option.

check out it.

require('cmp').setup({
  ...
  completion = {
    get_trigger_characters = function(trigger_characters)
      return vim.iter(trigger_characters):filter(function(char)
        return not chat:match('^%s*$')
      end)
    end
  }
  ...
})

Damn, nice, seems to work, but I am not finding this in the doc?

@tronikelis
Copy link
Author

With your config option there is a problem,

When completing a function the signature help and completion window does not open by itself anymore (which I want)

@tronikelis
Copy link
Author

Do you see any problems with this fix?

@tronikelis
Copy link
Author

this seems to work

get_trigger_characters = function(trigger_characters)
    if #vim.trim(vim.api.nvim_get_current_line()) == 0 then
        return {}
    end

    return trigger_characters
end,

@nmrtv
Copy link

nmrtv commented Nov 27, 2024

This commit introduced a bug where the 'enabled' option is not respected in prompt buffers. For example, in the Telescope prompt, it provides autocomplete, which is quite annoying.

@tronikelis
Copy link
Author

This commit introduced a bug where the 'enabled' option is not respected in prompt buffers. For example, in the Telescope prompt, it provides autocomplete, which is quite annoying.

hmm i cant seem to reproduce, what is your config?

@nmrtv
Copy link

nmrtv commented Nov 28, 2024

The configuration is fairly standard. With this commit enabled, you can open Telescope's live grep, type a few characters, and try navigating the search results using the standard Ctrl+n and Ctrl+p shortcuts. However, instead of navigating through the Telescope search results, the nvim-cmp completion menu appears.

@nmrtv
Copy link

nmrtv commented Nov 28, 2024

This happens because nvim-cmp is disabled in prompt buffers. However, with this commit, it is enabled in prompts.

@tronikelis
Copy link
Author

This happens because nvim-cmp is disabled in prompt buffers. However, with this commit, it is enabled in prompts.

doesnt happen in my config which I havent touched the enabled, can you send your enabled function?

@nmrtv
Copy link

nmrtv commented Nov 28, 2024

It's default:

enabled = function()

@nmrtv
Copy link

nmrtv commented Nov 28, 2024

I don't know if it helps, but nvim-cmp mappings are the same as telescope:

			["<C-n>"] = cmp.mapping(function(fallback)
				if cmp.visible() then
					cmp.select_next_item()
				elseif has_words_before() then
					cmp.complete()
				else
					fallback()
				end
			end, { "i", "c", "s" }),
			["<C-p>"] = cmp.mapping(function(fallback)
				if cmp.visible() then
					cmp.select_prev_item()
				else
					fallback()
				end
			end, { "i", "c", "s" }),

@tronikelis
Copy link
Author

tronikelis commented Nov 28, 2024

I don't know if it helps, but nvim-cmp mappings are the same as telescope:

			["<C-n>"] = cmp.mapping(function(fallback)
				if cmp.visible() then
					cmp.select_next_item()
				elseif has_words_before() then
					cmp.complete()
				else
					fallback()
				end
			end, { "i", "c", "s" }),
			["<C-p>"] = cmp.mapping(function(fallback)
				if cmp.visible() then
					cmp.select_prev_item()
				else
					fallback()
				end
			end, { "i", "c", "s" }),

thanks, however it shouldn't matter based on your keymaps, maybe I misunderstood what cmp:prepare does,

I just pushed a quick commit that tries to call less stuff, can you check it? If it still does not work then I will just use the alternative which is in an earlier comment and close the PR

@nmrtv
Copy link

nmrtv commented Nov 28, 2024

thanks, however it shouldn't matter based on your keymaps, maybe I misunderstood what cmp:prepare does,

Well, I think it matters because this issue wouldn’t occur if the mappings in Telescope and nvim-cmp were different, and ctrl+n and ctrl+p didn’t trigger nvim-cmp completion. Thanks, I'll look at your fix tomorrow.

@tronikelis
Copy link
Author

thanks, however it shouldn't matter based on your keymaps, maybe I misunderstood what cmp:prepare does,

Well, I think it matters because this issue wouldn’t occur if the mappings in Telescope and nvim-cmp were different, and ctrl+n and ctrl+p didn’t trigger nvim-cmp completion. Thanks, I'll look at your fix tomorrow.

I have the same c-n c-p mappings

@tronikelis
Copy link
Author

closing in favor of #2087 (comment)

@tronikelis tronikelis closed this Nov 30, 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.

3 participants