dap.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. icons = { expanded = "", collapsed = "", circular = "" },
  34. mappings = {
  35. -- Use a table to apply multiple mappings
  36. expand = { "<CR>", "<2-LeftMouse>" },
  37. open = "o",
  38. remove = "d",
  39. edit = "e",
  40. repl = "r",
  41. toggle = "t",
  42. },
  43. -- Use this to override mappings for specific elements
  44. element_mappings = {},
  45. expand_lines = true,
  46. layouts = {
  47. {
  48. elements = {
  49. { id = "scopes", size = 0.33 },
  50. { id = "breakpoints", size = 0.17 },
  51. { id = "stacks", size = 0.25 },
  52. { id = "watches", size = 0.25 },
  53. },
  54. size = 0.33,
  55. position = "right",
  56. },
  57. {
  58. elements = {
  59. { id = "repl", size = 0.45 },
  60. { id = "console", size = 0.55 },
  61. },
  62. size = 0.27,
  63. position = "bottom",
  64. },
  65. },
  66. controls = {
  67. enabled = true,
  68. -- Display controls in this element
  69. element = "repl",
  70. icons = {
  71. pause = "",
  72. play = "",
  73. step_into = "",
  74. step_over = "",
  75. step_out = "",
  76. step_back = "",
  77. run_last = "",
  78. terminate = "",
  79. },
  80. },
  81. floating = {
  82. max_height = 0.9,
  83. max_width = 0.5, -- Floats will be treated as percentage of your screen.
  84. border = "rounded",
  85. mappings = {
  86. close = { "q", "<Esc>" },
  87. },
  88. },
  89. windows = { indent = 1 },
  90. render = {
  91. max_type_length = nil, -- Can be integer or nil.
  92. max_value_lines = 100, -- Can be integer or nil.
  93. },
  94. },
  95. },
  96. }
  97. end
  98. M.setup = function()
  99. local status_ok, dap = pcall(require, "dap")
  100. if not status_ok then
  101. return
  102. end
  103. if lvim.use_icons then
  104. vim.fn.sign_define("DapBreakpoint", lvim.builtin.dap.breakpoint)
  105. vim.fn.sign_define("DapBreakpointRejected", lvim.builtin.dap.breakpoint_rejected)
  106. vim.fn.sign_define("DapStopped", lvim.builtin.dap.stopped)
  107. end
  108. dap.set_log_level(lvim.builtin.dap.log.level)
  109. if lvim.builtin.dap.on_config_done then
  110. lvim.builtin.dap.on_config_done(dap)
  111. end
  112. end
  113. M.setup_ui = function()
  114. local status_ok, dap = pcall(require, "dap")
  115. if not status_ok then
  116. return
  117. end
  118. local dapui = require "dapui"
  119. dapui.setup(lvim.builtin.dap.ui.config)
  120. if lvim.builtin.dap.ui.auto_open then
  121. dap.listeners.after.event_initialized["dapui_config"] = function()
  122. dapui.open()
  123. end
  124. -- dap.listeners.before.event_terminated["dapui_config"] = function()
  125. -- dapui.close()
  126. -- end
  127. -- dap.listeners.before.event_exited["dapui_config"] = function()
  128. -- dapui.close()
  129. -- end
  130. end
  131. local Log = require "lvim.core.log"
  132. -- until rcarriga/nvim-dap-ui#164 is fixed
  133. local function notify_handler(msg, level, opts)
  134. if level >= lvim.builtin.dap.ui.notify.threshold then
  135. return vim.notify(msg, level, opts)
  136. end
  137. opts = vim.tbl_extend("keep", opts or {}, {
  138. title = "dap-ui",
  139. icon = "",
  140. on_open = function(win)
  141. vim.api.nvim_set_option_value("filetype", "markdown", { buf = vim.api.nvim_win_get_buf(win) })
  142. end,
  143. })
  144. -- vim_log_level can be omitted
  145. if level == nil then
  146. level = Log.levels["INFO"]
  147. elseif type(level) == "string" then
  148. level = Log.levels[(level):upper()] or Log.levels["INFO"]
  149. else
  150. -- https://github.com/neovim/neovim/blob/685cf398130c61c158401b992a1893c2405cd7d2/runtime/lua/vim/lsp/log.lua#L5
  151. level = level + 1
  152. end
  153. msg = string.format("%s: %s", opts.title, msg)
  154. Log:add_entry(level, msg)
  155. end
  156. local dapui_ok, _ = xpcall(function()
  157. require("dapui.util").notify = notify_handler
  158. end, debug.traceback)
  159. if not dapui_ok then
  160. Log:debug "Unable to override dap-ui logging level"
  161. end
  162. end
  163. return M