diff --git a/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb b/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb index 2759152..24f92eb 100644 --- a/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb +++ b/lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb @@ -19,7 +19,9 @@ def initialize(uri, dispatcher, message_queue) @_response = T.let([], ResponseType) # 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) + @group_id = T.let(1, Integer) + @group_id_stack = T.let([], T::Array[Integer]) + dispatcher.register(self, :on_call_node_enter, :on_call_node_leave) @base_command = T.let( begin @@ -52,6 +54,19 @@ def on_call_node_enter(node) name = generate_name(node) add_test_code_lens(node, name: name, kind: :group) + + @group_id_stack.push(@group_id) + @group_id += 1 + end + end + + sig { params(node: Prism::CallNode).void } + def on_call_node_leave(node) + case node.message + when "context", "describe" + return if node.receiver && node.receiver.name.to_s != "RSpec" + + @group_id_stack.pop end end @@ -82,6 +97,9 @@ def add_test_code_lens(node, name:, kind:) line_number = node.location.start_line command = "#{@base_command} #{@path}:#{line_number}" + grouping_data = { group_id: @group_id_stack.last, kind: kind } + grouping_data[:id] = @group_id if kind == :group + arguments = [ @path, name, @@ -99,7 +117,7 @@ def add_test_code_lens(node, name:, kind:) title: "Run", command_name: "rubyLsp.runTest", arguments: arguments, - data: { type: "test", kind: kind }, + data: { type: "test", **grouping_data }, ) @_response << create_code_lens( @@ -107,7 +125,7 @@ def add_test_code_lens(node, name:, kind:) title: "Run In Terminal", command_name: "rubyLsp.runTestInTerminal", arguments: arguments, - data: { type: "test_in_terminal", kind: kind }, + data: { type: "test_in_terminal", **grouping_data }, ) @_response << create_code_lens( @@ -115,7 +133,7 @@ def add_test_code_lens(node, name:, kind:) title: "Debug", command_name: "rubyLsp.debugTest", arguments: arguments, - data: { type: "debug", kind: kind }, + data: { type: "debug", **grouping_data }, ) end end diff --git a/spec/ruby_lsp_rspec_spec.rb b/spec/ruby_lsp_rspec_spec.rb index 1e65398..64d1ab5 100644 --- a/spec/ruby_lsp_rspec_spec.rb +++ b/spec/ruby_lsp_rspec_spec.rb @@ -35,9 +35,9 @@ response = response.response expect(response.count).to eq(9) - expect(response[0].data).to eq({ type: "test", kind: :group }) - expect(response[1].data).to eq({ type: "test_in_terminal", kind: :group }) - expect(response[2].data).to eq({ type: "debug", kind: :group }) + expect(response[0].data).to eq({ type: "test", kind: :group, group_id: nil, id: 1 }) + expect(response[1].data).to eq({ type: "test_in_terminal", kind: :group, group_id: nil, id: 1 }) + expect(response[2].data).to eq({ type: "debug", kind: :group, group_id: nil, id: 1 }) 0.upto(2) do |i| expect(response[i].command.arguments).to eq([ @@ -48,9 +48,9 @@ ]) end - expect(response[3].data).to eq({ type: "test", kind: :group }) - expect(response[4].data).to eq({ type: "test_in_terminal", kind: :group }) - expect(response[5].data).to eq({ type: "debug", kind: :group }) + expect(response[3].data).to eq({ type: "test", kind: :group, group_id: 1, id: 2 }) + expect(response[4].data).to eq({ type: "test_in_terminal", kind: :group, group_id: 1, id: 2 }) + expect(response[5].data).to eq({ type: "debug", kind: :group, group_id: 1, id: 2 }) 3.upto(5) do |i| expect(response[i].command.arguments).to eq([ @@ -61,9 +61,9 @@ ]) end - expect(response[6].data).to eq({ type: "test", kind: :example }) - expect(response[7].data).to eq({ type: "test_in_terminal", kind: :example }) - expect(response[8].data).to eq({ type: "debug", kind: :example }) + expect(response[6].data).to eq({ type: "test", kind: :example, group_id: 2 }) + expect(response[7].data).to eq({ type: "test_in_terminal", kind: :example, group_id: 2 }) + expect(response[8].data).to eq({ type: "debug", kind: :example, group_id: 2 }) 6.upto(8) do |i| expect(response[i].command.arguments).to eq([