Documents: open cells keep presses; finds open their cell
A press inside the open cell stays with its field. Find opens the cell holding a match. A closing backslash no longer escapes the next pipe. A right press after typed pipes reads the right byte. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5dc4dd1805
commit
6a3f3486fd
9 files changed
+233
-39
No files matched your search
@@ -1,24 +1,62 @@
|
||||
//! A press in a table opens only its cell (R6.1).
|
||||
|
||||
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
|
||||
use gpui::{Entity, Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
|
||||
|
||||
use super::{open, opened, text};
|
||||
use crate::documents::MarkdownMode;
|
||||
use super::{open, opened, point::heard, text};
|
||||
use crate::documents::{MarkdownDocument, MarkdownMode};
|
||||
|
||||
const TABLE: &str = "| a | **bold** |\n| --- | --- |\n| c | |\n";
|
||||
|
||||
/// Presses cell `row`, `column` of the first table near its left.
|
||||
/// Presses cell `row`, `column` of the first table.
|
||||
fn press_cell(row: usize, column: usize, cx: &mut VisualTestContext) {
|
||||
press_cell_with(row, column, MouseButton::Left, cx);
|
||||
}
|
||||
|
||||
fn press_cell_with(row: usize, column: usize, button: MouseButton, cx: &mut VisualTestContext) {
|
||||
let name = format!("markdown-cell-0-{row}-{column}");
|
||||
let bounds = cx
|
||||
.debug_bounds(Box::leak(name.into_boxed_str()))
|
||||
.expect("the cell is drawn");
|
||||
let at = point(bounds.left() + px(2.0), bounds.top() + px(2.0));
|
||||
cx.simulate_mouse_down(at, MouseButton::Left, Modifiers::none());
|
||||
cx.simulate_mouse_up(at, MouseButton::Left, Modifiers::none());
|
||||
cx.simulate_mouse_down(at, button, Modifiers::none());
|
||||
cx.simulate_mouse_up(at, button, Modifiers::none());
|
||||
cx.run_until_parked();
|
||||
}
|
||||
|
||||
/// Presses the open cell's field `x` past its left.
|
||||
fn press_field(x: f32, button: MouseButton, cx: &mut VisualTestContext) {
|
||||
let field = cx
|
||||
.debug_bounds("markdown-open-cell")
|
||||
.expect("a cell is open");
|
||||
let at = point(field.left() + px(x), field.top() + px(10.0));
|
||||
cx.simulate_mouse_down(at, button, Modifiers::none());
|
||||
cx.simulate_mouse_up(at, button, Modifiers::none());
|
||||
cx.run_until_parked();
|
||||
}
|
||||
|
||||
fn caret(document: &Entity<MarkdownDocument>, cx: &mut VisualTestContext) -> usize {
|
||||
document.read_with(cx, |document, cx| {
|
||||
document
|
||||
.open
|
||||
.as_ref()
|
||||
.expect("open")
|
||||
.input
|
||||
.read(cx)
|
||||
.cursor()
|
||||
})
|
||||
}
|
||||
|
||||
/// Where byte `k` of the open cell lies.
|
||||
fn x_of(document: &Entity<MarkdownDocument>, k: usize, cx: &mut VisualTestContext) -> f32 {
|
||||
(0..400)
|
||||
.map(|step| step as f32)
|
||||
.find(|&x| {
|
||||
press_field(x, MouseButton::Left, cx);
|
||||
caret(document, cx) == k
|
||||
})
|
||||
.expect("the byte is drawn")
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_opens_its_cell_and_writes_one_undo_step(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(TABLE, MarkdownMode::Visual, cx);
|
||||
@@ -56,3 +94,80 @@ fn tab_and_arrows_walk_the_cells(cx: &mut TestAppContext) {
|
||||
assert!(document.read_with(cx, |document, _| document.open.is_none()));
|
||||
assert_eq!(text(&document, cx), TABLE);
|
||||
}
|
||||
|
||||
const WORDS: &str = "| alpha | beta |\n| --- | --- |\n| c | d |\n";
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_in_the_open_cell_moves_its_caret_and_keeps_one_step(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(WORDS, MarkdownMode::Visual, cx);
|
||||
press_cell(0, 0, cx);
|
||||
assert_eq!(caret(&document, cx), 5);
|
||||
cx.simulate_input("X");
|
||||
let x = x_of(&document, 1, cx);
|
||||
press_field(x, MouseButton::Left, cx);
|
||||
assert_eq!(caret(&document, cx), 1, "the cell stays open");
|
||||
cx.simulate_input("Y");
|
||||
cx.simulate_keystrokes("escape");
|
||||
assert_eq!(
|
||||
text(&document, cx),
|
||||
"| aYlphaX | beta |\n| --- | --- |\n| c | d |\n"
|
||||
);
|
||||
let editor = document.read_with(cx, |document, _| document.editor().clone());
|
||||
editor.update(cx, |editor, cx| editor.undo(cx));
|
||||
assert_eq!(text(&document, cx), WORDS);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_match_in_a_table_opens_its_cell_selected(cx: &mut TestAppContext) {
|
||||
let table = "| needle | B |\n| --- | --- |\n| C | tail |\n";
|
||||
let (document, cx) = open(table, MarkdownMode::Visual, cx);
|
||||
let at = table.find("needle").expect("the word");
|
||||
document.update_in(cx, |document, window, cx| {
|
||||
document.show_match(at..at + 6, window, cx)
|
||||
});
|
||||
assert_eq!(opened(&document, cx), "needle");
|
||||
let picked = document.read_with(cx, |document, cx| {
|
||||
document
|
||||
.open
|
||||
.as_ref()
|
||||
.expect("open")
|
||||
.input
|
||||
.read(cx)
|
||||
.selection()
|
||||
});
|
||||
assert_eq!(picked, 0..6);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_closing_backslash_keeps_the_table(cx: &mut TestAppContext) {
|
||||
let table = "|a|b|\n|---|---|\n|c|d|\n";
|
||||
let (document, cx) = open(table, MarkdownMode::Visual, cx);
|
||||
press_cell(0, 0, cx);
|
||||
cx.simulate_input("\\");
|
||||
cx.simulate_keystrokes("escape");
|
||||
assert_eq!(text(&document, cx), "|a\\\\|b|\n|---|---|\n|c|d|\n");
|
||||
press_cell(0, 1, cx);
|
||||
assert_eq!(opened(&document, cx), "b", "still its own cell");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_right_press_after_typed_pipes_finds_the_link(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(WORDS, MarkdownMode::Visual, cx);
|
||||
let heard = heard(&document, cx);
|
||||
press_cell(1, 1, cx);
|
||||
cx.simulate_keystrokes("backspace");
|
||||
cx.simulate_input("||[go](https://ely.dev)");
|
||||
let x = x_of(&document, 3, cx);
|
||||
press_field(x, MouseButton::Right, cx);
|
||||
let target = heard.borrow().last().cloned().expect("heard");
|
||||
assert_eq!(target.link.as_deref(), Some("https://ely.dev"));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_right_press_on_a_cell_points_at_it(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(WORDS, MarkdownMode::Visual, cx);
|
||||
let heard = heard(&document, cx);
|
||||
press_cell_with(1, 1, MouseButton::Right, cx);
|
||||
let at = heard.borrow().last().expect("heard").at;
|
||||
assert_eq!(at, WORDS.rfind(" d").expect("the cell") + 2);
|
||||
}
|
||||
Reference in new issue
Block a user