@@ -13,34 +13,19 @@ local refactor_edit_request_needed_actions = {
13
13
' extractVariableAllOccurrence' ,
14
14
}
15
15
16
- local selections_needed_refactoring_commands = {
17
- ' convertVariableToField' ,
18
- ' extractConstant' ,
19
- ' extractField' ,
20
- ' extractMethod' ,
21
- ' extractVariable' ,
22
- ' extractVariableAllOccurrence' ,
23
- }
24
-
25
- local available_actions = {
16
+ local available_actions = List :new ({
26
17
' assignField' ,
27
18
' assignVariable' ,
28
- -- 'changeSignature',
29
19
' convertAnonymousClassToNestedCommand' ,
30
- ' convertVariableToField' ,
31
- ' extractConstant' ,
32
- ' extractField' ,
33
- -- 'extractInterface',
34
- ' extractMethod' ,
35
- ' extractVariable' ,
36
- ' extractVariableAllOccurrence' ,
37
20
' introduceParameter' ,
38
21
' invertVariable' ,
39
- ' moveFile' ,
22
+ -- 'moveFile',
40
23
' moveInstanceMethod' ,
41
24
' moveStaticMember' ,
42
25
' moveType' ,
43
- }
26
+ -- 'changeSignature',
27
+ -- 'extractInterface',
28
+ }):concat (refactor_edit_request_needed_actions )
44
29
45
30
--- @class java-refactor.RefactorCommands
46
31
--- @field jdtls_client java-core.JdtlsClient
@@ -67,9 +52,7 @@ function RefactorCommands:refactor(action_name, action_context, action_info)
67
52
local formatting_options = RefactorCommands .make_formatting_options ()
68
53
local selections
69
54
70
- if
71
- vim .tbl_contains (selections_needed_refactoring_commands , action_name )
72
- then
55
+ if vim .tbl_contains (refactor_edit_request_needed_actions , action_name ) then
73
56
selections = self :get_selections (action_name , action_context )
74
57
end
75
58
@@ -81,17 +64,9 @@ function RefactorCommands:refactor(action_name, action_context, action_info)
81
64
vim .api .nvim_get_current_buf ()
82
65
)
83
66
84
- if not changes then
85
- notify .warn (' No edits suggested for action' )
86
- return
87
- end
88
-
89
- vim .lsp .util .apply_workspace_edit (changes .edit , ' utf-8' )
90
-
91
- RefactorCommands .run_lsp_client_command (
92
- changes .command .command ,
93
- changes .command .arguments
94
- )
67
+ RefactorCommands .perform_refactor_edit (changes )
68
+ elseif action_name == ' moveFile' then
69
+ self :move_file (action_info --[[ @as jdtls.CodeActionMoveTypeCommandInfo]] )
95
70
elseif action_name == ' moveType' then
96
71
self :move_type (
97
72
action_context ,
@@ -102,9 +77,113 @@ function RefactorCommands:refactor(action_name, action_context, action_info)
102
77
action_context ,
103
78
action_info --[[ @as jdtls.CodeActionMoveTypeCommandInfo]]
104
79
)
80
+ elseif action_name == ' moveInstanceMethod' then
81
+ self :move_instance_method (
82
+ action_context ,
83
+ action_info --[[ @as jdtls.CodeActionMoveTypeCommandInfo]]
84
+ )
105
85
end
106
86
end
107
87
88
+ --- @param action_info jdtls.CodeActionMoveTypeCommandInfo
89
+ function RefactorCommands :move_file (action_info )
90
+ if not action_info or not action_info .uri then
91
+ return
92
+ end
93
+
94
+ local move_des = self .jdtls_client :get_move_destination ({
95
+ moveKind = ' moveResource' ,
96
+ sourceUris = { action_info .uri },
97
+ params = nil ,
98
+ })
99
+
100
+ vim .print (move_des )
101
+
102
+ if
103
+ not move_des
104
+ or not move_des .destinations
105
+ or # move_des .destinations < 1
106
+ then
107
+ notify .error (
108
+ ' Cannot find available Java packages to move the selected files to.'
109
+ )
110
+ return
111
+ end
112
+
113
+ --- @type jdtls.ResourceMoveDestination[]
114
+ local destinations = move_des .destinations
115
+
116
+ local selected_destination = ui .select (
117
+ ' Choose the target package' ,
118
+ destinations ,
119
+ function (destination )
120
+ return destination .displayName .. ' ' .. destination .path
121
+ end
122
+ )
123
+
124
+ if not selected_destination then
125
+ return
126
+ end
127
+
128
+ local changes = self .jdtls_client :java_move ({
129
+ moveKind = ' moveResource' ,
130
+ sourceUris = { action_info .uri },
131
+ params = nil ,
132
+ destination = selected_destination ,
133
+ })
134
+
135
+ RefactorCommands .perform_refactor_edit (changes )
136
+ end
137
+
138
+ --- @param action_context lsp.CodeActionParams
139
+ --- @param action_info jdtls.CodeActionMoveTypeCommandInfo
140
+ function RefactorCommands :move_instance_method (action_context , action_info )
141
+ local move_des = self .jdtls_client :get_move_destination ({
142
+ moveKind = ' moveInstanceMethod' ,
143
+ sourceUris = { action_context .textDocument .uri },
144
+ params = action_context ,
145
+ })
146
+
147
+ if move_des and move_des .errorMessage then
148
+ notify .error (move_des .errorMessage )
149
+ return
150
+ end
151
+
152
+ if
153
+ not move_des
154
+ or not move_des .destinations
155
+ or # move_des .destinations < 1
156
+ then
157
+ notify .error (
158
+ ' Cannot find possible class targets to move the selected method to.'
159
+ )
160
+ return
161
+ end
162
+
163
+ --- @type jdtls.InstanceMethodMoveDestination[]
164
+ local destinations = move_des .destinations
165
+
166
+ local method_name = action_info and action_info .displayName or ' '
167
+
168
+ local selected_destination = ui .select (
169
+ string.format (
170
+ ' Select the new class for the instance method %s' ,
171
+ method_name
172
+ ),
173
+ destinations ,
174
+ function (destination )
175
+ return destination .type .. ' ' .. destination .name
176
+ end ,
177
+ { prompt_single = true }
178
+ )
179
+
180
+ if not selected_destination then
181
+ return
182
+ end
183
+
184
+ self :perform_move (' moveInstanceMethod' , action_context , selected_destination )
185
+ end
186
+
108
187
--- @param action_context lsp.CodeActionParams
109
188
--- @param action_info jdtls.CodeActionMoveTypeCommandInfo
110
189
function RefactorCommands :move_static_member (action_context , action_info )
@@ -139,21 +218,7 @@ function RefactorCommands:move_static_member(action_context, action_info)
139
218
return
140
219
end
141
220
142
- local changes = self .jdtls_client :java_move ({
143
- moveKind = ' moveStaticMember' ,
144
- sourceUris = { action_context .textDocument .uri },
145
- params = action_context ,
146
- destination = selected_class ,
147
- })
148
-
149
- vim .lsp .util .apply_workspace_edit (changes .edit , ' utf-8' )
150
-
151
- if changes .command then
152
- RefactorCommands .run_lsp_client_command (
153
- changes .command .command ,
154
- changes .command .arguments
155
- )
156
- end
221
+ self :perform_move (' moveStaticMember' , action_context , selected_class )
157
222
end
158
223
159
224
--- @param action_context lsp.CodeActionParams
@@ -185,15 +250,8 @@ function RefactorCommands:move_type(action_context, action_info)
185
250
return
186
251
end
187
252
188
- --- @type jdtls.RefactorWorkspaceEdit
189
- local changes
190
-
191
253
if selected_destination_kind == ' newFile' then
192
- changes = self .jdtls_client :java_move ({
193
- moveKind = ' moveTypeToNewFile' ,
194
- sourceUris = { action_context .textDocument .uri },
195
- params = action_context ,
196
- })
254
+ self :perform_move (' moveTypeToNewFile' , action_context )
197
255
else
198
256
local exclude = List :new ()
199
257
@@ -217,12 +275,29 @@ function RefactorCommands:move_type(action_context, action_info)
217
275
return
218
276
end
219
277
220
- changes = self .jdtls_client :java_move ({
221
- moveKind = ' moveStaticMember' ,
222
- sourceUris = { action_context .textDocument .uri },
223
- params = action_context ,
224
- destination = selected_class ,
225
- })
278
+ self :perform_move (' moveStaticMember' , action_context , selected_class )
279
+ end
280
+ end
281
+
282
+ --- @param move_kind string
283
+ --- @param action_context lsp.CodeActionParams
284
+ --- @param destination ? jdtls.InstanceMethodMoveDestination | jdtls.ResourceMoveDestination | lsp.SymbolInformation
285
+ function RefactorCommands :perform_move (move_kind , action_context , destination )
286
+ local changes = self .jdtls_client :java_move ({
287
+ moveKind = move_kind ,
288
+ sourceUris = { action_context .textDocument .uri },
289
+ params = action_context ,
290
+ destination = destination ,
291
+ })
292
+
293
+ RefactorCommands .perform_refactor_edit (changes )
294
+ end
295
+
296
+ --- @param changes jdtls.RefactorWorkspaceEdit
297
+ function RefactorCommands .perform_refactor_edit (changes )
298
+ if not changes then
299
+ notify .warn (' No edits suggested for the code action' )
300
+ return
226
301
end
227
302
228
303
vim .lsp .util .apply_workspace_edit (changes .edit , ' utf-8' )
325
400
--- @field memberType number
326
401
--- @field projectName string
327
402
--- @field supportedDestinationKinds string[]
403
+ --- @field uri ? string
328
404
329
405
return RefactorCommands
0 commit comments