Skip to content

Commit 94d35d1

Browse files
committed
feat: allow switchbuf to be a function
1 parent 0158221 commit 94d35d1

File tree

9 files changed

+42
-10
lines changed

9 files changed

+42
-10
lines changed

docs/sidebar.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export const sidebar = {
5353
title: 'Recipes',
5454
collapsible: true,
5555
items: [
56+
{
57+
title: "Advanced 'switchbuf'",
58+
to: '/advanced-switchbuf'
59+
},
5660
{
5761
title: 'Hide Terminal',
5862
to: '/hide-terminal'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Advanced 'switchbuf'
3+
category: Recipes
4+
---
5+
6+
You can tune the behavior of nvim-dap-view's jump to breakpoint and frame features, by using nvim-dap-view's own `switchbuf` config option. Similarly to the built-in option, you can combine different behaviors with a comma separated list. Available options include:
7+
8+
- `newtab`: always creates a new tab
9+
- `useopen`: tries to find the buffer to jump to _only_ in the current tab
10+
- `usetab`: like `useopen`, but searches _every_ tab (**default**)
11+
- `uselast`: jump to the previously used window, if eligible
12+
13+
For instance, with `useopen,newtab`, if the buffer is not found in the current tab, a new tab is created.
14+
15+
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.
16+
17+
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.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ return {
138138
border = nil,
139139
},
140140
-- Controls how to jump when selecting a breakpoint or navigating the stack
141+
-- Comma separated list, like the built-in 'switchbuf'. See :help 'switchbuf'
142+
-- Only a subset of the options is available: newtab, useopen, usetab and uselast
143+
-- Can also be a function that takes the current winnr and the bufnr that will jumped to
144+
-- If a function, should return the winnr of the destination window
141145
switchbuf = "usetab",
142146
-- Auto open when a session is started and auto close when all sessions finish
143147
auto_toggle = false,

lua/dap-view/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ local M = {}
7575
---@field windows dapview.WindowsConfig
7676
---@field help dapview.HelpConfig
7777
---@field icons dapview.IconsConfig Icons for each button
78-
---@field switchbuf string Control how to jump when selecting a breakpoint or a call in the stack
78+
---@field switchbuf string|dapview.SwitchBufFun Control how to jump when selecting a breakpoint or a call in the stack
7979
---@field auto_toggle boolean|"keep_terminal"
8080
---@field follow_tab boolean Reopen dapview when switching tabs
8181

lua/dap-view/setup/validate/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function M.validate(config)
66
windows = { config.windows, "table" },
77
winbar = { config.winbar, "table" },
88
help = { config.help, "table" },
9-
switchbuf = { config.switchbuf, "string" },
9+
switchbuf = { config.switchbuf, { "string", "function" } },
1010
icons = { config.icons, "table" },
1111
auto_toggle = { config.auto_toggle, { "boolean", "string" } },
1212
follow_tab = { config.follow_tab, "boolean" },

lua/dap-view/util/exprs.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ M.get_trimmed_selection = function()
2929
end
3030

3131
-- EmmyLuaLs is not smart enough to infer that #lines > 1
32-
-- It alsoe does not care about assertting that #lines > 1
32+
-- It also does not care about asserting that #lines > 1
3333
lines[1] = string.sub(lines[1], start_col)
3434
lines[#lines] = string.sub(lines[#lines], 1, finish_col)
3535

lua/dap-view/views/util.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ M.jump_to_location = function(pattern, column)
5151

5252
local config = setup.config
5353

54-
local switchbufopt = config.switchbuf
55-
local win = window.get_win_respecting_switchbuf(switchbufopt, bufnr)
54+
local win = window.get_win_respecting_switchbuf(config.switchbuf, bufnr)
5655

5756
if not win then
57+
local windows = config.windows
58+
5859
win = api.nvim_open_win(0, true, {
59-
split = util.inverted_directions[config.windows.position],
60+
split = util.inverted_directions[windows.position],
6061
win = -1,
61-
height = config.windows.height < 1 and math.floor(vim.go.lines * (1 - config.windows.height))
62-
or math.floor(vim.go.lines - config.windows.height),
62+
height = windows.height < 1 and math.floor(vim.go.lines * (1 - windows.height))
63+
or math.floor(vim.go.lines - windows.height),
6364
})
6465
end
6566

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ local M = {}
44

55
local api = vim.api
66

7-
---@param switchbufopt string
7+
---@param switchbufopt string|dapview.SwitchBufFun
88
---@param bufnr integer
99
M.get_win_respecting_switchbuf = function(switchbufopt, bufnr)
1010
local winnr = api.nvim_get_current_win()
1111

1212
local switchbuf_winfn = switchbuf.switchbuf_winfn
1313

14+
if type(switchbufopt) == "function" then
15+
return switchbufopt(bufnr, winnr)
16+
end
17+
1418
if switchbufopt:find("usetab") then
1519
switchbuf_winfn.useopen = switchbuf_winfn.usetab
1620
end

lua/dap-view/views/windows/switchbuf.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ local M = {}
22

33
local api = vim.api
44

5-
---@type table<string, fun(bufnr: integer, winnr: integer): integer?>
5+
---@alias dapview.SwitchBufFun fun(bufnr: integer, winnr: integer): (integer?)
6+
7+
---@type table<string, dapview.SwitchBufFun>
68
M.switchbuf_winfn = {}
79

810
M.switchbuf_winfn.newtab = function(bufnr)

0 commit comments

Comments
 (0)