Skip to content

feat: configurable notification level #1693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ Subsequent calls to setup will replace the previous configuration.
watcher = false,
},
},
notify = {
threshold = vim.log.levels.INFO,
},
} -- END_DEFAULT_OPTS
<

Expand Down Expand Up @@ -992,6 +995,18 @@ The filter can be cleared with the `F` key by default.
Whether to filter folders or not.
Type: `boolean`, Default: `true`

*nvim-tree.notify*
Configuration for notification.

*nvim-tree.notify.threshold*
Specify minimum notification level, uses the values from |vim.log.levels|
Type: `enum`, Default: `vim.log.levels.INFO`

`ERROR`: hard errors e.g. failure to read from the file system.
`WARNING`: non-fatal errors e.g. unable to system open a file.
`INFO:` information only e.g. file copy path confirmation.
`DEBUG:` not used.

*nvim-tree.log*
Configuration for diagnostic logging.

Expand Down
13 changes: 9 additions & 4 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
prefix = "[FILTER]: ",
always_show_folders = true,
},
notify = {
threshold = vim.log.levels.INFO,
},
log = {
enable = false,
truncate = false,
Expand Down Expand Up @@ -686,10 +689,11 @@ local function validate_options(conf)
local override_typecheck = FIELD_OVERRIDE_TYPECHECK[k] or {}
if def[k] == nil then
-- option does not exist
invalid = string.format("unknown option: %s%s", prefix, k)
invalid = string.format("[NvimTree] unknown option: %s%s", prefix, k)
elseif type(v) ~= type(def[k]) and not override_typecheck[type(v)] then
-- option is of the wrong type and is not a function
invalid = string.format("invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
invalid =
string.format("[NvimTree] invalid option: %s%s expected: %s actual: %s", prefix, k, type(def[k]), type(v))
end

if invalid then
Expand All @@ -709,13 +713,13 @@ local function validate_options(conf)
validate(conf, DEFAULT_OPTS, "")

if msg then
utils.notify.warn(msg .. " | see :help nvim-tree-setup for available configuration options")
vim.notify_once(msg .. " | see :help nvim-tree-setup for available configuration options", vim.log.levels.WARN)
end
end

function M.setup(conf)
if vim.fn.has "nvim-0.7" == 0 then
utils.notify.warn "nvim-tree.lua requires Neovim 0.7 or higher"
vim.notify_once("nvim-tree.lua requires Neovim 0.7 or higher", vim.log.levels.WARN)
return
end

Expand All @@ -742,6 +746,7 @@ function M.setup(conf)
manage_netrw(opts.disable_netrw, opts.hijack_netrw)

M.config = opts
require("nvim-tree.notify").setup(opts)
require("nvim-tree.log").setup(opts)

log.line("config", "default config + user")
Expand Down
17 changes: 9 additions & 8 deletions lua/nvim-tree/actions/fs/copy-paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local lib = require "nvim-tree.lib"
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"
local notify = require "nvim-tree.notify"

local M = {}

Expand Down Expand Up @@ -81,14 +82,14 @@ local function do_single_paste(source, dest, action_type, action_fn)

dest_stats, errmsg, errcode = uv.fs_stat(dest)
if not dest_stats and errcode ~= "ENOENT" then
utils.notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
end

local function on_process()
success, errmsg = action_fn(source, dest)
if not success then
utils.notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
return false, errmsg
end
end
Expand Down Expand Up @@ -122,11 +123,11 @@ local function add_to_clipboard(node, clip)
for idx, _node in ipairs(clip) do
if _node.absolute_path == node.absolute_path then
table.remove(clip, idx)
return utils.notify.info(node.absolute_path .. " removed to clipboard.")
return notify.info(node.absolute_path .. " removed to clipboard.")
end
end
table.insert(clip, node)
utils.notify.info(node.absolute_path .. " added to clipboard.")
notify.info(node.absolute_path .. " added to clipboard.")
end

function M.copy(node)
Expand All @@ -151,7 +152,7 @@ local function do_paste(node, action_type, action_fn)
local stats, errmsg, errcode = uv.fs_stat(destination)
if not stats and errcode ~= "ENOENT" then
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
utils.notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
return
end
local is_dir = stats and stats.type == "directory"
Expand Down Expand Up @@ -213,18 +214,18 @@ function M.print_clipboard()
end
end

return utils.notify.info(table.concat(content, "\n") .. "\n")
return notify.info(table.concat(content, "\n") .. "\n")
end

local function copy_to_clipboard(content)
if M.use_system_clipboard == true then
vim.fn.setreg("+", content)
vim.fn.setreg('"', content)
return utils.notify.info(string.format("Copied %s to system clipboard!", content))
return notify.info(string.format("Copied %s to system clipboard!", content))
else
vim.fn.setreg('"', content)
vim.fn.setreg("1", content)
return utils.notify.info(string.format("Copied %s to neovim clipboard!", content))
return notify.info(string.format("Copied %s to neovim clipboard!", content))
end
end

Expand Down
9 changes: 5 additions & 4 deletions lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ local events = require "nvim-tree.events"
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
local watch = require "nvim-tree.explorer.watch"
local notify = require "nvim-tree.notify"

local M = {}

local function create_and_notify(file)
local ok, fd = pcall(uv.fs_open, file, "w", 420)
if not ok then
utils.notify.error("Couldn't create file " .. file)
notify.error("Couldn't create file " .. file)
return
end
uv.fs_close(fd)
Expand Down Expand Up @@ -71,7 +72,7 @@ function M.fn(node)
end

if utils.file_exists(new_file_path) then
utils.notify.warn "Cannot create: file already exists"
notify.warn "Cannot create: file already exists"
return
end

Expand All @@ -96,14 +97,14 @@ function M.fn(node)
elseif not utils.file_exists(path_to_create) then
local success = uv.fs_mkdir(path_to_create, 493)
if not success then
utils.notify.error("Could not create folder " .. path_to_create)
notify.error("Could not create folder " .. path_to_create)
is_error = true
break
end
end
end
if not is_error then
utils.notify.info(new_file_path .. " was properly created")
notify.info(new_file_path .. " was properly created")
end
events._dispatch_folder_created(new_file_path)
if M.enable_reload then
Expand Down
9 changes: 5 additions & 4 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local view = require "nvim-tree.view"
local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"

local M = {}

Expand Down Expand Up @@ -44,7 +45,7 @@ end
local function remove_dir(cwd)
local handle = luv.fs_scandir(cwd)
if type(handle) == "string" then
return utils.notify.error(handle)
return notify.error(handle)
end

while true do
Expand Down Expand Up @@ -83,18 +84,18 @@ function M.fn(node)
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
return utils.notify.error("Could not remove " .. node.name)
return notify.error("Could not remove " .. node.name)
end
events._dispatch_folder_removed(node.absolute_path)
else
local success = luv.fs_unlink(node.absolute_path)
if not success then
return utils.notify.error("Could not remove " .. node.name)
return notify.error("Could not remove " .. node.name)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
utils.notify.info(node.absolute_path .. " was properly removed.")
notify.info(node.absolute_path .. " was properly removed.")
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
Expand Down
7 changes: 4 additions & 3 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local uv = vim.loop
local lib = require "nvim-tree.lib"
local utils = require "nvim-tree.utils"
local events = require "nvim-tree.events"
local notify = require "nvim-tree.notify"

local M = {}

Expand All @@ -12,15 +13,15 @@ end

function M.rename(node, to)
if utils.file_exists(to) then
utils.notify.warn(err_fmt(node.absolute_path, to, "file already exists"))
notify.warn(err_fmt(node.absolute_path, to, "file already exists"))
return
end

local success, err = uv.fs_rename(node.absolute_path, to)
if not success then
return utils.notify.warn(err_fmt(node.absolute_path, to, err))
return notify.warn(err_fmt(node.absolute_path, to, err))
end
utils.notify.info(node.absolute_path .. " ➜ " .. to)
notify.info(node.absolute_path .. " ➜ " .. to)
utils.rename_loaded_buffers(node.absolute_path, to)
events._dispatch_node_renamed(node.absolute_path, to)
end
Expand Down
9 changes: 5 additions & 4 deletions lua/nvim-tree/actions/fs/trash.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local a = vim.api

local lib = require "nvim-tree.lib"
local notify = require "nvim-tree.notify"

local M = {
config = {
Expand Down Expand Up @@ -43,13 +44,13 @@ function M.fn(node)
M.config.trash.require_confirm = true
end
else
utils.notify.warn "Trash is currently a UNIX only feature!"
notify.warn "Trash is currently a UNIX only feature!"
return
end

local binary = M.config.trash.cmd:gsub(" .*$", "")
if vim.fn.executable(binary) == 0 then
utils.notify.warn(binary .. " is not executable.")
notify.warn(binary .. " is not executable.")
return
end

Expand All @@ -71,7 +72,7 @@ function M.fn(node)
if node.nodes ~= nil and not node.link_to then
trash_path(function(_, rc)
if rc ~= 0 then
utils.notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_folder_removed(node.absolute_path)
Expand All @@ -82,7 +83,7 @@ function M.fn(node)
else
trash_path(function(_, rc)
if rc ~= 0 then
utils.notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
return
end
events._dispatch_file_removed(node.absolute_path)
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local a = vim.api

local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local util = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"

-- BEGIN_DEFAULT_MAPPINGS
local DEFAULT_MAPPINGS = {
Expand Down Expand Up @@ -307,7 +307,7 @@ local function merge_mappings(user_mappings)
if not is_empty(map.action) then
M.custom_keypress_funcs[map.action] = map.action_cb
else
util.notify.warn "action can't be empty if action_cb provided"
notify.warn "action can't be empty if action_cb provided"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/actions/tree-modifiers/expand-all.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local core = require "nvim-tree.core"
local renderer = require "nvim-tree.renderer"
local utils = require "nvim-tree.utils"
local Iterator = require "nvim-tree.iterators.node-iterator"
local notify = require "nvim-tree.notify"

local M = {}

Expand Down Expand Up @@ -58,7 +58,7 @@ end
function M.fn(base_node)
local node = base_node.nodes and base_node or core.get_explorer()
if gen_iterator()(node) then
utils.notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
notify.warn("expansion iteration was halted after " .. M.MAX_FOLDER_DISCOVERY .. " discovered folders")
end
renderer.draw()
end
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-tree/events.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require "nvim-tree.utils"
local notify = require "nvim-tree.notify"

local M = {}

Expand Down Expand Up @@ -30,7 +30,7 @@ local function dispatch(event_name, payload)
for _, handler in pairs(get_handlers(event_name)) do
local success, error = pcall(handler, payload)
if not success then
utils.notify.error("Handler for event " .. event_name .. " errored. " .. vim.inspect(error))
notify.error("Handler for event " .. event_name .. " errored. " .. vim.inspect(error))
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lua/nvim-tree/explorer/explore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local common = require "nvim-tree.explorer.common"
local sorters = require "nvim-tree.explorer.sorters"
local filters = require "nvim-tree.explorer.filters"
local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify"

local M = {}

Expand Down Expand Up @@ -52,7 +53,7 @@ end
local function get_dir_handle(cwd)
local handle = uv.fs_scandir(cwd)
if type(handle) == "string" then
utils.notify.error(handle)
notify.error(handle)
return
end
return handle
Expand Down
3 changes: 2 additions & 1 deletion lua/nvim-tree/explorer/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local common = require "nvim-tree.explorer.common"
local filters = require "nvim-tree.explorer.filters"
local sorters = require "nvim-tree.explorer.sorters"
local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify"

local M = {}

Expand All @@ -22,7 +23,7 @@ function M.reload(node, status)
local cwd = node.link_to or node.absolute_path
local handle = uv.fs_scandir(cwd)
if type(handle) == "string" then
utils.notify.error(handle)
notify.error(handle)
return
end

Expand Down
Loading