support: nvim-jdtls
This commit is contained in:
23
lua/custom/configs/java.lua
Normal file
23
lua/custom/configs/java.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
local g = require("custom.configs.table")
|
||||
|
||||
local jdtls_bin = g.jdtls_home .. "/bin/jdtls"
|
||||
local lombok_jar = g.jdtls_home .. "/lombok.jar"
|
||||
|
||||
if g.is_windows then
|
||||
jdtls_bin = g.path_to_windows(jdtls_bin)
|
||||
lombok_jar = g.path_to_windows(lombok_jar)
|
||||
end
|
||||
|
||||
if not g.is_file_exist(jdtls_bin) then
|
||||
vim.notify("jdtls plugin not exist", "error")
|
||||
return
|
||||
end
|
||||
|
||||
if not g.is_file_exist(lombok_jar) then
|
||||
vim.notify("lombok plugin not exist", "error")
|
||||
return
|
||||
end
|
||||
|
||||
if not g.is_directory_exist(g.jdtls_workspace) then
|
||||
os.execute("mkdir " .. g.jdtls_workspace)
|
||||
end
|
||||
136
lua/custom/configs/jdtls.lua
Normal file
136
lua/custom/configs/jdtls.lua
Normal file
@@ -0,0 +1,136 @@
|
||||
local g = require "custom.configs.table"
|
||||
|
||||
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 project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
|
||||
|
||||
local workspace = g.jdtls_workspace .. project_name
|
||||
local launcher = g.jdtls_home .. "/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar"
|
||||
local configuration = g.jdtls_home .. "/config_linux"
|
||||
local lombok_bin = g.jdtls_home .. "/lombok.jar"
|
||||
|
||||
if g.is_windows then
|
||||
workspace = g.path_to_windows(workspace)
|
||||
launcher = g.path_to_windows(launcher)
|
||||
configuration = g.path_to_windows(g.jdtls_home) .. "\\config_win"
|
||||
lombok_bin = g.path_to_windows(lombok_bin)
|
||||
end
|
||||
|
||||
local jdtls = require "jdtls"
|
||||
local cmp_nvim_lsp = require "cmp_nvim_lsp"
|
||||
local client_capabilities = vim.lsp.protocol.make_client_capabilities()
|
||||
local capabilities = cmp_nvim_lsp.default_capabilities(client_capabilities)
|
||||
|
||||
return {
|
||||
flags = {
|
||||
debounce_text_changes = 80,
|
||||
},
|
||||
capabilities = capabilities,
|
||||
on_attach = function(_, bufnr)
|
||||
-- https://github.com/mfussenegger/dotfiles/blob/833d634251ebf3bf7e9899ed06ac710735d392da/vim/.config/nvim/ftplugin/java.lua#L88-L94
|
||||
local opts = { silent = true, buffer = bufnr }
|
||||
vim.keymap.set("n", "<leader>lo", jdtls.organize_imports, { desc = "Organize imports", buffer = bufnr })
|
||||
-- Should 'd' be reserved for debug?
|
||||
vim.keymap.set("n", "<leader>df", jdtls.test_class, opts)
|
||||
vim.keymap.set("n", "<leader>dn", jdtls.test_nearest_method, opts)
|
||||
vim.keymap.set("n", "<leader>rv", jdtls.extract_variable_all, { desc = "Extract variable", buffer = bufnr })
|
||||
vim.keymap.set(
|
||||
"v",
|
||||
"<leader>rm",
|
||||
[[<ESC><CMD>lua require('jdtls').extract_method(true)<CR>]],
|
||||
{ desc = "Extract method", buffer = bufnr }
|
||||
)
|
||||
vim.keymap.set("n", "<leader>rc", jdtls.extract_constant, { desc = "Extract constant", buffer = bufnr })
|
||||
end,
|
||||
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",
|
||||
"-Xmx1g",
|
||||
"--add-modules=ALL-SYSTEM",
|
||||
"--add-opens",
|
||||
"java.base/java.util=ALL-UNNAMED",
|
||||
"--add-opens",
|
||||
"java.base/java.lang=ALL-UNNAMED",
|
||||
"-javaagent:" .. lombok_bin,
|
||||
"-Xbootclasspath/a:" .. lombok_bin,
|
||||
"-jar",
|
||||
launcher,
|
||||
"-configuration",
|
||||
configuration,
|
||||
"-data",
|
||||
workspace,
|
||||
},
|
||||
root_dir = root_dir,
|
||||
settings = {
|
||||
java = {
|
||||
maven = {
|
||||
downloadSources = true,
|
||||
},
|
||||
eclipse = {
|
||||
downloadSources = true,
|
||||
},
|
||||
implementationsCodeLens = {
|
||||
enabled = true,
|
||||
},
|
||||
referencesCodeLens = {
|
||||
enabled = true,
|
||||
},
|
||||
references = {
|
||||
includeDecompiledSources = true,
|
||||
},
|
||||
format = {
|
||||
enabled = true,
|
||||
settings = {
|
||||
url = vim.fn.stdpath "config" .. "/intellij-java-google-style.xml",
|
||||
profile = "GoogleStyle",
|
||||
},
|
||||
},
|
||||
signatureHelp = { enabled = true },
|
||||
contentProvider = { preferred = "fernflower" }, -- Use fernflower to decompile library code
|
||||
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.*",
|
||||
},
|
||||
},
|
||||
sources = {
|
||||
organizeImports = {
|
||||
starThreshold = 9999,
|
||||
staticStarThreshold = 9999,
|
||||
},
|
||||
},
|
||||
codeGeneration = {
|
||||
toString = {
|
||||
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
|
||||
},
|
||||
hashCodeEquals = {
|
||||
useJava7Objects = true,
|
||||
},
|
||||
useBlocks = true,
|
||||
},
|
||||
},
|
||||
},
|
||||
init_options = {
|
||||
bundles = {},
|
||||
},
|
||||
}
|
||||
@@ -11,6 +11,7 @@ table.insert(runtime_path, "lua/?/init.lua")
|
||||
lspconfig.lua_ls.setup {
|
||||
on_attach = on_attach,
|
||||
capabilities = capabilities,
|
||||
filetypes = { "lua" },
|
||||
settings = {
|
||||
Lua = {
|
||||
runtime = {
|
||||
@@ -52,3 +53,22 @@ lspconfig.gopls.setup {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
-- VUE LSP
|
||||
lspconfig.volar.setup {
|
||||
filetypes = { "typescript", "javascript", "javascriptreact", "typescriptreact", "vue", "json" },
|
||||
init_options = {
|
||||
typescript = {
|
||||
-- Alternative location if installed as root:
|
||||
tsdk = "/usr/local/lib/node_modules/typescript/lib",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
-- BASH LSP
|
||||
lspconfig.bashls.setup {
|
||||
filetypes = { "sh" },
|
||||
}
|
||||
|
||||
-- JAVA LSP
|
||||
|
||||
|
||||
49
lua/custom/configs/table.lua
Normal file
49
lua/custom/configs/table.lua
Normal file
@@ -0,0 +1,49 @@
|
||||
local g = {}
|
||||
|
||||
g.platform_windows = function()
|
||||
local delimeter = package.config:sub(1, 1)
|
||||
if delimeter == "/" then
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
g.get_homepage = function()
|
||||
if g.platform_windows() then
|
||||
return os.getenv "USERPROFILE"
|
||||
end
|
||||
return os.getenv "HOME"
|
||||
end
|
||||
|
||||
g.is_file_exist = function(filename)
|
||||
local f = io.open(filename)
|
||||
if f ~= nil then
|
||||
io.close(f)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
g.path_to_windows = function(path)
|
||||
return (string.gsub(path, "/", "\\"))
|
||||
end
|
||||
|
||||
g.is_directory_exist = function(dirname)
|
||||
return os.rename(dirname, dirname) and true or false
|
||||
end
|
||||
|
||||
local jdtls_home = g.get_homepage() .. "/.local/share/nvim/mason/packages/jdtls"
|
||||
local workspace = g.get_homepage() .. "/.jdtls"
|
||||
|
||||
if g.is_windows then
|
||||
jdtls_home = g.get_homepage() .. "\\AppData\\Local\\nvim-data\\mason\\packages\\jdtls"
|
||||
workspace = g.get_homepage() .. "\\.jdtls"
|
||||
end
|
||||
|
||||
g.is_windows = g.platform_windows()
|
||||
g.is_linux = not g.is_windows
|
||||
g.jdtls_home = jdtls_home
|
||||
g.jdtls_workspace = workspace
|
||||
|
||||
return g
|
||||
@@ -69,3 +69,9 @@ vim.opt.listchars:append "eol:↴"
|
||||
-- 文件树配置
|
||||
vim.g.loaded_netrw = 1
|
||||
vim.g.loaded_netrwPlugin = 1
|
||||
-- 开启 Folding
|
||||
vim.wo.foldmethod = "expr"
|
||||
vim.wo.foldexpr = "nvim_treesitter#foldexpr()"
|
||||
-- 默认不要折叠
|
||||
-- https://stackoverflow.com/questions/8316139/how-to-set-the-default-to-unfolded-when-you-open-a-file
|
||||
vim.wo.foldlevel = 99
|
||||
|
||||
@@ -1,5 +1,24 @@
|
||||
local M = {}
|
||||
|
||||
M.windows = {
|
||||
n = {
|
||||
-- 折叠配置
|
||||
["<leader>z+"] = { "zM", "" },
|
||||
["<leaderz->"] = { "zR", "" },
|
||||
["<leader>zz"] = { "za", "" },
|
||||
-- 分屏
|
||||
["<leader>sv"] = { ":vsp<CR>", "" },
|
||||
["<leader>sh"] = { ":sp<CR>", "" },
|
||||
-- 快所退出
|
||||
["q"] = { ":wq<CR>", "" },
|
||||
["qn"] = { ":q!<CR>", "" },
|
||||
["qq"] = { ":qa!<CR>", "" },
|
||||
},
|
||||
i = {
|
||||
["jk"] = { "<ESC>", "" },
|
||||
},
|
||||
}
|
||||
|
||||
M.format = {
|
||||
n = {
|
||||
["="] = { "<cmd>lua vim.lsp.buf.format()<cr>", "general format" },
|
||||
|
||||
@@ -17,7 +17,6 @@ local plugins = {
|
||||
"golangci-lint-langserver",
|
||||
"golines",
|
||||
"gopls",
|
||||
"gradle-language-server",
|
||||
"html-lsp",
|
||||
"json-lsp",
|
||||
"lua-language-server",
|
||||
@@ -30,17 +29,13 @@ local plugins = {
|
||||
"yaml-language-server",
|
||||
"yamlfmt",
|
||||
"yamllint",
|
||||
"jdtls",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"jose-elias-alvarez/null-ls.nvim",
|
||||
config = function()
|
||||
require "custom.configs.null-ls"
|
||||
end,
|
||||
},
|
||||
{
|
||||
"neovim/nvim-lspconfig",
|
||||
lazy = false,
|
||||
dependencies = {
|
||||
"jose-elias-alvarez/null-ls.nvim",
|
||||
config = function()
|
||||
@@ -52,6 +47,14 @@ local plugins = {
|
||||
require "custom.configs.lspconfig"
|
||||
end,
|
||||
},
|
||||
{
|
||||
"mfussenegger/nvim-jdtls",
|
||||
ft = { "java" },
|
||||
config = function()
|
||||
require "custom.configs.java"
|
||||
-- require "custom.configs.jdtls"
|
||||
end,
|
||||
},
|
||||
{
|
||||
"ray-x/go.nvim",
|
||||
dependencies = { -- optional packages
|
||||
|
||||
Reference in New Issue
Block a user