init.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 keymaps = {
  31. { "<C-h>", vim.lsp.buf.signature_help, "Signature Help" },
  32. { "<leader>cw", vim.lsp.buf.rename, "Rename" },
  33. { "<leader>r", vim.lsp.buf.rename, "Rename" },
  34. { "vd", vim.diagnostic.open_float, "Open Diagnostics" },
  35. { "<leader>lr", ":LspRestart<CR>", "Restart LSP" },
  36. { "<leader>li", ":LspInfo<CR>", "LSP Info" },
  37. }
  38. for _, map in ipairs(keymaps) do
  39. nmap(map[1], map[2], {
  40. buffer = bufnr,
  41. desc = map[3],
  42. })
  43. end
  44. end
  45. -- Function called when LSP attaches to a buffer
  46. local function on_attach(client, bufnr)
  47. setup_autocommands(client, bufnr)
  48. setup_keymaps(bufnr)
  49. end
  50. local BORDER = "rounded"
  51. -- Customize hover and signature help handlers
  52. local handlers = {
  53. ["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = BORDER }),
  54. ["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = BORDER }),
  55. }
  56. function M.config()
  57. -- Set up Mason
  58. require("mason").setup({
  59. max_concurrent_installers = 4,
  60. })
  61. -- Ensure all tools are installed
  62. local ensure_installed = {
  63. -- LSP servers
  64. "gopls",
  65. "graphql-language-service-cli",
  66. "html-lsp",
  67. "htmx-lsp",
  68. "json-lsp",
  69. "lua-language-server",
  70. "omnisharp",
  71. "vtsls",
  72. "yaml-language-server",
  73. -- Formatters
  74. "prettierd",
  75. "shfmt",
  76. "stylua",
  77. "latexindent",
  78. "clang-format",
  79. -- Additional tools
  80. "eslint_d",
  81. "templ",
  82. }
  83. -- Install missing tools
  84. local registry = require("mason-registry")
  85. for _, tool in ipairs(ensure_installed) do
  86. if not registry.is_installed(tool) then
  87. vim.cmd("MasonInstall " .. tool)
  88. end
  89. end
  90. -- Set up Mason LSP config
  91. require("mason-lspconfig").setup({
  92. automatic_installation = true,
  93. ensure_installed = {
  94. "gopls",
  95. "html",
  96. "htmx",
  97. "jsonls",
  98. "lua_ls",
  99. "omnisharp",
  100. "yamlls",
  101. "graphql",
  102. },
  103. handlers = {
  104. function(server_name)
  105. local capabilities = require("blink.cmp").get_lsp_capabilities()
  106. -- Enable folding capabilities
  107. capabilities.textDocument.foldingRange = {
  108. dynamicRegistration = false,
  109. lineFoldingOnly = true,
  110. }
  111. local base_opts = {
  112. on_attach = on_attach,
  113. capabilities = capabilities,
  114. handlers = handlers,
  115. flags = { debounce_text_changes = 150 },
  116. }
  117. -- Load server-specific configuration if it exists
  118. local ok, server_opts = pcall(require, "plugins.lsp.servers." .. server_name)
  119. if ok then
  120. base_opts = vim.tbl_deep_extend("force", base_opts, server_opts)
  121. end
  122. -- Set up the LSP server with the combined options
  123. require("lspconfig")[server_name].setup(base_opts)
  124. end,
  125. },
  126. })
  127. -- Set up non-Mason LSP servers
  128. local non_mason_servers = { "ccls" }
  129. for _, server in ipairs(non_mason_servers) do
  130. require("lspconfig")[server].setup({
  131. on_attach = on_attach,
  132. capabilities = require("blink.cmp").get_lsp_capabilities(),
  133. handlers = handlers,
  134. flags = { debounce_text_changes = 150 },
  135. })
  136. end
  137. end
  138. return M