Skip to content

Commit 6ad5606

Browse files
author
Andrew Ferrier
committed
feat: Ignore blank calc'ing snippet - closes #55
1 parent c4219fd commit 6ad5606

File tree

2 files changed

+157
-24
lines changed

2 files changed

+157
-24
lines changed

lua/debugprint/init.lua

+45-24
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,54 @@ end
4646

4747
local counter = 0
4848

49-
local get_current_line_for_printing = function(current_line)
50-
local current_line_contents =
51-
vim.api.nvim_buf_get_lines(0, current_line - 1, current_line, true)[1]
49+
local get_trimmed_content_of_line = function(line)
50+
local line_contents = vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]
5251

5352
-- Remove whitespace and any quoting characters which could potentially
5453
-- cause a syntax error in the statement being printed, or any characters
5554
-- which could cause unintended interpolation of expressions
56-
current_line_contents = current_line_contents:gsub("^%s+", "") -- leading
57-
current_line_contents = current_line_contents:gsub("%s+$", "") -- trailing
58-
current_line_contents = current_line_contents:gsub("[\"'\\`%${}]", "")
59-
60-
if current_line_contents:len() > MAX_SNIPPET_LENGTH then
61-
current_line_contents = string.sub(
62-
current_line_contents,
63-
0,
64-
MAX_SNIPPET_LENGTH
65-
) .. ""
55+
line_contents = line_contents:gsub("^%s+", "") -- leading
56+
line_contents = line_contents:gsub("%s+$", "") -- trailing
57+
line_contents = line_contents:gsub("[\"'\\`%${}]", "")
58+
59+
return line_contents
60+
end
61+
62+
local get_snippet = function(current_line, above)
63+
local line_contents = ""
64+
65+
while line_contents == "" do
66+
line_contents = get_trimmed_content_of_line(current_line)
67+
68+
if line_contents == "" then
69+
if above then
70+
current_line = current_line + 1
71+
else
72+
current_line = current_line - 1
73+
end
74+
75+
if current_line < 1 then
76+
return "(start of file)"
77+
end
78+
79+
if current_line > vim.api.nvim_buf_line_count(0) then
80+
return "(end of file)"
81+
end
82+
end
83+
end
84+
85+
if line_contents:len() > MAX_SNIPPET_LENGTH then
86+
line_contents = string.sub(line_contents, 0, MAX_SNIPPET_LENGTH)
87+
.. ""
6688
end
6789

68-
return current_line_contents
90+
if above then
91+
line_contents = "(before " .. line_contents .. ")"
92+
else
93+
line_contents = "(after " .. line_contents .. ")"
94+
end
95+
96+
return line_contents
6997
end
7098

7199
local debuginfo = function(opts)
@@ -82,18 +110,11 @@ local debuginfo = function(opts)
82110
line = line .. ": " .. vim.fn.expand("%:t") .. ":" .. current_line
83111

84112
if global_opts.display_snippet and opts.variable_name == nil then
85-
local snippet
113+
local snippet = get_snippet(current_line, opts.above)
86114

87-
if opts.above then
88-
snippet = " (before "
89-
else
90-
snippet = " (after "
115+
if snippet then
116+
line = line .. " " .. snippet
91117
end
92-
93-
line = line
94-
.. snippet
95-
.. get_current_line_for_printing(current_line)
96-
.. ")"
97118
end
98119

99120
if opts.variable_name ~= nil then

tests/debugprint.lua

+112
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,118 @@ describe("snippet handling", function()
144144
end)
145145
end)
146146

147+
describe("will ignore blank lines when calculating snippet", function()
148+
before_each(function()
149+
debugprint.setup({ ignore_treesitter = true })
150+
end)
151+
152+
it("can insert a basic statement above", function()
153+
local filename = init_file({
154+
"foo",
155+
"",
156+
"",
157+
"bar",
158+
}, "lua", 3, 0)
159+
160+
feedkeys("g?P")
161+
162+
check_lines({
163+
"foo",
164+
"",
165+
"print('DEBUGPRINT[1]: " .. filename .. ":3 (before bar)')",
166+
"",
167+
"bar",
168+
})
169+
end)
170+
171+
it("can insert a basic statement below", function()
172+
local filename = init_file({
173+
"foo",
174+
"",
175+
"",
176+
"bar",
177+
}, "lua", 2, 0)
178+
179+
feedkeys("g?p")
180+
181+
check_lines({
182+
"foo",
183+
"",
184+
"print('DEBUGPRINT[1]: " .. filename .. ":2 (after foo)')",
185+
"",
186+
"bar",
187+
})
188+
end)
189+
190+
it("can insert a basic statement above first line", function()
191+
local filename = init_file({
192+
"",
193+
"foo",
194+
"bar",
195+
}, "lua", 1, 0)
196+
197+
feedkeys("g?P")
198+
199+
check_lines({
200+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (before foo)')",
201+
"",
202+
"foo",
203+
"bar",
204+
})
205+
end)
206+
207+
it("can insert a basic statement below last line", function()
208+
local filename = init_file({
209+
"foo",
210+
"bar",
211+
"",
212+
}, "lua", 3, 0)
213+
214+
feedkeys("g?p")
215+
216+
check_lines({
217+
"foo",
218+
"bar",
219+
"",
220+
"print('DEBUGPRINT[1]: " .. filename .. ":3 (after bar)')",
221+
})
222+
end)
223+
224+
it("can insert a basic statement before first line", function()
225+
local filename = init_file({
226+
"",
227+
"foo",
228+
"bar",
229+
}, "lua", 1, 0)
230+
231+
feedkeys("g?p")
232+
233+
check_lines({
234+
"",
235+
"print('DEBUGPRINT[1]: " .. filename .. ":1 (start of file)')",
236+
"foo",
237+
"bar",
238+
})
239+
end)
240+
241+
it("can insert a basic statement above last line", function()
242+
local filename = init_file({
243+
"foo",
244+
"bar",
245+
"",
246+
}, "lua", 3, 0)
247+
248+
feedkeys("g?P")
249+
250+
check_lines({
251+
"foo",
252+
"bar",
253+
"print('DEBUGPRINT[1]: " .. filename .. ":3 (end of file)')",
254+
"",
255+
})
256+
end)
257+
end)
258+
147259
describe("can do variable debug statement insertion", function()
148260
before_each(function()
149261
debugprint.setup({ ignore_treesitter = true })

0 commit comments

Comments
 (0)