Skip to content

Commit 88695df

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

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

doc/nvim-tree-lua.txt

Lines changed: 13 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,18 @@ 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 check if the nvim-notify plugin is present on the
1231+
runtime path and use it, otherwise it will fallback to use `vim.notify`.
1232+
Type: `string` | `function`, Default: `"default"`
1233+
1234+
The function will be called with following parameters:
1235+
1236+
`msg`: notification message
1237+
`level`: notification level (see `vim.log.levels` for possible values)
1238+
`opts`: table with additional parameters (e.g. `title`)
1239+
12271240
*nvim-tree.ui*
12281241
General UI configuration.
12291242

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: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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,
59
}
@@ -13,19 +17,13 @@ local modes = {
1317
}
1418

1519
do
16-
local has_notify, notify_plugin = pcall(require, "notify")
17-
1820
local dispatch = function(level, msg)
1921
if level < config.threshold then
2022
return
2123
end
2224

2325
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
26+
config.handler(msg, level, { title = "NvimTree" })
2927
end)
3028
end
3129

@@ -36,9 +34,23 @@ do
3634
end
3735
end
3836

37+
local create_default_handler = function()
38+
local has_notify, notify_plugin = pcall(require, "notify")
39+
if has_notify and notify_plugin then
40+
return notify_plugin
41+
else
42+
return fallback_handler
43+
end
44+
end
45+
3946
function M.setup(opts)
4047
opts = opts or {}
4148
config.threshold = opts.notify.threshold or vim.log.levels.INFO
49+
if type(opts.notify.handler) == "function" then
50+
config.handler = opts.notify.handler
51+
else
52+
config.handler = create_default_handler()
53+
end
4254
end
4355

4456
return M

0 commit comments

Comments
 (0)