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