Skip to content

Commit 0a2a860

Browse files
committed
feat: Add display_location and enhance tests - closes #122
1 parent 1275d42 commit 0a2a860

File tree

5 files changed

+202
-17
lines changed

5 files changed

+202
-17
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ reason, please open an
189189
| Option | Default | Purpose |
190190
| ------------------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
191191
| `move_to_debugline` | `false` | When adding a debug line, moves the cursor to that line |
192-
| `display_counter` | `true` | Whether to display/include the increasing integer counter in each debug message. Can also be set to a function to customize, see below |
193-
| `display_snippet` | `true` | Whether to include a snippet of the line above/below in plain debug lines |
192+
| `display_location` | `true` | Include the filename and linenumber of the line being debugged in the debug message |
193+
| `display_counter` | `true` | Include the increasing integer counter in the debug message. Can also be set to a function to customize, see below |
194+
| `display_snippet` | `true` | Include a snippet of the line above/below in the debug message (plain debug lines only) for context |
194195
| `filetypes` | See below | Custom filetypes - see below |
195196
| `print_tag` | `DEBUGPRINT` | The string inserted into each print statement, which can be used to uniquely identify statements inserted by `debugprint`. If you set this to `''`, no print tag will be included, but this will disable the ability to delete or comment print statements via `debugprint` |
196197

lua/debugprint/init.lua

+49-15
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,73 @@ local default_display_counter = function()
5656
return "[" .. tostring(default_counter) .. "]"
5757
end
5858

59-
---@param opts DebugprintFunctionOptionsInternal
6059
---@return string
61-
local debuginfo = function(opts)
62-
local current_line = vim.api.nvim_win_get_cursor(0)[1]
63-
64-
local line
60+
local debuginfo_tag_and_counter = function()
61+
local tag_and_counter = ""
6562

6663
if global_opts.print_tag then
67-
line = global_opts.print_tag
64+
tag_and_counter = global_opts.print_tag
6865
end
6966

7067
if global_opts.display_counter == true then
71-
line = line .. default_display_counter()
68+
tag_and_counter = tag_and_counter .. default_display_counter()
7269
elseif type(global_opts.display_counter) == "function" then
73-
line = line .. tostring(global_opts.display_counter())
70+
tag_and_counter = tag_and_counter
71+
.. tostring(global_opts.display_counter())
7472
end
7573

76-
if line ~= "" then
77-
line = line .. ": "
74+
return tag_and_counter
75+
end
76+
77+
---@param opts DebugprintFunctionOptionsInternal
78+
---@return string
79+
local debuginfo = function(opts)
80+
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1]
81+
82+
local line_components = {}
83+
local force_snippet_for_plain = false
84+
85+
if
86+
not global_opts.display_location
87+
and not global_opts.display_snippet
88+
and not global_opts.display_counter
89+
and global_opts.print_tag == ""
90+
then
91+
force_snippet_for_plain = true
7892
end
7993

80-
line = line .. vim.fn.expand("%:t") .. ":" .. current_line
94+
local tag_and_counter = debuginfo_tag_and_counter()
95+
96+
if tag_and_counter ~= "" then
97+
table.insert(line_components, tag_and_counter .. ":")
98+
end
8199

82-
if global_opts.display_snippet and opts.variable_name == nil then
83-
local snippet = get_snippet(current_line, opts.above)
100+
if global_opts.display_location then
101+
table.insert(
102+
line_components,
103+
vim.fn.expand("%:t") .. ":" .. current_line_nr
104+
)
105+
end
106+
107+
if
108+
(global_opts.display_snippet or force_snippet_for_plain)
109+
and opts.variable_name == nil
110+
then
111+
local snippet = get_snippet(current_line_nr, opts.above)
84112

85113
if snippet then
86-
line = line .. " " .. snippet
114+
table.insert(line_components, snippet)
87115
end
88116
end
89117

118+
local line = vim.fn.trim(table.concat(line_components, " "), ":")
119+
90120
if opts.variable_name ~= nil then
91-
line = line .. ": " .. opts.variable_name .. "="
121+
if line ~= "" then
122+
line = line .. ": "
123+
end
124+
125+
line = line .. opts.variable_name .. "="
92126
end
93127

94128
return line

lua/debugprint/options.lua

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ local GLOBAL_OPTION_DEFAULTS = {
2525
delete_debug_prints = "DeleteDebugPrints",
2626
},
2727
display_counter = true,
28+
display_location = true,
2829
display_snippet = true,
2930
move_to_debugline = false,
3031
filetypes = require("debugprint.filetypes"),
@@ -40,6 +41,7 @@ local validate_global_opts = function(o)
4041
keymaps = { o.keymaps, "table" },
4142
commands = { o.commands, "table" },
4243
display_counter = { o.display_counter, { "function", "boolean" } },
44+
display_location = { o.display_location, "boolean" },
4345
display_snippet = { o.display_snippet, "boolean" },
4446
move_to_debugline = { o.move_to_debugline, "boolean" },
4547
filetypes = { o.filetypes, "table" },

