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 all 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: 4 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,10 @@ exists.
- create
- remove
- trash
- rename_node `(node: table, modifier?: string vim.fn.fnamemodify argument)`
Copy link
Member

Choose a reason for hiding this comment

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

I took the liberty of adding this new pattern. Will be improved later: #1753

- rename
- rename_sub
- rename_basename
- cut
- paste
- clear_clipboard
Expand Down Expand Up @@ -1337,6 +1339,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 @@ -1388,6 +1391,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
5 changes: 3 additions & 2 deletions lua/nvim-tree/actions/dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ local Actions = {
copy = require("nvim-tree.actions.fs.copy-paste").copy,
create = require("nvim-tree.actions.fs.create-file").fn,
cut = require("nvim-tree.actions.fs.copy-paste").cut,
full_rename = require("nvim-tree.actions.fs.rename-file").fn(true),
full_rename = require("nvim-tree.actions.fs.rename-file").fn ":p",
paste = require("nvim-tree.actions.fs.copy-paste").paste,
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 = require("nvim-tree.actions.fs.rename-file").fn ":t",
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
44 changes: 39 additions & 5 deletions lua/nvim-tree/actions/fs/rename-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ local notify = require "nvim-tree.notify"

local M = {}

local ALLOWED_MODIFIERS = {
[":p"] = true,
[":t"] = true,
[":t:r"] = true,
}

local function err_fmt(from, to, reason)
return string.format("Cannot rename %s -> %s: %s", from, to, reason)
end
Expand All @@ -25,25 +31,53 @@ 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(default_modifier)
default_modifier = default_modifier or ":t"

return function(node, modifier)
Copy link
Member

Choose a reason for hiding this comment

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

I removed capability for the first argument to be a modifier after adding the API, as I couldn't see a use case.

I may be missing something...

if type(node) ~= "table" then
node = lib.get_node_at_cursor()
end

if type(modifier) ~= "string" then
modifier = default_modifier
end

-- support for only specific modifiers have been implemented
if not ALLOWED_MODIFIERS[modifier] then
return notify.warn(
"Modifier " .. vim.inspect(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 - 1)
local default_path
local prepend = ""
local append = ""
default_path = vim.fn.fnamemodify(node.absolute_path, modifier)
if modifier:sub(0, 2) == ":t" then
prepend = directory
end
if modifier == ":t:r" then
local extension = vim.fn.fnamemodify(node.name, ":e")
append = extension:len() == 0 and "" or "." .. extension
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 @@ -131,6 +131,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
10 changes: 6 additions & 4 deletions lua/nvim-tree/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ local Api = {
}

local function inject_node(f)
return function(node)
return function(node, ...)
node = node or require("nvim-tree.lib").get_node_at_cursor()
f(node)
f(node, ...)
end
end

Expand Down Expand Up @@ -47,8 +47,10 @@ 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_sub = inject_node(require("nvim-tree.actions.fs.rename-file").fn(true))
Api.fs.rename_node = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":t")
Api.fs.rename = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":t")
Api.fs.rename_sub = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":p")
Api.fs.rename_basename = inject_node(require("nvim-tree.actions.fs.rename-file").fn ":t:r")
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)
Api.fs.clear_clipboard = require("nvim-tree.actions.fs.copy-paste").clear_clipboard
Expand Down