Skip to content

Commit c281695

Browse files
committed
feat: add support for more refactoring commands
1 parent ba8af51 commit c281695

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

lua/java-refactor/lsp-refactor-commands.lua

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
local ui = require('java.utils.ui')
1+
-- local ui = require('java.utils.ui')
22

33
local M = {
4+
45
commands = {
6+
-- ['java.action.applyRefactoringCommand'] = function() end,
7+
58
---@class java-refactor.RenameAction
69
---@field length number
710
---@field offset number
@@ -12,29 +15,21 @@ local M = {
1215
['java.action.rename'] = function(arguments)
1316
for _, rename in ipairs(arguments) do
1417
local buffer = vim.uri_to_bufnr(rename.uri)
18+
1519
local line
1620

1721
vim.api.nvim_buf_call(buffer, function()
1822
line = vim.fn.byte2line(rename.offset)
1923
end)
2024

2125
local start_char = rename.offset - vim.fn.line2byte(line) + 1
22-
local end_char = start_char + rename.length
23-
24-
local name = ui.input('Variable Name')
25-
26-
if not name then
27-
return
28-
end
29-
30-
vim.api.nvim_buf_set_text(
31-
buffer,
32-
line - 1,
33-
start_char,
34-
line - 1,
35-
end_char,
36-
{ name }
37-
)
26+
27+
vim.api.nvim_win_set_cursor(0, { line, start_char })
28+
29+
vim.lsp.buf.rename(nil, {
30+
name = 'jdtls',
31+
bufnr = buffer,
32+
})
3833
end
3934
end,
4035
},
@@ -47,7 +42,6 @@ id = vim.api.nvim_create_autocmd('LspAttach', {
4742
local client = vim.lsp.get_client_by_id(args.data.client_id)
4843
if client and client.name == 'jdtls' then
4944
for name, command in pairs(M.commands) do
50-
vim.print(name, command)
5145
vim.lsp.commands[name] = command
5246
end
5347

lua/java-refactor/refactor-commands.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,38 @@ local class = require('java-core.utils.class')
22
local notify = require('java-core.utils.notify')
33
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
44

5-
---@class java-refactor.ClientCommands
5+
---@class java-refactor.RefactorCommands
66
---@field client vim.lsp.Client
7+
---@field jdtls_client java-core.JdtlsClient
78
local RefactorCommands = class()
89

10+
---@param client vim.lsp.Client
911
function RefactorCommands:_init(client)
1012
self.client = client
1113
self.jdtls_client = JdtlsClient(client)
1214
end
1315

14-
function RefactorCommands:extract_variable()
16+
---Run refactor command
17+
---@param refactor_type jdtls.CodeActionCommand
18+
function RefactorCommands:refactor(refactor_type)
1519
local context = vim.lsp.util.make_range_params(0)
1620
context.context = {}
1721
context.context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0)
1822

23+
local formatting_options = {
24+
tabSize = vim.bo.tabstop,
25+
insertSpaces = vim.bo.expandtab,
26+
}
27+
1928
local buffer = vim.api.nvim_get_current_buf()
2029

2130
local selection =
22-
self.jdtls_client:java_infer_selection('extractVariable', context, buffer)
31+
self.jdtls_client:java_infer_selection(refactor_type, context, buffer)
2332

2433
local edit = self.jdtls_client:java_get_refactor_edit(
25-
'extractVariable',
34+
refactor_type,
2635
context,
27-
{},
36+
formatting_options,
2837
selection,
2938
buffer
3039
)
@@ -38,6 +47,7 @@ function RefactorCommands:extract_variable()
3847
end
3948

4049
function RefactorCommands.run_lsp_client_command(command_name, arguments)
50+
-- vim.print(command_name, arguments)
4151
local command = vim.lsp.commands[command_name]
4252

4353
if not command then

0 commit comments

Comments
 (0)