-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merge snippets to single source
- Loading branch information
Showing
16 changed files
with
120 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- @class blink.cmp.SnippetsOpts | ||
--- @field friendly_snippets? boolean | ||
--- @field search_paths? string[] | ||
--- @field global_snippets? string[] | ||
--- @field extended_filetypes? table<string, string[]> | ||
--- @field ignored_filetypes? string[] | ||
--- @field get_filetype? fun(context: blink.cmp.Context): string | ||
--- @field clipboard_register? string | ||
|
||
local snippets = {} | ||
|
||
function snippets.new(opts) | ||
--- @cast opts blink.cmp.SnippetsOpts | ||
|
||
local self = setmetatable({}, { __index = snippets }) | ||
--- @type table<string, blink.cmp.CompletionItem[]> | ||
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 | ||
|
||
function snippets:get_completions(context, callback) | ||
local filetype = self.get_filetype(context) | ||
if vim.tbl_contains(self.registry.config.ignored_filetypes, filetype) then return callback() end | ||
|
||
if not self.cache[filetype] then | ||
local global_snippets = self.registry:get_global_snippets() | ||
local extended_snippets = self.registry:get_extended_snippets(filetype) | ||
local ft_snippets = self.registry:get_snippets_for_ft(filetype) | ||
local snips = vim.list_extend({}, global_snippets) | ||
vim.list_extend(snips, extended_snippets) | ||
vim.list_extend(snips, ft_snippets) | ||
|
||
self.cache[filetype] = snips | ||
end | ||
|
||
local items = vim.tbl_map( | ||
function(item) return self.registry:snippet_to_completion_item(item) end, | ||
self.cache[filetype] | ||
) | ||
callback({ | ||
is_incomplete_forward = false, | ||
is_incomplete_backward = false, | ||
items = items, | ||
}) | ||
end | ||
|
||
function snippets:resolve(item, callback) | ||
local parsed_snippet = require('blink.cmp.sources.snippets.utils').safe_parse(item.insertText) | ||
local snippet = parsed_snippet and tostring(parsed_snippet) or item.insertText | ||
|
||
local resolved_item = vim.deepcopy(item) | ||
resolved_item.detail = snippet | ||
resolved_item.documentation = { | ||
kind = 'markdown', | ||
value = item.description, | ||
} | ||
callback(resolved_item) | ||
end | ||
|
||
--- For external integrations to force reloading the snippets | ||
function snippets:reload() self.cache = {} end | ||
|
||
return snippets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,9 @@ | ||
--- @class blink.cmp.SnippetsOpts | ||
--- @field friendly_snippets? boolean | ||
--- @field search_paths? string[] | ||
--- @field global_snippets? string[] | ||
--- @field extended_filetypes? table<string, string[]> | ||
--- @field ignored_filetypes? string[] | ||
--- @field get_filetype? fun(context: blink.cmp.Context): string | ||
--- @field clipboard_register? string | ||
local source = {} | ||
|
||
local snippets = {} | ||
|
||
function snippets.new(opts) | ||
--- @type blink.cmp.SnippetsOpts | ||
opts = opts or {} | ||
local self = setmetatable({}, { __index = snippets }) | ||
--- @type table<string, blink.cmp.CompletionItem[]> | ||
self.cache = {} | ||
self.registry = require('blink.cmp.sources.snippets.registry').new(opts) | ||
self.get_filetype = opts.get_filetype or function() return vim.bo.filetype end | ||
return self | ||
end | ||
|
||
function snippets:get_completions(context, callback) | ||
local filetype = self.get_filetype(context) | ||
if vim.tbl_contains(self.registry.config.ignored_filetypes, filetype) then return callback() end | ||
|
||
if not self.cache[filetype] then | ||
local global_snippets = self.registry:get_global_snippets() | ||
local extended_snippets = self.registry:get_extended_snippets(filetype) | ||
local ft_snippets = self.registry:get_snippets_for_ft(filetype) | ||
local snips = vim.list_extend({}, global_snippets) | ||
vim.list_extend(snips, extended_snippets) | ||
vim.list_extend(snips, ft_snippets) | ||
|
||
self.cache[filetype] = snips | ||
end | ||
|
||
local items = vim.tbl_map( | ||
function(item) return self.registry:snippet_to_completion_item(item) end, | ||
self.cache[filetype] | ||
) | ||
callback({ | ||
is_incomplete_forward = false, | ||
is_incomplete_backward = false, | ||
items = items, | ||
}) | ||
function source.new(opts) | ||
local preset = opts.preset or require('blink.cmp.config').snippets.preset | ||
local module = 'blink.cmp.sources.snippets.' .. preset | ||
return require(module).new(opts) | ||
end | ||
|
||
function snippets:resolve(item, callback) | ||
local parsed_snippet = require('blink.cmp.sources.snippets.utils').safe_parse(item.insertText) | ||
local snippet = parsed_snippet and tostring(parsed_snippet) or item.insertText | ||
|
||
local resolved_item = vim.deepcopy(item) | ||
resolved_item.detail = snippet | ||
resolved_item.documentation = { | ||
kind = 'markdown', | ||
value = item.description, | ||
} | ||
callback(resolved_item) | ||
end | ||
|
||
--- For external integrations to force reloading the snippets | ||
function snippets:reload() self.cache = {} end | ||
|
||
return snippets | ||
return source |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters