Skip to content

Commit

Permalink
organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
s1n7ax committed Jul 24, 2024
1 parent 98db44a commit c6189ef
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 148 deletions.
19 changes: 19 additions & 0 deletions lua/java-refactor/api/build.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---@param client_command jdtls.ClientCommand
local function run_client_command(client_command, ...)
local handlers = require('java-refactor.client-command-handlers')
handlers[client_command](...)
end

local M = {
build_workspace = function()
local ClientCommand = require('java-refactor.client-command')
run_client_command(ClientCommand.COMPILE_WORKSPACE, true)
end,

clean_workspace = function()
local ClientCommand = require('java-refactor.client-command')
run_client_command(ClientCommand.CLEAN_WORKSPACE)
end,
}

return M
38 changes: 0 additions & 38 deletions lua/java-refactor/api/code-action.lua

This file was deleted.

38 changes: 38 additions & 0 deletions lua/java-refactor/api/refactor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---@param action_type string
---@param filter? string
local function run_code_action(action_type, filter)
vim.lsp.buf.code_action({
apply = true,
context = {
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0),
only = { action_type },
},
filter = filter and function(refactor)
return refactor.command.arguments[1] == filter
end or nil,
})
end

local M = {
extract_variable = function()
run_code_action('refactor.extract.variable', 'extractVariable')
end,

extract_variable_all_occurrence = function()
run_code_action('refactor.extract.variable', 'extractVariableAllOccurrence')
end,

extract_constant = function()
run_code_action('refactor.extract.constant')
end,

extract_method = function()
run_code_action('refactor.extract.function')
end,

extract_field = function()
run_code_action('refactor.extract.field')
end,
}

return M
102 changes: 102 additions & 0 deletions lua/java-refactor/client-command-handlers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
local ClientCommand = require('java-refactor.client-command')

---@param message string
---@param func fun(action: java-refactor.Action)
local run = function(message, func)
local runner = require('async.runner')
local get_error_handler = require('java-refactor.utils.error_handler')
local instance = require('java-refactor.utils.instance-factory')

runner(function()
func(instance.get_action())
end)
.catch(get_error_handler(message))
.run()
end

local M = {
---@param params java-refactor.RenameAction[]
[ClientCommand.RENAME_COMMAND] = function(params)
run('Failed to rename the symbol', function(action)
action:rename(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[ClientCommand.GENERATE_CONSTRUCTORS_PROMPT] = function(_, params)
run('Failed to generate constructor', function(action)
action:generate_constructor(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[ClientCommand.GENERATE_TOSTRING_PROMPT] = function(_, params)
run('Failed to generate toString', function(action)
action:generate_to_string(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[ClientCommand.HASHCODE_EQUALS_PROMPT] = function(_, params)
run('Failed to generate hash code and equals', function(action)
action:generate_hash_code_and_equals(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[ClientCommand.GENERATE_DELEGATE_METHODS_PROMPT] = function(_, params)
run('Failed to generate delegate methods', function(action)
action:generate_delegate_mothods_prompt(params)
end)
end,

---@param command lsp.Command
[ClientCommand.APPLY_REFACTORING_COMMAND] = function(command)
run('Failed to apply refactoring command', function(action)
action:apply_refactoring_command(command)
end)
end,

---@param is_full_build boolean
[ClientCommand.COMPILE_WORKSPACE] = function(is_full_build)
run('Failed to build workspace', function(action)
action:build_workspace(is_full_build)
require('java-core.utils.notify').info('Successfully built the workspace')
end)
end,

[ClientCommand.CLEAN_WORKSPACE] = function()
run('Failed to clean workspace', function(action)
action:clean_workspace()
require('java-core.utils.notify').info(
'Successfully cleared the workspace cache'
.. '\nPlease close and reopen neovim to restart JDTLS'
)
end)
end,
}

local ignored_commands = { ClientCommand.REFRESH_BUNDLES_COMMAND }

for _, command in pairs(ClientCommand) do
if not M[command] and not vim.tbl_contains(ignored_commands, command) then
local message = string.format(
'"%s" is not supported yet!'
.. '\nPlease request the feature using below link'
.. '\nhttps://github.com/nvim-java/nvim-java/issues/new?assignees='
.. '&labels=enhancement&projects=&template=feature_request.yml&title=feature%%3A+',
command
)

M[command] = function()
require('java-core.utils.notify').warn(message)

return vim.lsp.rpc_response_error(
vim.lsp.protocol.ErrorCodes.MethodNotFound,
'Not implemented yes'
)
end
end
end

return M
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local M = {}

M.commands = {
---@enum jdtls.ClientCommand
local M = {
ADD_TO_SOURCEPATH = 'java.project.addToSourcePath',
ADD_TO_SOURCEPATH_CMD = 'java.project.addToSourcePath.command',
APPLY_REFACTORING_COMMAND = 'java.action.applyRefactoringCommand',
Expand Down Expand Up @@ -91,100 +90,4 @@ M.commands = {
UPGRADE_GRADLE_WRAPPER_CMD = 'java.project.upgradeGradle.command',
}

---@param message string
---@param func fun(action: java-refactor.Action)
local run = function(message, func)
local runner = require('async.runner')
local get_error_handler = require('java-refactor.utils.error_handler')
local instance = require('java-refactor.utils.instance-factory')

runner(function()
func(instance.get_action())
end)
.catch(get_error_handler(message))
.run()
end

M.handlers = {
---@param params java-refactor.RenameAction[]
[M.commands.RENAME_COMMAND] = function(params)
run('Failed to rename the symbol', function(action)
action:rename(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[M.commands.GENERATE_CONSTRUCTORS_PROMPT] = function(_, params)
run('Failed to generate constructor', function(action)
action:generate_constructor(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[M.commands.GENERATE_TOSTRING_PROMPT] = function(_, params)
run('Failed to generate toString', function(action)
action:generate_to_string(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[M.commands.HASHCODE_EQUALS_PROMPT] = function(_, params)
run('Failed to generate hash code and equals', function(action)
action:generate_hash_code_and_equals(params)
end)
end,

---@param params nvim.CodeActionParamsResponse
[M.commands.GENERATE_DELEGATE_METHODS_PROMPT] = function(_, params)
run('Failed to generate delegate methods', function(action)
action:generate_delegate_mothods_prompt(params)
end)
end,

---@param command lsp.Command
[M.commands.APPLY_REFACTORING_COMMAND] = function(command)
run('Failed to apply refactoring command', function(action)
action:apply_refactoring_command(command)
end)
end,

---@param is_full_build boolean
[M.commands.COMPILE_WORKSPACE] = function(is_full_build)
run('Failed to build workspace', function(action)
action:build_workspace(is_full_build)
end)
end,

[M.commands.CLEAN_WORKSPACE] = function()
run('Failed to clean workspace', function(action)
action:clean_workspace()
end)
end,
}

local ignored_commands = { M.commands.REFRESH_BUNDLES_COMMAND }

for _, command in pairs(M.commands) do
if
not M.handlers[command] and not vim.tbl_contains(ignored_commands, command)
then
local message = string.format(
'"%s" is not supported yet!'
.. '\nPlease request the feature using below link'
.. '\nhttps://github.com/nvim-java/nvim-java/issues/new?assignees='
.. '&labels=enhancement&projects=&template=feature_request.yml&title=feature%%3A+',
command
)

M.handlers[command] = function()
require('java-core.utils.notify').warn(message)

return vim.lsp.rpc_response_error(
vim.lsp.protocol.ErrorCodes.MethodNotFound,
'Not implemented yes'
)
end
end
end

return M
36 changes: 25 additions & 11 deletions lua/java-refactor/init.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
local event = require('java-core.utils.event')

local M = {}

local group = vim.api.nvim_create_augroup('java-refactor-command-register', {})

local setup = function()
-- setting all the vim.lsp.commands
local code_action_handler = require('java-refactor.code-action-handlers')
event.on_jdtls_attach({
group = group,
once = true,
callback = function()
M.reg_client_commands()
M.reg_refactor_commands()
M.reg_build_commands()
end,
})

M.reg_client_commands = function()
local code_action_handlers = require('java-refactor.client-command-handlers')

for key, handler in pairs(code_action_handler.handlers) do
for key, handler in pairs(code_action_handlers) do
vim.lsp.commands[key] = handler
end
end

-- setting all the user commands and APIs
local code_action_api = require('java-refactor.api.code-action')
M.reg_refactor_commands = function()
local code_action_api = require('java-refactor.api.refactor')

for api_name, api in pairs(code_action_api) do
require('java').register_api({ 'refactor', api_name }, api, { range = 2 })
end
end

event.on_jdtls_attach({
group = group,
once = true,
callback = setup,
})
M.reg_build_commands = function()
local code_action_api = require('java-refactor.api.build')

for api_name, api in pairs(code_action_api) do
require('java').register_api({ 'build', api_name }, api, {})
end
end

0 comments on commit c6189ef

Please sign in to comment.