Skip to content

Commit

Permalink
Merge pull request #734 from snutij/clean-make-create-code-lens-usabl…
Browse files Browse the repository at this point in the history
…e-for-other-kind-of-code-lens

Make Common#create_code_lens more re-usable
  • Loading branch information
st0012 authored Jun 7, 2023
2 parents 66f4b78 + bd051b2 commit efb1d9a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 93 deletions.
58 changes: 33 additions & 25 deletions lib/ruby_lsp/requests/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ def on_class(node)
class_name = node.constant.constant.value
if class_name.end_with?("Test")
@class_stack.push(class_name)
add_code_lens(node, name: class_name, command: generate_test_command(class_name: class_name))
add_test_code_lens(
node,
name: class_name,
command: generate_test_command(class_name: class_name),
kind: :group,
)
end
end

Expand All @@ -79,10 +84,11 @@ def on_def(node)
method_name = node.name.value
if method_name.start_with?("test_")
class_name = T.must(@class_stack.last)
add_code_lens(
add_test_code_lens(
node,
name: method_name,
command: generate_test_command(method_name: method_name, class_name: class_name),
kind: :example,
)
end
end
Expand Down Expand Up @@ -145,36 +151,42 @@ def merge_response!(other)

private

sig { params(node: SyntaxTree::Node, name: String, command: String).void }
def add_code_lens(node, name:, command:)
sig { params(node: SyntaxTree::Node, name: String, command: String, kind: Symbol).void }
def add_test_code_lens(node, name:, command:, kind:)
arguments = [
@path,
name,
command,
{
start_line: node.location.start_line - 1,
start_column: node.location.start_column,
end_line: node.location.end_line - 1,
end_column: node.location.end_column,
},
]

@response << create_code_lens(
node,
title: "Run",
command_name: "rubyLsp.runTest",
path: @path,
name: name,
test_command: command,
type: "test",
arguments: arguments,
data: { type: "test", kind: kind },
)

@response << create_code_lens(
node,
title: "Run In Terminal",
command_name: "rubyLsp.runTestInTerminal",
path: @path,
name: name,
test_command: command,
type: "test_in_terminal",
arguments: arguments,
data: { type: "test_in_terminal", kind: kind },
)

@response << create_code_lens(
node,
title: "Debug",
command_name: "rubyLsp.debugTest",
path: @path,
name: name,
test_command: command,
type: "debug",
arguments: arguments,
data: { type: "debug", kind: kind },
)
end

Expand Down Expand Up @@ -204,15 +216,11 @@ def generate_test_command(class_name:, method_name: nil)

sig { params(node: SyntaxTree::Command, remote: String).void }
def add_open_gem_remote_code_lens(node, remote)
range = range_from_syntax_tree_node(node)

@response << Interface::CodeLens.new(
range: range,
command: Interface::Command.new(
title: "Open remote",
command: "rubyLsp.openLink",
arguments: [remote],
),
@response << create_code_lens(
node,
title: "Open remote",
command_name: "rubyLsp.openLink",
arguments: [remote],
data: { type: "link" },
)
end
Expand Down
21 changes: 4 additions & 17 deletions lib/ruby_lsp/requests/support/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,12 @@ def visible?(node, range)
node: SyntaxTree::Node,
title: String,
command_name: String,
path: String,
name: String,
test_command: String,
type: String,
arguments: T.nilable(T::Array[T.untyped]),
data: T.nilable(T::Hash[T.untyped, T.untyped]),
).returns(Interface::CodeLens)
end
def create_code_lens(node, title:, command_name:, path:, name:, test_command:, type:)
def create_code_lens(node, title:, command_name:, arguments:, data:)
range = range_from_syntax_tree_node(node)
arguments = [
path,
name,
test_command,
{
start_line: node.location.start_line - 1,
start_column: node.location.start_column,
end_line: node.location.end_line - 1,
end_column: node.location.end_column,
},
]

Interface::CodeLens.new(
range: range,
Expand All @@ -84,7 +71,7 @@ def create_code_lens(node, title:, command_name:, path:, name:, test_command:, t
command: command_name,
arguments: arguments,
),
data: { type: type, test_library: "minitest" },
data: data,
)
end
end
Expand Down
52 changes: 25 additions & 27 deletions test/expectations/code_lens/minitest_tests.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "group"
}
},
{
Expand Down Expand Up @@ -59,7 +59,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "group"
}
},
{
Expand Down Expand Up @@ -90,7 +90,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "group"
}
},
{
Expand Down Expand Up @@ -121,7 +121,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -152,7 +152,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -183,7 +183,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -214,7 +214,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -245,7 +245,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -276,7 +276,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -307,7 +307,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -338,7 +338,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -369,7 +369,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -400,7 +400,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -431,7 +431,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -462,7 +462,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -493,7 +493,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -524,7 +524,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -555,7 +555,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -586,7 +586,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "group"
}
},
{
Expand Down Expand Up @@ -617,7 +617,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "group"
}
},
{
Expand Down Expand Up @@ -648,7 +648,7 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "group"
}
},
{
Expand Down Expand Up @@ -679,7 +679,7 @@
},
"data": {
"type": "test",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -710,7 +710,7 @@
},
"data": {
"type": "test_in_terminal",
"test_library": "minitest"
"kind": "example"
}
},
{
Expand Down Expand Up @@ -741,11 +741,9 @@
},
"data": {
"type": "debug",
"test_library": "minitest"
"kind": "example"
}
}
],
"params": [

]
"params": []
}
Loading

0 comments on commit efb1d9a

Please sign in to comment.