Skip to content

Commit

Permalink
fix(actions.move): correctly rename open (dir) buffers (#336)
Browse files Browse the repository at this point in the history
* fix(actions): correctly rename open (dir) buffers

* docs(actions): document order sensitivity of moving

* [docgen] Update doc/telescope-file-browser.txt
skip-checks: true

---------

Co-authored-by: Github Actions <actions@github>
  • Loading branch information
fdschmidt93 and Github Actions authored Nov 16, 2023
1 parent da2a20c commit f41675f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
7 changes: 6 additions & 1 deletion doc/telescope-file-browser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ fb_actions.rename({prompt_bufnr}) *telescope-file-browser.actions.rename()*
fb_actions.move({prompt_bufnr}) *telescope-file-browser.actions.move()*
Move multi-selected files or folders to current directory in
|telescope-file-browser.picker.file_browser|.
Note: Performs a blocking synchronized file-system operation.

- Notes:
- Performs a blocking synchronized file-system operation.
- Moving multi-selections is sensitive to order of selection, which
potentially unpacks files from parent(s) dirs if files are selected
first.


Parameters: ~
Expand Down
26 changes: 19 additions & 7 deletions lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ fb_actions.rename = function(prompt_bufnr)
end

--- Move multi-selected files or folders to current directory in |telescope-file-browser.picker.file_browser|.<br>
--- Note: Performs a blocking synchronized file-system operation.
--- - Notes:
--- - Performs a blocking synchronized file-system operation.
--- - Moving multi-selections is sensitive to order of selection,
--- which potentially unpacks files from parent(s) dirs
--- if files are selected first.
---@param prompt_bufnr number: The prompt bufnr
fb_actions.move = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
Expand All @@ -342,17 +346,25 @@ fb_actions.move = function(prompt_bufnr)
local skipped = {}

for idx, selection in ipairs(selections) do
local filename = selection.filename:sub(#selection:parent().filename + 2)
local new_path = Path:new { target_dir, filename }
-- use vim.fs rather than plenary to fetch basename, more battle-tested
local old_path_absolute = selection:absolute()
local basename = vim.fs.basename(old_path_absolute)
local new_path = Path:new { target_dir, basename }
if new_path:exists() then
table.insert(skipped, filename)
table.insert(skipped, basename)
else
local new_path_absolute = new_path:absolute()
selection:rename {
new_name = new_path.filename,
new_name = new_path_absolute,
}
table.insert(moved, filename)
if not selection:is_dir() then
fb_utils.rename_buf(old_path_absolute, new_path_absolute)
else
fb_utils.rename_dir_buf(old_path_absolute, new_path_absolute)
end
table.insert(moved, basename)
if idx == 1 and #selections == 1 then
fb_utils.selection_callback(current_picker, new_path:absolute())
fb_utils.selection_callback(current_picker, new_path_absolute)
end
end
end
Expand Down

0 comments on commit f41675f

Please sign in to comment.