-
-
Notifications
You must be signed in to change notification settings - Fork 635
feat: add rename with substitute command #3338
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
Open
Uanela
wants to merge
2
commits into
master
Choose a base branch
from
feat/rename-with-substitute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,6 +140,52 @@ function M.global() | |
| end, | ||
| }) | ||
| end | ||
|
|
||
| vim.api.nvim_create_autocmd("CmdlineLeave", { | ||
| group = augroup_id, | ||
| pattern = ":", | ||
| callback = function(ev) | ||
| if require("nvim-tree.view").get_bufnr() == ev.buf then | ||
| local cmd = vim.fn.getcmdline() | ||
|
|
||
| if cmd:match("s([^%w])") then | ||
| local delimiter = cmd:match("s([^%w])") | ||
| local escaped_delim = delimiter:gsub("[%%%.%+%-%*%?%^%$%(%)%[%]]", "%%%1") | ||
|
|
||
| -- Looks for: s <delimiter> <old> <delimiter> <new> <delimiter or end> | ||
| local pattern = "s" .. escaped_delim .. "([^" .. escaped_delim .. "]*)" .. escaped_delim .. "([^" .. escaped_delim .. "]*)" | ||
|
|
||
| local old_part, new_part = cmd:match(pattern) | ||
|
|
||
| local core = require("nvim-tree.core") | ||
| local explorer = core.get_explorer() | ||
| local utils = require("nvim-tree.utils") | ||
|
Comment on lines
+160
to
+162
Member
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. rename-file.lua already has these, so we should be able to remove these lines 🤞 |
||
| local bulk_rename = require("nvim-tree.actions.fs.rename-file").bulk_rename | ||
|
|
||
| local visual_marker = "'<,'>" | ||
| local nodes = cmd:sub(1, #visual_marker) == visual_marker and | ||
| utils.get_visual_nodes({ use_native = true, should_exit = false }) or | ||
| explorer and explorer:get_nodes_by_line(core.get_nodes_starting_line()) or {} | ||
| local matching_nodes = {} | ||
|
|
||
| for i = #nodes, 1, -1 do | ||
| local node = nodes[i] | ||
| if node and node.name:find(old_part) ~= nil then | ||
| table.insert(matching_nodes, node) | ||
| end | ||
| end | ||
|
|
||
| if #matching_nodes > 0 then | ||
| bulk_rename(matching_nodes, old_part, new_part) | ||
| else | ||
| require("nvim-tree.notify").notify(string.format("Not matching nodes with '%s'", old_part)) | ||
| end | ||
|
|
||
| vim.fn.setcmdline("") | ||
| end | ||
| end | ||
| end | ||
| }) | ||
| end | ||
|
|
||
| return M | ||
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 |
|---|---|---|
|
|
@@ -527,19 +527,25 @@ function M.exit_visual_mode() | |
| end | ||
|
|
||
| ---Get the visual selection range nodes, exiting visual mode. | ||
| ---@param opts? { should_exit?: boolean, use_native?: boolean } | ||
| ---@return Node[]? | ||
| function M.get_visual_nodes() | ||
| function M.get_visual_nodes(opts) | ||
| opts = opts and opts or {} | ||
|
|
||
| local explorer = require("nvim-tree.core").get_explorer() | ||
| if not explorer then | ||
| return nil | ||
| end | ||
| local start_line = vim.fn.line("v") | ||
| local end_line = vim.fn.line(".") | ||
| local start_line = vim.fn.line(opts.use_native == true and "'<" or "v") | ||
| local end_line = vim.fn.line(opts.use_native == true and "'>" or ".") | ||
|
Comment on lines
+539
to
+540
Collaborator
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. Good catch |
||
| if start_line > end_line then | ||
| start_line, end_line = end_line, start_line | ||
| end | ||
| local nodes = explorer:get_nodes_in_range(start_line, end_line) | ||
| M.exit_visual_mode() | ||
|
|
||
| if opts.should_exit ~= false then | ||
| M.exit_visual_mode() | ||
| end | ||
| return nodes | ||
| end | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Firstly, I apologise for the state of autocommands. Having them all in one place is not desirable, and was done purely for startup performance.
Fortunately, this does not have to be a global - it can be placed in it's correct place - rename-file.lua
It can be created when needed i.e. user invokes
bulk_renameand removed once the operation is complete. We could probably automatically clean it up after it is done via the{once}option of:help nvim_create_autocmd(). See help.lua for an example.