Skip to content

Commit b3bc749

Browse files
committed
feat(breakpoints): custom formatting
1 parent 265e5cc commit b3bc749

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

docs/src/routes/custom-formatting/+page.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Custom Formatting
33
category: Recipes
44
---
55

6-
You can control how the text is displayed for some views, using special `format` functions. Each customizable view has its own parameters, but the expected return type is the same: an array of `{part: string, hl?: string}`. The `part` is the "content" itself, and `hl` is one of `nvim-dap-view`'s [highlight groups](./highlight-groups) (without the `NvimDapView` prefix).
6+
You can control how the text is displayed for some views, using special `format` functions. Each customizable view has its own parameters, but the expected return type is the same: an array of `{part: string, hl?: string, separator?: string}`. The `part` is the "content" itself, `hl` is one of `nvim-dap-view`'s [highlight groups](./highlight-groups) (without the `NvimDapView` prefix) and the separator can be used to customize the divider between the current part and the next one.
77

88
## Threads
99

@@ -47,3 +47,25 @@ return {
4747
},
4848
}
4949
```
50+
51+
## Breakpoints
52+
53+
Besides the path (and line number) to the breakpoint, one can also manipulate the content of the line itself. It can be highlighted with treesitter by using the specifying `hl` as `true`.
54+
55+
### Example
56+
57+
```lua
58+
return {
59+
render = {
60+
breakpoints = {
61+
-- The line number HATER
62+
format = function(line, _, path)
63+
return {
64+
{ part = path, hl = "FileName" },
65+
{ part = line, hl = true },
66+
}
67+
end,
68+
},
69+
},
70+
}
71+
```

lua/dap-view/breakpoints/view.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ local state = require("dap-view.state")
22
local vendor = require("dap-view.breakpoints.vendor")
33
local extmarks = require("dap-view.breakpoints.util.extmarks")
44
local treesitter = require("dap-view.breakpoints.util.treesitter")
5+
local setup = require("dap-view.setup")
56
local views = require("dap-view.views")
67
local util = require("dap-view.util")
78
local hl = require("dap-view.util.hl")
89

910
local M = {}
1011

12+
---@class dapview.Breakpoint
13+
---@field path string
14+
---@field lnum string
15+
---@field line string
16+
1117
local api = vim.api
1218

1319
M.show = function()
@@ -37,11 +43,7 @@ M.show = function()
3743
local buf_lines = api.nvim_buf_get_lines(buf, entry.lnum - 1, entry.lnum, true)
3844
local text = table.concat(buf_lines, "\n")
3945

40-
local parts = {
41-
{ part = relative_path, hl = "FileName" },
42-
{ part = tostring(entry.lnum), hl = "LineNumber" },
43-
{ part = text, hl = true },
44-
}
46+
local parts = setup.config.render.breakpoints.format(text, tostring(entry.lnum), relative_path)
4547

4648
table.insert(state.breakpoint_paths_by_line, relative_path)
4749
table.insert(state.breakpoint_lines_by_line, entry.lnum)
@@ -62,6 +64,7 @@ M.show = function()
6264
local hl_end = hl_init + #p.part
6365

6466
if type(p.hl) == "string" then
67+
---@cast p {hl: string}
6568
hl.hl_range(p.hl, { line, hl_init }, { line, hl_end })
6669
else
6770
treesitter.copy_highlights(buf, entry.lnum - 1, line, hl_init)

lua/dap-view/config.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ local M = {}
7373
---@class dapview.HelpConfig
7474
---@field border? string|string[] Override `winborder` in the help window
7575

76+
---@class dapview.RenderBreakpointsConfig
77+
---@field format fun(line: string, lnum: string, path: string): dapview.Content[]
78+
7679
---@class dapview.RenderThreadsConfig
7780
---@field format fun(name: string, lnum: string, path: string): dapview.Content[]
7881

7982
---@class dapview.RenderConfig
8083
---@field sort_variables? fun(lhs: dap.Variable, rhs: dap.Variable): boolean Override order of variables
8184
---@field threads dapview.RenderThreadsConfig
85+
---@field breakpoints dapview.RenderBreakpointsConfig
8286

8387
---@class (exact) dapview.ConfigStrict
8488
---@field winbar dapview.WinbarConfig
@@ -193,6 +197,15 @@ M.config = {
193197
}
194198
end,
195199
},
200+
breakpoints = {
201+
format = function(line, lnum, path)
202+
return {
203+
{ part = path, hl = "FileName" },
204+
{ part = lnum, hl = "LineNumber" },
205+
{ part = line, hl = true },
206+
}
207+
end,
208+
},
196209
},
197210
switchbuf = "usetab,uselast",
198211
auto_toggle = false,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ function M.validate(config)
77
validate("render", {
88
sort_variables = { config.sort_variables, { "function", "nil" } },
99
threads = { config.threads, { "table" } },
10+
breakpoints = { config.breakpoints, { "table" } },
1011
}, config)
1112

1213
local threads = config.threads
1314
validate("render.threads", {
1415
format = { threads.format, { "function" } },
1516
}, threads)
17+
18+
local breakpoints = config.breakpoints
19+
validate("render.breakpoints", {
20+
format = { breakpoints.format, { "function" } },
21+
}, breakpoints)
1622
end
1723

1824
return M

lua/dap-view/types.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
---@field base_sections? table<dapview.Section,dapview.SectionConfigPartial>
1414
---@field controls? dapview.ControlsConfigPartial
1515

16+
---@class dapview.RenderBreakpointsConfigPartial : dapview.RenderBreakpointsConfig, {}
17+
1618
---@class dapview.RenderThreadsConfigPartial : dapview.RenderThreadsConfig, {}
1719

1820
---@class dapview.RenderConfigPartial : dapview.RenderConfig, {}
1921
---@field threads? dapview.RenderThreadsConfigPartial
22+
---@field breakpoints? dapview.RenderBreakpointsConfigPartial
2023

2124
---@class dapview.HelpConfigPartial : dapview.HelpConfig, {}
2225

0 commit comments

Comments
 (0)