Skip to content

Instantly share code, notes, and snippets.

@enesusta
Created May 13, 2024 21:04
Show Gist options
  • Save enesusta/d4fbad084ac115cf64018a40213a1d82 to your computer and use it in GitHub Desktop.
Save enesusta/d4fbad084ac115cf64018a40213a1d82 to your computer and use it in GitHub Desktop.
AstroNVIM user/init.lua jdtls
return {
lsp = {
skip_setup = {"jdtls"},
formatting = {
format_on_save = true,
filter = function(client)
if client.name == "tsserver" then
return false
end
return true
end
},
["server-settings"] = {
jdtls = function()
-- use this function notation to build some variables
local root_markers = {".git", "mvnw", "gradlew", "pom.xml", "build.gradle"}
local root_dir = require("jdtls.setup").find_root(root_markers)
-- calculate workspace dir
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = vim.fn.stdpath "data" .. "/site/java/workspace-root/" .. project_name
os.execute("mkdir " .. workspace_dir)
local extendedClientCapabilities = require("jdtls").extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
local install_path = require("mason-registry").get_package("jdtls"):get_install_path()
local debug_install_path =
require("mason-registry").get_package("java-debug-adapter"):get_install_path()
local os
if vim.fn.has "macunix" then
os = "mac"
elseif vim.fn.has "win32" then
os = "win"
else
os = "linux"
end
local on_attach = function(client, bufnr)
require("plugins.configs.lspconfig").on_attach(client, bufnr)
end
-- Here you can configure eclipse.jdt.ls specific settings
-- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
-- for a list of options
local bundles = {vim.fn.glob(debug_install_path ..
"/extension/server/com.microsoft.java.debug.plugin-*.jar", 1)}
local config = {
cmd = { -- '/Users/enesusta/.sdkman/candidates/java/current/bin/java',
install_path .. "/bin/jdtls", '-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', '-Xmx1g', '--add-modules=ALL-SYSTEM', '--add-opens',
'java.base/java.util=ALL-UNNAMED', '--add-opens', 'java.base/java.lang=ALL-UNNAMED', "-jar",
vim.fn.glob(install_path .. "/plugins/org.eclipse.equinox.launcher_*.jar"), "-configuration",
install_path .. "/config_" .. os, "-data", workspace_dir},
on_attach = on_attach,
capabilities = capabilities,
root_dir = root_dir,
settings = {
java = {
-- jdt = {
-- ls = {
-- vmargs = "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx1G -Xms100m"
-- }
-- },
eclipse = {
downloadSources = true
},
configuration = {
updateBuildConfiguration = "interactive"
-- runtimes = {
-- {
-- name = "JavaSE-17",
-- path = "/home/jrakhman/.sdkman/candidates/java/17.0.4-oracle",
-- },
-- },
},
maven = {
downloadSources = true
},
implementationsCodeLens = {
enabled = true
},
referencesCodeLens = {
enabled = true
},
references = {
includeDecompiledSources = true
},
inlayHints = {
parameterNames = {
enabled = "all" -- literals, all, none
}
},
format = {
enabled = false
}
},
signatureHelp = {
enabled = true
},
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.*"}
},
contentProvider = {
preferred = "fernflower"
},
extendedClientCapabilities = extendedClientCapabilities,
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999
}
},
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}"
},
useBlocks = true
}
},
flags = {
allow_incremental_sync = true
},
init_options = {
bundles = {}
}
}
end
}
},
mappings = {
-- first key is the mode
n = {
["<leader><Tab>"] = {
"<cmd>Telescope buffers<CR>",
name = "tab buffers"
},
["<C-f>"] = {
function()
require("smart-splits").move_cursor_right()
end,
desc = "Move to right split"
}
}
},
plugins = {
init = {
["mfussenegger/nvim-jdtls"] = {
module = "jdtls"
},
{
"jay-babu/mason-null-ls.nvim",
opts = {
handlers = {
-- for prettier
prettier = function()
require("null-ls").register(require("null-ls").builtins.formatting.prettier.with {
condition = function(utils)
return utils.root_has_file "package.json" or utils.root_has_file ".prettierrc" or
utils.root_has_file ".prettierrc.json" or
utils.root_has_file ".prettierrc.js"
end
})
end,
-- for prettierd
prettierd = function()
require("null-ls").register(require("null-ls").builtins.formatting.prettierd.with {
condition = function(utils)
return utils.root_has_file "package.json" or utils.root_has_file ".prettierrc" or
utils.root_has_file ".prettierrc.json" or
utils.root_has_file ".prettierrc.js"
end
})
end,
-- For eslint_d:
eslint_d = function()
require("null-ls").register(require("null-ls").builtins.diagnostics.eslint_d.with {
condition = function(utils)
return utils.root_has_file "package.json" or utils.root_has_file ".eslintrc.json" or
utils.root_has_file ".eslintrc.js" or
utils.root_has_file ".eslint.config.js"
end
})
end
}
}
}
},
["mason-lspconfig"] = {
ensure_installed = {"jdtls"}
}
},
polish = function()
vim.api.nvim_create_autocmd("Filetype", {
pattern = "java", -- autocmd to start jdtls
callback = function()
local config = astronvim.lsp.server_settings "jdtls"
if config.root_dir and config.root_dir ~= "" then
print(config)
require("jdtls").start_or_attach(config)
end
end
})
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment