outline.rs

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