Skip to content

Commit 6ef8571

Browse files
committed
feat: Keybinding: delete_debug_lines - closes #87
1 parent 32137d7 commit 6ef8571

File tree

4 files changed

+95
-83
lines changed

4 files changed

+95
-83
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ return {
143143
variable_above_alwaysprompt = nil,
144144
textobj_below = "g?o",
145145
textobj_above = "g?O",
146+
delete_debug_prints = nil,
146147
},
147148
visual = {
148149
variable_below = "g?v",

lua/debugprint/init.lua

+73-83
Original file line numberDiff line numberDiff line change
@@ -233,107 +233,97 @@ M.deleteprints = function(opts)
233233
end
234234
end
235235

236-
local map_key = function(mode, lhs, rhs, desc)
236+
local map_key = function(mode, lhs, opts)
237237
if lhs ~= nil then
238-
if type(rhs) == "function" then
239-
vim.api.nvim_set_keymap(
240-
mode,
241-
lhs,
242-
"",
243-
{ expr = true, desc = desc, callback = rhs }
244-
)
245-
else
246-
vim.api.nvim_set_keymap(mode, lhs, "", {
247-
expr = true,
248-
desc = desc,
249-
callback = function()
250-
return M.debugprint(rhs)
251-
end,
252-
})
253-
end
238+
opts = vim.tbl_extend("force", { expr = true }, opts)
239+
240+
vim.api.nvim_set_keymap(mode, lhs, "", opts)
254241
end
255242
end
256243

257244
M.setup = function(opts)
258245
global_opts =
259246
require("debugprint.options").get_and_validate_global_opts(opts)
260247

261-
map_key(
262-
"n",
263-
global_opts.keymaps.normal.plain_below,
264-
{},
265-
"Plain debug below current line"
266-
)
248+
map_key("n", global_opts.keymaps.normal.plain_below, {
249+
callback = function()
250+
return M.debugprint({})
251+
end,
252+
desc = "Plain debug below current line",
253+
})
267254

268-
map_key(
269-
"n",
270-
global_opts.keymaps.normal.plain_above,
271-
{ above = true },
272-
"Plain debug below current line"
273-
)
255+
map_key("n", global_opts.keymaps.normal.plain_above, {
256+
callback = function()
257+
return M.debugprint({ above = true })
258+
end,
259+
desc = "Plain debug below current line",
260+
})
274261

275-
map_key(
276-
"n",
277-
global_opts.keymaps.normal.variable_below,
278-
{ variable = true },
279-
"Variable debug below current line"
280-
)
262+
map_key("n", global_opts.keymaps.normal.variable_below, {
263+
callback = function()
264+
return M.debugprint({ variable = true })
265+
end,
266+
desc = "Variable debug below current line",
267+
})
281268

282-
map_key(
283-
"n",
284-
global_opts.keymaps.normal.variable_above,
285-
{ above = true, variable = true },
286-
"Variable debug above current line"
287-
)
269+
map_key("n", global_opts.keymaps.normal.variable_above, {
270+
callback = function()
271+
return M.debugprint({ above = true, variable = true })
272+
end,
273+
desc = "Variable debug above current line",
274+
})
288275

289-
map_key(
290-
"n",
291-
global_opts.keymaps.normal.variable_below_alwaysprompt,
292-
{ variable = true, ignore_treesitter = true },
293-
"Variable debug below current line (always prompt)"
294-
)
276+
map_key("n", global_opts.keymaps.normal.variable_below_alwaysprompt, {
277+
callback = function()
278+
return M.debugprint({ variable = true, ignore_treesitter = true })
279+
end,
280+
desc = "Variable debug below current line (always prompt})",
281+
})
295282

296-
map_key(
297-
"n",
298-
global_opts.keymaps.normal.variable_above_alwaysprompt,
299-
{ above = true, variable = true, ignore_treesitter = true },
300-
"Variable debug above current line (always prompt)"
301-
)
283+
map_key("n", global_opts.keymaps.normal.variable_above_alwaysprompt, {
284+
callback = function()
285+
return M.debugprint({
286+
above = true,
287+
variable = true,
288+
ignore_treesitter = true,
289+
})
290+
end,
291+
desc = "Variable debug above current line (always prompt})",
292+
})
302293

303-
map_key(
304-
"n",
305-
global_opts.keymaps.normal.textobj_below,
306-
{ motion = true },
307-
"Text-obj-selected variable debug below current line"
308-
)
294+
map_key("n", global_opts.keymaps.normal.textobj_below, {
295+
callback = function()
296+
return M.debugprint({ motion = true })
297+
end,
298+
desc = "Text-obj-selected variable debug below current line",
299+
})
309300

310-
map_key(
311-
"n",
312-
global_opts.keymaps.normal.textobj_above,
313-
{ motion = true, above = true },
314-
"Text-obj-selected variable debug above current line"
315-
)
301+
map_key("n", global_opts.keymaps.normal.textobj_above, {
302+
callback = function()
303+
return M.debugprint({ motion = true, above = true })
304+
end,
305+
desc = "Text-obj-selected variable debug above current line",
306+
})
316307

317-
map_key(
318-
"n",
319-
global_opts.keymaps.normal.delete_debug_prints,
320-
M.deleteprints,
321-
"Delete all debugprint statements in the current buffer"
322-
)
308+
map_key("n", global_opts.keymaps.normal.delete_debug_prints, {
309+
callback = M.deleteprints,
310+
desc = "Delete all debugprint statements in the current buffer",
311+
expr = false,
312+
})
323313

324-
map_key(
325-
"x",
326-
global_opts.keymaps.visual.variable_below,
327-
{ variable = true },
328-
"Variable debug below current line"
329-
)
314+
map_key("x", global_opts.keymaps.visual.variable_below, {
315+
callback = function()
316+
return M.debugprint({ variable = true })
317+
end,
318+
desc = "Variable debug below current line",
319+
})
330320

331-
map_key(
332-
"x",
333-
global_opts.keymaps.visual.variable_above,
334-
{ above = true, variable = true },
335-
"Variable debug above current line"
336-
)
321+
map_key("x", global_opts.keymaps.visual.variable_above, {
322+
callback = function()
323+
return M.debugprint({ above = true, variable = true })
324+
end,
325+
desc = "Variable debug above current line",
326+
})
337327

338328
if global_opts.commands.delete_debug_prints then
339329
vim.api.nvim_create_user_command(

lua/debugprint/options.lua

+2
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+
delete_debug_prints = nil,
1415
},
1516
visual = {
1617
variable_below = "g?v",
@@ -70,6 +71,7 @@ local validate_global_opts = function(o)
7071
},
7172
textobj_below = { normal.textobj_below, STRING_NIL },
7273
textobj_above = { normal.textobj_above, STRING_NIL },
74+
delete_debug_prints = { normal.delete_debug_prints, STRING_NIL }
7375
})
7476

7577
vim.validate({

tests/debugprint.lua

+19
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,25 @@ describe("delete lines command", function()
13811381
"end",
13821382
})
13831383
end)
1384+
1385+
it("basic - with key binding", function()
1386+
debugprint.setup({ keymaps = { normal = { delete_debug_prints = "g?x" }}})
1387+
1388+
init_file({
1389+
"function x()",
1390+
" local xyz = 3",
1391+
"end",
1392+
}, "lua", 2, 1)
1393+
1394+
feedkeys("g?p")
1395+
feedkeys("g?x")
1396+
1397+
check_lines({
1398+
"function x()",
1399+
" local xyz = 3",
1400+
"end",
1401+
})
1402+
end)
13841403
end)
13851404

13861405
describe("don't display counter", function()

0 commit comments

Comments
 (0)