Skip to content

Commit 949913f

Browse files
feat(api): rename_basename API and action (#1791)
* relative rename action * 🔥 remove debug print statement * 🐛 better handling of dot files Also pickout extension in filename with more one dot * 🔧 keymap e for relative-rename action * 📝 update help with relative-rename mapping * ✨ add API for rename_relative * 🚨 correct lint warnings * rename_relative -> rename_root * stylua * ♻️ use fnamemodify instead of custom logic * 💥 refactor renaming api using vim filename modifiers Rename API now supports filename modifiers as arguments, although only with limited support of options. The function signature however will allow improvements going forward. The API signature is backward compatible, although the behviour has changed as per the next comment. This change changes the default behaviour of the renames, rename_full is what rename was, rename now just renames the tail (i.e. the filename) * 🐛 make api rename, without args, functional * ✨ allow modifier argument to be used in API call * 📝 update documentation with new command name * rename-file.fn takes only a modifier as argument * add Api.fs.rename_basename, specify modifiers for rename, rename_sub * add Api.fs.rename_node * rename-file tidy allowed modifiers * 🐛 fix bugs after last refactoring rename ":t" and ":t:r" was moving file to root of project and not maintaining sub-directory * 🐛 correct absolute rename which was loosing sub-directory on rename * 🔥 remove debug print statements * stylua Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent a8d26bb commit 949913f

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,10 @@ exists.
12031203
- create
12041204
- remove
12051205
- trash
1206+
- rename_node `(node: table, modifier?: string vim.fn.fnamemodify argument)`
12061207
- rename
12071208
- rename_sub
1209+
- rename_basename
12081210
- cut
12091211
- paste
12101212
- clear_clipboard
@@ -1337,6 +1339,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13371339
`D` trash trash a file via |trash| option
13381340
`r` rename rename a file
13391341
`<C-r>` full_rename rename a file and omit the filename on input
1342+
`e` rename_basename rename a file with filename-modifiers ':t:r' without changing extension
13401343
`x` cut add/remove file/directory to cut clipboard
13411344
`c` copy add/remove file/directory to copy clipboard
13421345
`p` paste paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation
@@ -1388,6 +1391,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
13881391
{ key = "D", action = "trash" },
13891392
{ key = "r", action = "rename" },
13901393
{ key = "<C-r>", action = "full_rename" },
1394+
{ key = "e", action = "rename_basename" },
13911395
{ key = "x", action = "cut" },
13921396
{ key = "c", action = "copy" },
13931397
{ key = "p", action = "paste" },

lua/nvim-tree/actions/dispatch.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ local Actions = {
2222
copy = require("nvim-tree.actions.fs.copy-paste").copy,
2323
create = require("nvim-tree.actions.fs.create-file").fn,
2424
cut = require("nvim-tree.actions.fs.copy-paste").cut,
25-
full_rename = require("nvim-tree.actions.fs.rename-file").fn(true),
25+
full_rename = require("nvim-tree.actions.fs.rename-file").fn ":p",
2626
paste = require("nvim-tree.actions.fs.copy-paste").paste,
2727
trash = require("nvim-tree.actions.fs.trash").fn,
2828
remove = require("nvim-tree.actions.fs.remove-file").fn,
29-
rename = require("nvim-tree.actions.fs.rename-file").fn(false),
29+
rename = require("nvim-tree.actions.fs.rename-file").fn ":t",
30+
rename_basename = require("nvim-tree.actions.fs.rename-file").fn ":t:r",
3031

3132
-- Movements in tree
3233
close_node = require("nvim-tree.actions.moves.parent").fn(true),

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ local notify = require "nvim-tree.notify"
55

66
local M = {}
77

8+
local ALLOWED_MODIFIERS = {
9+
[":p"] = true,
10+
[":t"] = true,
11+
[":t:r"] = true,
12+
}
13+
814
local function err_fmt(from, to, reason)
915
return string.format("Cannot rename %s -> %s: %s", from, to, reason)
1016
end
@@ -25,25 +31,53 @@ function M.rename(node, to)
2531
events._dispatch_node_renamed(node.absolute_path, to)
2632
end
2733

28-
function M.fn(with_sub)
29-
return function(node)
34+
function M.fn(default_modifier)
35+
default_modifier = default_modifier or ":t"
36+
37+
return function(node, modifier)
38+
if type(node) ~= "table" then
39+
node = lib.get_node_at_cursor()
40+
end
41+
42+
if type(modifier) ~= "string" then
43+
modifier = default_modifier
44+
end
45+
46+
-- support for only specific modifiers have been implemented
47+
if not ALLOWED_MODIFIERS[modifier] then
48+
return notify.warn(
49+
"Modifier " .. vim.inspect(modifier) .. " is not in allowed list : " .. table.concat(ALLOWED_MODIFIERS, ",")
50+
)
51+
end
52+
3053
node = lib.get_last_group_node(node)
3154
if node.name == ".." then
3255
return
3356
end
3457

3558
local namelen = node.name:len()
36-
local abs_path = with_sub and node.absolute_path:sub(0, namelen * -1 - 1) or node.absolute_path
59+
local directory = node.absolute_path:sub(0, namelen * -1 - 1)
60+
local default_path
61+
local prepend = ""
62+
local append = ""
63+
default_path = vim.fn.fnamemodify(node.absolute_path, modifier)
64+
if modifier:sub(0, 2) == ":t" then
65+
prepend = directory
66+
end
67+
if modifier == ":t:r" then
68+
local extension = vim.fn.fnamemodify(node.name, ":e")
69+
append = extension:len() == 0 and "" or "." .. extension
70+
end
3771

38-
local input_opts = { prompt = "Rename to ", default = abs_path, completion = "file" }
72+
local input_opts = { prompt = "Rename to ", default = default_path, completion = "file" }
3973

4074
vim.ui.input(input_opts, function(new_file_path)
4175
utils.clear_prompt()
4276
if not new_file_path then
4377
return
4478
end
4579

46-
M.rename(node, new_file_path)
80+
M.rename(node, prepend .. new_file_path .. append)
4781
if M.enable_reload then
4882
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
4983
end

lua/nvim-tree/actions/init.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ local DEFAULT_MAPPINGS = {
131131
action = "full_rename",
132132
desc = "rename a file and omit the filename on input",
133133
},
134+
{
135+
key = "e",
136+
action = "rename_basename",
137+
desc = "rename a file with filename-modifiers ':t:r' without changing extension",
138+
},
134139
{
135140
key = "x",
136141
action = "cut",

lua/nvim-tree/api.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ local Api = {
99
}
1010

1111
local function inject_node(f)
12-
return function(node)
12+
return function(node, ...)
1313
node = node or require("nvim-tree.lib").get_node_at_cursor()
14-
f(node)
14+
f(node, ...)
1515
end
1616
end
1717

@@ -47,8 +47,10 @@ Api.tree.toggle_help = require("nvim-tree.actions.tree-modifiers.toggles").help
4747
Api.fs.create = inject_node(require("nvim-tree.actions.fs.create-file").fn)
4848
Api.fs.remove = inject_node(require("nvim-tree.actions.fs.remove-file").fn)
4949
Api.fs.trash = inject_node(require("nvim-tree.actions.fs.trash").fn)
50-
Api.fs.rename = inject_node(require("nvim-tree.actions.fs.rename-file").fn(false))
51-
Api.fs.rename_sub = inject_node(require("nvim-tree.actions.fs.rename-file").fn(true))
50+
Api.fs.rename_node = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":t")
51+
Api.fs.rename = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":t")
52+
Api.fs.rename_sub = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":p")
53+
Api.fs.rename_basename = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":t:r")
5254
Api.fs.cut = inject_node(require("nvim-tree.actions.fs.copy-paste").cut)
5355
Api.fs.paste = inject_node(require("nvim-tree.actions.fs.copy-paste").paste)
5456
Api.fs.clear_clipboard = require("nvim-tree.actions.fs.copy-paste").clear_clipboard

0 commit comments

Comments
 (0)