@@ -10,6 +10,7 @@ module Rails
10
10
# - Run tests in the VS Terminal
11
11
# - Run tests in the VS Code Test Explorer
12
12
# - Debug tests
13
+ # - Run migrations in the VS Terminal
13
14
#
14
15
# The
15
16
# [code lens](https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens)
@@ -34,6 +35,14 @@ module Rails
34
35
# end
35
36
# ````
36
37
#
38
+ # # Example:
39
+ # ```ruby
40
+ # Run
41
+ # class AddFirstNameToUsers < ActiveRecord::Migration[7.1]
42
+ # # ...
43
+ # end
44
+ # ````
45
+ #
37
46
# The code lenses will be displayed above the class and above each test method.
38
47
#
39
48
# Note: When using the Test Explorer view, if your code contains a statement to pause execution (e.g. `debugger`) it
@@ -74,6 +83,7 @@ def on_call_node_enter(node)
74
83
sig { params ( node : Prism ::DefNode ) . void }
75
84
def on_def_node_enter ( node )
76
85
method_name = node . name . to_s
86
+
77
87
if method_name . start_with? ( "test_" )
78
88
line_number = node . location . start_line
79
89
command = "#{ test_command } #{ @path } :#{ line_number } "
@@ -84,12 +94,19 @@ def on_def_node_enter(node)
84
94
sig { params ( node : Prism ::ClassNode ) . void }
85
95
def on_class_node_enter ( node )
86
96
class_name = node . constant_path . slice
97
+ superclass_name = node . superclass &.slice
98
+
87
99
if class_name . end_with? ( "Test" )
88
100
command = "#{ test_command } #{ @path } "
89
101
add_test_code_lens ( node , name : class_name , command : command , kind : :group )
90
102
@group_id_stack . push ( @group_id )
91
103
@group_id += 1
92
104
end
105
+
106
+ if superclass_name &.start_with? ( "ActiveRecord::Migration" )
107
+ command = "#{ migrate_command } VERSION=#{ migration_version } "
108
+ add_migrate_code_lens ( node , name : class_name , command : command )
109
+ end
93
110
end
94
111
95
112
sig { params ( node : Prism ::ClassNode ) . void }
@@ -104,11 +121,30 @@ def on_class_node_leave(node)
104
121
105
122
sig { returns ( String ) }
106
123
def test_command
107
- if Gem . win_platform?
108
- "ruby bin/rails test"
109
- else
110
- "bin/rails test"
111
- end
124
+ "#{ RbConfig . ruby } bin/rails test"
125
+ end
126
+
127
+ sig { returns ( String ) }
128
+ def migrate_command
129
+ "#{ RbConfig . ruby } bin/rails db:migrate"
130
+ end
131
+
132
+ sig { returns ( T . nilable ( String ) ) }
133
+ def migration_version
134
+ File . basename ( T . must ( @path ) ) . split ( "_" ) . first
135
+ end
136
+
137
+ sig { params ( node : Prism ::Node , name : String , command : String ) . void }
138
+ def add_migrate_code_lens ( node , name :, command :)
139
+ return unless @path
140
+
141
+ @response_builder << create_code_lens (
142
+ node ,
143
+ title : "Run" ,
144
+ command_name : "rubyLsp.runTask" ,
145
+ arguments : [ command ] ,
146
+ data : { type : "migrate" } ,
147
+ )
112
148
end
113
149
114
150
sig { params ( node : Prism ::Node , name : String , command : String , kind : Symbol ) . void }
0 commit comments