init.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. return {
  2. "neovim/nvim-lspconfig",
  3. dependencies = {
  4. require("plugins.lsp.extras.lazydev"),
  5. require("plugins.lsp.extras.gopher"),
  6. "j-hui/fidget.nvim",
  7. "ibhagwan/fzf-lua",
  8. },
  9. config = function()
  10. require("fidget").setup({})
  11. -- Enhance floating preview windows
  12. local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
  13. function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
  14. opts = opts or {}
  15. opts.border = opts.border or "rounded"
  16. opts.max_width = opts.max_width or 80
  17. opts.max_height = opts.max_height or 20
  18. return orig_util_open_floating_preview(contents, syntax, opts, ...)
  19. end
  20. -- Diagnostics configuration
  21. vim.diagnostic.config({
  22. virtual_text = { spacing = 2, source = "if_many" },
  23. float = { border = "rounded", source = "if_many" },
  24. signs = vim.g.have_nerd_font and {
  25. text = {
  26. [vim.diagnostic.severity.ERROR] = "󰅚",
  27. [vim.diagnostic.severity.WARN] = "󰀪",
  28. [vim.diagnostic.severity.INFO] = "󰋽",
  29. [vim.diagnostic.severity.HINT] = "󰌶",
  30. },
  31. } or true,
  32. underline = true,
  33. update_in_insert = true,
  34. severity_sort = true,
  35. })
  36. -- Global floating options
  37. _G.floating_options = {
  38. focusable = true,
  39. focus = false,
  40. max_height = 50,
  41. max_width = 100,
  42. }
  43. -- Auto-format on save
  44. vim.api.nvim_create_autocmd("BufWritePre", {
  45. callback = function()
  46. if vim.lsp.buf_is_attached() then
  47. vim.lsp.buf.format()
  48. end
  49. end,
  50. })
  51. -- Filter out unwanted code actions
  52. vim.lsp.buf.code_action = (function(orig)
  53. return function(opts)
  54. opts = opts or {}
  55. opts.filter = function(action)
  56. if not action then
  57. return false
  58. end
  59. -- Ignore gopls "Browse" actions
  60. if action.title and action.title:match("Browse gopls") then
  61. return false
  62. end
  63. return true
  64. end
  65. return orig(opts)
  66. end
  67. end)(vim.lsp.buf.code_action)
  68. -- Keymaps setup function
  69. local function setup_keymaps(bufnr)
  70. local fzf = require("fzf-lua")
  71. local opts = { buffer = bufnr }
  72. -- Helper function for mapping
  73. local function nmap(key, func, desc_opts)
  74. vim.keymap.set("n", key, func, desc_opts)
  75. end
  76. -- Basic LSP
  77. nmap("K", vim.lsp.buf.hover, vim.tbl_extend("force", opts, { desc = "Hover Doc" }))
  78. nmap("<C-h>", vim.lsp.buf.signature_help, vim.tbl_extend("force", opts, { desc = "Signature Help" }))
  79. nmap("<leader>r", vim.lsp.buf.rename, vim.tbl_extend("force", opts, { desc = "Rename" }))
  80. nmap("<leader>ca", vim.lsp.buf.code_action, vim.tbl_extend("force", opts, { desc = "Code Action" }))
  81. -- Navigation
  82. nmap("gd", fzf.lsp_definitions, vim.tbl_extend("force", opts, { desc = "Go to Definition" }))
  83. nmap("gr", fzf.lsp_references, vim.tbl_extend("force", opts, { desc = "Go to References" }))
  84. nmap("gD", vim.lsp.buf.declaration, vim.tbl_extend("force", opts, { desc = "Go to Declaration" }))
  85. nmap("gi", fzf.lsp_implementations, vim.tbl_extend("force", opts, { desc = "Go to Implementation" }))
  86. nmap("gt", fzf.lsp_typedefs, vim.tbl_extend("force", opts, { desc = "Go to Type Definition" }))
  87. -- Diagnostics
  88. nmap("vd", vim.diagnostic.open_float, vim.tbl_extend("force", opts, { desc = "View Diagnostics" }))
  89. nmap("<leader>dl", fzf.diagnostics_document, vim.tbl_extend("force", opts, { desc = "Document Diagnostics" }))
  90. nmap("<leader>dw", fzf.diagnostics_workspace, vim.tbl_extend("force", opts, { desc = "Workspace Diagnostics" }))
  91. nmap("<leader>ds", fzf.lsp_document_symbols, vim.tbl_extend("force", opts, { desc = "Document Symbols" }))
  92. nmap("<leader>ws", fzf.lsp_workspace_symbols, vim.tbl_extend("force", opts, { desc = "Workspace Symbols" }))
  93. -- LSP management
  94. nmap("<leader>lr", function()
  95. local clients = vim.lsp.get_clients({ bufnr = bufnr })
  96. if #clients == 0 then
  97. vim.notify("No LSP clients attached to buffer", vim.log.levels.WARN)
  98. return
  99. end
  100. local client_names = {}
  101. for _, client in ipairs(clients) do
  102. table.insert(client_names, client.name)
  103. vim.cmd("LspRestart " .. client.name)
  104. end
  105. vim.notify("Restarted LSP clients: " .. table.concat(client_names, ", "), vim.log.levels.INFO)
  106. end, vim.tbl_extend("force", opts, { desc = "Restart LSP" }))
  107. nmap("<leader>li", ":LspInfo<CR>", vim.tbl_extend("force", opts, { desc = "LSP Info" }))
  108. end
  109. local lspconfig = require("lspconfig")
  110. -- Lista de servidores LSP a habilitar
  111. local servers = {
  112. "gopls",
  113. "jsonls",
  114. "clangd",
  115. "lua_ls",
  116. "yamlls",
  117. "graphql",
  118. "html",
  119. "cssls",
  120. "omnisharp",
  121. "svelte",
  122. "templ",
  123. "tinymist",
  124. "jdtls",
  125. -- "ts_ls",
  126. "nixd",
  127. }
  128. -- Setup automático - Neovim 11 carga las configs automáticamente
  129. for _, server in ipairs(servers) do
  130. lspconfig[server].setup({})
  131. end
  132. -- LSP Attach autocmd
  133. vim.api.nvim_create_autocmd("LspAttach", {
  134. group = vim.api.nvim_create_augroup("UserLspConfig", { clear = true }),
  135. callback = function(args)
  136. local client = vim.lsp.get_client_by_id(args.data.client_id)
  137. if not client then
  138. return
  139. end
  140. -- Setup keymaps
  141. setup_keymaps(args.buf)
  142. -- Inlay hints
  143. if client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint, args.buf) then
  144. vim.keymap.set("n", "<leader>th", function()
  145. vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = args.buf }), { bufnr = args.buf })
  146. end, { buffer = args.buf, desc = "Toggle Inlay Hints" })
  147. end
  148. -- Document highlights
  149. if client.server_capabilities.documentHighlightProvider then
  150. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  151. buffer = args.buf,
  152. callback = vim.lsp.buf.document_highlight,
  153. })
  154. vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
  155. buffer = args.buf,
  156. callback = vim.lsp.buf.clear_references,
  157. })
  158. end
  159. end,
  160. })
  161. end,
  162. }