Skip to content

Commit 7fac302

Browse files
committed
feat: Implement comment toggle - closes #85
1 parent 26e70ab commit 7fac302

File tree

7 files changed

+201
-17
lines changed

7 files changed

+201
-17
lines changed

.github/workflows/tests.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
run: |
4545
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
4646
git clone --depth 1 https://github.com/nvim-treesitter/nvim-treesitter ~/.local/share/nvim/site/pack/vendor/start/nvim-treesitter
47+
git clone --depth 1 https://github.com/echasnovski/mini.nvim ~/.local/share/nvim/site/pack/vendor/start/mini.nvim
4748
4849
- name: Run tests
4950
run: |

README.md

+23-16
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ Example for [`lazy.nvim`](https://github.com/folke/lazy.nvim):
7070
return {
7171
"andrewferrier/debugprint.nvim",
7272
opts = { … },
73-
-- Dependency only needed for NeoVim 0.8
7473
dependencies = {
75-
"nvim-treesitter/nvim-treesitter"
74+
"echasnovski/mini.nvim" -- Needed to enable :ToggleCommentDebugPrints
75+
"nvim-treesitter/nvim-treesitter" -- Only needed for NeoVim 0.8
7676
},
7777
-- Remove the following line to use development versions,
7878
-- not just the formal releases
@@ -91,6 +91,9 @@ packer.startup(function(use)
9191
opts = { … }
9292
require("debugprint").setup(opts)
9393
end,
94+
requires = {
95+
"echasnovski/mini.nvim" -- Needed to enable :ToggleCommentDebugPrints
96+
}
9497
})
9598
9699
end)
@@ -110,20 +113,22 @@ the box'. There are also some function invocations which are not mapped to any
110113
keymappings or commands by default, but could be. This is all shown in the
111114
following table.
112115

113-
| Mode | Default Key / Cmd | Purpose | Above/Below Line |
114-
| ---------- | -------------------- | ------------------------------------------- | ---------------- |
115-
| Normal | `g?p` | Plain debug | Below |
116-
| Normal | `g?P` | Plain debug | Above |
117-
| Normal | `g?v` | Variable debug | Below |
118-
| Normal | `g?V` | Variable debug | Above |
119-
| Normal | None | Variable debug (always prompt for variable) | Below |
120-
| Normal | None | Variable debug (always prompt for variable) | Above |
121-
| Normal | None | Delete debug lines in buffer | - |
122-
| Visual | `g?v` | Variable debug | Below |
123-
| Visual | `g?v` | Variable debug | Above |
124-
| Op-pending | `g?o` | Variable debug | Below |
125-
| Op-pending | `g?O` | Variable debug | Above |
126-
| Command | `:DeleteDebugPrints` | Delete debug lines in buffer | - |
116+
| Mode | Default Key / Cmd | Purpose | Above/Below Line |
117+
| ---------- | --------------------------- | ------------------------------------------- | ---------------- |
118+
| Normal | `g?p` | Plain debug | Below |
119+
| Normal | `g?P` | Plain debug | Above |
120+
| Normal | `g?v` | Variable debug | Below |
121+
| Normal | `g?V` | Variable debug | Above |
122+
| Normal | None | Variable debug (always prompt for variable) | Below |
123+
| Normal | None | Variable debug (always prompt for variable) | Above |
124+
| Normal | None | Delete debug lines in buffer | - |
125+
| Normal | None | Comment/uncomment debug lines in buffer | - |
126+
| Visual | `g?v` | Variable debug | Below |
127+
| Visual | `g?v` | Variable debug | Above |
128+
| Op-pending | `g?o` | Variable debug | Below |
129+
| Op-pending | `g?O` | Variable debug | Above |
130+
| Command | `:DeleteDebugPrints` | Delete debug lines in buffer | - |
131+
| Command | `:ToggleCommentDebugPrints` | Comment/uncomment debug lines in buffer | - |
127132

128133
The keys and commands outlined above can be specifically overridden using the
129134
`keymaps` and `commands` objects inside the `opts` object used above during
@@ -144,6 +149,7 @@ return {
144149
variable_above_alwaysprompt = nil,
145150
textobj_below = "g?o",
146151
textobj_above = "g?O",
152+
toggle_comment_debug_prints = nil,
147153
delete_debug_prints = nil,
148154
},
149155
visual = {
@@ -152,6 +158,7 @@ return {
152158
},
153159
},
154160
commands = {
161+
toggle_comment_debug_prints = "ToggleCommentDebugPrints",
155162
delete_debug_prints = "DeleteDebugPrints",
156163
},
157164
}

