Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ Yep! That's all :)

- `JavaProfile` - Opens the profiles UI

### Refactor

- `JavaRefactorExtractVariable` - Create a variable from returned value at cursor

</details>

## :computer: APIs
Expand Down Expand Up @@ -199,6 +203,14 @@ require('java').test.view_last_report()
require('java').profile.ui()
```

### Refactor

- `extract_variable` - Create a variable from returned value at cursor

```lua
require('java').refactor.extract_variable()
```

</details>

## :clamp: How to Use JDK X.X Version?
Expand Down
8 changes: 8 additions & 0 deletions lua/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local test = require('java.api.test')
local dap = require('java.api.dap')
local runner = require('java.api.runner')
local profile_ui = require('java.ui.profile')
local refactor = require('java.api.refactor')

local global_config = require('java.config')

Expand Down Expand Up @@ -49,9 +50,16 @@ M.test.view_last_report = test.view_last_report
----------------------------------------------------------------------
-- Manipulate --
----------------------------------------------------------------------

M.manipulate = {}
-- M.manipulate.organize_imports = {}

----------------------------------------------------------------------
-- Refactor --
----------------------------------------------------------------------
M.refactor = {}
M.refactor.extract_variable = refactor.extract_variable

----------------------------------------------------------------------
-- Runner APIs --
----------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions lua/java/api/refactor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local jdtls = require('java.utils.jdtls2')
local get_error_handler = require('java.handlers.error')

local async = require('java-core.utils.async').sync

local M = {}

function M.extract_variable()
return async(function()
local RefactorCommands = require('java-refactor.refactor-commands')
local refactor_commands = RefactorCommands(jdtls())
refactor_commands:extract_variable()
end)
.catch(get_error_handler('failed to refactor variable'))
.run()
end

return M
25 changes: 23 additions & 2 deletions lua/java/startup/nvim-dep.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local notify = require('java-core.utils.notify')
local log = require('java.utils.log')

local pkgs = {
Expand All @@ -14,6 +15,21 @@ Please follow the install guide in https://github.com/nvim-java/nvim-java to ins
{
name = 'lspconfig',
err = [[nvim-lspconfig is not installed. nvim-lspconfig requires nvim-lspconfig to show diagnostics & auto completion.
Please follow the install guide in https://github.com/nvim-java/nvim-java to install nvim-java]],
},
{
name = 'java-refactor',
warn = [[nvim-java-refactor is not installed. nvim-java-refactor requires nvim-java to do code refactoring
Please add nvim-java-refactor to the current dependency list

{
"nvim-java/nvim-java",
dependencies = {
"nvim-java/nvim-java-refactor",
....
}
}

Please follow the install guide in https://github.com/nvim-java/nvim-java to install nvim-java]],
},
}
Expand All @@ -31,8 +47,13 @@ function M.neovim_plugin_check()
local ok, _ = pcall(require, pkg.name)

if not ok then
log.error(pkg.err)
error(pkg.err)
if pkg.warn then
log.warn(pkg.warn)
notify.warn(pkg.warn)
else
log.error(pkg.err)
error(pkg.err)
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions lua/java/utils/jdtls2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local get_error_handler = require('java.handlers.error')

---Returns an active jdtls client
---@return { client: LspClient }
local function get_jdtls()
local clients

if vim.lsp.get_clients then
clients = vim.lsp.get_clients({ name = 'jdtls' })
else
clients = vim.lsp.get_active_clients({ name = 'jdtls' })
end

if #clients == 0 then
get_error_handler('could not find an active jdtls client')()
end

return clients[1]
end

return get_jdtls
8 changes: 8 additions & 0 deletions lua/java/utils/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ function M.select(prompt, values)
end)
end

function M.input(prompt)
return await(function(callback)
vim.ui.input({
prompt = prompt,
}, callback)
end)
end

---@param configs table
function M.select_from_dap_configs(configs)
local config_names = {}
Expand Down
2 changes: 2 additions & 0 deletions plugin/java.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ local cmd_map = {
JavaRunnerToggleLogs = { java.runner.built_in.toggle_logs },

JavaProfile = { java.profile.ui },

JavaRefactorExtractVariable = { java.refactor.extract_variable },
}

for cmd, details in pairs(cmd_map) do
Expand Down