Skip to content

Commit ada210a

Browse files
committed
refactor(*): cleanup
1 parent 6befafd commit ada210a

File tree

11 files changed

+64
-68
lines changed

11 files changed

+64
-68
lines changed

lua/dap-view/actions.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ local state = require("dap-view.state")
88
local settings = require("dap-view.options.settings")
99
local globals = require("dap-view.globals")
1010

11-
local api = vim.api
12-
1311
local M = {}
1412

13+
local api = vim.api
14+
1515
---@param hide_terminal? boolean
1616
M.toggle = function(hide_terminal)
1717
if state.bufnr then
@@ -81,9 +81,9 @@ M.open = function()
8181
end
8282

8383
M.add_expr = function()
84-
require("dap-view.watches.actions").add_watch_expr(vim.fn.expand("<cexpr>"))
85-
86-
require("dap-view.views").switch(require("dap-view.watches.view").show)
84+
if require("dap-view.watches.actions").add_watch_expr(vim.fn.expand("<cexpr>")) then
85+
require("dap-view.views").switch_to_view(require("dap-view.watches.view").show)
86+
end
8787
end
8888

8989
return M

lua/dap-view/exceptions/view.lua

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@ M.show = function()
1616
-- Clear previous content
1717
api.nvim_buf_set_lines(state.bufnr, 0, -1, true, {})
1818

19-
if
20-
views.cleanup_view(not dap.session(), "No active session")
21-
or views.cleanup_view(not state.exceptions_options, "Not supported by debug adapter")
22-
then
19+
if views.cleanup_view(not dap.session(), "No active session") then
2320
return
2421
end
2522

26-
if state.exceptions_options then
27-
local content = vim.iter(state.exceptions_options)
28-
:map(function(opt)
29-
local icon = opt.enabled and "" or ""
30-
return " " .. icon .. " " .. opt.exception_filter.label
31-
end)
32-
:totable()
23+
if views.cleanup_view(not state.exceptions_options, "Not supported by debug adapter") then
24+
return
25+
end
26+
27+
local content = vim.iter(state.exceptions_options or {})
28+
:map(function(opt)
29+
local icon = opt.enabled and "" or ""
30+
return " " .. icon .. " " .. opt.exception_filter.label
31+
end)
32+
:totable()
3333

34-
api.nvim_buf_set_lines(state.bufnr, 0, -1, false, content)
34+
api.nvim_buf_set_lines(state.bufnr, 0, -1, false, content)
3535

36-
for i, opt in ipairs(state.exceptions_options) do
37-
local hl_type = opt.enabled and "Enabled" or "Disabled"
38-
hl.hl_range("ExceptionFilter" .. hl_type, { i - 1, 0 }, { i - 1, 4 })
39-
end
36+
for i, opt in ipairs(state.exceptions_options) do
37+
local hl_type = opt.enabled and "Enabled" or "Disabled"
38+
hl.hl_range("ExceptionFilter" .. hl_type, { i - 1, 0 }, { i - 1, 4 })
4039
end
4140
end
4241
end

lua/dap-view/options/settings.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ M.set_keymaps = function()
6161
local current_expr = state.watched_expressions[line]
6262

6363
vim.ui.input({ prompt = "Expression: ", default = current_expr }, function(input)
64-
if input and watches_actions.is_expr_valid(input) then
64+
if input then
6565
watches_actions.edit_watch_expr(input, line)
6666
end
6767
end)

lua/dap-view/options/winbar.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local winbar_info = {
99
keymap = "B",
1010
action = function()
1111
if vim.tbl_contains(setup.config.winbar.sections, "breakpoints") then
12-
require("dap-view.views").switch(require("dap-view.breakpoints.view").show)
12+
require("dap-view.views").switch_to_view(require("dap-view.breakpoints.view").show)
1313
end
1414
end,
1515
},
@@ -18,7 +18,7 @@ local winbar_info = {
1818
keymap = "E",
1919
action = function()
2020
if vim.tbl_contains(setup.config.winbar.sections, "exceptions") then
21-
require("dap-view.views").switch(require("dap-view.exceptions.view").show)
21+
require("dap-view.views").switch_to_view(require("dap-view.exceptions.view").show)
2222
end
2323
end,
2424
},
@@ -27,7 +27,7 @@ local winbar_info = {
2727
keymap = "W",
2828
action = function()
2929
if vim.tbl_contains(setup.config.winbar.sections, "watches") then
30-
require("dap-view.views").switch(require("dap-view.watches.view").show)
30+
require("dap-view.views").switch_to_view(require("dap-view.watches.view").show)
3131
end
3232
end,
3333
},
@@ -36,7 +36,7 @@ local winbar_info = {
3636
keymap = "T",
3737
action = function()
3838
if vim.tbl_contains(setup.config.winbar.sections, "threads") then
39-
require("dap-view.views").switch(require("dap-view.threads.view").show)
39+
require("dap-view.views").switch_to_view(require("dap-view.threads.view").show)
4040
end
4141
end,
4242
},

lua/dap-view/state.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
---@field exception_filter dap.ExceptionBreakpointsFilter
33
---@field enabled boolean
44

