Skip to content

Commit

Permalink
fix(accept/brackets): respect item.kind when moving cursor (#779)
Browse files Browse the repository at this point in the history
* chore(gitignore): add `.lazy.lua`

* fix(accept/brackets): respect `item.kind` when moving cursor

* refactor: respect `item.kind` when moving cursor

---------

Co-authored-by: Liam Dyer <[email protected]>
  • Loading branch information
ruslanSorokin and Saghen authored Dec 29, 2024
1 parent a1476d3 commit c54dfbf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
target/
.archive.lua
_*.lua
.lazy.lua
dual/
result
.direnv
Expand Down
10 changes: 4 additions & 6 deletions lua/blink/cmp/completion/brackets/kind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ local function add_brackets(ctx, filetype, item)
-- if there's already the correct brackets in front, skip but indicate the cursor should move in front of the bracket
-- TODO: what if the brackets_for_filetype[1] == '' or ' ' (haskell/ocaml)?
if utils.has_brackets_in_front(text_edit, brackets_for_filetype[1]) then
return 'skipped', text_edit, #brackets_for_filetype[1]
local offset = utils.can_have_brackets(item, brackets_for_filetype) and #brackets_for_filetype[1] or 0
return 'skipped', text_edit, offset
end

-- if the item already contains the brackets, conservatively skip adding brackets
Expand All @@ -27,11 +28,8 @@ local function add_brackets(ctx, filetype, item)

-- check if configuration incidates we should skip
if not utils.should_run_resolution(filetype, 'kind') then return 'check_semantic_token', text_edit, 0 end
-- not a function, skip
local CompletionItemKind = require('blink.cmp.types').CompletionItemKind
if item.kind ~= CompletionItemKind.Function and item.kind ~= CompletionItemKind.Method 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

text_edit = vim.deepcopy(text_edit)
-- For snippets, we add the cursor position between the brackets as the last placeholder
Expand Down
8 changes: 8 additions & 0 deletions lua/blink/cmp/completion/brackets/utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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 = {}

Expand Down Expand Up @@ -50,4 +51,11 @@ function utils.has_brackets_in_front(text_edit, bracket)
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

0 comments on commit c54dfbf

Please sign in to comment.