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, MultiBuffer,
 7};
 8
 9use gpui::{ModelHandle, 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 family_id = cx
30        .font_cache()
31        .load_family(&["Helvetica"], &Default::default())
32        .unwrap();
33    let font_id = cx
34        .font_cache()
35        .select_font(family_id, &Default::default())
36        .unwrap();
37    let font_size = 14.0;
38
39    let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
40    let display_map =
41        cx.add_model(|cx| DisplayMap::new(buffer, font_id, font_size, None, 1, 1, cx));
42    let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
43    let markers = markers
44        .into_iter()
45        .map(|offset| offset.to_display_point(&snapshot))
46        .collect();
47
48    (snapshot, markers)
49}
50
51pub fn select_ranges(editor: &mut Editor, marked_text: &str, cx: &mut ViewContext<Editor>) {
52    let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
53    assert_eq!(editor.text(cx), unmarked_text);
54    editor.change_selections(None, cx, |s| s.select_ranges(text_ranges));
55}
56
57pub fn assert_text_with_selections(
58    editor: &mut Editor,
59    marked_text: &str,
60    cx: &mut ViewContext<Editor>,
61) {
62    let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
63    assert_eq!(editor.text(cx), unmarked_text);
64    assert_eq!(editor.selections.ranges(cx), text_ranges);
65}
66
67// RA thinks this is dead code even though it is used in a whole lot of tests
68#[allow(dead_code)]
69#[cfg(any(test, feature = "test-support"))]
70pub(crate) fn build_editor(
71    buffer: ModelHandle<MultiBuffer>,
72    cx: &mut ViewContext<Editor>,
73) -> Editor {
74    Editor::new(EditorMode::Full, buffer, None, None, cx)
75}
76
77pub(crate) fn build_editor_with_project(
78    project: ModelHandle<Project>,
79    buffer: ModelHandle<MultiBuffer>,
80    cx: &mut ViewContext<Editor>,
81) -> Editor {
82    Editor::new(EditorMode::Full, buffer, Some(project), None, cx)
83}