From 95ae4c03c4df0adff9491f010287a6bca6fd6c2f Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Wed, 11 Oct 2023 12:08:47 +0200 Subject: [PATCH] neovim: Use nvim-jdtls --- users/simon/modules/neovim/default.nix | 1 + users/simon/modules/neovim/init.lua | 63 ++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/users/simon/modules/neovim/default.nix b/users/simon/modules/neovim/default.nix index 8d7f79b..7c068f8 100644 --- a/users/simon/modules/neovim/default.nix +++ b/users/simon/modules/neovim/default.nix @@ -84,6 +84,7 @@ in luasnip neogit nvim-cmp + nvim-jdtls nvim-lspconfig nvim-solarized-lua nvim-treesitter diff --git a/users/simon/modules/neovim/init.lua b/users/simon/modules/neovim/init.lua index cd6a599..009f7c7 100644 --- a/users/simon/modules/neovim/init.lua +++ b/users/simon/modules/neovim/init.lua @@ -299,10 +299,6 @@ lsp.hls.setup { on_attach = on_attach, root_dir = lsp.util.root_pattern('*.cabal', 'stack.yaml', 'cabal.project', 'package.yaml', 'hie.yaml', '.git'), } -lsp.jdtls.setup { - on_attach = on_attach, - cmd = {'jdt-language-server', '-configuration', '/home/simon/.cache/jdtls/config', '-data', '/home/simon/.cache/jdtls/workspace'}, -- copied from upstream, but changed executable name -} lsp.ltex.setup { on_attach = on_attach, settings = { @@ -361,6 +357,65 @@ lsp.rust_analyzer.setup { }, } +vim.api.nvim_create_autocmd({"FileType"}, { + pattern = "java", + callback = function() + local jdtls = require('jdtls') + + local config = { + cmd = { + 'jdt-language-server', + '-configuration', vim.fn.expand('~') .. '/.cache/jdtls/config', + '-data', vim.fn.expand('~') .. '/.local/share/jdtls/workspace/' .. vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t'), + }, + + -- https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request + settings = { + java = { + signatureHelp = { enabled = true }; + completion = { + favoriteStaticMembers = { + }, + filteredTypes = { + "com.sun.*", + "java.awt.*", + "jdk.*", + "sun.*", + }, + }; + maven = { + downloadSources = true, + }, + + implementationsCodeLens = { + enabled = true, + }, + referencesCodeLens = { + enabled = true, + }, + }, + }, + + on_attach = function(client, bufnr) + on_attach(client, bufnr) -- global lsp configuration + + jdtls.setup.add_commands() + + local opts = { noremap = true, silent = true } + + vim.keymap.set('n', 'jo', jdtls.organize_imports, opts) + vim.keymap.set('n', 'jtc', jdtls.test_class, opts) + vim.keymap.set('n', 'jtm', jdtls.test_nearest_method, opts) + vim.keymap.set('n', 'jev', jdtls.extract_variable, opts) + vim.keymap.set('v', 'jem', [[lua require('jdtls').extract_method(true)]], opts) + vim.keymap.set('n', 'jec', jdtls.extract_constanconstant, opts) + end, + } + + jdtls.start_or_attach(config) + end +}) + require('trouble').setup {} -- Tree Sitter