nvim-cmp icon indicating copy to clipboard operation
nvim-cmp copied to clipboard

Cmp menu width varies greatly when using Rust

Open wonbyte opened this issue 2 years ago • 4 comments

FAQ

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

Announcement

Minimal reproducible full config

local cmp = require("cmp")

local select_opts = { behavior = cmp.SelectBehavior.Select }

local has_words_before = function()
  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  return col ~= 0
    and vim.api
        .nvim_buf_get_lines(0, line - 1, line, true)[1]
        :sub(col, col)
        :match("%s")
      == nil
end

local feedkey = function(key, mode)
  vim.api.nvim_feedkeys(
    vim.api.nvim_replace_termcodes(key, true, true, true),
    mode,
    true
  )
end

cmp.setup({
  window = {
    documentation = cmp.config.window.bordered(),
  },
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },
  mapping = {
    ["<Up>"] = cmp.mapping.select_prev_item(select_opts),
    ["<Down>"] = cmp.mapping.select_next_item(select_opts),

    ["<C-p>"] = cmp.mapping.select_prev_item(select_opts),
    ["<C-n>"] = cmp.mapping.select_next_item(select_opts),

    ["<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 }),

    ["<Tab>"] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif vim.fn["vsnip#available"](1) == 1 then
        feedkey("<Plug>(vsnip-expand-or-jump)", "")
      elseif has_words_before() then
        cmp.complete()
      else
        fallback()
      end
    end, { "i", "s" }),
    ["<S-Tab>"] = cmp.mapping(function()
      if cmp.visible() then
        cmp.select_prev_item()
      elseif vim.fn["vsnip#jumpable"](-1) == 1 then
        feedkey("<Plug>(vsnip-jump-prev)", "")
      end
    end, { "i", "s" }),
  },
  sources = cmp.config.sources({
    { name = "path" },
    { name = "nvim_lsp", keyword_length = 3 },
    { name = "buffer", keyword_length = 3 },
    { name = "vsnip", keyword_length = 2 },
  }, {
    { name = "nvim_lua" },
    { name = "nvim_lsp_signature_help" },
  }),
})

local cmp_autopairs = require("nvim-autopairs.completion.cmp")
cmp.event:on(
  "confirm_done",
  cmp_autopairs.on_confirm_done({ map_char = { tex = "" } })
)

https://github.com/wonbyte/.dotfiles/blob/main/nvim/.config/nvim/lua/config/cmp.lua

Description

This may be a duplicate of #88 but I'm not sure. Is there a way to completely hide the type info? I think that's what's causing my menu to do this:

Screen Shot 2022-08-25 at 11 16 33 Screen Shot 2022-08-25 at 11 23 35

I tried looking for something in the docs that may let me use the

formating = {
  format = format,
}

but I only see these 3 options

---@alias cmp.ItemField 'abbr' | 'kind' | 'menu'
cmp.ItemField = {
  Abbr = 'abbr',
  Kind = 'kind',
  Menu = 'menu',
}

Steps to reproduce

Using rust-analyzer try the following code:

pub struct Flatten<O>
where
    O: Iterator,
{
    outer: O,
    inner: Option<O::Item>,
}

impl<O> Flatten<O>
where
    O: Iterator,
{
    pub fn new(iter: O) -> Self {
        Self {
            outer: iter,
            inner: None,
        }
    }
}

impl<O> Iterator for Flatten<O>
where
    O: Iterator,
    O::Item: IntoIterator,
{
    type Item = <O::Item as IntoIterator>::Item;

    fn next(&mut self) -> Option<Self::Item> {
        let inner_item = self.outer.next()?;
        let mut inner_iter = inner_item.into_iter();
        inner_iter.next()
    }
}


After you type inner_iter and then dot the menu should appear with the spacing issues.

Expected behavior

I can hopefully not have that type information shown so my menu width can stay at a consistent and normal width.

Actual behavior

Menu size varies greatly and can hide selection options.

Additional context

No response

wonbyte avatar Aug 25 '22 20:08 wonbyte

I ran into the same problem today, and after some debugging, found that just by remove the menu field will solve this.

cmp.setup {
  formatting = {
    format = function(entry, vim_item)
      vim_item.menu = nil
      return vim_item
    end,
  }
}

For someone using lspkind

cmp.setup {
  formatting = {
    format = lspkind.cmp_format {
      menu = {},
    },  
  }
}

ikws4 avatar Sep 11 '22 08:09 ikws4

formatting = {
    format = function(entry, vim_item)
      vim_item.menu = nil
      return vim_item
    end,
  }

It works!!!! Bless you random stranger from the internet! I thought setting

formatting = {
  fields = {'abbr', 'kind'}
},

and omitting the menu would do the same thing, but it doesn't. Thank you again 🙏

wonbyte avatar Sep 11 '22 16:09 wonbyte

It seems that there are too many characters in the menu, causing the menu to become wider. Try truncating the characters.

  formatting = {
    format = lspkind.cmp_format({
      with_text = true, -- do not show text alongside icons
      maxwidth = 50,
      before = function(entry, vim_item)
        local m = vim_item.menu and vim_item.menu or ""
        if #m > 20 then
          vim_item.menu = string.sub(m, 1, 20) .. "..."
        end
        return vim_item
      end,
    }),
  },
屏幕截图 2023-12-31 193113

JavaHello avatar Dec 31 '23 11:12 JavaHello

Had the same issue with C++ and this solved it, maybe we can add this to the plugin's documentation? Or even have an option for configuring the max width of completion items?

tom-anders avatar Jan 03 '24 14:01 tom-anders