Skip to content

Commit 34e4483

Browse files
xieyonnSaghen
andauthored
feat(snippets): add option sources.providers.snippets.opts.use_label_description (#2094)
* feat: add option completion.menu.draw.snippet_desc_column Show snippet desc in completion column label_detail or label_description * feat: move to `sources.providers.snippets.opts.use_label_description` * feat: mini_snippets and luasnip support --------- Co-authored-by: Liam Dyer <[email protected]>
1 parent f6ca10f commit 34e4483

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

doc/configuration/reference.md

Lines changed: 3 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.
@@ -554,6 +554,8 @@ sources.providers = {
554554
end,
555555
-- Set to '+' to use the system clipboard, or '"' to use the unnamed register
556556
clipboard_register = nil,
557+
-- Whether to put the snippet description in the label description
558+
use_label_description = false,
557559
}
558560

559561
-- For `snippets.preset == 'luasnip'`

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
--- @field get_filetype? fun(context: blink.cmp.Context): string
77
--- @field filter_snippets? fun(filetype: string, file: string): boolean
88
--- @field clipboard_register? string
9+
--- @field use_label_description? boolean Whether to put the snippet description in the label description
910

1011
local snippets = {}
1112

@@ -17,6 +18,7 @@ function snippets.new(opts)
1718
self.cache = {}
1819
self.registry = require('blink.cmp.sources.snippets.default.registry').new(opts)
1920
self.get_filetype = opts.get_filetype or function() return vim.bo.filetype end
21+
2022
return self
2123
end
2224

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local default_config = {
1919
extended_filetypes = {},
2020
--- @type string?
2121
clipboard_register = nil,
22+
use_label_description = false,
2223
}
2324

2425
--- @param config blink.cmp.SnippetsOpts
@@ -116,6 +117,8 @@ function registry:snippet_to_completion_item(snippet, cache_key)
116117
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
117118
insertText = self:expand_vars(body, cache_key),
118119
description = snippet.description,
120+
labelDetails = snippet.description and self.config.use_label_description and { description = snippet.description }
121+
or nil,
119122
}
120123
end
121124

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
--- @field use_show_condition? boolean Whether to use show_condition for filtering snippets
33
--- @field show_autosnippets? boolean Whether to show autosnippets in the completion list
44
--- @field prefer_doc_trig? boolean When expanding `regTrig` snippets, prefer `docTrig` over `trig` placeholder
5+
--- @field use_label_description? boolean Whether to put the snippet description in the label description
56

67
--- @class blink.cmp.LuasnipSource : blink.cmp.Source
78
--- @field config blink.cmp.LuasnipSourceOptions
@@ -17,6 +18,7 @@ local defaults_config = {
1718
use_show_condition = true,
1819
show_autosnippets = true,
1920
prefer_doc_trig = false,
21+
use_label_description = false,
2022
}
2123

2224
---@param snippet table
@@ -36,6 +38,7 @@ function source.new(opts)
3638
use_show_condition = { config.use_show_condition, 'boolean' },
3739
show_autosnippets = { config.show_autosnippets, 'boolean' },
3840
prefer_doc_trig = { config.prefer_doc_trig, 'boolean' },
41+
use_label_description = { config.use_label_description, 'boolean' },
3942
}, config)
4043

4144
local self = setmetatable({}, { __index = source })
@@ -111,6 +114,9 @@ function source:get_completions(ctx, callback)
111114
insertTextFormat = vim.lsp.protocol.InsertTextFormat.PlainText,
112115
sortText = sort_text,
113116
data = { snip_id = snip.id, show_condition = snip.show_condition },
117+
labelDetails = snip.dscr and self.config.use_label_description and {
118+
description = table.concat(snip.dscr, ' '),
119+
} or nil,
114120
}
115121
-- populate snippet cache for this filetype
116122
table.insert(self.items_cache[ft], item)

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
--- @class blink.cmp.MiniSnippetsSourceOptions
44
--- @field use_items_cache? boolean completion items are cached using default mini.snippets context
5+
--- @field use_label_description? boolean Whether to put the snippet description in the label description
56

67
--- @class blink.cmp.MiniSnippetsSource : blink.cmp.Source
78
--- @field config blink.cmp.MiniSnippetsSourceOptions
@@ -19,6 +20,8 @@ local source = {}
1920
local defaults_config = {
2021
--- Whether to use a cache for completion items
2122
use_items_cache = true,
23+
--- Whether to put the snippet description in the label description
24+
use_label_description = false,
2225
}
2326

2427
function source.new(opts)
@@ -29,6 +32,7 @@ function source.new(opts)
2932
'boolean',
3033
'use_items_cache must be a boolean when using mini__snippets preset',
3134
},
35+
use_label_description = { config.use_label_description, 'boolean' },
3236
}, opts)
3337

3438
local self = setmetatable({}, { __index = source })
@@ -42,7 +46,7 @@ function source:enabled()
4246
return _G.MiniSnippets ~= nil -- ensure that user has explicitly setup mini.snippets
4347
end
4448

45-
local function to_completion_items(snippets)
49+
local function to_completion_items(snippets, use_label_description)
4650
local result = {}
4751

4852
for _, snip in ipairs(snippets) do
@@ -53,6 +57,7 @@ local function to_completion_items(snippets)
5357
insertText = snip.prefix,
5458
insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet,
5559
data = { snip = snip },
60+
labelDetails = snip.desc and use_label_description and { description = snip.desc } or nil,
5661
}
5762
table.insert(result, item)
5863
end
@@ -67,8 +72,10 @@ end
6772
-- See :h MiniSnippets.default_prepare
6873
--
6974
-- Return completion items produced from snippets either directly or from cache
70-
local function get_completion_items(cache)
71-
if not cache then return to_completion_items(MiniSnippets.expand({ match = false, insert = false })) end
75+
local function get_completion_items(cache, use_label_description)
76+
if not cache then
77+
return to_completion_items(MiniSnippets.expand({ match = false, insert = false }), use_label_description)
78+
end
7279

7380
-- Compute cache id
7481
local _, context = MiniSnippets.default_prepare({})
@@ -80,7 +87,7 @@ local function get_completion_items(cache)
8087
-- Retrieve all raw snippets in context and transform into completion items
8188
local snippets = MiniSnippets.expand({ match = false, insert = false })
8289
--- @cast snippets table
83-
local items = to_completion_items(vim.deepcopy(snippets))
90+
local items = to_completion_items(vim.deepcopy(snippets), use_label_description)
8491
cache[id] = items
8592

8693
return items
@@ -90,7 +97,7 @@ function source:get_completions(ctx, callback)
9097
local cache = self.config.use_items_cache and self.items_cache or nil
9198

9299
--- @type blink.cmp.CompletionItem[]
93-
local items = get_completion_items(cache)
100+
local items = get_completion_items(cache, self.config.use_label_description)
94101
callback({
95102
is_incomplete_forward = false,
96103
is_incomplete_backward = false,

0 commit comments

Comments
 (0)