Skip to content

Commit 61dbc50

Browse files
committed
fix: Lazy loading - no autocmds/warn on unmodifiable buffers - closes #97
1 parent e6bc11e commit 61dbc50

File tree

4 files changed

+166
-120
lines changed

4 files changed

+166
-120
lines changed

lua/debugprint/init.lua

+12
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ M.debugprint = function(opts)
188188
local func_opts =
189189
require("debugprint.options").get_and_validate_function_opts(opts)
190190

191+
if not utils.is_modifiable() then
192+
return
193+
end
194+
191195
if func_opts.motion == true then
192196
cache_request = func_opts
193197
vim.go.operatorfunc =
@@ -234,6 +238,10 @@ M.deleteprints = function(opts)
234238
local lines_to_consider, initial_line = get_lines_to_handle(opts)
235239
local delete_adjust = 0
236240

241+
if not utils.is_modifiable() then
242+
return
243+
end
244+
237245
for count, line in ipairs(lines_to_consider) do
238246
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
239247
local line_to_delete = count
@@ -255,6 +263,10 @@ end
255263
M.toggle_comment_debugprints = function(opts)
256264
local lines_to_consider, initial_line = get_lines_to_handle(opts)
257265

266+
if not utils.is_modifiable() then
267+
return
268+
end
269+
258270
for count, line in ipairs(lines_to_consider) do
259271
if string.find(line, global_opts.print_tag, 1, true) ~= nil then
260272
local line_to_toggle = count + initial_line - 1

lua/debugprint/setup.lua

+117-120
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ local M = {}
22

33
local debugprint = require("debugprint")
44

5-
local map_key = function(mode, lhs, buffer, opts)
6-
if
7-
lhs ~= nil
8-
and vim.api.nvim_get_option_value("modifiable", { buf = buffer })
9-
then
10-
vim.api.nvim_buf_set_keymap(buffer, mode, lhs, "", opts)
5+
local map_key = function(mode, lhs, opts)
6+
if lhs ~= nil then
7+
vim.api.nvim_set_keymap(mode, lhs, "", opts)
118
end
129
end
1310

@@ -18,130 +15,130 @@ local feedkeys = function(keys)
1815
end
1916

2017
M.map_keys_and_commands = function(global_opts)
21-
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
22-
group = vim.api.nvim_create_augroup(
23-
"debugprint_augroup",
24-
{ clear = true }
25-
),
26-
callback = function(opts)
27-
map_key("n", global_opts.keymaps.normal.plain_below, opts.buf, {
28-
callback = function()
29-
feedkeys(debugprint.debugprint({}))
30-
end,
31-
desc = "Plain debug below current line",
32-
})
18+
map_key("n", global_opts.keymaps.normal.plain_below, {
19+
callback = function()
20+
feedkeys(debugprint.debugprint({}))
21+
end,
22+
desc = "Plain debug below current line",
23+
})
3324

34-
map_key("n", global_opts.keymaps.normal.plain_above, opts.buf, {
35-
callback = function()
36-
feedkeys(debugprint.debugprint({ above = true }))
37-
end,
38-
desc = "Plain debug below current line",
39-
})
25+
map_key("n", global_opts.keymaps.normal.plain_above, {
26+
callback = function()
27+
feedkeys(debugprint.debugprint({ above = true }))
28+
end,
29+
desc = "Plain debug below current line",
30+
})
4031

41-
map_key("n", global_opts.keymaps.normal.variable_below, opts.buf, {
42-
callback = function()
43-
feedkeys(debugprint.debugprint({ variable = true }))
44-
end,
45-
desc = "Variable debug below current line",
46-
})
32+
map_key("n", global_opts.keymaps.normal.variable_below, {
33+
callback = function()
34+
feedkeys(debugprint.debugprint({ variable = true }))
35+
end,
36+
desc = "Variable debug below current line",
37+
})
4738

48-
map_key("n", global_opts.keymaps.normal.variable_above, opts.buf, {
49-
callback = function()
50-
feedkeys(debugprint.debugprint({
51-
above = true,
52-
variable = true,
53-
}))
54-
end,
55-
desc = "Variable debug above current line",
56-
})
39+
map_key("n", global_opts.keymaps.normal.variable_above, {
40+
callback = function()
41+
feedkeys(debugprint.debugprint({
42+
above = true,
43+
variable = true,
44+
}))
45+
end,
46+
desc = "Variable debug above current line",
47+
})
5748

58-
map_key(
59-
"n",
60-
global_opts.keymaps.normal.variable_below_alwaysprompt,
61-
opts.buf,
62-
{
63-
callback = function()
64-
feedkeys(debugprint.debugprint({
65-
variable = true,
66-
ignore_treesitter = true,
67-
}))
68-
end,
69-
desc = "Variable debug below current line (always prompt})",
70-
}
71-
)
72-
73-
map_key(
74-
"n",
75-
global_opts.keymaps.normal.variable_above_alwaysprompt,
76-
opts.buf,
77-
{
78-
callback = function()
79-
feedkeys(debugprint.debugprint({
80-
above = true,
81-
variable = true,
82-
ignore_treesitter = true,
83-
}))
84-
end,
85-
desc = "Variable debug above current line (always prompt})",
86-
}
87-
)
88-
89-
map_key("n", global_opts.keymaps.normal.textobj_below, opts.buf, {
90-
callback = function()
91-
return debugprint.debugprint({ motion = true })
92-
end,
93-
expr = true,
94-
desc = "Text-obj-selected variable debug below current line",
95-
})
49+
map_key("n", global_opts.keymaps.normal.variable_below_alwaysprompt, {
50+
callback = function()
51+
feedkeys(debugprint.debugprint({
52+
variable = true,
53+
ignore_treesitter = true,
54+
}))
55+
end,
56+
desc = "Variable debug below current line (always prompt})",
57+
})
9658

97-
map_key("n", global_opts.keymaps.normal.textobj_above, opts.buf, {
98-
callback = function()
99-
return debugprint.debugprint({
100-
motion = true,
101-
above = true,
102-
})
103-
end,
104-
expr = true,
105-
desc = "Text-obj-selected variable debug above current line",
106-
})
59+
map_key("n", global_opts.keymaps.normal.variable_above_alwaysprompt, {
60+
callback = function()
61+
feedkeys(debugprint.debugprint({
62+
above = true,
63+
variable = true,
64+
ignore_treesitter = true,
65+
}))
66+
end,
67+
desc = "Variable debug above current line (always prompt})",
68+
})
10769

108-
map_key(
109-
"n",
110-
global_opts.keymaps.normal.delete_debug_prints,
111-
opts.buf,
112-
{
113-
callback = debugprint.deleteprints,
114-
desc = "Delete all debugprint statements in the current buffer",
115-
}
116-
)
117-
118-
map_key(
119-
"n",
120-
global_opts.keymaps.normal.toggle_comment_debug_prints,
121-
opts.buf,
122-
{
123-
callback = debugprint.toggle_comment_debugprints,
124-
desc = "Comment/uncomment all debugprint statements in the current buffer",
125-
}
126-
)
127-
128-
map_key("x", global_opts.keymaps.visual.variable_below, opts.buf, {
129-
callback = function()
130-
feedkeys(debugprint.debugprint({ variable = true }))
131-
end,
132-
desc = "Variable debug below current line",
70+
map_key("n", global_opts.keymaps.normal.textobj_below, {
71+
callback = function()
72+
return debugprint.debugprint({ motion = true })
73+
end,
74+
expr = true,
75+
desc = "Text-obj-selected variable debug below current line",
76+
})
77+
78+
map_key("n", global_opts.keymaps.normal.textobj_above, {
79+
callback = function()
80+
return debugprint.debugprint({
81+
motion = true,
82+
above = true,
13383
})
84+
end,
85+
expr = true,
86+
desc = "Text-obj-selected variable debug above current line",
87+
})
88+
89+
map_key("x", global_opts.keymaps.visual.variable_below, {
90+
callback = function()
91+
feedkeys(debugprint.debugprint({ variable = true }))
92+
end,
93+
desc = "Variable debug below current line",
94+
})
95+
96+
map_key("x", global_opts.keymaps.visual.variable_above, {
97+
callback = function()
98+
feedkeys(debugprint.debugprint({
99+
above = true,
100+
variable = true,
101+
}))
102+
end,
103+
desc = "Variable debug above current line",
104+
})
105+
106+
map_key("n", global_opts.keymaps.normal.delete_debug_prints, {
107+
callback = debugprint.deleteprints,
108+
desc = "Delete all debugprint statements in the current buffer",
109+
})
110+
111+
map_key("n", global_opts.keymaps.normal.toggle_comment_debug_prints, {
112+
callback = debugprint.toggle_comment_debugprints,
113+
desc = "Comment/uncomment all debugprint statements in the current buffer",
114+
})
115+
116+
map_key("x", global_opts.keymaps.visual.variable_below, {
117+
callback = function()
118+
feedkeys(debugprint.debugprint({ variable = true }))
119+
end,
120+
desc = "Variable debug below current line",
121+
})
122+
123+
map_key("x", global_opts.keymaps.visual.variable_above, {
124+
callback = function()
125+
feedkeys(debugprint.debugprint({
126+
above = true,
127+
variable = true,
128+
}))
129+
end,
130+
desc = "Variable debug above current line",
131+
})
134132

135-
map_key("x", global_opts.keymaps.visual.variable_above, opts.buf, {
136-
callback = function()
137-
feedkeys(debugprint.debugprint({
138-
above = true,
139-
variable = true,
140-
}))
141-
end,
142-
desc = "Variable debug above current line",
133+
map_key("n", global_opts.keymaps.normal.variable_above_alwaysprompt, {
134+
callback = function()
135+
return debugprint.debugprint({
136+
above = true,
137+
variable = true,
138+
ignore_treesitter = true,
143139
})
144140
end,
141+
desc = "Variable debug above current line (always prompt})",
145142
})
146143

147144
if global_opts.commands.delete_debug_prints then

lua/debugprint/utils.lua

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ local get_node_at_cursor = function()
2929
end
3030
end
3131

32+
M.is_modifiable = function()
33+
if not vim.api.nvim_get_option_value("modifiable", { buf = vim.api.nvim_get_current_buf() }) then
34+
vim.notify('Buffer is not modifiable.', vim.log.levels.ERROR)
35+
return false
36+
else
37+
return true
38+
end
39+
end
40+
3241
M.get_variable_name = function(
3342
global_ignore_treesitter,
3443
local_ignore_treesitter

tests/debugprint.lua

+28
Original file line numberDiff line numberDiff line change
@@ -1837,3 +1837,31 @@ describe("handle deprecated options, create_keymaps=true", function()
18371837
})
18381838
end)
18391839
end)
1840+
1841+
describe("unmodifiable buffer", function()
1842+
before_each(function()
1843+
debugprint.setup({ create_keymaps = true })
1844+
end)
1845+
1846+
after_each(teardown)
1847+
1848+
it("basic", function()
1849+
assert.equals(notify_message, nil)
1850+
1851+
init_file({
1852+
"foo",
1853+
"bar",
1854+
}, "lua", 1, 0)
1855+
1856+
vim.cmd("set nomodifiable")
1857+
1858+
feedkeys("g?p")
1859+
1860+
check_lines({
1861+
"foo",
1862+
"bar",
1863+
})
1864+
1865+
assert.equals(notify_message, "Buffer is not modifiable.")
1866+
end)
1867+
end)

0 commit comments

Comments
 (0)