Skip to content

Commit 9463ab8

Browse files
azdanovmehalter
andauthored
refactor: extract truncate logic
Co-authored-by: Micah Halter <[email protected]>
1 parent 092b571 commit 9463ab8

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/content/docs/recipes/cmp.mdx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,20 @@ return {
153153
"hrsh7th/nvim-cmp",
154154
opts = function(_, opts)
155155
local astrocore, astroui = require "astrocore", require "astroui"
156+
local function truncate(str, len)
157+
if not str then return end
158+
local truncated = vim.fn.strcharpart(str, 0, len)
159+
return truncated == str and str or truncated .. astroui.get_icon "Ellipsis"
160+
end
161+
156162
if not opts.formatting then opts.formatting = {} end
157163
opts.formatting.format = astrocore.patch_func(opts.formatting.format, function(format, ...)
158164
-- get item from original formatting function
159165
local vim_item = format(...)
160166

161-
-- truncate label to maximum of 25% of the window
162-
local label = vim_item.abbr
163-
local truncated_label = vim.fn.strcharpart(label, 0, math.floor(0.25 * vim.o.columns))
164-
if truncated_label ~= label then vim_item.abbr = truncated_label .. astroui.get_icon "Ellipsis" end
165-
166-
-- truncate menu to maximum of 25% of the window
167-
if vim_item.menu then
168-
local menu = vim_item.menu
169-
local truncated_menu = vim.fn.strcharpart(menu, 0, math.floor(0.25 * vim.o.columns))
170-
if truncated_menu ~= menu then vim_item.menu = truncated_menu .. astroui.get_icon "Ellipsis" end
171-
end
167+
-- truncate text fields to maximum of 25% of the window
168+
vim_item.abbr = truncate(vim_item.abbr, math.floor(0.25 * vim.o.columns))
169+
vim_item.menu = truncate(vim_item.menu, math.floor(0.25 * vim.o.columns))
172170

173171
return vim_item
174172
end)

0 commit comments

Comments
 (0)