added treesitter builtin (#31)
* added treesitter builtim * fixed treesitter pr comments * fix the buffer previewer to keep lnum visable
This commit is contained in:
@@ -370,4 +370,60 @@ builtin.buffers = function(opts)
|
|||||||
}):find()
|
}):find()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function prepare_match(entry, kind)
|
||||||
|
local entries = {}
|
||||||
|
|
||||||
|
if entry.node then
|
||||||
|
entry["kind"] = kind
|
||||||
|
table.insert(entries, entry)
|
||||||
|
else
|
||||||
|
for name, item in pairs(entry) do
|
||||||
|
vim.list_extend(entries, prepare_match(item, name))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return entries
|
||||||
|
end
|
||||||
|
|
||||||
|
builtin.treesitter = function(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
|
local has_nvim_treesitter, nvim_treesitter = pcall(require, 'nvim-treesitter')
|
||||||
|
if not has_nvim_treesitter then
|
||||||
|
print('You need to install nvim-treesitter')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local parsers = require('nvim-treesitter.parsers')
|
||||||
|
if not parsers.has_parser() then
|
||||||
|
print('No parser for the current buffer')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local ts_locals = require('nvim-treesitter.locals')
|
||||||
|
local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
|
local results = {}
|
||||||
|
for _, definitions in ipairs(ts_locals.get_definitions(bufnr)) do
|
||||||
|
local entries = prepare_match(definitions)
|
||||||
|
for _, entry in ipairs(entries) do
|
||||||
|
table.insert(results, entry)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.tbl_isempty(results) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
pickers.new(opts, {
|
||||||
|
prompt = 'Treesitter Symbols',
|
||||||
|
finder = finders.new_table {
|
||||||
|
results = results,
|
||||||
|
entry_maker = make_entry.gen_from_treesitter(opts)
|
||||||
|
},
|
||||||
|
previewer = previewers.vim_buffer.new(opts),
|
||||||
|
sorter = sorters.get_norcalli_sorter(),
|
||||||
|
}):find()
|
||||||
|
end
|
||||||
|
|
||||||
return builtin
|
return builtin
|
||||||
|
|||||||
@@ -191,4 +191,29 @@ function make_entry.gen_from_buffer(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function make_entry.gen_from_treesitter(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
return function(entry)
|
||||||
|
local ts_utils = require('nvim-treesitter.ts_utils')
|
||||||
|
local start_row, start_col, end_row, end_col = ts_utils.get_node_range(entry.node)
|
||||||
|
local node_text = ts_utils.get_node_text(entry.node)[1]
|
||||||
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
return {
|
||||||
|
valid = true,
|
||||||
|
|
||||||
|
value = entry.node,
|
||||||
|
ordinal = entry.kind .. " " .. node_text,
|
||||||
|
display = entry.kind .. " " .. node_text,
|
||||||
|
|
||||||
|
filename = vim.api.nvim_buf_get_name(bufnr),
|
||||||
|
-- need to add one since the previewer substacts one
|
||||||
|
lnum = start_row + 1,
|
||||||
|
col = start_col,
|
||||||
|
text = node_text,
|
||||||
|
start = start_row,
|
||||||
|
finish = end_row
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return make_entry
|
return make_entry
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ previewers.vim_buffer = defaulter(function(_)
|
|||||||
|
|
||||||
if entry.lnum then
|
if entry.lnum then
|
||||||
vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1)
|
vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1)
|
||||||
|
vim.api.nvim_win_set_option(status.preview_win, 'scrolloff', 10)
|
||||||
|
vim.api.nvim_win_set_cursor(status.preview_win, {entry.lnum, 0})
|
||||||
-- print("LNUM:", entry.lnum)
|
-- print("LNUM:", entry.lnum)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|||||||
Reference in New Issue
Block a user