Skip to content

Commit 731a6f5

Browse files
committed
feat: add support for move refactoring code commands
1 parent 7f68edd commit 731a6f5

File tree

1 file changed

+139
-63
lines changed

1 file changed

+139
-63
lines changed

lua/java-refactor/refactor-commands.lua

Lines changed: 139 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,19 @@ local refactor_edit_request_needed_actions = {
1313
'extractVariableAllOccurrence',
1414
}
1515

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({
2617
'assignField',
2718
'assignVariable',
28-
-- 'changeSignature',
2919
'convertAnonymousClassToNestedCommand',
30-
'convertVariableToField',
31-
'extractConstant',
32-
'extractField',
33-
-- 'extractInterface',
34-
'extractMethod',
35-
'extractVariable',
36-
'extractVariableAllOccurrence',
3720
'introduceParameter',
3821
'invertVariable',
39-
'moveFile',
22+
-- 'moveFile',
4023
'moveInstanceMethod',
4124
'moveStaticMember',
4225
'moveType',
43-
}
26+
-- 'changeSignature',
27+
-- 'extractInterface',
28+
}):concat(refactor_edit_request_needed_actions)
4429

4530
---@class java-refactor.RefactorCommands
4631
---@field jdtls_client java-core.JdtlsClient
@@ -67,9 +52,7 @@ function RefactorCommands:refactor(action_name, action_context, action_info)
6752
local formatting_options = RefactorCommands.make_formatting_options()
6853
local selections
6954

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
7356
selections = self:get_selections(action_name, action_context)
7457
end
7558

@@ -81,17 +64,9 @@ function RefactorCommands:refactor(action_name, action_context, action_info)
8164
vim.api.nvim_get_current_buf()
8265
)
8366

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]])
9570
elseif action_name == 'moveType' then
9671
self:move_type(
9772
action_context,
@@ -102,9 +77,113 @@ function RefactorCommands:refactor(action_name, action_context, action_info)
10277
action_context,
10378
action_info --[[@as jdtls.CodeActionMoveTypeCommandInfo]]
10479
)
80+
elseif action_name == 'moveInstanceMethod' then
81+
self:move_instance_method(
82+
action_context,
83+
action_info --[[@as jdtls.CodeActionMoveTypeCommandInfo]]
84+
)
10585
end
10686
end
10787

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+
108187
---@param action_context lsp.CodeActionParams
109188
---@param action_info jdtls.CodeActionMoveTypeCommandInfo
110189
function RefactorCommands:move_static_member(action_context, action_info)
@@ -139,21 +218,7 @@ function RefactorCommands:move_static_member(action_context, action_info)
139218
return
140219
end
141220

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)
157222
end
158223

159224
---@param action_context lsp.CodeActionParams
@@ -185,15 +250,8 @@ function RefactorCommands:move_type(action_context, action_info)
185250
return
186251
end
187252

188-
---@type jdtls.RefactorWorkspaceEdit
189-
local changes
190-
191253
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)
197255
else
198256
local exclude = List:new()
199257

@@ -217,12 +275,29 @@ function RefactorCommands:move_type(action_context, action_info)
217275
return
218276
end
219277

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
226301
end
227302

228303
vim.lsp.util.apply_workspace_edit(changes.edit, 'utf-8')
@@ -325,5 +400,6 @@ end
325400
---@field memberType number
326401
---@field projectName string
327402
---@field supportedDestinationKinds string[]
403+
---@field uri? string
328404

329405
return RefactorCommands

0 commit comments

Comments
 (0)