|
| 1 | +local M = {} |
| 2 | +M = { |
| 3 | + namespace = vim.api.nvim_create_namespace("dap-view"), |
| 4 | + win = Snacks.win({ |
| 5 | + enter = false, |
| 6 | + show = false, |
| 7 | + position = "bottom", |
| 8 | + height = 0.25, |
| 9 | + on_close = function() |
| 10 | + M.watched_expressions = {} |
| 11 | + M.expression_results = {} |
| 12 | + M.updated_evaluations = {} |
| 13 | + end, |
| 14 | + keys = { |
| 15 | + ["<cr>"] = function() |
| 16 | + local line = vim.api.nvim_win_get_cursor(0)[1] |
| 17 | + Snacks.win({ |
| 18 | + position = "bottom", |
| 19 | + height = 0.25, |
| 20 | + width = 0.75, |
| 21 | + text = function() |
| 22 | + return M.expression_results[line] |
| 23 | + end, |
| 24 | + }) |
| 25 | + end, |
| 26 | + ["d"] = function() |
| 27 | + local line = vim.api.nvim_win_get_cursor(0)[1] |
| 28 | + table.remove(M.watched_expressions, line) |
| 29 | + table.remove(M.expression_results, line) |
| 30 | + M._writeWindowContent() |
| 31 | + end, |
| 32 | + ["e"] = function() |
| 33 | + local line = vim.api.nvim_win_get_cursor(0)[1] |
| 34 | + local exp = vim.fn.input("Expression: ", M.watched_expressions[line]) |
| 35 | + if exp == "" then |
| 36 | + return |
| 37 | + end |
| 38 | + M.watched_expressions[line] = exp |
| 39 | + M._evalExpression(exp, function(result) |
| 40 | + M.expression_results[line] = result |
| 41 | + end) |
| 42 | + end, |
| 43 | + }, |
| 44 | + }), |
| 45 | + watched_expressions = {}, |
| 46 | + expression_results = {}, |
| 47 | + updated_evaluations = {}, |
| 48 | +} |
| 49 | + |
| 50 | +local dap = require("dap") |
| 51 | + |
| 52 | +local function split_string_to_table(str) |
| 53 | + local lines = {} |
| 54 | + for line in str:gmatch("([^\n]*)\n?") do |
| 55 | + if line ~= "" then |
| 56 | + table.insert(lines, line) |
| 57 | + end |
| 58 | + end |
| 59 | + return lines |
| 60 | +end |
| 61 | + |
| 62 | +M._writeWindowContent = function() |
| 63 | + if not M.win.buf then |
| 64 | + return |
| 65 | + end |
| 66 | + -- Clear previous content |
| 67 | + vim.api.nvim_buf_set_lines(M.win.buf, 0, -1, true, {}) |
| 68 | + |
| 69 | + if #M.watched_expressions == 0 then |
| 70 | + vim.wo[M.win.win].cursorline = false |
| 71 | + vim.api.nvim_buf_set_lines(M.win.buf, 0, -1, false, { "No expressions" }) |
| 72 | + return |
| 73 | + else |
| 74 | + vim.wo[M.win.win].cursorline = true |
| 75 | + end |
| 76 | + |
| 77 | + vim.api.nvim_buf_set_lines(M.win.buf, 0, #M.watched_expressions + 1, false, M.watched_expressions) |
| 78 | + |
| 79 | + for i = 1, #M.watched_expressions do |
| 80 | + local hl_group = M.updated_evaluations[i] and "DiagnosticVirtualTextWarn" or "Comment" |
| 81 | + local expr_result = M.expression_results[i] |
| 82 | + |
| 83 | + if expr_result then |
| 84 | + local split_lines = split_string_to_table(expr_result) |
| 85 | + local virt_lines = vim |
| 86 | + .iter(split_lines) |
| 87 | + :map(function(r) |
| 88 | + return { { r, hl_group } } |
| 89 | + end) |
| 90 | + :totable() |
| 91 | + |
| 92 | + if not vim.tbl_isempty(virt_lines) then |
| 93 | + vim.api.nvim_buf_set_extmark(M.win.buf, M.namespace, i - 1, 0, { |
| 94 | + virt_lines = virt_lines, |
| 95 | + }) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +end |
| 100 | + |
| 101 | +local function evalExpressionRecurse(prepend, result) |
| 102 | + local session = assert(require("dap").session(), "has active session") |
| 103 | + local frame_id = session.current_frame and session.current_frame.id |
| 104 | + |
| 105 | + local var_ref = result and result.variablesReference |
| 106 | + if var_ref and var_ref > 0 then |
| 107 | + local vars = {} |
| 108 | + |
| 109 | + local var_ref_err, var_ref_result = |
| 110 | + session:request("variables", { variablesReference = var_ref, context = "watch", frameId = frame_id }) |
| 111 | + |
| 112 | + if var_ref_err then |
| 113 | + table.insert(vars, tostring(var_ref_err)) |
| 114 | + end |
| 115 | + |
| 116 | + if var_ref_result and not var_ref_err then |
| 117 | + for _, k in pairs(var_ref_result.variables) do |
| 118 | + if k.name ~= "" then |
| 119 | + table.insert(vars, prepend .. k.name .. " = " .. k.value) |
| 120 | + end |
| 121 | + local appendable = evalExpressionRecurse(prepend .. " ", k) |
| 122 | + if appendable ~= "" then |
| 123 | + table.insert(vars, appendable) |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + return table.concat(vars, "\n") |
| 128 | + end |
| 129 | + return "" |
| 130 | +end |
| 131 | + |
| 132 | +M._evalExpression = function(expr, callback) |
| 133 | + local session = assert(require("dap").session(), "has active session") |
| 134 | + local frame_id = session.current_frame and session.current_frame.id |
| 135 | + |
| 136 | + coroutine.wrap(function() |
| 137 | + local err, result = session:request("evaluate", { expression = expr, context = "watch", frameId = frame_id }) |
| 138 | + |
| 139 | + local expr_result = result and result.result or err and tostring(err):gsub("%s+", " ") or "" |
| 140 | + |
| 141 | + callback(expr_result .. evalExpressionRecurse(" ", result)) |
| 142 | + end)() |
| 143 | +end |
| 144 | + |
| 145 | +M.add_expr = function() |
| 146 | + local exp = vim.fn.input("Expression: ") |
| 147 | + if exp == "" then |
| 148 | + exp = vim.fn.expand("<cexpr>") |
| 149 | + end |
| 150 | + M.add_watch_expr(exp) |
| 151 | + M.open() |
| 152 | +end |
| 153 | + |
| 154 | +M.add_watch_expr = function(expr) |
| 155 | + if not (#expr > 0 and not vim.tbl_contains(M.watched_expressions, expr)) then |
| 156 | + return |
| 157 | + end |
| 158 | + |
| 159 | + if not require("dap").session() then |
| 160 | + vim.notify("No active session") |
| 161 | + return |
| 162 | + end |
| 163 | + |
| 164 | + M._evalExpression(expr, function(result) |
| 165 | + table.insert(M.expression_results, result) |
| 166 | + end) |
| 167 | + |
| 168 | + table.insert(M.watched_expressions, expr) |
| 169 | +end |
| 170 | + |
| 171 | +M.open = function() |
| 172 | + M.win:show() |
| 173 | + M._writeWindowContent() |
| 174 | +end |
| 175 | + |
| 176 | +M.toggle = function() |
| 177 | + local win = M.win:toggle() |
| 178 | + if win:valid() then |
| 179 | + M._writeWindowContent() |
| 180 | + end |
| 181 | +end |
| 182 | + |
| 183 | +local SUBSCRIPTION_ID = "debugger" |
| 184 | + |
| 185 | +dap.listeners.after.evaluate[SUBSCRIPTION_ID] = function() |
| 186 | + M._writeWindowContent() |
| 187 | +end |
| 188 | + |
| 189 | +dap.listeners.after.variables[SUBSCRIPTION_ID] = function() |
| 190 | + M._writeWindowContent() |
| 191 | +end |
| 192 | + |
| 193 | +dap.listeners.after.event_stopped[SUBSCRIPTION_ID] = function() |
| 194 | + for i, expr in ipairs(M.watched_expressions) do |
| 195 | + M._evalExpression(expr, function(result) |
| 196 | + M.updated_evaluations[i] = M.expression_results[i] and M.expression_results[i] ~= result |
| 197 | + M.expression_results[i] = result |
| 198 | + end) |
| 199 | + end |
| 200 | +end |
| 201 | + |
| 202 | +dap.listeners.after.event_terminated[SUBSCRIPTION_ID] = function() |
| 203 | + for k in ipairs(M.expression_results) do |
| 204 | + M.expression_results[k] = nil |
| 205 | + end |
| 206 | +end |
| 207 | + |
| 208 | +return M |
0 commit comments