Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename indexables to indexable_uris #2924

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exe/ruby-lsp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ if options[:doctor]

puts "Globbing for indexable files"

index.configuration.indexables.each do |uri|
index.configuration.indexable_uris.each do |uri|
puts "indexing: #{uri.full_path}"
index.index_single(uri)
end
Expand Down
6 changes: 3 additions & 3 deletions exe/ruby-lsp-check
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ puts "\n"
puts "Verifying that indexing executes successfully. This may take a while..."

index = RubyIndexer::Index.new
indexables = index.configuration.indexables
uris = index.configuration.indexable_uris

indexables.each_with_index do |uri, i|
uris.each_with_index do |uri, i|
index.index_single(uri)
rescue => e
errors[uri.full_path] = e
ensure
print("\033[M\033[0KIndexed #{i + 1}/#{indexables.length}") unless ENV["CI"]
print("\033[M\033[0KIndexed #{i + 1}/#{uris.length}") unless ENV["CI"]
end
puts "\n"

Expand Down
20 changes: 10 additions & 10 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def merged_excluded_file_pattern
end

sig { returns(T::Array[URI::Generic]) }
def indexables
def indexable_uris
excluded_gems = @excluded_gems - @included_gems
locked_gems = Bundler.locked_gems&.specs

Expand All @@ -107,12 +107,12 @@ def indexables
[included_path, relative_path]
end

indexables = T.let([], T::Array[URI::Generic])
uris = T.let([], T::Array[URI::Generic])

# Handle top level files separately. The path below is an optimization to prevent descending down directories that
# are going to be excluded anyway, so we need to handle top level scripts separately
Dir.glob(File.join(@workspace_path, "*.rb"), flags).each do |path|
indexables << URI::Generic.from_path(path: path)
uris << URI::Generic.from_path(path: path)
end

# Add user specified patterns
Expand All @@ -134,7 +134,7 @@ def indexables
load_path_entry = $LOAD_PATH.find { |load_path| path.start_with?(load_path) }
end

indexables << URI::Generic.from_path(path: path, load_path_entry: load_path_entry)
uris << URI::Generic.from_path(path: path, load_path_entry: load_path_entry)
end
end
end
Expand All @@ -150,7 +150,7 @@ def indexables
end

# Remove user specified patterns
indexables.reject! do |indexable|
uris.reject! do |indexable|
excluded_patterns.any? do |pattern|
File.fnmatch?(pattern, T.must(indexable.full_path), File::FNM_PATHNAME | File::FNM_EXTGLOB)
end
Expand Down Expand Up @@ -182,14 +182,14 @@ def indexables

