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-commands.lua
More file actions
96 lines (79 loc) · 2.26 KB
/
refactor-commands.lua
File metadata and controls
96 lines (79 loc) · 2.26 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
local class = require('java-core.utils.class')
local notify = require('java-core.utils.notify')
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
local available_commands = {
-- 'assignField',
-- 'assignVariable',
-- 'changeSignature',
-- 'convertAnonymousClassToNestedCommand',
-- 'convertVariableToField',
'extractConstant',
'extractField',
-- 'extractInterface',
'extractMethod',
'extractVariable',
'extractVariableAllOccurrence',
-- 'introduceParameter',
-- 'invertVariable',
}
---@class java-refactor.RefactorCommands
---@field jdtls_client java-core.JdtlsClient
local RefactorCommands = class()
---@param client vim.lsp.Client
function RefactorCommands:_init(client)
self.jdtls_client = JdtlsClient(client)
end
---Run refactor command
---@param refactor_type jdtls.CodeActionCommand
---@param context lsp.CodeActionContext
function RefactorCommands:refactor(refactor_type, context)
if not vim.tbl_contains(available_commands, refactor_type) then
notify.error(
string.format('Refactoring command "%s" is not supported', refactor_type)
)
return
end
if not context then
context = vim.lsp.util.make_range_params(0)
context.context = {}
context.context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0)
end
local formatting_options = {
tabSize = vim.bo.tabstop,
insertSpaces = vim.bo.expandtab,
}
local buffer = vim.api.nvim_get_current_buf()
local selection = {}
if
context.range.start.character == context.range['end'].character
and context.range.start.line == context.range['end'].line
then
selection =
self.jdtls_client:java_infer_selection(refactor_type, context, buffer)
end
local edit = self.jdtls_client:java_get_refactor_edit(
refactor_type,
context,
formatting_options,
selection,
buffer
)
if not edit then
notify.warn('No edits suggested for action')
return
end
vim.lsp.util.apply_workspace_edit(edit.edit, 'utf-8')
RefactorCommands.run_lsp_client_command(
edit.command.command,
edit.command.arguments
)
end
function RefactorCommands.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
return RefactorCommands