Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.5 KB
Raw Blame
use serde_json::json;

use super::*;

fn thread(handle: u64, line: u64, column: u64) -> (u64, u64, Option<Value>) {
    (1, handle, Some(json!({ "startLineNumber": line, "startColumn": column, "endLineNumber": line, "endColumn": column + 1 })))
}

#[test]
fn the_nearest_thread_starts_after_the_cursor_or_before_it_in_reverse() {
    let threads = vec![thread(3, 9, 1), thread(1, 2, 1), thread(2, 2, 5), (1, 4, None)];
    let handle = |found: Option<(u64, u64, Option<Value>)>| found.map(|(_, handle, _)| handle);
    assert_eq!(handle(nearest_thread(threads.clone(), Position::new(1, 0), false)), Some(2));
    assert_eq!(handle(nearest_thread(threads.clone(), Position::new(1, 4), false)), Some(3));
    assert_eq!(handle(nearest_thread(threads.clone(), Position::new(8, 0), false)), None);
    assert_eq!(handle(nearest_thread(threads.clone(), Position::new(1, 4), true)), Some(1));
    assert_eq!(handle(nearest_thread(threads, Position::new(0, 0), true)), Some(4));
}

#[test]
fn the_nearest_commenting_range_skips_the_one_holding_the_cursor_and_its_neighbors() {
    let lines = |start: usize, end: usize| (Position::new(start, 0), Position::new(end, 0));
    let ranges = [lines(0, 1), lines(2, 3), lines(6, 7), lines(9, 9)];
    assert_eq!(nearest_commenting_range(&ranges, 0, false), Some(lines(6, 7)));
    assert_eq!(nearest_commenting_range(&ranges, 4, false), Some(lines(6, 7)));
    assert_eq!(nearest_commenting_range(&ranges, 9, false), Some(lines(0, 1)));
    assert_eq!(nearest_commenting_range(&ranges, 7, true), Some(lines(2, 3)));
    assert_eq!(nearest_commenting_range(&ranges, 0, true), Some(lines(9, 9)));
    assert_eq!(nearest_commenting_range(&[], 0, false), None);
}

/// A range away from the cursor is reached at the provider's columns; the one holding it at column 1.
#[test]
fn a_commenting_range_is_reached_at_its_columns_unless_it_holds_the_cursor() {
    let ranges = [(Position::new(2, 4), Position::new(4, 9))];
    assert_eq!(nearest_commenting_position(&ranges, 0, false), Some(Position::new(2, 4)));
    assert_eq!(nearest_commenting_position(&ranges, 0, true), Some(Position::new(4, 9)));
    assert_eq!(nearest_commenting_position(&ranges, 3, false), Some(Position::new(2, 0)));
    assert_eq!(nearest_commenting_position(&ranges, 3, true), Some(Position::new(4, 0)));
}

/// Dismiss Comment in a reply collapses its thread and selects the thread's range; Expand All opens it again.
#[gpui::test]
fn dismissing_a_reply_collapses_its_thread(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "comment-dismiss", "one\ntwo\nthree\n");
    let resource = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
    let range = json!({ "startLineNumber": 2, "startColumn": 1, "endLineNumber": 2, "endColumn": 4 });
    let comment = json!({ "uniqueIdInThread": 1, "body": "Looks good", "userName": "Ann" });
    let state = |cx: &mut gpui::VisualTestContext| {
        workbench.read_with(cx, |workbench, _| {
            let thread = &workbench.comments.controllers[0].threads[0];
            (thread.collapse_state, !workbench.comments.forms.is_empty(), workbench.editors.active().unwrap().selection().start())
        })
    };
    workbench.update_in(cx, |workbench, window, cx| {
        let request = |workbench: &mut Workbench, method: &str, args: Vec<Value>, window: &mut Window, cx: &mut Context<Workbench>| drop(workbench.handle_comments_request(method, &args, window, cx));
        request(workbench, "$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], window, cx);
        request(workbench, "$createCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), range, json!([comment]), json!({ "value": "test.ext" }), json!(false)], window, cx);
        drop(workbench.execute_command("workbench.action.expandAllComments", Vec::new(), window, cx));
        workbench.expand_comment_reply(1, 7, window, cx);
    });
    assert_eq!(state(cx), (EXPANDED, true, Position::new(0, 0)));
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.hideComment", Vec::new(), window, cx)));
    assert_eq!(state(cx), (COLLAPSED, false, Position::new(1, 0)));

    let zones = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.comments.widgets.len());
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.focusCommentOnCurrentLine", Vec::new(), window, cx)));
    assert_eq!((state(cx).0, zones(cx)), (EXPANDED, 1));
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.toggleCommenting", Vec::new(), window, cx)));
    assert_eq!(zones(cx), 0);
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.toggleCommenting", Vec::new(), window, cx)));
    assert_eq!(zones(cx), 1);
}