5+
---@class ThreadWithErr: dap.Thread
6+
---@field err? string
7+
58
---@class State
69
---@field bufnr? integer
710
---@field winnr? integer
@@ -11,7 +14,7 @@
1114
---@field last_active_adapter? string
1215
---@field current_section? SectionType
1316
---@field exceptions_options? ExceptionsOption[]
14-
---@field threads dap.Thread[]
17+
---@field threads ThreadWithErr[]
1518
---@field threads_err? string
1619
---@field frames_by_line {[number]: dap.StackFrame[]}
1720
---@field subtle_frames boolean

lua/dap-view/threads/init.lua

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,31 @@ M.get_threads = function()
1010
coroutine.wrap(function()
1111
local err, result = session:request("threads", {})
1212

13-
if err then
14-
state.threads_err = tostring(err)
15-
return
16-
end
17-
1813
state.threads_err = nil
1914

20-
if result then
15+
if err then
16+
state.threads_err = tostring(err)
17+
state.threads = {}
18+
elseif result then
2119
state.threads = result.threads
2220
end
2321

24-
M.get_stack_frames()
22+
M.get_stack_frames(session)
2523
end)()
2624
end
2725

28-
M.get_stack_frames = function()
29-
local session = assert(dap.session(), "has active session")
30-
26+
---@param session dap.Session
27+
M.get_stack_frames = function(session)
3128
for _, thread in pairs(state.threads) do
3229
coroutine.wrap(function()
3330
local err, result = session:request("stackTrace", { threadId = thread.id })
3431

35-
if err then
36-
-- TODO
37-
return
38-
end
32+
thread.err = nil
3933

40-
if result then
34+
if err then
35+
thread.err = tostring(err)
36+
thread.frames = {}
37+
elseif result then
4138
thread.frames = result.stackFrames
4239
end
4340
end)()

lua/dap-view/threads/view.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ M.show = function()
4848
:totable()
4949

5050
if vim.tbl_isempty(valid_frames) then
51+
if thread.err then
52+
api.nvim_buf_set_lines(state.bufnr, line - 1, line, true, { thread.err })
53+
line = line + 1
54+
end
5155
line = line + 1
5256
else
5357
local content = vim.iter(valid_frames):fold(

lua/dap-view/views/init.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ M.cleanup_view = function(cond, message)
2121
return cond
2222
end
2323

24-
local switch_to_dapview_buf = function()
25-
if not (state.winnr and api.nvim_win_is_valid(state.winnr)) then
24+
---@param callback fun(): nil
25+
M.switch_to_view = function(callback)
26+
if not state.bufnr or not state.winnr or not api.nvim_win_is_valid(state.winnr) then
2627
return
2728
end
28-
-- The REPL is actually another buffer
29+
30+
-- Update buffer if on REPL
2931
if state.current_section == "repl" then
3032
api.nvim_win_call(state.winnr, function()
3133
api.nvim_set_current_buf(state.bufnr)
3234
end)
3335
end
34-
end
3536

36-
---@param callback fun(): nil
37-
M.switch = function(callback)
38-
switch_to_dapview_buf()
3937
callback()
4038
end
4139

lua/dap-view/watches/actions.lua

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ local eval = require("dap-view.watches.eval")
55
local M = {}
66

77
---@param expr string
8-
M.is_expr_valid = function(expr)
8+
local is_expr_valid = function(expr)
99
-- Avoid duplicate expressions
1010
return #expr > 0 and not vim.tbl_contains(state.watched_expressions, expr)
1111
end
1212

1313
---@param expr string
14+
---@return boolean
1415
M.add_watch_expr = function(expr)
15-
if not M.is_expr_valid(expr) then
16-
return
17-
end
18-
19-
if not guard.expect_session() then
20-
return
16+
if not is_expr_valid(expr) or not guard.expect_session() then
17+
return false
2118
end
2219

2320
eval.eval_expr(expr, function(result)
2421
table.insert(state.expression_results, result)
2522
end)
2623

2724
table.insert(state.watched_expressions, expr)
25+
26+
return true
2827
end
2928

3029
---@param line number
@@ -36,7 +35,7 @@ end
3635
---@param expr string
3736
---@param line number
3837
M.edit_watch_expr = function(expr, line)
39-
if not guard.expect_session() then
38+
if not is_expr_valid(expr) or not guard.expect_session() then
4039
return
4140
end
4241

lua/dap-view/watches/eval.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@ M.eval_expr = function(expr, callback)
1616
-- TODO currently, we only check for variables reference for the top level expression
1717
-- It would be nice to expose functionality to let the user control the depth
1818
-- This could be a config parameter (eg, base depth) but could also extend with an action (BFS)
19-
local var_ref = result and result.variablesReference
20-
if var_ref and var_ref > 0 then
19+
local variables_reference = result and result.variablesReference
20+
if variables_reference and variables_reference > 0 then
2121
local enhanced_expr_result = { expr_result }
2222

2323
local var_ref_err, var_ref_result = session:request(
2424
"variables",
25-
{ variablesReference = var_ref, context = "watch", frameId = frame_id }
25+
{ variablesReference = variables_reference, context = "watch", frameId = frame_id }
2626
)
2727

2828
if var_ref_err then
2929
table.insert(enhanced_expr_result, tostring(err))
30-
end
31-
32-
if var_ref_result and not var_ref_err then
30+
elseif var_ref_result then
3331
for _, k in pairs(var_ref_result.variables) do
3432
table.insert(enhanced_expr_result, "\t" .. k.name .. " = " .. k.value)
3533
end

0 commit comments

Comments
 (0)