Skip to content

Commit e2adb2a

Browse files
committed
feat: restore cursor position per view
1 parent 995f5c6 commit e2adb2a

File tree

11 files changed

+56
-52
lines changed

11 files changed

+56
-52
lines changed

docs/src/posts/highlight-groups.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Highlight Groups
33
---
44

5-
`nvim-dap-view` defines 28 highlight groups linked to (somewhat) reasonable defaults, but they may look odd with your colorscheme. If the links aren't defined, no highlighting will be applied. To fix that, you have to manually define the highlight groups (see `:h nvim_set_hl()`). Consider contributing to your colorscheme by sending a PR to add support to `nvim-dap-view`!
5+
`nvim-dap-view` defines 29 highlight groups linked to (somewhat) reasonable defaults, but they may look odd with your colorscheme. If the links aren't defined, no highlighting will be applied. To fix that, you have to manually define the highlight groups (see `:h nvim_set_hl()`). Consider contributing to your colorscheme by sending a PR to add support to `nvim-dap-view`!
66

77
| Highlight Group | Default Link |
88
| ------------------------------------ | ------------------------- |
@@ -29,6 +29,7 @@ title: Highlight Groups
2929
| `NvimDapViewString` | String |
3030
| `NvimDapViewTabSelected` | TabLineSel |
3131
| `NvimDapViewTab` | TabLine |
32+
| `NvimDapViewThreadError` | DiagnosticError |
3233
| `NvimDapViewThreadStopped` | Conditional |
3334
| `NvimDapViewThread` | Tag |
3435
| `NvimDapViewWatchError` | DiagnosticError |

lua/dap-view/actions.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ end
8787
M.add_expr = function(expr)
8888
local final_expr = expr or require("dap-view.util.exprs").get_current_expr()
8989
if require("dap-view.watches.actions").add_watch_expr(final_expr) then
90-
require("dap-view.views").switch_to_view(require("dap-view.watches.view").show)
90+
require("dap-view.views").switch_to_view("watches")
9191
end
9292
end
9393

lua/dap-view/autocmds.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local state = require("dap-view.state")
2+
local globals = require("dap-view.globals")
23
local winbar = require("dap-view.options.winbar")
34

45
local api = vim.api
@@ -69,3 +70,10 @@ api.nvim_create_autocmd("BufEnter", {
6970
end
7071
end,
7172
})
73+
74+
api.nvim_create_autocmd("CursorMoved", {
75+
pattern = globals.MAIN_BUF_NAME,
76+
callback = function()
77+
state.cur_pos[state.current_section] = api.nvim_win_get_cursor(state.winnr)[1]
78+
end,
79+
})

lua/dap-view/breakpoints/view.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local winbar = require("dap-view.options.winbar")
21
local state = require("dap-view.state")
32
local vendor = require("dap-view.breakpoints.vendor")
43
local extmarks = require("dap-view.breakpoints.util.extmarks")
@@ -11,8 +10,6 @@ local M = {}
1110
local api = vim.api
1211

1312
M.show = function()
14-
winbar.update_section("breakpoints")
15-
1613
if state.bufnr and state.winnr then
1714
local breakpoints = vendor.get()
1815

@@ -22,8 +19,6 @@ M.show = function()
2219
return
2320
end
2421

25-
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
26-
2722
for buf, buf_entries in pairs(breakpoints) do
2823
local filename = api.nvim_buf_get_name(buf)
2924
local relative_path = vim.fn.fnamemodify(filename, ":.")
@@ -47,10 +42,6 @@ M.show = function()
4742
end
4843
end
4944

50-
if line > 0 then
51-
api.nvim_win_set_cursor(state.winnr, { math.min(cursor_line, line), 1 })
52-
end
53-
5445
-- Clear previous content
5546
api.nvim_buf_set_lines(state.bufnr, line, -1, true, {})
5647
end

lua/dap-view/exceptions/view.lua

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

3-
local winbar = require("dap-view.options.winbar")
43
local state = require("dap-view.state")
54
local views = require("dap-view.views")
65
local hl = require("dap-view.util.hl")
@@ -10,11 +9,7 @@ local M = {}
109
local api = vim.api
1110

