autoload | ||
lua | ||
plugin | ||
.editorconfig | ||
.luacheckrc | ||
README.md |
nvim-jdtls
Extensions for the built-in Language Server Protocol support in Neovim (>= 0.5) for eclipse.jdt.ls.
Warning: This is early state. Neovim 0.5 hasn't been released yet, so APIs can change and things may break.
Extensions
- Command to organize imports
- Open class file contents
- Code action extensions (
java.apply.workspaceEdit
). toString
generation.hashCode
andequals
generation.javap
command to show bytecode of current file- Integration with nvim-dap
Installation
- Requires Neovim HEAD/nightly
- nvim-jdtls is a plugin. Install it like any other Vim plugin.
- Call
:packadd nvim-jdtls
if you installnvim-jdtls
to'packpath'
.
Usage
nvim-jdtls
doesn't contain logic to spawn a LSP client for eclipse.jdt.ls, see :help lsp
for information on how to launch a LSP client.
To make use of all the functionality nvim-jdtls
provides, you need to override some of the lsp
callbacks, set some extra capabilities and set a couple of initialization options.
The callbacks:
local jdtls = require 'jdtls'
vim.lsp.callbacks['workspace/applyEdit'] = jdtls.workspace_apply_edit,
Additional capabilities:
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.codeAction = {
dynamicRegistration = false;
codeActionLiteralSupport = {
codeActionKind = {
valueSet = {
"source.generate.toString",
"source.generate.hashCodeEquals"
};
};
};
}
capabilities.workspace = {
applyEdit = true;
}
Initialization options:
config['init_options'] = {
extendedClientCapabilities = {
classFileContentsSupport = true;
generateToStringPromptSupport = true;
hashCodeEqualsPromptSupport = true;
};
}
You may also want to create mappings for the code action command and to organize imports:
nnoremap <A-CR> <Cmd>lua require'jdtls'.code_action()<CR>
nnoremap <A-o> <Cmd>lua require'jdtls'.organize_imports()<CR>
nnoremap <leader>df <Cmd>lua require'jdtls'.test_class()<CR>
nnoremap <leader>dn <Cmd>lua require'jdtls'.test_nearest_method()<CR>
nvim-dap
nvim-jdtls
provides integration with nvim-dap.
For this to work, eclipse.jdt.ls needs to load the java-debug extension.
To do so, clone java-debug and run ./mvnw clean install
in the cloned directory, then extend the initializationOptions
with which you start eclipse.jdt.ls:
config['init_options'] = {
bundles = {
vim.fn.glob("path/to/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar")
};
}
You also need to call require('jdtls').setup_dap()
to have it register a java
adapter for nvim-dap
and to create configurations for all discovered main classes:
config['on_attach'] = function(client, bufnr)
require('jdtls').setup_dap()
end
Furthermore, nvim-jdtls
supports running and debugging tests. For this to work the bundles from vscode-java-test need to be installed:
- Clone the repo
- Run
npm install
- Run
npm run build-plugin
- Extend the bundles:
local bundles = {
vim.fn.glob("path/to/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar"),
};
vim.list_extend(bundles, vim.split(vim.fn.glob("/path/to/microsoft/vscode-java-test/server/*.jar"), "\n"))
config['init_options'] = {
bundles = bundles;
}