init.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. local M = {
  2. "WhoIsSethDaniel/mason-tool-installer.nvim",
  3. dependencies = {
  4. "mason-org/mason-lspconfig.nvim",
  5. "mason-org/mason.nvim",
  6. "j-hui/fidget.nvim",
  7. "ibhagwan/fzf-lua",
  8. require("plugins.lsp.extras.lazydev"),
  9. require("plugins.lsp.extras.gopher"),
  10. },
  11. }
  12. local function setup_keymaps(bufnr)
  13. local fzf = require("fzf-lua")
  14. local opts = { buffer = bufnr }
  15. -- Basic LSP
  16. nmap("K", vim.lsp.buf.hover, vim.tbl_extend("force", opts, { desc = "Hover Doc" }))
  17. nmap("<C-h>", vim.lsp.buf.signature_help, vim.tbl_extend("force", opts, { desc = "Signature Help" }))
  18. nmap("<leader>r", vim.lsp.buf.rename, vim.tbl_extend("force", opts, { desc = "Rename" }))
  19. nmap("<leader>ca", vim.lsp.buf.code_action, vim.tbl_extend("force", opts, { desc = "Code Action" }))
  20. -- Navigation
  21. nmap("gd", fzf.lsp_definitions, vim.tbl_extend("force", opts, { desc = "Go to Definition" }))
  22. nmap("gr", fzf.lsp_references, vim.tbl_extend("force", opts, { desc = "Go to References" }))
  23. nmap("gD", vim.lsp.buf.declaration, vim.tbl_extend("force", opts, { desc = "Go to Declaration" }))
  24. nmap("gi", fzf.lsp_implementations, vim.tbl_extend("force", opts, { desc = "Go to Implementation" }))
  25. nmap("gt", fzf.lsp_typedefs, vim.tbl_extend("force", opts, { desc = "Go to Type Definition" }))
  26. -- Diagnostics
  27. nmap("vd", vim.diagnostic.open_float, vim.tbl_extend("force", opts, { desc = "View Diagnostics" }))
  28. nmap("<leader>dl", fzf.diagnostics_document, vim.tbl_extend("force", opts, { desc = "Document Diagnostics" }))
  29. nmap("<leader>dw", fzf.diagnostics_workspace, vim.tbl_extend("force", opts, { desc = "Workspace Diagnostics" }))
  30. nmap("<leader>ds", fzf.lsp_document_symbols, vim.tbl_extend("force", opts, { desc = "Document Symbols" }))
  31. nmap("<leader>ws", fzf.lsp_workspace_symbols, vim.tbl_extend("force", opts, { desc = "Workspace Symbols" }))
  32. -- LSP management
  33. nmap("<leader>lr", function()
  34. local clients = vim.lsp.get_clients({ bufnr = bufnr })
  35. if #clients == 0 then
  36. vim.notify("No LSP clients attached to buffer", vim.log.levels.WARN)
  37. return
  38. end
  39. local client_names = {}
  40. for _, client in ipairs(clients) do
  41. table.insert(client_names, client.name)
  42. vim.cmd("LspRestart " .. client.name)
  43. end
  44. vim.notify("Restarted LSP clients: " .. table.concat(client_names, ", "), vim.log.levels.INFO)
  45. end, vim.tbl_extend("force", opts, { desc = "Restart LSP" }))
  46. nmap("<leader>li", ":LspInfo<CR>", vim.tbl_extend("force", opts, { desc = "LSP Info" }))
  47. end
  48. function M.config()
  49. require("mason").setup({ max_concurrent_installers = 4 })
  50. require("fidget").setup({})
  51. local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
  52. function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
  53. opts = opts or {}
  54. opts.border = opts.border or "rounded"
  55. opts.max_width = opts.max_width or 80
  56. opts.max_height = opts.max_height or 20
  57. return orig_util_open_floating_preview(contents, syntax, opts, ...)
  58. end
  59. -- Diagnostics
  60. vim.diagnostic.config({
  61. virtual_text = { spacing = 2, source = "if_many" },
  62. float = { border = "rounded", source = "if_many" },
  63. severity_sort = true,
  64. signs = vim.g.have_nerd_font and {
  65. text = {
  66. [vim.diagnostic.severity.ERROR] = "󰅚",
  67. [vim.diagnostic.severity.WARN] = "󰀪",
  68. [vim.diagnostic.severity.INFO] = "󰋽",
  69. [vim.diagnostic.severity.HINT] = "󰌶",
  70. },
  71. } or {},
  72. })
  73. local servers = {
  74. "gopls",
  75. "jsonls",
  76. "lua_ls",
  77. "yamlls",
  78. "graphql",
  79. "html",
  80. "omnisharp",
  81. "svelte",
  82. "vtsls",
  83. "ccls",
  84. "templ",
  85. }
  86. local tools = {
  87. "prettierd",
  88. "shfmt",
  89. "stylua",
  90. "latexindent",
  91. "clang-format",
  92. "csharpier",
  93. "quick-lint-js",
  94. }
  95. local mason_unsupported = { "ccls" }
  96. local mason_servers = vim.tbl_filter(function(server)
  97. return not vim.tbl_contains(mason_unsupported, server)
  98. end, servers)
  99. require("mason-lspconfig").setup({
  100. ensure_installed = mason_servers,
  101. automatic_installation = true,
  102. automatic_enable = true,
  103. })
  104. require("mason-tool-installer").setup({ ensure_installed = tools })
  105. vim.lsp.enable(servers)
  106. -- LSP Attach
  107. vim.api.nvim_create_autocmd("LspAttach", {
  108. group = vim.api.nvim_create_augroup("UserLspConfig", { clear = true }),
  109. callback = function(args)
  110. local client = vim.lsp.get_client_by_id(args.data.client_id)
  111. if not client then
  112. return
  113. end
  114. setup_keymaps(args.buf)
  115. -- Inlay hints
  116. if client:supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint, args.buf) then
  117. nmap("<leader>th", function()
  118. vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = args.buf }), { bufnr = args.buf })
  119. end, { buffer = args.buf, desc = "Toggle Inlay Hints" })
  120. end
  121. -- Document highlights
  122. if client.server_capabilities.documentHighlightProvider then
  123. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  124. buffer = args.buf,
  125. callback = vim.lsp.buf.document_highlight,
  126. })
  127. vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
  128. buffer = args.buf,
  129. callback = vim.lsp.buf.clear_references,
  130. })
  131. end
  132. end,
  133. })
  134. end
  135. return M