Skip to content

Commit 3cc698b

Browse files
asror1alex-courtis
andauthored
feat(#2270): add notify.absolute_path - show file or absolute path (default) names with notifications (#2286)
Co-authored-by: Alexander Courtis <[email protected]>
1 parent 7aff29d commit 3cc698b

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

doc/nvim-tree-lua.txt

+5
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ applying configuration.
520520
},
521521
notify = {
522522
threshold = vim.log.levels.INFO,
523+
absolute_path = true,
523524
},
524525
ui = {
525526
confirm = {
@@ -1284,6 +1285,10 @@ Configuration for notification.
12841285
`INFO:` information only e.g. file copy path confirmation.
12851286
`DEBUG:` not used.
12861287

1288+
*nvim-tree.notify.absolute_path*
1289+
Whether to use absolute paths or item names in fs action notifications.
1290+
Type: `boolean`, Default: `true`
1291+
12871292
*nvim-tree.ui*
12881293
General UI configuration.
12891294

lua/nvim-tree.lua

+1
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
574574
},
575575
notify = {
576576
threshold = vim.log.levels.INFO,
577+
absolute_path = true,
577578
},
578579
ui = {
579580
confirm = {

lua/nvim-tree/actions/fs/copy-paste.lua

+9-7
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,20 @@ end
8080
local function do_single_paste(source, dest, action_type, action_fn)
8181
local dest_stats
8282
local success, errmsg, errcode
83+
local notify_source = notify.render_path(source)
8384

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

8687
dest_stats, errmsg, errcode = vim.loop.fs_stat(dest)
8788
if not dest_stats and errcode ~= "ENOENT" then
88-
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
89+
notify.error("Could not " .. action_type .. " " .. notify_source .. " - " .. (errmsg or "???"))
8990
return false, errmsg
9091
end
9192

9293
local function on_process()
9394
success, errmsg = action_fn(source, dest)
9495
if not success then
95-
notify.error("Could not " .. action_type .. " " .. source .. " - " .. (errmsg or "???"))
96+
notify.error("Could not " .. action_type .. " " .. notify_source .. " - " .. (errmsg or "???"))
9697
return false, errmsg
9798
end
9899

@@ -133,15 +134,16 @@ local function add_to_clipboard(node, clip)
133134
if node.name == ".." then
134135
return
135136
end
137+
local notify_node = notify.render_path(node.absolute_path)
136138

137139
for idx, _node in ipairs(clip) do
138140
if _node.absolute_path == node.absolute_path then
139141
table.remove(clip, idx)
140-
return notify.info(node.absolute_path .. " removed from clipboard.")
142+
return notify.info(notify_node .. " removed from clipboard.")
141143
end
142144
end
143145
table.insert(clip, node)
144-
notify.info(node.absolute_path .. " added to clipboard.")
146+
notify.info(notify_node .. " added to clipboard.")
145147
end
146148

147149
function M.clear_clipboard()
@@ -172,7 +174,7 @@ local function do_paste(node, action_type, action_fn)
172174
local stats, errmsg, errcode = vim.loop.fs_stat(destination)
173175
if not stats and errcode ~= "ENOENT" then
174176
log.line("copy_paste", "do_paste fs_stat '%s' failed '%s'", destination, errmsg)
175-
notify.error("Could not " .. action_type .. " " .. destination .. " - " .. (errmsg or "???"))
177+
notify.error("Could not " .. action_type .. " " .. notify.render_path(destination) .. " - " .. (errmsg or "???"))
176178
return
177179
end
178180
local is_dir = stats and stats.type == "directory"
@@ -223,13 +225,13 @@ function M.print_clipboard()
223225
if #clipboard.move > 0 then
224226
table.insert(content, "Cut")
225227
for _, item in pairs(clipboard.move) do
226-
table.insert(content, " * " .. item.absolute_path)
228+
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
227229
end
228230
end
229231
if #clipboard.copy > 0 then
230232
table.insert(content, "Copy")
231233
for _, item in pairs(clipboard.copy) do
232-
table.insert(content, " * " .. item.absolute_path)
234+
table.insert(content, " * " .. (notify.render_path(item.absolute_path)))
233235
end
234236
end
235237

lua/nvim-tree/actions/fs/create-file.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local function create_and_notify(file)
1212
events._dispatch_will_create_file(file)
1313
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
1414
if not ok then
15-
notify.error("Couldn't create file " .. file)
15+
notify.error("Couldn't create file " .. notify.render_path(file))
1616
return
1717
end
1818
vim.loop.fs_close(fd)
@@ -81,15 +81,15 @@ function M.fn(node)
8181
elseif not utils.file_exists(path_to_create) then
8282
local success = vim.loop.fs_mkdir(path_to_create, 493)
8383
if not success then
84-
notify.error("Could not create folder " .. path_to_create)
84+
notify.error("Could not create folder " .. notify.render_path(path_to_create))
8585
is_error = true
8686
break
8787
end
8888
events._dispatch_folder_created(new_file_path)
8989
end
9090
end
9191
if not is_error then
92-
notify.info(new_file_path .. " was properly created")
92+
notify.info(notify.render_path(new_file_path) .. " was properly created")
9393
end
9494

9595
-- synchronously refreshes as we can't wait for the watchers

lua/nvim-tree/actions/fs/remove-file.lua

+4-3
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,23 @@ end
7474
--- Remove a node, notify errors, dispatch events
7575
--- @param node table
7676
function M.remove(node)
77+
local notify_node = notify.render_path(node.absolute_path)
7778
if node.nodes ~= nil and not node.link_to then
7879
local success = remove_dir(node.absolute_path)
7980
if not success then
80-
return notify.error("Could not remove " .. node.name)
81+
return notify.error("Could not remove " .. notify_node)
8182
end
8283
events._dispatch_folder_removed(node.absolute_path)
8384
else
8485
events._dispatch_will_remove_file(node.absolute_path)
8586
local success = vim.loop.fs_unlink(node.absolute_path)
8687
if not success then
87-
return notify.error("Could not remove " .. node.name)
88+
return notify.error("Could not remove " .. notify_node)
8889
end
8990
events._dispatch_file_removed(node.absolute_path)
9091
clear_buffer(node.absolute_path)
9192
end
92-
notify.info(node.absolute_path .. " was properly removed.")
93+
notify.info(notify_node .. " was properly removed.")
9394
end
9495

9596
function M.fn(node)

lua/nvim-tree/actions/fs/rename-file.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@ local function err_fmt(from, to, reason)
2020
end
2121

2222
function M.rename(node, to)
23+
local notify_from = notify.render_path(node.absolute_path)
24+
local notify_to = notify.render_path(to)
25+
2326
if utils.file_exists(to) then
24-
notify.warn(err_fmt(node.absolute_path, to, "file already exists"))
27+
notify.warn(err_fmt(notify_from, notify_to, "file already exists"))
2528
return
2629
end
2730

2831
events._dispatch_will_rename_node(node.absolute_path, to)
2932
local success, err = vim.loop.fs_rename(node.absolute_path, to)
3033
if not success then
31-
return notify.warn(err_fmt(node.absolute_path, to, err))
34+
return notify.warn(err_fmt(notify_from, notify_to, err))
3235
end
33-
notify.info(node.absolute_path .. " " .. to)
36+
notify.info(notify_from .. " " .. notify_to)
3437
utils.rename_loaded_buffers(node.absolute_path, to)
3538
events._dispatch_node_renamed(node.absolute_path, to)
3639
end

lua/nvim-tree/notify.lua

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local M = {}
22

33
local config = {
44
threshold = vim.log.levels.INFO,
5+
absolute_path = true,
56
}
67

78
local modes = {
@@ -30,9 +31,18 @@ do
3031
end
3132
end
3233

34+
function M.render_path(path)
35+
if config.absolute_path then
36+
return path
37+
else
38+
return vim.fn.fnamemodify(path .. "/", ":h:t")
39+
end
40+
end
41+
3342
function M.setup(opts)
3443
opts = opts or {}
3544
config.threshold = opts.notify.threshold or vim.log.levels.INFO
45+
config.absolute_path = opts.notify.absolute_path
3646
end
3747

3848
return M

0 commit comments

Comments
 (0)