@@ -207,27 +207,43 @@ def refactor_method
207
207
208
208
# Find the closest method declaration node, so that we place the refactor in a valid position
209
209
node_context = @document . locate ( @document . parse_result . value , start_index , node_types : [ Prism ::DefNode ] )
210
- closest_def = T . cast ( node_context . node , Prism :: DefNode )
211
- return Error ::InvalidTargetRange if closest_def . nil?
210
+ closest_node = node_context . node
211
+ return Error ::InvalidTargetRange unless closest_node
212
212
213
- end_keyword_loc = closest_def . end_keyword_loc
214
- return Error ::InvalidTargetRange if end_keyword_loc . nil?
213
+ target_range = if closest_node . is_a? ( Prism ::DefNode )
214
+ end_keyword_loc = closest_node . end_keyword_loc
215
+ return Error ::InvalidTargetRange unless end_keyword_loc
215
216
216
- end_line = end_keyword_loc . end_line - 1
217
- character = end_keyword_loc . end_column
218
- indentation = " " * end_keyword_loc . start_column
219
- target_range = {
220
- start : { line : end_line , character : character } ,
221
- end : { line : end_line , character : character } ,
222
- }
217
+ end_line = end_keyword_loc . end_line - 1
218
+ character = end_keyword_loc . end_column
219
+ indentation = " " * end_keyword_loc . start_column
223
220
224
- new_method_source = <<~RUBY . chomp
221
+ new_method_source = <<~RUBY . chomp
225
222
226
223
227
- #{ indentation } def #{ NEW_METHOD_NAME }
228
- #{ indentation } #{ extracted_source }
229
- #{ indentation } end
230
- RUBY
224
+ #{ indentation } def #{ NEW_METHOD_NAME }
225
+ #{ indentation } #{ extracted_source }
226
+ #{ indentation } end
227
+ RUBY
228
+
229
+ {
230
+ start : { line : end_line , character : character } ,
231
+ end : { line : end_line , character : character } ,
232
+ }
233
+ else
234
+ new_method_source = <<~RUBY
235
+ #{ indentation } def #{ NEW_METHOD_NAME }
236
+ #{ indentation } #{ extracted_source . gsub ( "\n " , "\n " ) }
237
+ #{ indentation } end
238
+
239
+ RUBY
240
+
241
+ line = [ 0 , source_range . dig ( :start , :line ) - 1 ] . max
242
+ {
243
+ start : { line : line , character : source_range . dig ( :start , :character ) } ,
244
+ end : { line : line , character : source_range . dig ( :start , :character ) } ,
245
+ }
246
+ end
231
247
232
248
Interface ::CodeAction . new (
233
249
title : CodeActions ::EXTRACT_TO_METHOD_TITLE ,
0 commit comments