fix treesitter defination lookup for go

neovim_0_5
ray-x 3 years ago
parent f430fdfbb9
commit 7c6c37d169

@ -251,16 +251,25 @@ function M.locations_to_items(locations)
item.range = locations[i].range
if TS_analysis_enabled then
if uri_def[item.uri] == nil then
if uri_def[item.uri] == nil or uri_def[item.uri] == {} then
-- find def in file
local def = ts_defination(item.uri, item.range)
uri_def[item.uri] = def or {}
if def and def.start then
uri_def[item.uri] = def
if def.start then -- find for the 1st time
for i = 1, #items do
if items[i].uri == item.uri and items[i].range.start.line == def.start.line then
items[i].definition = true
end
end
end
end
end
log(uri_def[item.uri], item.range)
local def = uri_def[item.uri]
if def.start and item.range then
if def and def.start and item.range then
if def.start.line == item.range.start.line then
log("ts def found")
log("ts def in current line")
item.definition = true
end
end
@ -271,10 +280,11 @@ function M.locations_to_items(locations)
item.display_filename = filename or item.filename
item.call_by = find_ts_func_by_range(funcs, item.range)
item.rpath = util.get_relative_path(cwd, item.filename)
table.insert(items, item)
width = math.max(width, #item.text)
item.symbol_name = get_symbol(item.text, item.range)
item.lhs = check_lhs(item.text, item.symbol_name)
table.insert(items, item)
end
trace(uri_def)
return items, width + 24 -- TODO handle long line?

@ -103,6 +103,7 @@ function M.prepare_for_render(items, opts)
if item.definition then
ts_report = ts_report .. '🦕 '
end
local header_len = #ts_report + 2 -- magic number 2
trace(ts_report)
item.text = item.text:gsub('%s*[%[%(%{]*%s*$', '')
@ -116,7 +117,7 @@ function M.prepare_for_render(items, opts)
endwise = '()'
ts_report = ts_report .. ''
end
if #ts_report > 8 then
if #ts_report > header_len then
ts_report = ts_report .. ''
end
ts_report = ts_report .. value.kind .. txt .. endwise

@ -39,7 +39,7 @@ local get_icon = function(kind)
return match_kinds[kind]
end
end
-- require'navigator.treesitter'.goto_definition()
function M.goto_definition(bufnr)
bufnr = bufnr or api.nvim_get_current_buf()
local node_at_point = ts_utils.get_node_at_cursor()
@ -51,6 +51,7 @@ function M.goto_definition(bufnr)
local definition = locals.find_definition(node_at_point, bufnr)
if definition ~= node_at_point then
log("def found:", definition:range())
ts_utils.goto_node(definition)
end
end
@ -66,22 +67,24 @@ function M.find_definition(range, 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
return {}
return
end
local node_at_point = root:named_descendant_for_range(symbolpos[1], symbolpos[2], symbolpos[1],
symbolpos[2])
if not node_at_point then
lerr("no node at cursor")
return {}
return
end
local definition = locals.find_definition(node_at_point, bufnr)
log("def found:", definition, definition:range())
if definition then
if definition ~= node_at_point then
log("def found:", definition:range())
local r, c = definition:range()
return {start = {line = r, character = c}}
else
log("def not found in ", bufnr)
end
return {}
end
--- Get definitions of bufnr (unique and sorted by order of appearance).

Loading…
Cancel
Save