ruby-lsp-rails 0.3.9 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2499b5ff0e291d166c0fe823ccd3c34d3c60606500a7f9db6a152508cb6f1346
4
- data.tar.gz: 7ef48a79261a23f531f0b86ca18f7cec27c9a75490b4b111a495badcdf267fa5
3
+ metadata.gz: 8e684ecacd45b09fee18e386a95d0dfa61bd3cce2c64869bf34f711d9d0c2fa3
4
+ data.tar.gz: c575d062c88f997967989f1cca84a1241fb6d07750a0535e122112b890e9dd0e
5
5
  SHA512:
6
- metadata.gz: ee15545d1a6e8dd9221b06f403af4219e59a0b823b527d92edce63539ca11b7fa01faeea1a7c2d8f91933686c8b92884a961698075d7c8826ba24e27619cd509
7
- data.tar.gz: dbee9b4f8dc8658254ec9db2d2e585a0fa6a78545a8e296d3b3f4d5881a53cb642475cf92c0915ddfaeeede2306d925957bb75e0d0c52029d3bca4ba39195061
6
+ metadata.gz: 17c1805f2e1d7363e2734d6983fdba8b11b72e083ff07b0d872745121f9b4c1d20898e439a19c7dc9f179f8729260cf2f90e701e47967d4901576166fe66c420
7
+ data.tar.gz: '039f940ff0ea3247316609b379396a7096ada44ed0b4127969223c3277d946bbbd913e106ede27912fc29c53a0e349999231e1561940e5d75ff6adbf0cea1a89'
@@ -51,9 +51,7 @@ module RubyLsp
51
51
  ).void
52
52
  end
53
53
  def create_code_lens_listener(response_builder, uri, dispatcher)
54
- return unless T.must(@global_state).test_library == "rails"
55
-
56
- CodeLens.new(@client, response_builder, uri, dispatcher)
54
+ CodeLens.new(@client, T.must(@global_state), response_builder, uri, dispatcher)
57
55
  end
58
56
 
59
57
  sig do
@@ -79,20 +79,30 @@ module RubyLsp
79
79
  sig do
80
80
  params(
81
81
  client: RunnerClient,
82
+ global_state: GlobalState,
82
83
  response_builder: ResponseBuilders::CollectionResponseBuilder[Interface::CodeLens],
83
84
  uri: URI::Generic,
84
85
  dispatcher: Prism::Dispatcher,
85
86
  ).void
86
87
  end
87
- def initialize(client, response_builder, uri, dispatcher)
88
+ def initialize(client, global_state, response_builder, uri, dispatcher)
88
89
  @client = client
90
+ @global_state = global_state
89
91
  @response_builder = response_builder
90
92
  @path = T.let(uri.to_standardized_path, T.nilable(String))
91
93
  @group_id = T.let(1, Integer)
92
94
  @group_id_stack = T.let([], T::Array[Integer])
93
95
  @constant_name_stack = T.let([], T::Array[[String, T.nilable(String)]])
94
96
 
95
- dispatcher.register(self, :on_call_node_enter, :on_class_node_enter, :on_def_node_enter, :on_class_node_leave)
97
+ dispatcher.register(
98
+ self,
99
+ :on_call_node_enter,
100
+ :on_class_node_enter,
101
+ :on_def_node_enter,
102
+ :on_class_node_leave,
103
+ :on_module_node_enter,
104
+ :on_module_node_leave,
105
+ )
96
106
  end
97
107
 
98
108
  sig { params(node: Prism::CallNode).void }
@@ -119,6 +129,7 @@ module RubyLsp
119
129
 
120
130
  if controller?
121
131
  add_route_code_lens_to_action(node)
132
+ add_jump_to_view(node)
122
133
  end
123
134
  end
124
135
 
@@ -156,43 +167,63 @@ module RubyLsp
156
167
  @constant_name_stack.pop
157
168
  end
158
169
 
170
+ sig { params(node: Prism::ModuleNode).void }
171
+ def on_module_node_enter(node)
172
+ @constant_name_stack << [node.constant_path.slice, nil]
173
+ end
174
+
175
+ sig { params(node: Prism::ModuleNode).void }
176
+ def on_module_node_leave(node)
177
+ @constant_name_stack.pop
178
+ end
179
+
159
180
  private
160
181
 
161
182
  sig { returns(T.nilable(T::Boolean)) }
162
183
  def controller?
163
- class_name, superclass_name = T.must(@constant_name_stack.last)
164
- class_name.end_with?("Controller") && superclass_name&.end_with?("Controller")
184
+ class_name, superclass_name = @constant_name_stack.last
185
+ return false unless class_name && superclass_name
186
+
187
+ class_name.end_with?("Controller") && superclass_name.end_with?("Controller")
165
188
  end
166
189
 
