Skip to content

Commit

Permalink
Improve search efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
jenny-codes committed Jan 22, 2025
1 parent 187b898 commit b362347
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions lib/ruby_lsp/requests/goto_relevant_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class GotoRelevantFile < Request
sig { params(path: String).void }
def initialize(path)
super()
@path = T.let(path.delete_prefix(Bundler.root.to_s), String)

@workspace_path = T.let(Dir.pwd, String)
@path = T.let(path.delete_prefix(@workspace_path), String)
end

sig { override.returns(T::Array[String]) }
def perform
filename = relevant_filename_pattern(@path)
recursively_find_paths(File.join(File.dirname(@path), filename))
search_pattern = relevant_path_pattern(@path)
recursively_find_paths(search_pattern)
end

private
Expand All @@ -29,35 +31,30 @@ def recursively_find_paths(path)
pattern = File.join("**", path)
matches = Dir.glob(pattern)
if matches.any?
matches.map { File.join(Bundler.root.to_s, _1) }
matches.map { File.join(@workspace_path, _1) }
elsif File.dirname(path) == "."
[]
else
new_path = exclude_leading_directory(path)
new_path = path.sub(%r{\A/?[^/]+/}, "")
recursively_find_paths(new_path)
end
end

sig { params(path: String).returns(String) }
def exclude_leading_directory(path)
parts = path.split(File::SEPARATOR)
if parts.one?
T.must(parts.first)
else
T.must(parts[1..]).join(File::SEPARATOR)
end
end

sig { params(path: String).returns(String) }
def relevant_filename_pattern(path)
def relevant_path_pattern(path)
dir = File.dirname(path)
basename = File.basename(path, File.extname(path))
result = if basename.end_with?("test") || basename.end_with?("spec")
basename.gsub(/(.test|_test|_spec|-spec|expections_test|integration_test)$/, "")

new_dir, new_basename = if basename.end_with?("test") || basename.end_with?("spec")
[
dir.gsub(%r{(test/|unit/|integration/|spec/)}, ""),
basename.gsub(/(.test|_test|_spec|-spec|expections_test|integration_test)$/, ""),
]
else
"#{basename}{.test,_test,_spec,-spec,_expectations_test,_integration_test}"
[dir, "#{basename}{.test,_test,_spec,-spec,_expectations_test,_integration_test}"]
end

"#{result}#{File.extname(path)}"
File.join(new_dir, "#{new_basename}#{File.extname(path)}")
end
end
end
Expand Down

0 comments on commit b362347

Please sign in to comment.