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