dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

lsp.lua (7550B)


      1 -- LSP settings.
      2 --  This function gets run when an LSP connects to a particular buffer.
      3 local on_attach = function(_, bufnr)
      4   -- Enable inlay hints for current buffer
      5   vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
      6 
      7   -- NOTE: Remember that lua is a real programming language, and as such it is possible
      8   -- to define small helper and utility functions so you don't have to repeat yourself
      9   -- many times.
     10   --
     11   -- In this case, we create a function that lets us more easily define mappings specific
     12   -- for LSP related items. It sets the mode, buffer and description for us each time.
     13   local nmap = function(keys, func, desc)
     14     if desc then
     15       desc = 'LSP: ' .. desc
     16     end
     17 
     18     vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
     19   end
     20 
     21   nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
     22   nmap('<leader>ra', vim.lsp.buf.code_action, '[C]ode [A]ction')
     23   vim.keymap.set('v', '<leader>ra', vim.lsp.buf.code_action, { buffer = bufnr, desc = '[C]ode [A]ction' })
     24 
     25   nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
     26   nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
     27   nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
     28   nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
     29   nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
     30   nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
     31 
     32   -- See `:help K` for why this keymap
     33   nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
     34   nmap('<leader>K', vim.lsp.buf.signature_help, 'Signature Documentation')
     35 
     36   -- Lesser used LSP functionality
     37   nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
     38   nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
     39   nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
     40   nmap('<leader>wl', function()
     41     print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
     42   end, '[W]orkspace [L]ist Folders')
     43 
     44   -- Create a command `:Format` local to the LSP buffer
     45   vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
     46     vim.lsp.buf.format()
     47   end, { desc = 'Format current buffer with LSP' })
     48 
     49   vim.api.nvim_buf_create_user_command(bufnr, 'LspInspect', function(_)
     50     vim.ui.input({ prompt = 'Enter LSP Client name: ' }, function(client_name)
     51       if client_name then
     52         local client = vim.lsp.get_clients { name = client_name }
     53 
     54         if #client == 0 then
     55           vim.notify('No active LSP clients found with this name: ' .. client_name, vim.log.levels.WARN)
     56           return
     57         end
     58 
     59         -- Create a temporary buffer to show the configuration
     60         local buf = vim.api.nvim_create_buf(false, true)
     61         local win = vim.api.nvim_open_win(buf, true, {
     62           relative = 'editor',
     63           width = math.floor(vim.o.columns * 0.75),
     64           height = math.floor(vim.o.lines * 0.90),
     65           col = math.floor(vim.o.columns * 0.125),
     66           row = math.floor(vim.o.lines * 0.05),
     67           style = 'minimal',
     68           border = 'rounded',
     69           title = ' ' .. (client_name:gsub('^%l', string.upper)) .. ': LSP Configuration ',
     70           title_pos = 'center',
     71         })
     72 
     73         local lines = {}
     74         for i, this_client in ipairs(client) do
     75           if i > 1 then
     76             table.insert(lines, string.rep('-', 80))
     77           end
     78           table.insert(lines, 'Client: ' .. this_client.name)
     79           table.insert(lines, 'ID: ' .. this_client.id)
     80           table.insert(lines, '')
     81           table.insert(lines, 'Configuration:')
     82 
     83           local config_lines = vim.split(vim.inspect(this_client.config), '\n')
     84           vim.list_extend(lines, config_lines)
     85         end
     86 
     87         -- Set the lines in the buffer
     88         vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
     89 
     90         -- Set buffer options
     91         vim.bo[buf].modifiable = false
     92         vim.bo[buf].filetype = 'lua'
     93         vim.bo[buf].bh = 'delete'
     94 
     95         vim.api.nvim_buf_set_keymap(buf, 'n', 'q', ':q<CR>', { noremap = true, silent = true })
     96       end
     97     end)
     98   end, { desc = 'Inspect LSP config' })
     99 end
    100 
    101 -- :h lspconfig-all
    102 local config = function()
    103   vim.lsp.config('rust_analyzer', {
    104     on_attach = on_attach,
    105     settings = {
    106       ['rust-analyzer'] = {
    107         check = {
    108           command = "clippy",
    109           features = "all",
    110           extraArgs = {
    111             "--",
    112             "-D", "clippy::pedantic",
    113             "-D", "clippy::nursery",
    114             "-D", "clippy::restriction",
    115             "-A", "clippy::blanket_clippy_restriction_lints",
    116             "-A", "clippy::missing_docs_in_private_items",
    117             "-A", "clippy::implicit_return",
    118             "-A", "clippy::question_mark_used",
    119             "-A", "clippy::min_ident_chars",
    120             "-A", "clippy::pattern_type_mismatch",
    121             "-A", "clippy::single_call_fn",
    122             "-A", "clippy::as_conversions",
    123             "-A", "clippy::pub_with_shorthand",
    124             "-A", "clippy::shadow_reuse",
    125             "-A", "clippy::separated_literal_suffix",
    126             "-A", "clippy::float_arithmetic",
    127             "-A", "clippy::pub_use",
    128             "-A", "clippy::single_char_lifetime_names",
    129             "-A", "clippy::missing_trait_methods",
    130             "-A", "clippy::multiple_unsafe_ops_per_block", -- broken on 0.1.74
    131             "-A", "clippy::mod_module_files",
    132             "-A", "clippy::std_instead_of_alloc",
    133             "-A", "clippy::integer_division_remainder_used",
    134             "-D", "rust_2018_idioms",
    135             "-D", "missing_docs",
    136             "-D", "warnings",
    137             "-A", "clippy::too_many_lines",
    138             "-A", "clippy::arbitrary_source_item_ordering",
    139           },
    140         }
    141       }
    142     },
    143   })
    144   vim.lsp.enable('rust_analyzer')
    145 
    146   vim.lsp.config('bacon_ls', {
    147     on_attach = on_attach,
    148     init_options = {
    149       updateOnSave = true,
    150       updateOnSaveWaitMillis = 3000,
    151       updateOnChange = true,
    152     },
    153   })
    154   --[[
    155   Some issues I found:
    156     - bacon-ls kept running after vim closed
    157     - diagnostics did not update, the old ones showed, I had to :e to refresh
    158   --]]
    159   -- vim.lsp.enable('bacon_ls')
    160 
    161   vim.lsp.config('yamlls', { on_attach = on_attach })
    162   vim.lsp.enable('yamlls')
    163 
    164   vim.lsp.config('nixd', { on_attach = on_attach })
    165   vim.lsp.enable('nixd')
    166 
    167   vim.lsp.config('lua_ls', {
    168     on_attach = on_attach,
    169     settings = {
    170       Lua = {
    171         runtime = {
    172           -- Tell the language server which version of Lua you're using
    173           -- (most likely LuaJIT in the case of Neovim)
    174           version = 'LuaJIT',
    175         },
    176         diagnostics = {
    177           -- Get the language server to recognize the `vim` global
    178           globals = {
    179             'vim',
    180             'require'
    181           },
    182         },
    183         workspace = {
    184           -- Make the server aware of Neovim runtime files
    185           library = vim.api.nvim_get_runtime_file("", true),
    186         },
    187         -- Do not send telemetry data containing a randomized but unique identifier
    188         telemetry = {
    189           enable = false,
    190         },
    191       }
    192     }
    193   })
    194   vim.lsp.enable('lua_ls')
    195 
    196   vim.lsp.enable('basedpyright')
    197 
    198   vim.lsp.config('vue_ls', {
    199     -- add filetypes for typescript, javascript and vue
    200     filetypes = { 'typescript', 'javascript', 'javascriptreact', 'typescriptreact', 'vue' },
    201   })
    202   vim.lsp.enable('vue_ls')
    203 end
    204 
    205 return {
    206   'neovim/nvim-lspconfig',
    207   config = config,
    208   dependencies = {
    209     -- Useful status updates for LSP.
    210     { 'j-hui/fidget.nvim', opts = {} },
    211   }
    212 }