feat(refactor): add smart rename module

This commit is contained in:
Steven Sojka 2020-06-26 11:11:21 -05:00 committed by Kiyan Yazdani
parent 058e8d2296
commit 64838e51c0
5 changed files with 115 additions and 24 deletions

View file

@ -15,14 +15,16 @@ function M.get_node_text(node, bufnr)
-- We have to remember that end_col is end-exclusive
local start_row, start_col, end_row, end_col = node:range()
if start_row ~= end_row then
local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row+1, false)
lines[1] = string.sub(lines[1], start_col+1)
lines[#lines] = string.sub(lines[#lines], 1, end_col)
return lines
else
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row+1, true)[1]
return { string.sub(line, start_col+1, end_col) }
local line = api.nvim_buf_get_lines(bufnr, start_row, start_row+1, false)[1]
-- If line is nil then the line is empty
return line and { string.sub(line, start_col+1, end_col) } or {}
end
end
@ -263,4 +265,26 @@ function M.get_local_nodes(local_def)
end
end
-- Finds usages of a node in a particula scope
-- @param node the node to find usages for
-- @param scope_node the node to look within
-- @returns a list of nodes
function M.find_usages(node, scope_node)
local usages = {}
local node_text = M.get_node_text(node)[1]
if not node_text or #node_text < 1 then return {} end
for _, def in ipairs(locals.collect_locals(bufnr, scope_node)) do
if def.reference
and def.reference.node
and M.get_node_text(def.reference.node)[1] == node_text then
table.insert(usages, def.reference.node)
end
end
return usages
end
return M