Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

89.7 KB
Raw Blame
#[cfg(test)]
mod file_finder_tests;
#[cfg(test)]
mod multi_select_tests;

use futures::future::join_all;
pub use open_path_prompt::OpenPathDelegate;

use channel::ChannelStore;
use client::ChannelId;
use collections::{HashMap, HashSet};
use editor::Editor;
use file_icons::FileIcons;
use fuzzy::{StringMatch, StringMatchCandidate};
use fuzzy_nucleo::{PathMatch, PathMatchCandidate};
use gpui::{
    Action, AnyElement, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable,
    KeyContext, Modifiers, ModifiersChangedEvent, ParentElement, Render, Styled, Task, TaskExt,
    WeakEntity, Window, actions,
};
use language::{BufferSnapshot, Point};
use open_path_prompt::{OpenPathPrompt, file_finder_settings::FileFinderSettings};
use picker::{Picker, PickerDelegate};
use project::{
    PathMatchCandidateSet, Project, ProjectPath, WorktreeId, worktree_store::WorktreeStore,
};

use settings::{ModalWidthContent, SeedQuerySetting, Settings, SettingsStore};
use std::{
    borrow::Cow,
    cmp, mem,
    ops::{Range, RangeInclusive},
    path::{Component, Path, PathBuf},
    sync::{
        Arc,
        atomic::{self, AtomicBool},
    },
    time::Duration,
};
use ui::{Checkbox, HighlightedLabel, ListItem, ListItemSpacing, Tooltip, prelude::*};
use util::{
    ResultExt, maybe,
    paths::{PathStyle, PathWithPosition},
    post_inc,
    rel_path::RelPath,
};
use workspace::{
    MAX_RECENT_SELECTIONS, ModalView, OpenChannelNotesById, OpenOptions, OpenVisible,
    SplitDirection, Workspace, item::PreviewTabsSettings, notifications::NotifyResultExt, pane,
};
use zed_actions::search::ToggleIncludeIgnored;

actions!(
    file_finder,
    [
        /// Selects the previous item in the file finder.
        SelectPrevious,
        /// Opens the selected file in the editor without dismissing the file finder,
        /// so additional files can be opened in sequence.
        OpenWithoutDismiss
    ]
);

impl ModalView for FileFinder {}

pub struct FileFinder {
    picker: Entity<Picker<FileFinderDelegate>>,
    picker_focus_handle: FocusHandle,
    init_modifiers: Option<Modifiers>,
}

pub fn init(cx: &mut App) {
    cx.observe_new(FileFinder::register).detach();
    cx.observe_new(OpenPathPrompt::register).detach();
    cx.observe_new(OpenPathPrompt::register_new_path).detach();
}

impl FileFinder {
    fn register(
        workspace: &mut Workspace,
        _window: Option<&mut Window>,
        _: &mut Context<Workspace>,
    ) {
        workspace.register_action(
            |workspace, action: &workspace::ToggleFileFinder, window, cx| {
                let Some(file_finder) = workspace.active_modal::<Self>(cx) else {
                    let seed_query = Self::seed_query(workspace, window, cx);
                    Self::open(
                        workspace,
                        action.separate_history,
                        action.include_ignored,
                        seed_query,
                        window,
                        cx,
                    )
                    .detach();
                    return;
                };

                file_finder.update(cx, |file_finder, cx| {
                    file_finder.init_modifiers = Some(window.modifiers());
                    file_finder.picker.update(cx, |picker, cx| {
                        picker.cycle_selection(window, cx);
                    });
                });
            },
        );
    }

    fn open(
        workspace: &mut Workspace,
        separate_history: bool,
        include_ignored: Option<bool>,
        seed_query: Option<String>,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) -> Task<()> {
        let project = workspace.project().read(cx);

        let currently_opened_path = workspace.active_item(cx).and_then(|item| {
            let project_path = item.project_path(cx)?;
            let abs_path = project
                .worktree_for_id(project_path.worktree_id, cx)?
                .read(cx)
                .absolutize(&project_path.path);
            Some(FoundPath::new(project_path, abs_path))
        });

        let history_items = workspace
            .recent_navigation_history(Some(MAX_RECENT_SELECTIONS), cx)
            .into_iter()
            .filter_map(|(project_path, abs_path)| {
                let path = FoundPath::new(project_path, abs_path?);
                let is_file = path.is_file(project, cx);
                Some(cx.background_spawn(async move { is_file.await.then_some(path) }))
            })
            .collect::<Vec<_>>();
        cx.spawn_in(window, async move |workspace, cx| {
            let history_items = join_all(history_items).await.into_iter().flatten();

            workspace
                .update_in(cx, |workspace, window, cx| {
                    let project = workspace.project().clone();
                    let weak_workspace = cx.entity().downgrade();
                    workspace.toggle_modal(window, cx, |window, cx| {
                        let delegate = FileFinderDelegate::new(
                            cx.entity().downgrade(),
                            weak_workspace,
                            project,
                            currently_opened_path,
                            history_items.collect(),
                            separate_history,
                            include_ignored,
                            window,
                            cx,
                        );

                        FileFinder::new(delegate, seed_query, window, cx)
                    });
                })
                .ok();
        })
    }

    /// The query to pre-fill the finder with from the user's latest selection,
    /// when `prefill_query_from_selection` is enabled. The focused pane's item
    /// (e.g. a terminal in a dock) is consulted before the active center pane's
    /// item, so the selection next to the focus wins.
    fn seed_query(
        workspace: &mut Workspace,
        window: &mut Window,
        cx: &mut Context<Workspace>,
    ) -> Option<String> {
        if !FileFinderSettings::get_global(cx).prefill_query_from_selection {
            return None;
        }

        let focused_item = workspace.focused_pane(window, cx).read(cx).active_item();
        let active_item = workspace
            .active_item(cx)
            .filter(|active| match &focused_item {
                Some(focused) => focused.item_id() != active.item_id(),
                None => true,
            });

        focused_item
            .into_iter()
            .chain(active_item)
            .find_map(|item| {
                let query = item.to_searchable_item_handle(cx)?.query_suggestion(
                    Some(SeedQuerySetting::Selection),
                    window,
                    cx,
                );
                sanitize_file_query(&query)
            })
    }

    fn new(
        delegate: FileFinderDelegate,
        seed_query: Option<String>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let modal_max_width_setting = FileFinderSettings::get_global(cx).modal_max_width;

        let project = delegate.project.clone();
        let modal_max_width = Self::modal_max_width(modal_max_width_setting, window);
        let preview = picker_preview::editor_preview(project, window, cx);
        let picker = cx.new(|cx| {
            Picker::uniform_list_with_preview(delegate, preview, window, cx)
                .initial_width(Rems::from_pixels(modal_max_width, window))
        });
        let picker_focus_handle = picker.focus_handle(cx);
        picker.update(cx, |picker, cx| {
            picker.delegate.focus_handle = picker_focus_handle.clone();
            if let Some(seed_query) = seed_query {
                picker.set_query(&seed_query, window, cx);
                picker.select_query(window, cx);
            }
            picker.delegate.refresh_history(window, cx);
        });
        Self {
            picker,
            picker_focus_handle,
            init_modifiers: window.modifiers().modified().then_some(window.modifiers()),
        }
    }

    fn handle_modifiers_changed(
        &mut self,
        event: &ModifiersChangedEvent,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(init_modifiers) = self.init_modifiers.take() else {
            return;
        };
        if self.picker.read(cx).delegate.has_changed_selected_index
            && (!event.modified() || !init_modifiers.is_subset_of(event))
        {
            self.init_modifiers = None;
            window.dispatch_action(menu::Confirm.boxed_clone(), cx);
        }
    }

    fn handle_select_prev(
        &mut self,
        _: &SelectPrevious,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.init_modifiers = Some(window.modifiers());
        window.dispatch_action(Box::new(menu::SelectPrevious), cx);
    }

    fn handle_toggle_ignored(
        &mut self,
        _: &ToggleIncludeIgnored,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.picker.update(cx, |picker, cx| {
            picker.delegate.include_ignored = match picker.delegate.include_ignored {
                Some(true) => FileFinderSettings::get_global(cx)
                    .include_ignored
                    .map(|_| false),
                Some(false) => Some(true),
                None => Some(true),
            };
            picker.delegate.include_ignored_refresh =
                picker.delegate.update_matches(picker.query(cx), window, cx);
        });
    }

    fn go_to_file_split_left(
        &mut self,
        _: &pane::SplitLeft,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_file_split_inner(SplitDirection::Left, window, cx)
    }

    fn go_to_file_split_right(
        &mut self,
        _: &pane::SplitRight,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_file_split_inner(SplitDirection::Right, window, cx)
    }

    fn go_to_file_split_up(
        &mut self,
        _: &pane::SplitUp,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_file_split_inner(SplitDirection::Up, window, cx)
    }

    fn go_to_file_split_down(
        &mut self,
        _: &pane::SplitDown,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.go_to_file_split_inner(SplitDirection::Down, window, cx)
    }