lua/debugprint/init.lua

+46-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ M.debugprint_motion_callback = function()
192192
utils.set_callback("v:lua.require'debugprint'.debugprint_cache")
193193
end
194194

195-
M.deleteprints = function(opts)
195+
local get_lines_to_handle = function(opts)
196196
local lines_to_consider
197197
local initial_line
198198

@@ -213,6 +213,15 @@ M.deleteprints = function(opts)
213213
initial_line = 1
214214
end
215215

216+
return lines_to_consider, initial_line
217+
end
218+
219+
M.deleteprints = function(opts)
220+
local lines_to_consider
221+
local initial_line
222+
223+
lines_to_consider, initial_line = get_lines_to_handle(opts)
224+
216225
local delete_adjust = 0
217226

218227
for count, line in ipairs(lines_to_consider) do
@@ -233,6 +242,42 @@ M.deleteprints = function(opts)
233242
end
234243
end
235244

245+
M.toggle_comment_debugprints = function(opts)
246+
local status, comment = pcall(require, "mini.comment")
247+
248+
if status == true then
249+
local lines_to_consider
250+
local initial_line
251+
252+
lines_to_consider, initial_line = get_lines_to_handle(opts)
253+
254+
for count, line in ipairs(lines_to_consider) do
255+
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
256+
local line_to_toggle = count + initial_line - 1
257+
vim.api.nvim_buf_set_lines(
258+
0,
259+
line_to_toggle,
260+
line_to_toggle,
261+
false,
262+
{}
263+
)
264+
265+
require("mini.comment").toggle_lines(
266+
line_to_toggle,
267+
line_to_toggle,
268+
{}
269+
)
270+
end
271+
end
272+
else
273+
vim.notify(
274+
"mini.nvim is required to toggle comment debugprint lines",
275+
vim.log.levels.ERROR,
276+
{}
277+
)
278+
end
279+
end
280+
236281
M.setup = function(opts)
237282
global_opts =
238283
require("debugprint.options").get_and_validate_global_opts(opts)

lua/debugprint/options.lua

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local GLOBAL_OPTION_DEFAULTS = {
1111
variable_above_alwaysprompt = nil,
1212
textobj_below = "g?o",
1313
textobj_above = "g?O",
14+
toggle_comment_debug_prints = nil,
1415
delete_debug_prints = nil,
1516
},
1617
visual = {
@@ -19,6 +20,7 @@ local GLOBAL_OPTION_DEFAULTS = {
1920
},
2021
},
2122
commands = {
23+
toggle_comment_debug_prints = "ToggleCommentDebugPrints",
2224
delete_debug_prints = "DeleteDebugPrints",
2325
},
2426
display_counter = true,
@@ -51,6 +53,11 @@ local validate_global_opts = function(o)
5153
o.commands.delete_debug_prints,
5254
STRING_NIL,
5355
},
56+
57+
commands_toggle_comment_debug_prints = {
58+
o.commands_toggle_comment_debug_prints,
59+
STRING_NIL,
60+
},
5461
})
5562

5663
local normal = o.keymaps.normal
@@ -72,6 +79,10 @@ local validate_global_opts = function(o)
7279
textobj_below = { normal.textobj_below, STRING_NIL },
7380
textobj_above = { normal.textobj_above, STRING_NIL },
7481
delete_debug_prints = { normal.delete_debug_prints, STRING_NIL },
82+
commands_toggle_comment_debug_prints = {
83+
normal.commands_toggle_comment_debug_prints,
84+
STRING_NIL,
85+
},
7586
})
7687

7788
vim.validate({

lua/debugprint/setup.lua

+19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ M.map_keys_and_commands = function(global_opts)
8080
expr = false,
8181
})
8282

83+
map_key("n", global_opts.keymaps.normal.toggle_comment_debug_prints, {
84+
callback = debugprint.toggle_comment_debugprints,
85+
desc = "Comment/uncomment all debugprint statements in the current buffer",
86+
expr = false,
87+
})
88+
8389
map_key("x", global_opts.keymaps.visual.variable_below, {
8490
callback = function()
8591
return debugprint.debugprint({ variable = true })
@@ -106,6 +112,19 @@ M.map_keys_and_commands = function(global_opts)
106112
}
107113
)
108114
end
115+
116+
if global_opts.commands.toggle_comment_debug_prints then
117+
vim.api.nvim_create_user_command(
118+
global_opts.commands.toggle_comment_debug_prints,
119+
function(cmd_opts)
120+
debugprint.toggle_comment_debugprints(cmd_opts)
121+
end,
122+
{
123+
range = true,
124+
desc = "Comment/uncomment all debugprint statements in the current buffer",
125+
}
126+
)
127+
end
109128
end
110129

