Closed anthony-S93 closed 5 months ago
It turns out that gopls
pre-selects an item each time an auto-completion list is generated. And gopls
's pre-selection does not respect the ordering of the list generated by the completion source. As a workaround, you can prevent this behavior by setting preselect
to cmp.PreselectMode.None
. E.g:
local cmp = require("cmp")
cmp.setup({
preselect = cmp.PreselectMode.None
})
It turns out that
gopls
pre-selects an item each time an auto-completion list is generated. Andgopls
's pre-selection does not respect the ordering of the list generated by the completion source. This behavior can be prevented by settingpreselect
tocmp.PreselectMode.None
. E.g:local cmp = require("cmp") cmp.setup({ preselect = cmp.PreselectMode.None })
With this setting there is no preselect at all. What if I want the first one to be selected always?
The problem is that nvim-cmp
reorders the items of the completion list dynamically. What this means is that the order of items in the completion list depends on the current state of the source file. For example, if the name of a function has already appeared in the file at least once, then that item will be placed higher up the list. I've already described the issue in detail here: https://github.com/golang/go/issues/61533#issuecomment-1923037686
Unfortunatly, nvim-cmp
's dynamic reordering does not take LSP preselection into account. By right, nvim-cmp
should place the item that is preselected by the LSP at the top of the list. That is why I resorted to turn off the preselect
feature altogether.
I can reopen this issue if you want. But at the moment, I'm not interested in any further investigation into this issue.
FAQ
Announcement
Minimal reproducible full config
Description
With
gopls
, the selection cursor will not appear at the top of the autocompletion list as it does with other language servers such aspyright
,jdtls
,tsserver
, etc. This inconsistency bothers me because it often led to typing mistakes (since all other language servers ensure that the cursor always appears at the beginning of the list, withgopls
, I have the tendency to press enter thinking that the top item is selected when in reality, the cursor is located at an item somewhere below the list).I will use two examples to illustrate this issue.
Example 1: snippets
https://github.com/neovim/neovim/assets/69449791/a65f897f-5779-4657-afc4-53ed16d5f22b
As you can see, when the completion list pops up, the second item is selected instead of the top item. Here is the lsp log file for this example: snippets-completion-issue.log
Example 2: normal autocompletion
https://github.com/neovim/neovim/assets/69449791/82867e68-be15-4a7d-8b94-73a568998359
In this example, it seems to me like the cursor pre-selects the item based on alphabetical order despite the fact that the autocompletion list isn't ordered alphabetically. Here is the lsp log file: normal-autocompletion-issue.log
Steps to reproduce
The environment used to demonstrate the issue can be set up via the following steps:
go
andgopls
$ mkdir testing
cd testing
$ go mod init example/testing
$touch test.go
nvim -u /path/to/minimal_init.lua test.go
package
on the first line of the file.At this point, the behavior on your screen should mirror the behavior in the video I posted above.
Expected behavior
The cursor should select the top item in the autocompletion list.
Actual behavior
The cursor does not select the top item in the autocompletion list.
Additional context
Please note that this behavior occurs with
gopls
only and no other language server. I'm aware that this likely indicates that the issue is withgopls
. But I have already opened an issue withgopls
golang/go#61533. The developer's response is that the issue isn't with them.