This repository was archived by the owner on Nov 30, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactor.lua
More file actions
414 lines (351 loc) · 10 KB
/
refactor.lua
File metadata and controls
414 lines (351 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
local class = require('java-core.utils.class')
local notify = require('java-core.utils.notify')
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
local List = require('java-core.utils.list')
local ui = require('java.utils.ui')
local refactor_edit_request_needed_actions = {
'convertVariableToField',
'extractConstant',
'extractField',
'extractMethod',
'extractVariable',
'extractVariableAllOccurrence',
}
local available_actions = List:new({
'assignField',
'assignVariable',
'convertAnonymousClassToNestedCommand',
'introduceParameter',
'invertVariable',
-- 'moveFile',
'moveInstanceMethod',
'moveStaticMember',
'moveType',
-- 'changeSignature',
-- 'extractInterface',
}):concat(refactor_edit_request_needed_actions)
---@class java-refactor.Refactor
---@field jdtls_client java-core.JdtlsClient
local Refactor = class()
---@param client vim.lsp.Client
function Refactor:_init(client)
self.jdtls_client = JdtlsClient(client)
end
---Run refactor command
---@param action_name jdtls.CodeActionCommand
---@param action_context lsp.CodeActionParams
---@param action_info lsp.LSPAny
function Refactor:refactor(action_name, action_context, action_info)
if not vim.tbl_contains(available_actions, action_name) then
notify.error(
string.format('Refactoring command "%s" is not supported', action_name)
)
return
end
if vim.tbl_contains(refactor_edit_request_needed_actions, action_name) then
local formatting_options = Refactor.make_formatting_options()
local selections
if vim.tbl_contains(refactor_edit_request_needed_actions, action_name) then
selections = self:get_selections(action_name, action_context)
end
local changes = self.jdtls_client:java_get_refactor_edit(
action_name,
action_context,
formatting_options,
selections,
vim.api.nvim_get_current_buf()
)
Refactor.perform_refactor_edit(changes)
elseif action_name == 'moveFile' then
self:move_file(action_info --[[@as jdtls.CodeActionMoveTypeCommandInfo]])
elseif action_name == 'moveType' then
self:move_type(
action_context,
action_info --[[@as jdtls.CodeActionMoveTypeCommandInfo]]
)
elseif action_name == 'moveStaticMember' then
self:move_static_member(
action_context,
action_info --[[@as jdtls.CodeActionMoveTypeCommandInfo]]
)
elseif action_name == 'moveInstanceMethod' then
self:move_instance_method(
action_context,
action_info --[[@as jdtls.CodeActionMoveTypeCommandInfo]]
)
end
end
---@private
---@param action_info jdtls.CodeActionMoveTypeCommandInfo
function Refactor:move_file(action_info)
if not action_info or not action_info.uri then
return
end
local move_des = self.jdtls_client:get_move_destination({
moveKind = 'moveResource',
sourceUris = { action_info.uri },
params = nil,
})
if
not move_des
or not move_des.destinations
or #move_des.destinations < 1
then
notify.error(
'Cannot find available Java packages to move the selected files to.'
)
return
end
---@type jdtls.ResourceMoveDestination[]
local destinations = move_des.destinations
local selected_destination = ui.select(
'Choose the target package',
destinations,
function(destination)
return destination.displayName .. ' ' .. destination.path
end
)
if not selected_destination then
return
end
local changes = self.jdtls_client:java_move({
moveKind = 'moveResource',
sourceUris = { action_info.uri },
params = nil,
destination = selected_destination,
})
Refactor.perform_refactor_edit(changes)
end
---@private
---@param action_context lsp.CodeActionParams
---@param action_info jdtls.CodeActionMoveTypeCommandInfo
function Refactor:move_instance_method(action_context, action_info)
local move_des = self.jdtls_client:get_move_destination({
moveKind = 'moveInstanceMethod',
sourceUris = { action_context.textDocument.uri },
params = action_context,
})
if move_des and move_des.errorMessage then
notify.error(move_des.errorMessage)
return
end
if
not move_des
or not move_des.destinations
or #move_des.destinations < 1
then
notify.error(
'Cannot find possible class targets to move the selected method to.'
)
return
end
---@type jdtls.InstanceMethodMoveDestination[]
local destinations = move_des.destinations
local method_name = action_info and action_info.displayName or ''
local selected_destination = ui.select(
string.format(
'Select the new class for the instance method %s',
method_name
),
destinations,
function(destination)
return destination.type .. ' ' .. destination.name
end,
{ prompt_single = true }
)
if not selected_destination then
return
end
self:perform_move('moveInstanceMethod', action_context, selected_destination)
end
---@private
---@param action_context lsp.CodeActionParams
---@param action_info jdtls.CodeActionMoveTypeCommandInfo
function Refactor:move_static_member(action_context, action_info)
local exclude = List:new()
if action_info.enclosingTypeName then
exclude:push(action_info.enclosingTypeName)
if
action_info.memberType == 55
or action_info.memberType == 71
or action_info.memberType == 81
then
exclude:push(
action_info.enclosingTypeName .. '.' .. action_info.displayName
)
end
end
local project_name = action_info and action_info.projectName or nil
local member_name = action_info
and action_info.displayName
and action_info.displayName
or ''
local selected_class = self:select_target_class(
string.format('Select the new class for the static member %s.', member_name),
project_name,
exclude
)
if not selected_class then
return
end
self:perform_move('moveStaticMember', action_context, selected_class)
end
---@private
---@param action_context lsp.CodeActionParams
---@param action_info jdtls.CodeActionMoveTypeCommandInfo
function Refactor:move_type(action_context, action_info)
if not action_info or not action_info.supportedDestinationKinds then
return
end
local selected_destination_kind = ui.select(
'What would you like to do?',
action_info.supportedDestinationKinds,
function(kind)
if kind == 'newFile' then
return string.format(
'Move type "%s" to new file',
action_info.displayName
)
else
return string.format(
'Move type "%s" to another class',
action_info.displayName
)
end
end
)
if not selected_destination_kind then
return
end
if selected_destination_kind == 'newFile' then
self:perform_move('moveTypeToNewFile', action_context)
else
local exclude = List:new()
if action_info.enclosingTypeName then
exclude:push(action_info.enclosingTypeName)
exclude:push(
action_info.enclosingTypeName .. ':' .. action_info.displayName
)
end
local selected_class = self:select_target_class(
string.format(
'Select the new class for the type %s.',
action_info.displayName
),
action_info.projectName,
exclude
)
if not selected_class then
return
end
self:perform_move('moveStaticMember', action_context, selected_class)
end
end
---@private
---@param move_kind string
---@param action_context lsp.CodeActionParams
---@param destination? jdtls.InstanceMethodMoveDestination | jdtls.ResourceMoveDestination | lsp.SymbolInformation
function Refactor:perform_move(move_kind, action_context, destination)
local changes = self.jdtls_client:java_move({
moveKind = move_kind,
sourceUris = { action_context.textDocument.uri },
params = action_context,
destination = destination,
})
Refactor.perform_refactor_edit(changes)
end
---@private
---@param changes jdtls.RefactorWorkspaceEdit
function Refactor.perform_refactor_edit(changes)
if not changes then
notify.warn('No edits suggested for the code action')
return
end
if changes.errorMessage then
notify.error(changes.errorMessage)
return
end
vim.lsp.util.apply_workspace_edit(changes.edit, 'utf-8')
if changes.command then
Refactor.run_lsp_client_command(
changes.command.command,
changes.command.arguments
)
end
end
---@private
---@param prompt string
---@param project_name string
---@param exclude string[]
function Refactor:select_target_class(prompt, project_name, exclude)
local classes = self.jdtls_client:java_search_symbols({
query = '*',
projectName = project_name,
sourceOnly = true,
})
---@type lsp.SymbolInformation[]
local filtered_classes = List:new(classes):filter(function(cls)
local type_name = cls.containerName .. '.' .. cls.name
return not vim.tbl_contains(exclude, type_name)
end)
local selected = ui.select(prompt, filtered_classes, function(cls)
return cls.containerName .. '.' .. cls.name
end)
return selected
end
---@private
---@param command_name string
---@param arguments any
function Refactor.run_lsp_client_command(command_name, arguments)
local command = vim.lsp.commands[command_name]
if not command then
notify.error('Command "' .. command_name .. '" is not supported')
return
end
command(arguments)
end
---@private
---@return lsp.FormattingOptions
function Refactor.make_formatting_options()
return {
tabSize = vim.bo.tabstop,
insertSpaces = vim.bo.expandtab,
}
end
---@private
---@param refactor_type jdtls.CodeActionCommand
---@param params lsp.CodeActionParams
---@return jdtls.SelectionInfo[]
function Refactor:get_selections(refactor_type, params)
local selections = List:new()
local buffer = vim.api.nvim_get_current_buf()
if
params.range.start.character == params.range['end'].character
and params.range.start.line == params.range['end'].line
then
local selection_res =
self.jdtls_client:java_infer_selection(refactor_type, params, buffer)
if not selection_res then
return selections
end
local selection = selection_res[1]
if selection.params and vim.islist(selection.params) then
local initialize_in =
ui.select('Initialize the field in', selection.params)
if not initialize_in then
return selections
end
selections:push(initialize_in)
end
selections:push(selection)
end
return selections
end
---@class jdtls.CodeActionMoveTypeCommandInfo
---@field displayName string
---@field enclosingTypeName string
---@field memberType number
---@field projectName string
---@field supportedDestinationKinds string[]
---@field uri? string
return Refactor