my configs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

103 lines
3.6 KiB

  1. local map = require('utils').map
  2. local nvim_lsp_config = require('lspconfig')
  3. local nvim_lsp_install = require('lspinstall')
  4. local cmp = require('cmp')
  5. require("nvim-ale-diagnostic")
  6. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
  7. vim.lsp.diagnostic.on_publish_diagnostics, {
  8. underline = false,
  9. virtual_text = false,
  10. signs = true,
  11. update_in_insert = false,
  12. }
  13. )
  14. -- keymaps
  15. local on_attach = function(client, bufnr)
  16. local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
  17. local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
  18. -- Enable completion triggered by <c-x><c-o>
  19. buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
  20. -- Mappings.
  21. map{'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>'}
  22. map{'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>'}
  23. map{'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>'}
  24. map{'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>'}
  25. map{'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>'}
  26. map{'n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>'}
  27. map{'n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>'}
  28. map{'n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>'}
  29. map{'n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>'}
  30. map{'n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>'}
  31. map{'n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>'}
  32. map{'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>'}
  33. map{'n', '<space>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>'}
  34. map{'n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>'}
  35. map{'n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>'}
  36. map{'n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>'}
  37. -- Set some keybinds conditional on server capabilities
  38. if client.resolved_capabilities.document_formatting then
  39. map{"n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>"}
  40. elseif client.resolved_capabilities.document_range_formatting then
  41. map{"n", "<space>f", "<cmd>lua vim.lsp.buf.range_formatting()<CR>"}
  42. end
  43. -- Set autocommands conditional on server_capabilities
  44. if client.resolved_capabilities.document_highlight then
  45. vim.api.nvim_exec([[
  46. augroup lsp_document_highlight
  47. autocmd! * <buffer>
  48. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  49. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  50. augroup END
  51. ]], false)
  52. end
  53. end
  54. -- config that activates keymaps and enables snippet support
  55. local function make_config()
  56. local capabilities = vim.lsp.protocol.make_client_capabilities()
  57. capabilities.textDocument.completion.completionItem.snippetSupport = true
  58. return {
  59. -- enable snippet support
  60. capabilities = capabilities,
  61. -- map buffer local keybindings when the language server attaches
  62. on_attach = on_attach,
  63. }
  64. end
  65. -- lsp-install
  66. local function setup_servers()
  67. require'lspinstall'.setup()
  68. -- get all installed servers
  69. local servers = nvim_lsp_install.installed_servers()
  70. for _, server in pairs(servers) do
  71. local config = make_config()
  72. nvim_lsp_config[server].setup(config)
  73. end
  74. end
  75. setup_servers()
  76. -- Automatically reload after `:LspInstall <server>` so we don't have to restart neovim
  77. nvim_lsp_install.post_install_hook = function ()
  78. setup_servers() -- reload installed servers
  79. vim.cmd("bufdo e") -- this triggers the FileType autocmd that starts the server
  80. end
  81. cmp.setup({
  82. snippet = {
  83. expand = function(args)
  84. vim.fn["vsnip#anonymous"](args.body)
  85. end,
  86. },
  87. mapping = {
  88. ['<C-y>'] = cmp.mapping.confirm({ select = true }),
  89. },
  90. })