Skip to content

Commit 1896124

Browse files
author
Andrew Ferrier
committed
feat: Support command for deleting lines - closes #14
1 parent 238dc89 commit 1896124

File tree

3 files changed

+216
-27
lines changed

3 files changed

+216
-27
lines changed

README.md

+45-27
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ It provides various improvements:
4444
* The output when printing a 'plain' debug line, or a variable, is more
4545
consistent.
4646

47+
* It provides a command to delete all debugging lines added to the current buffer.
48+
4749
* Able to optionally move to the inserted line (or not).
4850

4951
## Demo
@@ -53,7 +55,7 @@ It provides various improvements:
5355
</div>
5456

5557
(This demo is not yet updated to show automatically picking up a variable name
56-
from under the cursor, and uses old keybindings - the new ones are `g?p`, `g?P`,
58+
from under the cursor, and uses old keymappings - the new ones are `g?p`, `g?P`,
5759
`g?v` and `g?V`).
5860

5961
## Installation
@@ -101,33 +103,36 @@ Please subscribe to [this GitHub issue](https://github.com/andrewferrier/debugpr
101103
especially while `debugprint` is still in active development, to be notified of
102104
any breaking issues.
103105

104-
## Keymappings
105-
106-
By default, the plugin will create some keymappings, which are the standard way
107-
to use it. There are also some function invocations which are not mapped to any
108-
keymappings by default, but could be. This is all shown in the following table.
109-
110-
| Mode | Keymap | Purpose | Equivalent Lua Function |
111-
| ---------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
112-
| Normal | `g?p` | Insert a 'plain' debug line appropriate to the filetype just below the current line | `require('debugprint').debugprint()` |
113-
| Normal | `g?P` | The same, but above the current line | `require('debugprint').debugprint({above = true})` |
114-
| Normal | `g?v` | Insert a variable debugging line below the current line. If the cursor is on a variable name, use that, otherwise prompt for one. | `require('debugprint').debugprint({variable = true})` |
115-
| Normal | `g?V` | The same, but above the current line | `require('debugprint').debugprint({above = true, variable = true})` |
116-
| Normal | None by default | Always prompt for a variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, variable = true})` |
117-
| Normal | None by default | Always prompt for a variable name, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, above = true, variable = true})` |
118-
| Visual | `g?v` | Find the visually select variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({variable = true})` |
119-
| Visual | `g?v` | Find the visually select variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({variable = true})` |
120-
| Operator-pending | `g?o` | Locate a variable using a motion, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({above = true, variable = true})` |
121-
| Operator-pending | `g?O` | Locate a variable using a motion, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({above = true, variable = true})` |
122-
123-
These keybindings are chosen specifically because by default in NeoVim they are
106+
## Keymappings and Commands
107+
108+
By default, the plugin will create some keymappings and commands, which are the
109+
standard way to use it. There are also some function invocations which are not
110+
mapped to any keymappings or commands by default, but could be. This is all
111+
shown in the following table.
112+
113+
| Mode | Default Keymap/Command | Purpose | Equivalent Lua Function |
114+
| ---------------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
115+
| Normal | `g?p` | Insert a 'plain' debug line appropriate to the filetype just below the current line | `require('debugprint').debugprint()` |
116+
| Normal | `g?P` | The same, but above the current line | `require('debugprint').debugprint({above = true})` |
117+
| Normal | `g?v` | Insert a variable debugging line below the current line. If the cursor is on a variable name, use that, otherwise prompt for one. | `require('debugprint').debugprint({variable = true})` |
118+
| Normal | `g?V` | The same, but above the current line | `require('debugprint').debugprint({above = true, variable = true})` |
119+
| Normal | None by default | Always prompt for a variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, variable = true})` |
120+
| Normal | None by default | Always prompt for a variable name, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({ignore_treesitter = true, above = true, variable = true})` |
121+
| Visual | `g?v` | Find the visually select variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({variable = true})` |
122+
| Visual | `g?v` | Find the visually select variable name, and insert a debugging line just below the current line which outputs it | `require('debugprint').debugprint({variable = true})` |
123+
| Operator-pending | `g?o` | Locate a variable using a motion, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({above = true, variable = true})` |
124+
| Operator-pending | `g?O` | Locate a variable using a motion, and insert a debugging line just above the current line which outputs it | `require('debugprint').debugprint({above = true, variable = true})` |
125+
| Command | `:DeleteDebugPrints` | Delete all debug lines added to this buffer. | `require('debugprint').deleteprints() |
126+
127+
The keymappings are chosen specifically because by default in NeoVim they are
124128
used to convert sections to ROT-13, which most folks don't use. You can disable
125-
them from being created by setting `create_keymaps`, and map them yourself to
126-
something else if you prefer:
129+
the defaults above from being created by setting `create_keymaps` and/or
130+
`create_commands`, and map them yourself to something else if you prefer:
127131

128132
```lua
129133
opts = {
130-
create_keymaps = false
134+
create_keymaps = false,
135+
create_commands = false
131136
...
132137
}
133138

