outline.rs

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