lspconfig.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- import lspconfig plugin safely
  2. local lspconfig_status, lspconfig = pcall(require, "lspconfig")
  3. if not lspconfig_status then
  4. return
  5. end
  6. -- import cmp-nvim-lsp plugin safely
  7. local cmp_nvim_lsp_status, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
  8. if not cmp_nvim_lsp_status then
  9. return
  10. end
  11. -- import typescript plugin safely
  12. local typescript_setup, typescript = pcall(require, "typescript")
  13. if not typescript_setup then
  14. return
  15. end
  16. -- import rust-tools plugin safely
  17. local rust_setup, rt = pcall(require, "rust-tools")
  18. if not rust_setup then
  19. return
  20. end
  21. local keymap = vim.keymap -- for conciseness
  22. -- enable keybinds only for when lsp server available
  23. local on_attach = function(client, bufnr)
  24. -- keybind options
  25. local opts = { noremap = true, silent = true, buffer = bufnr }
  26. -- set keybinds
  27. keymap.set("n", "gr", "<cmd>Lspsaga lsp_finder<CR>", opts) -- show definition, references
  28. keymap.set("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts) -- got to declaration
  29. keymap.set("n", "gd", "<cmd>Lspsaga peek_definition<CR>", opts) -- see definition and make edits in window
  30. keymap.set("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts) -- go to implementation
  31. keymap.set("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", opts) -- see available code actions
  32. keymap.set("n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opts) -- smart rename
  33. keymap.set("n", "<leader>D", "<cmd>Lspsaga show_line_diagnostics<CR>", opts) -- show diagnostics for line
  34. keymap.set("n", "<leader>d", "<cmd>Lspsaga show_cursor_diagnostics<CR>", opts) -- show diagnostics for cursor
  35. keymap.set("n", "[d", "<cmd>Lspsaga diagnostic_jump_prev<CR>", opts) -- jump to previous diagnostic in buffer
  36. keymap.set("n", "]d", "<cmd>Lspsaga diagnostic_jump_next<CR>", opts) -- jump to next diagnostic in buffer
  37. keymap.set("n", "K", "<cmd>Lspsaga hover_doc<CR>", opts) -- show documentation for what is under cursor
  38. keymap.set("n", "<leader>o", "<cmd>LSoutlineToggle<CR>", opts) -- see outline on right hand side
  39. keymap.set("n", "<leader>tt", "<cmd>Lspsaga term_toggle<CR>", opts) -- see outline on right hand side
  40. -- typescript specific keymaps (e.g. rename file and update imports)
  41. if client.name == "tsserver" then
  42. keymap.set("n", "<leader>rf", ":TypescriptRenameFile<CR>") -- rename file and update imports
  43. keymap.set("n", "<leader>oi", ":TypescriptOrganizeImports<CR>") -- organize imports (not in youtube nvim video)
  44. keymap.set("n", "<leader>ru", ":TypescriptRemoveUnused<CR>") -- remove unused variables (not in youtube nvim video)
  45. end
  46. end
  47. -- used to enable autocompletion (assign to every lsp server config)
  48. local capabilities = cmp_nvim_lsp.default_capabilities()
  49. -- Change the Diagnostic symbols in the sign column (gutter)
  50. -- (not in youtube nvim video)
  51. local signs = { Error = " ", Warn = " ", Hint = "ﴞ ", Info = " " }
  52. for type, icon in pairs(signs) do
  53. local hl = "DiagnosticSign" .. type
  54. vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
  55. end
  56. -- configure typescript server with plugin
  57. typescript.setup({
  58. server = {
  59. capabilities = capabilities,
  60. on_attach = on_attach,
  61. },
  62. })
  63. -- configure rust server with plugin
  64. rt.setup({
  65. server = {
  66. capabilities = capabilities,
  67. on_attach = on_attach,
  68. },
  69. })
  70. -- configure cpp clangd
  71. lspconfig["clangd"].setup({
  72. capabilities = capabilities,
  73. on_attach = on_attach,
  74. })
  75. -- configure html server
  76. lspconfig["html"].setup({
  77. capabilities = capabilities,
  78. on_attach = on_attach,
  79. filetypes = { "html", "svelte", "vue" },
  80. })
  81. --configure cssls server
  82. lspconfig["cssls"].setup({
  83. capabilities = capabilities,
  84. on_attach = on_attach,
  85. filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte", "vue" },
  86. })
  87. -- configure emmet language server
  88. lspconfig["emmet_ls"].setup({
  89. capabilities = capabilities,
  90. on_attach = on_attach,
  91. filetypes = { "html", "svelte", "vue" },
  92. })
  93. -- configure pyright server
  94. lspconfig["pyright"].setup({
  95. capabilities = capabilities,
  96. on_attach = on_attach,
  97. settings = {
  98. pyright = {
  99. autoImportCompletion = true,
  100. python = {
  101. analysis = {
  102. autoSearchPaths = true,
  103. useLibraryCodeForTypes = true,
  104. },
  105. },
  106. },
  107. },
  108. })
  109. -- configure lua server (with special settings)
  110. lspconfig["lua_ls"].setup({
  111. capabilities = capabilities,
  112. on_attach = on_attach,
  113. settings = {
  114. -- custom settings for lua
  115. Lua = {
  116. -- make the language server recognize "vim" global
  117. diagnostics = {
  118. globals = { "vim" },
  119. },
  120. workspace = {
  121. -- make language server aware of runtime files
  122. library = {
  123. [vim.fn.expand("$VIMRUNTIME/lua")] = true,
  124. [vim.fn.stdpath("config") .. "/lua"] = true,
  125. },
  126. },
  127. },
  128. },
  129. })