From 86453ced76881adf252897a484103b39ba3efb75 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Wed, 30 Aug 2023 13:21:30 -0400 Subject: [PATCH] Rename Indexable to IndexablePath --- .../lib/ruby_indexer/configuration.rb | 10 +++++----- lib/ruby_indexer/lib/ruby_indexer/index.rb | 6 +++--- lib/ruby_indexer/lib/ruby_indexer/indexable.rb | 2 +- .../test/classes_and_modules_test.rb | 2 +- lib/ruby_indexer/test/index_test.rb | 18 +++++++++--------- lib/ruby_indexer/test/test_case.rb | 2 +- lib/ruby_lsp/executor.rb | 6 ++++-- test/requests/definition_expectations_test.rb | 4 ++-- test/requests/hover_expectations_test.rb | 4 ++-- test/requests/workspace_symbol_test.rb | 12 ++++++------ 10 files changed, 34 insertions(+), 32 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb index 36baff2858..c0d47f9a9d 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/ruby_indexer/lib/ruby_indexer/index.rb b/lib/ruby_indexer/lib/ruby_indexer/index.rb index ea6a4d1b13..d83a64ff4f 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/index.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/index.rb @@ -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. @@ -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) diff --git a/lib/ruby_indexer/lib/ruby_indexer/indexable.rb b/lib/ruby_indexer/lib/ruby_indexer/indexable.rb index 91aba49796..32c37b0303 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/indexable.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/indexable.rb @@ -2,7 +2,7 @@ # frozen_string_literal: true module RubyIndexer - class Indexable + class IndexablePath extend T::Sig sig { returns(T.nilable(String)) } diff --git a/lib/ruby_indexer/test/classes_and_modules_test.rb b/lib/ruby_indexer/test/classes_and_modules_test.rb index d515012490..ed66967469 100644 --- a/lib/ruby_indexer/test/classes_and_modules_test.rb +++ b/lib/ruby_indexer/test/classes_and_modules_test.rb @@ -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 diff --git a/lib/ruby_indexer/test/index_test.rb b/lib/ruby_indexer/test/index_test.rb index 68d47e30f6..2b01274c22 100644 --- a/lib/ruby_indexer/test/index_test.rb +++ b/lib/ruby_indexer/test/index_test.rb @@ -6,11 +6,11 @@ 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 @@ -18,13 +18,13 @@ class Foo 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 @@ -32,13 +32,13 @@ class Foo 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 @@ -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 @@ -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 @@ -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 diff --git a/lib/ruby_indexer/test/test_case.rb b/lib/ruby_indexer/test/test_case.rb index 8ec4a4b9a7..c42306d063 100644 --- a/lib/ruby_indexer/test/test_case.rb +++ b/lib/ruby_indexer/test/test_case.rb @@ -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) diff --git a/lib/ruby_lsp/executor.rb b/lib/ruby_lsp/executor.rb index 52db7ab625..022139ee9e 100644 --- a/lib/ruby_lsp/executor.rb +++ b/lib/ruby_lsp/executor.rb @@ -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 @@ -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 diff --git a/test/requests/definition_expectations_test.rb b/test/requests/definition_expectations_test.rb index 97acae36d6..2109d359ba 100644 --- a/test/requests/definition_expectations_test.rb +++ b/test/requests/definition_expectations_test.rb @@ -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", @@ -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), ), diff --git a/test/requests/hover_expectations_test.rb b/test/requests/hover_expectations_test.rb index f0bb8f3ccb..e83b91f6e7 100644 --- a/test/requests/hover_expectations_test.rb +++ b/test/requests/hover_expectations_test.rb @@ -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 @@ -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, ) diff --git a/test/requests/workspace_symbol_test.rb b/test/requests/workspace_symbol_test.rb index 53ac224eda..818e130f8f 100644 --- a/test/requests/workspace_symbol_test.rb +++ b/test/requests/workspace_symbol_test.rb @@ -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 @@ -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 @@ -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", ) @@ -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 @@ -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 @@ -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)