Skip to content

Commit ed2c345

Browse files
committed
feat(watches): add visual/cexpr expression
See #160
1 parent b2fdea8 commit ed2c345

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

lua/dapui/elements/watches.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ return function(client)
3232
})
3333

3434
--- Add a new watch expression
35-
---@param expr string
35+
---@param expr? string
3636
function dapui.elements.watches.add(expr)
37+
if not expr then
38+
expr = util.get_current_expr()
39+
end
3740
watches.add(expr)
3841
end
3942

lua/dapui/init.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ local prev_expr = nil
226226
---
227227
--- If no fixed dimensions are given, the window will expand to fit the contents
228228
--- of the buffer.
229-
---@param expr string Expression to evaluate. If nil, then in normal more the
229+
---@param expr? string Expression to evaluate. If nil, then in normal more the
230230
--- current word is used, and in visual mode the currently highlighted text.
231231
---@param args dapui.EvalArgs
232232
function dapui.eval(expr, args)
@@ -237,14 +237,7 @@ function dapui.eval(expr, args)
237237
end
238238
args = args or {}
239239
if not expr then
240-
if vim.fn.mode() == "v" then
241-
local start = async.fn.getpos("v")
242-
local finish = async.fn.getpos(".")
243-
local lines = util.get_selection(start, finish)
244-
expr = table.concat(lines, "\n")
245-
else
246-
expr = expr or async.fn.expand("<cexpr>")
247-
end
240+
expr = util.get_current_expr()
248241
end
249242
if open_float then
250243
if prev_expr == expr then

lua/dapui/util.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ function M.create_render_loop(render)
2626
end
2727
end
2828

29+
function M.get_current_expr()
30+
if async.fn.mode() == "v" then
31+
local start = async.fn.getpos("v")
32+
local finish = async.fn.getpos(".")
33+
local lines = M.get_selection(start, finish)
34+
return table.concat(lines, "\n")
35+
end
36+
return async.fn.expand("<cexpr>")
37+
end
38+
2939
function M.create_buffer(name, options)
3040
local buf = async.api.nvim_create_buf(true, true)
3141
options = vim.tbl_extend("keep", options or {}, {

0 commit comments

Comments
 (0)