Visual Studio Code, rebuilt in Rust
Rust
use gpui::{EntityInputHandler, TestAppContext};
use super::*;
use crate::workbench::goto_symbol::tests::{open, run};
/// nativeEditContext.ts `_getNewEditContextState`: the IME sees the selection's whole lines, joined by the EOL.
#[gpui::test]
fn the_input_handler_sees_the_lines_of_the_selection(cx: &mut TestAppContext) {
let (workbench, cx, _, _) = open(cx, "input-lines", "ab
cd
ef");
run(&workbench, cx, "cursorDown");
run(&workbench, cx, "cursorRight");
run(&workbench, cx, "cursorDownSelect");
let (text, selected) = workbench.update_in(cx, |workbench, window, cx| {
let mut adjusted = None;
let text = workbench.text_for_range(0..100, &mut adjusted, window, cx);
(text, workbench.selected_text_range(false, window, cx).map(|selection| (selection.range, selection.reversed)))
});
assert_eq!(text.as_deref(), Some("cd
ef"));
assert_eq!(selected, Some((1..4, false)));
// Reconversion replaces the selected text across the line break.
workbench.update_in(cx, |workbench, window, cx| workbench.replace_text_in_range(Some(1..4), "X", window, cx));
let lines = workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().lines.clone());
assert_eq!(lines, ["ab", "cXf"]);
}
/// `compositionType` with `positionDelta`: the IME's caret inside the marked text places the cursor.
#[gpui::test]
fn the_ime_caret_inside_the_marked_text_places_the_cursor(cx: &mut TestAppContext) {
let (workbench, cx, _, _) = open(cx, "input-caret", "ab");
run(&workbench, cx, "cursorEnd");
workbench.update_in(cx, |workbench, window, cx| workbench.replace_and_mark_text_in_range(None, "\u{304b}\u{306a}", Some(1..1), window, cx));
let (lines, head) = workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
(editor.document().lines.clone(), editor.selection().head)
});
assert_eq!(lines, ["ab\u{304b}\u{306a}"]);
assert_eq!(head, Position::new(0, 3));
let marked = workbench.update_in(cx, |workbench, window, cx| workbench.marked_text_range(window, cx));
assert_eq!(marked, Some(2..4));
}
/// `_updateCharacterBounds`: the IME's candidate window opens at the chars it asks for.
#[gpui::test]
fn character_bounds_follow_the_text(cx: &mut TestAppContext) {
let (workbench, cx, _, _) = open(cx, "input-bounds", "abc");
let bounds = |range: std::ops::Range<usize>, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| workbench.bounds_for_range(range, Bounds::default(), window, cx)).expect("bounds");
let (first, second, both) = (bounds(0..1, cx), bounds(1..2, cx), bounds(0..2, cx));
assert!(first.size.width > px(0.) && first.size.height > px(0.));
let close = |a: Pixels, b: Pixels| (f32::from(a) - f32::from(b)).abs() < 0.01;
assert!(close(second.origin.x - first.origin.x, first.size.width));
assert!(both.origin == first.origin && close(both.size.width, first.size.width * 2.));
}
#[test]
fn utf16_offsets_cross_lines() {
let lines = ["a\u{1F600}".to_owned(), "bc".to_owned()];
assert_eq!(utf16_offset(&lines, 2, Position::new(1, 1)), 6);
assert_eq!(position_at_utf16(&lines, 2, 6), Position::new(1, 1));
assert_eq!(position_at_utf16(&lines, 2, 4), Position::new(0, 2), "inside the EOL");
assert_eq!(position_at_utf16(&lines, 2, 99), Position::new(1, 2));
}