diff --git a/lib/ruby_lsp/ruby_lsp_rspec/addon.rb b/lib/ruby_lsp/ruby_lsp_rspec/addon.rb index 98d13ff..3cde027 100644 --- a/lib/ruby_lsp/ruby_lsp_rspec/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rspec/addon.rb @@ -64,6 +64,8 @@ def create_document_symbol_listener(response_builder, dispatcher) ).void end def create_definition_listener(response_builder, uri, node_context, dispatcher) + return unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb") + Definition.new(response_builder, uri, node_context, T.must(@index), dispatcher) end diff --git a/spec/ruby_lsp_rspec_spec.rb b/spec/ruby_lsp_rspec_spec.rb index 5781976..87dfbfa 100644 --- a/spec/ruby_lsp_rspec_spec.rb +++ b/spec/ruby_lsp_rspec_spec.rb @@ -21,7 +21,7 @@ end RUBY - tempfile = Tempfile.new + tempfile = Tempfile.new(["", "_fake_spec.rb"]) tempfile.write(source) tempfile.close uri = URI(tempfile.path) @@ -89,7 +89,7 @@ end RUBY - tempfile = Tempfile.new + tempfile = Tempfile.new(["", "_fake_spec.rb"]) tempfile.write(source) tempfile.close uri = URI(tempfile.path) @@ -150,7 +150,7 @@ end RUBY - tempfile = Tempfile.new + tempfile = Tempfile.new(["", "_fake_spec.rb"]) tempfile.write(source) tempfile.close uri = URI(tempfile.path) @@ -205,6 +205,39 @@ ensure tempfile&.unlink end + + context "when the file is not a test file" do + let(:uri) { URI("file:///not_spec_file.rb") } + + it "ignores file" do + source = <<~RUBY + class FooBar + def bar + foo + end + + def foo; end + end + RUBY + + with_server(source, uri) do |server, uri| + server.process_message( + { + id: 1, + method: "textDocument/definition", + params: { + textDocument: { uri: uri }, + position: { character: 4, line: 2 }, + }, + }, + ) + + response = server.pop_response.response + + expect(response.count).to eq(1) + end + end + end end describe "document symbol" do