dap.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. local M = {}
  2. M.config = function()
  3. lvim.builtin.dap = {
  4. active = true,
  5. on_config_done = nil,
  6. breakpoint = {
  7. text = lvim.icons.ui.Bug,
  8. texthl = "DiagnosticSignError",
  9. linehl = "",
  10. numhl = "",
  11. },
  12. breakpoint_rejected = {
  13. text = lvim.icons.ui.Bug,
  14. texthl = "DiagnosticSignError",
  15. linehl = "",
  16. numhl = "",
  17. },
  18. stopped = {
  19. text = lvim.icons.ui.BoldArrowRight,
  20. texthl = "DiagnosticSignWarn",
  21. linehl = "Visual",
  22. numhl = "DiagnosticSignWarn",
  23. },
  24. log = {
  25. level = "info",
  26. },
  27. ui = {
  28. auto_open = true,
  29. notify = {
  30. threshold = vim.log.levels.INFO,
  31. },
  32. config = {
  33. expand_lines = true,
  34. icons = { expanded = "", collapsed = "", circular = "" },
  35. mappings = {
  36. -- Use a table to apply multiple mappings
  37. expand = { "<CR>", "<2-LeftMouse>" },
  38. open = "o",
  39. remove = "d",
  40. edit = "e",
  41. repl = "r",
  42. toggle = "t",
  43. },
  44. layouts = {
  45. {
  46. elements = {
  47. { id = "scopes", size = 0.33 },
  48. { id = "breakpoints", size = 0.17 },
  49. { id = "stacks", size = 0.25 },
  50. { id = "watches", size = 0.25 },
  51. },
  52. size = 0.33,
  53. position = "right",
  54. },
  55. {
  56. elements = {
  57. { id = "repl", size = 0.45 },
  58. { id = "console", size = 0.55 },
  59. },
  60. size = 0.27,
  61. position = "bottom",
  62. },
  63. },
  64. floating = {
  65. max_height = 0.9,
  66. max_width = 0.5, -- Floats will be treated as percentage of your screen.
  67. border = vim.g.border_chars, -- Border style. Can be 'single', 'double' or 'rounded'
  68. mappings = {
  69. close = { "q", "<Esc>" },
  70. },
  71. },
  72. },
  73. },
  74. }
  75. end
  76. M.setup = function()
  77. local status_ok, dap = pcall(require, "dap")
  78. if not status_ok then
  79. return
  80. end
  81. if lvim.use_icons then
  82. vim.fn.sign_define("DapBreakpoint", lvim.builtin.dap.breakpoint)
  83. vim.fn.sign_define("DapBreakpointRejected", lvim.builtin.dap.breakpoint_rejected)
  84. vim.fn.sign_define("DapStopped", lvim.builtin.dap.stopped)
  85. end
  86. dap.set_log_level(lvim.builtin.dap.log.level)
  87. if lvim.builtin.dap.on_config_done then
  88. lvim.builtin.dap.on_config_done(dap)
  89. end
  90. end
  91. M.setup_ui = function()
  92. local status_ok, dap = pcall(require, "dap")
  93. if not status_ok then
  94. return
  95. end
  96. local dapui = require "dapui"
  97. dapui.setup(lvim.builtin.dap.ui.config)
  98. if lvim.builtin.dap.ui.auto_open then
  99. dap.listeners.after.event_initialized["dapui_config"] = function()
  100. dapui.open()
  101. end
  102. -- dap.listeners.before.event_terminated["dapui_config"] = function()
  103. -- dapui.close()
  104. -- end
  105. -- dap.listeners.before.event_exited["dapui_config"] = function()
  106. -- dapui.close()
  107. -- end
  108. end
  109. local Log = require "lvim.core.log"
  110. -- until rcarriga/nvim-dap-ui#164 is fixed
  111. local function notify_handler(msg, level, opts)
  112. if level >= lvim.builtin.dap.ui.notify.threshold then
  113. return vim.notify(msg, level, opts)
  114. end
  115. opts = vim.tbl_extend("keep", opts or {}, {
  116. title = "dap-ui",
  117. icon = "",
  118. on_open = function(win)
  119. vim.api.nvim_buf_set_option(vim.api.nvim_win_get_buf(win), "filetype", "markdown")
  120. end,
  121. })
  122. -- vim_log_level can be omitted
  123. if level == nil then
  124. level = Log.levels["INFO"]
  125. elseif type(level) == "string" then
  126. level = Log.levels[(level):upper()] or Log.levels["INFO"]
  127. else
  128. -- https://github.com/neovim/neovim/blob/685cf398130c61c158401b992a1893c2405cd7d2/runtime/lua/vim/lsp/log.lua#L5
  129. level = level + 1
  130. end
  131. msg = string.format("%s: %s", opts.title, msg)
  132. Log:add_entry(level, msg)
  133. end
  134. local dapui_ok, _ = xpcall(function()
  135. require("dapui.util").notify = notify_handler
  136. end, debug.traceback)
  137. if not dapui_ok then
  138. Log:debug "Unable to override dap-ui logging level"
  139. end
  140. end
  141. return M