我正在开发一个大型 Java 项目。该项目使用Java 1.8,但我已经为jdtls安装了Java 20。 该项目仅使用 and build 和 tomcat。我目前正在使用 eclipse 进行该项目,但我想更改为 Neovim。我在其他语言中使用 vim,但 Java 是我的头号敌人。 :))。 问题是 jdtls 似乎不起作用,“转到定义”功能将我带到 import 语句,但没有任何作用。 在 Eclipse 中,我在构建路径中只有几个文件夹,我也尝试将它们添加到配置中,但它似乎不起作用。 我在控制台中也有一些错误,但它们不应该干扰“转到定义”功能。我在 Windows 10 上使用 Neovim。 这是我的控制台:
[ERROR][2024-03-04 01:13:37] .../vim/lsp/rpc.lua:734 "rpc" "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe" "stderr" "WARNING: Using incubator modules: jdk.incubator.vector, jdk.incubator.concurrent\r\n"
[ERROR][2024-03-04 01:13:38] .../vim/lsp/rpc.lua:734 "rpc" "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe" "stderr" "Mar 04, 2024 1:13:38 AM org.apache.aries.spifly.BaseActivator log\r\nINFO: Registered provider ch.qos.logback.classic.servlet.LogbackServletContainerInitializer of service jakarta.servlet.ServletContainerInitializer in bundle ch.qos.logback.classic\r\n"
[ERROR][2024-03-04 01:13:38] .../vim/lsp/rpc.lua:734 "rpc" "C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe" "stderr" "Mar 04, 2024 1:13:38 AM org.apache.aries.spifly.BaseActivator log\r\nINFO: Registered provider ch.qos.logback.classic.spi.LogbackServiceProvider of service org.slf4j.spi.SLF4JServiceProvider in bundle ch.qos.logback.classic\r\n"
[WARN][2024-03-04 01:13:40] ...lsp/handlers.lua:137 "The language server jdtls triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"
[ERROR][2024-03-04 01:13:40] ...lsp/handlers.lua:535 "4 Mar 2024, 01:13:40 Command _java.reloadBundles.command not supported on client"
这是我的配置:
return {
{ 'mfussenegger/nvim-dap' },
{
'mfussenegger/nvim-jdtls',
config = function()
local jdtls = require('jdtls')
local root_markers = {'gui.bat', '.git', 'bin'}
local root_dir = require('jdtls.setup').find_root(root_markers)
local workspace_folder = "C:\\Users\\myName\\Workspace\\" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
function nnoremap(rhs, lhs, bufopts, desc)
bufopts.desc = desc
vim.keymap.set("n", rhs, lhs, bufopts)
end
local on_attach = function(client, bufnr)
local bufopts = { noremap=true, silent=true, buffer=bufnr }
nnoremap('gD', vim.lsp.buf.declaration, bufopts, "Go to declaration")
nnoremap('gd', vim.lsp.buf.definition, bufopts, "Go to definition")
nnoremap('gi', vim.lsp.buf.implementation, bufopts, "Go to implementation")
nnoremap('K', vim.lsp.buf.hover, bufopts, "Hover text")
nnoremap('<C-k>', vim.lsp.buf.signature_help, bufopts, "Show signature")
nnoremap('<space>wa', vim.lsp.buf.add_workspace_folder, bufopts, "Add workspace folder")
nnoremap('<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts, "Remove workspace folder")
nnoremap('<space>wl', function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, bufopts, "List workspace folders")
nnoremap('<space>D', vim.lsp.buf.type_definition, bufopts, "Go to type definition")
nnoremap('<space>rn', vim.lsp.buf.rename, bufopts, "Rename")
nnoremap('<space>ca', vim.lsp.buf.code_action, bufopts, "Code actions")
vim.keymap.set('v', "<space>ca", "<ESC><CMD>lua vim.lsp.buf.range_code_action()<CR>",
{ noremap=true, silent=true, buffer=bufnr, desc = "Code actions" })
nnoremap('<space>f', function() vim.lsp.buf.format { async = true } end, bufopts, "Format file")
nnoremap("<C-o>", jdtls.organize_imports, bufopts, "Organize imports")
nnoremap("<space>ev", jdtls.extract_variable, bufopts, "Extract variable")
nnoremap("<space>ec", jdtls.extract_constant, bufopts, "Extract constant")
vim.keymap.set('v', "<space>em", [[<ESC><CMD>lua require('jdtls').extract_method(true)<CR>]],
{ noremap=true, silent=true, buffer=bufnr, desc = "Extract method" })
end
local config = {
flags = {
debounce_text_changes = 80,
},
on_attach = on_attach,
root_dir = root_dir,
settings = {
java = {
format = {
settings = {
url = "C:\\Users\\myName\\Downloads\\eclipse-java-google-style.xml",
profile = "GoogleStyle",
},
},
signatureHelp = { enabled = true },
contentProvider = { preferred = 'fernflower' },
-- Specify any completion options
-- completion = {
-- favoriteStaticMembers = {
-- "org.hamcrest.MatcherAssert.assertThat",
-- "org.hamcrest.Matchers.*",
-- "org.hamcrest.CoreMatchers.*",
-- "org.junit.jupiter.api.Assertions.*",
-- "java.util.Objects.requireNonNull",
-- "java.util.Objects.requireNonNullElse",
-- "org.mockito.Mockito.*"
-- },
-- filteredTypes = {
-- "com.sun.*",
-- "io.micrometer.shaded.*",
-- "java.awt.*",
-- "jdk.*", "sun.*",
-- },
-- },
-- Specify any options for organizing imports
sources = {
organizeImports = {
starThreshold = 9999;
staticStarThreshold = 9999;
},
},
-- How code generation should act
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}"
},
hashCodeEquals = {
useJava7Objects = true,
},
useBlocks = true,
},
configuration = {
runtimes = {
{
name = "JavaSE-1.8",
path = "C:\\jdk_1.8",
}
}
},
-- I have tried do add the full paths and the relative paths but they don't seem to work at all.
build = {
sources = {
root_dir .. "path/in/my/project",
root_dir .. "another_path/in/my/project",
root_dir .. "more/path/in/my/project/src",
"path/in/my/project",
"another_path/in/my/project",
"more/path/in/my/project/src",
}
}
}
},
cmd = {
"C:\\Users\\myName\\jdk-20.0.2\\bin\\java.exe",
'-Declipse.application=org.eclipse.jdt.ls.core.id1',
-- '-Dosgi.bundles.defaultStartLevel=4',
'-Declipse.product=org.eclipse.jdt.ls.core.product',
'-Dlog.protocol=true',
'-Dlog.level=ALL',
'-Xmx4g',
'--add-modules=ALL-SYSTEM',
'--add-opens', 'java.base/java.util=ALL-UNNAMED',
'--add-opens', 'java.base/java.lang=ALL-UNNAMED',
'-jar', vim.fn.glob('C:\\Users\\myName\\Downloads\\jdtls\\plugins\\org.eclipse.equinox.launcher_*.jar'),
'-configuration', 'C:\\Users\\myName\\Downloads\\jdtls\\config_win',
'-data', workspace_folder,
},
}
jdtls.start_or_attach(config)
end
},
{ 'nvim-lua/plenary.nvim' }
}
很抱歉,我真的不知道如何举一个小例子。 欢迎任何帮助,谢谢!
也许我的回答有点晚了,但我想我已经解决了你的问题。在 Windows 终端中将 WSL 与 Ubuntu 结合使用。基本上,在 WSL 中,您要做的就是创建和配置 Neovim,就像在 Windows 中配置它一样(当然要考虑路径问题的更改等)。但这解决了我在大型项目中使用 nvim-jdtls 的问题。您甚至会注意到 Neovim 性能的改进。 这是一个从头开始配置所有内容的频道:https://youtu.be/zbpF3te0M3g?si=d-5InekD4uPQCxLD