From 2f81b9e3f7da320b2c54eb5e4e85584f6c2e83de Mon Sep 17 00:00:00 2001 From: David Ibia Date: Tue, 16 Jan 2024 01:34:57 +0100 Subject: [PATCH] chore(lsp-config.lua): refactor code formatting and improve readability feat(lsp-config.lua): add support for tailwindcss lsp and attach tailwind_colors only when using tailwindcss lsp feat(lsp-config.lua): add key mappings for various LSP functionalities like go to declaration, go to definitions, show documentation, show LSP implementation, get help, show LSP type definitions, smart rename, see available code actions, show LSP references, format file, and restart LSP feat(lsp-config.lua): configure language servers for html, typescript, css, tailwindcss, svelte, emmet, python, jedi, docker, json, vue, rust, lua, css, htmx, docker file, docker compose, and eslint --- lua/absolute/after/lsp-config.lua | 221 +++++++++++++++--------------- 1 file changed, 112 insertions(+), 109 deletions(-) diff --git a/lua/absolute/after/lsp-config.lua b/lua/absolute/after/lsp-config.lua index 52086c4..6843749 100644 --- a/lua/absolute/after/lsp-config.lua +++ b/lua/absolute/after/lsp-config.lua @@ -5,53 +5,56 @@ local tailwind_colors = require("tailwindcss-colors") local opts = { noremap = true, silent = true } local on_attach = function(client, bufnr) - opts.buffer = bufnr + opts.buffer = bufnr - tailwind_colors.buf_attach(bufnr) + -- Only attach when we are using the tailwindcss lsp + if client.name == "tailwindcss" then + tailwind_colors.buf_attach(bufnr) + end - -- Enable completion triggered by - -- vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' + -- Enable completion triggered by + -- vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' - opts.desc = "Go to declaration" - vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) + opts.desc = "Go to declaration" + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) - opts.desc = "Go to definitions" - vim.keymap.set("n", "gd", "Telescope lsp_definitions", opts) + opts.desc = "Go to definitions" + vim.keymap.set("n", "gd", "Telescope lsp_definitions", opts) - opts.desc = "Show documentation for what is under cursor" - vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + opts.desc = "Show documentation for what is under cursor" + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) - opts.desc = "Show LSP Implementation" - vim.keymap.set("n", "gi", "Telescope lsp_implementations", opts) + opts.desc = "Show LSP Implementation" + vim.keymap.set("n", "gi", "Telescope lsp_implementations", opts) - opts.desc = "Get Help" - vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) + opts.desc = "Get Help" + vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) - -- vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) - -- vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) - -- vim.keymap.set('n', 'wl', function() - -- print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - -- end, opts) + -- vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) + -- vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) + -- vim.keymap.set('n', 'wl', function() + -- print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + -- end, opts) - opts.desc = "Show LSP type definitions" - vim.keymap.set("n", "gt", "Telescope lsp_type_definitions", opts) + opts.desc = "Show LSP type definitions" + vim.keymap.set("n", "gt", "Telescope lsp_type_definitions", opts) - opts.desc = "Smart rename" - vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) + opts.desc = "Smart rename" + vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) - opts.desc = "See available code actions" - vim.keymap.set({ "n", "v" }, "vca", vim.lsp.buf.code_action, opts) + opts.desc = "See available code actions" + vim.keymap.set({ "n", "v" }, "vca", vim.lsp.buf.code_action, opts) - opts.desc = "Show LSP references" - vim.keymap.set("n", "vrr", "Telescope lsp_references", opts) + opts.desc = "Show LSP references" + vim.keymap.set("n", "vrr", "Telescope lsp_references", opts) - opts.desc = "Format File" - vim.keymap.set("n", "f", function() - vim.lsp.buf.format({ async = true }) - end, opts) + opts.desc = "Format File" + vim.keymap.set("n", "f", function() + vim.lsp.buf.format({ async = true }) + end, opts) - opts.desc = "Restart LSP" - vim.keymap.set("n", "rs", "LspRestart", opts) + opts.desc = "Restart LSP" + vim.keymap.set("n", "rs", "LspRestart", opts) end local capabilities = cmp_nvim_lsp.default_capabilities() @@ -59,153 +62,153 @@ local capabilities = cmp_nvim_lsp.default_capabilities() -- Change the Diagnostic symbols in the sign column (gutter) local signs = { Error = " ", Warn = " ", Hint = "󰠠 ", Info = " " } for type, icon in pairs(signs) do - local hl = "DiagnosticSign" .. type - vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" }) + local hl = "DiagnosticSign" .. type + vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" }) end -- configure html server lspconfig["html"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure typescript server with plugin lspconfig["tsserver"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure css server lspconfig["cssls"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure tailwindcss server lspconfig["tailwindcss"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure svelte server lspconfig["svelte"].setup({ - capabilities = capabilities, - on_attach = function(client, bufnr) - on_attach(client, bufnr) + capabilities = capabilities, + on_attach = function(client, bufnr) + on_attach(client, bufnr) - vim.api.nvim_create_autocmd("BufWritePost", { - pattern = { "*.js", "*.ts" }, - callback = function(ctx) - if client.name == "svelte" then - client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.file }) - end - end, - }) - end, + vim.api.nvim_create_autocmd("BufWritePost", { + pattern = { "*.js", "*.ts" }, + callback = function(ctx) + if client.name == "svelte" then + client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.file }) + end + end, + }) + end, }) -- configure emmet language server lspconfig["emmet_ls"].setup({ - capabilities = capabilities, - on_attach = on_attach, - filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" }, + capabilities = capabilities, + on_attach = on_attach, + filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" }, }) -- configure python lsp lspconfig["pylsp"].setup({ - capabilities = capabilities, - on_attach = on_attach, - pylsp = { - plugins = { - jedi_completion = { - include_params = true, - }, - }, - }, + capabilities = capabilities, + on_attach = on_attach, + pylsp = { + plugins = { + jedi_completion = { + include_params = true, + }, + }, + }, }) -- configure jedi language server lspconfig["jedi_language_server"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure docker server lspconfig["dockerls"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure json server lspconfig["jsonls"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure vue server lspconfig["volar"].setup({ - capabilities = capabilities, - on_attach = on_attach, - filetypes = { "vue" }, + capabilities = capabilities, + on_attach = on_attach, + filetypes = { "vue" }, }) -- configure rust server lspconfig.rust_analyzer.setup({ - -- Server-specific settings. See `:help lspconfig-setup` - on_attach = on_attach, - capabilities = capabilities, - settings = { - ["rust-analyzer"] = {}, - }, + -- Server-specific settings. See `:help lspconfig-setup` + on_attach = on_attach, + capabilities = capabilities, + settings = { + ["rust-analyzer"] = {}, + }, }) -- configure lua server (with special settings) lspconfig["lua_ls"].setup({ - capabilities = capabilities, - on_attach = on_attach, - settings = { -- custom settings for lua - Lua = { - -- make the language server recognize "vim" global - diagnostics = { - globals = { "vim" }, - }, - workspace = { - -- make language server aware of runtime files - library = { - [vim.fn.expand("$VIMRUNTIME/lua")] = true, - [vim.fn.stdpath("config") .. "/lua"] = true, - }, - }, - }, - }, + capabilities = capabilities, + on_attach = on_attach, + settings = { -- custom settings for lua + Lua = { + -- make the language server recognize "vim" global + diagnostics = { + globals = { "vim" }, + }, + workspace = { + -- make language server aware of runtime files + library = { + [vim.fn.expand("$VIMRUNTIME/lua")] = true, + [vim.fn.stdpath("config") .. "/lua"] = true, + }, + }, + }, + }, }) -- configure css server lspconfig["cssls"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure htmx server lspconfig["htmx"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) --configure docker file server lspconfig["dockerls"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure docker compose server lspconfig["docker_compose_language_service"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, }) -- configure eslint server lspconfig["eslint"].setup({ - capabilities = capabilities, - on_attach = on_attach, + capabilities = capabilities, + on_attach = on_attach, })