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, None)),
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: Pixels = px(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 layout.offset.x * indent_size + LEFT_OFFSET,
4606 layout.offset.y * item_height,
4607 ),
4608 size(px(1.), layout.length * item_height),
4609 );
4610 ui::RenderedIndentGuide {
4611 bounds,
4612 layout,
4613 is_active: active_indent_guide_ix == Some(ix),
4614 hitbox: None,
4615 }
4616 })
4617 .collect()
4618 },
4619 ),
4620 )
4621 })
4622 };
4623
4624 v_flex()
4625 .flex_shrink()
4626 .size_full()
4627 .child(list_contents.size_full().flex_shrink())
4628 .children(self.render_vertical_scrollbar(cx))
4629 .when_some(
4630 self.render_horizontal_scrollbar(window, cx),
4631 |this, scrollbar| this.pb_4().child(scrollbar),
4632 )
4633 }
4634 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
4635 deferred(
4636 anchored()
4637 .position(*position)
4638 .anchor(gpui::Corner::TopLeft)
4639 .child(menu.clone()),
4640 )
4641 .with_priority(1)
4642 }));
4643
4644 v_flex().w_full().flex_1().overflow_hidden().child(contents)
4645 }
4646
4647 fn render_filter_footer(&mut self, pinned: bool, cx: &mut Context<Self>) -> Div {
4648 v_flex().flex_none().child(horizontal_separator(cx)).child(
4649 h_flex()
4650 .p_2()
4651 .w_full()
4652 .child(self.filter_editor.clone())
4653 .child(
4654 div().child(
4655 IconButton::new(
4656 "outline-panel-menu",
4657 if pinned {
4658 IconName::Unpin
4659 } else {
4660 IconName::Pin
4661 },
4662 )
4663 .tooltip(Tooltip::text(if pinned {
4664 "Unpin Outline"
4665 } else {
4666 "Pin Active Outline"
4667 }))
4668 .shape(IconButtonShape::Square)
4669 .on_click(cx.listener(
4670 |outline_panel, _, window, cx| {
4671 outline_panel.toggle_active_editor_pin(
4672 &ToggleActiveEditorPin,
4673 window,
4674 cx,
4675 );
4676 },
4677 )),
4678 ),
4679 ),
4680 )
4681 }
4682
4683 fn buffers_inside_directory(
4684 &self,
4685 dir_worktree: WorktreeId,
4686 dir_entry: &GitEntry,
4687 ) -> HashSet<BufferId> {
4688 if !dir_entry.is_dir() {
4689 debug_panic!("buffers_inside_directory called on a non-directory entry {dir_entry:?}");
4690 return HashSet::default();
4691 }
4692
4693 self.fs_entries
4694 .iter()
4695 .skip_while(|fs_entry| match fs_entry {
4696 FsEntry::Directory(directory) => {
4697 directory.worktree_id != dir_worktree || &directory.entry != dir_entry
4698 }
4699 _ => true,
4700 })
4701 .skip(1)
4702 .take_while(|fs_entry| match fs_entry {
4703 FsEntry::ExternalFile(..) => false,
4704 FsEntry::Directory(directory) => {
4705 directory.worktree_id == dir_worktree
4706 && directory.entry.path.starts_with(&dir_entry.path)
4707 }
4708 FsEntry::File(file) => {
4709 file.worktree_id == dir_worktree && file.entry.path.starts_with(&dir_entry.path)
4710 }
4711 })
4712 .filter_map(|fs_entry| match fs_entry {
4713 FsEntry::File(file) => Some(file.buffer_id),
4714 _ => None,
4715 })
4716 .collect()
4717 }
4718}
4719
4720fn workspace_active_editor(
4721 workspace: &Workspace,
4722 cx: &App,
4723) -> Option<(Box<dyn ItemHandle>, Entity<Editor>)> {
4724 let active_item = workspace.active_item(cx)?;
4725 let active_editor = active_item
4726 .act_as::<Editor>(cx)
4727 .filter(|editor| editor.read(cx).mode() == EditorMode::Full)?;
4728 Some((active_item, active_editor))
4729}
4730
4731fn back_to_common_visited_parent(
4732 visited_dirs: &mut Vec<(ProjectEntryId, Arc<Path>)>,
4733 worktree_id: &WorktreeId,
4734 new_entry: &Entry,
4735) -> Option<(WorktreeId, ProjectEntryId)> {
4736 while let Some((visited_dir_id, visited_path)) = visited_dirs.last() {
4737 match new_entry.path.parent() {
4738 Some(parent_path) => {
4739 if parent_path == visited_path.as_ref() {
4740 return Some((*worktree_id, *visited_dir_id));
4741 }
4742 }
4743 None => {
4744 break;
4745 }
4746 }
4747 visited_dirs.pop();
4748 }
4749 None
4750}
4751
4752fn file_name(path: &Path) -> String {
4753 let mut current_path = path;
4754 loop {
4755 if let Some(file_name) = current_path.file_name() {
4756 return file_name.to_string_lossy().into_owned();
4757 }
4758 match current_path.parent() {
4759 Some(parent) => current_path = parent,
4760 None => return path.to_string_lossy().into_owned(),
4761 }
4762 }
4763}
4764
4765impl Panel for OutlinePanel {
4766 fn persistent_name() -> &'static str {
4767 "Outline Panel"
4768 }
4769
4770 fn position(&self, _: &Window, cx: &App) -> DockPosition {
4771 match OutlinePanelSettings::get_global(cx).dock {
4772 OutlinePanelDockPosition::Left => DockPosition::Left,
4773 OutlinePanelDockPosition::Right => DockPosition::Right,
4774 }
4775 }
4776
4777 fn position_is_valid(&self, position: DockPosition) -> bool {
4778 matches!(position, DockPosition::Left | DockPosition::Right)
4779 }
4780
4781 fn set_position(&mut self, position: DockPosition, _: &mut Window, cx: &mut Context<Self>) {
4782 settings::update_settings_file::<OutlinePanelSettings>(
4783 self.fs.clone(),
4784 cx,
4785 move |settings, _| {
4786 let dock = match position {
4787 DockPosition::Left | DockPosition::Bottom => OutlinePanelDockPosition::Left,
4788 DockPosition::Right => OutlinePanelDockPosition::Right,
4789 };
4790 settings.dock = Some(dock);
4791 },
4792 );
4793 }
4794
4795 fn size(&self, _: &Window, cx: &App) -> Pixels {
4796 self.width
4797 .unwrap_or_else(|| OutlinePanelSettings::get_global(cx).default_width)
4798 }
4799
4800 fn set_size(&mut self, size: Option<Pixels>, _: &mut Window, cx: &mut Context<Self>) {
4801 self.width = size;
4802 self.serialize(cx);
4803 cx.notify();
4804 }
4805
4806 fn icon(&self, _: &Window, cx: &App) -> Option<IconName> {
4807 OutlinePanelSettings::get_global(cx)
4808 .button
4809 .then_some(IconName::ListTree)
4810 }
4811
4812 fn icon_tooltip(&self, _window: &Window, _: &App) -> Option<&'static str> {
4813 Some("Outline Panel")
4814 }
4815
4816 fn toggle_action(&self) -> Box<dyn Action> {
4817 Box::new(ToggleFocus)
4818 }
4819
4820 fn starts_open(&self, _window: &Window, _: &App) -> bool {
4821 self.active
4822 }
4823
4824 fn set_active(&mut self, active: bool, window: &mut Window, cx: &mut Context<Self>) {
4825 cx.spawn_in(window, async move |outline_panel, cx| {
4826 outline_panel
4827 .update_in(cx, |outline_panel, window, cx| {
4828 let old_active = outline_panel.active;
4829 outline_panel.active = active;
4830 if old_active != active {
4831 if active {
4832 if let Some((active_item, active_editor)) =
4833 outline_panel.workspace.upgrade().and_then(|workspace| {
4834 workspace_active_editor(workspace.read(cx), cx)
4835 })
4836 {
4837 if outline_panel.should_replace_active_item(active_item.as_ref()) {
4838 outline_panel.replace_active_editor(
4839 active_item,
4840 active_editor,
4841 window,
4842 cx,
4843 );
4844 } else {
4845 outline_panel.update_fs_entries(active_editor, None, window, cx)
4846 }
4847 return;
4848 }
4849 }
4850
4851 if !outline_panel.pinned {
4852 outline_panel.clear_previous(window, cx);
4853 }
4854 }
4855 outline_panel.serialize(cx);
4856 })
4857 .ok();
4858 })
4859 .detach()
4860 }
4861
4862 fn activation_priority(&self) -> u32 {
4863 5
4864 }
4865}
4866
4867impl Focusable for OutlinePanel {
4868 fn focus_handle(&self, cx: &App) -> FocusHandle {
4869 self.filter_editor.focus_handle(cx).clone()
4870 }
4871}
4872
4873impl EventEmitter<Event> for OutlinePanel {}
4874
4875impl EventEmitter<PanelEvent> for OutlinePanel {}
4876
4877impl Render for OutlinePanel {
4878 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
4879 let (is_local, is_via_ssh) = self
4880 .project
4881 .read_with(cx, |project, _| (project.is_local(), project.is_via_ssh()));
4882 let query = self.query(cx);
4883 let pinned = self.pinned;
4884 let settings = OutlinePanelSettings::get_global(cx);
4885 let indent_size = settings.indent_size;
4886 let show_indent_guides = settings.indent_guides.show == ShowIndentGuides::Always;
4887
4888 let search_query = match &self.mode {
4889 ItemsDisplayMode::Search(search_query) => Some(search_query),
4890 _ => None,
4891 };
4892
4893 v_flex()
4894 .id("outline-panel")
4895 .size_full()
4896 .overflow_hidden()
4897 .relative()
4898 .on_hover(cx.listener(|this, hovered, window, cx| {
4899 if *hovered {
4900 this.show_scrollbar = true;
4901 this.hide_scrollbar_task.take();
4902 cx.notify();
4903 } else if !this.focus_handle.contains_focused(window, cx) {
4904 this.hide_scrollbar(window, cx);
4905 }
4906 }))
4907 .key_context(self.dispatch_context(window, cx))
4908 .on_action(cx.listener(Self::open))
4909 .on_action(cx.listener(Self::cancel))
4910 .on_action(cx.listener(Self::select_next))
4911 .on_action(cx.listener(Self::select_previous))
4912 .on_action(cx.listener(Self::select_first))
4913 .on_action(cx.listener(Self::select_last))
4914 .on_action(cx.listener(Self::select_parent))
4915 .on_action(cx.listener(Self::expand_selected_entry))
4916 .on_action(cx.listener(Self::collapse_selected_entry))
4917 .on_action(cx.listener(Self::expand_all_entries))
4918 .on_action(cx.listener(Self::collapse_all_entries))
4919 .on_action(cx.listener(Self::copy_path))
4920 .on_action(cx.listener(Self::copy_relative_path))
4921 .on_action(cx.listener(Self::toggle_active_editor_pin))
4922 .on_action(cx.listener(Self::unfold_directory))
4923 .on_action(cx.listener(Self::fold_directory))
4924 .on_action(cx.listener(Self::open_excerpts))
4925 .on_action(cx.listener(Self::open_excerpts_split))
4926 .when(is_local, |el| {
4927 el.on_action(cx.listener(Self::reveal_in_finder))
4928 })
4929 .when(is_local || is_via_ssh, |el| {
4930 el.on_action(cx.listener(Self::open_in_terminal))
4931 })
4932 .on_mouse_down(
4933 MouseButton::Right,
4934 cx.listener(move |outline_panel, event: &MouseDownEvent, window, cx| {
4935 if let Some(entry) = outline_panel.selected_entry().cloned() {
4936 outline_panel.deploy_context_menu(event.position, entry, window, cx)
4937 } else if let Some(entry) = outline_panel.fs_entries.first().cloned() {
4938 outline_panel.deploy_context_menu(
4939 event.position,
4940 PanelEntry::Fs(entry),
4941 window,
4942 cx,
4943 )
4944 }
4945 }),
4946 )
4947 .track_focus(&self.focus_handle)
4948 .when_some(search_query, |outline_panel, search_state| {
4949 outline_panel.child(
4950 h_flex()
4951 .py_1p5()
4952 .px_2()
4953 .h(DynamicSpacing::Base32.px(cx))
4954 .flex_shrink_0()
4955 .border_b_1()
4956 .border_color(cx.theme().colors().border)
4957 .gap_0p5()
4958 .child(Label::new("Searching:").color(Color::Muted))
4959 .child(Label::new(format!("'{}'", search_state.query))),
4960 )
4961 })
4962 .child(self.render_main_contents(query, show_indent_guides, indent_size, window, cx))
4963 .child(self.render_filter_footer(pinned, cx))
4964 }
4965}
4966
4967fn find_active_indent_guide_ix(
4968 outline_panel: &OutlinePanel,
4969 candidates: &[IndentGuideLayout],
4970) -> Option<usize> {
4971 let SelectedEntry::Valid(_, target_ix) = &outline_panel.selected_entry else {
4972 return None;
4973 };
4974 let target_depth = outline_panel
4975 .cached_entries
4976 .get(*target_ix)
4977 .map(|cached_entry| cached_entry.depth)?;
4978
4979 let (target_ix, target_depth) = if let Some(target_depth) = outline_panel
4980 .cached_entries
4981 .get(target_ix + 1)
4982 .filter(|cached_entry| cached_entry.depth > target_depth)
4983 .map(|entry| entry.depth)
4984 {
4985 (target_ix + 1, target_depth.saturating_sub(1))
4986 } else {
4987 (*target_ix, target_depth.saturating_sub(1))
4988 };
4989
4990 candidates
4991 .iter()
4992 .enumerate()
4993 .find(|(_, guide)| {
4994 guide.offset.y <= target_ix
4995 && target_ix < guide.offset.y + guide.length
4996 && guide.offset.x == target_depth
4997 })
4998 .map(|(ix, _)| ix)
4999}
5000
5001fn subscribe_for_editor_events(
5002 editor: &Entity<Editor>,
5003 window: &mut Window,
5004 cx: &mut Context<OutlinePanel>,
5005) -> Subscription {
5006 let debounce = Some(UPDATE_DEBOUNCE);
5007 cx.subscribe_in(
5008 editor,
5009 window,
5010 move |outline_panel, editor, e: &EditorEvent, window, cx| {
5011 if !outline_panel.active {
5012 return;
5013 }
5014 match e {
5015 EditorEvent::SelectionsChanged { local: true } => {
5016 outline_panel.reveal_entry_for_selection(editor.clone(), window, cx);
5017 cx.notify();
5018 }
5019 EditorEvent::ExcerptsAdded { excerpts, .. } => {
5020 outline_panel
5021 .new_entries_for_fs_update
5022 .extend(excerpts.iter().map(|&(excerpt_id, _)| excerpt_id));
5023 outline_panel.update_fs_entries(editor.clone(), debounce, window, cx);
5024 }
5025 EditorEvent::ExcerptsRemoved { ids } => {
5026 let mut ids = ids.iter().collect::<HashSet<_>>();
5027 for excerpts in outline_panel.excerpts.values_mut() {
5028 excerpts.retain(|excerpt_id, _| !ids.remove(excerpt_id));
5029 if ids.is_empty() {
5030 break;
5031 }
5032 }
5033 outline_panel.update_fs_entries(editor.clone(), debounce, window, cx);
5034 }
5035 EditorEvent::ExcerptsExpanded { ids } => {
5036 outline_panel.invalidate_outlines(ids);
5037 let update_cached_items = outline_panel.update_non_fs_items(window, cx);
5038 if update_cached_items {
5039 outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
5040 }
5041 }
5042 EditorEvent::ExcerptsEdited { ids } => {
5043 outline_panel.invalidate_outlines(ids);
5044 let update_cached_items = outline_panel.update_non_fs_items(window, cx);
5045 if update_cached_items {
5046 outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
5047 }
5048 }
5049 EditorEvent::BufferFoldToggled { ids, .. } => {
5050 outline_panel.invalidate_outlines(ids);
5051 let mut latest_unfolded_buffer_id = None;
5052 let mut latest_folded_buffer_id = None;
5053 let mut ignore_selections_change = false;
5054 outline_panel.new_entries_for_fs_update.extend(
5055 ids.iter()
5056 .filter(|id| {
5057 outline_panel
5058 .excerpts
5059 .iter()
5060 .find_map(|(buffer_id, excerpts)| {
5061 if excerpts.contains_key(id) {
5062 ignore_selections_change |= outline_panel
5063 .preserve_selection_on_buffer_fold_toggles
5064 .remove(buffer_id);
5065 Some(buffer_id)
5066 } else {
5067 None
5068 }
5069 })
5070 .map(|buffer_id| {
5071 if editor.read(cx).is_buffer_folded(*buffer_id, cx) {
5072 latest_folded_buffer_id = Some(*buffer_id);
5073 false
5074 } else {
5075 latest_unfolded_buffer_id = Some(*buffer_id);
5076 true
5077 }
5078 })
5079 .unwrap_or(true)
5080 })
5081 .copied(),
5082 );
5083 if !ignore_selections_change {
5084 if let Some(entry_to_select) = latest_unfolded_buffer_id
5085 .or(latest_folded_buffer_id)
5086 .and_then(|toggled_buffer_id| {
5087 outline_panel.fs_entries.iter().find_map(
5088 |fs_entry| match fs_entry {
5089 FsEntry::ExternalFile(external) => {
5090 if external.buffer_id == toggled_buffer_id {
5091 Some(fs_entry.clone())
5092 } else {
5093 None
5094 }
5095 }
5096 FsEntry::File(FsEntryFile { buffer_id, .. }) => {
5097 if *buffer_id == toggled_buffer_id {
5098 Some(fs_entry.clone())
5099 } else {
5100 None
5101 }
5102 }
5103 FsEntry::Directory(..) => None,
5104 },
5105 )
5106 })
5107 .map(PanelEntry::Fs)
5108 {
5109 outline_panel.select_entry(entry_to_select, true, window, cx);
5110 }
5111 }
5112
5113 outline_panel.update_fs_entries(editor.clone(), debounce, window, cx);
5114 }
5115 EditorEvent::Reparsed(buffer_id) => {
5116 if let Some(excerpts) = outline_panel.excerpts.get_mut(buffer_id) {
5117 for (_, excerpt) in excerpts {
5118 excerpt.invalidate_outlines();
5119 }
5120 }
5121 let update_cached_items = outline_panel.update_non_fs_items(window, cx);
5122 if update_cached_items {
5123 outline_panel.update_cached_entries(Some(UPDATE_DEBOUNCE), window, cx);
5124 }
5125 }
5126 _ => {}
5127 }
5128 },
5129 )
5130}
5131
5132fn empty_icon() -> AnyElement {
5133 h_flex()
5134 .size(IconSize::default().rems())
5135 .invisible()
5136 .flex_none()
5137 .into_any_element()
5138}
5139
5140fn horizontal_separator(cx: &mut App) -> Div {
5141 div().mx_2().border_primary(cx).border_t_1()
5142}
5143
5144#[derive(Debug, Default)]
5145struct GenerationState {
5146 entries: Vec<CachedEntry>,
5147 match_candidates: Vec<StringMatchCandidate>,
5148 max_width_estimate_and_index: Option<(u64, usize)>,
5149}
5150
5151impl GenerationState {
5152 fn clear(&mut self) {
5153 self.entries.clear();
5154 self.match_candidates.clear();
5155 self.max_width_estimate_and_index = None;
5156 }
5157}
5158
5159#[cfg(test)]
5160mod tests {
5161 use db::indoc;
5162 use gpui::{TestAppContext, VisualTestContext, WindowHandle};
5163 use language::{tree_sitter_rust, Language, LanguageConfig, LanguageMatcher};
5164 use pretty_assertions::assert_eq;
5165 use project::FakeFs;
5166 use search::project_search::{self, perform_project_search};
5167 use serde_json::json;
5168 use util::path;
5169 use workspace::{OpenOptions, OpenVisible};
5170
5171 use super::*;
5172
5173 const SELECTED_MARKER: &str = " <==== selected";
5174
5175 #[gpui::test(iterations = 10)]
5176 async fn test_project_search_results_toggling(cx: &mut TestAppContext) {
5177 init_test(cx);
5178
5179 let fs = FakeFs::new(cx.background_executor.clone());
5180 populate_with_test_ra_project(&fs, "/rust-analyzer").await;
5181 let project = Project::test(fs.clone(), ["/rust-analyzer".as_ref()], cx).await;
5182 project.read_with(cx, |project, _| {
5183 project.languages().add(Arc::new(rust_lang()))
5184 });
5185 let workspace = add_outline_panel(&project, cx).await;
5186 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5187 let outline_panel = outline_panel(&workspace, cx);
5188 outline_panel.update_in(cx, |outline_panel, window, cx| {
5189 outline_panel.set_active(true, window, cx)
5190 });
5191
5192 workspace
5193 .update(cx, |workspace, window, cx| {
5194 ProjectSearchView::deploy_search(
5195 workspace,
5196 &workspace::DeploySearch::default(),
5197 window,
5198 cx,
5199 )
5200 })
5201 .unwrap();
5202 let search_view = workspace
5203 .update(cx, |workspace, _, cx| {
5204 workspace
5205 .active_pane()
5206 .read(cx)
5207 .items()
5208 .find_map(|item| item.downcast::<ProjectSearchView>())
5209 .expect("Project search view expected to appear after new search event trigger")
5210 })
5211 .unwrap();
5212
5213 let query = "param_names_for_lifetime_elision_hints";
5214 perform_project_search(&search_view, query, cx);
5215 search_view.update(cx, |search_view, cx| {
5216 search_view
5217 .results_editor()
5218 .update(cx, |results_editor, cx| {
5219 assert_eq!(
5220 results_editor.display_text(cx).match_indices(query).count(),
5221 9
5222 );
5223 });
5224 });
5225
5226 let all_matches = r#"/rust-analyzer/
5227 crates/
5228 ide/src/
5229 inlay_hints/
5230 fn_lifetime_fn.rs
5231 search: match config.param_names_for_lifetime_elision_hints {
5232 search: allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
5233 search: Some(it) if config.param_names_for_lifetime_elision_hints => {
5234 search: InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
5235 inlay_hints.rs
5236 search: pub param_names_for_lifetime_elision_hints: bool,
5237 search: param_names_for_lifetime_elision_hints: self
5238 static_index.rs
5239 search: param_names_for_lifetime_elision_hints: false,
5240 rust-analyzer/src/
5241 cli/
5242 analysis_stats.rs
5243 search: param_names_for_lifetime_elision_hints: true,
5244 config.rs
5245 search: param_names_for_lifetime_elision_hints: self"#;
5246 let select_first_in_all_matches = |line_to_select: &str| {
5247 assert!(all_matches.contains(line_to_select));
5248 all_matches.replacen(
5249 line_to_select,
5250 &format!("{line_to_select}{SELECTED_MARKER}"),
5251 1,
5252 )
5253 };
5254
5255 cx.executor()
5256 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5257 cx.run_until_parked();
5258 outline_panel.update(cx, |outline_panel, cx| {
5259 assert_eq!(
5260 display_entries(
5261 &project,
5262 &snapshot(&outline_panel, cx),
5263 &outline_panel.cached_entries,
5264 outline_panel.selected_entry(),
5265 cx,
5266 ),
5267 select_first_in_all_matches(
5268 "search: match config.param_names_for_lifetime_elision_hints {"
5269 )
5270 );
5271 });
5272
5273 outline_panel.update_in(cx, |outline_panel, window, cx| {
5274 outline_panel.select_parent(&SelectParent, window, cx);
5275 assert_eq!(
5276 display_entries(
5277 &project,
5278 &snapshot(&outline_panel, cx),
5279 &outline_panel.cached_entries,
5280 outline_panel.selected_entry(),
5281 cx,
5282 ),
5283 select_first_in_all_matches("fn_lifetime_fn.rs")
5284 );
5285 });
5286 outline_panel.update_in(cx, |outline_panel, window, cx| {
5287 outline_panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
5288 });
5289 cx.executor()
5290 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5291 cx.run_until_parked();
5292 outline_panel.update(cx, |outline_panel, cx| {
5293 assert_eq!(
5294 display_entries(
5295 &project,
5296 &snapshot(&outline_panel, cx),
5297 &outline_panel.cached_entries,
5298 outline_panel.selected_entry(),
5299 cx,
5300 ),
5301 format!(
5302 r#"/rust-analyzer/
5303 crates/
5304 ide/src/
5305 inlay_hints/
5306 fn_lifetime_fn.rs{SELECTED_MARKER}
5307 inlay_hints.rs
5308 search: pub param_names_for_lifetime_elision_hints: bool,
5309 search: param_names_for_lifetime_elision_hints: self
5310 static_index.rs
5311 search: param_names_for_lifetime_elision_hints: false,
5312 rust-analyzer/src/
5313 cli/
5314 analysis_stats.rs
5315 search: param_names_for_lifetime_elision_hints: true,
5316 config.rs
5317 search: param_names_for_lifetime_elision_hints: self"#,
5318 )
5319 );
5320 });
5321
5322 outline_panel.update_in(cx, |outline_panel, window, cx| {
5323 outline_panel.expand_all_entries(&ExpandAllEntries, window, cx);
5324 });
5325 cx.executor()
5326 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5327 cx.run_until_parked();
5328 outline_panel.update_in(cx, |outline_panel, window, cx| {
5329 outline_panel.select_parent(&SelectParent, window, cx);
5330 assert_eq!(
5331 display_entries(
5332 &project,
5333 &snapshot(&outline_panel, cx),
5334 &outline_panel.cached_entries,
5335 outline_panel.selected_entry(),
5336 cx,
5337 ),
5338 select_first_in_all_matches("inlay_hints/")
5339 );
5340 });
5341
5342 outline_panel.update_in(cx, |outline_panel, window, cx| {
5343 outline_panel.select_parent(&SelectParent, window, cx);
5344 assert_eq!(
5345 display_entries(
5346 &project,
5347 &snapshot(&outline_panel, cx),
5348 &outline_panel.cached_entries,
5349 outline_panel.selected_entry(),
5350 cx,
5351 ),
5352 select_first_in_all_matches("ide/src/")
5353 );
5354 });
5355
5356 outline_panel.update_in(cx, |outline_panel, window, cx| {
5357 outline_panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
5358 });
5359 cx.executor()
5360 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5361 cx.run_until_parked();
5362 outline_panel.update(cx, |outline_panel, cx| {
5363 assert_eq!(
5364 display_entries(
5365 &project,
5366 &snapshot(&outline_panel, cx),
5367 &outline_panel.cached_entries,
5368 outline_panel.selected_entry(),
5369 cx,
5370 ),
5371 format!(
5372 r#"/rust-analyzer/
5373 crates/
5374 ide/src/{SELECTED_MARKER}
5375 rust-analyzer/src/
5376 cli/
5377 analysis_stats.rs
5378 search: param_names_for_lifetime_elision_hints: true,
5379 config.rs
5380 search: param_names_for_lifetime_elision_hints: self"#,
5381 )
5382 );
5383 });
5384 outline_panel.update_in(cx, |outline_panel, window, cx| {
5385 outline_panel.expand_selected_entry(&ExpandSelectedEntry, window, cx);
5386 });
5387 cx.executor()
5388 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5389 cx.run_until_parked();
5390 outline_panel.update(cx, |outline_panel, cx| {
5391 assert_eq!(
5392 display_entries(
5393 &project,
5394 &snapshot(&outline_panel, cx),
5395 &outline_panel.cached_entries,
5396 outline_panel.selected_entry(),
5397 cx,
5398 ),
5399 select_first_in_all_matches("ide/src/")
5400 );
5401 });
5402 }
5403
5404 #[gpui::test(iterations = 10)]
5405 async fn test_item_filtering(cx: &mut TestAppContext) {
5406 init_test(cx);
5407
5408 let fs = FakeFs::new(cx.background_executor.clone());
5409 populate_with_test_ra_project(&fs, "/rust-analyzer").await;
5410 let project = Project::test(fs.clone(), ["/rust-analyzer".as_ref()], cx).await;
5411 project.read_with(cx, |project, _| {
5412 project.languages().add(Arc::new(rust_lang()))
5413 });
5414 let workspace = add_outline_panel(&project, cx).await;
5415 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5416 let outline_panel = outline_panel(&workspace, cx);
5417 outline_panel.update_in(cx, |outline_panel, window, cx| {
5418 outline_panel.set_active(true, window, cx)
5419 });
5420
5421 workspace
5422 .update(cx, |workspace, window, cx| {
5423 ProjectSearchView::deploy_search(
5424 workspace,
5425 &workspace::DeploySearch::default(),
5426 window,
5427 cx,
5428 )
5429 })
5430 .unwrap();
5431 let search_view = workspace
5432 .update(cx, |workspace, _, cx| {
5433 workspace
5434 .active_pane()
5435 .read(cx)
5436 .items()
5437 .find_map(|item| item.downcast::<ProjectSearchView>())
5438 .expect("Project search view expected to appear after new search event trigger")
5439 })
5440 .unwrap();
5441
5442 let query = "param_names_for_lifetime_elision_hints";
5443 perform_project_search(&search_view, query, cx);
5444 search_view.update(cx, |search_view, cx| {
5445 search_view
5446 .results_editor()
5447 .update(cx, |results_editor, cx| {
5448 assert_eq!(
5449 results_editor.display_text(cx).match_indices(query).count(),
5450 9
5451 );
5452 });
5453 });
5454 let all_matches = r#"/rust-analyzer/
5455 crates/
5456 ide/src/
5457 inlay_hints/
5458 fn_lifetime_fn.rs
5459 search: match config.param_names_for_lifetime_elision_hints {
5460 search: allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
5461 search: Some(it) if config.param_names_for_lifetime_elision_hints => {
5462 search: InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
5463 inlay_hints.rs
5464 search: pub param_names_for_lifetime_elision_hints: bool,
5465 search: param_names_for_lifetime_elision_hints: self
5466 static_index.rs
5467 search: param_names_for_lifetime_elision_hints: false,
5468 rust-analyzer/src/
5469 cli/
5470 analysis_stats.rs
5471 search: param_names_for_lifetime_elision_hints: true,
5472 config.rs
5473 search: param_names_for_lifetime_elision_hints: self"#;
5474
5475 cx.executor()
5476 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5477 cx.run_until_parked();
5478 outline_panel.update(cx, |outline_panel, cx| {
5479 assert_eq!(
5480 display_entries(
5481 &project,
5482 &snapshot(&outline_panel, cx),
5483 &outline_panel.cached_entries,
5484 None,
5485 cx,
5486 ),
5487 all_matches,
5488 );
5489 });
5490
5491 let filter_text = "a";
5492 outline_panel.update_in(cx, |outline_panel, window, cx| {
5493 outline_panel.filter_editor.update(cx, |filter_editor, cx| {
5494 filter_editor.set_text(filter_text, window, cx);
5495 });
5496 });
5497 cx.executor()
5498 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5499 cx.run_until_parked();
5500
5501 outline_panel.update(cx, |outline_panel, cx| {
5502 assert_eq!(
5503 display_entries(
5504 &project,
5505 &snapshot(&outline_panel, cx),
5506 &outline_panel.cached_entries,
5507 None,
5508 cx,
5509 ),
5510 all_matches
5511 .lines()
5512 .skip(1) // `/rust-analyzer/` is a root entry with path `` and it will be filtered out
5513 .filter(|item| item.contains(filter_text))
5514 .collect::<Vec<_>>()
5515 .join("\n"),
5516 );
5517 });
5518
5519 outline_panel.update_in(cx, |outline_panel, window, cx| {
5520 outline_panel.filter_editor.update(cx, |filter_editor, cx| {
5521 filter_editor.set_text("", window, cx);
5522 });
5523 });
5524 cx.executor()
5525 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5526 cx.run_until_parked();
5527 outline_panel.update(cx, |outline_panel, cx| {
5528 assert_eq!(
5529 display_entries(
5530 &project,
5531 &snapshot(&outline_panel, cx),
5532 &outline_panel.cached_entries,
5533 None,
5534 cx,
5535 ),
5536 all_matches,
5537 );
5538 });
5539 }
5540
5541 #[gpui::test(iterations = 10)]
5542 async fn test_item_opening(cx: &mut TestAppContext) {
5543 init_test(cx);
5544
5545 let fs = FakeFs::new(cx.background_executor.clone());
5546 populate_with_test_ra_project(&fs, path!("/rust-analyzer")).await;
5547 let project = Project::test(fs.clone(), [path!("/rust-analyzer").as_ref()], cx).await;
5548 project.read_with(cx, |project, _| {
5549 project.languages().add(Arc::new(rust_lang()))
5550 });
5551 let workspace = add_outline_panel(&project, cx).await;
5552 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5553 let outline_panel = outline_panel(&workspace, cx);
5554 outline_panel.update_in(cx, |outline_panel, window, cx| {
5555 outline_panel.set_active(true, window, cx)
5556 });
5557
5558 workspace
5559 .update(cx, |workspace, window, cx| {
5560 ProjectSearchView::deploy_search(
5561 workspace,
5562 &workspace::DeploySearch::default(),
5563 window,
5564 cx,
5565 )
5566 })
5567 .unwrap();
5568 let search_view = workspace
5569 .update(cx, |workspace, _, cx| {
5570 workspace
5571 .active_pane()
5572 .read(cx)
5573 .items()
5574 .find_map(|item| item.downcast::<ProjectSearchView>())
5575 .expect("Project search view expected to appear after new search event trigger")
5576 })
5577 .unwrap();
5578
5579 let query = "param_names_for_lifetime_elision_hints";
5580 perform_project_search(&search_view, query, cx);
5581 search_view.update(cx, |search_view, cx| {
5582 search_view
5583 .results_editor()
5584 .update(cx, |results_editor, cx| {
5585 assert_eq!(
5586 results_editor.display_text(cx).match_indices(query).count(),
5587 9
5588 );
5589 });
5590 });
5591 let root_path = format!("{}/", path!("/rust-analyzer"));
5592 let all_matches = format!(
5593 r#"{root_path}
5594 crates/
5595 ide/src/
5596 inlay_hints/
5597 fn_lifetime_fn.rs
5598 search: match config.param_names_for_lifetime_elision_hints {{
5599 search: allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {{
5600 search: Some(it) if config.param_names_for_lifetime_elision_hints => {{
5601 search: InlayHintsConfig {{ param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG }},
5602 inlay_hints.rs
5603 search: pub param_names_for_lifetime_elision_hints: bool,
5604 search: param_names_for_lifetime_elision_hints: self
5605 static_index.rs
5606 search: param_names_for_lifetime_elision_hints: false,
5607 rust-analyzer/src/
5608 cli/
5609 analysis_stats.rs
5610 search: param_names_for_lifetime_elision_hints: true,
5611 config.rs
5612 search: param_names_for_lifetime_elision_hints: self"#
5613 );
5614 let select_first_in_all_matches = |line_to_select: &str| {
5615 assert!(all_matches.contains(line_to_select));
5616 all_matches.replacen(
5617 line_to_select,
5618 &format!("{line_to_select}{SELECTED_MARKER}"),
5619 1,
5620 )
5621 };
5622 cx.executor()
5623 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5624 cx.run_until_parked();
5625
5626 let active_editor = outline_panel.update(cx, |outline_panel, _| {
5627 outline_panel
5628 .active_editor()
5629 .expect("should have an active editor open")
5630 });
5631 let initial_outline_selection =
5632 "search: match config.param_names_for_lifetime_elision_hints {";
5633 outline_panel.update_in(cx, |outline_panel, window, cx| {
5634 assert_eq!(
5635 display_entries(
5636 &project,
5637 &snapshot(&outline_panel, cx),
5638 &outline_panel.cached_entries,
5639 outline_panel.selected_entry(),
5640 cx,
5641 ),
5642 select_first_in_all_matches(initial_outline_selection)
5643 );
5644 assert_eq!(
5645 selected_row_text(&active_editor, cx),
5646 initial_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5647 "Should place the initial editor selection on the corresponding search result"
5648 );
5649
5650 outline_panel.select_next(&SelectNext, window, cx);
5651 outline_panel.select_next(&SelectNext, window, cx);
5652 });
5653
5654 let navigated_outline_selection =
5655 "search: Some(it) if config.param_names_for_lifetime_elision_hints => {";
5656 outline_panel.update(cx, |outline_panel, cx| {
5657 assert_eq!(
5658 display_entries(
5659 &project,
5660 &snapshot(&outline_panel, cx),
5661 &outline_panel.cached_entries,
5662 outline_panel.selected_entry(),
5663 cx,
5664 ),
5665 select_first_in_all_matches(navigated_outline_selection)
5666 );
5667 });
5668 cx.executor()
5669 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5670 outline_panel.update(cx, |_, cx| {
5671 assert_eq!(
5672 selected_row_text(&active_editor, cx),
5673 navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5674 "Should still have the initial caret position after SelectNext calls"
5675 );
5676 });
5677
5678 outline_panel.update_in(cx, |outline_panel, window, cx| {
5679 outline_panel.open(&Open, window, cx);
5680 });
5681 outline_panel.update(cx, |_outline_panel, cx| {
5682 assert_eq!(
5683 selected_row_text(&active_editor, cx),
5684 navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5685 "After opening, should move the caret to the opened outline entry's position"
5686 );
5687 });
5688
5689 outline_panel.update_in(cx, |outline_panel, window, cx| {
5690 outline_panel.select_next(&SelectNext, window, cx);
5691 });
5692 let next_navigated_outline_selection =
5693 "search: InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },";
5694 outline_panel.update(cx, |outline_panel, cx| {
5695 assert_eq!(
5696 display_entries(
5697 &project,
5698 &snapshot(&outline_panel, cx),
5699 &outline_panel.cached_entries,
5700 outline_panel.selected_entry(),
5701 cx,
5702 ),
5703 select_first_in_all_matches(next_navigated_outline_selection)
5704 );
5705 });
5706 cx.executor()
5707 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5708 outline_panel.update(cx, |_outline_panel, cx| {
5709 assert_eq!(
5710 selected_row_text(&active_editor, cx),
5711 next_navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5712 "Should again preserve the selection after another SelectNext call"
5713 );
5714 });
5715
5716 outline_panel.update_in(cx, |outline_panel, window, cx| {
5717 outline_panel.open_excerpts(&editor::OpenExcerpts, window, cx);
5718 });
5719 cx.executor()
5720 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5721 cx.run_until_parked();
5722 let new_active_editor = outline_panel.update(cx, |outline_panel, _| {
5723 outline_panel
5724 .active_editor()
5725 .expect("should have an active editor open")
5726 });
5727 outline_panel.update(cx, |outline_panel, cx| {
5728 assert_ne!(
5729 active_editor, new_active_editor,
5730 "After opening an excerpt, new editor should be open"
5731 );
5732 assert_eq!(
5733 display_entries(
5734 &project,
5735 &snapshot(&outline_panel, cx),
5736 &outline_panel.cached_entries,
5737 outline_panel.selected_entry(),
5738 cx,
5739 ),
5740 "fn_lifetime_fn.rs <==== selected"
5741 );
5742 assert_eq!(
5743 selected_row_text(&new_active_editor, cx),
5744 next_navigated_outline_selection.replace("search: ", ""), // Clear outline metadata prefixes
5745 "When opening the excerpt, should navigate to the place corresponding the outline entry"
5746 );
5747 });
5748 }
5749
5750 #[gpui::test]
5751 async fn test_multiple_workrees(cx: &mut TestAppContext) {
5752 init_test(cx);
5753
5754 let fs = FakeFs::new(cx.background_executor.clone());
5755 fs.insert_tree(
5756 "/root",
5757 json!({
5758 "one": {
5759 "a.txt": "aaa aaa"
5760 },
5761 "two": {
5762 "b.txt": "a aaa"
5763 }
5764
5765 }),
5766 )
5767 .await;
5768 let project = Project::test(fs.clone(), [Path::new("/root/one")], cx).await;
5769 let workspace = add_outline_panel(&project, cx).await;
5770 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5771 let outline_panel = outline_panel(&workspace, cx);
5772 outline_panel.update_in(cx, |outline_panel, window, cx| {
5773 outline_panel.set_active(true, window, cx)
5774 });
5775
5776 let items = workspace
5777 .update(cx, |workspace, window, cx| {
5778 workspace.open_paths(
5779 vec![PathBuf::from("/root/two")],
5780 OpenOptions {
5781 visible: Some(OpenVisible::OnlyDirectories),
5782 ..Default::default()
5783 },
5784 None,
5785 window,
5786 cx,
5787 )
5788 })
5789 .unwrap()
5790 .await;
5791 assert_eq!(items.len(), 1, "Were opening another worktree directory");
5792 assert!(
5793 items[0].is_none(),
5794 "Directory should be opened successfully"
5795 );
5796
5797 workspace
5798 .update(cx, |workspace, window, cx| {
5799 ProjectSearchView::deploy_search(
5800 workspace,
5801 &workspace::DeploySearch::default(),
5802 window,
5803 cx,
5804 )
5805 })
5806 .unwrap();
5807 let search_view = workspace
5808 .update(cx, |workspace, _, cx| {
5809 workspace
5810 .active_pane()
5811 .read(cx)
5812 .items()
5813 .find_map(|item| item.downcast::<ProjectSearchView>())
5814 .expect("Project search view expected to appear after new search event trigger")
5815 })
5816 .unwrap();
5817
5818 let query = "aaa";
5819 perform_project_search(&search_view, query, cx);
5820 search_view.update(cx, |search_view, cx| {
5821 search_view
5822 .results_editor()
5823 .update(cx, |results_editor, cx| {
5824 assert_eq!(
5825 results_editor.display_text(cx).match_indices(query).count(),
5826 3
5827 );
5828 });
5829 });
5830
5831 cx.executor()
5832 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5833 cx.run_until_parked();
5834 outline_panel.update(cx, |outline_panel, cx| {
5835 assert_eq!(
5836 display_entries(
5837 &project,
5838 &snapshot(&outline_panel, cx),
5839 &outline_panel.cached_entries,
5840 outline_panel.selected_entry(),
5841 cx,
5842 ),
5843 r#"/root/one/
5844 a.txt
5845 search: aaa aaa <==== selected
5846 search: aaa aaa
5847/root/two/
5848 b.txt
5849 search: a aaa"#
5850 );
5851 });
5852
5853 outline_panel.update_in(cx, |outline_panel, window, cx| {
5854 outline_panel.select_previous(&SelectPrevious, window, cx);
5855 outline_panel.open(&Open, window, cx);
5856 });
5857 cx.executor()
5858 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5859 cx.run_until_parked();
5860 outline_panel.update(cx, |outline_panel, cx| {
5861 assert_eq!(
5862 display_entries(
5863 &project,
5864 &snapshot(&outline_panel, cx),
5865 &outline_panel.cached_entries,
5866 outline_panel.selected_entry(),
5867 cx,
5868 ),
5869 r#"/root/one/
5870 a.txt <==== selected
5871/root/two/
5872 b.txt
5873 search: a aaa"#
5874 );
5875 });
5876
5877 outline_panel.update_in(cx, |outline_panel, window, cx| {
5878 outline_panel.select_next(&SelectNext, window, cx);
5879 outline_panel.open(&Open, window, cx);
5880 });
5881 cx.executor()
5882 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5883 cx.run_until_parked();
5884 outline_panel.update(cx, |outline_panel, cx| {
5885 assert_eq!(
5886 display_entries(
5887 &project,
5888 &snapshot(&outline_panel, cx),
5889 &outline_panel.cached_entries,
5890 outline_panel.selected_entry(),
5891 cx,
5892 ),
5893 r#"/root/one/
5894 a.txt
5895/root/two/ <==== selected"#
5896 );
5897 });
5898
5899 outline_panel.update_in(cx, |outline_panel, window, cx| {
5900 outline_panel.open(&Open, window, cx);
5901 });
5902 cx.executor()
5903 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5904 cx.run_until_parked();
5905 outline_panel.update(cx, |outline_panel, cx| {
5906 assert_eq!(
5907 display_entries(
5908 &project,
5909 &snapshot(&outline_panel, cx),
5910 &outline_panel.cached_entries,
5911 outline_panel.selected_entry(),
5912 cx,
5913 ),
5914 r#"/root/one/
5915 a.txt
5916/root/two/ <==== selected
5917 b.txt
5918 search: a aaa"#
5919 );
5920 });
5921 }
5922
5923 #[gpui::test]
5924 async fn test_navigating_in_singleton(cx: &mut TestAppContext) {
5925 init_test(cx);
5926
5927 let root = path!("/root");
5928 let fs = FakeFs::new(cx.background_executor.clone());
5929 fs.insert_tree(
5930 root,
5931 json!({
5932 "src": {
5933 "lib.rs": indoc!("
5934#[derive(Clone, Debug, PartialEq, Eq, Hash)]
5935struct OutlineEntryExcerpt {
5936 id: ExcerptId,
5937 buffer_id: BufferId,
5938 range: ExcerptRange<language::Anchor>,
5939}"),
5940 }
5941 }),
5942 )
5943 .await;
5944 let project = Project::test(fs.clone(), [root.as_ref()], cx).await;
5945 project.read_with(cx, |project, _| {
5946 project.languages().add(Arc::new(
5947 rust_lang()
5948 .with_outline_query(
5949 r#"
5950 (struct_item
5951 (visibility_modifier)? @context
5952 "struct" @context
5953 name: (_) @name) @item
5954
5955 (field_declaration
5956 (visibility_modifier)? @context
5957 name: (_) @name) @item
5958"#,
5959 )
5960 .unwrap(),
5961 ))
5962 });
5963 let workspace = add_outline_panel(&project, cx).await;
5964 let cx = &mut VisualTestContext::from_window(*workspace, cx);
5965 let outline_panel = outline_panel(&workspace, cx);
5966 cx.update(|window, cx| {
5967 outline_panel.update(cx, |outline_panel, cx| {
5968 outline_panel.set_active(true, window, cx)
5969 });
5970 });
5971
5972 let _editor = workspace
5973 .update(cx, |workspace, window, cx| {
5974 workspace.open_abs_path(
5975 PathBuf::from(path!("/root/src/lib.rs")),
5976 OpenOptions {
5977 visible: Some(OpenVisible::All),
5978 ..Default::default()
5979 },
5980 window,
5981 cx,
5982 )
5983 })
5984 .unwrap()
5985 .await
5986 .expect("Failed to open Rust source file")
5987 .downcast::<Editor>()
5988 .expect("Should open an editor for Rust source file");
5989
5990 cx.executor()
5991 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
5992 cx.run_until_parked();
5993 outline_panel.update(cx, |outline_panel, cx| {
5994 assert_eq!(
5995 display_entries(
5996 &project,
5997 &snapshot(&outline_panel, cx),
5998 &outline_panel.cached_entries,
5999 outline_panel.selected_entry(),
6000 cx,
6001 ),
6002 indoc!(
6003 "
6004outline: struct OutlineEntryExcerpt
6005 outline: id
6006 outline: buffer_id
6007 outline: range"
6008 )
6009 );
6010 });
6011
6012 cx.update(|window, cx| {
6013 outline_panel.update(cx, |outline_panel, cx| {
6014 outline_panel.select_next(&SelectNext, window, cx);
6015 });
6016 });
6017 cx.executor()
6018 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6019 cx.run_until_parked();
6020 outline_panel.update(cx, |outline_panel, cx| {
6021 assert_eq!(
6022 display_entries(
6023 &project,
6024 &snapshot(&outline_panel, cx),
6025 &outline_panel.cached_entries,
6026 outline_panel.selected_entry(),
6027 cx,
6028 ),
6029 indoc!(
6030 "
6031outline: struct OutlineEntryExcerpt <==== selected
6032 outline: id
6033 outline: buffer_id
6034 outline: range"
6035 )
6036 );
6037 });
6038
6039 cx.update(|window, cx| {
6040 outline_panel.update(cx, |outline_panel, cx| {
6041 outline_panel.select_next(&SelectNext, window, cx);
6042 });
6043 });
6044 cx.executor()
6045 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6046 cx.run_until_parked();
6047 outline_panel.update(cx, |outline_panel, cx| {
6048 assert_eq!(
6049 display_entries(
6050 &project,
6051 &snapshot(&outline_panel, cx),
6052 &outline_panel.cached_entries,
6053 outline_panel.selected_entry(),
6054 cx,
6055 ),
6056 indoc!(
6057 "
6058outline: struct OutlineEntryExcerpt
6059 outline: id <==== selected
6060 outline: buffer_id
6061 outline: range"
6062 )
6063 );
6064 });
6065
6066 cx.update(|window, cx| {
6067 outline_panel.update(cx, |outline_panel, cx| {
6068 outline_panel.select_next(&SelectNext, window, cx);
6069 });
6070 });
6071 cx.executor()
6072 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6073 cx.run_until_parked();
6074 outline_panel.update(cx, |outline_panel, cx| {
6075 assert_eq!(
6076 display_entries(
6077 &project,
6078 &snapshot(&outline_panel, cx),
6079 &outline_panel.cached_entries,
6080 outline_panel.selected_entry(),
6081 cx,
6082 ),
6083 indoc!(
6084 "
6085outline: struct OutlineEntryExcerpt
6086 outline: id
6087 outline: buffer_id <==== selected
6088 outline: range"
6089 )
6090 );
6091 });
6092
6093 cx.update(|window, cx| {
6094 outline_panel.update(cx, |outline_panel, cx| {
6095 outline_panel.select_next(&SelectNext, window, cx);
6096 });
6097 });
6098 cx.executor()
6099 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6100 cx.run_until_parked();
6101 outline_panel.update(cx, |outline_panel, cx| {
6102 assert_eq!(
6103 display_entries(
6104 &project,
6105 &snapshot(&outline_panel, cx),
6106 &outline_panel.cached_entries,
6107 outline_panel.selected_entry(),
6108 cx,
6109 ),
6110 indoc!(
6111 "
6112outline: struct OutlineEntryExcerpt
6113 outline: id
6114 outline: buffer_id
6115 outline: range <==== selected"
6116 )
6117 );
6118 });
6119
6120 cx.update(|window, cx| {
6121 outline_panel.update(cx, |outline_panel, cx| {
6122 outline_panel.select_next(&SelectNext, window, cx);
6123 });
6124 });
6125 cx.executor()
6126 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6127 cx.run_until_parked();
6128 outline_panel.update(cx, |outline_panel, cx| {
6129 assert_eq!(
6130 display_entries(
6131 &project,
6132 &snapshot(&outline_panel, cx),
6133 &outline_panel.cached_entries,
6134 outline_panel.selected_entry(),
6135 cx,
6136 ),
6137 indoc!(
6138 "
6139outline: struct OutlineEntryExcerpt <==== selected
6140 outline: id
6141 outline: buffer_id
6142 outline: range"
6143 )
6144 );
6145 });
6146
6147 cx.update(|window, cx| {
6148 outline_panel.update(cx, |outline_panel, cx| {
6149 outline_panel.select_previous(&SelectPrevious, window, cx);
6150 });
6151 });
6152 cx.executor()
6153 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6154 cx.run_until_parked();
6155 outline_panel.update(cx, |outline_panel, cx| {
6156 assert_eq!(
6157 display_entries(
6158 &project,
6159 &snapshot(&outline_panel, cx),
6160 &outline_panel.cached_entries,
6161 outline_panel.selected_entry(),
6162 cx,
6163 ),
6164 indoc!(
6165 "
6166outline: struct OutlineEntryExcerpt
6167 outline: id
6168 outline: buffer_id
6169 outline: range <==== selected"
6170 )
6171 );
6172 });
6173
6174 cx.update(|window, cx| {
6175 outline_panel.update(cx, |outline_panel, cx| {
6176 outline_panel.select_previous(&SelectPrevious, window, cx);
6177 });
6178 });
6179 cx.executor()
6180 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6181 cx.run_until_parked();
6182 outline_panel.update(cx, |outline_panel, cx| {
6183 assert_eq!(
6184 display_entries(
6185 &project,
6186 &snapshot(&outline_panel, cx),
6187 &outline_panel.cached_entries,
6188 outline_panel.selected_entry(),
6189 cx,
6190 ),
6191 indoc!(
6192 "
6193outline: struct OutlineEntryExcerpt
6194 outline: id
6195 outline: buffer_id <==== selected
6196 outline: range"
6197 )
6198 );
6199 });
6200
6201 cx.update(|window, cx| {
6202 outline_panel.update(cx, |outline_panel, cx| {
6203 outline_panel.select_previous(&SelectPrevious, window, cx);
6204 });
6205 });
6206 cx.executor()
6207 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6208 cx.run_until_parked();
6209 outline_panel.update(cx, |outline_panel, cx| {
6210 assert_eq!(
6211 display_entries(
6212 &project,
6213 &snapshot(&outline_panel, cx),
6214 &outline_panel.cached_entries,
6215 outline_panel.selected_entry(),
6216 cx,
6217 ),
6218 indoc!(
6219 "
6220outline: struct OutlineEntryExcerpt
6221 outline: id <==== selected
6222 outline: buffer_id
6223 outline: range"
6224 )
6225 );
6226 });
6227
6228 cx.update(|window, cx| {
6229 outline_panel.update(cx, |outline_panel, cx| {
6230 outline_panel.select_previous(&SelectPrevious, window, cx);
6231 });
6232 });
6233 cx.executor()
6234 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6235 cx.run_until_parked();
6236 outline_panel.update(cx, |outline_panel, cx| {
6237 assert_eq!(
6238 display_entries(
6239 &project,
6240 &snapshot(&outline_panel, cx),
6241 &outline_panel.cached_entries,
6242 outline_panel.selected_entry(),
6243 cx,
6244 ),
6245 indoc!(
6246 "
6247outline: struct OutlineEntryExcerpt <==== selected
6248 outline: id
6249 outline: buffer_id
6250 outline: range"
6251 )
6252 );
6253 });
6254
6255 cx.update(|window, cx| {
6256 outline_panel.update(cx, |outline_panel, cx| {
6257 outline_panel.select_previous(&SelectPrevious, window, cx);
6258 });
6259 });
6260 cx.executor()
6261 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6262 cx.run_until_parked();
6263 outline_panel.update(cx, |outline_panel, cx| {
6264 assert_eq!(
6265 display_entries(
6266 &project,
6267 &snapshot(&outline_panel, cx),
6268 &outline_panel.cached_entries,
6269 outline_panel.selected_entry(),
6270 cx,
6271 ),
6272 indoc!(
6273 "
6274outline: struct OutlineEntryExcerpt
6275 outline: id
6276 outline: buffer_id
6277 outline: range <==== selected"
6278 )
6279 );
6280 });
6281 }
6282
6283 #[gpui::test(iterations = 10)]
6284 async fn test_frontend_repo_structure(cx: &mut TestAppContext) {
6285 init_test(cx);
6286
6287 let root = "/frontend-project";
6288 let fs = FakeFs::new(cx.background_executor.clone());
6289 fs.insert_tree(
6290 root,
6291 json!({
6292 "public": {
6293 "lottie": {
6294 "syntax-tree.json": r#"{ "something": "static" }"#
6295 }
6296 },
6297 "src": {
6298 "app": {
6299 "(site)": {
6300 "(about)": {
6301 "jobs": {
6302 "[slug]": {
6303 "page.tsx": r#"static"#
6304 }
6305 }
6306 },
6307 "(blog)": {
6308 "post": {
6309 "[slug]": {
6310 "page.tsx": r#"static"#
6311 }
6312 }
6313 },
6314 }
6315 },
6316 "components": {
6317 "ErrorBoundary.tsx": r#"static"#,
6318 }
6319 }
6320
6321 }),
6322 )
6323 .await;
6324 let project = Project::test(fs.clone(), [root.as_ref()], cx).await;
6325 let workspace = add_outline_panel(&project, cx).await;
6326 let cx = &mut VisualTestContext::from_window(*workspace, cx);
6327 let outline_panel = outline_panel(&workspace, cx);
6328 outline_panel.update_in(cx, |outline_panel, window, cx| {
6329 outline_panel.set_active(true, window, cx)
6330 });
6331
6332 workspace
6333 .update(cx, |workspace, window, cx| {
6334 ProjectSearchView::deploy_search(
6335 workspace,
6336 &workspace::DeploySearch::default(),
6337 window,
6338 cx,
6339 )
6340 })
6341 .unwrap();
6342 let search_view = workspace
6343 .update(cx, |workspace, _, cx| {
6344 workspace
6345 .active_pane()
6346 .read(cx)
6347 .items()
6348 .find_map(|item| item.downcast::<ProjectSearchView>())
6349 .expect("Project search view expected to appear after new search event trigger")
6350 })
6351 .unwrap();
6352
6353 let query = "static";
6354 perform_project_search(&search_view, query, cx);
6355 search_view.update(cx, |search_view, cx| {
6356 search_view
6357 .results_editor()
6358 .update(cx, |results_editor, cx| {
6359 assert_eq!(
6360 results_editor.display_text(cx).match_indices(query).count(),
6361 4
6362 );
6363 });
6364 });
6365
6366 cx.executor()
6367 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6368 cx.run_until_parked();
6369 outline_panel.update(cx, |outline_panel, cx| {
6370 assert_eq!(
6371 display_entries(
6372 &project,
6373 &snapshot(&outline_panel, cx),
6374 &outline_panel.cached_entries,
6375 outline_panel.selected_entry(),
6376 cx,
6377 ),
6378 r#"/frontend-project/
6379 public/lottie/
6380 syntax-tree.json
6381 search: { "something": "static" } <==== selected
6382 src/
6383 app/(site)/
6384 (about)/jobs/[slug]/
6385 page.tsx
6386 search: static
6387 (blog)/post/[slug]/
6388 page.tsx
6389 search: static
6390 components/
6391 ErrorBoundary.tsx
6392 search: static"#
6393 );
6394 });
6395
6396 outline_panel.update_in(cx, |outline_panel, window, cx| {
6397 // Move to 5th element in the list, 3 items down.
6398 for _ in 0..2 {
6399 outline_panel.select_next(&SelectNext, window, cx);
6400 }
6401 outline_panel.collapse_selected_entry(&CollapseSelectedEntry, window, cx);
6402 });
6403 cx.executor()
6404 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6405 cx.run_until_parked();
6406 outline_panel.update(cx, |outline_panel, cx| {
6407 assert_eq!(
6408 display_entries(
6409 &project,
6410 &snapshot(&outline_panel, cx),
6411 &outline_panel.cached_entries,
6412 outline_panel.selected_entry(),
6413 cx,
6414 ),
6415 r#"/frontend-project/
6416 public/lottie/
6417 syntax-tree.json
6418 search: { "something": "static" }
6419 src/
6420 app/(site)/ <==== selected
6421 components/
6422 ErrorBoundary.tsx
6423 search: static"#
6424 );
6425 });
6426
6427 outline_panel.update_in(cx, |outline_panel, window, cx| {
6428 // Move to the next visible non-FS entry
6429 for _ in 0..3 {
6430 outline_panel.select_next(&SelectNext, window, cx);
6431 }
6432 });
6433 cx.run_until_parked();
6434 outline_panel.update(cx, |outline_panel, cx| {
6435 assert_eq!(
6436 display_entries(
6437 &project,
6438 &snapshot(&outline_panel, cx),
6439 &outline_panel.cached_entries,
6440 outline_panel.selected_entry(),
6441 cx,
6442 ),
6443 r#"/frontend-project/
6444 public/lottie/
6445 syntax-tree.json
6446 search: { "something": "static" }
6447 src/
6448 app/(site)/
6449 components/
6450 ErrorBoundary.tsx
6451 search: static <==== selected"#
6452 );
6453 });
6454
6455 outline_panel.update_in(cx, |outline_panel, window, cx| {
6456 outline_panel
6457 .active_editor()
6458 .expect("Should have an active editor")
6459 .update(cx, |editor, cx| {
6460 editor.toggle_fold(&editor::actions::ToggleFold, window, cx)
6461 });
6462 });
6463 cx.executor()
6464 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6465 cx.run_until_parked();
6466 outline_panel.update(cx, |outline_panel, cx| {
6467 assert_eq!(
6468 display_entries(
6469 &project,
6470 &snapshot(&outline_panel, cx),
6471 &outline_panel.cached_entries,
6472 outline_panel.selected_entry(),
6473 cx,
6474 ),
6475 r#"/frontend-project/
6476 public/lottie/
6477 syntax-tree.json
6478 search: { "something": "static" }
6479 src/
6480 app/(site)/
6481 components/
6482 ErrorBoundary.tsx <==== selected"#
6483 );
6484 });
6485
6486 outline_panel.update_in(cx, |outline_panel, window, cx| {
6487 outline_panel
6488 .active_editor()
6489 .expect("Should have an active editor")
6490 .update(cx, |editor, cx| {
6491 editor.toggle_fold(&editor::actions::ToggleFold, window, cx)
6492 });
6493 });
6494 cx.executor()
6495 .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
6496 cx.run_until_parked();
6497 outline_panel.update(cx, |outline_panel, cx| {
6498 assert_eq!(
6499 display_entries(
6500 &project,
6501 &snapshot(&outline_panel, cx),
6502 &outline_panel.cached_entries,
6503 outline_panel.selected_entry(),
6504 cx,
6505 ),
6506 r#"/frontend-project/
6507 public/lottie/
6508 syntax-tree.json
6509 search: { "something": "static" }
6510 src/
6511 app/(site)/
6512 components/
6513 ErrorBoundary.tsx <==== selected
6514 search: static"#
6515 );
6516 });
6517 }
6518
6519 async fn add_outline_panel(
6520 project: &Entity<Project>,
6521 cx: &mut TestAppContext,
6522 ) -> WindowHandle<Workspace> {
6523 let window = cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
6524
6525 let outline_panel = window
6526 .update(cx, |_, window, cx| {
6527 cx.spawn_in(window, async |this, cx| {
6528 OutlinePanel::load(this, cx.clone()).await
6529 })
6530 })
6531 .unwrap()
6532 .await
6533 .expect("Failed to load outline panel");
6534
6535 window
6536 .update(cx, |workspace, window, cx| {
6537 workspace.add_panel(outline_panel, window, cx);
6538 })
6539 .unwrap();
6540 window
6541 }
6542
6543 fn outline_panel(
6544 workspace: &WindowHandle<Workspace>,
6545 cx: &mut TestAppContext,
6546 ) -> Entity<OutlinePanel> {
6547 workspace
6548 .update(cx, |workspace, _, cx| {
6549 workspace
6550 .panel::<OutlinePanel>(cx)
6551 .expect("no outline panel")
6552 })
6553 .unwrap()
6554 }
6555
6556 fn display_entries(
6557 project: &Entity<Project>,
6558 multi_buffer_snapshot: &MultiBufferSnapshot,
6559 cached_entries: &[CachedEntry],
6560 selected_entry: Option<&PanelEntry>,
6561 cx: &mut App,
6562 ) -> String {
6563 let mut display_string = String::new();
6564 for entry in cached_entries {
6565 if !display_string.is_empty() {
6566 display_string += "\n";
6567 }
6568 for _ in 0..entry.depth {
6569 display_string += " ";
6570 }
6571 display_string += &match &entry.entry {
6572 PanelEntry::Fs(entry) => match entry {
6573 FsEntry::ExternalFile(_) => {
6574 panic!("Did not cover external files with tests")
6575 }
6576 FsEntry::Directory(directory) => {
6577 match project
6578 .read(cx)
6579 .worktree_for_id(directory.worktree_id, cx)
6580 .and_then(|worktree| {
6581 if worktree.read(cx).root_entry() == Some(&directory.entry.entry) {
6582 Some(worktree.read(cx).abs_path())
6583 } else {
6584 None
6585 }
6586 }) {
6587 Some(root_path) => format!(
6588 "{}/{}",
6589 root_path.display(),
6590 directory.entry.path.display(),
6591 ),
6592 None => format!(
6593 "{}/",
6594 directory
6595 .entry
6596 .path
6597 .file_name()
6598 .unwrap_or_default()
6599 .to_string_lossy()
6600 ),
6601 }
6602 }
6603 FsEntry::File(file) => file
6604 .entry
6605 .path
6606 .file_name()
6607 .map(|name| name.to_string_lossy().to_string())
6608 .unwrap_or_default(),
6609 },
6610 PanelEntry::FoldedDirs(folded_dirs) => folded_dirs
6611 .entries
6612 .iter()
6613 .filter_map(|dir| dir.path.file_name())
6614 .map(|name| name.to_string_lossy().to_string() + "/")
6615 .collect(),
6616 PanelEntry::Outline(outline_entry) => match outline_entry {
6617 OutlineEntry::Excerpt(_) => continue,
6618 OutlineEntry::Outline(outline_entry) => {
6619 format!("outline: {}", outline_entry.outline.text)
6620 }
6621 },
6622 PanelEntry::Search(search_entry) => {
6623 format!(
6624 "search: {}",
6625 search_entry
6626 .render_data
6627 .get_or_init(|| SearchData::new(
6628 &search_entry.match_range,
6629 &multi_buffer_snapshot
6630 ))
6631 .context_text
6632 )
6633 }
6634 };
6635
6636 if Some(&entry.entry) == selected_entry {
6637 display_string += SELECTED_MARKER;
6638 }
6639 }
6640 display_string
6641 }
6642
6643 fn init_test(cx: &mut TestAppContext) {
6644 cx.update(|cx| {
6645 let settings = SettingsStore::test(cx);
6646 cx.set_global(settings);
6647
6648 theme::init(theme::LoadThemes::JustBase, cx);
6649
6650 language::init(cx);
6651 editor::init(cx);
6652 workspace::init_settings(cx);
6653 Project::init_settings(cx);
6654 project_search::init(cx);
6655 super::init(cx);
6656 });
6657 }
6658
6659 // Based on https://github.com/rust-lang/rust-analyzer/
6660 async fn populate_with_test_ra_project(fs: &FakeFs, root: &str) {
6661 fs.insert_tree(
6662 root,
6663 json!({
6664 "crates": {
6665 "ide": {
6666 "src": {
6667 "inlay_hints": {
6668 "fn_lifetime_fn.rs": r##"
6669 pub(super) fn hints(
6670 acc: &mut Vec<InlayHint>,
6671 config: &InlayHintsConfig,
6672 func: ast::Fn,
6673 ) -> Option<()> {
6674 // ... snip
6675
6676 let mut used_names: FxHashMap<SmolStr, usize> =
6677 match config.param_names_for_lifetime_elision_hints {
6678 true => generic_param_list
6679 .iter()
6680 .flat_map(|gpl| gpl.lifetime_params())
6681 .filter_map(|param| param.lifetime())
6682 .filter_map(|lt| Some((SmolStr::from(lt.text().as_str().get(1..)?), 0)))
6683 .collect(),
6684 false => Default::default(),
6685 };
6686 {
6687 let mut potential_lt_refs = potential_lt_refs.iter().filter(|&&(.., is_elided)| is_elided);
6688 if self_param.is_some() && potential_lt_refs.next().is_some() {
6689 allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
6690 // self can't be used as a lifetime, so no need to check for collisions
6691 "'self".into()
6692 } else {
6693 gen_idx_name()
6694 });
6695 }
6696 potential_lt_refs.for_each(|(name, ..)| {
6697 let name = match name {
6698 Some(it) if config.param_names_for_lifetime_elision_hints => {
6699 if let Some(c) = used_names.get_mut(it.text().as_str()) {
6700 *c += 1;
6701 SmolStr::from(format!("'{text}{c}", text = it.text().as_str()))
6702 } else {
6703 used_names.insert(it.text().as_str().into(), 0);
6704 SmolStr::from_iter(["\'", it.text().as_str()])
6705 }
6706 }
6707 _ => gen_idx_name(),
6708 };
6709 allocated_lifetimes.push(name);
6710 });
6711 }
6712
6713 // ... snip
6714 }
6715
6716 // ... snip
6717
6718 #[test]
6719 fn hints_lifetimes_named() {
6720 check_with_config(
6721 InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
6722 r#"
6723 fn nested_in<'named>(named: & &X< &()>) {}
6724 // ^'named1, 'named2, 'named3, $
6725 //^'named1 ^'named2 ^'named3
6726 "#,
6727 );
6728 }
6729
6730 // ... snip
6731 "##,
6732 },
6733 "inlay_hints.rs": r#"
6734 #[derive(Clone, Debug, PartialEq, Eq)]
6735 pub struct InlayHintsConfig {
6736 // ... snip
6737 pub param_names_for_lifetime_elision_hints: bool,
6738 pub max_length: Option<usize>,
6739 // ... snip
6740 }
6741
6742 impl Config {
6743 pub fn inlay_hints(&self) -> InlayHintsConfig {
6744 InlayHintsConfig {
6745 // ... snip
6746 param_names_for_lifetime_elision_hints: self
6747 .inlayHints_lifetimeElisionHints_useParameterNames()
6748 .to_owned(),
6749 max_length: self.inlayHints_maxLength().to_owned(),
6750 // ... snip
6751 }
6752 }
6753 }
6754 "#,
6755 "static_index.rs": r#"
6756// ... snip
6757 fn add_file(&mut self, file_id: FileId) {
6758 let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
6759 let folds = self.analysis.folding_ranges(file_id).unwrap();
6760 let inlay_hints = self
6761 .analysis
6762 .inlay_hints(
6763 &InlayHintsConfig {
6764 // ... snip
6765 closure_style: hir::ClosureStyle::ImplFn,
6766 param_names_for_lifetime_elision_hints: false,
6767 binding_mode_hints: false,
6768 max_length: Some(25),
6769 closure_capture_hints: false,
6770 // ... snip
6771 },
6772 file_id,
6773 None,
6774 )
6775 .unwrap();
6776 // ... snip
6777 }
6778// ... snip
6779 "#
6780 }
6781 },
6782 "rust-analyzer": {
6783 "src": {
6784 "cli": {
6785 "analysis_stats.rs": r#"
6786 // ... snip
6787 for &file_id in &file_ids {
6788 _ = analysis.inlay_hints(
6789 &InlayHintsConfig {
6790 // ... snip
6791 implicit_drop_hints: true,
6792 lifetime_elision_hints: ide::LifetimeElisionHints::Always,
6793 param_names_for_lifetime_elision_hints: true,
6794 hide_named_constructor_hints: false,
6795 hide_closure_initialization_hints: false,
6796 closure_style: hir::ClosureStyle::ImplFn,
6797 max_length: Some(25),
6798 closing_brace_hints_min_lines: Some(20),
6799 fields_to_resolve: InlayFieldsToResolve::empty(),
6800 range_exclusive_hints: true,
6801 },
6802 file_id.into(),
6803 None,
6804 );
6805 }
6806 // ... snip
6807 "#,
6808 },
6809 "config.rs": r#"
6810 config_data! {
6811 /// Configs that only make sense when they are set by a client. As such they can only be defined
6812 /// by setting them using client's settings (e.g `settings.json` on VS Code).
6813 client: struct ClientDefaultConfigData <- ClientConfigInput -> {
6814 // ... snip
6815 /// Maximum length for inlay hints. Set to null to have an unlimited length.
6816 inlayHints_maxLength: Option<usize> = Some(25),
6817 // ... snip
6818 /// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
6819 inlayHints_lifetimeElisionHints_useParameterNames: bool = false,
6820 // ... snip
6821 }
6822 }
6823
6824 impl Config {
6825 // ... snip
6826 pub fn inlay_hints(&self) -> InlayHintsConfig {
6827 InlayHintsConfig {
6828 // ... snip
6829 param_names_for_lifetime_elision_hints: self
6830 .inlayHints_lifetimeElisionHints_useParameterNames()
6831 .to_owned(),
6832 max_length: self.inlayHints_maxLength().to_owned(),
6833 // ... snip
6834 }
6835 }
6836 // ... snip
6837 }
6838 "#
6839 }
6840 }
6841 }
6842 }),
6843 )
6844 .await;
6845 }
6846
6847 fn rust_lang() -> Language {
6848 Language::new(
6849 LanguageConfig {
6850 name: "Rust".into(),
6851 matcher: LanguageMatcher {
6852 path_suffixes: vec!["rs".to_string()],
6853 ..Default::default()
6854 },
6855 ..Default::default()
6856 },
6857 Some(tree_sitter_rust::LANGUAGE.into()),
6858 )
6859 .with_highlights_query(
6860 r#"
6861 (field_identifier) @field
6862 (struct_expression) @struct
6863 "#,
6864 )
6865 .unwrap()
6866 .with_injection_query(
6867 r#"
6868 (macro_invocation
6869 (token_tree) @injection.content
6870 (#set! injection.language "rust"))
6871 "#,
6872 )
6873 .unwrap()
6874 }
6875
6876 fn snapshot(outline_panel: &OutlinePanel, cx: &App) -> MultiBufferSnapshot {
6877 outline_panel
6878 .active_editor()
6879 .unwrap()
6880 .read(cx)
6881 .buffer()
6882 .read(cx)
6883 .snapshot(cx)
6884 }
6885
6886 fn selected_row_text(editor: &Entity<Editor>, cx: &mut App) -> String {
6887 editor.update(cx, |editor, cx| {
6888 let selections = editor.selections.all::<language::Point>(cx);
6889 assert_eq!(selections.len(), 1, "Active editor should have exactly one selection after any outline panel interactions");
6890 let selection = selections.first().unwrap();
6891 let multi_buffer_snapshot = editor.buffer().read(cx).snapshot(cx);
6892 let line_start = language::Point::new(selection.start.row, 0);
6893 let line_end = multi_buffer_snapshot.clip_point(language::Point::new(selection.end.row, u32::MAX), language::Bias::Right);
6894 multi_buffer_snapshot.text_for_range(line_start..line_end).collect::<String>().trim().to_owned()
6895 })
6896 }
6897}