Skip to content

Commit

Permalink
Rename Indexable to IndexablePath
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Aug 30, 2023
1 parent 5a494b9 commit 86453ce
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 32 deletions.
10 changes: 5 additions & 5 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def load_config
raise e, "Syntax error while loading .index.yml configuration: #{e.message}"
end

sig { returns(T::Array[Indexable]) }
sig { returns(T::Array[IndexablePath]) }
def indexables
excluded_gems = @excluded_gems - @included_gems
locked_gems = Bundler.locked_gems&.specs
Expand All @@ -73,7 +73,7 @@ def indexables
indexables = @included_patterns.flat_map do |pattern|
Dir.glob(pattern, File::FNM_PATHNAME | File::FNM_EXTGLOB).map! do |path|
base_path = $LOAD_PATH.find { |load_path| path.start_with?(load_path) }
Indexable.new(base_path, path)
IndexablePath.new(base_path, path)
end
end

Expand Down Expand Up @@ -108,12 +108,12 @@ def indexables
# If the default_path is a directory, we index all the Ruby files in it
indexables.concat(
Dir.glob(File.join(default_path, "**", "*.rb"), File::FNM_PATHNAME | File::FNM_EXTGLOB).map! do |path|
Indexable.new(RbConfig::CONFIG["rubylibdir"], path)
IndexablePath.new(RbConfig::CONFIG["rubylibdir"], path)
end,
)
else
# If the default_path is a Ruby file, we index it
indexables << Indexable.new(RbConfig::CONFIG["rubylibdir"], default_path)
indexables << IndexablePath.new(RbConfig::CONFIG["rubylibdir"], default_path)
end
end

Expand All @@ -131,7 +131,7 @@ def indexables
indexables.concat(
spec.require_paths.flat_map do |require_path|
base_path = File.join(spec.full_gem_path, require_path)
Dir.glob(File.join(base_path, "**", "*.rb")).map! { |path| Indexable.new(base_path, path) }
Dir.glob(File.join(base_path, "**", "*.rb")).map! { |path| IndexablePath.new(base_path, path) }
end,
)
rescue Gem::MissingSpecError
Expand Down
6 changes: 3 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize
@files_to_entries = T.let({}, T::Hash[String, T::Array[Entry]])
end

sig { params(indexable: Indexable).void }
sig { params(indexable: IndexablePath).void }
def delete(indexable)
# For each constant discovered in `path`, delete the associated entry from the index. If there are no entries
# left, delete the constant from the index.
Expand Down Expand Up @@ -85,12 +85,12 @@ def resolve(name, nesting)
nil
end

sig { params(indexables: T::Array[Indexable]).void }
sig { params(indexables: T::Array[IndexablePath]).void }
def index_all(indexables: RubyIndexer.configuration.indexables)
indexables.each { |indexable| index_single(indexable) }
end

sig { params(indexable: Indexable, source: T.nilable(String)).void }
sig { params(indexable: IndexablePath, source: T.nilable(String)).void }
def index_single(indexable, source = nil)
content = source || File.read(indexable.full_path)
visitor = IndexVisitor.new(self, YARP.parse(content), indexable.full_path)
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/lib/ruby_indexer/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

module RubyIndexer
class Indexable
class IndexablePath
extend T::Sig

sig { returns(T.nilable(String)) }
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/test/classes_and_modules_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Foo

assert_entry("Foo", Index::Entry::Class, "/fake/path/foo.rb:0-0:1-2")

@index.delete(Indexable.new(nil, "/fake/path/foo.rb"))
@index.delete(IndexablePath.new(nil, "/fake/path/foo.rb"))
refute_entry("Foo")
assert_empty(@index.instance_variable_get(:@files_to_entries))
end
Expand Down
18 changes: 9 additions & 9 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@
module RubyIndexer
class IndexTest < TestCase
def test_deleting_one_entry_for_a_class
@index.index_single(Indexable.new(nil, "/fake/path/foo.rb"), <<~RUBY)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
class Foo
end
RUBY
@index.index_single(Indexable.new(nil, "/fake/path/other_foo.rb"), <<~RUBY)
@index.index_single(IndexablePath.new(nil, "/fake/path/other_foo.rb"), <<~RUBY)
class Foo
end
RUBY

entries = @index["Foo"]
assert_equal(2, entries.length)

@index.delete(Indexable.new(nil, "/fake/path/other_foo.rb"))
@index.delete(IndexablePath.new(nil, "/fake/path/other_foo.rb"))
entries = @index["Foo"]
assert_equal(1, entries.length)
end

def test_deleting_all_entries_for_a_class
@index.index_single(Indexable.new(nil, "/fake/path/foo.rb"), <<~RUBY)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
class Foo
end
RUBY

entries = @index["Foo"]
assert_equal(1, entries.length)

@index.delete(Indexable.new(nil, "/fake/path/foo.rb"))
@index.delete(IndexablePath.new(nil, "/fake/path/foo.rb"))
entries = @index["Foo"]
assert_nil(entries)
end

def test_index_resolve
@index.index_single(Indexable.new(nil, "/fake/path/foo.rb"), <<~RUBY)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
class Bar; end
module Foo
Expand Down Expand Up @@ -72,7 +72,7 @@ class Something
end

def test_accessing_with_colon_colon_prefix
@index.index_single(Indexable.new(nil, "/fake/path/foo.rb"), <<~RUBY)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
class Bar; end
module Foo
Expand All @@ -92,7 +92,7 @@ class Something
end

