Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/configuration/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'`
Expand Down
2 changes: 2 additions & 0 deletions lua/blink/cmp/sources/snippets/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lua/blink/cmp/sources/snippets/default/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local default_config = {
extended_filetypes = {},
--- @type string?
clipboard_register = nil,
use_label_description = false,
}

--- @param config blink.cmp.SnippetsOpts
Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lua/blink/cmp/sources/snippets/luasnip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 })
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions lua/blink/cmp/sources/snippets/mini_snippets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 })
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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({})
Expand All @@ -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
Expand All @@ -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,
Expand Down
Loading