-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add find references support for constants
- Loading branch information
Showing
10 changed files
with
204 additions
and
12 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,111 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Requests | ||
# The | ||
# [references](https://microsoft.github.io/language-server-protocol/specification#textDocument_references) | ||
# request finds all references for the selected symbol. | ||
class References < Request | ||
extend T::Sig | ||
include Support::Common | ||
|
||
sig do | ||
params( | ||
global_state: GlobalState, | ||
store: Store, | ||
document: T.any(RubyDocument, ERBDocument), | ||
params: T::Hash[Symbol, T.untyped], | ||
).void | ||
end | ||
def initialize(global_state, store, document, params) | ||
super() | ||
@global_state = global_state | ||
@store = store | ||
@document = document | ||
@params = params | ||
@locations = T.let([], T::Array[Interface::Location]) | ||
end | ||
|
||
sig { override.returns(T::Array[Interface::Location]) } | ||
def perform | ||
position = @params[:position] | ||
char_position = @document.create_scanner.find_char_position(position) | ||
|
||
node_context = RubyDocument.locate( | ||
@document.parse_result.value, | ||
char_position, | ||
node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode], | ||
) | ||
target = node_context.node | ||
parent = node_context.parent | ||
return @locations unless target | ||
|
||
if target.is_a?(Prism::ConstantReadNode) && parent.is_a?(Prism::ConstantPathNode) | ||
target = determine_target( | ||
target, | ||
parent, | ||
position, | ||
) | ||
end | ||
|
||
target = T.cast( | ||
target, | ||
T.any(Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode), | ||
) | ||
|
||
name = constant_name(target) | ||
return @locations unless name | ||
|
||
entries = @global_state.index.resolve(name, node_context.nesting) | ||
return @locations unless entries | ||
|
||
fully_qualified_name = T.must(entries.first).name | ||
|
||
Dir.glob(File.join(@global_state.workspace_path, "**/*.rb")).each do |path| | ||
uri = URI::Generic.from_path(path: path) | ||
# If the document is being managed by the client, then we should use whatever is present in the store instead | ||
# of reading from disk | ||
next if @store.key?(uri) | ||
|
||
parse_result = Prism.parse_file(path) | ||
collect_references(fully_qualified_name, parse_result, uri) | ||
end | ||
|
||
@store.each do |_uri, document| | ||
collect_references(fully_qualified_name, document.parse_result, document.uri) | ||
end | ||
|
||
@locations | ||
end | ||
|
||
private | ||
|
||
sig do | ||
params( | ||
fully_qualified_name: String, | ||
parse_result: Prism::ParseResult, | ||
uri: URI::Generic, | ||
).void | ||
end | ||
def collect_references(fully_qualified_name, parse_result, uri) | ||
dispatcher = Prism::Dispatcher.new | ||
finder = RubyIndexer::ReferenceFinder.new( | ||
fully_qualified_name, | ||
@global_state.index, | ||
dispatcher, | ||
parse_result, | ||
include_declarations: @params.dig(:context, :includeDeclaration) || true, | ||
) | ||
dispatcher.visit(parse_result.value) | ||
|
||
finder.references.uniq(&:location).each do |reference| | ||
@locations << Interface::Location.new( | ||
uri: uri.to_s, | ||
range: range_from_location(reference.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
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,32 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class ReferencesTest < Minitest::Test | ||
def test_finds_constant_references | ||
refs = find_references("test/fixtures/rename.rb", { line: 0, character: 6 }).map do |ref| | ||
ref.range.start.line | ||
end | ||
|
||
assert_equal([0, 3], refs) | ||
end | ||
|
||
private | ||
|
||
def find_references(fixture_path, position) | ||
source = File.read(fixture_path) | ||
global_state = RubyLsp::GlobalState.new | ||
global_state.index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), source) | ||
|
||
store = RubyLsp::Store.new | ||
document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: URI::Generic.from_path(path: "/fake.rb")) | ||
|
||
RubyLsp::Requests::References.new( | ||
global_state, | ||
store, | ||
document, | ||
{ position: position }, | ||
).perform | ||
end | ||
end |