init.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. -- Formatters
  76. "prettierd",
  77. "shfmt",
  78. "stylua",
  79. "latexindent",
  80. "clang-format",
  81. -- Additional tools
  82. "eslint_d",
  83. "templ",
  84. }
  85. -- Install missing tools
  86. local registry = require("mason-registry")
  87. for _, tool in ipairs(ensure_installed) do
  88. if not registry.is_installed(tool) then
  89. vim.cmd("MasonInstall " .. tool)
  90. end
  91. end
  92. -- Set up Mason LSP config
  93. require("mason-lspconfig").setup({
  94. automatic_installation = true,
  95. ensure_installed = {
  96. "gopls",
  97. "html",
  98. "htmx",
  99. "jsonls",
  100. "lua_ls",
  101. "omnisharp",
  102. "yamlls",
  103. "graphql",
  104. },
  105. handlers = {
  106. function(server_name)
  107. local capabilities = require("blink.cmp").get_lsp_capabilities()
  108. -- Enable folding capabilities
  109. capabilities.textDocument.foldingRange = {
  110. dynamicRegistration = false,
  111. lineFoldingOnly = true,
  112. }
  113. local base_opts = {
  114. on_attach = on_attach,
  115. capabilities = capabilities,
  116. handlers = handlers,
  117. flags = { debounce_text_changes = 150 },
  118. }
  119. -- Load server-specific configuration if it exists
  120. local ok, server_opts = pcall(require, "config.lsp." .. server_name)
  121. if ok then
  122. base_opts = vim.tbl_deep_extend("force", base_opts, server_opts)
  123. end
  124. -- Set up the LSP server with the combined options
  125. require("lspconfig")[server_name].setup(base_opts)
  126. end,
  127. },
  128. })
  129. -- Set up non-Mason LSP servers
  130. local non_mason_servers = { "ccls" }
  131. for _, server in ipairs(non_mason_servers) do
  132. require("lspconfig")[server].setup({
  133. on_attach = on_attach,
  134. capabilities = require("blink.cmp").get_lsp_capabilities(),
  135. handlers = handlers,
  136. flags = { debounce_text_changes = 150 },
  137. })
  138. end
  139. end
  140. return M