# chezmoi:template:left-delimiter=--{{ --{{- /* vim: set filetype=lua: */ -}} local function on_attach(_, buffer) local function format_buffer(_) vim.lsp.buf.format() end -- Create a command `:Format` local to the LSP buffer vim.api.nvim_buf_create_user_command(buffer, 'Format', format_buffer, { desc = 'Format current buffer with LSP', }) end local function config() local cmp_nvim_lsp = require 'cmp_nvim_lsp' local lspconfig = require 'lspconfig' local mason_lspconfig = require 'mason-lspconfig' local builtin = require 'telescope.builtin' vim.api.nvim_create_autocmd('LspAttach', { group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }), callback = function (event) local function map(keys, func, desc) vim.keymap.set('n', keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc}) end map('rn', vim.lsp.buf.rename, '[R]e[n]ame') map('ca', vim.lsp.buf.code_action, '[C]ode [A]ction') map('gd', builtin.lsp_definitions, '[G]oto [D]efinition') map('gr', builtin.lsp_references, '[G]oto [R]eferences') map('gI', builtin.lsp_implementations, '[G]oto [I]mplementation') map('D', builtin.lsp_type_definitions, 'Type [D]efinition') map('ds', builtin.lsp_document_symbols, '[D]ocument [S]ymbols') map('ws', builtin.lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols') -- See `:help K` for why this keymap map('K', vim.lsp.buf.hover, 'Hover Documentation') map('', vim.lsp.buf.signature_help, 'Signature Documentation') local function inspect_workspace_folders() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end -- Lesser used LSP functionality map('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') map('wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder') map('wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder') map('wl', inspect_workspace_folders, '[W]orkspace [L]ist Folders') -- The following two autocommands are used to highlight references of the -- word under your cursor when your cursor rests there for a little while. -- See `:help CursorHold` for information about when this is executed -- -- When you move your cursor, the highlights will be cleared (the second autocommand). local client = vim.lsp.get_client_by_id(event.data.client_id) if client and client.server_capabilities.documentHighlightProvider then vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { buffer = event.buf, callback = vim.lsp.buf.document_highlight, }) vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { buffer = event.buf, callback = vim.lsp.buf.clear_references, }) end end }) local servers = { bashls = {}, bufls = {}, taplo = {}, dockerls = {}, jsonls = {}, jqls = {}, marksman = {}, sqlls = {}, vimls = {}, yamlls = {}, cmake = {}, cssls = {}, --{{ if lookPath "node" -}} eslint = {}, ts_ls = {}, --{{- end }} --{{ if lookPath "go" -}} gopls = {}, templ = {}, -- golangci_lint_ls = {}, --{{- end }} lua_ls = { Lua = { workspace = { checkThirdParty = false }, telemetry = { enable = false }, }, }, } local capabilities = vim.lsp.protocol.make_client_capabilities() capabilities = cmp_nvim_lsp.default_capabilities(capabilities) mason_lspconfig.setup { ensure_installed = vim.tbl_keys(servers), } mason_lspconfig.setup_handlers { function(server_name) lspconfig[server_name].setup { capabilities = capabilities, on_attach = on_attach, settings = servers[server_name], filetypes = (servers[server_name] or {}).filetypes, } end, } end return { 'neovim/nvim-lspconfig', name = 'lspconfig', tag = 'v1.0.0', config = config, dependencies = { { 'williamboman/mason.nvim', tag = 'v1.10.0', opts = {} }, { 'williamboman/mason-lspconfig.nvim', tag = 'v1.31.0' }, { 'j-hui/fidget.nvim', tag = 'legacy', opts = {} }, { 'folke/trouble.nvim', tag = 'v3.6.0' }, -- { 'folke/lazydev.nvim', -- tag = 'v1.8.0', -- ft = "lua", -- opts = { -- } -- }, }, }