Skip to content

Commit

Permalink
Improve treesitter completion
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbeam committed Feb 22, 2024
1 parent 86f6275 commit 9c2a199
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions lua/autocomplete/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,32 @@ local function complete_treesitter(bufnr, prefix, cmp_start)
end

local locals = require('nvim-treesitter.locals')
local defs = locals.get_definitions(bufnr)
local defs = locals.get_definitions_lookup_table(bufnr)
local items = {}

for _, def in ipairs(defs) do
local node
local kind
for k, cap in pairs(def) do
if k ~= 'associated' then
node = cap.node
kind = k
break
end
end

if node then
-- Use nicer name for completion kind
for id, entry in pairs(defs) do
local name = id:match('k_(.+)_%d+_%d+_%d+_%d+$')
local node = entry.node
local kind = entry.kind
if node and kind then
vim.print(name, kind)
for _, k in ipairs(vim.lsp.protocol.CompletionItemKind) do
if string.find(k:lower(), kind:lower()) then
kind = k
break
end
end

-- Get full text of the node
local start_line_node, _, _ = node:start()
local end_line_node, _, _ = node:end_()

local full_text = vim.trim(
vim.api.nvim_buf_get_lines(bufnr, start_line_node, start_line_node + 1, false)[1]
vim.api.nvim_buf_get_lines(bufnr, start_line_node, end_line_node + 1, false)[1]
or ''
)
local node_text = vim.treesitter.get_node_text(node, bufnr)

items[#items + 1] = {
word = node_text,
word = name,
kind = kind,
info = full_text,
icase = 1,
Expand Down

0 comments on commit 9c2a199

Please sign in to comment.