From dc9f6a8b1fab4e2af7e21619c972b12efa441a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Fr=C3=B8kj=C3=A6r=20Wortmann?= Date: Sun, 22 Oct 2023 21:27:54 +0200 Subject: [PATCH] my configuration --- init.lua | 99 +++++++--------- lua/custom/plugins/autopairs.lua | 15 +++ lua/custom/plugins/filetree.lua | 27 +++++ lua/custom/plugins/nvim-jdtls.lua | 169 +++++++++++++++++++++++++++ lua/kickstart/plugins/autoformat.lua | 5 + lua/kickstart/plugins/debug.lua | 4 +- lua/lsp/keymap.lua | 47 ++++++++ vscode-java-test | 1 + 8 files changed, 311 insertions(+), 56 deletions(-) create mode 100644 lua/custom/plugins/autopairs.lua create mode 100644 lua/custom/plugins/filetree.lua create mode 100644 lua/custom/plugins/nvim-jdtls.lua create mode 100644 lua/lsp/keymap.lua create mode 120000 vscode-java-test diff --git a/init.lua b/init.lua index ee1f390e..8db54233 100644 --- a/init.lua +++ b/init.lua @@ -110,7 +110,11 @@ require('lazy').setup({ }, -- Useful plugin to show you pending keybinds. - { 'folke/which-key.nvim', opts = {} }, + { + 'folke/which-key.nvim', + opts = {} + }, + { -- Adds git related signs to the gutter, as well as utilities for managing changes 'lewis6991/gitsigns.nvim', @@ -183,7 +187,7 @@ require('lazy').setup({ }, -- "gc" to comment visual regions/lines - { 'numToStr/Comment.nvim', opts = {} }, + { 'numToStr/Comment.nvim', opts = {} }, -- Fuzzy Finder (files, lsp, etc) { @@ -218,8 +222,8 @@ require('lazy').setup({ -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart -- These are some example plugins that I've included in the kickstart repository. -- Uncomment any of the lines below to enable them. - -- require 'kickstart.plugins.autoformat', - -- require 'kickstart.plugins.debug', + require 'kickstart.plugins.autoformat', + require 'kickstart.plugins.debug', -- NOTE: The import below can automatically add your own plugins, configuration, etc from `lua/custom/plugins/*.lua` -- You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping @@ -227,7 +231,7 @@ require('lazy').setup({ -- Uncomment the following line and add your plugins to `lua/custom/plugins/*.lua` to get going. -- -- For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins - -- { import = 'custom.plugins' }, + { import = 'custom.plugins' }, }, {}) -- [[ Setting options ]] @@ -241,13 +245,32 @@ vim.o.hlsearch = false vim.wo.number = true -- Enable mouse mode -vim.o.mouse = 'a' +vim.o.mouse = false -- Sync clipboard between OS and Neovim. -- Remove this option if you want your OS clipboard to remain independent. -- See `:help 'clipboard'` -vim.o.clipboard = 'unnamedplus' +-- vim.o.clipboard = 'unnamedplus' +--- Copy-paste +vim.keymap.set('v', 'p', '"_dP') +vim.keymap.set('n', 'y', '"+y') +vim.keymap.set('v', 'y', '"+y') +vim.keymap.set('n', 'Y', 'gg"+yG') +vim.keymap.set('n', 'd', '"_d') +vim.keymap.set('v', 'd', '"_d') +vim.g.clipboard = { + name= 'WslClipboard', + copy= { + ['+'] = 'clip.exe', + ['*'] = 'clip.exe', + }, + paste = { + ['+'] = 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))', + ['*'] = 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))', + }, + cache_enabled= 0, +} -- Enable break indent vim.o.breakindent = true @@ -296,6 +319,8 @@ vim.api.nvim_create_autocmd('TextYankPost', { -- See `:help telescope` and `:help telescope.setup()` require('telescope').setup { defaults = { + path_display = { shorten = { len = 3, exclude = { 1, 2, 3, -3, -2, -1 } } }, + mappings = { i = { [''] = false, @@ -333,7 +358,7 @@ vim.keymap.set('n', 'sr', require('telescope.builtin').resume, { desc = vim.defer_fn(function() require('nvim-treesitter.configs').setup { -- Add languages to be installed here that you want installed for treesitter - ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'javascript', 'typescript', 'vimdoc', 'vim', 'bash' }, + ensure_installed = { 'java', 'go', 'lua', 'python', 'rust', 'javascript', 'vimdoc', 'vim', 'bash' }, -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!) auto_install = false, @@ -403,50 +428,7 @@ vim.keymap.set('n', 'e', vim.diagnostic.open_float, { desc = 'Open float vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' }) -- [[ Configure LSP ]] --- This function gets run when an LSP connects to a particular buffer. -local on_attach = function(_, bufnr) - -- NOTE: Remember that lua is a real programming language, and as such it is possible - -- to define small helper and utility functions so you don't have to repeat yourself - -- many times. - -- - -- In this case, we create a function that lets us more easily define mappings specific - -- for LSP related items. It sets the mode, buffer and description for us each time. - local nmap = function(keys, func, desc) - if desc then - desc = 'LSP: ' .. desc - end - - vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) - end - - nmap('rn', vim.lsp.buf.rename, '[R]e[n]ame') - nmap('ca', vim.lsp.buf.code_action, '[C]ode [A]ction') - - nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition') - nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') - nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation') - nmap('D', vim.lsp.buf.type_definition, 'Type [D]efinition') - nmap('ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') - nmap('ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') - - -- See `:help K` for why this keymap - nmap('K', vim.lsp.buf.hover, 'Hover Documentation') - nmap('', vim.lsp.buf.signature_help, 'Signature Documentation') - - -- Lesser used LSP functionality - nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') - nmap('wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') - nmap('wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') - nmap('wl', function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end, '[W]orkspace [L]ist Folders') - - -- Create a command `:Format` local to the LSP buffer - vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) - vim.lsp.buf.format() - end, { desc = 'Format current buffer with LSP' }) -end - +local lsp_settings = require('lsp.keymap') -- document existing key chains require('which-key').register { ['c'] = { name = '[C]ode', _ = 'which_key_ignore' }, @@ -478,7 +460,6 @@ local servers = { -- rust_analyzer = {}, -- tsserver = {}, -- html = { filetypes = { 'html', 'twig', 'hbs'} }, - lua_ls = { Lua = { workspace = { checkThirdParty = false }, @@ -498,14 +479,22 @@ capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) local mason_lspconfig = require 'mason-lspconfig' mason_lspconfig.setup { - ensure_installed = vim.tbl_keys(servers), + ensure_installed = vim.list_extend(vim.tbl_keys(servers), { "jdtls" }) } mason_lspconfig.setup_handlers { function(server_name) + if server_name == 'jdtls' then + -- disable jdtls config from lspconfig. jdtls will be configured by nvim-jdtls since it much more powerful + require('lspconfig')[server_name].setup = function() + return true + end + end + + -- enable language servers for all other require('lspconfig')[server_name].setup { capabilities = capabilities, - on_attach = on_attach, + on_attach = lsp_settings.on_attach, settings = servers[server_name], filetypes = (servers[server_name] or {}).filetypes, } diff --git a/lua/custom/plugins/autopairs.lua b/lua/custom/plugins/autopairs.lua new file mode 100644 index 00000000..d271227f --- /dev/null +++ b/lua/custom/plugins/autopairs.lua @@ -0,0 +1,15 @@ +return { + "windwp/nvim-autopairs", + -- Optional dependency + dependencies = { 'hrsh7th/nvim-cmp' }, + config = function() + require("nvim-autopairs").setup {} + -- If you want to automatically add `(` after selecting a function or method + local cmp_autopairs = require('nvim-autopairs.completion.cmp') + local cmp = require('cmp') + cmp.event:on( + 'confirm_done', + cmp_autopairs.on_confirm_done() + ) + end, +} diff --git a/lua/custom/plugins/filetree.lua b/lua/custom/plugins/filetree.lua new file mode 100644 index 00000000..a79f7a73 --- /dev/null +++ b/lua/custom/plugins/filetree.lua @@ -0,0 +1,27 @@ +-- Unless you are still migrating, remove the deprecated commands from v1.x +vim.cmd([[ let g:neo_tree_remove_legacy_commands = 1 ]]) + +return { + "nvim-neo-tree/neo-tree.nvim", + version = "*", + dependencies = { + "nvim-lua/plenary.nvim", + "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended + "MunifTanjim/nui.nvim", + }, + config = function () + require('neo-tree').setup { + filesystem = { + hide_by_pattern = { -- uses glob style patterns + --"*.meta", + --"*/src/*/tsconfig.json", + "*/dk/nykredit/*" + }, + follow_current_file = { + enabled = true, -- This will find and focus the file in the active buffer every time + leave_dirs_open = false + } + } + } + end, +} diff --git a/lua/custom/plugins/nvim-jdtls.lua b/lua/custom/plugins/nvim-jdtls.lua new file mode 100644 index 00000000..6235e301 --- /dev/null +++ b/lua/custom/plugins/nvim-jdtls.lua @@ -0,0 +1,169 @@ +return { + + -- correctly setup mason lsp / dap extensions + -- { + -- "williamboman/mason.nvim", + -- opts = function(_, opts) + -- vim.list_extend(opts.ensure_installed, {"jdtls", "java-test", "java-debug-adapter" }) + -- end, + -- }, + { + "mfussenegger/nvim-jdtls", + ft = "java", + config = function() + local home = os.getenv('HOME') + local mason_registry = require("mason-registry") + local jdtls_pkg = mason_registry.get_package("jdtls") + local jdtls_path = jdtls_pkg:get_install_path() + local jdtls_bin = jdtls_path .. "/bin/jdtls" + + local java_test_pkg = mason_registry.get_package("java-test") + local java_test_path = java_test_pkg:get_install_path() + + local java_dbg_pkg = mason_registry.get_package("java-debug-adapter") + local java_dbg_path = java_dbg_pkg:get_install_path() + + local vscode_java_test_path = home .. "/.config/nvim/vscode-java-test" + + local jar_patterns = { + java_dbg_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar", + java_test_path .. "/extension/server/*.jar", + vscode_java_test_path .. "/*.jar" + } + + local bundles = {} + for _, jar_pattern in ipairs(jar_patterns) do + for _, bundle in ipairs(vim.split(vim.fn.glob(jar_pattern), '\n')) do + table.insert(bundles, bundle) + end + end + + local settings = { + java = { + signatureHelp = { enabled = true }, + contentProvider = { preferred = 'fernflower' }, + 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.*" + } + }, + sources = { + organizeImports = { + starThreshold = 9999, + staticStarThreshold = 9999, + }, + }, + codeGeneration = { + toString = { + template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}" + } + }, + maven = { + downloadSources = true, + }, + import = { + maven = { + enabled = true, + }, + }, + configuration = { + runtimes = { + { + name = "JavaSE-1.8", + path = "/usr/lib/jvm/java-8-openjdk-amd64", + }, + { + name = "JavaSE-11", + path = "/usr/lib/jvm/java-11-openjdk-amd64", + default = true, + }, + { + name = "JavaSE-17", + path = "/usr/lib/jvm/java-17-openjdk-amd64", + }, + -- { + -- name = "JavaSE-19", + -- path = "/usr/lib/jvm/java-19-openjdk-amd64", + -- }, + } + } + } + } + + local function print_test_results(items) + if #items > 0 then + vim.cmd([[Trouble quickfix]]) + else + vim.cmd([[TroubleClose quickfix]]) + end + end + + local extendedClientCapabilities = require('jdtls').extendedClientCapabilities + extendedClientCapabilities.resolveAdditionalTextEditsSupport = true + extendedClientCapabilities.progressReportProvider = false + + + local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t") + -- calculate workspace dir + local workspace_folder = home .. "/.cache/jdtls/workspace/" .. project_name + + local capabilities = require("cmp_nvim_lsp").default_capabilities() + + local on_attach = function(_, buffer) + require('lsp.keymap').on_attach(_, buffer) + + -- custom keymaps + vim.keymap.set("n", "co", function() require("jdtls").organize_imports() end, + { buffer = buffer, desc = "LSP: Organize Imports" }) + + vim.keymap.set("n", "ct", + function() require("jdtls").pick_test({ bufnr = buffer, after_test = print_test_results }) end, + { buffer = buffer, desc = "LSP: Run Test" }) + require("jdtls").setup_dap({ hotcodereplace = "auto" }) + require("jdtls.dap").setup_dap_main_class_configs() + end + + -- get the mason install path + local config = { + init_options = { + bundles = bundles, + extendedClientCapabilities = extendedClientCapabilities, + }, + cmd = { + jdtls_bin, + "--jvm-arg=-javaagent:" .. jdtls_path .. "/lombok.jar", + "-data", + workspace_folder, + }, + -- attach general lsp 'on_attach function' + -- on_attach = require("lsp.keymap").on_attach, + on_attach = on_attach, + capabilities = capabilities, + -- we naively believe that the whole project is versioned through git, and therefore + root_dir = vim.fs.dirname( + vim.fs.find({ ".git" }, { upward = true })[1] + ), + settings = settings, + flags = { + allow_incremental_sync = true, + }, + on_init = function(client, _) + client.notify('workspace/didChangeConfiguration', { settings = settings }) + end, + } + + vim.api.nvim_create_autocmd("FileType", { + pattern = "java", + callback = function() + require("jdtls").start_or_attach(config) + end, + }) + end, + } +} diff --git a/lua/kickstart/plugins/autoformat.lua b/lua/kickstart/plugins/autoformat.lua index bc56b15b..57a16c03 100644 --- a/lua/kickstart/plugins/autoformat.lua +++ b/lua/kickstart/plugins/autoformat.lua @@ -50,6 +50,11 @@ return { return end + -- disable auto format for java + if client.name == 'jdtls' then + return + end + -- Create an autocmd that will run *before* we save the buffer. -- Run the formatting command for the LSP that has just attached. vim.api.nvim_create_autocmd('BufWritePre', { diff --git a/lua/kickstart/plugins/debug.lua b/lua/kickstart/plugins/debug.lua index 7fc783fa..ee539614 100644 --- a/lua/kickstart/plugins/debug.lua +++ b/lua/kickstart/plugins/debug.lua @@ -29,6 +29,7 @@ return { -- Makes a best effort to setup the various debuggers with -- reasonable debug configurations automatic_setup = true, + automatic_installation = true, -- You can provide additional configuration to the handlers, -- see mason-nvim-dap README for more information @@ -38,7 +39,8 @@ return { -- online, please don't ask me how to install them :) ensure_installed = { -- Update this to ensure that you have the debuggers for the langs you want - 'delve', + 'java-test', + 'java-debug-adapter', }, } diff --git a/lua/lsp/keymap.lua b/lua/lsp/keymap.lua new file mode 100644 index 00000000..9c520eb3 --- /dev/null +++ b/lua/lsp/keymap.lua @@ -0,0 +1,47 @@ + + +-- This function gets run when an LSP connects to a particular buffer. +return { + on_attach = function(_, bufnr) + -- NOTE: Remember that lua is a real programming language, and as such it is possible + -- to define small helper and utility functions so you don't have to repeat yourself + -- many times. + -- + -- In this case, we create a function that lets us more easily define mappings specific + -- for LSP related items. It sets the mode, buffer and description for us each time. + local nmap = function(keys, func, desc) + if desc then + desc = 'LSP: ' .. desc + end + + vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) + end + + nmap('rn', vim.lsp.buf.rename, '[R]e[n]ame') + nmap('ca', vim.lsp.buf.code_action, '[C]ode [A]ction') + + nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition') + nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences') + nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation') + nmap('D', vim.lsp.buf.type_definition, 'Type [D]efinition') + nmap('ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols') + nmap('ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') + + -- See `:help K` for why this keymap + nmap('K', vim.lsp.buf.hover, 'Hover Documentation') + nmap('', vim.lsp.buf.signature_help, 'Signature Documentation') + + -- Lesser used LSP functionality + nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') + nmap('wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') + nmap('wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') + nmap('wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, '[W]orkspace [L]ist Folders') + + -- Create a command `:Format` local to the LSP buffer + vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) + vim.lsp.buf.format() + end, { desc = 'Format current buffer with LSP' }) + end +} diff --git a/vscode-java-test b/vscode-java-test new file mode 120000 index 00000000..778a1a4f --- /dev/null +++ b/vscode-java-test @@ -0,0 +1 @@ +/home/jfw/.dotfiles/nvim/dependencies/java/vscode-java-test \ No newline at end of file