summaryrefslogtreecommitdiff
path: root/mut/neovim/pack/plugins/start/blink.cmp/lua/blink/cmp/completion/accept/preview.lua
blob: 88b46e2a946c095c625949d3cb275f2bb33aeccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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