|
| 1 | +require('mason').setup({ |
| 2 | + ui = { |
| 3 | + icons = { |
| 4 | + package_installed = "✓", |
| 5 | + package_pending = "➜", |
| 6 | + package_uninstalled = "✗" |
| 7 | + } |
| 8 | + } |
| 9 | +}) |
| 10 | + |
| 11 | +require('mason-lspconfig').setup({ |
| 12 | + -- A list of servers to automatically install if they're not already installed |
| 13 | + ensure_installed = { 'pylsp', 'lua_ls', 'rust_analyzer', 'eslint', 'gopls' }}, |
| 14 | +}) |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +-- Set different settings for different languages' LSP |
| 20 | +-- LSP list: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md |
| 21 | +-- How to use setup({}): https://github.com/neovim/nvim-lspconfig/wiki/Understanding-setup-%7B%7D |
| 22 | +-- - the settings table is sent to the LSP |
| 23 | +-- - on_attach: a lua callback function to run after LSP atteches to a given buffer |
| 24 | +local lspconfig = require('lspconfig') |
| 25 | + |
| 26 | +-- Customized on_attach function |
| 27 | +-- See `:help vim.diagnostic.*` for documentation on any of the below functions |
| 28 | +local opts = { noremap = true, silent = true } |
| 29 | +vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts) |
| 30 | +vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts) |
| 31 | +vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts) |
| 32 | +vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts) |
| 33 | + |
| 34 | +-- Use an on_attach function to only map the following keys |
| 35 | +-- after the language server attaches to the current buffer |
| 36 | +local on_attach = function(client, bufnr) |
| 37 | + -- Enable completion triggered by <c-x><c-o> |
| 38 | + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') |
| 39 | + |
| 40 | + -- See `:help vim.lsp.*` for documentation on any of the below functions |
| 41 | + local bufopts = { noremap = true, silent = true, buffer = bufnr } |
| 42 | + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) |
| 43 | + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) |
| 44 | + vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) |
| 45 | + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) |
| 46 | + vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts) |
| 47 | + vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts) |
| 48 | + vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts) |
| 49 | + vim.keymap.set('n', '<space>wl', function() |
| 50 | + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) |
| 51 | + end, bufopts) |
| 52 | + vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts) |
| 53 | + vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts) |
| 54 | + vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts) |
| 55 | + vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) |
| 56 | + vim.keymap.set("n", "<space>f", function() |
| 57 | + vim.lsp.buf.format({ async = true }) |
| 58 | + end, bufopts) |
| 59 | +end |
| 60 | + |
| 61 | +-- Configure each language |
| 62 | +-- How to add LSP for a specific language? |
| 63 | +-- 1. use `:Mason` to install corresponding LSP |
| 64 | +-- 2. add configuration below |
| 65 | +lspconfig.pylsp.setup({ |
| 66 | + on_attach = on_attach, |
| 67 | +}) |
| 68 | + |
| 69 | +lspconfig.rust_analyzer.setup({ |
| 70 | + on_attach = on_attach, |
| 71 | +}) |
| 72 | + |
| 73 | +lspconfig.eslint.setup({ |
| 74 | + on_attach = on_attach, |
| 75 | +}) |
| 76 | + |
| 77 | +lspconfig.gopls.setup({ |
| 78 | + on_attach = on_attach, |
| 79 | +}) |
| 80 | + |
| 81 | +lspconfig.lua_ls.setup({ |
| 82 | + on_attach = on_attach, |
| 83 | +}) |
0 commit comments