1pub mod editor_lsp_test_context;
2pub mod editor_test_context;
3
4pub use crate::rust_analyzer_ext::expand_macro_recursively;
5use crate::{
6 DisplayPoint, Editor, EditorMode, FoldPlaceholder, MultiBuffer,
7 display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
8};
9use gpui::{
10 AppContext as _, Context, Entity, Font, FontFeatures, FontStyle, FontWeight, Pixels, Window,
11 font,
12};
13use pretty_assertions::assert_eq;
14use project::Project;
15use std::sync::LazyLock;
16use util::test::{marked_text_offsets, marked_text_ranges};
17
18#[cfg(test)]
19#[ctor::ctor]
20fn init_logger() {
21 if std::env::var("RUST_LOG").is_ok() {
22 env_logger::init();
23 }
24}
25
26pub fn test_font() -> Font {
27 static TEST_FONT: LazyLock<Font> = LazyLock::new(|| {
28 #[cfg(not(target_os = "windows"))]
29 {
30 font("Helvetica")
31 }
32
33 #[cfg(target_os = "windows")]
34 {
35 font("Courier New")
36 }
37 });
38
39 TEST_FONT.clone()
40}
41
42// Returns a snapshot from text containing '|' character markers with the markers removed, and DisplayPoints for each one.
43pub fn marked_display_snapshot(
44 text: &str,
45 cx: &mut gpui::App,
46) -> (DisplaySnapshot, Vec<DisplayPoint>) {
47 let (unmarked_text, markers) = marked_text_offsets(text);
48
49 let font = Font {
50 family: "Zed Plex Mono".into(),
51 features: FontFeatures::default(),
52 fallbacks: None,
53 weight: FontWeight::default(),
54 style: FontStyle::default(),
55 };
56 let font_size: Pixels = 14usize.into();
57
58 let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
59 let display_map = cx.new(|cx| {
60 DisplayMap::new(
61 buffer,
62 font,
63 font_size,
64 None,
65 1,
66 1,
67 FoldPlaceholder::test(),
68 cx,
69 )
70 });
71 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
72 let markers = markers
73 .into_iter()
74 .map(|offset| offset.to_display_point(&snapshot))
75 .collect();
76
77 (snapshot, markers)
78}
79
80pub fn select_ranges(
81 editor: &mut Editor,
82 marked_text: &str,
83 window: &mut Window,
84 cx: &mut Context<Editor>,
85) {
86 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
87 assert_eq!(editor.text(cx), unmarked_text);
88 editor.change_selections(None, window, cx, |s| s.select_ranges(text_ranges));
89}
90
91#[track_caller]
92pub fn assert_text_with_selections(
93 editor: &mut Editor,
94 marked_text: &str,
95 cx: &mut Context<Editor>,
96) {
97 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
98 assert_eq!(editor.text(cx), unmarked_text, "text doesn't match");
99 assert_eq!(
100 editor.selections.ranges(cx),
101 text_ranges,
102 "selections don't match",
103 );
104}
105
106// RA thinks this is dead code even though it is used in a whole lot of tests
107#[allow(dead_code)]
108#[cfg(any(test, feature = "test-support"))]
109pub(crate) fn build_editor(
110 buffer: Entity<MultiBuffer>,
111 window: &mut Window,
112 cx: &mut Context<Editor>,
113) -> Editor {
114 Editor::new(EditorMode::full(), buffer, None, window, cx)
115}
116
117pub(crate) fn build_editor_with_project(
118 project: Entity<Project>,
119 buffer: Entity<MultiBuffer>,
120 window: &mut Window,
121 cx: &mut Context<Editor>,
122) -> Editor {
123 Editor::new(EditorMode::full(), buffer, Some(project), window, cx)
124}