diff options
Diffstat (limited to 'lua/blink/cmp/completion/accept/preview.lua')
| -rw-r--r-- | lua/blink/cmp/completion/accept/preview.lua | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lua/blink/cmp/completion/accept/preview.lua b/lua/blink/cmp/completion/accept/preview.lua new file mode 100644 index 0000000..88b46e2 --- /dev/null +++ b/lua/blink/cmp/completion/accept/preview.lua @@ -0,0 +1,35 @@ +--- @param item blink.cmp.CompletionItem +--- @return lsp.TextEdit undo_text_edit, integer[]? undo_cursor_pos The text edit to apply and the original cursor +--- position to move to when undoing the preview, +local function preview(item) + local text_edits_lib = require('blink.cmp.lib.text_edits') + local text_edit = text_edits_lib.get_from_item(item) + + if item.insertTextFormat == vim.lsp.protocol.InsertTextFormat.Snippet then + local expanded_snippet = require('blink.cmp.sources.snippets.utils').safe_parse(text_edit.newText) + local snippet = expanded_snippet and tostring(expanded_snippet) or text_edit.newText + local get_prefix_before_brackets_and_quotes = require('blink.cmp.completion.accept.prefix') + text_edit.newText = get_prefix_before_brackets_and_quotes(snippet) + end + + local undo_text_edit = text_edits_lib.get_undo_text_edit(text_edit) + local cursor_pos = { + text_edit.range.start.line + 1, + text_edit.range.start.character + #text_edit.newText, + } + + text_edits_lib.apply({ text_edit }) + + local original_cursor = vim.api.nvim_win_get_cursor(0) + local cursor_moved = false + + -- TODO: remove when text_edits_lib.apply begins setting cursor position + if vim.api.nvim_get_mode().mode ~= 'c' then + vim.api.nvim_win_set_cursor(0, cursor_pos) + cursor_moved = true + end + + return undo_text_edit, cursor_moved and original_cursor or nil +end + +return preview |
