Skip to content

Commit 77a6079

Browse files
committed
feat: move all client commands to java-refactor
1 parent 1ad4911 commit 77a6079

File tree

7 files changed

+545
-77
lines changed

7 files changed

+545
-77
lines changed

lua/java-refactor/action.lua

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
local ui = require('java.utils.ui')
2+
local class = require('java-core.utils.class')
3+
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
4+
local RefactorCommands = require('java-refactor.refactor-commands')
5+
local notify = require('java-core.utils.notify')
6+
local List = require('java-core.utils.list')
7+
8+
---@class java-refactor.Action
9+
---@field client vim.lsp.Client
10+
---@field jdtls java-core.JdtlsClient
11+
local Action = class()
12+
13+
---@param client vim.lsp.Client
14+
function Action:_init(client)
15+
self.client = client
16+
self.jdtls = JdtlsClient(client)
17+
self.refactor = RefactorCommands(client)
18+
end
19+
20+
---@class java-refactor.RenameAction
21+
---@field length number
22+
---@field offset number
23+
---@field uri string
24+
25+
---@param params java-refactor.RenameAction[]
26+
function Action:rename(params)
27+
for _, rename in ipairs(params) do
28+
local buffer = vim.uri_to_bufnr(rename.uri)
29+
30+
local line
31+
32+
vim.api.nvim_buf_call(buffer, function()
33+
line = vim.fn.byte2line(rename.offset)
34+
end)
35+
36+
local start_char = rename.offset - vim.fn.line2byte(line) + 1
37+
38+
vim.api.nvim_win_set_cursor(0, { line, start_char })
39+
40+
vim.lsp.buf.rename(nil, {
41+
name = 'jdtls',
42+
bufnr = buffer,
43+
})
44+
end
45+
end
46+
47+
---@param params nvim.CodeActionParamsResponse
48+
function Action:generate_constructor(params)
49+
local status = self.jdtls:java_check_constructors_status(params.params)
50+
51+
if not status or not status.constructors then
52+
return
53+
end
54+
55+
local selected_constructor = ui.select(
56+
'Select super class constructor(s).',
57+
status.constructors,
58+
function(constructor)
59+
return string.format(
60+
'%s %s',
61+
constructor.name,
62+
table.concat(constructor.parameters, ', ')
63+
)
64+
end
65+
)
66+
67+
if not selected_constructor then
68+
return
69+
end
70+
71+
local selected_fields = ui.multi_select(
72+
'Select Fields:',
73+
status.fields,
74+
function(field)
75+
return field.name
76+
end
77+
)
78+
79+
local edit = self.jdtls:java_generate_constructor({
80+
context = params.params,
81+
constructors = { selected_constructor },
82+
fields = selected_fields or {},
83+
})
84+
85+
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
86+
end
87+
88+
---@param params nvim.CodeActionParamsResponse
89+
function Action:generate_to_string(params)
90+
local status = self.jdtls:java_check_to_string_status(params.params)
91+
92+
if status.exists then
93+
local prompt = string.format(
94+
'Method "toString()" already exists in the Class %s. Do you want to replace the implementation?',
95+
status.type
96+
)
97+
local choice = ui.select(prompt, { 'Replace', 'Cancel' })
98+
99+
if choice ~= 'Replace' then
100+
return
101+
end
102+
end
103+
104+
local fields = ui.multi_select(
105+
'Select the fields to include in the toString() method.',
106+
status.fields,
107+
function(field)
108+
return field.name
109+
end
110+
)
111+
112+
if not fields then
113+
return
114+
end
115+
116+
local edit = self.jdtls:java_generate_to_string({
117+
context = params.params,
118+
fields = fields,
119+
})
120+
121+
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
122+
end
123+
124+
---@param params nvim.CodeActionParamsResponse
125+
function Action:generate_hash_code_and_equals(params)
126+
local status = self.jdtls:java_check_hash_code_equals_status(params.params)
127+
128+
if not status or not status.fields or #status.fields < 1 then
129+
local message = string.format(
130+
'The operation is not applicable to the type %s.',
131+
status.type
132+
)
133+
notify.warn(message)
134+
return
135+
end
136+
137+
local regenerate = false
138+
139+
if status.existingMethods and #status.existingMethods > 0 then
140+
local prompt = string.format(
141+
'Methods %s already exists in the Class %s. Do you want to regenerate the implementation?',
142+
'Regenerate',
143+
'Cancel'
144+
)
145+
146+
local choice = ui.select(prompt, { 'Regenerate', 'Cancel' })
147+
148+
if choice == 'Regenerate' then
149+
regenerate = true
150+
end
151+
end
152+
153+
local fields = ui.multi_select(
154+
'Select the fields to include in the hashCode() and equals() methods.',
155+
status.fields,
156+
function(field)
157+
return field.name
158+
end
159+
)
160+
161+
if not fields or #fields < 1 then
162+
return
163+
end
164+
165+
local edit = self.jdtls:java_generate_hash_code_equals({
166+
context = params.params,
167+
fields = fields,
168+
regenerate = regenerate,
169+
})
170+
171+
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
172+
end
173+
174+
---@param params nvim.CodeActionParamsResponse
175+
function Action:generate_delegate_mothods_prompt(params)
176+
local status = self.jdtls:java_check_delegate_methods_status(params.params)
177+
178+
if not status or not status.delegateFields or #status.delegateFields < 1 then
179+
notify.warn('All delegatable methods are already implemented.')
180+
return
181+
end
182+
183+
local selected_delegate_field = ui.select(
184+
'Select target to generate delegates for.',
185+
status.delegateFields,
186+
function(field)
187+
return field.field.name .. ': ' .. field.field.type
188+
end
189+
)
190+
191+
if not selected_delegate_field then
192+
return
193+
end
194+
195+
if #selected_delegate_field.delegateMethods < 1 then
196+
notify.warn('All delegatable methods are already implemented.')
197+
return
198+
end
199+
200+
local selected_delegate_methods = ui.multi_select(
201+
'Select methods to generate delegates for.',
202+
selected_delegate_field.delegateMethods,
203+
function(method)
204+
return string.format(
205+
'%s.%s(%s)',
206+
selected_delegate_field.field.name,
207+
method.name,
208+
table.concat(method.parameters, ', ')
209+
)
210+
end
211+
)
212+
213+
if not selected_delegate_methods or #selected_delegate_methods < 1 then
214+
return
215+
end
216+
217+
local delegate_entries = List:new(selected_delegate_methods):map(
218+
---@param method jdtls.MethodBinding
219+
function(method)
220+
return {
221+
field = selected_delegate_field.field,
222+
delegateMethod = method,
223+
}
224+
end
225+
)
226+
227+
local edit = self.jdtls:java_generate_delegate_methods({
228+
context = params.params,
229+
delegateEntries = delegate_entries,
230+
})
231+
232+
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
233+
end
234+
235+
---@param command lsp.Command
236+
function Action:apply_refactoring_command(command)
237+
local action_name = command.arguments[1] --[[@as jdtls.CodeActionCommand]]
238+
local action_context = command.arguments[2] --[[@as lsp.CodeActionParams]]
239+
local action_info = command.arguments[3] --[[@as lsp.LSPAny]]
240+
241+
self.refactor:refactor(action_name, action_context, action_info)
242+
end
243+
244+
---comment
245+
---@param is_full_compile boolean
246+
---@return java-core.CompileWorkspaceStatus
247+
function Action:build_workspace(is_full_compile)
248+
return self.jdtls:java_build_workspace(is_full_compile, 0)
249+
end
250+
251+
function Action:clean_workspace()
252+
local workpace_path =
253+
vim.tbl_get(self.client, 'config', 'init_options', 'workspace')
254+
255+
local prompt = string.format('Do you want to delete "%s"', workpace_path)
256+
257+
local choice = ui.select(prompt, { 'Yes', 'No' })
258+
259+
if choice ~= 'Yes' then
260+
return
261+
end
262+
263+
vim.fn.delete(workpace_path, 'rf')
264+
end
265+
266+
---@class java-refactor.ApplyRefactoringCommandParams
267+
---@field bufnr number
268+
---@field client_id number
269+
---@field method string
270+
---@field params lsp.CodeActionParams
271+
---@field version number
272+
273+
return Action

lua/java-refactor/api/code-action.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local M = {}
2+
3+
---@param action_type string
4+
---@param filter? string
5+
local function run_code_action(action_type, filter)
6+
vim.lsp.buf.code_action({
7+
apply = true,
8+
context = {
9+
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0),
10+
only = { action_type },
11+
},
12+
filter = filter and function(refactor)
13+
return refactor.command.arguments[1] == filter
14+
end or nil,
15+
})
16+
end
17+
18+
function M.extract_variable()
19+
run_code_action('refactor.extract.variable', 'extractVariable')
20+
end
21+
22+
function M.extract_variable_all_occurrence()
23+
run_code_action('refactor.extract.variable', 'extractVariableAllOccurrence')
24+
end
25+
26+
function M.extract_constant()
27+
run_code_action('refactor.extract.constant')
28+
end
29+
30+
function M.extract_method()
31+
run_code_action('refactor.extract.function')
32+
end
33+
34+
function M.extract_field()
35+
run_code_action('refactor.extract.field')
36+
end
37+
38+
return M

0 commit comments

Comments
 (0)