Skip to content

Commit 0e61713

Browse files
authored
feat: add extract variable refactor command (nvim-java#171)
1 parent f53dc51 commit 0e61713

7 files changed

Lines changed: 92 additions & 2 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ Yep! That's all :)
122122

123123
- `JavaProfile` - Opens the profiles UI
124124

125+
### Refactor
126+
127+
- `JavaRefactorExtractVariable` - Create a variable from returned value at cursor
128+
125129
</details>
126130

127131
## :computer: APIs
@@ -199,6 +203,14 @@ require('java').test.view_last_report()
199203
require('java').profile.ui()
200204
```
201205

206+
### Refactor
207+
208+
- `extract_variable` - Create a variable from returned value at cursor
209+
210+
```lua
211+
require('java').refactor.extract_variable()
212+
```
213+
202214
</details>
203215

204216
## :clamp: How to Use JDK X.X Version?

lua/java.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local test = require('java.api.test')
77
local dap = require('java.api.dap')
88
local runner = require('java.api.runner')
99
local profile_ui = require('java.ui.profile')
10+
local refactor = require('java.api.refactor')
1011

1112
local global_config = require('java.config')
1213

@@ -49,9 +50,16 @@ M.test.view_last_report = test.view_last_report
4950
----------------------------------------------------------------------
5051
-- Manipulate --
5152
----------------------------------------------------------------------
53+
5254
M.manipulate = {}
5355
-- M.manipulate.organize_imports = {}
5456

57+
----------------------------------------------------------------------
58+
-- Refactor --
59+
----------------------------------------------------------------------
60+
M.refactor = {}
61+
M.refactor.extract_variable = refactor.extract_variable
62+
5563
----------------------------------------------------------------------
5664
-- Runner APIs --
5765
----------------------------------------------------------------------

lua/java/api/refactor.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local jdtls = require('java.utils.jdtls2')
2+
local get_error_handler = require('java.handlers.error')
3+
4+
local async = require('java-core.utils.async').sync
5+
6+
local M = {}
7+
8+
function M.extract_variable()
9+
return async(function()
10+
local RefactorCommands = require('java-refactor.refactor-commands')
11+
local refactor_commands = RefactorCommands(jdtls())
12+
refactor_commands:extract_variable()
13+
end)
14+
.catch(get_error_handler('failed to refactor variable'))
15+
.run()
16+
end
17+
18+
return M

lua/java/startup/nvim-dep.lua

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local notify = require('java-core.utils.notify')
12
local log = require('java.utils.log')
23

34
local pkgs = {
@@ -14,6 +15,21 @@ Please follow the install guide in https://github.com/nvim-java/nvim-java to ins
1415
{
1516
name = 'lspconfig',
1617
err = [[nvim-lspconfig is not installed. nvim-lspconfig requires nvim-lspconfig to show diagnostics & auto completion.
18+
Please follow the install guide in https://github.com/nvim-java/nvim-java to install nvim-java]],
19+
},
20+
{
21+
name = 'java-refactor',
22+
warn = [[nvim-java-refactor is not installed. nvim-java-refactor requires nvim-java to do code refactoring
23+
Please add nvim-java-refactor to the current dependency list
24+
25+
{
26+
"nvim-java/nvim-java",
27+
dependencies = {
28+
"nvim-java/nvim-java-refactor",
29+
....
30+
}
31+
}
32+
1733
Please follow the install guide in https://github.com/nvim-java/nvim-java to install nvim-java]],
1834
},
1935
}
@@ -31,8 +47,13 @@ function M.neovim_plugin_check()
3147
local ok, _ = pcall(require, pkg.name)
3248

3349
if not ok then
34-
log.error(pkg.err)
35-
error(pkg.err)
50+
if pkg.warn then
51+
log.warn(pkg.warn)
52+
notify.warn(pkg.warn)
53+
else
54+
log.error(pkg.err)
55+
error(pkg.err)
56+
end
3657
end
3758
end
3859
end

lua/java/utils/jdtls2.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local get_error_handler = require('java.handlers.error')
2+
3+
---Returns an active jdtls client
4+
---@return { client: LspClient }
5+
local function get_jdtls()
6+
local clients
7+
8+
if vim.lsp.get_clients then
9+
clients = vim.lsp.get_clients({ name = 'jdtls' })
10+
else
11+
clients = vim.lsp.get_active_clients({ name = 'jdtls' })
12+
end
13+
14+
if #clients == 0 then
15+
get_error_handler('could not find an active jdtls client')()
16+
end
17+
18+
return clients[1]
19+
end
20+
21+
return get_jdtls

lua/java/utils/ui.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ function M.select(prompt, values)
1212
end)
1313
end
1414

15+
function M.input(prompt)
16+
return await(function(callback)
17+
vim.ui.input({
18+
prompt = prompt,
19+
}, callback)
20+
end)
21+
end
22+
1523
---@param configs table
1624
function M.select_from_dap_configs(configs)
1725
local config_names = {}

plugin/java.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ local cmd_map = {
2020
JavaRunnerToggleLogs = { java.runner.built_in.toggle_logs },
2121

2222
JavaProfile = { java.profile.ui },
23+
24+
JavaRefactorExtractVariable = { java.refactor.extract_variable },
2325
}
2426

2527
for cmd, details in pairs(cmd_map) do

0 commit comments

Comments
 (0)