lib/ruby_lsp/requests/base_request.rb in ruby-lsp-0.4.1 vs lib/ruby_lsp/requests/base_request.rb in ruby-lsp-0.4.2
- old
+ new
@@ -26,10 +26,18 @@
end
sig { abstract.returns(Object) }
def run; end
+ # Syntax Tree implements `visit_all` using `map` instead of `each` for users who want to use the pattern
+ # `result = visitor.visit(tree)`. However, we don't use that pattern and should avoid producing a new array for
+ # every single node visited
+ sig { params(nodes: T::Array[SyntaxTree::Node]).void }
+ def visit_all(nodes)
+ nodes.each { |node| visit(node) }
+ end
+
sig { params(node: SyntaxTree::Node).returns(LanguageServer::Protocol::Interface::Range) }
def range_from_syntax_tree_node(node)
loc = node.location
LanguageServer::Protocol::Interface::Range.new(
@@ -87,17 +95,18 @@
next unless (loc.start_char...loc.end_char).cover?(position)
# If the node's start character is already past the position, then we should've found the closest node already
break if position < loc.start_char
+ # If there are node types to filter by, and the current node is not one of those types, then skip it
+ next if node_types.any? && node_types.none? { |type| candidate.is_a?(type) }
+
# If the current node is narrower than or equal to the previous closest node, then it is more precise
closest_loc = closest.location
if loc.end_char - loc.start_char <= closest_loc.end_char - closest_loc.start_char
parent = T.let(closest, SyntaxTree::Node)
closest = candidate
end
-
- break if node_types.any? { |type| candidate.is_a?(type) }
end
[closest, parent]
end