Skip to content

Commit a871787

Browse files
committed
fixup! Add experimental/gotoRelevantFile lsp method
1 parent c2a6a8b commit a871787

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

lib/ruby_lsp/requests/goto_relevant_file.rb

+33-29
Original file line numberDiff line numberDiff line change
@@ -12,56 +12,60 @@ class GotoRelevantFile < Request
1212

1313
TEST_KEYWORDS = ["test", "spec", "integration_test"]
1414

15-
sig { params(path: String).void }
16-
def initialize(path)
15+
TEST_PREFIX_PATTERN = /^(#{TEST_KEYWORDS.join("_|")}_)/
16+
TEST_SUFFIX_PATTERN = /(_#{TEST_KEYWORDS.join("|_")})$/
17+
TEST_PATTERN = /#{TEST_PREFIX_PATTERN}|#{TEST_SUFFIX_PATTERN}/
18+
19+
TEST_PREFIX_GLOB = T.let("#{TEST_KEYWORDS.join("_,")}_", String)
20+
TEST_SUFFIX_GLOB = T.let("_#{TEST_KEYWORDS.join(",_")}", String)
21+
22+
#: (String path, String workspace_path) -> void
23+
def initialize(path, workspace_path)
1724
super()
1825

19-
@workspace_path = T.let(Dir.pwd, String)
20-
@path = T.let(path.delete_prefix(@workspace_path), String)
26+
@workspace_path = workspace_path
27+
@path = T.let(path.delete_prefix(workspace_path), String)
2128
end
2229

23-
sig { override.returns(T::Array[String]) }
30+
# @override
31+
#: -> Array[String]
2432
def perform
2533
find_relevant_paths
2634
end
2735

2836
private
2937

30-
sig { returns(T::Array[String]) }
38+
#: -> Array[String]
3139
def find_relevant_paths
32-
workspace_path = Dir.pwd
33-
relative_path = @path.delete_prefix(workspace_path)
34-
35-
candidate_paths = Dir.glob(File.join("**", relevant_filename_pattern(relative_path)))
40+
candidate_paths = Dir.glob(File.join("**", relevant_filename_pattern))
3641
return [] if candidate_paths.empty?
3742

38-
find_most_similar_with_jacaard(relative_path, candidate_paths).map { File.join(workspace_path, _1) }
43+
find_most_similar_with_jaccard(candidate_paths).map { File.join(@workspace_path, _1) }
3944
end
4045

41-
sig { params(path: String).returns(String) }
42-
def relevant_filename_pattern(path)
43-
input_basename = File.basename(path, File.extname(path))
44-
45-
test_prefix_pattern = /^(#{TEST_KEYWORDS.join("_|")}_)/
46-
test_suffix_pattern = /(_#{TEST_KEYWORDS.join("|_")})$/
47-
test_pattern = /#{test_prefix_pattern}|#{test_suffix_pattern}/
46+
#: -> String
47+
def relevant_filename_pattern
48+
input_basename = File.basename(@path, File.extname(@path))
4849

4950
relevant_basename_pattern =
50-
if input_basename.match?(test_pattern)
51-
input_basename.gsub(test_pattern, "")
51+
if input_basename.match?(TEST_PATTERN)
52+
input_basename.gsub(TEST_PATTERN, "")
5253
else
53-
test_prefix_glob = "#{TEST_KEYWORDS.join("_,")}_"
54-
test_suffix_glob = "_#{TEST_KEYWORDS.join(",_")}"
55-
56-
"{{#{test_prefix_glob}}#{input_basename},#{input_basename}{#{test_suffix_glob}}}"
54+
"{{#{TEST_PREFIX_GLOB}}#{input_basename},#{input_basename}{#{TEST_SUFFIX_GLOB}}}"
5755
end
5856

59-
"#{relevant_basename_pattern}#{File.extname(path)}"
57+
"#{relevant_basename_pattern}#{File.extname(@path)}"
6058
end
6159

62-
sig { params(path: String, candidates: T::Array[String]).returns(T::Array[String]) }
63-
def find_most_similar_with_jacaard(path, candidates)
64-
dirs = get_dir_parts(path)
60+
# Using the Jaccard algorithm to determine the similarity between the
61+
# input path and the candidate relevant file paths.
62+
# Ref: https://en.wikipedia.org/wiki/Jaccard_index
63+
# The main idea of this algorithm is to take the size of interaction and divide
64+
# it by the size of union between two sets (in our case the elements in each set
65+
# would be the parts of the path separated by path divider.)
66+
#: (Array[String] candidates) -> Array[String]
67+
def find_most_similar_with_jaccard(candidates)
68+
dirs = get_dir_parts(@path)
6569

6670
_, results = candidates
6771
.group_by do |other_path|
@@ -74,7 +78,7 @@ def find_most_similar_with_jacaard(path, candidates)
7478
results || []
7579
end
7680

77-
sig { params(path: String).returns(T::Set[String]) }
81+
#: (String path) -> Set[String]
7882
def get_dir_parts(path)
7983
Set.new(File.dirname(path).split(File::SEPARATOR))
8084
end

lib/ruby_lsp/server.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ def experimental_goto_relevant_file(message)
11431143
end
11441144

11451145
response = {
1146-
locations: Requests::GotoRelevantFile.new(path).perform,
1146+
locations: Requests::GotoRelevantFile.new(path, @global_state.workspace_path).perform,
11471147
}
11481148
send_message(Result.new(id: message[:id], response: response))
11491149
end

project-words

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ ipairs
4444
Ispec
4545
Itest
4646
ivar
47-
jacaard
47+
jaccard
48+
Jaccard
4849
Jaro
4950
Kaigi
5051
klass

test/requests/goto_relevant_file_test.rb

+13-17
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
require "test_helper"
55

66
class GotoRelevantFileTest < Minitest::Test
7-
def setup
8-
Dir.stubs(:pwd).returns("/workspace")
9-
end
10-
117
def test_when_input_is_test_file_returns_array_of_implementation_file_locations
128
stub_glob_pattern("**/goto_relevant_file.rb", ["lib/ruby_lsp/requests/goto_relevant_file.rb"])
139

1410
test_file_path = "/workspace/test/requests/goto_relevant_file_test.rb"
1511
expected = ["/workspace/lib/ruby_lsp/requests/goto_relevant_file.rb"]
1612

17-
result = RubyLsp::Requests::GotoRelevantFile.new(test_file_path).perform
13+
result = RubyLsp::Requests::GotoRelevantFile.new(test_file_path, "/workspace").perform
1814
assert_equal(expected, result)
1915
end
2016

@@ -26,7 +22,7 @@ def test_when_input_is_implementation_file_returns_array_of_test_file_locations
2622
impl_path = "/workspace/lib/ruby_lsp/requests/goto_relevant_file.rb"
2723
expected = ["/workspace/test/requests/goto_relevant_file_test.rb"]
2824

29-
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path).perform
25+
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path, "/workspace").perform
3026
assert_equal(expected, result)
3127
end
3228

@@ -44,7 +40,7 @@ def test_return_all_file_locations_that_have_the_same_highest_coefficient
4440
"/workspace/test/integration/some_feature_test.rb",
4541
]
4642

47-
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path).perform
43+
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path, "/workspace").perform
4844
assert_equal(expected.sort, result.sort)
4945
end
5046

