dap.lua 4.8 KB

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