init.lua 4.1 KB

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