Skip to content

Commit 30e3fb3

Browse files
committed
feat: Override display_* on a per-filetype basis - closes #135
1 parent 95d2a3e commit 30e3fb3

File tree

5 files changed

+152
-15
lines changed

5 files changed

+152
-15
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ the NeoVim generation. It:
2727

2828
* Includes reference information in each 'print line' such as file names, line
2929
numbers, a counter, and snippets of other lines to make it easier to
30-
cross-reference them in output (each of these can be [optionally disabled](#other-options)).
30+
cross-reference them in output (each of these can be optionally
31+
disabled [globally](#other-options) or on a per-filetype basis).
3132

3233
* Can output the value of variables (or in some cases, expressions).
3334

SHOWCASE.md

+14
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ return {
8787
}
8888
```
8989

90+
## Setting `display_*` options on per-filetype basis
91+
92+
The three `display_*` options [supported on a global basis](README.md#other-options) by `debugprint` can also be overridden on a per-filetype basis so you can show and hide differently for different filetypes. Filetypes without these set (which is the default for all filetypes) will continue to use the values set globally. Pass them into the `setup()` method or the `add_custom_filetypes()` method like this:
93+
94+
```lua
95+
require('debugprint').setup({ filetypes = { ["filetype"] = { display_counter = false }}})
96+
```
97+
98+
or
99+
100+
```lua
101+
require('debugprint').add_custom_filetypes({ ["filetype"] = { display_counter = false }, … })
102+
```
103+
90104
## Use lazy-loading with lazy.nvim
91105

92106
`debugprint` can be configured, when using [lazy.nvim](https://github.com/folke/lazy.nvim) as a plugin manager, to lazy load itself. Use configuration that looks like this:

lua/debugprint/init.lua

+45-14
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,87 @@ local default_display_counter = function()
1414
return "[" .. tostring(default_counter) .. "]"
1515
end
1616

17+
---@param display_counter? boolean|function
1718
---@return string
18-
local get_debugline_tag_and_counter = function()
19+
local get_debugline_tag_and_counter = function(display_counter)
1920
local tag_and_counter = ""
2021

2122
if global_opts.print_tag then
2223
tag_and_counter = global_opts.print_tag
2324
end
2425

25-
if global_opts.display_counter == true then
26+
if display_counter == true then
2627
tag_and_counter = tag_and_counter .. default_display_counter()
27-
elseif type(global_opts.display_counter) == "function" then
28-
tag_and_counter = tag_and_counter
29-
.. tostring(global_opts.display_counter())
28+
elseif type(display_counter) == "function" then
29+
tag_and_counter = tag_and_counter .. tostring(display_counter())
3030
end
3131

3232
return tag_and_counter
3333
end
3434

35+
---@param fileconfig DebugprintFileTypeConfig
36+
---@return function|boolean?, boolean?, boolean?
37+
local get_display_options = function(fileconfig)
38+
local display_counter
39+
if fileconfig.display_counter ~= nil then
40+
display_counter = fileconfig.display_counter
41+
else
42+
display_counter = global_opts.display_counter
43+
end
44+
45+
local display_location
46+
if fileconfig.display_location ~= nil then
47+
display_location = fileconfig.display_location
48+
else
49+
display_location = global_opts.display_location
50+
end
51+
52+
local display_snippet
53+
if fileconfig.display_snippet ~= nil then
54+
display_snippet = fileconfig.display_snippet
55+
else
56+
display_snippet = global_opts.display_snippet
57+
end
58+
59+
return display_counter, display_location, display_snippet
60+
end
61+
3562
---@param opts DebugprintFunctionOptionsInternal
63+
---@param fileconfig DebugprintFileTypeConfig
3664
---@return string
37-
local get_debugline_textcontent = function(opts)
65+
local get_debugline_textcontent = function(opts, fileconfig)
3866
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1]
3967

4068
local line_components = {}
4169
local force_snippet_for_plain = false
4270

71+
local display_counter, display_location, display_snippet =
72+
get_display_options(fileconfig)
73+
4374
if
44-
not global_opts.display_location
45-
and not global_opts.display_snippet
46-
and not global_opts.display_counter
75+
not display_location
76+
and not display_snippet
77+
and not display_counter
4778
and global_opts.print_tag == ""
4879
then
4980
force_snippet_for_plain = true
5081
end
5182

52-
local tag_and_counter = get_debugline_tag_and_counter()
83+
local tag_and_counter = get_debugline_tag_and_counter(display_counter)
5384

5485
if tag_and_counter ~= "" then
5586
table.insert(line_components, tag_and_counter .. ":")
5687
end
5788

58-
if global_opts.display_location then
89+
if display_location then
5990
table.insert(
6091
line_components,
6192
vim.fn.expand("%:t") .. ":" .. current_line_nr
6293
)
6394
end
6495

6596
if
66-
(global_opts.display_snippet or force_snippet_for_plain)
97+
(display_snippet or force_snippet_for_plain)
6798
and opts.variable_name == nil
6899
then
69100
local snippet = utils.get_snippet(current_line_nr, opts.above)
@@ -115,14 +146,14 @@ local construct_debugprint_line = function(opts, fileconfig)
115146
end
116147

117148
line_to_insert = left
118-
.. get_debugline_textcontent(opts)
149+
.. get_debugline_textcontent(opts, fileconfig)
119150
.. fileconfig.mid_var
120151
.. opts.variable_name
121152
.. fileconfig.right_var
122153
else
123154
opts.variable_name = nil
124155
line_to_insert = fileconfig.left
125-
.. get_debugline_textcontent(opts)
156+
.. get_debugline_textcontent(opts, fileconfig)
126157
.. fileconfig.right
127158
end
128159

lua/debugprint/types.lua

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
---@field mid_var string
99
---@field right_var string
1010
---@field find_treesitter_variable? function
11+
---@field display_counter? boolean|function
12+
---@field display_location? boolean
13+
---@field display_snippet? boolean
1114

1215
---@class DebugprintGlobalOptions
1316
---@field keymaps? DebugprintKeymapOptions

tests/debugprint.lua

+88
Original file line numberDiff line numberDiff line change
@@ -2324,3 +2324,91 @@ describe("variations of display_* options", function()
23242324
})
23252325
end)
23262326
end)
2327+
2328+
describe("allow display_* to be set in filetypes", function()
2329+
after_each(teardown)
2330+
2331+
it("display_counter", function()
2332+
debugprint.setup({ filetypes = { bash = { display_counter = false } } })
2333+
2334+
local lua_filename = init_file({
2335+
"foo",
2336+
"bar",
2337+
}, "lua", 1, 0)
2338+
2339+
feedkeys("g?p")
2340+
2341+
check_lines({
2342+
"foo",
2343+
"print('DEBUGPRINT[1]: " .. lua_filename .. ":1 (after foo)')",
2344+
"bar",
2345+
})
2346+
2347+
local sh_filename = init_file({
2348+
"XYZ=123",
2349+
}, "bash", 1, 1)
2350+
2351+
feedkeys("g?v")
2352+
2353+
check_lines({
2354+
"XYZ=123",
2355+
'>&2 echo "DEBUGPRINT: ' .. sh_filename .. ':1: XYZ=${XYZ}"',
2356+
})
2357+
end)
2358+
2359+
it("display_location", function()
2360+
debugprint.setup({ filetypes = { lua = { display_location = false } } })
2361+
2362+
local lua_filename = init_file({
2363+
"foo",
2364+
"bar",
2365+
}, "lua", 1, 0)
2366+
2367+
feedkeys("g?p")
2368+
2369+
check_lines({
2370+
"foo",
2371+
"print('DEBUGPRINT[1]: (after foo)')",
2372+
"bar",
2373+
})
2374+
2375+
local sh_filename = init_file({
2376+
"XYZ=123",
2377+
}, "bash", 1, 1)
2378+
2379+
feedkeys("g?v")
2380+
2381+
check_lines({
2382+
"XYZ=123",
2383+
'>&2 echo "DEBUGPRINT[2]: ' .. sh_filename .. ':1: XYZ=${XYZ}"',
2384+
})
2385+
end)
2386+
2387+
it("display_snippet", function()
2388+
debugprint.setup({ filetypes = { lua = { display_snippet = false } } })
2389+
2390+
local lua_filename = init_file({
2391+
"foo",
2392+
"bar",
2393+
}, "lua", 1, 0)
2394+
2395+
feedkeys("g?p")
2396+
2397+
check_lines({
2398+
"foo",
2399+
"print('DEBUGPRINT[1]: " .. lua_filename .. ":1')",
2400+
"bar",
2401+
})
2402+
2403+
local sh_filename = init_file({
2404+
"XYZ=123",
2405+
}, "bash", 1, 1)
2406+
2407+
feedkeys("g?p")
2408+
2409+
check_lines({
2410+
"XYZ=123",
2411+
'>&2 echo "DEBUGPRINT[2]: ' .. sh_filename .. ':1 (after XYZ=123)"',
2412+
})
2413+
end)
2414+
end)

0 commit comments

Comments
 (0)