From b77413ff8f59f380612074f0c9bd49093d8db695 Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Sun, 19 Jan 2025 13:52:52 +0100 Subject: Squashed 'mut/neovim/pack/plugins/start/blink.cmp/' content from commit 1cc3b1a git-subtree-dir: mut/neovim/pack/plugins/start/blink.cmp git-subtree-split: 1cc3b1a908fbcfd15451c4772759549724f38524 --- lua/blink/cmp/completion/brackets/utils.lua | 61 +++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lua/blink/cmp/completion/brackets/utils.lua (limited to 'lua/blink/cmp/completion/brackets/utils.lua') diff --git a/lua/blink/cmp/completion/brackets/utils.lua b/lua/blink/cmp/completion/brackets/utils.lua new file mode 100644 index 0000000..cf84f11 --- /dev/null +++ b/lua/blink/cmp/completion/brackets/utils.lua @@ -0,0 +1,61 @@ +local config = require('blink.cmp.config').completion.accept.auto_brackets +local CompletionItemKind = require('blink.cmp.types').CompletionItemKind +local brackets = require('blink.cmp.completion.brackets.config') +local utils = {} + +--- @param snippet string +function utils.snippets_extract_placeholders(snippet) + local placeholders = {} + local pattern = [=[(\$\{(\d+)(:([^}\\]|\\.)*?)?\})]=] + + for _, number, _, _ in snippet:gmatch(pattern) do + table.insert(placeholders, tonumber(number)) + end + + return placeholders +end + +--- @param filetype string +--- @param item blink.cmp.CompletionItem +--- @return string[] +function utils.get_for_filetype(filetype, item) + local default = config.default_brackets + local per_filetype = config.override_brackets_for_filetypes[filetype] or brackets.per_filetype[filetype] + + if type(per_filetype) == 'function' then return per_filetype(item) or default end + return per_filetype or default +end + +--- @param filetype string +--- @param resolution_method 'kind' | 'semantic_token' +--- @return boolean +function utils.should_run_resolution(filetype, resolution_method) + -- resolution method specific + if not config[resolution_method .. '_resolution'].enabled then return false end + local resolution_blocked_filetypes = config[resolution_method .. '_resolution'].blocked_filetypes + if vim.tbl_contains(resolution_blocked_filetypes, filetype) then return false end + + -- global + if not config.enabled then return false end + if vim.tbl_contains(config.force_allow_filetypes, filetype) then return true end + return not vim.tbl_contains(config.blocked_filetypes, filetype) + and not vim.tbl_contains(brackets.blocked_filetypes, filetype) +end + +--- @param text_edit lsp.TextEdit | lsp.InsertReplaceEdit +--- @param bracket string +--- @return boolean +function utils.has_brackets_in_front(text_edit, bracket) + local line = vim.api.nvim_get_current_line() + local col = text_edit.range['end'].character + 1 + return line:sub(col, col) == bracket +end + +--- @param item blink.cmp.CompletionItem +--- @param _ string[] +-- TODO: for edge cases, we should probably also take brackets themselves into consideration +function utils.can_have_brackets(item, _) + return item.kind == CompletionItemKind.Function or item.kind == CompletionItemKind.Method +end + +return utils -- cgit v1.2.3