Improve scopes sorter

This commit is contained in:
hrsh7th
2022-03-31 13:35:31 +09:00
parent a97d2f46f0
commit 3a1649297a

View File

@@ -177,14 +177,16 @@ compare.scopes = setmetatable({
-- Cursor scope. -- Cursor scope.
local cursor_scope = nil local cursor_scope = nil
for _, scope in ipairs(locals.get_scopes(buf)) do for _, scope in ipairs(locals.get_scopes(buf)) do
if not cursor_scope then if scope:start() <= cursor_row and cursor_row <= scope:end_() then
cursor_scope = scope if not cursor_scope then
else cursor_scope = scope
if scope:start() <= cursor_row and cursor_row <= scope:end_() then else
if math.abs(scope:end_() - scope:start()) < math.abs(cursor_scope:end_() - cursor_scope:start()) then if cursor_scope:start() <= scope:start() and scope:end_() <= cursor_scope:end_() then
cursor_scope = scope cursor_scope = scope
end end
end end
elseif cursor_scope and cursor_scope:end_() <= scope:start() then
break
end end
end end
@@ -194,12 +196,16 @@ compare.scopes = setmetatable({
-- Narrow definitions. -- Narrow definitions.
local depth = 0 local depth = 0
for scope in locals.iter_scope_tree(cursor_scope, buf) do for scope in locals.iter_scope_tree(cursor_scope, buf) do
local s, e = scope:start(), scope:end_()
-- Check scope's direct child. -- Check scope's direct child.
for _, definition in pairs(definitions) do for _, definition in pairs(definitions) do
if scope:id() == locals.containing_scope(definition.node, buf):id() then if s <= definition.node:start() and definition.node:end_() <= e then
local text = ts_utils.get_node_text(definition.node)[1] if scope:id() == locals.containing_scope(definition.node, buf):id() then
if not self.scopes_map[text] then local text = ts_utils.get_node_text(definition.node)[1]
self.scopes_map[text] = depth if not self.scopes_map[text] then
self.scopes_map[text] = depth
end
end end
end end
end end