init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.context"),
  8. require("plugins.lsp.extras.lazydev"),
  9. require("plugins.lsp.extras.gopher"),
  10. require("plugins.lsp.extras.typescript"),
  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. "graphql-language-service-cli",
  89. "html-lsp",
  90. "htmx-lsp",
  91. "json-lsp",
  92. "lua-language-server",
  93. "omnisharp",
  94. "yaml-language-server",
  95. "svelte-language-server",
  96. -- Formatters
  97. "prettierd",
  98. "shfmt",
  99. "stylua",
  100. "latexindent",
  101. "clang-format",
  102. "csharpier",
  103. -- Additional tools
  104. "eslint_d",
  105. "templ",
  106. }
  107. -- Install missing tools
  108. local registry = require("mason-registry")
  109. for _, tool in ipairs(ensure_installed) do
  110. if not registry.is_installed(tool) then
  111. vim.cmd("MasonInstall " .. tool)
  112. end
  113. end
  114. -- Set up Mason LSP config
  115. require("mason-lspconfig").setup({
  116. automatic_installation = true,
  117. ensure_installed = {
  118. "gopls",
  119. "html",
  120. "htmx",
  121. "jsonls",
  122. "lua_ls",
  123. "omnisharp",
  124. "yamlls",
  125. "graphql",
  126. },
  127. handlers = {
  128. function(server_name)
  129. local base_opts = M.get_common_config()
  130. -- Load server-specific configuration if it exists
  131. local ok, server_opts = pcall(require, "config.lsp." .. server_name)
  132. if ok then
  133. base_opts = vim.tbl_deep_extend("force", base_opts, server_opts)
  134. end
  135. -- Set up the LSP server with the combined options
  136. require("lspconfig")[server_name].setup(base_opts)
  137. end,
  138. },
  139. })
  140. -- Set up non-Mason LSP servers
  141. local non_mason_servers = { "ccls" }
  142. for _, server in ipairs(non_mason_servers) do
  143. require("lspconfig")[server].setup(M.get_common_config())
  144. end
  145. end
  146. return M