def test_fuzzy_search
@index.index_single(Indexable.new(nil, "/fake/path/foo.rb"), <<~RUBY)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), <<~RUBY)
class Bar; end
module Foo
Expand Down Expand Up @@ -121,7 +121,7 @@ class Something

def test_index_single_ignores_directories
FileUtils.mkdir("lib/this_is_a_dir.rb")
@index.index_single(Indexable.new(nil, "lib/this_is_a_dir.rb"))
@index.index_single(IndexablePath.new(nil, "lib/this_is_a_dir.rb"))
ensure
FileUtils.rm_r("lib/this_is_a_dir.rb")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/test/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup
private

def index(source)
@index.index_single(Indexable.new(nil, "/fake/path/foo.rb"), source)
@index.index_single(IndexablePath.new(nil, "/fake/path/foo.rb"), source)
end

def assert_entry(expected_name, type, expected_location)
Expand Down
6 changes: 4 additions & 2 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def did_change_watched_files(changes)
next if file_path.nil? || File.directory?(file_path)

base_path = $LOAD_PATH.find { |load_path| file_path.start_with?(load_path) }
indexable = RubyIndexer::Indexable.new(base_path, file_path)
indexable = RubyIndexer::IndexablePath.new(base_path, file_path)

case change[:type]
when Constant::FileChangeType::CREATED
Expand Down Expand Up @@ -300,7 +300,9 @@ def hover(uri, position)
hover.response
end

sig { params(uri: URI::Generic, content_changes: T::Array[Document::EditShape], version: Integer).returns(Object) }
sig do
params(uri: URI::Generic, content_changes: T::Array[Document::EditShape], version: Integer).returns(Object)
end
def text_document_did_change(uri, content_changes, version)
@store.push_edits(uri: uri, edits: content_changes, version: version)
VOID
Expand Down
4 changes: 2 additions & 2 deletions test/requests/definition_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_expectations(source)
executor = RubyLsp::Executor.new(store, message_queue)

executor.instance_variable_get(:@index).index_single(
RubyIndexer::Indexable.new(
RubyIndexer::IndexablePath.new(
nil,
File.expand_path(
"../../lib/ruby_lsp/event_emitter.rb",
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_jumping_to_default_gems

executor = RubyLsp::Executor.new(store, message_queue)
executor.instance_variable_get(:@index).index_single(
RubyIndexer::Indexable.new(
RubyIndexer::IndexablePath.new(
nil,
T.must(uri.to_standardized_path),
),
Expand Down
4 changes: 2 additions & 2 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def run_expectations(source)

executor = RubyLsp::Executor.new(store, message_queue)
index = executor.instance_variable_get(:@index)
index.index_single(RubyIndexer::Indexable.new(nil, T.must(uri.to_standardized_path)), source)
index.index_single(RubyIndexer::IndexablePath.new(nil, T.must(uri.to_standardized_path)), source)

begin
# We need to pretend that Sorbet is not a dependency or else we can't properly test
Expand Down Expand Up @@ -50,7 +50,7 @@ class Post

executor = RubyLsp::Executor.new(store, message_queue)
executor.instance_variable_get(:@index).index_single(
RubyIndexer::Indexable.new(nil, T.must(uri.to_standardized_path)),
RubyIndexer::IndexablePath.new(nil, T.must(uri.to_standardized_path)),
source,
)

Expand Down
12 changes: 6 additions & 6 deletions test/requests/workspace_symbol_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def teardown
end

def test_returns_index_entries_based_on_query
@index.index_single(RubyIndexer::Indexable.new(nil, "/fake.rb"), <<~RUBY)
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
class Foo; end
module Bar; end
Expand All @@ -35,7 +35,7 @@ module Bar; end
end

def test_fuzzy_matches_symbols
@index.index_single(RubyIndexer::Indexable.new(nil, "/fake.rb"), <<~RUBY)
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
class Foo; end
module Bar; end
Expand All @@ -57,7 +57,7 @@ module Bar; end

def test_matches_only_gem_symbols_if_typechecker_is_present
RubyLsp::DependencyDetector.const_set(:HAS_TYPECHECKER, true)
indexable = RubyIndexer::Indexable.new(
indexable = RubyIndexer::IndexablePath.new(
nil,
"#{RubyLsp::WORKSPACE_URI.to_standardized_path}/workspace_symbol_foo.rb",
)
Expand All @@ -67,7 +67,7 @@ class Foo; end
RUBY

path = "#{Bundler.bundle_path}/gems/fake-gem-0.1.0/lib/gem_symbol_foo.rb"
@index.index_single(RubyIndexer::Indexable.new(nil, path), <<~RUBY)
@index.index_single(RubyIndexer::IndexablePath.new(nil, path), <<~RUBY)
class Foo; end
RUBY

Expand All @@ -77,7 +77,7 @@ class Foo; end
end

def test_symbols_include_container_name
@index.index_single(RubyIndexer::Indexable.new(nil, "/fake.rb"), <<~RUBY)
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
module Foo
class Bar; end
end
Expand All @@ -90,7 +90,7 @@ class Bar; end
end

def test_finds_default_gem_symbols
@index.index_single(RubyIndexer::Indexable.new(nil, "#{RbConfig::CONFIG["rubylibdir"]}/pathname.rb"))
@index.index_single(RubyIndexer::IndexablePath.new(nil, "#{RbConfig::CONFIG["rubylibdir"]}/pathname.rb"))

result = RubyLsp::Requests::WorkspaceSymbol.new("Pathname", @index).run
refute_empty(result)
Expand Down

0 comments on commit 86453ce

Please sign in to comment.