Skip to content

Commit 7881908

Browse files
committed
feat: allow overriding switchbuf for a single jump
1 parent 94d35d1 commit 7881908

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

docs/src/routes/advanced-switchbuf/+page.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ For instance, with `useopen,newtab`, if the buffer is not found in the current t
1515
For more advanced use cases, you can write your own `switchbuf` function. It receives 2 arguments: the _current_ window number and the buffer number _to jump to_. It should (optionally) return a window number for the jump's destination window.
1616

1717
If a combination of options does not yield a valid window number for the destination (e.g., `useopen` but the buffer is hidden), there's a fallback to create a top-level split.
18+
19+
## Force Behavior
20+
21+
Sometimes, having a custom `switchbuf` is not enough. In these scenarios, you can override your own setting by using `<C-w><CR>` to bring a list with all available options, affecting only the current jump.

docs/src/routes/keymaps/+page.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ The help window itself has only 1 mapping: it can be closed with `q`.
4444
| **Help** |
4545
| `q` | Close |
4646

47+
Additionally, when jumping to a frame or breakpoint you can use `<C-w><CR>` to pick a specific [jump behavior](advanced-switchbuf#).
48+
4749
`nvim-dap-view` doesn't define any keybindings outside its own buffers: you have to create your own bindings to call `open`, `close` or `toggle` and other API [functions](api) (or [commands](commands)).
4850

4951
[^1]: Read `:help lua-pattern` to learn more. You can also read the full documentation [here](https://www.lua.org/pil/20.2.html).

lua/dap-view/threads/actions.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ local M = {}
88
local log = vim.log.levels
99

1010
---@param lnum number
11-
M.jump_or_noop = function(lnum)
11+
---@param switchbuffun? dapview.SwitchBufFun
12+
M.jump_and_set_frame = function(lnum, switchbuffun)
1213
local line = vim.fn.getline(".")
1314

1415
if string.find(line, "\t") then
15-
util.jump_to_location("^\t(.-)|(%d+)|")
16+
util.jump_to_location("^\t(.-)|(%d+)|", nil, switchbuffun)
1617

1718
local frame = state.frames_by_line[lnum]
1819
if frame then

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ M.show_help = function()
3434
" `o` Trigger actions",
3535
" `s` Set the value of an expression",
3636
"### Threads",
37-
"`<CR>` Jump to a frame",
37+
"`<CR>` Jump to a frame. Use `<C-w><CR>` to force a specific behavior.",
3838
" `t` Toggle subtle frames",
3939
" `f` Filter frames (via Lua patterns)",
4040
" `o` Omit results matching filter (invert filter)",
4141
"### Breakpoints",
42-
"`<CR>` Jump to a breakpoint",
42+
"`<CR>` Jump to a breakpoint. Use `<C-w><CR>` to force a specific behavior.",
4343
" `d` Delete a breakpoint",
4444
"### Watches",
4545
"`<CR>` Expand or collapse a variable",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ M.set_keymaps = function(buf)
2222
end, buf)
2323

2424
if not buf then
25-
require("dap-view.views.keymaps.views").views_keysmps()
25+
require("dap-view.views.keymaps.views").views_keymaps()
2626
end
2727

2828
keymap("g?", function()

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
local state = require("dap-view.state")
22
local watches_actions = require("dap-view.watches.actions")
3+
local setup = require("dap-view.setup")
34
local keymap = require("dap-view.views.keymaps.util").keymap
5+
local switchbuf = require("dap-view.views.windows.switchbuf")
46

57
local M = {}
68

79
local api = vim.api
810

9-
M.views_keysmps = function()
11+
M.views_keymaps = function()
1012
keymap("<CR>", function()
1113
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
1214

1315
if state.current_section == "breakpoints" then
1416
require("dap-view.views.util").jump_to_location("^(.-)|(%d+)|")
1517
elseif state.current_section == "threads" then
16-
require("dap-view.threads.actions").jump_or_noop(cursor_line)
18+
require("dap-view.threads.actions").jump_and_set_frame(cursor_line)
1719
elseif state.current_section == "exceptions" then
1820
require("dap-view.exceptions.actions").toggle_exception_filter()
1921
elseif state.current_section == "scopes" or state.current_section == "sessions" then
@@ -29,6 +31,42 @@ M.views_keysmps = function()
2931
end
3032
end)
3133

34+
keymap("<C-w><CR>", function()
35+
if state.current_section == "breakpoints" or state.current_section == "threads" then
36+
local options = vim.iter(switchbuf.switchbuf_winfn):fold({}, function(acc, k, v)
37+
acc[#acc + 1] = { label = k, cb = v }
38+
return acc
39+
end)
40+
41+
if type(setup.config.switchbuf) == "function" then
42+
options[#options + 1] = { label = "custom", cb = setup.config.switchbuf }
43+
end
44+
45+
local cursor_line = api.nvim_win_get_cursor(state.winnr)[1]
46+
47+
vim.ui.select(
48+
options,
49+
{
50+
prompt = "Specify jump behavior: ",
51+
---@param item {label: string}
52+
format_item = function(item)
53+
return item.label
54+
end,
55+
},
56+
---@param choice {label: string, cb: dapview.SwitchBufFun}?
57+
function(choice)
58+
if choice ~= nil then
59+
if state.current_section == "breakpoints" then
60+
require("dap-view.views.util").jump_to_location("^(.-)|(%d+)|", nil, choice.cb)
61+
elseif state.current_section == "threads" then
62+
require("dap-view.threads.actions").jump_and_set_frame(cursor_line, choice.cb)
63+
end
64+
end
65+
end
66+
)
67+
end
68+
end)
69+
3270
keymap("o", function()
3371
if state.current_section == "scopes" then
3472
require("dap.ui").trigger_actions()

lua/dap-view/views/util.lua

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ end
4242

4343
---@param pattern string
4444
---@param column? integer
45-
M.jump_to_location = function(pattern, column)
45+
---@param switchbuffun? dapview.SwitchBufFun
46+
M.jump_to_location = function(pattern, column, switchbuffun)
4647
local bufnr, line_num = unpack(M.get_bufnr(pattern) or {})
4748

4849
if bufnr == nil then
@@ -51,9 +52,16 @@ M.jump_to_location = function(pattern, column)
5152

5253
local config = setup.config
5354

54-
local win = window.get_win_respecting_switchbuf(config.switchbuf, bufnr)
55+
---@type integer?
56+
local win
5557

56-
if not win then
58+
if switchbuffun then
59+
win = window.get_win_respecting_switchbuf(switchbuffun, bufnr)
60+
else
61+
win = window.get_win_respecting_switchbuf(config.switchbuf, bufnr)
62+
end
63+
64+
if win == nil then
5765
local windows = config.windows
5866

5967
win = api.nvim_open_win(0, true, {

0 commit comments

Comments
 (0)