Skip to content

Commit 1e8f057

Browse files
committed
feat: Add ability to customize notification handler
1 parent 086bf31 commit 1e8f057

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

doc/nvim-tree-lua.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ applying configuration.
440440
},
441441
notify = {
442442
threshold = vim.log.levels.INFO,
443+
handler = "default",
443444
},
444445
ui = {
445446
confirm = {
@@ -1224,6 +1225,19 @@ Configuration for notification.
12241225
`INFO:` information only e.g. file copy path confirmation.
12251226
`DEBUG:` not used.
12261227

1228+
*nvim-tree.notify.handler*
1229+
Change the default notification handler, can be a string `"default"` or a function.
1230+
The default handler will use |vim.notify()|
1231+
Type: `string` | `function`, Default: `"default"`
1232+
1233+
Parameters: ~
1234+
{msg} (string) See |vim.notify()|
1235+
{level} (number) See |vim.notify()|
1236+
{opts} (table) Optional parameters
1237+
1238+
Options: ~
1239+
{title} (string) Usually `"NvimTree"`
1240+
12271241
*nvim-tree.ui*
12281242
General UI configuration.
12291243

lua/nvim-tree.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
629629
},
630630
notify = {
631631
threshold = vim.log.levels.INFO,
632+
handler = "default",
632633
},
633634
ui = {
634635
confirm = {
@@ -674,6 +675,7 @@ local FIELD_OVERRIDE_TYPECHECK = {
674675
sort_by = { ["function"] = true, string = true },
675676
root_folder_label = { ["function"] = true, string = true },
676677
picker = { ["function"] = true, string = true },
678+
handler = { ["function"] = true, string = true },
677679
}
678680

679681
local function validate_options(conf)

lua/nvim-tree/notify.lua

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
local M = {}
22

3+
local fallback_handler = function(msg, level, opts)
4+
vim.notify(string.format("[%s] %s", opts.title, vim.inspect(msg)), level)
5+
end
6+
37
local config = {
48
threshold = vim.log.levels.INFO,
9+
handler = fallback_handler,
510
}
611

712
local modes = {
@@ -13,19 +18,13 @@ local modes = {
1318
}
1419

1520
do
16-
local has_notify, notify_plugin = pcall(require, "notify")
17-
1821
local dispatch = function(level, msg)
1922
if level < config.threshold then
2023
return
2124
end
2225

2326
vim.schedule(function()
24-
if has_notify and notify_plugin then
25-
notify_plugin(msg, level, { title = "NvimTree" })
26-
else
27-
vim.notify(string.format("[NvimTree] %s", vim.inspect(msg)), level)
28-
end
27+
config.handler(msg, level, { title = "NvimTree" })
2928
end)
3029
end
3130

@@ -36,9 +35,23 @@ do
3635
end
3736
end
3837

38+
local create_default_handler = function()
39+
local has_notify, notify_plugin = pcall(require, "notify")
40+
if has_notify and notify_plugin then
41+
return notify_plugin
42+
else
43+
return fallback_handler
44+
end
45+
end
46+
3947
function M.setup(opts)
4048
opts = opts or {}
4149
config.threshold = opts.notify.threshold or vim.log.levels.INFO
50+
if type(opts.notify.handler) == "function" then
51+
config.handler = opts.notify.handler
52+
else
53+
config.handler = create_default_handler()
54+
end
4255
end
4356

4457
return M

0 commit comments

Comments
 (0)