if pathname.directory?
# If the default_path is a directory, we index all the Ruby files in it
indexables.concat(
uris.concat(
Dir.glob(File.join(default_path, "**", "*.rb"), File::FNM_PATHNAME | File::FNM_EXTGLOB).map! do |path|
URI::Generic.from_path(path: path, load_path_entry: RbConfig::CONFIG["rubylibdir"])
end,
)
elsif pathname.extname == ".rb"
# If the default_path is a Ruby file, we index it
indexables << URI::Generic.from_path(path: default_path, load_path_entry: RbConfig::CONFIG["rubylibdir"])
uris << URI::Generic.from_path(path: default_path, load_path_entry: RbConfig::CONFIG["rubylibdir"])
end
end

Expand All @@ -204,7 +204,7 @@ def indexables
# duplicates or accidentally ignoring exclude patterns
next if spec.full_gem_path == @workspace_path

indexables.concat(
uris.concat(
spec.require_paths.flat_map do |require_path|
load_path_entry = File.join(spec.full_gem_path, require_path)
Dir.glob(File.join(load_path_entry, "**", "*.rb")).map! do |path|
Expand All @@ -218,8 +218,8 @@ def indexables
# just ignore if they're missing
end

indexables.uniq!(&:to_s)
indexables
uris.uniq!(&:to_s)
uris
end

sig { returns(Regexp) }
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def resolve(name, nesting, seen_names = [])
block: T.nilable(T.proc.params(progress: Integer).returns(T::Boolean)),
).void
end
def index_all(uris: @configuration.indexables, &block)
vinistock marked this conversation as resolved.
Show resolved Hide resolved
def index_all(uris: @configuration.indexable_uris, &block)
# When troubleshooting an indexing issue, e.g. through irb, it's not obvious that `index_all` will augment the
# existing index values, meaning it may contain 'stale' entries. This check ensures that the user is aware of this
# behavior and can take appropriate action.
Expand Down
50 changes: 25 additions & 25 deletions lib/ruby_indexer/test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup

def test_load_configuration_executes_configure_block
@config.apply_config({ "excluded_patterns" => ["**/fixtures/**/*.rb"] })
uris = @config.indexables
uris = @config.indexable_uris

assert(uris.none? { |uri| uri.full_path.include?("test/fixtures") })
assert(uris.none? { |uri| uri.full_path.include?("minitest-reporters") })
Expand All @@ -22,16 +22,16 @@ def test_load_configuration_executes_configure_block
assert(uris.none? { |uri| uri.full_path == __FILE__ })
end

def test_indexables_have_expanded_full_paths
def test_indexable_uris_have_expanded_full_paths
@config.apply_config({ "included_patterns" => ["**/*.rb"] })
uris = @config.indexables
uris = @config.indexable_uris

# All paths should be expanded
assert(uris.all? { |uri| File.absolute_path?(uri.full_path) })
end

def test_indexables_only_includes_gem_require_paths
uris = @config.indexables
def test_indexable_uris_only_includes_gem_require_paths
uris = @config.indexable_uris

Bundler.locked_gems.specs.each do |lazy_spec|
next if lazy_spec.name == "ruby-lsp"
Expand All @@ -43,21 +43,21 @@ def test_indexables_only_includes_gem_require_paths
end
end

def test_indexables_does_not_include_default_gem_path_when_in_bundle
uris = @config.indexables
def test_indexable_uris_does_not_include_default_gem_path_when_in_bundle
uris = @config.indexable_uris
assert(uris.none? { |uri| uri.full_path.start_with?("#{RbConfig::CONFIG["rubylibdir"]}/psych") })
end

def test_indexables_includes_default_gems
paths = @config.indexables.map(&:full_path)
def test_indexable_uris_includes_default_gems
paths = @config.indexable_uris.map(&:full_path)

assert_includes(paths, "#{RbConfig::CONFIG["rubylibdir"]}/pathname.rb")
assert_includes(paths, "#{RbConfig::CONFIG["rubylibdir"]}/ipaddr.rb")
assert_includes(paths, "#{RbConfig::CONFIG["rubylibdir"]}/erb.rb")
end

def test_indexables_includes_project_files
paths = @config.indexables.map(&:full_path)
def test_indexable_uris_includes_project_files
paths = @config.indexable_uris.map(&:full_path)

Dir.glob("#{Dir.pwd}/lib/**/*.rb").each do |path|
next if path.end_with?("_test.rb")
Expand All @@ -66,39 +66,39 @@ def test_indexables_includes_project_files
end
end

def test_indexables_avoids_duplicates_if_bundle_path_is_inside_project
def test_indexable_uris_avoids_duplicates_if_bundle_path_is_inside_project
Bundler.settings.temporary(path: "vendor/bundle") do
config = Configuration.new

assert_includes(config.instance_variable_get(:@excluded_patterns), "vendor/bundle/**/*.rb")
end
end

def test_indexables_does_not_include_gems_own_installed_files
uris = @config.indexables
indexables_inside_bundled_lsp = uris.select do |uri|
def test_indexable_uris_does_not_include_gems_own_installed_files
uris = @config.indexable_uris
uris_inside_bundled_lsp = uris.select do |uri|
uri.full_path.start_with?(Bundler.bundle_path.join("gems", "ruby-lsp").to_s)
end

assert_empty(
indexables_inside_bundled_lsp,
"Indexables should not include files from the gem currently being worked on. " \
"Included: #{indexables_inside_bundled_lsp.map(&:full_path)}",
uris_inside_bundled_lsp,
"Indexable URIs should not include files from the gem currently being worked on. " \
"Included: #{uris_inside_bundled_lsp.map(&:full_path)}",
)
end

def test_indexables_does_not_include_non_ruby_files_inside_rubylibdir
def test_indexable_uris_does_not_include_non_ruby_files_inside_rubylibdir
path = Pathname.new(RbConfig::CONFIG["rubylibdir"]).join("extra_file.txt").to_s
FileUtils.touch(path)

uris = @config.indexables
uris = @config.indexable_uris
assert(uris.none? { |uri| uri.full_path == path })
ensure
FileUtils.rm(T.must(path))
end

def test_paths_are_unique
uris = @config.indexables
uris = @config.indexable_uris
assert_equal(uris.uniq.length, uris.length)
end

Expand Down Expand Up @@ -128,7 +128,7 @@ def test_magic_comments_regex
end
end

def test_indexables_respect_given_workspace_path
def test_indexable_uris_respect_given_workspace_path
Dir.mktmpdir do |dir|
FileUtils.mkdir(File.join(dir, "ignore"))
FileUtils.touch(File.join(dir, "ignore", "file0.rb"))
Expand All @@ -138,10 +138,10 @@ def test_indexables_respect_given_workspace_path
@config.apply_config({ "excluded_patterns" => ["ignore/**/*.rb"] })
@config.workspace_path = dir

uris = @config.indexables
uris = @config.indexable_uris
assert(uris.none? { |uri| uri.full_path.start_with?(File.join(dir, "ignore")) })

# After switching the workspace path, all indexables will be found in one of these places:
# After switching the workspace path, all indexable URIs will be found in one of these places:
# - The new workspace path
# - The Ruby LSP's own code (because Bundler is requiring the dependency from source)
# - Bundled gems
Expand All @@ -162,7 +162,7 @@ def test_includes_top_level_files
FileUtils.touch(File.join(dir, "find_me.rb"))
@config.workspace_path = dir

uris = @config.indexables
uris = @config.indexable_uris
assert(uris.find { |u| File.basename(u.full_path) == "find_me.rb" })
end
end
Expand Down