Skip to content

Commit

Permalink
Switch to storing URIs in entries instead of file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jul 22, 2024
1 parent 5f8fb1f commit 0d47a2d
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 95 deletions.
34 changes: 17 additions & 17 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class DeclarationListener
BASIC_OBJECT_NESTING = T.let(["BasicObject"].freeze, T::Array[String])

sig do
params(index: Index, dispatcher: Prism::Dispatcher, parse_result: Prism::ParseResult, file_path: String).void
params(index: Index, dispatcher: Prism::Dispatcher, parse_result: Prism::ParseResult, uri: URI::Generic).void
end
def initialize(index, dispatcher, parse_result, file_path)
def initialize(index, dispatcher, parse_result, uri)
@index = index
@file_path = file_path
@uri = uri
@visibility_stack = T.let([Entry::Visibility::PUBLIC], T::Array[Entry::Visibility])
@comments_by_line = T.let(
parse_result.comments.to_h do |c|
Expand Down Expand Up @@ -92,7 +92,7 @@ def on_class_node_enter(node)

entry = Entry::Class.new(
nesting,
@file_path,
@uri,
node.location,
constant_path.location,
comments,
Expand All @@ -119,7 +119,7 @@ def on_module_node_enter(node)

comments = collect_comments(node)

entry = Entry::Module.new(actual_nesting(name), @file_path, node.location, constant_path.location, comments)
entry = Entry::Module.new(actual_nesting(name), @uri, node.location, constant_path.location, comments)

@owner_stack << entry
@index.add(entry)
Expand Down Expand Up @@ -151,7 +151,7 @@ def on_singleton_class_node_enter(node)
else
entry = Entry::SingletonClass.new(
@stack,
@file_path,
@uri,
node.location,
expression.location,
collect_comments(node),
Expand Down Expand Up @@ -304,7 +304,7 @@ def on_def_node_enter(node)
when nil
@index.add(Entry::Method.new(
method_name,
@file_path,
@uri,
node.location,
node.name_loc,
comments,
Expand All @@ -320,7 +320,7 @@ def on_def_node_enter(node)

@index.add(Entry::Method.new(
method_name,
@file_path,
@uri,
node.location,
node.name_loc,
comments,
Expand Down Expand Up @@ -379,7 +379,7 @@ def on_alias_method_node_enter(node)
method_name,
node.old_name.slice,
@owner_stack.last,
@file_path,
@uri,
node.new_name.location,
comments,
),
Expand Down Expand Up @@ -412,7 +412,7 @@ def handle_instance_variable(node, loc)
owner = @index.existing_or_new_singleton_class(owner.name)
end

@index.add(Entry::InstanceVariable.new(name, @file_path, loc, collect_comments(node), owner))
@index.add(Entry::InstanceVariable.new(name, @uri, loc, collect_comments(node), owner))
end

sig { params(node: Prism::CallNode).void }
Expand Down Expand Up @@ -472,7 +472,7 @@ def handle_alias_method(node)
new_name_value,
old_name_value,
@owner_stack.last,
@file_path,
@uri,
new_name.location,
comments,
),
Expand Down Expand Up @@ -504,19 +504,19 @@ def add_constant(node, name, value = nil)
@index.add(
case value
when Prism::ConstantReadNode, Prism::ConstantPathNode
Entry::UnresolvedAlias.new(value.slice, @stack.dup, name, @file_path, node.location, comments)
Entry::UnresolvedAlias.new(value.slice, @stack.dup, name, @uri, node.location, comments)
when Prism::ConstantWriteNode, Prism::ConstantAndWriteNode, Prism::ConstantOrWriteNode,
Prism::ConstantOperatorWriteNode

# If the right hand side is another constant assignment, we need to visit it because that constant has to be
# indexed too
Entry::UnresolvedAlias.new(value.name.to_s, @stack.dup, name, @file_path, node.location, comments)
Entry::UnresolvedAlias.new(value.name.to_s, @stack.dup, name, @uri, node.location, comments)
when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathOperatorWriteNode,
Prism::ConstantPathAndWriteNode

Entry::UnresolvedAlias.new(value.target.slice, @stack.dup, name, @file_path, node.location, comments)
Entry::UnresolvedAlias.new(value.target.slice, @stack.dup, name, @uri, node.location, comments)
else
Entry::Constant.new(name, @file_path, node.location, comments)
Entry::Constant.new(name, @uri, node.location, comments)
end,
)
end
Expand Down Expand Up @@ -576,14 +576,14 @@ def handle_attribute(node, reader:, writer:)
next unless name && loc

if reader
@index.add(Entry::Accessor.new(name, @file_path, loc, comments, current_visibility, @owner_stack.last))
@index.add(Entry::Accessor.new(name, @uri, loc, comments, current_visibility, @owner_stack.last))
end

next unless writer

@index.add(Entry::Accessor.new(
"#{name}=",
@file_path,
@uri,
loc,
comments,
current_visibility,
Expand Down
75 changes: 46 additions & 29 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Visibility < T::Enum
sig { returns(String) }
attr_reader :name

sig { returns(String) }
attr_reader :file_path
sig { returns(URI::Generic) }
attr_reader :uri

sig { returns(RubyIndexer::Location) }
attr_reader :location
Expand All @@ -33,14 +33,14 @@ class Visibility < T::Enum
sig do
params(
name: String,
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
comments: T::Array[String],
).void
end
def initialize(name, file_path, location, comments)
def initialize(name, uri, location, comments)
@name = name
@file_path = file_path
@uri = uri
@comments = comments
@visibility = T.let(Visibility::PUBLIC, Visibility)

Expand Down Expand Up @@ -76,7 +76,24 @@ def private?

sig { returns(String) }
def file_name
File.basename(@file_path)
path = @uri.to_standardized_path
# For unsaved files, the name is a part of the URI's opaque `untitled:Untitled-1`
return T.must(@uri.opaque) unless path

File.basename(path)
end

# Returns the entry's URI including the range of the declaration as part of the URI's fragment
sig { returns(URI::Generic) }
def declaration_uri
# We always handle locations as zero based. However, for file links in Markdown we need them to be one
# based, which is why instead of the usual subtraction of 1 to line numbers, we are actually adding 1 to
# columns. The format for VS Code file URIs is
# `file:///path/to/file.rb#Lstart_line,start_column-end_line,end_column`
uri = @uri.dup
uri.fragment = "L#{@location.start_line},#{@location.start_column + 1}-" \
"#{@location.end_line},#{@location.end_column + 1}"
uri
end

class ModuleOperation
Expand Down Expand Up @@ -113,18 +130,18 @@ class Namespace < Entry
sig do
params(
nesting: T::Array[String],
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
name_location: T.any(Prism::Location, Location),
comments: T::Array[String],
).void
end
def initialize(nesting, file_path, location, name_location, comments)
def initialize(nesting, uri, location, name_location, comments)
@name = T.let(nesting.join("::"), String)
# The original nesting where this namespace was discovered
@nesting = nesting

super(@name, file_path, location, comments)
super(@name, uri, location, comments)

@name_location = T.let(
if name_location.is_a?(Prism::Location)
Expand Down Expand Up @@ -174,15 +191,15 @@ class Class < Namespace
sig do
params(
nesting: T::Array[String],
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
name_location: T.any(Prism::Location, Location),
comments: T::Array[String],
parent_class: T.nilable(String),
).void
end
def initialize(nesting, file_path, location, name_location, comments, parent_class) # rubocop:disable Metrics/ParameterLists
super(nesting, file_path, location, name_location, comments)
def initialize(nesting, uri, location, name_location, comments, parent_class) # rubocop:disable Metrics/ParameterLists
super(nesting, uri, location, name_location, comments)
@parent_class = parent_class
end

Expand Down Expand Up @@ -319,15 +336,15 @@ def parameters
sig do
params(
name: String,
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
comments: T::Array[String],
visibility: Visibility,
owner: T.nilable(Entry::Namespace),
).void
end
def initialize(name, file_path, location, comments, visibility, owner) # rubocop:disable Metrics/ParameterLists
super(name, file_path, location, comments)
def initialize(name, uri, location, comments, visibility, owner) # rubocop:disable Metrics/ParameterLists
super(name, uri, location, comments)
@visibility = visibility
@owner = owner
end
Expand Down Expand Up @@ -373,7 +390,7 @@ class Method < Member
sig do
params(
name: String,
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
name_location: T.any(Prism::Location, Location),
comments: T::Array[String],
Expand All @@ -382,8 +399,8 @@ class Method < Member
owner: T.nilable(Entry::Namespace),
).void
end
def initialize(name, file_path, location, name_location, comments, signatures, visibility, owner) # rubocop:disable Metrics/ParameterLists
super(name, file_path, location, comments, visibility, owner)
def initialize(name, uri, location, name_location, comments, signatures, visibility, owner) # rubocop:disable Metrics/ParameterLists
super(name, uri, location, comments, visibility, owner)
@signatures = signatures
@name_location = T.let(
if name_location.is_a?(Prism::Location)
Expand Down Expand Up @@ -425,13 +442,13 @@ class UnresolvedAlias < Entry
target: String,
nesting: T::Array[String],
name: String,
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
comments: T::Array[String],
).void
end
def initialize(target, nesting, name, file_path, location, comments) # rubocop:disable Metrics/ParameterLists
super(name, file_path, location, comments)
def initialize(target, nesting, name, uri, location, comments) # rubocop:disable Metrics/ParameterLists
super(name, uri, location, comments)

@target = target
@nesting = nesting
Expand All @@ -447,7 +464,7 @@ class Alias < Entry

sig { params(target: String, unresolved_alias: UnresolvedAlias).void }
def initialize(target, unresolved_alias)
super(unresolved_alias.name, unresolved_alias.file_path, unresolved_alias.location, unresolved_alias.comments)
super(unresolved_alias.name, unresolved_alias.uri, unresolved_alias.location, unresolved_alias.comments)

@visibility = unresolved_alias.visibility
@target = target
Expand All @@ -462,14 +479,14 @@ class InstanceVariable < Entry
sig do
params(
name: String,
file_path: String,
uri: URI::Generic,
location: T.any(Prism::Location, RubyIndexer::Location),
comments: T::Array[String],
owner: T.nilable(Entry::Namespace),
).void
end
def initialize(name, file_path, location, comments, owner)
super(name, file_path, location, comments)
def initialize(name, uri, location, comments, owner)
super(name, uri, location, comments)
@owner = owner
end
end
Expand All @@ -491,13 +508,13 @@ class UnresolvedMethodAlias < Entry
new_name: String,
old_name: String,
owner: T.nilable(Entry::Namespace),
file_path: String,
uri: URI::Generic,
location: Prism::Location,
comments: T::Array[String],
).void
end
def initialize(new_name, old_name, owner, file_path, location, comments) # rubocop:disable Metrics/ParameterLists
super(new_name, file_path, location, comments)
def initialize(new_name, old_name, owner, uri, location, comments) # rubocop:disable Metrics/ParameterLists
super(new_name, uri, location, comments)

@new_name = new_name
@old_name = old_name
Expand All @@ -524,7 +541,7 @@ def initialize(target, unresolved_alias)

super(
unresolved_alias.new_name,
unresolved_alias.file_path,
unresolved_alias.uri,
unresolved_alias.location,
full_comments,
)
Expand Down
Loading

0 comments on commit 0d47a2d

Please sign in to comment.