Skip to content

Commit ffaaee1

Browse files
author
Andrew Ferrier
committed
feat: Include snippets - closes #33
1 parent c529cbc commit ffaaee1

File tree

3 files changed

+145
-41
lines changed

3 files changed

+145
-41
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ end)
179179
| `create_keymaps` | `true` | Creates default keymappings - see above |
180180
| `move_to_debugline` | `false` | When adding a debug line, moves the cursor to that line |
181181
| `display_counter` | `true` | Whether to display/include the monotonically increasing counter in each debug message added |
182+
| `display_snippet` | `true` | Whether to include a snippet of the line above/below in plain debug lines |
182183
| `filetypes` | See below | Custom filetypes - see below |
183184
| `ignore_treesitter` | `false` | Never use treesitter to find a variable under the cursor, always prompt for it - overrides the same setting on `debugprint()` if set to true |
184185
| `print_tag` | `DEBUGPRINT` | The string inserted into each print statement, which can be used to uniquely identify statements inserted by `debugprint`. |

lua/debugprint/init.lua

+50-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ GLOBAL_OPTION_DEFAULTS = {
88
create_keymaps = true,
99
create_commands = true,
1010
display_counter = true,
11+
display_snippet = true,
1112
move_to_debugline = false,
1213
ignore_treesitter = false,
1314
filetypes = require("debugprint.filetypes"),
@@ -20,11 +21,14 @@ FUNCTION_OPTION_DEFAULTS = {
2021
ignore_treesitter = false,
2122
}
2223

24+
MAX_SNIPPET_LENGTH = 40
25+
2326
local validate_global_opts = function(o)
2427
vim.validate({
2528
create_keymaps = { o.create_keymaps, "boolean" },
2629
create_commands = { o.create_commands, "boolean" },
2730
display_counter = { o.move_to_debugline, "boolean" },
31+
display_snippet = { o.move_to_debugline, "boolean" },
2832
move_to_debugline = { o.move_to_debugline, "boolean" },
2933
ignore_treesitter = { o.ignore_treesitter, "boolean" },
3034
filetypes = { o.filetypes, "table" },
@@ -42,8 +46,33 @@ end
4246

4347
local counter = 0
4448

45-
local debuginfo = function(variable_name)
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]
52+
53+
-- Remove whitespace and any quoting characters which could potentially
54+
-- cause a syntax error in the statement being printed.
55+
current_line_contents = current_line_contents:gsub("^%s+", "")
56+
current_line_contents = current_line_contents:gsub("%s+$", "")
57+
current_line_contents = current_line_contents:gsub('"', "")
58+
current_line_contents = current_line_contents:gsub("'", "")
59+
current_line_contents = current_line_contents:gsub("\\", "")
60+
current_line_contents = current_line_contents:gsub("`", "")
61+
62+
if current_line_contents:len() > MAX_SNIPPET_LENGTH then
63+
current_line_contents = string.sub(
64+
current_line_contents,
65+
0,
66+
MAX_SNIPPET_LENGTH
67+
) .. ""
68+
end
69+
70+
return current_line_contents
71+
end
72+
73+
local debuginfo = function(opts)
4674
local current_line = vim.api.nvim_win_get_cursor(0)[1]
75+
4776
counter = counter + 1
4877

4978
local line = global_opts.print_tag
@@ -54,8 +83,23 @@ local debuginfo = function(variable_name)
5483

5584
line = line .. ": " .. vim.fn.expand("%:t") .. ":" .. current_line
5685

57-
if variable_name ~= nil then
58-
line = line .. ": " .. variable_name .. "="
86+
if global_opts.display_snippet and opts.variable_name == nil then
87+
local snippet
88+
89+
if opts.above then
90+
snippet = " (before "
91+
else
92+
snippet = " (after "
93+
end
94+
95+
line = line
96+
.. snippet
97+
.. get_current_line_for_printing(current_line)
98+
.. ")"
99+
end
100+
101+
if opts.variable_name ~= nil then
102+
line = line .. ": " .. opts.variable_name .. "="
59103
end
60104

61105
return line
@@ -109,12 +153,13 @@ local debugprint_addline = function(opts)
109153

110154
if opts.variable_name then
111155
line_to_insert_content = fixes.left
112-
.. debuginfo(opts.variable_name)
156+
.. debuginfo(opts)
113157
.. fixes.mid_var
114158
.. opts.variable_name
115159
.. fixes.right_var
116160
else
117-
line_to_insert_content = fixes.left .. debuginfo() .. fixes.right
161+
opts.variable_name = nil
162+
line_to_insert_content = fixes.left .. debuginfo(opts) .. fixes.right
118163
end
119164

120165
-- Inserting the leading space from the current line effectively acts as a

0 commit comments

Comments
 (0)