Skip to content

Commit c39227a

Browse files
fix: handle newlines in autocomplete suggestions (#110)
1 parent 7f2f74f commit c39227a

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lua/blink/cmp/windows/autocomplete.lua

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
--- @class blink.cmp.CompletionRenderContext
22
--- @field item blink.cmp.CompletionItem
3+
--- @field label string
34
--- @field kind string
45
--- @field kind_icon string
56
--- @field icon_gap string
@@ -250,11 +251,15 @@ function autocomplete.draw()
250251
for _, item in ipairs(autocomplete.items) do
251252
local kind = require('blink.cmp.types').CompletionItemKind[item.kind] or 'Unknown'
252253
local kind_icon = config.kind_icons[kind] or config.kind_icons.Field
254+
-- Some LSPs can return labels with newlines.
255+
-- Escape them to avoid errors in nvim_buf_set_lines when rendering the autocomplete menu.
256+
local label = item.label:gsub('\n', '\\n')
253257

254258
table.insert(
255259
components_list,
256260
draw_fn({
257261
item = item,
262+
label = label,
258263
kind = kind,
259264
kind_icon = kind_icon,
260265
icon_gap = icon_gap,
@@ -295,7 +300,7 @@ function autocomplete.render_item_simple(ctx)
295300
' ',
296301
{ ctx.kind_icon, ctx.icon_gap, hl_group = 'BlinkCmpKind' .. ctx.kind },
297302
{
298-
ctx.item.label,
303+
ctx.label,
299304
ctx.kind == 'Snippet' and '~' or nil,
300305
fill = true,
301306
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
@@ -311,7 +316,7 @@ function autocomplete.render_item_reversed(ctx)
311316
return {
312317
' ',
313318
{
314-
ctx.item.label,
319+
ctx.label,
315320
ctx.kind == 'Snippet' and '~' or nil,
316321
fill = true,
317322
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',
@@ -329,7 +334,7 @@ function autocomplete.render_item_minimal(ctx)
329334
return {
330335
' ',
331336
{
332-
ctx.item.label,
337+
ctx.label,
333338
ctx.kind == 'Snippet' and '~' or nil,
334339
fill = true,
335340
hl_group = ctx.deprecated and 'BlinkCmpLabelDeprecated' or 'BlinkCmpLabel',

0 commit comments

Comments
 (0)