lua/debugprint/types.lua

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
---@field keymaps? table
1414
---@field commands? table
1515
---@field display_counter? boolean
16+
---@field display_location? boolean
1617
---@field display_snippet? boolean
1718
---@field move_to_debugline? boolean
1819
---@field filetypes? DebugprintFileTypeConfig[]

tests/debugprint.lua

+147
Original file line numberDiff line numberDiff line change
@@ -2171,3 +2171,150 @@ describe("check for variations of printtag/display_counter", function()
21712171
})
21722172
end)
21732173
end)
2174+
2175+
describe("variations of display_* options", function()
2176+
after_each(teardown)
2177+
2178+
it("no display_location", function()
2179+
debugprint.setup({ display_location = false })
2180+
2181+
init_file({
2182+
"foo",
2183+
"bar",
2184+
}, "lua", 1, 0)
2185+
2186+
feedkeys("g?p")
2187+
2188+
check_lines({
2189+
"foo",
2190+
"print('DEBUGPRINT[1]: (after foo)')",
2191+
"bar",
2192+
})
2193+
end)
2194+
2195+
it("no display_location, counter", function()
2196+
debugprint.setup({
2197+
display_location = false,
2198+
display_counter = false,
2199+
})
2200+
2201+
init_file({
2202+
"foo",
2203+
"bar",
2204+
}, "lua", 1, 0)
2205+
2206+
feedkeys("g?p")
2207+
2208+
check_lines({
2209+
"foo",
2210+
"print('DEBUGPRINT: (after foo)')",
2211+
"bar",
2212+
})
2213+
end)
2214+
2215+
it("no display_location, counter, snippet", function()
2216+
debugprint.setup({
2217+
display_location = false,
2218+
display_counter = false,
2219+
display_snippet = false,
2220+
})
2221+
2222+
init_file({
2223+
"foo",
2224+
"bar",
2225+
}, "lua", 1, 0)
2226+
2227+
feedkeys("g?p")
2228+
2229+
check_lines({
2230+
"foo",
2231+
"print('DEBUGPRINT')",
2232+
"bar",
2233+
})
2234+
end)
2235+
2236+
it("no display_location, counter, snippet, print_tag", function()
2237+
debugprint.setup({
2238+
display_location = false,
2239+
display_counter = false,
2240+
display_snippet = false,
2241+
print_tag = "",
2242+
})
2243+
2244+
init_file({
2245+
"foo",
2246+
"bar",
2247+
}, "lua", 1, 0)
2248+
2249+
feedkeys("g?p")
2250+
2251+
-- In this case we print the snippet anyway, because otherwise this makes no sense and the plain print statement will print nothing.
2252+
check_lines({
2253+
"foo",
2254+
"print('(after foo)')",
2255+
"bar",
2256+
})
2257+
end)
2258+
2259+
it("variable, no display_location", function()
2260+
debugprint.setup({
2261+
display_location = false,
2262+
})
2263+
2264+
init_file({
2265+
"foo",
2266+
"bar",
2267+
}, "lua", 1, 0)
2268+
2269+
feedkeys("g?v")
2270+
2271+
check_lines({
2272+
"foo",
2273+
"print('DEBUGPRINT[1]: foo=' .. vim.inspect(foo))",
2274+
"bar",
2275+
})
2276+
end)
2277+
2278+
it("variable, no display_location, counter, snippet", function()
2279+
debugprint.setup({
2280+
display_location = false,
2281+
display_counter = false,
2282+
display_snippet = false,
2283+
})
2284+
2285+
init_file({
2286+
"foo",
2287+
"bar",
2288+
}, "lua", 1, 0)
2289+
2290+
feedkeys("g?v")
2291+
2292+
check_lines({
2293+
"foo",
2294+
"print('DEBUGPRINT: foo=' .. vim.inspect(foo))",
2295+
"bar",
2296+
})
2297+
end)
2298+
2299+
it("variable, no display_location, counter, snippet, print_tag", function()
2300+
debugprint.setup({
2301+
display_location = false,
2302+
display_counter = false,
2303+
display_snippet = false,
2304+
print_tag = "",
2305+
})
2306+
2307+
init_file({
2308+
"foo",
2309+
"bar",
2310+
}, "lua", 1, 0)
2311+
2312+
feedkeys("g?v")
2313+
2314+
check_lines({
2315+
"foo",
2316+
"print('foo=' .. vim.inspect(foo))",
2317+
"bar",
2318+
})
2319+
end)
2320+
end)

0 commit comments

Comments
 (0)