test.rs

  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            true,
 65            1,
 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, true, 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), true, window, cx)
121}