Skip to content

Commit 1628800

Browse files
authored
fix: completion label details containing newline characters (#265)
* Fix completion details containing newline characters * Compute santized detail inline * Add `detail` to `CompletionRenderContext`
1 parent 77f037c commit 1628800

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lua/blink/cmp/windows/autocomplete.lua

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
--- @class blink.cmp.CompletionRenderContext
22
--- @field item blink.cmp.CompletionItem
33
--- @field label string
4+
--- @field detail string
45
--- @field kind string
56
--- @field kind_icon string
67
--- @field icon_gap string
@@ -324,16 +325,19 @@ function autocomplete.draw()
324325
for _, item in ipairs(autocomplete.items) do
325326
local kind = require('blink.cmp.types').CompletionItemKind[item.kind] or 'Unknown'
326327
local kind_icon = config.kind_icons[kind] or config.kind_icons.Field
327-
-- Some LSPs can return labels with newlines.
328+
-- Some LSPs can return labels and details with newlines.
328329
-- Escape them to avoid errors in nvim_buf_set_lines when rendering the autocomplete menu.
329330
local label = item.label:gsub('\n', '\\n')
331+
local detail = (item.labelDetails and item.labelDetails.detail) and
332+
item.labelDetails.detail:gsub('\n', '\\n') or ''
330333
if config.nerd_font_variant == 'normal' then label = label:gsub('', '') end
331334

332335
table.insert(
333336
components_list,
334337
draw_fn({
335338
item = item,
336339
label = label,
340+
detail = detail,
337341
kind = kind,
338342
kind_icon = kind_icon,
339343
icon_gap = icon_gap,
@@ -380,7 +384,7 @@ function autocomplete.draw_item_simple(ctx)
380384
{
381385
ctx.label,
382386
ctx.kind == 'Snippet' and '~' or '',
383-
(ctx.item.labelDetails and ctx.item.labelDetails.detail) and ctx.item.labelDetails.detail or '',
387+
ctx.detail,
384388
fill = true,
385389
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
386390
max_width = 80,
@@ -397,7 +401,7 @@ function autocomplete.draw_item_reversed(ctx)
397401
{
398402
ctx.label,
399403
ctx.kind == 'Snippet' and '~' or nil,
400-
(ctx.item.labelDetails and ctx.item.labelDetails.detail) and ctx.item.labelDetails.detail or '',
404+
ctx.detail,
401405
fill = true,
402406
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
403407
max_width = 50,
@@ -421,7 +425,7 @@ function autocomplete.draw_item_minimal(ctx)
421425
{
422426
ctx.label,
423427
ctx.kind == 'Snippet' and '~' or nil,
424-
(ctx.item.labelDetails and ctx.item.labelDetails.detail) and ctx.item.labelDetails.detail or '',
428+
ctx.detail,
425429
fill = true,
426430
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
427431
max_width = 50,

0 commit comments

Comments
 (0)