Skip to content

Commit 371ad28

Browse files
lopi-pySaghen
andauthored
fix: handle gap for empty text (#301)
* fix: handle gap for empty text fix: set kind component fill to true * refactor: minor stylistic changes --------- Co-authored-by: Liam Dyer <[email protected]>
1 parent a1442b9 commit 371ad28

File tree

6 files changed

+66
-73
lines changed

6 files changed

+66
-73
lines changed

README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -429,19 +429,20 @@ MiniDeps.add({
429429
components = {
430430
kind_icon = {
431431
ellipsis = false,
432-
text = function(ctx) return ctx.kind_icon .. ' ' end,
432+
text = function(ctx) return ctx.kind_icon end,
433433
highlight = function(ctx) return 'BlinkCmpKind' .. ctx.kind end,
434434
},
435435

436436
kind = {
437437
ellipsis = false,
438-
text = function(ctx) return ctx.kind .. ' ' end,
438+
width = { fill = true },
439+
text = function(ctx) return ctx.kind end,
439440
highlight = function(ctx) return 'BlinkCmpKind' .. ctx.kind end,
440441
},
441442

442443
label = {
443444
width = { fill = true, max = 60 },
444-
text = function(ctx) return ctx.label .. (ctx.label_detail or '') end,
445+
text = function(ctx) return ctx.label .. ctx.label_detail end,
445446
highlight = function(ctx)
446447
-- label and label details
447448
local highlights = {
@@ -452,10 +453,8 @@ MiniDeps.add({
452453
end
453454

454455
-- characters matched on the label by the fuzzy matcher
455-
if ctx.label_matched_indices ~= nil then
456-
for _, idx in ipairs(ctx.label_matched_indices) do
457-
table.insert(highlights, { idx, idx + 1, group = 'BlinkCmpLabelMatch' })
458-
end
456+
for _, idx in ipairs(ctx.label_matched_indices) do
457+
table.insert(highlights, { idx, idx + 1, group = 'BlinkCmpLabelMatch' })
459458
end
460459

461460
return highlights
@@ -464,7 +463,7 @@ MiniDeps.add({
464463

465464
label_description = {
466465
width = { max = 30 },
467-
text = function(ctx) return ctx.label_description or '' end,
466+
text = function(ctx) return ctx.label_description end,
468467
highlight = 'BlinkCmpLabelDescription',
469468
},
470469
},

lua/blink/cmp/config.lua

+8-9
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ local config = {
270270
timeout_ms = 400,
271271
},
272272
},
273-
expand_snippet = vim.snippet.expand
273+
expand_snippet = vim.snippet.expand,
274274
},
275275

276276
trigger = {
@@ -407,19 +407,20 @@ local config = {
407407
components = {
408408
kind_icon = {
409409
ellipsis = false,
410-
text = function(ctx) return ctx.kind_icon .. ' ' end,
410+
text = function(ctx) return ctx.kind_icon end,
411411
highlight = function(ctx) return 'BlinkCmpKind' .. ctx.kind end,
412412
},
413413

414414
kind = {
415415
ellipsis = false,
416-
text = function(ctx) return ctx.kind .. ' ' end,
416+
width = { fill = true },
417+
text = function(ctx) return ctx.kind end,
417418
highlight = function(ctx) return 'BlinkCmpKind' .. ctx.kind end,
418419
},
419420

420421
label = {
421422
width = { fill = true, max = 60 },
422-
text = function(ctx) return ctx.label .. (ctx.label_detail or '') end,
423+
text = function(ctx) return ctx.label .. ctx.label_detail end,
423424
highlight = function(ctx)
424425
-- label and label details
425426
local label = ctx.label
@@ -431,10 +432,8 @@ local config = {
431432
end
432433

433434
-- characters matched on the label by the fuzzy matcher
434-
if ctx.label_matched_indices ~= nil then
435-
for _, idx in ipairs(ctx.label_matched_indices) do
436-
table.insert(highlights, { idx, idx + 1, group = 'BlinkCmpLabelMatch' })
437-
end
435+
for _, idx in ipairs(ctx.label_matched_indices) do
436+
table.insert(highlights, { idx, idx + 1, group = 'BlinkCmpLabelMatch' })
438437
end
439438

440439
return highlights
@@ -443,7 +442,7 @@ local config = {
443442

444443
label_description = {
445444
width = { max = 30 },
446-
text = function(ctx) return ctx.label_description or '' end,
445+
text = function(ctx) return ctx.label_description end,
447446
highlight = 'BlinkCmpLabelDescription',
448447
},
449448
},

lua/blink/cmp/utils.lua

+2-4
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,15 @@ function utils.get_regex_around_cursor(range, regex, exclude_from_prefix_regex)
8383
return { start_col = start_col, length = length }
8484
end
8585

86-
--- @param ctx blink.cmp.CompletionRenderContext
86+
--- @param ctx blink.cmp.DrawItemContext
8787
--- @return string|nil
8888
function utils.try_get_tailwind_hl(ctx)
8989
local doc = ctx.item.documentation
9090
if ctx.kind == 'Color' and doc then
9191
local content = type(doc) == 'string' and doc or doc.value
9292
if ctx.kind == 'Color' and content and content:match('^#%x%x%x%x%x%x$') then
9393
local hl_name = 'HexColor' .. content:sub(2)
94-
if #vim.api.nvim_get_hl(0, { name = hl_name }) == 0 then
95-
vim.api.nvim_set_hl(0, hl_name, { fg = content })
96-
end
94+
if #vim.api.nvim_get_hl(0, { name = hl_name }) == 0 then vim.api.nvim_set_hl(0, hl_name, { fg = content }) end
9795
return hl_name
9896
end
9997
end

lua/blink/cmp/windows/render/column.lua

+28-26
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
--- @field gap number
44
--- @field lines string[][]
55
--- @field width number
6-
--- @field ctxs blink.cmp.CompletionRenderContext[]
6+
--- @field ctxs blink.cmp.DrawItemContext[]
77
---
88
--- @field new fun(components: blink.cmp.DrawComponent[], gap: number): blink.cmp.DrawColumn
99
--- @field render fun(self: blink.cmp.DrawColumn, ctxs: blink.cmp.DrawItemContext[])
10-
--- @field get_line_text fun(self: blink.cmp.DrawColumn, line_idx: number): string[]
10+
--- @field get_line_text fun(self: blink.cmp.DrawColumn, line_idx: number): string
1111
--- @field get_line_highlights fun(self: blink.cmp.DrawColumn, line_idx: number): blink.cmp.DrawHighlight[]
1212

1313
local text_lib = require('blink.cmp.windows.render.text')
@@ -46,9 +46,9 @@ function column:render(ctxs)
4646
--- get the total width of the column
4747
local column_width = 0
4848
for _, max_component_width in ipairs(max_component_widths) do
49-
column_width = column_width + max_component_width
49+
if max_component_width > 0 then column_width = column_width + max_component_width + self.gap end
5050
end
51-
column_width = column_width + self.gap * (#self.components - 1)
51+
column_width = math.max(column_width - self.gap, 0)
5252

5353
--- find the component that will fill the empty space
5454
local fill_idx = -1
@@ -63,9 +63,10 @@ function column:render(ctxs)
6363
--- and add extra spaces until we reach the column width
6464
for _, line in ipairs(lines) do
6565
local line_width = 0
66-
for idx, component_text in ipairs(line) do
67-
line_width = line_width + vim.api.nvim_strwidth(component_text) + (idx == #line and 0 or self.gap)
66+
for _, component_text in ipairs(line) do
67+
if #component_text > 0 then line_width = line_width + vim.api.nvim_strwidth(component_text) + self.gap end
6868
end
69+
line_width = line_width - self.gap
6970
local remaining_width = column_width - line_width
7071
line[fill_idx] = text_lib.pad(line[fill_idx], vim.api.nvim_strwidth(line[fill_idx]) + remaining_width)
7172
end
@@ -77,12 +78,12 @@ function column:render(ctxs)
7778
end
7879

7980
function column:get_line_text(line_idx)
81+
local text = ''
8082
local line = self.lines[line_idx]
81-
local concatenated = ''
82-
for idx, component in ipairs(line) do
83-
concatenated = concatenated .. component .. string.rep(' ', idx == #line and 0 or self.gap)
83+
for _, component in ipairs(line) do
84+
if #component > 0 then text = text .. component .. string.rep(' ', self.gap) end
8485
end
85-
return concatenated
86+
return text:sub(1, -self.gap - 1)
8687
end
8788

8889
function column:get_line_highlights(line_idx)
@@ -92,24 +93,25 @@ function column:get_line_highlights(line_idx)
9293

9394
for component_idx, component in ipairs(self.components) do
9495
local text = self.lines[line_idx][component_idx]
95-
96-
local column_highlights = type(component.highlight) == 'function' and component.highlight(ctx, text)
97-
or component.highlight
98-
99-
if type(column_highlights) == 'string' then
100-
table.insert(highlights, { offset, offset + #text, group = column_highlights })
101-
elseif type(column_highlights) == 'table' then
102-
for _, highlight in ipairs(column_highlights) do
103-
table.insert(highlights, {
104-
offset + highlight[1],
105-
offset + highlight[2],
106-
group = highlight.group,
107-
params = highlight.params,
108-
})
96+
if #text > 0 then
97+
local column_highlights = type(component.highlight) == 'function' and component.highlight(ctx, text)
98+
or component.highlight
99+
100+
if type(column_highlights) == 'string' then
101+
table.insert(highlights, { offset, offset + #text, group = column_highlights })
102+
elseif type(column_highlights) == 'table' then
103+
for _, highlight in ipairs(column_highlights) do
104+
table.insert(highlights, {
105+
offset + highlight[1],
106+
offset + highlight[2],
107+
group = highlight.group,
108+
params = highlight.params,
109+
})
110+
end
109111
end
110-
end
111112

112-
offset = offset + #text + self.gap
113+
offset = offset + #text + self.gap
114+
end
113115
end
114116

115117
return highlights

lua/blink/cmp/windows/render/init.lua

+20-25
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ function renderer:draw(bufnr, items)
6464
local line = ''
6565
if self.padding[1] > 0 then line = string.rep(' ', self.padding[1]) end
6666

67-
for column_idx, column in ipairs(self.columns) do
68-
line = line .. column:get_line_text(idx)
69-
if column_idx ~= #self.columns then line = line .. string.rep(' ', self.gap) end
67+
for _, column in ipairs(self.columns) do
68+
local text = column:get_line_text(idx)
69+
if #text > 0 then line = line .. text .. string.rep(' ', self.gap) end
7070
end
71+
line = line:sub(1, -self.gap - 1)
7172

7273
if self.padding[2] > 0 then line = line .. string.rep(' ', self.padding[2]) end
7374

@@ -81,31 +82,25 @@ function renderer:draw(bufnr, items)
8182
-- like nvim-cmp does, which breaks on UIs like neovide
8283
vim.api.nvim_set_decoration_provider(ns, {
8384
on_win = function(_, _, win_bufnr) return bufnr == win_bufnr end,
84-
on_line = function(_, _, _, line_number)
85+
on_line = function(_, _, _, line)
8586
local offset = self.padding[1]
86-
local highlights = {}
87-
8887
for _, column in ipairs(self.columns) do
89-
local highlights_for_column = column:get_line_highlights(line_number + 1)
90-
for _, highlight in ipairs(highlights_for_column) do
91-
table.insert(highlights, {
92-
offset + highlight[1],
93-
offset + highlight[2],
94-
group = highlight.group,
95-
params = highlight.params,
96-
})
88+
local text = column:get_line_text(line + 1)
89+
if #text > 0 then
90+
local highlights = column:get_line_highlights(line + 1)
91+
for _, highlight in ipairs(highlights) do
92+
local col = offset + highlight[1]
93+
local end_col = offset + highlight[2]
94+
vim.api.nvim_buf_set_extmark(bufnr, ns, line, col, {
95+
end_col = end_col,
96+
hl_group = highlight.group,
97+
hl_mode = 'combine',
98+
hl_eol = true,
99+
ephemeral = true,
100+
})
101+
end
102+
offset = offset + #text + self.gap
97103
end
98-
offset = offset + #column:get_line_text(line_number + 1) + self.gap
99-
end
100-
101-
for _, highlight in ipairs(highlights) do
102-
vim.api.nvim_buf_set_extmark(bufnr, ns, line_number, highlight[1], {
103-
end_col = highlight[2],
104-
hl_group = highlight.group,
105-
hl_mode = 'combine',
106-
hl_eol = true,
107-
ephemeral = true,
108-
})
109104
end
110105
end,
111106
})

lua/blink/cmp/windows/render/types.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
--- @class blink.cmp.DrawComponent
2020
--- @field width? blink.cmp.DrawWidth
2121
--- @field ellipsis? boolean Whether to add an ellipsis when truncating the text
22-
--- @field text fun(ctx: blink.cmp.DrawItemContext): string Renders the text of the component
22+
--- @field text fun(ctx: blink.cmp.DrawItemContext): string? Renders the text of the component
2323
--- @field highlight? string | fun(ctx: blink.cmp.DrawItemContext, text: string): string | blink.cmp.DrawHighlight[] Renders the highlights of the component

0 commit comments

Comments
 (0)