init.lua 3.9 KB

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