1pub mod editor_lsp_test_context;
2pub mod editor_test_context;
3
4use crate::{
5 display_map::{DisplayMap, DisplaySnapshot, ToDisplayPoint},
6 DisplayPoint, Editor, EditorMode, MultiBuffer,
7};
8
9use gpui::{Context, Font, FontFeatures, FontStyle, FontWeight, Model, Pixels, ViewContext};
10
11use project::Project;
12use util::test::{marked_text_offsets, marked_text_ranges};
13
14#[cfg(test)]
15#[ctor::ctor]
16fn init_logger() {
17 if std::env::var("RUST_LOG").is_ok() {
18 env_logger::init();
19 }
20}
21
22// Returns a snapshot from text containing '|' character markers with the markers removed, and DisplayPoints for each one.
23pub fn marked_display_snapshot(
24 text: &str,
25 cx: &mut gpui::AppContext,
26) -> (DisplaySnapshot, Vec<DisplayPoint>) {
27 let (unmarked_text, markers) = marked_text_offsets(text);
28
29 let font = Font {
30 family: "Courier".into(),
31 features: FontFeatures::default(),
32 weight: FontWeight::default(),
33 style: FontStyle::default(),
34 };
35 let font_size: Pixels = 14usize.into();
36
37 let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
38 let display_map = cx.new_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, cx));
39 let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
40 let markers = markers
41 .into_iter()
42 .map(|offset| offset.to_display_point(&snapshot))
43 .collect();
44
45 (snapshot, markers)
46}
47
48pub fn select_ranges(editor: &mut Editor, marked_text: &str, cx: &mut ViewContext<Editor>) {
49 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
50 assert_eq!(editor.text(cx), unmarked_text);
51 editor.change_selections(None, cx, |s| s.select_ranges(text_ranges));
52}
53
54pub fn assert_text_with_selections(
55 editor: &mut Editor,
56 marked_text: &str,
57 cx: &mut ViewContext<Editor>,
58) {
59 let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
60 assert_eq!(editor.text(cx), unmarked_text);
61 assert_eq!(editor.selections.ranges(cx), text_ranges);
62}
63
64// RA thinks this is dead code even though it is used in a whole lot of tests
65#[allow(dead_code)]
66#[cfg(any(test, feature = "test-support"))]
67pub(crate) fn build_editor(buffer: Model<MultiBuffer>, cx: &mut ViewContext<Editor>) -> Editor {
68 Editor::new(EditorMode::Full, buffer, None, cx)
69}
70
71pub(crate) fn build_editor_with_project(
72 project: Model<Project>,
73 buffer: Model<MultiBuffer>,
74 cx: &mut ViewContext<Editor>,
75) -> Editor {
76 Editor::new(EditorMode::Full, buffer, Some(project), cx)
77}