Visual Studio Code, rebuilt in Rust
Rust
//! computeGhostText.test.ts: `[`/`]` mark the replaced range; the result is rendered with each part in brackets.
use super::*;
fn output(text: &str, suggestion: &str, mode: GhostTextMode) -> Option<String> {
let start = text.find('[').unwrap();
let end = text.find(']').unwrap() - 1;
let cleaned = text.replacen('[', "", 1).replacen(']', "", 1);
let lines: Vec<String> = cleaned.split('\n').map(String::from).collect();
let position = |offset: usize| position_after(Position::default(), &cleaned[..offset]);
let edit = Replacement { start: position(start), end: position(end), text: suggestion.to_owned() };
compute_ghost_text(&edit, &lines, mode, None, 0).map(|ghost| ghost.render(&lines))
}
fn both(text: &str, suggestion: &str) -> (Option<String>, Option<String>) {
(output(text, suggestion, GhostTextMode::Prefix), output(text, suggestion, GhostTextMode::Subword))
}
fn same(text: &str, suggestion: &str, expected: Option<&str>) {
let expected = expected.map(String::from);
assert_eq!(both(text, suggestion), (expected.clone(), expected), "{text} {suggestion}");
}
#[test]
fn basic() {
same("[foo]baz", "foobar", Some("foo[bar]baz"));
same("[aaa]aaa", "aaaaaa", Some("aaa[aaa]aaa"));
same("[foo]baz", "boobar", None);
same("[foo]foo", "foofoo", Some("foo[foo]foo"));
same("foo[]", "bar\nhello", Some("foo[bar\nhello]"));
}
#[test]
fn empty_ghost_text() {
same("[foo]", "foo", Some("foo"));
}
#[test]
fn whitespace_in_the_indentation() {
same("[ foo]", "foobar", Some(" foo[bar]"));
same("[\tfoo]", "foobar", Some("\tfoo[bar]"));
same("[\t foo]", "\tfoobar", Some("\t foo[bar]"));
assert_eq!(both("[\tfoo]", "\t\tfoobar"), (None, Some("\t[\t]foo[bar]".into())));
same("[\t]", "\t\tfoobar", Some("\t[\tfoobar]"));
same("\t[]", "\t", Some("\t[\t]"));
same("\t[\t]", "", Some("\t\t"));
same("[ ]", "return 1", Some(" [return 1]"));
}
#[test]
fn whitespace_outside_the_indentation() {
same("bar[ foo]", "foobar", None);
same("bar[\tfoo]", "foobar", None);
}
#[test]
fn new_lines() {
same("fo[o\n]", "x\nbar", None);
same("fo[o\n]", "o\nbar", Some("foo\n[bar]"));
}
#[test]
fn multi_part_diffing() {
assert_eq!(both("foo[()]", "(x);"), (None, Some("foo([x])[;]".into())));
assert_eq!(both("[(y ===)]", "(y === 1) { f(); }"), (None, Some("(y ===[ 1])[ { f(); }]".into())));
assert_eq!(both("[(y ==)]", "(y === 1) { f(); }"), (None, Some("(y ==[= 1])[ { f(); }]".into())));
assert_eq!(both("[if () ()]", "if (1 == f()) ()"), (None, Some("if ([1 == f()]) ()".into())));
assert_eq!(both("[)]", "())"), (None, Some("[(])[)]".into())));
assert_eq!(both("[))]", "(())"), (None, Some("[((]))".into())));
}
#[test]
fn parentheses_match() {
assert_eq!(both("[console.log()]", "console.log({ label: \"(\" })"), (None, Some("console.log([{ label: \"(\" }])".into())));
}
#[test]
fn a_completion_shows_while_the_typed_text_is_its_prefix() {
let lines = vec!["con".to_owned()];
let edit = |text: &str| Replacement { start: Position::new(0, 0), end: Position::new(0, 3), text: text.to_owned() };
assert!(inline_completion_is_visible(&edit("console"), None, &lines, Position::new(0, 3)));
assert!(!inline_completion_is_visible(&edit("cat"), None, &lines, Position::new(0, 3)));
// On another line than the cursor, or nothing left to insert.
assert!(!inline_completion_is_visible(&edit("console"), None, &lines, Position::new(1, 0)));
assert!(!inline_completion_is_visible(&edit("con"), None, &lines, Position::new(0, 3)));
}
// getSecondaryEdits.test.ts
fn lines_of(text: &str) -> Vec<String> {
text.split('\n').map(String::from).collect()
}
fn replacement(start: (usize, usize), end: (usize, usize), text: &str) -> Replacement {
Replacement { start: Position::new(start.0, start.1), end: Position::new(end.0, end.1), text: text.to_owned() }
}
#[test]
fn secondary_edits_preserve_a_common_prefix_containing_a_surrogate_pair() {
let lines = lines_of("😀a\n😀a");
let edits = secondary_edits(&lines, &[Position::new(0, 2), Position::new(1, 2)], &replacement((0, 0), (0, 2), "😀ab"));
assert_eq!(edits, [replacement((1, 2), (1, 2), "b")]);
}
#[test]
fn secondary_edits_basic() {
let lines = lines_of("function fib(\nfunction fib(");
let edits = secondary_edits(&lines, &[Position::new(0, 13), Position::new(1, 13)], &replacement((0, 0), (0, 13), "function fib() {"));
assert_eq!(edits, [replacement((1, 13), (1, 13), ") {")]);
}
#[test]
fn secondary_edits_with_the_cursor_not_on_the_primary_edits_line() {
let lines = lines_of("function fib(\n\nfunction fib(\n");
let edits = secondary_edits(&lines, &[Position::new(1, 0), Position::new(3, 0)], &replacement((0, 0), (1, 0), "function fib() {\n\treturn 0;\n}"));
assert_eq!(edits, [replacement((2, 13), (3, 0), ") {\n\treturn 0;\n}")]);
let lines = lines_of("class A {\n\nclass B {\n\nfunction f() {}");
let text = "class A {\n\tpublic x: number = 0;\n public y: number = 0;\n}";
let edits = secondary_edits(&lines, &[Position::new(1, 0), Position::new(3, 0)], &replacement((0, 0), (1, 0), text));
assert_eq!(edits, [replacement((3, 0), (3, 0), "\tpublic x: number = 0;\n public y: number = 0;\n}")]);
}
#[test]
fn end_positions_after_applying_keep_the_edits_order() {
let edits = [replacement((1, 2), (1, 2), "ab\nc"), replacement((0, 1), (0, 3), "x"), replacement((1, 0), (1, 1), "yy")];
assert_eq!(end_positions_after_applying(&edits), Some(vec![Position::new(2, 1), Position::new(0, 2), Position::new(1, 2)]));
assert_eq!(end_positions_after_applying(&[replacement((0, 1), (0, 3), "x"), replacement((0, 2), (0, 4), "y")]), None);
}