Skip to content

Commit

Permalink
Support CodeLens hierarchy by adding grouping info (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored Nov 29, 2023
1 parent c8f1af9 commit d3fe4c5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
26 changes: 22 additions & 4 deletions lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -99,23 +117,23 @@ 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(
node,
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(
node,
title: "Debug",
command_name: "rubyLsp.debugTest",
arguments: arguments,
data: { type: "debug", kind: kind },
data: { type: "debug", **grouping_data },
)
end
end
Expand Down
18 changes: 9 additions & 9 deletions spec/ruby_lsp_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand All @@ -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([
Expand Down

0 comments on commit d3fe4c5

Please sign in to comment.