Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.4 KB
Raw Blame
use crate::editor::OpenEditor;
use crate::editor::cursor::{Position, Selection};
use crate::editor::edit::tests::editor;
use crate::editor::word_operations::{DEFAULT_WORD_SEPARATORS, WordClassifier};
use crate::languages::Languages;
use crate::syntax::Tokenization;

fn at(line: usize, column: usize) -> Position {
    Position::new(line, column)
}

fn range(anchor: (usize, usize), head: (usize, usize)) -> Selection {
    Selection { anchor: at(anchor.0, anchor.1), head: at(head.0, head.1) }
}

fn classifier() -> WordClassifier {
    WordClassifier::new(DEFAULT_WORD_SEPARATORS, &[])
}

#[test]
fn a_double_click_selects_the_word_and_its_drag_goes_on_by_words() {
    let mut lines = editor("one two three\nfour five", "\n");
    lines.word_select(&classifier(), at(0, 5), false);
    assert_eq!(lines.selections, [range((0, 4), (0, 7))]);
    lines.word_select(&classifier(), at(1, 6), true);
    assert_eq!(lines.selections, [range((0, 4), (1, 9))], "below, to the end of the word under the pointer");
    lines.word_select(&classifier(), at(0, 1), true);
    assert_eq!(lines.selections, [range((0, 7), (0, 0))], "above, from the end of the first word");
    lines.word_select(&classifier(), at(0, 3), false);
    assert_eq!(lines.selections, [range((0, 0), (0, 3))], "WordOperations.word takes the word ending at the press");
}

#[test]
fn a_triple_click_selects_the_line_and_its_drag_goes_on_by_lines() {
    let mut lines = editor("a\nb\nc", "\n");
    lines.line_select(at(1, 0), false);
    assert_eq!(lines.selections, [range((1, 0), (2, 0))]);
    lines.line_select(at(0, 0), true);
    assert_eq!(lines.selections, [range((2, 0), (0, 0))]);
    lines.line_select(at(2, 0), true);
    assert_eq!(lines.selections, [range((1, 0), (2, 1))], "the last line to its end");
}

#[test]
fn an_alt_double_click_drag_goes_on_by_words_on_the_cursor_added_last() {
    let mut lines = editor("one two three", "\n");
    lines.toggle_cursor(at(0, 5));
    lines.last_cursor_word_select(&classifier(), at(0, 5));
    assert_eq!(lines.selections, [Selection::caret(at(0, 0)), range((0, 4), (0, 7))]);
    lines.last_cursor_word_select(&classifier(), at(0, 10));
    assert_eq!(lines.selections, [Selection::caret(at(0, 0)), range((0, 4), (0, 13))]);
}

fn typescript(text: &str) -> (OpenEditor, Languages) {
    let app_root = std::path::Path::new(env!("VSCODE_SRC")).to_path_buf();
    let languages = Languages::new(&[exthost::scan::scan_extension(&app_root.join("extensions/typescript-basics"), true).unwrap()]);
    let mut editor = editor(text, "\n");
    editor.document_mut().language_id = "typescript".into();
    editor.document_mut().tokens = Tokenization::new(languages.grammars.grammar("typescript"));
    (editor, languages)
}

#[test]
fn a_double_click_inside_brackets_or_quotes_selects_what_they_hold() {
    let (lines, languages) = typescript("call(one, 'two three');");
    let block = |column: usize| lines.double_click_block(&languages, at(0, column));
    assert_eq!(block(5), Some(range((0, 5), (0, 21))), "right after the open bracket");
    assert_eq!(block(21), Some(range((0, 5), (0, 21))), "right before the close bracket");
    assert_eq!(block(11), Some(range((0, 11), (0, 20))), "right after the open quote");
    assert_eq!(block(20), Some(range((0, 11), (0, 20))), "right before the close quote");
    assert_eq!(block(7), None, "inside a word");
    assert_eq!(block(14), None, "inside the string");
}