outline.rs

  1use std::ops::Range;
  2use std::{
  3    cmp::{self, Reverse},
  4    sync::Arc,
  5};
  6
  7use editor::{Anchor, AnchorRangeExt, Editor, scroll::Autoscroll};
  8use editor::{RowHighlightOptions, SelectionEffects};
  9use fuzzy::StringMatch;
 10use gpui::{
 11    App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, HighlightStyle,
 12    ParentElement, Point, Render, Styled, StyledText, Task, TextStyle, WeakEntity, Window, div,
 13    rems,
 14};
 15use language::{Outline, OutlineItem};
 16use ordered_float::OrderedFloat;
 17use picker::{Picker, PickerDelegate};
 18use settings::Settings;
 19use theme::{ActiveTheme, ThemeSettings};
 20use ui::{ListItem, ListItemSpacing, prelude::*};
 21use util::ResultExt;
 22use workspace::{DismissDecision, ModalView};
 23
 24pub fn init(cx: &mut App) {
 25    cx.observe_new(OutlineView::register).detach();
 26    zed_actions::outline::TOGGLE_OUTLINE
 27        .set(|view, window, cx| {
 28            let Ok(editor) = view.downcast::<Editor>() else {
 29                return;
 30            };
 31
 32            toggle(editor, &Default::default(), window, cx);
 33        })
 34        .ok();
 35}
 36
 37pub fn toggle(
 38    editor: Entity<Editor>,
 39    _: &zed_actions::outline::ToggleOutline,
 40    window: &mut Window,
 41    cx: &mut App,
 42) {
 43    let outline = editor
 44        .read(cx)
 45        .buffer()
 46        .read(cx)
 47        .snapshot(cx)
 48        .outline(Some(cx.theme().syntax()));
 49
 50    if let Some((workspace, outline)) = editor.read(cx).workspace().zip(outline) {
 51        workspace.update(cx, |workspace, cx| {
 52            workspace.toggle_modal(window, cx, |window, cx| {
 53                OutlineView::new(outline, editor, window, cx)
 54            });
 55        })
 56    }
 57}
 58
 59pub struct OutlineView {
 60    picker: Entity<Picker<OutlineViewDelegate>>,
 61}
 62
 63impl Focusable for OutlineView {
 64    fn focus_handle(&self, cx: &App) -> FocusHandle {
 65        self.picker.focus_handle(cx)
 66    }
 67}
 68
 69impl EventEmitter<DismissEvent> for OutlineView {}
 70impl ModalView for OutlineView {
 71    fn on_before_dismiss(
 72        &mut self,
 73        window: &mut Window,
 74        cx: &mut Context<Self>,
 75    ) -> DismissDecision {
 76        self.picker.update(cx, |picker, cx| {
 77            picker.delegate.restore_active_editor(window, cx)
 78        });
 79        DismissDecision::Dismiss(true)
 80    }
 81}
 82
 83impl Render for OutlineView {
 84    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
 85        v_flex().w(rems(34.)).child(self.picker.clone())
 86    }
 87}
 88
 89impl OutlineView {
 90    fn register(editor: &mut Editor, _: Option<&mut Window>, cx: &mut Context<Editor>) {
 91        if editor.mode().is_full() {
 92            let handle = cx.entity().downgrade();
 93            editor
 94                .register_action(move |action, window, cx| {
 95                    if let Some(editor) = handle.upgrade() {
 96                        toggle(editor, action, window, cx);
 97                    }
 98                })
 99                .detach();
100        }
101    }
102
103    fn new(
104        outline: Outline<Anchor>,
105        editor: Entity<Editor>,
106        window: &mut Window,
107        cx: &mut Context<Self>,
108    ) -> OutlineView {
109        let delegate = OutlineViewDelegate::new(cx.entity().downgrade(), outline, editor, cx);
110        let picker = cx.new(|cx| {
111            Picker::uniform_list(delegate, window, cx).max_height(Some(vh(0.75, window)))
112        });
113        OutlineView { picker }
114    }
115}
116
117struct OutlineViewDelegate {
118    outline_view: WeakEntity<OutlineView>,
119    active_editor: Entity<Editor>,
120    outline: Outline<Anchor>,
121    selected_match_index: usize,
122    prev_scroll_position: Option<Point<f32>>,
123    matches: Vec<StringMatch>,
124    last_query: String,
125}
126
127enum OutlineRowHighlights {}
128
129impl OutlineViewDelegate {
130    fn new(
131        outline_view: WeakEntity<OutlineView>,
132        outline: Outline<Anchor>,
133        editor: Entity<Editor>,
134
135        cx: &mut Context<OutlineView>,
136    ) -> Self {
137        Self {
138            outline_view,
139            last_query: Default::default(),
140            matches: Default::default(),
141            selected_match_index: 0,
142            prev_scroll_position: Some(editor.update(cx, |editor, cx| editor.scroll_position(cx))),
143            active_editor: editor,
144            outline,
145        }
146    }
147
148    fn restore_active_editor(&mut self, window: &mut Window, cx: &mut App) {
149        self.active_editor.update(cx, |editor, cx| {
150            editor.clear_row_highlights::<OutlineRowHighlights>();
151            if let Some(scroll_position) = self.prev_scroll_position {
152                editor.set_scroll_position(scroll_position, window, cx);
153            }
154        })
155    }
156
157    fn set_selected_index(
158        &mut self,
159        ix: usize,
160        navigate: bool,
161
162        cx: &mut Context<Picker<OutlineViewDelegate>>,
163    ) {
164        self.selected_match_index = ix;
165
166        if navigate && !self.matches.is_empty() {
167            let selected_match = &self.matches[self.selected_match_index];
168            let outline_item = &self.outline.items[selected_match.candidate_id];
169
170            self.active_editor.update(cx, |active_editor, cx| {
171                active_editor.clear_row_highlights::<OutlineRowHighlights>();
172                active_editor.highlight_rows::<OutlineRowHighlights>(
173                    outline_item.range.start..outline_item.range.end,
174                    cx.theme().colors().editor_highlighted_line_background,
175                    RowHighlightOptions {
176                        autoscroll: true,
177                        ..Default::default()
178                    },
179                    cx,
180                );
181                active_editor.request_autoscroll(Autoscroll::center(), cx);
182            });
183        }
184    }
185}
186
187impl PickerDelegate for OutlineViewDelegate {
188    type ListItem = ListItem;
189
190    fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> Arc<str> {
191        "Search buffer symbols...".into()
192    }
193
194    fn match_count(&self) -> usize {
195        self.matches.len()
196    }
197
198    fn selected_index(&self) -> usize {
199        self.selected_match_index
200    }
201
202    fn set_selected_index(
203        &mut self,
204        ix: usize,
205        _: &mut Window,
206        cx: &mut Context<Picker<OutlineViewDelegate>>,
207    ) {
208        self.set_selected_index(ix, true, cx);
209    }
210
211    fn update_matches(
212        &mut self,
213        query: String,
214        window: &mut Window,
215        cx: &mut Context<Picker<OutlineViewDelegate>>,
216    ) -> Task<()> {
217        let selected_index;
218        if query.is_empty() {
219            self.restore_active_editor(window, cx);
220            self.matches = self
221                .outline
222                .items
223                .iter()
224                .enumerate()
225                .map(|(index, _)| StringMatch {
226                    candidate_id: index,
227                    score: Default::default(),
228                    positions: Default::default(),
229                    string: Default::default(),
230                })
231                .collect();
232
233            let (buffer, cursor_offset) = self.active_editor.update(cx, |editor, cx| {
234                let buffer = editor.buffer().read(cx).snapshot(cx);
235                let cursor_offset = editor.selections.newest::<usize>(cx).head();
236                (buffer, cursor_offset)
237            });
238            selected_index = self
239                .outline
240                .items
241                .iter()
242                .enumerate()
243                .map(|(ix, item)| {
244                    let range = item.range.to_offset(&buffer);
245                    let distance_to_closest_endpoint = cmp::min(
246                        (range.start as isize - cursor_offset as isize).abs(),
247                        (range.end as isize - cursor_offset as isize).abs(),
248                    );
249                    let depth = if range.contains(&cursor_offset) {
250                        Some(item.depth)
251                    } else {
252                        None
253                    };
254                    (ix, depth, distance_to_closest_endpoint)
255                })
256                .max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance)))
257                .map(|(ix, _, _)| ix)
258                .unwrap_or(0);
259        } else {
260            self.matches = smol::block_on(
261                self.outline
262                    .search(&query, cx.background_executor().clone()),
263            );
264            selected_index = self
265                .matches
266                .iter()
267                .enumerate()
268                .max_by_key(|(_, m)| OrderedFloat(m.score))
269                .map(|(ix, _)| ix)
270                .unwrap_or(0);
271        }
272        self.last_query = query;
273        self.set_selected_index(selected_index, !self.last_query.is_empty(), cx);
274        Task::ready(())
275    }
276
277    fn confirm(
278        &mut self,
279        _: bool,
280        window: &mut Window,
281        cx: &mut Context<Picker<OutlineViewDelegate>>,
282    ) {
283        self.prev_scroll_position.take();
284        self.set_selected_index(self.selected_match_index, true, cx);
285
286        self.active_editor.update(cx, |active_editor, cx| {
287            let highlight = active_editor
288                .highlighted_rows::<OutlineRowHighlights>()
289                .next();
290            if let Some((rows, _)) = highlight {
291                active_editor.change_selections(
292                    SelectionEffects::scroll(Autoscroll::center()),
293                    window,
294                    cx,
295                    |s| s.select_ranges([rows.start..rows.start]),
296                );
297                active_editor.clear_row_highlights::<OutlineRowHighlights>();
298                window.focus(&active_editor.focus_handle(cx));
299            }
300        });
301
302        self.dismissed(window, cx);
303    }
304
305    fn dismissed(&mut self, window: &mut Window, cx: &mut Context<Picker<OutlineViewDelegate>>) {
306        self.outline_view
307            .update(cx, |_, cx| cx.emit(DismissEvent))
308            .log_err();
309        self.restore_active_editor(window, cx);
310    }
311
312    fn render_match(
313        &self,
314        ix: usize,
315        selected: bool,
316        _: &mut Window,
317        cx: &mut Context<Picker<Self>>,
318    ) -> Option<Self::ListItem> {
319        let mat = self.matches.get(ix)?;
320        let outline_item = self.outline.items.get(mat.candidate_id)?;
321
322        Some(
323            ListItem::new(ix)
324                .inset(true)
325                .spacing(ListItemSpacing::Sparse)
326                .toggle_state(selected)
327                .child(
328                    div()
329                        .text_ui(cx)
330                        .pl(rems(outline_item.depth as f32))
331                        .child(render_item(outline_item, mat.ranges(), cx)),
332                ),
333        )
334    }
335}
336
337pub fn render_item<T>(
338    outline_item: &OutlineItem<T>,
339    match_ranges: impl IntoIterator<Item = Range<usize>>,
340    cx: &App,
341) -> StyledText {
342    let highlight_style = HighlightStyle {
343        background_color: Some(cx.theme().colors().text_accent.alpha(0.3)),
344        ..Default::default()
345    };
346    let custom_highlights = match_ranges
347        .into_iter()
348        .map(|range| (range, highlight_style));
349
350    let settings = ThemeSettings::get_global(cx);
351
352    // TODO: We probably shouldn't need to build a whole new text style here
353    // but I'm not sure how to get the current one and modify it.
354    // Before this change TextStyle::default() was used here, which was giving us the wrong font and text color.
355    let text_style = TextStyle {
356        color: cx.theme().colors().text,
357        font_family: settings.buffer_font.family.clone(),
358        font_features: settings.buffer_font.features.clone(),
359        font_fallbacks: settings.buffer_font.fallbacks.clone(),
360        font_size: settings.buffer_font_size(cx).into(),
361        font_weight: settings.buffer_font.weight,
362        line_height: relative(1.),
363        ..Default::default()
364    };
365    let highlights = gpui::combine_highlights(
366        custom_highlights,
367        outline_item.highlight_ranges.iter().cloned(),
368    );
369
370    StyledText::new(outline_item.text.clone()).with_default_highlights(&text_style, highlights)
371}
372
373#[cfg(test)]
374mod tests {
375    use super::*;
376    use gpui::{TestAppContext, VisualTestContext};
377    use indoc::indoc;
378    use language::{Language, LanguageConfig, LanguageMatcher};
379    use project::{FakeFs, Project};
380    use serde_json::json;
381    use util::path;
382    use workspace::{AppState, Workspace};
383
384    #[gpui::test]
385    async fn test_outline_view_row_highlights(cx: &mut TestAppContext) {
386        init_test(cx);
387        let fs = FakeFs::new(cx.executor());
388        fs.insert_tree(
389            path!("/dir"),
390            json!({
391                "a.rs": indoc!{"
392                                       // display line 0
393                    struct SingleLine; // display line 1
394                                       // display line 2
395                    struct MultiLine { // display line 3
396                        field_1: i32,  // display line 4
397                        field_2: i32,  // display line 5
398                    }                  // display line 6
399                "}
400            }),
401        )
402        .await;
403
404        let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;
405        project.read_with(cx, |project, _| project.languages().add(rust_lang()));
406
407        let (workspace, cx) =
408            cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
409        let worktree_id = workspace.update(cx, |workspace, cx| {
410            workspace.project().update(cx, |project, cx| {
411                project.worktrees(cx).next().unwrap().read(cx).id()
412            })
413        });
414        let _buffer = project
415            .update(cx, |project, cx| {
416                project.open_local_buffer(path!("/dir/a.rs"), cx)
417            })
418            .await
419            .unwrap();
420        let editor = workspace
421            .update_in(cx, |workspace, window, cx| {
422                workspace.open_path((worktree_id, "a.rs"), None, true, window, cx)
423            })
424            .await
425            .unwrap()
426            .downcast::<Editor>()
427            .unwrap();
428        let ensure_outline_view_contents =
429            |outline_view: &Entity<Picker<OutlineViewDelegate>>, cx: &mut VisualTestContext| {
430                assert_eq!(query(outline_view, cx), "");
431                assert_eq!(
432                    outline_names(outline_view, cx),
433                    vec![
434                        "struct SingleLine",
435                        "struct MultiLine",
436                        "field_1",
437                        "field_2"
438                    ],
439                );
440            };
441
442        let outline_view = open_outline_view(&workspace, cx);
443        ensure_outline_view_contents(&outline_view, cx);
444        assert_eq!(
445            highlighted_display_rows(&editor, cx),
446            Vec::<u32>::new(),
447            "Initially opened outline view should have no highlights"
448        );
449        assert_single_caret_at_row(&editor, 0, cx);
450
451        cx.dispatch_action(menu::Confirm);
452        // Ensures that outline still goes to entry even if no queries have been made
453        assert_single_caret_at_row(&editor, 1, cx);
454
455        let outline_view = open_outline_view(&workspace, cx);
456
457        cx.dispatch_action(menu::SelectNext);
458        ensure_outline_view_contents(&outline_view, cx);
459        assert_eq!(
460            highlighted_display_rows(&editor, cx),
461            vec![3, 4, 5, 6],
462            "Second struct's rows should be highlighted"
463        );
464        assert_single_caret_at_row(&editor, 1, cx);
465
466        cx.dispatch_action(menu::SelectPrevious);
467        ensure_outline_view_contents(&outline_view, cx);
468        assert_eq!(
469            highlighted_display_rows(&editor, cx),
470            vec![1],
471            "First struct's row should be highlighted"
472        );
473        assert_single_caret_at_row(&editor, 1, cx);
474
475        cx.dispatch_action(menu::Cancel);
476        ensure_outline_view_contents(&outline_view, cx);
477        assert_eq!(
478            highlighted_display_rows(&editor, cx),
479            Vec::<u32>::new(),
480            "No rows should be highlighted after outline view is cancelled and closed"
481        );
482        assert_single_caret_at_row(&editor, 1, cx);
483
484        let outline_view = open_outline_view(&workspace, cx);
485        ensure_outline_view_contents(&outline_view, cx);
486        assert_eq!(
487            highlighted_display_rows(&editor, cx),
488            Vec::<u32>::new(),
489            "Reopened outline view should have no highlights"
490        );
491        assert_single_caret_at_row(&editor, 1, cx);
492
493        let expected_first_highlighted_row = 3;
494        cx.dispatch_action(menu::SelectNext);
495        ensure_outline_view_contents(&outline_view, cx);
496        assert_eq!(
497            highlighted_display_rows(&editor, cx),
498            vec![expected_first_highlighted_row, 4, 5, 6]
499        );
500        assert_single_caret_at_row(&editor, 1, cx);
501        cx.dispatch_action(menu::Confirm);
502        ensure_outline_view_contents(&outline_view, cx);
503        assert_eq!(
504            highlighted_display_rows(&editor, cx),
505            Vec::<u32>::new(),
506            "No rows should be highlighted after outline view is confirmed and closed"
507        );
508        // On confirm, should place the caret on the first row of the highlighted rows range.
509        assert_single_caret_at_row(&editor, expected_first_highlighted_row, cx);
510    }
511
512    fn open_outline_view(
513        workspace: &Entity<Workspace>,
514        cx: &mut VisualTestContext,
515    ) -> Entity<Picker<OutlineViewDelegate>> {
516        cx.dispatch_action(zed_actions::outline::ToggleOutline);
517        workspace.update(cx, |workspace, cx| {
518            workspace
519                .active_modal::<OutlineView>(cx)
520                .unwrap()
521                .read(cx)
522                .picker
523                .clone()
524        })
525    }
526
527    fn query(
528        outline_view: &Entity<Picker<OutlineViewDelegate>>,
529        cx: &mut VisualTestContext,
530    ) -> String {
531        outline_view.update(cx, |outline_view, cx| outline_view.query(cx))
532    }
533
534    fn outline_names(
535        outline_view: &Entity<Picker<OutlineViewDelegate>>,
536        cx: &mut VisualTestContext,
537    ) -> Vec<String> {
538        outline_view.read_with(cx, |outline_view, _| {
539            let items = &outline_view.delegate.outline.items;
540            outline_view
541                .delegate
542                .matches
543                .iter()
544                .map(|hit| items[hit.candidate_id].text.clone())
545                .collect::<Vec<_>>()
546        })
547    }
548
549    fn highlighted_display_rows(editor: &Entity<Editor>, cx: &mut VisualTestContext) -> Vec<u32> {
550        editor.update_in(cx, |editor, window, cx| {
551            editor
552                .highlighted_display_rows(window, cx)
553                .into_keys()
554                .map(|r| r.0)
555                .collect()
556        })
557    }
558
559    fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
560        cx.update(|cx| {
561            let state = AppState::test(cx);
562            language::init(cx);
563            crate::init(cx);
564            editor::init(cx);
565            workspace::init_settings(cx);
566            Project::init_settings(cx);
567            state
568        })
569    }
570
571    fn rust_lang() -> Arc<Language> {
572        Arc::new(
573            Language::new(
574                LanguageConfig {
575                    name: "Rust".into(),
576                    matcher: LanguageMatcher {
577                        path_suffixes: vec!["rs".to_string()],
578                        ..Default::default()
579                    },
580                    ..Default::default()
581                },
582                Some(tree_sitter_rust::LANGUAGE.into()),
583            )
584            .with_outline_query(
585                r#"(struct_item
586            (visibility_modifier)? @context
587            "struct" @context
588            name: (_) @name) @item
589
590        (enum_item
591            (visibility_modifier)? @context
592            "enum" @context
593            name: (_) @name) @item
594
595        (enum_variant
596            (visibility_modifier)? @context
597            name: (_) @name) @item
598
599        (impl_item
600            "impl" @context
601            trait: (_)? @name
602            "for"? @context
603            type: (_) @name) @item
604
605        (trait_item
606            (visibility_modifier)? @context
607            "trait" @context
608            name: (_) @name) @item
609
610        (function_item
611            (visibility_modifier)? @context
612            (function_modifiers)? @context
613            "fn" @context
614            name: (_) @name) @item
615
616        (function_signature_item
617            (visibility_modifier)? @context
618            (function_modifiers)? @context
619            "fn" @context
620            name: (_) @name) @item
621
622        (macro_definition
623            . "macro_rules!" @context
624            name: (_) @name) @item
625
626        (mod_item
627            (visibility_modifier)? @context
628            "mod" @context
629            name: (_) @name) @item
630
631        (type_item
632            (visibility_modifier)? @context
633            "type" @context
634            name: (_) @name) @item
635
636        (associated_type
637            "type" @context
638            name: (_) @name) @item
639
640        (const_item
641            (visibility_modifier)? @context
642            "const" @context
643            name: (_) @name) @item
644
645        (field_declaration
646            (visibility_modifier)? @context
647            name: (_) @name) @item
648"#,
649            )
650            .unwrap(),
651        )
652    }
653
654    #[track_caller]
655    fn assert_single_caret_at_row(
656        editor: &Entity<Editor>,
657        buffer_row: u32,
658        cx: &mut VisualTestContext,
659    ) {
660        let selections = editor.update(cx, |editor, cx| {
661            editor
662                .selections
663                .all::<rope::Point>(cx)
664                .into_iter()
665                .map(|s| s.start..s.end)
666                .collect::<Vec<_>>()
667        });
668        assert!(
669            selections.len() == 1,
670            "Expected one caret selection but got: {selections:?}"
671        );
672        let selection = &selections[0];
673        assert!(
674            selection.start == selection.end,
675            "Expected a single caret selection, but got: {selection:?}"
676        );
677        assert_eq!(selection.start.row, buffer_row);
678    }
679}