2021-05-22 00:54:10 +00:00
|
|
|
--- Note: some of the functions/code coped from treesitter/refactor/navigation.lua and may be modified
|
|
|
|
-- to fit in navigator.lua
|
2021-04-24 02:38:47 +00:00
|
|
|
local gui = require "navigator.gui"
|
2021-08-19 15:14:03 +00:00
|
|
|
local fn = vim.fn
|
2021-08-29 07:35:46 +00:00
|
|
|
local lru = require('navigator.lru').new(500, 1024 * 1024)
|
2021-05-17 03:15:15 +00:00
|
|
|
|
|
|
|
local ok, ts_locals = pcall(require, "nvim-treesitter.locals")
|
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
if not ok then
|
|
|
|
error("treesitter not installed")
|
2021-10-17 04:29:17 +00:00
|
|
|
return nil
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
local parsers = require "nvim-treesitter.parsers"
|
2021-05-08 04:54:37 +00:00
|
|
|
local utils = require "nvim-treesitter.utils"
|
2021-05-22 00:54:10 +00:00
|
|
|
local locals = require 'nvim-treesitter.locals'
|
|
|
|
local ts_utils = require 'nvim-treesitter.ts_utils'
|
2021-04-24 02:38:47 +00:00
|
|
|
local api = vim.api
|
2021-04-28 11:57:05 +00:00
|
|
|
local util = require "navigator.util"
|
2021-04-24 02:38:47 +00:00
|
|
|
local M = {}
|
|
|
|
|
2021-09-09 02:59:58 +00:00
|
|
|
local cwd = vim.loop.cwd()
|
2021-05-15 01:11:20 +00:00
|
|
|
local log = require"navigator.util".log
|
2021-05-22 00:54:10 +00:00
|
|
|
local lerr = require"navigator.util".error
|
2021-05-21 11:39:46 +00:00
|
|
|
local trace = require"navigator.util".trace
|
2021-04-24 02:38:47 +00:00
|
|
|
|
|
|
|
local get_icon = function(kind)
|
2021-06-15 20:46:04 +00:00
|
|
|
if kind == nil or _NgConfigValues.icons.match_kinds[kind] == nil then
|
|
|
|
return _NgConfigValues.icons.treesitter_defult
|
2021-04-24 02:38:47 +00:00
|
|
|
else
|
2021-06-15 20:46:04 +00:00
|
|
|
return _NgConfigValues.icons.match_kinds[kind]
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
|
|
|
end
|
2021-05-22 14:03:34 +00:00
|
|
|
-- require'navigator.treesitter'.goto_definition()
|
2021-05-22 00:54:10 +00:00
|
|
|
function M.goto_definition(bufnr)
|
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
|
|
|
local node_at_point = ts_utils.get_node_at_cursor()
|
|
|
|
|
|
|
|
if not node_at_point then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local definition = locals.find_definition(node_at_point, bufnr)
|
|
|
|
|
|
|
|
if definition ~= node_at_point then
|
2021-05-22 14:03:34 +00:00
|
|
|
log("def found:", definition:range())
|
2021-05-22 00:54:10 +00:00
|
|
|
ts_utils.goto_node(definition)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
local function node_is_definination(node)
|
|
|
|
if node:parent() == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local nd_type = node:parent():type()
|
|
|
|
local decl = {'short_var_declaration', 'short_var_declaration', 'declaration'}
|
|
|
|
|
|
|
|
if vim.tbl_contains(decl, nd_type) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
if node:parent():parent() == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
nd_type = node:parent():parent():type()
|
|
|
|
if vim.tbl_contains(decl, nd_type) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-05-22 00:54:10 +00:00
|
|
|
-- use lsp range to find def
|
|
|
|
function M.find_definition(range, bufnr)
|
2021-05-22 05:34:46 +00:00
|
|
|
if not range or not range.start then
|
|
|
|
lerr("find_def incorrect range", range)
|
2021-05-22 00:54:10 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
2021-05-22 05:34:46 +00:00
|
|
|
local parser = parsers.get_parser(bufnr)
|
|
|
|
local symbolpos = {range.start.line, range.start.character} -- +1 or not?
|
|
|
|
local root = ts_utils.get_root_for_position(range.start.line, range.start.character, parser)
|
|
|
|
if not root then
|
2021-05-22 14:03:34 +00:00
|
|
|
return
|
2021-05-22 05:34:46 +00:00
|
|
|
end
|
2021-10-17 04:29:17 +00:00
|
|
|
local node_at_point = root:named_descendant_for_range(symbolpos[1], symbolpos[2], symbolpos[1], symbolpos[2])
|
2021-05-22 00:54:10 +00:00
|
|
|
if not node_at_point then
|
|
|
|
lerr("no node at cursor")
|
2021-05-22 14:03:34 +00:00
|
|
|
return
|
2021-05-22 00:54:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local definition = locals.find_definition(node_at_point, bufnr)
|
2021-09-13 13:18:54 +00:00
|
|
|
if definition ~= node_at_point then -- NOTE: it may not worksfor some of languages. if def not found, ts
|
|
|
|
-- returns current node. if your node is def, then it also return self... then I have no idea weather it is
|
|
|
|
-- def or not
|
|
|
|
trace("info: def found:", definition:range(), definition:type())
|
|
|
|
local r, c = definition:range()
|
|
|
|
return {start = {line = r, character = c}}
|
|
|
|
elseif node_is_definination(node_at_point) then
|
|
|
|
trace("declaraction here ", definition:type())
|
2021-05-22 05:34:46 +00:00
|
|
|
local r, c = definition:range()
|
|
|
|
return {start = {line = r, character = c}}
|
2021-05-22 14:03:34 +00:00
|
|
|
else
|
2021-10-17 04:29:17 +00:00
|
|
|
trace("error: def not found in ", bufnr, definition:range(), definition:type(), definition:parent():type())
|
2021-05-22 05:34:46 +00:00
|
|
|
end
|
2021-05-22 00:54:10 +00:00
|
|
|
end
|
|
|
|
|
2021-04-28 11:57:05 +00:00
|
|
|
--- Get definitions of bufnr (unique and sorted by order of appearance).
|
|
|
|
--- This function copy from treesitter/refactor/navigation.lua
|
|
|
|
local function get_definitions(bufnr)
|
|
|
|
local local_nodes = ts_locals.get_locals(bufnr)
|
|
|
|
|
|
|
|
-- Make sure the nodes are unique.
|
|
|
|
local nodes_set = {}
|
|
|
|
for _, loc in ipairs(local_nodes) do
|
|
|
|
if loc.definition then
|
2021-05-15 01:11:20 +00:00
|
|
|
ts_locals.recurse_local_nodes(loc.definition, function(_, node, _, match)
|
|
|
|
-- lua doesn't compare tables by value,
|
|
|
|
-- use the value from byte count instead.
|
|
|
|
local _, _, start = node:start()
|
|
|
|
nodes_set[start] = {node = node, type = match or ""}
|
|
|
|
end)
|
2021-04-28 11:57:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort by order of appearance.
|
|
|
|
local definition_nodes = vim.tbl_values(nodes_set)
|
2021-05-15 01:11:20 +00:00
|
|
|
table.sort(definition_nodes, function(a, b)
|
|
|
|
local _, _, start_a = a.node:start()
|
|
|
|
local _, _, start_b = b.node:start()
|
|
|
|
return start_a < start_b
|
|
|
|
end)
|
2021-04-28 11:57:05 +00:00
|
|
|
|
|
|
|
return definition_nodes
|
|
|
|
end
|
|
|
|
|
2021-05-15 01:11:20 +00:00
|
|
|
local function prepare_node(node, kind)
|
|
|
|
local matches = {}
|
|
|
|
kind = kind or node.type
|
|
|
|
if node.node then
|
|
|
|
table.insert(matches, {kind = get_icon(kind), def = node.node, type = kind})
|
|
|
|
else
|
2021-05-21 11:39:46 +00:00
|
|
|
for name, item in pairs(node) do
|
|
|
|
vim.list_extend(matches, prepare_node(item, name))
|
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
end
|
|
|
|
return matches
|
|
|
|
end
|
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
local function get_scope(type, source)
|
2021-05-17 03:15:15 +00:00
|
|
|
local sbl, sbc, sel, sec = source:range()
|
2021-04-24 02:38:47 +00:00
|
|
|
local current = source
|
2021-05-17 03:15:15 +00:00
|
|
|
local result = current
|
|
|
|
local next = ts_utils.get_next_node(source)
|
|
|
|
local parent = current:parent()
|
2021-05-21 11:39:46 +00:00
|
|
|
trace(source:type(), source:range(), parent)
|
|
|
|
|
|
|
|
if type == 'method' or type == 'function' and parent ~= nil then
|
|
|
|
trace(parent:type(), parent:range())
|
|
|
|
-- a function name
|
|
|
|
if parent:type() == 'function_name' then
|
|
|
|
-- up one level
|
|
|
|
return parent:parent(), true
|
|
|
|
end
|
|
|
|
if parent:type() == 'function_name_field' then
|
|
|
|
return parent:parent():parent(), true
|
|
|
|
end
|
2021-05-22 05:34:46 +00:00
|
|
|
|
|
|
|
-- for C++
|
|
|
|
local n = source
|
|
|
|
for i = 1, 4, 1 do
|
|
|
|
if n == nil or n:parent() == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
n = n:parent()
|
|
|
|
if n:type() == 'function_definition' then
|
|
|
|
return n, true
|
|
|
|
end
|
|
|
|
end
|
2021-05-21 11:39:46 +00:00
|
|
|
return parent, true
|
|
|
|
end
|
2021-04-24 02:38:47 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
if type == "var" and next ~= nil then
|
2021-10-17 04:29:17 +00:00
|
|
|
if next:type() == "function" or next:type() == "arrow_function" or next:type() == "function_definition" then
|
2021-05-21 11:39:46 +00:00
|
|
|
trace(current:type(), current:range())
|
|
|
|
return next, true
|
|
|
|
elseif parent:type() == 'function_declaration' then
|
|
|
|
return parent, true
|
|
|
|
else
|
|
|
|
trace(source, source:type())
|
|
|
|
return source, false
|
|
|
|
end
|
2021-05-22 05:34:46 +00:00
|
|
|
else
|
|
|
|
-- M.fun1 = function() end
|
|
|
|
-- lets work up and see next node, lua
|
2021-05-21 11:39:46 +00:00
|
|
|
local n = source
|
|
|
|
for i = 1, 4, 1 do
|
|
|
|
if n == nil or n:parent() == nil then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
n = n:parent()
|
|
|
|
next = ts_utils.get_next_node(n)
|
|
|
|
if next ~= nil and next:type() == 'function_definition' then
|
|
|
|
return next, true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if source:type() == "type_identifier" then
|
|
|
|
return source:parent(), true
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
2021-05-21 11:39:46 +00:00
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_smallest_context(source)
|
|
|
|
local scopes = ts_locals.get_scopes()
|
2021-05-21 11:39:46 +00:00
|
|
|
for key, value in pairs(scopes) do
|
|
|
|
trace(key, value)
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
local current = source
|
2021-05-21 11:39:46 +00:00
|
|
|
while current ~= nil and not vim.tbl_contains(scopes, current) do
|
|
|
|
current = current:parent()
|
|
|
|
end
|
|
|
|
if current ~= nil then
|
|
|
|
return current, true
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
-- if source:type() == "identifier" then return get_var_context(source) end
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
|
|
|
|
2021-05-15 01:11:20 +00:00
|
|
|
local lsp_reference = require"navigator.dochighlight".goto_adjent_reference
|
2021-05-08 04:54:37 +00:00
|
|
|
|
|
|
|
function M.goto_adjacent_usage(bufnr, delta)
|
|
|
|
local opt = {forward = true}
|
2021-05-17 03:15:15 +00:00
|
|
|
-- log(delta)
|
2021-05-21 11:39:46 +00:00
|
|
|
if delta < 0 then
|
2021-10-24 01:30:52 +00:00
|
|
|
opt.forward = false
|
2021-05-21 11:39:46 +00:00
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
2021-05-08 04:54:37 +00:00
|
|
|
local node_at_point = ts_utils.get_node_at_cursor()
|
|
|
|
if not node_at_point then
|
|
|
|
lsp_reference(opt)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local def_node, scope = ts_locals.find_definition(node_at_point, bufnr)
|
|
|
|
local usages = ts_locals.find_usages(def_node, scope, bufnr)
|
|
|
|
|
|
|
|
local index = utils.index_of(usages, node_at_point)
|
|
|
|
if not index then
|
|
|
|
lsp_reference(opt)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local target_index = (index + delta + #usages - 1) % #usages + 1
|
|
|
|
ts_utils.goto_node(usages[target_index])
|
|
|
|
end
|
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
function M.goto_next_usage(bufnr)
|
|
|
|
return M.goto_adjacent_usage(bufnr, 1)
|
|
|
|
end
|
|
|
|
function M.goto_previous_usage(bufnr)
|
|
|
|
return M.goto_adjacent_usage(bufnr, -1)
|
|
|
|
end
|
2021-05-08 04:54:37 +00:00
|
|
|
|
2021-08-29 07:35:46 +00:00
|
|
|
local function key(fname, filter)
|
|
|
|
return fname .. vim.inspect(filter)
|
|
|
|
end
|
|
|
|
|
2021-05-15 01:11:20 +00:00
|
|
|
local function get_all_nodes(bufnr, filter, summary)
|
2021-04-24 02:38:47 +00:00
|
|
|
local fname = vim.fn.expand("%:p:f")
|
|
|
|
local uri = vim.uri_from_fname(fname)
|
|
|
|
if bufnr ~= 0 then
|
|
|
|
uri = vim.uri_from_bufnr(bufnr)
|
|
|
|
fname = vim.uri_to_fname(uri)
|
|
|
|
end
|
2021-08-29 07:35:46 +00:00
|
|
|
|
|
|
|
local ftime = vim.fn.getftime(fname)
|
|
|
|
|
|
|
|
local hash = key(fname, filter)
|
|
|
|
|
|
|
|
local result = lru:get(hash)
|
|
|
|
if result ~= nil and result.ftime == ftime then
|
|
|
|
log("get data from cache")
|
|
|
|
return result.nodes, result.length
|
|
|
|
end
|
|
|
|
|
|
|
|
if result ~= nil and result.ftime ~= ftime then
|
|
|
|
lru:delete(hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
trace(bufnr, filter, summary)
|
|
|
|
if not bufnr then
|
|
|
|
print("get_all_node invalide bufnr")
|
|
|
|
end
|
|
|
|
summary = summary or false
|
|
|
|
if not parsers.has_parser() then
|
|
|
|
print("ts not loaded")
|
|
|
|
end
|
|
|
|
|
2021-09-07 15:06:58 +00:00
|
|
|
local path_sep = require"navigator.util".path_sep()
|
|
|
|
local path_cur = require"navigator.util".path_cur()
|
2021-06-25 00:33:04 +00:00
|
|
|
local display_filename = fname:gsub(cwd .. path_sep, path_cur, 1)
|
2021-04-24 02:38:47 +00:00
|
|
|
|
|
|
|
local all_nodes = {}
|
|
|
|
-- Support completion-nvim customized label map
|
|
|
|
local customized_labels = vim.g.completion_customize_lsp_label or {}
|
|
|
|
|
2021-04-28 11:57:05 +00:00
|
|
|
-- Force some types to act like they are parents
|
|
|
|
-- instead of neighbors of the next nodes.
|
2021-05-17 03:15:15 +00:00
|
|
|
local containers = {
|
|
|
|
["function"] = true,
|
2021-09-13 13:18:54 +00:00
|
|
|
["local_function"] = true,
|
2021-05-17 03:15:15 +00:00
|
|
|
["arrow_function"] = true,
|
|
|
|
["type"] = true,
|
|
|
|
["class"] = true,
|
2021-11-02 09:16:16 +00:00
|
|
|
["struct"] = true,
|
2021-05-17 03:15:15 +00:00
|
|
|
["method"] = true
|
|
|
|
}
|
2021-09-13 13:18:54 +00:00
|
|
|
|
|
|
|
-- check and load buff
|
|
|
|
|
|
|
|
local should_unload = false
|
|
|
|
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
|
|
|
should_unload = true
|
|
|
|
vim.fn.bufload(bufnr)
|
|
|
|
end
|
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
-- Step 2 find correct completions
|
2021-04-28 11:57:05 +00:00
|
|
|
local length = 10
|
|
|
|
local parents = {} -- stack of nodes a clever algorithm from treesiter refactor @Santos Gallegos
|
|
|
|
for _, def in ipairs(get_definitions(bufnr)) do
|
|
|
|
local n = #parents
|
|
|
|
for i = 1, n do
|
|
|
|
local index = n + 1 - i
|
|
|
|
local parent_def = parents[index]
|
2021-05-26 12:15:32 +00:00
|
|
|
if ts_utils.is_parent(parent_def.node, def.node)
|
|
|
|
or (containers[parent_def.type] and ts_utils.is_parent(parent_def.node:parent(), def.node)) then
|
2021-04-28 11:57:05 +00:00
|
|
|
break
|
|
|
|
else
|
|
|
|
parents[index] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
parents[#parents + 1] = def
|
2021-04-24 02:38:47 +00:00
|
|
|
local nodes = prepare_node(def)
|
|
|
|
local item = {}
|
2021-05-15 01:11:20 +00:00
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
for _, node in ipairs(nodes) do
|
|
|
|
item.kind = node.kind
|
2021-05-15 01:11:20 +00:00
|
|
|
item.type = node.type
|
2021-05-21 11:39:46 +00:00
|
|
|
|
|
|
|
if filter ~= nil and not filter[item.type] then
|
|
|
|
trace(item.type, item.kind)
|
|
|
|
goto continue
|
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
local tsdata = node.def
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
if node.def == nil then
|
|
|
|
goto continue
|
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
item.node_text = ts_utils.get_node_text(tsdata, bufnr)[1]
|
2021-05-21 11:39:46 +00:00
|
|
|
local scope, is_func
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
if summary then
|
|
|
|
scope, is_func = get_scope(item.type, tsdata)
|
|
|
|
else
|
|
|
|
scope, is_func = get_smallest_context(tsdata)
|
|
|
|
end
|
|
|
|
if is_func then
|
|
|
|
-- hack for lua and maybe other language aswell
|
|
|
|
local parent = tsdata:parent()
|
2021-10-17 04:29:17 +00:00
|
|
|
if parent ~= nil and parent:type() == 'function_name' or parent:type() == 'function_name_field' then
|
2021-05-21 11:39:46 +00:00
|
|
|
item.node_text = ts_utils.get_node_text(parent, bufnr)[1]
|
|
|
|
log(parent:type(), item.node_text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-09 20:39:37 +00:00
|
|
|
trace(item.node_text, item.kind, item.type)
|
2021-05-17 03:15:15 +00:00
|
|
|
if scope ~= nil then
|
|
|
|
-- it is strange..
|
2021-05-21 11:39:46 +00:00
|
|
|
if not is_func and summary then
|
|
|
|
goto continue
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
item.node_scope = ts_utils.node_to_lsp_range(scope)
|
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
if summary then
|
2021-05-21 11:39:46 +00:00
|
|
|
if item.node_scope ~= nil then
|
|
|
|
table.insert(all_nodes, item)
|
|
|
|
end
|
|
|
|
|
2021-06-09 20:39:37 +00:00
|
|
|
if item.node_scope then
|
2021-06-11 11:33:22 +00:00
|
|
|
trace(item.type, tsdata:type(), item.node_text, item.kind, item.node_text, "range",
|
|
|
|
item.node_scope.start.line, item.node_scope['end'].line) -- set to log if need to trace result
|
2021-06-09 20:39:37 +00:00
|
|
|
end
|
2021-05-08 04:54:37 +00:00
|
|
|
goto continue
|
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
item.range = ts_utils.node_to_lsp_range(tsdata)
|
|
|
|
local start_line_node, _, _ = tsdata:start()
|
2021-05-21 11:39:46 +00:00
|
|
|
if item.node_text == "_" then
|
|
|
|
goto continue
|
|
|
|
end
|
2021-10-17 04:29:17 +00:00
|
|
|
item.full_text = vim.trim(api.nvim_buf_get_lines(bufnr, start_line_node, start_line_node + 1, false)[1] or "")
|
2021-05-22 09:49:01 +00:00
|
|
|
|
|
|
|
item.full_text = item.full_text:gsub('%s*[%[%(%{]*%s*$', '')
|
2021-04-24 02:38:47 +00:00
|
|
|
item.uri = uri
|
|
|
|
item.name = node.node_text
|
|
|
|
item.filename = fname
|
|
|
|
item.display_filename = display_filename
|
2021-04-28 11:57:05 +00:00
|
|
|
item.lnum, item.col, _ = def.node:start()
|
|
|
|
item.lnum = item.lnum + 1
|
|
|
|
item.col = item.col + 1
|
2021-05-08 04:54:37 +00:00
|
|
|
local indent = ""
|
2021-05-21 11:39:46 +00:00
|
|
|
if #parents > 1 then
|
|
|
|
indent = string.rep(" ", #parents - 1) .. " "
|
|
|
|
end
|
2021-04-28 11:57:05 +00:00
|
|
|
|
2021-10-17 04:29:17 +00:00
|
|
|
item.text = string.format(" %s %s%-10s\t %s", item.kind, indent, item.node_text, item.full_text)
|
2021-05-21 11:39:46 +00:00
|
|
|
if #item.text > length then
|
|
|
|
length = #item.text
|
|
|
|
end
|
2021-04-24 02:38:47 +00:00
|
|
|
table.insert(all_nodes, item)
|
2021-04-28 11:57:05 +00:00
|
|
|
::continue::
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
|
|
|
end
|
2021-05-21 11:39:46 +00:00
|
|
|
trace(all_nodes)
|
2021-08-29 07:35:46 +00:00
|
|
|
local nd = {nodes = all_nodes, ftime = vim.fn.getftime(fname), length = length}
|
|
|
|
lru:set(hash, nd)
|
2021-09-13 13:18:54 +00:00
|
|
|
if should_unload then
|
|
|
|
vim.api.nvim_buf_delete(bufnr, {unload = true})
|
|
|
|
end
|
2021-04-28 11:57:05 +00:00
|
|
|
return all_nodes, length
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
function M.buf_func(bufnr)
|
|
|
|
if not ok or ts_locals == nil then
|
2021-05-15 01:11:20 +00:00
|
|
|
error("treesitter not loaded")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
bufnr = bufnr or api.nvim_get_current_buf()
|
2021-09-13 13:18:54 +00:00
|
|
|
|
2021-05-17 03:15:15 +00:00
|
|
|
local all_nodes, width = get_all_nodes(bufnr, {
|
|
|
|
["function"] = true,
|
|
|
|
["var"] = true,
|
|
|
|
["method"] = true,
|
|
|
|
["class"] = true,
|
|
|
|
["type"] = true
|
|
|
|
}, true)
|
2021-05-21 11:39:46 +00:00
|
|
|
if #all_nodes < 1 then
|
2021-06-11 11:33:22 +00:00
|
|
|
trace("no node found for ", bufnr) -- set to log
|
2021-05-21 11:39:46 +00:00
|
|
|
return
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
if all_nodes[1].node_scope then
|
|
|
|
table.sort(all_nodes, function(i, j)
|
|
|
|
if i.node_scope and j.node_scope then
|
|
|
|
if i.node_scope['end'].line == j.node_scope['end'].line then
|
|
|
|
return i.node_scope.start.line > j.node_scope.start.line
|
|
|
|
else
|
|
|
|
return i.node_scope['end'].line < j.node_scope['end'].line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
table.sort(all_nodes, function(i, j)
|
|
|
|
if i.range and j.range then
|
|
|
|
if i.range['end'].line == j.range['end'].line then
|
|
|
|
return i.range.start.line > j.range.start.line
|
|
|
|
else
|
|
|
|
return i.range['end'].line < j.range['end'].line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end)
|
|
|
|
end
|
2021-05-17 03:15:15 +00:00
|
|
|
|
2021-05-21 11:39:46 +00:00
|
|
|
return all_nodes, width
|
2021-05-15 01:11:20 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
function M.buf_ts()
|
2021-04-24 04:20:42 +00:00
|
|
|
if ts_locals == nil then
|
|
|
|
error("treesitter not loaded")
|
|
|
|
return
|
|
|
|
end
|
2021-05-15 01:11:20 +00:00
|
|
|
|
|
|
|
local bufnr = api.nvim_get_current_buf()
|
|
|
|
local all_nodes, width = get_all_nodes(bufnr)
|
|
|
|
|
|
|
|
local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
|
2021-05-17 03:15:15 +00:00
|
|
|
gui.new_list_view({
|
|
|
|
items = all_nodes,
|
|
|
|
prompt = true,
|
|
|
|
ft = ft,
|
|
|
|
rawdata = true,
|
|
|
|
width = width + 10,
|
2021-06-15 20:46:04 +00:00
|
|
|
api = _NgConfigValues.icons.treesitter_defult
|
2021-05-17 03:15:15 +00:00
|
|
|
})
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
2021-04-24 04:20:42 +00:00
|
|
|
|
2021-11-02 09:16:16 +00:00
|
|
|
M.get_all_nodes = get_all_nodes
|
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
function M.bufs_ts()
|
2021-04-24 04:20:42 +00:00
|
|
|
if ts_locals == nil then
|
|
|
|
error("treesitter not loaded")
|
|
|
|
return
|
|
|
|
end
|
2021-04-24 02:38:47 +00:00
|
|
|
local bufs = vim.api.nvim_list_bufs()
|
|
|
|
local ts_opened = {}
|
2021-04-28 11:57:05 +00:00
|
|
|
local max_length = 10
|
2021-04-24 02:38:47 +00:00
|
|
|
for _, buf in ipairs(bufs) do
|
|
|
|
local bname = vim.fn.bufname(buf)
|
2021-04-24 09:55:48 +00:00
|
|
|
if #bname > 0 and not util.exclude(bname) then
|
2021-04-24 02:38:47 +00:00
|
|
|
if vim.api.nvim_buf_is_loaded(buf) then
|
2021-04-28 11:57:05 +00:00
|
|
|
local all_nodes, length = get_all_nodes(buf)
|
2021-04-24 02:38:47 +00:00
|
|
|
if all_nodes ~= nil then
|
2021-05-21 11:39:46 +00:00
|
|
|
if length > max_length then
|
|
|
|
max_length = length
|
|
|
|
end
|
2021-04-24 02:38:47 +00:00
|
|
|
vim.list_extend(ts_opened, all_nodes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #ts_opened > 1 then
|
2021-05-21 11:39:46 +00:00
|
|
|
trace(ts_opened)
|
2021-05-15 01:11:20 +00:00
|
|
|
|
|
|
|
local ft = vim.api.nvim_buf_get_option(0, "ft")
|
2021-05-17 03:15:15 +00:00
|
|
|
gui.new_list_view({
|
|
|
|
items = ts_opened,
|
|
|
|
prompt = true,
|
|
|
|
ft = ft,
|
|
|
|
width = max_length + 10,
|
2021-06-15 20:46:04 +00:00
|
|
|
api = _NgConfigValues.icons.treesitter_defult
|
2021-05-17 03:15:15 +00:00
|
|
|
})
|
2021-04-24 02:38:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-19 15:14:03 +00:00
|
|
|
local function node_in_range(parser, range)
|
|
|
|
for _, child in pairs(parser._children) do
|
|
|
|
if child:contains(range) then
|
|
|
|
local result = node_in_range(child, range)
|
|
|
|
if not vim.tbl_contains({vim.bo.filetype}, result:lang()) then
|
2021-08-27 00:25:48 +00:00
|
|
|
-- log("not correct tree embedded or comment?", result:lang())
|
2021-08-19 15:14:03 +00:00
|
|
|
return parser
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return parser
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.get_node_at_line(lnum)
|
|
|
|
if not parsers.has_parser() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Get the position for the queried node
|
|
|
|
if lnum == nil then
|
|
|
|
local cursor = api.nvim_win_get_cursor(0)
|
|
|
|
lnum = cursor[1]
|
|
|
|
end
|
|
|
|
local first_non_whitespace_col = fn.match(fn.getline(lnum), '\\S')
|
|
|
|
local range = {lnum - 1, first_non_whitespace_col, lnum - 1, first_non_whitespace_col}
|
|
|
|
|
|
|
|
-- Get the language tree with nodes inside the given range
|
|
|
|
local root = parsers.get_parser()
|
|
|
|
local ts_tree = node_in_range(root, range)
|
2021-08-21 23:19:11 +00:00
|
|
|
-- log(ts_tree:trees())
|
2021-08-19 15:14:03 +00:00
|
|
|
local tree = ts_tree:trees()[1]
|
|
|
|
|
|
|
|
local node = tree:root():named_descendant_for_range(unpack(range))
|
|
|
|
|
2021-09-13 13:18:54 +00:00
|
|
|
-- trace(node, node:type()) -- log all lines and all nodes
|
2021-08-19 15:14:03 +00:00
|
|
|
return node
|
|
|
|
end
|
|
|
|
|
2021-10-17 04:29:17 +00:00
|
|
|
local usage_namespace = vim.api.nvim_create_namespace("nvim-treesitter-usages")
|
|
|
|
|
|
|
|
function M.highlight_usages(bufnr)
|
|
|
|
M.clear_usage_highlights(bufnr)
|
|
|
|
|
|
|
|
local node_at_point = ts_utils.get_node_at_cursor()
|
|
|
|
local references = locals.get_references(bufnr)
|
|
|
|
|
|
|
|
if not node_at_point or not vim.tbl_contains(references, node_at_point) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local def_node, scope = locals.find_definition(node_at_point, bufnr)
|
|
|
|
local usages = locals.find_usages(def_node, scope, bufnr)
|
|
|
|
|
|
|
|
for _, usage_node in ipairs(usages) do
|
|
|
|
if usage_node ~= node_at_point then
|
|
|
|
ts_utils.highlight_node(usage_node, bufnr, usage_namespace, "TSDefinitionUsage")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if def_node ~= node_at_point then
|
|
|
|
ts_utils.highlight_node(def_node, bufnr, usage_namespace, "TSDefinition")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.clear_usage_highlights(bufnr)
|
|
|
|
api.nvim_buf_clear_namespace(bufnr, usage_namespace, 0, -1)
|
|
|
|
end
|
|
|
|
|
2021-11-02 09:16:16 +00:00
|
|
|
function M.get_node_at_pos(pos, parser)
|
|
|
|
-- local cursor = api.nvim_win_get_cursor(winnr or 0)
|
|
|
|
local cursor_range = {pos[1], pos[2]}
|
|
|
|
|
|
|
|
log(cursor_range)
|
|
|
|
local root = ts_utils.get_root_for_position(unpack(cursor_range), parser)
|
|
|
|
|
|
|
|
if not root then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local node = root:named_descendant_for_range(cursor_range[1], cursor_range[2], cursor_range[1], cursor_range[2])
|
|
|
|
log(node, node:range())
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.get_node_scope(node)
|
|
|
|
-- local
|
|
|
|
local n = node
|
|
|
|
if n == nil then
|
|
|
|
return 0, 0, 0, 0
|
|
|
|
end
|
|
|
|
local sr, sc, er, ec = n:range()
|
|
|
|
|
|
|
|
log(n:range())
|
|
|
|
for _ = 1, 6 do
|
|
|
|
if n == nil then
|
|
|
|
return 0, 0, 0, 0
|
|
|
|
end
|
|
|
|
|
|
|
|
local nsr, nsc, ner, nec = n:range()
|
|
|
|
|
|
|
|
if nsr < sr then
|
|
|
|
log(sr, er)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
sr, sc, er, ec = nsr, nsc, ner, nec
|
|
|
|
if n:parent() then
|
|
|
|
n = n:parent()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return sr, sc, er, ec
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-04-24 02:38:47 +00:00
|
|
|
return M
|