lib/ruby_lsp/requests/base_request.rb in ruby-lsp-0.3.8 vs lib/ruby_lsp/requests/base_request.rb in ruby-lsp-0.4.0
- old
+ new
@@ -8,12 +8,16 @@
extend T::Sig
extend T::Helpers
abstract!
- sig { params(document: Document).void }
- def initialize(document)
+ # We must accept rest keyword arguments here, so that the argument count matches when
+ # SyntaxTree::WithScope#initialize invokes `super` for Sorbet. We don't actually use these parameters for
+ # anything. We can remove these arguments once we drop support for Ruby 2.7
+ # https://github.com/ruby-syntax-tree/syntax_tree/blob/4dac90b53df388f726dce50ce638a1ba71cc59f8/lib/syntax_tree/with_scope.rb#L122
+ sig { params(document: Document, _kwargs: T.untyped).void }
+ def initialize(document, **_kwargs)
@document = document
# Parsing the document here means we're taking a lazy approach by only doing it when the first feature request
# is received by the server. This happens because {Document#parse} remembers if there are new edits to be parsed
@document.parse
@@ -60,13 +64,14 @@
sig do
params(
node: SyntaxTree::Node,
position: Integer,
+ node_types: T::Array[T.class_of(SyntaxTree::Node)],
).returns([T.nilable(SyntaxTree::Node), T.nilable(SyntaxTree::Node)])
end
- def locate(node, position)
+ def locate(node, position, node_types: [])
queue = T.let(node.child_nodes.compact, T::Array[T.nilable(SyntaxTree::Node)])
closest = node
until queue.empty?
candidate = queue.shift
@@ -88,9 +93,11 @@
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