@@ -7,62 +7,96 @@ class CodeLens < ::RubyLsp::Listener
7
7
extend T ::Sig
8
8
extend T ::Generic
9
9
10
+ # This feature is currently experimental. Clients will need to pass `experimentalFeaturesEnabled`
11
+ # in the initialization options to enable it.
12
+ #
13
+ # The
14
+ # [code lens](https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens)
15
+ # request informs the editor of runnable commands such as tests
16
+ #
17
+ # # Example
18
+ #
19
+ # ```ruby
20
+ # # Run
21
+ # class Test < Minitest::Test
22
+ # end
23
+ # ```
24
+
10
25
ResponseType = type_member { { fixed : T . nilable ( T ::Array [ ::RubyLsp ::Interface ::CodeLens ] ) } }
26
+ BASE_COMMAND = "bin/rails test"
11
27
12
28
::RubyLsp ::Requests ::CodeLens . add_listener ( self )
13
29
14
30
sig { override . returns ( ResponseType ) }
15
31
attr_reader :response
16
32
17
- sig { params ( uri : String , message_queue : Thread ::Queue ) . void }
18
- def initialize ( uri , message_queue )
33
+ sig { params ( uri : String , emitter : EventEmitter , message_queue : Thread ::Queue ) . void }
34
+ def initialize ( uri , emitter , message_queue )
19
35
@response = T . let ( [ ] , ResponseType )
20
36
@visibility = T . let ( "public" , String )
21
37
@prev_visibility = T . let ( "public" , String )
22
38
@path = T . let ( uri . delete_prefix ( "file://" ) , String )
23
- super
39
+ emitter . register ( self , :on_command , :on_class )
40
+
41
+ super ( emitter , message_queue ) # can be just super later?
24
42
end
25
43
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 )
44
+ sig { params ( node : SyntaxTree ::Command ) . void }
45
+ def on_command ( node )
46
+ if @visibility == "public"
47
+ message_value = node . message . value
48
+ if message_value == "test" && node . arguments . parts . any?
49
+ first_argument = node . arguments . parts . first
50
+ method_name = first_argument . parts . first . value if first_argument . is_a? ( SyntaxTree ::StringLiteral )
51
+ test_file_path = Pathname . new ( @path ) . relative_path_from ( RailsClient . instance . root ) . to_s
52
+ line_number = node . location . start_line
53
+ command = "#{ BASE_COMMAND } #{ test_file_path } :#{ line_number } "
34
54
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
55
+ if method_name
56
+ add_code_lens ( node , name : method_name , command : command )
44
57
end
45
58
end
46
59
end
60
+ end
47
61
48
- sig { params ( node : SyntaxTree ::DefNode ) . void }
49
- def on_def ( node ) ; end
62
+ sig { params ( node : SyntaxTree ::ClassDeclaration ) . void }
63
+ def on_class ( node )
64
+ class_name = node . constant . constant . value
65
+ if class_name . end_with? ( "Test" )
66
+ add_code_lens ( node , name : class_name , command : "#{ BASE_COMMAND } #{ @path } " )
67
+ end
50
68
end
51
69
52
70
private
53
71
54
72
sig { params ( node : SyntaxTree ::Node , name : String , command : String ) . void }
55
73
def add_code_lens ( node , name :, command :)
56
- @response << ::RubyLsp ::Requests ::CodeLens . create_code_lens (
74
+ @response = T . must ( @response )
75
+
76
+ @response << create_code_lens (
57
77
node ,
78
+ title : "Run" ,
79
+ command_name : "rubyLsp.runTest" ,
58
80
path : @path ,
59
81
name : name ,
60
82
test_command : command ,
61
83
type : "test" ,
62
84
)
63
85
64
- @response << ::RubyLsp ::Requests ::CodeLens . create_code_lens (
86
+ @response << create_code_lens (
87
+ node ,
88
+ title : "Run In Terminal" ,
89
+ command_name : "rubyLsp.runTestInTerminal" ,
90
+ path : @path ,
91
+ name : name ,
92
+ test_command : command ,
93
+ type : "test_in_terminal" ,
94
+ )
95
+
96
+ @response << create_code_lens (
65
97
node ,
98
+ title : "Debug" ,
99
+ command_name : "rubyLsp.debugTest" ,
66
100
path : @path ,
67
101
name : name ,
68
102
test_command : command ,
0 commit comments