    fn go_to_file_split_inner(
        &mut self,
        split_direction: SplitDirection,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.picker.update(cx, |picker, cx| {
            let delegate = &mut picker.delegate;
            if !delegate.selected_matches.is_empty() {
                delegate.open_selected_in_one_split(split_direction, window, cx);
                return;
            }
            if let Some(workspace) = delegate.workspace.upgrade()
                && let Some(m) = delegate.matches.get(delegate.selected_index())
            {
                let path = match m {
                    Match::History { path, .. } => {
                        let worktree_id = path.project.worktree_id;
                        ProjectPath {
                            worktree_id,
                            path: Arc::clone(&path.project.path),
                        }
                    }
                    Match::Search(m) => project_path_for_search_match(&delegate.project, &m.0, cx),
                    Match::CreateNew(p) => p.clone(),
                    Match::Channel { .. } => return,
                };
                let open_task = workspace.update(cx, move |workspace, cx| {
                    workspace.split_path_preview(path, false, Some(split_direction), window, cx)
                });
                open_task.detach_and_log_err(cx);
            }
        })
    }

    fn open_without_dismiss(
        &mut self,
        _: &OpenWithoutDismiss,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        self.picker.update(cx, |picker, cx| {
            picker.delegate.confirm_without_dismiss(window, cx);
        });
    }

    pub fn modal_max_width(width_setting: ModalWidthContent, window: &mut Window) -> Pixels {
        let small_width = picker::DEFAULT_MODAL_WIDTH.to_pixels(window.rem_size());
        width_setting.to_pixels(small_width, window.viewport_size().width)
    }
}

const MAX_SEED_QUERY_LENGTH: usize = 100;

/// Flattens a selection into a single-line file query, capped to a sane length,
/// since the selection seeds a file-name lookup.
fn sanitize_file_query(selection: &str) -> Option<String> {
    let query = selection.split_whitespace().collect::<Vec<_>>().join(" ");
    (!query.is_empty()).then(|| query.chars().take(MAX_SEED_QUERY_LENGTH).collect())
}

impl EventEmitter<DismissEvent> for FileFinder {}

impl Focusable for FileFinder {
    fn focus_handle(&self, _: &App) -> FocusHandle {
        self.picker_focus_handle.clone()
    }
}

impl Render for FileFinder {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let key_context = self.picker.read(cx).delegate.key_context(window, cx);

        v_flex()
            .key_context(key_context)
            .on_modifiers_changed(cx.listener(Self::handle_modifiers_changed))
            .on_action(cx.listener(Self::handle_select_prev))
            .on_action(cx.listener(Self::handle_toggle_ignored))
            .on_action(cx.listener(Self::go_to_file_split_left))
            .on_action(cx.listener(Self::go_to_file_split_right))
            .on_action(cx.listener(Self::go_to_file_split_up))
            .on_action(cx.listener(Self::go_to_file_split_down))
            .on_action(cx.listener(Self::open_without_dismiss))
            .child(self.picker.clone())
    }
}

pub struct FileFinderDelegate {
    file_finder: WeakEntity<FileFinder>,
    workspace: WeakEntity<Workspace>,
    project: Entity<Project>,
    channel_store: Option<Entity<ChannelStore>>,
    search_count: usize,
    latest_search_id: usize,
    latest_search_did_cancel: bool,
    latest_search_query: Option<FileSearchQuery>,
    currently_opened_path: Option<FoundPath>,
    matches: Matches,
    selected_matches: Vec<SelectedMatch>,
    selected_index: usize,
    has_changed_selected_index: bool,
    cancel_flag: Arc<AtomicBool>,
    search_in_flight: Arc<AtomicBool>,
    history_items: Vec<FoundPath>,
    history_refresh: Task<()>,
    separate_history: bool,
    first_update: bool,
    focus_handle: FocusHandle,
    include_ignored: Option<bool>,
    include_ignored_refresh: Task<()>,
    debounce_next_refresh: bool,
}

/// Use a custom ordering for file finder: the regular one
/// defines max element with the highest score and the latest alphanumerical path (in case of a tie on other params), e.g:
/// `[{score: 0.5, path = "c/d" }, { score: 0.5, path = "/a/b" }]`
///
/// In the file finder, we would prefer to have the max element with the highest score and the earliest alphanumerical path, e.g:
/// `[{ score: 0.5, path = "/a/b" }, {score: 0.5, path = "c/d" }]`
/// as the files are shown in the project panel lists.
#[derive(Debug, Clone, PartialEq, Eq)]
struct ProjectPanelOrdMatch(PathMatch);

impl Ord for ProjectPanelOrdMatch {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.0
            .score
            .partial_cmp(&other.0.score)
            .unwrap_or(cmp::Ordering::Equal)
            .then_with(|| self.0.worktree_id.cmp(&other.0.worktree_id))
            .then_with(|| {
                other
                    .0
                    .distance_to_relative_ancestor
                    .cmp(&self.0.distance_to_relative_ancestor)
            })
            .then_with(|| self.0.path.cmp(&other.0.path).reverse())
    }
}

impl PartialOrd for ProjectPanelOrdMatch {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

#[derive(Debug, Default)]
struct Matches {
    separate_history: bool,
    matches: Vec<Match>,
}

#[derive(Debug, Clone)]
enum Match {
    History {
        path: FoundPath,
        panel_match: Option<ProjectPanelOrdMatch>,
    },
    Search(ProjectPanelOrdMatch),
    Channel {
        channel_id: ChannelId,
        channel_name: SharedString,
        string_match: StringMatch,
    },
    CreateNew(ProjectPath),
}

impl Match {
    fn relative_path(&self) -> Option<&Arc<RelPath>> {
        match self {
            Match::History { path, .. } => Some(&path.project.path),
            Match::Search(panel_match) => Some(&panel_match.0.path),
            Match::Channel { .. } | Match::CreateNew(_) => None,
        }
    }

    fn abs_path(&self, project: &Entity<Project>, cx: &App) -> Option<PathBuf> {
        match self {
            Match::History { path, .. } => Some(path.absolute.clone()),
            Match::Search(ProjectPanelOrdMatch(path_match)) => Some(
                project
                    .read(cx)
                    .worktree_for_id(WorktreeId::from_usize(path_match.worktree_id), cx)?
                    .read(cx)
                    .absolutize(&path_match.path),
            ),
            Match::Channel { .. } | Match::CreateNew(_) => None,
        }
    }

    fn panel_match(&self) -> Option<&ProjectPanelOrdMatch> {
        match self {
            Match::History { panel_match, .. } => panel_match.as_ref(),
            Match::Search(panel_match) => Some(panel_match),
            Match::Channel { .. } | Match::CreateNew(_) => None,
        }
    }
}

/// Wrapper with Eq comparing the worktree-qualified path of file matches
#[derive(Clone)]
struct SelectedMatch(pub Match);

impl SelectedMatch {
    fn new(m: Match) -> Option<Self> {
        match m {
            Match::History { .. } | Match::Search(_) => Some(Self(m)),
            Match::Channel { .. } | Match::CreateNew(_) => None,
        }
    }
}

impl PartialEq for SelectedMatch {
    fn eq(&self, other: &Self) -> bool {
        *self == other.0
    }
}

impl Eq for SelectedMatch {}

impl PartialEq<Match> for SelectedMatch {
    fn eq(&self, other: &Match) -> bool {
        match (&self.0, other) {
            (Match::History { path: a, .. }, Match::History { path: b, .. }) => {
                a.project == b.project
            }
            (Match::Search(a), Match::Search(b)) => {
                a.0.worktree_id == b.0.worktree_id && a.0.path == b.0.path
            }
            (Match::History { path: h, .. }, Match::Search(s))
            | (Match::Search(s), Match::History { path: h, .. }) => {
                h.project.worktree_id.to_usize() == s.0.worktree_id && h.project.path == s.0.path
            }
            _ => false,
        }
    }
}

impl Matches {
    fn len(&self) -> usize {
        self.matches.len()
    }

    fn get(&self, index: usize) -> Option<&Match> {
        self.matches.get(index)
    }

    fn position(
        &self,
        entry: &Match,
        currently_opened: Option<&FoundPath>,
    ) -> Result<usize, usize> {
        if let Match::History {
            path,
            panel_match: None,
        } = entry
        {
            // Slow case: linear search by path. Should not happen actually,
            // since we call `position` only if matches set changed, but the query has not changed.
            // And History entries do not have panel_match if query is empty, so there's no
            // reason for the matches set to change.
            self.matches
                .iter()
                .position(|m| match m.relative_path() {
                    Some(p) => path.project.path == *p,
                    None => false,
                })
                .ok_or(0)
        } else {
            self.matches.binary_search_by(|m| {
                // `reverse()` since if cmp_matches(a, b) == Ordering::Greater, then a is better than b.
                // And we want the better entries go first.
                Self::cmp_matches(self.separate_history, currently_opened, m, entry).reverse()
            })
        }
    }

