Skip to content

Commit

Permalink
feat: add support for more refactoring commands
Browse files Browse the repository at this point in the history
  • Loading branch information
s1n7ax committed Jul 10, 2024
1 parent ba8af51 commit c281695
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
30 changes: 12 additions & 18 deletions lua/java-refactor/lsp-refactor-commands.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local ui = require('java.utils.ui')
-- local ui = require('java.utils.ui')

local M = {

commands = {
-- ['java.action.applyRefactoringCommand'] = function() end,

---@class java-refactor.RenameAction
---@field length number
---@field offset number
Expand All @@ -12,29 +15,21 @@ local M = {
['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 }
)

vim.api.nvim_win_set_cursor(0, { line, start_char })

vim.lsp.buf.rename(nil, {
name = 'jdtls',
bufnr = buffer,
})
end
end,
},
Expand All @@ -47,7 +42,6 @@ id = vim.api.nvim_create_autocmd('LspAttach', {
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

Expand Down
20 changes: 15 additions & 5 deletions lua/java-refactor/refactor-commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ local class = require('java-core.utils.class')
local notify = require('java-core.utils.notify')
local JdtlsClient = require('java-core.ls.clients.jdtls-client')

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

---@param client vim.lsp.Client
function RefactorCommands:_init(client)
self.client = client
self.jdtls_client = JdtlsClient(client)
end

function RefactorCommands:extract_variable()
---Run refactor command
---@param refactor_type jdtls.CodeActionCommand
function RefactorCommands:refactor(refactor_type)
local context = vim.lsp.util.make_range_params(0)
context.context = {}
context.context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0)

local formatting_options = {
tabSize = vim.bo.tabstop,
insertSpaces = vim.bo.expandtab,
}

local buffer = vim.api.nvim_get_current_buf()

local selection =
self.jdtls_client:java_infer_selection('extractVariable', context, buffer)
self.jdtls_client:java_infer_selection(refactor_type, context, buffer)

local edit = self.jdtls_client:java_get_refactor_edit(
'extractVariable',
refactor_type,
context,
{},
formatting_options,
selection,
buffer
)
Expand All @@ -38,6 +47,7 @@ function RefactorCommands:extract_variable()
end

function RefactorCommands.run_lsp_client_command(command_name, arguments)
-- vim.print(command_name, arguments)
local command = vim.lsp.commands[command_name]

if not command then
Expand Down

0 comments on commit c281695

Please sign in to comment.