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

Completion tips are returned in a different order from the lsp service. #1984

Closed
2 tasks done
tttoad opened this issue Jul 14, 2024 · 1 comment · Fixed by #2122
Closed
2 tasks done

Completion tips are returned in a different order from the lsp service. #1984

tttoad opened this issue Jul 14, 2024 · 1 comment · Fixed by #2122
Labels
bug Something isn't working

Comments

@tttoad
Copy link

tttoad commented Jul 14, 2024

FAQ

  • I have checked the FAQ and it didn't resolve my problem.

Announcement

Minimal reproducible full config

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/plug.vim')
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
call plug#end()

PlugInstall | quit
lua <<EOF
  -- Set up nvim-cmp.
  local cmp = require'cmp'
  cmp.setup({
    mapping = cmp.mapping.preset.insert({
      ['<C-b>'] = cmp.mapping.scroll_docs(-4),
      ['<C-f>'] = cmp.mapping.scroll_docs(4),
      ['<C-Space>'] = cmp.mapping.complete(),
      ['<C-e>'] = cmp.mapping.abort(),
      ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
    }),
    sources = cmp.config.sources({
      { name = 'nvim_lsp' },
      }, {
      { name = 'buffer' },
	  })
    })
	local capabilities = require('cmp_nvim_lsp').default_capabilities()
  -- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
  require('lspconfig')['gopls'].setup {
    capabilities = capabilities
  }
EOF

Description

Incorrect order of completions.
I modified gopls so that the package name contains the number.
I'm not sure this is neovim problem.
image

vscode:
image

Steps to reproduce

mkdir api/ttt.go .
Using p to trigger the package name completion tip.

Expected behavior

package0 api
package1 api_test
package2 main

Actual behavior

package0 api
package2 main
package1 api_test

Additional context

No response

@AThePeanut4
Copy link
Contributor

This happens because the default comparator list includes the length comparator:

comparators = {
compare.offset,
compare.exact,
-- compare.scopes,
compare.score,
compare.recently_used,
compare.locality,
compare.kind,
-- compare.sort_text,
compare.length,
compare.order,
},

The length comparator compares using the length of the label, and since the score of the items are the same, they end up in label length order. So removing the length comparator would solve the issue.
But a better way I think would be to use the sort_text comparator, which uses the sortText property which gopls sets to "0001", "0002", etc in the correct order. But this seems to have intentionally been commented out, not sure why.
#2020 overrides the score using the order the items were received in, but some LSPs don't send the items in the correct order (I'm seeing this in (based)pyright) - so it's not an ideal solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants