From d3945cc6210072a8683ab39f2421fc3d0a8e9cbc Mon Sep 17 00:00:00 2001 From: Dennis Date: Wed, 9 Apr 2025 18:40:28 +0200 Subject: [PATCH] WIP: Move to vim.lsp --- home/modules/nvim/config/.luarc.json | 12 +- home/modules/nvim/config/init.lua | 1 + home/modules/nvim/config/lazy-lock.json | 31 +- home/modules/nvim/config/lsp/ts_ls.lua | 32 ++ home/modules/nvim/config/lua/dnsc/lsp.lua | 1 + home/modules/nvim/config/lua/dnsc/utils.lua | 20 ++ .../nvim/config/lua/plugins/conform.lua | 4 - .../nvim/config/lua/plugins/lsp.lua.archive | 276 ++++++++++++++++++ .../lua/plugins/mason-tool-installer.lua | 12 - 9 files changed, 348 insertions(+), 41 deletions(-) create mode 100644 home/modules/nvim/config/lsp/ts_ls.lua create mode 100644 home/modules/nvim/config/lua/dnsc/lsp.lua create mode 100644 home/modules/nvim/config/lua/plugins/lsp.lua.archive delete mode 100644 home/modules/nvim/config/lua/plugins/mason-tool-installer.lua diff --git a/home/modules/nvim/config/.luarc.json b/home/modules/nvim/config/.luarc.json index b243cc8..4c33a66 100644 --- a/home/modules/nvim/config/.luarc.json +++ b/home/modules/nvim/config/.luarc.json @@ -1,6 +1,8 @@ { - "diagnostics.globals": [ - "vim", - "Snacks" - ] -} \ No newline at end of file + "runtime.version": "LuaJIT", + "runtime.path": ["lua/?.lua", "lua/?/init.lua"], + "diagnostics.globals": ["vim", "Snacks"], + "workspace.checkThirdParty": false, + "workspace.library": ["$VIMRUNTIME"] +} + diff --git a/home/modules/nvim/config/init.lua b/home/modules/nvim/config/init.lua index 1a700e1..c3e9db1 100644 --- a/home/modules/nvim/config/init.lua +++ b/home/modules/nvim/config/init.lua @@ -13,4 +13,5 @@ Version: 1.0.0 require("dnsc.lazy") require("dnsc.utils") +require("dnsc.lsp") require("dnsc.keymaps") diff --git a/home/modules/nvim/config/lazy-lock.json b/home/modules/nvim/config/lazy-lock.json index 0821afa..52f03d6 100644 --- a/home/modules/nvim/config/lazy-lock.json +++ b/home/modules/nvim/config/lazy-lock.json @@ -1,35 +1,26 @@ { - "LuaSnip": { "branch": "master", "commit": "c9b9a22904c97d0eb69ccb9bab76037838326817" }, "barbar.nvim": { "branch": "master", "commit": "53b5a2f34b68875898f0531032fbf090e3952ad7" }, - "cmp-buffer": { "branch": "main", "commit": "b74fab3656eea9de20a9b8116afa3cfc4ec09657" }, - "cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" }, - "cmp-nvim-lsp": { "branch": "main", "commit": "a8912b88ce488f411177fc8aed358b04dc246d7b" }, - "cmp-path": { "branch": "main", "commit": "c6635aae33a50d6010bf1aa756ac2398a2d54c32" }, - "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, - "conform.nvim": { "branch": "master", "commit": "b1a75324ddf96b7bb84963a297b1ed334db087c0" }, + "blink.cmp": { "branch": "main", "commit": "cb5e346d9e0efa7a3eee7fd4da0b690c48d2a98e" }, + "conform.nvim": { "branch": "master", "commit": "eebc724d12c5579d733d1f801386e0ceb909d001" }, "flash.nvim": { "branch": "main", "commit": "3c942666f115e2811e959eabbdd361a025db8b63" }, "follow-md-links.nvim": { "branch": "main", "commit": "ce8735a15dc3e5fc5bb052ec51b849c03e57df53" }, - "fzf-lua": { "branch": "main", "commit": "6488ada2f376e47789391dc353b6d91a3f9de6f6" }, - "gitsigns.nvim": { "branch": "main", "commit": "3c76f7fabac723aa682365ef782f88a83ccdb4ac" }, - "go.nvim": { "branch": "master", "commit": "680de9a7c43c524f9baecbc24dd7ff3ae395b19b" }, + "friendly-snippets": { "branch": "main", "commit": "efff286dd74c22f731cdec26a70b46e5b203c619" }, + "fzf-lua": { "branch": "main", "commit": "47b85a25c0c0b2c20b4e75199ed01bb71e7814f5" }, + "gitsigns.nvim": { "branch": "main", "commit": "17ab794b6fce6fce768430ebc925347e349e1d60" }, + "go.nvim": { "branch": "master", "commit": "ef38820e413e10f47d83688dee41785bd885fb2a" }, "guihua.lua": { "branch": "master", "commit": "d783191eaa75215beae0c80319fcce5e6b3beeda" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, - "lualine.nvim": { "branch": "master", "commit": "1517caa8fff05e4b4999857319d3b0609a7f57fa" }, - "mason-lspconfig.nvim": { "branch": "main", "commit": "1a31f824b9cd5bc6f342fc29e9a53b60d74af245" }, - "mason-tool-installer.nvim": { "branch": "main", "commit": "98767d37f8e5255a5111fc1e3163232d4dc07bda" }, - "mason.nvim": { "branch": "main", "commit": "fc98833b6da5de5a9c5b1446ac541577059555be" }, + "lualine.nvim": { "branch": "master", "commit": "1ba400068bc178eb698b96ecfde82db59e7a7b8f" }, "mini.icons": { "branch": "main", "commit": "397ed3807e96b59709ef3292f0a3e253d5c1dc0a" }, "mini.pairs": { "branch": "main", "commit": "69864a2efb36c030877421634487fd90db1e4298" }, - "nvim-cmp": { "branch": "main", "commit": "059e89495b3ec09395262f16b1ad441a38081d04" }, - "nvim-lspconfig": { "branch": "master", "commit": "0ef64599b8aa0187ee5f6d92cb39c951f348f041" }, + "nvim-lspconfig": { "branch": "master", "commit": "94dda50b2d9a29d0b76562a9027029538840e2d7" }, "nvim-surround": { "branch": "main", "commit": "caf6f633d4d77a29b6e265b560c5a035d171a913" }, - "nvim-treesitter": { "branch": "master", "commit": "30654ee72a69e7c76a54b66d748dae088429e863" }, - "nvim-web-devicons": { "branch": "master", "commit": "4c3a5848ee0b09ecdea73adcd2a689190aeb728c" }, + "nvim-treesitter": { "branch": "master", "commit": "0e21ee8df6235511c02bab4a5b391d18e165a58d" }, + "nvim-web-devicons": { "branch": "master", "commit": "57dfa947cc88cdf1baa2c7e13ed31edddd8fb1d1" }, "oil.nvim": { "branch": "master", "commit": "302bbaceeafc690e6419e0c8296e804d60cb9446" }, "plenary.nvim": { "branch": "master", "commit": "857c5ac632080dba10aae49dba902ce3abf91b35" }, - "render-markdown.nvim": { "branch": "main", "commit": "c283dec1ea94947499c36bb17443e15d6acf5cda" }, + "render-markdown.nvim": { "branch": "main", "commit": "05e6a6d119f90b99829ecb7eb85428a226c0c05f" }, "rose-pine": { "branch": "main", "commit": "96ff3993a67356ee85d1cdab9be652cdc1c5d1ac" }, - "schemastore.nvim": { "branch": "main", "commit": "e659e0c6ca06727ed898aaaeea3850f528898684" }, "snacks.nvim": { "branch": "main", "commit": "bc0630e43be5699bb94dadc302c0d21615421d93" }, "todo-comments.nvim": { "branch": "main", "commit": "304a8d204ee787d2544d8bc23cd38d2f929e7cc5" }, "todo.txt-vim": { "branch": "master", "commit": "3bb5f9cf0d6c7ee91b476a97054c336104d2b6f5" }, diff --git a/home/modules/nvim/config/lsp/ts_ls.lua b/home/modules/nvim/config/lsp/ts_ls.lua new file mode 100644 index 0000000..bf60ab3 --- /dev/null +++ b/home/modules/nvim/config/lsp/ts_ls.lua @@ -0,0 +1,32 @@ +local dnscUtils = require("dnsc.utils") + +return { + init_options = { hostInfo = "neovim" }, + cmd = { "typescript-language-server", "--stdio" }, + filetypes = { + "javascript", + "javascriptreact", + "javascript.jsx", + "typescript", + "typescriptreact", + "typescript.tsx", + }, + root_dir = dnscUtils.root_pattern("tsconfig.json", "jsconfig.json", "package.json", ".git"), + init_options = { + preferences = { + importModuleSpecifierPreference = "relative", + importModuleSpecifierEnding = "minimal", + }, + }, + handlers = { + ["textDocument/definition"] = function(err, result, method, ...) + if vim.tbl_islist(result) and #result > 1 then + local filtered_result = dnscUtils.filter(result, dnscUtils.filterReactDTS) + return vim.lsp.handlers["textDocument/definition"](err, filtered_result, method, ...) + end + + vim.lsp.handlers["textDocument/definition"](err, result, method, ...) + end, + }, + single_file_support = true, +} diff --git a/home/modules/nvim/config/lua/dnsc/lsp.lua b/home/modules/nvim/config/lua/dnsc/lsp.lua new file mode 100644 index 0000000..b2817f0 --- /dev/null +++ b/home/modules/nvim/config/lua/dnsc/lsp.lua @@ -0,0 +1 @@ +vim.lsp.enable("ts_ls") diff --git a/home/modules/nvim/config/lua/dnsc/utils.lua b/home/modules/nvim/config/lua/dnsc/utils.lua index caa542d..4a97445 100644 --- a/home/modules/nvim/config/lua/dnsc/utils.lua +++ b/home/modules/nvim/config/lua/dnsc/utils.lua @@ -9,6 +9,26 @@ vim.api.nvim_create_autocmd("TextYankPost", { pattern = "*", }) +function root_pattern(...) + local patterns = M.tbl_flatten({ ... }) + return function(startpath) + startpath = M.strip_archive_subpath(startpath) + for _, pattern in ipairs(patterns) do + local match = M.search_ancestors(startpath, function(path) + for _, p in ipairs(vim.fn.glob(table.concat({ escape_wildcards(path), pattern }, "/"), true, true)) do + if vim.loop.fs_stat(p) then + return path + end + end + end) + + if match ~= nil then + return match + end + end + end +end + local function filter(arr, fn) if type(arr) ~= "table" then return arr diff --git a/home/modules/nvim/config/lua/plugins/conform.lua b/home/modules/nvim/config/lua/plugins/conform.lua index 4dbbf97..c7e01a1 100644 --- a/home/modules/nvim/config/lua/plugins/conform.lua +++ b/home/modules/nvim/config/lua/plugins/conform.lua @@ -1,9 +1,5 @@ return { "stevearc/conform.nvim", - dependencies = { - "williamboman/mason.nvim", - "WhoIsSethDaniel/mason-tool-installer.nvim", - }, opts = { formatters_by_ft = { typescriptreact = { "prettierd" }, diff --git a/home/modules/nvim/config/lua/plugins/lsp.lua.archive b/home/modules/nvim/config/lua/plugins/lsp.lua.archive new file mode 100644 index 0000000..c4d20f9 --- /dev/null +++ b/home/modules/nvim/config/lua/plugins/lsp.lua.archive @@ -0,0 +1,276 @@ +return { + "neovim/nvim-lspconfig", + version = "0.1.9", + dependencies = { + "L3MON4D3/LuaSnip", + "saadparwaiz1/cmp_luasnip", + "hrsh7th/cmp-path", + "hrsh7th/cmp-cmdline", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/nvim-cmp", + "williamboman/mason.nvim", + "williamboman/mason-lspconfig.nvim", + "b0o/schemastore.nvim", + }, + config = function() + local lspconfig = require("lspconfig") + local luasnip = require("luasnip") + local dnscUtils = require("dnsc.utils") + local lspUtils = require("lspconfig.util") + local cmp = require("cmp") + local capabilities = require("cmp_nvim_lsp").default_capabilities() + + local servers = { + "eslint", + "angularls", + "ts_ls", + "lua_ls", + "jsonls", + "html", + "cssls", + "tailwindcss", + "gopls", + "astro", + "tinymist", + "ols", + } + + require("mason").setup() + -- Automatic Server Installation with Mason + require("mason-lspconfig").setup({ + ensure_installed = servers, + automatic_installation = true, + }) + + -- LANGUAGE SERVERS + -- Lua + lspconfig.lua_ls.setup({ + capabilities = capabilities, + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + telemetry = { + enable = false, + }, + }, + }, + }) + + -- Typescript + lspconfig.tsserver.setup({ + capabilities = capabilities, + root_dir = lspUtils.root_pattern("package.json"), + init_options = { + preferences = { + importModuleSpecifierPreference = "relative", + importModuleSpecifierEnding = "minimal", + }, + }, + handlers = { + ["textDocument/definition"] = function(err, result, method, ...) + if vim.tbl_islist(result) and #result > 1 then + local filtered_result = dnscUtils.filter(result, dnscUtils.filterReactDTS) + return vim.lsp.handlers["textDocument/definition"](err, filtered_result, method, ...) + end + + vim.lsp.handlers["textDocument/definition"](err, result, method, ...) + end, + }, + }) + + -- JSON + lspconfig.jsonls.setup({ + capabilities = capabilities, + settings = { + json = { + schemas = require("schemastore").json.schemas(), + validate = { enable = true }, + }, + }, + }) + + -- HTML + lspconfig.html.setup({ + capabilities = capabilities, + }) + + -- Astro + lspconfig.astro.setup({}) + + -- Tailwind + lspconfig.tailwindcss.setup({}) + + -- Typst + lspconfig.tinymist.setup({ + single_file_support = true, + root_dir = function() + return vim.fn.getcwd() + end, + settings = {}, + }) + + -- CSS + lspconfig.cssls.setup({}) + + -- Angular + lspconfig.angularls.setup({}) + + -- ESLint + lspconfig.eslint.setup({}) + + -- Go + lspconfig.gopls.setup({ + capabilities = capabilities, + }) + + -- Run gofmt + goimports on save + local format_sync_grp = vim.api.nvim_create_augroup("goimports", {}) + vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*.go", + callback = function() + require("go.format").goimports() + end, + group = format_sync_grp, + }) + + -- Odin + lspconfig.ols.setup({}) + + -- LSP Keybindings + -- Global mappings. + -- See `:help vim.diagnostic.*` for documentation on any of the below functions + vim.keymap.set("n", "e", vim.diagnostic.open_float) + vim.keymap.set("n", "[d", vim.diagnostic.goto_prev) + vim.keymap.set("n", "]d", vim.diagnostic.goto_next) + vim.keymap.set("n", "q", vim.diagnostic.setloclist) + + -- Use LspAttach autocommand to only map the following keys + -- after the language server attaches to the current buffer + vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + callback = function(ev) + -- Enable completion triggered by + vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" + + -- Buffer local mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local opts = { buffer = ev.buf } + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) + vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) + vim.keymap.set("n", "D", vim.lsp.buf.type_definition, opts) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + vim.keymap.set("n", "f", function() + vim.lsp.buf.format({ async = true }) + end, opts) + end, + }) + + cmp.setup({ + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) -- For `luasnip` users. + end, + }, + formatting = { + format = function(entry, vim_item) + -- Kind icons + vim_item.kind = string.format("%s ", vim_item.kind) -- This concatenates the icons with the name of the item kind + -- Source + vim_item.menu = ({ + buffer = "[Buffer]", + nvim_lsp = "[LSP]", + luasnip = "[Snip]", + nvim_lua = "[Lua]", + })[entry.source.name] + return vim_item + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.abort(), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + if luasnip.expandable() then + luasnip.expand() + else + cmp.confirm({ + select = true, + }) + end + else + fallback() + end + end), + + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.locally_jumpable(1) then + luasnip.jump(1) + else + fallback() + end + end, { "i", "s" }), + + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { "i", "s" }), + }), + sources = cmp.config.sources({ + { name = "nvim_lsp", keyword_length = 1 }, + { name = "path" }, + { name = "buffer", keyword_length = 3 }, + { name = "luasnip" }, -- For luasnip users. + { name = "render-markdown" }, + }), + }) + + -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore). + cmp.setup.cmdline({ "/", "?" }, { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = "buffer" }, + }, + }) + + -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). + cmp.setup.cmdline(":", { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = "path" }, + }, { + { name = "cmdline" }, + }), + }) + + -- Set up vim-dadbod completions + cmp.setup.filetype({ "sql", "mysql", "psql" }, { + sources = { + { name = "vim-dadbod-completion" }, + { name = "buffer" }, + }, + }) + + -- Set up org mode + cmp.setup.filetype({ "org" }, { + sources = { + { name = "orgmode" }, + }, + }) + end, +} diff --git a/home/modules/nvim/config/lua/plugins/mason-tool-installer.lua b/home/modules/nvim/config/lua/plugins/mason-tool-installer.lua deleted file mode 100644 index 6782186..0000000 --- a/home/modules/nvim/config/lua/plugins/mason-tool-installer.lua +++ /dev/null @@ -1,12 +0,0 @@ -return { - "WhoIsSethDaniel/mason-tool-installer.nvim", - dependencies = { - "williamboman/mason.nvim", - }, - opts = { - ensure_installed = { - "prettierd", - "stylua" - } - } -}