diff --git a/doc/configuration/reference.md b/doc/configuration/reference.md index 2deccb86..7636fb26 100644 --- a/doc/configuration/reference.md +++ b/doc/configuration/reference.md @@ -74,7 +74,7 @@ completion.trigger = { -- When true, will show the completion window after entering insert mode show_on_insert = false, - + -- LSPs can indicate when to show the completion window via trigger characters -- however, some LSPs (i.e. tsserver) return characters that would essentially -- always show the window. We block these by default. @@ -554,6 +554,8 @@ sources.providers = { end, -- Set to '+' to use the system clipboard, or '"' to use the unnamed register clipboard_register = nil, + -- Whether to put the snippet description in the label description + use_label_description = false, } -- For `snippets.preset == 'luasnip'` diff --git a/lua/blink/cmp/sources/snippets/default/init.lua b/lua/blink/cmp/sources/snippets/default/init.lua index e04d1cfa..13dcfe4b 100644 --- a/lua/blink/cmp/sources/snippets/default/init.lua +++ b/lua/blink/cmp/sources/snippets/default/init.lua @@ -6,6 +6,7 @@ --- @field get_filetype? fun(context: blink.cmp.Context): string --- @field filter_snippets? fun(filetype: string, file: string): boolean --- @field clipboard_register? string +--- @field use_label_description? boolean Whether to put the snippet description in the label description local snippets = {} @@ -17,6 +18,7 @@ function snippets.new(opts) self.cache = {} self.registry = require('blink.cmp.sources.snippets.default.registry').new(opts) self.get_filetype = opts.get_filetype or function() return vim.bo.filetype end + return self end diff --git a/lua/blink/cmp/sources/snippets/default/registry.lua b/lua/blink/cmp/sources/snippets/default/registry.lua index 0f774bbe..e5069291 100644 --- a/lua/blink/cmp/sources/snippets/default/registry.lua +++ b/lua/blink/cmp/sources/snippets/default/registry.lua @@ -19,6 +19,7 @@ local default_config = { extended_filetypes = {}, --- @type string? clipboard_register = nil, + use_label_description = false, } --- @param config blink.cmp.SnippetsOpts @@ -116,6 +117,8 @@ function registry:snippet_to_completion_item(snippet, cache_key) insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet, insertText = self:expand_vars(body, cache_key), description = snippet.description, + labelDetails = snippet.description and self.config.use_label_description and { description = snippet.description } + or nil, } end diff --git a/lua/blink/cmp/sources/snippets/luasnip.lua b/lua/blink/cmp/sources/snippets/luasnip.lua index 4d89f4fb..036a9357 100644 --- a/lua/blink/cmp/sources/snippets/luasnip.lua +++ b/lua/blink/cmp/sources/snippets/luasnip.lua @@ -2,6 +2,7 @@ --- @field use_show_condition? boolean Whether to use show_condition for filtering snippets --- @field show_autosnippets? boolean Whether to show autosnippets in the completion list --- @field prefer_doc_trig? boolean When expanding `regTrig` snippets, prefer `docTrig` over `trig` placeholder +--- @field use_label_description? boolean Whether to put the snippet description in the label description --- @class blink.cmp.LuasnipSource : blink.cmp.Source --- @field config blink.cmp.LuasnipSourceOptions @@ -17,6 +18,7 @@ local defaults_config = { use_show_condition = true, show_autosnippets = true, prefer_doc_trig = false, + use_label_description = false, } ---@param snippet table @@ -36,6 +38,7 @@ function source.new(opts) use_show_condition = { config.use_show_condition, 'boolean' }, show_autosnippets = { config.show_autosnippets, 'boolean' }, prefer_doc_trig = { config.prefer_doc_trig, 'boolean' }, + use_label_description = { config.use_label_description, 'boolean' }, }, config) local self = setmetatable({}, { __index = source }) @@ -111,6 +114,9 @@ function source:get_completions(ctx, callback) insertTextFormat = vim.lsp.protocol.InsertTextFormat.PlainText, sortText = sort_text, data = { snip_id = snip.id, show_condition = snip.show_condition }, + labelDetails = snip.dscr and self.config.use_label_description and { + description = table.concat(snip.dscr, ' '), + } or nil, } -- populate snippet cache for this filetype table.insert(self.items_cache[ft], item) diff --git a/lua/blink/cmp/sources/snippets/mini_snippets.lua b/lua/blink/cmp/sources/snippets/mini_snippets.lua index edb2aff6..3ed3044b 100644 --- a/lua/blink/cmp/sources/snippets/mini_snippets.lua +++ b/lua/blink/cmp/sources/snippets/mini_snippets.lua @@ -2,6 +2,7 @@ --- @class blink.cmp.MiniSnippetsSourceOptions --- @field use_items_cache? boolean completion items are cached using default mini.snippets context +--- @field use_label_description? boolean Whether to put the snippet description in the label description --- @class blink.cmp.MiniSnippetsSource : blink.cmp.Source --- @field config blink.cmp.MiniSnippetsSourceOptions @@ -19,6 +20,8 @@ local source = {} local defaults_config = { --- Whether to use a cache for completion items use_items_cache = true, + --- Whether to put the snippet description in the label description + use_label_description = false, } function source.new(opts) @@ -29,6 +32,7 @@ function source.new(opts) 'boolean', 'use_items_cache must be a boolean when using mini__snippets preset', }, + use_label_description = { config.use_label_description, 'boolean' }, }, opts) local self = setmetatable({}, { __index = source }) @@ -42,7 +46,7 @@ function source:enabled() return _G.MiniSnippets ~= nil -- ensure that user has explicitly setup mini.snippets end -local function to_completion_items(snippets) +local function to_completion_items(snippets, use_label_description) local result = {} for _, snip in ipairs(snippets) do @@ -53,6 +57,7 @@ local function to_completion_items(snippets) insertText = snip.prefix, insertTextFormat = vim.lsp.protocol.InsertTextFormat.Snippet, data = { snip = snip }, + labelDetails = snip.desc and use_label_description and { description = snip.desc } or nil, } table.insert(result, item) end @@ -67,8 +72,10 @@ end -- See :h MiniSnippets.default_prepare -- -- Return completion items produced from snippets either directly or from cache -local function get_completion_items(cache) - if not cache then return to_completion_items(MiniSnippets.expand({ match = false, insert = false })) end +local function get_completion_items(cache, use_label_description) + if not cache then + return to_completion_items(MiniSnippets.expand({ match = false, insert = false }), use_label_description) + end -- Compute cache id local _, context = MiniSnippets.default_prepare({}) @@ -80,7 +87,7 @@ local function get_completion_items(cache) -- Retrieve all raw snippets in context and transform into completion items local snippets = MiniSnippets.expand({ match = false, insert = false }) --- @cast snippets table - local items = to_completion_items(vim.deepcopy(snippets)) + local items = to_completion_items(vim.deepcopy(snippets), use_label_description) cache[id] = items return items @@ -90,7 +97,7 @@ function source:get_completions(ctx, callback) local cache = self.config.use_items_cache and self.items_cache or nil --- @type blink.cmp.CompletionItem[] - local items = get_completion_items(cache) + local items = get_completion_items(cache, self.config.use_label_description) callback({ is_incomplete_forward = false, is_incomplete_backward = false,