Skip to content

Commit f4fe93b

Browse files
committed
feat: add extract variable API
1 parent 8aed200 commit f4fe93b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local M = {
2+
---@class java-refactor.RenameAction
3+
---@field length number
4+
---@field offset number
5+
---@field uri string
6+
7+
---Rename a given item
8+
---@param arguments java-refactor.RenameAction[]
9+
['java.action.rename'] = function(arguments)
10+
for _, rename in ipairs(arguments) do
11+
local buffer = vim.uri_to_bufnr(rename.uri)
12+
local line
13+
14+
vim.api.nvim_buf_call(buffer, function()
15+
line = vim.fn.byte2line(rename.offset)
16+
end)
17+
18+
local start_char = rename.offset - vim.fn.line2byte(line) + 1
19+
local end_char = start_char + rename.length
20+
21+
local name = ui.input({ prompt = 'name' })
22+
23+
if not name then
24+
return
25+
end
26+
27+
vim.api.nvim_buf_set_text(
28+
buffer,
29+
line - 1,
30+
start_char,
31+
line - 1,
32+
end_char,
33+
{ name }
34+
)
35+
end
36+
end,
37+
}
38+
39+
return M
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local class = require('java-core.utils.class')
2+
local notify = require('java-core.utils.notify')
3+
4+
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
5+
6+
---@class java-refactor.ClientCommands
7+
---@field client lsp.Client
8+
local RefactorCommands = class()
9+
10+
function RefactorCommands:_init(client)
11+
self.client = client
12+
self.jdtls_client = JdtlsClient(client)
13+
end
14+
15+
function RefactorCommands:extract_variable()
16+
local context = vim.lsp.util.make_range_params(0)
17+
context.context = {}
18+
context.context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0)
19+
20+
local buffer = vim.api.nvim_get_current_buf()
21+
22+
local selection =
23+
self.jdtls_client:java_infer_selection('extractVariable', context, buffer)
24+
25+
local edit = self.jdtls_client:java_get_refactor_edit(
26+
'extractVariable',
27+
context,
28+
{},
29+
selection,
30+
buffer
31+
)
32+
33+
vim.print(vim.lsp.handlers)
34+
vim.lsp.util.apply_workspace_edit(edit.edit, 'utf-8')
35+
36+
RefactorCommands.run_lsp_client_command(
37+
edit.command.command,
38+
edit.command.arguments
39+
)
40+
end
41+
42+
function RefactorCommands.run_lsp_client_command(command_name, arguments)
43+
local command = vim.lsp.commands[command_name]
44+
45+
if not command then
46+
notify.error('Command "' .. command_name .. '" is not supported')
47+
return
48+
end
49+
50+
vim.lsp.commands[command_name](arguments)
51+
end
52+
53+
return RefactorCommands

0 commit comments

Comments
 (0)