pull/420/head
ray-x 3 months ago
parent 41c126718a
commit 096f656920

@ -1,12 +1,12 @@
local api = vim.api
local HAS09 = vim.fn.has("nvim-0.9") == 1
local HAS09 = vim.fn.has('nvim-0.9') == 1
local get_node_text = vim.treesitter.get_node_text
if not HAS09 then
local get_node_text = vim.treesitter.query.get_node_text
end
local ts_utils = require("nvim-treesitter.ts_utils")
local util = require("go.utils")
local ts_utils = require('nvim-treesitter.ts_utils')
local util = require('go.utils')
local log = util.log
local trace = util.trace
-- local trace = util.log
@ -30,7 +30,7 @@ M.intersects = function(row, col, sRow, sCol, eRow, eCol)
return true
end
local locals = require("nvim-treesitter.locals")
local locals = require('nvim-treesitter.locals')
-- from navigator/treesitter.lua
-- modified from nvim-treesitter/treesitter-refactor plugin
-- Get definitions of bufnr (unique and sorted by order of appearance).
@ -40,35 +40,41 @@ local function get_definitions(bufnr)
-- Make sure the nodes are unique.
local nodes_set = {}
for _, loc in ipairs(local_nodes) do
loc = loc["local"]
loc = loc['local'] or loc -- TODO: remove when 0.9.6
if loc.definition then
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()
-- variadic_parameter_declaration
if node and node:parent() and string.find(node:parent():type(), "parameter_declaration") then
log("parameter_declaration skip")
if
node
and node:parent()
and string.find(node:parent():type(), 'parameter_declaration')
then
log('parameter_declaration skip')
return
end
nodes_set[start] = { node = node, type = match or "" }
nodes_set[start] = { node = node, type = match or '' }
end)
end
if loc.method then -- for go
locals.recurse_local_nodes(loc.method, function(_, node, full_match, match)
local k, l, start = node:start()
trace(node, k, l, full_match, match) -- match: parameter_list
if node:type() == "field_identifier" and nodes_set[start] == nil then
nodes_set[start] = { node = node, type = "method" }
if node:type() == 'field_identifier' and nodes_set[start] == nil then
nodes_set[start] = { node = node, type = 'method' }
end
end)
end
if loc.interface then -- for go using interface can output full method definition
locals.recurse_local_nodes(loc.interface, function(def, node, full_match, match)
local k, l, start = node:start()
trace(k, l, start, def, node, full_match, match, node:parent(), node:parent():start(), node:parent():type())
-- stylua: ignore start
trace( k, l, start, def, node, full_match, match, node:parent(), node:parent():start(), node:parent():type())
-- stylua: ignore end
if nodes_set[start] == nil then
nodes_set[start] = { node = node, type = match or "" }
nodes_set[start] = { node = node, type = match or '' }
end
end)
end
@ -76,7 +82,9 @@ local function get_definitions(bufnr)
locals.recurse_local_nodes(loc.reference, function(def, node, full_match, match)
local k, l, start = node:start()
trace(k, l, start, def, node, full_match, match, node:parent(), node:parent():start(), node:parent():type())
-- stylua: ignore start
trace( k, l, start, def, node, full_match, match, node:parent(), node:parent():start(), node:parent():type())
-- stylua: ignore end
if nodes_set[start] == nil then
-- if node:parent() and node:parent():type() == "field_declaration" then
-- nodes_set[start] = { node = node, type = match or "field" }
@ -85,11 +93,11 @@ local function get_definitions(bufnr)
if
node:parent()
and node:parent():parent()
and node:type() == "type_identifier"
and node:parent():type() == "qualified_type"
and string.find(node:parent():parent():type(), "interface")
and node:type() == 'type_identifier'
and node:parent():type() == 'qualified_type'
and string.find(node:parent():parent():type(), 'interface')
then
nodes_set[start] = { node = node, type = "interface" }
nodes_set[start] = { node = node, type = 'interface' }
end
end
end)
@ -110,11 +118,11 @@ end
-- a hack to treesitter-refactor plugin to return list node for outline
function M.list_definitions_toc(bufnr)
bufnr = bufnr or api.nvim_win_get_buf(api.nvim_get_current_win())
vim.api.nvim_buf_set_option(bufnr, "filetype", "go")
vim.api.nvim_buf_set_option(bufnr, "syntax", "enable")
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'go')
vim.api.nvim_buf_set_option(bufnr, 'syntax', 'enable')
log('get_definitions', bufnr)
local definitions = get_definitions(bufnr)
log("definitions: ", definitions)
log('definitions: ', definitions)
if #definitions < 1 then
log('unable to find definitions')
@ -126,9 +134,9 @@ function M.list_definitions_toc(bufnr)
-- Force some types to act like they are parents
-- instead of neighbors of the next nodes.
local containers = {
["function"] = true,
["type"] = true,
["method"] = true,
['function'] = true,
['type'] = true,
['method'] = true,
}
local parents = {}
@ -155,13 +163,13 @@ function M.list_definitions_toc(bufnr)
local lnum, col, _ = def.node:start()
local type = def.type
-- local kind = string.upper(def.type:sub(1, 1))
local symbol = get_node_text(def.node, bufnr) or ""
local symbol = get_node_text(def.node, bufnr) or ''
local text = symbol
local line_before = api.nvim_buf_get_lines(bufnr, lnum - 1, lnum, false)[1]
local hint = {}
if line_before and not line_before:find("^%s*//") then
if line_before and not line_before:find('^%s*//') then
hint = { line_before }
end
-- go pkg hack
@ -170,32 +178,32 @@ function M.list_definitions_toc(bufnr)
-- lets do a match if it is method
-- Note: the gopls can not find MethodName with MethodName, the format must be ReceviverType.MethodName
if type == "method" then
if type == 'method' then
local method_def = vim.fn.matchlist(line_text, regx)
if method_def == nil or method_def[3] == nil then
log("incorrect format", line_text, method_def)
log('incorrect format', line_text, method_def)
else
if method_def[4] ~= symbol then -- field 4 is Method name
log("please check regex", method_def, method_def[4], symbol)
log('please check regex', method_def, method_def[4], symbol)
end
if method_def[3] then -- field 3 is ReceiverType
symbol = method_def[3] .. "." .. symbol
symbol = method_def[3] .. '.' .. symbol
log(symbol)
end
end
end
if type == "var" and definitions[idx + 1] and definitions[idx + 1].type == "method" then
if type == 'var' and definitions[idx + 1] and definitions[idx + 1].type == 'method' then
-- we should remove receiver
local method_def = vim.fn.matchlist(line_text, regx)
if method_def ~= nil and util.trim(method_def[2]) == util.trim(symbol) then
log("we ignore var", symbol)
log('we ignore var', symbol)
goto continue
end
end
table.insert(hint, line_text)
for i = 1, 5 do
local line_after = api.nvim_buf_get_lines(bufnr, lnum + i, lnum + i + 1, false)[1]
if line_after and line_after:find("^%s*//") then
if line_after and line_after:find('^%s*//') then
table.insert(hint, line_after)
else
break

Loading…
Cancel
Save