Skip to content

Commit

Permalink
Only handle requests in _{spec,test}.rb files (#6)
Browse files Browse the repository at this point in the history
* only handle requests in _{spec,test}.rb files

Co-authored-by: Stan Lo <[email protected]>
  • Loading branch information
rohitpaulk and st0012 authored Oct 29, 2023
1 parent 1d6dc1e commit ade0a14
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 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 @@ -26,6 +26,8 @@ def deactivate; end
).returns(T.nilable(Listener[T::Array[Interface::CodeLens]]))
end
def create_code_lens_listener(uri, emitter, message_queue)
return unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb")

CodeLens.new(uri, emitter, message_queue)
end

Expand Down
5 changes: 2 additions & 3 deletions lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class CodeLens < ::RubyLsp::Listener
sig { params(uri: URI::Generic, dispatcher: Prism::Dispatcher, message_queue: Thread::Queue).void }
def initialize(uri, dispatcher, message_queue)
@_response = T.let([], ResponseType)
@path = T.let(uri.to_standardized_path, T.nilable(String))
# Listener is only initialized if uri.to_standardized_path is valid
@path = T.let(T.must(uri.to_standardized_path), String)
dispatcher.register(self, :on_call_node_enter)

@base_command = T.let(
Expand Down Expand Up @@ -78,8 +79,6 @@ def generate_name(node)

sig { params(node: Prism::Node, name: String, kind: Symbol).void }
def add_test_code_lens(node, name:, kind:)
return unless @path

line_number = node.location.start_line
command = "#{@base_command} #{@path}:#{line_number}"

Expand Down
42 changes: 34 additions & 8 deletions spec/ruby_lsp_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

RSpec.describe RubyLsp::RSpec do
let(:uri) { URI("file:///fake.rb") }
let(:uri) { URI("file:///fake_spec.rb") }
let(:store) { RubyLsp::Store.new }
let(:message_queue) { Thread::Queue.new }

Expand Down Expand Up @@ -41,9 +41,9 @@

0.upto(2) do |i|
expect(response[i].command.arguments).to eq([
"/fake.rb",
"/fake_spec.rb",
"Foo",
"bundle exec rspec /fake.rb:1",
"bundle exec rspec /fake_spec.rb:1",
{ start_line: 0, start_column: 0, end_line: 5, end_column: 3 },
])
end
Expand All @@ -54,9 +54,9 @@

3.upto(5) do |i|
expect(response[i].command.arguments).to eq([
"/fake.rb",
"/fake_spec.rb",
"when something",
"bundle exec rspec /fake.rb:2",
"bundle exec rspec /fake_spec.rb:2",
{ start_line: 1, start_column: 2, end_line: 4, end_column: 5 },
])
end
Expand All @@ -67,9 +67,9 @@

6.upto(8) do |i|
expect(response[i].command.arguments).to eq([
"/fake.rb",
"/fake_spec.rb",
"does something",
"bundle exec rspec /fake.rb:3",
"bundle exec rspec /fake_spec.rb:3",
{ start_line: 2, start_column: 4, end_line: 3, end_column: 7 },
])
end
Expand Down Expand Up @@ -145,6 +145,32 @@
expect(response[15].command.arguments[1]).to eq("<var>")
end

context "when the file is not a test file" do
let(:uri) { URI("file:///not_spec_file.rb") }

it "ignores file" do
store.set(uri: uri, source: <<~RUBY, version: 1)
class FooBar
context "when something" do
end
end
RUBY

response = RubyLsp::Executor.new(store, message_queue).execute(
{
method: "textDocument/codeLens",
params: {
textDocument: { uri: uri },
position: { line: 0, character: 0 },
},
},
)

expect(response.error).to(be_nil)
expect(response.response.count).to eq(0)
end
end

context "when there's a binstub" do
let(:binstub_path) { File.expand_path("../bin/rspec", __dir__) }

Expand Down Expand Up @@ -179,7 +205,7 @@

response = response.response
expect(response.count).to eq(3)
expect(response[0].command.arguments[2]).to eq("bundle exec bin/rspec /fake.rb:1")
expect(response[0].command.arguments[2]).to eq("bundle exec bin/rspec /fake_spec.rb:1")
end
end
end

0 comments on commit ade0a14

Please sign in to comment.