outline.rs

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