Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.9 KB
Raw Blame
use super::*;
use crate::editor::word_wrap::{BREAK_AFTER, BREAK_BEFORE, Classifier, WrapOptions, WrappingIndent, line_breaks};

#[test]
fn injections_move_the_later_columns_right() {
    // "ab|cd" with a 3 column hint before `c`.
    let layout = LineLayout::new("abcd", 4, [(2, 3., "abc")]);
    assert_eq!((layout.x(1), layout.x(2), layout.char_x(2), layout.x(3)), (1., 2., 5., 6.));
    // A cursor at the hint draws before it, a range starting there after it, one ending there before it.
    assert_eq!(layout.range(2, 2), (2., 2.));
    assert_eq!(layout.range(2, 4), (5., 7.));
    assert_eq!(layout.range(0, 2), (0., 2.));
    assert_eq!(layout.width(), 7.);
}

#[test]
fn an_injection_at_the_line_end_widens_the_line() {
    let layout = LineLayout::new("ab", 4, [(2, 1.5, "")]);
    assert_eq!((layout.x(2), layout.width()), (2., 3.5));
}

#[test]
fn a_click_on_an_injection_goes_to_its_column() {
    let layout = LineLayout::new("abcd", 4, [(2, 3., "abc")]);
    assert_eq!(layout.column_at(1.4), 1);
    assert_eq!(layout.column_at(1.6), 2);
    assert_eq!(layout.column_at(3.), 2);
    assert_eq!(layout.column_at(5.4), 2);
    assert_eq!(layout.column_at(5.6), 3);
    assert_eq!(layout.column_at(20.), 4);
    assert_eq!(layout.model_visible(4.), 2.);
    // `columnFromVisibleColumn` takes the left column on a tie.
    assert_eq!(LineLayout::new("\ta", 4, []).column_at(2.), 0);
}

#[test]
fn a_hover_takes_the_char_under_the_mouse() {
    // "ab" + "abc" (3 columns wide) + "cd": a hover x on a char's right half stays on that char.
    let layout = LineLayout::new("abcd", 4, [(2, 3., "abc")]);
    assert_eq!(layout.model_x(1.8), 1.8);
    assert_eq!(layout.model_x(3.), 2.);
    assert_eq!(layout.model_x(5.5), 2.5);
    assert_eq!(layout.model_x(9.), 6.);
}

#[test]
fn tab_stops_count_the_injected_chars() {
    // viewLineRenderer.ts `_renderLine` over "a" + "xy" + "\tb": the tab reaches visible column 4 from 3, one column.
    let layout = LineLayout::new("a\tb", 4, [(1, 2.5, "xy")]);
    assert_eq!((layout.char_x(1), layout.char_x(2), layout.visible(2)), (3.5, 4.5, 4));
    // A width-only injection (inlay hint padding) has no chars.
    assert_eq!(LineLayout::new("a\tb", 4, [(1, 0.5, "")]).char_x(2), 4.5);
}

#[test]
fn an_injection_moves_with_the_text_typed_before_it() {
    let mut editor = crate::editor::edit::tests::editor("let x = f(1);", "\n");
    let hint = |column| InjectedText { position: Position::new(0, column), columns: 3., content: "abc".into(), wrap: WrapWidth::Chars, owner: InjectionOwner::InlayHint, id: 0, at_range_start: true };
    editor.set_injected_text(InjectionOwner::InlayHint, vec![hint(10)]);
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(0, 0))]);
    editor.type_text("  ");
    assert_eq!(editor.injected_text_on(0)[0].position, Position::new(0, 12));
    // Typed at its own column, the text goes after an injection at its range's start.
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(0, 12))]);
    editor.type_text("x");
    assert_eq!(editor.injected_text_on(0)[0].position, Position::new(0, 12));
    assert_eq!(editor.line_layout(0).char_x(12), 15.);
    editor.set_injected_text(InjectionOwner::InlayHint, Vec::new());
    assert!(editor.injected_text_on(0).is_empty());
}

/// modelLineProjectionData.ts with an inlay hint's `Right` stop: Right stops after the hint, then passes the char;
/// Left from there stops after it and skips the stop before it.
#[test]
fn cursor_moves_stop_after_an_inlay_hint() {
    let mut editor = crate::editor::edit::tests::editor("abcd\nf(1);\nabcd", "\n");
    let hint = |column| InjectedText { position: Position::new(1, column), columns: 3., content: "abc".into(), wrap: WrapWidth::Chars, owner: InjectionOwner::InlayHint, id: 0, at_range_start: true };
    editor.set_injected_text(InjectionOwner::InlayHint, vec![hint(2), hint(5)]);
    let head = |editor: &OpenEditor| (editor.selection().head.column, editor.head_past_injections(0));
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(1, 1))]);
    let mut heads = Vec::new();
    for right in [true, true, false, false] {
        editor.move_horizontally(right, false);
        heads.push(head(&editor));
    }
    assert_eq!(heads, [(2, 1), (3, 0), (2, 1), (1, 0)]);
    assert_eq!(editor.line_layout(1).cursor_x(2, 1), 5.);
    // Down onto the hint's column lands after it, up before it.
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(0, 2))]);
    editor.move_vertically(true, false);
    assert_eq!(head(&editor), (2, 1));
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(2, 2))]);
    editor.move_vertically(false, false);
    assert_eq!(head(&editor), (2, 0));
    // `MoveOperations.vertical` counts the hint's chars of the view line "f(abc1);abc": down from "f(1|" keeps visible
    // column 6, up from "abcd|" lands in the hint and goes before it.
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(1, 3))]);
    editor.move_vertically(true, false);
    assert_eq!(editor.selection().head, Position::new(2, 4));
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(2, 4))]);
    editor.move_vertically(false, false);
    assert_eq!(head(&editor), (2, 0));
    // End goes to the view line's max column, after a hint at the line's end.
    editor.move_to_line_edge(true, false);
    assert_eq!(head(&editor), (5, 1));
}

