Skip to content

Visual Studio Code, rebuilt in Rust

Rust
12.5 KB
Raw Blame
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
use serde_json::{Value, json};

use super::*;
use crate::workbench::goto_symbol::tests::open;

fn floating(workbench: &gpui::Entity<Workbench>, cx: &mut VisualTestContext) -> Option<(Option<Layout>, f32, f32, f32)> {
    floating_in(workbench, None, cx)
}

/// The first floating widget, of editor `model_id` when given.
fn floating_in(workbench: &gpui::Entity<Workbench>, model_id: Option<u32>, cx: &mut VisualTestContext) -> Option<(Option<Layout>, f32, f32, f32)> {
    workbench.read_with(cx, |workbench, _| {
        workbench.comments.widgets.iter().filter(|widget| model_id.is_none_or(|id| widget.model_id == id)).find_map(|widget| match &widget.place {
            Place::Floating(floating) => Some((floating.layout, floating.top, floating.width, floating.height)),
            Place::Zone(_) => None,
        })
    })
}

fn set_style(workbench: &gpui::Entity<Workbench>, style: &str, cx: &mut VisualTestContext) {
    workbench.update(cx, |workbench, _| {
        workbench.settings.lock().unwrap().update_value_derived("comments.thread.experimental.style", Some(&json!(style))).unwrap();
        workbench.sync_comment_widgets();
    });
}

