Skip to content

Instantly share code, notes, and snippets.

@liujoey
Last active June 14, 2023 04:52
Show Gist options
  • Save liujoey/eb800a0eafcbdb78a7ac7d758e5370bc to your computer and use it in GitHub Desktop.
Save liujoey/eb800a0eafcbdb78a7ac7d758e5370bc to your computer and use it in GitHub Desktop.
nvim-jdtls settings
local status_ok, jdtls = pcall(require, "jdtls")
if not status_ok then
return
end
-- Determine OS
local home = vim.env.HOME
local launcher_path =
vim.fn.glob(home .. "/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_*.jar")
if #launcher_path == 0 then
launcher_path =
vim.fn.glob(home .. "/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_*.jar", 1, 1)[1]
end
if vim.fn.has "mac" == 1 then
WORKSPACE_PATH = home .. "/workspace/"
CONFIG = "mac"
elseif vim.fn.has "unix" == 1 then
WORKSPACE_PATH = home .. "/workspace/"
CONFIG = "linux"
else
print "Unsupported system"
end
-- Find root of project
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
local root_dir = require("jdtls.setup").find_root(root_markers)
if root_dir == "" then
return
end
local extendedClientCapabilities = jdtls.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = WORKSPACE_PATH .. project_name
-- NOTE: for debugging
-- git clone [email protected]:microsoft/java-debug.git ~/.config/lvim/.java-debug
-- cd ~/.config/lvim/.java-debug/
-- ./mvnw clean install
-- OR use Mason to install, here I use Mason
local bundles = vim.fn.glob(home .. "/.local/share/nvim/mason/packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar", true, true)
-- NOTE: for testing
-- git clone [email protected]:microsoft/vscode-java-test.git ~/.config/lvim/.vscode-java-test
-- cd ~/.config/lvim/vscode-java-test
-- npm install
-- npm run build-plugin
-- Or use Mason to install, here I use Mason
local extra_bundles = vim.fn.glob(home .. "/.local/share/nvim/mason/packages/java-test/extension/server/*.jar", true, true)
vim.list_extend(bundles, extra_bundles)
local config = {
cmd = {
"java",
"-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",
"-javaagent:" .. home .. "/.local/share/nvim/mason/packages/jdtls/lombok.jar",
"-Xms1g",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
"-jar",
launcher_path,
"-configuration",
home .. "/.local/share/nvim/mason/packages/jdtls/config_" .. CONFIG,
"-data",
workspace_dir,
},
on_attach = require("lvim.lsp").common_on_attach,
on_init = require("lvim.lsp").common_on_init,
on_exit = require("lvim.lsp").common_on_exit,
capabilities = require("lvim.lsp").common_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",
},
maven = {
downloadSources = true,
},
implementationsCodeLens = {
enabled = true,
},
referencesCodeLens = {
enabled = true,
},
references = {
includeDecompiledSources = true,
},
format = {
enabled = true,
settings = {
profile = "GoogleStyle",
url = home .. "/.config/lvim/.java-google-formatter.xml",
},
},
},
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,
server_side_fuzzy_completion = true,
},
init_options = {
bundles = bundles,
},
}
jdtls.start_or_attach(config)
jdtls.setup_dap { hotcodereplace = "auto" }
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 JdtJol lua require('jdtls').jol()"
vim.cmd "command! -buffer JdtBytecode lua require('jdtls').javap()"
-- vim.cmd "command! -buffer JdtJshell lua require('jdtls').jshell()"
local wkstatus_ok, which_key = pcall(require, "which-key")
if not wkstatus_ok then
return
end
local opts = {
mode = "n",
prefix = "<leader>",
buffer = nil,
silent = true,
noremap = true,
nowait = true,
}
local vopts = {
mode = "v",
prefix = "<leader>",
buffer = nil,
silent = true,
noremap = true,
nowait = true,
}
local mappings = {
j = {
name = "Java",
o = { "<Cmd>lua require'jdtls'.organize_imports()<CR>", "Organize Imports" },
v = { "<Cmd>lua require('jdtls').extract_variable()<CR>", "Extract Variable" },
c = { "<Cmd>lua require('jdtls').extract_constant()<CR>", "Extract Constant" },
t = { "<Cmd>lua require'jdtls'.test_nearest_method()<CR>", "Test Method" },
T = { "<Cmd>lua require'jdtls'.test_class()<CR>", "Test Class" },
u = { "<Cmd>JdtUpdateConfig<CR>", "Update Config" },
},
}
local vmappings = {
j = {
name = "Java",
v = { "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", "Extract Variable" },
c = { "<Esc><Cmd>lua require('jdtls').extract_constant(true)<CR>", "Extract Constant" },
m = { "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", "Extract Method" },
},
}
which_key.register(mappings, opts)
which_key.register(vmappings, vopts)
vim.cmd [[setlocal shiftwidth=2]]
vim.cmd [[setlocal tabstop=2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment