From 0411baab8985450a15a54a46c48197b704bc24cc Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sat, 22 Jul 2023 03:12:42 -0400 Subject: [PATCH] feat(lang): add java (#1132) --- lua/lazyvim/plugins/extras/lang/java.lua | 74 ++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lua/lazyvim/plugins/extras/lang/java.lua diff --git a/lua/lazyvim/plugins/extras/lang/java.lua b/lua/lazyvim/plugins/extras/lang/java.lua new file mode 100644 index 00000000..0f406287 --- /dev/null +++ b/lua/lazyvim/plugins/extras/lang/java.lua @@ -0,0 +1,74 @@ +return { + -- Add java to treesitter. + { + "nvim-treesitter/nvim-treesitter", + opts = function(_, opts) + if type(opts.ensure_installed) == "table" then + vim.list_extend(opts.ensure_installed, { "java" }) + end + end, + }, + + -- Set up lsp with mfussenegger/nvim-jdtls instead of nvim-lspconfig. + { + "neovim/nvim-lspconfig", + dependencies = { "folke/which-key.nvim", "mfussenegger/nvim-jdtls" }, + opts = { + -- make sure mason installs the server + servers = { + jdtls = { + -- stylua: ignore + keys = { + { "co", function() require("jdtls").organize_imports() end, desc = "Organize Imports", }, + { "cR", function() require("jdtls").rename_file() end, desc = "Rename File", }, + { "cxv", function() require("jdtls").extract_variable() end, desc = "Extract Variable", }, + { "cxv", function() require("jdtls").extract_variable({ visual = true }) end, mode = "v", desc = "Extract Variable", }, + { "cxc", function() require("jdtls").extract_constant() end, desc = "Extract Constant", }, + { "cxc", function() require("jdtls").extract_constant({ visual = true }) end, mode = "v", desc = "Extract Constant", }, + { "cxm", function() require("jdtls").extract_method({ visual = true }) end, mode = "v", desc = "Extract Method", }, + }, + }, + }, + setup = { + -- Override the default jdtls server startup to use nvim-jdtls. + jdtls = function() + -- The lspconfig configuration for jdtls contains two useful items: + -- 1. The list of filetypes on which to match. + -- 2. Custom method for finding the root for a java project. + local lsp_config = require("lspconfig.server_configurations.jdtls").default_config + local find_java_project_root = lsp_config.root_dir + local filetypes = lsp_config.filetypes + + -- Attach jdtls for the proper filetypes (i.e. java). + -- Existing server will be reused if the root_dir matches. + vim.api.nvim_create_autocmd("FileType", { + group = vim.api.nvim_create_augroup("MyJdtls", { clear = true }), + pattern = filetypes, + callback = function() + local fname = vim.api.nvim_buf_get_name(0) + local root_dir = find_java_project_root(fname) + local project_name = root_dir and vim.fs.basename(root_dir) + local cmd = { "jdtls" } + if project_name then + local jdtls_cache_dir = vim.fs.joinpath(vim.fn.stdpath("cache"), "jdtls", project_name) + vim.list_extend(cmd, { + "-configuration", + vim.fs.joinpath(jdtls_cache_dir, "config"), + "-data", + vim.fs.joinpath(jdtls_cache_dir, "workspace"), + }) + end + require("jdtls").start_or_attach({ + cmd = cmd, + root_dir = root_dir, + }) + require("which-key").register({ c = { x = { name = "Extract" } } }, { prefix = "" }) + end, + }) + + return true -- avoid duplicate servers + end, + }, + }, + }, +}