my configuration

This commit is contained in:
Jacob Frøkjær Wortmann 2023-10-22 21:27:54 +02:00
parent 58f2dbab70
commit dc9f6a8b1f
8 changed files with 311 additions and 56 deletions

View file

@ -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,
}

View file

@ -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,
}

View file

@ -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", "<leader>co", function() require("jdtls").organize_imports() end,
{ buffer = buffer, desc = "LSP: Organize Imports" })
vim.keymap.set("n", "<leader>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,
}
}

View file

@ -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', {

View file

@ -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',
},
}

47
lua/lsp/keymap.lua Normal file
View file

@ -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('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
nmap('<leader>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('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
nmap('<leader>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('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
-- Lesser used LSP functionality
nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
nmap('<leader>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
}