Skip to content

Commit 17939a5

Browse files
committed
feat: Add debugprint 'surrounding' - closes #162
1 parent 649ad63 commit 17939a5

File tree

5 files changed

+270
-2
lines changed

5 files changed

+270
-2
lines changed

lua/debugprint/init.lua

+20-2
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,16 @@ local handle_debugprint_line = function(opts)
208208
line_nr = line_nr - 1
209209
end
210210

211+
local move_to_debugline = global_opts.move_to_debugline
212+
213+
if opts.surround == true then
214+
move_to_debugline = false
215+
end
216+
211217
utils_buffer.insert_and_indent_line(
212218
line_nr,
213219
line_content,
214-
global_opts.move_to_debugline
220+
move_to_debugline
215221
)
216222
end
217223
end
@@ -220,7 +226,18 @@ local cache_request = {}
220226

221227
---@return nil
222228
M.debugprint_operatorfunc_regular = function()
223-
handle_debugprint_line(cache_request)
229+
if cache_request.surround then
230+
local cache_request_copy = vim.deepcopy(cache_request)
231+
232+
cache_request_copy.above = true
233+
handle_debugprint_line(cache_request_copy)
234+
235+
cache_request_copy.above = false
236+
handle_debugprint_line(cache_request_copy)
237+
else
238+
handle_debugprint_line(cache_request)
239+
end
240+
224241
utils_operator.set_callback(
225242
"v:lua.require'debugprint'.debugprint_operatorfunc_regular"
226243
)
@@ -252,6 +269,7 @@ M.debugprint = function(opts)
252269

253270
opts.register = require("debugprint.utils.register").register_named()
254271

272+
assert(not (opts.surround and opts.register))
255273
assert(not (opts.insert and opts.register))
256274

257275
if not utils_buffer.is_modifiable() then

lua/debugprint/options.lua

+11
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ local GLOBAL_OPTION_DEFAULTS = {
1010
variable_above = "g?V",
1111
variable_below_alwaysprompt = "",
1212
variable_above_alwaysprompt = "",
13+
surround_plain = "g?sp",
14+
surround_variable = "g?sv",
15+
surround_variable_alwaysprompt = "",
1316
textobj_below = "g?o",
1417
textobj_above = "g?O",
18+
textobj_surround = "g?so",
1519
toggle_comment_debug_prints = "",
1620
delete_debug_prints = "",
1721
},
@@ -95,8 +99,15 @@ local validate_global_opts = function(o)
9599
normal.variable_above_alwaysprompt,
96100
STRING_FALSE_NIL,
97101
},
102+
surround_plain = { normal.surround_plain, STRING_FALSE_NIL },
103+
surround_variable = { normal.surround_variable, STRING_FALSE_NIL },
104+
surround_variable_alwaysprompt = {
105+
normal.surround_variable_alwaysprompt,
106+
STRING_FALSE_NIL,
107+
},
98108
textobj_below = { normal.textobj_below, STRING_FALSE_NIL },
99109
textobj_above = { normal.textobj_above, STRING_FALSE_NIL },
110+
textobj_surround = { normal.textobj_surround, STRING_FALSE_NIL },
100111
delete_debug_prints = {
101112
normal.delete_debug_prints,
102113
STRING_FALSE_NIL,

lua/debugprint/setup.lua

+36
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ M.map_keys_and_commands = function(global_opts)
7777
desc = "Variable debug above current line (always prompt)",
7878
})
7979

80+
map_key("n", global_opts.keymaps.normal.surround_plain, {
81+
callback = function()
82+
debugprint.debugprint({ surround = true })
83+
end,
84+
desc = "Surround plain debug",
85+
})
86+
87+
map_key("n", global_opts.keymaps.normal.surround_variable, {
88+
callback = function()
89+
debugprint.debugprint({ surround = true, variable = true })
90+
end,
91+
desc = "Surround variable debug",
92+
})
93+
94+
map_key("n", global_opts.keymaps.normal.surround_variable_alwaysprompt, {
95+
callback = function()
96+
debugprint.debugprint({
97+
surround = true,
98+
variable = true,
99+
ignore_treesitter = true,
100+
})
101+
end,
102+
desc = "Surround variable debug (always prompt)",
103+
})
104+
80105
map_key("n", global_opts.keymaps.normal.textobj_below, {
81106
callback = function()
82107
return debugprint.debugprint({ motion = true })
@@ -96,6 +121,17 @@ M.map_keys_and_commands = function(global_opts)
96121
desc = "Text-obj-selected variable debug above current line",
97122
})
98123

