Skip to content

Commit 1400e08

Browse files
committed
feat: Customizable display_counter - closes #104
1 parent c7d04c3 commit 1400e08

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

README.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,31 @@ reason, please open an
200200

201201
`debugprint` supports the following options in its global `opts` object:
202202

203-
| Option | Default | Purpose |
204-
| ------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
205-
| `move_to_debugline` | `false` | When adding a debug line, moves the cursor to that line |
206-
| `display_counter` | `true` | Whether to display/include the monotonically increasing counter in each debug message |
207-
| `display_snippet` | `true` | Whether to include a snippet of the line above/below in plain debug lines |
208-
| `filetypes` | See below | Custom filetypes - see below |
209-
| `print_tag` | `DEBUGPRINT` | The string inserted into each print statement, which can be used to uniquely identify statements inserted by `debugprint`. |
203+
| Option | Default | Purpose |
204+
| ------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
205+
| `move_to_debugline` | `false` | When adding a debug line, moves the cursor to that line |
206+
| `display_counter` | `true` | Whether to display/include the increasing integer counter in each debug message. Can also be set to a function to customize, see below |
207+
| `display_snippet` | `true` | Whether to include a snippet of the line above/below in plain debug lines |
208+
| `filetypes` | See below | Custom filetypes - see below |
209+
| `print_tag` | `DEBUGPRINT` | The string inserted into each print statement, which can be used to uniquely identify statements inserted by `debugprint`. |
210+
211+
### Customizing Counter Logic
212+
213+
`display_counter` can also be set to a custom callback function to implement
214+
custom counter logic. In this case you are responsible for implementing your own
215+
counter. For example, this logic will implement essentially the same as the
216+
default counter:
217+
218+
```lua
219+
local counter = 0
220+
221+
local counter_func = function()
222+
counter = counter + 1
223+
return '[' .. tostring(counter) .. ']'
224+
end
225+
226+
debugprint.setup({display_counter = counter_func})
227+
```
210228

211229
## Add Custom Filetypes
212230

lua/debugprint/init.lua

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local M = {}
33
local utils = require("debugprint.utils")
44

55
local global_opts
6-
local counter = 0
6+
local default_counter = 0
77

88
MAX_SNIPPET_LENGTH = 40
99

@@ -44,15 +44,20 @@ local get_snippet = function(current_line, above)
4444
return line_contents
4545
end
4646

47+
local default_display_counter = function()
48+
default_counter = default_counter + 1
49+
return "[" .. tostring(default_counter) .. "]"
50+
end
51+
4752
local debuginfo = function(opts)
4853
local current_line = vim.api.nvim_win_get_cursor(0)[1]
4954

50-
counter = counter + 1
51-
5255
local line = global_opts.print_tag
5356

54-
if global_opts.display_counter then
55-
line = line .. "[" .. counter .. "]"
57+
if global_opts.display_counter == true then
58+
line = line .. default_display_counter()
59+
elseif type(global_opts.display_counter) == "function" then
60+
line = line .. tostring(global_opts.display_counter())
5661
end
5762

5863
line = line .. ": " .. vim.fn.expand("%:t") .. ":" .. current_line
@@ -307,7 +312,7 @@ M.setup = function(opts)
307312
require("debugprint.setup").map_keys_and_commands(global_opts)
308313

309314
-- Because we want to be idempotent, re-running setup() resets the counter
310-
counter = 0
315+
default_counter = 0
311316
end
312317

313318
M.add_custom_filetypes = function(filetypes)

lua/debugprint/options.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ local validate_global_opts = function(o)
3737
vim.validate({
3838
keymaps = { o.keymaps, "table" },
3939
commands = { o.commands, "table" },
40-
display_counter = { o.display_counter, "boolean" },
40+
display_counter = { o.display_counter, { "function", "boolean" }},
4141
display_snippet = { o.display_snippet, "boolean" },
4242
move_to_debugline = { o.move_to_debugline, "boolean" },
4343
ignore_treesitter = { o.ignore_treesitter, "boolean" },

tests/debugprint.lua

+41
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ local teardown = function()
8585
pcall(vim.keymap.del, { "n", "x" }, "g?V")
8686
pcall(vim.keymap.del, "n", "g?o")
8787
pcall(vim.keymap.del, "n", "g?O")
88+
vim.cmd("set modifiable")
8889
end
8990

9091
describe("can do setup()", function()
@@ -2013,3 +2014,43 @@ describe("unmodifiable buffer", function()
20132014
assert.equals(notify_message, "Buffer is not modifiable.")
20142015
end)
20152016
end)
2017+
2018+
describe("custom counter", function()
2019+
local count = 0
2020+
2021+
before_each(function()
2022+
debugprint.setup({
2023+
display_counter = function(opts)
2024+
count = count + 2
2025+
return "-" .. tostring(count) .. "x"
2026+
end,
2027+
})
2028+
end)
2029+
2030+
after_each(teardown)
2031+
2032+
it("basic", function()
2033+
local filename = init_file({
2034+
"foo",
2035+
"bar",
2036+
}, "lua", 1, 0)
2037+
2038+
feedkeys("g?p")
2039+
feedkeys("g?p")
2040+
feedkeys("g?p")
2041+
2042+
check_lines({
2043+
"foo",
2044+
"print('DEBUGPRINT-6x: "
2045+
.. filename
2046+
.. ":1 (after foo)')",
2047+
"print('DEBUGPRINT-4x: "
2048+
.. filename
2049+
.. ":1 (after foo)')",
2050+
"print('DEBUGPRINT-2x: "
2051+
.. filename
2052+
.. ":1 (after foo)')",
2053+
"bar",
2054+
})
2055+
end)
2056+
end)

0 commit comments

Comments
 (0)