Skip to content

Commit ba8af51

Browse files
committed
refactor: set rename as a lsp client command
1 parent 7b841d5 commit ba8af51

File tree

4 files changed

+52
-42
lines changed

4 files changed

+52
-42
lines changed

.luacheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ globals = {
44
'vim.wo',
55
'vim.bo',
66
'vim.opt',
7+
'vim.lsp',
78
}
89
read_globals = {
910
'vim',

lua/java-refactor/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('java-refactor.lsp-refactor-commands')
Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
local ui = require('java.utils.ui')
22

33
local M = {
4-
---@class java-refactor.RenameAction
5-
---@field length number
6-
---@field offset number
7-
---@field uri string
8-
9-
---Rename a given item
10-
---@param arguments java-refactor.RenameAction[]
11-
['java.action.rename'] = function(arguments)
12-
for _, rename in ipairs(arguments) do
13-
local buffer = vim.uri_to_bufnr(rename.uri)
14-
local line
15-
16-
vim.api.nvim_buf_call(buffer, function()
17-
line = vim.fn.byte2line(rename.offset)
18-
end)
19-
20-
local start_char = rename.offset - vim.fn.line2byte(line) + 1
21-
local end_char = start_char + rename.length
4+
commands = {
5+
---@class java-refactor.RenameAction
6+
---@field length number
7+
---@field offset number
8+
---@field uri string
9+
10+
---Rename a given item
11+
---@param arguments java-refactor.RenameAction[]
12+
['java.action.rename'] = function(arguments)
13+
for _, rename in ipairs(arguments) do
14+
local buffer = vim.uri_to_bufnr(rename.uri)
15+
local line
16+
17+
vim.api.nvim_buf_call(buffer, function()
18+
line = vim.fn.byte2line(rename.offset)
19+
end)
20+
21+
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+
)
38+
end
39+
end,
40+
},
41+
}
2242

23-
local name = ui.input('Variable Name')
43+
local id
2444

25-
if not name then
26-
return
45+
id = vim.api.nvim_create_autocmd('LspAttach', {
46+
callback = function(args)
47+
local client = vim.lsp.get_client_by_id(args.data.client_id)
48+
if client and client.name == 'jdtls' then
49+
for name, command in pairs(M.commands) do
50+
vim.print(name, command)
51+
vim.lsp.commands[name] = command
2752
end
2853

29-
vim.api.nvim_buf_set_text(
30-
buffer,
31-
line - 1,
32-
start_char,
33-
line - 1,
34-
end_char,
35-
{ name }
36-
)
54+
vim.api.nvim_del_autocmd(id)
3755
end
3856
end,
39-
}
40-
41-
return M
57+
})

lua/java-refactor/refactor-commands.lua

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
local class = require('java-core.utils.class')
22
local notify = require('java-core.utils.notify')
3-
local lsp_refactor_commands = require('java-refactor.lsp-refactor-commands')
4-
53
local JdtlsClient = require('java-core.ls.clients.jdtls-client')
64

75
---@class java-refactor.ClientCommands
8-
---@field client lsp.Client
6+
---@field client vim.lsp.Client
97
local RefactorCommands = class()
108

119
function RefactorCommands:_init(client)
@@ -40,13 +38,7 @@ function RefactorCommands:extract_variable()
4038
end
4139

4240
function RefactorCommands.run_lsp_client_command(command_name, arguments)
43-
local command
44-
45-
if lsp_refactor_commands[command_name] then
46-
command = lsp_refactor_commands[command_name]
47-
else
48-
command = vim.lsp.commands[command_name]
49-
end
41+
local command = vim.lsp.commands[command_name]
5042

5143
if not command then
5244
notify.error('Command "' .. command_name .. '" is not supported')

0 commit comments

Comments
 (0)