Skip to content

Commit bb47042

Browse files
committed
feat: Support insert mode - closes #86
1 parent 575f2e3 commit bb47042

File tree

7 files changed

+122
-10
lines changed

7 files changed

+122
-10
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ the NeoVim generation. It:
3838
or will prompt with a sensible default. It understands Treesitter embedded
3939
languages (e.g. JavaScript-in-HTML).
4040

41-
* Provides [keymappings](#keymappings-and-commands) for normal, visual, and operator-pending modes.
41+
* Provides [keymappings](#keymappings-and-commands) for normal, insert,
42+
visual, and operator-pending modes.
4243

4344
* Provides [commands](#keymappings-and-commands) to delete debugging lines added to the current buffer or
4445
comment/uncomment those lines.
@@ -100,6 +101,8 @@ following table.
100101
| Normal | None | Variable debug (always prompt for variable) | Above |
101102
| Normal | None | Delete debug lines in buffer | - |
102103
| Normal | None | Comment/uncomment debug lines in buffer | - |
104+
| Insert | `Ctrl-G p` | Plain debug | In-place |
105+
| Insert | `Ctrl-G v` | Variable debug (always prompt for variable) | In-place |
103106
| Visual | `g?v` | Variable debug | Below |
104107
| Visual | `g?V` | Variable debug | Above |
105108
| Op-pending | `g?o` | Variable debug | Below |
@@ -129,6 +132,10 @@ return {
129132
toggle_comment_debug_prints = nil,
130133
delete_debug_prints = nil,
131134
},
135+
insert = {
136+
plain = "<C-G>p",
137+
variable = "<C-G>v",
138+
},
132139
visual = {
133140
variable_below = "g?v",
134141
variable_above = "g?V",
@@ -196,7 +203,8 @@ they are used to convert sections to ROT-13, which most folks don't use.
196203
| Print variables using treesitter | :+1: | :+1: | :x: | :+1: | :x: | :x: |
197204
| Print variables/expressions using prompts | :+1: | :x: | :x: | :x: | :x: | :x: |
198205
| Print variables using motions | :+1: | :x: | :+1: | :x: | :x: | :x: |
199-
| Print variables using visual mode | :+1: | :+1: | :+1: | :+1: | :+1: | :x: |
206+
| Add plain or variable debug lines in insert mode | :+1: | :x: | :x:: | :x: | :x: | :x: |
207+
| Add variable debug lines in visual mode | :+1: | :+1: | :+1: | :+1: | :+1: | :x: |
200208
| Print assertions | :x: | :+1: | :x: | :x: | :x: | :x: |
201209
| Print stack traces | :x: | :+1: | :x: | :x: | :x: | :x: |
202210
| Add time-tracking logic | :x: | :+1: | :x: | :x: | :x: | :x: |

SHOWCASE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ return {
111111

112112
-- opts = { … },
113113

114-
-- The 'keys' and 'cmds' sections of this configuration will need to be changed if you
114+
-- The 'keys' and 'cmds' sections of this configuration will need to be adjusted if you
115115
-- customize the keys/commands.
116116

117117
keys = {
118118
{ "g?", mode = 'n' },
119119
{ "g?", mode = 'x' },
120+
{ "<C-G>", mode = 'i' },
120121
},
121122
cmd = {
122123
"ToggleCommentDebugPrints",

lua/debugprint/init.lua

+19-7
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ local construct_debugprint_line = function(opts, fileconfig)
176176
end
177177

178178
---@param opts DebugprintFunctionOptionsInternal
179-
---@return nil
180-
local insert_debugprint_line = function(opts)
179+
---@return string
180+
local get_debugprint_line = function(opts)
181181
local line_to_insert
182182

183183
local fileconfig = get_filetype_config()
@@ -193,6 +193,14 @@ local insert_debugprint_line = function(opts)
193193
)
194194
end
195195

196+
return line_to_insert
197+
end
198+
199+
---@param opts DebugprintFunctionOptionsInternal
200+
---@return nil
201+
local insert_debugprint_line = function(opts)
202+
local line_to_insert = get_debugprint_line(opts)
203+
196204
-- Inserting the leading space from the current line effectively acts as a
197205
-- 'default' indent for languages like Python, where the NeoVim or Treesitter
198206
-- indenter doesn't know how to indent them.
@@ -260,11 +268,15 @@ M.debugprint_regular = function(opts)
260268
end
261269
end
262270

263-
cache_request = opts
264-
utils_operator.set_operatorfunc(
265-
"v:lua.require'debugprint'.debugprint_operatorfunc_regular"
266-
)
267-
return "g@l"
271+
if opts.insert == true then
272+
return get_debugprint_line(opts)
273+
else
274+
cache_request = opts
275+
utils_operator.set_operatorfunc(
276+
"v:lua.require'debugprint'.debugprint_operatorfunc_regular"
277+
)
278+
return "g@l"
279+
end
268280
end
269281

270282
---@param opts? DebugprintFunctionOptions

lua/debugprint/options.lua

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ local GLOBAL_OPTION_DEFAULTS = {
1515
toggle_comment_debug_prints = nil,
1616
delete_debug_prints = nil,
1717
},
18+
insert = {
19+
plain = "<C-G>p",
20+
variable = "<C-G>v",
21+
},
1822
visual = {
1923
variable_below = "g?v",
2024
variable_above = "g?V",

lua/debugprint/setup.lua

+20
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ M.map_keys_and_commands = function(global_opts)
9494
desc = "Text-obj-selected variable debug above current line",
9595
})
9696

97+
map_key("i", global_opts.keymaps.insert.plain, {
98+
callback = function()
99+
return debugprint.debugprint({ insert = true })
100+
end,
101+
expr = true,
102+
desc = "Plain debug in-place",
103+
})
104+
105+
map_key("i", global_opts.keymaps.insert.variable, {
106+
callback = function()
107+
return debugprint.debugprint({
108+
insert = true,
109+
variable = true,
110+
ignore_treesitter = true,
111+
})
112+
end,
113+
expr = true,
114+
desc = "Variable debug in-place (always prompt)",
115+
})
116+
97117
map_key("x", global_opts.keymaps.visual.variable_below, {
98118
callback = function()
99119
feedkeys(debugprint.debugprint({ variable = true }))

lua/debugprint/types.lua

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
---@class DebugprintKeymapOptions
3030
---@field normal? DebugprintKeymapNormalOptions
31+
---@field insert? DebugprintKeymapInsertOptions
3132
---@field visual? DebugprintKeymapVisualOptions
3233

3334
---@class DebugprintKeymapNormalOptions
@@ -42,6 +43,10 @@
4243
---@field delete_debug_prints? string
4344
---@field toggle_comment_debug_prints? string
4445

46+
---@class DebugprintKeymapInsertOptions
47+
---@field plain? string
48+
---@field variable? string
49+
4550
---@class DebugprintKeymapVisualOptions
4651
---@field variable_below? string
4752
---@field variable_above? string
@@ -54,6 +59,7 @@
5459
---@field above? boolean
5560
---@field variable? boolean
5661
---@field ignore_treesitter? boolean
62+
---@field insert? boolean
5763

5864
---@class DebugprintFunctionOptionsInternal: DebugprintFunctionOptions
5965
---@field motion? boolean

tests/debugprint.lua

+61
Original file line numberDiff line numberDiff line change
@@ -2412,3 +2412,64 @@ describe("allow display_* to be set in filetypes", function()
24122412
})
24132413
end)
24142414
end)
2415+
2416+
describe("can support insert mode", function()
2417+
before_each(function()
2418+
debugprint.setup()
2419+
end)
2420+
2421+
after_each(teardown)
2422+
2423+
it("can insert a basic statement below", function()
2424+
assert.equals(notify_message, nil)
2425+
2426+
local filename = init_file({
2427+
"foo",
2428+
"bar",
2429+
}, "lua", 1, 0)
2430+
2431+
feedkeys("o<C-G>p")
2432+
2433+
check_lines({
2434+
"foo",
2435+
"print('DEBUGPRINT[1]: " .. filename .. ":2 (after foo)')",
2436+
"bar",
2437+
})
2438+
end)
2439+
2440+
it("can insert a variable statement below", function()
2441+
assert.equals(notify_message, nil)
2442+
2443+
local filename = init_file({
2444+
"foo",
2445+
"bar",
2446+
}, "lua", 1, 0)
2447+
2448+
feedkeys("o<C-G>vwibble<CR>")
2449+
2450+
check_lines({
2451+
"foo",
2452+
"print('DEBUGPRINT[1]: "
2453+
.. filename
2454+
.. ":2: wibble=' .. vim.inspect(wibble))",
2455+
"bar",
2456+
})
2457+
end)
2458+
2459+
it("don't insert when skipping variable name", function()
2460+
assert.equals(notify_message, nil)
2461+
2462+
init_file({
2463+
"foo",
2464+
"bar",
2465+
}, "lua", 1, 0)
2466+
2467+
feedkeys("o<C-G>v<CR>")
2468+
2469+
check_lines({
2470+
"foo",
2471+
"",
2472+
"bar",
2473+
})
2474+
end)
2475+
end)

0 commit comments

Comments
 (0)