Skip to content
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

feat: toggle view of multi selections #48

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions doc/telescope-file-browser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ fb_actions.select_all({prompt_bufnr}) *fb_actions.select_all()*
{prompt_bufnr} (number) The prompt bufnr


fb_actions.toggle_selections({prompt_bufnr}) *fb_actions.toggle_selections()*
Toggle between view of multi-selections and previous browser.
- Note:
- WARNING: many |fb_actions| might not work.
- De-selections are persisted to other finders!


Parameters: ~
{prompt_bufnr} (number) The prompt bufnr



================================================================================
*telescope-file-browser.finders*
Expand Down
2 changes: 2 additions & 0 deletions lua/telescope/_extensions/file_browser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ local pconf = {
["<C-s>"] = fb_actions.goto_home_dir,
["<C-t>"] = fb_actions.change_cwd,
["<C-y>"] = fb_actions.copy_file,
["<C-k>"] = fb_actions.toggle_selections,
},
["n"] = {
["dd"] = fb_actions.remove_file,
Expand All @@ -87,6 +88,7 @@ local pconf = {
["s"] = fb_actions.goto_home_dir,
["t"] = fb_actions.change_cwd,
["y"] = fb_actions.copy_file,
["k"] = fb_actions.toggle_selections,
},
},
attach_mappings = function(prompt_bufnr, _)
Expand Down
42 changes: 42 additions & 0 deletions lua/telescope/_extensions/file_browser/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -506,5 +506,47 @@ fb_actions.select_all = function(prompt_bufnr)
end)
end

--- Toggle between view of multi-selections and previous browser.
--- - Note:
--- - WARNING: many |fb_actions| might not work.
--- - De-selections are persisted to other finders!
---@param prompt_bufnr number: The prompt bufnr
fb_actions.toggle_selections = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
if current_picker._cached_finder == nil then
local finder = current_picker.finder
current_picker._cached_finder = current_picker.finder
local selections = current_picker:get_multi_selection()
local entries = {}
for _, sel in ipairs(selections) do
table.insert(entries, sel.value)
end
local make_entry = finder.entry_maker
local new_finder = require("telescope.finders").new_table {
results = entries,
entry_maker = make_entry { cwd = vim.loop.cwd() },
}
if current_picker.prompt_border then
current_picker.prompt_border:change_title "File Browser: Multi Selections"
end
if current_picker.results_border then
current_picker.results_border:change_title(vim.loop.cwd())
end
current_picker:refresh(new_finder, { reset_prompt = true, multi = current_picker._multi })
else
local finder = current_picker._cached_finder
current_picker._cached_finder = nil
if current_picker.prompt_border then
local new_title = finder.files and "File Browser" or "Folder Browser"
current_picker.prompt_border:change_title(new_title)
end
if current_picker.results_border then
local new_title = finder.files and Path:new(finder.path):make_relative(vim.loop.cwd()) .. os_sep or finder.cwd
current_picker.results_border:change_title(new_title)
end
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
end
end

fb_actions = transform_mod(fb_actions)
return fb_actions