mirror of
https://github.com/mfussenegger/nvim-jdtls
synced 2024-09-16 14:34:13 +02:00
Implicitly add commands/debug adapter
Should make the setup slightly more convenient
This commit is contained in:
parent
c6a3c47a0c
commit
0261cf5a76
2 changed files with 77 additions and 54 deletions
41
README.md
41
README.md
|
@ -265,22 +265,18 @@ nnoremap <leader>df <Cmd>lua require'jdtls'.test_class()<CR>
|
|||
nnoremap <leader>dn <Cmd>lua require'jdtls'.test_nearest_method()<CR>
|
||||
```
|
||||
|
||||
`nvim-jdtls` also adds several commands if the server starts up correctly:
|
||||
|
||||
Some methods are better exposed via commands. As a shortcut you can also call
|
||||
`:lua require('jdtls.setup').add_commands()` to declare these.
|
||||
- `JdtCompile`
|
||||
- `JdtSetRuntime`
|
||||
- `JdtUpdateConfig`
|
||||
- `JdtUpdateDebugConfig` (if `dap` and java-debug bundles are available)
|
||||
- `JdtUpdateHotcode` (if `dap` and java-debug bundles are available)
|
||||
- `JdtBytecode`
|
||||
- `JdtJol`
|
||||
- `JdtJshell`
|
||||
- `JdtRestart`
|
||||
|
||||
It's recommended to call `add_commands` within the `on_attach` handler that can be set on the `config` table which is passed to `start_or_attach`.
|
||||
If you use jdtls together with nvim-dap, call `add_commands` *after* `setup_dap` to ensure it includes debugging related commands. (More about this is in the debugger setup section further below)
|
||||
|
||||
|
||||
```vimL
|
||||
command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile(<f-args>)
|
||||
command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)
|
||||
command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()
|
||||
command! -buffer JdtJol lua require('jdtls').jol()
|
||||
command! -buffer JdtBytecode lua require('jdtls').javap()
|
||||
command! -buffer JdtJshell lua require('jdtls').jshell()
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
|
@ -326,26 +322,15 @@ config['init_options'] = {
|
|||
|
||||
### nvim-dap setup
|
||||
|
||||
You also need to call `require('jdtls').setup_dap()` to have it register a
|
||||
`java` adapter.
|
||||
`nvim-jdtls` will automatically register a `java` debug adapter with nvim-dap,
|
||||
if nvim-dap is available.
|
||||
|
||||
To do that, extend the [configuration](#Configuration-verbose):
|
||||
|
||||
```lua
|
||||
config['on_attach'] = function(client, bufnr)
|
||||
-- With `hotcodereplace = 'auto' the debug adapter will try to apply code changes
|
||||
-- you make during a debug session immediately.
|
||||
-- Remove the option if you do not want that.
|
||||
-- You can use the `JdtHotcodeReplace` command to trigger it manually
|
||||
require('jdtls').setup_dap({ hotcodereplace = 'auto' })
|
||||
end
|
||||
```
|
||||
|
||||
### nvim-dap configuration
|
||||
|
||||
`nvim-jdtls` includes functionality to discover main classes and create `nvim-dap` configuration entries for them.
|
||||
|
||||
To discover the main classes you have to call `require('jdtls.dap').setup_dap_main_class_configs()` or use the `JdtRefreshDebugConfigs` command. It will only discover classes once eclipse.jdt.ls fully loaded the project. Depending on the project that may take a while. Because of that, calling `require('jdtls.dap').setup_dap_main_class_configs()` as part of an `on_attach` handler may not work well.
|
||||
To discover the main classes you have to call `require('jdtls.dap').setup_dap_main_class_configs()` or use the `JdtUpdateDebugConfigs` command. It will only discover classes once eclipse.jdt.ls fully loaded the project. Depending on the project that may take a while. Because of that, calling `require('jdtls.dap').setup_dap_main_class_configs()` as part of an `on_attach` handler may not work well.
|
||||
|
||||
For manual configuration see [nvim-dap Adapter Installation Wiki](https://github.com/mfussenegger/nvim-dap/wiki/Java).
|
||||
|
||||
|
|
|
@ -182,7 +182,62 @@ local function extract_data_dir(bufnr)
|
|||
end
|
||||
|
||||
|
||||
function M.start_or_attach(config)
|
||||
---@param client lsp.Client
|
||||
---@param opts jdtls.start.opts
|
||||
local function add_commands(client, bufnr, opts)
|
||||
local function create_cmd(name, command, cmdopts)
|
||||
api.nvim_buf_create_user_command(bufnr, name, command, cmdopts or {})
|
||||
end
|
||||
create_cmd("JdtCompile", "lua require('jdtls').compile(<f-args>)", {
|
||||
nargs = "?",
|
||||
complete = "custom,v:lua.require'jdtls'._complete_compile"
|
||||
})
|
||||
create_cmd("JdtSetRuntime", "lua require('jdtls').set_runtime(<f-args>)", {
|
||||
nargs = "?",
|
||||
complete = "custom,v:lua.require'jdtls'._complete_set_runtime"
|
||||
})
|
||||
create_cmd("JdtUpdateConfig", "lua require('jdtls').update_project_config()")
|
||||
create_cmd("JdtJol", "lua require('jdtls').jol(<f-args>)", {
|
||||
nargs = "*"
|
||||
})
|
||||
create_cmd("JdtBytecode", "lua require('jdtls').javap()")
|
||||
create_cmd("JdtJshell", "lua require('jdtls').jshell()")
|
||||
create_cmd("JdtRestart", "lua require('jdtls.setup').restart()")
|
||||
local ok, dap = pcall(require, 'dap')
|
||||
if ok then
|
||||
local command_provider = client.server_capabilities.executeCommandProvider or {}
|
||||
local commands = command_provider.commands or {}
|
||||
if not vim.tbl_contains(commands, "vscode.java.startDebugSession") then
|
||||
return
|
||||
end
|
||||
|
||||
require("jdtls.dap").setup_dap(opts.dap or {})
|
||||
api.nvim_command "command! -buffer JdtUpdateDebugConfig lua require('jdtls.dap').setup_dap_main_class_configs({ verbose = true })"
|
||||
local redefine_classes = function()
|
||||
local session = dap.session()
|
||||
if not session then
|
||||
vim.notify('No active debug session')
|
||||
else
|
||||
vim.notify('Applying code changes')
|
||||
session:request('redefineClasses', nil, function(err)
|
||||
assert(not err, vim.inspect(err))
|
||||
end)
|
||||
end
|
||||
end
|
||||
api.nvim_create_user_command('JdtUpdateHotcode', redefine_classes, {
|
||||
desc = "Trigger reload of changed classes for current debug session",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---@class jdtls.start.opts
|
||||
---@field dap? JdtSetupDapOpts
|
||||
|
||||
|
||||
---@param opts? jdtls.start.opts
|
||||
function M.start_or_attach(config, opts)
|
||||
opts = opts or {}
|
||||
assert(config, 'config is required')
|
||||
assert(
|
||||
config.cmd and type(config.cmd) == 'table',
|
||||
|
@ -190,6 +245,13 @@ function M.start_or_attach(config)
|
|||
.. table.concat(config.cmd, ' ')
|
||||
)
|
||||
config.name = 'jdtls'
|
||||
local on_attach = config.on_attach
|
||||
config.on_attach = function(client, bufnr)
|
||||
if on_attach then
|
||||
on_attach(client, bufnr)
|
||||
end
|
||||
add_commands(client, bufnr, opts)
|
||||
end
|
||||
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
local bufname = api.nvim_buf_get_name(bufnr)
|
||||
|
@ -273,32 +335,8 @@ function M.wipe_data_and_restart()
|
|||
end
|
||||
|
||||
|
||||
---@deprecated not needed, start automatically adds commands
|
||||
function M.add_commands()
|
||||
vim.cmd [[command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile(<f-args>)]]
|
||||
vim.cmd [[command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)]]
|
||||
vim.cmd [[command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()]]
|
||||
vim.cmd [[command! -buffer -nargs=* JdtJol lua require('jdtls').jol(<f-args>)]]
|
||||
vim.cmd [[command! -buffer JdtBytecode lua require('jdtls').javap()]]
|
||||
vim.cmd [[command! -buffer JdtJshell lua require('jdtls').jshell()]]
|
||||
vim.cmd [[command! -buffer JdtRestart lua require('jdtls.setup').restart()]]
|
||||
local ok, dap = pcall(require, 'dap')
|
||||
if ok and dap.adapters.java then
|
||||
api.nvim_command "command! -buffer JdtRefreshDebugConfigs lua require('jdtls.dap').setup_dap_main_class_configs({ verbose = true })"
|
||||
local redefine_classes = function()
|
||||
local session = dap.session()
|
||||
if not session then
|
||||
vim.notify('No active debug session')
|
||||
else
|
||||
vim.notify('Applying code changes')
|
||||
session:request('redefineClasses', nil, function(err)
|
||||
assert(not err, vim.inspect(err))
|
||||
end)
|
||||
end
|
||||
end
|
||||
api.nvim_create_user_command('JdtHotcodeReplace', redefine_classes, {
|
||||
desc = "Trigger reload of changed classes for current debug session",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue