debian-dots

dotfiles (does the obvious)
git clone [email protected]:dracuxan/debian-dots.git
Log | Files | Refs | README | LICENSE

lsp.lua (5767B)


      1 local vim = vim
      2 
      3 return {
      4 	"neovim/nvim-lspconfig",
      5 	dependencies = {
      6 		{ "williamboman/mason.nvim", config = true }, -- NOTE: Must be loaded before dependants
      7 		"williamboman/mason-lspconfig.nvim",
      8 		"WhoIsSethDaniel/mason-tool-installer.nvim",
      9 		{ "j-hui/fidget.nvim", opts = {} },
     10 
     11 		"hrsh7th/cmp-nvim-lsp",
     12 	},
     13 	config = function()
     14 		local lspconfig = require("lspconfig")
     15 		local util = lspconfig.util
     16 		vim.api.nvim_create_autocmd("LspAttach", {
     17 			group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
     18 			callback = function(event)
     19 				local map = function(keys, func, desc, mode)
     20 					mode = mode or "n"
     21 					vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
     22 				end
     23 
     24 				local telescope = require("telescope.builtin")
     25 				-- Jump to the definition of the word under your cursor
     26 				map("gd", telescope.lsp_definitions, "[G]oto [D]efinition")
     27 
     28 				-- Find references for the word under your cursor
     29 				map("gr", telescope.lsp_references, "[G]oto [R]eferences")
     30 
     31 				-- Jump to the implementation of the word under your cursor
     32 				map("gI", telescope.lsp_implementations, "[G]oto [I]mplementation")
     33 
     34 				-- Jump to the type of the word under your cursor
     35 				map("<leader>D", telescope.lsp_type_definitions, "Type [D]efinition")
     36 
     37 				-- Fuzzy find all the symbols in your current document
     38 				map("<leader>ds", telescope.lsp_document_symbols, "[D]ocument [S]ymbols")
     39 
     40 				-- Fuzzy find all the symbols in your current workspace
     41 				map("<leader>ws", telescope.lsp_dynamic_workspace_symbols, "[W]orkspace [S]ymbols")
     42 				-- Rename the variable under your cursor.
     43 				--  Most Language Servers support renaming across files, etc.
     44 				map("<leader>rn", vim.lsp.buf.rename, "[R]e[n]ame")
     45 
     46 				-- Execute a code action, usually your cursor needs to be on top of an error
     47 				-- or a suggestion from your LSP for this to activate.
     48 				map("<leader>ca", vim.lsp.buf.code_action, "[C]ode [A]ction", { "n", "x" })
     49 
     50 				local client = vim.lsp.get_client_by_id(event.data.client_id)
     51 				if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then
     52 					local highlight_augroup = vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
     53 					vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
     54 						buffer = event.buf,
     55 						group = highlight_augroup,
     56 						callback = vim.lsp.buf.document_highlight,
     57 					})
     58 
     59 					vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
     60 						buffer = event.buf,
     61 						group = highlight_augroup,
     62 						callback = vim.lsp.buf.clear_references,
     63 					})
     64 
     65 					vim.api.nvim_create_autocmd("LspDetach", {
     66 						group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
     67 						callback = function(event2)
     68 							vim.lsp.buf.clear_references()
     69 							vim.api.nvim_clear_autocmds({ group = "kickstart-lsp-highlight", buffer = event2.buf })
     70 						end,
     71 					})
     72 				end
     73 
     74 				if client.supports_method("textDocument/formatting") then
     75 					local format_augroup = vim.api.nvim_create_augroup("lsp-format-" .. event.buf, { clear = true })
     76 
     77 					vim.api.nvim_create_autocmd("BufWritePre", {
     78 						group = format_augroup,
     79 						buffer = event.buf,
     80 						callback = function()
     81 							if vim.bo.filetype == "oil" then
     82 								return
     83 							end
     84 							vim.lsp.buf.format({ bufnr = event.buf })
     85 						end,
     86 					})
     87 
     88 					vim.diagnostic.config({
     89 						update_in_insert = false, -- Show errors while typing
     90 						virtual_text = true, -- Show inline errors
     91 						signs = true, -- Show signs in the gutter
     92 						underline = true, -- Underline errors
     93 					})
     94 				end
     95 
     96 				vim.api.nvim_create_autocmd("BufWritePre", {
     97 					pattern = "*.go",
     98 					callback = function()
     99 						if vim.bo.filetype == "oil" then
    100 							return
    101 						end
    102 						local params = vim.lsp.util.make_range_params(0, "utf-16")
    103 						params.context = { only = { "source.organizeImports" } }
    104 						local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params)
    105 						for cid, res in pairs(result or {}) do
    106 							for _, r in pairs(res.result or {}) do
    107 								if r.edit then
    108 									local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16"
    109 									vim.lsp.util.apply_workspace_edit(r.edit, enc)
    110 								end
    111 							end
    112 						end
    113 						vim.lsp.buf.format({ async = false })
    114 					end,
    115 				})
    116 			end,
    117 		})
    118 
    119 		local servers = {
    120 			lua_ls = {
    121 				settings = {
    122 					Lua = {
    123 						completion = {
    124 							callSnippet = "Replace",
    125 						},
    126 						runtime = { version = "LuaJIT" },
    127 						diagnostics = { disable = { "missing-fields" }, globals = { "vim" } },
    128 						format = {
    129 							enable = false,
    130 						},
    131 					},
    132 				},
    133 			},
    134 			html = {},
    135 			cssls = {},
    136 			gopls = {},
    137 			rust_analyzer = {},
    138 			nil_ls = {},
    139 			bashls = {},
    140 			pyright = {},
    141 			zls = {},
    142 			ts_ls = {},
    143 			eslint = {},
    144 			elixirls = {
    145 				cmd = { "elixir-ls" },
    146 				settings = {
    147 					elixirLS = {
    148 						dialyzerEnabled = false,
    149 						fetchDeps = false,
    150 					},
    151 				},
    152 			},
    153 		}
    154 
    155 		require("mason").setup()
    156 
    157 		local ensure_installed = vim.tbl_keys(servers or {})
    158 		vim.list_extend(ensure_installed, {
    159 			"stylua",
    160 			"shfmt",
    161 			"checkmake",
    162 			"prettierd",
    163 			"nixfmt",
    164 			"black",
    165 		})
    166 		require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
    167 
    168 		local capabilities = vim.lsp.protocol.make_client_capabilities()
    169 		capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())
    170 
    171 		require("mason-lspconfig").setup({
    172 			handlers = {
    173 				function(server_name)
    174 					local server = servers[server_name] or {}
    175 					server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
    176 					require("lspconfig")[server_name].setup(server)
    177 				end,
    178 			},
    179 		})
    180 	end,
    181 }