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