167
190
  sig { params(node: Prism::DefNode).void }
168
- def add_route_code_lens_to_action(node)
169
- class_name, _ = T.must(@constant_name_stack.last)
170
- route = @client.route(
171
- controller: class_name,
172
- action: node.name.to_s,
191
+ def add_jump_to_view(node)
192
+ class_name = @constant_name_stack.map(&:first).join("::")
193
+ action_name = node.name
194
+ controller_name = class_name
195
+ .delete_suffix("Controller")
196
+ .gsub(/([a-z])([A-Z])/, "\\1_\\2")
197
+ .gsub("::", "/")
198
+ .downcase
199
+
200
+ view_uris = Dir.glob("#{@client.rails_root}/app/views/#{controller_name}/#{action_name}*").map! do |path|
201
+ URI::Generic.from_path(path: path).to_s
202
+ end
203
+ return if view_uris.empty?
204
+
205
+ @response_builder << create_code_lens(
206
+ node,
207
+ title: "Jump to view",
208
+ command_name: "rubyLsp.openFile",
209
+ arguments: [view_uris],
210
+ data: { type: "file" },
173
211
  )
212
+ end
174
213
 
214
+ sig { params(node: Prism::DefNode).void }
215
+ def add_route_code_lens_to_action(node)
216
+ class_name, _ = T.must(@constant_name_stack.last)
217
+ route = @client.route(controller: class_name, action: node.name.to_s)
175
218
  return unless route
176
219
 
177
- path = route[:path]
178
- verb = route[:verb]
179
- source_location = route[:source_location]
180
-
181
- arguments = [
182
- source_location,
183
- {
184
- start_line: node.location.start_line - 1,
185
- start_column: node.location.start_column,
186
- end_line: node.location.end_line - 1,
187
- end_column: node.location.end_column,
188
- },
189
- ]
220
+ file_path, line = route[:source_location]
190
221
 
191
222
  @response_builder << create_code_lens(
192
223
  node,
193
- title: [verb, path].join(" "),
224
+ title: "#{route[:verb]} #{route[:path]}",
194
225
  command_name: "rubyLsp.openFile",
195
- arguments: arguments,
226
+ arguments: [["file://#{file_path}#L#{line}"]],
196
227
  data: { type: "file" },
197
228
  )
198
229
  end
@@ -228,6 +259,7 @@ module RubyLsp
228
259
  sig { params(node: Prism::Node, name: String, command: String, kind: Symbol).void }
229
260
  def add_test_code_lens(node, name:, command:, kind:)
230
261
  return unless @path
262
+ return unless @global_state.test_library == "rails"
231
263
 
232
264
  arguments = [
233
265
  @path,
@@ -36,6 +36,9 @@ module RubyLsp
36
36
 
37
37
  extend T::Sig
38
38
 
39
+ sig { returns(String) }
40
+ attr_reader :rails_root
41
+
39
42
  sig { void }
40
43
  def initialize
41
44
  # Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
@@ -67,7 +70,8 @@ module RubyLsp
67
70
 
68
71
  begin
69
72
  count += 1
70
- read_response
73
+ initialize_response = T.must(read_response)
74
+ @rails_root = T.let(initialize_response[:root], String)
71
75
  rescue EmptyMessageError
72
76
  $stderr.puts("Ruby LSP Rails is retrying initialize (#{count})")
73
77
  retry if count < MAX_RETRIES
@@ -218,6 +222,11 @@ module RubyLsp
218
222
  true
219
223
  end
220
224
 
225
+ sig { override.returns(String) }
226
+ def rails_root
227
+ Dir.pwd
228
+ end
229
+
221
230
  private
222
231
 
223
232
  sig { override.params(request: String, params: T.nilable(T::Hash[Symbol, T.untyped])).void }
@@ -24,7 +24,7 @@ module RubyLsp
24
24
  routes_reloader = ::Rails.application.routes_reloader
25
25
  routes_reloader.execute_unless_loaded if routes_reloader&.respond_to?(:execute_unless_loaded)
26
26
 
27
- initialize_result = { result: { message: "ok" } }.to_json
27
+ initialize_result = { result: { message: "ok", root: ::Rails.root.to_s } }.to_json
28
28
  $stdout.write("Content-Length: #{initialize_result.length}\r\n\r\n#{initialize_result}")
29
29
 
30
30
  while @running
@@ -3,6 +3,6 @@
3
3
 
4
4
  module RubyLsp
5
5
  module Rails
6
- VERSION = "0.3.9"
6
+ VERSION = "0.3.11"
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-lsp-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.3.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shopify
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-10 00:00:00.000000000 Z
11
+ date: 2024-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-lsp
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  requirements: []
82
- rubygems_version: 3.5.14
82
+ rubygems_version: 3.5.15
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: A Ruby LSP addon for Rails