From 9cd123657fce6e563a7d24b438f61b012ca1559f Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Wed, 30 Oct 2024 12:42:13 -0400 Subject: [PATCH] feat: accept error handling, expose autocomplete.select --- lua/blink/cmp/accept/init.lua | 93 +++++++++++++------------- lua/blink/cmp/windows/autocomplete.lua | 6 +- 2 files changed, 51 insertions(+), 48 deletions(-) diff --git a/lua/blink/cmp/accept/init.lua b/lua/blink/cmp/accept/init.lua index 73201af1..bfa28763 100644 --- a/lua/blink/cmp/accept/init.lua +++ b/lua/blink/cmp/accept/init.lua @@ -9,61 +9,64 @@ local function accept(item) -- start the resolve immediately since text changes can invalidate the item -- with some LSPs (i.e. rust-analyzer) causing them to return the item as-is -- without i.e. auto-imports - require('blink.cmp.sources.lib').resolve(item):map(function(resolved_item) - local all_text_edits = - vim.deepcopy(resolved_item and resolved_item.additionalTextEdits or item.additionalTextEdits or {}) + require('blink.cmp.sources.lib') + .resolve(item) + :map(function(resolved_item) + local all_text_edits = + vim.deepcopy(resolved_item and resolved_item.additionalTextEdits or item.additionalTextEdits or {}) - -- create an undo point - if require('blink.cmp.config').accept.create_undo_point then - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('u', true, true, true), 'n', true) - end + -- create an undo point + if require('blink.cmp.config').accept.create_undo_point then + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('u', true, true, true), 'n', true) + end - item = vim.deepcopy(item) - item.textEdit = text_edits_lib.get_from_item(item) + item = vim.deepcopy(item) + item.textEdit = text_edits_lib.get_from_item(item) - -- Add brackets to the text edit if needed - local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item) - item.textEdit = text_edit_with_brackets + -- Add brackets to the text edit if needed + local brackets_status, text_edit_with_brackets, offset = brackets_lib.add_brackets(vim.bo.filetype, item) + 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(item.textEdit) - temp_text_edit.newText = '' - table.insert(all_text_edits, temp_text_edit) - text_edits_lib.apply_text_edits(item.client_id, all_text_edits) + -- 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(item.textEdit) + temp_text_edit.newText = '' + table.insert(all_text_edits, temp_text_edit) + text_edits_lib.apply_text_edits(item.client_id, all_text_edits) - -- Expand the snippet - vim.snippet.expand(item.textEdit.newText) + -- Expand the snippet + vim.snippet.expand(item.textEdit.newText) - -- OR Normal: Apply the text edit and move the cursor - else - table.insert(all_text_edits, item.textEdit) - text_edits_lib.apply_text_edits(item.client_id, all_text_edits) - -- TODO: should move the cursor only by the offset since text edit handles everything else? - vim.api.nvim_win_set_cursor(0, { - vim.api.nvim_win_get_cursor(0)[1], - item.textEdit.range.start.character + #item.textEdit.newText + offset, - }) - end + -- OR Normal: Apply the text edit and move the cursor + else + table.insert(all_text_edits, item.textEdit) + text_edits_lib.apply_text_edits(item.client_id, all_text_edits) + -- TODO: should move the cursor only by the offset since text edit handles everything else? + vim.api.nvim_win_set_cursor(0, { + vim.api.nvim_win_get_cursor(0)[1], + item.textEdit.range.start.character + #item.textEdit.newText + offset, + }) + end - -- Check semantic tokens for brackets, if needed, and apply additional text edits - 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() + -- Check semantic tokens for brackets, if needed, and apply additional text edits + 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() + require('blink.cmp.trigger.completion').show_if_on_trigger_character({ is_accept = true }) + require('blink.cmp.trigger.signature').show_if_on_trigger_character() + end) + else require('blink.cmp.trigger.completion').show_if_on_trigger_character({ is_accept = true }) require('blink.cmp.trigger.signature').show_if_on_trigger_character() - end) - else - require('blink.cmp.trigger.completion').show_if_on_trigger_character({ is_accept = true }) - require('blink.cmp.trigger.signature').show_if_on_trigger_character() - end + end - -- Notify the rust module that the item was accessed - require('blink.cmp.fuzzy').access(item) - end) + -- Notify the rust module that the item was accessed + require('blink.cmp.fuzzy').access(item) + end) + :catch(function(err) vim.notify(err, vim.log.levels.ERROR) end) end return accept diff --git a/lua/blink/cmp/windows/autocomplete.lua b/lua/blink/cmp/windows/autocomplete.lua index 697d18f8..7351c7c5 100644 --- a/lua/blink/cmp/windows/autocomplete.lua +++ b/lua/blink/cmp/windows/autocomplete.lua @@ -200,7 +200,7 @@ end --- @param line number --- @param skip_auto_insert? boolean -local function select(line, skip_auto_insert) +function autocomplete.select(line, skip_auto_insert) autocomplete.set_has_selected(true) vim.api.nvim_win_set_cursor(autocomplete.win:get_win(), { line, 0 }) @@ -238,7 +238,7 @@ function autocomplete.select_next(opts) line = line + 1 end - select(line, opts and opts.skip_auto_insert) + autocomplete.select(line, opts and opts.skip_auto_insert) end --- @params opts? { skip_auto_insert?: boolean } @@ -256,7 +256,7 @@ function autocomplete.select_prev(opts) line = line - 1 end - select(line, opts and opts.skip_auto_insert) + autocomplete.select(line, opts and opts.skip_auto_insert) end function autocomplete.listen_on_select(callback) table.insert(autocomplete.event_targets.on_select, callback) end