outline_panel.rs

   1mod outline_panel_settings;
   2
   3use std::{
   4    cmp,
   5    collections::BTreeMap,
   6    hash::Hash,
   7    ops::Range,
   8    path::{Path, PathBuf, MAIN_SEPARATOR_STR},
   9    sync::{
  10        atomic::{self, AtomicBool},
  11        Arc, OnceLock,
  12    },
  13    time::Duration,
  14    u32,
  15};
  16
  17use anyhow::Context as _;
  18use collections::{hash_map, BTreeSet, HashMap, HashSet};
  19use db::kvp::KEY_VALUE_STORE;
  20use editor::{
  21    display_map::ToDisplayPoint,
  22    items::{entry_git_aware_label_color, entry_label_color},
  23    scroll::{Autoscroll, AutoscrollStrategy, ScrollAnchor, ScrollbarAutoHide},
  24    AnchorRangeExt, Bias, DisplayPoint, Editor, EditorEvent, EditorMode, EditorSettings, ExcerptId,
  25    ExcerptRange, MultiBufferSnapshot, RangeToAnchorExt, ShowScrollbar,
  26};
  27use file_icons::FileIcons;
  28use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
  29use gpui::{
  30    actions, anchored, deferred, div, point, px, size, uniform_list, Action, AnyElement, App,
  31    AppContext as _, AsyncWindowContext, Bounds, ClipboardItem, Context, DismissEvent, Div,
  32    ElementId, Entity, EventEmitter, FocusHandle, Focusable, HighlightStyle, InteractiveElement,
  33    IntoElement, KeyContext, ListHorizontalSizingBehavior, ListSizingBehavior, MouseButton,
  34    MouseDownEvent, ParentElement, Pixels, Point, Render, ScrollStrategy, SharedString, Stateful,
  35    StatefulInteractiveElement as _, Styled, Subscription, Task, UniformListScrollHandle,
  36    WeakEntity, Window,
  37};
  38use itertools::Itertools;
  39use language::{BufferId, BufferSnapshot, OffsetRangeExt, OutlineItem};
  40use menu::{Cancel, SelectFirst, SelectLast, SelectNext, SelectPrev};
  41
  42use outline_panel_settings::{OutlinePanelDockPosition, OutlinePanelSettings, ShowIndentGuides};
  43use project::{File, Fs, Project, ProjectItem};
  44use search::{BufferSearchBar, ProjectSearchView};
  45use serde::{Deserialize, Serialize};
  46use settings::{Settings, SettingsStore};
  47use smol::channel;
  48use theme::{SyntaxTheme, ThemeSettings};
  49use ui::{DynamicSpacing, IndentGuideColors, IndentGuideLayout};
  50use util::{debug_panic, RangeExt, ResultExt, TryFutureExt};
  51use workspace::{
  52    dock::{DockPosition, Panel, PanelEvent},
  53    item::ItemHandle,
  54    searchable::{SearchEvent, SearchableItem},
  55    ui::{
  56        h_flex, v_flex, ActiveTheme, ButtonCommon, Clickable, Color, ContextMenu, FluentBuilder,
  57        HighlightedLabel, Icon, IconButton, IconButtonShape, IconName, IconSize, Label,
  58        LabelCommon, ListItem, Scrollbar, ScrollbarState, StyledExt, StyledTypography, Toggleable,
  59        Tooltip,
  60    },
  61    OpenInTerminal, WeakItemHandle, Workspace,
  62};
  63use worktree::{Entry, GitEntry, ProjectEntryId, WorktreeId};
  64
  65actions!(
  66    outline_panel,
  67    [
  68        CollapseAllEntries,
  69        CollapseSelectedEntry,
  70        ExpandAllEntries,
  71        ExpandSelectedEntry,
  72        FoldDirectory,
  73        Open,
  74        RevealInFileManager,
  75        SelectParent,
  76        ToggleActiveEditorPin,
  77        ToggleFocus,
  78        UnfoldDirectory,
  79    ]
  80);
  81
  82const OUTLINE_PANEL_KEY: &str = "OutlinePanel";
  83const UPDATE_DEBOUNCE: Duration = Duration::from_millis(50);
  84
  85type Outline = OutlineItem<language::Anchor>;
  86type HighlightStyleData = Arc<OnceLock<Vec<(Range<usize>, HighlightStyle)>>>;
  87
  88pub struct OutlinePanel {
  89    fs: Arc<dyn Fs>,
  90    width: Option<Pixels>,
  91    project: Entity<Project>,
  92    workspace: WeakEntity<Workspace>,
  93    active: bool,
  94    pinned: bool,
  95    scroll_handle: UniformListScrollHandle,
  96    context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
  97    focus_handle: FocusHandle,
  98    pending_serialization: Task<Option<()>>,
  99    fs_entries_depth: HashMap<(WorktreeId, ProjectEntryId), usize>,
 100    fs_entries: Vec<FsEntry>,
 101    fs_children_count: HashMap<WorktreeId, HashMap<Arc<Path>, FsChildren>>,
 102    collapsed_entries: HashSet<CollapsedEntry>,
 103    unfolded_dirs: HashMap<WorktreeId, BTreeSet<ProjectEntryId>>,
 104    selected_entry: SelectedEntry,
 105    active_item: Option<ActiveItem>,
 106    _subscriptions: Vec<Subscription>,
 107    updating_fs_entries: bool,
 108    updating_cached_entries: bool,
 109    new_entries_for_fs_update: HashSet<ExcerptId>,
 110    fs_entries_update_task: Task<()>,
 111    cached_entries_update_task: Task<()>,
 112    reveal_selection_task: Task<anyhow::Result<()>>,
 113    outline_fetch_tasks: HashMap<(BufferId, ExcerptId), Task<()>>,
 114    excerpts: HashMap<BufferId, HashMap<ExcerptId, Excerpt>>,
 115    cached_entries: Vec<CachedEntry>,
 116    filter_editor: Entity<Editor>,
 117    mode: ItemsDisplayMode,
 118    show_scrollbar: bool,
 119    vertical_scrollbar_state: ScrollbarState,
 120    horizontal_scrollbar_state: ScrollbarState,
 121    hide_scrollbar_task: Option<Task<()>>,
 122    max_width_item_index: Option<usize>,
 123    preserve_selection_on_buffer_fold_toggles: HashSet<BufferId>,
 124}
 125
 126#[derive(Debug)]
 127enum ItemsDisplayMode {
 128    Search(SearchState),
 129    Outline,
 130}
 131
 132#[derive(Debug)]
 133struct SearchState {
 134    kind: SearchKind,
 135    query: String,
 136    matches: Vec<(Range<editor::Anchor>, Arc<OnceLock<SearchData>>)>,
 137    highlight_search_match_tx: channel::Sender<HighlightArguments>,
 138    _search_match_highlighter: Task<()>,
 139    _search_match_notify: Task<()>,
 140}
 141
 142struct HighlightArguments {
 143    multi_buffer_snapshot: MultiBufferSnapshot,
 144    match_range: Range<editor::Anchor>,
 145    search_data: Arc<OnceLock<SearchData>>,
 146}
 147
 148impl SearchState {
 149    fn new(
 150        kind: SearchKind,
 151        query: String,
 152        previous_matches: HashMap<Range<editor::Anchor>, Arc<OnceLock<SearchData>>>,
 153        new_matches: Vec<Range<editor::Anchor>>,
 154        theme: Arc<SyntaxTheme>,
 155        window: &mut Window,
 156        cx: &mut Context<OutlinePanel>,
 157    ) -> Self {
 158        let (highlight_search_match_tx, highlight_search_match_rx) = channel::unbounded();
 159        let (notify_tx, notify_rx) = channel::unbounded::<()>();
 160        Self {
 161            kind,
 162            query,
 163            matches: new_matches
 164                .into_iter()
 165                .map(|range| {
 166                    let search_data = previous_matches
 167                        .get(&range)
 168                        .map(Arc::clone)
 169                        .unwrap_or_default();
 170                    (range, search_data)
 171                })
 172                .collect(),
 173            highlight_search_match_tx,
 174            _search_match_highlighter: cx.background_spawn(async move {
 175                while let Ok(highlight_arguments) = highlight_search_match_rx.recv().await {
 176                    let needs_init = highlight_arguments.search_data.get().is_none();
 177                    let search_data = highlight_arguments.search_data.get_or_init(|| {
 178                        SearchData::new(
 179                            &highlight_arguments.match_range,
 180                            &highlight_arguments.multi_buffer_snapshot,
 181                        )
 182                    });
 183                    if needs_init {
 184                        notify_tx.try_send(()).ok();
 185                    }
 186
 187                    let highlight_data = &search_data.highlights_data;
 188                    if highlight_data.get().is_some() {
 189                        continue;
 190                    }
 191                    let mut left_whitespaces_count = 0;
 192                    let mut non_whitespace_symbol_occurred = false;
 193                    let context_offset_range = search_data
 194                        .context_range
 195                        .to_offset(&highlight_arguments.multi_buffer_snapshot);
 196                    let mut offset = context_offset_range.start;
 197                    let mut context_text = String::new();
 198                    let mut highlight_ranges = Vec::new();
 199                    for mut chunk in highlight_arguments
 200                        .multi_buffer_snapshot
 201                        .chunks(context_offset_range.start..context_offset_range.end, true)
 202                    {
 203                        if !non_whitespace_symbol_occurred {
 204                            for c in chunk.text.chars() {
 205                                if c.is_whitespace() {
 206                                    left_whitespaces_count += c.len_utf8();
 207                                } else {
 208                                    non_whitespace_symbol_occurred = true;
 209                                    break;
 210                                }
 211                            }
 212                        }
 213
 214                        if chunk.text.len() > context_offset_range.end - offset {
 215                            chunk.text = &chunk.text[0..(context_offset_range.end - offset)];
 216                            offset = context_offset_range.end;
 217                        } else {
 218                            offset += chunk.text.len();
 219                        }
 220                        let style = chunk
 221                            .syntax_highlight_id
 222                            .and_then(|highlight| highlight.style(&theme));
 223                        if let Some(style) = style {
 224                            let start = context_text.len();
 225                            let end = start + chunk.text.len();
 226                            highlight_ranges.push((start..end, style));
 227                        }
 228                        context_text.push_str(chunk.text);
 229                        if offset >= context_offset_range.end {
 230                            break;
 231                        }
 232                    }
 233
 234                    highlight_ranges.iter_mut().for_each(|(range, _)| {
 235                        range.start = range.start.saturating_sub(left_whitespaces_count);
 236                        range.end = range.end.saturating_sub(left_whitespaces_count);
 237                    });
 238                    if highlight_data.set(highlight_ranges).ok().is_some() {
 239                        notify_tx.try_send(()).ok();
 240                    }
 241
 242                    let trimmed_text = context_text[left_whitespaces_count..].to_owned();
 243                    debug_assert_eq!(
 244                        trimmed_text, search_data.context_text,
 245                        "Highlighted text that does not match the buffer text"
 246                    );
 247                }
 248            }),
 249            _search_match_notify: cx.spawn_in(window, |outline_panel, mut cx| async move {
 250                loop {
 251                    match notify_rx.recv().await {
 252                        Ok(()) => {}
 253                        Err(_) => break,
 254                    };
 255                    while let Ok(()) = notify_rx.try_recv() {
 256                        //
 257                    }
 258                    let update_result = outline_panel.update(&mut cx, |_, cx| {
 259                        cx.notify();
 260                    });
 261                    if update_result.is_err() {
 262                        break;
 263                    }
 264                }
 265            }),
 266        }
 267    }
 268}
 269
 270#[derive(Debug)]
 271enum SelectedEntry {
 272    Invalidated(Option<PanelEntry>),
 273    Valid(PanelEntry, usize),
 274    None,
 275}
 276
 277impl SelectedEntry {
 278    fn invalidate(&mut self) {
 279        match std::mem::replace(self, SelectedEntry::None) {
 280            Self::Valid(entry, _) => *self = Self::Invalidated(Some(entry)),
 281            Self::None => *self = Self::Invalidated(None),
 282            other => *self = other,
 283        }
 284    }
 285
 286    fn is_invalidated(&self) -> bool {
 287        matches!(self, Self::Invalidated(_))
 288    }
 289}
 290
 291#[derive(Debug, Clone, Copy, Default)]
 292struct FsChildren {
 293    files: usize,
 294    dirs: usize,
 295}
 296
 297impl FsChildren {
 298    fn may_be_fold_part(&self) -> bool {
 299        self.dirs == 0 || (self.dirs == 1 && self.files == 0)
 300    }
 301}
 302
 303#[derive(Clone, Debug)]
 304struct CachedEntry {
 305    depth: usize,
 306    string_match: Option<StringMatch>,
 307    entry: PanelEntry,
 308}
 309
 310#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
 311enum CollapsedEntry {
 312    Dir(WorktreeId, ProjectEntryId),
 313    File(WorktreeId, BufferId),
 314    ExternalFile(BufferId),
 315    Excerpt(BufferId, ExcerptId),
 316}
 317
 318#[derive(Debug)]
 319struct Excerpt {
 320    range: ExcerptRange<language::Anchor>,
 321    outlines: ExcerptOutlines,
 322}
 323
 324impl Excerpt {
 325    fn invalidate_outlines(&mut self) {
 326        if let ExcerptOutlines::Outlines(valid_outlines) = &mut self.outlines {
 327            self.outlines = ExcerptOutlines::Invalidated(std::mem::take(valid_outlines));
 328        }
 329    }
 330
 331    fn iter_outlines(&self) -> impl Iterator<Item = &Outline> {
 332        match &self.outlines {
 333            ExcerptOutlines::Outlines(outlines) => outlines.iter(),
 334            ExcerptOutlines::Invalidated(outlines) => outlines.iter(),
 335            ExcerptOutlines::NotFetched => [].iter(),
 336        }
 337    }
 338
 339    fn should_fetch_outlines(&self) -> bool {
 340        match &self.outlines {
 341            ExcerptOutlines::Outlines(_) => false,
 342            ExcerptOutlines::Invalidated(_) => true,
 343            ExcerptOutlines::NotFetched => true,
 344        }
 345    }
 346}
 347
 348#[derive(Debug)]
 349enum ExcerptOutlines {
 350    Outlines(Vec<Outline>),
 351    Invalidated(Vec<Outline>),
 352    NotFetched,
 353}
 354
 355#[derive(Clone, Debug, PartialEq, Eq)]
 356struct FoldedDirsEntry {
 357    worktree_id: WorktreeId,
 358    entries: Vec<GitEntry>,
 359}
 360
 361// TODO: collapse the inner enums into panel entry
 362#[derive(Clone, Debug)]
 363enum PanelEntry {
 364    Fs(FsEntry),
 365    FoldedDirs(FoldedDirsEntry),
 366    Outline(OutlineEntry),
 367    Search(SearchEntry),
 368}
 369
 370#[derive(Clone, Debug)]
 371struct SearchEntry {
 372    match_range: Range<editor::Anchor>,
 373    kind: SearchKind,
 374    render_data: Arc<OnceLock<SearchData>>,
 375}
 376
 377#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
 378enum SearchKind {
 379    Project,
 380    Buffer,
 381}
 382
 383#[derive(Clone, Debug)]
 384struct SearchData {
 385    context_range: Range<editor::Anchor>,
 386    context_text: String,
 387    truncated_left: bool,
 388    truncated_right: bool,
 389    search_match_indices: Vec<Range<usize>>,
 390    highlights_data: HighlightStyleData,
 391}
 392
 393impl PartialEq for PanelEntry {
 394    fn eq(&self, other: &Self) -> bool {
 395        match (self, other) {
 396            (Self::Fs(a), Self::Fs(b)) => a == b,
 397            (
 398                Self::FoldedDirs(FoldedDirsEntry {
 399                    worktree_id: worktree_id_a,
 400                    entries: entries_a,
 401                }),
 402                Self::FoldedDirs(FoldedDirsEntry {
 403                    worktree_id: worktree_id_b,
 404                    entries: entries_b,
 405                }),
 406            ) => worktree_id_a == worktree_id_b && entries_a == entries_b,
 407            (Self::Outline(a), Self::Outline(b)) => a == b,
 408            (
 409                Self::Search(SearchEntry {
 410                    match_range: match_range_a,
 411                    kind: kind_a,
 412                    ..
 413                }),
 414                Self::Search(SearchEntry {
 415                    match_range: match_range_b,
 416                    kind: kind_b,
 417                    ..
 418                }),
 419            ) => match_range_a == match_range_b && kind_a == kind_b,
 420            _ => false,
 421        }
 422    }
 423}
 424
 425impl Eq for PanelEntry {}
 426
 427const SEARCH_MATCH_CONTEXT_SIZE: u32 = 40;
 428const TRUNCATED_CONTEXT_MARK: &str = "";
 429
 430impl SearchData {
 431    fn new(
 432        match_range: &Range<editor::Anchor>,
 433        multi_buffer_snapshot: &MultiBufferSnapshot,
 434    ) -> Self {
 435        let match_point_range = match_range.to_point(multi_buffer_snapshot);
 436        let context_left_border = multi_buffer_snapshot.clip_point(
 437            language::Point::new(
 438                match_point_range.start.row,
 439                match_point_range
 440                    .start
 441                    .column
 442                    .saturating_sub(SEARCH_MATCH_CONTEXT_SIZE),
 443            ),
 444            Bias::Left,
 445        );
 446        let context_right_border = multi_buffer_snapshot.clip_point(
 447            language::Point::new(
 448                match_point_range.end.row,
 449                match_point_range.end.column + SEARCH_MATCH_CONTEXT_SIZE,
 450            ),
 451            Bias::Right,
 452        );
 453
 454        let context_anchor_range =
 455            (context_left_border..context_right_border).to_anchors(multi_buffer_snapshot);
 456        let context_offset_range = context_anchor_range.to_offset(multi_buffer_snapshot);
 457        let match_offset_range = match_range.to_offset(multi_buffer_snapshot);
 458
 459        let mut search_match_indices = vec![
 460            multi_buffer_snapshot.clip_offset(
 461                match_offset_range.start - context_offset_range.start,
 462                Bias::Left,
 463            )
 464                ..multi_buffer_snapshot.clip_offset(
 465                    match_offset_range.end - context_offset_range.start,
 466                    Bias::Right,
 467                ),
 468        ];
 469
 470        let entire_context_text = multi_buffer_snapshot
 471            .text_for_range(context_offset_range.clone())
 472            .collect::<String>();
 473        let left_whitespaces_offset = entire_context_text
 474            .chars()
 475            .take_while(|c| c.is_whitespace())
 476            .map(|c| c.len_utf8())
 477            .sum::<usize>();
 478
 479        let mut extended_context_left_border = context_left_border;
 480        extended_context_left_border.column = extended_context_left_border.column.saturating_sub(1);
 481        let extended_context_left_border =
 482            multi_buffer_snapshot.clip_point(extended_context_left_border, Bias::Left);
 483        let mut extended_context_right_border = context_right_border;
 484        extended_context_right_border.column += 1;
 485        let extended_context_right_border =
 486            multi_buffer_snapshot.clip_point(extended_context_right_border, Bias::Right);
 487
 488        let truncated_left = left_whitespaces_offset == 0
 489            && extended_context_left_border < context_left_border
 490            && multi_buffer_snapshot
 491                .chars_at(extended_context_left_border)
 492                .last()
 493                .map_or(false, |c| !c.is_whitespace());
 494        let truncated_right = entire_context_text
 495            .chars()
 496            .last()
 497            .map_or(true, |c| !c.is_whitespace())
 498            && extended_context_right_border > context_right_border
 499            && multi_buffer_snapshot
 500                .chars_at(extended_context_right_border)
 501                .next()
 502                .map_or(false, |c| !c.is_whitespace());
 503        search_match_indices.iter_mut().for_each(|range| {
 504            range.start = multi_buffer_snapshot.clip_offset(
 505                range.start.saturating_sub(left_whitespaces_offset),
 506                Bias::Left,
 507            );
 508            range.end = multi_buffer_snapshot.clip_offset(
 509                range.end.saturating_sub(left_whitespaces_offset),
 510                Bias::Right,
 511            );
 512        });
 513
 514        let trimmed_row_offset_range =
 515            context_offset_range.start + left_whitespaces_offset..context_offset_range.end;
 516        let trimmed_text = entire_context_text[left_whitespaces_offset..].to_owned();
 517        Self {
 518            highlights_data: Arc::default(),
 519            search_match_indices,
 520            context_range: trimmed_row_offset_range.to_anchors(multi_buffer_snapshot),
 521            context_text: trimmed_text,
 522            truncated_left,
 523            truncated_right,
 524        }
 525    }
 526}
 527
 528#[derive(Clone, Debug, PartialEq, Eq, Hash)]
 529struct OutlineEntryExcerpt {
 530    id: ExcerptId,
 531    buffer_id: BufferId,
 532    range: ExcerptRange<language::Anchor>,
 533}
 534
 535#[derive(Clone, Debug, Eq)]
 536struct OutlineEntryOutline {
 537    buffer_id: BufferId,
 538    excerpt_id: ExcerptId,
 539    outline: Outline,
 540}
 541
 542impl PartialEq for OutlineEntryOutline {
 543    fn eq(&self, other: &Self) -> bool {
 544        self.buffer_id == other.buffer_id
 545            && self.excerpt_id == other.excerpt_id
 546            && self.outline.depth == other.outline.depth
 547            && self.outline.range == other.outline.range
 548            && self.outline.text == other.outline.text
 549    }
 550}
 551
 552impl Hash for OutlineEntryOutline {
 553    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 554        (
 555            self.buffer_id,
 556            self.excerpt_id,
 557            self.outline.depth,
 558            &self.outline.range,
 559            &self.outline.text,
 560        )
 561            .hash(state);
 562    }
 563}
 564
 565#[derive(Clone, Debug, PartialEq, Eq)]
 566enum OutlineEntry {
 567    Excerpt(OutlineEntryExcerpt),
 568    Outline(OutlineEntryOutline),
 569}
 570
 571impl OutlineEntry {
 572    fn ids(&self) -> (BufferId, ExcerptId) {
 573        match self {
 574            OutlineEntry::Excerpt(excerpt) => (excerpt.buffer_id, excerpt.id),
 575            OutlineEntry::Outline(outline) => (outline.buffer_id, outline.excerpt_id),
 576        }
 577    }
 578}
 579
 580#[derive(Debug, Clone, Eq)]
 581struct FsEntryFile {
 582    worktree_id: WorktreeId,
 583    entry: GitEntry,
 584    buffer_id: BufferId,
 585    excerpts: Vec<ExcerptId>,
 586}
 587
 588impl PartialEq for FsEntryFile {
 589    fn eq(&self, other: &Self) -> bool {
 590        self.worktree_id == other.worktree_id
 591            && self.entry.id == other.entry.id
 592            && self.buffer_id == other.buffer_id
 593    }
 594}
 595
 596impl Hash for FsEntryFile {
 597    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 598        (self.buffer_id, self.entry.id, self.worktree_id).hash(state);
 599    }
 600}
 601
 602#[derive(Debug, Clone, Eq)]
 603struct FsEntryDirectory {
 604    worktree_id: WorktreeId,
 605    entry: GitEntry,
 606}
 607
 608impl PartialEq for FsEntryDirectory {
 609    fn eq(&self, other: &Self) -> bool {
 610        self.worktree_id == other.worktree_id && self.entry.id == other.entry.id
 611    }
 612}
 613
 614impl Hash for FsEntryDirectory {
 615    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 616        (self.worktree_id, self.entry.id).hash(state);
 617    }
 618}
 619
 620#[derive(Debug, Clone, Eq)]
 621struct FsEntryExternalFile {
 622    buffer_id: BufferId,
 623    excerpts: Vec<ExcerptId>,
 624}
 625
 626impl PartialEq for FsEntryExternalFile {
 627    fn eq(&self, other: &Self) -> bool {
 628        self.buffer_id == other.buffer_id
 629    }
 630}
 631
 632impl Hash for FsEntryExternalFile {
 633    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 634        self.buffer_id.hash(state);
 635    }
 636}
 637
 638#[derive(Clone, Debug, Eq, PartialEq)]
 639enum FsEntry {
 640    ExternalFile(FsEntryExternalFile),
 641    Directory(FsEntryDirectory),
 642    File(FsEntryFile),
 643}
 644
 645struct ActiveItem {
 646    item_handle: Box<dyn WeakItemHandle>,
 647    active_editor: WeakEntity<Editor>,
 648    _buffer_search_subscription: Subscription,
 649    _editor_subscrpiption: Subscription,
 650}
 651
 652#[derive(Debug)]
 653pub enum Event {
 654    Focus,
 655}
 656
 657#[derive(Serialize, Deserialize)]
 658struct SerializedOutlinePanel {
 659    width: Option<Pixels>,
 660    active: Option<bool>,
 661}
 662
 663pub fn init_settings(cx: &mut App) {
 664    OutlinePanelSettings::register(cx);
 665}
 666
 667pub fn init(cx: &mut App) {
 668    init_settings(cx);
 669
 670    cx.observe_new(|workspace: &mut Workspace, _, _| {
 671        workspace.register_action(|workspace, _: &ToggleFocus, window, cx| {
 672            workspace.toggle_panel_focus::<OutlinePanel>(window, cx);
 673        });
 674    })
 675    .detach();
 676}
 677
 678impl OutlinePanel {
 679    pub async fn load(
 680        workspace: WeakEntity<Workspace>,
 681        mut cx: AsyncWindowContext,
 682    ) -> anyhow::Result<Entity<Self>> {
 683        let serialized_panel = cx
 684            .background_spawn(async move { KEY_VALUE_STORE.read_kvp(OUTLINE_PANEL_KEY) })
 685            .await
 686            .context("loading outline panel")
 687            .log_err()
 688            .flatten()
 689            .map(|panel| serde_json::from_str::<SerializedOutlinePanel>(&panel))
 690            .transpose()
 691            .log_err()
 692            .flatten();
 693
 694        workspace.update_in(&mut cx, |workspace, window, cx| {
 695            let panel = Self::new(workspace, window, cx);
 696            if let Some(serialized_panel) = serialized_panel {
 697                panel.update(cx, |panel, cx| {
 698                    panel.width = serialized_panel.width.map(|px| px.round());
 699                    panel.active = serialized_panel.active.unwrap_or(false);
 700                    cx.notify();
 701                });
 702            }
 703            panel
 704        })
 705    }
 706
 707    fn new(
 708        workspace: &mut Workspace,
 709        window: &mut Window,
 710        cx: &mut Context<Workspace>,
 711    ) -> Entity<Self> {
 712        let project = workspace.project().clone();
 713        let workspace_handle = cx.entity().downgrade();
 714        let outline_panel = cx.new(|cx| {
 715            let filter_editor = cx.new(|cx| {
 716                let mut editor = Editor::single_line(window, cx);
 717                editor.set_placeholder_text("Filter...", cx);
 718                editor
 719            });
 720            let filter_update_subscription = cx.subscribe_in(
 721                &filter_editor,
 722                window,
 723                |outline_panel: &mut Self, _, event, window, cx| {
 724                    if let editor::EditorEvent::BufferEdited = event {
 725                        outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
 726                    }
 727                },
 728            );
 729
 730            let focus_handle = cx.focus_handle();
 731            let focus_subscription = cx.on_focus(&focus_handle, window, Self::focus_in);
 732            let focus_out_subscription =
 733                cx.on_focus_out(&focus_handle, window, |outline_panel, _, window, cx| {
 734                    outline_panel.hide_scrollbar(window, cx);
 735                });
 736            let workspace_subscription = cx.subscribe_in(
 737                &workspace
 738                    .weak_handle()
 739                    .upgrade()
 740                    .expect("have a &mut Workspace"),
 741                window,
 742                move |outline_panel, workspace, event, window, cx| {
 743                    if let workspace::Event::ActiveItemChanged = event {
 744                        if let Some((new_active_item, new_active_editor)) =
 745                            workspace_active_editor(workspace.read(cx), cx)
 746                        {
 747                            if outline_panel.should_replace_active_item(new_active_item.as_ref()) {
 748                                outline_panel.replace_active_editor(
 749                                    new_active_item,
 750                                    new_active_editor,
 751                                    window,
 752                                    cx,
 753                                );
 754                            }
 755                        } else {
 756                            outline_panel.clear_previous(window, cx);
 757                            cx.notify();
 758                        }
 759                    }
 760                },
 761            );
 762
 763            let icons_subscription = cx.observe_global::<FileIcons>(|_, cx| {
 764                cx.notify();
 765            });
 766
 767            let mut outline_panel_settings = *OutlinePanelSettings::get_global(cx);
 768            let mut current_theme = ThemeSettings::get_global(cx).clone();
 769            let settings_subscription =
 770                cx.observe_global_in::<SettingsStore>(window, move |outline_panel, window, cx| {
 771                    let new_settings = OutlinePanelSettings::get_global(cx);
 772                    let new_theme = ThemeSettings::get_global(cx);
 773                    if &current_theme != new_theme {
 774                        outline_panel_settings = *new_settings;
 775                        current_theme = new_theme.clone();
 776                        for excerpts in outline_panel.excerpts.values_mut() {
 777                            for excerpt in excerpts.values_mut() {
 778                                excerpt.invalidate_outlines();
 779                            }
 780                        }
 781                        let update_cached_items = outline_panel.update_non_fs_items(window, cx);
 782                        if update_cached_items {
 783                            outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
 784                        }
 785                    } else if &outline_panel_settings != new_settings {
 786                        outline_panel_settings = *new_settings;
 787                        cx.notify();
 788                    }
 789                });
 790
 791            let scroll_handle = UniformListScrollHandle::new();
 792
 793            let mut outline_panel = Self {
 794                mode: ItemsDisplayMode::Outline,
 795                active: false,
 796                pinned: false,
 797                workspace: workspace_handle,
 798                project,
 799                fs: workspace.app_state().fs.clone(),
 800                show_scrollbar: !Self::should_autohide_scrollbar(cx),
 801                hide_scrollbar_task: None,
 802                vertical_scrollbar_state: ScrollbarState::new(scroll_handle.clone())
 803                    .parent_entity(&cx.entity()),
 804                horizontal_scrollbar_state: ScrollbarState::new(scroll_handle.clone())
 805                    .parent_entity(&cx.entity()),
 806                max_width_item_index: None,
 807                scroll_handle,
 808                focus_handle,
 809                filter_editor,
 810                fs_entries: Vec::new(),
 811                fs_entries_depth: HashMap::default(),
 812                fs_children_count: HashMap::default(),
 813                collapsed_entries: HashSet::default(),
 814                unfolded_dirs: HashMap::default(),
 815                selected_entry: SelectedEntry::None,
 816                context_menu: None,
 817                width: None,
 818                active_item: None,
 819                pending_serialization: Task::ready(None),
 820                updating_fs_entries: false,
 821                updating_cached_entries: false,
 822                new_entries_for_fs_update: HashSet::default(),
 823                preserve_selection_on_buffer_fold_toggles: HashSet::default(),
 824                fs_entries_update_task: Task::ready(()),
 825                cached_entries_update_task: Task::ready(()),
 826                reveal_selection_task: Task::ready(Ok(())),
 827                outline_fetch_tasks: HashMap::default(),
 828                excerpts: HashMap::default(),
 829                cached_entries: Vec::new(),
 830                _subscriptions: vec![
 831                    settings_subscription,
 832                    icons_subscription,
 833                    focus_subscription,
 834                    focus_out_subscription,
 835                    workspace_subscription,
 836                    filter_update_subscription,
 837                ],
 838            };
 839            if let Some((item, editor)) = workspace_active_editor(workspace, cx) {
 840                outline_panel.replace_active_editor(item, editor, window, cx);
 841            }
 842            outline_panel
 843        });
 844
 845        outline_panel
 846    }
 847
 848    fn serialize(&mut self, cx: &mut Context<Self>) {
 849        let width = self.width;
 850        let active = Some(self.active);
 851        self.pending_serialization = cx.background_spawn(
 852            async move {
 853                KEY_VALUE_STORE
 854                    .write_kvp(
 855                        OUTLINE_PANEL_KEY.into(),
 856                        serde_json::to_string(&SerializedOutlinePanel { width, active })?,
 857                    )
 858                    .await?;
 859                anyhow::Ok(())
 860            }
 861            .log_err(),
 862        );
 863    }
 864
 865    fn dispatch_context(&self, window: &mut Window, cx: &mut Context<Self>) -> KeyContext {
 866        let mut dispatch_context = KeyContext::new_with_defaults();
 867        dispatch_context.add("OutlinePanel");
 868        dispatch_context.add("menu");
 869        let identifier = if self.filter_editor.focus_handle(cx).is_focused(window) {
 870            "editing"
 871        } else {
 872            "not_editing"
 873        };
 874        dispatch_context.add(identifier);
 875        dispatch_context
 876    }
 877
 878    fn unfold_directory(
 879        &mut self,
 880        _: &UnfoldDirectory,
 881        window: &mut Window,
 882        cx: &mut Context<Self>,
 883    ) {
 884        if let Some(PanelEntry::FoldedDirs(FoldedDirsEntry {
 885            worktree_id,
 886            entries,
 887            ..
 888        })) = self.selected_entry().cloned()
 889        {
 890            self.unfolded_dirs
 891                .entry(worktree_id)
 892                .or_default()
 893                .extend(entries.iter().map(|entry| entry.id));
 894            self.update_cached_entries(None, window, cx);
 895        }
 896    }
 897
 898    fn fold_directory(&mut self, _: &FoldDirectory, window: &mut Window, cx: &mut Context<Self>) {
 899        let (worktree_id, entry) = match self.selected_entry().cloned() {
 900            Some(PanelEntry::Fs(FsEntry::Directory(directory))) => {
 901                (directory.worktree_id, Some(directory.entry))
 902            }
 903            Some(PanelEntry::FoldedDirs(folded_dirs)) => {
 904                (folded_dirs.worktree_id, folded_dirs.entries.last().cloned())
 905            }
 906            _ => return,
 907        };
 908        let Some(entry) = entry else {
 909            return;
 910        };
 911        let unfolded_dirs = self.unfolded_dirs.get_mut(&worktree_id);
 912        let worktree = self
 913            .project
 914            .read(cx)
 915            .worktree_for_id(worktree_id, cx)
 916            .map(|w| w.read(cx).snapshot());
 917        let Some((_, unfolded_dirs)) = worktree.zip(unfolded_dirs) else {
 918            return;
 919        };
 920
 921        unfolded_dirs.remove(&entry.id);
 922        self.update_cached_entries(None, window, cx);
 923    }
 924
 925    fn open(&mut self, _: &Open, window: &mut Window, cx: &mut Context<Self>) {
 926        if self.filter_editor.focus_handle(cx).is_focused(window) {
 927            cx.propagate()
 928        } else if let Some(selected_entry) = self.selected_entry().cloned() {
 929            self.toggle_expanded(&selected_entry, window, cx);
 930            self.scroll_editor_to_entry(&selected_entry, true, true, window, cx);
 931        }
 932    }
 933
 934    fn cancel(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context<Self>) {
 935        if self.filter_editor.focus_handle(cx).is_focused(window) {
 936            self.focus_handle.focus(window);
 937        } else {
 938            self.filter_editor.focus_handle(cx).focus(window);
 939        }
 940
 941        if self.context_menu.is_some() {
 942            self.context_menu.take();
 943            cx.notify();
 944        }
 945    }
 946
 947    fn open_excerpts(
 948        &mut self,
 949        action: &editor::OpenExcerpts,
 950        window: &mut Window,
 951        cx: &mut Context<Self>,
 952    ) {
 953        if self.filter_editor.focus_handle(cx).is_focused(window) {
 954            cx.propagate()
 955        } else if let Some((active_editor, selected_entry)) =
 956            self.active_editor().zip(self.selected_entry().cloned())
 957        {
 958            self.scroll_editor_to_entry(&selected_entry, true, true, window, cx);
 959            active_editor.update(cx, |editor, cx| editor.open_excerpts(action, window, cx));
 960        }
 961    }
 962
 963    fn open_excerpts_split(
 964        &mut self,
 965        action: &editor::OpenExcerptsSplit,
 966        window: &mut Window,
 967        cx: &mut Context<Self>,
 968    ) {
 969        if self.filter_editor.focus_handle(cx).is_focused(window) {
 970            cx.propagate()
 971        } else if let Some((active_editor, selected_entry)) =
 972            self.active_editor().zip(self.selected_entry().cloned())
 973        {
 974            self.scroll_editor_to_entry(&selected_entry, true, true, window, cx);
 975            active_editor.update(cx, |editor, cx| {
 976                editor.open_excerpts_in_split(action, window, cx)
 977            });
 978        }
 979    }
 980
 981    fn scroll_editor_to_entry(
 982        &mut self,
 983        entry: &PanelEntry,
 984        prefer_selection_change: bool,
 985        prefer_focus_change: bool,
 986        window: &mut Window,
 987        cx: &mut Context<OutlinePanel>,
 988    ) {
 989        let Some(active_editor) = self.active_editor() else {
 990            return;
 991        };
 992        let active_multi_buffer = active_editor.read(cx).buffer().clone();
 993        let multi_buffer_snapshot = active_multi_buffer.read(cx).snapshot(cx);
 994        let mut change_selection = prefer_selection_change;
 995        let mut change_focus = prefer_focus_change;
 996        let mut scroll_to_buffer = None;
 997        let scroll_target = match entry {
 998            PanelEntry::FoldedDirs(..) | PanelEntry::Fs(FsEntry::Directory(..)) => {
 999                change_focus = false;
1000                None
1001            }
1002            PanelEntry::Fs(FsEntry::ExternalFile(file)) => {
1003                change_selection = false;
1004                scroll_to_buffer = Some(file.buffer_id);
1005                multi_buffer_snapshot.excerpts().find_map(
1006                    |(excerpt_id, buffer_snapshot, excerpt_range)| {
1007                        if buffer_snapshot.remote_id() == file.buffer_id {
1008                            multi_buffer_snapshot
1009                                .anchor_in_excerpt(excerpt_id, excerpt_range.context.start)
1010                        } else {
1011                            None
1012                        }
1013                    },
1014                )
1015            }
1016
1017            PanelEntry::Fs(FsEntry::File(file)) => {
1018                change_selection = false;
1019                scroll_to_buffer = Some(file.buffer_id);
1020                self.project
1021                    .update(cx, |project, cx| {
1022                        project
1023                            .path_for_entry(file.entry.id, cx)
1024                            .and_then(|path| project.get_open_buffer(&path, cx))
1025                    })
1026                    .map(|buffer| {
1027                        active_multi_buffer
1028                            .read(cx)
1029                            .excerpts_for_buffer(buffer.read(cx).remote_id(), cx)
1030                    })
1031                    .and_then(|excerpts| {
1032                        let (excerpt_id, excerpt_range) = excerpts.first()?;
1033                        multi_buffer_snapshot
1034                            .anchor_in_excerpt(*excerpt_id, excerpt_range.context.start)
1035                    })
1036            }
1037            PanelEntry::Outline(OutlineEntry::Outline(outline)) => multi_buffer_snapshot
1038                .anchor_in_excerpt(outline.excerpt_id, outline.outline.range.start)
1039                .or_else(|| {
1040                    multi_buffer_snapshot
1041                        .anchor_in_excerpt(outline.excerpt_id, outline.outline.range.end)
1042                }),
1043            PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
1044                change_selection = false;
1045                change_focus = false;
1046                multi_buffer_snapshot.anchor_in_excerpt(excerpt.id, excerpt.range.context.start)
1047            }
1048            PanelEntry::Search(search_entry) => Some(search_entry.match_range.start),
1049        };
1050
1051        if let Some(anchor) = scroll_target {
1052            let activate = self
1053                .workspace
1054                .update(cx, |workspace, cx| match self.active_item() {
1055                    Some(active_item) => workspace.activate_item(
1056                        active_item.as_ref(),
1057                        true,
1058                        change_focus,
1059                        window,
1060                        cx,
1061                    ),
1062                    None => workspace.activate_item(&active_editor, true, change_focus, window, cx),
1063                });
1064
1065            if activate.is_ok() {
1066                self.select_entry(entry.clone(), true, window, cx);
1067                if change_selection {
1068                    active_editor.update(cx, |editor, cx| {
1069                        editor.change_selections(
1070                            Some(Autoscroll::Strategy(AutoscrollStrategy::Center)),
1071                            window,
1072                            cx,
1073                            |s| s.select_ranges(Some(anchor..anchor)),
1074                        );
1075                    });
1076                } else {
1077                    let mut offset = Point::default();
1078                    let show_excerpt_controls = active_editor
1079                        .read(cx)
1080                        .display_map
1081                        .read(cx)
1082                        .show_excerpt_controls();
1083                    let expand_excerpt_control_height = 1.0;
1084                    if let Some(buffer_id) = scroll_to_buffer {
1085                        let current_folded = active_editor.read(cx).is_buffer_folded(buffer_id, cx);
1086                        if current_folded {
1087                            if show_excerpt_controls {
1088                                let previous_buffer_id = self
1089                                    .fs_entries
1090                                    .iter()
1091                                    .rev()
1092                                    .filter_map(|entry| match entry {
1093                                        FsEntry::File(file) => Some(file.buffer_id),
1094                                        FsEntry::ExternalFile(external_file) => {
1095                                            Some(external_file.buffer_id)
1096                                        }
1097                                        FsEntry::Directory(..) => None,
1098                                    })
1099                                    .skip_while(|id| *id != buffer_id)
1100                                    .nth(1);
1101                                if let Some(previous_buffer_id) = previous_buffer_id {
1102                                    if !active_editor
1103                                        .read(cx)
1104                                        .is_buffer_folded(previous_buffer_id, cx)
1105                                    {
1106                                        offset.y += expand_excerpt_control_height;
1107                                    }
1108                                }
1109                            }
1110                        } else {
1111                            if multi_buffer_snapshot.as_singleton().is_none() {
1112                                offset.y = -(active_editor.read(cx).file_header_size() as f32);
1113                            }
1114                            if show_excerpt_controls {
1115                                offset.y -= expand_excerpt_control_height;
1116                            }
1117                        }
1118                    }
1119                    active_editor.update(cx, |editor, cx| {
1120                        editor.set_scroll_anchor(ScrollAnchor { offset, anchor }, window, cx);
1121                    });
1122                }
1123
1124                if change_focus {
1125                    active_editor.focus_handle(cx).focus(window);
1126                } else {
1127                    self.focus_handle.focus(window);
1128                }
1129            }
1130        }
1131    }
1132
1133    fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
1134        if let Some(entry_to_select) = self.selected_entry().and_then(|selected_entry| {
1135            self.cached_entries
1136                .iter()
1137                .map(|cached_entry| &cached_entry.entry)
1138                .skip_while(|entry| entry != &selected_entry)
1139                .nth(1)
1140                .cloned()
1141        }) {
1142            self.select_entry(entry_to_select, true, window, cx);
1143        } else {
1144            self.select_first(&SelectFirst {}, window, cx)
1145        }
1146        if let Some(selected_entry) = self.selected_entry().cloned() {
1147            self.scroll_editor_to_entry(&selected_entry, true, false, window, cx);
1148        }
1149    }
1150
1151    fn select_prev(&mut self, _: &SelectPrev, window: &mut Window, cx: &mut Context<Self>) {
1152        if let Some(entry_to_select) = self.selected_entry().and_then(|selected_entry| {
1153            self.cached_entries
1154                .iter()
1155                .rev()
1156                .map(|cached_entry| &cached_entry.entry)
1157                .skip_while(|entry| entry != &selected_entry)
1158                .nth(1)
1159                .cloned()
1160        }) {
1161            self.select_entry(entry_to_select, true, window, cx);
1162        } else {
1163            self.select_last(&SelectLast, window, cx)
1164        }
1165        if let Some(selected_entry) = self.selected_entry().cloned() {
1166            self.scroll_editor_to_entry(&selected_entry, true, false, window, cx);
1167        }
1168    }
1169
1170    fn select_parent(&mut self, _: &SelectParent, window: &mut Window, cx: &mut Context<Self>) {
1171        if let Some(entry_to_select) = self.selected_entry().and_then(|selected_entry| {
1172            let mut previous_entries = self
1173                .cached_entries
1174                .iter()
1175                .rev()
1176                .map(|cached_entry| &cached_entry.entry)
1177                .skip_while(|entry| entry != &selected_entry)
1178                .skip(1);
1179            match &selected_entry {
1180                PanelEntry::Fs(fs_entry) => match fs_entry {
1181                    FsEntry::ExternalFile(..) => None,
1182                    FsEntry::File(FsEntryFile {
1183                        worktree_id, entry, ..
1184                    })
1185                    | FsEntry::Directory(FsEntryDirectory {
1186                        worktree_id, entry, ..
1187                    }) => entry.path.parent().and_then(|parent_path| {
1188                        previous_entries.find(|entry| match entry {
1189                            PanelEntry::Fs(FsEntry::Directory(directory)) => {
1190                                directory.worktree_id == *worktree_id
1191                                    && directory.entry.path.as_ref() == parent_path
1192                            }
1193                            PanelEntry::FoldedDirs(FoldedDirsEntry {
1194                                worktree_id: dirs_worktree_id,
1195                                entries: dirs,
1196                                ..
1197                            }) => {
1198                                dirs_worktree_id == worktree_id
1199                                    && dirs
1200                                        .last()
1201                                        .map_or(false, |dir| dir.path.as_ref() == parent_path)
1202                            }
1203                            _ => false,
1204                        })
1205                    }),
1206                },
1207                PanelEntry::FoldedDirs(folded_dirs) => folded_dirs
1208                    .entries
1209                    .first()
1210                    .and_then(|entry| entry.path.parent())
1211                    .and_then(|parent_path| {
1212                        previous_entries.find(|entry| {
1213                            if let PanelEntry::Fs(FsEntry::Directory(directory)) = entry {
1214                                directory.worktree_id == folded_dirs.worktree_id
1215                                    && directory.entry.path.as_ref() == parent_path
1216                            } else {
1217                                false
1218                            }
1219                        })
1220                    }),
1221                PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
1222                    previous_entries.find(|entry| match entry {
1223                        PanelEntry::Fs(FsEntry::File(file)) => {
1224                            file.buffer_id == excerpt.buffer_id
1225                                && file.excerpts.contains(&excerpt.id)
1226                        }
1227                        PanelEntry::Fs(FsEntry::ExternalFile(external_file)) => {
1228                            external_file.buffer_id == excerpt.buffer_id
1229                                && external_file.excerpts.contains(&excerpt.id)
1230                        }
1231                        _ => false,
1232                    })
1233                }
1234                PanelEntry::Outline(OutlineEntry::Outline(outline)) => {
1235                    previous_entries.find(|entry| {
1236                        if let PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) = entry {
1237                            outline.buffer_id == excerpt.buffer_id
1238                                && outline.excerpt_id == excerpt.id
1239                        } else {
1240                            false
1241                        }
1242                    })
1243                }
1244                PanelEntry::Search(_) => {
1245                    previous_entries.find(|entry| !matches!(entry, PanelEntry::Search(_)))
1246                }
1247            }
1248        }) {
1249            self.select_entry(entry_to_select.clone(), true, window, cx);
1250        } else {
1251            self.select_first(&SelectFirst {}, window, cx);
1252        }
1253    }
1254
1255    fn select_first(&mut self, _: &SelectFirst, window: &mut Window, cx: &mut Context<Self>) {
1256        if let Some(first_entry) = self.cached_entries.first() {
1257            self.select_entry(first_entry.entry.clone(), true, window, cx);
1258        }
1259    }
1260
1261    fn select_last(&mut self, _: &SelectLast, window: &mut Window, cx: &mut Context<Self>) {
1262        if let Some(new_selection) = self
1263            .cached_entries
1264            .iter()
1265            .rev()
1266            .map(|cached_entry| &cached_entry.entry)
1267            .next()
1268        {
1269            self.select_entry(new_selection.clone(), true, window, cx);
1270        }
1271    }
1272
1273    fn autoscroll(&mut self, cx: &mut Context<Self>) {
1274        if let Some(selected_entry) = self.selected_entry() {
1275            let index = self
1276                .cached_entries
1277                .iter()
1278                .position(|cached_entry| &cached_entry.entry == selected_entry);
1279            if let Some(index) = index {
1280                self.scroll_handle
1281                    .scroll_to_item(index, ScrollStrategy::Center);
1282                cx.notify();
1283            }
1284        }
1285    }
1286
1287    fn focus_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1288        if !self.focus_handle.contains_focused(window, cx) {
1289            cx.emit(Event::Focus);
1290        }
1291    }
1292
1293    fn deploy_context_menu(
1294        &mut self,
1295        position: Point<Pixels>,
1296        entry: PanelEntry,
1297        window: &mut Window,
1298        cx: &mut Context<Self>,
1299    ) {
1300        self.select_entry(entry.clone(), true, window, cx);
1301        let is_root = match &entry {
1302            PanelEntry::Fs(FsEntry::File(FsEntryFile {
1303                worktree_id, entry, ..
1304            }))
1305            | PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
1306                worktree_id, entry, ..
1307            })) => self
1308                .project
1309                .read(cx)
1310                .worktree_for_id(*worktree_id, cx)
1311                .map(|worktree| {
1312                    worktree.read(cx).root_entry().map(|entry| entry.id) == Some(entry.id)
1313                })
1314                .unwrap_or(false),
1315            PanelEntry::FoldedDirs(FoldedDirsEntry {
1316                worktree_id,
1317                entries,
1318                ..
1319            }) => entries
1320                .first()
1321                .and_then(|entry| {
1322                    self.project
1323                        .read(cx)
1324                        .worktree_for_id(*worktree_id, cx)
1325                        .map(|worktree| {
1326                            worktree.read(cx).root_entry().map(|entry| entry.id) == Some(entry.id)
1327                        })
1328                })
1329                .unwrap_or(false),
1330            PanelEntry::Fs(FsEntry::ExternalFile(..)) => false,
1331            PanelEntry::Outline(..) => {
1332                cx.notify();
1333                return;
1334            }
1335            PanelEntry::Search(_) => {
1336                cx.notify();
1337                return;
1338            }
1339        };
1340        let auto_fold_dirs = OutlinePanelSettings::get_global(cx).auto_fold_dirs;
1341        let is_foldable = auto_fold_dirs && !is_root && self.is_foldable(&entry);
1342        let is_unfoldable = auto_fold_dirs && !is_root && self.is_unfoldable(&entry);
1343
1344        let context_menu = ContextMenu::build(window, cx, |menu, _, _| {
1345            menu.context(self.focus_handle.clone())
1346                .when(cfg!(target_os = "macos"), |menu| {
1347                    menu.action("Reveal in Finder", Box::new(RevealInFileManager))
1348                })
1349                .when(cfg!(not(target_os = "macos")), |menu| {
1350                    menu.action("Reveal in File Manager", Box::new(RevealInFileManager))
1351                })
1352                .action("Open in Terminal", Box::new(OpenInTerminal))
1353                .when(is_unfoldable, |menu| {
1354                    menu.action("Unfold Directory", Box::new(UnfoldDirectory))
1355                })
1356                .when(is_foldable, |menu| {
1357                    menu.action("Fold Directory", Box::new(FoldDirectory))
1358                })
1359                .separator()
1360                .action("Copy Path", Box::new(zed_actions::workspace::CopyPath))
1361                .action(
1362                    "Copy Relative Path",
1363                    Box::new(zed_actions::workspace::CopyRelativePath),
1364                )
1365        });
1366        window.focus(&context_menu.focus_handle(cx));
1367        let subscription = cx.subscribe(&context_menu, |outline_panel, _, _: &DismissEvent, cx| {
1368            outline_panel.context_menu.take();
1369            cx.notify();
1370        });
1371        self.context_menu = Some((context_menu, position, subscription));
1372        cx.notify();
1373    }
1374
1375    fn is_unfoldable(&self, entry: &PanelEntry) -> bool {
1376        matches!(entry, PanelEntry::FoldedDirs(..))
1377    }
1378
1379    fn is_foldable(&self, entry: &PanelEntry) -> bool {
1380        let (directory_worktree, directory_entry) = match entry {
1381            PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
1382                worktree_id,
1383                entry: directory_entry,
1384                ..
1385            })) => (*worktree_id, Some(directory_entry)),
1386            _ => return false,
1387        };
1388        let Some(directory_entry) = directory_entry else {
1389            return false;
1390        };
1391
1392        if self
1393            .unfolded_dirs
1394            .get(&directory_worktree)
1395            .map_or(true, |unfolded_dirs| {
1396                !unfolded_dirs.contains(&directory_entry.id)
1397            })
1398        {
1399            return false;
1400        }
1401
1402        let children = self
1403            .fs_children_count
1404            .get(&directory_worktree)
1405            .and_then(|entries| entries.get(&directory_entry.path))
1406            .copied()
1407            .unwrap_or_default();
1408
1409        children.may_be_fold_part() && children.dirs > 0
1410    }
1411
1412    fn expand_selected_entry(
1413        &mut self,
1414        _: &ExpandSelectedEntry,
1415        window: &mut Window,
1416        cx: &mut Context<Self>,
1417    ) {
1418        let Some(active_editor) = self.active_editor() else {
1419            return;
1420        };
1421        let Some(selected_entry) = self.selected_entry().cloned() else {
1422            return;
1423        };
1424        let mut buffers_to_unfold = HashSet::default();
1425        let entry_to_expand = match &selected_entry {
1426            PanelEntry::FoldedDirs(FoldedDirsEntry {
1427                entries: dir_entries,
1428                worktree_id,
1429                ..
1430            }) => dir_entries.last().map(|entry| {
1431                buffers_to_unfold.extend(self.buffers_inside_directory(*worktree_id, entry));
1432                CollapsedEntry::Dir(*worktree_id, entry.id)
1433            }),
1434            PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
1435                worktree_id, entry, ..
1436            })) => {
1437                buffers_to_unfold.extend(self.buffers_inside_directory(*worktree_id, entry));
1438                Some(CollapsedEntry::Dir(*worktree_id, entry.id))
1439            }
1440            PanelEntry::Fs(FsEntry::File(FsEntryFile {
1441                worktree_id,
1442                buffer_id,
1443                ..
1444            })) => {
1445                buffers_to_unfold.insert(*buffer_id);
1446                Some(CollapsedEntry::File(*worktree_id, *buffer_id))
1447            }
1448            PanelEntry::Fs(FsEntry::ExternalFile(external_file)) => {
1449                buffers_to_unfold.insert(external_file.buffer_id);
1450                Some(CollapsedEntry::ExternalFile(external_file.buffer_id))
1451            }
1452            PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
1453                Some(CollapsedEntry::Excerpt(excerpt.buffer_id, excerpt.id))
1454            }
1455            PanelEntry::Search(_) | PanelEntry::Outline(..) => return,
1456        };
1457        let Some(collapsed_entry) = entry_to_expand else {
1458            return;
1459        };
1460        let expanded = self.collapsed_entries.remove(&collapsed_entry);
1461        if expanded {
1462            if let CollapsedEntry::Dir(worktree_id, dir_entry_id) = collapsed_entry {
1463                let task = self.project.update(cx, |project, cx| {
1464                    project.expand_entry(worktree_id, dir_entry_id, cx)
1465                });
1466                if let Some(task) = task {
1467                    task.detach_and_log_err(cx);
1468                }
1469            };
1470
1471            active_editor.update(cx, |editor, cx| {
1472                buffers_to_unfold.retain(|buffer_id| editor.is_buffer_folded(*buffer_id, cx));
1473            });
1474            self.select_entry(selected_entry, true, window, cx);
1475            if buffers_to_unfold.is_empty() {
1476                self.update_cached_entries(None, window, cx);
1477            } else {
1478                self.toggle_buffers_fold(buffers_to_unfold, false, window, cx)
1479                    .detach();
1480            }
1481        } else {
1482            self.select_next(&SelectNext, window, cx)
1483        }
1484    }
1485
1486    fn collapse_selected_entry(
1487        &mut self,
1488        _: &CollapseSelectedEntry,
1489        window: &mut Window,
1490        cx: &mut Context<Self>,
1491    ) {
1492        let Some(active_editor) = self.active_editor() else {
1493            return;
1494        };
1495        let Some(selected_entry) = self.selected_entry().cloned() else {
1496            return;
1497        };
1498
1499        let mut buffers_to_fold = HashSet::default();
1500        let collapsed = match &selected_entry {
1501            PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
1502                worktree_id, entry, ..
1503            })) => {
1504                if self
1505                    .collapsed_entries
1506                    .insert(CollapsedEntry::Dir(*worktree_id, entry.id))
1507                {
1508                    buffers_to_fold.extend(self.buffers_inside_directory(*worktree_id, entry));
1509                    true
1510                } else {
1511                    false
1512                }
1513            }
1514            PanelEntry::Fs(FsEntry::File(FsEntryFile {
1515                worktree_id,
1516                buffer_id,
1517                ..
1518            })) => {
1519                if self
1520                    .collapsed_entries
1521                    .insert(CollapsedEntry::File(*worktree_id, *buffer_id))
1522                {
1523                    buffers_to_fold.insert(*buffer_id);
1524                    true
1525                } else {
1526                    false
1527                }
1528            }
1529            PanelEntry::Fs(FsEntry::ExternalFile(external_file)) => {
1530                if self
1531                    .collapsed_entries
1532                    .insert(CollapsedEntry::ExternalFile(external_file.buffer_id))
1533                {
1534                    buffers_to_fold.insert(external_file.buffer_id);
1535                    true
1536                } else {
1537                    false
1538                }
1539            }
1540            PanelEntry::FoldedDirs(folded_dirs) => {
1541                let mut folded = false;
1542                if let Some(dir_entry) = folded_dirs.entries.last() {
1543                    if self
1544                        .collapsed_entries
1545                        .insert(CollapsedEntry::Dir(folded_dirs.worktree_id, dir_entry.id))
1546                    {
1547                        folded = true;
1548                        buffers_to_fold.extend(
1549                            self.buffers_inside_directory(folded_dirs.worktree_id, dir_entry),
1550                        );
1551                    }
1552                }
1553                folded
1554            }
1555            PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => self
1556                .collapsed_entries
1557                .insert(CollapsedEntry::Excerpt(excerpt.buffer_id, excerpt.id)),
1558            PanelEntry::Search(_) | PanelEntry::Outline(..) => false,
1559        };
1560
1561        if collapsed {
1562            active_editor.update(cx, |editor, cx| {
1563                buffers_to_fold.retain(|buffer_id| !editor.is_buffer_folded(*buffer_id, cx));
1564            });
1565            self.select_entry(selected_entry, true, window, cx);
1566            if buffers_to_fold.is_empty() {
1567                self.update_cached_entries(None, window, cx);
1568            } else {
1569                self.toggle_buffers_fold(buffers_to_fold, true, window, cx)
1570                    .detach();
1571            }
1572        } else {
1573            self.select_parent(&SelectParent, window, cx);
1574        }
1575    }
1576
1577    pub fn expand_all_entries(
1578        &mut self,
1579        _: &ExpandAllEntries,
1580        window: &mut Window,
1581        cx: &mut Context<Self>,
1582    ) {
1583        let Some(active_editor) = self.active_editor() else {
1584            return;
1585        };
1586        let mut buffers_to_unfold = HashSet::default();
1587        let expanded_entries =
1588            self.fs_entries
1589                .iter()
1590                .fold(HashSet::default(), |mut entries, fs_entry| {
1591                    match fs_entry {
1592                        FsEntry::ExternalFile(external_file) => {
1593                            buffers_to_unfold.insert(external_file.buffer_id);
1594                            entries.insert(CollapsedEntry::ExternalFile(external_file.buffer_id));
1595                            entries.extend(
1596                                self.excerpts
1597                                    .get(&external_file.buffer_id)
1598                                    .into_iter()
1599                                    .flat_map(|excerpts| {
1600                                        excerpts.iter().map(|(excerpt_id, _)| {
1601                                            CollapsedEntry::Excerpt(
1602                                                external_file.buffer_id,
1603                                                *excerpt_id,
1604                                            )
1605                                        })
1606                                    }),
1607                            );
1608                        }
1609                        FsEntry::Directory(directory) => {
1610                            entries.insert(CollapsedEntry::Dir(
1611                                directory.worktree_id,
1612                                directory.entry.id,
1613                            ));
1614                        }
1615                        FsEntry::File(file) => {
1616                            buffers_to_unfold.insert(file.buffer_id);
1617                            entries.insert(CollapsedEntry::File(file.worktree_id, file.buffer_id));
1618                            entries.extend(
1619                                self.excerpts.get(&file.buffer_id).into_iter().flat_map(
1620                                    |excerpts| {
1621                                        excerpts.iter().map(|(excerpt_id, _)| {
1622                                            CollapsedEntry::Excerpt(file.buffer_id, *excerpt_id)
1623                                        })
1624                                    },
1625                                ),
1626                            );
1627                        }
1628                    };
1629                    entries
1630                });
1631        self.collapsed_entries
1632            .retain(|entry| !expanded_entries.contains(entry));
1633        active_editor.update(cx, |editor, cx| {
1634            buffers_to_unfold.retain(|buffer_id| editor.is_buffer_folded(*buffer_id, cx));
1635        });
1636        if buffers_to_unfold.is_empty() {
1637            self.update_cached_entries(None, window, cx);
1638        } else {
1639            self.toggle_buffers_fold(buffers_to_unfold, false, window, cx)
1640                .detach();
1641        }
1642    }
1643
1644    pub fn collapse_all_entries(
1645        &mut self,
1646        _: &CollapseAllEntries,
1647        window: &mut Window,
1648        cx: &mut Context<Self>,
1649    ) {
1650        let Some(active_editor) = self.active_editor() else {
1651            return;
1652        };
1653        let mut buffers_to_fold = HashSet::default();
1654        let new_entries = self
1655            .cached_entries
1656            .iter()
1657            .flat_map(|cached_entry| match &cached_entry.entry {
1658                PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
1659                    worktree_id, entry, ..
1660                })) => Some(CollapsedEntry::Dir(*worktree_id, entry.id)),
1661                PanelEntry::Fs(FsEntry::File(FsEntryFile {
1662                    worktree_id,
1663                    buffer_id,
1664                    ..
1665                })) => {
1666                    buffers_to_fold.insert(*buffer_id);
1667                    Some(CollapsedEntry::File(*worktree_id, *buffer_id))
1668                }
1669                PanelEntry::Fs(FsEntry::ExternalFile(external_file)) => {
1670                    buffers_to_fold.insert(external_file.buffer_id);
1671                    Some(CollapsedEntry::ExternalFile(external_file.buffer_id))
1672                }
1673                PanelEntry::FoldedDirs(FoldedDirsEntry {
1674                    worktree_id,
1675                    entries,
1676                    ..
1677                }) => Some(CollapsedEntry::Dir(*worktree_id, entries.last()?.id)),
1678                PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
1679                    Some(CollapsedEntry::Excerpt(excerpt.buffer_id, excerpt.id))
1680                }
1681                PanelEntry::Search(_) | PanelEntry::Outline(..) => None,
1682            })
1683            .collect::<Vec<_>>();
1684        self.collapsed_entries.extend(new_entries);
1685
1686        active_editor.update(cx, |editor, cx| {
1687            buffers_to_fold.retain(|buffer_id| !editor.is_buffer_folded(*buffer_id, cx));
1688        });
1689        if buffers_to_fold.is_empty() {
1690            self.update_cached_entries(None, window, cx);
1691        } else {
1692            self.toggle_buffers_fold(buffers_to_fold, true, window, cx)
1693                .detach();
1694        }
1695    }
1696
1697    fn toggle_expanded(&mut self, entry: &PanelEntry, window: &mut Window, cx: &mut Context<Self>) {
1698        let Some(active_editor) = self.active_editor() else {
1699            return;
1700        };
1701        let mut fold = false;
1702        let mut buffers_to_toggle = HashSet::default();
1703        match entry {
1704            PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
1705                worktree_id,
1706                entry: dir_entry,
1707                ..
1708            })) => {
1709                let entry_id = dir_entry.id;
1710                let collapsed_entry = CollapsedEntry::Dir(*worktree_id, entry_id);
1711                buffers_to_toggle.extend(self.buffers_inside_directory(*worktree_id, dir_entry));
1712                if self.collapsed_entries.remove(&collapsed_entry) {
1713                    self.project
1714                        .update(cx, |project, cx| {
1715                            project.expand_entry(*worktree_id, entry_id, cx)
1716                        })
1717                        .unwrap_or_else(|| Task::ready(Ok(())))
1718                        .detach_and_log_err(cx);
1719                } else {
1720                    self.collapsed_entries.insert(collapsed_entry);
1721                    fold = true;
1722                }
1723            }
1724            PanelEntry::Fs(FsEntry::File(FsEntryFile {
1725                worktree_id,
1726                buffer_id,
1727                ..
1728            })) => {
1729                let collapsed_entry = CollapsedEntry::File(*worktree_id, *buffer_id);
1730                buffers_to_toggle.insert(*buffer_id);
1731                if !self.collapsed_entries.remove(&collapsed_entry) {
1732                    self.collapsed_entries.insert(collapsed_entry);
1733                    fold = true;
1734                }
1735            }
1736            PanelEntry::Fs(FsEntry::ExternalFile(external_file)) => {
1737                let collapsed_entry = CollapsedEntry::ExternalFile(external_file.buffer_id);
1738                buffers_to_toggle.insert(external_file.buffer_id);
1739                if !self.collapsed_entries.remove(&collapsed_entry) {
1740                    self.collapsed_entries.insert(collapsed_entry);
1741                    fold = true;
1742                }
1743            }
1744            PanelEntry::FoldedDirs(FoldedDirsEntry {
1745                worktree_id,
1746                entries: dir_entries,
1747                ..
1748            }) => {
1749                if let Some(dir_entry) = dir_entries.first() {
1750                    let entry_id = dir_entry.id;
1751                    let collapsed_entry = CollapsedEntry::Dir(*worktree_id, entry_id);
1752                    buffers_to_toggle
1753                        .extend(self.buffers_inside_directory(*worktree_id, dir_entry));
1754                    if self.collapsed_entries.remove(&collapsed_entry) {
1755                        self.project
1756                            .update(cx, |project, cx| {
1757                                project.expand_entry(*worktree_id, entry_id, cx)
1758                            })
1759                            .unwrap_or_else(|| Task::ready(Ok(())))
1760                            .detach_and_log_err(cx);
1761                    } else {
1762                        self.collapsed_entries.insert(collapsed_entry);
1763                        fold = true;
1764                    }
1765                }
1766            }
1767            PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
1768                let collapsed_entry = CollapsedEntry::Excerpt(excerpt.buffer_id, excerpt.id);
1769                if !self.collapsed_entries.remove(&collapsed_entry) {
1770                    self.collapsed_entries.insert(collapsed_entry);
1771                }
1772            }
1773            PanelEntry::Search(_) | PanelEntry::Outline(..) => return,
1774        }
1775
1776        active_editor.update(cx, |editor, cx| {
1777            buffers_to_toggle.retain(|buffer_id| {
1778                let folded = editor.is_buffer_folded(*buffer_id, cx);
1779                if fold {
1780                    !folded
1781                } else {
1782                    folded
1783                }
1784            });
1785        });
1786
1787        self.select_entry(entry.clone(), true, window, cx);
1788        if buffers_to_toggle.is_empty() {
1789            self.update_cached_entries(None, window, cx);
1790        } else {
1791            self.toggle_buffers_fold(buffers_to_toggle, fold, window, cx)
1792                .detach();
1793        }
1794    }
1795
1796    fn toggle_buffers_fold(
1797        &self,
1798        buffers: HashSet<BufferId>,
1799        fold: bool,
1800        window: &mut Window,
1801        cx: &mut Context<Self>,
1802    ) -> Task<()> {
1803        let Some(active_editor) = self.active_editor() else {
1804            return Task::ready(());
1805        };
1806        cx.spawn_in(window, |outline_panel, mut cx| async move {
1807            outline_panel
1808                .update_in(&mut cx, |outline_panel, window, cx| {
1809                    active_editor.update(cx, |editor, cx| {
1810                        for buffer_id in buffers {
1811                            outline_panel
1812                                .preserve_selection_on_buffer_fold_toggles
1813                                .insert(buffer_id);
1814                            if fold {
1815                                editor.fold_buffer(buffer_id, cx);
1816                            } else {
1817                                editor.unfold_buffer(buffer_id, cx);
1818                            }
1819                        }
1820                    });
1821                    if let Some(selection) = outline_panel.selected_entry().cloned() {
1822                        outline_panel.scroll_editor_to_entry(&selection, false, false, window, cx);
1823                    }
1824                })
1825                .ok();
1826        })
1827    }
1828
1829    fn copy_path(
1830        &mut self,
1831        _: &zed_actions::workspace::CopyPath,
1832        _: &mut Window,
1833        cx: &mut Context<Self>,
1834    ) {
1835        if let Some(clipboard_text) = self
1836            .selected_entry()
1837            .and_then(|entry| self.abs_path(entry, cx))
1838            .map(|p| p.to_string_lossy().to_string())
1839        {
1840            cx.write_to_clipboard(ClipboardItem::new_string(clipboard_text));
1841        }
1842    }
1843
1844    fn copy_relative_path(
1845        &mut self,
1846        _: &zed_actions::workspace::CopyRelativePath,
1847        _: &mut Window,
1848        cx: &mut Context<Self>,
1849    ) {
1850        if let Some(clipboard_text) = self
1851            .selected_entry()
1852            .and_then(|entry| match entry {
1853                PanelEntry::Fs(entry) => self.relative_path(entry, cx),
1854                PanelEntry::FoldedDirs(folded_dirs) => {
1855                    folded_dirs.entries.last().map(|entry| entry.path.clone())
1856                }
1857                PanelEntry::Search(_) | PanelEntry::Outline(..) => None,
1858            })
1859            .map(|p| p.to_string_lossy().to_string())
1860        {
1861            cx.write_to_clipboard(ClipboardItem::new_string(clipboard_text));
1862        }
1863    }
1864
1865    fn reveal_in_finder(
1866        &mut self,
1867        _: &RevealInFileManager,
1868        _: &mut Window,
1869        cx: &mut Context<Self>,
1870    ) {
1871        if let Some(abs_path) = self
1872            .selected_entry()
1873            .and_then(|entry| self.abs_path(entry, cx))
1874        {
1875            cx.reveal_path(&abs_path);
1876        }
1877    }
1878
1879    fn open_in_terminal(
1880        &mut self,
1881        _: &OpenInTerminal,
1882        window: &mut Window,
1883        cx: &mut Context<Self>,
1884    ) {
1885        let selected_entry = self.selected_entry();
1886        let abs_path = selected_entry.and_then(|entry| self.abs_path(entry, cx));
1887        let working_directory = if let (
1888            Some(abs_path),
1889            Some(PanelEntry::Fs(FsEntry::File(..) | FsEntry::ExternalFile(..))),
1890        ) = (&abs_path, selected_entry)
1891        {
1892            abs_path.parent().map(|p| p.to_owned())
1893        } else {
1894            abs_path
1895        };
1896
1897        if let Some(working_directory) = working_directory {
1898            window.dispatch_action(
1899                workspace::OpenTerminal { working_directory }.boxed_clone(),
1900                cx,
1901            )
1902        }
1903    }
1904
1905    fn reveal_entry_for_selection(
1906        &mut self,
1907        editor: Entity<Editor>,
1908        window: &mut Window,
1909        cx: &mut Context<Self>,
1910    ) {
1911        if !self.active
1912            || !OutlinePanelSettings::get_global(cx).auto_reveal_entries
1913            || self.focus_handle.contains_focused(window, cx)
1914        {
1915            return;
1916        }
1917        let project = self.project.clone();
1918        self.reveal_selection_task = cx.spawn_in(window, |outline_panel, mut cx| async move {
1919            cx.background_executor().timer(UPDATE_DEBOUNCE).await;
1920            let entry_with_selection =
1921                outline_panel.update_in(&mut cx, |outline_panel, window, cx| {
1922                    outline_panel.location_for_editor_selection(&editor, window, cx)
1923                })?;
1924            let Some(entry_with_selection) = entry_with_selection else {
1925                outline_panel.update(&mut cx, |outline_panel, cx| {
1926                    outline_panel.selected_entry = SelectedEntry::None;
1927                    cx.notify();
1928                })?;
1929                return Ok(());
1930            };
1931            let related_buffer_entry = match &entry_with_selection {
1932                PanelEntry::Fs(FsEntry::File(FsEntryFile {
1933                    worktree_id,
1934                    buffer_id,
1935                    ..
1936                })) => project.update(&mut cx, |project, cx| {
1937                    let entry_id = project
1938                        .buffer_for_id(*buffer_id, cx)
1939                        .and_then(|buffer| buffer.read(cx).entry_id(cx));
1940                    project
1941                        .worktree_for_id(*worktree_id, cx)
1942                        .zip(entry_id)
1943                        .and_then(|(worktree, entry_id)| {
1944                            let entry = worktree.read(cx).entry_for_id(entry_id)?.clone();
1945                            Some((worktree, entry))
1946                        })
1947                })?,
1948                PanelEntry::Outline(outline_entry) => {
1949                    let (buffer_id, excerpt_id) = outline_entry.ids();
1950                    outline_panel.update(&mut cx, |outline_panel, cx| {
1951                        outline_panel
1952                            .collapsed_entries
1953                            .remove(&CollapsedEntry::ExternalFile(buffer_id));
1954                        outline_panel
1955                            .collapsed_entries
1956                            .remove(&CollapsedEntry::Excerpt(buffer_id, excerpt_id));
1957                        let project = outline_panel.project.read(cx);
1958                        let entry_id = project
1959                            .buffer_for_id(buffer_id, cx)
1960                            .and_then(|buffer| buffer.read(cx).entry_id(cx));
1961
1962                        entry_id.and_then(|entry_id| {
1963                            project
1964                                .worktree_for_entry(entry_id, cx)
1965                                .and_then(|worktree| {
1966                                    let worktree_id = worktree.read(cx).id();
1967                                    outline_panel
1968                                        .collapsed_entries
1969                                        .remove(&CollapsedEntry::File(worktree_id, buffer_id));
1970                                    let entry = worktree.read(cx).entry_for_id(entry_id)?.clone();
1971                                    Some((worktree, entry))
1972                                })
1973                        })
1974                    })?
1975                }
1976                PanelEntry::Fs(FsEntry::ExternalFile(..)) => None,
1977                PanelEntry::Search(SearchEntry { match_range, .. }) => match_range
1978                    .start
1979                    .buffer_id
1980                    .or(match_range.end.buffer_id)
1981                    .map(|buffer_id| {
1982                        outline_panel.update(&mut cx, |outline_panel, cx| {
1983                            outline_panel
1984                                .collapsed_entries
1985                                .remove(&CollapsedEntry::ExternalFile(buffer_id));
1986                            let project = project.read(cx);
1987                            let entry_id = project
1988                                .buffer_for_id(buffer_id, cx)
1989                                .and_then(|buffer| buffer.read(cx).entry_id(cx));
1990
1991                            entry_id.and_then(|entry_id| {
1992                                project
1993                                    .worktree_for_entry(entry_id, cx)
1994                                    .and_then(|worktree| {
1995                                        let worktree_id = worktree.read(cx).id();
1996                                        outline_panel
1997                                            .collapsed_entries
1998                                            .remove(&CollapsedEntry::File(worktree_id, buffer_id));
1999                                        let entry =
2000                                            worktree.read(cx).entry_for_id(entry_id)?.clone();
2001                                        Some((worktree, entry))
2002                                    })
2003                            })
2004                        })
2005                    })
2006                    .transpose()?
2007                    .flatten(),
2008                _ => return anyhow::Ok(()),
2009            };
2010            if let Some((worktree, buffer_entry)) = related_buffer_entry {
2011                outline_panel.update(&mut cx, |outline_panel, cx| {
2012                    let worktree_id = worktree.read(cx).id();
2013                    let mut dirs_to_expand = Vec::new();
2014                    {
2015                        let mut traversal = worktree.read(cx).traverse_from_path(
2016                            true,
2017                            true,
2018                            true,
2019                            buffer_entry.path.as_ref(),
2020                        );
2021                        let mut current_entry = buffer_entry;
2022                        loop {
2023                            if current_entry.is_dir()
2024                                && outline_panel
2025                                    .collapsed_entries
2026                                    .remove(&CollapsedEntry::Dir(worktree_id, current_entry.id))
2027                            {
2028                                dirs_to_expand.push(current_entry.id);
2029                            }
2030
2031                            if traversal.back_to_parent() {
2032                                if let Some(parent_entry) = traversal.entry() {
2033                                    current_entry = parent_entry.clone();
2034                                    continue;
2035                                }
2036                            }
2037                            break;
2038                        }
2039                    }
2040                    for dir_to_expand in dirs_to_expand {
2041                        project
2042                            .update(cx, |project, cx| {
2043                                project.expand_entry(worktree_id, dir_to_expand, cx)
2044                            })
2045                            .unwrap_or_else(|| Task::ready(Ok(())))
2046                            .detach_and_log_err(cx)
2047                    }
2048                })?
2049            }
2050
2051            outline_panel.update_in(&mut cx, |outline_panel, window, cx| {
2052                outline_panel.select_entry(entry_with_selection, false, window, cx);
2053                outline_panel.update_cached_entries(None, window, cx);
2054            })?;
2055
2056            anyhow::Ok(())
2057        });
2058    }
2059
2060    fn render_excerpt(
2061        &self,
2062        excerpt: &OutlineEntryExcerpt,
2063        depth: usize,
2064        window: &mut Window,
2065        cx: &mut Context<OutlinePanel>,
2066    ) -> Option<Stateful<Div>> {
2067        let item_id = ElementId::from(excerpt.id.to_proto() as usize);
2068        let is_active = match self.selected_entry() {
2069            Some(PanelEntry::Outline(OutlineEntry::Excerpt(selected_excerpt))) => {
2070                selected_excerpt.buffer_id == excerpt.buffer_id && selected_excerpt.id == excerpt.id
2071            }
2072            _ => false,
2073        };
2074        let has_outlines = self
2075            .excerpts
2076            .get(&excerpt.buffer_id)
2077            .and_then(|excerpts| match &excerpts.get(&excerpt.id)?.outlines {
2078                ExcerptOutlines::Outlines(outlines) => Some(outlines),
2079                ExcerptOutlines::Invalidated(outlines) => Some(outlines),
2080                ExcerptOutlines::NotFetched => None,
2081            })
2082            .map_or(false, |outlines| !outlines.is_empty());
2083        let is_expanded = !self
2084            .collapsed_entries
2085            .contains(&CollapsedEntry::Excerpt(excerpt.buffer_id, excerpt.id));
2086        let color = entry_label_color(is_active);
2087        let icon = if has_outlines {
2088            FileIcons::get_chevron_icon(is_expanded, cx)
2089                .map(|icon_path| Icon::from_path(icon_path).color(color).into_any_element())
2090        } else {
2091            None
2092        }
2093        .unwrap_or_else(empty_icon);
2094
2095        let label = self.excerpt_label(excerpt.buffer_id, &excerpt.range, cx)?;
2096        let label_element = Label::new(label)
2097            .single_line()
2098            .color(color)
2099            .into_any_element();
2100
2101        Some(self.entry_element(
2102            PanelEntry::Outline(OutlineEntry::Excerpt(excerpt.clone())),
2103            item_id,
2104            depth,
2105            Some(icon),
2106            is_active,
2107            label_element,
2108            window,
2109            cx,
2110        ))
2111    }
2112
2113    fn excerpt_label(
2114        &self,
2115        buffer_id: BufferId,
2116        range: &ExcerptRange<language::Anchor>,
2117        cx: &App,
2118    ) -> Option<String> {
2119        let buffer_snapshot = self.buffer_snapshot_for_id(buffer_id, cx)?;
2120        let excerpt_range = range.context.to_point(&buffer_snapshot);
2121        Some(format!(
2122            "Lines {}- {}",
2123            excerpt_range.start.row + 1,
2124            excerpt_range.end.row + 1,
2125        ))
2126    }
2127
2128    fn render_outline(
2129        &self,
2130        outline: &OutlineEntryOutline,
2131        depth: usize,
2132        string_match: Option<&StringMatch>,
2133        window: &mut Window,
2134        cx: &mut Context<Self>,
2135    ) -> Stateful<Div> {
2136        let item_id = ElementId::from(SharedString::from(format!(
2137            "{:?}|{:?}{:?}|{:?}",
2138            outline.buffer_id, outline.excerpt_id, outline.outline.range, &outline.outline.text,
2139        )));
2140
2141        let label_element = outline::render_item(
2142            &outline.outline,
2143            string_match
2144                .map(|string_match| string_match.ranges().collect::<Vec<_>>())
2145                .unwrap_or_default(),
2146            cx,
2147        )
2148        .into_any_element();
2149
2150        let is_active = match self.selected_entry() {
2151            Some(PanelEntry::Outline(OutlineEntry::Outline(selected))) => {
2152                outline == selected && outline.outline == selected.outline
2153            }
2154            _ => false,
2155        };
2156
2157        let icon = if self.is_singleton_active(cx) {
2158            None
2159        } else {
2160            Some(empty_icon())
2161        };
2162
2163        self.entry_element(
2164            PanelEntry::Outline(OutlineEntry::Outline(outline.clone())),
2165            item_id,
2166            depth,
2167            icon,
2168            is_active,
2169            label_element,
2170            window,
2171            cx,
2172        )
2173    }
2174
2175    fn render_entry(
2176        &self,
2177        rendered_entry: &FsEntry,
2178        depth: usize,
2179        string_match: Option<&StringMatch>,
2180        window: &mut Window,
2181        cx: &mut Context<Self>,
2182    ) -> Stateful<Div> {
2183        let settings = OutlinePanelSettings::get_global(cx);
2184        let is_active = match self.selected_entry() {
2185            Some(PanelEntry::Fs(selected_entry)) => selected_entry == rendered_entry,
2186            _ => false,
2187        };
2188        let (item_id, label_element, icon) = match rendered_entry {
2189            FsEntry::File(FsEntryFile {
2190                worktree_id, entry, ..
2191            }) => {
2192                let name = self.entry_name(worktree_id, entry, cx);
2193                let color =
2194                    entry_git_aware_label_color(entry.git_summary, entry.is_ignored, is_active);
2195                let icon = if settings.file_icons {
2196                    FileIcons::get_icon(&entry.path, cx)
2197                        .map(|icon_path| Icon::from_path(icon_path).color(color).into_any_element())
2198                } else {
2199                    None
2200                };
2201                (
2202                    ElementId::from(entry.id.to_proto() as usize),
2203                    HighlightedLabel::new(
2204                        name,
2205                        string_match
2206                            .map(|string_match| string_match.positions.clone())
2207                            .unwrap_or_default(),
2208                    )
2209                    .color(color)
2210                    .into_any_element(),
2211                    icon.unwrap_or_else(empty_icon),
2212                )
2213            }
2214            FsEntry::Directory(directory) => {
2215                let name = self.entry_name(&directory.worktree_id, &directory.entry, cx);
2216
2217                let is_expanded = !self.collapsed_entries.contains(&CollapsedEntry::Dir(
2218                    directory.worktree_id,
2219                    directory.entry.id,
2220                ));
2221                let color = entry_git_aware_label_color(
2222                    directory.entry.git_summary,
2223                    directory.entry.is_ignored,
2224                    is_active,
2225                );
2226                let icon = if settings.folder_icons {
2227                    FileIcons::get_folder_icon(is_expanded, cx)
2228                } else {
2229                    FileIcons::get_chevron_icon(is_expanded, cx)
2230                }
2231                .map(Icon::from_path)
2232                .map(|icon| icon.color(color).into_any_element());
2233                (
2234                    ElementId::from(directory.entry.id.to_proto() as usize),
2235                    HighlightedLabel::new(
2236                        name,
2237                        string_match
2238                            .map(|string_match| string_match.positions.clone())
2239                            .unwrap_or_default(),
2240                    )
2241                    .color(color)
2242                    .into_any_element(),
2243                    icon.unwrap_or_else(empty_icon),
2244                )
2245            }
2246            FsEntry::ExternalFile(external_file) => {
2247                let color = entry_label_color(is_active);
2248                let (icon, name) = match self.buffer_snapshot_for_id(external_file.buffer_id, cx) {
2249                    Some(buffer_snapshot) => match buffer_snapshot.file() {
2250                        Some(file) => {
2251                            let path = file.path();
2252                            let icon = if settings.file_icons {
2253                                FileIcons::get_icon(path.as_ref(), cx)
2254                            } else {
2255                                None
2256                            }
2257                            .map(Icon::from_path)
2258                            .map(|icon| icon.color(color).into_any_element());
2259                            (icon, file_name(path.as_ref()))
2260                        }
2261                        None => (None, "Untitled".to_string()),
2262                    },
2263                    None => (None, "Unknown buffer".to_string()),
2264                };
2265                (
2266                    ElementId::from(external_file.buffer_id.to_proto() as usize),
2267                    HighlightedLabel::new(
2268                        name,
2269                        string_match
2270                            .map(|string_match| string_match.positions.clone())
2271                            .unwrap_or_default(),
2272                    )
2273                    .color(color)
2274                    .into_any_element(),
2275                    icon.unwrap_or_else(empty_icon),
2276                )
2277            }
2278        };
2279
2280        self.entry_element(
2281            PanelEntry::Fs(rendered_entry.clone()),
2282            item_id,
2283            depth,
2284            Some(icon),
2285            is_active,
2286            label_element,
2287            window,
2288            cx,
2289        )
2290    }
2291
2292    fn render_folded_dirs(
2293        &self,
2294        folded_dir: &FoldedDirsEntry,
2295        depth: usize,
2296        string_match: Option<&StringMatch>,
2297        window: &mut Window,
2298        cx: &mut Context<OutlinePanel>,
2299    ) -> Stateful<Div> {
2300        let settings = OutlinePanelSettings::get_global(cx);
2301        let is_active = match self.selected_entry() {
2302            Some(PanelEntry::FoldedDirs(selected_dirs)) => {
2303                selected_dirs.worktree_id == folded_dir.worktree_id
2304                    && selected_dirs.entries == folded_dir.entries
2305            }
2306            _ => false,
2307        };
2308        let (item_id, label_element, icon) = {
2309            let name = self.dir_names_string(&folded_dir.entries, folded_dir.worktree_id, cx);
2310
2311            let is_expanded = folded_dir.entries.iter().all(|dir| {
2312                !self
2313                    .collapsed_entries
2314                    .contains(&CollapsedEntry::Dir(folded_dir.worktree_id, dir.id))
2315            });
2316            let is_ignored = folded_dir.entries.iter().any(|entry| entry.is_ignored);
2317            let git_status = folded_dir
2318                .entries
2319                .first()
2320                .map(|entry| entry.git_summary)
2321                .unwrap_or_default();
2322            let color = entry_git_aware_label_color(git_status, is_ignored, is_active);
2323            let icon = if settings.folder_icons {
2324                FileIcons::get_folder_icon(is_expanded, cx)
2325            } else {
2326                FileIcons::get_chevron_icon(is_expanded, cx)
2327            }
2328            .map(Icon::from_path)
2329            .map(|icon| icon.color(color).into_any_element());
2330            (
2331                ElementId::from(
2332                    folded_dir
2333                        .entries
2334                        .last()
2335                        .map(|entry| entry.id.to_proto())
2336                        .unwrap_or_else(|| folded_dir.worktree_id.to_proto())
2337                        as usize,
2338                ),
2339                HighlightedLabel::new(
2340                    name,
2341                    string_match
2342                        .map(|string_match| string_match.positions.clone())
2343                        .unwrap_or_default(),
2344                )
2345                .color(color)
2346                .into_any_element(),
2347                icon.unwrap_or_else(empty_icon),
2348            )
2349        };
2350
2351        self.entry_element(
2352            PanelEntry::FoldedDirs(folded_dir.clone()),
2353            item_id,
2354            depth,
2355            Some(icon),
2356            is_active,
2357            label_element,
2358            window,
2359            cx,
2360        )
2361    }
2362
2363    #[allow(clippy::too_many_arguments)]
2364    fn render_search_match(
2365        &mut self,
2366        multi_buffer_snapshot: Option<&MultiBufferSnapshot>,
2367        match_range: &Range<editor::Anchor>,
2368        render_data: &Arc<OnceLock<SearchData>>,
2369        kind: SearchKind,
2370        depth: usize,
2371        string_match: Option<&StringMatch>,
2372        window: &mut Window,
2373        cx: &mut Context<Self>,
2374    ) -> Option<Stateful<Div>> {
2375        let search_data = match render_data.get() {
2376            Some(search_data) => search_data,
2377            None => {
2378                if let ItemsDisplayMode::Search(search_state) = &mut self.mode {
2379                    if let Some(multi_buffer_snapshot) = multi_buffer_snapshot {
2380                        search_state
2381                            .highlight_search_match_tx
2382                            .try_send(HighlightArguments {
2383                                multi_buffer_snapshot: multi_buffer_snapshot.clone(),
2384                                match_range: match_range.clone(),
2385                                search_data: Arc::clone(render_data),
2386                            })
2387                            .ok();
2388                    }
2389                }
2390                return None;
2391            }
2392        };
2393        let search_matches = string_match
2394            .iter()
2395            .flat_map(|string_match| string_match.ranges())
2396            .collect::<Vec<_>>();
2397        let match_ranges = if search_matches.is_empty() {
2398            &search_data.search_match_indices
2399        } else {
2400            &search_matches
2401        };
2402        let label_element = outline::render_item(
2403            &OutlineItem {
2404                depth,
2405                annotation_range: None,
2406                range: search_data.context_range.clone(),
2407                text: search_data.context_text.clone(),
2408                highlight_ranges: search_data
2409                    .highlights_data
2410                    .get()
2411                    .cloned()
2412                    .unwrap_or_default(),
2413                name_ranges: search_data.search_match_indices.clone(),
2414                body_range: Some(search_data.context_range.clone()),
2415            },
2416            match_ranges.iter().cloned(),
2417            cx,
2418        );
2419        let truncated_contents_label = || Label::new(TRUNCATED_CONTEXT_MARK);
2420        let entire_label = h_flex()
2421            .justify_center()
2422            .p_0()
2423            .when(search_data.truncated_left, |parent| {
2424                parent.child(truncated_contents_label())
2425            })
2426            .child(label_element)
2427            .when(search_data.truncated_right, |parent| {
2428                parent.child(truncated_contents_label())
2429            })
2430            .into_any_element();
2431
2432        let is_active = match self.selected_entry() {
2433            Some(PanelEntry::Search(SearchEntry {
2434                match_range: selected_match_range,
2435                ..
2436            })) => match_range == selected_match_range,
2437            _ => false,
2438        };
2439        Some(self.entry_element(
2440            PanelEntry::Search(SearchEntry {
2441                kind,
2442                match_range: match_range.clone(),
2443                render_data: render_data.clone(),
2444            }),
2445            ElementId::from(SharedString::from(format!("search-{match_range:?}"))),
2446            depth,
2447            None,
2448            is_active,
2449            entire_label,
2450            window,
2451            cx,
2452        ))
2453    }
2454
2455    #[allow(clippy::too_many_arguments)]
2456    fn entry_element(
2457        &self,
2458        rendered_entry: PanelEntry,
2459        item_id: ElementId,
2460        depth: usize,
2461        icon_element: Option<AnyElement>,
2462        is_active: bool,
2463        label_element: gpui::AnyElement,
2464        window: &mut Window,
2465        cx: &mut Context<OutlinePanel>,
2466    ) -> Stateful<Div> {
2467        let settings = OutlinePanelSettings::get_global(cx);
2468        div()
2469            .text_ui(cx)
2470            .id(item_id.clone())
2471            .on_click({
2472                let clicked_entry = rendered_entry.clone();
2473                cx.listener(move |outline_panel, event: &gpui::ClickEvent, window, cx| {
2474                    if event.down.button == MouseButton::Right || event.down.first_mouse {
2475                        return;
2476                    }
2477                    let change_focus = event.down.click_count > 1;
2478                    outline_panel.toggle_expanded(&clicked_entry, window, cx);
2479                    outline_panel.scroll_editor_to_entry(
2480                        &clicked_entry,
2481                        true,
2482                        change_focus,
2483                        window,
2484                        cx,
2485                    );
2486                })
2487            })
2488            .cursor_pointer()
2489            .child(
2490                ListItem::new(item_id)
2491                    .indent_level(depth)
2492                    .indent_step_size(px(settings.indent_size))
2493                    .toggle_state(is_active)
2494                    .when_some(icon_element, |list_item, icon_element| {
2495                        list_item.child(h_flex().child(icon_element))
2496                    })
2497                    .child(h_flex().h_6().child(label_element).ml_1())
2498                    .on_secondary_mouse_down(cx.listener(
2499                        move |outline_panel, event: &MouseDownEvent, window, cx| {
2500                            // Stop propagation to prevent the catch-all context menu for the project
2501                            // panel from being deployed.
2502                            cx.stop_propagation();
2503                            outline_panel.deploy_context_menu(
2504                                event.position,
2505                                rendered_entry.clone(),
2506                                window,
2507                                cx,
2508                            )
2509                        },
2510                    )),
2511            )
2512            .border_1()
2513            .border_r_2()
2514            .rounded_none()
2515            .hover(|style| {
2516                if is_active {
2517                    style
2518                } else {
2519                    let hover_color = cx.theme().colors().ghost_element_hover;
2520                    style.bg(hover_color).border_color(hover_color)
2521                }
2522            })
2523            .when(
2524                is_active && self.focus_handle.contains_focused(window, cx),
2525                |div| div.border_color(Color::Selected.color(cx)),
2526            )
2527    }
2528
2529    fn entry_name(&self, worktree_id: &WorktreeId, entry: &Entry, cx: &App) -> String {
2530        let name = match self.project.read(cx).worktree_for_id(*worktree_id, cx) {
2531            Some(worktree) => {
2532                let worktree = worktree.read(cx);
2533                match worktree.snapshot().root_entry() {
2534                    Some(root_entry) => {
2535                        if root_entry.id == entry.id {
2536                            file_name(worktree.abs_path().as_ref())
2537                        } else {
2538                            let path = worktree.absolutize(entry.path.as_ref()).ok();
2539                            let path = path.as_deref().unwrap_or_else(|| entry.path.as_ref());
2540                            file_name(path)
2541                        }
2542                    }
2543                    None => {
2544                        let path = worktree.absolutize(entry.path.as_ref()).ok();
2545                        let path = path.as_deref().unwrap_or_else(|| entry.path.as_ref());
2546                        file_name(path)
2547                    }
2548                }
2549            }
2550            None => file_name(entry.path.as_ref()),
2551        };
2552        name
2553    }
2554
2555    fn update_fs_entries(
2556        &mut self,
2557        active_editor: Entity<Editor>,
2558        debounce: Option<Duration>,
2559        window: &mut Window,
2560        cx: &mut Context<Self>,
2561    ) {
2562        if !self.active {
2563            return;
2564        }
2565
2566        let auto_fold_dirs = OutlinePanelSettings::get_global(cx).auto_fold_dirs;
2567        let active_multi_buffer = active_editor.read(cx).buffer().clone();
2568        let new_entries = self.new_entries_for_fs_update.clone();
2569        self.updating_fs_entries = true;
2570        self.fs_entries_update_task = cx.spawn_in(window, |outline_panel, mut cx| async move {
2571            if let Some(debounce) = debounce {
2572                cx.background_executor().timer(debounce).await;
2573            }
2574
2575            let mut new_collapsed_entries = HashSet::default();
2576            let mut new_unfolded_dirs = HashMap::default();
2577            let mut root_entries = HashSet::default();
2578            let mut new_excerpts = HashMap::<BufferId, HashMap<ExcerptId, Excerpt>>::default();
2579            let Ok(buffer_excerpts) = outline_panel.update(&mut cx, |outline_panel, cx| {
2580                new_collapsed_entries = outline_panel.collapsed_entries.clone();
2581                new_unfolded_dirs = outline_panel.unfolded_dirs.clone();
2582                let multi_buffer_snapshot = active_multi_buffer.read(cx).snapshot(cx);
2583                let buffer_excerpts = multi_buffer_snapshot.excerpts().fold(
2584                    HashMap::default(),
2585                    |mut buffer_excerpts, (excerpt_id, buffer_snapshot, excerpt_range)| {
2586                        let buffer_id = buffer_snapshot.remote_id();
2587                        let file = File::from_dyn(buffer_snapshot.file());
2588                        let entry_id = file.and_then(|file| file.project_entry_id(cx));
2589                        let worktree = file.map(|file| file.worktree.read(cx).snapshot());
2590                        let is_new = new_entries.contains(&excerpt_id)
2591                            || !outline_panel.excerpts.contains_key(&buffer_id);
2592                        let is_folded = active_editor.read(cx).is_buffer_folded(buffer_id, cx);
2593                        buffer_excerpts
2594                            .entry(buffer_id)
2595                            .or_insert_with(|| (is_new, is_folded, Vec::new(), entry_id, worktree))
2596                            .2
2597                            .push(excerpt_id);
2598
2599                        let outlines = match outline_panel
2600                            .excerpts
2601                            .get(&buffer_id)
2602                            .and_then(|excerpts| excerpts.get(&excerpt_id))
2603                        {
2604                            Some(old_excerpt) => match &old_excerpt.outlines {
2605                                ExcerptOutlines::Outlines(outlines) => {
2606                                    ExcerptOutlines::Outlines(outlines.clone())
2607                                }
2608                                ExcerptOutlines::Invalidated(_) => ExcerptOutlines::NotFetched,
2609                                ExcerptOutlines::NotFetched => ExcerptOutlines::NotFetched,
2610                            },
2611                            None => ExcerptOutlines::NotFetched,
2612                        };
2613                        new_excerpts.entry(buffer_id).or_default().insert(
2614                            excerpt_id,
2615                            Excerpt {
2616                                range: excerpt_range,
2617                                outlines,
2618                            },
2619                        );
2620                        buffer_excerpts
2621                    },
2622                );
2623                buffer_excerpts
2624            }) else {
2625                return;
2626            };
2627
2628            let Some((
2629                new_collapsed_entries,
2630                new_unfolded_dirs,
2631                new_fs_entries,
2632                new_depth_map,
2633                new_children_count,
2634            )) = cx
2635                .background_spawn(async move {
2636                    let mut processed_external_buffers = HashSet::default();
2637                    let mut new_worktree_entries =
2638                        BTreeMap::<WorktreeId, HashMap<ProjectEntryId, GitEntry>>::default();
2639                    let mut worktree_excerpts = HashMap::<
2640                        WorktreeId,
2641                        HashMap<ProjectEntryId, (BufferId, Vec<ExcerptId>)>,
2642                    >::default();
2643                    let mut external_excerpts = HashMap::default();
2644
2645                    for (buffer_id, (is_new, is_folded, excerpts, entry_id, worktree)) in
2646                        buffer_excerpts
2647                    {
2648                        if is_folded {
2649                            match &worktree {
2650                                Some(worktree) => {
2651                                    new_collapsed_entries
2652                                        .insert(CollapsedEntry::File(worktree.id(), buffer_id));
2653                                }
2654                                None => {
2655                                    new_collapsed_entries
2656                                        .insert(CollapsedEntry::ExternalFile(buffer_id));
2657                                }
2658                            }
2659                        } else if is_new {
2660                            match &worktree {
2661                                Some(worktree) => {
2662                                    new_collapsed_entries
2663                                        .remove(&CollapsedEntry::File(worktree.id(), buffer_id));
2664                                }
2665                                None => {
2666                                    new_collapsed_entries
2667                                        .remove(&CollapsedEntry::ExternalFile(buffer_id));
2668                                }
2669                            }
2670                        }
2671
2672                        if let Some(worktree) = worktree {
2673                            let worktree_id = worktree.id();
2674                            let unfolded_dirs = new_unfolded_dirs.entry(worktree_id).or_default();
2675
2676                            match entry_id.and_then(|id| worktree.entry_for_id(id)).cloned() {
2677                                Some(entry) => {
2678                                    let entry = GitEntry {
2679                                        git_summary: worktree
2680                                            .status_for_file(&entry.path)
2681                                            .map(|status| status.summary())
2682                                            .unwrap_or_default(),
2683                                        entry,
2684                                    };
2685                                    let mut traversal = worktree
2686                                        .traverse_from_path(true, true, true, entry.path.as_ref())
2687                                        .with_git_statuses();
2688
2689                                    let mut entries_to_add = HashMap::default();
2690                                    worktree_excerpts
2691                                        .entry(worktree_id)
2692                                        .or_default()
2693                                        .insert(entry.id, (buffer_id, excerpts));
2694                                    let mut current_entry = entry;
2695                                    loop {
2696                                        if current_entry.is_dir() {
2697                                            let is_root =
2698                                                worktree.root_entry().map(|entry| entry.id)
2699                                                    == Some(current_entry.id);
2700                                            if is_root {
2701                                                root_entries.insert(current_entry.id);
2702                                                if auto_fold_dirs {
2703                                                    unfolded_dirs.insert(current_entry.id);
2704                                                }
2705                                            }
2706                                            if is_new {
2707                                                new_collapsed_entries.remove(&CollapsedEntry::Dir(
2708                                                    worktree_id,
2709                                                    current_entry.id,
2710                                                ));
2711                                            }
2712                                        }
2713
2714                                        let new_entry_added = entries_to_add
2715                                            .insert(current_entry.id, current_entry)
2716                                            .is_none();
2717                                        if new_entry_added && traversal.back_to_parent() {
2718                                            if let Some(parent_entry) = traversal.entry() {
2719                                                current_entry = parent_entry.to_owned();
2720                                                continue;
2721                                            }
2722                                        }
2723                                        break;
2724                                    }
2725                                    new_worktree_entries
2726                                        .entry(worktree_id)
2727                                        .or_insert_with(HashMap::default)
2728                                        .extend(entries_to_add);
2729                                }
2730                                None => {
2731                                    if processed_external_buffers.insert(buffer_id) {
2732                                        external_excerpts
2733                                            .entry(buffer_id)
2734                                            .or_insert_with(Vec::new)
2735                                            .extend(excerpts);
2736                                    }
2737                                }
2738                            }
2739                        } else if processed_external_buffers.insert(buffer_id) {
2740                            external_excerpts
2741                                .entry(buffer_id)
2742                                .or_insert_with(Vec::new)
2743                                .extend(excerpts);
2744                        }
2745                    }
2746
2747                    let mut new_children_count =
2748                        HashMap::<WorktreeId, HashMap<Arc<Path>, FsChildren>>::default();
2749
2750                    let worktree_entries = new_worktree_entries
2751                        .into_iter()
2752                        .map(|(worktree_id, entries)| {
2753                            let mut entries = entries.into_values().collect::<Vec<_>>();
2754                            entries.sort_by(|a, b| a.path.as_ref().cmp(b.path.as_ref()));
2755                            (worktree_id, entries)
2756                        })
2757                        .flat_map(|(worktree_id, entries)| {
2758                            {
2759                                entries
2760                                    .into_iter()
2761                                    .filter_map(|entry| {
2762                                        if auto_fold_dirs {
2763                                            if let Some(parent) = entry.path.parent() {
2764                                                let children = new_children_count
2765                                                    .entry(worktree_id)
2766                                                    .or_default()
2767                                                    .entry(Arc::from(parent))
2768                                                    .or_default();
2769                                                if entry.is_dir() {
2770                                                    children.dirs += 1;
2771                                                } else {
2772                                                    children.files += 1;
2773                                                }
2774                                            }
2775                                        }
2776
2777                                        if entry.is_dir() {
2778                                            Some(FsEntry::Directory(FsEntryDirectory {
2779                                                worktree_id,
2780                                                entry,
2781                                            }))
2782                                        } else {
2783                                            let (buffer_id, excerpts) = worktree_excerpts
2784                                                .get_mut(&worktree_id)
2785                                                .and_then(|worktree_excerpts| {
2786                                                    worktree_excerpts.remove(&entry.id)
2787                                                })?;
2788                                            Some(FsEntry::File(FsEntryFile {
2789                                                worktree_id,
2790                                                buffer_id,
2791                                                entry,
2792                                                excerpts,
2793                                            }))
2794                                        }
2795                                    })
2796                                    .collect::<Vec<_>>()
2797                            }
2798                        })
2799                        .collect::<Vec<_>>();
2800
2801                    let mut visited_dirs = Vec::new();
2802                    let mut new_depth_map = HashMap::default();
2803                    let new_visible_entries = external_excerpts
2804                        .into_iter()
2805                        .sorted_by_key(|(id, _)| *id)
2806                        .map(|(buffer_id, excerpts)| {
2807                            FsEntry::ExternalFile(FsEntryExternalFile {
2808                                buffer_id,
2809                                excerpts,
2810                            })
2811                        })
2812                        .chain(worktree_entries)
2813                        .filter(|visible_item| {
2814                            match visible_item {
2815                                FsEntry::Directory(directory) => {
2816                                    let parent_id = back_to_common_visited_parent(
2817                                        &mut visited_dirs,
2818                                        &directory.worktree_id,
2819                                        &directory.entry,
2820                                    );
2821
2822                                    let mut depth = 0;
2823                                    if !root_entries.contains(&directory.entry.id) {
2824                                        if auto_fold_dirs {
2825                                            let children = new_children_count
2826                                                .get(&directory.worktree_id)
2827                                                .and_then(|children_count| {
2828                                                    children_count.get(&directory.entry.path)
2829                                                })
2830                                                .copied()
2831                                                .unwrap_or_default();
2832
2833                                            if !children.may_be_fold_part()
2834                                                || (children.dirs == 0
2835                                                    && visited_dirs
2836                                                        .last()
2837                                                        .map(|(parent_dir_id, _)| {
2838                                                            new_unfolded_dirs
2839                                                                .get(&directory.worktree_id)
2840                                                                .map_or(true, |unfolded_dirs| {
2841                                                                    unfolded_dirs
2842                                                                        .contains(parent_dir_id)
2843                                                                })
2844                                                        })
2845                                                        .unwrap_or(true))
2846                                            {
2847                                                new_unfolded_dirs
2848                                                    .entry(directory.worktree_id)
2849                                                    .or_default()
2850                                                    .insert(directory.entry.id);
2851                                            }
2852                                        }
2853
2854                                        depth = parent_id
2855                                            .and_then(|(worktree_id, id)| {
2856                                                new_depth_map.get(&(worktree_id, id)).copied()
2857                                            })
2858                                            .unwrap_or(0)
2859                                            + 1;
2860                                    };
2861                                    visited_dirs
2862                                        .push((directory.entry.id, directory.entry.path.clone()));
2863                                    new_depth_map
2864                                        .insert((directory.worktree_id, directory.entry.id), depth);
2865                                }
2866                                FsEntry::File(FsEntryFile {
2867                                    worktree_id,
2868                                    entry: file_entry,
2869                                    ..
2870                                }) => {
2871                                    let parent_id = back_to_common_visited_parent(
2872                                        &mut visited_dirs,
2873                                        worktree_id,
2874                                        file_entry,
2875                                    );
2876                                    let depth = if root_entries.contains(&file_entry.id) {
2877                                        0
2878                                    } else {
2879                                        parent_id
2880                                            .and_then(|(worktree_id, id)| {
2881                                                new_depth_map.get(&(worktree_id, id)).copied()
2882                                            })
2883                                            .unwrap_or(0)
2884                                            + 1
2885                                    };
2886                                    new_depth_map.insert((*worktree_id, file_entry.id), depth);
2887                                }
2888                                FsEntry::ExternalFile(..) => {
2889                                    visited_dirs.clear();
2890                                }
2891                            }
2892
2893                            true
2894                        })
2895                        .collect::<Vec<_>>();
2896
2897                    anyhow::Ok((
2898                        new_collapsed_entries,
2899                        new_unfolded_dirs,
2900                        new_visible_entries,
2901                        new_depth_map,
2902                        new_children_count,
2903                    ))
2904                })
2905                .await
2906                .log_err()
2907            else {
2908                return;
2909            };
2910
2911            outline_panel
2912                .update_in(&mut cx, |outline_panel, window, cx| {
2913                    outline_panel.updating_fs_entries = false;
2914                    outline_panel.new_entries_for_fs_update.clear();
2915                    outline_panel.excerpts = new_excerpts;
2916                    outline_panel.collapsed_entries = new_collapsed_entries;
2917                    outline_panel.unfolded_dirs = new_unfolded_dirs;
2918                    outline_panel.fs_entries = new_fs_entries;
2919                    outline_panel.fs_entries_depth = new_depth_map;
2920                    outline_panel.fs_children_count = new_children_count;
2921                    outline_panel.update_non_fs_items(window, cx);
2922                    outline_panel.update_cached_entries(debounce, window, cx);
2923
2924                    cx.notify();
2925                })
2926                .ok();
2927        });
2928    }
2929
2930    fn replace_active_editor(
2931        &mut self,
2932        new_active_item: Box<dyn ItemHandle>,
2933        new_active_editor: Entity<Editor>,
2934        window: &mut Window,
2935        cx: &mut Context<Self>,
2936    ) {
2937        self.clear_previous(window, cx);
2938        let buffer_search_subscription = cx.subscribe_in(
2939            &new_active_editor,
2940            window,
2941            |outline_panel: &mut Self,
2942             _,
2943             e: &SearchEvent,
2944             window: &mut Window,
2945             cx: &mut Context<Self>| {
2946                if matches!(e, SearchEvent::MatchesInvalidated) {
2947                    let update_cached_items = outline_panel.update_search_matches(window, cx);
2948                    if update_cached_items {
2949                        outline_panel.selected_entry.invalidate();
2950                        outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
2951                    }
2952                };
2953                outline_panel.autoscroll(cx);
2954            },
2955        );
2956        self.active_item = Some(ActiveItem {
2957            _buffer_search_subscription: buffer_search_subscription,
2958            _editor_subscrpiption: subscribe_for_editor_events(&new_active_editor, window, cx),
2959            item_handle: new_active_item.downgrade_item(),
2960            active_editor: new_active_editor.downgrade(),
2961        });
2962        self.new_entries_for_fs_update
2963            .extend(new_active_editor.read(cx).buffer().read(cx).excerpt_ids());
2964        self.selected_entry.invalidate();
2965        self.update_fs_entries(new_active_editor, None, window, cx);
2966    }
2967
2968    fn clear_previous(&mut self, window: &mut Window, cx: &mut App) {
2969        self.fs_entries_update_task = Task::ready(());
2970        self.outline_fetch_tasks.clear();
2971        self.cached_entries_update_task = Task::ready(());
2972        self.reveal_selection_task = Task::ready(Ok(()));
2973        self.filter_editor
2974            .update(cx, |editor, cx| editor.clear(window, cx));
2975        self.collapsed_entries.clear();
2976        self.unfolded_dirs.clear();
2977        self.active_item = None;
2978        self.fs_entries.clear();
2979        self.fs_entries_depth.clear();
2980        self.fs_children_count.clear();
2981        self.excerpts.clear();
2982        self.cached_entries = Vec::new();
2983        self.selected_entry = SelectedEntry::None;
2984        self.pinned = false;
2985        self.mode = ItemsDisplayMode::Outline;
2986    }
2987
2988    fn location_for_editor_selection(
2989        &self,
2990        editor: &Entity<Editor>,
2991        window: &mut Window,
2992        cx: &mut Context<Self>,
2993    ) -> Option<PanelEntry> {
2994        let selection = editor.update(cx, |editor, cx| {
2995            editor.selections.newest::<language::Point>(cx).head()
2996        });
2997        let editor_snapshot = editor.update(cx, |editor, cx| editor.snapshot(window, cx));
2998        let multi_buffer = editor.read(cx).buffer();
2999        let multi_buffer_snapshot = multi_buffer.read(cx).snapshot(cx);
3000        let (excerpt_id, buffer, _) = editor
3001            .read(cx)
3002            .buffer()
3003            .read(cx)
3004            .excerpt_containing(selection, cx)?;
3005        let buffer_id = buffer.read(cx).remote_id();
3006
3007        if editor.read(cx).is_buffer_folded(buffer_id, cx) {
3008            return self
3009                .fs_entries
3010                .iter()
3011                .find(|fs_entry| match fs_entry {
3012                    FsEntry::Directory(..) => false,
3013                    FsEntry::File(FsEntryFile {
3014                        buffer_id: other_buffer_id,
3015                        ..
3016                    })
3017                    | FsEntry::ExternalFile(FsEntryExternalFile {
3018                        buffer_id: other_buffer_id,
3019                        ..
3020                    }) => buffer_id == *other_buffer_id,
3021                })
3022                .cloned()
3023                .map(PanelEntry::Fs);
3024        }
3025
3026        let selection_display_point = selection.to_display_point(&editor_snapshot);
3027
3028        match &self.mode {
3029            ItemsDisplayMode::Search(search_state) => search_state
3030                .matches
3031                .iter()
3032                .rev()
3033                .min_by_key(|&(match_range, _)| {
3034                    let match_display_range =
3035                        match_range.clone().to_display_points(&editor_snapshot);
3036                    let start_distance = if selection_display_point < match_display_range.start {
3037                        match_display_range.start - selection_display_point
3038                    } else {
3039                        selection_display_point - match_display_range.start
3040                    };
3041                    let end_distance = if selection_display_point < match_display_range.end {
3042                        match_display_range.end - selection_display_point
3043                    } else {
3044                        selection_display_point - match_display_range.end
3045                    };
3046                    start_distance + end_distance
3047                })
3048                .and_then(|(closest_range, _)| {
3049                    self.cached_entries.iter().find_map(|cached_entry| {
3050                        if let PanelEntry::Search(SearchEntry { match_range, .. }) =
3051                            &cached_entry.entry
3052                        {
3053                            if match_range == closest_range {
3054                                Some(cached_entry.entry.clone())
3055                            } else {
3056                                None
3057                            }
3058                        } else {
3059                            None
3060                        }
3061                    })
3062                }),
3063            ItemsDisplayMode::Outline => self.outline_location(
3064                buffer_id,
3065                excerpt_id,
3066                multi_buffer_snapshot,
3067                editor_snapshot,
3068                selection_display_point,
3069            ),
3070        }
3071    }
3072
3073    fn outline_location(
3074        &self,
3075        buffer_id: BufferId,
3076        excerpt_id: ExcerptId,
3077        multi_buffer_snapshot: editor::MultiBufferSnapshot,
3078        editor_snapshot: editor::EditorSnapshot,
3079        selection_display_point: DisplayPoint,
3080    ) -> Option<PanelEntry> {
3081        let excerpt_outlines = self
3082            .excerpts
3083            .get(&buffer_id)
3084            .and_then(|excerpts| excerpts.get(&excerpt_id))
3085            .into_iter()
3086            .flat_map(|excerpt| excerpt.iter_outlines())
3087            .flat_map(|outline| {
3088                let start = multi_buffer_snapshot
3089                    .anchor_in_excerpt(excerpt_id, outline.range.start)?
3090                    .to_display_point(&editor_snapshot);
3091                let end = multi_buffer_snapshot
3092                    .anchor_in_excerpt(excerpt_id, outline.range.end)?
3093                    .to_display_point(&editor_snapshot);
3094                Some((start..end, outline))
3095            })
3096            .collect::<Vec<_>>();
3097
3098        let mut matching_outline_indices = Vec::new();
3099        let mut children = HashMap::default();
3100        let mut parents_stack = Vec::<(&Range<DisplayPoint>, &&Outline, usize)>::new();
3101
3102        for (i, (outline_range, outline)) in excerpt_outlines.iter().enumerate() {
3103            if outline_range
3104                .to_inclusive()
3105                .contains(&selection_display_point)
3106            {
3107                matching_outline_indices.push(i);
3108            } else if (outline_range.start.row()..outline_range.end.row())
3109                .to_inclusive()
3110                .contains(&selection_display_point.row())
3111            {
3112                matching_outline_indices.push(i);
3113            }
3114
3115            while let Some((parent_range, parent_outline, _)) = parents_stack.last() {
3116                if parent_outline.depth >= outline.depth
3117                    || !parent_range.contains(&outline_range.start)
3118                {
3119                    parents_stack.pop();
3120                } else {
3121                    break;
3122                }
3123            }
3124            if let Some((_, _, parent_index)) = parents_stack.last_mut() {
3125                children
3126                    .entry(*parent_index)
3127                    .or_insert_with(Vec::new)
3128                    .push(i);
3129            }
3130            parents_stack.push((outline_range, outline, i));
3131        }
3132
3133        let outline_item = matching_outline_indices
3134            .into_iter()
3135            .flat_map(|i| Some((i, excerpt_outlines.get(i)?)))
3136            .filter(|(i, _)| {
3137                children
3138                    .get(i)
3139                    .map(|children| {
3140                        children.iter().all(|child_index| {
3141                            excerpt_outlines
3142                                .get(*child_index)
3143                                .map(|(child_range, _)| child_range.start > selection_display_point)
3144                                .unwrap_or(false)
3145                        })
3146                    })
3147                    .unwrap_or(true)
3148            })
3149            .min_by_key(|(_, (outline_range, outline))| {
3150                let distance_from_start = if outline_range.start > selection_display_point {
3151                    outline_range.start - selection_display_point
3152                } else {
3153                    selection_display_point - outline_range.start
3154                };
3155                let distance_from_end = if outline_range.end > selection_display_point {
3156                    outline_range.end - selection_display_point
3157                } else {
3158                    selection_display_point - outline_range.end
3159                };
3160
3161                (
3162                    cmp::Reverse(outline.depth),
3163                    distance_from_start + distance_from_end,
3164                )
3165            })
3166            .map(|(_, (_, outline))| *outline)
3167            .cloned();
3168
3169        let closest_container = match outline_item {
3170            Some(outline) => PanelEntry::Outline(OutlineEntry::Outline(OutlineEntryOutline {
3171                buffer_id,
3172                excerpt_id,
3173                outline,
3174            })),
3175            None => {
3176                self.cached_entries.iter().rev().find_map(|cached_entry| {
3177                    match &cached_entry.entry {
3178                        PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
3179                            if excerpt.buffer_id == buffer_id && excerpt.id == excerpt_id {
3180                                Some(cached_entry.entry.clone())
3181                            } else {
3182                                None
3183                            }
3184                        }
3185                        PanelEntry::Fs(
3186                            FsEntry::ExternalFile(FsEntryExternalFile {
3187                                buffer_id: file_buffer_id,
3188                                excerpts: file_excerpts,
3189                            })
3190                            | FsEntry::File(FsEntryFile {
3191                                buffer_id: file_buffer_id,
3192                                excerpts: file_excerpts,
3193                                ..
3194                            }),
3195                        ) => {
3196                            if file_buffer_id == &buffer_id && file_excerpts.contains(&excerpt_id) {
3197                                Some(cached_entry.entry.clone())
3198                            } else {
3199                                None
3200                            }
3201                        }
3202                        _ => None,
3203                    }
3204                })?
3205            }
3206        };
3207        Some(closest_container)
3208    }
3209
3210    fn fetch_outdated_outlines(&mut self, window: &mut Window, cx: &mut Context<Self>) {
3211        let excerpt_fetch_ranges = self.excerpt_fetch_ranges(cx);
3212        if excerpt_fetch_ranges.is_empty() {
3213            return;
3214        }
3215
3216        let syntax_theme = cx.theme().syntax().clone();
3217        let first_update = Arc::new(AtomicBool::new(true));
3218        for (buffer_id, (buffer_snapshot, excerpt_ranges)) in excerpt_fetch_ranges {
3219            for (excerpt_id, excerpt_range) in excerpt_ranges {
3220                let syntax_theme = syntax_theme.clone();
3221                let buffer_snapshot = buffer_snapshot.clone();
3222                let first_update = first_update.clone();
3223                self.outline_fetch_tasks.insert(
3224                    (buffer_id, excerpt_id),
3225                    cx.spawn_in(window, |outline_panel, mut cx| async move {
3226                        let fetched_outlines = cx
3227                            .background_spawn(async move {
3228                                buffer_snapshot
3229                                    .outline_items_containing(
3230                                        excerpt_range.context,
3231                                        false,
3232                                        Some(&syntax_theme),
3233                                    )
3234                                    .unwrap_or_default()
3235                            })
3236                            .await;
3237                        outline_panel
3238                            .update_in(&mut cx, |outline_panel, window, cx| {
3239                                if let Some(excerpt) = outline_panel
3240                                    .excerpts
3241                                    .entry(buffer_id)
3242                                    .or_default()
3243                                    .get_mut(&excerpt_id)
3244                                {
3245                                    let debounce = if first_update
3246                                        .fetch_and(false, atomic::Ordering::AcqRel)
3247                                    {
3248                                        None
3249                                    } else {
3250                                        Some(UPDATE_DEBOUNCE)
3251                                    };
3252                                    excerpt.outlines = ExcerptOutlines::Outlines(fetched_outlines);
3253                                    outline_panel.update_cached_entries(debounce, window, cx);
3254                                }
3255                            })
3256                            .ok();
3257                    }),
3258                );
3259            }
3260        }
3261    }
3262
3263    fn is_singleton_active(&self, cx: &App) -> bool {
3264        self.active_editor().map_or(false, |active_editor| {
3265            active_editor.read(cx).buffer().read(cx).is_singleton()
3266        })
3267    }
3268
3269    fn invalidate_outlines(&mut self, ids: &[ExcerptId]) {
3270        self.outline_fetch_tasks.clear();
3271        let mut ids = ids.iter().collect::<HashSet<_>>();
3272        for excerpts in self.excerpts.values_mut() {
3273            ids.retain(|id| {
3274                if let Some(excerpt) = excerpts.get_mut(id) {
3275                    excerpt.invalidate_outlines();
3276                    false
3277                } else {
3278                    true
3279                }
3280            });
3281            if ids.is_empty() {
3282                break;
3283            }
3284        }
3285    }
3286
3287    fn excerpt_fetch_ranges(
3288        &self,
3289        cx: &App,
3290    ) -> HashMap<
3291        BufferId,
3292        (
3293            BufferSnapshot,
3294            HashMap<ExcerptId, ExcerptRange<language::Anchor>>,
3295        ),
3296    > {
3297        self.fs_entries
3298            .iter()
3299            .fold(HashMap::default(), |mut excerpts_to_fetch, fs_entry| {
3300                match fs_entry {
3301                    FsEntry::File(FsEntryFile {
3302                        buffer_id,
3303                        excerpts: file_excerpts,
3304                        ..
3305                    })
3306                    | FsEntry::ExternalFile(FsEntryExternalFile {
3307                        buffer_id,
3308                        excerpts: file_excerpts,
3309                    }) => {
3310                        let excerpts = self.excerpts.get(buffer_id);
3311                        for &file_excerpt in file_excerpts {
3312                            if let Some(excerpt) = excerpts
3313                                .and_then(|excerpts| excerpts.get(&file_excerpt))
3314                                .filter(|excerpt| excerpt.should_fetch_outlines())
3315                            {
3316                                match excerpts_to_fetch.entry(*buffer_id) {
3317                                    hash_map::Entry::Occupied(mut o) => {
3318                                        o.get_mut().1.insert(file_excerpt, excerpt.range.clone());
3319                                    }
3320                                    hash_map::Entry::Vacant(v) => {
3321                                        if let Some(buffer_snapshot) =
3322                                            self.buffer_snapshot_for_id(*buffer_id, cx)
3323                                        {
3324                                            v.insert((buffer_snapshot, HashMap::default()))
3325                                                .1
3326                                                .insert(file_excerpt, excerpt.range.clone());
3327                                        }
3328                                    }
3329                                }
3330                            }
3331                        }
3332                    }
3333                    FsEntry::Directory(..) => {}
3334                }
3335                excerpts_to_fetch
3336            })
3337    }
3338
3339    fn buffer_snapshot_for_id(&self, buffer_id: BufferId, cx: &App) -> Option<BufferSnapshot> {
3340        let editor = self.active_editor()?;
3341        Some(
3342            editor
3343                .read(cx)
3344                .buffer()
3345                .read(cx)
3346                .buffer(buffer_id)?
3347                .read(cx)
3348                .snapshot(),
3349        )
3350    }
3351
3352    fn abs_path(&self, entry: &PanelEntry, cx: &App) -> Option<PathBuf> {
3353        match entry {
3354            PanelEntry::Fs(
3355                FsEntry::File(FsEntryFile { buffer_id, .. })
3356                | FsEntry::ExternalFile(FsEntryExternalFile { buffer_id, .. }),
3357            ) => self
3358                .buffer_snapshot_for_id(*buffer_id, cx)
3359                .and_then(|buffer_snapshot| {
3360                    let file = File::from_dyn(buffer_snapshot.file())?;
3361                    file.worktree.read(cx).absolutize(&file.path).ok()
3362                }),
3363            PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
3364                worktree_id, entry, ..
3365            })) => self
3366                .project
3367                .read(cx)
3368                .worktree_for_id(*worktree_id, cx)?
3369                .read(cx)
3370                .absolutize(&entry.path)
3371                .ok(),
3372            PanelEntry::FoldedDirs(FoldedDirsEntry {
3373                worktree_id,
3374                entries: dirs,
3375                ..
3376            }) => dirs.last().and_then(|entry| {
3377                self.project
3378                    .read(cx)
3379                    .worktree_for_id(*worktree_id, cx)
3380                    .and_then(|worktree| worktree.read(cx).absolutize(&entry.path).ok())
3381            }),
3382            PanelEntry::Search(_) | PanelEntry::Outline(..) => None,
3383        }
3384    }
3385
3386    fn relative_path(&self, entry: &FsEntry, cx: &App) -> Option<Arc<Path>> {
3387        match entry {
3388            FsEntry::ExternalFile(FsEntryExternalFile { buffer_id, .. }) => {
3389                let buffer_snapshot = self.buffer_snapshot_for_id(*buffer_id, cx)?;
3390                Some(buffer_snapshot.file()?.path().clone())
3391            }
3392            FsEntry::Directory(FsEntryDirectory { entry, .. }) => Some(entry.path.clone()),
3393            FsEntry::File(FsEntryFile { entry, .. }) => Some(entry.path.clone()),
3394        }
3395    }
3396
3397    fn update_cached_entries(
3398        &mut self,
3399        debounce: Option<Duration>,
3400        window: &mut Window,
3401        cx: &mut Context<OutlinePanel>,
3402    ) {
3403        if !self.active {
3404            return;
3405        }
3406
3407        let is_singleton = self.is_singleton_active(cx);
3408        let query = self.query(cx);
3409        self.updating_cached_entries = true;
3410        self.cached_entries_update_task = cx.spawn_in(window, |outline_panel, mut cx| async move {
3411            if let Some(debounce) = debounce {
3412                cx.background_executor().timer(debounce).await;
3413            }
3414            let Some(new_cached_entries) = outline_panel
3415                .update_in(&mut cx, |outline_panel, window, cx| {
3416                    outline_panel.generate_cached_entries(is_singleton, query, window, cx)
3417                })
3418                .ok()
3419            else {
3420                return;
3421            };
3422            let (new_cached_entries, max_width_item_index) = new_cached_entries.await;
3423            outline_panel
3424                .update_in(&mut cx, |outline_panel, window, cx| {
3425                    outline_panel.cached_entries = new_cached_entries;
3426                    outline_panel.max_width_item_index = max_width_item_index;
3427                    if outline_panel.selected_entry.is_invalidated()
3428                        || matches!(outline_panel.selected_entry, SelectedEntry::None)
3429                    {
3430                        if let Some(new_selected_entry) =
3431                            outline_panel.active_editor().and_then(|active_editor| {
3432                                outline_panel.location_for_editor_selection(
3433                                    &active_editor,
3434                                    window,
3435                                    cx,
3436                                )
3437                            })
3438                        {
3439                            outline_panel.select_entry(new_selected_entry, false, window, cx);
3440                        }
3441                    }
3442
3443                    outline_panel.autoscroll(cx);
3444                    outline_panel.updating_cached_entries = false;
3445                    cx.notify();
3446                })
3447                .ok();
3448        });
3449    }
3450
3451    fn generate_cached_entries(
3452        &self,
3453        is_singleton: bool,
3454        query: Option<String>,
3455        window: &mut Window,
3456        cx: &mut Context<Self>,
3457    ) -> Task<(Vec<CachedEntry>, Option<usize>)> {
3458        let project = self.project.clone();
3459        let Some(active_editor) = self.active_editor() else {
3460            return Task::ready((Vec::new(), None));
3461        };
3462        cx.spawn_in(window, |outline_panel, mut cx| async move {
3463            let mut generation_state = GenerationState::default();
3464
3465            let Ok(()) = outline_panel.update(&mut cx, |outline_panel, cx| {
3466                let auto_fold_dirs = OutlinePanelSettings::get_global(cx).auto_fold_dirs;
3467                let mut folded_dirs_entry = None::<(usize, FoldedDirsEntry)>;
3468                let track_matches = query.is_some();
3469
3470                #[derive(Debug)]
3471                struct ParentStats {
3472                    path: Arc<Path>,
3473                    folded: bool,
3474                    expanded: bool,
3475                    depth: usize,
3476                }
3477                let mut parent_dirs = Vec::<ParentStats>::new();
3478                for entry in outline_panel.fs_entries.clone() {
3479                    let is_expanded = outline_panel.is_expanded(&entry);
3480                    let (depth, should_add) = match &entry {
3481                        FsEntry::Directory(directory_entry) => {
3482                            let mut should_add = true;
3483                            let is_root = project
3484                                .read(cx)
3485                                .worktree_for_id(directory_entry.worktree_id, cx)
3486                                .map_or(false, |worktree| {
3487                                    worktree.read(cx).root_entry() == Some(&directory_entry.entry)
3488                                });
3489                            let folded = auto_fold_dirs
3490                                && !is_root
3491                                && outline_panel
3492                                    .unfolded_dirs
3493                                    .get(&directory_entry.worktree_id)
3494                                    .map_or(true, |unfolded_dirs| {
3495                                        !unfolded_dirs.contains(&directory_entry.entry.id)
3496                                    });
3497                            let fs_depth = outline_panel
3498                                .fs_entries_depth
3499                                .get(&(directory_entry.worktree_id, directory_entry.entry.id))
3500                                .copied()
3501                                .unwrap_or(0);
3502                            while let Some(parent) = parent_dirs.last() {
3503                                if !is_root && directory_entry.entry.path.starts_with(&parent.path)
3504                                {
3505                                    break;
3506                                }
3507                                parent_dirs.pop();
3508                            }
3509                            let auto_fold = match parent_dirs.last() {
3510                                Some(parent) => {
3511                                    parent.folded
3512                                        && Some(parent.path.as_ref())
3513                                            == directory_entry.entry.path.parent()
3514                                        && outline_panel
3515                                            .fs_children_count
3516                                            .get(&directory_entry.worktree_id)
3517                                            .and_then(|entries| {
3518                                                entries.get(&directory_entry.entry.path)
3519                                            })
3520                                            .copied()
3521                                            .unwrap_or_default()
3522                                            .may_be_fold_part()
3523                                }
3524                                None => false,
3525                            };
3526                            let folded = folded || auto_fold;
3527                            let (depth, parent_expanded, parent_folded) = match parent_dirs.last() {
3528                                Some(parent) => {
3529                                    let parent_folded = parent.folded;
3530                                    let parent_expanded = parent.expanded;
3531                                    let new_depth = if parent_folded {
3532                                        parent.depth
3533                                    } else {
3534                                        parent.depth + 1
3535                                    };
3536                                    parent_dirs.push(ParentStats {
3537                                        path: directory_entry.entry.path.clone(),
3538                                        folded,
3539                                        expanded: parent_expanded && is_expanded,
3540                                        depth: new_depth,
3541                                    });
3542                                    (new_depth, parent_expanded, parent_folded)
3543                                }
3544                                None => {
3545                                    parent_dirs.push(ParentStats {
3546                                        path: directory_entry.entry.path.clone(),
3547                                        folded,
3548                                        expanded: is_expanded,
3549                                        depth: fs_depth,
3550                                    });
3551                                    (fs_depth, true, false)
3552                                }
3553                            };
3554
3555                            if let Some((folded_depth, mut folded_dirs)) = folded_dirs_entry.take()
3556                            {
3557                                if folded
3558                                    && directory_entry.worktree_id == folded_dirs.worktree_id
3559                                    && directory_entry.entry.path.parent()
3560                                        == folded_dirs
3561                                            .entries
3562                                            .last()
3563                                            .map(|entry| entry.path.as_ref())
3564                                {
3565                                    folded_dirs.entries.push(directory_entry.entry.clone());
3566                                    folded_dirs_entry = Some((folded_depth, folded_dirs))
3567                                } else {
3568                                    if !is_singleton {
3569                                        let start_of_collapsed_dir_sequence = !parent_expanded
3570                                            && parent_dirs
3571                                                .iter()
3572                                                .rev()
3573                                                .nth(folded_dirs.entries.len() + 1)
3574                                                .map_or(true, |parent| parent.expanded);
3575                                        if start_of_collapsed_dir_sequence
3576                                            || parent_expanded
3577                                            || query.is_some()
3578                                        {
3579                                            if parent_folded {
3580                                                folded_dirs
3581                                                    .entries
3582                                                    .push(directory_entry.entry.clone());
3583                                                should_add = false;
3584                                            }
3585                                            let new_folded_dirs =
3586                                                PanelEntry::FoldedDirs(folded_dirs.clone());
3587                                            outline_panel.push_entry(
3588                                                &mut generation_state,
3589                                                track_matches,
3590                                                new_folded_dirs,
3591                                                folded_depth,
3592                                                cx,
3593                                            );
3594                                        }
3595                                    }
3596
3597                                    folded_dirs_entry = if parent_folded {
3598                                        None
3599                                    } else {
3600                                        Some((
3601                                            depth,
3602                                            FoldedDirsEntry {
3603                                                worktree_id: directory_entry.worktree_id,
3604                                                entries: vec![directory_entry.entry.clone()],
3605                                            },
3606                                        ))
3607                                    };
3608                                }
3609                            } else if folded {
3610                                folded_dirs_entry = Some((
3611                                    depth,
3612                                    FoldedDirsEntry {
3613                                        worktree_id: directory_entry.worktree_id,
3614                                        entries: vec![directory_entry.entry.clone()],
3615                                    },
3616                                ));
3617                            }
3618
3619                            let should_add =
3620                                should_add && parent_expanded && folded_dirs_entry.is_none();
3621                            (depth, should_add)
3622                        }
3623                        FsEntry::ExternalFile(..) => {
3624                            if let Some((folded_depth, folded_dir)) = folded_dirs_entry.take() {
3625                                let parent_expanded = parent_dirs
3626                                    .iter()
3627                                    .rev()
3628                                    .find(|parent| {
3629                                        folded_dir
3630                                            .entries
3631                                            .iter()
3632                                            .all(|entry| entry.path != parent.path)
3633                                    })
3634                                    .map_or(true, |parent| parent.expanded);
3635                                if !is_singleton && (parent_expanded || query.is_some()) {
3636                                    outline_panel.push_entry(
3637                                        &mut generation_state,
3638                                        track_matches,
3639                                        PanelEntry::FoldedDirs(folded_dir),
3640                                        folded_depth,
3641                                        cx,
3642                                    );
3643                                }
3644                            }
3645                            parent_dirs.clear();
3646                            (0, true)
3647                        }
3648                        FsEntry::File(file) => {
3649                            if let Some((folded_depth, folded_dirs)) = folded_dirs_entry.take() {
3650                                let parent_expanded = parent_dirs
3651                                    .iter()
3652                                    .rev()
3653                                    .find(|parent| {
3654                                        folded_dirs
3655                                            .entries
3656                                            .iter()
3657                                            .all(|entry| entry.path != parent.path)
3658                                    })
3659                                    .map_or(true, |parent| parent.expanded);
3660                                if !is_singleton && (parent_expanded || query.is_some()) {
3661                                    outline_panel.push_entry(
3662                                        &mut generation_state,
3663                                        track_matches,
3664                                        PanelEntry::FoldedDirs(folded_dirs),
3665                                        folded_depth,
3666                                        cx,
3667                                    );
3668                                }
3669                            }
3670
3671                            let fs_depth = outline_panel
3672                                .fs_entries_depth
3673                                .get(&(file.worktree_id, file.entry.id))
3674                                .copied()
3675                                .unwrap_or(0);
3676                            while let Some(parent) = parent_dirs.last() {
3677                                if file.entry.path.starts_with(&parent.path) {
3678                                    break;
3679                                }
3680                                parent_dirs.pop();
3681                            }
3682                            match parent_dirs.last() {
3683                                Some(parent) => {
3684                                    let new_depth = parent.depth + 1;
3685                                    (new_depth, parent.expanded)
3686                                }
3687                                None => (fs_depth, true),
3688                            }
3689                        }
3690                    };
3691
3692                    if !is_singleton
3693                        && (should_add || (query.is_some() && folded_dirs_entry.is_none()))
3694                    {
3695                        outline_panel.push_entry(
3696                            &mut generation_state,
3697                            track_matches,
3698                            PanelEntry::Fs(entry.clone()),
3699                            depth,
3700                            cx,
3701                        );
3702                    }
3703
3704                    match outline_panel.mode {
3705                        ItemsDisplayMode::Search(_) => {
3706                            if is_singleton || query.is_some() || (should_add && is_expanded) {
3707                                outline_panel.add_search_entries(
3708                                    &mut generation_state,
3709                                    &active_editor,
3710                                    entry.clone(),
3711                                    depth,
3712                                    query.clone(),
3713                                    is_singleton,
3714                                    cx,
3715                                );
3716                            }
3717                        }
3718                        ItemsDisplayMode::Outline => {
3719                            let excerpts_to_consider =
3720                                if is_singleton || query.is_some() || (should_add && is_expanded) {
3721                                    match &entry {
3722                                        FsEntry::File(FsEntryFile {
3723                                            buffer_id,
3724                                            excerpts,
3725                                            ..
3726                                        })
3727                                        | FsEntry::ExternalFile(FsEntryExternalFile {
3728                                            buffer_id,
3729                                            excerpts,
3730                                            ..
3731                                        }) => Some((*buffer_id, excerpts)),
3732                                        _ => None,
3733                                    }
3734                                } else {
3735                                    None
3736                                };
3737                            if let Some((buffer_id, entry_excerpts)) = excerpts_to_consider {
3738                                if !active_editor.read(cx).is_buffer_folded(buffer_id, cx) {
3739                                    outline_panel.add_excerpt_entries(
3740                                        &mut generation_state,
3741                                        buffer_id,
3742                                        entry_excerpts,
3743                                        depth,
3744                                        track_matches,
3745                                        is_singleton,
3746                                        query.as_deref(),
3747                                        cx,
3748                                    );
3749                                }
3750                            }
3751                        }
3752                    }
3753
3754                    if is_singleton
3755                        && matches!(entry, FsEntry::File(..) | FsEntry::ExternalFile(..))
3756                        && !generation_state.entries.iter().any(|item| {
3757                            matches!(item.entry, PanelEntry::Outline(..) | PanelEntry::Search(_))
3758                        })
3759                    {
3760                        outline_panel.push_entry(
3761                            &mut generation_state,
3762                            track_matches,
3763                            PanelEntry::Fs(entry.clone()),
3764                            0,
3765                            cx,
3766                        );
3767                    }
3768                }
3769
3770                if let Some((folded_depth, folded_dirs)) = folded_dirs_entry.take() {
3771                    let parent_expanded = parent_dirs
3772                        .iter()
3773                        .rev()
3774                        .find(|parent| {
3775                            folded_dirs
3776                                .entries
3777                                .iter()
3778                                .all(|entry| entry.path != parent.path)
3779                        })
3780                        .map_or(true, |parent| parent.expanded);
3781                    if parent_expanded || query.is_some() {
3782                        outline_panel.push_entry(
3783                            &mut generation_state,
3784                            track_matches,
3785                            PanelEntry::FoldedDirs(folded_dirs),
3786                            folded_depth,
3787                            cx,
3788                        );
3789                    }
3790                }
3791            }) else {
3792                return (Vec::new(), None);
3793            };
3794
3795            let Some(query) = query else {
3796                return (
3797                    generation_state.entries,
3798                    generation_state
3799                        .max_width_estimate_and_index
3800                        .map(|(_, index)| index),
3801                );
3802            };
3803
3804            let mut matched_ids = match_strings(
3805                &generation_state.match_candidates,
3806                &query,
3807                true,
3808                usize::MAX,
3809                &AtomicBool::default(),
3810                cx.background_executor().clone(),
3811            )
3812            .await
3813            .into_iter()
3814            .map(|string_match| (string_match.candidate_id, string_match))
3815            .collect::<HashMap<_, _>>();
3816
3817            let mut id = 0;
3818            generation_state.entries.retain_mut(|cached_entry| {
3819                let retain = match matched_ids.remove(&id) {
3820                    Some(string_match) => {
3821                        cached_entry.string_match = Some(string_match);
3822                        true
3823                    }
3824                    None => false,
3825                };
3826                id += 1;
3827                retain
3828            });
3829
3830            (
3831                generation_state.entries,
3832                generation_state
3833                    .max_width_estimate_and_index
3834                    .map(|(_, index)| index),
3835            )
3836        })
3837    }
3838
3839    #[allow(clippy::too_many_arguments)]
3840    fn push_entry(
3841        &self,
3842        state: &mut GenerationState,
3843        track_matches: bool,
3844        entry: PanelEntry,
3845        depth: usize,
3846        cx: &mut App,
3847    ) {
3848        let entry = if let PanelEntry::FoldedDirs(folded_dirs_entry) = &entry {
3849            match folded_dirs_entry.entries.len() {
3850                0 => {
3851                    debug_panic!("Empty folded dirs receiver");
3852                    return;
3853                }
3854                1 => PanelEntry::Fs(FsEntry::Directory(FsEntryDirectory {
3855                    worktree_id: folded_dirs_entry.worktree_id,
3856                    entry: folded_dirs_entry.entries[0].clone(),
3857                })),
3858                _ => entry,
3859            }
3860        } else {
3861            entry
3862        };
3863
3864        if track_matches {
3865            let id = state.entries.len();
3866            match &entry {
3867                PanelEntry::Fs(fs_entry) => {
3868                    if let Some(file_name) =
3869                        self.relative_path(fs_entry, cx).as_deref().map(file_name)
3870                    {
3871                        state
3872                            .match_candidates
3873                            .push(StringMatchCandidate::new(id, &file_name));
3874                    }
3875                }
3876                PanelEntry::FoldedDirs(folded_dir_entry) => {
3877                    let dir_names = self.dir_names_string(
3878                        &folded_dir_entry.entries,
3879                        folded_dir_entry.worktree_id,
3880                        cx,
3881                    );
3882                    {
3883                        state
3884                            .match_candidates
3885                            .push(StringMatchCandidate::new(id, &dir_names));
3886                    }
3887                }
3888                PanelEntry::Outline(OutlineEntry::Outline(outline_entry)) => state
3889                    .match_candidates
3890                    .push(StringMatchCandidate::new(id, &outline_entry.outline.text)),
3891                PanelEntry::Outline(OutlineEntry::Excerpt(_)) => {}
3892                PanelEntry::Search(new_search_entry) => {
3893                    if let Some(search_data) = new_search_entry.render_data.get() {
3894                        state
3895                            .match_candidates
3896                            .push(StringMatchCandidate::new(id, &search_data.context_text));
3897                    }
3898                }
3899            }
3900        }
3901
3902        let width_estimate = self.width_estimate(depth, &entry, cx);
3903        if Some(width_estimate)
3904            > state
3905                .max_width_estimate_and_index
3906                .map(|(estimate, _)| estimate)
3907        {
3908            state.max_width_estimate_and_index = Some((width_estimate, state.entries.len()));
3909        }
3910        state.entries.push(CachedEntry {
3911            depth,
3912            entry,
3913            string_match: None,
3914        });
3915    }
3916
3917    fn dir_names_string(&self, entries: &[GitEntry], worktree_id: WorktreeId, cx: &App) -> String {
3918        let dir_names_segment = entries
3919            .iter()
3920            .map(|entry| self.entry_name(&worktree_id, entry, cx))
3921            .collect::<PathBuf>();
3922        dir_names_segment.to_string_lossy().to_string()
3923    }
3924
3925    fn query(&self, cx: &App) -> Option<String> {
3926        let query = self.filter_editor.read(cx).text(cx);
3927        if query.trim().is_empty() {
3928            None
3929        } else {
3930            Some(query)
3931        }
3932    }
3933
3934    fn is_expanded(&self, entry: &FsEntry) -> bool {
3935        let entry_to_check = match entry {
3936            FsEntry::ExternalFile(FsEntryExternalFile { buffer_id, .. }) => {
3937                CollapsedEntry::ExternalFile(*buffer_id)
3938            }
3939            FsEntry::File(FsEntryFile {
3940                worktree_id,
3941                buffer_id,
3942                ..
3943            }) => CollapsedEntry::File(*worktree_id, *buffer_id),
3944            FsEntry::Directory(FsEntryDirectory {
3945                worktree_id, entry, ..
3946            }) => CollapsedEntry::Dir(*worktree_id, entry.id),
3947        };
3948        !self.collapsed_entries.contains(&entry_to_check)
3949    }
3950
3951    fn update_non_fs_items(&mut self, window: &mut Window, cx: &mut Context<OutlinePanel>) -> bool {
3952        if !self.active {
3953            return false;
3954        }
3955
3956        let mut update_cached_items = false;
3957        update_cached_items |= self.update_search_matches(window, cx);
3958        self.fetch_outdated_outlines(window, cx);
3959        if update_cached_items {
3960            self.selected_entry.invalidate();
3961        }
3962        update_cached_items
3963    }
3964
3965    fn update_search_matches(
3966        &mut self,
3967        window: &mut Window,
3968        cx: &mut Context<OutlinePanel>,
3969    ) -> bool {
3970        if !self.active {
3971            return false;
3972        }
3973
3974        let project_search = self
3975            .active_item()
3976            .and_then(|item| item.downcast::<ProjectSearchView>());
3977        let project_search_matches = project_search
3978            .as_ref()
3979            .map(|project_search| project_search.read(cx).get_matches(cx))
3980            .unwrap_or_default();
3981
3982        let buffer_search = self
3983            .active_item()
3984            .as_deref()
3985            .and_then(|active_item| {
3986                self.workspace
3987                    .upgrade()
3988                    .and_then(|workspace| workspace.read(cx).pane_for(active_item))
3989            })
3990            .and_then(|pane| {
3991                pane.read(cx)
3992                    .toolbar()
3993                    .read(cx)
3994                    .item_of_type::<BufferSearchBar>()
3995            });
3996        let buffer_search_matches = self
3997            .active_editor()
3998            .map(|active_editor| {
3999                active_editor.update(cx, |editor, cx| editor.get_matches(window, cx))
4000            })
4001            .unwrap_or_default();
4002
4003        let mut update_cached_entries = false;
4004        if buffer_search_matches.is_empty() && project_search_matches.is_empty() {
4005            if matches!(self.mode, ItemsDisplayMode::Search(_)) {
4006                self.mode = ItemsDisplayMode::Outline;
4007                update_cached_entries = true;
4008            }
4009        } else {
4010            let (kind, new_search_matches, new_search_query) = if buffer_search_matches.is_empty() {
4011                (
4012                    SearchKind::Project,
4013                    project_search_matches,
4014                    project_search
4015                        .map(|project_search| project_search.read(cx).search_query_text(cx))
4016                        .unwrap_or_default(),
4017                )
4018            } else {
4019                (
4020                    SearchKind::Buffer,
4021                    buffer_search_matches,
4022                    buffer_search
4023                        .map(|buffer_search| buffer_search.read(cx).query(cx))
4024                        .unwrap_or_default(),
4025                )
4026            };
4027
4028            let mut previous_matches = HashMap::default();
4029            update_cached_entries = match &mut self.mode {
4030                ItemsDisplayMode::Search(current_search_state) => {
4031                    let update = current_search_state.query != new_search_query
4032                        || current_search_state.kind != kind
4033                        || current_search_state.matches.is_empty()
4034                        || current_search_state.matches.iter().enumerate().any(
4035                            |(i, (match_range, _))| new_search_matches.get(i) != Some(match_range),
4036                        );
4037                    if current_search_state.kind == kind {
4038                        previous_matches.extend(current_search_state.matches.drain(..));
4039                    }
4040                    update
4041                }
4042                ItemsDisplayMode::Outline => true,
4043            };
4044            self.mode = ItemsDisplayMode::Search(SearchState::new(
4045                kind,
4046                new_search_query,
4047                previous_matches,
4048                new_search_matches,
4049                cx.theme().syntax().clone(),
4050                window,
4051                cx,
4052            ));
4053        }
4054        update_cached_entries
4055    }
4056
4057    #[allow(clippy::too_many_arguments)]
4058    fn add_excerpt_entries(
4059        &self,
4060        state: &mut GenerationState,
4061        buffer_id: BufferId,
4062        entries_to_add: &[ExcerptId],
4063        parent_depth: usize,
4064        track_matches: bool,
4065        is_singleton: bool,
4066        query: Option<&str>,
4067        cx: &mut Context<Self>,
4068    ) {
4069        if let Some(excerpts) = self.excerpts.get(&buffer_id) {
4070            for &excerpt_id in entries_to_add {
4071                let Some(excerpt) = excerpts.get(&excerpt_id) else {
4072                    continue;
4073                };
4074                let excerpt_depth = parent_depth + 1;
4075                self.push_entry(
4076                    state,
4077                    track_matches,
4078                    PanelEntry::Outline(OutlineEntry::Excerpt(OutlineEntryExcerpt {
4079                        buffer_id,
4080                        id: excerpt_id,
4081                        range: excerpt.range.clone(),
4082                    })),
4083                    excerpt_depth,
4084                    cx,
4085                );
4086
4087                let mut outline_base_depth = excerpt_depth + 1;
4088                if is_singleton {
4089                    outline_base_depth = 0;
4090                    state.clear();
4091                } else if query.is_none()
4092                    && self
4093                        .collapsed_entries
4094                        .contains(&CollapsedEntry::Excerpt(buffer_id, excerpt_id))
4095                {
4096                    continue;
4097                }
4098
4099                for outline in excerpt.iter_outlines() {
4100                    self.push_entry(
4101                        state,
4102                        track_matches,
4103                        PanelEntry::Outline(OutlineEntry::Outline(OutlineEntryOutline {
4104                            buffer_id,
4105                            excerpt_id,
4106                            outline: outline.clone(),
4107                        })),
4108                        outline_base_depth + outline.depth,
4109                        cx,
4110                    );
4111                }
4112            }
4113        }
4114    }
4115
4116    #[allow(clippy::too_many_arguments)]
4117    fn add_search_entries(
4118        &mut self,
4119        state: &mut GenerationState,
4120        active_editor: &Entity<Editor>,
4121        parent_entry: FsEntry,
4122        parent_depth: usize,
4123        filter_query: Option<String>,
4124        is_singleton: bool,
4125        cx: &mut Context<Self>,
4126    ) {
4127        let ItemsDisplayMode::Search(search_state) = &mut self.mode else {
4128            return;
4129        };
4130
4131        let kind = search_state.kind;
4132        let related_excerpts = match &parent_entry {
4133            FsEntry::Directory(_) => return,
4134            FsEntry::ExternalFile(external) => &external.excerpts,
4135            FsEntry::File(file) => &file.excerpts,
4136        }
4137        .iter()
4138        .copied()
4139        .collect::<HashSet<_>>();
4140
4141        let depth = if is_singleton { 0 } else { parent_depth + 1 };
4142        let new_search_matches = search_state
4143            .matches
4144            .iter()
4145            .filter(|(match_range, _)| {
4146                related_excerpts.contains(&match_range.start.excerpt_id)
4147                    || related_excerpts.contains(&match_range.end.excerpt_id)
4148            })
4149            .filter(|(match_range, _)| {
4150                let editor = active_editor.read(cx);
4151                if let Some(buffer_id) = match_range.start.buffer_id {
4152                    if editor.is_buffer_folded(buffer_id, cx) {
4153                        return false;
4154                    }
4155                }
4156                if let Some(buffer_id) = match_range.start.buffer_id {
4157                    if editor.is_buffer_folded(buffer_id, cx) {
4158                        return false;
4159                    }
4160                }
4161                true
4162            });
4163
4164        let new_search_entries = new_search_matches
4165            .map(|(match_range, search_data)| SearchEntry {
4166                match_range: match_range.clone(),
4167                kind,
4168                render_data: Arc::clone(search_data),
4169            })
4170            .collect::<Vec<_>>();
4171        for new_search_entry in new_search_entries {
4172            self.push_entry(
4173                state,
4174                filter_query.is_some(),
4175                PanelEntry::Search(new_search_entry),
4176                depth,
4177                cx,
4178            );
4179        }
4180    }
4181
4182    fn active_editor(&self) -> Option<Entity<Editor>> {
4183        self.active_item.as_ref()?.active_editor.upgrade()
4184    }
4185
4186    fn active_item(&self) -> Option<Box<dyn ItemHandle>> {
4187        self.active_item.as_ref()?.item_handle.upgrade()
4188    }
4189
4190    fn should_replace_active_item(&self, new_active_item: &dyn ItemHandle) -> bool {
4191        self.active_item().map_or(true, |active_item| {
4192            !self.pinned && active_item.item_id() != new_active_item.item_id()
4193        })
4194    }
4195
4196    pub fn toggle_active_editor_pin(
4197        &mut self,
4198        _: &ToggleActiveEditorPin,
4199        window: &mut Window,
4200        cx: &mut Context<Self>,
4201    ) {
4202        self.pinned = !self.pinned;
4203        if !self.pinned {
4204            if let Some((active_item, active_editor)) = self
4205                .workspace
4206                .upgrade()
4207                .and_then(|workspace| workspace_active_editor(workspace.read(cx), cx))
4208            {
4209                if self.should_replace_active_item(active_item.as_ref()) {
4210                    self.replace_active_editor(active_item, active_editor, window, cx);
4211                }
4212            }
4213        }
4214
4215        cx.notify();
4216    }
4217
4218    fn selected_entry(&self) -> Option<&PanelEntry> {
4219        match &self.selected_entry {
4220            SelectedEntry::Invalidated(entry) => entry.as_ref(),
4221            SelectedEntry::Valid(entry, _) => Some(entry),
4222            SelectedEntry::None => None,
4223        }
4224    }
4225
4226    fn select_entry(
4227        &mut self,
4228        entry: PanelEntry,
4229        focus: bool,
4230        window: &mut Window,
4231        cx: &mut Context<Self>,
4232    ) {
4233        if focus {
4234            self.focus_handle.focus(window);
4235        }
4236        let ix = self
4237            .cached_entries
4238            .iter()
4239            .enumerate()
4240            .find(|(_, cached_entry)| &cached_entry.entry == &entry)
4241            .map(|(i, _)| i)
4242            .unwrap_or_default();
4243
4244        self.selected_entry = SelectedEntry::Valid(entry, ix);
4245
4246        self.autoscroll(cx);
4247        cx.notify();
4248    }
4249
4250    fn render_vertical_scrollbar(&self, cx: &mut Context<Self>) -> Option<Stateful<Div>> {
4251        if !Self::should_show_scrollbar(cx)
4252            || !(self.show_scrollbar || self.vertical_scrollbar_state.is_dragging())
4253        {
4254            return None;
4255        }
4256        Some(
4257            div()
4258                .occlude()
4259                .id("project-panel-vertical-scroll")
4260                .on_mouse_move(cx.listener(|_, _, _, cx| {
4261                    cx.notify();
4262                    cx.stop_propagation()
4263                }))
4264                .on_hover(|_, _, cx| {
4265                    cx.stop_propagation();
4266                })
4267                .on_any_mouse_down(|_, _, cx| {
4268                    cx.stop_propagation();
4269                })
4270                .on_mouse_up(
4271                    MouseButton::Left,
4272                    cx.listener(|outline_panel, _, window, cx| {
4273                        if !outline_panel.vertical_scrollbar_state.is_dragging()
4274                            && !outline_panel.focus_handle.contains_focused(window, cx)
4275                        {
4276                            outline_panel.hide_scrollbar(window, cx);
4277                            cx.notify();
4278                        }
4279
4280                        cx.stop_propagation();
4281                    }),
4282                )
4283                .on_scroll_wheel(cx.listener(|_, _, _, cx| {
4284                    cx.notify();
4285                }))
4286                .h_full()
4287                .absolute()
4288                .right_1()
4289                .top_1()
4290                .bottom_0()
4291                .w(px(12.))
4292                .cursor_default()
4293                .children(Scrollbar::vertical(self.vertical_scrollbar_state.clone())),
4294        )
4295    }
4296
4297    fn render_horizontal_scrollbar(
4298        &self,
4299        _: &mut Window,
4300        cx: &mut Context<Self>,
4301    ) -> Option<Stateful<Div>> {
4302        if !Self::should_show_scrollbar(cx)
4303            || !(self.show_scrollbar || self.horizontal_scrollbar_state.is_dragging())
4304        {
4305            return None;
4306        }
4307
4308        let scroll_handle = self.scroll_handle.0.borrow();
4309        let longest_item_width = scroll_handle
4310            .last_item_size
4311            .filter(|size| size.contents.width > size.item.width)?
4312            .contents
4313            .width
4314            .0 as f64;
4315        if longest_item_width < scroll_handle.base_handle.bounds().size.width.0 as f64 {
4316            return None;
4317        }
4318
4319        Some(
4320            div()
4321                .occlude()
4322                .id("project-panel-horizontal-scroll")
4323                .on_mouse_move(cx.listener(|_, _, _, cx| {
4324                    cx.notify();
4325                    cx.stop_propagation()
4326                }))
4327                .on_hover(|_, _, cx| {
4328                    cx.stop_propagation();
4329                })
4330                .on_any_mouse_down(|_, _, cx| {
4331                    cx.stop_propagation();
4332                })
4333                .on_mouse_up(
4334                    MouseButton::Left,
4335                    cx.listener(|outline_panel, _, window, cx| {
4336                        if !outline_panel.horizontal_scrollbar_state.is_dragging()
4337                            && !outline_panel.focus_handle.contains_focused(window, cx)
4338                        {
4339                            outline_panel.hide_scrollbar(window, cx);
4340                            cx.notify();
4341                        }
4342
4343                        cx.stop_propagation();
4344                    }),
4345                )
4346                .on_scroll_wheel(cx.listener(|_, _, _, cx| {
4347                    cx.notify();
4348                }))
4349                .w_full()
4350                .absolute()
4351                .right_1()
4352                .left_1()
4353                .bottom_0()
4354                .h(px(12.))
4355                .cursor_default()
4356                .when(self.width.is_some(), |this| {
4357                    this.children(Scrollbar::horizontal(
4358                        self.horizontal_scrollbar_state.clone(),
4359                    ))
4360                }),
4361        )
4362    }
4363
4364    fn should_show_scrollbar(cx: &App) -> bool {
4365        let show = OutlinePanelSettings::get_global(cx)
4366            .scrollbar
4367            .show
4368            .unwrap_or_else(|| EditorSettings::get_global(cx).scrollbar.show);
4369        match show {
4370            ShowScrollbar::Auto => true,
4371            ShowScrollbar::System => true,
4372            ShowScrollbar::Always => true,
4373            ShowScrollbar::Never => false,
4374        }
4375    }
4376
4377    fn should_autohide_scrollbar(cx: &App) -> bool {
4378        let show = OutlinePanelSettings::get_global(cx)
4379            .scrollbar
4380            .show
4381            .unwrap_or_else(|| EditorSettings::get_global(cx).scrollbar.show);
4382        match show {
4383            ShowScrollbar::Auto => true,
4384            ShowScrollbar::System => cx
4385                .try_global::<ScrollbarAutoHide>()
4386                .map_or_else(|| cx.should_auto_hide_scrollbars(), |autohide| autohide.0),
4387            ShowScrollbar::Always => false,
4388            ShowScrollbar::Never => true,
4389        }
4390    }
4391
4392    fn hide_scrollbar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
4393        const SCROLLBAR_SHOW_INTERVAL: Duration = Duration::from_secs(1);
4394        if !Self::should_autohide_scrollbar(cx) {
4395            return;
4396        }
4397        self.hide_scrollbar_task = Some(cx.spawn_in(window, |panel, mut cx| async move {
4398            cx.background_executor()
4399                .timer(SCROLLBAR_SHOW_INTERVAL)
4400                .await;
4401            panel
4402                .update(&mut cx, |panel, cx| {
4403                    panel.show_scrollbar = false;
4404                    cx.notify();
4405                })
4406                .log_err();
4407        }))
4408    }
4409
4410    fn width_estimate(&self, depth: usize, entry: &PanelEntry, cx: &App) -> u64 {
4411        let item_text_chars = match entry {
4412            PanelEntry::Fs(FsEntry::ExternalFile(external)) => self
4413                .buffer_snapshot_for_id(external.buffer_id, cx)
4414                .and_then(|snapshot| {
4415                    Some(snapshot.file()?.path().file_name()?.to_string_lossy().len())
4416                })
4417                .unwrap_or_default(),
4418            PanelEntry::Fs(FsEntry::Directory(directory)) => directory
4419                .entry
4420                .path
4421                .file_name()
4422                .map(|name| name.to_string_lossy().len())
4423                .unwrap_or_default(),
4424            PanelEntry::Fs(FsEntry::File(file)) => file
4425                .entry
4426                .path
4427                .file_name()
4428                .map(|name| name.to_string_lossy().len())
4429                .unwrap_or_default(),
4430            PanelEntry::FoldedDirs(folded_dirs) => {
4431                folded_dirs
4432                    .entries
4433                    .iter()
4434                    .map(|dir| {
4435                        dir.path
4436                            .file_name()
4437                            .map(|name| name.to_string_lossy().len())
4438                            .unwrap_or_default()
4439                    })
4440                    .sum::<usize>()
4441                    + folded_dirs.entries.len().saturating_sub(1) * MAIN_SEPARATOR_STR.len()
4442            }
4443            PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => self
4444                .excerpt_label(excerpt.buffer_id, &excerpt.range, cx)
4445                .map(|label| label.len())
4446                .unwrap_or_default(),
4447            PanelEntry::Outline(OutlineEntry::Outline(entry)) => entry.outline.text.len(),
4448            PanelEntry::Search(search) => search
4449                .render_data
4450                .get()
4451                .map(|data| data.context_text.len())
4452                .unwrap_or_default(),
4453        };
4454
4455        (item_text_chars + depth) as u64
4456    }
4457
4458    fn render_main_contents(
4459        &mut self,
4460        query: Option<String>,
4461        show_indent_guides: bool,
4462        indent_size: f32,
4463        window: &mut Window,
4464        cx: &mut Context<Self>,
4465    ) -> Div {
4466        let contents = if self.cached_entries.is_empty() {
4467            let header = if self.updating_fs_entries || self.updating_cached_entries {
4468                None
4469            } else if query.is_some() {
4470                Some("No matches for query")
4471            } else {
4472                Some("No outlines available")
4473            };
4474
4475            v_flex()
4476                .flex_1()
4477                .justify_center()
4478                .size_full()
4479                .when_some(header, |panel, header| {
4480                    panel
4481                        .child(h_flex().justify_center().child(Label::new(header)))
4482                        .when_some(query.clone(), |panel, query| {
4483                            panel.child(h_flex().justify_center().child(Label::new(query)))
4484                        })
4485                        .child(
4486                            h_flex()
4487                                .pt(DynamicSpacing::Base04.rems(cx))
4488                                .justify_center()
4489                                .child({
4490                                    let keystroke =
4491                                        match self.position(window, cx) {
4492                                            DockPosition::Left => window
4493                                                .keystroke_text_for(&workspace::ToggleLeftDock),
4494                                            DockPosition::Bottom => window
4495                                                .keystroke_text_for(&workspace::ToggleBottomDock),
4496                                            DockPosition::Right => window
4497                                                .keystroke_text_for(&workspace::ToggleRightDock),
4498                                        };
4499                                    Label::new(format!("Toggle this panel with {keystroke}"))
4500                                }),
4501                        )
4502                })
4503        } else {
4504            let list_contents = {
4505                let items_len = self.cached_entries.len();
4506                let multi_buffer_snapshot = self
4507                    .active_editor()
4508                    .map(|editor| editor.read(cx).buffer().read(cx).snapshot(cx));
4509                uniform_list(cx.entity().clone(), "entries", items_len, {
4510                    move |outline_panel, range, window, cx| {
4511                        let entries = outline_panel.cached_entries.get(range);
4512                        entries
4513                            .map(|entries| entries.to_vec())
4514                            .unwrap_or_default()
4515                            .into_iter()
4516                            .filter_map(|cached_entry| match cached_entry.entry {
4517                                PanelEntry::Fs(entry) => Some(outline_panel.render_entry(
4518                                    &entry,
4519                                    cached_entry.depth,
4520                                    cached_entry.string_match.as_ref(),
4521                                    window,
4522                                    cx,
4523                                )),
4524                                PanelEntry::FoldedDirs(folded_dirs_entry) => {
4525                                    Some(outline_panel.render_folded_dirs(
4526                                        &folded_dirs_entry,
4527                                        cached_entry.depth,
4528                                        cached_entry.string_match.as_ref(),
4529                                        window,
4530                                        cx,
4531                                    ))
4532                                }
4533                                PanelEntry::Outline(OutlineEntry::Excerpt(excerpt)) => {
4534                                    outline_panel.render_excerpt(
4535                                        &excerpt,
4536                                        cached_entry.depth,
4537                                        window,
4538                                        cx,
4539                                    )
4540                                }
4541                                PanelEntry::Outline(OutlineEntry::Outline(entry)) => {
4542                                    Some(outline_panel.render_outline(
4543                                        &entry,
4544                                        cached_entry.depth,
4545                                        cached_entry.string_match.as_ref(),
4546                                        window,
4547                                        cx,
4548                                    ))
4549                                }
4550                                PanelEntry::Search(SearchEntry {
4551                                    match_range,
4552                                    render_data,
4553                                    kind,
4554                                    ..
4555                                }) => outline_panel.render_search_match(
4556                                    multi_buffer_snapshot.as_ref(),
4557                                    &match_range,
4558                                    &render_data,
4559                                    kind,
4560                                    cached_entry.depth,
4561                                    cached_entry.string_match.as_ref(),
4562                                    window,
4563                                    cx,
4564                                ),
4565                            })
4566                            .collect()
4567                    }
4568                })
4569                .with_sizing_behavior(ListSizingBehavior::Infer)
4570                .with_horizontal_sizing_behavior(ListHorizontalSizingBehavior::Unconstrained)
4571                .with_width_from_item(self.max_width_item_index)
4572                .track_scroll(self.scroll_handle.clone())
4573                .when(show_indent_guides, |list| {
4574                    list.with_decoration(
4575                        ui::indent_guides(
4576                            cx.entity().clone(),
4577                            px(indent_size),
4578                            IndentGuideColors::panel(cx),
4579                            |outline_panel, range, _, _| {
4580                                let entries = outline_panel.cached_entries.get(range);
4581                                if let Some(entries) = entries {
4582                                    entries.into_iter().map(|item| item.depth).collect()
4583                                } else {
4584                                    smallvec::SmallVec::new()
4585                                }
4586                            },
4587                        )
4588                        .with_render_fn(
4589                            cx.entity().clone(),
4590                            move |outline_panel, params, _, _| {
4591                                const LEFT_OFFSET: f32 = 14.;
4592
4593                                let indent_size = params.indent_size;
4594                                let item_height = params.item_height;
4595                                let active_indent_guide_ix = find_active_indent_guide_ix(
4596                                    outline_panel,
4597                                    &params.indent_guides,
4598                                );
4599
4600                                params
4601                                    .indent_guides
4602                                    .into_iter()
4603                                    .enumerate()
4604                                    .map(|(ix, layout)| {
4605                                        let bounds = Bounds::new(
4606                                            point(
4607                                                px(layout.offset.x as f32) * indent_size
4608                                                    + px(LEFT_OFFSET),
4609                                                px(layout.offset.y as f32) * item_height,
4610                                            ),
4611                                            size(px(1.), px(layout.length as f32) * item_height),
4612                                        );
4613                                        ui::RenderedIndentGuide {
4614                                            bounds,
4615                                            layout,
4616                                            is_active: active_indent_guide_ix == Some(ix),
4617                                            hitbox: None,
4618                                        }
4619                                    })
4620                                    .collect()
4621                            },
4622                        ),
4623                    )
4624                })
4625            };
4626
4627            v_flex()
4628                .flex_shrink()
4629                .size_full()
4630                .child(list_contents.size_full().flex_shrink())
4631                .children(self.render_vertical_scrollbar(cx))
4632                .when_some(
4633                    self.render_horizontal_scrollbar(window, cx),
4634                    |this, scrollbar| this.pb_4().child(scrollbar),
4635                )
4636        }
4637        .children(self.context_menu.as_ref().map(|(menu, position, _)| {
4638            deferred(
4639                anchored()
4640                    .position(*position)
4641                    .anchor(gpui::Corner::TopLeft)
4642                    .child(menu.clone()),
4643            )
4644            .with_priority(1)
4645        }));
4646
4647        v_flex().w_full().flex_1().overflow_hidden().child(contents)
4648    }
4649
4650    fn render_filter_footer(&mut self, pinned: bool, cx: &mut Context<Self>) -> Div {
4651        v_flex().flex_none().child(horizontal_separator(cx)).child(
4652            h_flex()
4653                .p_2()
4654                .w_full()
4655                .child(self.filter_editor.clone())
4656                .child(
4657                    div().child(
4658                        IconButton::new(
4659                            "outline-panel-menu",
4660                            if pinned {
4661                                IconName::Unpin
4662                            } else {
4663                                IconName::Pin
4664                            },
4665                        )
4666                        .tooltip(Tooltip::text(if pinned {
4667                            "Unpin Outline"
4668                        } else {
4669                            "Pin Active Outline"
4670                        }))
4671                        .shape(IconButtonShape::Square)
4672                        .on_click(cx.listener(
4673                            |outline_panel, _, window, cx| {
4674                                outline_panel.toggle_active_editor_pin(
4675                                    &ToggleActiveEditorPin,
4676                                    window,
4677                                    cx,
4678                                );
4679                            },
4680                        )),
4681                    ),
4682                ),
4683        )
4684    }
4685
4686    fn buffers_inside_directory(
4687        &self,
4688        dir_worktree: WorktreeId,
4689        dir_entry: &GitEntry,
4690    ) -> HashSet<BufferId> {
4691        if !dir_entry.is_dir() {
4692            debug_panic!("buffers_inside_directory called on a non-directory entry {dir_entry:?}");
4693            return HashSet::default();
4694        }
4695
4696        self.fs_entries
4697            .iter()
4698            .skip_while(|fs_entry| match fs_entry {
4699                FsEntry::Directory(directory) => {
4700                    directory.worktree_id != dir_worktree || &directory.entry != dir_entry
4701                }
4702                _ => true,
4703            })
4704            .skip(1)
4705            .take_while(|fs_entry| match fs_entry {
4706                FsEntry::ExternalFile(..) => false,
4707                FsEntry::Directory(directory) => {
4708                    directory.worktree_id == dir_worktree
4709                        && directory.entry.path.starts_with(&dir_entry.path)
4710                }
4711                FsEntry::File(file) => {
4712                    file.worktree_id == dir_worktree && file.entry.path.starts_with(&dir_entry.path)
4713                }
4714            })
4715            .filter_map(|fs_entry| match fs_entry {
4716                FsEntry::File(file) => Some(file.buffer_id),
4717                _ => None,
4718            })
4719            .collect()
4720    }
4721}
4722
4723fn workspace_active_editor(
4724    workspace: &Workspace,
4725    cx: &App,
4726) -> Option<(Box<dyn ItemHandle>, Entity<Editor>)> {
4727    let active_item = workspace.active_item(cx)?;
4728    let active_editor = active_item
4729        .act_as::<Editor>(cx)
4730        .filter(|editor| editor.read(cx).mode() == EditorMode::Full)?;
4731    Some((active_item, active_editor))
4732}
4733
4734fn back_to_common_visited_parent(
4735    visited_dirs: &mut Vec<(ProjectEntryId, Arc<Path>)>,
4736    worktree_id: &WorktreeId,
4737    new_entry: &Entry,
4738) -> Option<(WorktreeId, ProjectEntryId)> {
4739    while let Some((visited_dir_id, visited_path)) = visited_dirs.last() {
4740        match new_entry.path.parent() {
4741            Some(parent_path) => {
4742                if parent_path == visited_path.as_ref() {
4743                    return Some((*worktree_id, *visited_dir_id));
4744                }
4745            }
4746            None => {
4747                break;
4748            }
4749        }
4750        visited_dirs.pop();
4751    }
4752    None
4753}
4754
4755fn file_name(path: &Path) -> String {
4756    let mut current_path = path;
4757    loop {
4758        if let Some(file_name) = current_path.file_name() {
4759            return file_name.to_string_lossy().into_owned();
4760        }
4761        match current_path.parent() {
4762            Some(parent) => current_path = parent,
4763            None => return path.to_string_lossy().into_owned(),
4764        }
4765    }
4766}
4767
4768impl Panel for OutlinePanel {
4769    fn persistent_name() -> &'static str {
4770        "Outline Panel"
4771    }
4772
4773    fn position(&self, _: &Window, cx: &App) -> DockPosition {
4774        match OutlinePanelSettings::get_global(cx).dock {
4775            OutlinePanelDockPosition::Left => DockPosition::Left,
4776            OutlinePanelDockPosition::Right => DockPosition::Right,
4777        }
4778    }
4779
4780    fn position_is_valid(&self, position: DockPosition) -> bool {
4781        matches!(position, DockPosition::Left | DockPosition::Right)
4782    }
4783
4784    fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
4785        settings::update_settings_file::<OutlinePanelSettings>(
4786            self.fs.clone(),
4787            cx,
4788            move |settings, _| {
4789                let dock = match position {
4790                    DockPosition::Left | DockPosition::Bottom => OutlinePanelDockPosition::Left,
4791                    DockPosition::Right => OutlinePanelDockPosition::Right,
4792                };
4793                settings.dock = Some(dock);
4794            },
4795        );
4796    }
4797
4798    fn size(&self, _: &Window, cx: &App) -> Pixels {
4799        self.width
4800            .unwrap_or_else(|| OutlinePanelSettings::get_global(cx).default_width)
4801    }
4802
4803    fn set_size(&mut self, size: Option<Pixels>, _: &mut Window, cx: &mut Context<Self>) {
4804        self.width = size;
4805        self.serialize(cx);
4806        cx.notify();
4807    }
4808
4809    fn icon(&self, _: &Window, cx: &App) -> Option<IconName> {
4810        OutlinePanelSettings::get_global(cx)
4811            .button
4812            .then_some(IconName::ListTree)
4813    }
4814
4815    fn icon_tooltip(&self, _window: &Window, _: &App) -> Option<&'static str> {
4816        Some("Outline Panel")
4817    }
4818
4819    fn toggle_action(&self) -> Box<dyn Action> {
4820        Box::new(ToggleFocus)
4821    }
4822
4823    fn starts_open(&self, _window: &Window, _: &App) -> bool {
4824        self.active
4825    }
4826
4827    fn set_active(&mut self, active: bool, window: &mut Window, cx: &mut Context<Self>) {
4828        cx.spawn_in(window, |outline_panel, mut cx| async move {
4829            outline_panel
4830                .update_in(&mut cx, |outline_panel, window, cx| {
4831                    let old_active = outline_panel.active;
4832                    outline_panel.active = active;
4833                    if old_active != active {
4834                        if active {
4835                            if let Some((active_item, active_editor)) =
4836                                outline_panel.workspace.upgrade().and_then(|workspace| {
4837                                    workspace_active_editor(workspace.read(cx), cx)
4838                                })
4839                            {
4840                                if outline_panel.should_replace_active_item(active_item.as_ref()) {
4841                                    outline_panel.replace_active_editor(
4842                                        active_item,
4843                                        active_editor,
4844                                        window,
4845                                        cx,
4846                                    );
4847                                } else {
4848                                    outline_panel.update_fs_entries(active_editor, None, window, cx)
4849                                }
4850                                return;
4851                            }
4852                        }
4853
4854                        if !outline_panel.pinned {
4855                            outline_panel.clear_previous(window, cx);
4856                        }
4857                    }
4858                    outline_panel.serialize(cx);
4859                })
4860                .ok();
4861        })
4862        .detach()
4863    }
4864
4865    fn activation_priority(&self) -> u32 {
4866        5
4867    }
4868}
4869
4870impl Focusable for OutlinePanel {
4871    fn focus_handle(&self, cx: &App) -> FocusHandle {
4872        self.filter_editor.focus_handle(cx).clone()
4873    }
4874}
4875
4876impl EventEmitter<Event> for OutlinePanel {}
4877
4878impl EventEmitter<PanelEvent> for OutlinePanel {}
4879
4880impl Render for OutlinePanel {
4881    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
4882        let (is_local, is_via_ssh) = self
4883            .project
4884            .read_with(cx, |project, _| (project.is_local(), project.is_via_ssh()));
4885        let query = self.query(cx);
4886        let pinned = self.pinned;
4887        let settings = OutlinePanelSettings::get_global(cx);
4888        let indent_size = settings.indent_size;
4889        let show_indent_guides = settings.indent_guides.show == ShowIndentGuides::Always;
4890
4891        let search_query = match &self.mode {
4892            ItemsDisplayMode::Search(search_query) => Some(search_query),
4893            _ => None,
4894        };
4895
4896        v_flex()
4897            .id("outline-panel")
4898            .size_full()
4899            .overflow_hidden()
4900            .relative()
4901            .on_hover(cx.listener(|this, hovered, window, cx| {
4902                if *hovered {
4903                    this.show_scrollbar = true;
4904                    this.hide_scrollbar_task.take();
4905                    cx.notify();
4906                } else if !this.focus_handle.contains_focused(window, cx) {
4907                    this.hide_scrollbar(window, cx);
4908                }
4909            }))
4910            .key_context(self.dispatch_context(window, cx))
4911            .on_action(cx.listener(Self::open))
4912            .on_action(cx.listener(Self::cancel))
4913            .on_action(cx.listener(Self::select_next))
4914            .on_action(cx.listener(Self::select_prev))
4915            .on_action(cx.listener(Self::select_first))
4916            .on_action(cx.listener(Self::select_last))
4917            .on_action(cx.listener(Self::select_parent))
4918            .on_action(cx.listener(Self::expand_selected_entry))
4919            .on_action(cx.listener(Self::collapse_selected_entry))
4920            .on_action(cx.listener(Self::expand_all_entries))
4921            .on_action(cx.listener(Self::collapse_all_entries))
4922            .on_action(cx.listener(Self::copy_path))
4923            .on_action(cx.listener(Self::copy_relative_path))
4924            .on_action(cx.listener(Self::toggle_active_editor_pin))
4925            .on_action(cx.listener(Self::unfold_directory))
4926            .on_action(cx.listener(Self::fold_directory))
4927            .on_action(cx.listener(Self::open_excerpts))
4928            .on_action(cx.listener(Self::open_excerpts_split))
4929            .when(is_local, |el| {
4930                el.on_action(cx.listener(Self::reveal_in_finder))
4931            })
4932            .when(is_local || is_via_ssh, |el| {
4933                el.on_action(cx.listener(Self::open_in_terminal))
4934            })
4935            .on_mouse_down(
4936                MouseButton::Right,
4937                cx.listener(move |outline_panel, event: &MouseDownEvent, window, cx| {
4938                    if let Some(entry) = outline_panel.selected_entry().cloned() {
4939                        outline_panel.deploy_context_menu(event.position, entry, window, cx)
4940                    } else if let Some(entry) = outline_panel.fs_entries.first().cloned() {
4941                        outline_panel.deploy_context_menu(
4942                            event.position,
4943                            PanelEntry::Fs(entry),
4944                            window,
4945                            cx,
4946                        )
4947                    }
4948                }),
4949            )
4950            .track_focus(&self.focus_handle)
4951            .when_some(search_query, |outline_panel, search_state| {
4952                outline_panel.child(
4953                    v_flex()
4954                        .child(
4955                            Label::new(format!("Searching: '{}'", search_state.query))
4956                                .color(Color::Muted)
4957                                .mx_2(),
4958                        )
4959                        .child(horizontal_separator(cx)),
4960                )
4961            })
4962            .child(self.render_main_contents(query, show_indent_guides, indent_size, window, cx))
4963            .child(self.render_filter_footer(pinned, cx))
4964    }
4965}
4966
4967fn find_active_indent_guide_ix(
4968    outline_panel: &OutlinePanel,
4969    candidates: &[IndentGuideLayout],
4970) -> Option<usize> {
4971    let SelectedEntry::Valid(_, target_ix) = &outline_panel.selected_entry else {
4972        return None;
4973    };
4974    let target_depth = outline_panel
4975        .cached_entries
4976        .get(*target_ix)
4977        .map(|cached_entry| cached_entry.depth)?;
4978
4979    let (target_ix, target_depth) = if let Some(target_depth) = outline_panel
4980        .cached_entries
4981        .get(target_ix + 1)
4982        .filter(|cached_entry| cached_entry.depth > target_depth)
4983        .map(|entry| entry.depth)
4984    {
4985        (target_ix + 1, target_depth.saturating_sub(1))
4986    } else {
4987        (*target_ix, target_depth.saturating_sub(1))
4988    };
4989
4990    candidates
4991        .iter()
4992        .enumerate()
4993        .find(|(_, guide)| {
4994            guide.offset.y <= target_ix
4995                && target_ix < guide.offset.y + guide.length
4996                && guide.offset.x == target_depth
4997        })
4998        .map(|(ix, _)| ix)
4999}
5000
5001fn subscribe_for_editor_events(
5002    editor: &Entity<Editor>,
5003    window: &mut Window,
5004    cx: &mut Context<OutlinePanel>,
5005) -> Subscription {
5006    let debounce = Some(UPDATE_DEBOUNCE);
5007    cx.subscribe_in(
5008        editor,
5009        window,
5010        move |outline_panel, editor, e: &EditorEvent, window, cx| {
5011            if !outline_panel.active {
5012                return;
5013            }
5014            match e {
5015                EditorEvent::SelectionsChanged { local: true } => {
5016                    outline_panel.reveal_entry_for_selection(editor.clone(), window, cx);
5017                    cx.notify();
5018                }
5019                EditorEvent::ExcerptsAdded { excerpts, .. } => {
5020                    outline_panel
5021                        .new_entries_for_fs_update
5022                        .extend(excerpts.iter().map(|&(excerpt_id, _)| excerpt_id));
5023                    outline_panel.update_fs_entries(editor.clone(), debounce, window, cx);
5024                }
5025                EditorEvent::ExcerptsRemoved { ids } => {
5026                    let mut ids = ids.iter().collect::<HashSet<_>>();
5027                    for excerpts in outline_panel.excerpts.values_mut() {
5028                        excerpts.retain(|excerpt_id, _| !ids.remove(excerpt_id));
5029                        if ids.is_empty() {
5030                            break;
5031                        }
5032                    }
5033                    outline_panel.update_fs_entries(editor.clone(), debounce, window, cx);
5034                }
5035                EditorEvent::ExcerptsExpanded { ids } => {
5036                    outline_panel.invalidate_outlines(ids);
5037                    let update_cached_items = outline_panel.update_non_fs_items(window, cx);
5038                    if update_cached_items {
5039                        outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
5040                    }
5041                }
5042                EditorEvent::ExcerptsEdited { ids } => {
5043                    outline_panel.invalidate_outlines(ids);
5044                    let update_cached_items = outline_panel.update_non_fs_items(window, cx);
5045                    if update_cached_items {
5046                        outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
5047                    }
5048                }
5049                EditorEvent::BufferFoldToggled { ids, .. } => {
5050                    outline_panel.invalidate_outlines(ids);
5051                    let mut latest_unfolded_buffer_id = None;
5052                    let mut latest_folded_buffer_id = None;
5053                    let mut ignore_selections_change = false;
5054                    outline_panel.new_entries_for_fs_update.extend(
5055                        ids.iter()
5056                            .filter(|id| {
5057                                outline_panel
5058                                    .excerpts
5059                                    .iter()
5060                                    .find_map(|(buffer_id, excerpts)| {
5061                                        if excerpts.contains_key(id) {
5062                                            ignore_selections_change |= outline_panel
5063                                                .preserve_selection_on_buffer_fold_toggles
5064                                                .remove(buffer_id);
5065                                            Some(buffer_id)
5066                                        } else {
5067                                            None
5068                                        }
5069                                    })
5070                                    .map(|buffer_id| {
5071                                        if editor.read(cx).is_buffer_folded(*buffer_id, cx) {
5072                                            latest_folded_buffer_id = Some(*buffer_id);
5073                                            false
5074                                        } else {
5075                                            latest_unfolded_buffer_id = Some(*buffer_id);
5076                                            true
5077                                        }
5078                                    })
5079                                    .unwrap_or(true)
5080                            })
5081                            .copied(),
5082                    );
5083                    if !ignore_selections_change {
5084                        if let Some(entry_to_select) = latest_unfolded_buffer_id
5085                            .or(latest_folded_buffer_id)
5086                            .and_then(|toggled_buffer_id| {
5087                                outline_panel.fs_entries.iter().find_map(
5088                                    |fs_entry| match fs_entry {
5089                                        FsEntry::ExternalFile(external) => {
5090                                            if external.buffer_id == toggled_buffer_id {
5091                                                Some(fs_entry.clone())
5092                                            } else {
5093                                                None
5094                                            }
5095                                        }
5096                                        FsEntry::File(FsEntryFile { buffer_id, .. }) => {
5097                                            if *buffer_id == toggled_buffer_id {
5098                                                Some(fs_entry.clone())
5099                                            } else {
5100                                                None
5101                                            }
5102                                        }
5103                                        FsEntry::Directory(..) => None,
5104                                    },
5105                                )
5106                            })
5107                            .map(PanelEntry::Fs)
5108                        {
5109                            outline_panel.select_entry(entry_to_select, true, window, cx);
5110                        }
5111                    }
5112
5113                    outline_panel.update_fs_entries(editor.clone(), debounce, window, cx);
5114                }
5115                EditorEvent::Reparsed(buffer_id) => {
5116                    if let Some(excerpts) = outline_panel.excerpts.get_mut(buffer_id) {
5117                        for (_, excerpt) in excerpts {
5118                            excerpt.invalidate_outlines();
5119                        }
5120                    }
5121                    let update_cached_items = outline_panel.update_non_fs_items(window, cx);
5122                    if update_cached_items {
5123                        outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
5124                    }
5125                }
5126                _ => {}
5127            }
5128        },
5129    )
5130}
5131
5132fn empty_icon() -> AnyElement {
5133    h_flex()
5134        .size(IconSize::default().rems())
5135        .invisible()
5136        .flex_none()
5137        .into_any_element()
5138}
5139
5140fn horizontal_separator(cx: &mut App) -> Div {
5141    div().mx_2().border_primary(cx).border_t_1()
5142}
5143
5144#[derive(Debug, Default)]
5145struct GenerationState {
5146    entries: Vec<CachedEntry>,
5147    match_candidates: Vec<StringMatchCandidate>,
5148    max_width_estimate_and_index: Option<(u64, usize)>,
5149}
5150
5151impl GenerationState {
5152    fn clear(&mut self) {
5153        self.entries.clear();
5154        self.match_candidates.clear();
5155        self.max_width_estimate_and_index = None;
5156    }
5157}
5158
5159#[cfg(test)]
5160mod tests {
5161    use db::indoc;
5162    use gpui::{TestAppContext, VisualTestContext, WindowHandle};
5163    use language::{tree_sitter_rust, Language, LanguageConfig, LanguageMatcher};
5164    use pretty_assertions::assert_eq;
5165    use project::FakeFs;
5166    use search::project_search::{self, perform_project_search};
5167    use serde_json::json;
5168    use util::path;
5169    use workspace::OpenVisible;
5170
5171    use super::*;
5172
5173    const SELECTED_MARKER: &str = "  <==== selected";
5174
5175    #[gpui::test(iterations = 10)]
5176    async fn test_project_search_results_toggling(cx: &mut TestAppContext) {
5177        init_test(cx);
5178
5179        let fs = FakeFs::new(cx.background_executor.clone());
5180        populate_with_test_ra_project(&fs, "/rust-analyzer").await;
5181        let project = Project::test(fs.clone(), ["/rust-analyzer".as_ref()], cx).await;
5182        project.read_with(cx, |project, _| {
5183            project.languages().add(Arc::new(rust_lang()))
5184        });
5185        let workspace = add_outline_panel(&project, cx).await;
5186        let cx = &mut VisualTestContext::from_window(*workspace, cx);
5187        let outline_panel = outline_panel(&workspace, cx);
5188        outline_panel.update_in(cx, |outline_panel, window, cx| {
5189            outline_panel.set_active(true, window, cx)
5190        });
5191
5192        workspace
5193            .update(cx, |workspace, window, cx| {
5194                ProjectSearchView::deploy_search(
5195                    workspace,
5196                    &workspace::DeploySearch::default(),
5197                    window,
5198                    cx,
5199                )
5200            })
5201            .unwrap();
5202        let search_view = workspace
5203            .update(cx, |workspace, _, cx| {
5204                workspace
5205                    .active_pane()
5206                    .read(cx)
5207                    .items()
5208                    .find_map(|item| item.downcast::<ProjectSearchView>())
5209                    .expect("Project search view expected to appear after new search event trigger")
5210            })
5211            .unwrap();
5212
5213        let query = "param_names_for_lifetime_elision_hints";
5214        perform_project_search(&search_view, query, cx);
5215        search_view.update(cx, |search_view, cx| {
5216            search_view
5217                .results_editor()
5218                .update(cx, |results_editor, cx| {
5219                    assert_eq!(
5220                        results_editor.display_text(cx).match_indices(query).count(),
5221                        9
5222                    );
5223                });
5224        });
5225
5226        let all_matches = r#"/rust-analyzer/
5227  crates/
5228    ide/src/
5229      inlay_hints/
5230        fn_lifetime_fn.rs
5231          search: match config.param_names_for_lifetime_elision_hints {
5232          search: allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
5233          search: Some(it) if config.param_names_for_lifetime_elision_hints => {
5234          search: InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
5235      inlay_hints.rs
5236        search: pub param_names_for_lifetime_elision_hints: bool,
5237        search: param_names_for_lifetime_elision_hints: self
5238      static_index.rs
5239        search: param_names_for_lifetime_elision_hints: false,
5240    rust-analyzer/src/
5241      cli/
5242        analysis_stats.rs
5243          search: param_names_for_lifetime_elision_hints: true,
5244      config.rs
5245        search: param_names_for_lifetime_elision_hints: self"#;
5246        let select_first_in_all_matches = |line_to_select: &str| {
5247            assert!(all_matches.contains(line_to_select));
5248            all_matches.replacen(
5249                line_to_select,
5250                &format!("{line_to_select}{SELECTED_MARKER}"),
5251                1,
5252            )
5253        };
5254
5255        cx.executor()
5256            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5257        cx.run_until_parked();
5258        outline_panel.update(cx, |outline_panel, cx| {
5259            assert_eq!(
5260                display_entries(
5261                    &project,
5262                    &snapshot(&outline_panel, cx),
5263                    &outline_panel.cached_entries,
5264                    outline_panel.selected_entry(),
5265                    cx,
5266                ),
5267                select_first_in_all_matches(
5268                    "search: match config.param_names_for_lifetime_elision_hints {"
5269                )
5270            );
5271        });
5272
5273        outline_panel.update_in(cx, |outline_panel, window, cx| {
5274            outline_panel.select_parent(&SelectParent, window, cx);
5275            assert_eq!(
5276                display_entries(
5277                    &project,
5278                    &snapshot(&outline_panel, cx),
5279                    &outline_panel.cached_entries,
5280                    outline_panel.selected_entry(),
5281                    cx,
5282                ),
5283                select_first_in_all_matches("fn_lifetime_fn.rs")
5284            );
5285        });
5286        outline_panel.update_in(cx, |outline_panel, window, cx| {
5287            outline_panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
5288        });
5289        cx.executor()
5290            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5291        cx.run_until_parked();
5292        outline_panel.update(cx, |outline_panel, cx| {
5293            assert_eq!(
5294                display_entries(
5295                    &project,
5296                    &snapshot(&outline_panel, cx),
5297                    &outline_panel.cached_entries,
5298                    outline_panel.selected_entry(),
5299                    cx,
5300                ),
5301                format!(
5302                    r#"/rust-analyzer/
5303  crates/
5304    ide/src/
5305      inlay_hints/
5306        fn_lifetime_fn.rs{SELECTED_MARKER}
5307      inlay_hints.rs
5308        search: pub param_names_for_lifetime_elision_hints: bool,
5309        search: param_names_for_lifetime_elision_hints: self
5310      static_index.rs
5311        search: param_names_for_lifetime_elision_hints: false,
5312    rust-analyzer/src/
5313      cli/
5314        analysis_stats.rs
5315          search: param_names_for_lifetime_elision_hints: true,
5316      config.rs
5317        search: param_names_for_lifetime_elision_hints: self"#,
5318                )
5319            );
5320        });
5321
5322        outline_panel.update_in(cx, |outline_panel, window, cx| {
5323            outline_panel.expand_all_entries(&ExpandAllEntries, window, cx);
5324        });
5325        cx.executor()
5326            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5327        cx.run_until_parked();
5328        outline_panel.update_in(cx, |outline_panel, window, cx| {
5329            outline_panel.select_parent(&SelectParent, window, cx);
5330            assert_eq!(
5331                display_entries(
5332                    &project,
5333                    &snapshot(&outline_panel, cx),
5334                    &outline_panel.cached_entries,
5335                    outline_panel.selected_entry(),
5336                    cx,
5337                ),
5338                select_first_in_all_matches("inlay_hints/")
5339            );
5340        });
5341
5342        outline_panel.update_in(cx, |outline_panel, window, cx| {
5343            outline_panel.select_parent(&SelectParent, window, cx);
5344            assert_eq!(
5345                display_entries(
5346                    &project,
5347                    &snapshot(&outline_panel, cx),
5348                    &outline_panel.cached_entries,
5349                    outline_panel.selected_entry(),
5350                    cx,
5351                ),
5352                select_first_in_all_matches("ide/src/")
5353            );
5354        });
5355
5356        outline_panel.update_in(cx, |outline_panel, window, cx| {
5357            outline_panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
5358        });
5359        cx.executor()
5360            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5361        cx.run_until_parked();
5362        outline_panel.update(cx, |outline_panel, cx| {
5363            assert_eq!(
5364                display_entries(
5365                    &project,
5366                    &snapshot(&outline_panel, cx),
5367                    &outline_panel.cached_entries,
5368                    outline_panel.selected_entry(),
5369                    cx,
5370                ),
5371                format!(
5372                    r#"/rust-analyzer/
5373  crates/
5374    ide/src/{SELECTED_MARKER}
5375    rust-analyzer/src/
5376      cli/
5377        analysis_stats.rs
5378          search: param_names_for_lifetime_elision_hints: true,
5379      config.rs
5380        search: param_names_for_lifetime_elision_hints: self"#,
5381                )
5382            );
5383        });
5384        outline_panel.update_in(cx, |outline_panel, window, cx| {
5385            outline_panel.expand_selected_entry(&ExpandSelectedEntry, window, cx);
5386        });
5387        cx.executor()
5388            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5389        cx.run_until_parked();
5390        outline_panel.update(cx, |outline_panel, cx| {
5391            assert_eq!(
5392                display_entries(
5393                    &project,
5394                    &snapshot(&outline_panel, cx),
5395                    &outline_panel.cached_entries,
5396                    outline_panel.selected_entry(),
5397                    cx,
5398                ),
5399                select_first_in_all_matches("ide/src/")
5400            );
5401        });
5402    }
5403
5404    #[gpui::test(iterations = 10)]
5405    async fn test_item_filtering(cx: &mut TestAppContext) {
5406        init_test(cx);
5407
5408        let fs = FakeFs::new(cx.background_executor.clone());
5409        populate_with_test_ra_project(&fs, "/rust-analyzer").await;
5410        let project = Project::test(fs.clone(), ["/rust-analyzer".as_ref()], cx).await;
5411        project.read_with(cx, |project, _| {
5412            project.languages().add(Arc::new(rust_lang()))
5413        });
5414        let workspace = add_outline_panel(&project, cx).await;
5415        let cx = &mut VisualTestContext::from_window(*workspace, cx);
5416        let outline_panel = outline_panel(&workspace, cx);
5417        outline_panel.update_in(cx, |outline_panel, window, cx| {
5418            outline_panel.set_active(true, window, cx)
5419        });
5420
5421        workspace
5422            .update(cx, |workspace, window, cx| {
5423                ProjectSearchView::deploy_search(
5424                    workspace,
5425                    &workspace::DeploySearch::default(),
5426                    window,
5427                    cx,
5428                )
5429            })
5430            .unwrap();
5431        let search_view = workspace
5432            .update(cx, |workspace, _, cx| {
5433                workspace
5434                    .active_pane()
5435                    .read(cx)
5436                    .items()
5437                    .find_map(|item| item.downcast::<ProjectSearchView>())
5438                    .expect("Project search view expected to appear after new search event trigger")
5439            })
5440            .unwrap();
5441
5442        let query = "param_names_for_lifetime_elision_hints";
5443        perform_project_search(&search_view, query, cx);
5444        search_view.update(cx, |search_view, cx| {
5445            search_view
5446                .results_editor()
5447                .update(cx, |results_editor, cx| {
5448                    assert_eq!(
5449                        results_editor.display_text(cx).match_indices(query).count(),
5450                        9
5451                    );
5452                });
5453        });
5454        let all_matches = r#"/rust-analyzer/
5455  crates/
5456    ide/src/
5457      inlay_hints/
5458        fn_lifetime_fn.rs
5459          search: match config.param_names_for_lifetime_elision_hints {
5460          search: allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
5461          search: Some(it) if config.param_names_for_lifetime_elision_hints => {
5462          search: InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
5463      inlay_hints.rs
5464        search: pub param_names_for_lifetime_elision_hints: bool,
5465        search: param_names_for_lifetime_elision_hints: self
5466      static_index.rs
5467        search: param_names_for_lifetime_elision_hints: false,
5468    rust-analyzer/src/
5469      cli/
5470        analysis_stats.rs
5471          search: param_names_for_lifetime_elision_hints: true,
5472      config.rs
5473        search: param_names_for_lifetime_elision_hints: self"#;
5474
5475        cx.executor()
5476            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5477        cx.run_until_parked();
5478        outline_panel.update(cx, |outline_panel, cx| {
5479            assert_eq!(
5480                display_entries(
5481                    &project,
5482                    &snapshot(&outline_panel, cx),
5483                    &outline_panel.cached_entries,
5484                    None,
5485                    cx,
5486                ),
5487                all_matches,
5488            );
5489        });
5490
5491        let filter_text = "a";
5492        outline_panel.update_in(cx, |outline_panel, window, cx| {
5493            outline_panel.filter_editor.update(cx, |filter_editor, cx| {
5494                filter_editor.set_text(filter_text, window, cx);
5495            });
5496        });
5497        cx.executor()
5498            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5499        cx.run_until_parked();
5500
5501        outline_panel.update(cx, |outline_panel, cx| {
5502            assert_eq!(
5503                display_entries(
5504                    &project,
5505                    &snapshot(&outline_panel, cx),
5506                    &outline_panel.cached_entries,
5507                    None,
5508                    cx,
5509                ),
5510                all_matches
5511                    .lines()
5512                    .skip(1) // `/rust-analyzer/` is a root entry with path `` and it will be filtered out
5513                    .filter(|item| item.contains(filter_text))
5514                    .collect::<Vec<_>>()
5515                    .join("\n"),
5516            );
5517        });
5518
5519        outline_panel.update_in(cx, |outline_panel, window, cx| {
5520            outline_panel.filter_editor.update(cx, |filter_editor, cx| {
5521                filter_editor.set_text("", window, cx);
5522            });
5523        });
5524        cx.executor()
5525            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5526        cx.run_until_parked();
5527        outline_panel.update(cx, |outline_panel, cx| {
5528            assert_eq!(
5529                display_entries(
5530                    &project,
5531                    &snapshot(&outline_panel, cx),
5532                    &outline_panel.cached_entries,
5533                    None,
5534                    cx,
5535                ),
5536                all_matches,
5537            );
5538        });
5539    }
5540
5541    #[gpui::test(iterations = 10)]
5542    async fn test_item_opening(cx: &mut TestAppContext) {
5543        init_test(cx);
5544
5545        let fs = FakeFs::new(cx.background_executor.clone());
5546        populate_with_test_ra_project(&fs, path!("/rust-analyzer")).await;
5547        let project = Project::test(fs.clone(), [path!("/rust-analyzer").as_ref()], cx).await;
5548        project.read_with(cx, |project, _| {
5549            project.languages().add(Arc::new(rust_lang()))
5550        });
5551        let workspace = add_outline_panel(&project, cx).await;
5552        let cx = &mut VisualTestContext::from_window(*workspace, cx);
5553        let outline_panel = outline_panel(&workspace, cx);
5554        outline_panel.update_in(cx, |outline_panel, window, cx| {
5555            outline_panel.set_active(true, window, cx)
5556        });
5557
5558        workspace
5559            .update(cx, |workspace, window, cx| {
5560                ProjectSearchView::deploy_search(
5561                    workspace,
5562                    &workspace::DeploySearch::default(),
5563                    window,
5564                    cx,
5565                )
5566            })
5567            .unwrap();
5568        let search_view = workspace
5569            .update(cx, |workspace, _, cx| {
5570                workspace
5571                    .active_pane()
5572                    .read(cx)
5573                    .items()
5574                    .find_map(|item| item.downcast::<ProjectSearchView>())
5575                    .expect("Project search view expected to appear after new search event trigger")
5576            })
5577            .unwrap();
5578
5579        let query = "param_names_for_lifetime_elision_hints";
5580        perform_project_search(&search_view, query, cx);
5581        search_view.update(cx, |search_view, cx| {
5582            search_view
5583                .results_editor()
5584                .update(cx, |results_editor, cx| {
5585                    assert_eq!(
5586                        results_editor.display_text(cx).match_indices(query).count(),
5587                        9
5588                    );
5589                });
5590        });
5591        let root_path = format!("{}/", path!("/rust-analyzer"));
5592        let all_matches = format!(
5593            r#"{root_path}
5594  crates/
5595    ide/src/
5596      inlay_hints/
5597        fn_lifetime_fn.rs
5598          search: match config.param_names_for_lifetime_elision_hints {{
5599          search: allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {{
5600          search: Some(it) if config.param_names_for_lifetime_elision_hints => {{
5601          search: InlayHintsConfig {{ param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG }},
5602      inlay_hints.rs
5603        search: pub param_names_for_lifetime_elision_hints: bool,
5604        search: param_names_for_lifetime_elision_hints: self
5605      static_index.rs
5606        search: param_names_for_lifetime_elision_hints: false,
5607    rust-analyzer/src/
5608      cli/
5609        analysis_stats.rs
5610          search: param_names_for_lifetime_elision_hints: true,
5611      config.rs
5612        search: param_names_for_lifetime_elision_hints: self"#
5613        );
5614        let select_first_in_all_matches = |line_to_select: &str| {
5615            assert!(all_matches.contains(line_to_select));
5616            all_matches.replacen(
5617                line_to_select,
5618                &format!("{line_to_select}{SELECTED_MARKER}"),
5619                1,
5620            )
5621        };
5622        cx.executor()
5623            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5624        cx.run_until_parked();
5625
5626        let active_editor = outline_panel.update(cx, |outline_panel, _| {
5627            outline_panel
5628                .active_editor()
5629                .expect("should have an active editor open")
5630        });
5631        let initial_outline_selection =
5632            "search: match config.param_names_for_lifetime_elision_hints {";
5633        outline_panel.update_in(cx, |outline_panel, window, cx| {
5634            assert_eq!(
5635                display_entries(
5636                    &project,
5637                    &snapshot(&outline_panel, cx),
5638                    &outline_panel.cached_entries,
5639                    outline_panel.selected_entry(),
5640                    cx,
5641                ),
5642                select_first_in_all_matches(initial_outline_selection)
5643            );
5644            assert_eq!(
5645                selected_row_text(&active_editor, cx),
5646                initial_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5647                "Should place the initial editor selection on the corresponding search result"
5648            );
5649
5650            outline_panel.select_next(&SelectNext, window, cx);
5651            outline_panel.select_next(&SelectNext, window, cx);
5652        });
5653
5654        let navigated_outline_selection =
5655            "search: Some(it) if config.param_names_for_lifetime_elision_hints => {";
5656        outline_panel.update(cx, |outline_panel, cx| {
5657            assert_eq!(
5658                display_entries(
5659                    &project,
5660                    &snapshot(&outline_panel, cx),
5661                    &outline_panel.cached_entries,
5662                    outline_panel.selected_entry(),
5663                    cx,
5664                ),
5665                select_first_in_all_matches(navigated_outline_selection)
5666            );
5667        });
5668        cx.executor()
5669            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5670        outline_panel.update(cx, |_, cx| {
5671            assert_eq!(
5672                selected_row_text(&active_editor, cx),
5673                navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5674                "Should still have the initial caret position after SelectNext calls"
5675            );
5676        });
5677
5678        outline_panel.update_in(cx, |outline_panel, window, cx| {
5679            outline_panel.open(&Open, window, cx);
5680        });
5681        outline_panel.update(cx, |_outline_panel, cx| {
5682            assert_eq!(
5683                selected_row_text(&active_editor, cx),
5684                navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5685                "After opening, should move the caret to the opened outline entry's position"
5686            );
5687        });
5688
5689        outline_panel.update_in(cx, |outline_panel, window, cx| {
5690            outline_panel.select_next(&SelectNext, window, cx);
5691        });
5692        let next_navigated_outline_selection =
5693            "search: InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },";
5694        outline_panel.update(cx, |outline_panel, cx| {
5695            assert_eq!(
5696                display_entries(
5697                    &project,
5698                    &snapshot(&outline_panel, cx),
5699                    &outline_panel.cached_entries,
5700                    outline_panel.selected_entry(),
5701                    cx,
5702                ),
5703                select_first_in_all_matches(next_navigated_outline_selection)
5704            );
5705        });
5706        cx.executor()
5707            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5708        outline_panel.update(cx, |_outline_panel, cx| {
5709            assert_eq!(
5710                selected_row_text(&active_editor, cx),
5711                next_navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5712                "Should again preserve the selection after another SelectNext call"
5713            );
5714        });
5715
5716        outline_panel.update_in(cx, |outline_panel, window, cx| {
5717            outline_panel.open_excerpts(&editor::OpenExcerpts, window, cx);
5718        });
5719        cx.executor()
5720            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5721        cx.run_until_parked();
5722        let new_active_editor = outline_panel.update(cx, |outline_panel, _| {
5723            outline_panel
5724                .active_editor()
5725                .expect("should have an active editor open")
5726        });
5727        outline_panel.update(cx, |outline_panel, cx| {
5728            assert_ne!(
5729                active_editor, new_active_editor,
5730                "After opening an excerpt, new editor should be open"
5731            );
5732            assert_eq!(
5733                display_entries(
5734                    &project,
5735                    &snapshot(&outline_panel, cx),
5736                    &outline_panel.cached_entries,
5737                    outline_panel.selected_entry(),
5738                    cx,
5739                ),
5740                "fn_lifetime_fn.rs  <==== selected"
5741            );
5742            assert_eq!(
5743                selected_row_text(&new_active_editor, cx),
5744                next_navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5745                "When opening the excerpt, should navigate to the place corresponding the outline entry"
5746            );
5747        });
5748    }
5749
5750    #[gpui::test]
5751    async fn test_multiple_workrees(cx: &mut TestAppContext) {
5752        init_test(cx);
5753
5754        let fs = FakeFs::new(cx.background_executor.clone());
5755        fs.insert_tree(
5756            "/root",
5757            json!({
5758                "one": {
5759                    "a.txt": "aaa aaa"
5760                },
5761                "two": {
5762                    "b.txt": "a aaa"
5763                }
5764
5765            }),
5766        )
5767        .await;
5768        let project = Project::test(fs.clone(), [Path::new("/root/one")], cx).await;
5769        let workspace = add_outline_panel(&project, cx).await;
5770        let cx = &mut VisualTestContext::from_window(*workspace, cx);
5771        let outline_panel = outline_panel(&workspace, cx);
5772        outline_panel.update_in(cx, |outline_panel, window, cx| {
5773            outline_panel.set_active(true, window, cx)
5774        });
5775
5776        let items = workspace
5777            .update(cx, |workspace, window, cx| {
5778                workspace.open_paths(
5779                    vec![PathBuf::from("/root/two")],
5780                    OpenVisible::OnlyDirectories,
5781                    None,
5782                    window,
5783                    cx,
5784                )
5785            })
5786            .unwrap()
5787            .await;
5788        assert_eq!(items.len(), 1, "Were opening another worktree directory");
5789        assert!(
5790            items[0].is_none(),
5791            "Directory should be opened successfully"
5792        );
5793
5794        workspace
5795            .update(cx, |workspace, window, cx| {
5796                ProjectSearchView::deploy_search(
5797                    workspace,
5798                    &workspace::DeploySearch::default(),
5799                    window,
5800                    cx,
5801                )
5802            })
5803            .unwrap();
5804        let search_view = workspace
5805            .update(cx, |workspace, _, cx| {
5806                workspace
5807                    .active_pane()
5808                    .read(cx)
5809                    .items()
5810                    .find_map(|item| item.downcast::<ProjectSearchView>())
5811                    .expect("Project search view expected to appear after new search event trigger")
5812            })
5813            .unwrap();
5814
5815        let query = "aaa";
5816        perform_project_search(&search_view, query, cx);
5817        search_view.update(cx, |search_view, cx| {
5818            search_view
5819                .results_editor()
5820                .update(cx, |results_editor, cx| {
5821                    assert_eq!(
5822                        results_editor.display_text(cx).match_indices(query).count(),
5823                        3
5824                    );
5825                });
5826        });
5827
5828        cx.executor()
5829            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5830        cx.run_until_parked();
5831        outline_panel.update(cx, |outline_panel, cx| {
5832            assert_eq!(
5833                display_entries(
5834                    &project,
5835                    &snapshot(&outline_panel, cx),
5836                    &outline_panel.cached_entries,
5837                    outline_panel.selected_entry(),
5838                    cx,
5839                ),
5840                r#"/root/one/
5841  a.txt
5842    search: aaa aaa  <==== selected
5843    search: aaa aaa
5844/root/two/
5845  b.txt
5846    search: a aaa"#
5847            );
5848        });
5849
5850        outline_panel.update_in(cx, |outline_panel, window, cx| {
5851            outline_panel.select_prev(&SelectPrev, window, cx);
5852            outline_panel.open(&Open, window, cx);
5853        });
5854        cx.executor()
5855            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5856        cx.run_until_parked();
5857        outline_panel.update(cx, |outline_panel, cx| {
5858            assert_eq!(
5859                display_entries(
5860                    &project,
5861                    &snapshot(&outline_panel, cx),
5862                    &outline_panel.cached_entries,
5863                    outline_panel.selected_entry(),
5864                    cx,
5865                ),
5866                r#"/root/one/
5867  a.txt  <==== selected
5868/root/two/
5869  b.txt
5870    search: a aaa"#
5871            );
5872        });
5873
5874        outline_panel.update_in(cx, |outline_panel, window, cx| {
5875            outline_panel.select_next(&SelectNext, window, cx);
5876            outline_panel.open(&Open, window, cx);
5877        });
5878        cx.executor()
5879            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5880        cx.run_until_parked();
5881        outline_panel.update(cx, |outline_panel, cx| {
5882            assert_eq!(
5883                display_entries(
5884                    &project,
5885                    &snapshot(&outline_panel, cx),
5886                    &outline_panel.cached_entries,
5887                    outline_panel.selected_entry(),
5888                    cx,
5889                ),
5890                r#"/root/one/
5891  a.txt
5892/root/two/  <==== selected"#
5893            );
5894        });
5895
5896        outline_panel.update_in(cx, |outline_panel, window, cx| {
5897            outline_panel.open(&Open, window, cx);
5898        });
5899        cx.executor()
5900            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5901        cx.run_until_parked();
5902        outline_panel.update(cx, |outline_panel, cx| {
5903            assert_eq!(
5904                display_entries(
5905                    &project,
5906                    &snapshot(&outline_panel, cx),
5907                    &outline_panel.cached_entries,
5908                    outline_panel.selected_entry(),
5909                    cx,
5910                ),
5911                r#"/root/one/
5912  a.txt
5913/root/two/  <==== selected
5914  b.txt
5915    search: a aaa"#
5916            );
5917        });
5918    }
5919
5920    #[gpui::test]
5921    async fn test_navigating_in_singleton(cx: &mut TestAppContext) {
5922        init_test(cx);
5923
5924        let root = path!("/root");
5925        let fs = FakeFs::new(cx.background_executor.clone());
5926        fs.insert_tree(
5927            root,
5928            json!({
5929                "src": {
5930                    "lib.rs": indoc!("
5931#[derive(Clone, Debug, PartialEq, Eq, Hash)]
5932struct OutlineEntryExcerpt {
5933    id: ExcerptId,
5934    buffer_id: BufferId,
5935    range: ExcerptRange<language::Anchor>,
5936}"),
5937                }
5938            }),
5939        )
5940        .await;
5941        let project = Project::test(fs.clone(), [root.as_ref()], cx).await;
5942        project.read_with(cx, |project, _| {
5943            project.languages().add(Arc::new(
5944                rust_lang()
5945                    .with_outline_query(
5946                        r#"
5947                (struct_item
5948                    (visibility_modifier)? @context
5949                    "struct" @context
5950                    name: (_) @name) @item
5951
5952                (field_declaration
5953                    (visibility_modifier)? @context
5954                    name: (_) @name) @item
5955"#,
5956                    )
5957                    .unwrap(),
5958            ))
5959        });
5960        let workspace = add_outline_panel(&project, cx).await;
5961        let cx = &mut VisualTestContext::from_window(*workspace, cx);
5962        let outline_panel = outline_panel(&workspace, cx);
5963        cx.update(|window, cx| {
5964            outline_panel.update(cx, |outline_panel, cx| {
5965                outline_panel.set_active(true, window, cx)
5966            });
5967        });
5968
5969        let _editor = workspace
5970            .update(cx, |workspace, window, cx| {
5971                workspace.open_abs_path(PathBuf::from(path!("/root/src/lib.rs")), true, window, cx)
5972            })
5973            .unwrap()
5974            .await
5975            .expect("Failed to open Rust source file")
5976            .downcast::<Editor>()
5977            .expect("Should open an editor for Rust source file");
5978
5979        cx.executor()
5980            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5981        cx.run_until_parked();
5982        outline_panel.update(cx, |outline_panel, cx| {
5983            assert_eq!(
5984                display_entries(
5985                    &project,
5986                    &snapshot(&outline_panel, cx),
5987                    &outline_panel.cached_entries,
5988                    outline_panel.selected_entry(),
5989                    cx,
5990                ),
5991                indoc!(
5992                    "
5993outline: struct OutlineEntryExcerpt
5994  outline: id
5995  outline: buffer_id
5996  outline: range"
5997                )
5998            );
5999        });
6000
6001        cx.update(|window, cx| {
6002            outline_panel.update(cx, |outline_panel, cx| {
6003                outline_panel.select_next(&SelectNext, window, cx);
6004            });
6005        });
6006        cx.executor()
6007            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6008        cx.run_until_parked();
6009        outline_panel.update(cx, |outline_panel, cx| {
6010            assert_eq!(
6011                display_entries(
6012                    &project,
6013                    &snapshot(&outline_panel, cx),
6014                    &outline_panel.cached_entries,
6015                    outline_panel.selected_entry(),
6016                    cx,
6017                ),
6018                indoc!(
6019                    "
6020outline: struct OutlineEntryExcerpt  <==== selected
6021  outline: id
6022  outline: buffer_id
6023  outline: range"
6024                )
6025            );
6026        });
6027
6028        cx.update(|window, cx| {
6029            outline_panel.update(cx, |outline_panel, cx| {
6030                outline_panel.select_next(&SelectNext, window, cx);
6031            });
6032        });
6033        cx.executor()
6034            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6035        cx.run_until_parked();
6036        outline_panel.update(cx, |outline_panel, cx| {
6037            assert_eq!(
6038                display_entries(
6039                    &project,
6040                    &snapshot(&outline_panel, cx),
6041                    &outline_panel.cached_entries,
6042                    outline_panel.selected_entry(),
6043                    cx,
6044                ),
6045                indoc!(
6046                    "
6047outline: struct OutlineEntryExcerpt
6048  outline: id  <==== selected
6049  outline: buffer_id
6050  outline: range"
6051                )
6052            );
6053        });
6054
6055        cx.update(|window, cx| {
6056            outline_panel.update(cx, |outline_panel, cx| {
6057                outline_panel.select_next(&SelectNext, window, cx);
6058            });
6059        });
6060        cx.executor()
6061            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6062        cx.run_until_parked();
6063        outline_panel.update(cx, |outline_panel, cx| {
6064            assert_eq!(
6065                display_entries(
6066                    &project,
6067                    &snapshot(&outline_panel, cx),
6068                    &outline_panel.cached_entries,
6069                    outline_panel.selected_entry(),
6070                    cx,
6071                ),
6072                indoc!(
6073                    "
6074outline: struct OutlineEntryExcerpt
6075  outline: id
6076  outline: buffer_id  <==== selected
6077  outline: range"
6078                )
6079            );
6080        });
6081
6082        cx.update(|window, cx| {
6083            outline_panel.update(cx, |outline_panel, cx| {
6084                outline_panel.select_next(&SelectNext, window, cx);
6085            });
6086        });
6087        cx.executor()
6088            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6089        cx.run_until_parked();
6090        outline_panel.update(cx, |outline_panel, cx| {
6091            assert_eq!(
6092                display_entries(
6093                    &project,
6094                    &snapshot(&outline_panel, cx),
6095                    &outline_panel.cached_entries,
6096                    outline_panel.selected_entry(),
6097                    cx,
6098                ),
6099                indoc!(
6100                    "
6101outline: struct OutlineEntryExcerpt
6102  outline: id
6103  outline: buffer_id
6104  outline: range  <==== selected"
6105                )
6106            );
6107        });
6108
6109        cx.update(|window, cx| {
6110            outline_panel.update(cx, |outline_panel, cx| {
6111                outline_panel.select_next(&SelectNext, window, cx);
6112            });
6113        });
6114        cx.executor()
6115            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6116        cx.run_until_parked();
6117        outline_panel.update(cx, |outline_panel, cx| {
6118            assert_eq!(
6119                display_entries(
6120                    &project,
6121                    &snapshot(&outline_panel, cx),
6122                    &outline_panel.cached_entries,
6123                    outline_panel.selected_entry(),
6124                    cx,
6125                ),
6126                indoc!(
6127                    "
6128outline: struct OutlineEntryExcerpt  <==== selected
6129  outline: id
6130  outline: buffer_id
6131  outline: range"
6132                )
6133            );
6134        });
6135
6136        cx.update(|window, cx| {
6137            outline_panel.update(cx, |outline_panel, cx| {
6138                outline_panel.select_prev(&SelectPrev, window, cx);
6139            });
6140        });
6141        cx.executor()
6142            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6143        cx.run_until_parked();
6144        outline_panel.update(cx, |outline_panel, cx| {
6145            assert_eq!(
6146                display_entries(
6147                    &project,
6148                    &snapshot(&outline_panel, cx),
6149                    &outline_panel.cached_entries,
6150                    outline_panel.selected_entry(),
6151                    cx,
6152                ),
6153                indoc!(
6154                    "
6155outline: struct OutlineEntryExcerpt
6156  outline: id
6157  outline: buffer_id
6158  outline: range  <==== selected"
6159                )
6160            );
6161        });
6162
6163        cx.update(|window, cx| {
6164            outline_panel.update(cx, |outline_panel, cx| {
6165                outline_panel.select_prev(&SelectPrev, window, cx);
6166            });
6167        });
6168        cx.executor()
6169            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6170        cx.run_until_parked();
6171        outline_panel.update(cx, |outline_panel, cx| {
6172            assert_eq!(
6173                display_entries(
6174                    &project,
6175                    &snapshot(&outline_panel, cx),
6176                    &outline_panel.cached_entries,
6177                    outline_panel.selected_entry(),
6178                    cx,
6179                ),
6180                indoc!(
6181                    "
6182outline: struct OutlineEntryExcerpt
6183  outline: id
6184  outline: buffer_id  <==== selected
6185  outline: range"
6186                )
6187            );
6188        });
6189
6190        cx.update(|window, cx| {
6191            outline_panel.update(cx, |outline_panel, cx| {
6192                outline_panel.select_prev(&SelectPrev, window, cx);
6193            });
6194        });
6195        cx.executor()
6196            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6197        cx.run_until_parked();
6198        outline_panel.update(cx, |outline_panel, cx| {
6199            assert_eq!(
6200                display_entries(
6201                    &project,
6202                    &snapshot(&outline_panel, cx),
6203                    &outline_panel.cached_entries,
6204                    outline_panel.selected_entry(),
6205                    cx,
6206                ),
6207                indoc!(
6208                    "
6209outline: struct OutlineEntryExcerpt
6210  outline: id  <==== selected
6211  outline: buffer_id
6212  outline: range"
6213                )
6214            );
6215        });
6216
6217        cx.update(|window, cx| {
6218            outline_panel.update(cx, |outline_panel, cx| {
6219                outline_panel.select_prev(&SelectPrev, window, cx);
6220            });
6221        });
6222        cx.executor()
6223            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6224        cx.run_until_parked();
6225        outline_panel.update(cx, |outline_panel, cx| {
6226            assert_eq!(
6227                display_entries(
6228                    &project,
6229                    &snapshot(&outline_panel, cx),
6230                    &outline_panel.cached_entries,
6231                    outline_panel.selected_entry(),
6232                    cx,
6233                ),
6234                indoc!(
6235                    "
6236outline: struct OutlineEntryExcerpt  <==== selected
6237  outline: id
6238  outline: buffer_id
6239  outline: range"
6240                )
6241            );
6242        });
6243
6244        cx.update(|window, cx| {
6245            outline_panel.update(cx, |outline_panel, cx| {
6246                outline_panel.select_prev(&SelectPrev, window, cx);
6247            });
6248        });
6249        cx.executor()
6250            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6251        cx.run_until_parked();
6252        outline_panel.update(cx, |outline_panel, cx| {
6253            assert_eq!(
6254                display_entries(
6255                    &project,
6256                    &snapshot(&outline_panel, cx),
6257                    &outline_panel.cached_entries,
6258                    outline_panel.selected_entry(),
6259                    cx,
6260                ),
6261                indoc!(
6262                    "
6263outline: struct OutlineEntryExcerpt
6264  outline: id
6265  outline: buffer_id
6266  outline: range  <==== selected"
6267                )
6268            );
6269        });
6270    }
6271
6272    #[gpui::test(iterations = 10)]
6273    async fn test_frontend_repo_structure(cx: &mut TestAppContext) {
6274        init_test(cx);
6275
6276        let root = "/frontend-project";
6277        let fs = FakeFs::new(cx.background_executor.clone());
6278        fs.insert_tree(
6279            root,
6280            json!({
6281                "public": {
6282                    "lottie": {
6283                        "syntax-tree.json": r#"{ "something": "static" }"#
6284                    }
6285                },
6286                "src": {
6287                    "app": {
6288                        "(site)": {
6289                            "(about)": {
6290                                "jobs": {
6291                                    "[slug]": {
6292                                        "page.tsx": r#"static"#
6293                                    }
6294                                }
6295                            },
6296                            "(blog)": {
6297                                "post": {
6298                                    "[slug]": {
6299                                        "page.tsx": r#"static"#
6300                                    }
6301                                }
6302                            },
6303                        }
6304                    },
6305                    "components": {
6306                        "ErrorBoundary.tsx": r#"static"#,
6307                    }
6308                }
6309
6310            }),
6311        )
6312        .await;
6313        let project = Project::test(fs.clone(), [root.as_ref()], cx).await;
6314        let workspace = add_outline_panel(&project, cx).await;
6315        let cx = &mut VisualTestContext::from_window(*workspace, cx);
6316        let outline_panel = outline_panel(&workspace, cx);
6317        outline_panel.update_in(cx, |outline_panel, window, cx| {
6318            outline_panel.set_active(true, window, cx)
6319        });
6320
6321        workspace
6322            .update(cx, |workspace, window, cx| {
6323                ProjectSearchView::deploy_search(
6324                    workspace,
6325                    &workspace::DeploySearch::default(),
6326                    window,
6327                    cx,
6328                )
6329            })
6330            .unwrap();
6331        let search_view = workspace
6332            .update(cx, |workspace, _, cx| {
6333                workspace
6334                    .active_pane()
6335                    .read(cx)
6336                    .items()
6337                    .find_map(|item| item.downcast::<ProjectSearchView>())
6338                    .expect("Project search view expected to appear after new search event trigger")
6339            })
6340            .unwrap();
6341
6342        let query = "static";
6343        perform_project_search(&search_view, query, cx);
6344        search_view.update(cx, |search_view, cx| {
6345            search_view
6346                .results_editor()
6347                .update(cx, |results_editor, cx| {
6348                    assert_eq!(
6349                        results_editor.display_text(cx).match_indices(query).count(),
6350                        4
6351                    );
6352                });
6353        });
6354
6355        cx.executor()
6356            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6357        cx.run_until_parked();
6358        outline_panel.update(cx, |outline_panel, cx| {
6359            assert_eq!(
6360                display_entries(
6361                    &project,
6362                    &snapshot(&outline_panel, cx),
6363                    &outline_panel.cached_entries,
6364                    outline_panel.selected_entry(),
6365                    cx,
6366                ),
6367                r#"/frontend-project/
6368  public/lottie/
6369    syntax-tree.json
6370      search: { "something": "static" }  <==== selected
6371  src/
6372    app/(site)/
6373      (about)/jobs/[slug]/
6374        page.tsx
6375          search: static
6376      (blog)/post/[slug]/
6377        page.tsx
6378          search: static
6379    components/
6380      ErrorBoundary.tsx
6381        search: static"#
6382            );
6383        });
6384
6385        outline_panel.update_in(cx, |outline_panel, window, cx| {
6386            // Move to 5th element in the list, 3 items down.
6387            for _ in 0..2 {
6388                outline_panel.select_next(&SelectNext, window, cx);
6389            }
6390            outline_panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
6391        });
6392        cx.executor()
6393            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6394        cx.run_until_parked();
6395        outline_panel.update(cx, |outline_panel, cx| {
6396            assert_eq!(
6397                display_entries(
6398                    &project,
6399                    &snapshot(&outline_panel, cx),
6400                    &outline_panel.cached_entries,
6401                    outline_panel.selected_entry(),
6402                    cx,
6403                ),
6404                r#"/frontend-project/
6405  public/lottie/
6406    syntax-tree.json
6407      search: { "something": "static" }
6408  src/
6409    app/(site)/  <==== selected
6410    components/
6411      ErrorBoundary.tsx
6412        search: static"#
6413            );
6414        });
6415
6416        outline_panel.update_in(cx, |outline_panel, window, cx| {
6417            // Move to the next visible non-FS entry
6418            for _ in 0..3 {
6419                outline_panel.select_next(&SelectNext, window, cx);
6420            }
6421        });
6422        cx.run_until_parked();
6423        outline_panel.update(cx, |outline_panel, cx| {
6424            assert_eq!(
6425                display_entries(
6426                    &project,
6427                    &snapshot(&outline_panel, cx),
6428                    &outline_panel.cached_entries,
6429                    outline_panel.selected_entry(),
6430                    cx,
6431                ),
6432                r#"/frontend-project/
6433  public/lottie/
6434    syntax-tree.json
6435      search: { "something": "static" }
6436  src/
6437    app/(site)/
6438    components/
6439      ErrorBoundary.tsx
6440        search: static  <==== selected"#
6441            );
6442        });
6443
6444        outline_panel.update_in(cx, |outline_panel, window, cx| {
6445            outline_panel
6446                .active_editor()
6447                .expect("Should have an active editor")
6448                .update(cx, |editor, cx| {
6449                    editor.toggle_fold(&editor::actions::ToggleFold, window, cx)
6450                });
6451        });
6452        cx.executor()
6453            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6454        cx.run_until_parked();
6455        outline_panel.update(cx, |outline_panel, cx| {
6456            assert_eq!(
6457                display_entries(
6458                    &project,
6459                    &snapshot(&outline_panel, cx),
6460                    &outline_panel.cached_entries,
6461                    outline_panel.selected_entry(),
6462                    cx,
6463                ),
6464                r#"/frontend-project/
6465  public/lottie/
6466    syntax-tree.json
6467      search: { "something": "static" }
6468  src/
6469    app/(site)/
6470    components/
6471      ErrorBoundary.tsx  <==== selected"#
6472            );
6473        });
6474
6475        outline_panel.update_in(cx, |outline_panel, window, cx| {
6476            outline_panel
6477                .active_editor()
6478                .expect("Should have an active editor")
6479                .update(cx, |editor, cx| {
6480                    editor.toggle_fold(&editor::actions::ToggleFold, window, cx)
6481                });
6482        });
6483        cx.executor()
6484            .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6485        cx.run_until_parked();
6486        outline_panel.update(cx, |outline_panel, cx| {
6487            assert_eq!(
6488                display_entries(
6489                    &project,
6490                    &snapshot(&outline_panel, cx),
6491                    &outline_panel.cached_entries,
6492                    outline_panel.selected_entry(),
6493                    cx,
6494                ),
6495                r#"/frontend-project/
6496  public/lottie/
6497    syntax-tree.json
6498      search: { "something": "static" }
6499  src/
6500    app/(site)/
6501    components/
6502      ErrorBoundary.tsx  <==== selected
6503        search: static"#
6504            );
6505        });
6506    }
6507
6508    async fn add_outline_panel(
6509        project: &Entity<Project>,
6510        cx: &mut TestAppContext,
6511    ) -> WindowHandle<Workspace> {
6512        let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
6513
6514        let outline_panel = window
6515            .update(cx, |_, window, cx| cx.spawn_in(window, OutlinePanel::load))
6516            .unwrap()
6517            .await
6518            .expect("Failed to load outline panel");
6519
6520        window
6521            .update(cx, |workspace, window, cx| {
6522                workspace.add_panel(outline_panel, window, cx);
6523            })
6524            .unwrap();
6525        window
6526    }
6527
6528    fn outline_panel(
6529        workspace: &WindowHandle<Workspace>,
6530        cx: &mut TestAppContext,
6531    ) -> Entity<OutlinePanel> {
6532        workspace
6533            .update(cx, |workspace, _, cx| {
6534                workspace
6535                    .panel::<OutlinePanel>(cx)
6536                    .expect("no outline panel")
6537            })
6538            .unwrap()
6539    }
6540
6541    fn display_entries(
6542        project: &Entity<Project>,
6543        multi_buffer_snapshot: &MultiBufferSnapshot,
6544        cached_entries: &[CachedEntry],
6545        selected_entry: Option<&PanelEntry>,
6546        cx: &mut App,
6547    ) -> String {
6548        let mut display_string = String::new();
6549        for entry in cached_entries {
6550            if !display_string.is_empty() {
6551                display_string += "\n";
6552            }
6553            for _ in 0..entry.depth {
6554                display_string += "  ";
6555            }
6556            display_string += &match &entry.entry {
6557                PanelEntry::Fs(entry) => match entry {
6558                    FsEntry::ExternalFile(_) => {
6559                        panic!("Did not cover external files with tests")
6560                    }
6561                    FsEntry::Directory(directory) => {
6562                        match project
6563                            .read(cx)
6564                            .worktree_for_id(directory.worktree_id, cx)
6565                            .and_then(|worktree| {
6566                                if worktree.read(cx).root_entry() == Some(&directory.entry.entry) {
6567                                    Some(worktree.read(cx).abs_path())
6568                                } else {
6569                                    None
6570                                }
6571                            }) {
6572                            Some(root_path) => format!(
6573                                "{}/{}",
6574                                root_path.display(),
6575                                directory.entry.path.display(),
6576                            ),
6577                            None => format!(
6578                                "{}/",
6579                                directory
6580                                    .entry
6581                                    .path
6582                                    .file_name()
6583                                    .unwrap_or_default()
6584                                    .to_string_lossy()
6585                            ),
6586                        }
6587                    }
6588                    FsEntry::File(file) => file
6589                        .entry
6590                        .path
6591                        .file_name()
6592                        .map(|name| name.to_string_lossy().to_string())
6593                        .unwrap_or_default(),
6594                },
6595                PanelEntry::FoldedDirs(folded_dirs) => folded_dirs
6596                    .entries
6597                    .iter()
6598                    .filter_map(|dir| dir.path.file_name())
6599                    .map(|name| name.to_string_lossy().to_string() + "/")
6600                    .collect(),
6601                PanelEntry::Outline(outline_entry) => match outline_entry {
6602                    OutlineEntry::Excerpt(_) => continue,
6603                    OutlineEntry::Outline(outline_entry) => {
6604                        format!("outline: {}", outline_entry.outline.text)
6605                    }
6606                },
6607                PanelEntry::Search(search_entry) => {
6608                    format!(
6609                        "search: {}",
6610                        search_entry
6611                            .render_data
6612                            .get_or_init(|| SearchData::new(
6613                                &search_entry.match_range,
6614                                &multi_buffer_snapshot
6615                            ))
6616                            .context_text
6617                    )
6618                }
6619            };
6620
6621            if Some(&entry.entry) == selected_entry {
6622                display_string += SELECTED_MARKER;
6623            }
6624        }
6625        display_string
6626    }
6627
6628    fn init_test(cx: &mut TestAppContext) {
6629        cx.update(|cx| {
6630            let settings = SettingsStore::test(cx);
6631            cx.set_global(settings);
6632
6633            theme::init(theme::LoadThemes::JustBase, cx);
6634
6635            language::init(cx);
6636            editor::init(cx);
6637            workspace::init_settings(cx);
6638            Project::init_settings(cx);
6639            project_search::init(cx);
6640            super::init(cx);
6641        });
6642    }
6643
6644    // Based on https://github.com/rust-lang/rust-analyzer/
6645    async fn populate_with_test_ra_project(fs: &FakeFs, root: &str) {
6646        fs.insert_tree(
6647            root,
6648            json!({
6649                    "crates": {
6650                        "ide": {
6651                            "src": {
6652                                "inlay_hints": {
6653                                    "fn_lifetime_fn.rs": r##"
6654        pub(super) fn hints(
6655            acc: &mut Vec<InlayHint>,
6656            config: &InlayHintsConfig,
6657            func: ast::Fn,
6658        ) -> Option<()> {
6659            // ... snip
6660
6661            let mut used_names: FxHashMap<SmolStr, usize> =
6662                match config.param_names_for_lifetime_elision_hints {
6663                    true => generic_param_list
6664                        .iter()
6665                        .flat_map(|gpl| gpl.lifetime_params())
6666                        .filter_map(|param| param.lifetime())
6667                        .filter_map(|lt| Some((SmolStr::from(lt.text().as_str().get(1..)?), 0)))
6668                        .collect(),
6669                    false => Default::default(),
6670                };
6671            {
6672                let mut potential_lt_refs = potential_lt_refs.iter().filter(|&&(.., is_elided)| is_elided);
6673                if self_param.is_some() && potential_lt_refs.next().is_some() {
6674                    allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
6675                        // self can't be used as a lifetime, so no need to check for collisions
6676                        "'self".into()
6677                    } else {
6678                        gen_idx_name()
6679                    });
6680                }
6681                potential_lt_refs.for_each(|(name, ..)| {
6682                    let name = match name {
6683                        Some(it) if config.param_names_for_lifetime_elision_hints => {
6684                            if let Some(c) = used_names.get_mut(it.text().as_str()) {
6685                                *c += 1;
6686                                SmolStr::from(format!("'{text}{c}", text = it.text().as_str()))
6687                            } else {
6688                                used_names.insert(it.text().as_str().into(), 0);
6689                                SmolStr::from_iter(["\'", it.text().as_str()])
6690                            }
6691                        }
6692                        _ => gen_idx_name(),
6693                    };
6694                    allocated_lifetimes.push(name);
6695                });
6696            }
6697
6698            // ... snip
6699        }
6700
6701        // ... snip
6702
6703            #[test]
6704            fn hints_lifetimes_named() {
6705                check_with_config(
6706                    InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
6707                    r#"
6708        fn nested_in<'named>(named: &        &X<      &()>) {}
6709        //          ^'named1, 'named2, 'named3, $
6710                                  //^'named1 ^'named2 ^'named3
6711        "#,
6712                );
6713            }
6714
6715        // ... snip
6716        "##,
6717                                },
6718                        "inlay_hints.rs": r#"
6719    #[derive(Clone, Debug, PartialEq, Eq)]
6720    pub struct InlayHintsConfig {
6721        // ... snip
6722        pub param_names_for_lifetime_elision_hints: bool,
6723        pub max_length: Option<usize>,
6724        // ... snip
6725    }
6726
6727    impl Config {
6728        pub fn inlay_hints(&self) -> InlayHintsConfig {
6729            InlayHintsConfig {
6730                // ... snip
6731                param_names_for_lifetime_elision_hints: self
6732                    .inlayHints_lifetimeElisionHints_useParameterNames()
6733                    .to_owned(),
6734                max_length: self.inlayHints_maxLength().to_owned(),
6735                // ... snip
6736            }
6737        }
6738    }
6739    "#,
6740                        "static_index.rs": r#"
6741// ... snip
6742        fn add_file(&mut self, file_id: FileId) {
6743            let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
6744            let folds = self.analysis.folding_ranges(file_id).unwrap();
6745            let inlay_hints = self
6746                .analysis
6747                .inlay_hints(
6748                    &InlayHintsConfig {
6749                        // ... snip
6750                        closure_style: hir::ClosureStyle::ImplFn,
6751                        param_names_for_lifetime_elision_hints: false,
6752                        binding_mode_hints: false,
6753                        max_length: Some(25),
6754                        closure_capture_hints: false,
6755                        // ... snip
6756                    },
6757                    file_id,
6758                    None,
6759                )
6760                .unwrap();
6761            // ... snip
6762    }
6763// ... snip
6764    "#
6765                            }
6766                        },
6767                        "rust-analyzer": {
6768                            "src": {
6769                                "cli": {
6770                                    "analysis_stats.rs": r#"
6771        // ... snip
6772                for &file_id in &file_ids {
6773                    _ = analysis.inlay_hints(
6774                        &InlayHintsConfig {
6775                            // ... snip
6776                            implicit_drop_hints: true,
6777                            lifetime_elision_hints: ide::LifetimeElisionHints::Always,
6778                            param_names_for_lifetime_elision_hints: true,
6779                            hide_named_constructor_hints: false,
6780                            hide_closure_initialization_hints: false,
6781                            closure_style: hir::ClosureStyle::ImplFn,
6782                            max_length: Some(25),
6783                            closing_brace_hints_min_lines: Some(20),
6784                            fields_to_resolve: InlayFieldsToResolve::empty(),
6785                            range_exclusive_hints: true,
6786                        },
6787                        file_id.into(),
6788                        None,
6789                    );
6790                }
6791        // ... snip
6792                                    "#,
6793                                },
6794                                "config.rs": r#"
6795                config_data! {
6796                    /// Configs that only make sense when they are set by a client. As such they can only be defined
6797                    /// by setting them using client's settings (e.g `settings.json` on VS Code).
6798                    client: struct ClientDefaultConfigData <- ClientConfigInput -> {
6799                        // ... snip
6800                        /// Maximum length for inlay hints. Set to null to have an unlimited length.
6801                        inlayHints_maxLength: Option<usize>                        = Some(25),
6802                        // ... snip
6803                        /// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
6804                        inlayHints_lifetimeElisionHints_useParameterNames: bool    = false,
6805                        // ... snip
6806                    }
6807                }
6808
6809                impl Config {
6810                    // ... snip
6811                    pub fn inlay_hints(&self) -> InlayHintsConfig {
6812                        InlayHintsConfig {
6813                            // ... snip
6814                            param_names_for_lifetime_elision_hints: self
6815                                .inlayHints_lifetimeElisionHints_useParameterNames()
6816                                .to_owned(),
6817                            max_length: self.inlayHints_maxLength().to_owned(),
6818                            // ... snip
6819                        }
6820                    }
6821                    // ... snip
6822                }
6823                "#
6824                                }
6825                        }
6826                    }
6827            }),
6828        )
6829        .await;
6830    }
6831
6832    fn rust_lang() -> Language {
6833        Language::new(
6834            LanguageConfig {
6835                name: "Rust".into(),
6836                matcher: LanguageMatcher {
6837                    path_suffixes: vec!["rs".to_string()],
6838                    ..Default::default()
6839                },
6840                ..Default::default()
6841            },
6842            Some(tree_sitter_rust::LANGUAGE.into()),
6843        )
6844        .with_highlights_query(
6845            r#"
6846                (field_identifier) @field
6847                (struct_expression) @struct
6848            "#,
6849        )
6850        .unwrap()
6851        .with_injection_query(
6852            r#"
6853                (macro_invocation
6854                    (token_tree) @injection.content
6855                    (#set! injection.language "rust"))
6856            "#,
6857        )
6858        .unwrap()
6859    }
6860
6861    fn snapshot(outline_panel: &OutlinePanel, cx: &App) -> MultiBufferSnapshot {
6862        outline_panel
6863            .active_editor()
6864            .unwrap()
6865            .read(cx)
6866            .buffer()
6867            .read(cx)
6868            .snapshot(cx)
6869    }
6870
6871    fn selected_row_text(editor: &Entity<Editor>, cx: &mut App) -> String {
6872        editor.update(cx, |editor, cx| {
6873                let selections = editor.selections.all::<language::Point>(cx);
6874                assert_eq!(selections.len(), 1, "Active editor should have exactly one selection after any outline panel interactions");
6875                let selection = selections.first().unwrap();
6876                let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx);
6877                let line_start = language::Point::new(selection.start.row, 0);
6878                let line_end = multi_buffer_snapshot.clip_point(language::Point::new(selection.end.row, u32::MAX), language::Bias::Right);
6879                multi_buffer_snapshot.text_for_range(line_start..line_end).collect::<String>().trim().to_owned()
6880        })
6881    }
6882}