diagnostics.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. local M = {
  2. "folke/trouble.nvim",
  3. branch = "main",
  4. }
  5. local function setup_keymaps(trouble)
  6. -- Diagnostic navigation
  7. nmap("[d", function()
  8. vim.diagnostic.jump({ count = -1, float = true })
  9. end, { desc = "Previous diagnostic" })
  10. nmap("]d", function()
  11. vim.diagnostic.jump({ count = 1, float = true })
  12. end, { desc = "Next diagnostic" })
  13. -- Trouble specific navigation
  14. nmap("<a-k>", function()
  15. trouble.previous({ skip_groups = true, jump = true })
  16. end, { desc = "Previous trouble item" })
  17. nmap("<a-j>", function()
  18. trouble.next({ skip_groups = true, jump = true })
  19. end, { desc = "Next trouble item" })
  20. -- Trouble mode toggles
  21. nmap("<leader>tt", "<cmd>TroubleToggle<cr>", { desc = "Toggle trouble" })
  22. nmap("<leader>tw", "<cmd>TroubleToggle workspace_diagnostics<cr>", { desc = "Workspace diagnostics" })
  23. nmap("<leader>td", "<cmd>TroubleToggle document_diagnostics<cr>", { desc = "Document diagnostics" })
  24. nmap("<leader>tq", "<cmd>TroubleToggle quickfix<cr>", { desc = "Quickfix list" })
  25. nmap("<leader>tl", "<cmd>TroubleToggle loclist<cr>", { desc = "Location list" })
  26. end
  27. local function setup_diagnostic_config()
  28. vim.diagnostic.config({
  29. virtual_text = {
  30. prefix = "●",
  31. suffix = "",
  32. format = function(diagnostic)
  33. local icons = {
  34. [vim.diagnostic.severity.ERROR] = " ",
  35. [vim.diagnostic.severity.WARN] = " ",
  36. [vim.diagnostic.severity.HINT] = " ",
  37. [vim.diagnostic.severity.INFO] = " ",
  38. }
  39. local icon = icons[diagnostic.severity] or ""
  40. return string.format("%s %s", icon, diagnostic.message)
  41. end,
  42. },
  43. underline = false,
  44. update_in_insert = false,
  45. signs = {
  46. active = true,
  47. text = {
  48. [vim.diagnostic.severity.ERROR] = "",
  49. [vim.diagnostic.severity.WARN] = "",
  50. [vim.diagnostic.severity.HINT] = "",
  51. [vim.diagnostic.severity.INFO] = "",
  52. },
  53. },
  54. float = {
  55. focusable = true,
  56. style = "minimal",
  57. border = "rounded",
  58. source = true,
  59. header = "",
  60. prefix = "",
  61. format = function(diagnostic)
  62. local severity = vim.diagnostic.severity[diagnostic.severity]
  63. return string.format("%s: %s", severity:lower(), diagnostic.message)
  64. end,
  65. },
  66. severity_sort = true,
  67. })
  68. end
  69. function M.config()
  70. local trouble = require("trouble")
  71. trouble.setup({
  72. position = "bottom",
  73. height = 10,
  74. width = 50,
  75. -- icons = false,
  76. mode = "workspace_diagnostics",
  77. fold_open = "",
  78. fold_closed = "",
  79. group = true,
  80. padding = true,
  81. action_keys = {
  82. close = "q", -- close the list
  83. cancel = "<esc>", -- cancel the preview and get back to your last window / buffer / cursor
  84. refresh = "r", -- manually refresh
  85. jump = { "<cr>", "<tab>" }, -- jump to the diagnostic or open / close folds
  86. open_split = { "<c-x>" }, -- open buffer in new split
  87. open_vsplit = { "<c-v>" }, -- open buffer in new vsplit
  88. open_tab = { "<c-t>" }, -- open buffer in new tab
  89. toggle_mode = "m", -- toggle between "workspace" and "document" mode
  90. toggle_preview = "P", -- toggle auto_preview
  91. preview = "p", -- preview the diagnostic location
  92. close_folds = { "zM", "zm" }, -- close all folds
  93. open_folds = { "zR", "zr" }, -- open all folds
  94. toggle_fold = { "zA", "za" }, -- toggle fold of current file
  95. previous = "k", -- previous item
  96. next = "j", -- next item
  97. },
  98. auto_preview = true,
  99. auto_fold = false,
  100. auto_jump = { "lsp_definitions" },
  101. -- signs = {
  102. -- -- Icons / text used for a diagnostic
  103. -- error = "",
  104. -- warning = "",
  105. -- hint = "",
  106. -- information = "",
  107. -- other = "",
  108. -- },
  109. use_diagnostic_signs = false,
  110. })
  111. -- Setup keymaps
  112. setup_keymaps(trouble)
  113. -- Setup diagnostic configuration
  114. setup_diagnostic_config()
  115. end
  116. return M