@@ -53,7 +49,7 @@ def test_return_empty_array_when_no_filename_matches
5349
stub_glob_pattern(pattern, [])
5450

5551
file_path = "/workspace/lib/ruby_lsp/requests/nonexistent_file.rb"
56-
result = RubyLsp::Requests::GotoRelevantFile.new(file_path).perform
52+
result = RubyLsp::Requests::GotoRelevantFile.new(file_path, "/workspace").perform
5753
assert_empty(result)
5854
end
5955

@@ -63,7 +59,7 @@ def test_it_finds_implementation_when_file_has_test_suffix
6359
test_path = "/workspace/test/feature_test.rb"
6460
expected = ["/workspace/lib/feature.rb"]
6561

66-
result = RubyLsp::Requests::GotoRelevantFile.new(test_path).perform
62+
result = RubyLsp::Requests::GotoRelevantFile.new(test_path, "/workspace").perform
6763
assert_equal(expected, result)
6864
end
6965

@@ -73,7 +69,7 @@ def test_it_finds_implementation_when_file_has_spec_suffix
7369
test_path = "/workspace/spec/feature_spec.rb"
7470
expected = ["/workspace/lib/feature.rb"]
7571

76-
result = RubyLsp::Requests::GotoRelevantFile.new(test_path).perform
72+
result = RubyLsp::Requests::GotoRelevantFile.new(test_path, "/workspace").perform
7773
assert_equal(expected, result)
7874
end
7975

