Skip to content

Commit abab823

Browse files
committed
Add code lens extension
1 parent 35342f9 commit abab823

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module RubyLsp
5+
module Rails
6+
class CodeLens < ::RubyLsp::Listener
7+
extend T::Sig
8+
extend T::Generic
9+
10+
ResponseType = type_member { { fixed: T.nilable(T::Array[::RubyLsp::Interface::CodeLens]) } }
11+
12+
::RubyLsp::Requests::CodeLens.add_listener(self)
13+
14+
sig { override.returns(ResponseType) }
15+
attr_reader :response
16+
17+
sig { params(uri: String, message_queue: Thread::Queue).void }
18+
def initialize(uri, message_queue)
19+
@response = T.let([], ResponseType)
20+
@visibility = T.let("public", String)
21+
@prev_visibility = T.let("public", String)
22+
@path = T.let(uri.delete_prefix("file://"), String)
23+
super
24+
end
25+
26+
listener_events do
27+
sig { params(node: SyntaxTree::Command).void }
28+
def on_command(node)
29+
if @visibility == "public"
30+
message_value = node.message.value
31+
if message_value == "test" && node.arguments.parts.any?
32+
first_argument = node.arguments.parts.first
33+
method_name = first_argument.parts.first.value if first_argument.is_a?(SyntaxTree::StringLiteral)
34+
35+
if method_name
36+
add_code_lens(
37+
node,
38+
name: method_name,
39+
command: RubyLsp::Requests::CodeLens::BASE_COMMAND + @path + " --name " + "test_" + method_name.gsub(
40+
" ", "_"
41+
),
42+
)
43+
end
44+
end
45+
end
46+
end
47+
48+
sig { params(node: SyntaxTree::DefNode).void }
49+
def on_def(node); end
50+
end
51+
52+
private
53+
54+
sig { params(node: SyntaxTree::Node, name: String, command: String).void }
55+
def add_code_lens(node, name:, command:)
56+
@response << ::RubyLsp::Requests::CodeLens.create_code_lens(
57+
node,
58+
path: @path,
59+
name: name,
60+
test_command: command,
61+
type: "test",
62+
)
63+
64+
@response << ::RubyLsp::Requests::CodeLens.create_code_lens(
65+
node,
66+
path: @path,
67+
name: name,
68+
test_command: command,
69+
type: "debug",
70+
)
71+
end
72+
end
73+
end
74+
end

lib/ruby_lsp/ruby_lsp_rails/extension.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
require_relative "rails_client"
77
require_relative "hover"
8+
require_relative "code_lens"
89

910
module RubyLsp
1011
module Rails

lib/ruby_lsp/ruby_lsp_rails/hover.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Hover < ::RubyLsp::Listener
1414
sig { override.returns(ResponseType) }
1515
attr_reader :response
1616

17-
sig { void }
18-
def initialize
17+
sig { params(uri: String, message_queue: Thread::Queue).void }
18+
def initialize(uri, message_queue)
1919
@response = T.let(nil, ResponseType)
2020
super
2121
end

test/ruby_lsp_rails/code_lens_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "test_helper"
5+
6+
module RubyLsp
7+
module Rails
8+
class CodeLensTest < ActiveSupport::TestCase
9+
test "recognizes Rails active support test cases" do
10+
message_queue = Thread::Queue.new
11+
listener = CodeLens.new("", message_queue)
12+
13+
test_name = "handles test case"
14+
15+
RubyLsp::EventEmitter.new(listener).emit_for_target(Command(
16+
Ident("test"),
17+
Args([StringLiteral([TStringContent(test_name)], "")]),
18+
BodyStmt(SyntaxTree::VoidStmt, "", "", "", ""),
19+
))
20+
21+
assert_equal(test_name, T.must(T.must(listener.response).first).command.arguments[1])
22+
end
23+
end
24+
end
25+
end

test/ruby_lsp_rails/hover_test.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class HoverTest < ActiveSupport::TestCase
1919
],
2020
}
2121

22-
listener = Hover.new
22+
message_queue = Thread::Queue.new
23+
listener = Hover.new("", message_queue)
2324

2425
stub_http_request("200", expected_response.to_json)
2526
RailsClient.instance.stubs(check_if_server_is_running!: true)
@@ -48,7 +49,8 @@ class HoverTest < ActiveSupport::TestCase
4849
columns: [],
4950
}
5051

51-
listener = Hover.new
52+
message_queue = Thread::Queue.new
53+
listener = Hover.new("", message_queue)
5254

5355
stub_http_request("200", expected_response.to_json)
5456
RailsClient.instance.stubs(check_if_server_is_running!: true)
@@ -66,7 +68,8 @@ class HoverTest < ActiveSupport::TestCase
6668
columns: [],
6769
}
6870

69-
listener = Hover.new
71+
message_queue = Thread::Queue.new
72+
listener = Hover.new("", message_queue)
7073

7174
stub_http_request("200", expected_response.to_json)
7275
RailsClient.instance.stubs(check_if_server_is_running!: true)

0 commit comments

Comments
 (0)