-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support go-to-definition for let(!) and subject declaration
- Loading branch information
Showing
5 changed files
with
372 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module RSpec | ||
class Definition | ||
extend T::Sig | ||
|
||
include ::RubyLsp::Requests::Support::Common | ||
|
||
sig do | ||
params( | ||
response_builder: ResponseBuilders::CollectionResponseBuilder[T.any( | ||
Interface::Location, | ||
Interface::LocationLink, | ||
)], | ||
uri: URI::Generic, | ||
node_context: NodeContext, | ||
index: RubyIndexer::Index, | ||
dispatcher: Prism::Dispatcher, | ||
).void | ||
end | ||
def initialize(response_builder, uri, node_context, index, dispatcher) | ||
@response_builder = response_builder | ||
@uri = uri | ||
@node_context = node_context | ||
@index = index | ||
dispatcher.register(self, :on_call_node_enter) | ||
end | ||
|
||
sig { params(node: Prism::CallNode).void } | ||
def on_call_node_enter(node) | ||
message = node.message | ||
return unless message | ||
|
||
return if @node_context.locals_for_scope.include?(message) | ||
|
||
entries = @index[message] | ||
return unless entries | ||
return if entries.empty? | ||
|
||
entries.each do |entry| | ||
# Technically, let can be defined in a different file, but we're not going to handle that case yet | ||
next unless entry.file_path == @uri.to_standardized_path | ||
|
||
@response_builder << Interface::LocationLink.new( | ||
target_uri: URI::Generic.from_path(path: entry.file_path).to_s, | ||
target_range: range_from_location(entry.location), | ||
target_selection_range: range_from_location(entry.name_location), | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module RSpec | ||
class IndexingEnhancement | ||
extend T::Sig | ||
include RubyIndexer::Enhancement | ||
|
||
sig do | ||
override.params( | ||
index: RubyIndexer::Index, | ||
owner: T.nilable(RubyIndexer::Entry::Namespace), | ||
node: Prism::CallNode, | ||
file_path: String, | ||
).void | ||
end | ||
def on_call_node(index, owner, node, file_path) | ||
return if node.receiver | ||
|
||
name = node.name | ||
|
||
case name | ||
when :let, :let! | ||
block_node = node.block | ||
return unless block_node | ||
|
||
arguments = node.arguments | ||
return unless arguments | ||
|
||
return if arguments.arguments.count != 1 | ||
|
||
method_name_node = T.must(arguments.arguments.first) | ||
|
||
method_name = case method_name_node | ||
when Prism::StringNode | ||
method_name_node.slice | ||
when Prism::SymbolNode | ||
method_name_node.unescaped | ||
end | ||
|
||
return unless method_name | ||
|
||
index.add(RubyIndexer::Entry::Method.new( | ||
method_name, | ||
file_path, | ||
block_node.location, | ||
block_node.location, | ||
nil, | ||
index.configuration.encoding, | ||
[RubyIndexer::Entry::Signature.new([])], | ||
RubyIndexer::Entry::Visibility::PUBLIC, | ||
owner, | ||
)) | ||
when :subject, :subject! | ||
block_node = node.block | ||
return unless block_node | ||
|
||
arguments = node.arguments | ||
|
||
if arguments && arguments.arguments.count == 1 | ||
method_name_node = T.must(arguments.arguments.first) | ||
end | ||
|
||
method_name = if method_name_node | ||
case method_name_node | ||
when Prism::StringNode | ||
method_name_node.slice | ||
when Prism::SymbolNode | ||
method_name_node.unescaped | ||
end | ||
else | ||
"subject" | ||
end | ||
|
||
return unless method_name | ||
|
||
index.add(RubyIndexer::Entry::Method.new( | ||
method_name, | ||
file_path, | ||
block_node.location, | ||
block_node.location, | ||
nil, | ||
index.configuration.encoding, | ||
[RubyIndexer::Entry::Signature.new([])], | ||
RubyIndexer::Entry::Visibility::PUBLIC, | ||
owner, | ||
)) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.