Skip to content

Commit

Permalink
fix: accept auto brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Oct 4, 2024
1 parent 13304d4 commit 3927e23
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
14 changes: 8 additions & 6 deletions lua/blink/cmp/accept/brackets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function brackets.add_brackets(filetype, item)

-- if already contains the brackets, conservatively skip adding brackets
-- todo: won't work for snippets when the brackets_for_filetype is { '{', '}' }
if brackets_for_filetype[1] ~= ' ' and text_edit.newText:match('\\' .. brackets_for_filetype[1]) then
if brackets_for_filetype[1] ~= ' ' and text_edit.newText:match('[\\' .. brackets_for_filetype[1] .. ']') ~= nil then
return 'skipped', text_edit, 0
end

Expand All @@ -64,12 +64,16 @@ function brackets.add_brackets(filetype, item)
if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
local placeholders = brackets.snippets_extract_placeholders(text_edit.newText)
local last_placeholder_index = math.max(0, unpack(placeholders))
text_edit.newText = text_edit.newText .. brackets[1] .. '$' .. tostring(last_placeholder_index + 1) .. brackets[2]
text_edit.newText = text_edit.newText
.. brackets_for_filetype[1]
.. '$'
.. tostring(last_placeholder_index + 1)
.. brackets_for_filetype[2]
-- Otherwise, we add as usual
else
text_edit.newText = text_edit.newText .. brackets[1] .. brackets[2]
text_edit.newText = text_edit.newText .. brackets_for_filetype[1] .. brackets_for_filetype[2]
end
return 'added', text_edit, -#brackets[2]
return 'added', text_edit, -#brackets_for_filetype[2]
end

--- @param snippet string
Expand All @@ -89,7 +93,6 @@ end
--- @param item blink.cmp.CompletionItem
--- @param callback fun()
function brackets.add_brackets_via_semantic_token(filetype, item, callback)
vim.print('yo', brackets.should_run_resolution(filetype, 'semantic_token'))
if not brackets.should_run_resolution(filetype, 'semantic_token') then return callback() end

local text_edit = item.textEdit
Expand Down Expand Up @@ -164,7 +167,6 @@ function brackets.should_run_resolution(filetype, resolution_method)
if vim.tbl_contains(resolution_blocked_filetypes, filetype) then return false end

-- global
vim.print(config)
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)
Expand Down
30 changes: 14 additions & 16 deletions lua/blink/cmp/accept/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,39 @@ local brackets_lib = require('blink.cmp.accept.brackets')
local function accept(item)
item = vim.deepcopy(item)

local text_edit = item.textEdit
if text_edit ~= nil then
-- Adjust the position of the text edit to be the current cursor position
-- since the data might be outdated. We compare the cursor column position
-- from when the items were fetched versus the current.
-- hack: figure out a better way
-- Adjust the position of the text edit to be the current cursor position
-- since the data might be outdated. We compare the cursor column position
-- from when the items were fetched versus the current.
-- hack: figure out a better way
if item.textEdit ~= nil then
local offset = vim.api.nvim_win_get_cursor(0)[2] - item.cursor_column
text_edit.range['end'].character = text_edit.range['end'].character + offset
item.textEdit.range['end'].character = item.textEdit.range['end'].character + offset
-- No text edit so we fallback to our own resolution
else
-- No text edit so we fallback to our own resolution
text_edit = text_edits_lib.guess_text_edit(vim.api.nvim_get_current_buf(), item)
item.textEdit = text_edits_lib.guess_text_edit(vim.api.nvim_get_current_buf(), item)
end
item.textEdit = text_edit

-- Add brackets to the text edit if needed
local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item)
text_edit = text_edit_with_brackets
item.textEdit = text_edit_with_brackets

-- Snippet
if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then
-- We want to handle offset_encoding and the text edit api can do this for us
-- so we empty the newText and apply
local temp_text_edit = vim.deepcopy(text_edit)
local temp_text_edit = vim.deepcopy(item.textEdit)
temp_text_edit.newText = ''
text_edits_lib.apply_text_edits(item.client_id, { temp_text_edit })

-- Expand the snippet
vim.snippet.expand(text_edit.newText)
vim.snippet.expand(item.textEdit.newText)

-- OR Normal: Apply the text edit and move the cursor
else
text_edits_lib.apply_text_edits(item.client_id, { text_edit })
text_edits_lib.apply_text_edits(item.client_id, { item.textEdit })
vim.api.nvim_win_set_cursor(0, {
text_edit.range.start.line + 1,
text_edit.range.start.character + #text_edit.newText + offset,
item.textEdit.range.start.line + 1,
item.textEdit.range.start.character + #item.textEdit.newText + offset,
})
end

Expand Down

0 comments on commit 3927e23

Please sign in to comment.