Skip to content

Commit 25d8ef1

Browse files
committed
fix(watches): improve cursor position restoration
1 parent 57f31df commit 25d8ef1

File tree

5 files changed

+22
-38
lines changed

5 files changed

+22
-38
lines changed

lua/dap-view/events.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ local dap = require("dap")
22

33
local state = require("dap-view.state")
44
local breakpoints = require("dap-view.breakpoints.view")
5-
local watches = require("dap-view.watches.view")
65
local scopes = require("dap-view.scopes.view")
76
local threads = require("dap-view.threads.view")
87
local exceptions = require("dap-view.exceptions.view")
@@ -68,7 +67,7 @@ end
6867

6968
dap.listeners.after.variables[SUBSCRIPTION_ID] = function()
7069
if state.current_section == "watches" then
71-
watches.show()
70+
require("dap-view.views").switch_to_view("watches")
7271
end
7372
end
7473

lua/dap-view/views/keymaps/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local state = require("dap-view.state")
22
local threads_view = require("dap-view.threads.view")
3-
local watches_view = require("dap-view.watches.view")
43
local watches_actions = require("dap-view.watches.actions")
54
local docs = require("dap-view.views.keymaps.docs")
65
local keymap = require("dap-view.views.keymaps.util").keymap
@@ -48,7 +47,7 @@ M.set_keymaps = function()
4847

4948
watches_actions.remove_watch_expr(cursor_line)
5049

51-
watches_view.show()
50+
require("dap-view.views").switch_to_view("watches")
5251
elseif state.current_section == "breakpoints" then
5352
require("dap-view.breakpoints.actions").remove_breakpoint()
5453

lua/dap-view/watches/actions.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local guard = require("dap-view.guard")
33
local eval = require("dap-view.watches.eval")
44
local set = require("dap-view.watches.set")
55
local traversal = require("dap-view.tree.traversal")
6-
local watches_view = require("dap-view.watches.view")
76

87
local M = {}
98

@@ -15,7 +14,7 @@ M.add_watch_expr = function(expr)
1514
end
1615

1716
eval.eval_expr(expr, function()
18-
watches_view.show()
17+
require("dap-view.views").switch_to_view("watches")
1918
end)
2019

2120
return true
@@ -118,7 +117,7 @@ M.edit_watch_expr = function(expr, line)
118117
M.remove_watch_expr(line)
119118

120119
eval.eval_expr(expr, function()
121-
watches_view.show()
120+
require("dap-view.views").switch_to_view("watches")
122121
end)
123122
end
124123

@@ -136,7 +135,7 @@ M.expand_or_collapse = function(line)
136135
e.expanded = not e.expanded
137136

138137
eval.eval_expr(expr.name, function()
139-
watches_view.show()
138+
require("dap-view.views").switch_to_view("watches")
140139
end)
141140
end
142141
else
@@ -150,6 +149,8 @@ M.expand_or_collapse = function(line)
150149
var_state.expanded = not var_state.expanded
151150
eval.expand_var(reference, var_state.children, function(result)
152151
var_state.children = result
152+
153+
require("dap-view.views").switch_to_view("watches")
153154
end)
154155
end
155156
else

lua/dap-view/watches/eval.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local watches = require("dap-view.watches.view")
21
local state = require("dap-view.state")
32

43
local M = {}
@@ -172,7 +171,7 @@ M.reeval = function()
172171
end
173172

174173
if state.current_section == "watches" then
175-
watches.show()
174+
require("dap-view.views").switch_to_view("watches")
176175
end
177176
end
178177

lua/dap-view/watches/view.lua

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,21 @@ M.show = function()
9191
return
9292
end
9393

94-
if state.bufnr then
95-
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
96-
97-
-- TODO this SHOULD NOT be necessary
98-
-- Refactor the `eval` code so I can have a callback with this function
99-
-- that triggers only when I'm done evaluating the whole tree
100-
views.cleanup_view(#state.watched_expressions == 0, "No expressions")
94+
-- Since variables aren't ordered, lines may change unexpectedly
95+
-- To handle that, always clear the storage table
96+
for k, _ in pairs(state.expressions_by_line) do
97+
state.expressions_by_line[k] = nil
98+
end
99+
-- Also clear variables for the same reason
100+
for k, _ in pairs(state.variables_by_line) do
101+
state.variables_by_line[k] = nil
102+
end
101103

102-
-- Since variables aren't ordered, lines may change unexpectedly
103-
-- To handle that, always clear the storage table
104-
for k, _ in pairs(state.expressions_by_line) do
105-
state.expressions_by_line[k] = nil
106-
end
107-
-- Also clear variables for the same reason
108-
for k, _ in pairs(state.variables_by_line) do
109-
state.variables_by_line[k] = nil
110-
end
104+
if views.cleanup_view(vim.tbl_isempty(state.watched_expressions), "No expressions") then
105+
return
106+
end
111107

108+
if state.bufnr then
112109
local line = 0
113110

114111
for expr_name, expr in pairs(state.watched_expressions) do
@@ -142,18 +139,7 @@ M.show = function()
142139
line = show_variables_or_err(line, expr)
143140
end
144141

145-
-- Workaround to reduce jankiness when redrawing
146-
if line > 0 then
147-
local content = {}
148-
if cursor_line > line then
149-
for i = 1, cursor_line - line do
150-
content[i] = ""
151-
end
152-
end
153-
api.nvim_buf_set_lines(state.bufnr, line, -1, true, content)
154-
155-
state.cur_pos["watches"] = api.nvim_win_set_cursor(state.winnr, { cursor_line, 1 })
156-
end
142+
api.nvim_buf_set_lines(state.bufnr, line, -1, true, {})
157143
end
158144
end
159145

0 commit comments

Comments
 (0)