Visual Studio Code, rebuilt in Rust
Rust
use gpui::TestAppContext;
use serde_json::json;
use super::*;
use crate::workbench::goto_symbol::tests::{answer, open};
/// The view asks the providers for the active editor, lists the symbols by position with children below their
/// expanded parent, and opens a symbol at the start of its name; Left collapses, Up and Down move the focus.
#[gpui::test]
fn the_outline_lists_the_symbols_and_opens_the_selected_one(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "outline-pane", "class A {\n run() {}\n}\nconst b = 1;\n");
let rows = |cx: &mut gpui::VisualTestContext| {
workbench.update(cx, |workbench, cx| {
workbench.refresh_outline_pane(cx);
let Status::Ready { model_id, outline, .. } = &workbench.outline_pane.status else { return Vec::new() };
workbench.outline_rows(*model_id, outline).iter().map(|row| format!("{}{}", " ".repeat(row.depth), row.element.unwrap().symbol.name)).collect::<Vec<_>>()
})
};
assert!(rows(cx).is_empty());
assert!(workbench.read_with(cx, |workbench, _| matches!(workbench.outline_pane.status, Status::NoProvider)), "no provider, no outline");
workbench.update(cx, |workbench, _| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.outline_pane.key = None;
});
rows(cx);
let range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
let selection = |line: usize, column: usize| json!({ "startLineNumber": line, "startColumn": column, "endLineNumber": line, "endColumn": column + 1 });
answer(&receiver, "$provideDocumentSymbols", json!([
{ "name": "b", "detail": "", "kind": 13, "range": range(4, 4), "selectionRange": selection(4, 7), "children": [] },
{ "name": "A", "detail": "", "kind": 4, "range": range(1, 3), "selectionRange": selection(1, 7), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": selection(2, 3), "children": [] },
] },
]), cx);
assert_eq!(rows(cx), ["A", " run", "b"]);
let key = |key: &'static str, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| workbench.outline_key_down(key, window, cx));
key("down", cx);
key("down", cx);
key("enter", cx);
workbench.read_with(cx, |workbench, _| {
assert_eq!(workbench.outline_pane.selected, ["root/provider_0/A/run"]);
let editor = workbench.editors.active().unwrap();
assert_eq!(editor.selection().head, crate::editor::Position::new(1, 2), "the start of the name");
assert_eq!(workbench.breadcrumb_symbols(editor), [(Some(4), "A".to_string()), (Some(5), "run".to_string())], "the breadcrumbs follow the cursor");
});
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("breadcrumbs.copyPath", vec![], window, cx)));
assert_eq!(cx.read_from_clipboard().and_then(|item| item.text()).as_deref(), Some("A.run"), "Copy Breadcrumbs Path joins the symbols with `.`");
key("left", cx);
assert_eq!(rows(cx), ["A", " run", "b"], "a childless row has nothing to collapse");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.outline_pane.focused.clone()).as_deref(), Some("root/provider_0/A"), "Left on a leaf goes to its parent");
key("left", cx);
assert_eq!(rows(cx), ["A", "b"]);
key("right", cx);
assert_eq!(rows(cx), ["A", " run", "b"]);
}
/// outlineActions.ts: Sort By reorders siblings, Collapse All folds every parent, and Follow Cursor expands to and
/// selects the symbol around the cursor.
#[gpui::test]
fn the_title_actions_sort_collapse_and_follow_the_cursor(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "outline-actions", "class B {\n run() {}\n}\nconst a = 1;\n");
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.refresh_outline_pane(cx);
assert!(matches!(workbench.outline_pane.status, Status::NoProvider), "the editor opened before the provider");
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
answer(&receiver, "$provideDocumentSymbols", json!([
{ "name": "a", "detail": "", "kind": 13, "range": range(4, 4), "selectionRange": range(4, 4), "children": [] },
{ "name": "B", "detail": "", "kind": 4, "range": range(1, 3), "selectionRange": range(1, 1), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": range(2, 2), "children": [] },
] },
]), cx);
let rows = |cx: &mut gpui::VisualTestContext| {
workbench.update(cx, |workbench, _| {
let Status::Ready { model_id, outline, .. } = &workbench.outline_pane.status else { return Vec::new() };
workbench.outline_rows(*model_id, outline).iter().map(|row| format!("{}{}", " ".repeat(row.depth), row.element.unwrap().symbol.name)).collect::<Vec<_>>()
})
};
let run = |command: &str, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(command, Vec::new(), window, cx)));
assert_eq!(rows(cx), ["B", " run", "a"]);
// outlineModel.ts `getOrCreate`: the cached outline of this version is stale once another provider registers.
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(12), json!("*"), json!("Second")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
answer(&receiver, "$provideDocumentSymbols", json!([{ "name": "c", "detail": "", "kind": 13, "range": range(4, 4), "selectionRange": range(4, 4), "children": [] }]), cx);
assert!(workbench.read_with(cx, |workbench, _| matches!(&workbench.outline_pane.status, Status::Ready { outline, .. } if outline.groups.len() == 2)), "both providers answered");
workbench.update(cx, |workbench, cx| {
workbench.language_features.unregister(12);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
answer(&receiver, "$provideDocumentSymbols", json!([
{ "name": "a", "detail": "", "kind": 13, "range": range(4, 4), "selectionRange": range(4, 4), "children": [] },
{ "name": "B", "detail": "", "kind": 4, "range": range(1, 3), "selectionRange": range(1, 1), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": range(2, 2), "children": [] },
] },
]), cx);
run("outline.sortByName", cx);
assert_eq!(rows(cx), ["a", "B", " run"]);
run("outline.sortByKind", cx);
assert_eq!(rows(cx), ["B", " run", "a"], "class (4) before constant (13)");
run("outline.collapse", cx);
assert_eq!(rows(cx), ["B", "a"]);
assert!(workbench.read_with(cx, |workbench, _| workbench.outline_all_collapsed()));
run("outline.followCursor", cx);
workbench.update(cx, |workbench, cx| {
workbench.editors.active_mut().unwrap().set_selections(vec![crate::editor::Selection::caret(crate::editor::Position::new(1, 3))]);
workbench.refresh_outline_pane(cx);
});
assert_eq!(rows(cx), ["B", " run", "a"], "the parent of the followed symbol expands");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.outline_pane.selected.clone()), ["root/provider_0/B/run"]);
let stored = workbench.read_with(cx, |workbench, _| workbench.workspace_storage.lock().unwrap().get("outline/state").map(str::to_owned));
assert_eq!(stored.as_deref(), Some(r#"{"followCursor":true,"sortBy":2,"filterOnType":true}"#));
}
/// abstractTree.ts `FindController`: Alt+Cmd+F opens the widget over the focused Outline; with Filter on Type the rows
/// are the matches and their parents, all expanded, in highlight mode every row stays; closing restores the expansion.
#[gpui::test]
fn find_filters_or_highlights_the_symbols(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "outline-find", "class B {\n run() {}\n}\nconst a = 1;\n");
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
answer(&receiver, "$provideDocumentSymbols", json!([
{ "name": "a", "detail": "", "kind": 13, "range": range(4, 4), "selectionRange": range(4, 4), "children": [] },
{ "name": "B", "detail": "", "kind": 4, "range": range(1, 3), "selectionRange": range(1, 1), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": range(2, 2), "children": [] },
] },
]), cx);
let rows = |cx: &mut gpui::VisualTestContext| {
workbench.update(cx, |workbench, _| {
let Status::Ready { model_id, outline, .. } = &workbench.outline_pane.status else { return Vec::new() };
workbench.outline_rows(*model_id, outline).iter().map(|row| format!("{}{}", " ".repeat(row.depth), row.label())).collect::<Vec<_>>()
})
};
let run = |command: &str, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(command, Vec::new(), window, cx)));
workbench.update(cx, |workbench, _| {
let path = std::env::temp_dir().join("outline-find-keybindings.json");
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), path);
});
run("outline.collapse", cx);
run("outline.focus", cx);
assert_eq!(rows(cx), ["B", "a"]);
cx.simulate_keystrokes(&crate::keybindings::primary_keys("alt-secondary-f"));
assert!(workbench.update_in(cx, |workbench, window, _| workbench.outline_pane.find.is_open() && workbench.outline_pane.find.focus.is_focused(window)));
cx.simulate_input("ru");
assert_eq!(rows(cx), ["B", " run"], "the filter keeps the match and its parent, expanded");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.outline_query().unwrap().matches("run")), Some(vec![0..2]));
cx.simulate_input("x");
assert_eq!(rows(cx), Vec::<String>::new());
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.outline_find_message()).as_deref(), Some("No results found."));
cx.run_until_parked();
// The reference's find input is 155.5px wide (its widget 187.5px) and the message box half a point left of it.
let (input, message) = (cx.debug_bounds("tree-find-input").unwrap(), cx.debug_bounds("tree-find-message").unwrap());
assert_eq!((f32::from(input.size.width), f32::from(message.origin.x - input.origin.x)), (155.5, -0.5));
assert!(workbench.read_with(cx, |workbench, _| !workbench.outline_all_collapsed()), "the filtered-out rows stay expanded, so Collapse All stays");
cx.simulate_keystrokes("backspace");
run("list.toggleFindMode", cx);
assert!(workbench.read_with(cx, |workbench, _| !workbench.outline_pane.view_state.filter_on_type), "the widget's toggle is Filter on Type");
assert_eq!(rows(cx), ["B", " run", "a"], "highlight mode keeps every row");
cx.simulate_keystrokes("enter");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.outline_pane.focused.clone()).as_deref(), Some("root/provider_0/B/run"), "the tree takes the focus on the first match");
run("outline.filterOnType", cx);
cx.simulate_keystrokes("escape");
assert!(workbench.update_in(cx, |workbench, window, _| !workbench.outline_pane.find.is_open() && workbench.outline_pane.focus.is_focused(window)));
assert_eq!(rows(cx), ["B", "a"], "closing the filter restores the expansion");
}
/// listWidget.ts: the keys move the focus trait, Enter selects it, Space toggles a parent; the list commands page,
/// collapse, clear, toggle the selection and show the focused symbol's title hover.
#[gpui::test]
fn the_keys_move_the_focus_and_the_list_commands_act_on_it(cx: &mut TestAppContext) {
let source: String = (0..40).map(|line| format!("const s{line} = 1;\n")).collect();
let (workbench, cx, receiver, _) = open(cx, "outline-focus", &format!("class B {{\n run() {{}}\n}}\n{source}"));
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
let mut symbols = vec![json!({ "name": "B", "detail": "", "kind": 4, "range": range(1, 3), "selectionRange": range(1, 1), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": range(2, 2), "selectionRange": range(2, 2), "children": [] },
] })];
symbols.extend((0..40).map(|index| json!({ "name": format!("s{index}"), "detail": "", "kind": 13, "range": range(index + 4, index + 4), "selectionRange": range(index + 4, index + 4), "children": [] })));
answer(&receiver, "$provideDocumentSymbols", json!(symbols), cx);
let run = |command: &str, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command(command, Vec::new(), window, cx)));
let key = |key: &'static str, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| {
workbench.outline_key_down(key, window, cx);
});
let state = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.outline_pane.focused.clone(), workbench.outline_pane.selected.clone()));
let id = |name: &str| format!("root/provider_0/{name}");
let focus_and_selection = |focused: &str, selected: &[&str]| (Some(id(focused)), selected.iter().map(|name| id(name)).collect::<Vec<_>>());
let row_count = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.outline_shown_rows().len());
run("outline.focus", cx);
cx.run_until_parked();
key("down", cx);
key("down", cx);
assert_eq!(state(cx), focus_and_selection("B/run", &[]), "the keys move the focus, not the selection");
run("list.toggleSelection", cx);
assert_eq!(state(cx), focus_and_selection("B/run", &["B/run"]));
key("up", cx);
run("list.toggleSelection", cx);
assert_eq!(state(cx), focus_and_selection("B", &["B/run", "B"]), "the focused row joins the selection");
run("list.toggleSelection", cx);
key("down", cx);
run("list.toggleSelection", cx);
assert_eq!(state(cx), focus_and_selection("B/run", &[]));
run("list.showHover", cx);
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.hovers.shown_text().map(str::to_string)).as_deref(), Some("run (method)"));
run("list.focusParent", cx);
assert_eq!(state(cx), focus_and_selection("B", &[]));
key("space", cx);
assert_eq!(row_count(cx), 41, "Space collapses the focused parent");
key("space", cx);
run("list.collapseAllToFocus", cx);
assert_eq!((row_count(cx), state(cx)), (41, focus_and_selection("B", &["B"])), "the focused row collapses and is selected");
key("right", cx);
run("list.collapseAll", cx);
assert_eq!(row_count(cx), 41);
let (viewport, offset) = workbench.read_with(cx, |workbench, _| (f32::from(workbench.outline_pane.scroll.bounds().size.height), f32::from(workbench.outline_pane.scroll.offset().y)));
assert!(viewport > 0. && viewport < 41. * 22., "the rows overflow the view");
assert_eq!(offset, 0.);
run("list.focusPageDown", cx);
let last_on_page = (viewport / 22.).floor() as usize - 1;
assert_eq!(state(cx).0, Some(id(&format!("s{}", last_on_page - 1))), "the last row of the page");
run("list.focusPageDown", cx);
cx.run_until_parked();
let offset = workbench.read_with(cx, |workbench, _| f32::from(workbench.outline_pane.scroll.offset().y));
assert!(offset < 0., "a second Page Down scrolls a page");
run("list.scrollDown", cx);
cx.run_until_parked();
let scrolled = workbench.read_with(cx, |workbench, _| f32::from(workbench.outline_pane.scroll.offset().y));
assert_eq!(scrolled, offset - 10., "the focused Outline is the list `list.scrollDown` moves");
run("list.focusPageUp", cx);
key("enter", cx);
let (focused, selected) = state(cx);
assert_eq!(Vec::from_iter(focused), selected, "Enter selects the focused row");
run("outline.focus", cx);
run("list.clear", cx);
assert_eq!(state(cx), (None, Vec::new()));
}
/// listView.ts renders only the rows in view: a long outline draws the first rows, and the last ones once revealed,
/// in their place below the rows above them.
#[gpui::test]
fn the_outline_draws_only_the_rows_in_view(cx: &mut TestAppContext) {
let source: String = (0..2000).map(|line| format!("const s{line} = 1;\n")).collect();
let (workbench, cx, receiver, _) = open(cx, "outline-virtual", &source);
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = |line: usize| json!({ "startLineNumber": line, "startColumn": 1, "endLineNumber": line, "endColumn": 20 });
let symbols: Vec<_> = (0..2000).map(|index| json!({ "name": format!("s{index}"), "detail": "", "kind": 13, "range": range(index + 1), "selectionRange": range(index + 1), "children": [] })).collect();
answer(&receiver, "$provideDocumentSymbols", json!(symbols), cx);
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("outline.focus", Vec::new(), window, cx)));
cx.run_until_parked();
assert!(cx.debug_bounds("outline-row-0").is_some());
assert!(cx.debug_bounds("outline-row-1999").is_none(), "a row far below the view is not drawn");
workbench.update_in(cx, |workbench, window, cx| {
workbench.outline_key_down("end", window, cx);
});
cx.run_until_parked();
let list = workbench.read_with(cx, |workbench, _| workbench.outline_pane.scroll.bounds());
let last = cx.debug_bounds("outline-row-1999").expect("the revealed row is drawn");
assert!(cx.debug_bounds("outline-row-0").is_none(), "a row scrolled out of view is not drawn");
assert_eq!(last.bottom(), list.bottom(), "the last row ends the scrolled list");
}
/// breadcrumbsWidget.css: a symbol item is its icon, 3px, its label at the label's exact width and the 16px separator.
#[gpui::test]
fn the_breadcrumb_separator_follows_the_label_s_exact_width(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "breadcrumb-width", "class B {\n run() {}\n}\n");
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = json!({ "startLineNumber": 1, "startColumn": 1, "endLineNumber": 3, "endColumn": 2 });
let run = json!({ "startLineNumber": 2, "startColumn": 1, "endLineNumber": 2, "endColumn": 11 });
answer(&receiver, "$provideDocumentSymbols", json!([{ "name": "Bb", "detail": "", "kind": 4, "range": range, "selectionRange": range, "children": [
{ "name": "run", "detail": "", "kind": 5, "range": run, "selectionRange": run, "children": [] },
] }]), cx);
workbench.update(cx, |workbench, cx| {
workbench.editors.active_mut().unwrap().set_selections(vec![crate::editor::Selection::caret(crate::editor::Position::new(1, 3))]);
cx.notify();
});
cx.run_until_parked();
// NoopTextSystem shapes "Bb" 15.6px wide at 13px: 31 device px at 2x, where GPUI's text box rounds up to 16px.
let width = f32::from(cx.debug_bounds("breadcrumb-item-1").unwrap().size.width);
assert_eq!(width, 16. + 3. + 15.5 + 16.);
}
/// tree.css: the twistie is 8px indent, 16px and 6px padding-right, drawn translateX(3px).
#[gpui::test]
fn a_row_s_contents_start_30px_into_the_row(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "outline-row-inset", "class B {\n run() {}\n}\n");
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = json!({ "startLineNumber": 1, "startColumn": 1, "endLineNumber": 3, "endColumn": 2 });
answer(&receiver, "$provideDocumentSymbols", json!([{ "name": "B", "detail": "", "kind": 4, "range": range, "selectionRange": range, "children": [] }]), cx);
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("outline.focus", Vec::new(), window, cx)));
cx.run_until_parked();
let left = |selector: &'static str, cx: &mut gpui::VisualTestContext| f32::from(cx.debug_bounds(selector).unwrap().origin.x);
assert_eq!(left("outline-row-contents-0", cx) - left("outline-row-0", cx), 30.);
}
/// iconlabel.css: the detail sits 0.5em of its 11.7px after the name's exact width, after the 16px icon and 6px.
#[gpui::test]
fn the_detail_follows_the_name_s_exact_width(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "outline-detail", "class B {\n run() {}\n}\n");
workbench.update(cx, |workbench, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
workbench.language_providers_changed(crate::workbench::document_symbols::REGISTER, cx);
});
let range = json!({ "startLineNumber": 1, "startColumn": 1, "endLineNumber": 3, "endColumn": 2 });
answer(&receiver, "$provideDocumentSymbols", json!([{ "name": "Bb", "detail": "d", "kind": 4, "range": range, "selectionRange": range, "children": [] }]), cx);
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("outline.focus", Vec::new(), window, cx)));
cx.run_until_parked();
let left = |selector: &'static str, cx: &mut gpui::VisualTestContext| f32::from(cx.debug_bounds(selector).unwrap().origin.x);
// NoopTextSystem shapes "Bb" 15.6px wide at 13px; 16 + 6 + 15.6 + 5.85 lands on 43.5 at 2x device px.
assert_eq!(left("outline-detail", cx) - left("outline-row-contents-0", cx), 43.5);
}
/// mainThreadCustomEditors.ts `resolveCustomTextEditorNavigation`: a custom text editor has an outline only once its
/// navigation resolved; the outline and breadcrumbs follow its selection, a selection the host reports while
/// resolving wins, and the open's `options.selection` and a symbol's click reveal in the panel, not in the text editor.
#[gpui::test]
fn a_custom_text_editor_s_navigation_drives_the_outline(cx: &mut TestAppContext) {
let (workbench, cx, receiver, _) = open(cx, "custom-navigation", "class A {\n run() {}\n}\nconst b = 1;\n");
let extension = json!({ "id": "port.test", "location": { "scheme": "file", "path": "/ext" } });
let resource = workbench.update_in(cx, |workbench, window, cx| {
workbench.language_features.register(crate::workbench::document_symbols::REGISTER, vec![json!(11), json!("*"), json!("Test")]);
let args = [extension, json!("test.preview"), json!({}), json!({ "supportsMove": false, "supportsNavigation": true }), json!(false)];
drop(workbench.handle_custom_editors_request("$registerTextEditorProvider", &args, window, cx));
let resource = workbench.editors.active().unwrap().document().uri();
let options = json!({ "selection": { "startLineNumber": 4, "startColumn": 7 } });
drop(workbench.open_with_resolver(&resource, Some("test.preview"), options, window, cx));
resource
});
cx.run_until_parked();
answer(&receiver, "$resolveCustomEditor", json!(null), cx);
let status_is_ready = |cx: &mut gpui::VisualTestContext| {
workbench.update(cx, |workbench, cx| {
workbench.refresh_outline_pane(cx);
matches!(workbench.outline_pane.status, Status::Ready { .. })
})
};
assert!(!status_is_ready(cx), "no outline before the navigation resolved");
let navigation = receiver.try_iter().find(|message| message.method == "$resolveCustomTextEditorNavigation").expect("the navigation asked for");
let handle = navigation.args[0].clone();
assert_eq!(navigation.args[1..], [json!("test.preview"), resource]);
let selection = |line: usize, column: usize| json!({ "selectionStartLineNumber": line, "selectionStartColumn": column, "positionLineNumber": line, "positionColumn": column });
workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_custom_editors_request("$onDidChangeCustomTextEditorSelection", &[handle.clone(), selection(2, 3)], window, cx)));
let _ = navigation.reply.unwrap().send(Ok(json!({ "selection": selection(1, 1) })));
cx.run_until_parked();
let messages: Vec<_> = receiver.try_iter().collect();
let reveal = messages.iter().find(|message| message.method == "$revealCustomTextEditorRange").expect("options.selection revealed");
let range = |start: (usize, usize), end: (usize, usize)| json!({ "startLineNumber": start.0, "startColumn": start.1, "endLineNumber": end.0, "endColumn": end.1 });
let ltr = |start: (usize, usize), end: (usize, usize)| json!({ "selectionStartLineNumber": start.0, "selectionStartColumn": start.1, "positionLineNumber": end.0, "positionColumn": end.1 });
assert_eq!(reveal.args, [handle.clone(), range((4, 7), (4, 7)), ltr((4, 7), (4, 7)), json!(false)]);
let head = workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().selection().head);
assert_eq!((head.line, head.column), (0, 0), "options.selection leaves the text editor under the panel alone");
let symbols = messages.into_iter().find(|message| message.method == "$provideDocumentSymbols").expect("the outline asks once the navigation resolved");
let symbol_range = |start: usize, end: usize| json!({ "startLineNumber": start, "startColumn": 1, "endLineNumber": end, "endColumn": 20 });
let name = |line: usize, column: usize| json!({ "startLineNumber": line, "startColumn": column, "endLineNumber": line, "endColumn": column + 1 });
let _ = symbols.reply.unwrap().send(Ok(json!([
{ "name": "b", "detail": "", "kind": 13, "range": symbol_range(4, 4), "selectionRange": name(4, 7), "children": [] },
{ "name": "A", "detail": "", "kind": 4, "range": symbol_range(1, 3), "selectionRange": name(1, 7), "children": [
{ "name": "run", "detail": "", "kind": 5, "range": symbol_range(2, 2), "selectionRange": name(2, 3), "children": [] },
] },
])));
cx.run_until_parked();
assert!(status_is_ready(cx));
let breadcrumbs = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.breadcrumb_symbols(workbench.editors.active().unwrap()));
assert_eq!(breadcrumbs(cx), [(Some(4), "A".to_string()), (Some(5), "run".to_string())], "the selection reported while resolving, not the text editor's cursor");
workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_custom_editors_request("$onDidChangeCustomTextEditorSelection", &[handle.clone(), selection(4, 7)], window, cx)));
assert_eq!(breadcrumbs(cx), [(Some(13), "b".to_string())]);
let head = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().selection().head);
let before = head(cx);
workbench.update_in(cx, |workbench, window, cx| workbench.reveal_outline_symbol("root/provider_0/A", false, window, cx));
let reveal = receiver.try_iter().find(|message| message.method == "$revealCustomTextEditorRange").expect("a click reveals in the panel");
assert_eq!(reveal.args, [handle, range((1, 1), (3, 20)), ltr((1, 7), (1, 7)), json!(true)], "the symbol's range, its name's start selected");
assert_eq!(head(cx), before, "the text editor under the panel keeps its cursor");
}