init.lua 4.2 KB

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