Skip to content

Commit 55676bc

Browse files
committed
fix(logger): stop overwriting global vim.notify function
1 parent 6ad76a1 commit 55676bc

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

lua/parrot/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ M.hooks = nil
255255

256256
function M.setup(opts)
257257
if vim.fn.has("nvim-0.10") == 0 then
258-
return vim.notify("parrot.nvim requires Neovim >= 0.10", vim.log.levels.ERROR)
258+
return M.logger.notify("parrot.nvim requires Neovim >= 0.10", vim.log.levels.ERROR)
259259
end
260260

261261
math.randomseed(os.time())

lua/parrot/context.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function M.insert_contexts(msg)
6161
end
6262
content = content:gsub("\n+$", "")
6363
if show_hints then
64-
vim.notify("Attached context " .. source_info .. ": " .. vim.fs.basename(filepath))
64+
logger.notify("Attached context " .. source_info .. ": " .. vim.fs.basename(filepath))
6565
end
6666
table.insert(contexts, { content = content, filetype = ft, name = filepath })
6767
else
@@ -123,7 +123,7 @@ function M.insert_contexts(msg)
123123
local buf_lines = vim.api.nvim_buf_get_lines(buf_nr, 0, -1, false)
124124
local name = vim.api.nvim_buf_get_name(buf_nr)
125125
if show_hints then
126-
vim.notify("Attached context @buffer: " .. vim.fs.basename(name))
126+
logger.notify("Attached context @buffer: " .. vim.fs.basename(name))
127127
end
128128
return { content = table.concat(buf_lines, "\n"), filetype = ft, name = name }
129129
end)
@@ -165,7 +165,7 @@ function M.insert_contexts(msg)
165165
end
166166
content = content:gsub("\n+$", "")
167167
if show_hints then
168-
vim.notify(
168+
logger.notify(
169169
"Attached context @directory: " .. vim.fs.basename(dir_path) .. " - " .. vim.fs.basename(file)
170170
)
171171
end

lua/parrot/logger.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ local M = {
33
_logfile = vim.fn.stdpath("state") .. "/parrot.nvim.log",
44
_max_log_lines = 10000,
55
_debug_enabled = vim.env.DEBUG_PARROT ~= nil,
6+
notify = vim.notify
67
}
78

89
-- Use pcall to safely require the notify plugin
910
local notify_ok, notify = pcall(require, "notify")
1011
if notify_ok then
11-
vim.notify = notify
12+
M.notify = notify
1213
end
1314

1415
-- Get stack trace information
@@ -84,9 +85,9 @@ local function write_to_logfile(msg, kind, stack_info)
8485

8586
local success = write_file(M._logfile, limited_log .. log_entry .. "\n")
8687
if not success and kind ~= "Debug" then
87-
-- Fallback to vim.notify if file write fails
88+
-- Fallback to M.notify if file write fails
8889
vim.schedule(function()
89-
vim.notify("Failed to write to log file: " .. M._logfile, vim.log.levels.WARN, { title = M._plugin_name })
90+
M.notify("Failed to write to log file: " .. M._logfile, vim.log.levels.WARN, { title = M._plugin_name })
9091
end)
9192
end
9293
end
@@ -109,7 +110,7 @@ local function log(msg, kind, level, include_stack)
109110
msg = msg .. "\nLocation: " .. stack_info
110111
end
111112

112-
vim.notify(msg, level, notify_opts)
113+
M.notify(msg, level, notify_opts)
113114
end)
114115
end
115116
end

tests/parrot/logger_spec.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local logger = require("parrot.logger")
33
describe("Logger", function()
44
local original_notify
55
local original_schedule
6+
local original_logger_notify
67
local notify_calls = {}
78
local schedule_calls = {}
89

@@ -13,6 +14,12 @@ describe("Logger", function()
1314
table.insert(notify_calls, { msg = msg, level = level, opts = opts })
1415
end
1516

17+
-- Mock logger.notify
18+
original_logger_notify = logger.notify
19+
logger.notify = function(msg, level, opts)
20+
table.insert(notify_calls, { msg = msg, level = level, opts = opts })
21+
end
22+
1623
-- Mock vim.schedule
1724
original_schedule = vim.schedule
1825
vim.schedule = function(fn)
@@ -28,6 +35,7 @@ describe("Logger", function()
2835
after_each(function()
2936
vim.notify = original_notify
3037
vim.schedule = original_schedule
38+
logger.notify = original_logger_notify
3139
end)
3240

3341
describe("error function", function()

0 commit comments

Comments
 (0)