Skip to content

Commit

Permalink
Disable ruby-lsp-rspec's definition listener outside test files(#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicolBen authored Oct 22, 2024
1 parent 010dcbf commit 3931cac
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rspec/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 36 additions & 3 deletions spec/ruby_lsp_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end
RUBY

tempfile = Tempfile.new
tempfile = Tempfile.new(["", "_fake_spec.rb"])
tempfile.write(source)
tempfile.close
uri = URI(tempfile.path)
Expand Down Expand Up @@ -89,7 +89,7 @@
end
RUBY

tempfile = Tempfile.new
tempfile = Tempfile.new(["", "_fake_spec.rb"])
tempfile.write(source)
tempfile.close
uri = URI(tempfile.path)
Expand Down Expand Up @@ -150,7 +150,7 @@
end
RUBY

tempfile = Tempfile.new
tempfile = Tempfile.new(["", "_fake_spec.rb"])
tempfile.write(source)
tempfile.close
uri = URI(tempfile.path)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3931cac

Please sign in to comment.