124+
map_key("n", global_opts.keymaps.normal.textobj_surround, {
125+
callback = function()
126+
return debugprint.debugprint({
127+
motion = true,
128+
surround = true,
129+
})
130+
end,
131+
expr = true,
132+
desc = "Text-obj-selected variable debug surrounded",
133+
})
134+
99135
map_key("i", global_opts.keymaps.insert.plain, {
100136
callback = function()
101137
debugprint.debugprint({ insert = true })

lua/debugprint/types.lua

+5
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@
4848
---@field variable_above? string|false
4949
---@field variable_below_alwaysprompt? string|false
5050
---@field variable_above_alwaysprompt? string|false
51+
---@field surround_plain? string|false
52+
---@field surround_variable? string|false
53+
---@field surround_variable_alwaysprompt? string|false
5154
---@field textobj_below? string|false
5255
---@field textobj_above? string|false
56+
---@field textobj_surround? string|false
5357
---@field delete_debug_prints? string|false
5458
---@field toggle_comment_debug_prints? string|false
5559

@@ -72,6 +76,7 @@
7276
---@field ignore_treesitter? boolean
7377
---@field insert? boolean
7478
---@field motion? boolean
79+
---@field surround? boolean
7580

7681
---@class debugprint.FunctionOptionsInternal: debugprint.FunctionOptions
7782
---@field variable_name? string

tests/debugprint.lua

+198
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ local ALWAYS_PROMPT_KEYMAP = {
8888
local teardown = function(opts)
8989
opts = vim.tbl_extend("keep", opts or {}, { reset_counter = true })
9090

91+
-- Reset filetypes
92+
debugprint.setup({ filetypes = require("debugprint.filetypes") })
93+
9194
notify_message = nil
9295
pcall(vim.keymap.del, "n", "g?p")
9396
pcall(vim.keymap.del, "n", "g?P")
@@ -3161,3 +3164,198 @@ describe("dynamic filetype configuration", function()
31613164
})
31623165
end)
31633166
end)
3167+
3168+
describe("double statement insertion", function()
3169+
after_each(teardown)
3170+
3171+
it("plain", function()
3172+
assert.equals(notify_message, nil)
3173+
3174+
local filename = init_file({
3175+
"foo",
3176+
"bar",
3177+
}, "lua", 1, 0)
3178+
3179+
feedkeys("g?sp")
3180+
3181+
check_lines({
3182+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (before foo)')",
3183+
"foo",
3184+
"print('DEBUGPRINT[2]: " .. filename .. ":2 (after foo)')",
3185+
"bar",
3186+
})
3187+
3188+
assert.equals(notify_message, nil)
3189+
end)
3190+
3191+
it("plain - undo is atomic", function()
3192+
assert.equals(notify_message, nil)
3193+
3194+
local filename = init_file({
3195+
"foo",
3196+
"bar",
3197+
}, "lua", 1, 0)
3198+
3199+
feedkeys("g?sp")
3200+
3201+
check_lines({
3202+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (before foo)')",
3203+
"foo",
3204+
"print('DEBUGPRINT[2]: " .. filename .. ":2 (after foo)')",
3205+
"bar",
3206+
})
3207+
3208+
feedkeys("u")
3209+
3210+
check_lines({
3211+
"foo",
3212+
"bar",
3213+
})
3214+
3215+
assert.equals(notify_message, nil)
3216+
end)
3217+
3218+
it("plain - repeat", function()
3219+
assert.equals(notify_message, nil)
3220+
3221+
local filename = init_file({
3222+
"foo",
3223+
"bar",
3224+
}, "lua", 1, 0)
3225+
3226+
feedkeys("g?sp")
3227+
feedkeys("jj")
3228+
feedkeys("g?sp")
3229+
3230+
check_lines({
3231+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (before foo)')",
3232+
"foo",
3233+
"print('DEBUGPRINT[2]: " .. filename .. ":2 (after foo)')",
3234+
"print('DEBUGPRINT[3]: " .. filename .. ":4 (before bar)')",
3235+
"bar",
3236+
"print('DEBUGPRINT[4]: " .. filename .. ":5 (after bar)')",
3237+
})
3238+
3239+
assert.equals(notify_message, nil)
3240+
end)
3241+
3242+
it("plain - complex indentation", function()
3243+
assert.equals(notify_message, nil)
3244+
3245+
local filename = init_file({
3246+
"function()",
3247+
" foo = 1",
3248+
"end",
3249+
}, "lua", 1, 0)
3250+
3251+
feedkeys("g?sp")
3252+
3253+
check_lines({
3254+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (before function())')",
3255+
"function()",
3256+
" print('DEBUGPRINT[2]: "
3257+
.. filename
3258+
.. ":2 (after function())')",
3259+
" foo = 1",
3260+
"end",
3261+
})
3262+
3263+
assert.equals(notify_message, nil)
3264+
end)
3265+
3266+
it("variable", function()
3267+
local filename = init_file({
3268+
"local foo = 1",
3269+
"local bar = 2",
3270+
}, "lua", 1, 7)
3271+
3272+
feedkeys("g?sv")
3273+
3274+
check_lines({
3275+
"print('DEBUGPRINT[1]: "
3276+
.. filename
3277+
.. ":1: foo=' .. vim.inspect(foo))",
3278+
"local foo = 1",
3279+
"print('DEBUGPRINT[2]: "
3280+
.. filename
3281+
.. ":2: foo=' .. vim.inspect(foo))",
3282+
"local bar = 2",
3283+
})
3284+
3285+
assert.equals(notify_message, nil)
3286+
end)
3287+
3288+
it("variable (prompt)", function()
3289+
local filename = init_file({
3290+
"-- local foo = 1",
3291+
"local bar = 2",
3292+
}, "lua", 1, 10)
3293+
3294+
feedkeys("g?sv<CR>")
3295+
3296+
check_lines({
3297+
"print('DEBUGPRINT[1]: "
3298+
.. filename
3299+
.. ":1: foo=' .. vim.inspect(foo))",
3300+
"-- local foo = 1",
3301+
"print('DEBUGPRINT[2]: "
3302+
.. filename
3303+
.. ":2: foo=' .. vim.inspect(foo))",
3304+
"local bar = 2",
3305+
})
3306+
3307+
assert.equals(notify_message, nil)
3308+
end)
3309+
3310+
it("variable (always prompt)", function()
3311+
debugprint.setup({
3312+
keymaps = {
3313+
normal = {
3314+
surround_variable_alwaysprompt = "g?sz",
3315+
},
3316+
},
3317+
})
3318+
3319+
local filename = init_file({
3320+
"local foo = 1",
3321+
"local bar = 2",
3322+
}, "lua", 1, 7)
3323+
3324+
feedkeys("g?sz<CR>")
3325+
3326+
check_lines({
3327+
"print('DEBUGPRINT[1]: "
3328+
.. filename
3329+
.. ":1: foo=' .. vim.inspect(foo))",
3330+
"local foo = 1",
3331+
"print('DEBUGPRINT[2]: "
3332+
.. filename
3333+
.. ":2: foo=' .. vim.inspect(foo))",
3334+
"local bar = 2",
3335+
})
3336+
3337+
assert.equals(notify_message, nil)
3338+
end)
3339+
3340+
it("variable (textobj)", function()
3341+
local filename = init_file({
3342+
"local foo = 1",
3343+
"local bar = 2",
3344+
}, "lua", 1, 7)
3345+
3346+
feedkeys("g?soiw")
3347+
3348+
check_lines({
3349+
"print('DEBUGPRINT[1]: "
3350+
.. filename
3351+
.. ":1: foo=' .. vim.inspect(foo))",
3352+
"local foo = 1",
3353+
"print('DEBUGPRINT[2]: "
3354+
.. filename
3355+
.. ":2: foo=' .. vim.inspect(foo))",
3356+
"local bar = 2",
3357+
})
3358+
3359+
assert.equals(notify_message, nil)
3360+
end)
3361+
end)

0 commit comments

Comments
 (0)