init.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. local function setup_autocommands(client, bufnr)
  13. if client.server_capabilities.documentHighlightProvider then
  14. local group = vim.api.nvim_create_augroup("LSPDocumentHighlight", { clear = true })
  15. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  16. group = group,
  17. buffer = bufnr,
  18. callback = vim.lsp.buf.document_highlight,
  19. })
  20. vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
  21. group = group,
  22. buffer = bufnr,
  23. callback = vim.lsp.buf.clear_references,
  24. })
  25. end
  26. end
  27. local function setup_keymaps(bufnr)
  28. local keymaps = {
  29. { "<C-h>", vim.lsp.buf.signature_help, "Signature Help" },
  30. { "<leader>cw", vim.lsp.buf.rename, "Rename" },
  31. { "<leader>r", vim.lsp.buf.rename, "Rename" },
  32. { "vd", vim.diagnostic.open_float, "Open Diagnostics" },
  33. { "<leader>lr", ":LspRestart<CR>", "Restart LSP" },
  34. { "<leader>li", ":LspInfo<CR>", "LSP Info" },
  35. }
  36. for _, map in ipairs(keymaps) do
  37. nmap(map[1], map[2], {
  38. buffer = bufnr,
  39. desc = map[3],
  40. })
  41. end
  42. end
  43. local function on_attach(client, bufnr)
  44. setup_autocommands(client, bufnr)
  45. setup_keymaps(bufnr)
  46. end
  47. local BORDER = "rounded"
  48. local handlers = {
  49. ["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = BORDER }),
  50. ["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = BORDER }),
  51. }
  52. function M.config()
  53. require("mason").setup({
  54. max_concurrent_installers = 4,
  55. })
  56. -- Ensure all tools are installed
  57. local ensure_installed = {
  58. -- LSP servers
  59. "gopls",
  60. "graphql-language-service-cli",
  61. "html-lsp",
  62. "htmx-lsp",
  63. "json-lsp",
  64. "lua-language-server",
  65. "omnisharp",
  66. "vtsls",
  67. "yaml-language-server",
  68. -- Formatters
  69. "prettierd",
  70. "shfmt",
  71. "stylua",
  72. "latexindent",
  73. -- Additional tools
  74. "eslint_d",
  75. "templ",
  76. }
  77. local registry = require("mason-registry")
  78. for _, tool in ipairs(ensure_installed) do
  79. if not registry.is_installed(tool) then
  80. vim.cmd("MasonInstall " .. tool)
  81. end
  82. end
  83. require("mason-lspconfig").setup({
  84. automatic_installation = true,
  85. ensure_installed = {
  86. "gopls",
  87. "html",
  88. "htmx",
  89. "jsonls",
  90. "lua_ls",
  91. "omnisharp",
  92. "yamlls",
  93. "graphql",
  94. },
  95. handlers = {
  96. function(server_name)
  97. local capabilities = require("blink.cmp").get_lsp_capabilities()
  98. -- Enable folding capabilities
  99. capabilities.textDocument.foldingRange = {
  100. dynamicRegistration = false,
  101. lineFoldingOnly = true,
  102. }
  103. local base_opts = {
  104. on_attach = on_attach,
  105. capabilities = capabilities,
  106. handlers = handlers,
  107. flags = {
  108. debounce_text_changes = 150,
  109. },
  110. }
  111. -- Try to load server-specific configuration
  112. local ok, server_opts = pcall(require, "plugins.lsp.servers." .. server_name)
  113. if ok then
  114. base_opts = vim.tbl_deep_extend("force", base_opts, server_opts)
  115. end
  116. -- Set up the LSP server
  117. require("lspconfig")[server_name].setup(base_opts)
  118. end,
  119. },
  120. })
  121. end
  122. return M