@@ -145,9 +150,25 @@ end)
145150
vim.keymap.set("n", "<Leader>Dq", function()
146151
require('debugprint').debugprint({ above = true, variable = true })
147152
end)
153+
154+
vim.api.nvim_create_user_command("DeleteDebugs", function(opts)
155+
-- Note: you must set `range=true` and pass through opts for ranges to work
156+
M.deleteprints(opts)
157+
end, {
158+
range = true})
159+
end)
148160
...
149161
```
150162

163+
or, to have a keymapping instead for deleting debug lines (this will only affect
164+
the entire buffer, visual and operator-pending modes will not work):
165+
166+
```lua
167+
vim.keymap.set("n", "g?d", function()
168+
M.deleteprints()
169+
end)
170+
```
171+
151172
## Other Options
152173

153174
`debugprint` supports the following options in its global `opts` object:
@@ -231,9 +252,6 @@ If it helps to understand these, you can look at the built-in configurations in
231252

232253
## Planned Future Improvements
233254

234-
* Provide a semi-automated way to get rid of all debugging lines
235-
([issue](https://github.com/andrewferrier/debugprint.nvim/issues/14))
236-
237255
* Dynamically adapt filetype for embedded languages (e.g. code embedded in
238256
Markdown)
239257
([issue](https://github.com/andrewferrier/debugprint.nvim/issues/9))

lua/debugprint/init.lua

+52
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local global_opts
66

77
GLOBAL_OPTION_DEFAULTS = {
88
create_keymaps = true,
9+
create_commands = true,
910
move_to_debugline = false,
1011
ignore_treesitter = false,
1112
filetypes = require("debugprint.filetypes"),
@@ -176,6 +177,47 @@ M.debugprint_motion_callback = function()
176177
set_callback("v:lua.require'debugprint'.debugprint_cache")
177178
end
178179

180+
M.deleteprints = function(opts)
181+
local lines_to_consider
182+
local initial_line
183+
184+
-- opts.range appears to be the magic value that indicates a range is passed
185+
-- in and valid.
186+
187+
if
188+
opts
189+
and (opts.range == 1 or opts.range == 2)
190+
and opts.line1
191+
and opts.line2
192+
then
193+
lines_to_consider =
194+
vim.api.nvim_buf_get_lines(0, opts.line1 - 1, opts.line2, false)
195+
initial_line = opts.line1
196+
else
197+
lines_to_consider = vim.api.nvim_buf_get_lines(0, 0, -1, true)
198+
initial_line = 1
199+
end
200+
201+
local delete_adjust = 0
202+
203+
for count, line in ipairs(lines_to_consider) do
204+
if string.find(line, global_opts.print_tag) ~= nil then
205+
local line_to_delete = count
206+
- 1
207+
- delete_adjust
208+
+ (initial_line - 1)
209+
vim.api.nvim_buf_set_lines(
210+
0,
211+
line_to_delete,
212+
line_to_delete + 1,
213+
false,
214+
{}
215+
)
216+
delete_adjust = delete_adjust + 1
217+
end
218+
end
219+
end
220+
179221
local notify_deprecated = function()
180222
vim.notify(
181223
"dqp and similar keymappings are deprecated for debugprint and are "
@@ -191,6 +233,7 @@ M.setup = function(opts)
191233

192234
vim.validate({
193235
create_keymaps = { global_opts.create_keymaps, "boolean" },
236+
create_commands = { global_opts.create_commands, "boolean" },
194237
move_to_debugline = { global_opts.move_to_debugline, "boolean" },
195238
ignore_treesitter = { global_opts.ignore_treesitter, "boolean" },
196239
filetypes = { global_opts.filetypes, "table" },
@@ -265,6 +308,15 @@ M.setup = function(opts)
265308
})
266309
end
267310

311+
if global_opts.create_commands then
312+
vim.api.nvim_create_user_command("DeleteDebugPrints", function(opts)
313+
M.deleteprints(opts)
314+
end, {
315+
range = true,
316+
desc = "Delete all debugprint statements in the current buffer.",
317+
})
318+
end
319+
268320
-- Because we want to be idempotent, re-running setup() resets the counter
269321
counter = 0
270322
end

tests/debugprint.lua

+119
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,122 @@ describe("motion mode", function()
876876
)
877877
end)
878878
end)
879+
880+
describe("delete lines command", function()
881+
it("basic", function()
882+
debugprint.setup({})
883+
884+
init_file({
885+
"function x()",
886+
" local xyz = 3",
887+
"end",
888+
}, "lua", 2, 1)
889+
890+
feedkeys("g?p")
891+
vim.cmd('DeleteDebugPrints')
892+
893+
check_lines({
894+
"function x()",
895+
" local xyz = 3",
896+
"end",
897+
})
898+
end)
899+
900+
it("complex", function()
901+
debugprint.setup({})
902+
903+
init_file({
904+
"function x()",
905+
" local xyz = 3",
906+
"end",
907+
}, "lua", 1, 0)
908+
909+
feedkeys("g?pg?vwibble<CR>g?p")
910+
vim.cmd('DeleteDebugPrints')
911+
912+
check_lines({
913+
"function x()",
914+
" local xyz = 3",
915+
"end",
916+
})
917+
end)
918+
919+
it("range - one line", function()
920+
debugprint.setup({})
921+
922+
local filename = init_file({
923+
"function x()",
924+
" local xyz = 3",
925+
"end",
926+
}, "lua", 1, 0)
927+
928+
feedkeys("g?pg?pg?pg?p")
929+
930+
vim.cmd("2 DeleteDebugPrints")
931+
932+
check_lines({
933+
"function x()",
934+
" print('DEBUGPRINT[3]: " .. filename .. ":1')",
935+
" print('DEBUGPRINT[2]: " .. filename .. ":1')",
936+
" print('DEBUGPRINT[1]: " .. filename .. ":1')",
937+
" local xyz = 3",
938+
"end",
939+
})
940+
end)
941+
942+
it("range", function()
943+
debugprint.setup({})
944+
945+
local filename = init_file({
946+
"function x()",
947+
" local xyz = 3",
948+
"end",
949+
}, "lua", 1, 0)
950+
951+
feedkeys("g?pg?pg?pg?p")
952+
953+
vim.cmd("2,3 DeleteDebugPrints")
954+
955+
check_lines({
956+
"function x()",
957+
" print('DEBUGPRINT[2]: " .. filename .. ":1')",
958+
" print('DEBUGPRINT[1]: " .. filename .. ":1')",
959+
" local xyz = 3",
960+
"end",
961+
})
962+
end)
963+
964+
it("range at top", function()
965+
debugprint.setup({})
966+
967+
local filename = init_file({
968+
"function x()",
969+
}, "lua", 1, 0)
970+
971+
feedkeys("g?pg?P")
972+
973+
vim.cmd("1 DeleteDebugPrints")
974+
975+
check_lines({
976+
"function x()",
977+
" print('DEBUGPRINT[1]: " .. filename .. ":1')",
978+
})
979+
end)
980+
981+
it("range at bottom", function()
982+
debugprint.setup({})
983+
984+
local filename = init_file({
985+
"function x()",
986+
}, "lua", 1, 0)
987+
988+
feedkeys("g?pg?P")
989+
990+
vim.cmd("$ DeleteDebugPrints")
991+
992+
check_lines({
993+
"print('DEBUGPRINT[2]: " .. filename .. ":1')",
994+
"function x()",
995+
})
996+
end)
997+
end)

0 commit comments

Comments
 (0)