@@ -83,7 +79,7 @@ def test_it_finds_implementation_when_file_has_integration_test_suffix
8379
test_path = "/workspace/test/feature_integration_test.rb"
8480
expected = ["/workspace/lib/feature.rb"]
8581

86-
result = RubyLsp::Requests::GotoRelevantFile.new(test_path).perform
82+
result = RubyLsp::Requests::GotoRelevantFile.new(test_path, "/workspace").perform
8783
assert_equal(expected, result)
8884
end
8985

@@ -93,7 +89,7 @@ def test_it_finds_implementation_when_file_has_test_prefix
9389
test_path = "/workspace/test/test_feature.rb"
9490
expected = ["/workspace/lib/feature.rb"]
9591

96-
result = RubyLsp::Requests::GotoRelevantFile.new(test_path).perform
92+
result = RubyLsp::Requests::GotoRelevantFile.new(test_path, "/workspace").perform
9793
assert_equal(expected, result)
9894
end
9995

@@ -103,7 +99,7 @@ def test_it_finds_implementation_when_file_has_spec_prefix
10399
test_path = "/workspace/test/spec_feature.rb"
104100
expected = ["/workspace/lib/feature.rb"]
105101

106-
result = RubyLsp::Requests::GotoRelevantFile.new(test_path).perform
102+
result = RubyLsp::Requests::GotoRelevantFile.new(test_path, "/workspace").perform
107103
assert_equal(expected, result)
108104
end
109105

@@ -113,7 +109,7 @@ def test_it_finds_implementation_when_file_has_integration_test_prefix
113109
test_path = "/workspace/test/integration_test_feature.rb"
114110
expected = ["/workspace/lib/feature.rb"]
115111

116-
result = RubyLsp::Requests::GotoRelevantFile.new(test_path).perform
112+
result = RubyLsp::Requests::GotoRelevantFile.new(test_path, "/workspace").perform
117113
assert_equal(expected, result)
118114
end
119115

@@ -124,7 +120,7 @@ def test_it_finds_tests_for_implementation
124120
impl_path = "/workspace/lib/feature.rb"
125121
expected = ["/workspace/test/feature_test.rb"]
126122

127-
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path).perform
123+
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path, "/workspace").perform
128124
assert_equal(expected, result)
129125
end
130126

@@ -135,7 +131,7 @@ def test_it_finds_specs_for_implementation
135131
impl_path = "/workspace/lib/feature.rb"
136132
expected = ["/workspace/spec/feature_spec.rb"]
137133

138-
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path).perform
134+
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path, "/workspace").perform
139135
assert_equal(expected, result)
140136
end
141137

@@ -146,7 +142,7 @@ def test_it_finds_integration_tests_for_implementation
146142
impl_path = "/workspace/lib/feature.rb"
147143
expected = ["/workspace/test/feature_integration_test.rb"]
148144

149-
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path).perform
145+
result = RubyLsp::Requests::GotoRelevantFile.new(impl_path, "/workspace").perform
150146
assert_equal(expected, result)
151147
end
152148

0 commit comments

Comments
 (0)