Skip to content

Commit 7048b21

Browse files
author
Andrew Ferrier
committed
feat: Support dot-repeat - closes #3
1 parent e1f932b commit 7048b21

File tree

2 files changed

+147
-15
lines changed

2 files changed

+147
-15
lines changed

lua/debugprint/init.lua

+49-14
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ local indent_line = function(current_line)
5050
end
5151
end
5252

53-
M.debugprint = function(o)
53+
local debugprint_logic = function(o)
5454
local funcopts = vim.tbl_deep_extend(
5555
"force",
5656
{ above = false, variable = false },
@@ -72,13 +72,11 @@ M.debugprint = function(o)
7272
local line_to_insert
7373
local line_to_insert_on
7474

75-
if funcopts.variable then
76-
local variable_name = vim.fn.input("Variable name: ")
77-
75+
if funcopts.variable_name then
7876
line_to_insert = fixes.left
79-
.. debuginfo(variable_name)
77+
.. debuginfo(funcopts.variable_name)
8078
.. fixes.mid_var
81-
.. variable_name
79+
.. funcopts.variable_name
8280
.. fixes.right_var
8381
else
8482
line_to_insert = fixes.left .. debuginfo() .. fixes.right
@@ -101,6 +99,43 @@ M.debugprint = function(o)
10199
indent_line(line_to_insert_on)
102100
end
103101

102+
local cache_request = nil
103+
104+
M.NOOP = function() end
105+
106+
local set_callback = function(func_name)
107+
vim.go.operatorfunc = "v:lua.require'debugprint'.NOOP"
108+
vim.cmd("normal! g@l")
109+
vim.go.operatorfunc = func_name
110+
end
111+
112+
local debugprint_cache = function(o)
113+
if o and o.prerepeat == true then
114+
if o.variable == true then
115+
o.variable_name = vim.fn.input("Variable name: ")
116+
end
117+
118+
cache_request = o
119+
vim.go.operatorfunc = "v:lua.require'debugprint'.debugprint_callback"
120+
return "g@l"
121+
end
122+
123+
debugprint_logic(cache_request)
124+
125+
set_callback("v:lua.require'debugprint'.debugprint_callback")
126+
end
127+
128+
M.debugprint_callback = function()
129+
debugprint_cache()
130+
end
131+
132+
M.debugprint = function(o)
133+
o = o or {}
134+
o.prerepeat = true
135+
cache_request = nil
136+
return debugprint_cache(o)
137+
end
138+
104139
M.setup = function(o)
105140
opts = vim.tbl_deep_extend("force", OPTION_DEFAULTS, o or {})
106141

@@ -110,17 +145,17 @@ M.setup = function(o)
110145

111146
if opts.create_keymaps then
112147
vim.keymap.set("n", "dqp", function()
113-
M.debugprint()
114-
end)
148+
return M.debugprint()
149+
end, { expr = true })
115150
vim.keymap.set("n", "dqP", function()
116-
M.debugprint({ above = true })
117-
end)
151+
return M.debugprint({ above = true })
152+
end, { expr = true })
118153
vim.keymap.set("n", "dQp", function()
119-
M.debugprint({ variable = true })
120-
end)
154+
return M.debugprint({ variable = true })
155+
end, { expr = true })
121156
vim.keymap.set("n", "dQP", function()
122-
M.debugprint({ above = true, variable = true })
123-
end)
157+
return M.debugprint({ above = true, variable = true })
158+
end, { expr = true })
124159
end
125160

126161
-- Because we want to be idempotent, re-running setup() resets the counter

tests/debugprint.lua

+98-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ describe("add custom filetype with add_custom_filetypes()", function()
360360
"foo",
361361
"bar",
362362
})
363-
364363
local filename = write_file("foo")
365364
vim.api.nvim_win_set_cursor(0, { 1, 0 })
366365
feedkeys("dqp")
@@ -442,3 +441,101 @@ describe("move to new line", function()
442441
assert.are.same(vim.api.nvim_win_get_cursor(0), { 1, 0 })
443442
end)
444443
end)
444+
445+
describe("can repeat", function()
446+
before_each(function()
447+
debugprint.setup()
448+
end)
449+
450+
it("can insert a basic statement and repeat", function()
451+
set_lines({
452+
"foo",
453+
"bar",
454+
})
455+
456+
local filename = write_file("lua")
457+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
458+
feedkeys("dqp")
459+
feedkeys(".")
460+
461+
check_lines({
462+
"foo",
463+
"print('DEBUG[2]: " .. filename .. ":1')",
464+
"print('DEBUG[1]: " .. filename .. ":1')",
465+
"bar",
466+
})
467+
end)
468+
469+
it("can insert a basic statement and repeat above", function()
470+
set_lines({
471+
"foo",
472+
"bar",
473+
})
474+
475+
local filename = write_file("lua")
476+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
477+
feedkeys("dqP")
478+
feedkeys(".")
479+
480+
check_lines({
481+
"print('DEBUG[1]: " .. filename .. ":1')",
482+
"print('DEBUG[2]: " .. filename .. ":2')",
483+
"foo",
484+
"bar",
485+
})
486+
end)
487+
488+
it("can insert a basic statement and repeat in different directions", function()
489+
set_lines({
490+
"foo",
491+
"bar",
492+
})
493+
494+
local filename = write_file("lua")
495+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
496+
feedkeys("dqP")
497+
feedkeys(".")
498+
feedkeys("jdqp")
499+
feedkeys(".")
500+
501+
check_lines({
502+
"print('DEBUG[1]: " .. filename .. ":1')",
503+
"print('DEBUG[2]: " .. filename .. ":2')",
504+
"foo",
505+
"bar",
506+
"print('DEBUG[4]: " .. filename .. ":4')",
507+
"print('DEBUG[3]: " .. filename .. ":4')",
508+
})
509+
end)
510+
511+
it("can insert a variable statement and repeat", function()
512+
set_lines({
513+
"foo",
514+
"bar",
515+
})
516+
517+
local filename = write_file("lua")
518+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
519+
feedkeys("dQpbanana<CR>")
520+
feedkeys(".")
521+
feedkeys("dQPapple<CR>")
522+
feedkeys(".")
523+
524+
check_lines({
525+
"print('DEBUG[3]: "
526+
.. filename
527+
.. ":1: apple=' .. vim.inspect(apple))",
528+
"print('DEBUG[4]: "
529+
.. filename
530+
.. ":2: apple=' .. vim.inspect(apple))",
531+
"foo",
532+
"print('DEBUG[2]: "
533+
.. filename
534+
.. ":1: banana=' .. vim.inspect(banana))",
535+
"print('DEBUG[1]: "
536+
.. filename
537+
.. ":1: banana=' .. vim.inspect(banana))",
538+
"bar",
539+
})
540+
end)
541+
end)

0 commit comments

Comments
 (0)