Skip to content

Commit 3931cac

Browse files
authored
Disable ruby-lsp-rspec's definition listener outside test files(#46)
1 parent 010dcbf commit 3931cac

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/ruby_lsp/ruby_lsp_rspec/addon.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def create_document_symbol_listener(response_builder, dispatcher)
6464
).void
6565
end
6666
def create_definition_listener(response_builder, uri, node_context, dispatcher)
67+
return unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb")
68+
6769
Definition.new(response_builder, uri, node_context, T.must(@index), dispatcher)
6870
end
6971

spec/ruby_lsp_rspec_spec.rb

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
end
2222
RUBY
2323

24-
tempfile = Tempfile.new
24+
tempfile = Tempfile.new(["", "_fake_spec.rb"])
2525
tempfile.write(source)
2626
tempfile.close
2727
uri = URI(tempfile.path)
@@ -89,7 +89,7 @@
8989
end
9090
RUBY
9191

92-
tempfile = Tempfile.new
92+
tempfile = Tempfile.new(["", "_fake_spec.rb"])
9393
tempfile.write(source)
9494
tempfile.close
9595
uri = URI(tempfile.path)
@@ -150,7 +150,7 @@
150150
end
151151
RUBY
152152

153-
tempfile = Tempfile.new
153+
tempfile = Tempfile.new(["", "_fake_spec.rb"])
154154
tempfile.write(source)
155155
tempfile.close
156156
uri = URI(tempfile.path)
@@ -205,6 +205,39 @@
205205
ensure
206206
tempfile&.unlink
207207
end
208+
209+
context "when the file is not a test file" do
210+
let(:uri) { URI("file:///not_spec_file.rb") }
211+
212+
it "ignores file" do
213+
source = <<~RUBY
214+
class FooBar
215+
def bar
216+
foo
217+
end
218+
219+
def foo; end
220+
end
221+
RUBY
222+
223+
with_server(source, uri) do |server, uri|
224+
server.process_message(
225+
{
226+
id: 1,
227+
method: "textDocument/definition",
228+
params: {
229+
textDocument: { uri: uri },
230+
position: { character: 4, line: 2 },
231+
},
232+
},
233+
)
234+
235+
response = server.pop_response.response
236+
237+
expect(response.count).to eq(1)
238+
end
239+
end
240+
end
208241
end
209242

210243
describe "document symbol" do

0 commit comments

Comments
 (0)