test.rs

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