diagnostics.lua 3.8 KB

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