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 = cx.new_view(|cx| Picker::uniform_list(delegate, cx).max_height(vh(0.75, cx)));
 87        OutlineView { picker }
 88    }
 89}
 90
 91struct OutlineViewDelegate {
 92    outline_view: WeakView<OutlineView>,
 93    active_editor: View<Editor>,
 94    outline: Outline<Anchor>,
 95    selected_match_index: usize,
 96    prev_scroll_position: Option<Point<f32>>,
 97    matches: Vec<StringMatch>,
 98    last_query: String,
 99}
100
101enum OutlineRowHighlights {}
102
103impl OutlineViewDelegate {
104    fn new(
105        outline_view: WeakView<OutlineView>,
106        outline: Outline<Anchor>,
107        editor: View<Editor>,
108        cx: &mut ViewContext<OutlineView>,
109    ) -> Self {
110        Self {
111            outline_view,
112            last_query: Default::default(),
113            matches: Default::default(),
114            selected_match_index: 0,
115            prev_scroll_position: Some(editor.update(cx, |editor, cx| editor.scroll_position(cx))),
116            active_editor: editor,
117            outline,
118        }
119    }
120
121    fn restore_active_editor(&mut self, cx: &mut WindowContext) {
122        self.active_editor.update(cx, |editor, cx| {
123            editor.clear_row_highlights::<OutlineRowHighlights>();
124            if let Some(scroll_position) = self.prev_scroll_position {
125                editor.set_scroll_position(scroll_position, cx);
126            }
127        })
128    }
129
130    fn set_selected_index(
131        &mut self,
132        ix: usize,
133        navigate: bool,
134        cx: &mut ViewContext<Picker<OutlineViewDelegate>>,
135    ) {
136        self.selected_match_index = ix;
137
138        if navigate && !self.matches.is_empty() {
139            let selected_match = &self.matches[self.selected_match_index];
140            let outline_item = &self.outline.items[selected_match.candidate_id];
141
142            self.active_editor.update(cx, |active_editor, cx| {
143                active_editor.clear_row_highlights::<OutlineRowHighlights>();
144                active_editor.highlight_rows::<OutlineRowHighlights>(
145                    outline_item.range.start..=outline_item.range.end,
146                    Some(cx.theme().colors().editor_highlighted_line_background),
147                    cx,
148                );
149                active_editor.request_autoscroll(Autoscroll::center(), cx);
150            });
151        }
152    }
153}
154
155impl PickerDelegate for OutlineViewDelegate {
156    type ListItem = ListItem;
157
158    fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
159        "Search buffer symbols...".into()
160    }
161
162    fn match_count(&self) -> usize {
163        self.matches.len()
164    }
165
166    fn selected_index(&self) -> usize {
167        self.selected_match_index
168    }
169
170    fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<OutlineViewDelegate>>) {
171        self.set_selected_index(ix, true, cx);
172    }
173
174    fn update_matches(
175        &mut self,
176        query: String,
177        cx: &mut ViewContext<Picker<OutlineViewDelegate>>,
178    ) -> Task<()> {
179        let selected_index;
180        if query.is_empty() {
181            self.restore_active_editor(cx);
182            self.matches = self
183                .outline
184                .items
185                .iter()
186                .enumerate()
187                .map(|(index, _)| StringMatch {
188                    candidate_id: index,
189                    score: Default::default(),
190                    positions: Default::default(),
191                    string: Default::default(),
192                })
193                .collect();
194
195            let editor = self.active_editor.read(cx);
196            let cursor_offset = editor.selections.newest::<usize>(cx).head();
197            let buffer = editor.buffer().read(cx).snapshot(cx);
198            selected_index = self
199                .outline
200                .items
201                .iter()
202                .enumerate()
203                .map(|(ix, item)| {
204                    let range = item.range.to_offset(&buffer);
205                    let distance_to_closest_endpoint = cmp::min(
206                        (range.start as isize - cursor_offset as isize).abs(),
207                        (range.end as isize - cursor_offset as isize).abs(),
208                    );
209                    let depth = if range.contains(&cursor_offset) {
210                        Some(item.depth)
211                    } else {
212                        None
213                    };
214                    (ix, depth, distance_to_closest_endpoint)
215                })
216                .max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance)))
217                .map(|(ix, _, _)| ix)
218                .unwrap_or(0);
219        } else {
220            self.matches = smol::block_on(
221                self.outline
222                    .search(&query, cx.background_executor().clone()),
223            );
224            selected_index = self
225                .matches
226                .iter()
227                .enumerate()
228                .max_by_key(|(_, m)| OrderedFloat(m.score))
229                .map(|(ix, _)| ix)
230                .unwrap_or(0);
231        }
232        self.last_query = query;
233        self.set_selected_index(selected_index, !self.last_query.is_empty(), cx);
234        Task::ready(())
235    }
236
237    fn confirm(&mut self, _: bool, cx: &mut ViewContext<Picker<OutlineViewDelegate>>) {
238        self.prev_scroll_position.take();
239
240        self.active_editor.update(cx, |active_editor, cx| {
241            if let Some(rows) = active_editor
242                .highlighted_rows::<OutlineRowHighlights>()
243                .and_then(|highlights| highlights.into_iter().next().map(|(rows, _)| rows.clone()))
244            {
245                active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
246                    s.select_ranges([*rows.start()..*rows.start()])
247                });
248                active_editor.clear_row_highlights::<OutlineRowHighlights>();
249                active_editor.focus(cx);
250            }
251        });
252
253        self.dismissed(cx);
254    }
255
256    fn dismissed(&mut self, cx: &mut ViewContext<Picker<OutlineViewDelegate>>) {
257        self.outline_view
258            .update(cx, |_, cx| cx.emit(DismissEvent))
259            .log_err();
260        self.restore_active_editor(cx);
261    }
262
263    fn render_match(
264        &self,
265        ix: usize,
266        selected: bool,
267        cx: &mut ViewContext<Picker<Self>>,
268    ) -> Option<Self::ListItem> {
269        let settings = ThemeSettings::get_global(cx);
270
271        // TODO: We probably shouldn't need to build a whole new text style here
272        // but I'm not sure how to get the current one and modify it.
273        // Before this change TextStyle::default() was used here, which was giving us the wrong font and text color.
274        let text_style = TextStyle {
275            color: cx.theme().colors().text,
276            font_family: settings.buffer_font.family.clone(),
277            font_features: settings.buffer_font.features.clone(),
278            font_size: settings.buffer_font_size(cx).into(),
279            font_weight: FontWeight::NORMAL,
280            font_style: FontStyle::Normal,
281            line_height: relative(1.),
282            background_color: None,
283            underline: None,
284            strikethrough: None,
285            white_space: WhiteSpace::Normal,
286        };
287
288        let mut highlight_style = HighlightStyle::default();
289        highlight_style.background_color = Some(color_alpha(cx.theme().colors().text_accent, 0.3));
290
291        let mat = &self.matches[ix];
292        let outline_item = &self.outline.items[mat.candidate_id];
293
294        let highlights = gpui::combine_highlights(
295            mat.ranges().map(|range| (range, highlight_style)),
296            outline_item.highlight_ranges.iter().cloned(),
297        );
298
299        let styled_text =
300            StyledText::new(outline_item.text.clone()).with_highlights(&text_style, highlights);
301
302        Some(
303            ListItem::new(ix)
304                .inset(true)
305                .spacing(ListItemSpacing::Sparse)
306                .selected(selected)
307                .child(
308                    div()
309                        .text_ui(cx)
310                        .pl(rems(outline_item.depth as f32))
311                        .child(styled_text),
312                ),
313        )
314    }
315}
316
317#[cfg(test)]
318mod tests {
319    use collections::HashSet;
320    use gpui::{TestAppContext, VisualTestContext};
321    use indoc::indoc;
322    use language::{Language, LanguageConfig, LanguageMatcher};
323    use project::{FakeFs, Project};
324    use serde_json::json;
325    use workspace::{AppState, Workspace};
326
327    use super::*;
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(HashSet::default(), 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}