Skip to content

Commit

Permalink
refactor: set rename as a lsp client command
Browse files Browse the repository at this point in the history
  • Loading branch information
s1n7ax committed Jul 10, 2024
1 parent 7b841d5 commit ba8af51
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ globals = {
'vim.wo',
'vim.bo',
'vim.opt',
'vim.lsp',
}
read_globals = {
'vim',
Expand Down
1 change: 1 addition & 0 deletions lua/java-refactor/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('java-refactor.lsp-refactor-commands')
80 changes: 48 additions & 32 deletions lua/java-refactor/lsp-refactor-commands.lua
Original file line number Diff line number Diff line change
@@ -1,41 +1,57 @@
local ui = require('java.utils.ui')

local M = {
---@class java-refactor.RenameAction
---@field length number
---@field offset number
---@field uri string

---Rename a given item
---@param arguments java-refactor.RenameAction[]
['java.action.rename'] = function(arguments)
for _, rename in ipairs(arguments) 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
local end_char = start_char + rename.length
commands = {
---@class java-refactor.RenameAction
---@field length number
---@field offset number
---@field uri string

---Rename a given item
---@param arguments java-refactor.RenameAction[]
['java.action.rename'] = function(arguments)
for _, rename in ipairs(arguments) 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
local end_char = start_char + rename.length

local name = ui.input('Variable Name')

if not name then
return
end

vim.api.nvim_buf_set_text(
buffer,
line - 1,
start_char,
line - 1,
end_char,
{ name }
)
end
end,
},
}

local name = ui.input('Variable Name')
local id

if not name then
return
id = vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client and client.name == 'jdtls' then
for name, command in pairs(M.commands) do
vim.print(name, command)
vim.lsp.commands[name] = command
end

vim.api.nvim_buf_set_text(
buffer,
line - 1,
start_char,
line - 1,
end_char,
{ name }
)
vim.api.nvim_del_autocmd(id)
end
end,
}

return M
})
12 changes: 2 additions & 10 deletions lua/java-refactor/refactor-commands.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
local class = require('java-core.utils.class')
local notify = require('java-core.utils.notify')
local lsp_refactor_commands = require('java-refactor.lsp-refactor-commands')

local JdtlsClient = require('java-core.ls.clients.jdtls-client')

---@class java-refactor.ClientCommands
---@field client lsp.Client
---@field client vim.lsp.Client
local RefactorCommands = class()

function RefactorCommands:_init(client)
Expand Down Expand Up @@ -40,13 +38,7 @@ function RefactorCommands:extract_variable()
end

function RefactorCommands.run_lsp_client_command(command_name, arguments)
local command

if lsp_refactor_commands[command_name] then
command = lsp_refactor_commands[command_name]
else
command = vim.lsp.commands[command_name]
end
local command = vim.lsp.commands[command_name]

if not command then
notify.error('Command "' .. command_name .. '" is not supported')
Expand Down

0 comments on commit ba8af51

Please sign in to comment.