diff --git a/lib/ruby_lsp/ruby_lsp_rspec/addon.rb b/lib/ruby_lsp/ruby_lsp_rspec/addon.rb index 94351dd..b3dcdb7 100644 --- a/lib/ruby_lsp/ruby_lsp_rspec/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rspec/addon.rb @@ -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 nil unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb") + CodeLens.new(uri, emitter, message_queue) end diff --git a/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb b/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb index 4972ae7..2d28adc 100644 --- a/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb +++ b/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb @@ -17,7 +17,7 @@ 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)) + @path = T.let(T.must(uri.to_standardized_path), String) # Listener is only initialized if uri.to_standardized_path is valid dispatcher.register(self, :on_call_node_enter) @base_command = T.let( @@ -78,8 +78,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}" diff --git a/spec/ruby_lsp_rspec_spec.rb b/spec/ruby_lsp_rspec_spec.rb index 9688b85..a727988 100644 --- a/spec/ruby_lsp_rspec_spec.rb +++ b/spec/ruby_lsp_rspec_spec.rb @@ -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 } @@ -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 @@ -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 @@ -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 @@ -145,6 +145,34 @@ expect(response[15].command.arguments[1]).to eq("") 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) + + response = response.response + expect(response.count).to eq(0) + end + end + context "when there's a binstub" do let(:binstub_path) { File.expand_path("../bin/rspec", __dir__) } @@ -179,7 +207,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