@@ -133,6 +133,7 @@ local function restore_from_state(state, source_label)
133133 M .state .grep_config = state .grep_config
134134 M .state .grep_mode = state .grep_mode
135135 M .state .selected_files = vim .deepcopy (state .selected_files or {})
136+ M .state .selected_file_order = vim .deepcopy (state .selected_file_order or {})
136137 M .state .selected_items = vim .deepcopy (state .selected_items or {})
137138
138139 -- Restore the saved base_path for the indexer if it differs from the current CWD
@@ -266,6 +267,7 @@ function M.toggle_debug()
266267 local current_grep_config = M .state .grep_config
267268 local current_filtered_items = M .state .filtered_items
268269 local current_selected_files = M .state .selected_files
270+ local current_selected_file_order = M .state .selected_file_order
269271 local current_selected_items = M .state .selected_items
270272
271273 M .close ()
@@ -281,6 +283,7 @@ function M.toggle_debug()
281283 M .state .grep_mode = current_grep_mode
282284 M .state .filtered_items = current_filtered_items
283285 M .state .selected_files = current_selected_files
286+ M .state .selected_file_order = current_selected_file_order
284287 M .state .selected_items = current_selected_items
285288 M .render_list ()
286289 M .update_preview ()
@@ -461,6 +464,12 @@ function M.select(action)
461464
462465 -- In grep mode (or when selecting a grep suggestion), derive location from the match item
463466 local is_grep_item = mode == ' grep' or suggestion_source == ' grep'
467+
468+ -- When opening with selections active, open every selected file. The first
469+ -- selected file is focused; the rest are added as listed buffers.
470+ local selected_file_entries = {}
471+ if action == ' edit' and not is_grep_item then selected_file_entries = picker_ui_state .get_selected_file_entries () end
472+
464473 if is_grep_item and item .line_number and item .line_number > 0 then
465474 location = { line = item .line_number }
466475 if item .col and item .col > 0 then
@@ -485,6 +494,10 @@ function M.select(action)
485494 end
486495 end
487496
497+ -- The focused file is the first selection, which may differ from the cursor
498+ -- item; a cursor-derived location no longer applies, so drop it.
499+ if # selected_file_entries > 0 and selected_file_entries [1 ].relative_path ~= item .relative_path then location = nil end
500+
488501 vim .cmd (' stopinsert' )
489502 M .close ()
490503
@@ -514,15 +527,23 @@ function M.select(action)
514527 end
515528
516529 if action == ' edit' then
530+ -- Add every additional selection as a listed buffer before focusing one.
531+ for _ , entry in ipairs (selected_file_entries ) do
532+ local buf = vim .fn .bufadd (entry .edit_path )
533+ vim .bo [buf ].buflisted = true
534+ end
535+
536+ local edit_path = # selected_file_entries > 0 and selected_file_entries [1 ].edit_path or relative_path
537+
517538 -- Hard guard against E1513 ("Cannot switch buffer. 'winfixbuf' is enabled"):
518539 -- if the (post-hook) current window is pinned, fall back to :split.
519540 local opened_via_split = false
520541 if window_has_winfixbuf (vim .api .nvim_get_current_win ()) then
521- vim .cmd (' split ' .. vim .fn .fnameescape (relative_path ))
542+ vim .cmd (' split ' .. vim .fn .fnameescape (edit_path ))
522543 opened_via_split = true
523544 end
524545
525- if not opened_via_split then vim .cmd (' edit ' .. vim .fn .fnameescape (relative_path )) end
546+ if not opened_via_split then vim .cmd (' edit ' .. vim .fn .fnameescape (edit_path )) end
526547 elseif action == ' split' then
527548 vim .cmd (' split ' .. vim .fn .fnameescape (relative_path ))
528549 elseif action == ' vsplit' then
@@ -541,6 +562,10 @@ function M.select(action)
541562 -- Track in background thread (non-blocking, handled by Rust)
542563 if mode == ' grep' then
543564 pcall (fff .track_grep_query , query )
565+ elseif # selected_file_entries > 0 then
566+ for _ , entry in ipairs (selected_file_entries ) do
567+ pcall (fff .track_query_completion , query , entry .relative_path )
568+ end
544569 else
545570 pcall (fff .track_query_completion , query , item .relative_path )
546571 end
@@ -700,6 +725,7 @@ function M.open(opts)
700725 if M .state .active then return end
701726
702727 M .state .selected_files = {}
728+ M .state .selected_file_order = {}
703729 M .state .selected_items = {}
704730 M .state .renderer = opts and opts .renderer or nil
705731 M .state .mode = opts and opts .mode or nil
0 commit comments