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::{
  9    AppContext as _, Context, Entity, Font, FontFeatures, FontStyle, FontWeight, Pixels, Window,
 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::App,
 26) -> (DisplaySnapshot, Vec<DisplayPoint>) {
 27    let (unmarked_text, markers) = marked_text_offsets(text);
 28
 29    let font = Font {
 30        family: "Zed Plex Mono".into(),
 31        features: FontFeatures::default(),
 32        fallbacks: None,
 33        weight: FontWeight::default(),
 34        style: FontStyle::default(),
 35    };
 36    let font_size: Pixels = 14usize.into();
 37
 38    let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
 39    let display_map = cx.new(|cx| {
 40        DisplayMap::new(
 41            buffer,
 42            font,
 43            font_size,
 44            None,
 45            true,
 46            1,
 47            1,
 48            1,
 49            FoldPlaceholder::test(),
 50            cx,
 51        )
 52    });
 53    let snapshot = display_map.update(cx, |map, cx| map.snapshot(cx));
 54    let markers = markers
 55        .into_iter()
 56        .map(|offset| offset.to_display_point(&snapshot))
 57        .collect();
 58
 59    (snapshot, markers)
 60}
 61
 62pub fn select_ranges(
 63    editor: &mut Editor,
 64    marked_text: &str,
 65    window: &mut Window,
 66    cx: &mut Context<Editor>,
 67) {
 68    let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
 69    assert_eq!(editor.text(cx), unmarked_text);
 70    editor.change_selections(None, window, cx, |s| s.select_ranges(text_ranges));
 71}
 72
 73#[track_caller]
 74pub fn assert_text_with_selections(
 75    editor: &mut Editor,
 76    marked_text: &str,
 77    cx: &mut Context<Editor>,
 78) {
 79    let (unmarked_text, text_ranges) = marked_text_ranges(marked_text, true);
 80    assert_eq!(editor.text(cx), unmarked_text);
 81    assert_eq!(editor.selections.ranges(cx), text_ranges);
 82}
 83
 84// RA thinks this is dead code even though it is used in a whole lot of tests
 85#[allow(dead_code)]
 86#[cfg(any(test, feature = "test-support"))]
 87pub(crate) fn build_editor(
 88    buffer: Entity<MultiBuffer>,
 89    window: &mut Window,
 90    cx: &mut Context<Editor>,
 91) -> Editor {
 92    Editor::new(EditorMode::Full, buffer, None, true, window, cx)
 93}
 94
 95pub(crate) fn build_editor_with_project(
 96    project: Entity<Project>,
 97    buffer: Entity<MultiBuffer>,
 98    window: &mut Window,
 99    cx: &mut Context<Editor>,
100) -> Editor {
101    Editor::new(EditorMode::Full, buffer, Some(project), true, window, cx)
102}