Skip to content

Commit

Permalink
feat: auto bracket exceptions
Browse files Browse the repository at this point in the history
Closes #1008
  • Loading branch information
Saghen committed Jan 22, 2025
1 parent 6c87840 commit e121ec2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lua/blink/cmp/completion/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ local function accept(ctx, item, callback)
if brackets_status == 'check_semantic_token' then
-- TODO: since we apply the additional text edits after, auto imported functions will not
-- get auto brackets. If we apply them before, we have to modify the textEdit to compensate
brackets_lib.add_brackets_via_semantic_token(vim.bo.filetype, item, function()
brackets_lib.add_brackets_via_semantic_token(ctx, vim.bo.filetype, item, function()
require('blink.cmp.completion.trigger').show_if_on_trigger_character({ is_accept = true })
require('blink.cmp.signature.trigger').show_if_on_trigger_character()
callback()
Expand Down
8 changes: 8 additions & 0 deletions lua/blink/cmp/completion/brackets/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ return {
tex = { '{', '}' },
plaintex = { '{', '}' },
},
exceptions = {
by_filetype = {
-- ignore `use` imports
rust = function(ctx) return ctx.line:find('^%s*use%s') == nil end,
-- ignore import statements
python = function(ctx) return ctx.line:find('^%s*import%s') == nil and ctx.line:find('^%s*from%s') == nil end,
},
},
}
4 changes: 2 additions & 2 deletions lua/blink/cmp/completion/brackets/kind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ local function add_brackets(ctx, filetype, item)
return 'skipped', text_edit, 0
end

-- check if configuration incidates we should skip
if not utils.should_run_resolution(filetype, 'kind') then return 'check_semantic_token', text_edit, 0 end
-- check if configuration indicates we should skip
if not utils.should_run_resolution(ctx, filetype, 'kind') then return 'check_semantic_token', text_edit, 0 end
-- cannot have brackets, skip
if not utils.can_have_brackets(item, brackets_for_filetype) then return 'check_semantic_token', text_edit, 0 end

Expand Down
5 changes: 3 additions & 2 deletions lua/blink/cmp/completion/brackets/semantic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ local utils = require('blink.cmp.completion.brackets.utils')
local semantic = {}

--- Asynchronously use semantic tokens to determine if brackets should be added
--- @param ctx blink.cmp.Context
--- @param filetype string
--- @param item blink.cmp.CompletionItem
--- @param callback fun()
function semantic.add_brackets_via_semantic_token(filetype, item, callback)
if not utils.should_run_resolution(filetype, 'semantic_token') then return callback() end
function semantic.add_brackets_via_semantic_token(ctx, filetype, item, callback)
if not utils.should_run_resolution(ctx, filetype, 'semantic_token') then return callback() end

local text_edit = item.textEdit
assert(text_edit ~= nil, 'Got nil text edit while adding brackets via semantic tokens')
Expand Down
10 changes: 9 additions & 1 deletion lua/blink/cmp/completion/brackets/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,25 @@ function utils.get_for_filetype(filetype, item)
return per_filetype or default
end

--- @param ctx blink.cmp.Context
--- @param filetype string
--- @param resolution_method 'kind' | 'semantic_token'
--- @return boolean
function utils.should_run_resolution(filetype, resolution_method)
function utils.should_run_resolution(ctx, 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

-- filetype specific exceptions
local exceptions = require('blink.cmp.completion.brackets.config').exceptions
if exceptions.by_filetype[filetype] ~= nil then
if not exceptions.by_filetype[filetype](ctx) then return false end
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)
Expand Down

0 comments on commit e121ec2

Please sign in to comment.