    fn push_new_matches<'a>(
        &'a mut self,
        worktree_store: Entity<WorktreeStore>,
        cx: &'a App,
        history_items: impl IntoIterator<Item = &'a FoundPath> + Clone,
        currently_opened: Option<&'a FoundPath>,
        query: Option<&FileSearchQuery>,
        new_search_matches: impl Iterator<Item = ProjectPanelOrdMatch>,
        extend_old_matches: bool,
        path_style: PathStyle,
    ) {
        let Some(query) = query else {
            // assuming that if there's no query, then there's no search matches.
            self.matches.clear();
            let path_to_entry = |found_path: &FoundPath| Match::History {
                path: found_path.clone(),
                panel_match: None,
            };

            self.matches
                .extend(history_items.into_iter().map(path_to_entry));
            return;
        };

        let worktree_name_by_id = worktree_names_for_history_matching(&worktree_store, cx);
        let new_history_matches = matching_history_items(
            history_items,
            currently_opened,
            worktree_name_by_id,
            query,
            path_style,
        );
        let new_search_matches: Vec<Match> = new_search_matches
            .filter(|path_match| {
                !new_history_matches.contains_key(&ProjectPath {
                    path: path_match.0.path.clone(),
                    worktree_id: WorktreeId::from_usize(path_match.0.worktree_id),
                })
            })
            .map(Match::Search)
            .collect();

        if extend_old_matches {
            // since we take history matches instead of new search matches
            // and history matches has not changed(since the query has not changed and we do not extend old matches otherwise),
            // old matches can't contain paths present in history_matches as well.
            self.matches.retain(|m| matches!(m, Match::Search(_)));
        } else {
            self.matches.clear();
        }

        // At this point we have an unsorted set of new history matches, an unsorted set of new search matches
        // and a sorted set of old search matches.
        // It is possible that the new search matches' paths contain some of the old search matches' paths.
        // History matches' paths are unique, since store in a HashMap by path.
        // We build a sorted Vec<Match>, eliminating duplicate search matches.
        // Search matches with the same paths should have equal `ProjectPanelOrdMatch`, so we should
        // not have any duplicates after building the final list.
        for new_match in new_history_matches.into_values().chain(new_search_matches) {
            match self.position(&new_match, currently_opened) {
                Ok(_duplicate) => continue,
                Err(i) => {
                    self.matches.insert(i, new_match);
                    if self.matches.len() == 100 {
                        break;
                    }
                }
            }
        }
    }

    /// If a < b, then a is a worse match, aligning with the `ProjectPanelOrdMatch` ordering.
    fn cmp_matches(
        separate_history: bool,
        currently_opened: Option<&FoundPath>,
        a: &Match,
        b: &Match,
    ) -> cmp::Ordering {
        // Handle CreateNew variant - always put it at the end
        match (a, b) {
            (Match::CreateNew(_), _) => return cmp::Ordering::Less,
            (_, Match::CreateNew(_)) => return cmp::Ordering::Greater,
            _ => {}
        }

        match (&a, &b) {
            // bubble currently opened files to the top
            (Match::History { path, .. }, _) if Some(path) == currently_opened => {
                return cmp::Ordering::Greater;
            }
            (_, Match::History { path, .. }) if Some(path) == currently_opened => {
                return cmp::Ordering::Less;
            }

            _ => {}
        }

        if separate_history {
            match (a, b) {
                (Match::History { .. }, Match::Search(_)) => return cmp::Ordering::Greater,
                (Match::Search(_), Match::History { .. }) => return cmp::Ordering::Less,

                _ => {}
            }
        }

        // For file-vs-file matches, use the existing detailed comparison.
        if let (Some(a_panel), Some(b_panel)) = (a.panel_match(), b.panel_match()) {
            return a_panel.cmp(b_panel);
        }

        let a_score = Self::match_score(a);
        let b_score = Self::match_score(b);
        // When at least one side is a channel, compare by raw score.
        a_score
            .partial_cmp(&b_score)
            .unwrap_or(cmp::Ordering::Equal)
    }

    fn match_score(m: &Match) -> f64 {
        match m {
            Match::History { panel_match, .. } => panel_match.as_ref().map_or(0.0, |pm| pm.0.score),
            Match::Search(pm) => pm.0.score,
            Match::Channel { string_match, .. } => string_match.score,
            Match::CreateNew(_) => 0.0,
        }
    }
}

fn matching_history_items<'a>(
    history_items: impl IntoIterator<Item = &'a FoundPath>,
    currently_opened: Option<&'a FoundPath>,
    worktree_name_by_id: Option<HashMap<WorktreeId, Arc<RelPath>>>,
    query: &FileSearchQuery,
    path_style: PathStyle,
) -> HashMap<ProjectPath, Match> {
    let mut candidates_paths = HashMap::default();

    let history_items_by_worktrees = history_items
        .into_iter()
        .chain(currently_opened)
        .map(|found_path| {
            // Only match history items names, otherwise their paths may match too many queries,
            // producing false positives. E.g. `foo` would match both `something/foo/bar.rs` and
            // `something/foo/foo.rs` and if the former is a history item, it would be shown first
            // always, despite the latter being a better match.
            let candidate = PathMatchCandidate::new(
                &found_path.project.path,
                false,
                worktree_name_by_id
                    .as_ref()
                    .and_then(|m| m.get(&found_path.project.worktree_id))
                    .map(|prefix| prefix.as_ref()),
            );
            candidates_paths.insert(&found_path.project, found_path);
            (found_path.project.worktree_id, candidate)
        })
        .fold(
            HashMap::default(),
            |mut candidates, (worktree_id, new_candidate)| {
                candidates
                    .entry(worktree_id)
                    .or_insert_with(Vec::new)
                    .push(new_candidate);
                candidates
            },
        );
    let mut matching_history_paths = HashMap::default();
    for (worktree, candidates) in history_items_by_worktrees {
        let max_results = candidates.len() + 1;
        let worktree_root_name = worktree_name_by_id
            .as_ref()
            .and_then(|w| w.get(&worktree).cloned());

        matching_history_paths.extend(
            fuzzy_nucleo::match_fixed_path_set(
                candidates,
                worktree.to_usize(),
                worktree_root_name,
                query.path_query(),
                fuzzy_nucleo::Case::Ignore,
                max_results,
                path_style,
            )
            .into_iter()
            // filter matches where at least one matched position is in filename portion, to prevent directory matches, nucleo scores them higher as history items are matched against their full path
            .filter(|path_match| {
                if let Some(filename) = path_match.path.file_name() {
                    let filename_start = path_match.path.as_unix_str().len() - filename.len();
                    path_match
                        .positions
                        .iter()
                        .any(|&pos| pos >= filename_start)
                } else {
                    true
                }
            })
            .filter_map(|path_match| {
                let worktree_id = WorktreeId::from_usize(path_match.worktree_id);
                let project_path = ProjectPath {
                    worktree_id,
                    path: Arc::clone(&path_match.path),
                };
                // For single-file worktrees, fuzzy_nucleo moves the worktree root name
                // into path_match.path (root_is_file handling), so the stored key of ""
                // won't match. Fall back to an empty-path lookup for those entries.
                let (_, found_path) =
                    candidates_paths.remove_entry(&project_path).or_else(|| {
                        candidates_paths.remove_entry(&ProjectPath {
                            worktree_id,
                            path: RelPath::empty_arc(),
                        })
                    })?;
                // Key with path_match.path so the deduplication check in push_new_matches
                // (which also uses path_match.path) correctly suppresses the search duplicate.
                Some((
                    project_path,
                    Match::History {
                        path: found_path.clone(),
                        panel_match: Some(ProjectPanelOrdMatch(path_match)),
                    },
                ))
            }),
        );
    }
    matching_history_paths
}

fn should_hide_root_in_entry_path(worktree_store: &Entity<WorktreeStore>, cx: &App) -> bool {
    let multiple_worktrees = worktree_store
        .read(cx)
        .visible_worktrees(cx)
        .filter(|worktree| !worktree.read(cx).is_single_file())
        .nth(1)
        .is_some();
    let hide_root = cx
        .global::<SettingsStore>()
        .merged_settings()
        .project_panel
        .as_ref()
        .and_then(|project_panel| project_panel.hide_root)
        .unwrap_or(false);
    hide_root && !multiple_worktrees
}

fn worktree_names_for_history_matching(
    worktree_store: &Entity<WorktreeStore>,
    cx: &App,
) -> Option<HashMap<WorktreeId, Arc<RelPath>>> {
    let hide_root = should_hide_root_in_entry_path(worktree_store, cx);
    let names = worktree_store
        .read(cx)
        .worktrees()
        .filter_map(|worktree| {
            let worktree = worktree.read(cx);
            if hide_root && !worktree.is_single_file() {
                None
            } else {
                Some((worktree.id(), worktree.root_name().into()))
            }
        })
        .collect::<HashMap<_, _>>();

    if names.is_empty() { None } else { Some(names) }
}

