Skip to content

Commit b843178

Browse files
author
Andrew Ferrier
committed
feat: Support variable insertion - closes #1
1 parent c373014 commit b843178

File tree

2 files changed

+125
-32
lines changed

2 files changed

+125
-32
lines changed

lua/debugprint/init.lua

+68-32
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,104 @@ local opts
55
OPTION_DEFAULTS = {
66
create_keymaps = true,
77
filetypes = {
8-
["lua"] = { "print('", "')" },
9-
["vim"] = { 'echo "', '"' },
8+
["lua"] = {
9+
left = "print('",
10+
right = "')",
11+
mid_var = "' .. vim.inspect(",
12+
right_var = "))",
13+
},
14+
["vim"] = {
15+
left = 'echo "',
16+
right = '"',
17+
mid_var = '" .. ',
18+
right_var = "",
19+
},
1020
},
1121
}
1222

1323
local counter = 0
1424

15-
local debuginfo = function()
25+
local debuginfo = function(variable_name)
1626
local current_line = vim.api.nvim_win_get_cursor(0)[1]
1727
counter = counter + 1
1828

19-
return "DEBUG: "
29+
local line = "DEBUG: "
2030
.. vim.fn.expand("%:t")
2131
.. ":"
2232
.. current_line
2333
.. " ["
2434
.. counter
2535
.. "]"
36+
37+
if variable_name ~= nil then
38+
line = line .. ": " .. variable_name .. "="
39+
end
40+
41+
return line
2642
end
2743

2844
local get_fix = function(filetype)
2945
if vim.tbl_contains(vim.tbl_keys(opts.filetypes), filetype) then
30-
return opts.filetypes[filetype][1], opts.filetypes[filetype][2]
46+
return opts.filetypes[filetype]
3147
else
3248
vim.notify(
3349
"Don't have debugprint configuration for filetype " .. filetype,
3450
vim.log.levels.WARN
3551
)
36-
return nil, nil
52+
return nil
3753
end
3854
end
3955

40-
M.debugprint = function(above)
56+
M.debugprint = function(o)
57+
local funcopts = vim.tbl_deep_extend(
58+
"force",
59+
{ above = false, variable = false },
60+
o or {}
61+
)
62+
63+
vim.validate({
64+
above = { funcopts.above, "boolean" },
65+
})
66+
4167
local current_line = vim.api.nvim_win_get_cursor(0)[1]
4268
local filetype = vim.api.nvim_get_option_value("filetype", {})
4369
local indent = string.rep(" ", vim.fn.indent(current_line))
44-
local prefix, postfix = get_fix(filetype)
70+
local fixes = get_fix(filetype)
4571

46-
if prefix == nil or postfix == nil then
72+
if fixes == nil then
4773
return
74+
end
75+
76+
local line_to_insert
77+
local line_to_insert_on
78+
79+
if funcopts.variable then
80+
local variable_name = vim.fn.input("Variable name: ")
81+
82+
line_to_insert = indent
83+
.. fixes.left
84+
.. debuginfo(variable_name)
85+
.. fixes.mid_var
86+
.. variable_name
87+
.. fixes.right_var
4888
else
49-
local line_to_insert = indent .. prefix .. debuginfo() .. postfix
50-
51-
local line_to_insert_on
52-
53-
if above then
54-
line_to_insert_on = current_line - 1
55-
else
56-
line_to_insert_on = current_line
57-
end
58-
59-
vim.api.nvim_buf_set_lines(
60-
0,
61-
line_to_insert_on,
62-
line_to_insert_on,
63-
true,
64-
{ line_to_insert }
65-
)
89+
line_to_insert = indent .. fixes.left .. debuginfo() .. fixes.right
6690
end
67-
end
6891

69-
M.debugprintvar = function(above) end
92+
if funcopts.above then
93+
line_to_insert_on = current_line - 1
94+
else
95+
line_to_insert_on = current_line
96+
end
97+
98+
vim.api.nvim_buf_set_lines(
99+
0,
100+
line_to_insert_on,
101+
line_to_insert_on,
102+
true,
103+
{ line_to_insert }
104+
)
105+
end
70106

71107
M.setup = function(o)
72108
opts = vim.tbl_deep_extend("force", OPTION_DEFAULTS, o or {})
@@ -77,16 +113,16 @@ M.setup = function(o)
77113

78114
if opts.create_keymaps then
79115
vim.keymap.set("n", "dqp", function()
80-
M.debugprint(false)
116+
M.debugprint()
81117
end)
82118
vim.keymap.set("n", "dqP", function()
83-
M.debugprint(true)
119+
M.debugprint({ above = true })
84120
end)
85121
vim.keymap.set("n", "dQp", function()
86-
M.debugprintvar(false)
122+
M.debugprint({ variable = true })
87123
end)
88124
vim.keymap.set("n", "dQP", function()
89-
M.debugprintvar(true)
125+
M.debugprint({ above = true, variable = true })
90126
end)
91127
end
92128

tests/debugprint.lua

+57
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,46 @@ describe("can do basic debug statement insertion", function()
107107
end)
108108
end)
109109

110+
describe("can do variable debug statement insertion", function()
111+
before_each(function()
112+
debugprint.setup()
113+
end)
114+
115+
it("can insert a variable statement below", function()
116+
set_lines({
117+
"foo",
118+
"bar",
119+
})
120+
121+
local filename = write_file("lua")
122+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
123+
feedkeys("dQpbanana<CR>")
124+
125+
check_lines({
126+
"foo",
127+
"print('DEBUG: " .. filename .. ":1 [1]: banana=' .. vim.inspect(banana))",
128+
"bar",
129+
})
130+
end)
131+
132+
it("can insert a variable statement above", function()
133+
set_lines({
134+
"foo",
135+
"bar",
136+
})
137+
138+
local filename = write_file("lua")
139+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
140+
feedkeys("dQPbanana<CR>")
141+
142+
check_lines({
143+
"print('DEBUG: " .. filename .. ":1 [1]: banana=' .. vim.inspect(banana))",
144+
"foo",
145+
"bar",
146+
})
147+
end)
148+
end)
149+
110150
describe("can do various file types", function()
111151
before_each(function()
112152
debugprint.setup()
@@ -129,6 +169,23 @@ describe("can do various file types", function()
129169
})
130170
end)
131171

172+
it("can handle a .vim file variable", function()
173+
set_lines({
174+
"foo",
175+
"bar",
176+
})
177+
178+
local filename = write_file("vim")
179+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
180+
feedkeys("dQpbanana<CR>")
181+
182+
check_lines({
183+
"foo",
184+
'echo "DEBUG: ' .. filename .. ':1 [1]: banana=" .. banana',
185+
"bar",
186+
})
187+
end)
188+
132189
it("can gracefully handle unknown filetypes", function()
133190
set_lines({
134191
"foo",

0 commit comments

Comments
 (0)