Skip to content

Commit ee33572

Browse files
committed
feat: add option completion.menu.draw.snippet_desc_column
Show snippet desc in completion column label_detail or label_description
1 parent 4e9edba commit ee33572

File tree

7 files changed

+39
-1
lines changed

7 files changed

+39
-1
lines changed

doc/configuration/reference.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ completion.trigger = {
7474

7575
-- When true, will show the completion window after entering insert mode
7676
show_on_insert = false,
77-
77+
7878
-- LSPs can indicate when to show the completion window via trigger characters
7979
-- however, some LSPs (i.e. tsserver) return characters that would essentially
8080
-- always show the window. We block these by default.
@@ -219,6 +219,12 @@ completion.menu.draw = {
219219
cursorline_priority = 10000,
220220
-- Appends an indicator to snippets label
221221
snippet_indicator = '~',
222+
-- Display snippet description in completion menu column.
223+
-- `nil` Do NOT show description in completion menu.
224+
-- `label_detail` Show in column label_detail.
225+
-- `label_description` Show in column label_description.
226+
-- menu column see config `completion.menu.draw.columns`
227+
snippet_desc_column = nil,
222228
-- Use treesitter to highlight the label text for the given list of sources
223229
treesitter = {},
224230
-- treesitter = { 'lsp' }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
--- @field gap? number Gap between columns
55
--- @field cursorline_priority? number Priority of the background highlight for the cursorline, defaults to 10000. Setting this to 0 will render it below other highlights
66
--- @field snippet_indicator? string Appends an indicator to snippets label, `'~'` by default
7+
--- @field snippet_desc_column? string Display snippet description in completion menu column.
78
--- @field treesitter? string[] Use treesitter to highlight the label text of completions from these sources
89
--- @field columns? blink.cmp.DrawColumnDefinition[] | fun(context: blink.cmp.Context): blink.cmp.DrawColumnDefinition[] Components to render, grouped by column
910
--- @field components? table<string, blink.cmp.DrawComponent> Component definitions

lua/blink/cmp/config/completion/menu.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ local window = {
6464
cursorline_priority = 10000,
6565
-- Appends an indicator to snippets label, `'~'` by default
6666
snippet_indicator = '~',
67+
-- Display snippet description in completion menu column.
68+
-- `nil` Do NOT show description in completion menu.
69+
-- `label_detail` Show in menu column label_detail.
70+
-- `label_description` Show in menu column label_description.
71+
snippet_desc_column = nil,
6772
-- Use treesitter to highlight the label text of completions from these sources
6873
treesitter = {},
6974
-- Components to render, grouped by column
@@ -197,6 +202,7 @@ function window.validate(config)
197202
gap = { config.draw.gap, 'number' },
198203
cursorline_priority = { config.draw.cursorline_priority, 'number' },
199204
snippet_indicator = { config.draw.snippet_indicator, 'string' },
205+
snippet_desc_column = { config.draw.snippet_desc_column, 'string', true },
200206

201207
treesitter = { config.draw.treesitter, 'table' },
202208

lua/blink/cmp/sources/snippets/default/registry.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ function registry:snippet_to_completion_item(snippet, cache_key)
116116
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
117117
insertText = self:expand_vars(body, cache_key),
118118
description = snippet.description,
119+
labelDetails = require('blink.cmp.sources.snippets.utils').build_label_details(snippet.description or ''),
119120
}
120121
end
121122

lua/blink/cmp/sources/snippets/luasnip.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ function source:get_completions(ctx, callback)
111111
insertTextFormat = vim.lsp.protocol.InsertTextFormat.PlainText,
112112
sortText = sort_text,
113113
data = { snip_id = snip.id, show_condition = snip.show_condition },
114+
labelDetails = require('blink.cmp.sources.snippets.utils').build_label_details(
115+
snip.dscr and table.concat(snip.dscr, ' ') or ''
116+
),
114117
}
115118
-- populate snippet cache for this filetype
116119
table.insert(self.items_cache[ft], item)

lua/blink/cmp/sources/snippets/mini_snippets.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ local function to_completion_items(snippets)
5353
insertText = snip.prefix,
5454
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
5555
data = { snip = snip },
56+
labelDetails = require('blink.cmp.sources.snippets.utils').build_label_details(snip.desc),
5657
}
5758
table.insert(result, item)
5859
end

lua/blink/cmp/sources/snippets/utils.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ function utils.get_tab_stops(snippet)
111111
return tabstops
112112
end
113113

114+
--- Build field `labelDetails` for snippet source.
115+
---
116+
--- @param desc string
117+
--- @return nil | lsp.CompletionItemLabelDetails
118+
function utils.build_label_details(desc)
119+
local column = require('blink.cmp.config').completion.menu.draw.snippet_desc_column
120+
121+
if column == nil then return nil end
122+
123+
if column == 'label_detail' then return {
124+
detail = desc,
125+
} end
126+
127+
if column == 'label_description' then return {
128+
description = desc,
129+
} end
130+
131+
return nil
132+
end
133+
114134
return utils

0 commit comments

Comments
 (0)