Skip to content

Commit 9a008c9

Browse files
committed
feat: add back min_width to autocomplete
1 parent 7afb06c commit 9a008c9

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ For LazyVim/distro users, you can disable nvim-cmp via:
235235

236236
windows = {
237237
autocomplete = {
238+
min_width = 30,
238239
max_height = 10,
239240
border = 'none',
240241
winhighlight = 'Normal:BlinkCmpMenu,FloatBorder:BlinkCmpMenuBorder,CursorLine:BlinkCmpMenuSelection,Search:None',

lua/blink/cmp/config.lua

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
--- @field use_nvim_cmp_as_default? boolean
8383

8484
--- @class blink.cmp.AutocompleteConfig
85+
--- @field min_width? number
8586
--- @field max_height? number
8687
--- @field border? blink.cmp.WindowBorder
8788
--- @field order? "top_down" | "bottom_up"
@@ -232,6 +233,7 @@ local config = {
232233

233234
windows = {
234235
autocomplete = {
236+
min_width = 15,
235237
max_height = 10,
236238
border = 'none',
237239
winhighlight = 'Normal:BlinkCmpMenu,FloatBorder:BlinkCmpMenuBorder,CursorLine:BlinkCmpMenuSelection,Search:None',

lua/blink/cmp/windows/autocomplete.lua

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local autocomplete = {
2121

2222
function autocomplete.setup()
2323
autocomplete.win = require('blink.cmp.windows.lib').new({
24+
min_width = autocmp_config.min_width,
2425
max_height = autocmp_config.max_height,
2526
border = autocmp_config.border,
2627
winhighlight = autocmp_config.winhighlight,
@@ -207,7 +208,7 @@ function autocomplete.draw()
207208
)
208209
end
209210

210-
local max_lengths = renderer.get_max_lengths(components_list)
211+
local max_lengths = renderer.get_max_lengths(components_list, autocmp_config.min_width)
211212
autocomplete.rendered_items = vim.tbl_map(
212213
function(component) return renderer.render(component, max_lengths) end,
213214
components_list
@@ -236,7 +237,12 @@ function autocomplete.render_item_simple(ctx)
236237
return {
237238
' ',
238239
{ ctx.kind_icon, ctx.icon_gap, hl_group = 'BlinkCmpKind' .. ctx.kind },
239-
{ ctx.item.label, fill = true, hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel' },
240+
{
241+
ctx.item.label,
242+
fill = true,
243+
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
244+
max_width = 50,
245+
},
240246
' ',
241247
}
242248
end
@@ -250,6 +256,7 @@ function autocomplete.render_item_reversed(ctx)
250256
ctx.item.label,
251257
fill = true,
252258
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
259+
max_width = 50,
253260
},
254261
' ',
255262
{ ctx.kind_icon, ctx.icon_gap, ctx.kind, hl_group = 'BlinkCmpKind' .. ctx.kind },

lua/blink/cmp/windows/lib/render.lua

+19-11
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ function renderer.draw_highlights(rendered, bufnr, ns, line_number)
4141
end
4242
end
4343

44-
---@param strings string[]
45-
---@param max_width? number
46-
---@return blink.cmp.StringsBuild
47-
function renderer.build_strings(strings, max_width)
44+
--- Gets the concatenated text and length for a list of strings
45+
--- and truncates if necessary when max_width is set
46+
--- @param strings string[]
47+
--- @param max_width? number
48+
--- @return blink.cmp.StringsBuild
49+
function renderer.concat_strings(strings, max_width)
4850
local text = ''
4951
for _, component in ipairs(strings) do
5052
text = text .. component
@@ -67,20 +69,20 @@ function renderer.render(components, lengths)
6769
text = text .. component
6870
offset = offset + #component
6971
else
70-
local build = renderer.build_strings(component, component.max_width)
72+
local concatenated = renderer.concat_strings(component, component.max_width)
7173

7274
table.insert(highlights, {
7375
start = offset + 1,
74-
stop = offset + #build.text + 1,
76+
stop = offset + #concatenated.text + 1,
7577
group = component.hl_group,
7678
params = component.hl_params,
7779
})
7880

79-
text = text .. build.text
80-
offset = offset + #build.text
81+
text = text .. concatenated.text
82+
offset = offset + #concatenated.text
8183

8284
if component.fill then
83-
local spaces = lengths[i] - build.length
85+
local spaces = lengths[i] - concatenated.length
8486
text = text .. string.rep(' ', spaces)
8587
offset = offset + spaces
8688
end
@@ -96,21 +98,27 @@ function renderer.get_length(component)
9698
if type(component) == 'string' then
9799
return vim.api.nvim_strwidth(component)
98100
else
99-
local build = renderer.build_strings(component, component.max_width)
101+
local build = renderer.concat_strings(component, component.max_width)
100102
return build.length
101103
end
102104
end
103105

104106
--- @param components_list (blink.cmp.Component | string)[][]
107+
--- @param min_width number
105108
--- @return number[]
106-
function renderer.get_max_lengths(components_list)
109+
function renderer.get_max_lengths(components_list, min_width)
107110
local lengths = {}
108111
for _, components in ipairs(components_list) do
109112
for i, component in ipairs(components) do
110113
local length = renderer.get_length(component)
111114
if not lengths[i] or lengths[i] < length then lengths[i] = length end
112115
end
113116
end
117+
118+
for i, length in ipairs(lengths) do
119+
if length < min_width then lengths[i] = min_width end
120+
end
121+
114122
return lengths
115123
end
116124

0 commit comments

Comments
 (0)