111130
return M

tests/debugprint.lua

+99
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ vim.opt.runtimepath:prepend(
77
"~/.local/share/nvim/site/pack/vendor/start/nvim-treesitter"
88
)
99
vim.opt.runtimepath:prepend("../nvim-treesitter")
10+
vim.opt.runtimepath:prepend(
11+
"~/.local/share/nvim/site/pack/vendor/start/mini.nvim"
12+
)
13+
vim.opt.runtimepath:prepend("../mini.nvim")
1014
vim.cmd("runtime! plugin/nvim-treesitter.lua")
1115

1216
local install_parser_if_needed = function(filetype)
@@ -1606,6 +1610,101 @@ if vim.fn.has("nvim-0.9.0") == 1 then
16061610
end)
16071611
end
16081612

1613+
describe("comment toggle", function()
1614+
after_each(teardown)
1615+
1616+
it("basic", function()
1617+
debugprint.setup({})
1618+
1619+
local filename = init_file({
1620+
"function x()",
1621+
" local xyz = 3",
1622+
"end",
1623+
}, "lua", 1, 1)
1624+
1625+
feedkeys("g?p")
1626+
vim.cmd("ToggleCommentDebugPrint")
1627+
feedkeys("jjg?p")
1628+
1629+
check_lines({
1630+
"function x()",
1631+
" -- print('DEBUGPRINT[1]: "
1632+
.. filename
1633+
.. ":1 (after function x())')",
1634+
" local xyz = 3",
1635+
" print('DEBUGPRINT[2]: "
1636+
.. filename
1637+
.. ":3 (after local xyz = 3)')",
1638+
"end",
1639+
})
1640+
1641+
vim.cmd("ToggleCommentDebugPrint")
1642+
1643+
check_lines({
1644+
"function x()",
1645+
" print('DEBUGPRINT[1]: "
1646+
.. filename
1647+
.. ":1 (after function x())')",
1648+
" local xyz = 3",
1649+
" -- print('DEBUGPRINT[2]: "
1650+
.. filename
1651+
.. ":3 (after local xyz = 3)')",
1652+
"end",
1653+
})
1654+
end)
1655+
1656+
it("range", function()
1657+
debugprint.setup({})
1658+
1659+
local filename = init_file({
1660+
"function x()",
1661+
" local xyz = 3",
1662+
"end",
1663+
}, "lua", 1, 1)
1664+
1665+
feedkeys("g?pjjg?p")
1666+
vim.cmd("2 ToggleCommentDebugPrint")
1667+
1668+
check_lines({
1669+
"function x()",
1670+
" -- print('DEBUGPRINT[1]: "
1671+
.. filename
1672+
.. ":1 (after function x())')",
1673+
" local xyz = 3",
1674+
" print('DEBUGPRINT[2]: "
1675+
.. filename
1676+
.. ":3 (after local xyz = 3)')",
1677+
"end",
1678+
})
1679+
end)
1680+
1681+
it("basic with keymaps", function()
1682+
debugprint.setup({
1683+
keymaps = { normal = { toggle_comment_debug_prints = "g?x" } },
1684+
})
1685+
1686+
local filename = init_file({
1687+
"function x()",
1688+
" local xyz = 3",
1689+
"end",
1690+
}, "lua", 1, 1)
1691+
1692+
feedkeys("g?pg?xjjg?p")
1693+
1694+
check_lines({
1695+
"function x()",
1696+
" -- print('DEBUGPRINT[1]: "
1697+
.. filename
1698+
.. ":1 (after function x())')",
1699+
" local xyz = 3",
1700+
" print('DEBUGPRINT[2]: "
1701+
.. filename
1702+
.. ":3 (after local xyz = 3)')",
1703+
"end",
1704+
})
1705+
end)
1706+
end)
1707+
16091708
describe("handle deprecated options, create_keymaps=false", function()
16101709
before_each(function()
16111710
debugprint.setup({ create_keymaps = false })

tests/minimal.vim

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
set rtp+=~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
2+
set rtp+=~/.local/share/nvim/site/pack/vendor/start/mini.nvim
23
set rtp+=../plenary.nvim
4+
set rtp+=../mini.nvim
35

46
runtime! plugin/plenary.vim

0 commit comments

Comments
 (0)