fn project_path_for_search_match(
    project: &Entity<Project>,
    path_match: &PathMatch,
    cx: &App,
) -> ProjectPath {
    let worktree_id = WorktreeId::from_usize(path_match.worktree_id);
    let path = if project
        .read(cx)
        .worktree_for_id(worktree_id, cx)
        .is_some_and(|worktree| worktree.read(cx).is_single_file())
    {
        RelPath::empty_arc()
    } else {
        path_match.path.clone()
    };

    ProjectPath { worktree_id, path }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct FoundPath {
    project: ProjectPath,
    absolute: PathBuf,
}

impl FoundPath {
    fn new(project: ProjectPath, absolute: PathBuf) -> Self {
        Self { project, absolute }
    }

    fn is_file(&self, project: &Project, cx: &App) -> Task<bool> {
        if let Some(entry) = project.entry_for_path(&self.project, cx) {
            return Task::ready(entry.is_file());
        }
        if project.is_local() {
            let fs = project.fs().clone();
            let path = self.absolute.clone();
            cx.background_spawn(async move { fs.is_file(&path).await })
        } else if project.is_via_remote_server() {
            let Some(path) = self.absolute.to_str() else {
                return Task::ready(false);
            };
            let resolve_path = project.resolve_abs_path(path, cx);
            cx.background_spawn(
                async move { resolve_path.await.is_some_and(|path| path.is_file()) },
            )
        } else {
            Task::ready(true)
        }
    }
}

const SEARCH_DEBOUNCE: Duration = Duration::from_millis(100);
const WORKTREE_UPDATE_REFRESH_DEBOUNCE: Duration = Duration::from_millis(200);

pub enum Event {
    Selected(ProjectPath),
    Dismissed,
}

#[derive(Debug, Clone)]
struct FileSearchQuery {
    raw_query: String,
    file_query_end: Option<usize>,
    path_position: PathWithPosition,
    line_range: Option<RangeInclusive<u32>>,
}

impl FileSearchQuery {
    fn path_query(&self) -> &str {
        match self.file_query_end {
            Some(file_path_end) => &self.raw_query[..file_path_end],
            None => &self.raw_query,
        }
    }

    fn selection_range(&self, buffer_snapshot: &BufferSnapshot) -> Option<Range<Point>> {
        if let Some(line_range) = self.line_range.clone() {
            return Some(buffer_range_for_line_range(buffer_snapshot, line_range));
        }

        let row = self.path_position.row.map(|row| row.saturating_sub(1))?;
        let col = self.path_position.column.unwrap_or(0).saturating_sub(1);
        let point = buffer_snapshot.point_from_external_input(row, col);
        Some(point..point)
    }
}

fn parse_file_search_query(raw_query: &str) -> FileSearchQuery {
    let raw_query = raw_query.trim().trim_end_matches(':').to_owned();

    if let Some((path_query, start_line, end_line)) = parse_line_range_query(&raw_query) {
        let path_query = path_query.to_owned();
        return FileSearchQuery {
            raw_query,
            file_query_end: Some(path_query.len()),
            path_position: PathWithPosition {
                path: PathBuf::from(&path_query),
                row: Some(start_line),
                column: None,
            },
            line_range: end_line.map(|end| start_line..=end),
        };
    }

    let path_position = PathWithPosition::parse_str(&raw_query);
    let path_str = path_position.path.to_str();
    let path_trimmed = path_str.unwrap_or(&raw_query).trim_end_matches(':');
    let file_query_end = if path_trimmed == raw_query {
        None
    } else {
        path_str.map(str::len)
    };

    FileSearchQuery {
        raw_query,
        file_query_end,
        path_position,
        line_range: None,
    }
}

fn parse_line_range_query(raw_query: &str) -> Option<(&str, u32, Option<u32>)> {
    let (path_query, line_range) = raw_query.rsplit_once(':')?;
    if path_query.is_empty() {
        return None;
    }

    let (start_line, end_line) = line_range.split_once('-')?;
    let start_line = start_line.parse::<u32>().ok()?;
    if start_line == 0 {
        return None;
    }

    let end_line = end_line
        .parse::<u32>()
        .ok()
        .filter(|&end| end != 0 && end >= start_line);

    Some((path_query, start_line, end_line))
}

fn buffer_range_for_line_range(
    buffer_snapshot: &BufferSnapshot,
    line_range: RangeInclusive<u32>,
) -> Range<Point> {
    let max_point = buffer_snapshot.max_point();
    let start_line = line_range.start().saturating_sub(1);
    let start_point = if start_line > max_point.row {
        max_point
    } else {
        Point::new(start_line, 0)
    };
    let end_line = *line_range.end();
    let end_point = if end_line > max_point.row {
        max_point
    } else {
        Point::new(end_line, 0)
    };

    start_point..end_point
}

impl FileFinderDelegate {
    fn new(
        file_finder: WeakEntity<FileFinder>,
        workspace: WeakEntity<Workspace>,
        project: Entity<Project>,
        currently_opened_path: Option<FoundPath>,
        history_items: Vec<FoundPath>,
        separate_history: bool,
        include_ignored: Option<bool>,
        window: &mut Window,
        cx: &mut Context<FileFinder>,
    ) -> Self {
        Self::subscribe_to_updates(&project, window, cx);
        let channel_store = if FileFinderSettings::get_global(cx).include_channels {
            ChannelStore::try_global(cx)
        } else {
            None
        };
        Self {
            file_finder,
            workspace,
            project,
            channel_store,
            search_count: 0,
            latest_search_id: 0,
            latest_search_did_cancel: false,
            latest_search_query: None,
            currently_opened_path,
            matches: Matches::default(),
            selected_matches: Vec::new(),
            has_changed_selected_index: false,
            selected_index: 0,
            cancel_flag: Arc::new(AtomicBool::new(false)),
            search_in_flight: Arc::new(AtomicBool::new(false)),
            history_items,
            history_refresh: Task::ready(()),
            separate_history,
            first_update: true,
            focus_handle: cx.focus_handle(),
            include_ignored: include_ignored.or(FileFinderSettings::get_global(cx).include_ignored),
            include_ignored_refresh: Task::ready(()),
            debounce_next_refresh: false,
        }
    }

    fn subscribe_to_updates(
        project: &Entity<Project>,
        window: &mut Window,
        cx: &mut Context<FileFinder>,
    ) {
        cx.subscribe_in(project, window, |file_finder, _, event, window, cx| {
            match event {
                project::Event::WorktreeUpdatedEntries(_, _) => {
                    file_finder.picker.update(cx, |picker, cx| {
                        picker.delegate.refresh_history(window, cx);
                        picker.delegate.debounce_next_refresh = true;
                        picker.refresh(window, cx);
                    })
                }
                project::Event::WorktreeAdded(_) | project::Event::WorktreeRemoved(_) => {
                    file_finder
                        .picker
                        .update(cx, |picker, cx| picker.refresh(window, cx))
                }
                _ => {}
            };
        })
        .detach();
    }

    fn refresh_history(&mut self, window: &mut Window, cx: &mut Context<Picker<Self>>) {
        let project = self.project.read(cx);
        let paths = self
            .history_items
            .iter()
            .chain(self.currently_opened_path.iter())
            .cloned()
            .chain(
                self.selected_matches
                    .iter()
                    .filter_map(|entry| match &entry.0 {
                        Match::History { path, .. } => Some(path.clone()),
                        Match::Search(path_match) => {
                            let path =
                                project_path_for_search_match(&self.project, &path_match.0, cx);
                            let absolute = project.absolute_path(&path, cx)?;
                            Some(FoundPath::new(path, absolute))
                        }
                        Match::Channel { .. } | Match::CreateNew(_) => None,
                    }),
            )
            .filter(|path| {
                project
                    .entry_for_path(&path.project, cx)
                    .is_none_or(|entry| !entry.is_file())
            })
            .map(|path| (path.project.clone(), path))
            .collect::<HashMap<_, _>>();
        if paths.is_empty() {
            self.history_refresh = Task::ready(());
            return;
        }
        let checks = paths
            .into_values()
            .map(|path| {
                let is_file = path.is_file(project, cx);
                async move { (!is_file.await).then_some(path.project) }
            })
            .collect::<Vec<_>>();
        self.history_refresh = cx.spawn_in(window, async move |picker, cx| {
            let removed_paths = join_all(checks)
                .await
                .into_iter()
                .flatten()
                .collect::<HashSet<_>>();
            if removed_paths.is_empty() {
                return;
            }
            picker
                .update_in(cx, |picker, window, cx| {
                    picker.delegate.remove_invalid_paths(&removed_paths, cx);
                    picker.delegate.debounce_next_refresh = true;
                    picker.refresh(window, cx);
                })
                .ok();
        });
    }

    fn remove_invalid_paths(&mut self, removed_paths: &HashSet<ProjectPath>, cx: &App) {
        let retain_path = |path: &ProjectPath| !removed_paths.contains(path);
        self.history_items.retain(|path| retain_path(&path.project));
        if self
            .currently_opened_path
            .as_ref()
            .is_some_and(|path| !retain_path(&path.project))
        {
            self.currently_opened_path = None;
        }

        let project = &self.project;
        let retain_match = |entry: &Match| match entry {
            Match::History { path, .. } => retain_path(&path.project),
            Match::Search(path_match) => {
                retain_path(&project_path_for_search_match(project, &path_match.0, cx))
            }
            Match::Channel { .. } | Match::CreateNew(_) => true,
        };
        self.selected_matches.retain(|entry| retain_match(&entry.0));

        let mut index = 0;
        let mut selected_index = self.selected_index;
        self.matches.matches.retain(|entry| {
            let retain = retain_match(entry);
            if !retain && index < self.selected_index {
                selected_index -= 1;
            }
            index += 1;
            retain
        });
        self.selected_index = selected_index.min(self.matches.len().saturating_sub(1));
    }

    fn prepend_selected_matches(&mut self) {
        if self.selected_matches.is_empty() {
            return;
        }
        self.matches
            .matches
            .retain(|m| !self.selected_matches.iter().any(|selected| selected == m));
        let mut new: Vec<Match> = self
            .selected_matches
            .iter()
            .map(|selected| selected.0.clone())
            .collect();
        new.append(&mut self.matches.matches);
        self.matches.matches = new;
    }

    fn spawn_search(
        &mut self,
        query: FileSearchQuery,
        window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Task<()> {
        let relative_to = self
            .currently_opened_path
            .as_ref()
            .map(|found_path| Arc::clone(&found_path.project.path));
        let worktree_store = self.project.read(cx).worktree_store();
        let worktrees = worktree_store
            .read(cx)
            .visible_worktrees_and_single_files(cx)
            .collect::<Vec<_>>();
        let include_root_name = !should_hide_root_in_entry_path(&worktree_store, cx);
        let candidate_sets = worktrees
            .into_iter()
            .map(|worktree| {
                let worktree = worktree.read(cx);
                PathMatchCandidateSet {
                    snapshot: worktree.snapshot(),
                    include_ignored: self.include_ignored.unwrap_or_else(|| {
                        worktree.root_entry().is_some_and(|entry| entry.is_ignored)
                    }),
                    include_root_name,
                    candidates: project::Candidates::Files,
                }
            })
            .collect::<Vec<_>>();

        let search_id = util::post_inc(&mut self.search_count);
        self.cancel_flag.store(true, atomic::Ordering::Release);
        self.cancel_flag = Arc::new(AtomicBool::new(false));
        let cancel_flag = self.cancel_flag.clone();
        cx.spawn_in(window, async move |picker, cx| {
            let matches = fuzzy_nucleo::match_path_sets(
                candidate_sets.as_slice(),
                query.path_query(),
                &relative_to,
                fuzzy_nucleo::Case::Ignore,
                100,
                &cancel_flag,
                cx.background_executor().clone(),
            )
            .await
            .into_iter()
            .map(ProjectPanelOrdMatch);
            let did_cancel = cancel_flag.load(atomic::Ordering::Acquire);
            picker
                .update(cx, |picker, cx| {
                    picker
                        .delegate
                        .set_search_matches(search_id, did_cancel, query, matches, cx)
                })
                .log_err();
        })
    }

    fn set_search_matches(
        &mut self,
        search_id: usize,
        did_cancel: bool,
        query: FileSearchQuery,
        matches: impl IntoIterator<Item = ProjectPanelOrdMatch>,
        cx: &mut Context<Picker<Self>>,
    ) {
        if search_id >= self.latest_search_id {
            self.latest_search_id = search_id;
            let path_query_changed = Some(query.path_query())
                != self
                    .latest_search_query
                    .as_ref()
                    .map(|query| query.path_query());
            let extend_old_matches = self.latest_search_did_cancel && !path_query_changed;

            // The line/column suffix doesn't affect which files match, but it
            // does change the user's intent ("go inside this file" vs. "pick a
            // file"). Drop the preserved selection when it appears or changes
            // so the new intent can be honored by `calculate_selected_index`.
            let position_changed = self
                .latest_search_query
                .as_ref()
                .and_then(|q| q.path_position.row)
                != query.path_position.row;
            let selected_match = if path_query_changed || position_changed {
                None
            } else {
                self.matches.get(self.selected_index).cloned()
            };

            let path_style = self.project.read(cx).path_style(cx);
            self.matches.push_new_matches(
                self.project.read(cx).worktree_store(),
                cx,
                &self.history_items,
                self.currently_opened_path.as_ref(),
                Some(&query),
                matches.into_iter(),
                extend_old_matches,
                path_style,
            );

            // Add channel matches
            if let Some(channel_store) = &self.channel_store {
                let channel_store = channel_store.read(cx);
                let channels: Vec<_> = channel_store.channels().cloned().collect();
                if !channels.is_empty() {
                    let candidates = channels
                        .iter()
                        .enumerate()
                        .map(|(id, channel)| StringMatchCandidate::new(id, &channel.name));
                    let channel_query = query.path_query();
                    let query_lower = channel_query.to_lowercase();
                    let mut channel_matches = Vec::new();
                    for candidate in candidates {
                        let channel_name = candidate.string;
                        let name_lower = channel_name.to_lowercase();

                        let mut positions = Vec::new();
                        let mut query_idx = 0;
                        for (name_idx, name_char) in name_lower.char_indices() {
                            if query_idx < query_lower.len() {
                                let query_char =
                                    query_lower[query_idx..].chars().next().unwrap_or_default();
                                if name_char == query_char {
                                    positions.push(name_idx);
                                    query_idx += query_char.len_utf8();
                                }
                            }
                        }

                        if query_idx == query_lower.len() {
                            let channel = &channels[candidate.id];
                            let score = if name_lower == query_lower {
                                1.0
                            } else if name_lower.starts_with(&query_lower) {
                                0.8
                            } else {
                                0.5 * (query_lower.len() as f64 / name_lower.len() as f64)
                            };
                            channel_matches.push(Match::Channel {
                                channel_id: channel.id,
                                channel_name: channel.name.clone(),
                                string_match: StringMatch {
                                    candidate_id: candidate.id,
                                    score,
                                    positions,
                                    string: channel_name,
                                },
                            });
                        }
                    }
                    for channel_match in channel_matches {
                        match self
                            .matches
                            .position(&channel_match, self.currently_opened_path.as_ref())
                        {
                            Ok(_duplicate) => {}
                            Err(ix) => self.matches.matches.insert(ix, channel_match),
                        }
                    }
                }
            }

            let query_path = query.path_query();
            if let Ok(mut query_path) = RelPath::new(Path::new(query_path), path_style) {
                let available_worktree = self
                    .project
                    .read(cx)
                    .visible_worktrees(cx)
                    .filter(|worktree| !worktree.read(cx).is_single_file())
                    .collect::<Vec<_>>();
                let worktree_count = available_worktree.len();
                let mut expect_worktree = available_worktree.first().cloned();
                for worktree in &available_worktree {
                    let worktree_root = worktree.read(cx).root_name();
                    if worktree_count > 1 {
                        if let Ok(suffix) = query_path.strip_prefix(worktree_root) {
                            query_path = Cow::Owned(suffix.to_owned());
                            expect_worktree = Some(worktree.clone());
                            break;
                        }
                    }
                }

                if let Some(FoundPath { ref project, .. }) = self.currently_opened_path {
                    let worktree_id = project.worktree_id;
                    let focused_file_in_available_worktree = available_worktree
                        .iter()
                        .any(|wt| wt.read(cx).id() == worktree_id);

                    if focused_file_in_available_worktree {
                        expect_worktree = self.project.read(cx).worktree_for_id(worktree_id, cx);
                    }
                }

                if let Some(worktree) = expect_worktree {
                    let worktree = worktree.read(cx);
                    if worktree.entry_for_path(&query_path).is_none()
                        && !query.path_query().ends_with('/')
                        && !(path_style.is_windows() && query.path_query().ends_with('\\'))
                    {
                        self.matches.matches.push(Match::CreateNew(ProjectPath {
                            worktree_id: worktree.id(),
                            path: query_path.into_arc(),
                        }));
                    }
                }
            }

            self.prepend_selected_matches();

            self.selected_index = if !self.selected_matches.is_empty() {
                0
            } else {
                let query_has_position = query.path_position.row.is_some();
                selected_match.map_or_else(
                    || self.calculate_selected_index(query_has_position, cx),
                    |m| {
                        self.matches
                            .position(&m, self.currently_opened_path.as_ref())
                            .unwrap_or(0)
                    },
                )
            };

            self.latest_search_query = Some(query);
            self.latest_search_did_cancel = did_cancel;

            cx.notify();
        }
    }

    fn labels_for_match(
        &self,
        path_match: &Match,
        window: &mut Window,
        cx: &App,
    ) -> (HighlightedLabel, HighlightedLabel) {
        let path_style = self.project.read(cx).path_style(cx);
        let (file_name, file_name_positions, mut full_path, mut full_path_positions) =
            match &path_match {
                Match::History {
                    path: entry_path,
                    panel_match,
                } => {
                    let worktree_id = entry_path.project.worktree_id;
                    let worktree = self
                        .project
                        .read(cx)
                        .worktree_for_id(worktree_id, cx)
                        .filter(|worktree| worktree.read(cx).is_visible());

                    if let Some(panel_match) = panel_match {
                        self.labels_for_path_match(&panel_match.0, path_style)
                    } else if let Some(worktree) = worktree {
                        let worktree_store = self.project.read(cx).worktree_store();
                        let full_path = if should_hide_root_in_entry_path(&worktree_store, cx) {
                            entry_path.project.path.clone()
                        } else {
                            worktree
                                .read(cx)
                                .root_name()
                                .join(&entry_path.project.path)
                                .into()
                        };
                        let mut components = full_path.components();
                        let filename = components.next_back().unwrap_or("");
                        let prefix = components.rest();
                        (
                            filename.to_string(),
                            Vec::new(),
                            prefix.display(path_style).to_string() + path_style.primary_separator(),
                            Vec::new(),
                        )
                    } else {
                        (
                            entry_path
                                .absolute
                                .file_name()
                                .map_or(String::new(), |f| f.to_string_lossy().into_owned()),
                            Vec::new(),
                            entry_path.absolute.parent().map_or(String::new(), |path| {
                                path.to_string_lossy().into_owned() + path_style.primary_separator()
                            }),
                            Vec::new(),
                        )
                    }
                }
                Match::Search(path_match) => self.labels_for_path_match(&path_match.0, path_style),
                Match::Channel {
                    channel_name,
                    string_match,
                    ..
                } => (
                    channel_name.to_string(),
                    string_match.positions.clone(),
                    "Channel Notes".to_string(),
                    vec![],
                ),
                Match::CreateNew(project_path) => (
                    format!("Create File: {}", project_path.path.display(path_style)),
                    vec![],
                    String::from(""),
                    vec![],
                ),
            };

        if file_name_positions.is_empty() {
            let user_home_path = util::paths::home_dir().to_string_lossy();
            if !user_home_path.is_empty() && full_path.starts_with(&*user_home_path) {
                full_path.replace_range(0..user_home_path.len(), "~");
                full_path_positions.retain_mut(|pos| {
                    if *pos >= user_home_path.len() {
                        *pos -= user_home_path.len();
                        *pos += 1;
                        true
                    } else {
                        false
                    }
                })
            }
        }

        if full_path.is_ascii() {
            let file_finder_settings = FileFinderSettings::get_global(cx);
            let max_width =
                FileFinder::modal_max_width(file_finder_settings.modal_max_width, window);
            let (normal_em, small_em) = {
                let style = window.text_style();
                let font_id = window.text_system().resolve_font(&style.font());
                let font_size = TextSize::Default.rems(cx).to_pixels(window.rem_size());
                let normal = cx
                    .text_system()
                    .em_width(font_id, font_size)
                    .unwrap_or(px(16.));
                let font_size = TextSize::Small.rems(cx).to_pixels(window.rem_size());
                let small = cx
                    .text_system()
                    .em_width(font_id, font_size)
                    .unwrap_or(px(10.));
                (normal, small)
            };
            let budget = full_path_budget(&file_name, normal_em, small_em, max_width);
            // If the computed budget is zero, we certainly won't be able to achieve it,
            // so no point trying to elide the path.
            if budget > 0 && full_path.len() > budget {
                let components = PathComponentSlice::new(&full_path);
                if let Some(elided_range) =
                    components.elision_range(budget - 1, &full_path_positions)
                {
                    let elided_len = elided_range.end - elided_range.start;
                    let placeholder = "…";
                    full_path_positions.retain_mut(|mat| {
                        if *mat >= elided_range.end {
                            *mat -= elided_len;
                            *mat += placeholder.len();
                        } else if *mat >= elided_range.start {
                            return false;
                        }
                        true
                    });
                    full_path.replace_range(elided_range, placeholder);
                }
            }
        }

        (
            HighlightedLabel::new(file_name, file_name_positions).single_line(),
            HighlightedLabel::new(full_path, full_path_positions)
                .single_line()
                .size(LabelSize::Small)
                .color(Color::Muted),
        )
    }

    fn labels_for_path_match(
        &self,
        path_match: &PathMatch,
        path_style: PathStyle,
    ) -> (String, Vec<usize>, String, Vec<usize>) {
        let full_path = path_match.path_prefix.join(&path_match.path);
        let mut path_positions = path_match.positions.clone();

        let file_name = full_path.file_name().unwrap_or("");
        let file_name_start = full_path.as_unix_str().len() - file_name.len();
        let file_name_positions = path_positions
            .iter()
            .filter_map(|pos| {
                if pos >= &file_name_start {
                    Some(pos - file_name_start)
                } else {
                    None
                }
            })
            .collect::<Vec<_>>();

        let full_path = full_path
            .display(path_style)
            .trim_end_matches(&file_name)
            .to_string();
        path_positions.retain(|idx| *idx < full_path.len());

        debug_assert!(
            file_name_positions
                .iter()
                .all(|ix| file_name[*ix..].chars().next().is_some()),
            "invalid file name positions {file_name:?} {file_name_positions:?}"
        );
        debug_assert!(
            path_positions
                .iter()
                .all(|ix| full_path[*ix..].chars().next().is_some()),
            "invalid path positions {full_path:?} {path_positions:?}"
        );

        (
            file_name.to_string(),
            file_name_positions,
            full_path,
            path_positions,
        )
    }

    /// Attempts to resolve an absolute file path and update the search matches if found.
    ///
    /// If the query path resolves to an absolute file that exists in the project,
    /// this method will find the corresponding worktree and relative path, create a
    /// match for it, and update the picker's search results.
    ///
    /// Returns `true` if the absolute path exists, otherwise returns `false`.
    fn lookup_absolute_path(
        &self,
        query: FileSearchQuery,
        window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Task<bool> {
        cx.spawn_in(window, async move |picker, cx| {
            let Some(project) = picker
                .read_with(cx, |picker, _| picker.delegate.project.clone())
                .log_err()
            else {
                return false;
            };

            let query_path = Path::new(query.path_query());
            let mut path_matches = Vec::new();

            let abs_file_exists = project
                .update(cx, |this, cx| {
                    this.resolve_abs_file_path(query.path_query(), cx)
                })
                .await
                .is_some();

            if abs_file_exists {
                project.update(cx, |project, cx| {
                    if let Some((worktree, relative_path)) = project.find_worktree(query_path, cx) {
                        path_matches.push(ProjectPanelOrdMatch(PathMatch {
                            score: 1.0,
                            positions: Vec::new(),
                            worktree_id: worktree.read(cx).id().to_usize(),
                            path: relative_path,
                            path_prefix: RelPath::empty_arc(),
                            is_dir: false, // File finder doesn't support directories
                            distance_to_relative_ancestor: usize::MAX,
                        }));
                    }
                });
            }

            picker
                .update_in(cx, |picker, _, cx| {
                    let picker_delegate = &mut picker.delegate;
                    let search_id = util::post_inc(&mut picker_delegate.search_count);
                    picker_delegate.set_search_matches(search_id, false, query, path_matches, cx);

                    anyhow::Ok(())
                })
                .log_err();
            abs_file_exists
        })
    }

    /// Skips first history match (that is displayed topmost) if it's currently opened.
    ///
    /// When the query carries a row (e.g. `foo.rs:42`), the user is asking to
    /// navigate inside a specific file, so we never skip past the active match
    /// even if the setting is enabled.
    fn calculate_selected_index(
        &self,
        query_has_position: bool,
        cx: &mut Context<Picker<Self>>,
    ) -> usize {
        if !query_has_position
            && FileFinderSettings::get_global(cx).skip_focus_for_active_in_search
            && let Some(Match::History { path, .. }) = self.matches.get(0)
            && Some(path) == self.currently_opened_path.as_ref()
        {
            let elements_after_first = self.matches.len() - 1;
            if elements_after_first > 0 {
                return 1;
            }
        }

        0
    }

    fn key_context(&self, _window: &Window, cx: &App) -> KeyContext {
        let mut key_context = KeyContext::new_with_defaults(cx.platform_info().os);
        key_context.add("FileFinder");
        key_context
    }

    /// Shared file-opening logic for both `confirm` and `confirm_without_dismiss`.
    ///
    /// When `dismiss_after_open` is true this behaves like a normal confirm: the file is focused
    /// and the finder is dismissed.  When false the finder stays open so the user can continue
    /// opening more files.
    fn open_selected_file(
        &mut self,
        secondary: bool,
        dismiss_after_open: bool,
        window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        let Some(m) = self.matches.get(self.selected_index()).cloned() else {
            return;
        };
        let allow_preview = PreviewTabsSettings::get_global(cx).enable_preview_from_file_finder;
        self.open_match(m, secondary, dismiss_after_open, allow_preview, window, cx);
    }

    /// `allow_preview` is forced off for batch opens so every file gets its
    /// own tab instead of consecutive opens reusing one preview tab.
    fn open_match(
        &mut self,
        m: Match,
        secondary: bool,
        dismiss_after_open: bool,
        allow_preview: bool,
        window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        let Some(workspace) = self.workspace.upgrade() else {
            return;
        };

        // Channel matches always dismiss the finder.
        if let Match::Channel { channel_id, .. } = &m {
            let channel_id = channel_id.0;
            let finder = self.file_finder.clone();
            window.dispatch_action(OpenChannelNotesById { channel_id }.boxed_clone(), cx);
            finder.update(cx, |_, cx| cx.emit(DismissEvent)).log_err();
            return;
        }

        // Focus the new item only when dismissing — this avoids stealing focus from the modal.
        // Always activate (make the tab current) so every opened file is visually reflected.
        let focus_item = dismiss_after_open;

        let open_task = workspace.update(cx, |workspace, cx| {
            let split_or_open = |workspace: &mut Workspace,
                                 project_path,
                                 window: &mut Window,
                                 cx: &mut Context<Workspace>| {
                if secondary {
                    workspace.split_path_preview(project_path, allow_preview, None, window, cx)
                } else {
                    workspace.open_path_preview(
                        project_path,
                        None,
                        focus_item,
                        allow_preview,
                        true,
                        window,
                        cx,
                    )
                }
            };

            match &m {
                Match::CreateNew(project_path) => {
                    if secondary {
                        workspace.split_path_preview(project_path.clone(), false, None, window, cx)
                    } else {
                        workspace.open_path_preview(
                            project_path.clone(),
                            None,
                            focus_item,
                            false,
                            true,
                            window,
                            cx,
                        )
                    }
                }
                Match::History { path, .. } => {
                    let worktree_id = path.project.worktree_id;
                    if workspace
                        .project()
                        .read(cx)
                        .worktree_for_id(worktree_id, cx)
                        .is_some()
                    {
                        split_or_open(
                            workspace,
                            ProjectPath {
                                worktree_id,
                                path: Arc::clone(&path.project.path),
                            },
                            window,
                            cx,
                        )
                    } else if secondary {
                        workspace.split_abs_path(path.absolute.clone(), false, window, cx)
                    } else {
                        workspace.open_abs_path(
                            path.absolute.clone(),
                            OpenOptions {
                                visible: Some(OpenVisible::None),
                                ..Default::default()
                            },
                            window,
                            cx,
                        )
                    }
                }
                Match::Search(path_match) => {
                    let project_path =
                        project_path_for_search_match(workspace.project(), &path_match.0, cx);
                    split_or_open(workspace, project_path, window, cx)
                }
                Match::Channel { .. } => unreachable!("handled above"),
            }
        });

        let selection_query = self.latest_search_query.clone();
        let finder = self.file_finder.clone();
        let workspace = self.workspace.clone();

        cx.spawn_in(window, async move |_, mut cx| {
            let item = open_task
                .await
                .notify_workspace_async_err(workspace, &mut cx)?;
            if let Some(active_editor) = item.downcast::<Editor>() {
                active_editor
                    .downgrade()
                    .update_in(cx, |editor, window, cx| {
                        let Some(buffer) = editor.buffer().read(cx).as_singleton() else {
                            return;
                        };
                        let buffer_snapshot = buffer.read(cx).snapshot();
                        let Some(selection_query) = selection_query.as_ref() else {
                            return;
                        };
                        let Some(selection_range) =
                            selection_query.selection_range(&buffer_snapshot)
                        else {
                            return;
                        };
                        editor.go_to_singleton_buffer_range(selection_range, window, cx);
                    })
                    .log_err();
            }
            if dismiss_after_open {
                finder.update(cx, |_, cx| cx.emit(DismissEvent)).ok()?;
            }
            Some(())
        })
        .detach();
    }

    fn confirm_without_dismiss(
        &mut self,
        window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        self.open_selected_file(false, false, window, cx);
    }

    /// Opens every multi-selected file as a tab in a single new split, then
    /// dismisses the finder.
    fn open_selected_in_one_split(
        &mut self,
        split_direction: SplitDirection,
        window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        let Some(workspace) = self.workspace.upgrade() else {
            return;
        };
        let selected = std::mem::take(&mut self.selected_matches);
        let paths: Vec<ProjectPath> = selected
            .iter()
            .filter_map(|selected| match &selected.0 {
                Match::History { path, .. } => Some(ProjectPath {
                    worktree_id: path.project.worktree_id,
                    path: Arc::clone(&path.project.path),
                }),
                Match::Search(m) => Some(project_path_for_search_match(&self.project, &m.0, cx)),
                Match::Channel { .. } | Match::CreateNew(_) => None,
            })
            .collect();
        if paths.is_empty() {
            return;
        }
        workspace.update(cx, |workspace, cx| {
            let new_pane =
                workspace.split_pane(workspace.active_pane().clone(), split_direction, window, cx);
            let count = paths.len();
            for (i, path) in paths.into_iter().enumerate() {
                let focus_item = i + 1 == count;
                workspace
                    .open_path_preview(
                        path,
                        Some(new_pane.downgrade()),
                        focus_item,
                        false,
                        true,
                        window,
                        cx,
                    )
                    .detach_and_log_err(cx);
            }
        });
        // Deferred because this runs from a `FileFinder` action handler, so
        // the entity is already being updated.
        let finder = self.file_finder.clone();
        cx.defer(move |cx| {
            finder.update(cx, |_, cx| cx.emit(DismissEvent)).log_err();
        });
    }
}

fn full_path_budget(
    file_name: &str,
    normal_em: Pixels,
    small_em: Pixels,
    max_width: Pixels,
) -> usize {
    (((max_width / 0.8) - file_name.len() * normal_em) / small_em) as usize
}

impl PickerDelegate for FileFinderDelegate {
    type ListItem = ListItem;

    fn name() -> &'static str {
        "file finder"
    }

    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
        "Search project files...".into()
    }

    fn searchbar_trailer(
        &self,
        _window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Option<AnyElement> {
        let focus_handle = self.focus_handle.clone();
        let including_ignored = self.include_ignored == Some(true);
        // Clicking includes ignored files unless they're already included, in
        // which case it excludes them again (see `handle_toggle_ignored`).
        let tooltip_label = if including_ignored {
            "Exclude Ignored Files"
        } else {
            "Include Ignored Files"
        };

        let filter_button = IconButton::new("filter-ignored", IconName::FileIgnored)
            .icon_size(IconSize::Small)
            .toggle_state(including_ignored)
            .tooltip(move |_window, cx| {
                Tooltip::for_action_in(tooltip_label, &ToggleIncludeIgnored, &focus_handle, cx)
            })
            .on_click(|_, window, cx| {
                window.dispatch_action(ToggleIncludeIgnored.boxed_clone(), cx)
            });
        Some(
            h_flex()
                .gap_1()
                .child(filter_button)
                .children(picker::parts::project_scan_indicator(
                    self.latest_search_query.is_some(),
                    &self.project,
                    cx,
                ))
                .into_any_element(),
        )
    }

    fn match_count(&self) -> usize {
        self.matches.len()
    }

    fn selected_index(&self) -> usize {
        self.selected_index
    }

    fn set_hovered_index(&mut self, ix: usize, _: &mut Window, cx: &mut Context<Picker<Self>>) {
        self.selected_index = ix;
        cx.notify();
    }

    fn set_selected_index(&mut self, ix: usize, _: &mut Window, cx: &mut Context<Picker<Self>>) {
        self.has_changed_selected_index = true;
        self.selected_index = ix;
        cx.notify();
    }

    fn separators_after_indices(&self) -> Vec<usize> {
        if self.separate_history {
            let first_non_history_index = self
                .matches
                .matches
                .iter()
                .enumerate()
                .find(|(_, m)| !matches!(m, Match::History { .. }))
                .map(|(i, _)| i);
            if let Some(first_non_history_index) = first_non_history_index
                && first_non_history_index > 0
            {
                return vec![first_non_history_index - 1];
            }
        }
        Vec::new()
    }

    fn update_matches(
        &mut self,
        raw_query: String,
        window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Task<()> {
        let debounce_refresh = mem::take(&mut self.debounce_next_refresh);
        let raw_query = raw_query.trim();

        let raw_query = match &raw_query.get(0..2) {
            Some(".\\" | "./") => &raw_query[2..],
            Some(prefix @ ("a\\" | "a/" | "b\\" | "b/")) => {
                if self
                    .workspace
                    .upgrade()
                    .into_iter()
                    .flat_map(|workspace| workspace.read(cx).worktrees(cx))
                    .all(|worktree| {
                        worktree
                            .read(cx)
                            .entry_for_path(RelPath::from_unix_str(prefix.split_at(1).0).unwrap())
                            .is_none_or(|entry| !entry.is_dir())
                    })
                {
                    &raw_query[2..]
                } else {
                    raw_query
                }
            }
            _ => raw_query,
        };

        if raw_query.is_empty() {
            // if there was no query before, and we already have some (history) matches
            // there's no need to update anything, since nothing has changed.
            // We also want to populate matches set from history entries on the first update.
            if self.latest_search_query.is_some() || self.first_update {
                let project = self.project.read(cx);

                self.latest_search_id = post_inc(&mut self.search_count);
                self.latest_search_query = None;
                self.matches = Matches {
                    separate_history: self.separate_history,
                    ..Matches::default()
                };
                let path_style = self.project.read(cx).path_style(cx);

                self.matches.push_new_matches(
                    project.worktree_store(),
                    cx,
                    self.history_items.iter().filter(|history_item| {
                        project
                            .worktree_for_id(history_item.project.worktree_id, cx)
                            .is_some()
                            || project.is_local()
                            || project.is_via_remote_server()
                    }),
                    self.currently_opened_path.as_ref(),
                    None,
                    None.into_iter(),
                    false,
                    path_style,
                );

                self.first_update = false;
                self.selected_index = 0;
            }
            self.prepend_selected_matches();
            cx.notify();
            self.search_in_flight
                .store(false, atomic::Ordering::Release);
            Task::ready(())
        } else {
            let query = parse_file_search_query(raw_query);
            let path = query.path_position.path.clone();

            let search_in_flight = self.search_in_flight.clone();
            let was_in_flight = search_in_flight.swap(true, atomic::Ordering::Relaxed);

            cx.spawn_in(window, async move |this, cx| {
                if debounce_refresh {
                    cx.background_executor()
                        .timer(WORKTREE_UPDATE_REFRESH_DEBOUNCE)
                        .await;
                } else if was_in_flight {
                    cx.background_executor().timer(SEARCH_DEBOUNCE).await;
                }
                let _ = maybe!(async move {
                    let is_absolute_path = path.is_absolute();
                    let did_resolve_abs_path = is_absolute_path
                        && this
                            .update_in(cx, |this, window, cx| {
                                this.delegate
                                    .lookup_absolute_path(query.clone(), window, cx)
                            })?
                            .await;

                    // Only check for relative paths if no absolute paths were
                    // found.
                    if !did_resolve_abs_path {
                        this.update_in(cx, |this, window, cx| {
                            this.delegate.spawn_search(query, window, cx)
                        })?
                        .await;
                    }
                    anyhow::Ok(())
                })
                .await;
                search_in_flight.store(false, atomic::Ordering::Relaxed);
            })
        }
    }

    fn confirm(
        &mut self,
        secondary: bool,
        window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        self.open_selected_file(secondary, true, window, cx);
    }

    fn supports_multi_select(&self) -> bool {
        true
    }

    fn is_item_selected(&self, ix: usize) -> bool {
        let Some(m) = self.matches.get(ix) else {
            return false;
        };
        self.selected_matches.iter().any(|selected| selected == m)
    }

    fn toggle_item_selected(
        &mut self,
        ix: usize,
        _window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        let Some(m) = self.matches.get(ix).cloned() else {
            return;
        };
        // `new` rejects channels and the `create-new` placeholder.
        let Some(selected) = SelectedMatch::new(m) else {
            return;
        };
        if let Some(position) = self
            .selected_matches
            .iter()
            .position(|existing| *existing == selected)
        {
            self.selected_matches.remove(position);
        } else {
            self.selected_matches.push(selected);
        }
        cx.notify();
    }

    fn selected_item_count(&self) -> usize {
        self.selected_matches.len()
    }

    fn clear_selection(&mut self, _cx: &mut Context<Picker<FileFinderDelegate>>) {
        self.selected_matches.clear();
    }

    fn confirm_multi(
        &mut self,
        secondary: bool,
        window: &mut Window,
        cx: &mut Context<Picker<FileFinderDelegate>>,
    ) {
        let selected = std::mem::take(&mut self.selected_matches);
        let count = selected.len();
        for (i, selected_match) in selected.into_iter().enumerate() {
            let is_last = i + 1 == count;
            self.open_match(selected_match.0, secondary, is_last, false, window, cx);
        }
    }

    fn dismissed(&mut self, _: &mut Window, cx: &mut Context<Picker<FileFinderDelegate>>) {
        self.file_finder
            .update(cx, |_, cx| cx.emit(DismissEvent))
            .ok();
    }

    fn try_get_preview_data_for_match(&self, cx: &App) -> Option<picker::PreviewUpdate> {
        let m = self.matches.get(self.selected_index)?;
        match m {
            Match::CreateNew(project_path) => {
                let path_style = self.project.read(cx).path_style(cx);
                let path_highlight = gpui::HighlightStyle {
                    color: Some(cx.theme().colors().text_accent),
                    ..Default::default()
                };
                let mut message = picker::HighlightedTextBuilder::default();
                message.push_plain("Create file ");
                message.push_styled(project_path.path.display(path_style), path_highlight);
                message.push_plain("?");
                Some(picker::PreviewUpdate::message(message.build()))
            }
            _ => Some(picker::PreviewUpdate::from_path(
                m.abs_path(&self.project, cx)?,
            )),
        }
    }

    fn render_match(
        &self,
        ix: usize,
        selected: bool,
        window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Option<Self::ListItem> {
        self.render_match_impl(ix, selected, None, window, cx)
    }

    fn render_match_with_checkbox(
        &self,
        ix: usize,
        selected: bool,
        checkbox: AnyElement,
        window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Option<Self::ListItem> {
        self.render_match_impl(ix, selected, Some(checkbox), window, cx)
    }

    fn actions_menu(
        &self,
        _window: &mut Window,
        _cx: &mut Context<Picker<Self>>,
    ) -> Vec<picker::PickerAction> {
        let open_label: SharedString = if self.selected_matches.len() > 1 {
            "Open multiple".into()
        } else {
            "Open File".into()
        };
        vec![
            picker::PickerAction::header("Split…"),
            picker::PickerAction::button("Left", pane::SplitLeft::default().boxed_clone()),
            picker::PickerAction::button("Right", pane::SplitRight::default().boxed_clone()),
            picker::PickerAction::button("Up", pane::SplitUp::default().boxed_clone()),
            picker::PickerAction::button("Down", pane::SplitDown::default().boxed_clone()),
            picker::PickerAction::separator(),
            picker::PickerAction::button(open_label, menu::Confirm.boxed_clone()),
        ]
    }
}

impl FileFinderDelegate {
    fn render_match_impl(
        &self,
        ix: usize,
        selected: bool,
        checkbox: Option<AnyElement>,
        window: &mut Window,
        cx: &mut Context<Picker<Self>>,
    ) -> Option<ListItem> {
        let settings = FileFinderSettings::get_global(cx);

        let path_match = self.matches.get(ix)?;

        let (file_name_label, full_path_label) = self.labels_for_match(path_match, window, cx);

        let start_icon = match path_match {
            Match::CreateNew(_) => Some(Icon::new(IconName::Plus).size(IconSize::Small)),
            Match::Channel { .. } => Some(Icon::new(IconName::Hash).color(Color::Muted)),
            _ => maybe!({
                if !settings.file_icons {
                    return None;
                }
                let abs_path = path_match.abs_path(&self.project, cx)?;
                let file_name = abs_path.file_name()?;
                let icon = FileIcons::get_icon(file_name.as_ref(), cx)?;
                Some(Icon::from_path(icon).color(Color::Muted))
            }),
        };

        let checkbox = checkbox.map(|checkbox| {
            if matches!(path_match, Match::CreateNew(_) | Match::Channel { .. }) {
                div()
                    .flex_none()
                    .size(Checkbox::container_size())
                    .into_any_element()
            } else {
                checkbox
            }
        });

        let start_slot: Option<AnyElement> = match (checkbox, start_icon) {
            (Some(checkbox), icon) => Some(
                h_flex()
                    .gap_1p5()
                    .child(checkbox)
                    .children(icon)
                    .into_any_element(),
            ),
            (None, icon) => icon.map(IntoElement::into_any_element),
        };

        let end_slot: Option<AnyElement> = match path_match {
            Match::History { .. } => Some(
                Icon::new(IconName::HistoryRerun)
                    .color(Color::Muted)
                    .size(IconSize::Small)
                    .into_any_element(),
            ),
            Match::Search(_) | Match::Channel { .. } => Some(
                div()
                    .flex_none()
                    .size(IconSize::Small.rems())
                    .into_any_element(),
            ),
            Match::CreateNew(_) => None,
        };

        Some(
            ListItem::new(ix)
                .spacing(ListItemSpacing::Sparse)
                .inset(true)
                .toggle_state(selected)
                .start_slot::<AnyElement>(start_slot)
                .child(
                    h_flex()
                        .w_full()
                        .min_w_0()
                        .gap_1p5()
                        .child(file_name_label.flex_none().truncate_middle())
                        .child(full_path_label.flex_1().truncate_start()),
                )
                .end_slot::<AnyElement>(end_slot),
        )
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct PathComponentSlice<'a> {
    path: Cow<'a, Path>,
    path_str: Cow<'a, str>,
    component_ranges: Vec<(Component<'a>, Range<usize>)>,
}

impl<'a> PathComponentSlice<'a> {
    fn new(path: &'a str) -> Self {
        let trimmed_path = Path::new(path).components().as_path().as_os_str();
        let mut component_ranges = Vec::new();
        let mut components = Path::new(trimmed_path).components();
        let len = trimmed_path.as_encoded_bytes().len();
        let mut pos = 0;
        while let Some(component) = components.next() {
            component_ranges.push((component, pos..0));
            pos = len - components.as_path().as_os_str().as_encoded_bytes().len();
        }
        for ((_, range), ancestor) in component_ranges
            .iter_mut()
            .rev()
            .zip(Path::new(trimmed_path).ancestors())
        {
            range.end = ancestor.as_os_str().as_encoded_bytes().len();
        }
        Self {
            path: Cow::Borrowed(Path::new(path)),
            path_str: Cow::Borrowed(path),
            component_ranges,
        }
    }

    fn elision_range(&self, budget: usize, matches: &[usize]) -> Option<Range<usize>> {
        let eligible_range = {
            assert!(matches.is_sorted());
            let mut matches = matches.iter().copied().peekable();
            let mut longest: Option<Range<usize>> = None;
            let mut cur = 0..0;
            let mut seen_normal = false;
            for (i, (component, range)) in self.component_ranges.iter().enumerate() {
                let is_normal = matches!(component, Component::Normal(_));
                let is_first_normal = is_normal && !seen_normal;
                seen_normal |= is_normal;
                let is_last = i == self.component_ranges.len() - 1;
                let contains_match = matches.peek().is_some_and(|mat| range.contains(mat));
                if contains_match {
                    matches.next();
                }
                if is_first_normal || is_last || !is_normal || contains_match {
                    if longest
                        .as_ref()
                        .is_none_or(|old| old.end - old.start <= cur.end - cur.start)
                    {
                        longest = Some(cur);
                    }
                    cur = i + 1..i + 1;
                } else {
                    cur.end = i + 1;
                }
            }
            if longest
                .as_ref()
                .is_none_or(|old| old.end - old.start <= cur.end - cur.start)
            {
                longest = Some(cur);
            }
            longest
        };

        let eligible_range = eligible_range?;
        assert!(eligible_range.start <= eligible_range.end);
        if eligible_range.is_empty() {
            return None;
        }

        let elided_range: Range<usize> = {
            let byte_range = self.component_ranges[eligible_range.start].1.start
                ..self.component_ranges[eligible_range.end - 1].1.end;
            let midpoint = self.path_str.len() / 2;
            let distance_from_start = byte_range.start.abs_diff(midpoint);
            let distance_from_end = byte_range.end.abs_diff(midpoint);
            let pick_from_end = distance_from_start > distance_from_end;
            let mut len_with_elision = self.path_str.len();
            let mut i = eligible_range.start;
            while i < eligible_range.end {
                let x = if pick_from_end {
                    eligible_range.end - i + eligible_range.start - 1
                } else {
                    i
                };
                len_with_elision -= self.component_ranges[x]
                    .0
                    .as_os_str()
                    .as_encoded_bytes()
                    .len()
                    + 1;
                if len_with_elision <= budget {
                    break;
                }
                i += 1;
            }
            if len_with_elision > budget {
                return None;
            } else if pick_from_end {
                let x = eligible_range.end - i + eligible_range.start - 1;
                x..eligible_range.end
            } else {
                let x = i;
                eligible_range.start..x + 1
            }
        };

        let byte_range = self.component_ranges[elided_range.start].1.start
            ..self.component_ranges[elided_range.end - 1].1.end;
        Some(byte_range)
    }
}