1211
M.show = function()
13-
winbar.update_section("exceptions")
14-
1512
if state.bufnr and state.winnr then
16-
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
17-
1813
if views.cleanup_view(not dap.session(), "No active session") then
1914
return
2015
end
@@ -38,8 +33,6 @@ M.show = function()
3833
local hl_type = opt.enabled and "Enabled" or "Disabled"
3934
hl.hl_range("ExceptionFilter" .. hl_type, { i - 1, 0 }, { i - 1, 4 })
4035
end
41-
42-
api.nvim_win_set_cursor(state.winnr, { math.min(cursor_line, #content), 1 })
4336
end
4437
end
4538

lua/dap-view/highlight.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ local define_base_links = function()
1313
hl_create("FileName", "qfFileName")
1414
hl_create("LineNumber", "qfLineNr")
1515
hl_create("Separator", "Comment")
16+
1617
hl_create("Thread", "Tag")
1718
hl_create("ThreadStopped", "Conditional")
19+
hl_create("ThreadError", "DiagnosticError")
1820

1921
hl_create("ExceptionFilterEnabled", "DiagnosticOk")
2022
hl_create("ExceptionFilterDisabled", "DiagnosticError")

lua/dap-view/options/winbar.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,39 @@ local winbar_info = {
1313
keymap = "B",
1414
action = function()
1515
if vim.tbl_contains(setup.config.winbar.sections, "breakpoints") then
16-
require("dap-view.views").switch_to_view(require("dap-view.breakpoints.view").show)
16+
require("dap-view.views").switch_to_view("breakpoints")
1717
end
1818
end,
1919
},
2020
scopes = {
2121
keymap = "S",
2222
action = function()
2323
if vim.tbl_contains(setup.config.winbar.sections, "scopes") then
24-
require("dap-view.views").switch_to_view(require("dap-view.scopes.view").show)
24+
require("dap-view.views").switch_to_view("scopes")
2525
end
2626
end,
2727
},
2828
exceptions = {
2929
keymap = "E",
3030
action = function()
3131
if vim.tbl_contains(setup.config.winbar.sections, "exceptions") then
32-
require("dap-view.views").switch_to_view(require("dap-view.exceptions.view").show)
32+
require("dap-view.views").switch_to_view("exceptions")
3333
end
3434
end,
3535
},
3636
watches = {
3737
keymap = "W",
3838
action = function()
3939
if vim.tbl_contains(setup.config.winbar.sections, "watches") then
40-
require("dap-view.views").switch_to_view(require("dap-view.watches.view").show)
40+
require("dap-view.views").switch_to_view("watches")
4141
end
4242
end,
4343
},
4444
threads = {
4545
keymap = "T",
4646
action = function()
4747
if vim.tbl_contains(setup.config.winbar.sections, "threads") then
48-
require("dap-view.views").switch_to_view(require("dap-view.threads.view").show)
48+
require("dap-view.views").switch_to_view("threads")
4949
end
5050
end,
5151
},

lua/dap-view/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
---@field expressions_by_line table<integer, {name: string, expression: dapview.ExpressionPack}>
3838
---@field variables_by_line table<integer, {response: dap.Variable, reference: number}>
3939
---@field watched_expressions table<string, dapview.ExpressionPack>
40+
---@field cur_pos table<dapview.SectionType,integer?>
4041
local M = {
4142
exceptions_options = {},
4243
threads = {},
@@ -45,6 +46,7 @@ local M = {
4546
variables_by_line = {},
4647
subtle_frames = false,
4748
watched_expressions = {},
49+
cur_pos = {},
4850
}
4951

5052
return M

lua/dap-view/threads/view.lua

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

3-
local winbar = require("dap-view.options.winbar")
43
local views = require("dap-view.views")
54
local state = require("dap-view.state")
65
local hl = require("dap-view.util.hl")
@@ -10,8 +9,6 @@ local M = {}
109
local api = vim.api
1110

1211
M.show = function()
13-
winbar.update_section("threads")
14-
1512
if state.bufnr and state.winnr then
1613
local session = dap.session()
1714
-- Redundant check to appease the type checker
@@ -31,15 +28,17 @@ M.show = function()
3128
state.frames_by_line[k] = nil
3229
end
3330

34-
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
35-
3631
local line = 0
37-
for _, thread in pairs(state.threads) do
38-
api.nvim_buf_set_lines(state.bufnr, line, line, true, { thread.name })
3932

33+
for _, thread in pairs(state.threads) do
4034
local is_stopped_thread = session.stopped_thread_id == thread.id
35+
local thread_name = is_stopped_thread and thread.name .. "" or thread.name
36+
api.nvim_buf_set_lines(state.bufnr, line, line, true, { thread_name })
37+
4138
hl.hl_range(is_stopped_thread and "ThreadStopped" or "Thread", { line, 0 }, { line, -1 })
4239

40+
line = line + 1
41+
4342
local valid_frames = vim.iter(thread.frames or {})
4443
:filter(
4544
---@param f dap.StackFrame
@@ -54,9 +53,9 @@ M.show = function()
5453
if vim.tbl_isempty(valid_frames) then
5554
if thread.err then
5655
api.nvim_buf_set_lines(state.bufnr, line, line, true, { thread.err })
56+
hl.hl_range("ThreadError", { line, 0 }, { line, -1 })
5757
line = line + 1
5858
end
59-
line = line + 1
6059
else
6160
local content = vim.iter(valid_frames):fold(
6261
{},
@@ -75,7 +74,6 @@ M.show = function()
7574
return acc
7675
end
7776
)
78-
line = line + 1
7977

8078
api.nvim_buf_set_lines(state.bufnr, line, line + #content, false, content)
8179

@@ -92,10 +90,6 @@ M.show = function()
9290
end
9391
end
9492

95-
if line > 0 then
96-
api.nvim_win_set_cursor(state.winnr, { math.min(cursor_line, line), 1 })
97-
end
98-
9993
-- Clear previous content
10094
api.nvim_buf_set_lines(state.bufnr, line, -1, true, {})
10195
end

lua/dap-view/views/init.lua

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
local state = require("dap-view.state")
2+
local winbar = require("dap-view.options.winbar")
23
local hl = require("dap-view.util.hl")
34

45
local M = {}
56

67
local api = vim.api
78

8-
---@param cond boolean
9+
---@param condition boolean
910
---@param message string
10-
M.cleanup_view = function(cond, message)
11-
if cond and state.winnr then
11+
M.cleanup_view = function(condition, message)
12+
assert(state.winnr ~= nil, "has nvim-dap-view window")
13+
14+
if condition then
1215
vim.wo[state.winnr][0].cursorline = false
1316

1417
api.nvim_buf_set_lines(state.bufnr, 0, -1, false, { message })
1518

1619
hl.hl_range("MissingData", { 0, 0 }, { 0, #message })
17-
elseif state.winnr then
20+
else
1821
vim.wo[state.winnr][0].cursorline = true
1922
end
2023

21-
return cond
24+
return condition
2225
end
2326

24-
---@param callback fun(): nil
25-
M.switch_to_view = function(callback)
27+
---@param view dapview.SectionType
28+
M.switch_to_view = function(view)
2629
if not state.bufnr or not state.winnr or not api.nvim_win_is_valid(state.winnr) then
2730
return
2831
end
2932

33+
local cursor_line = state.cur_pos[view] or 1
34+
3035
if vim.tbl_contains({ "repl", "console" }, state.current_section) then
3136
api.nvim_win_call(state.winnr, function()
3237
api.nvim_set_current_buf(state.bufnr)
3338
end)
3439
end
3540

36-
callback()
41+
winbar.update_section(view)
42+
43+
require("dap-view." .. view .. ".view").show()
44+
45+
local buf_len = api.nvim_buf_line_count(state.bufnr)
46+
state.cur_pos[view] = math.min(cursor_line, buf_len)
47+
48+
api.nvim_win_set_cursor(state.winnr, { state.cur_pos[view], 1 })
3749
end
3850

3951
return M

0 commit comments

Comments
 (0)