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