/// A new empty template shows through `shouldReveal`; after the host's Expanded echo, Dismiss Comment changes nothing:
/// the zone and the focused reply stay, as in the reference. Collapse All closes it.
#[gpui::test]
fn dismissing_a_new_template_keeps_it_open(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "comment-template", "one\ntwo\nthree\n");
    let resource = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
    let range = json!({ "startLineNumber": 3, "startColumn": 1, "endLineNumber": 3, "endColumn": 1 });
    let state = |cx: &mut gpui::VisualTestContext| {
        workbench.read_with(cx, |workbench, _| {
            let thread = &workbench.comments.controllers[0].threads[0];
            (thread.collapse_state, workbench.comments.widgets.len(), !workbench.comments.forms.is_empty())
        })
    };
    workbench.update_in(cx, |workbench, window, cx| {
        let request = |workbench: &mut Workbench, method: &str, args: Vec<Value>, window: &mut Window, cx: &mut Context<Workbench>| drop(workbench.handle_comments_request(method, &args, window, cx));
        request(workbench, "$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], window, cx);
        request(workbench, "$createCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), range, json!([]), json!({ "value": "test.ext" }), json!(true)], window, cx);
    });
    assert_eq!(state(cx), (COLLAPSED, 1, true));
    workbench.update_in(cx, |workbench, window, cx| {
        drop(workbench.handle_comments_request("$updateCommentThread", &[json!(1), json!(7), json!("t"), resource.clone(), json!({ "collapseState": 1, "isTemplate": false })], window, cx));
        drop(workbench.execute_command("workbench.action.hideComment", Vec::new(), window, cx));
    });
    assert_eq!(state(cx), (EXPANDED, 1, true));
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.collapseAllComments", Vec::new(), window, cx)));
    let (collapse_state, zones, _) = state(cx);
    assert_eq!((collapse_state, zones), (COLLAPSED, 0));
}

/// Go to Next Comment Thread hands the focus to the reached thread's comments (`CommentWidgetFocus.Widget`), where
/// Down singles out a comment; Escape collapses the thread and the editor takes the focus back on its range.
#[gpui::test]
fn the_reached_thread_takes_the_focus(cx: &mut gpui::TestAppContext) {
    let (workbench, cx, _, dir) = crate::workbench::goto_symbol::tests::open(cx, "comment-focus", "one\ntwo\nthree\n");
    workbench.update_in(cx, |workbench, _, _| workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json")));
    let resource = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
    let range = json!({ "startLineNumber": 2, "startColumn": 1, "endLineNumber": 2, "endColumn": 4 });
    let comments = json!([{ "uniqueIdInThread": 1, "body": "One", "userName": "Ann" }, { "uniqueIdInThread": 2, "body": "Two", "userName": "Bob" }]);
    workbench.update_in(cx, |workbench, window, cx| {
        let request = |workbench: &mut Workbench, method: &str, args: Vec<Value>, window: &mut Window, cx: &mut Context<Workbench>| drop(workbench.handle_comments_request(method, &args, window, cx));
        request(workbench, "$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], window, cx);
        request(workbench, "$createCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), range, comments, json!({ "value": "test.ext" }), json!(false)], window, cx);
    });
    cx.run_until_parked();
    let focus = |cx: &mut gpui::VisualTestContext| {
        workbench.update_in(cx, |workbench, window, _| {
            let thread = &workbench.comments.controllers[0].threads[0];
            (workbench.key_context(window)["commentFocused"].clone(), workbench.comments.focused_thread.map(|focused| focused.2), thread.collapse_state)
        })
    };
    cx.simulate_keystrokes("alt-f9");
    cx.run_until_parked();
    assert_eq!(focus(cx), (json!(true), Some(None), EXPANDED));
    cx.simulate_keystrokes("down down");
    assert_eq!(focus(cx).1, Some(Some(1)));
    cx.simulate_keystrokes("escape");
    cx.run_until_parked();
    let (focused, _, collapse_state) = focus(cx);
    assert_eq!((focused, collapse_state), (json!(false), COLLAPSED));
    workbench.update_in(cx, |workbench, window, _| {
        assert!(workbench.editor_focus.is_focused(window));
        assert_eq!(workbench.editors.active().unwrap().selection().start(), Position::new(1, 0));
    });
}