Skip to content

feat(#2270): add notify.absolute_path - show file or absolute path (default) names with notifications #2286

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 2 commits into from
Jun 25, 2023
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
5 changes: 5 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ applying configuration.
},
notify = {
threshold = vim.log.levels.INFO,
absolute_path = true,
},
ui = {
confirm = {
Expand Down Expand Up @@ -1284,6 +1285,10 @@ Configuration for notification.
`INFO:` information only e.g. file copy path confirmation.
`DEBUG:` not used.

*nvim-tree.notify.absolute_path*
Whether to use absolute paths or item names in fs action notifications.
Type: `boolean`, Default: `true`

*nvim-tree.ui*
General UI configuration.

Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
},
notify = {
threshold = vim.log.levels.INFO,
absolute_path = true,
},
ui = {
confirm = {
Expand Down
16 changes: 9 additions & 7 deletions lua/nvim-tree/actions/fs/copy-paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,20 @@ end
local function do_single_paste(source, dest, action_type, action_fn)
local dest_stats
local success, errmsg, errcode
local notify_source = notify.render_path(source)

log.line("copy_paste", "do_single_paste '%s' -> '%s'", source, dest)

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

local function on_process()
success, errmsg = action_fn(source, dest)
if not success then
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. notify_source .. " - " .. (errmsg or "???"))
return false, errmsg
end

Expand Down Expand Up @@ -133,15 +134,16 @@ local function add_to_clipboard(node, clip)
if node.name == ".." then
return
end
local notify_node = notify.render_path(node.absolute_path)

for idx, _node in ipairs(clip) do
if _node.absolute_path == node.absolute_path then
table.remove(clip, idx)
return notify.info(node.absolute_path .. " removed from clipboard.")
return notify.info(notify_node .. " removed from clipboard.")
end
end
table.insert(clip, node)
notify.info(node.absolute_path .. " added to clipboard.")
notify.info(notify_node .. " added to clipboard.")
end

function M.clear_clipboard()
Expand Down Expand Up @@ -172,7 +174,7 @@ local function do_paste(node, action_type, action_fn)
local stats, errmsg, errcode = vim.loop.fs_stat(destination)
if not stats and errcode ~= "ENOENT" then
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
notify.error("Could not " .. action_type .. " " .. notify.render_path(destination) .. " - " .. (errmsg or "???"))
return
end
local is_dir = stats and stats.type == "directory"
Expand Down Expand Up @@ -223,13 +225,13 @@ function M.print_clipboard()
if #clipboard.move > 0 then
table.insert(content, "Cut")
for _, item in pairs(clipboard.move) do
table.insert(content, " * " .. item.absolute_path)
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
end
end
if #clipboard.copy > 0 then
table.insert(content, "Copy")
for _, item in pairs(clipboard.copy) do
table.insert(content, " * " .. item.absolute_path)
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
end
end

Expand Down
6 changes: 3 additions & 3 deletions lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local function create_and_notify(file)
events._dispatch_will_create_file(file)
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
if not ok then
notify.error("Couldn't create file " .. file)
notify.error("Couldn't create file " .. notify.render_path(file))
return
end
vim.loop.fs_close(fd)
Expand Down Expand Up @@ -81,15 +81,15 @@ function M.fn(node)
elseif not utils.file_exists(path_to_create) then
local success = vim.loop.fs_mkdir(path_to_create, 493)
if not success then
notify.error("Could not create folder " .. path_to_create)
notify.error("Could not create folder " .. notify.render_path(path_to_create))
is_error = true
break
end
events._dispatch_folder_created(new_file_path)
end
end
if not is_error then
notify.info(new_file_path .. " was properly created")
notify.info(notify.render_path(new_file_path) .. " was properly created")
end

-- synchronously refreshes as we can't wait for the watchers
Expand Down
7 changes: 4 additions & 3 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,23 @@ end
--- Remove a node, notify errors, dispatch events
--- @param node table
function M.remove(node)
local notify_node = notify.render_path(node.absolute_path)
if node.nodes ~= nil and not node.link_to then
local success = remove_dir(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
return notify.error("Could not remove " .. notify_node)
end
events._dispatch_folder_removed(node.absolute_path)
else
events._dispatch_will_remove_file(node.absolute_path)
local success = vim.loop.fs_unlink(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
return notify.error("Could not remove " .. notify_node)
end
events._dispatch_file_removed(node.absolute_path)
clear_buffer(node.absolute_path)
end
notify.info(node.absolute_path .. " was properly removed.")
notify.info(notify_node .. " was properly removed.")
end

function M.fn(node)
Expand Down
9 changes: 6 additions & 3 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ local function err_fmt(from, to, reason)
end

function M.rename(node, to)
local notify_from = notify.render_path(node.absolute_path)
local notify_to = notify.render_path(to)

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

events._dispatch_will_rename_node(node.absolute_path, to)
local success, err = vim.loop.fs_rename(node.absolute_path, to)
if not success then
return notify.warn(err_fmt(node.absolute_path, to, err))
return notify.warn(err_fmt(notify_from, notify_to, err))
end
notify.info(node.absolute_path .. " " .. to)
notify.info(notify_from .. " " .. notify_to)
utils.rename_loaded_buffers(node.absolute_path, to)
events._dispatch_node_renamed(node.absolute_path, to)
end
Expand Down
10 changes: 10 additions & 0 deletions lua/nvim-tree/notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local M = {}

local config = {
threshold = vim.log.levels.INFO,
absolute_path = true,
}

local modes = {
Expand Down Expand Up @@ -30,9 +31,18 @@ do
end
end

function M.render_path(path)
if config.absolute_path then
return path
else
return vim.fn.fnamemodify(path .. "/", ":h:t")
end
end

function M.setup(opts)
opts = opts or {}
config.threshold = opts.notify.threshold or vim.log.levels.INFO
config.absolute_path = opts.notify.absolute_path
end

return M