/// mouseTarget.ts hit test, then `normalizePosition` with `PositionAffinity.None`: a press past the middle of a hint's
/// last char, or on the first half of the char after it, rests after the hint; a selection with its head there covers it.
#[test]
fn a_press_past_the_middle_of_a_hints_last_char_lands_after_it() {
    let layout = LineLayout::new("abcd", 4, [(2, 3., "abc")]);
    assert_eq!([1.6, 4.4, 4.6, 5.4, 5.6].map(|x| layout.position_at(x)), [(2, 0), (2, 0), (2, 1), (2, 1), (3, 0)]);
    let mut editor = crate::editor::edit::tests::editor("abcd", "\n");
    let hint = InjectedText { position: Position::new(0, 2), columns: 3., content: "abc".into(), wrap: WrapWidth::Chars, owner: InjectionOwner::InlayHint, id: 0, at_range_start: true };
    editor.set_injected_text(InjectionOwner::InlayHint, vec![hint]);
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(0, 2))]);
    editor.click_past_injections(0, 4.6);
    assert_eq!(editor.head_past_injections(0), 1, "the click lands after the hint");
    // Shift+Right from "a|b" stops after the hint, so the selection of `b` covers it.
    editor.set_selections(vec![crate::editor::Selection::caret(Position::new(0, 1))]);
    editor.move_horizontally(true, true);
    assert_eq!(editor.selection_x(0, &editor.line_layout(0), 0, 1, 2), (1., 5.));
}

/// `normalizeOffsetInInputWithInjectionsAroundInjections` with `PositionAffinity.None`.
#[test]
fn a_cursor_rests_at_the_injections_stops() {
    use CursorStops::{Both, Left, Right};
    // Ghost text has no stop after it, a color box both.
    assert_eq!((cursor_stop(&[Left], usize::MAX), cursor_stop(&[Both], 1), cursor_stop(&[Both], 0)), (0, 1, 0));
    // After a `Right` stop the cursor stays, after a `Left` only stop it goes back before it.
    assert_eq!((cursor_stop(&[Right, Left], 1), cursor_stop(&[Left, Right], 1), cursor_stop(&[Right, Right], 1)), (1, 0, 1));
    assert_eq!(cursor_stop(&[], 3), 0);
}

fn wrap_options() -> WrapOptions {
    WrapOptions {
        column: 10,
        viewport_wrapping: false,
        tab_size: 4,
        indent: WrappingIndent::None,
        keep_all: false,
        wrap_on_escaped_line_feeds: false,
        columns_for_full_width_char: 2.,
        two_cell_full_width: false,
        break_before: BREAK_BEFORE.into(),
        break_after: BREAK_AFTER.into(),
    }
}

fn injection(column: usize, columns: f32, content: &str, wrap: WrapWidth) -> InjectedText {
    InjectedText { position: Position::new(0, column), columns, content: content.into(), wrap, owner: InjectionOwner::InlayHint, id: 0, at_range_start: true }
}

#[test]
fn wrapping_counts_the_injected_width_and_breaks_before_it() {
    let options = wrap_options();
    let classifier = Classifier::new(BREAK_BEFORE, BREAK_AFTER);
    assert_eq!(line_breaks(&classifier, "aaaa bbbb", &options, &[]), None);
    // A 4 column hint before `bbbb` makes the line 13 columns: the hint goes to the second view line with it.
    let breaks = line_breaks(&classifier, "aaaa bbbb", &options, &[injection(5, 4., "", WrapWidth::Fixed)]).unwrap();
    assert_eq!(breaks.offsets, [5, 9]);
    // The tab after "a" + "xy" takes one column, so "a\tbbbbb" stays 9 columns wide.
    assert_eq!(line_breaks(&classifier, "a\tbbbbb", &options, &[injection(1, 2., "xy", WrapWidth::Chars)]), None);
}

/// monospaceLineBreaksComputer.ts measures a hint's label chars and its `paddingLeft` whitespace, not its padding
/// box: "ab |_[cccc |dddd]_|xy" breaks inside the hint before `dddd`, past the whitespace, the box's left side and 5
/// label chars. Each view line's span of the label draws its own box (inlayHintsController.ts `cssProperties.padding`
/// on the label's class), so the first ends past its right side too.
#[test]
fn wrapping_a_padded_inlay_hint_skips_its_padding_box() {
    let classifier = Classifier::new(BREAK_BEFORE, BREAK_AFTER);
    let hint = |first_part_end, last_part_start| injection(3, 13., "cccc dddd", WrapWidth::Hint { left: 1., right: 1., padding: 1., first_part_end, last_part_start });
    let breaks = line_breaks(&classifier, "ab xy", &wrap_options(), &[hint(9, 0)]).unwrap();
    assert_eq!(breaks.offsets, [3, 5]);
    assert!((breaks.injected[0] - 8.).abs() < 1e-4, "{:?}", breaks.injected);
    assert_eq!(breaks.hint_splits, [(0, 5)]);
    // Label parts "cccc " and "dddd": the wrap between them cuts no span.
    let breaks = line_breaks(&classifier, "ab xy", &wrap_options(), &[hint(5, 5)]).unwrap();
    assert!((breaks.injected[0] - 7.).abs() < 1e-4, "{:?}", breaks.injected);
    assert_eq!(breaks.hint_splits, []);
}