Skip to content

Commit 26ba69f

Browse files
committed
feat!: Use persistent counter - closes #143
This change introduces a persistent counter value, so that it persists between NeoVim invocations and even between multiple NeoVim instances open at the same time. This is done by storing a small state file in NeoVim's data directory. If you don't like this behaviour, you can revert back to the old behaviour by using a custom `display_counter` option, as shown in the showcase: https://github.com/andrewferrier/debugprint.nvim/blob/main/SHOWCASE.md#restoring-non-persistent-display_counter-counter
1 parent 56e1dd0 commit 26ba69f

File tree

8 files changed

+129
-18
lines changed

8 files changed

+129
-18
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ the NeoVim generation. It:
3131
support other languages or customize existing ones](SHOWCASE.md).
3232

3333
* Includes reference information in each 'print line' such as file names, line
34-
numbers, a counter, and snippets of other lines to make it easier to
35-
cross-reference them in output (each of these can be optionally
36-
disabled [globally](#other-options) or [on a per-filetype basis](SHOWCASE.md#setting-display_-options-on-per-filetype-basis)).
34+
numbers, a counter (which persists between NeoVim sessions), and snippets of
35+
other lines to make it easier to cross-reference them in output (each of these
36+
can be optionally disabled [globally](#other-options) or [on a per-filetype
37+
basis](SHOWCASE.md#setting-display_-options-on-per-filetype-basis)).
3738

3839
* Can output the value of variables (or in some cases, expressions) - it will
3940
detect a variable name under the cursor for some languages using Treesitter, or
@@ -114,6 +115,7 @@ following table.
114115
| Op-pending | `g?O` | Variable debug | Above |
115116
| Command | `:DeleteDebugPrints` | Delete debug lines in buffer | - |
116117
| Command | `:ToggleCommentDebugPrints` | Comment/uncomment debug lines in buffer | - |
118+
| Command | `:ResetDebugPrintsCounter` | Reset debug print persistent counter (only for built-in counter implementation) | - |
117119

118120
The keys and commands outlined above can be specifically overridden using the
119121
`keymaps` and `commands` objects inside the `opts` object used above during
@@ -149,6 +151,7 @@ return {
149151
commands = {
150152
toggle_comment_debug_prints = "ToggleCommentDebugPrints",
151153
delete_debug_prints = "DeleteDebugPrints",
154+
reset_debug_prints_counter = "ResetDebugPrintsCounter",
152155
},
153156
-- … Other options
154157
},
@@ -183,7 +186,7 @@ they are used to convert sections to ROT-13, which most folks don't use.
183186
| ------------------- | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
184187
| `move_to_debugline` | `false` | When adding a debug line, moves the cursor to that line |
185188
| `display_location` | `true` | Include the filename and linenumber of the line being debugged in the debug message |
186-
| `display_counter` | `true` | Include the increasing integer counter in the debug message. (Can also be set to a function to customize, see the [showcase](SHOWCASE.md#use-a-custom-display_counter-counter)) for an example |
189+
| `display_counter` | `true` | Include the increasing integer counter in the debug message. (Can also be set to a function to customize, see the [showcase](SHOWCASE.md#restoring-non-persistent-display_counter-counter)) for an example |
187190
| `display_snippet` | `true` | Include a snippet of the line above/below in the debug message (plain debug lines only) for context |
188191
| `filetypes` | See ([the code](lua/debugprint/filetypes.lua)) | Custom filetypes - see [showcase](SHOWCASE.md) |
189192
| `print_tag` | `DEBUGPRINT` | The string inserted into each print statement, which can be used to uniquely identify statements inserted by `debugprint`. If you set this to `''` (the empty string), no print tag will be included, but this will disable the ability to delete or comment print statements via `debugprint` |
@@ -205,6 +208,7 @@ they are used to convert sections to ROT-13, which most folks don't use.
205208
| ------------------------------------------------------------------- | ----------------- | ------------------------------------------------------- | -------------------------------------------------------------- | --------------------------------------------------------- | -------------------------------------------------------------------- | --------------------------------------------------- | ----------------------------------------------------- |
206209
| Include line numbers in log lines | :+1: | :+1: (via user config) | :+1: (via user config) | :+1: | :+1: | :x: | :+1: |
207210
| Include other location information in log lines | :+1: | :x: | :+1: (via user config) | :+1: | :+1: | :x: | :+1: |
211+
| Persistent location counter between NeoVim sessions | :+1: | :x: | :x: | :x: | :x: | :x: | :x: |
208212
| Print plain debug lines | :+1: | :+1: (via user config) | :+1: | :x: | :+1: | :x: | :x: |
209213
| Print variables using treesitter | :+1: | :+1: | :+1: | :x: | :+1: | :x: | :x: |
210214
| Use treesitter to locate log targets | (some languages) | :+1: | :x: | :x: | :x: | :x: | :x: |

SHOWCASE.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ return {
126126
}
127127
```
128128

129-
## Use a custom `display_counter` counter
129+
## Restoring non-persistent `display_counter` counter
130130

131-
The `display_counter` option can be set to a custom callback function to implement custom counter logic. In this case you are responsible for implementing your own counter. For example, this logic will implement essentially the same as the default counter:
131+
In older versions, `debugprint` used a `display_counter` which was only local to a particular NeoVim session; it was reset when exiting NeoVim and wasn't common between NeoVim sessions in different terminals. If you don't like the new 'persistent' counter, you can restore this old behaviour by setting a custom `display_counter`. This will recreate the old logic:
132132

133133
```lua
134134
local counter = 0
@@ -141,6 +141,8 @@ end
141141
debugprint.setup({display_counter = counter_func})
142142
```
143143

144+
You can also set `display_counter` to any other function you wish.
145+
144146
## Using package managers other than lazy.nvim
145147

146148
Example for [`packer.nvim`](https://github.com/wbthomason/packer.nvim):

lua/debugprint/counter.lua

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local M = {}
2+
3+
local default_counter
4+
5+
local DATA_PATH = vim.fs.joinpath(vim.fn.stdpath("data"), "debugprint")
6+
local COUNTER_FILE = vim.fs.joinpath(DATA_PATH, "counter")
7+
8+
---@return string
9+
M.default_display_counter = function()
10+
if vim.fn.mkdir(DATA_PATH, "p") == 1 then
11+
if vim.fn.filereadable(COUNTER_FILE) == 1 then
12+
local counter_lines = vim.fn.readfile(COUNTER_FILE)
13+
14+
if #counter_lines == 1 then
15+
default_counter = tonumber(counter_lines[1])
16+
end
17+
end
18+
end
19+
20+
if default_counter == nil then
21+
default_counter = 0
22+
end
23+
24+
default_counter = default_counter + 1
25+
26+
if vim.fn.filewritable(DATA_PATH) == 2 then
27+
vim.fn.writefile({ default_counter }, COUNTER_FILE)
28+
end
29+
30+
return "[" .. tostring(default_counter) .. "]"
31+
end
32+
33+
---@return nil
34+
M.reset_debug_prints_counter = function()
35+
if vim.fn.filewritable(COUNTER_FILE) == 1 then
36+
vim.fs.rm(COUNTER_FILE)
37+
end
38+
39+
default_counter = nil
40+
end
41+
42+
return M

lua/debugprint/init.lua

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ local utils_errors = require("debugprint.utils.errors")
66
local utils_operator = require("debugprint.utils.operator")
77

88
local global_opts
9-
local default_counter = 0
10-
11-
---@return string
12-
local default_display_counter = function()
13-
default_counter = default_counter + 1
14-
return "[" .. tostring(default_counter) .. "]"
15-
end
169

1710
---@param display_counter? boolean|function
1811
---@return string
@@ -24,7 +17,8 @@ local get_debugline_tag_and_counter = function(display_counter)
2417
end
2518

2619
if display_counter == true then
27-
tag_and_counter = tag_and_counter .. default_display_counter()
20+
tag_and_counter = tag_and_counter
21+
.. require("debugprint.counter").default_display_counter()
2822
elseif type(display_counter) == "function" then
2923
tag_and_counter = tag_and_counter .. tostring(display_counter())
3024
end
@@ -400,9 +394,6 @@ M.setup = function(opts)
400394
require("debugprint.options").get_and_validate_global_opts(opts)
401395

402396
require("debugprint.setup").map_keys_and_commands(global_opts)
403-
404-
-- Because we want to be idempotent, re-running setup() resets the counter
405-
default_counter = 0
406397
end
407398

408399
---@param filetypes DebugprintFileTypeConfig[]

lua/debugprint/options.lua

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ local GLOBAL_OPTION_DEFAULTS = {
2727
commands = {
2828
toggle_comment_debug_prints = "ToggleCommentDebugPrints",
2929
delete_debug_prints = "DeleteDebugPrints",
30+
reset_debug_prints_counter = "ResetDebugPrintsCounter",
3031
},
3132
display_counter = true,
3233
display_location = true,
@@ -65,6 +66,11 @@ local validate_global_opts = function(o)
6566
o.commands.toggle_comment_debug_prints,
6667
STRING_FALSE_NIL,
6768
},
69+
70+
commands_reset_debug_prints_counter = {
71+
o.commands.reset_debug_prints_counter,
72+
STRING_FALSE_NIL,
73+
},
6874
})
6975

7076
local normal = o.keymaps.normal

lua/debugprint/setup.lua

+8
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ M.map_keys_and_commands = function(global_opts)
186186
desc = "Comment/uncomment all debugprint statements in the current buffer",
187187
}
188188
)
189+
190+
create_command(
191+
global_opts.commands.reset_debug_prints_counter,
192+
require("debugprint.counter").reset_debug_prints_counter,
193+
{
194+
desc = "Reset the debugprint counter to 0",
195+
}
196+
)
189197
end
190198

191199
return M

lua/debugprint/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
---@class DebugprintCommandOptions
5555
---@field delete_debug_prints? string|false
5656
---@field toggle_comment_debug_prints? string|false
57+
---@field reset_debug_prints_counter? string|false
5758

5859
---@class DebugprintFunctionOptions
5960
---@field above? boolean

tests/debugprint.lua

+58-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ vim.notify = function(msg, _)
7777
notify_message = msg
7878
end
7979

80-
local teardown = function()
80+
local DATA_PATH = vim.fs.joinpath(vim.fn.stdpath("data"), "debugprint")
81+
local COUNTER_FILE = vim.fs.joinpath(DATA_PATH, "counter")
82+
83+
local teardown = function(opts)
84+
opts = vim.tbl_extend("keep", opts or {}, { reset_counter = true })
85+
8186
notify_message = nil
8287
pcall(vim.keymap.del, "n", "g?p")
8388
pcall(vim.keymap.del, "n", "g?P")
@@ -88,6 +93,10 @@ local teardown = function()
8893
pcall(vim.api.nvim_del_user_command, "DeleteDebugPrints")
8994
pcall(vim.api.nvim_del_user_command, "ToggleCommentDebugPrints")
9095
vim.cmd("set modifiable")
96+
97+
if opts.reset_counter then
98+
require("debugprint.counter").reset_debug_prints_counter()
99+
end
91100
end
92101

93102
---@param name string
@@ -2595,3 +2604,51 @@ describe("can disable built-in keymaps/commands", function()
25952604
assert.equals(command_exists("DeleteDebugPrints"), false)
25962605
end)
25972606
end)
2607+
2608+
describe("check that counter persistence works", function()
2609+
before_each(function()
2610+
debugprint.setup()
2611+
end)
2612+
2613+
after_each(function()
2614+
teardown({ reset_counter = false })
2615+
end)
2616+
2617+
it("statement 1", function()
2618+
assert.equals(vim.fn.filereadable(COUNTER_FILE), 0)
2619+
2620+
local filename = init_file({
2621+
"foo",
2622+
"bar",
2623+
}, "lua", 1, 0)
2624+
2625+
feedkeys("g?p")
2626+
2627+
check_lines({
2628+
"foo",
2629+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (after foo)')",
2630+
"bar",
2631+
})
2632+
2633+
assert.equals(vim.fn.filereadable(COUNTER_FILE), 1)
2634+
end)
2635+
2636+
it("statement 2", function()
2637+
assert.equals(vim.fn.filereadable(COUNTER_FILE), 1)
2638+
2639+
local filename = init_file({
2640+
"foo",
2641+
"bar",
2642+
}, "lua", 1, 0)
2643+
2644+
feedkeys("g?P")
2645+
2646+
check_lines({
2647+
"print('DEBUGPRINT[2]: " .. filename .. ":1 (before foo)')",
2648+
"foo",
2649+
"bar",
2650+
})
2651+
2652+
assert.equals(vim.fn.filereadable(COUNTER_FILE), 1)
2653+
end)
2654+
end)

0 commit comments

Comments
 (0)