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