-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: set rename as a lsp client command
- Loading branch information
Showing
4 changed files
with
52 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ globals = { | |
'vim.wo', | ||
'vim.bo', | ||
'vim.opt', | ||
'vim.lsp', | ||
} | ||
read_globals = { | ||
'vim', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('java-refactor.lsp-refactor-commands') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters