-
-
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.
- Loading branch information
Showing
6 changed files
with
186 additions
and
148 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 |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
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,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 |
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,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 |
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
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,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 |