From d54eb2cbc8a3c45334c1bebde4966ccfbfe60809 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 24 Nov 2023 20:43:41 +0000 Subject: [PATCH] Make test CodeLens request aware of test hierarchy (#1214) This is achieved by adding `group_id` (both group and example kinds) and `id` (only example kind) attributes to the CodeLens data. Co-authored-by: Vinicius Stock --- lib/ruby_lsp/requests/code_lens.rb | 15 ++++-- .../code_lens/minitest_tests.exp.json | 49 ++++++++++++++--- .../code_lens/nested_minitest_tests.exp.json | 52 +++++++++++++++---- 3 files changed, 96 insertions(+), 20 deletions(-) diff --git a/lib/ruby_lsp/requests/code_lens.rb b/lib/ruby_lsp/requests/code_lens.rb index 9e8fa5540..914b53988 100644 --- a/lib/ruby_lsp/requests/code_lens.rb +++ b/lib/ruby_lsp/requests/code_lens.rb @@ -55,6 +55,8 @@ def initialize(uri, dispatcher, message_queue) # visibility_stack is a stack of [current_visibility, previous_visibility] @visibility_stack = T.let([[:public, :public]], T::Array[T::Array[T.nilable(Symbol)]]) @class_stack = T.let([], T::Array[String]) + @group_id = T.let(1, Integer) + @group_id_stack = T.let([], T::Array[Integer]) super(dispatcher, message_queue) @@ -82,12 +84,16 @@ def on_class_node_enter(node) kind: :group, ) end + + @group_id_stack.push(@group_id) + @group_id += 1 end sig { params(node: Prism::ClassNode).void } def on_class_node_leave(node) @visibility_stack.pop @class_stack.pop + @group_id_stack.pop end sig { params(node: Prism::DefNode).void } @@ -174,12 +180,15 @@ def add_test_code_lens(node, name:, command:, kind:) }, ] + grouping_data = { group_id: @group_id_stack.last, kind: kind } + grouping_data[:id] = @group_id if kind == :group + @_response << create_code_lens( node, title: "Run", command_name: "rubyLsp.runTest", arguments: arguments, - data: { type: "test", kind: kind }, + data: { type: "test", **grouping_data }, ) @_response << create_code_lens( @@ -187,7 +196,7 @@ def add_test_code_lens(node, name:, command:, 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( @@ -195,7 +204,7 @@ def add_test_code_lens(node, name:, command:, kind:) title: "Debug", command_name: "rubyLsp.debugTest", arguments: arguments, - data: { type: "debug", kind: kind }, + data: { type: "debug", **grouping_data }, ) end diff --git a/test/expectations/code_lens/minitest_tests.exp.json b/test/expectations/code_lens/minitest_tests.exp.json index b40cf653f..964c744d3 100644 --- a/test/expectations/code_lens/minitest_tests.exp.json +++ b/test/expectations/code_lens/minitest_tests.exp.json @@ -28,7 +28,9 @@ }, "data": { "type": "test", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 1 } }, { @@ -59,7 +61,9 @@ }, "data": { "type": "test_in_terminal", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 1 } }, { @@ -90,7 +94,9 @@ }, "data": { "type": "debug", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 1 } }, { @@ -121,6 +127,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -152,6 +159,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -183,6 +191,7 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } }, @@ -214,6 +223,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -245,6 +255,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -276,6 +287,7 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } }, @@ -307,6 +319,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -338,6 +351,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -369,6 +383,7 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } }, @@ -400,6 +415,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -431,6 +447,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -462,6 +479,7 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } }, @@ -493,6 +511,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -524,6 +543,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -555,6 +575,7 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } }, @@ -586,7 +607,9 @@ }, "data": { "type": "test", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 2 } }, { @@ -617,7 +640,9 @@ }, "data": { "type": "test_in_terminal", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 2 } }, { @@ -648,7 +673,9 @@ }, "data": { "type": "debug", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 2 } }, { @@ -679,6 +706,7 @@ }, "data": { "type": "test", + "group_id": 2, "kind": "example" } }, @@ -710,6 +738,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 2, "kind": "example" } }, @@ -741,6 +770,7 @@ }, "data": { "type": "debug", + "group_id": 2, "kind": "example" } }, @@ -772,6 +802,7 @@ }, "data": { "type": "test", + "group_id": 2, "kind": "example" } }, @@ -803,6 +834,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 2, "kind": "example" } }, @@ -834,9 +866,12 @@ }, "data": { "type": "debug", + "group_id": 2, "kind": "example" } } ], - "params": [] + "params": [ + + ] } diff --git a/test/expectations/code_lens/nested_minitest_tests.exp.json b/test/expectations/code_lens/nested_minitest_tests.exp.json index edcd40a6f..bca0fe26f 100644 --- a/test/expectations/code_lens/nested_minitest_tests.exp.json +++ b/test/expectations/code_lens/nested_minitest_tests.exp.json @@ -28,7 +28,9 @@ }, "data": { "type": "test", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 1 } }, { @@ -59,7 +61,9 @@ }, "data": { "type": "test_in_terminal", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 1 } }, { @@ -90,7 +94,9 @@ }, "data": { "type": "debug", - "kind": "group" + "group_id": null, + "kind": "group", + "id": 1 } }, { @@ -121,6 +127,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -152,6 +159,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -183,6 +191,7 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } }, @@ -214,7 +223,9 @@ }, "data": { "type": "test", - "kind": "group" + "group_id": 1, + "kind": "group", + "id": 2 } }, { @@ -245,7 +256,9 @@ }, "data": { "type": "test_in_terminal", - "kind": "group" + "group_id": 1, + "kind": "group", + "id": 2 } }, { @@ -276,7 +289,9 @@ }, "data": { "type": "debug", - "kind": "group" + "group_id": 1, + "kind": "group", + "id": 2 } }, { @@ -307,6 +322,7 @@ }, "data": { "type": "test", + "group_id": 2, "kind": "example" } }, @@ -338,6 +354,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 2, "kind": "example" } }, @@ -369,6 +386,7 @@ }, "data": { "type": "debug", + "group_id": 2, "kind": "example" } }, @@ -400,7 +418,9 @@ }, "data": { "type": "test", - "kind": "group" + "group_id": 1, + "kind": "group", + "id": 3 } }, { @@ -431,7 +451,9 @@ }, "data": { "type": "test_in_terminal", - "kind": "group" + "group_id": 1, + "kind": "group", + "id": 3 } }, { @@ -462,7 +484,9 @@ }, "data": { "type": "debug", - "kind": "group" + "group_id": 1, + "kind": "group", + "id": 3 } }, { @@ -493,6 +517,7 @@ }, "data": { "type": "test", + "group_id": 3, "kind": "example" } }, @@ -524,6 +549,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 3, "kind": "example" } }, @@ -555,6 +581,7 @@ }, "data": { "type": "debug", + "group_id": 3, "kind": "example" } }, @@ -586,6 +613,7 @@ }, "data": { "type": "test", + "group_id": 1, "kind": "example" } }, @@ -617,6 +645,7 @@ }, "data": { "type": "test_in_terminal", + "group_id": 1, "kind": "example" } }, @@ -648,9 +677,12 @@ }, "data": { "type": "debug", + "group_id": 1, "kind": "example" } } ], - "params": [] + "params": [ + + ] }