hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.88k stars 394 forks source link

Some information is put in formatting field "menu", which doesn't get truncated by maxwidth #1791

Closed h3xOo closed 9 months ago

h3xOo commented 9 months ago

FAQ

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 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/vim-vsnip'
Plug 'neovim/nvim-lspconfig'
Plug 'onsails/lspkind.nvim'
call plug#end()
PlugInstall | quit

" Setup global configuration. More on configuration below.
lua << EOF
local cmp = require "cmp"
cmp.setup {
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },

  mapping = {
    ['<CR>'] = cmp.mapping.confirm({ select = true })
  },

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "buffer" },
  }),
  formatting = {
      fields = { 'abbr', 'kind', 'menu', },
      format = require('lspkind').cmp_format({
      preset = 'default',
      mode = 'symbol_text',
      maxwidth = 30,
      ellipsis_char = '...',
      menu = {
          buffer = '[Buffer]',
          nvim_lsp = '[LSP]',
          luasnip = '[LuaSnip]',
          nvim_lua = '[Lua]',
          latex_symbols = '[Latex]',
      },
      })
      },
}
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

require'lspconfig'.pyright.setup {
  capabilities = capabilities,
}
EOF

Description

Some information (like name of package) are in menu field, not abbr. Here's screenshot with example config pyright

Steps to reproduce

Setup cmp with lspkind and newest pyright.

Expected behavior

Information would be in abbr field.

Actual behavior

It's in menu, which doesn't get truncated by maxwidth, making some windows half-screen wide.

Additional context

I noticed it also happens in clangd 17.0.3 I have installed by mason. (This one sometimes takes full screen by function parameters after [LSP]) clangd_new Like this one clangd_screen_wide

But in clangd 16.0.6 installed from my system package manager this doesn't happen clangd_old This was screen wide in version 17, but in 16 function parameters are in abbr field and get truncated clangd_not_screen_wide

I also managed to hack function parameters in menu field like this (inspired by some example from wiki)

formatting = {
    fields = { "abbr", "kind", "menu" },
    format = function(entry, vim_item)
        local menu_length = 0
        if vim_item.menu ~= nil then
            menu_length = string.len(vim_item.menu)
        end
        local kind = require("lspkind").cmp_format({
            preset = 'default',
            mode = 'symbol_text',
            maxwidth = 50,
            ellipsis_char = '...',
            menu = {
                buffer = '[Buffer]',
                nvim_lsp = '[LSP]',
                luasnip = '[LuaSnip]',
                nvim_lua = '[Lua]',
                latex_symbols = '[Latex]',
            },
        })(entry, vim_item)
        if vim_item.menu ~= nil then
            local new_menu_length = string.len(vim_item.menu)
            kind.menu = string.sub(vim_item.menu, 1, new_menu_length - menu_length)
        end
        return kind
    end,
},
hrsh7th commented 9 months ago

This is not a issue of cmp. If you want to truncate menu field, you can implement it with formatting.format option. (or lspkind.nvim has option for it. You can use before option. see