Replies: 4 comments 4 replies
-
This is not exactly what you want, but you may find this solution useful: you can use the local ELLIPSIS_CHAR = '…'
local MAX_LABEL_WIDTH = 20
cmp.setup({
formatting = {
format = function(entry, vim_item)
local label = vim_item.abbr
local truncated_label = vim.fn.strcharpart(label, 0, MAX_LABEL_WIDTH)
if truncated_label ~= label then
vim_item.abbr = truncated_label .. ELLIPSIS_CHAR
end
end,
},
}) |
Beta Was this translation helpful? Give feedback.
-
Based on @dmitmel but it tries to keep the width consistent (note: scroll adds extra space) local ELLIPSIS_CHAR = '…'
local MAX_LABEL_WIDTH = 25
local MAX_KIND_WIDTH = 14
local get_ws = function (max, len)
return (" "):rep(max - len)
end
local format = function(_, item)
local content = item.abbr
-- local kind_symbol = symbols[item.kind]
-- item.kind = kind_symbol .. get_ws(MAX_KIND_WIDTH, #kind_symbol)
if #content > MAX_LABEL_WIDTH then
item.abbr = vim.fn.strcharpart(content, 0, MAX_LABEL_WIDTH) .. ELLIPSIS_CHAR
else
item.abbr = content .. get_ws(MAX_LABEL_WIDTH, #content)
end
return item
end
cmp.setup({
formatting = {
format = format,
},
}) |
Beta Was this translation helpful? Give feedback.
-
Here's a version of the completion entry truncation function that adapts the truncation and padding of completion entries based on Neovim's window width, or uses a fixed width for the completion menu, depending on the provided configuration, as per @krishnakumarg1984's comment. When Here's what it looks like inside the formatting block in my formatting = {
fields = { "abbr", "menu", "kind" },
format = function(entry, item)
-- Define menu shorthand for different completion sources.
local menu_icon = {
nvim_lsp = "NLSP",
nvim_lua = "NLUA",
luasnip = "LSNP",
buffer = "BUFF",
path = "PATH",
}
-- Set the menu "icon" to the shorthand for each completion source.
item.menu = menu_icon[entry.source.name]
-- Set the fixed width of the completion menu to 60 characters.
-- fixed_width = 20
-- Set 'fixed_width' to false if not provided.
fixed_width = fixed_width or false
-- Get the completion entry text shown in the completion window.
local content = item.abbr
-- Set the fixed completion window width.
if fixed_width then
vim.o.pumwidth = fixed_width
end
-- Get the width of the current window.
local win_width = vim.api.nvim_win_get_width(0)
-- Set the max content width based on either: 'fixed_width'
-- or a percentage of the window width, in this case 20%.
-- We subtract 10 from 'fixed_width' to leave room for 'kind' fields.
local max_content_width = fixed_width and fixed_width - 10 or math.floor(win_width * 0.2)
-- Truncate the completion entry text if it's longer than the
-- max content width. We subtract 3 from the max content width
-- to account for the "..." that will be appended to it.
if #content > max_content_width then
item.abbr = vim.fn.strcharpart(content, 0, max_content_width - 3) .. "..."
else
item.abbr = content .. (" "):rep(max_content_width - #content)
end
return item
end,
}, Fixed width menu example: Dynamic width menu example: |
Beta Was this translation helpful? Give feedback.
-
is it possible to do this for the documentation window as well? |
Beta Was this translation helpful? Give feedback.
-
Hi, I would like to hide my suggestions that are longer than 10 letters. Usually I never accept verylongsuggestionslikethis. How can I do it?
Beta Was this translation helpful? Give feedback.
All reactions