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