Skip to content

action to rename the base name of file without changing directory or extension (#1155) #1791

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 25 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
34a5b70
relative rename action
ianhomer Nov 30, 2022
2646bfa
🔥 remove debug print statement
ianhomer Nov 30, 2022
9dbef88
🐛 better handling of dot files
ianhomer Dec 3, 2022
af57e87
🔧 keymap e for relative-rename action
ianhomer Dec 3, 2022
470132e
📝 update help with relative-rename mapping
ianhomer Dec 3, 2022
47312e0
✨ add API for rename_relative
ianhomer Dec 3, 2022
feb22bb
🚨 correct lint warnings
ianhomer Dec 3, 2022
7c70489
rename_relative -> rename_root
alex-courtis Dec 4, 2022
999f41b
stylua
alex-courtis Dec 4, 2022
c573ba3
♻️ use fnamemodify instead of custom logic
ianhomer Dec 4, 2022
b3fd1f2
💥 refactor renaming api using vim filename modifiers
ianhomer Dec 9, 2022
8ef88c9
🐛 make api rename, without args, functional
ianhomer Dec 9, 2022
9bf034a
✨ allow modifier argument to be used in API call
ianhomer Dec 9, 2022
5ed4524
📝 update documentation with new command name
ianhomer Dec 9, 2022
c097df2
rename-file.fn takes only a modifier as argument
alex-courtis Dec 10, 2022
0f05d74
add Api.fs.rename_basename, specify modifiers for rename, rename_sub
alex-courtis Dec 10, 2022
c190007
add Api.fs.rename_node
alex-courtis Dec 10, 2022
fa01542
rename-file tidy allowed modifiers
alex-courtis Dec 10, 2022
ff670c8
🐛 fix bugs after last refactoring
ianhomer Dec 10, 2022
98bbb09
🐛 correct absolute rename
ianhomer Dec 10, 2022
7dc6eda
🔥 remove debug print statements
ianhomer Dec 10, 2022
3571b01
Merge remote-tracking branch 'upstream/master' into feature/relative-…
alex-courtis Dec 11, 2022
291487d
Merge remote-tracking branch 'upstream/master' into feature/relative-…
alex-courtis Dec 11, 2022
3354ab3
stylua
alex-courtis Dec 11, 2022
0a515ba
Merge remote-tracking branch 'upstream/master' into feature/relative-…
alex-courtis Dec 16, 2022
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
4 changes: 3 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Subsequent calls to setup will replace the previous configuration.
debounce_delay = 50,
severity = {
min = vim.diagnostic.severity.HINT,
max = vim.diagnostic.severity.ERROR
max = vim.diagnostic.severity.ERROR,
},
icons = {
hint = "",
Expand Down Expand Up @@ -1328,6 +1328,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
`D` trash trash a file via |trash| option
`r` rename rename a file
`<C-r>` full_rename rename a file and omit the filename on input
`e` rename_basename rename a file with filename-modifiers ':t:r' without changing extension
`x` cut add/remove file/directory to cut clipboard
`c` copy add/remove file/directory to copy clipboard
`p` paste paste from clipboard; cut clipboard has precedence over copy; will prompt for confirmation
Expand Down Expand Up @@ -1377,6 +1378,7 @@ DEFAULT MAPPINGS *nvim-tree-default-mappings
{ key = "D", action = "trash" },
{ key = "r", action = "rename" },
{ key = "<C-r>", action = "full_rename" },
{ key = "e", action = "rename_basename" },
{ key = "x", action = "cut" },
{ key = "c", action = "copy" },
{ key = "p", action = "paste" },
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local Actions = {
trash = require("nvim-tree.actions.fs.trash").fn,
remove = require("nvim-tree.actions.fs.remove-file").fn,
rename = require("nvim-tree.actions.fs.rename-file").fn(false),
rename_basename = require("nvim-tree.actions.fs.rename-file").fn(":t:r"),

-- Movements in tree
close_node = require("nvim-tree.actions.moves.parent").fn(true),
Expand Down
61 changes: 56 additions & 5 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,76 @@ function M.rename(node, to)
events._dispatch_node_renamed(node.absolute_path, to)
end

function M.fn(with_sub)
return function(node)
function M.fn(initialisation_arg)
local default_modifier = ":t"
-- backwards compatibility, support modifier as boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't API; we can break it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a fix.

if type(initialisation_arg) == "boolean" then
if initialisation_arg then
modifier = ":p"
end
elseif type(initialisation_arg) == "string" then
default_modifier = initialisation_arg
end

return function(modifier_arg)
local node
local modifier = default_modifier
if type(modifier_arg) == "table" then
node = modifier_arg
elseif type(modifier_arg) == "string" then
node = lib.get_node_at_cursor()
modifier = modifier_arg
else
return notify.warn("Type " .. type(modifier_arg) .. " not supported in rename")
end

-- support for only specific modifiers have been implemented
local allowed_modifiers = {
":p",
":t",
":t:r",
}

local lookup = {}
for _, v in ipairs(allowed_modifiers) do
lookup[v] = true
end

if lookup[modifier] == nil then
return notify.warn(
"Modifier " .. modifier .. " is not in allowed list : " .. table.concat(allowed_modifiers, ",")
)
end

node = lib.get_last_group_node(node)
if node.name == ".." then
return
end

local namelen = node.name:len()
local abs_path = with_sub and node.absolute_path:sub(0, namelen * -1 - 1) or node.absolute_path
local directory = node.absolute_path:sub(0, namelen - 1)
local default_path
local prepend = ""
local append = ""
if modifier == ":" then
default_path = directory
else
default_path = vim.fn.fnamemodify(node.name, modifier)
if modifier == ":t:r" then
local extension = vim.fn.fnamemodify(node.name, ":e")
append = extension:len() == 0 and "" or "." .. extension
end
end

local input_opts = { prompt = "Rename to ", default = abs_path, completion = "file" }
local input_opts = { prompt = "Rename to ", default = default_path, completion = "file" }

vim.ui.input(input_opts, function(new_file_path)
utils.clear_prompt()
if not new_file_path then
return
end

M.rename(node, new_file_path)
M.rename(node, prepend .. new_file_path .. append)
if M.enable_reload then
require("nvim-tree.actions.reloaders.reloaders").reload_explorer()
end
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ local DEFAULT_MAPPINGS = {
action = "full_rename",
desc = "rename a file and omit the filename on input",
},
{
key = "e",
action = "rename_basename",
desc = "rename a file with filename-modifiers ':t:r' without changing extension",
},
{
key = "x",
action = "cut",
Expand Down
2 changes: 1 addition & 1 deletion lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Api.tree.toggle_help = require("nvim-tree.actions.tree-modifiers.toggles").help
Api.fs.create = inject_node(require("nvim-tree.actions.fs.create-file").fn)
Api.fs.remove = inject_node(require("nvim-tree.actions.fs.remove-file").fn)
Api.fs.trash = inject_node(require("nvim-tree.actions.fs.trash").fn)
Api.fs.rename = inject_node(require("nvim-tree.actions.fs.rename-file").fn(false))
Api.fs.rename = inject_node(require("nvim-tree.actions.fs.rename-file").fn())
Api.fs.rename_sub = inject_node(require("nvim-tree.actions.fs.rename-file").fn(true))
Api.fs.cut = inject_node(require("nvim-tree.actions.fs.copy-paste").cut)
Api.fs.paste = inject_node(require("nvim-tree.actions.fs.copy-paste").paste)
Expand Down