Guest User

Untitled

a guest
Feb 8th, 2024
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. -- [[ Configure LSP ]]
  2. --  This function gets run when an LSP connects to a particular buffer.
  3. local on_attach = function(_, bufnr)
  4.   -- NOTE: Remember that lua is a real programming language, and as such it is possible
  5.   -- to define small helper and utility functions so you don't have to repeat yourself
  6.   -- many times.
  7.   --
  8.   -- In this case, we create a function that lets us more easily define mappings specific
  9.   -- for LSP related items. It sets the mode, buffer and description for us each time.
  10.   local nmap = function(keys, func, desc)
  11.     if desc then
  12.       desc = 'LSP: ' .. desc
  13.     end
  14.  
  15.     vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  16.   end
  17.  
  18.   nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  19.   nmap('<leader>ca', function()
  20.     vim.lsp.buf.code_action { context = { only = { 'quickfix', 'refactor', 'source' } } }
  21.   end, '[C]ode [A]ction')
  22.  
  23.   nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
  24.   nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  25.   nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
  26.   nmap('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
  27.   nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  28.   nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  29.  
  30.   -- See `:help K` for why this keymap
  31.   nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  32.   nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  33.  
  34.   -- Lesser used LSP functionality
  35.   nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  36.   nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  37.   nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  38.   nmap('<leader>wl', function()
  39.     print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  40.   end, '[W]orkspace [L]ist Folders')
  41.  
  42.   -- Create a command `:Format` local to the LSP buffer
  43.   vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  44.     vim.lsp.buf.format()
  45.   end, { desc = 'Format current buffer with LSP' })
  46. end
  47.  
  48. -- document existing key chains
  49. require('which-key').register {
  50.   ['<leader>c'] = { name = '[C]ode', _ = 'which_key_ignore' },
  51.   ['<leader>d'] = { name = '[D]ocument', _ = 'which_key_ignore' },
  52.   ['<leader>g'] = { name = '[G]it', _ = 'which_key_ignore' },
  53.   ['<leader>h'] = { name = 'Git [H]unk', _ = 'which_key_ignore' },
  54.   ['<leader>r'] = { name = '[R]ename', _ = 'which_key_ignore' },
  55.   ['<leader>s'] = { name = '[S]earch', _ = 'which_key_ignore' },
  56.   ['<leader>t'] = { name = '[T]oggle', _ = 'which_key_ignore' },
  57.   ['<leader>w'] = { name = '[W]orkspace', _ = 'which_key_ignore' },
  58. }
  59. -- register which-key VISUAL mode
  60. -- required for visual <leader>hs (hunk stage) to work
  61. require('which-key').register({
  62.   ['<leader>'] = { name = 'VISUAL <leader>' },
  63.   ['<leader>h'] = { 'Git [H]unk' },
  64. }, { mode = 'v' })
  65.  
  66. -- mason-lspconfig requires that these setup functions are called in this order
  67. -- before setting up the servers.
  68. require('mason').setup()
  69. require('mason-lspconfig').setup()
  70.  
  71. -- Enable the following language servers
  72. --  Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  73. --
  74. --  Add any additional override configuration in the following tables. They will be passed to
  75. --  the `settings` field of the server config. You must look up that documentation yourself.
  76. --
  77. --  If you want to override the default filetypes that your language server will attach to you can
  78. --  define the property 'filetypes' to the map in question.
  79. local servers = {
  80.   clangd = {},
  81.   -- gopls = {},
  82.   pyright = {},
  83.   -- rust_analyzer = {},
  84.   -- tsserver = {},
  85.   -- html = { filetypes = { 'html', 'twig', 'hbs'} },
  86.  
  87.   lua_ls = {
  88.     Lua = {
  89.       workspace = { checkThirdParty = false },
  90.       telemetry = { enable = false },
  91.       -- NOTE: toggle below to ignore Lua_LS's noisy `missing-fields` warnings
  92.       -- diagnostics = { disable = { 'missing-fields' } },
  93.     },
  94.   },
  95. }
  96.  
  97. -- Setup neovim lua configuration
  98. require('neodev').setup()
  99.  
  100. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  101. local capabilities = vim.lsp.protocol.make_client_capabilities()
  102. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  103.  
  104. -- Ensure the servers above are installed
  105. local mason_lspconfig = require 'mason-lspconfig'
  106.  
  107. mason_lspconfig.setup {
  108.   ensure_installed = vim.tbl_keys(servers),
  109. }
  110.  
  111. mason_lspconfig.setup_handlers {
  112.   function(server_name)
  113.     require('lspconfig')[server_name].setup {
  114.       capabilities = capabilities,
  115.       on_attach = on_attach,
  116.       settings = servers[server_name],
  117.       filetypes = (servers[server_name] or {}).filetypes,
  118.     }
  119.   end,
  120. }
  121.  
  122. require('lspconfig').pyright.setup{
  123. -- on_attach = on_attach,
  124. -- flags = lsp_flags,
  125. settings = {
  126.   python = {
  127.     analysis = {
  128.       diagnosticMode = "off",
  129.       typeCheckingMode = "off"
  130.     }
  131.   }
  132. }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment