Skip to content

Commit bb6d1c9

Browse files
committed
fix: Remove conflicts with nvim-notify - closes #91
1 parent 58c4722 commit bb6d1c9

File tree

3 files changed

+108
-59
lines changed

3 files changed

+108
-59
lines changed

lua/debugprint/init.lua

+45-32
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,18 @@ local debuginfo = function(opts)
7373
end
7474

7575
local filetype_configured = function()
76-
local filetype = utils.get_effective_filetype()
76+
local effective_filetype = utils.get_effective_filetype()
7777

78-
if not vim.tbl_contains(vim.tbl_keys(global_opts.filetypes), filetype) then
79-
vim.notify(
80-
"Don't have debugprint configuration for filetype " .. filetype,
81-
vim.log.levels.WARN
82-
)
83-
return false
84-
else
85-
return true
86-
end
78+
return vim.tbl_contains(
79+
vim.tbl_keys(global_opts.filetypes),
80+
effective_filetype
81+
)
8782
end
8883

89-
local addline = function(opts)
90-
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1]
91-
local filetype = utils.get_effective_filetype()
92-
local fileconfig = global_opts.filetypes[filetype]
84+
local construct_debugprint_line = function(opts, effective_filetype)
85+
local fileconfig = global_opts.filetypes[effective_filetype]
9386

94-
if fileconfig == nil then
95-
return
96-
end
97-
98-
local line_to_insert_content
99-
local line_to_insert_linenr
87+
local line_to_insert
10088

10189
if opts.variable_name then
10290
local left
@@ -107,23 +95,52 @@ local addline = function(opts)
10795
left = fileconfig["left"]
10896
end
10997

110-
line_to_insert_content = left
98+
line_to_insert = left
11199
.. debuginfo(opts)
112100
.. fileconfig.mid_var
113101
.. opts.variable_name
114102
.. fileconfig.right_var
115103
else
116104
opts.variable_name = nil
117-
line_to_insert_content = fileconfig.left
118-
.. debuginfo(opts)
119-
.. fileconfig.right
105+
line_to_insert = fileconfig.left .. debuginfo(opts) .. fileconfig.right
106+
end
107+
108+
return line_to_insert
109+
end
110+
111+
local construct_error_line = function(errormsg)
112+
local commentstring =
113+
vim.api.nvim_get_option_value("commentstring", { scope = "local" })
114+
115+
if string.find(commentstring, "%%s") then
116+
return vim.fn.substitute(commentstring, "%s", errormsg, "")
117+
else
118+
return errormsg
119+
end
120+
end
121+
122+
local addline = function(opts)
123+
local effective_filetype = utils.get_effective_filetype()
124+
125+
local line_to_insert
126+
127+
if filetype_configured() then
128+
line_to_insert = construct_debugprint_line(opts, effective_filetype)
129+
else
130+
line_to_insert = construct_error_line(
131+
"Don't have debugprint configuration for filetype "
132+
.. effective_filetype
133+
.. "; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes"
134+
)
120135
end
121136

122137
-- Inserting the leading space from the current line effectively acts as a
123138
-- 'default' indent for languages like Python, where the NeoVim or Treesitter
124139
-- indenter doesn't know how to indent them.
125-
local current_line = vim.api.nvim_get_current_line()
126-
local leading_space = current_line:match("^(%s+)") or ""
140+
local leading_space = vim.api.nvim_get_current_line():match("^(%s+)") or ""
141+
142+
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1]
143+
local line_to_insert_linenr
127144

128145
if opts.above then
129146
line_to_insert_linenr = current_line_nr - 1
@@ -136,7 +153,7 @@ local addline = function(opts)
136153
line_to_insert_linenr,
137154
line_to_insert_linenr,
138155
true,
139-
{ leading_space .. line_to_insert_content }
156+
{ leading_space .. line_to_insert }
140157
)
141158

142159
utils.indent_line(line_to_insert_linenr, global_opts.move_to_debugline)
@@ -146,11 +163,7 @@ local cache_request = nil
146163

147164
M.debugprint_cache = function(opts)
148165
if opts and opts.prerepeat == true then
149-
if not filetype_configured() then
150-
return
151-
end
152-
153-
if opts.variable == true then
166+
if filetype_configured() and opts.variable == true then
154167
opts.variable_name = utils.get_variable_name(
155168
global_opts.ignore_treesitter,
156169
opts.ignore_treesitter

lua/debugprint/utils.lua

+11-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ M.get_variable_name = function(
5151
variable_name = vim.fn.input("Variable name: ", word_under_cursor)
5252

5353
if variable_name == nil or variable_name == "" then
54-
vim.notify("No variable name entered.", vim.log.levels.WARN)
54+
-- Don't show a warning because of this issue:
55+
-- https://github.com/andrewferrier/debugprint.nvim/issues/91.
56+
-- Instead just silently end debugprint operation.
57+
vim.cmd("mode") -- Clear command
5558
return false
5659
end
5760
end
@@ -180,10 +183,11 @@ M.get_visual_selection = function()
180183
end
181184

182185
if line1 ~= line2 then
183-
vim.notify(
184-
"debugprint not supported when multiple lines selected.",
185-
vim.log.levels.ERROR
186-
)
186+
-- Multiple lines are selected; in this case, silently fail to find a
187+
-- visual selection, since it's unlikely to be what the user wants
188+
-- anyway, and there's no good way to give them an error (vim.notify()
189+
-- may fail because of an issue similar to this:
190+
-- https://github.com/andrewferrier/debugprint.nvim/issues/91).
187191
return false
188192
end
189193

@@ -199,6 +203,8 @@ M.get_operator_selection = function()
199203
local col2 = last_pos[3]
200204

201205
if line1 ~= line2 then
206+
-- This seems to work OK with nvim-notify and is not affected by
207+
-- https://github.com/andrewferrier/debugprint.nvim/issues/91
202208
vim.notify(
203209
"debugprint not supported when multiple lines in motion.",
204210
vim.log.levels.ERROR

tests/debugprint.lua

+52-22
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,13 @@ describe("can do variable debug statement insertion", function()
402402
})
403403
end)
404404

405-
it("entering no name gives an error", function()
405+
it("entering no name silently ends debugprint operation", function()
406406
init_file({
407407
"foo",
408408
"bar",
409409
}, "lua", 1, 0)
410410

411411
feedkeys("g?v<BS><BS><BS><CR>")
412-
assert.are.same("No variable name entered.", notify_message)
413412

414413
check_lines({
415414
"foo",
@@ -462,17 +461,42 @@ describe("can do various file types", function()
462461
}, "foo", 1, 0, { filetype = "foo" })
463462

464463
feedkeys("g?p")
465-
assert.are.same(
466-
"Don't have debugprint configuration for filetype foo",
467-
notify_message
468-
)
469464

470-
check_lines({
471-
"foo",
472-
"bar",
473-
})
465+
if vim.fn.has("nvim-0.9.0") == 1 then
466+
check_lines({
467+
"foo",
468+
"Don't have debugprint configuration for filetype foo; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes",
469+
"bar",
470+
})
471+
else
472+
check_lines({
473+
"foo",
474+
"/*Don't have debugprint configuration for filetype foo; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes*/",
475+
"bar",
476+
})
477+
end
474478
end)
475479

480+
it(
481+
"can gracefully handle known filetypes we don't have a config for: fennel",
482+
function()
483+
init_file({
484+
"(fn print-and-add [a b c]",
485+
" (print a)",
486+
" (+ b c))",
487+
}, "fnl", 1, 0)
488+
489+
feedkeys("g?p")
490+
491+
check_lines({
492+
"(fn print-and-add [a b c]",
493+
" ;Don't have debugprint configuration for filetype fennel; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes",
494+
" (print a)",
495+
" (+ b c))",
496+
})
497+
end
498+
)
499+
476500
it("don't prompt for a variable name with an unknown filetype", function()
477501
init_file({
478502
"foo",
@@ -481,15 +505,20 @@ describe("can do various file types", function()
481505

482506
feedkeys("g?v")
483507
feedkeys("<CR>")
484-
assert.are.same(
485-
"Don't have debugprint configuration for filetype foo",
486-
notify_message
487-
)
488508

489-
check_lines({
490-
"foo",
491-
"bar",
492-
})
509+
if vim.fn.has("nvim-0.9.0") == 1 then
510+
check_lines({
511+
"foo",
512+
"Don't have debugprint configuration for filetype foo; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes",
513+
"bar",
514+
})
515+
else
516+
check_lines({
517+
"foo",
518+
"/*Don't have debugprint configuration for filetype foo; see https://github.com/andrewferrier/debugprint.nvim?tab=readme-ov-file#add-custom-filetypes*/",
519+
"bar",
520+
})
521+
end
493522
end)
494523
end)
495524

@@ -1093,10 +1122,11 @@ describe("visual selection", function()
10931122

10941123
feedkeys("vjg?v")
10951124

1096-
assert.are.same(
1097-
"debugprint not supported when multiple lines selected.",
1098-
notify_message
1099-
)
1125+
check_lines({
1126+
"function x()",
1127+
"local xyz = 3",
1128+
"end",
1129+
})
11001130
end)
11011131
end)
11021132

0 commit comments

Comments
 (0)