~vigoux/complementree.nvim

8f7b2f7c747179d7dc12b6d7e5f4f67b7bf5b106 — Thomas Vigouroux 3 months ago 9ea158e master
perf(snippet): simplify snippet handling
1 files changed, 14 insertions(+), 56 deletions(-)

M lua/complementree/sources.lua
M lua/complementree/sources.lua => lua/complementree/sources.lua +14 -56
@@ 56,7 56,6 @@ end
function Sources.lsp_matches(opts)
  opts = options.get({}, opts)
  return cached('lsp', function(line_to_cursor, lnum)

    local function adjust_start_col(line_number, line, items, encoding)
      local min_start_char = nil



@@ 93,7 92,7 @@ function Sources.lsp_matches(opts)
    local start_col = vim.fn.match(line_to_cursor, '\\k*$') + 1
    for client_id, result in pairs(result_all) do
      local client = lsp.get_client_by_id(client_id)
      local items = result.result.items or {}
      local items = (result.result or {}).items or {}

      local tmp_col = adjust_start_col(lnum, line_to_cursor, items, client.offset_encoding or 'utf-16')
      if tmp_col and tmp_col < start_col then


@@ 142,60 141,22 @@ function Sources.lsp_matches(opts)
  end)
end

local function lsp_completedone(completed_item)
  -- TODO: over complicated
  local cursor = api.nvim_win_get_cursor(0)
  local col = cursor[2]
  local lnum = cursor[1] - 1
  local bufnr = api.nvim_get_current_buf()

  local extra = completed_item.user_data.extra
  local item = extra.item

  local client = lsp.get_client_by_id(extra.client_id)
  if not client then
    error(string.format("Could not find client %d", extra.client_id))
  end

  local expand_snippet = item.insertTextFormat == 2
  local resolveEdits = (client.server_capabilities.completionProvider or {}).resolveProvider
  local offset_encoding = client and client.offset_encoding or 'utf-16'

  local tidy = function() end
  local suffix = nil

  if expand_snippet then
    local line = api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
    tidy = function()
      local start_char = col - #completed_item.word
      local l = line
      api.nvim_buf_set_text(bufnr, lnum, start_char, lnum, #l, { '' })
    end
    suffix = line:sub(col + 1)
Sources.complete_done_cbs.lsp = function(completed_item)
  local lsp_item = completed_item.user_data.extra.item
  local snippet
  if lsp_item.textEdit then
    snippet = lsp_item.textEdit.newText
  elseif lsp_item.insertTextFormat == 2 then
    snippet = lsp_item.insertText
  end

  if item.additionalTextEdits then
    tidy()
    tidy = function() end
    lsp.util.apply_text_edits(item.additionalTextEdits, bufnr, offset_encoding)
  elseif resolveEdits and type(item) == 'table' then
    local v = client.request_sync('completionItem/resolve', item, 1000, bufnr)
    assert(not v.err, vim.inspect(v.err))
    local res = v.result
    if res.additionalTextEdits then
      tidy()
      tidy = function() end
      lsp.util.apply_text_edits(res.additionalTextEdits, bufnr, offset_encoding)
    end
  end
  if expand_snippet then
    tidy()
    apply_snippet(item, suffix, lnum)
  if snippet then
    local cursor = vim.api.nvim_win_get_cursor(0)
    vim.api.nvim_buf_set_text(0, cursor[1] - 1, cursor[2] - #completed_item.word, cursor[1] - 1, cursor[2], {})
    vim.snippet.expand(snippet)
  end
end

Sources.complete_done_cbs.lsp = lsp_completedone

-- Conditional loading of the snippets handling / sources
local lsnip_present, luasnip = pcall(require, "luasnip")
local snippy_present, snippy = pcall(require, "snippy")


@@ 267,7 228,6 @@ if lsnip_present then
      require('luasnip').expand()
    end
  end

elseif snippy_present then
  ---@class SnippyMatchesOpts



@@ 397,9 357,8 @@ function Sources.filepath_matches(opts)
          iter = iter_stack[#iter_stack]
          path = path_stack[#path_stack]
        elseif
          (vim.startswith(next_path, '.') and not config.show_hidden) or
          (#config.ignore_pattern > 0 and string.find(next_path, config.ignore_pattern) ~= nil) then

            (vim.startswith(next_path, '.') and not config.show_hidden) or
            (#config.ignore_pattern > 0 and string.find(next_path, config.ignore_pattern) ~= nil) then
          next_path = nil
          path_type = nil
        else


@@ 459,7 418,6 @@ function Sources.treesitter_matches(opts)
    local items = {}

    for _, def in ipairs(defs) do

      local node
      local kind
      for k, cap in pairs(def) do