/// With the floating style an expanded thread gets a widget instead of a zone, centered on its code at the right;
/// a title drag makes the place the user's, which the thread gets back after a collapse, and the inline style
/// brings the zone back.
#[gpui::test]
fn a_floating_thread_is_placed_by_its_code_and_keeps_where_the_user_moved_it(cx: &mut TestAppContext) {
    let text: String = (1..=60).map(|line| format!("line {line}\n")).collect();
    let (workbench, cx, _, dir) = open(cx, "comment-thread-floating", &text);
    let resource = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
    let request = |method: &str, args: Vec<Value>, cx: &mut VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_comments_request(method, &args, window, cx)));
    };
    set_style(&workbench, "floating", cx);
    request("$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], cx);
    let range = json!({ "startLineNumber": 20, "startColumn": 1, "endLineNumber": 20, "endColumn": 8 });
    request("$createCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), range, json!([]), json!({ "value": "test.ext" }), json!(false)], cx);
    let comment = json!({ "uniqueIdInThread": 1, "body": "Looks good", "userName": "Ann" });
    request("$updateCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), json!({ "comments": [comment], "collapseState": 1 })], cx);
    cx.run_until_parked();

    let zones = workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().view_zones.len());
    assert_eq!(zones, 0);
    // The expanded thread's range is highlighted on its line only, once more while the pointer is over the widget.
    let highlights = |line: usize, cx: &mut VisualTestContext| workbench.update_in(cx, |workbench, window, _| workbench.comment_range_highlights(workbench.editors.active().unwrap(), line, 7., window).len());
    assert_eq!((highlights(18, cx), highlights(19, cx), highlights(20, cx)), (0, 1, 0));
    workbench.update(cx, |workbench, _| workbench.comments.hovered = Some((1, 7)));
    assert_eq!(highlights(19, cx), 2);
    workbench.update(cx, |workbench, _| workbench.comments.hovered = None);
    let (layout, top, width, height) = floating(&workbench, cx).expect("a floating widget");
    let layout = layout.expect("placed at the first render");
    let (line_height, viewport_width) = workbench.read_with(cx, |workbench, _| (workbench.editors.viewport.line_height, workbench.editors.viewport.width));
    // Line 20's code is short, so the widget is centered on it after the lines next to it, and sized to its content.
    assert!(!layout.is_chosen_by_user && layout.offset_top.is_none(), "{layout:?}");
    assert_eq!(top, centered_top(19. * line_height, 20. * line_height, height));
    assert!(width >= 240. && layout.left + width <= viewport_width - VERTICAL_SCROLLBAR_WIDTH, "{layout:?} {width}");

    let head = cx.debug_bounds("comment-thread-floating-head").expect("the title is drawn");
    let grab = head.center();
    cx.simulate_mouse_down(grab, MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_move(grab + point(px(-10.), px(20.)), MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_move(grab + point(px(-20.), px(40.)), MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_up(grab + point(px(-20.), px(40.)), MouseButton::Left, Modifiers::default());
    cx.run_until_parked();
    let (moved, moved_top, ..) = floating(&workbench, cx).unwrap();
    let moved = moved.unwrap();
    assert!(moved.is_chosen_by_user);
    assert_eq!((moved.left, moved_top), (layout.left - 20., top + 40.));

    workbench.update(cx, |workbench, cx| workbench.toggle_comment_thread(1, 7, cx));
    assert!(floating(&workbench, cx).is_none());
    workbench.update(cx, |workbench, cx| workbench.toggle_comment_thread(1, 7, cx));
    cx.run_until_parked();
    assert_eq!(floating(&workbench, cx).unwrap().0, Some(moved));

    // An empty reply goes back to its button when the focus leaves it, here for the text editor.
    workbench.update_in(cx, |workbench, window, cx| {
        workbench.watch_comment_reply_blur(window, cx);
        workbench.expand_comment_reply(1, 7, window, cx);
    });
    cx.run_until_parked();
    workbench.update_in(cx, |workbench, window, cx| {
        let text_editor = workbench.editors.pane_editor().unwrap().model_id;
        workbench.focus_code_editor(text_editor, window, cx);
    });
    cx.run_until_parked();
    assert!(workbench.read_with(cx, |workbench, _| workbench.comments.forms.is_empty()));

    set_style(&workbench, "inline", cx);
    let zones = workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().view_zones.len());
    assert_eq!((zones, floating(&workbench, cx).is_none()), (1, true));
}

/// commentsController.ts runs in a diff's original editor too: its floating widget is placed by its code there, as
/// the original side lays it out, and moved by its title.
#[gpui::test]
fn a_floating_thread_shows_on_a_diffs_original_side(cx: &mut TestAppContext) {
    let text: String = (1..=60).map(|line| format!("line {line}\n")).collect();
    let (workbench, cx, _, dir) = open(cx, "comment-thread-floating-original", &text);
    std::fs::write(dir.join("b.txt"), text.replace("line 20\n", "line twenty\n")).unwrap();
    let uri = |name: &str| json!({ "scheme": "file", "path": dir.join(name).to_string_lossy() });
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("_workbench.diff", vec![uri("a.txt"), uri("b.txt")], window, cx)));
    cx.run_until_parked();
    let request = |method: &str, args: Vec<Value>, cx: &mut VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_comments_request(method, &args, window, cx)));
    };
    set_style(&workbench, "floating", cx);
    request("$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], cx);
    let range = json!({ "startLineNumber": 30, "startColumn": 1, "endLineNumber": 30, "endColumn": 8 });
    request("$createCommentThread", vec![json!(1), json!(7), json!("t"), uri("a.txt"), range, json!([]), json!({ "value": "test.ext" }), json!(false)], cx);
    let comment = json!({ "uniqueIdInThread": 1, "body": "Looks good", "userName": "Ann" });
    request("$updateCommentThread", vec![json!(1), json!(7), json!("t"), uri("a.txt"), json!({ "comments": [comment], "collapseState": 1 })], cx);
    cx.run_until_parked();

    let (original, line_height) = workbench.read_with(cx, |workbench, _| (workbench.editors.active_diff().unwrap().original_editor.as_ref().unwrap().model_id, workbench.editors.viewport.line_height));
    let (layout, top, _, height) = floating_in(&workbench, Some(original), cx).expect("a floating widget");
    let layout = layout.expect("placed at the first render");
    assert!(layout.offset_top.is_none(), "{layout:?}");
    assert_eq!(top, centered_top(29. * line_height, 30. * line_height, height));

    let grab = cx.debug_bounds("comment-thread-floating-head").expect("the title is drawn").center();
    cx.simulate_mouse_down(grab, MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_move(grab + point(px(-10.), px(30.)), MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_up(grab + point(px(-10.), px(30.)), MouseButton::Left, Modifiers::default());
    cx.run_until_parked();
    let (moved, moved_top, ..) = floating_in(&workbench, Some(original), cx).unwrap();
    let moved = moved.unwrap();
    assert!(moved.is_chosen_by_user);
    assert_eq!((moved.left, moved_top), (layout.left - 10., top + 30.));
    std::fs::remove_dir_all(dir).unwrap();
}

/// In the multi diff editor a file side's editor is as tall as its content: its floating widget is placed by its code
/// as the file lays it out, moved by its title, and the editor grows below its last line for the widget
/// (commentThreadFloatingWidgetRoom.ts) until the widget goes.
#[gpui::test]
fn a_multi_diff_file_makes_room_for_its_floating_thread(cx: &mut TestAppContext) {
    let text: String = (1..=60).map(|line| format!("line {line}\n")).collect();
    let (workbench, cx, _, dir) = open(cx, "comment-thread-floating-multi", &text);
    std::fs::write(dir.join("b.txt"), text.replace("line 20\n", "line twenty\n")).unwrap();
    let uri = |name: &str| json!({ "scheme": "file", "path": dir.join(name).to_string_lossy() });
    let options = json!({ "multiDiffSourceUri": { "scheme": "scm-history-item", "path": "/repo/1..2" }, "title": "Changes", "resources": [{ "originalUri": uri("a.txt"), "modifiedUri": uri("b.txt") }] });
    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("_workbench.openMultiDiffEditor", vec![options], window, cx)));
    cx.run_until_parked();
    let request = |method: &str, args: Vec<Value>, cx: &mut VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_comments_request(method, &args, window, cx)));
    };
    set_style(&workbench, "floating", cx);
    request("$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], cx);
    let range = json!({ "startLineNumber": 22, "startColumn": 1, "endLineNumber": 22, "endColumn": 8 });
    request("$createCommentThread", vec![json!(1), json!(7), json!("t"), uri("b.txt"), range, json!([]), json!({ "value": "test.ext" }), json!(false)], cx);
    let comment = json!({ "uniqueIdInThread": 1, "body": "Looks good", "userName": "Ann" });
    request("$updateCommentThread", vec![json!(1), json!(7), json!("t"), uri("b.txt"), json!({ "comments": [comment], "collapseState": 1 })], cx);
    cx.run_until_parked();

    let modified = workbench.read_with(cx, |workbench, _| workbench.editors.active_multi_diff().unwrap().files[0].modified_editor.as_ref().unwrap().model_id);
    let line_height = crate::editor::LINE_HEIGHT;
    // Lines 1-16 and 24-60 hide behind unchanged_regions_view.rs's 24px placeholders; line 22 is the sixth row.
    let (code_top, content_bottom) = (24. + 5. * line_height, 24. + 7. * line_height);
    let (layout, top, _, height) = floating_in(&workbench, Some(modified), cx).expect("a floating widget");
    assert!(layout.expect("placed at the first render").offset_top.is_none());
    assert_eq!(top, centered_top(code_top, code_top + line_height, height));
    let room = |cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active_multi_diff().unwrap().files[0].room);
    let expected = (top + height + EDGE_MARGIN - content_bottom).ceil();
    assert!(expected > 0.);
    assert_eq!(room(cx), (0., expected));

    let grab = cx.debug_bounds("comment-thread-floating-head").expect("the title is drawn").center();
    cx.simulate_mouse_down(grab, MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_move(grab + point(px(-10.), px(30.)), MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_up(grab + point(px(-10.), px(30.)), MouseButton::Left, Modifiers::default());
    cx.run_until_parked();
    let (moved, moved_top, ..) = floating_in(&workbench, Some(modified), cx).unwrap();
    assert!(moved.unwrap().is_chosen_by_user);
    assert_eq!(moved_top, top + 30.);
    assert_eq!(room(cx), (0., expected + 30.));

    workbench.update(cx, |workbench, cx| workbench.toggle_comment_thread(1, 7, cx));
    cx.run_until_parked();
    assert_eq!(room(cx), (0., 0.));
    std::fs::remove_dir_all(dir).unwrap();
}