Skip to content

Commit c6189ef

Browse files
committed
organize code
1 parent 98db44a commit c6189ef

File tree

6 files changed

+186
-148
lines changed

6 files changed

+186
-148
lines changed

lua/java-refactor/api/build.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---@param client_command jdtls.ClientCommand
2+
local function run_client_command(client_command, ...)
3+
local handlers = require('java-refactor.client-command-handlers')
4+
handlers[client_command](...)
5+
end
6+
7+
local M = {
8+
build_workspace = function()
9+
local ClientCommand = require('java-refactor.client-command')
10+
run_client_command(ClientCommand.COMPILE_WORKSPACE, true)
11+
end,
12+
13+
clean_workspace = function()
14+
local ClientCommand = require('java-refactor.client-command')
15+
run_client_command(ClientCommand.CLEAN_WORKSPACE)
16+
end,
17+
}
18+
19+
return M

lua/java-refactor/api/code-action.lua

Lines changed: 0 additions & 38 deletions
This file was deleted.

lua/java-refactor/api/refactor.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---@param action_type string
2+
---@param filter? string
3+
local function run_code_action(action_type, filter)
4+
vim.lsp.buf.code_action({
5+
apply = true,
6+
context = {
7+
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0),
8+
only = { action_type },
9+
},
10+
filter = filter and function(refactor)
11+
return refactor.command.arguments[1] == filter
12+
end or nil,
13+
})
14+
end
15+
16+
local M = {
17+
extract_variable = function()
18+
run_code_action('refactor.extract.variable', 'extractVariable')
19+
end,
20+
21+
extract_variable_all_occurrence = function()
22+
run_code_action('refactor.extract.variable', 'extractVariableAllOccurrence')
23+
end,
24+
25+
extract_constant = function()
26+
run_code_action('refactor.extract.constant')
27+
end,
28+
29+
extract_method = function()
30+
run_code_action('refactor.extract.function')
31+
end,
32+
33+
extract_field = function()
34+
run_code_action('refactor.extract.field')
35+
end,
36+
}
37+
38+
return M
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
local ClientCommand = require('java-refactor.client-command')
2+
3+
---@param message string
4+
---@param func fun(action: java-refactor.Action)
5+
local run = function(message, func)
6+
local runner = require('async.runner')
7+
local get_error_handler = require('java-refactor.utils.error_handler')
8+
local instance = require('java-refactor.utils.instance-factory')
9+
10+
runner(function()
11+
func(instance.get_action())
12+
end)
13+
.catch(get_error_handler(message))
14+
.run()
15+
end
16+
17+
local M = {
18+
---@param params java-refactor.RenameAction[]
19+
[ClientCommand.RENAME_COMMAND] = function(params)
20+
run('Failed to rename the symbol', function(action)
21+
action:rename(params)
22+
end)
23+
end,
24+
25+
---@param params nvim.CodeActionParamsResponse
26+
[ClientCommand.GENERATE_CONSTRUCTORS_PROMPT] = function(_, params)
27+
run('Failed to generate constructor', function(action)
28+
action:generate_constructor(params)
29+
end)
30+
end,
31+
32+
---@param params nvim.CodeActionParamsResponse
33+
[ClientCommand.GENERATE_TOSTRING_PROMPT] = function(_, params)
34+
run('Failed to generate toString', function(action)
35+
action:generate_to_string(params)
36+
end)
37+
end,
38+
39+
---@param params nvim.CodeActionParamsResponse
40+
[ClientCommand.HASHCODE_EQUALS_PROMPT] = function(_, params)
41+
run('Failed to generate hash code and equals', function(action)
42+
action:generate_hash_code_and_equals(params)
43+
end)
44+
end,
45+
46+
---@param params nvim.CodeActionParamsResponse
47+
[ClientCommand.GENERATE_DELEGATE_METHODS_PROMPT] = function(_, params)
48+
run('Failed to generate delegate methods', function(action)
49+
action:generate_delegate_mothods_prompt(params)
50+
end)
51+
end,
52+
53+
---@param command lsp.Command
54+
[ClientCommand.APPLY_REFACTORING_COMMAND] = function(command)
55+
run('Failed to apply refactoring command', function(action)
56+
action:apply_refactoring_command(command)
57+
end)
58+
end,
59+
60+
---@param is_full_build boolean
61+
[ClientCommand.COMPILE_WORKSPACE] = function(is_full_build)
62+
run('Failed to build workspace', function(action)
63+
action:build_workspace(is_full_build)
64+
require('java-core.utils.notify').info('Successfully built the workspace')
65+
end)
66+
end,
67+
68+
[ClientCommand.CLEAN_WORKSPACE] = function()
69+
run('Failed to clean workspace', function(action)
70+
action:clean_workspace()
71+
require('java-core.utils.notify').info(
72+
'Successfully cleared the workspace cache'
73+
.. '\nPlease close and reopen neovim to restart JDTLS'
74+
)
75+
end)
76+
end,
77+
}
78+
79+
local ignored_commands = { ClientCommand.REFRESH_BUNDLES_COMMAND }
80+
81+
for _, command in pairs(ClientCommand) do
82+
if not M[command] and not vim.tbl_contains(ignored_commands, command) then
83+
local message = string.format(
84+
'"%s" is not supported yet!'
85+
.. '\nPlease request the feature using below link'
86+
.. '\nhttps://github.com/nvim-java/nvim-java/issues/new?assignees='
87+
.. '&labels=enhancement&projects=&template=feature_request.yml&title=feature%%3A+',
88+
command
89+
)
90+
91+
M[command] = function()
92+
require('java-core.utils.notify').warn(message)
93+
94+
return vim.lsp.rpc_response_error(
95+
vim.lsp.protocol.ErrorCodes.MethodNotFound,
96+
'Not implemented yes'
97+
)
98+
end
99+
end
100+
end
101+
102+
return M
Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
local M = {}
2-
3-
M.commands = {
1+
---@enum jdtls.ClientCommand
2+
local M = {
43
ADD_TO_SOURCEPATH = 'java.project.addToSourcePath',
54
ADD_TO_SOURCEPATH_CMD = 'java.project.addToSourcePath.command',
65
APPLY_REFACTORING_COMMAND = 'java.action.applyRefactoringCommand',
@@ -91,100 +90,4 @@ M.commands = {
9190
UPGRADE_GRADLE_WRAPPER_CMD = 'java.project.upgradeGradle.command',
9291
}
9392

94-
---@param message string
95-
---@param func fun(action: java-refactor.Action)
96-
local run = function(message, func)
97-
local runner = require('async.runner')
98-
local get_error_handler = require('java-refactor.utils.error_handler')
99-
local instance = require('java-refactor.utils.instance-factory')
100-
101-
runner(function()
102-
func(instance.get_action())
103-
end)
104-
.catch(get_error_handler(message))
105-
.run()
106-
end
107-
108-
M.handlers = {
109-
---@param params java-refactor.RenameAction[]
110-
[M.commands.RENAME_COMMAND] = function(params)
111-
run('Failed to rename the symbol', function(action)
112-
action:rename(params)
113-
end)
114-
end,
115-
116-
---@param params nvim.CodeActionParamsResponse
117-
[M.commands.GENERATE_CONSTRUCTORS_PROMPT] = function(_, params)
118-
run('Failed to generate constructor', function(action)
119-
action:generate_constructor(params)
120-
end)
121-
end,
122-
123-
---@param params nvim.CodeActionParamsResponse
124-
[M.commands.GENERATE_TOSTRING_PROMPT] = function(_, params)
125-
run('Failed to generate toString', function(action)
126-
action:generate_to_string(params)
127-
end)
128-
end,
129-
130-
---@param params nvim.CodeActionParamsResponse
131-
[M.commands.HASHCODE_EQUALS_PROMPT] = function(_, params)
132-
run('Failed to generate hash code and equals', function(action)
133-
action:generate_hash_code_and_equals(params)
134-
end)
135-
end,
136-
137-
---@param params nvim.CodeActionParamsResponse
138-
[M.commands.GENERATE_DELEGATE_METHODS_PROMPT] = function(_, params)
139-
run('Failed to generate delegate methods', function(action)
140-
action:generate_delegate_mothods_prompt(params)
141-
end)
142-
end,
143-
144-
---@param command lsp.Command
145-
[M.commands.APPLY_REFACTORING_COMMAND] = function(command)
146-
run('Failed to apply refactoring command', function(action)
147-
action:apply_refactoring_command(command)
148-
end)
149-
end,
150-
151-
---@param is_full_build boolean
152-
[M.commands.COMPILE_WORKSPACE] = function(is_full_build)
153-
run('Failed to build workspace', function(action)
154-
action:build_workspace(is_full_build)
155-
end)
156-
end,
157-
158-
[M.commands.CLEAN_WORKSPACE] = function()
159-
run('Failed to clean workspace', function(action)
160-
action:clean_workspace()
161-
end)
162-
end,
163-
}
164-
165-
local ignored_commands = { M.commands.REFRESH_BUNDLES_COMMAND }
166-
167-
for _, command in pairs(M.commands) do
168-
if
169-
not M.handlers[command] and not vim.tbl_contains(ignored_commands, command)
170-
then
171-
local message = string.format(
172-
'"%s" is not supported yet!'
173-
.. '\nPlease request the feature using below link'
174-
.. '\nhttps://github.com/nvim-java/nvim-java/issues/new?assignees='
175-
.. '&labels=enhancement&projects=&template=feature_request.yml&title=feature%%3A+',
176-
command
177-
)
178-
179-
M.handlers[command] = function()
180-
require('java-core.utils.notify').warn(message)
181-
182-
return vim.lsp.rpc_response_error(
183-
vim.lsp.protocol.ErrorCodes.MethodNotFound,
184-
'Not implemented yes'
185-
)
186-
end
187-
end
188-
end
189-
19093
return M

lua/java-refactor/init.lua

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
local event = require('java-core.utils.event')
22

3+
local M = {}
4+
35
local group = vim.api.nvim_create_augroup('java-refactor-command-register', {})
46

5-
local setup = function()
6-
-- setting all the vim.lsp.commands
7-
local code_action_handler = require('java-refactor.code-action-handlers')
7+
event.on_jdtls_attach({
8+
group = group,
9+
once = true,
10+
callback = function()
11+
M.reg_client_commands()
12+
M.reg_refactor_commands()
13+
M.reg_build_commands()
14+
end,
15+
})
16+
17+
M.reg_client_commands = function()
18+
local code_action_handlers = require('java-refactor.client-command-handlers')
819

9-
for key, handler in pairs(code_action_handler.handlers) do
20+
for key, handler in pairs(code_action_handlers) do
1021
vim.lsp.commands[key] = handler
1122
end
23+
end
1224

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

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

21-
event.on_jdtls_attach({
22-
group = group,
23-
once = true,
24-
callback = setup,
25-
})
33+
M.reg_build_commands = function()
34+
local code_action_api = require('java-refactor.api.build')
35+
36+
for api_name, api in pairs(code_action_api) do
37+
require('java').register_api({ 'build', api_name }, api, {})
38+
end
39+
end

0 commit comments

Comments
 (0)