Make java and neotest config portable
jdtls hardcoded /Library/Java/JavaVirtualMachines paths, which resolve to nothing off macOS; discover JDKs across the macOS, Linux and sdkman roots instead and only declare runtimes that exist. neotest-mocha passed `docker exec -it`, which hangs without a TTY, and hardcoded the container name. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+42
-10
@@ -1,15 +1,50 @@
|
||||
local function first_jdk(candidates)
|
||||
for _, path in ipairs(candidates) do
|
||||
if vim.fn.executable(path .. "/bin/java") == 1 then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function find_jdk(patterns)
|
||||
local roots = {
|
||||
"/Library/Java/JavaVirtualMachines/%s/Contents/Home",
|
||||
"/usr/lib/jvm/%s",
|
||||
vim.fn.expand("~") .. "/.sdkman/candidates/java/%s",
|
||||
}
|
||||
local candidates = {}
|
||||
for _, pattern in ipairs(patterns) do
|
||||
for _, root in ipairs(roots) do
|
||||
for _, path in ipairs(vim.fn.glob(root:format(pattern), true, true)) do
|
||||
candidates[#candidates + 1] = path
|
||||
end
|
||||
end
|
||||
end
|
||||
return first_jdk(candidates)
|
||||
end
|
||||
|
||||
return {
|
||||
{
|
||||
"mfussenegger/nvim-jdtls",
|
||||
opts = function(_, opts)
|
||||
local corretto8 = "/Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home"
|
||||
local temurin21 = "/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home"
|
||||
local jdk8 = find_jdk({ "*corretto-8*", "*corretto*1.8*", "*-8-*", "*java-8*", "*1.8*" })
|
||||
local jdk21 = find_jdk({ "*temurin-21*", "*-21-*", "*java-21*", "*21*" })
|
||||
|
||||
opts.cmd = opts.cmd or {}
|
||||
table.insert(opts.cmd, "--java-executable")
|
||||
table.insert(opts.cmd, temurin21 .. "/bin/java")
|
||||
if jdk21 then
|
||||
opts.cmd = opts.cmd or {}
|
||||
table.insert(opts.cmd, "--java-executable")
|
||||
table.insert(opts.cmd, jdk21 .. "/bin/java")
|
||||
end
|
||||
|
||||
local project_jdk = vim.env.JAVA_HOME or temurin21
|
||||
local runtimes = {}
|
||||
if jdk8 then
|
||||
table.insert(runtimes, { name = "JavaSE-1.8", path = jdk8 })
|
||||
end
|
||||
if jdk21 then
|
||||
table.insert(runtimes, { name = "JavaSE-21", path = jdk21 })
|
||||
end
|
||||
|
||||
local project_jdk = vim.env.JAVA_HOME or jdk21
|
||||
|
||||
opts.settings = vim.tbl_deep_extend("force", opts.settings or {}, {
|
||||
java = {
|
||||
@@ -19,10 +54,7 @@ return {
|
||||
},
|
||||
},
|
||||
configuration = {
|
||||
runtimes = {
|
||||
{ name = "JavaSE-1.8", path = corretto8 },
|
||||
{ name = "JavaSE-21", path = temurin21 },
|
||||
},
|
||||
runtimes = runtimes,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ return {
|
||||
return {
|
||||
adapters = {
|
||||
["neotest-mocha"] = {
|
||||
command = "docker exec -it api-sandbox npm run test:integration",
|
||||
command = "docker exec " .. (vim.env.NEOTEST_MOCHA_CONTAINER or "api-sandbox") .. " npm run test:integration",
|
||||
command_args = function(context)
|
||||
-- The context contains:
|
||||
-- results_path: The file that json results are written to
|
||||
|
||||
Reference in New Issue
Block a user