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::{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::<usize>(&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 as isize - cursor_offset as isize).abs(),
263 (range.end as isize - cursor_offset 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 language::{Language, LanguageConfig, LanguageMatcher};
395 use project::{FakeFs, Project};
396 use serde_json::json;
397 use util::{path, rel_path::rel_path};
398 use workspace::{AppState, Workspace};
399
400 #[gpui::test]
401 async fn test_outline_view_row_highlights(cx: &mut TestAppContext) {
402 init_test(cx);
403 let fs = FakeFs::new(cx.executor());
404 fs.insert_tree(
405 path!("/dir"),
406 json!({
407 "a.rs": indoc!{"
408 // display line 0
409 struct SingleLine; // display line 1
410 // display line 2
411 struct MultiLine { // display line 3
412 field_1: i32, // display line 4
413 field_2: i32, // display line 5
414 } // display line 6
415 "}
416 }),
417 )
418 .await;
419
420 let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;
421 project.read_with(cx, |project, _| project.languages().add(rust_lang()));
422
423 let (workspace, cx) =
424 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
425 let worktree_id = workspace.update(cx, |workspace, cx| {
426 workspace.project().update(cx, |project, cx| {
427 project.worktrees(cx).next().unwrap().read(cx).id()
428 })
429 });
430 let _buffer = project
431 .update(cx, |project, cx| {
432 project.open_local_buffer(path!("/dir/a.rs"), cx)
433 })
434 .await
435 .unwrap();
436 let editor = workspace
437 .update_in(cx, |workspace, window, cx| {
438 workspace.open_path((worktree_id, rel_path("a.rs")), None, true, window, cx)
439 })
440 .await
441 .unwrap()
442 .downcast::<Editor>()
443 .unwrap();
444 let ensure_outline_view_contents =
445 |outline_view: &Entity<Picker<OutlineViewDelegate>>, cx: &mut VisualTestContext| {
446 assert_eq!(query(outline_view, cx), "");
447 assert_eq!(
448 outline_names(outline_view, cx),
449 vec![
450 "struct SingleLine",
451 "struct MultiLine",
452 "field_1",
453 "field_2"
454 ],
455 );
456 };
457
458 let outline_view = open_outline_view(&workspace, cx);
459 ensure_outline_view_contents(&outline_view, cx);
460 assert_eq!(
461 highlighted_display_rows(&editor, cx),
462 Vec::<u32>::new(),
463 "Initially opened outline view should have no highlights"
464 );
465 assert_single_caret_at_row(&editor, 0, cx);
466
467 cx.dispatch_action(menu::Confirm);
468 // Ensures that outline still goes to entry even if no queries have been made
469 assert_single_caret_at_row(&editor, 1, cx);
470
471 let outline_view = open_outline_view(&workspace, cx);
472
473 cx.dispatch_action(menu::SelectNext);
474 ensure_outline_view_contents(&outline_view, cx);
475 assert_eq!(
476 highlighted_display_rows(&editor, cx),
477 vec![3, 4, 5, 6],
478 "Second struct's rows should be highlighted"
479 );
480 assert_single_caret_at_row(&editor, 1, cx);
481
482 cx.dispatch_action(menu::SelectPrevious);
483 ensure_outline_view_contents(&outline_view, cx);
484 assert_eq!(
485 highlighted_display_rows(&editor, cx),
486 vec![1],
487 "First struct's row should be highlighted"
488 );
489 assert_single_caret_at_row(&editor, 1, cx);
490
491 cx.dispatch_action(menu::Cancel);
492 ensure_outline_view_contents(&outline_view, cx);
493 assert_eq!(
494 highlighted_display_rows(&editor, cx),
495 Vec::<u32>::new(),
496 "No rows should be highlighted after outline view is cancelled and closed"
497 );
498 assert_single_caret_at_row(&editor, 1, cx);
499
500 let outline_view = open_outline_view(&workspace, cx);
501 ensure_outline_view_contents(&outline_view, cx);
502 assert_eq!(
503 highlighted_display_rows(&editor, cx),
504 Vec::<u32>::new(),
505 "Reopened outline view should have no highlights"
506 );
507 assert_single_caret_at_row(&editor, 1, cx);
508
509 let expected_first_highlighted_row = 3;
510 cx.dispatch_action(menu::SelectNext);
511 ensure_outline_view_contents(&outline_view, cx);
512 assert_eq!(
513 highlighted_display_rows(&editor, cx),
514 vec![expected_first_highlighted_row, 4, 5, 6]
515 );
516 assert_single_caret_at_row(&editor, 1, cx);
517 cx.dispatch_action(menu::Confirm);
518 ensure_outline_view_contents(&outline_view, cx);
519 assert_eq!(
520 highlighted_display_rows(&editor, cx),
521 Vec::<u32>::new(),
522 "No rows should be highlighted after outline view is confirmed and closed"
523 );
524 // On confirm, should place the caret on the first row of the highlighted rows range.
525 assert_single_caret_at_row(&editor, expected_first_highlighted_row, cx);
526 }
527
528 fn open_outline_view(
529 workspace: &Entity<Workspace>,
530 cx: &mut VisualTestContext,
531 ) -> Entity<Picker<OutlineViewDelegate>> {
532 cx.dispatch_action(zed_actions::outline::ToggleOutline);
533 workspace.update(cx, |workspace, cx| {
534 workspace
535 .active_modal::<OutlineView>(cx)
536 .unwrap()
537 .read(cx)
538 .picker
539 .clone()
540 })
541 }
542
543 fn query(
544 outline_view: &Entity<Picker<OutlineViewDelegate>>,
545 cx: &mut VisualTestContext,
546 ) -> String {
547 outline_view.update(cx, |outline_view, cx| outline_view.query(cx))
548 }
549
550 fn outline_names(
551 outline_view: &Entity<Picker<OutlineViewDelegate>>,
552 cx: &mut VisualTestContext,
553 ) -> Vec<String> {
554 outline_view.read_with(cx, |outline_view, _| {
555 let items = &outline_view.delegate.outline.items;
556 outline_view
557 .delegate
558 .matches
559 .iter()
560 .map(|hit| items[hit.candidate_id].text.clone())
561 .collect::<Vec<_>>()
562 })
563 }
564
565 fn highlighted_display_rows(editor: &Entity<Editor>, cx: &mut VisualTestContext) -> Vec<u32> {
566 editor.update_in(cx, |editor, window, cx| {
567 editor
568 .highlighted_display_rows(window, cx)
569 .into_keys()
570 .map(|r| r.0)
571 .collect()
572 })
573 }
574
575 fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
576 cx.update(|cx| {
577 let state = AppState::test(cx);
578 language::init(cx);
579 crate::init(cx);
580 editor::init(cx);
581 workspace::init_settings(cx);
582 Project::init_settings(cx);
583 state
584 })
585 }
586
587 fn rust_lang() -> Arc<Language> {
588 Arc::new(
589 Language::new(
590 LanguageConfig {
591 name: "Rust".into(),
592 matcher: LanguageMatcher {
593 path_suffixes: vec!["rs".to_string()],
594 ..Default::default()
595 },
596 ..Default::default()
597 },
598 Some(tree_sitter_rust::LANGUAGE.into()),
599 )
600 .with_outline_query(
601 r#"(struct_item
602 (visibility_modifier)? @context
603 "struct" @context
604 name: (_) @name) @item
605
606 (enum_item
607 (visibility_modifier)? @context
608 "enum" @context
609 name: (_) @name) @item
610
611 (enum_variant
612 (visibility_modifier)? @context
613 name: (_) @name) @item
614
615 (impl_item
616 "impl" @context
617 trait: (_)? @name
618 "for"? @context
619 type: (_) @name) @item
620
621 (trait_item
622 (visibility_modifier)? @context
623 "trait" @context
624 name: (_) @name) @item
625
626 (function_item
627 (visibility_modifier)? @context
628 (function_modifiers)? @context
629 "fn" @context
630 name: (_) @name) @item
631
632 (function_signature_item
633 (visibility_modifier)? @context
634 (function_modifiers)? @context
635 "fn" @context
636 name: (_) @name) @item
637
638 (macro_definition
639 . "macro_rules!" @context
640 name: (_) @name) @item
641
642 (mod_item
643 (visibility_modifier)? @context
644 "mod" @context
645 name: (_) @name) @item
646
647 (type_item
648 (visibility_modifier)? @context
649 "type" @context
650 name: (_) @name) @item
651
652 (associated_type
653 "type" @context
654 name: (_) @name) @item
655
656 (const_item
657 (visibility_modifier)? @context
658 "const" @context
659 name: (_) @name) @item
660
661 (field_declaration
662 (visibility_modifier)? @context
663 name: (_) @name) @item
664"#,
665 )
666 .unwrap(),
667 )
668 }
669
670 #[track_caller]
671 fn assert_single_caret_at_row(
672 editor: &Entity<Editor>,
673 buffer_row: u32,
674 cx: &mut VisualTestContext,
675 ) {
676 let selections = editor.update(cx, |editor, cx| {
677 editor
678 .selections
679 .all::<rope::Point>(&editor.display_snapshot(cx))
680 .into_iter()
681 .map(|s| s.start..s.end)
682 .collect::<Vec<_>>()
683 });
684 assert!(
685 selections.len() == 1,
686 "Expected one caret selection but got: {selections:?}"
687 );
688 let selection = &selections[0];
689 assert!(
690 selection.start == selection.end,
691 "Expected a single caret selection, but got: {selection:?}"
692 );
693 assert_eq!(selection.start.row, buffer_row);
694 }
695}