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