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

Cmp menu width varies greatly when using Rust #1154

Open
2 tasks done
wonbyte opened this issue Aug 25, 2022 · 4 comments
Open
2 tasks done

Cmp menu width varies greatly when using Rust #1154

wonbyte opened this issue Aug 25, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@wonbyte
Copy link

wonbyte commented Aug 25, 2022

FAQ

  • 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 wonbyte added the bug Something isn't working label Aug 25, 2022
@wonbyte wonbyte changed the title Cmp menu width varies greatly why using Rust Cmp menu width varies greatly when using Rust Aug 25, 2022
@ikws4
Copy link

ikws4 commented Sep 11, 2022

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 = {},
    },  
  }
}

@wonbyte
Copy link
Author

wonbyte commented Sep 11, 2022

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 🙏

@JavaHello
Copy link

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

@tom-anders
Copy link

tom-anders commented Jan 3, 2024

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?

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

No branches or pull requests

4 participants