init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. local M = {
  2. "neovim/nvim-lspconfig",
  3. dependencies = {
  4. "saghen/blink.cmp",
  5. "williamboman/mason.nvim",
  6. "williamboman/mason-lspconfig.nvim",
  7. "WhoIsSethDaniel/mason-tool-installer.nvim",
  8. { "j-hui/fidget.nvim", opts = {} },
  9. require("plugins.lsp.extras.lazydev"),
  10. require("plugins.lsp.extras.gopher"),
  11. },
  12. }
  13. -- Set up autocommands for document highlights
  14. local function setup_autocommands(client, bufnr)
  15. if client.server_capabilities.documentHighlightProvider then
  16. local group = vim.api.nvim_create_augroup("LSPDocumentHighlight", { clear = true })
  17. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  18. group = group,
  19. buffer = bufnr,
  20. callback = vim.lsp.buf.document_highlight,
  21. })
  22. vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
  23. group = group,
  24. buffer = bufnr,
  25. callback = vim.lsp.buf.clear_references,
  26. })
  27. end
  28. end
  29. -- Set up key mappings for LSP functionality
  30. local function setup_keymaps(bufnr)
  31. local BORDER = "rounded"
  32. local keymaps = {
  33. {
  34. "<C-h>",
  35. function()
  36. vim.lsp.buf.signature_help({ border = BORDER })
  37. end,
  38. "Signature Help",
  39. },
  40. {
  41. "K",
  42. function()
  43. vim.lsp.buf.hover({ border = BORDER })
  44. end,
  45. "Hover Doc",
  46. },
  47. { "<leader>cw", vim.lsp.buf.rename, "Rename" },
  48. { "<leader>r", vim.lsp.buf.rename, "Rename" },
  49. { "vd", vim.diagnostic.open_float, "Open Diagnostics" },
  50. { "<leader>lr", ":LspRestart<CR>", "Restart LSP" },
  51. { "<leader>li", ":LspInfo<CR>", "LSP Info" },
  52. }
  53. for _, map in ipairs(keymaps) do
  54. nmap(map[1], map[2], {
  55. buffer = bufnr,
  56. desc = map[3],
  57. })
  58. end
  59. end
  60. -- Function called when LSP attaches to a buffer
  61. function M.on_attach(client, bufnr)
  62. setup_autocommands(client, bufnr)
  63. setup_keymaps(bufnr)
  64. end
  65. -- Function to get common LSP configuration
  66. function M.get_common_config()
  67. local capabilities = require("blink.cmp").get_lsp_capabilities()
  68. -- Enable folding capabilities
  69. capabilities.textDocument.foldingRange = {
  70. dynamicRegistration = false,
  71. lineFoldingOnly = true,
  72. }
  73. return {
  74. on_attach = M.on_attach,
  75. capabilities = capabilities,
  76. flags = { debounce_text_changes = 150 },
  77. }
  78. end
  79. function M.config()
  80. -- Set up Mason
  81. require("mason").setup({
  82. max_concurrent_installers = 4,
  83. })
  84. -- Ensure all tools are installed
  85. local ensure_installed = {
  86. -- LSP servers
  87. "gopls",
  88. "jsonls",
  89. "lua_ls",
  90. "yamlls",
  91. "graphql-language-service-cli",
  92. "html-lsp",
  93. "htmx-lsp",
  94. "json-lsp",
  95. "lua-language-server",
  96. "omnisharp",
  97. "yaml-language-server",
  98. "svelte-language-server",
  99. "vtsls",
  100. -- Formatters
  101. "prettierd",
  102. "shfmt",
  103. "stylua",
  104. "latexindent",
  105. "clang-format",
  106. "csharpier",
  107. "quick-lint-js",
  108. -- Additional tools
  109. "templ",
  110. }
  111. require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
  112. -- Set up Mason LSP config
  113. require("mason-lspconfig").setup({
  114. ensure_installed = {}, -- explicitly set to an empty table (populated via mason-tool-installer)
  115. automatic_installation = false,
  116. handlers = {
  117. function(server_name)
  118. local base_opts = M.get_common_config()
  119. -- Load server-specific configuration if it exists
  120. local ok, server_opts = pcall(require, "config.lsp." .. server_name)
  121. if ok then
  122. base_opts = vim.tbl_deep_extend("force", base_opts, server_opts)
  123. end
  124. -- Set up the LSP server with the combined options
  125. require("lspconfig")[server_name].setup(base_opts)
  126. end,
  127. },
  128. })
  129. -- Set up non-Mason LSP servers
  130. local non_mason_servers = { "ccls" }
  131. for _, server in ipairs(non_mason_servers) do
  132. require("lspconfig")[server].setup(M.get_common_config())
  133. end
  134. end
  135. return M