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 pathaction.lua
More file actions
322 lines (260 loc) · 7.67 KB
/
action.lua
File metadata and controls
322 lines (260 loc) · 7.67 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
local ui = require('java.utils.ui')
local class = require('java-core.utils.class')
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
local RefactorCommands = require('java-refactor.refactor')
local notify = require('java-core.utils.notify')
local List = require('java-core.utils.list')
---@class java-refactor.Action
---@field client vim.lsp.Client
---@field jdtls java-core.JdtlsClient
local Action = class()
---@param client vim.lsp.Client
function Action:_init(client)
self.client = client
self.jdtls = JdtlsClient(client)
self.refactor = RefactorCommands(client)
end
---@class java-refactor.RenameAction
---@field length number
---@field offset number
---@field uri string
---@param params java-refactor.RenameAction[]
function Action.rename(params)
for _, rename in ipairs(params) do
local buffer = vim.uri_to_bufnr(rename.uri)
local line
vim.api.nvim_buf_call(buffer, function()
line = vim.fn.byte2line(rename.offset)
end)
local start_char = rename.offset - vim.fn.line2byte(line) + 1
vim.api.nvim_win_set_cursor(0, { line, start_char })
vim.lsp.buf.rename(nil, {
name = 'jdtls',
bufnr = buffer,
})
end
end
---@param params nvim.CodeActionParamsResponse
function Action:generate_constructor(params)
local status = self.jdtls:java_check_constructors_status(params.params)
if not status or not status.constructors then
return
end
local selected_constructor = ui.select(
'Select super class constructor(s).',
status.constructors,
function(constructor)
return string.format(
'%s %s',
constructor.name,
table.concat(constructor.parameters, ', ')
)
end
)
if not selected_constructor then
return
end
local selected_fields = ui.multi_select(
'Select Fields:',
status.fields,
function(field)
return field.name
end
)
local edit = self.jdtls:java_generate_constructor({
context = params.params,
constructors = { selected_constructor },
fields = selected_fields or {},
})
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
end
---@param params nvim.CodeActionParamsResponse
function Action:generate_to_string(params)
local status = self.jdtls:java_check_to_string_status(params.params)
if status.exists then
local prompt = string.format(
'Method "toString()" already exists in the Class %s. Do you want to replace the implementation?',
status.type
)
local choice = ui.select(prompt, { 'Replace', 'Cancel' })
if choice ~= 'Replace' then
return
end
end
local fields = ui.multi_select(
'Select the fields to include in the toString() method.',
status.fields,
function(field)
return field.name
end
)
if not fields then
return
end
local edit = self.jdtls:java_generate_to_string({
context = params.params,
fields = fields,
})
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
end
---@param params nvim.CodeActionParamsResponse
function Action:generate_hash_code_and_equals(params)
local status = self.jdtls:java_check_hash_code_equals_status(params.params)
if not status or not status.fields or #status.fields < 1 then
local message = string.format(
'The operation is not applicable to the type %s.',
status.type
)
notify.warn(message)
return
end
local regenerate = false
if status.existingMethods and #status.existingMethods > 0 then
local prompt = string.format(
'Methods %s already exists in the Class %s. Do you want to regenerate the implementation?',
'Regenerate',
'Cancel'
)
local choice = ui.select(prompt, { 'Regenerate', 'Cancel' })
if choice == 'Regenerate' then
regenerate = true
end
end
local fields = ui.multi_select(
'Select the fields to include in the hashCode() and equals() methods.',
status.fields,
function(field)
return field.name
end
)
if not fields or #fields < 1 then
return
end
local edit = self.jdtls:java_generate_hash_code_equals({
context = params.params,
fields = fields,
regenerate = regenerate,
})
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
end
---@param params nvim.CodeActionParamsResponse
function Action:generate_delegate_mothods_prompt(params)
local status = self.jdtls:java_check_delegate_methods_status(params.params)
if not status or not status.delegateFields or #status.delegateFields < 1 then
notify.warn('All delegatable methods are already implemented.')
return
end
local selected_delegate_field = ui.select(
'Select target to generate delegates for.',
status.delegateFields,
function(field)
return field.field.name .. ': ' .. field.field.type
end
)
if not selected_delegate_field then
return
end
if #selected_delegate_field.delegateMethods < 1 then
notify.warn('All delegatable methods are already implemented.')
return
end
local selected_delegate_methods = ui.multi_select(
'Select methods to generate delegates for.',
selected_delegate_field.delegateMethods,
function(method)
return string.format(
'%s.%s(%s)',
selected_delegate_field.field.name,
method.name,
table.concat(method.parameters, ', ')
)
end
)
if not selected_delegate_methods or #selected_delegate_methods < 1 then
return
end
local delegate_entries = List:new(selected_delegate_methods):map(
---@param method jdtls.MethodBinding
function(method)
return {
field = selected_delegate_field.field,
delegateMethod = method,
}
end
)
local edit = self.jdtls:java_generate_delegate_methods({
context = params.params,
delegateEntries = delegate_entries,
})
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
end
---@param command lsp.Command
function Action:apply_refactoring_command(command)
local action_name = command.arguments[1] --[[@as jdtls.CodeActionCommand]]
local action_context = command.arguments[2] --[[@as lsp.CodeActionParams]]
local action_info = command.arguments[3] --[[@as lsp.LSPAny]]
self.refactor:refactor(action_name, action_context, action_info)
end
---comment
---@param is_full_compile boolean
---@return java-core.CompileWorkspaceStatus
function Action:build_workspace(is_full_compile)
return self.jdtls:java_build_workspace(is_full_compile, 0)
end
function Action:clean_workspace()
local workpace_path =
vim.tbl_get(self.client, 'config', 'init_options', 'workspace')
local prompt = string.format('Do you want to delete "%s"', workpace_path)
local choice = ui.select(prompt, { 'Yes', 'No' })
if choice ~= 'Yes' then
return
end
vim.fn.delete(workpace_path, 'rf')
end
---@class java-refactor.ApplyRefactoringCommandParams
---@field bufnr number
---@field client_id number
---@field method string
---@field params lsp.CodeActionParams
---@field version number
---@param params nvim.CodeActionParamsResponse
function Action:override_methods_prompt(params)
local status = self.jdtls:list_overridable_methods(params.params)
if not status or not status.methods or #status.methods < 1 then
notify.warn('No methods to override.')
return
end
local selected_methods = ui.multi_select(
'Select methods to override.',
status.methods,
function(method)
return string.format(
'%s(%s)',
method.name,
table.concat(method.parameters, ', ')
)
end
)
if not selected_methods or #selected_methods < 1 then
return
end
local edit =
self.jdtls:add_overridable_methods(params.params, selected_methods)
vim.lsp.util.apply_workspace_edit(edit, 'utf-8')
end
---@param selections jdtls.ImportSelection[]
function Action.choose_imports(selections)
local selected_candidates = {}
for _, selection in ipairs(selections) do
local selected_candidate = ui.select_sync(
'Select methods to override.',
selection.candidates,
function(candidate, index)
return index .. ' ' .. candidate.fullyQualifiedName
end
)
table.insert(selected_candidates, selected_candidate)
end
return selected_candidates
end
return Action