-
-
Notifications
You must be signed in to change notification settings - Fork 624
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
alex-courtis
merged 25 commits into
nvim-tree:master
from
ianhomer:feature/relative-rename
Dec 16, 2022
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
34a5b70
relative rename action
ianhomer 2646bfa
🔥 remove debug print statement
ianhomer 9dbef88
🐛 better handling of dot files
ianhomer af57e87
🔧 keymap e for relative-rename action
ianhomer 470132e
📝 update help with relative-rename mapping
ianhomer 47312e0
✨ add API for rename_relative
ianhomer feb22bb
🚨 correct lint warnings
ianhomer 7c70489
rename_relative -> rename_root
alex-courtis 999f41b
stylua
alex-courtis c573ba3
♻️ use fnamemodify instead of custom logic
ianhomer b3fd1f2
💥 refactor renaming api using vim filename modifiers
ianhomer 8ef88c9
🐛 make api rename, without args, functional
ianhomer 9bf034a
✨ allow modifier argument to be used in API call
ianhomer 5ed4524
📝 update documentation with new command name
ianhomer c097df2
rename-file.fn takes only a modifier as argument
alex-courtis 0f05d74
add Api.fs.rename_basename, specify modifiers for rename, rename_sub
alex-courtis c190007
add Api.fs.rename_node
alex-courtis fa01542
rename-file tidy allowed modifiers
alex-courtis ff670c8
🐛 fix bugs after last refactoring
ianhomer 98bbb09
🐛 correct absolute rename
ianhomer 7dc6eda
🔥 remove debug print statements
ianhomer 3571b01
Merge remote-tracking branch 'upstream/master' into feature/relative-…
alex-courtis 291487d
Merge remote-tracking branch 'upstream/master' into feature/relative-…
alex-courtis 3354ab3
stylua
alex-courtis 0a515ba
Merge remote-tracking branch 'upstream/master' into feature/relative-…
alex-courtis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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