Skip to content

Commit

Permalink
feat: add more refactor commands
Browse files Browse the repository at this point in the history
  • Loading branch information
s1n7ax committed Jul 13, 2024
1 parent 0dc87f7 commit 6adf3a7
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 61 deletions.
87 changes: 72 additions & 15 deletions lua/java-refactor/lsp-refactor-commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ local M = {
end
end,

---@class java-refactor.ApplyRefactoringCommandInfo
---@field bufnr number
---@field client_id number
---@field method string
---@field params lsp.CodeActionContext

---comment
---@param command lsp.Command
---@param command_info java-refactor.ApplyRefactoringCommandInfo
['java.action.applyRefactoringCommand'] = function(command, command_info)
---@param params java-refactor.ApplyRefactoringCommandParams
---@param command_info lsp.LSPAny
['java.action.applyRefactoringCommand'] = function(
command,
params,
command_info
)
runner(function()
vim.print('params', params)
vim.print('command_info', command_info)
local refactor_type = command.arguments[1] --[[@as jdtls.CodeActionCommand]]
local context = command_info.params

local client = vim.lsp.get_client_by_id(command_info.client_id)
local client = vim.lsp.get_client_by_id(params.client_id)

---@type java-refactor.RefactorCommands
local refactor_commands = RefactorCommands(client)
refactor_commands:refactor(refactor_type, context)
refactor_commands:refactor(refactor_type, params.params, command_info)
end)
.catch(get_error_handler('Failed to run refactoring command'))
.run()
Expand All @@ -74,7 +74,64 @@ id = vim.api.nvim_create_autocmd('LspAttach', {
end,
})

---@class java-refactor.RefactorContext
---@field context { diagnostics: any[], triggerKind: number }
---@field range nvim.Range
---@field textDocument { uri: string }
---@class java-refactor.ApplyRefactoringCommandParams
---@field bufnr number
---@field client_id number
---@field method string
---@field params lsp.CodeActionParams
---@field version number

--[[
{
arguments = {
'extractField',
{
context = {
diagnostics = {},
triggerKind = 1,
},
range = {
['end'] = {
character = 9,
line = 9,
},
start = {
character = 9,
line = 9,
},
},
textDocument = {
uri = 'file:///home/s1n7ax/Workspace/demo/src/main/java/com/example/demo/DemoApplication.java',
},
},
},
command = 'java.action.applyRefactoringCommand',
title = 'Extract to field',
})
{
bufnr = 3,
client_id = 2,
method = "textDocument/codeAction",
params = {
context = {
diagnostics = {},
triggerKind = 1
},
range = {
["end"] = {
character = 9,
line = 9
},
start = {
character = 9,
line = 9
}
},
textDocument = {
uri = "file:///home/s1n7ax/Workspace/demo/src/main/java/com/example/demo/DemoApplication.java"
}
},
version = 4
}
]]
135 changes: 89 additions & 46 deletions lua/java-refactor/refactor-commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ local JdtlsClient = require('java-core.ls.clients.jdtls-client')
local List = require('java-core.utils.list')
local ui = require('java.utils.ui')

local selections_needed_refactoring_commands = {
'convertVariableToField',
'extractConstant',
'extractField',
'extractMethod',
'extractVariable',
'extractVariableAllOccurrence',
}

local available_commands = {
-- 'assignField',
-- 'assignVariable',
'assignField',
'assignVariable',
-- 'changeSignature',
-- 'convertAnonymousClassToNestedCommand',
-- 'convertVariableToField',
'convertAnonymousClassToNestedCommand',
'convertVariableToField',
'extractConstant',
'extractField',
-- 'extractInterface',
'extractMethod',
'extractVariable',
'extractVariableAllOccurrence',
-- 'introduceParameter',
-- 'invertVariable',
'introduceParameter',
'invertVariable',
}

---@class java-refactor.RefactorCommands
Expand All @@ -31,60 +40,29 @@ end

---Run refactor command
---@param refactor_type jdtls.CodeActionCommand
---@param context lsp.CodeActionContext
function RefactorCommands:refactor(refactor_type, context)
---@param params lsp.CodeActionParams
function RefactorCommands:refactor(refactor_type, params)
if not vim.tbl_contains(available_commands, refactor_type) then
notify.error(
string.format('Refactoring command "%s" is not supported', refactor_type)
)
return
end

if not context then
context = vim.lsp.util.make_range_params(0)
context.context = {}
context.context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0)
end

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

local buffer = vim.api.nvim_get_current_buf()

local selections = List:new()

if
context.range.start.character == context.range['end'].character
and context.range.start.line == context.range['end'].line
then
local selection =
self.jdtls_client:java_infer_selection(refactor_type, context, buffer)[1]

if refactor_type == 'extractField' then
if selection.params and vim.islist(selection.params) then
local initialize_in =
ui.select('Initialize the field in', selection.params)

if not initialize_in then
return
end
params = params or RefactorCommands.make_action_params()
local formatting_options = RefactorCommands.make_formatting_options()
local selections

selections:push(initialize_in)
end
end

selections:push(selection)
vim.print(selections)
if selections_needed_refactoring_commands then
selections = self:get_selections(refactor_type, params)
end

local edit = self.jdtls_client:java_get_refactor_edit(
refactor_type,
context,
params,
formatting_options,
selections,
buffer
vim.api.nvim_get_current_buf()
)

if not edit then
Expand All @@ -100,6 +78,9 @@ function RefactorCommands:refactor(refactor_type, context)
)
end

---@private
---@param command_name string
---@param arguments any
function RefactorCommands.run_lsp_client_command(command_name, arguments)
local command = vim.lsp.commands[command_name]

Expand All @@ -111,4 +92,66 @@ function RefactorCommands.run_lsp_client_command(command_name, arguments)
command(arguments)
end

---Returns action params
---@private
---@return lsp.CodeActionParams
function RefactorCommands.make_action_params()
---@type lsp.CodeActionParams
local params = vim.lsp.util.make_range_params(0)

---@type lsp.CodeActionContext
local context = { diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0) }

params.context = context

return params
end

---@private
---@return lsp.FormattingOptions
function RefactorCommands.make_formatting_options()
return {
tabSize = vim.bo.tabstop,
insertSpaces = vim.bo.expandtab,
}
end

---@private
---@param refactor_type jdtls.CodeActionCommand
---@param params lsp.CodeActionParams
---@return jdtls.SelectionInfo[]
function RefactorCommands:get_selections(refactor_type, params)
local selections = List:new()
local buffer = vim.api.nvim_get_current_buf()

if
params.range.start.character == params.range['end'].character
and params.range.start.line == params.range['end'].line
then
local selection_res =
self.jdtls_client:java_infer_selection(refactor_type, params, buffer)

if not selection_res then
return selections
end

local selection = selection_res[1]

if selection.params and vim.islist(selection.params) then
local initialize_in =
ui.select('Initialize the field in', selection.params)

if not initialize_in then
return selections
end

selections:push(initialize_in)
end

selections:push(selection)
end

return selections
end

return RefactorCommands

0 comments on commit 6adf3a7

Please sign in to comment.