From fef1f4ddb1dd546f94de045318932d47fbc934d6 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 12 Oct 2026 20:25:14 +0200 Subject: [PATCH 001/687] Security: the --control server requires an auth token and closes bad connections (#777) Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 2 +- CLAUDE.md | 2 +- Cargo.lock | 2 + ROADMAP.md | 2 + apps/gridcraft/Cargo.toml | 3 + apps/gridcraft/src/control_server.rs | 139 +++++++++++++++++++++++++-- apps/gridcraft/src/main.rs | 10 +- crates/mcp/src/backend.rs | 35 ++++++- crates/mcp/src/control_auth.rs | 135 ++++++++++++++++++++++++++ crates/mcp/src/lib.rs | 1 + crates/mcp/src/tests.rs | 51 +++++++++- docs/control-protocol.md | 26 ++++- docs/mcp.md | 2 +- 13 files changed, 393 insertions(+), 17 deletions(-) create mode 100644 crates/mcp/src/control_auth.rs diff --git a/AGENTS.md b/AGENTS.md index 963fc6d748..ccead14c6f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -42,7 +42,7 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr ## Running and looking at the app - `cargo run --release -p gridcraft -- --sample sales --control 7979` (sample workbook + control channel). -- Drive it: JSON lines on `127.0.0.1:7979`, e.g. `{"id":1,"method":"engine.execute","params":{"command":"cell.set","params":{"cell":"B2","input":"=SUM(A1:A9)"}}}` then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`. +- Drive it: `gridcraft-cli send 7979 ''` (reads the token file the app printed on stderr), or JSON lines on `127.0.0.1:7979` whose first line is `{"id":0,"method":"auth","params":{"token":"…"}}` (token: `$XDG_RUNTIME_DIR/gridcraft/control-7979.token`, or your own `GRIDCRAFT_CONTROL_TOKEN`), e.g. `{"id":1,"method":"engine.execute","params":{"command":"cell.set","params":{"cell":"B2","input":"=SUM(A1:A9)"}}}` then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`. - **Offscreen UI render** (no window, from a source checkout): `cargo run -p gridcraft-ui-egui --example snapshot -- --sample sales out.png`. - Headless: `gridcraft-cli eval '=SUM(1,2,3)'`, `gridcraft-cli run --in book.xlsx --cmd 'home.bold={"range":"A1:C1"}' --out book.xlsx`, `gridcraft-cli mcp`. - **For UI work, look at the result** (snapshot PNG) and compare with `plan/excel/observed-ui.md`. diff --git a/CLAUDE.md b/CLAUDE.md index 280fe8bcb0..2a7ee363ee 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -42,7 +42,7 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr ## Running and looking at the app - `cargo run --release -p gridcraft -- --sample sales --control 7979` (sample workbook + control channel). -- Drive it: JSON lines on `127.0.0.1:7979`, e.g. `{"id":1,"method":"engine.execute","params":{"command":"cell.set","params":{"cell":"B2","input":"=SUM(A1:A9)"}}}` then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`. +- Drive it: `gridcraft-cli send 7979 ''` (reads the token file the app printed on stderr), or JSON lines on `127.0.0.1:7979` whose first line is `{"id":0,"method":"auth","params":{"token":"…"}}` (token: `$XDG_RUNTIME_DIR/gridcraft/control-7979.token`, or your own `GRIDCRAFT_CONTROL_TOKEN`), e.g. `{"id":1,"method":"engine.execute","params":{"command":"cell.set","params":{"cell":"B2","input":"=SUM(A1:A9)"}}}` then `{"id":2,"method":"ui.screenshot","params":{"path":"/tmp/shot.png"}}`. Methods: `crates/ui-egui/src/control.rs`, docs: `docs/control-protocol.md`. - **Offscreen UI render** (no window, from a source checkout): `cargo run -p gridcraft-ui-egui --example snapshot -- --sample sales out.png`. - Headless: `gridcraft-cli eval '=SUM(1,2,3)'`, `gridcraft-cli run --in book.xlsx --cmd 'home.bold={"range":"A1:C1"}' --out book.xlsx`, `gridcraft-cli mcp`. - **For UI work, look at the result** (snapshot PNG) and compare with `plan/excel/observed-ui.md`. diff --git a/Cargo.lock b/Cargo.lock index ade3a5adba..c3fa066500 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1503,7 +1503,9 @@ dependencies = [ "eframe", "egui", "fmv-macos-events", + "getrandom 0.4.3", "gridcraft-engine", + "gridcraft-mcp", "gridcraft-ui-egui", "image", "log", diff --git a/ROADMAP.md b/ROADMAP.md index e8e1c06b38..930ded70dc 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-12 | Security: the `--control` server requires an auth token (random, kept in a 0600 file or chosen with `GRIDCRAFT_CONTROL_TOKEN`) on each connection's first line and closes bad connections (#777) | | 2026-10-12 | File › Print selects a system printer, copies, duplex and grayscale, submits PDF jobs through craft-print on a worker and shows spooler errors; browser printing and PDF export remain available | | 2026-10-11 | Command-sequence fuzzing: random command sequences checked against undo/redo, XLSX round-trip and full recalculation; fixes for the table, legacy-array, spill, AutoSum, Watch Window and sheet-copy bugs it found | | 2026-10-11 | Password-protected workbooks open: GridCraft asks for the password and decrypts Agile and Standard encryption (#11) | @@ -178,6 +179,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-12 | minor | The control server requires an auth token and closes bad connections (#777); progress log row | | 2026-10-12 | minor | Desktop File > Print now discovers printers and submits PDF jobs through pinned craft-print with printer choice, copies, duplex and grayscale; browser printing and PDF export remain available. Remaining print parity includes page ranges and driver-specific tray/options UI; physical printer fidelity needs hardware validation. | | 2026-10-11 | minor | Password-encrypted workbooks open (#11); progress log row | | 2026-10-11 | minor | Forecast Sheet (#322); progress log row | diff --git a/apps/gridcraft/Cargo.toml b/apps/gridcraft/Cargo.toml index c867d24a2a..14bc66b69b 100644 --- a/apps/gridcraft/Cargo.toml +++ b/apps/gridcraft/Cargo.toml @@ -27,6 +27,9 @@ winresource = "0.1" craft-print = { git = "https://github.com/storytold/craft-libs", rev = "d27646a5f7c8b4fc85d9dd3213b8c88b58e0088d" } rfd = { workspace = true } arboard = { workspace = true } +gridcraft-mcp = { workspace = true } +# The control channel's token (OS random numbers); already in the tree. +getrandom = "0.4" [target.'cfg(any(target_os = "linux", target_os = "freebsd"))'.dependencies] arboard = { workspace = true, features = ["wayland-data-control"] } diff --git a/apps/gridcraft/src/control_server.rs b/apps/gridcraft/src/control_server.rs index e9e05a2e63..ab2046e3ce 100644 --- a/apps/gridcraft/src/control_server.rs +++ b/apps/gridcraft/src/control_server.rs @@ -1,34 +1,82 @@ //! Localhost JSON-lines control server. Each connection may send many requests; each line //! `{"id":…, "method":…, "params":…}` gets one reply line `{"id":…, "ok":…, …}`. +//! +//! The first line of a connection must authenticate with the app's token (#777; see +//! `gridcraft_mcp::control_auth`). A connection that doesn't, or later sends a line that isn't +//! JSON, is answered once and closed. -use std::io::{BufRead, BufReader, Write}; +use std::io::{BufRead, BufReader, Read, Write}; use std::net::{TcpListener, TcpStream}; +use std::sync::Arc; use std::sync::mpsc::{Receiver, Sender, channel}; use std::time::Duration; +use gridcraft_mcp::control_auth; use gridcraft_ui_egui::ControlRequest; use serde_json::{Value as Json, json}; const MAX_LINE: usize = 64 * 1024 * 1024; +/// How long a new connection may take to send its token. +const AUTH_TIMEOUT: Duration = Duration::from_secs(10); -pub fn start(port: u16, ctx: egui::Context) -> std::io::Result> { +/// The secret of this run: the one the user chose (`--control-token`, `GRIDCRAFT_CONTROL_TOKEN`), +/// else 256 random bits from the operating system, written to a file only the user can read. +fn make_token(port: u16, chosen: Option) -> std::io::Result { + if let Some(t) = chosen.map(|t| t.trim().to_string()).filter(|t| !t.is_empty()) { + if t.len() < control_auth::MIN_TOKEN_LEN { + return Err(std::io::Error::other(format!("the control token must have at least {} characters", control_auth::MIN_TOKEN_LEN))); + } + eprintln!("gridcraft: control channel on 127.0.0.1:{port} requires the token you set (clients: GRIDCRAFT_CONTROL_TOKEN)"); + return Ok(t); + } + let mut bytes = [0u8; 32]; + getrandom::fill(&mut bytes).map_err(|e| std::io::Error::other(format!("no random numbers for the control token: {e}")))?; + let token = control_auth::hex(&bytes); + let path = control_auth::write_token_file(port, &token)?; + eprintln!("gridcraft: control channel on 127.0.0.1:{port}; its token is in {} (gridcraft-cli reads it)", path.display()); + Ok(token) +} + +pub fn start(port: u16, chosen_token: Option, ctx: egui::Context) -> std::io::Result> { let listener = TcpListener::bind(("127.0.0.1", port))?; + let token: Arc = make_token(port, chosen_token)?.into(); let (tx, rx) = channel(); std::thread::Builder::new().name("control".into()).spawn(move || { for stream in listener.incoming().flatten() { let tx = tx.clone(); let ctx = ctx.clone(); - let _ = std::thread::Builder::new().name("control-conn".into()).spawn(move || serve(stream, tx, ctx)); + let token = Arc::clone(&token); + let _ = std::thread::Builder::new().name("control-conn".into()).spawn(move || serve(stream, &token, tx, ctx)); } })?; log::info!("control channel listening on 127.0.0.1:{port}"); Ok(rx) } -fn serve(stream: TcpStream, tx: Sender, ctx: egui::Context) { +/// Read the connection's first line (at most [`control_auth::MAX_AUTH_LINE`] bytes, within +/// [`AUTH_TIMEOUT`]) and check the token. `true` when the peer may go on. +fn authenticate(reader: &mut BufReader, out: &mut TcpStream, token: &str) -> bool { + let _ = out.set_read_timeout(Some(AUTH_TIMEOUT)); + let mut first = String::new(); + let read = reader.by_ref().take(control_auth::MAX_AUTH_LINE as u64).read_line(&mut first); + let verdict = match read { + Ok(n) if n > 0 && first.ends_with('\n') => control_auth::check_first_line(&first, token), + _ => Err(json!({"ok": false, "error": "authentication required"})), + }; + let allowed = verdict.is_ok(); + let reply = verdict.unwrap_or_else(|e| e); + let _ = writeln!(out, "{reply}"); + let _ = out.set_read_timeout(None); + allowed +} + +fn serve(stream: TcpStream, token: &str, tx: Sender, ctx: egui::Context) { let Ok(read) = stream.try_clone() else { return }; let mut out = stream; - let reader = BufReader::new(read); + let mut reader = BufReader::new(read); + if !authenticate(&mut reader, &mut out, token) { + return; + } for line in reader.lines() { let Ok(line) = line else { break }; if line.trim().is_empty() { @@ -42,7 +90,7 @@ fn serve(stream: TcpStream, tx: Sender, ctx: egui::Context) { Ok(v) => v, Err(e) => { let _ = writeln!(out, "{}", json!({"ok": false, "error": format!("invalid JSON: {e}")})); - continue; + break; // Not a client of ours; don't keep reading. } }; let id = msg.get("id").cloned().unwrap_or(Json::Null); @@ -65,3 +113,82 @@ fn serve(stream: TcpStream, tx: Sender, ctx: egui::Context) { } } } + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used)] + use super::*; + + const TOKEN: &str = "0123456789abcdef0123456789abcdef"; + + /// A server on a loopback port with a stand-in UI that echoes each method. + fn spawn() -> std::net::SocketAddr { + let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let addr = listener.local_addr().unwrap(); + let (tx, rx) = channel::(); + std::thread::spawn(move || { + for req in rx { + let _ = req.reply.send(json!({"ok": true, "result": req.method})); + } + }); + std::thread::spawn(move || { + for stream in listener.incoming().flatten() { + let tx = tx.clone(); + std::thread::spawn(move || serve(stream, TOKEN, tx, egui::Context::default())); + } + }); + addr + } + + fn lines(addr: std::net::SocketAddr, send: &str) -> Vec { + let mut c = TcpStream::connect(addr).unwrap(); + c.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + c.write_all(send.as_bytes()).unwrap(); + // Replies up to the server closing the connection. + BufReader::new(c).lines().map_while(Result::ok).map(|l| serde_json::from_str(&l).unwrap()).collect() + } + + #[test] + fn the_token_opens_the_connection() { + let addr = spawn(); + let auth = control_auth::auth_line(1, TOKEN); + let mut c = TcpStream::connect(addr).unwrap(); + c.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + c.write_all(format!("{auth}\n{{\"id\":2,\"method\":\"ping\"}}\n").as_bytes()).unwrap(); + let mut r = BufReader::new(c); + let mut line = String::new(); + r.read_line(&mut line).unwrap(); + assert_eq!(serde_json::from_str::(&line).unwrap()["ok"], json!(true)); + line.clear(); + r.read_line(&mut line).unwrap(); + let v: Json = serde_json::from_str(&line).unwrap(); + assert_eq!((v["id"].clone(), v["result"].clone()), (json!(2), json!("ping"))); + } + + #[test] + fn anything_but_the_token_first_closes_the_connection() { + let addr = spawn(); + let wrong = control_auth::auth_line(1, "ffffffffffffffffffffffffffffffff"); + for first in [wrong.as_str(), "{\"id\":1,\"method\":\"app.save\",\"params\":{\"path\":\"/tmp/x\"}}", "POST / HTTP/1.1", ""] { + // The second request is never served: one refusal, then end of stream. + let got = lines(addr, &format!("{first}\n{{\"id\":2,\"method\":\"ping\"}}\n")); + assert_eq!(got.len(), 1, "{first}: {got:?}"); + assert_eq!(got[0]["ok"], json!(false), "{first}"); + } + } + + #[test] + fn an_unparsable_line_after_auth_closes_the_connection() { + let addr = spawn(); + let auth = control_auth::auth_line(1, TOKEN); + let got = lines(addr, &format!("{auth}\nnot json\n{{\"id\":3,\"method\":\"ping\"}}\n")); + assert_eq!(got.len(), 2, "{got:?}"); + assert_eq!(got[1]["ok"], json!(false)); + } + + #[test] + fn a_chosen_token_must_be_long_enough() { + assert!(make_token(1, Some("short".into())).is_err()); + assert_eq!(make_token(1, Some(format!(" {TOKEN} "))).unwrap(), TOKEN); + } +} diff --git a/apps/gridcraft/src/main.rs b/apps/gridcraft/src/main.rs index 8093205c4f..18cff9844a 100644 --- a/apps/gridcraft/src/main.rs +++ b/apps/gridcraft/src/main.rs @@ -1,9 +1,10 @@ //! GridCraft desktop app. //! -//! Usage: `gridcraft [--control ] [--sample ] [files…]` +//! Usage: `gridcraft [--control [--control-token ]] [--sample ] [files…]` //! //! `--control ` (or `GRIDCRAFT_CONTROL_PORT`) starts a localhost JSON-lines control //! server: `{"id":1,"method":"ui.inspect","params":{}}` → `{"id":1,"ok":true,"result":…}`. +//! The first line of each connection must carry the token (`docs/control-protocol.md`). //! See `gridcraft_ui_egui::control` and `docs/control-protocol.md` for the methods. #![cfg_attr(all(target_os = "windows", not(debug_assertions)), windows_subsystem = "windows")] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -242,6 +243,7 @@ fn main() -> eframe::Result<()> { return Ok(()); } let mut control_port: Option = std::env::var("GRIDCRAFT_CONTROL_PORT").ok().and_then(|p| p.parse().ok()); + let mut control_token: Option = std::env::var("GRIDCRAFT_CONTROL_TOKEN").ok(); let mut sample: Option = None; let mut files: Vec = Vec::new(); let mut i = 0; @@ -251,6 +253,10 @@ fn main() -> eframe::Result<()> { i += 1; control_port = args.get(i).and_then(|p| p.parse().ok()); } + "--control-token" => { + i += 1; + control_token = args.get(i).cloned(); + } "--sample" => { i += 1; sample = Some(args.get(i).cloned().unwrap_or_else(|| "sales".into())); @@ -338,7 +344,7 @@ fn main() -> eframe::Result<()> { }; SheetApp::setup_context_for_language(&cc.egui_ctx, app.ui.dark, app.ui.language); if let Some(port) = control_port { - match control_server::start(port, cc.egui_ctx.clone()) { + match control_server::start(port, control_token.clone(), cc.egui_ctx.clone()) { Ok(rx) => app.control_rx = Some(rx), Err(e) => log::error!("control channel on port {port}: {e}"), } diff --git a/crates/mcp/src/backend.rs b/crates/mcp/src/backend.rs index adc866d2f8..c77b612f70 100644 --- a/crates/mcp/src/backend.rs +++ b/crates/mcp/src/backend.rs @@ -25,14 +25,22 @@ const MAX_REPLY: u64 = 256 * 1024 * 1024; /// A running GridCraft app, reached through its loopback control port. pub struct Remote { addr: String, + token: String, conn: Option<(BufReader, TcpStream)>, next_id: u64, } impl Remote { - /// Connect to `addr` (`127.0.0.1:7979`), failing fast when nothing is listening. + /// Connect to `addr` (`127.0.0.1:7979`), failing fast when nothing is listening. The app + /// requires a token (#777): `GRIDCRAFT_CONTROL_TOKEN`, else the file the app wrote for this port. pub fn connect(addr: &str) -> std::io::Result { - let mut r = Self { addr: addr.to_string(), conn: None, next_id: 1 }; + let token = crate::control_auth::client_token(addr).map_err(|e| std::io::Error::new(std::io::ErrorKind::PermissionDenied, e))?; + Self::connect_with_token(addr, token) + } + + /// Like [`Remote::connect`] with a token the caller already has. + pub fn connect_with_token(addr: &str, token: String) -> std::io::Result { + let mut r = Self { addr: addr.to_string(), token, conn: None, next_id: 1 }; r.reconnect()?; Ok(r) } @@ -51,7 +59,9 @@ impl Remote { s.set_read_timeout(Some(IO_TIMEOUT)).ok(); s.set_write_timeout(Some(IO_TIMEOUT)).ok(); let read = s.try_clone()?; - self.conn = Some((BufReader::new(read), s)); + let mut conn = (BufReader::new(read), s); + Self::authenticate(&mut conn, &self.token)?; + self.conn = Some(conn); return Ok(()); } Err(e) => last = e, @@ -60,6 +70,25 @@ impl Remote { Err(last) } + /// The handshake every connection starts with: the app closes one that doesn't send the token first. + fn authenticate(conn: &mut (BufReader, TcpStream), token: &str) -> std::io::Result<()> { + let (reader, writer) = conn; + writer.write_all(crate::control_auth::auth_line(0, token).as_bytes())?; + writer.write_all(b"\n")?; + writer.flush()?; + let mut reply = String::new(); + let n = std::io::Read::take(reader, 64 * 1024).read_line(&mut reply)?; + let ok = n > 0 && serde_json::from_str::(reply.trim()).ok().and_then(|v| v.get("ok").and_then(Value::as_bool)) == Some(true); + if ok { + Ok(()) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::PermissionDenied, + "the app refused the control token (is GRIDCRAFT_CONTROL_TOKEN or the app's token file for this port out of date?)", + )) + } + } + fn roundtrip(&mut self, line: &str) -> std::io::Result { if self.conn.is_none() { self.reconnect()?; diff --git a/crates/mcp/src/control_auth.rs b/crates/mcp/src/control_auth.rs new file mode 100644 index 0000000000..7ff589353d --- /dev/null +++ b/crates/mcp/src/control_auth.rs @@ -0,0 +1,135 @@ +//! Authentication of the control channel (#777). +//! +//! Anything on the machine, and a web page that POSTs `text/plain` to `127.0.0.1:`, can +//! reach a loopback port. So the first line of every connection must be +//! `{"id":…,"method":"auth","params":{"token":"…"}}`, with the secret the app made at start. The +//! app keeps it in memory and writes it to a file only the user can read (or the user chooses it +//! with `GRIDCRAFT_CONTROL_TOKEN`); [`Remote`](crate::Remote) reads it from the same places. + +use std::path::PathBuf; + +use serde_json::{Value, json}; + +/// Environment variable holding a token chosen by the user. The app uses it instead of making +/// one (and writes no file); clients use it instead of reading the file. +pub const TOKEN_ENV: &str = "GRIDCRAFT_CONTROL_TOKEN"; +/// The method of the handshake line. +pub const AUTH_METHOD: &str = "auth"; +/// Shortest token the app accepts from the user (a made one has 256 bits). +pub const MIN_TOKEN_LEN: usize = 16; +/// Longest first line the app reads from a peer that hasn't authenticated yet. +pub const MAX_AUTH_LINE: usize = 4096; + +/// Compare two secrets without stopping at the first difference. Unequal lengths are unequal +/// at once (the length of a token isn't secret). +pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { + if a.len() != b.len() { + return false; + } + let mut diff = 0u8; + for (x, y) in a.iter().zip(b) { + diff |= x ^ y; + } + // Keep the optimiser from turning the loop into an early exit. + std::hint::black_box(diff) == 0 +} + +/// A made token as lowercase hex (two characters per byte). +pub fn hex(bytes: &[u8]) -> String { + const DIGITS: &[u8; 16] = b"0123456789abcdef"; + let mut s = String::with_capacity(bytes.len().saturating_mul(2)); + for b in bytes { + s.push(char::from(DIGITS[usize::from(b >> 4)])); + s.push(char::from(DIGITS[usize::from(b & 15)])); + } + s +} + +/// The handshake line a client sends first. +pub fn auth_line(id: u64, token: &str) -> String { + json!({"id": id, "method": AUTH_METHOD, "params": {"token": token}}).to_string() +} + +/// Check a connection's first line. `Ok` is the reply to send before serving further requests; +/// `Err` is the reply for a refused connection, which the caller sends and then closes. The +/// refusal is the same whatever was wrong, so it tells a stranger nothing. +pub fn check_first_line(line: &str, token: &str) -> Result { + let refused = || json!({"ok": false, "error": "authentication required"}); + let msg: Value = serde_json::from_str(line.trim()).map_err(|_| refused())?; + if msg.get("method").and_then(Value::as_str) != Some(AUTH_METHOD) { + return Err(refused()); + } + let given = msg.get("params").and_then(|p| p.get("token")).and_then(Value::as_str).unwrap_or(""); + // Always compare, so a missing token costs the same as a wrong one. + if constant_time_eq(given.as_bytes(), token.as_bytes()) && !token.is_empty() { + Ok(json!({"id": msg.get("id").cloned().unwrap_or(Value::Null), "ok": true, "result": {"authenticated": true}})) + } else { + Err(refused()) + } +} + +/// Where the app writes the token it made for `port`, and where clients look for it: a folder +/// only the user can open (`$XDG_RUNTIME_DIR/gridcraft`, else `~/.cache/gridcraft`; on Windows +/// `%USERPROFILE%\.gridcraft`), file `control-.token`. `None` when no such folder is known. +pub fn token_path(port: u16) -> Option { + let dir = if cfg!(windows) { + PathBuf::from(std::env::var_os("USERPROFILE").filter(|v| !v.is_empty())?).join(".gridcraft") + } else if let Some(run) = std::env::var_os("XDG_RUNTIME_DIR").filter(|v| !v.is_empty()) { + PathBuf::from(run).join("gridcraft") + } else { + PathBuf::from(std::env::var_os("HOME").filter(|v| !v.is_empty())?).join(".cache").join("gridcraft") + }; + Some(dir.join(format!("control-{port}.token"))) +} + +/// The port of a control address (`127.0.0.1:7979` → 7979). +fn port_of(addr: &str) -> Option { + addr.rsplit(':').next()?.parse().ok() +} + +/// The token a client uses for `addr`: [`TOKEN_ENV`] if set, else the file the app wrote. +pub fn client_token(addr: &str) -> Result { + if let Ok(t) = std::env::var(TOKEN_ENV) + && !t.trim().is_empty() + { + return Ok(t.trim().to_string()); + } + let path = port_of(addr).and_then(token_path).ok_or_else(|| format!("no control token: set {TOKEN_ENV}"))?; + match std::fs::read_to_string(&path) { + Ok(t) if !t.trim().is_empty() => Ok(t.trim().to_string()), + _ => Err(format!("no control token: set {TOKEN_ENV}, or start the app on this machine and user (it writes {})", path.display())), + } +} + +/// Write `token` to [`token_path`] for the app: the folder is made private (mode 0700 on Unix), +/// an older file is replaced, and the new one is created readable by the user only (mode 0600). +/// Returns the path. +pub fn write_token_file(port: u16, token: &str) -> std::io::Result { + use std::io::Write; + let path = token_path(port).ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, "no user folder for the control token"))?; + let dir = path.parent().ok_or_else(|| std::io::Error::other("token path has no folder"))?; + std::fs::create_dir_all(dir)?; + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + // Only our own folder: leave a shared one (`$XDG_RUNTIME_DIR` itself) as it is. + std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700))?; + } + // Replace rather than open: a leftover file may have looser permissions, or be a link. + match std::fs::remove_file(&path) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e), + } + let mut opts = std::fs::OpenOptions::new(); + opts.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + opts.mode(0o600); + } + let mut f = opts.open(&path)?; + f.write_all(token.as_bytes())?; + f.write_all(b"\n")?; + Ok(path) +} diff --git a/crates/mcp/src/lib.rs b/crates/mcp/src/lib.rs index de3e25044c..7c66a26f93 100644 --- a/crates/mcp/src/lib.rs +++ b/crates/mcp/src/lib.rs @@ -15,6 +15,7 @@ #![forbid(unsafe_code)] mod backend; +pub mod control_auth; mod headless; mod server; mod tools; diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rs index eb1f20503b..df6c954e7a 100644 --- a/crates/mcp/src/tests.rs +++ b/crates/mcp/src/tests.rs @@ -363,6 +363,11 @@ fn remote_backend_over_tcp() { let stream = stream.unwrap(); let mut w = stream.try_clone().unwrap(); let mut r = BufReader::new(stream); + // Every connection starts with the token, as the app requires. + let mut first = String::new(); + r.read_line(&mut first).unwrap(); + let hello = crate::control_auth::check_first_line(&first, "secret-token-0123456789").unwrap(); + w.write_all(format!("{hello}\n").as_bytes()).unwrap(); loop { let mut line = String::new(); if r.read_line(&mut line).unwrap_or(0) == 0 { @@ -382,7 +387,7 @@ fn remote_backend_over_tcp() { }); let addr = control_addr(&port.to_string()); assert_eq!(addr, format!("127.0.0.1:{port}")); - let mut s = Server::new(Box::new(Remote::connect(&addr).unwrap())); + let mut s = Server::new(Box::new(Remote::connect_with_token(&addr, "secret-token-0123456789".into()).unwrap())); assert_eq!(ok(&mut s, "evaluate_formula", json!({"formula": "=1+1"}))["result"], json!(2.0)); // Second call reconnects transparently. ok(&mut s, "set_cell", json!({"cell": "A1", "input": "7"})); @@ -391,7 +396,49 @@ fn remote_backend_over_tcp() { assert!(msg.contains("unknown command"), "{msg}"); drop(s); handle.join().unwrap(); - assert!(Remote::connect("127.0.0.1:1").is_err()); + assert!(Remote::connect_with_token("127.0.0.1:1", "x".into()).is_err()); +} + +#[test] +fn control_auth_handshake_and_compare() { + use crate::control_auth::{auth_line, check_first_line, constant_time_eq, hex}; + let token = "0123456789abcdef0123456789abcdef"; + let ok = check_first_line(&auth_line(7, token), token).unwrap(); + assert_eq!((ok["id"].clone(), ok["ok"].clone()), (json!(7), json!(true))); + // Wrong or missing token, another method, and non-JSON are all refused the same way. + let wrong = check_first_line(&auth_line(1, "0123456789abcdef0123456789abcdee"), token).unwrap_err(); + for bad in [ + auth_line(1, ""), + r#"{"id":1,"method":"auth","params":{}}"#.into(), + r#"{"id":1,"method":"app.save","params":{"path":"/tmp/x","token":"0123456789abcdef0123456789abcdef"}}"#.into(), + "POST / HTTP/1.1".into(), + String::new(), + ] { + assert_eq!(check_first_line(&bad, token).unwrap_err(), wrong, "{bad}"); + } + // An empty configured token never matches an empty given one. + assert!(check_first_line(&auth_line(1, ""), "").is_err()); + assert!(constant_time_eq(b"abc", b"abc")); + assert!(!constant_time_eq(b"abc", b"abd")); + assert!(!constant_time_eq(b"abc", b"abcd")); + assert_eq!(hex(&[0x00, 0xab, 0xff]), "00abff"); +} + +#[cfg(unix)] +#[test] +fn control_token_file_is_private() { + use std::os::unix::fs::PermissionsExt; + // Unique port per run so parallel tests don't share a file; the folder is the real one. + let port = 40_000 + (std::process::id() % 20_000) as u16; + let path = crate::control_auth::write_token_file(port, "tok-0123456789abcdef").unwrap(); + assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); + assert_eq!(std::fs::metadata(path.parent().unwrap()).unwrap().permissions().mode() & 0o777, 0o700); + // A looser leftover is replaced, not reused. + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o666)).unwrap(); + crate::control_auth::write_token_file(port, "tok-1123456789abcdef").unwrap(); + assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600); + assert_eq!(std::fs::read_to_string(&path).unwrap().trim(), "tok-1123456789abcdef"); + std::fs::remove_file(&path).unwrap(); } #[test] diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 7cc6ec2305..39a3e5d674 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -6,7 +6,31 @@ Start the desktop app with a control port: gridcraft --control 7979 # or GRIDCRAFT_CONTROL_PORT=7979 ``` -Then send newline-delimited JSON to `127.0.0.1:7979`. Every request line gets one reply line: +## Authentication + +The control port is reachable by every program on the machine, and by a web page that POSTs +`text/plain` to `127.0.0.1:7979`, so every connection must prove it knows the app's token +(#777). **There is no way to turn this off.** + +- The app makes a token at start (256 random bits from the operating system, kept in memory) and + writes it to a file only you can read: `$XDG_RUNTIME_DIR/gridcraft/control-.token` (else + `~/.cache/gridcraft/…`; Windows: `%USERPROFILE%\.gridcraft\…`; the folder is mode 0700 and the + file 0600 on Unix). The path is printed on stderr. The token itself is never printed or logged. +- Or choose the token yourself: `GRIDCRAFT_CONTROL_TOKEN=…` (preferred) or `--control-token …` + (visible in the process list), at least 16 characters. The app then writes no file. +- The first line of a connection must be + `{"id":0,"method":"auth","params":{"token":"…"}}`; the reply is + `{"id":0,"ok":true,"result":{"authenticated":true}}`. Any other first line (a wrong or missing + token, another method, text that isn't JSON such as an HTTP request, no line within 10 s, or + one longer than 4 KiB) gets `{"ok":false,"error":"authentication required"}` and the + connection is closed. After that, a line that isn't JSON also closes the connection. +- `gridcraft-cli send`/`mcp --connect` and `gridcraft_mcp::Remote` do this themselves: they use + `GRIDCRAFT_CONTROL_TOKEN` if set, else the token file of that port (so they work for the same + user on the same machine without setup). Your own client reads the file or the variable. + +## Requests + +Then send newline-delimited JSON to `127.0.0.1:7979` (after the `auth` line). Every request line gets one reply line: ```json {"id": 1, "method": "engine.execute", "params": {"command": "cell.set", "params": {"cell": "B2", "input": "=SUM(A1:A9)"}}} diff --git a/docs/mcp.md b/docs/mcp.md index 3e6e7bc11e..811fc95006 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9,7 +9,7 @@ Two backends: | Mode | Command | What it drives | |---|---|---| | Headless (default) | `gridcraft-cli mcp [--in book.xlsx \| --sample budget]` | An in-process engine session — no window. | -| Remote | `gridcraft-cli mcp --connect 7979` | A running app started with `gridcraft --control 7979` (TCP JSON lines on `127.0.0.1`). | +| Remote | `gridcraft-cli mcp --connect 7979` | A running app started with `gridcraft --control 7979` (TCP JSON lines on `127.0.0.1`, authenticated with the app's token: it is read from the app's token file or `GRIDCRAFT_CONTROL_TOKEN`, see [`control-protocol.md`](control-protocol.md)). | ## Claude Code From 74de1b32ebc2097fb5545f3851c3db0ae6b136c7 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Wed, 14 Oct 2026 18:14:07 -0500 Subject: [PATCH 002/687] Keytips: Alt, H, B border letters apply their own preset Every Alt, H, B letter ran home.borders with no params, which defaults to a bottom border, so H,B,N (No Border) and H,B,A (All Borders) both drew a bottom border. keytips.json entries now take optional fixed `params` (Action::CommandWith / DialogWith), and each Borders menu letter passes the preset Excel's menu gives it. H,B,M opens Format Cells on the Border tab. Erase Border, Line Color and Line Style are reserved (pending) instead of drawing a bottom border. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 3 +- crates/ui-egui/src/keytips.rs | 5 + crates/ui-egui/src/keytips/keytip_table.rs | 4 +- .../src/keytips/keytip_table/generated.rs | 64 ++++++------- crates/ui-egui/src/keytips/keytips.json | 90 +++++++++++++----- crates/ui-egui/src/lib.rs | 12 +++ crates/ui-egui/tests/keytips.rs | 93 +++++++++++++++++++ crates/ui-egui/tests/keytips_generated.rs | 17 +++- docs/ui-parity.md | 5 +- xtask/src/keytips.rs | 39 ++++++-- 10 files changed, 260 insertions(+), 72 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..708f803833 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (Alt, H, B border keytips apply their own preset) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-14 | Border keytips: each Alt, H, B letter applies the preset Excel's Borders menu gives it (H,B,O bottom, H,B,B bottom double, H,B,N no border, H,B,A all borders and the rest); they all drew a bottom border before. H,B,M opens Format Cells on the Border tab | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | diff --git a/crates/ui-egui/src/keytips.rs b/crates/ui-egui/src/keytips.rs index e1163d9bbf..07324dac7b 100644 --- a/crates/ui-egui/src/keytips.rs +++ b/crates/ui-egui/src/keytips.rs @@ -10,7 +10,12 @@ pub mod keytip_table; pub enum Action { Tab(&'static str), Command(&'static str), + /// A command run with fixed params (a JSON object literal from keytips.json `params`), for + /// menu items that share one command id, like the Borders presets (HBO bottom, HBN none). + CommandWith(&'static str, &'static str), Dialog(&'static str), + /// A dialog opened with fixed params, like Format Cells on its Border tab (HBM). + DialogWith(&'static str, &'static str), /// The target command does not exist yet: the sequence is reserved, its badge shows, and /// invoking it is a silent no-op until the command lands (see keytips.json `pending`). Pending(&'static str), diff --git a/crates/ui-egui/src/keytips/keytip_table.rs b/crates/ui-egui/src/keytips/keytip_table.rs index 5415b9d2b1..d14af141bb 100644 --- a/crates/ui-egui/src/keytips/keytip_table.rs +++ b/crates/ui-egui/src/keytips/keytip_table.rs @@ -50,8 +50,8 @@ pub fn is_group_level(seq: &str) -> bool { fn matches_target(action: &Action, target: &str) -> bool { match action { - Action::Command(id) | Action::Pending(id) | Action::Control(id) => *id == target, - Action::Dialog(name) => format!("dialog:{name}") == target, + Action::Command(id) | Action::CommandWith(id, _) | Action::Pending(id) | Action::Control(id) => *id == target, + Action::Dialog(name) | Action::DialogWith(name, _) => format!("dialog:{name}") == target, Action::Tab(_) => false, } } diff --git a/crates/ui-egui/src/keytips/keytip_table/generated.rs b/crates/ui-egui/src/keytips/keytip_table/generated.rs index 79920b5b8c..5f750e770e 100644 --- a/crates/ui-egui/src/keytips/keytip_table/generated.rs +++ b/crates/ui-egui/src/keytips/keytip_table/generated.rs @@ -37,40 +37,40 @@ pub const SEQUENCES: &[(&str, Entry)] = &[ ("HAN", Entry::Finish(Action::Command("home.accounting"))), // Excel: Accounting Format; fires immediately in Excel; deeper aliases dropped as unreachable ("HAS", Entry::Finish(Action::Command("home.addIns"))), // Excel: Add-ins; containment(Add-ins) ("HB", Entry::Continue), // Excel: Borders Menu; group level: - ("HBA", Entry::Finish(Action::Command("home.borders"))), // Excel: All Borders; containment(Borders) - ("HBB", Entry::Finish(Action::Command("home.borders"))), // Excel: Bottom Double Border; border-side TBD - ("HBC", Entry::Finish(Action::Command("home.borders"))), // Excel: Top and Thick Bottom Border; border-side TBD - ("HBD", Entry::Finish(Action::Command("home.borders"))), // Excel: Top and Bottom Border; border-side TBD - ("HBE", Entry::Finish(Action::Command("home.borders"))), // Excel: Erase Border; border-side TBD - ("HBG", Entry::Finish(Action::Command("home.drawBorder"))), // Excel: Draw Border Grid; containment(Draw Border) - ("HBH", Entry::Finish(Action::Command("home.borders"))), // Excel: Thick Bottom Border; border-side TBD - ("HBI", Entry::Finish(Action::Command("home.borders"))), // Excel: Line Color; Line Color (submenu TBD) - ("HBL", Entry::Finish(Action::Command("home.borders"))), // Excel: Left Border; border-side TBD - ("HBM", Entry::Finish(Action::Command("home.borders"))), // Excel: More Borders; containment(Borders) - ("HBN", Entry::Finish(Action::Command("home.borders"))), // Excel: No Border; border-side TBD - ("HBO", Entry::Finish(Action::Command("home.borders"))), // Excel: Bottom Border; border-side TBD - ("HBP", Entry::Finish(Action::Command("home.borders"))), // Excel: Top Border; border-side TBD - ("HBR", Entry::Finish(Action::Command("home.borders"))), // Excel: Right Border; border-side TBD - ("HBS", Entry::Finish(Action::Command("home.borders"))), // Excel: Outside Borders; containment(Borders) - ("HBT", Entry::Finish(Action::Command("home.borders"))), // Excel: Thick Outside Borders; border-side TBD - ("HBU", Entry::Finish(Action::Command("home.borders"))), // Excel: Top and Double Bottom Border; border-side TBD - ("HBW", Entry::Finish(Action::Command("home.drawBorder"))), // Excel: Draw Border - ("HBY", Entry::Finish(Action::Command("home.borders"))), // Excel: Line Style; Line Style (submenu TBD) - ("HC", Entry::Finish(Action::Command("edit.copy"))), // Excel: Copy; fires immediately in Excel; deeper aliases dropped as unreachable - ("HDC", Entry::Finish(Action::Command("home.deleteColumns"))), // Excel: Delete Columns; Delete Columns - ("HDD", Entry::Finish(Action::Dialog("deleteCells"))), // Excel: Delete Cells - ("HDR", Entry::Finish(Action::Command("home.deleteRows"))), // Excel: Delete Rows; Delete Rows - ("HDS", Entry::Finish(Action::Command("home.deleteSheet"))), // Excel: Delete Sheet - ("HEA", Entry::Finish(Action::Command("edit.clearAll"))), // Excel: Clear All - ("HEC", Entry::Finish(Action::Command("edit.clearContents"))), // Excel: Clear Contents - ("HEF", Entry::Finish(Action::Command("edit.clearFormats"))), // Excel: Clear Formats + ("HBA", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"all\"}"))), // Excel: All Borders; Borders menu item + ("HBB", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"doubleBottom\"}"))), // Excel: Bottom Double Border; Borders menu item + ("HBC", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"topThickBottom\"}"))), // Excel: Top and Thick Bottom Border; Borders menu item + ("HBD", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"topBottom\"}"))), // Excel: Top and Bottom Border; Borders menu item + ("HBE", Entry::Finish(Action::Pending("home.eraseBorder"))), // pending; Excel: Erase Border; pending: Excel arms an eraser pen; GridCraft has no border pen mode yet + ("HBG", Entry::Finish(Action::Command("home.drawBorder"))), // Excel: Draw Border Grid; containment(Draw Border) + ("HBH", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"thickBottom\"}"))), // Excel: Thick Bottom Border; Borders menu item + ("HBI", Entry::Finish(Action::Pending("home.borderLineColor"))), // pending; Excel: Line Color; pending: Line Color submenu + ("HBL", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"left\"}"))), // Excel: Left Border; Borders menu item + ("HBM", Entry::Finish(Action::DialogWith("formatCells", "{\"tab\":\"Border\"}"))), // Excel: More Borders; Format Cells on its Border tab + ("HBN", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"none\"}"))), // Excel: No Border; Borders menu item + ("HBO", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"bottom\"}"))), // Excel: Bottom Border; Borders menu item + ("HBP", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"top\"}"))), // Excel: Top Border; Borders menu item + ("HBR", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"right\"}"))), // Excel: Right Border; Borders menu item + ("HBS", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"outside\"}"))), // Excel: Outside Borders; Borders menu item + ("HBT", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"thickOutside\"}"))), // Excel: Thick Outside Borders; Borders menu item + ("HBU", Entry::Finish(Action::CommandWith("home.borders", "{\"preset\":\"topDoubleBottom\"}"))), // Excel: Top and Double Bottom Border; Borders menu item + ("HBW", Entry::Finish(Action::Command("home.drawBorder"))), // Excel: Draw Border + ("HBY", Entry::Finish(Action::Pending("home.borderLineStyle"))), // pending; Excel: Line Style; pending: Line Style submenu + ("HC", Entry::Finish(Action::Command("edit.copy"))), // Excel: Copy; fires immediately in Excel; deeper aliases dropped as unreachable + ("HDC", Entry::Finish(Action::Command("home.deleteColumns"))), // Excel: Delete Columns; Delete Columns + ("HDD", Entry::Finish(Action::Dialog("deleteCells"))), // Excel: Delete Cells + ("HDR", Entry::Finish(Action::Command("home.deleteRows"))), // Excel: Delete Rows; Delete Rows + ("HDS", Entry::Finish(Action::Command("home.deleteSheet"))), // Excel: Delete Sheet + ("HEA", Entry::Finish(Action::Command("edit.clearAll"))), // Excel: Clear All + ("HEC", Entry::Finish(Action::Command("edit.clearContents"))), // Excel: Clear Contents + ("HEF", Entry::Finish(Action::Command("edit.clearFormats"))), // Excel: Clear Formats ("HEL", Entry::Finish(Action::Command("edit.clearHyperlinks"))), // Excel: Clear Hyperlinks - ("HEM", Entry::Finish(Action::Command("edit.clearComments"))), // Excel: Clear Comments and Notes; containment(Clear Comments and Notes) - ("HFC", Entry::Finish(Action::Control("home.fontColor"))), // Excel: Font Color; opens the font-color palette + ("HEM", Entry::Finish(Action::Command("edit.clearComments"))), // Excel: Clear Comments and Notes; containment(Clear Comments and Notes) + ("HFC", Entry::Finish(Action::Control("home.fontColor"))), // Excel: Font Color; opens the font-color palette ("HFDC", Entry::Finish(Action::Command("home.conditionalFormat"))), // Excel: Find Conditional Formatting; containment(Conditional Formatting) - ("HFDF", Entry::Finish(Action::Dialog("find"))), // Excel: Find - ("HFDG", Entry::Finish(Action::Dialog("goTo"))), // Excel: Go To - ("HFDM", Entry::Finish(Action::Command("edit.find"))), // Excel: Find Notes; containment(Find) + ("HFDF", Entry::Finish(Action::Dialog("find"))), // Excel: Find + ("HFDG", Entry::Finish(Action::Dialog("goTo"))), // Excel: Go To + ("HFDM", Entry::Finish(Action::Command("edit.find"))), // Excel: Find Notes; containment(Find) ("HFDO", Entry::Finish(Action::Pending("home.selectObjects"))), // pending; Excel: Select Objects; Select Objects; reserved until the command lands ("HFDP", Entry::Finish(Action::Command("arrange.selectionPane"))), // Excel: Selection Pane ("HFDR", Entry::Finish(Action::Dialog("find"))), // Excel: Replace; opens Find and Replace diff --git a/crates/ui-egui/src/keytips/keytips.json b/crates/ui-egui/src/keytips/keytips.json index 59aeadcfb8..b24241c3cc 100644 --- a/crates/ui-egui/src/keytips/keytips.json +++ b/crates/ui-egui/src/keytips/keytips.json @@ -8,6 +8,7 @@ " \"continue\" (an explicit group level with no deeper sequences yet).", " - Intermediate levels need no entries: the matcher waits automatically when deeper sequences exist.", " - pending: true = the command does not exist yet; the sequence stays reserved and silent until it lands.", + " - params: an optional JSON object passed to a command or dialog, for menu items that share one id (the Borders presets: HBO {\"preset\": \"bottom\"}).", " - Keep entries grouped by tab and sorted by sequence within a group; the FIRST entry for a tab is its canonical badge (legacy aliases come after it)." ], "sequences": [ @@ -196,36 +197,49 @@ "seq": "HBA", "kind": "command", "target": "home.borders", + "params": { + "preset": "all" + }, "excel": "All Borders", - "note": "containment(Borders)" + "note": "Borders menu item" }, { "seq": "HBB", "kind": "command", "target": "home.borders", + "params": { + "preset": "doubleBottom" + }, "excel": "Bottom Double Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBC", "kind": "command", "target": "home.borders", + "params": { + "preset": "topThickBottom" + }, "excel": "Top and Thick Bottom Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBD", "kind": "command", "target": "home.borders", + "params": { + "preset": "topBottom" + }, "excel": "Top and Bottom Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBE", "kind": "command", - "target": "home.borders", + "target": "home.eraseBorder", + "pending": true, "excel": "Erase Border", - "note": "border-side TBD" + "note": "pending: Excel arms an eraser pen; GridCraft has no border pen mode yet" }, { "seq": "HBG", @@ -238,78 +252,109 @@ "seq": "HBH", "kind": "command", "target": "home.borders", + "params": { + "preset": "thickBottom" + }, "excel": "Thick Bottom Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBI", "kind": "command", - "target": "home.borders", + "target": "home.borderLineColor", + "pending": true, "excel": "Line Color", - "note": "Line Color (submenu TBD)" + "note": "pending: Line Color submenu" }, { "seq": "HBL", "kind": "command", "target": "home.borders", + "params": { + "preset": "left" + }, "excel": "Left Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBM", - "kind": "command", - "target": "home.borders", + "kind": "dialog", + "target": "formatCells", + "params": { + "tab": "Border" + }, "excel": "More Borders", - "note": "containment(Borders)" + "note": "Format Cells on its Border tab" }, { "seq": "HBN", "kind": "command", "target": "home.borders", + "params": { + "preset": "none" + }, "excel": "No Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBO", "kind": "command", "target": "home.borders", + "params": { + "preset": "bottom" + }, "excel": "Bottom Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBP", "kind": "command", "target": "home.borders", + "params": { + "preset": "top" + }, "excel": "Top Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBR", "kind": "command", "target": "home.borders", + "params": { + "preset": "right" + }, "excel": "Right Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBS", "kind": "command", "target": "home.borders", + "params": { + "preset": "outside" + }, "excel": "Outside Borders", - "note": "containment(Borders)" + "note": "Borders menu item" }, { "seq": "HBT", "kind": "command", "target": "home.borders", + "params": { + "preset": "thickOutside" + }, "excel": "Thick Outside Borders", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBU", "kind": "command", "target": "home.borders", + "params": { + "preset": "topDoubleBottom" + }, "excel": "Top and Double Bottom Border", - "note": "border-side TBD" + "note": "Borders menu item" }, { "seq": "HBW", @@ -321,9 +366,10 @@ { "seq": "HBY", "kind": "command", - "target": "home.borders", + "target": "home.borderLineStyle", + "pending": true, "excel": "Line Style", - "note": "Line Style (submenu TBD)" + "note": "pending: Line Style submenu" }, { "seq": "HC", diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f96cdac3c0..4e7de595a9 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1193,7 +1193,19 @@ impl SheetApp { keytips::Action::Command(id @ ("edit.copy" | "edit.cut")) => self.copy_to_clipboard(ctx, id), keytips::Action::Command("edit.paste") => self.paste_from_clipboard(), keytips::Action::Command(id) => self.run_or_alert(id, json!({})), + keytips::Action::CommandWith(id, params) => { + let params: Json = serde_json::from_str(params).unwrap_or_else(|_| json!({})); + // A Borders preset picked by key tip becomes the split button's face, as a + // click on the same menu item does. + if id == "home.borders" + && let Some(preset) = params.get("preset").and_then(Json::as_str) + { + self.grid.last_border = preset.to_string(); + } + self.run_or_alert(id, params); + } keytips::Action::Dialog(name) => self.open_dialog(name, json!({})), + keytips::Action::DialogWith(name, params) => self.open_dialog(name, serde_json::from_str(params).unwrap_or_else(|_| json!({}))), // Reserved sequence: fires silently while its command is missing. When the // command lands in the catalog it goes live with no table edit. keytips::Action::Pending(id) => { diff --git a/crates/ui-egui/tests/keytips.rs b/crates/ui-egui/tests/keytips.rs index 24be742a59..76ddd44672 100644 --- a/crates/ui-egui/tests/keytips.rs +++ b/crates/ui-egui/tests/keytips.rs @@ -550,3 +550,96 @@ fn every_sequence_terminates_without_panicking() { } assert_eq!(SEQUENCES.len(), SEQUENCES.len(), "every sequence ran"); } + +fn borders(h: &Harness, cell: &str) -> gridcraft_engine::model::Borders { + let d = h.state().session.active().unwrap(); + let sh = d.wb.active().unwrap(); + d.wb.styles.get(sh.style_id(CellRef::parse(cell).unwrap())).borders +} + +/// [left, right, top, bottom] line styles of `cell`. +fn sides(h: &Harness, cell: &str) -> [gridcraft_engine::model::BorderStyle; 4] { + let b = borders(h, cell); + [b.left.style, b.right.style, b.top.style, b.bottom.style] +} + +fn selected(range: &str) -> Session { + let mut s = blank(); + s.execute("selection.set", json!({"range": range})).unwrap(); + s +} + +#[test] +fn border_keytips_apply_their_own_preset() { + use gridcraft_engine::model::BorderStyle::{Double, None as N, Thick, Thin}; + // Alt, H, B, letter on A1 alone: each Borders menu letter draws its own preset (they all drew + // a bare bottom border before). Letters observed in Excel's Borders menu. + for (k, want) in [ + (Key::O, [N, N, N, Thin]), + (Key::P, [N, N, Thin, N]), + (Key::L, [Thin, N, N, N]), + (Key::R, [N, Thin, N, N]), + (Key::A, [Thin, Thin, Thin, Thin]), + (Key::S, [Thin, Thin, Thin, Thin]), + (Key::T, [Thick, Thick, Thick, Thick]), + (Key::B, [N, N, N, Double]), + (Key::H, [N, N, N, Thick]), + (Key::D, [N, N, Thin, Thin]), + (Key::C, [N, N, Thin, Thick]), + (Key::U, [N, N, Thin, Double]), + ] { + let mut h = harness(selected("A1")); + sequence(&mut h, &[Key::H, Key::B, k], false); + assert!(h.state().message.is_none() && h.state().toast.is_none(), "H,B,{k:?} must run cleanly"); + assert_eq!(sides(&h, "A1"), want, "H,B,{k:?}"); + } +} + +#[test] +fn border_keytip_no_border_clears_a_bordered_cell() { + use gridcraft_engine::model::BorderStyle::None as N; + let mut s = selected("A1"); + s.execute("home.borders", json!({"range": "A1", "preset": "all"})).unwrap(); + let mut h = harness(s); + sequence(&mut h, &[Key::H, Key::B, Key::N], false); + assert_eq!(sides(&h, "A1"), [N, N, N, N], "H,B,N must clear every side, not add a bottom border"); + assert_eq!(h.state().grid.last_border, "none", "the picked preset becomes the Borders button's face"); +} + +#[test] +fn border_keytip_all_borders_covers_every_side_of_a_block() { + use gridcraft_engine::model::BorderStyle::Thin; + let mut h = harness(selected("A1:B2")); + sequence(&mut h, &[Key::H, Key::B, Key::A], false); + for cell in ["A1", "B1", "A2", "B2"] { + assert_eq!(sides(&h, cell), [Thin; 4], "H,B,A must border every side of {cell}"); + } +} + +#[test] +fn border_keytip_outside_borders_skips_the_inside_edges() { + use gridcraft_engine::model::BorderStyle::{None as N, Thin}; + let mut h = harness(selected("A1:B2")); + sequence(&mut h, &[Key::H, Key::B, Key::S], false); + assert_eq!(sides(&h, "A1"), [Thin, N, Thin, N]); + assert_eq!(sides(&h, "B2"), [N, Thin, N, Thin]); +} + +#[test] +fn border_keytip_more_borders_opens_format_cells_on_the_border_tab() { + let mut h = harness(blank()); + sequence(&mut h, &[Key::H, Key::B, Key::M], false); + let d = h.state().dialog.as_ref().expect("H,B,M opens Format Cells"); + assert_eq!(d.tab, "Border"); +} + +#[test] +fn border_keytips_without_a_command_stay_silent() { + // Erase Border (a pen mode), Line Color and Line Style are reserved: no border is drawn. + for k in [Key::E, Key::I, Key::Y] { + let mut h = harness(selected("A1")); + sequence(&mut h, &[Key::H, Key::B, k], false); + assert!(!h.state().session.journal.iter().any(|(id, _)| id.starts_with("home.")), "H,B,{k:?} must not run a command"); + assert_eq!(borders(&h, "A1"), Default::default(), "H,B,{k:?} must not draw a border"); + } +} diff --git a/crates/ui-egui/tests/keytips_generated.rs b/crates/ui-egui/tests/keytips_generated.rs index 653554857e..dcf71cde17 100644 --- a/crates/ui-egui/tests/keytips_generated.rs +++ b/crates/ui-egui/tests/keytips_generated.rs @@ -308,7 +308,10 @@ fn keytip_hbd_() { fn keytip_hbe_() { let mut h = harness(blank()); sequence(&mut h, "HBE", false); - assert!(h.state().session.journal.iter().any(|(id, _)| id == "home.borders"), "HBE must journal home.borders"); + assert!(h.state().dialog.is_none(), "HBE must not open a dialog"); + assert!(h.state().message.is_none(), "HBE must not open a message box"); + assert!(h.state().toast.is_none(), "HBE must not raise a toast"); + assert!(h.state().session.journal.is_empty(), "HBE must not journal"); } #[test] @@ -329,7 +332,10 @@ fn keytip_hbh_() { fn keytip_hbi_() { let mut h = harness(blank()); sequence(&mut h, "HBI", false); - assert!(h.state().session.journal.iter().any(|(id, _)| id == "home.borders"), "HBI must journal home.borders"); + assert!(h.state().dialog.is_none(), "HBI must not open a dialog"); + assert!(h.state().message.is_none(), "HBI must not open a message box"); + assert!(h.state().toast.is_none(), "HBI must not raise a toast"); + assert!(h.state().session.journal.is_empty(), "HBI must not journal"); } #[test] @@ -343,7 +349,7 @@ fn keytip_hbl_() { fn keytip_hbm_() { let mut h = harness(blank()); sequence(&mut h, "HBM", false); - assert!(h.state().session.journal.iter().any(|(id, _)| id == "home.borders"), "HBM must journal home.borders"); + assert!(h.state().dialog.is_some(), "HBM must open the formatCells dialog"); } #[test] @@ -406,7 +412,10 @@ fn keytip_hbw_() { fn keytip_hby_() { let mut h = harness(blank()); sequence(&mut h, "HBY", false); - assert!(h.state().session.journal.iter().any(|(id, _)| id == "home.borders"), "HBY must journal home.borders"); + assert!(h.state().dialog.is_none(), "HBY must not open a dialog"); + assert!(h.state().message.is_none(), "HBY must not open a message box"); + assert!(h.state().toast.is_none(), "HBY must not raise a toast"); + assert!(h.state().session.journal.is_empty(), "HBY must not journal"); } #[test] diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 22e221e698..209c55d932 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (horizontal scrollbar in the sheet tab bar, #840) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-14 · **Change:** minor (Alt, H, B border keytips apply their own preset) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -77,7 +77,7 @@ this logic, with one undo step. | Ctrl+1, Ctrl+; and Ctrl+Shift+; , Ctrl+D/R, Ctrl+E, Ctrl+T/L, Ctrl+K, Ctrl+Shift+L, Ctrl+Shift+~!@#$%^&_ | yes | done | | F2, F4, F5, F7, F9 family, Shift+F2/F3/F10/F11, F12 | yes | done; F11 chart sheet, F8 extend mode, F3 Paste Name, F4 Repeat missing | | Mac-specific (Cmd variants, Control variants, Fn keys) | yes | done where a feature exists (`keymap.rs` picks the Mac variant) | -| Sequential Alt access keys (Alt, H, N, …) on every ribbon tab | yes | 364-sequence keytip table for all tabs: per-control badges, prefix waiting, dropdowns open for editing (type/arrows/Enter), pending bindings for not-yet-built commands; QAT/Backstage excluded | +| Sequential Alt access keys (Alt, H, N, …) on every ribbon tab | yes | 364-sequence keytip table for all tabs: per-control badges, prefix waiting, dropdowns open for editing (type/arrows/Enter), pending bindings for not-yet-built commands; QAT/Backstage excluded. Keytips can carry fixed params, so each Borders menu letter (Alt, H, B, O/P/L/R/N/A/S/T/B/H/D/C/U) draws its own preset and B, M opens Format Cells on the Border tab; Erase Border, Line Color and Line Style are reserved | | Requests | — | #110 (more shortcuts and hot keys), #33 (sequential Alt; initial support landed in #43) | ### Shortcut audit (#110) @@ -161,6 +161,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | Alt, H, B border keytips apply the preset each letter names in Excel's Borders menu (they all drew a bottom border, so H,B,N No Border added one): keytips.json entries take optional `params`; H,B,M opens Format Cells on the Border tab; Erase Border, Line Color and Line Style stay reserved | | 2026-10-12 | minor | The File view's recent list tells when each workbook was last changed, read off the UI thread | | 2026-10-13 | minor | Horizontal scrollbar in the sheet tab bar: drag, track click pages, frozen columns excluded from the page (#840) | | 2026-10-12 | minor | Ctrl+wheel zoom steps along a ladder of percentages about 20% apart that never skips 100%, gliding to each step with animations on, so a notch out undoes a notch in (it drifted: 100 → 122 → 101); Excel steps 15 points, see decisions.md | diff --git a/xtask/src/keytips.rs b/xtask/src/keytips.rs index 36847222ff..11e7773aab 100644 --- a/xtask/src/keytips.rs +++ b/xtask/src/keytips.rs @@ -44,25 +44,35 @@ struct Entry { kind: String, target: String, pending: bool, + /// Fixed params as a compact JSON object (`params` in keytips.json), empty for none. + params: String, excel: String, note: String, } +impl Entry { + /// The probe key: a command and the params its keytip passes. + fn probe_key(&self) -> String { + if self.params.is_empty() { self.target.clone() } else { format!("{} {}", self.target, self.params) } + } +} + /// 0 = ran and journaled, 1 = ran without a journal step, 2 = errored (params -/// missing, disabled, or environment-dependent). +/// missing, disabled, or environment-dependent). Keyed by [`Entry::probe_key`]. type Probe = std::collections::HashMap; #[cfg(feature = "engine")] -fn probe_live_commands(ids: &[String]) -> Probe { +fn probe_live_commands(calls: &[(String, String)]) -> Probe { use gridcraft_engine::Session; let mut map = Probe::new(); - for id in ids { + for (id, params) in calls { if ENV_SENSITIVE.contains(&id.as_str()) { continue; } + let params = if params.is_empty() { serde_json::json!({}) } else { serde_json::from_str(params).unwrap_or_default() }; let mut s = Session::new(); s.new_workbook(); - let status = match s.execute(id, serde_json::json!({})) { + let status = match s.execute(id, params.clone()) { Ok(_) => { if s.journal.iter().any(|(jid, _)| jid == id) { 0 @@ -72,7 +82,8 @@ fn probe_live_commands(ids: &[String]) -> Probe { } Err(_) => 2, }; - map.insert(id.clone(), status); + let key = if params.as_object().is_some_and(|o| !o.is_empty()) { format!("{id} {params}") } else { id.clone() }; + map.insert(key, status); } map } @@ -132,6 +143,13 @@ fn parse_and_probe(source: &str) -> Result<(Vec, Probe), String> { if pending && kind != "command" { return Err(format!("{at}: `pending` is only valid with kind `command`")); } + let params = match e.get("params") { + None => String::new(), + Some(v) if v.is_object() && matches!(kind.as_str(), "command" | "dialog") && !pending => { + serde_json::to_string(v).map_err(|e| format!("{at}: params: {e}"))? + } + Some(_) => return Err(format!("{at}: `params` must be a JSON object, on a non-pending command or dialog")), + }; match kind.as_str() { "tab" => { if target.is_empty() || !TABS.contains(&target.as_str()) { @@ -163,6 +181,7 @@ fn parse_and_probe(source: &str) -> Result<(Vec, Probe), String> { kind, target, pending, + params, excel: e.get("excel").and_then(|v| v.as_str()).unwrap_or("").to_string(), note: e.get("note").and_then(|v| v.as_str()).unwrap_or("").to_string(), }); @@ -170,10 +189,10 @@ fn parse_and_probe(source: &str) -> Result<(Vec, Probe), String> { // JSON order is authoritative and curated: the first entry for a tab is its canonical // badge (legacy aliases come later), and groups stay together. Do not sort. - let live_ids: Vec = entries + let live_calls: Vec<(String, String)> = entries .iter() .filter(|e| e.kind == "command" && !e.pending) - .map(|e| e.target.clone()) + .map(|e| (e.target.clone(), e.params.clone())) .collect::>() .into_iter() .collect(); @@ -187,7 +206,7 @@ fn parse_and_probe(source: &str) -> Result<(Vec, Probe), String> { e.target.clone() }) .collect(); - let mut probe = probe_live_commands(&live_ids); + let mut probe = probe_live_commands(&live_calls); for id in landed { let mut s = gridcraft_engine::Session::new(); s.new_workbook(); @@ -219,7 +238,9 @@ fn render_table(entries: &[Entry]) -> String { let value = match e.kind.as_str() { "tab" => format!("Entry::Tab({:?})", e.target), "command" if e.pending => format!("Entry::Finish(Action::Pending({:?}))", e.target), + "command" if !e.params.is_empty() => format!("Entry::Finish(Action::CommandWith({:?}, {:?}))", e.target, e.params), "command" => format!("Entry::Finish(Action::Command({:?}))", e.target), + "dialog" if !e.params.is_empty() => format!("Entry::Finish(Action::DialogWith({:?}, {:?}))", e.target, e.params), "dialog" => format!("Entry::Finish(Action::Dialog({:?}))", e.target), "control" => format!("Entry::Finish(Action::Control({:?}))", e.target), "continue" => "Entry::Continue".to_string(), @@ -269,7 +290,7 @@ fn render_tests(entries: &[Entry], probe: &Probe) -> String { "sequence(&mut h, {seq:?}, false);\n assert!(h.state().dialog.is_none(), \"{seq} must not open a dialog\");\n assert!(h.state().message.is_none(), \"{seq} must not open a message box\");\n assert!(h.state().toast.is_none(), \"{seq} must not raise a toast\");\n assert!(h.state().session.journal.is_empty(), \"{seq} must not journal\");", seq = e.seq, ), - "command" => match probe.get(&e.target).copied().unwrap_or(2) { + "command" => match probe.get(&e.probe_key()).copied().unwrap_or(2) { 0 => format!( "sequence(&mut h, {seq:?}, false);\n assert!(h.state().session.journal.iter().any(|(id, _)| id == {id:?}), \"{seq} must journal {id}\");", seq = e.seq, From ab4d58ca6ffd65a8e9e71fe0470916343ea41133 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Wed, 14 Oct 2026 18:26:06 -0500 Subject: [PATCH 003/687] Lookups compare numbers exactly, like Excel MATCH, XMATCH, VLOOKUP, HLOOKUP, XLOOKUP and LOOKUP compared numbers at 15 significant digits (through compare / compare_numbers), so a computed key such as =0.1+0.2 (0.30000000000000004) found 0.3. Excel 365 compares lookup keys exactly in exact, approximate and binary search modes, while =, SWITCH and the criteria functions (COUNTIF(S), SUMIF(S), AVERAGEIF, MAXIFS) keep the 15-digit comparison. Observed in Excel via COM with synthetic values. lookup_equal now compares numbers with ==, and the lookup searches order values with lookup_order (exact for numbers, compare otherwise). compare_numbers and the criteria path are unchanged. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 4 +- crates/calc/src/tests.rs | 143 +++++++++++++++++++++++++++++++ crates/functions/src/criteria.rs | 9 +- crates/functions/src/lookup.rs | 63 ++++++++++++-- docs/function-parity.md | 3 +- 5 files changed, 211 insertions(+), 11 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..fba2c6a353 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (lookups compare numbers exactly, like Excel) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-14 | Lookups (MATCH, XMATCH, VLOOKUP, HLOOKUP, XLOOKUP, LOOKUP) compare numbers exactly like Excel, so a computed key such as `=0.1+0.2` no longer finds 0.3; `=` and COUNTIF/SUMIF keep their 15-digit comparison | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | @@ -193,6 +194,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | Progress log: lookups compare numbers exactly | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de78..4a2eb701a9 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -310,6 +310,149 @@ fn date_text_coerces_in_the_workbook_date_system() { } } +/// Lookups compare numbers exactly while `=` and the criteria functions compare them at 15 +/// significant digits. Every expectation was observed in Excel 365 with these fixtures +/// (`=0.1+0.2` is 0.30000000000000004, `=100+1E-14` is 100.00000000000001). +#[test] +fn lookups_compare_numbers_exactly() { + let mut t = T::new(); + for (at, input) in [ + ("A1", "=0.1+0.2"), + ("A2", "0.3"), + ("A3", "=100+1E-14"), + ("A4", "100"), + ("A5", "=1E15+0.5"), + ("A6", "1E15"), + // Ascending 0.1, 0.3+e, 0.5 and the same descending. + ("B1", "0.1"), + ("B2", "=0.1+0.2"), + ("B3", "0.5"), + ("C1", "0.5"), + ("C2", "=0.1+0.2"), + ("C3", "0.1"), + // 0.3 next to 0.3+e, ascending and descending. + ("D1", "0.3"), + ("D2", "=0.1+0.2"), + ("D3", "0.7"), + ("E1", "0.7"), + ("E2", "=0.1+0.2"), + ("E3", "0.3"), + ("G1", "=0.1+0.2"), + ("H1", "x"), + ("G2", "row2"), + // The bisection probes 0.3 first and must not run on into 0.3+e. + ("J1", "0.1"), + ("J2", "0.2"), + ("J3", "0.3"), + ("J4", "=0.1+0.2"), + ("J5", "0.4"), + ("K1", "a"), + ("K2", "b"), + ("K3", "c"), + ("K4", "d"), + ("K5", "e"), + ] { + t.set(at, input); + } + let e = 0.1 + 0.2; + let n = Value::Number; + let s = |x: &str| Value::from(x); + let na = Value::Error(CellError::NA); + let cases: Vec<(&str, Value)> = vec![ + // `=` and friends stay at 15 digits. + ("=A1=A2", Value::Bool(true)), + ("=A3=A4", Value::Bool(true)), + ("=A5=A6", Value::Bool(true)), + ("=SWITCH(A1,0.3,\"hit\",\"miss\")", s("hit")), + // Exact match. + ("=MATCH(A1,A2:A2,0)", na.clone()), + ("=MATCH(A2,A1:A1,0)", na.clone()), + ("=VLOOKUP(A1,A2:A2,1,FALSE)", na.clone()), + ("=HLOOKUP(A1,A2,1,FALSE)", na.clone()), + ("=HLOOKUP(0.3,G1:H2,2,FALSE)", na.clone()), + ("=XLOOKUP(A1,A2:A2,A2:A2,\"nf\")", s("nf")), + ("=XMATCH(A1,A2:A2)", na.clone()), + ("=MATCH(0.1+0.2,{0.3},0)", na.clone()), + ("=MATCH(0.3,A1:A2,0)", n(2.0)), + ("=MATCH(A1,A1:A2,0)", n(1.0)), + ("=MATCH(0.3,INDEX(A1:A2,0),0)", n(2.0)), + ("=MATCH(100,A3:A3,0)", na.clone()), + ("=MATCH(1E15,A5:A5,0)", na.clone()), + ("=XLOOKUP(0.3,A1:A2,{1;2},\"nf\",0,-1)", n(2.0)), + ("=XMATCH(A1,A1:A2,0,-1)", n(1.0)), + // Approximate match against a single entry. + ("=MATCH(0.3,A1:A1,1)", na.clone()), + ("=MATCH(0.3,A1:A1,-1)", n(1.0)), + ("=LOOKUP(0.3,A1:A1)", na.clone()), + ("=LOOKUP(0.3,A1:A1,G2)", na.clone()), + ("=VLOOKUP(0.3,A1:A1,1,TRUE)", na.clone()), + ("=HLOOKUP(0.3,G1:H2,2,TRUE)", na.clone()), + ("=XLOOKUP(0.3,A1:A1,A1:A1,\"nf\",-1)", s("nf")), + ("=XLOOKUP(0.3,A1:A1,A1:A1,\"nf\",1)", n(e)), + ("=XMATCH(0.3,A1:A1,-1)", na.clone()), + ("=XMATCH(0.3,A1:A1,1)", n(1.0)), + ("=MATCH(100,A3:A3,1)", na.clone()), + ("=MATCH(A3,A4:A4,1)", n(1.0)), + // Approximate match in sorted lists. + ("=MATCH(0.3,B1:B3,1)", n(1.0)), + ("=MATCH(A1,B1:B3,1)", n(2.0)), + ("=MATCH(0.3,C1:C3,-1)", n(2.0)), + ("=MATCH(0.3,D1:D3,1)", n(1.0)), + ("=MATCH(A1,D1:D3,1)", n(2.0)), + ("=MATCH(0.3,E1:E3,-1)", n(3.0)), + ("=MATCH(A1,E1:E3,-1)", n(2.0)), + ("=MATCH(0.3,J1:J5,1)", n(3.0)), + ("=MATCH(A1,J1:J5,1)", n(4.0)), + ("=MATCH(0.3,J1:J5,0)", n(3.0)), + ("=MATCH(A1,J1:J5,0)", n(4.0)), + ("=VLOOKUP(0.3,J1:K5,2,TRUE)", s("c")), + ("=VLOOKUP(A1,J1:K5,2,TRUE)", s("d")), + ("=LOOKUP(0.3,J1:J5,K1:K5)", s("c")), + ("=LOOKUP(A1,J1:J5,K1:K5)", s("d")), + // XLOOKUP / XMATCH match and search modes. + ("=XLOOKUP(0.3,B1:B3,B1:B3,\"nf\",0,2)", s("nf")), + ("=XLOOKUP(0.3,B1:B3,B1:B3,\"nf\",-1,2)", n(0.1)), + ("=XLOOKUP(0.3,B1:B3,B1:B3,\"nf\",1,2)", n(e)), + ("=XMATCH(0.3,B1:B3,-1,2)", n(1.0)), + ("=XMATCH(0.3,B1:B3,1,2)", n(2.0)), + ("=XMATCH(0.3,C1:C3,1,-2)", n(2.0)), + ("=XMATCH(0.3,C1:C3,-1,-2)", n(3.0)), + ("=XMATCH(0.3,C1:C3,0,-2)", na.clone()), + ("=XMATCH(0.3,J1:J5,0,2)", n(3.0)), + ("=XMATCH(A1,J1:J5,0,2)", n(4.0)), + ("=XMATCH(0.3,J1:J5,1,2)", n(3.0)), + ("=XMATCH(0.3,J1:J5,-1)", n(3.0)), + ("=XMATCH(0.3,J1:J5,1)", n(3.0)), + ("=XMATCH(A1,J1:J5,-1)", n(4.0)), + ("=XMATCH(0.3,B1:B3,1)", n(2.0)), + ("=XMATCH(0.3,B1:B3,-1)", n(1.0)), + ("=XMATCH(0.3,B1:B3,0,-1)", na.clone()), + ("=XMATCH(0.3,D1:D3,1,-1)", n(1.0)), + // The criteria functions keep 15 digits. + ("=COUNTIF(A1:A1,0.3)", n(1.0)), + ("=COUNTIF(A1:A1,\"<0.3\")", n(0.0)), + ("=COUNTIF(A1:A1,\">0.3\")", n(0.0)), + ("=COUNTIF(A2:A2,A1)", n(1.0)), + ("=COUNTIFS(A1:A1,0.3)", n(1.0)), + ("=SUMIF(A1:A1,0.3)", n(e)), + ("=SUMIFS(A1:A1,A1:A1,0.3)", n(e)), + ("=AVERAGEIF(A1:A1,0.3)", n(e)), + ("=MAXIFS(A1:A1,A1:A1,0.3)", n(e)), + ("=COUNTIF(A3:A3,\">100\")", n(0.0)), + ("=COUNTIF(J1:J5,0.3)", n(2.0)), + ("=ROWS(FILTER(A1:A2,A1:A2=0.3))", n(2.0)), + ("=MATCH(TRUE,A1:A2=0.3,0)", n(1.0)), + ]; + let mut wrong = Vec::new(); + for (formula, expected) in cases { + let got = crate::evaluate(&t.wb, 0, c("Z1"), formula); + if got != expected { + wrong.push(format!("{formula}: got {got:?}, Excel {expected:?}")); + } + } + assert!(wrong.is_empty(), "{}", wrong.join("\n")); +} + #[test] fn operators() { let mut t = T::new(); diff --git a/crates/functions/src/criteria.rs b/crates/functions/src/criteria.rs index e326267ce6..58f0e65945 100644 --- a/crates/functions/src/criteria.rs +++ b/crates/functions/src/criteria.rs @@ -314,7 +314,9 @@ pub(crate) fn wildcard_find(pattern: &str, text: &str, start: usize) -> Option bool { match (needle, hay) { (Value::Text(p), Value::Text(t)) => { @@ -324,7 +326,7 @@ pub(crate) fn lookup_equal(needle: &Value, hay: &Value, wild: bool) -> bool { compare_text(p, t) == Ordering::Equal } } - (Value::Number(a), Value::Number(b)) => compare_numbers(*a, *b) == Ordering::Equal, + (Value::Number(a), Value::Number(b)) => a == b, (Value::Bool(a), Value::Bool(b)) => a == b, (Value::Error(a), Value::Error(b)) => a == b, (Value::Empty, Value::Empty) => true, @@ -370,7 +372,8 @@ mod tests { assert!(m("#N/A", Value::Error(CellError::NA))); assert!(Criterion::parse(&Value::Number(3.0)).matches(&Value::Number(3.0))); assert!(Criterion::parse(&Value::Number(0.3)).matches(&Value::Number(0.1 + 0.2))); - assert!(lookup_equal(&Value::Number(0.3), &Value::Number(0.1 + 0.2), false)); + assert!(!lookup_equal(&Value::Number(0.3), &Value::Number(0.1 + 0.2), false)); + assert!(lookup_equal(&Value::Number(0.0), &Value::Number(-0.0), false)); assert!(m("<>a*", Value::from("bcd"))); assert!(m(">1/1/2020", Value::Number(44000.0))); // Text patterns never match blank cells, but an empty string is text. diff --git a/crates/functions/src/lookup.rs b/crates/functions/src/lookup.rs index 56739c9333..89ea86ad74 100644 --- a/crates/functions/src/lookup.rs +++ b/crates/functions/src/lookup.rs @@ -23,6 +23,19 @@ fn needle(args: &[Arg], i: usize) -> R { } } +/// Order of two values for the approximate and binary searches: [`compare`], except that numbers +/// compare exactly. Excel's `=` treats numbers equal to 15 significant digits as equal, but its +/// lookups don't. Observed in Excel 365 with A1 holding +/// `=0.1+0.2` (0.30000000000000004): `A1=0.3` is TRUE, yet `MATCH(0.3,A1,1)` is #N/A because +/// A1 is larger, and in {0.1;0.2;0.3;A1;0.4} `MATCH(0.3,…,1)` is 3 and `MATCH(A1,…,1)` is 4. +/// The same holds for `=100+1E-14` against 100, and in every XLOOKUP / XMATCH mode. +fn lookup_order(a: &Value, b: &Value) -> Ordering { + match (a, b) { + (Value::Number(x), Value::Number(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal), + _ => compare(a, b), + } +} + fn same_type(a: &Value, b: &Value) -> bool { matches!((a, b), (Value::Number(_), Value::Number(_)) | (Value::Text(_), Value::Text(_)) | (Value::Bool(_), Value::Bool(_))) } @@ -59,10 +72,10 @@ fn bsearch_last(x: &Value, hay: &[Value], desc: bool) -> Option { continue; }; let v = hay.get(j)?; - let o = compare(v, x); + let o = lookup_order(v, x); if o == Ordering::Equal { let mut k = j; - while hay.get(k + 1).is_some_and(|w| same_type(x, w) && compare(w, x) == Ordering::Equal) { + while hay.get(k + 1).is_some_and(|w| same_type(x, w) && lookup_order(w, x) == Ordering::Equal) { k += 1; } return Some(k); @@ -88,8 +101,8 @@ fn bsearch_last(x: &Value, hay: &[Value], desc: bool) -> Option { fn binary_order(v: &Value, x: &Value, desc: bool) -> Ordering { match v { Value::Empty => Ordering::Greater, - _ if desc => compare(v, x).reverse(), - _ => compare(v, x), + _ if desc => lookup_order(v, x).reverse(), + _ => lookup_order(v, x), } } @@ -261,7 +274,7 @@ fn xsearch(x: &Value, hay: &[Value], match_mode: i64, search_mode: i64) -> R R true, Some(bv) => { - let c = compare(v, bv); + let c = lookup_order(v, bv); if match_mode == -1 { c == Ordering::Greater } else { c == Ordering::Less } } }; @@ -747,6 +760,44 @@ mod tests { assert_eq!(rows_of(&r), vec![vec![nv(3.0), nv(1.0)]]); } + /// Lookups compare numbers exactly, unlike `=` (15 significant digits). Observed in Excel 365. + #[test] + fn lookups_compare_numbers_exactly() { + let e = 0.1 + 0.2; + let one = || rf(col(&[e])); + // Exact modes miss a key one bit away. + is_err(ev("MATCH", vec![n(0.3), one(), n(0.0)]), CellError::NA); + is_err(ev("VLOOKUP", vec![n(0.3), one(), n(1.0), b(false)]), CellError::NA); + is_err(ev("XMATCH", vec![n(0.3), one()]), CellError::NA); + close(ev("MATCH", vec![n(0.3), rf(col(&[e, 0.3])), n(0.0)]), 2.0); + close(ev("MATCH", vec![n(e), rf(col(&[e, 0.3])), n(0.0)]), 1.0); + is_err(ev("MATCH", vec![n(100.0), rf(col(&[100.0 + 1e-14])), n(0.0)]), CellError::NA); + // 0 and -0 are still the same key. + close(ev("MATCH", vec![n(-0.0), rf(col(&[0.0])), n(0.0)]), 1.0); + // Approximate modes: 0.3 sorts below 0.30000000000000004. + is_err(ev("MATCH", vec![n(0.3), one(), n(1.0)]), CellError::NA); + close(ev("MATCH", vec![n(0.3), one(), n(-1.0)]), 1.0); + is_err(ev("LOOKUP", vec![n(0.3), one()]), CellError::NA); + is_err(ev("XMATCH", vec![n(0.3), one(), n(-1.0)]), CellError::NA); + close(ev("XMATCH", vec![n(0.3), one(), n(1.0)]), 1.0); + // The bisection stops on 0.3 and doesn't run on into the next entry. + let list = || rf(col(&[0.1, 0.2, 0.3, e, 0.4])); + close(ev("MATCH", vec![n(0.3), list(), n(1.0)]), 3.0); + close(ev("MATCH", vec![n(e), list(), n(1.0)]), 4.0); + close(ev("XMATCH", vec![n(0.3), list(), n(0.0), n(2.0)]), 3.0); + close(ev("XMATCH", vec![n(e), list(), n(0.0), n(2.0)]), 4.0); + let asc = || rf(col(&[0.1, e, 0.5])); + let desc = || rf(col(&[0.5, e, 0.1])); + close(ev("MATCH", vec![n(0.3), asc(), n(1.0)]), 1.0); + close(ev("MATCH", vec![n(0.3), desc(), n(-1.0)]), 2.0); + is_err(ev("XMATCH", vec![n(0.3), asc(), n(0.0), n(2.0)]), CellError::NA); + close(ev("XMATCH", vec![n(0.3), asc(), n(-1.0), n(2.0)]), 1.0); + close(ev("XMATCH", vec![n(0.3), asc(), n(1.0), n(2.0)]), 2.0); + close(ev("XMATCH", vec![n(0.3), desc(), n(-1.0), n(-2.0)]), 3.0); + close(ev("XMATCH", vec![n(0.3), asc(), n(-1.0)]), 1.0); + close(ev("XMATCH", vec![n(0.3), asc(), n(1.0)]), 2.0); + } + #[test] fn index() { let t3 = table_rows3(); diff --git a/docs/function-parity.md b/docs/function-parity.md index 760ffd6ac3..6dad808042 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (USDOLLAR added as a compatibility function; MIRR, COUNTIF and INDIRECT edge cases match Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (lookups compare numbers exactly, like Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -108,6 +108,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | MATCH, XMATCH, VLOOKUP, HLOOKUP, XLOOKUP and LOOKUP compare numbers exactly in every match and search mode, as Excel 365 does: `MATCH(0.1+0.2,{0.3},0)` is `#N/A` and 0.3 sorts below 0.30000000000000004 in approximate and binary search. `=`, SWITCH and the criteria functions (COUNTIF(S), SUMIF(S), AVERAGEIF(S), MAXIFS…) keep comparing at 15 significant digits, also as observed | | 2026-10-12 | minor | USDOLLAR added as a Compatibility function (not on Microsoft's list, so the counts don't change); MIRR with no positive flow is -1, COUNTIF's bare `"<="` / `">="` skip empty strings, INDIRECT with `a1` = FALSE gives `#REF!` for A1-style text (#738) | | 2026-10-11 | minor | TEXT / number formats fixed against Excel-cached results: fraction literals and zero fractions, `0` denominators zero padded, `?` pads the thousands separator, booleans use the text section (226 mismatches → 0 in 352 TEXT cells) | | 2026-10-11 | minor | Iterative calculation: circular references iterate up to Maximum Iterations / Maximum Change when enabled | From 5c20f164342f0b443fb7e6897f52f6e0b3934163 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Wed, 14 Oct 2026 18:30:16 -0500 Subject: [PATCH 004/687] core: read accounting negatives like $(1,234) in number text Excel 365 reads a dollar sign before accounting parentheses ($(1,234), $ (5)) and a percent sign after them ((5)%) as negative numbers when the text is typed, coerced by arithmetic or passed to VALUE. GridCraft gave #VALUE! for these. The parentheses branch no longer recurses: Excel reads only an unsigned plain number or fraction inside, so (-5), ((5)), ($-5), and dates or times in parentheses are now #VALUE! as in Excel (they used to give 5 or a negative serial). $5% is #VALUE! too: Excel never combines the currency and percent signs. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 4 +- crates/calc/src/tests.rs | 15 +++++ crates/core/src/parse.rs | 115 ++++++++++++++++++++++++++++++++++- crates/functions/src/text.rs | 7 +++ docs/function-parity.md | 3 +- 5 files changed, 139 insertions(+), 5 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..610011d871 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (accounting negatives such as `$(1,234)` read as numbers) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-14 | Accounting negatives in text: `$(1,234)`, `$ (5)` and `(5)%` read as negative numbers when typed, coerced by arithmetic or passed to VALUE, like Excel 365; `(-5)`, `((5))`, `$5%` and dates or times in parentheses stay text | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | @@ -193,6 +194,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | Progress log: accounting negatives in number text | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de78..1d5a1a464a 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -310,6 +310,21 @@ fn date_text_coerces_in_the_workbook_date_system() { } } +#[test] +fn accounting_negative_text_coerces() { + let mut t = T::new(); + t.set("A1", "'$(1,234)"); + t.set("B1", "=A1+0"); + t.set("C1", "=VALUE(A1)"); + t.set("D1", "=--\"$ (5)\""); + t.set("E1", "=\"(5)%\"*100"); + t.set("F1", "=\"(-5)\"+0"); + t.set("G1", "$(1,234)"); + assert_eq!((t.num("B1"), t.num("C1"), t.num("D1"), t.num("E1")), (-1234.0, -1234.0, -5.0, -5.0)); + assert_eq!(t.get("F1"), Value::Error(CellError::Value)); + assert_eq!(t.num("G1"), -1234.0); +} + #[test] fn operators() { let mut t = T::new(); diff --git a/crates/core/src/parse.rs b/crates/core/src/parse.rs index b465beaf9e..bf84876225 100644 --- a/crates/core/src/parse.rs +++ b/crates/core/src/parse.rs @@ -54,8 +54,13 @@ fn parse_numeric(t: &str, sys: DateSystem) -> Option<(f64, Option<&'static str>) return None; } // Parenthesised negatives: (100) = -100. - if let Some(inner) = t.strip_prefix('(').and_then(|x| x.strip_suffix(')')) { - return parse_numeric(inner.trim(), sys).map(|(n, f)| (-n, f)); + if let Some(inner) = parenthesized(t) { + // Excel reads no sign and no nested parentheses inside: `(-5)`, `($-5)`, `((5))`. + if inner.strip_prefix('$').map_or(inner.as_str(), str::trim_start).starts_with(['-', '+', '(']) { + return None; + } + let r = parse_plain(&inner).or_else(|| parse_fraction(&inner).map(|n| (n, Some("# ?/?")))); + return r.map(|(n, f)| (-n, f)); } if let Some(r) = parse_plain(t) { return Some(r); @@ -69,6 +74,22 @@ fn parse_numeric(t: &str, sys: DateSystem) -> Option<(f64, Option<&'static str>) None } +/// The number inside accounting parentheses, as plain text: `(5)` gives `5`. A dollar sign may +/// stand before them (`$(5)` reads as `($5)`) and a percent sign after them (`(5)%` reads as +/// `(5%)`), with spaces around either. The caller reads only a plain number or a fraction from it. +fn parenthesized(t: &str) -> Option { + let (currency, rest) = match t.strip_prefix('$') { + Some(r) => ("$", r.trim_start()), + None => ("", t), + }; + let rest = rest.strip_prefix('(')?; + let (body, percent) = match rest.strip_suffix(')') { + Some(b) => (b, ""), + None => (rest.strip_suffix('%')?.trim_end().strip_suffix(')')?, "%"), + }; + Some(format!("{currency}{}{percent}", body.trim())) +} + /// Signs, currency, thousands separators, percent, exponent. fn parse_plain(t: &str) -> Option<(f64, Option<&'static str>)> { let mut s = t; @@ -93,7 +114,8 @@ fn parse_plain(t: &str) -> Option<(f64, Option<&'static str>)> { percent = true; s = r.trim_end(); } - if s.is_empty() { + // Excel reads `$5` and `5%` but not `$5%`. + if s.is_empty() || currency && percent { return None; } let has_sep = s.contains(','); @@ -352,6 +374,93 @@ mod tests { assert_eq!(p(".5").value, Value::Number(0.5)); } + /// Observed in Excel 365 (en-US): typed entry, VALUE and `--"text"` agree. + #[test] + fn accounting_parentheses() { + let accepted: [(&str, f64, Option<&str>); 22] = [ + ("$(5)", -5.0, Some("\"$\"#,##0")), + ("$ (5)", -5.0, Some("\"$\"#,##0")), + ("$( 5 )", -5.0, Some("\"$\"#,##0")), + ("$ ( 1,234 )", -1234.0, Some("\"$\"#,##0")), + ("$(1,234)", -1234.0, Some("\"$\"#,##0")), + ("$(1,000.50)", -1000.5, Some("\"$\"#,##0.00")), + ("$(1,234.)", -1234.0, Some("\"$\"#,##0.00")), + ("$(.5)", -0.5, Some("\"$\"#,##0.00")), + ("$(0)", 0.0, Some("\"$\"#,##0")), + (" $(5) ", -5.0, Some("\"$\"#,##0")), + ("($5)", -5.0, Some("\"$\"#,##0")), + ("( $ 1,234 )", -1234.0, Some("\"$\"#,##0")), + ("(5)", -5.0, None), + ("( 5 )", -5.0, None), + ("(1,234.5)", -1234.5, Some("#,##0.00")), + ("(1e-3)", -0.001, Some("0.00E+00")), + ("(5)%", -0.05, Some("0%")), + ("( 5 ) %", -0.05, Some("0%")), + ("(5%)", -0.05, Some("0%")), + ("(5.)%", -0.05, Some("0.00%")), + ("(1,234)%", -12.34, Some("0%")), + ("(1 1/2)", -1.5, Some("# ?/?")), + ]; + for (s, n, f) in accepted { + assert_eq!(p(s), Parsed { value: Value::Number(n), format: f }, "{s}"); + assert_eq!(parse_number_text(s), Some(n), "{s}"); + } + for s in [ + "$5%", + "$(5%)", + "$(5)%", + "($5%)", + "($5)%", + "$ ( 5 ) %", + "(5)%%", + "(5%)%", + "((5))", + "(-5)", + "(+5)", + "($-5)", + "($ -5)", + "($+5)", + "(-$5)", + "$(-5)", + "$ (-5)", + "$(+5)", + "-(5)", + "-$(5)", + "+$(5)", + "$+(5)", + "$$(5)", + "$ $(5)", + "$((5))", + "($(5))", + "(-1 1/2)", + "()", + "$()", + "(5)$", + "$(5)$", + "(5)(5)", + "$(5)0", + "(5)e3", + "(5)-", + "(5-)", + "$(5-)", + "$(5", + "$5)", + "(5", + "$(1,23)", + "$(,5)", + "(5e)", + "(1/2)", + "$(1/2)", + "(3/4/2026)", + "$(3/4/2026)", + "(10:30)", + "$(10:30)", + ] { + assert_eq!(p(s).value, Value::text(s), "{s}"); + assert_eq!(parse_number_text(s), None, "{s}"); + } + } + #[test] fn texts_and_specials() { assert_eq!(p("hello").value, Value::text("hello")); diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index 91962d6e74..84fad98987 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -1092,6 +1092,13 @@ mod tests { close(ev("VALUE", vec![t("12:00")]), 0.5); close(ev("VALUE", vec![t("2020-01-01")]), 43831.0); is_err(ev("VALUE", vec![t("abc")]), CellError::Value); + // Accounting negatives, as Excel 365 reads them. + close(ev("VALUE", vec![t("$(5)")]), -5.0); + close(ev("VALUE", vec![t("$ (1,000.50)")]), -1000.5); + close(ev("VALUE", vec![t("(5)%")]), -0.05); + for s in ["$(5)%", "$(5%)", "$5%", "(-5)", "((5))", "$(-5)", "(1/2)"] { + is_err(ev("VALUE", vec![t(s)]), CellError::Value); + } close(ev("NUMBERVALUE", vec![t("2.500,27"), t(","), t(".")]), 2500.27); close(ev("NUMBERVALUE", vec![t("3.5%")]), 0.035); close(ev("NUMBERVALUE", vec![t(" 1 000 ")]), 1000.0); diff --git a/docs/function-parity.md b/docs/function-parity.md index 760ffd6ac3..3b25b144f1 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (USDOLLAR added as a compatibility function; MIRR, COUNTIF and INDIRECT edge cases match Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (number text with accounting parentheses coerces like Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -108,6 +108,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | Number text coercion (VALUE, arithmetic, typed entry): a dollar sign before accounting parentheses (`$(1,234)`, `$ (5)`) and a percent sign after them (`(5)%`) read as negatives; `(-5)`, `((5))`, `($-5)`, `$5%`, `$(5)%` and dates or times in parentheses are `#VALUE!`, as observed in Excel 365 | | 2026-10-12 | minor | USDOLLAR added as a Compatibility function (not on Microsoft's list, so the counts don't change); MIRR with no positive flow is -1, COUNTIF's bare `"<="` / `">="` skip empty strings, INDIRECT with `a1` = FALSE gives `#REF!` for A1-style text (#738) | | 2026-10-11 | minor | TEXT / number formats fixed against Excel-cached results: fraction literals and zero fractions, `0` denominators zero padded, `?` pads the thousands separator, booleans use the text section (226 mismatches → 0 in 352 TEXT cells) | | 2026-10-11 | minor | Iterative calculation: circular references iterate up to Maximum Iterations / Maximum Change when enabled | From ea190adad6a1edb792521a7818ef426570a8229c Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Wed, 14 Oct 2026 19:55:46 -0500 Subject: [PATCH 005/687] functions: Analysis ToolPak value arguments reject multi-cell references EOMONTH, EDATE, WORKDAY(.INTL), NETWORKDAYS(.INTL), YEARFRAC, WEEKNUM, the coupon, bond and T-bill functions, DISC, BIN2DEC, IM* and the other Analysis ToolPak functions give #VALUE! in Excel 365 for a reference to more than one cell in a value argument, in dynamic-array and legacy formulas alike, where GridCraft lifted the range and spilled. Array constants and computed arrays still lift, and range arguments (holidays, cash flows, schedules) and the .INTL weekend still take ranges. FnSpec gains a value_args mask, filled at registration from one table in lift.rs; call_lifted turns a multi-cell reference at a value position into a scalar #VALUE! before lifting, so EOMONTH(A1:A3,{1;2}) is a 2x1 array of #VALUE! as in Excel. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 4 +- crates/calc/src/tests.rs | 85 +++++++++++++++++++++++++++++++++++ crates/functions/src/lib.rs | 18 ++++++++ crates/functions/src/lift.rs | 87 ++++++++++++++++++++++++++++++++++++ docs/function-parity.md | 4 +- 5 files changed, 196 insertions(+), 2 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..2abac72629 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (Analysis ToolPak functions reject multi-cell references in value arguments) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-14 | Functions: EOMONTH, EDATE, WORKDAY, NETWORKDAYS, YEARFRAC, WEEKNUM, the bond and coupon functions, DISC, BIN2DEC, IM* and the other Analysis ToolPak functions give `#VALUE!` for a reference to more than one cell in a value argument, as Excel 365 does, instead of spilling; array constants and computed arrays still lift | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | @@ -193,6 +194,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | Progress log: Analysis ToolPak value arguments reject multi-cell references | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de78..a9317528b8 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -1266,3 +1266,88 @@ fn offset_extreme_arguments_return_ref() { assert_eq!(crate::evaluate(&wb, 0, c("D1"), formula), Value::Number(expected), "{formula}"); } } + +#[test] +fn analysis_toolpak_value_params_reject_multi_cell_references() { + // Excel 365 (observed): EOMONTH, WORKDAY, COUPDAYS, BIN2DEC... give #VALUE! for a reference to more + // than one cell, however the reference is formed, and lift array constants and computed arrays. + let mut t = T::new(); + for (a, v) in [ + ("A1", "45000"), + ("A2", "45031"), + ("A3", "45061"), + ("B1", "1"), + ("B2", "2"), + ("B3", "3"), + ("C2", "7"), + ("F1", "1"), + ("F2", "2"), + ("F3", "11"), + ] { + t.set(a, v); + } + t.wb.names.push(gridcraft_model::DefinedName { + name: "Dates".into(), + scope: None, + formula: "Sheet1!$A$1:$A$3".into(), + comment: String::new(), + hidden: false, + }); + let value = Value::Error(CellError::Value); + for f in [ + "=EOMONTH(A1:A3,1)", + "=EOMONTH(A1,B1:B3)", + "=EDATE(A1:A3,1)", + "=SUMPRODUCT(EOMONTH(A1:A3,0))", + "=EOMONTH(INDEX(A1:A3,0,1),0)", + "=EOMONTH(OFFSET(A1,0,0,3,1),0)", + "=EOMONTH(IF(TRUE,A1:A3),0)", + "=EOMONTH(CHOOSE(1,A1:A3),0)", + "=LET(x,A1:A3,EOMONTH(x,0))", + "=EOMONTH(Dates,0)", + "=EOMONTH((A1,A2),0)", + "=EOMONTH(Z:Z,0)", + "=EOMONTH(A1:INDEX(A1:A3,3),0)", + "=WORKDAY(A1:A3,1)", + "=NETWORKDAYS(A1,A3:A4)", + "=YEARFRAC(A1,A3,B1:B2)", + "=WEEKNUM(A1,B1:B3)", + "=COUPDAYS(A1:A3,46000,2,0)", + "=DISC(A1,46000,98,100,B1:B3)", + "=BIN2DEC(B1:B3)", + "=ISEVEN(B1:B3)", + ] { + t.set("H5", f); + assert_eq!((t.get("H5"), t.get("H6")), (value.clone(), Value::Empty), "{f}"); + t.set("H5", ""); + } + // One-cell references, intersections, @ and the range parameters are fine. + for (f, want) in [ + ("=EOMONTH(A1:A1,0)", 45016.0), + ("=EOMONTH(INDEX(A1:A3,2),0)", 45046.0), + ("=EOMONTH(OFFSET(A1,0,0,1,1),0)", 45016.0), + ("=EOMONTH(A1:A3 A2:C2,0)", 45046.0), + ("=EOMONTH(@A1:A3,0)", 45046.0), + ("=WORKDAY(A1,1,A1:A3)", 45001.0), + ("=XNPV(0,B1:B3,A1:A3)", 6.0), + ] { + t.set("H2", f); + assert_eq!(t.num("H2"), want, "{f}"); + t.set("H2", ""); + } + // Arrays that aren't references lift; so does the WORKDAY.INTL/NETWORKDAYS.INTL weekend. + for (f, below, right) in [ + ("=EOMONTH(A1:A3+0,0)", Value::Number(45046.0), Value::Empty), + ("=EOMONTH(A1,{1,2})", Value::Empty, Value::Number(45077.0)), + ("=EOMONTH(TRANSPOSE(A1:A3),0)", Value::Empty, Value::Number(45046.0)), + ("=MAP(A1:A3,LAMBDA(x,EOMONTH(x,0)))", Value::Number(45046.0), Value::Empty), + ("=NETWORKDAYS.INTL(A1,A3+30,F1:F3)", Value::Number(66.0), Value::Empty), + ("=ISOWEEKNUM(A1:A3)", Value::Number(15.0), Value::Empty), + // The rejected reference adds no shape: a 2x1 array of #VALUE!. + ("=EOMONTH(A1:A3,{1;2})", value.clone(), Value::Empty), + ] { + t.set("H5", f); + assert_eq!((t.get("H6"), t.get("I5")), (below, right), "{f}"); + t.set("H5", ""); + } +} diff --git a/crates/functions/src/lib.rs b/crates/functions/src/lib.rs index 148d682217..5f9dcb67d8 100644 --- a/crates/functions/src/lib.rs +++ b/crates/functions/src/lib.rs @@ -30,6 +30,7 @@ macro_rules! f { category: $crate::Category::$cat, volatile: true, scalar_args: $scal, + value_args: &[], signature: $sig, description: $desc, imp: { @@ -48,6 +49,7 @@ macro_rules! f { category: $crate::Category::$cat, volatile: false, scalar_args: $scal, + value_args: &[], signature: $sig, description: $desc, imp: { @@ -147,6 +149,11 @@ pub struct FnSpec { /// (broadcast like Excel's dynamic arrays). The last entry repeats for variadic tails; an /// empty slice means no lifting. pub scalar_args: &'static [bool], + /// Parameter positions that take a single value and reject a reference to more than one + /// cell with `#VALUE!` (the Analysis ToolPak functions: EOMONTH, WORKDAY, COUPDAYS, BIN2DEC…), + /// where other scalar parameters lift the range. Arrays that aren't references still lift. + /// Same layout as `scalar_args`; filled at registration from [`lift::value_args`]. + pub value_args: &'static [bool], pub signature: &'static str, pub description: &'static str, pub imp: FnImpl, @@ -167,6 +174,14 @@ impl FnSpec { } } + /// Whether parameter `i` takes a single value, so a multi-cell reference there is `#VALUE!`. + pub fn is_value_arg(&self, i: usize) -> bool { + match self.value_args.get(i) { + Some(b) => *b, + None => self.value_args.last().copied().unwrap_or(false), + } + } + /// Whether a call with `n` arguments is valid (Excel allows at most 255). pub fn accepts(&self, n: usize) -> bool { (self.min_args..=self.max_args).contains(&n) && n <= 255 @@ -195,6 +210,9 @@ fn registry() -> &'static (Vec, HashMap<&'static str, usize>) { ] { v.extend(list); } + for s in &mut v { + s.value_args = lift::value_args(s.name); + } let mut map = HashMap::with_capacity(v.len()); for (i, s) in v.iter().enumerate() { map.entry(s.name).or_insert(i); diff --git a/crates/functions/src/lift.rs b/crates/functions/src/lift.rs index cea0067ee8..056cb6d312 100644 --- a/crates/functions/src/lift.rs +++ b/crates/functions/src/lift.rs @@ -6,7 +6,61 @@ use gridcraft_core::{Array, CellError, Value}; use crate::util::MAX_CELLS; use crate::{Arg, Ctx, FnSpec}; +/// Value-parameter masks (see [`FnSpec::value_args`]), observed in Excel 365: a reference to more +/// than one cell at these positions gives `#VALUE!` in dynamic-array and legacy formulas alike (no +/// implicit intersection), while array constants and computed arrays lift. Range parameters +/// (holidays, schedules, cash flows, coefficients) and the WORKDAY.INTL/NETWORKDAYS.INTL weekend +/// (which lifts a range) are `false`. +pub(crate) const VALUE_ARGS: &[(&str, &[bool])] = &[ + // Date and time. + ("EOMONTH EDATE YEARFRAC WEEKNUM", &[true]), + ("WORKDAY NETWORKDAYS", &[true, true, false]), + ("WORKDAY.INTL NETWORKDAYS.INTL", &[true, true, false, false]), + // Financial. + ( + "ACCRINT ACCRINTM AMORDEGRC AMORLINC COUPDAYBS COUPDAYS COUPDAYSNC COUPNCD COUPNUM COUPPCD CUMIPMT CUMPRINC DISC \ + DOLLARDE DOLLARFR DURATION EFFECT INTRATE MDURATION NOMINAL ODDFPRICE ODDFYIELD ODDLPRICE ODDLYIELD PRICE PRICEDISC \ + PRICEMAT RECEIVED TBILLEQ TBILLPRICE TBILLYIELD YIELD YIELDDISC YIELDMAT", + &[true], + ), + ("FVSCHEDULE XNPV", &[true, false]), + ("XIRR", &[false, false, true]), + // Math and information. + ("ISEVEN ISODD QUOTIENT MROUND FACTDOUBLE SQRTPI RANDBETWEEN", &[true]), + ("SERIESSUM", &[true, true, true, false]), + // Engineering (IMSUM and IMPRODUCT aggregate, so they aren't here). + ( + "BIN2DEC BIN2HEX BIN2OCT DEC2BIN DEC2HEX DEC2OCT HEX2BIN HEX2DEC HEX2OCT OCT2BIN OCT2DEC OCT2HEX CONVERT DELTA GESTEP \ + ERF ERF.PRECISE ERFC ERFC.PRECISE BESSELI BESSELJ BESSELK BESSELY COMPLEX IMABS IMAGINARY IMARGUMENT IMCONJUGATE \ + IMCOS IMCOSH IMCOT IMCSC IMCSCH IMDIV IMEXP IMLN IMLOG10 IMLOG2 IMPOWER IMREAL IMSEC IMSECH IMSIN IMSINH IMSQRT IMSUB \ + IMTAN", + &[true], + ), +]; + +/// The value-parameter mask of `name` (empty for functions without value parameters). +pub(crate) fn value_args(name: &str) -> &'static [bool] { + VALUE_ARGS.iter().find(|(names, _)| names.split_ascii_whitespace().any(|n| n == name)).map_or(&[], |(_, mask)| mask) +} + +/// A reference to more than one cell at a value parameter becomes a scalar `#VALUE!`, so it adds +/// no shape: `EOMONTH(A1:A3, {1;2})` is a 2x1 array of `#VALUE!`, as in Excel. +fn reject_multi_cell_refs(spec: &FnSpec, args: &[Arg]) -> Option> { + let multi = |i: usize, a: &Arg| a.from_ref && spec.is_value_arg(i) && matches!(&a.value, Value::Array(arr) if arr.data.len() > 1); + if !args.iter().enumerate().any(|(i, a)| multi(i, a)) { + return None; + } + Some( + args.iter() + .enumerate() + .map(|(i, a)| if multi(i, a) { Arg { value: Value::Error(CellError::Value), from_ref: false } } else { a.clone() }) + .collect(), + ) +} + pub(crate) fn call_lifted(spec: &FnSpec, args: &[Arg], ctx: &mut dyn Ctx) -> Value { + let rejected = if spec.value_args.is_empty() { None } else { reject_multi_cell_refs(spec, args) }; + let args = rejected.as_deref().unwrap_or(args); let mut rows = 0usize; let mut cols = 0usize; let mut any = false; @@ -94,4 +148,37 @@ mod tests { ); is_err(huge, CellError::Num); } + + #[test] + fn value_params_reject_multi_cell_references() { + let s = FnSpec { value_args: &[true, false], ..spec() }; + let mut ctx = TestCtx::default(); + // A multi-cell reference is #VALUE!; a one-cell reference and a computed array are fine. + is_err(crate::call(&s, &[rf(col(&[1.0, 2.0, 3.0])), n(1.0)], &mut ctx), CellError::Value); + assert_eq!(crate::call(&s, &[rf(col(&[4.0])), n(1.0)], &mut ctx), nv(5.0)); + assert_eq!(rows_of(&crate::call(&s, &[av(col(&[1.0, 2.0])), n(1.0)], &mut ctx)), vec![vec![nv(2.0)], vec![nv(3.0)]]); + // The rejected reference adds no shape: the other argument still lifts. + let v = Value::Error(CellError::Value); + assert_eq!(rows_of(&crate::call(&s, &[rf(col(&[1.0, 2.0, 3.0])), av(col(&[1.0, 2.0]))], &mut ctx)), vec![vec![v.clone()], vec![v]]); + // A position that isn't a value parameter still lifts the reference. + assert_eq!(rows_of(&crate::call(&s, &[n(1.0), rf(row(&[1.0, 2.0]))], &mut ctx)), vec![vec![nv(2.0), nv(3.0)]]); + } + + #[test] + fn value_arg_table_names_registered_scalar_positions() { + for (names, mask) in super::VALUE_ARGS { + assert!(!mask.is_empty()); + for name in names.split_ascii_whitespace() { + let Some(s) = crate::lookup(name) else { panic!("{name} isn't registered") }; + assert_eq!(s.value_args, *mask, "{name}"); + for i in 0..s.max_args.min(12) { + assert!(!s.is_value_arg(i) || s.is_scalar_arg(i), "{name} argument {i} is a value parameter but doesn't lift"); + } + } + } + assert!(crate::lookup("WORKDAY").is_some_and(|s| s.is_value_arg(1) && !s.is_value_arg(2))); + assert!(crate::lookup("WORKDAY.INTL").is_some_and(|s| !s.is_value_arg(2) && s.is_scalar_arg(2))); + assert!(crate::lookup("SUM").is_some_and(|s| s.value_args.is_empty())); + assert!(crate::lookup("ISOWEEKNUM").is_some_and(|s| s.value_args.is_empty())); + } } diff --git a/docs/function-parity.md b/docs/function-parity.md index 760ffd6ac3..91283721da 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (USDOLLAR added as a compatibility function; MIRR, COUNTIF and INDIRECT edge cases match Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-14 · **Change:** minor (Analysis ToolPak functions give #VALUE! for a multi-cell reference in a value argument, like Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -77,6 +77,7 @@ PIVOTBY, REGEXTEST…) are all present. | Defined names (workbook and sheet scope), Create from Selection | done | | Dynamic arrays, spill, `#SPILL!`, spill operator `A1#` | done; stale-spill bugs (#133, #139; PRs #148, #157) | | Implicit intersection `@`, legacy CSE array formulas | done; legacy array range fill (PR #156) | +| Array lifting: an array in a scalar argument evaluates the function per element | done. The Analysis ToolPak functions (EOMONTH, EDATE, WORKDAY(.INTL), NETWORKDAYS(.INTL), YEARFRAC, WEEKNUM, the COUP*, PRICE*, YIELD*, ODD*, TBILL*, ACCRINT*, AMOR*, DISC, DURATION, CUMIPMT/CUMPRINC, EFFECT, NOMINAL, DOLLARDE/FR, XNPV/XIRR rate and guess, FVSCHEDULE principal, ISEVEN/ISODD, QUOTIENT, MROUND, FACTDOUBLE, SQRTPI, RANDBETWEEN, SERIESSUM, the base conversions, CONVERT, DELTA, GESTEP, ERF/ERFC, BESSEL*, COMPLEX and IM* except IMSUM/IMPRODUCT) give `#VALUE!` for a reference to more than one cell in a value argument, as Excel 365 does in both dynamic-array and legacy formulas; array constants and computed arrays (`A1:A3+0`) still lift, and range arguments (holidays, schedule, cash flows) and the .INTL weekend accept ranges. Difference: BYROW/BYCOL pass their row or column to the LAMBDA as values, so `BYROW(A1:B3,LAMBDA(r,EOMONTH(r,0)))` lifts where Excel gives `#VALUE!` | | Trim references `A1:.E10` (trailing), `A1.:E10` (leading), `A1.:.E10` (both), also on whole rows/columns (`A:.A`) | done; evaluated as `TRIMRANGE(range, code, code)`, adjusted on insert/delete; files store `_xlfn.TRIMRANGE(range,code,code)` (Excel's own file spelling unconfirmed in public docs), `_xlfn._TRO_ALL/_TRO_LEADING/_TRO_TRAILING(...)` read too | | LET, LAMBDA, named LAMBDAs, ISOMITTED, MAP/REDUCE/SCAN/BYROW/BYCOL/MAKEARRAY | done | | Iterative calculation for circular references | done: with Enable Iterative Calculation on (`formulas.calculationOptions` `{iterative, maxIterations, maxChange}`, saved as ``), loops are evaluated again from their previous values (blank = 0) until no cell on a loop changes by Maximum Change or Maximum Iterations is reached; off, cycles give `#CIRC!`. Differences: no Options dialog yet (command only); GridCraft recalculates on open, so a loop that never settles (`=A1+1`) advances again each time the file is opened | @@ -108,6 +109,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-14 | minor | Analysis ToolPak value arguments (EOMONTH, WORKDAY, YEARFRAC, COUPDAYS, DISC, BIN2DEC, IM* and the rest, observed in Excel 365) give `#VALUE!` for a multi-cell reference instead of lifting it | | 2026-10-12 | minor | USDOLLAR added as a Compatibility function (not on Microsoft's list, so the counts don't change); MIRR with no positive flow is -1, COUNTIF's bare `"<="` / `">="` skip empty strings, INDIRECT with `a1` = FALSE gives `#REF!` for A1-style text (#738) | | 2026-10-11 | minor | TEXT / number formats fixed against Excel-cached results: fraction literals and zero fractions, `0` denominators zero padded, `?` pads the thousands separator, booleans use the text section (226 mismatches → 0 in 352 TEXT cells) | | 2026-10-11 | minor | Iterative calculation: circular references iterate up to Maximum Iterations / Maximum Change when enabled | From d280e6ff005beb9c45c440094951a36e1e2139c7 Mon Sep 17 00:00:00 2001 From: Gert Pellin Date: Thu, 15 Oct 2026 04:44:22 +0200 Subject: [PATCH 006/687] Clear: Delete and the Clear commands skip the rows a filter hides (#1419) As in Excel, clearing a filtered selection leaves the rows the filter hides as they are; rows hidden by hand are still cleared. Checked boxes in hidden rows no longer hold back Delete on the boxes showing. --- crates/engine/src/cmd/edit.rs | 27 ++++++++++++---- crates/engine/src/tests.rs | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 82dbe0be72..d31ebe08b1 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1220,6 +1220,13 @@ pub(crate) fn rewrite_all_formulas(wb: &mut gridcraft_model::Workbook, target: & // ---------------------------------------------------------------- clear +/// The rows of `range` an applied filter hides (rows hidden by hand are not among them). +fn filtered_rows(sh: &gridcraft_model::Sheet, range: RangeRef) -> std::collections::BTreeSet { + sh.rows.range(range.start.row..=range.end.row).filter(|(r, info)| info.hidden && sh.is_row_filtered(**r)).map(|(r, _)| *r).collect() +} + +/// The Clear commands (Delete is Clear Contents). As in Excel, the rows an applied filter hides +/// are left as they are; rows hidden by hand are cleared. fn clear(s: &mut Session, p: &Json, what: &str) -> Result { let sheet = target_sheet(s, p)?; let ranges = target_ranges(s, p)?; @@ -1230,7 +1237,10 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result { d.wb.sheet(sheet).is_some_and(|sh| { ranges.iter().any(|r| { r.intersection(&sh.used_range().unwrap_or(RangeRef::cell(r.start))).is_some_and(|r| { - sh.cells.iter_range(r).any(|(c, x)| x.value == Value::Bool(true) && super::extra::is_checkbox_cell(&d.wb, sh, c)) + let skip = filtered_rows(sh, r); + sh.cells + .iter_range(r) + .any(|(c, x)| !skip.contains(&c.row) && x.value == Value::Bool(true) && super::extra::is_checkbox_cell(&d.wb, sh, c)) }) }) }) @@ -1244,8 +1254,12 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result { }, None => continue, }; - let cells: Vec<(CellRef, Cell)> = - cx.wb.sheet(sheet).map(|sh| sh.cells.iter_range(r).map(|(c, x)| (c, x.clone())).collect()).unwrap_or_default(); + let skip = cx.wb.sheet(sheet).map(|sh| filtered_rows(sh, r)).unwrap_or_default(); + let cells: Vec<(CellRef, Cell)> = cx + .wb + .sheet(sheet) + .map(|sh| sh.cells.iter_range(r).filter(|(c, _)| !skip.contains(&c.row)).map(|(c, x)| (c, x.clone())).collect()) + .unwrap_or_default(); let mut cells = cells; if what == "contents" { for (_, cell) in cells.iter_mut() { @@ -1277,14 +1291,15 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result { _ => {} } } + let cleared = |c: CellRef| r.contains(c) && !skip.contains(&c.row); if matches!(what, "all" | "comments") { - sh.comments.retain(|c, _| !r.contains(*c)); + sh.comments.retain(|c, _| !cleared(*c)); } if matches!(what, "all" | "hyperlinks") { - sh.hyperlinks.retain(|c, _| !r.contains(*c)); + sh.hyperlinks.retain(|c, _| !cleared(*c)); } if matches!(what, "all" | "formats") { - sh.merges.retain(|m| !r.contains_range(m)); + sh.merges.retain(|m| !r.contains_range(m) || skip.range(m.start.row..=m.end.row).next().is_some()); } for c in r.iter().take(1_000_000) { cx.changed.push((sheet, c)); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index ae499a1895..3074ed6704 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -827,6 +827,48 @@ fn sort_and_filter() { assert_eq!(r["hiddenRows"], 1); } +/// Delete and the Clear commands on a filtered selection clear only the rows showing, as in +/// Excel (#1419); rows hidden by hand are cleared with the rest. +#[test] +fn clearing_a_filtered_selection_leaves_the_rows_the_filter_hides() { + let mut s = s(); + let rows = [["Name", "Status"], ["John", "Active"], ["Mary", "Inactive"], ["David", "Active"], ["Sarah", "Inactive"], ["Michael", "Active"]]; + s.execute("range.setValues", json!({"range": "A1", "values": rows})).unwrap(); + s.execute("home.bold", json!({"range": "A2:A6", "on": true})).unwrap(); + s.execute("review.newNote", json!({"cell": "A3", "text": "note", "author": "Example"})).unwrap(); + s.execute("selection.set", json!({"cell": "A1"})).unwrap(); + s.execute("data.filter", json!({})).unwrap(); + s.execute("data.filterBy", json!({"column": "B", "values": ["Active"]})).unwrap(); + let bold = |s: &Session, a: &str| { + let d = s.doc().unwrap(); + d.wb.styles.get(d.wb.active().unwrap().style_id(CellRef::parse(a).unwrap())).font.bold + }; + + s.execute("edit.clearContents", json!({"range": "A2:A6"})).unwrap(); + for a in ["A2", "A4", "A6"] { + assert_eq!(v(&s, a), Value::Empty, "{a} shows and is cleared"); + } + assert_eq!(v(&s, "A3"), Value::from("Mary")); + assert_eq!(v(&s, "A5"), Value::from("Sarah")); + + s.execute("edit.clearFormats", json!({"range": "A2:A6"})).unwrap(); + assert!(!bold(&s, "A2")); + assert!(bold(&s, "A3"), "a filtered-out row keeps its format"); + + s.execute("edit.clearAll", json!({"range": "A2:B6"})).unwrap(); + assert_eq!(v(&s, "B2"), Value::Empty); + assert_eq!(v(&s, "B3"), Value::from("Inactive")); + assert_eq!(v(&s, "A5"), Value::from("Sarah")); + assert!(s.doc().unwrap().wb.active().unwrap().comments.contains_key(&CellRef::parse("A3").unwrap())); + + // Without the filter, a row hidden by hand is cleared like the rows around it. + s.execute("data.clearFilter", json!({})).unwrap(); + s.execute("home.hideRows", json!({"rows": "5:5"})).unwrap(); + s.execute("edit.clearContents", json!({"range": "A3:A5"})).unwrap(); + assert_eq!(v(&s, "A3"), Value::Empty); + assert_eq!(v(&s, "A5"), Value::Empty); +} + /// Top 10 keeps the top or bottom N items or percent, ties at the threshold included. #[test] fn filter_top10_items_and_percent() { @@ -2101,6 +2143,24 @@ fn checkboxes_keep_values_toggle_and_delete_in_two_passes() { assert_ne!(fmt_of(&s, "B2"), "checkbox"); } +/// A checked box in a row the filter hides doesn't hold back Delete on the boxes showing, and +/// stays as it is (#1419). +#[test] +fn delete_on_filtered_checkboxes_skips_the_hidden_rows() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Status"], ["on"], ["off"], ["on"]]})).unwrap(); + s.execute("insert.checkbox", json!({"range": "B2:B4"})).unwrap(); + s.execute("cell.toggleCheckbox", json!({"cell": "B3"})).unwrap(); + s.execute("selection.set", json!({"cell": "A1"})).unwrap(); + s.execute("data.filter", json!({})).unwrap(); + s.execute("data.filterBy", json!({"column": "A", "values": ["on"]})).unwrap(); + s.execute("edit.clearContents", json!({"range": "B2:B4"})).unwrap(); + assert_ne!(fmt_of(&s, "B2"), "checkbox", "the unchecked boxes showing are removed at once"); + assert_ne!(fmt_of(&s, "B4"), "checkbox"); + assert_eq!(fmt_of(&s, "B3"), "checkbox"); + assert_eq!(v(&s, "B3"), Value::Bool(true)); +} + #[test] fn checkboxes_survive_xlsx_as_cell_controls() { let mut s = s(); From 5bd6cc3d04701b1f70442f2a2eaae5d285a0f3e8 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Thu, 15 Oct 2026 00:57:17 -0500 Subject: [PATCH 007/687] Options: Maximum Iterations and Maximum Change in File > Options > Formulas The Formulas section had the calculation-mode radios and Enable iterative calculation but not the two limits that formulas.calculationOptions already takes. They now sit under the checkbox, disabled while iteration is off, and commit when the field loses focus. Like Excel, out-of-range entries (Maximum Iterations outside 1-32767, a negative or non-numeric Maximum Change) are refused with a message instead of being clamped by the command; the Enter that committed the field no longer also dismisses that message. Labels and messages are translated for every interface language. A kittest covers the disabled state, valid and invalid entries, and undo refreshing the field. Drops the stale "no Options dialog yet" from gaps.md and function-parity.md. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 3 +- crates/ui-egui/src/backstage.rs | 53 +++++++++++++++++++ crates/ui-egui/src/i18n.rs | 4 ++ crates/ui-egui/src/i18n/cs.rs | 4 ++ crates/ui-egui/src/i18n/de.rs | 4 ++ crates/ui-egui/src/i18n/es.rs | 4 ++ crates/ui-egui/src/i18n/fr.rs | 6 +++ crates/ui-egui/src/i18n/ko.rs | 4 ++ crates/ui-egui/src/i18n/pt.rs | 4 ++ crates/ui-egui/src/i18n/ru.rs | 4 ++ crates/ui-egui/src/i18n/sr.rs | 4 ++ crates/ui-egui/src/i18n/zh.rs | 4 ++ crates/ui-egui/tests/file_menu.rs | 88 ++++++++++++++++++++++++++++++- docs/function-parity.md | 5 +- docs/gaps.md | 6 ++- docs/ui-parity.md | 5 +- 16 files changed, 194 insertions(+), 8 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..2c06f07d9b 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-15 · **Change:** minor (Maximum Iterations and Maximum Change in File › Options) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-15 | File › Options › Formulas: Maximum Iterations and Maximum Change fields for iterative calculation, disabled while it is off, with Excel's limits (1 to 32767, 0 or more) | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | diff --git a/crates/ui-egui/src/backstage.rs b/crates/ui-egui/src/backstage.rs index 922baabc59..bc41222e12 100644 --- a/crates/ui-egui/src/backstage.rs +++ b/crates/ui-egui/src/backstage.rs @@ -992,10 +992,63 @@ fn options(app: &mut SheetApp, ui: &mut Ui) { if ui.checkbox(&mut iterative, lang.tr("Enable iterative calculation")).changed() { app.run_or_alert("formulas.calculationOptions", json!({"iterative": iterative})); } + // Maximum Iterations and Maximum Change apply only while iteration is on, as in Excel. + ui.add_enabled_ui(calc.iterative, |ui| { + ui.horizontal(|ui| { + ui.add_space(24.0); + egui::Grid::new("backstage_options_iteration").num_columns(2).spacing(vec2(12.0, 6.0)).show(ui, |ui| { + ui.label(lang.tr("Maximum Iterations:")); + let id = ui.make_persistent_id("backstage_max_iterations"); + if let Some(text) = crate::widgets::committed_text(ui, id, &calc.max_iterations.to_string(), Some(80.0)) { + match parse_max_iterations(&text) { + Some(n) if n != calc.max_iterations => { + app.run_or_alert("formulas.calculationOptions", json!({"maxIterations": n})); + } + Some(_) => {} + None => refuse(app, ui, "Maximum Iterations must be a whole number from 1 to 32767."), + } + } + ui.end_row(); + ui.label(lang.tr("Maximum Change:")); + let id = ui.make_persistent_id("backstage_max_change"); + if let Some(text) = crate::widgets::committed_text(ui, id, &calc.max_change.to_string(), Some(80.0)) { + match parse_max_change(&text) { + Some(d) if d != calc.max_change => { + app.run_or_alert("formulas.calculationOptions", json!({"maxChange": d})); + } + Some(_) => {} + None => refuse(app, ui, "Maximum Change must be a number of 0 or more."), + } + } + ui.end_row(); + }); + }); + }); + // TODO: add "Set precision as displayed" here once `formulas.calculationOptions` takes + // `precisionAsDisplayed` (PR #1366); the model already stores it. ui.add_space(6.0); dim(ui, lang.tr("Calculation settings are saved with the workbook.")); } +/// Refuses a typed setting with a message, leaving the workbook as it was. The Enter that +/// committed the field is consumed so it doesn't also dismiss the message box in this frame. +fn refuse(app: &mut SheetApp, ui: &mut Ui, text: &str) { + ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Enter)); + app.message = Some(("GridCraft".into(), app.ui.language.tr(text).into())); +} + +/// Maximum Iterations as typed: a whole number from 1 to 32767, or `None` (Excel refuses others). +fn parse_max_iterations(text: &str) -> Option { + let n = text.trim().parse::().ok()?; + (1..=gridcraft_engine::model::CalcSettings::MAX_ITERATIONS).contains(&n).then_some(n) +} + +/// Maximum Change as typed: a finite number of 0 or more, or `None`. +fn parse_max_change(text: &str) -> Option { + let d = text.trim().parse::().ok()?; + (d.is_finite() && d >= 0.0).then_some(d) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 061698df9b..17b098de2b 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -587,6 +587,10 @@ const JAPANESE: &[(&str, &str)] = &[ ("Save .xlsx workbooks automatically after every change", "変更のたびに .xlsx ブックを自動的に保存する"), ("Workbook calculation", "ブックの計算"), ("Enable iterative calculation", "反復計算を行う"), + ("Maximum Iterations:", "最大反復回数:"), + ("Maximum Change:", "変化の最大値:"), + ("Maximum Iterations must be a whole number from 1 to 32767.", "最大反復回数には 1 から 32767 までの整数を指定してください。"), + ("Maximum Change must be a number of 0 or more.", "変化の最大値には 0 以上の数値を指定してください。"), ("Calculation settings are saved with the workbook.", "計算の設定はブックと共に保存されます。"), ]; diff --git a/crates/ui-egui/src/i18n/cs.rs b/crates/ui-egui/src/i18n/cs.rs index 7ed8372c7d..deaff822ce 100644 --- a/crates/ui-egui/src/i18n/cs.rs +++ b/crates/ui-egui/src/i18n/cs.rs @@ -323,6 +323,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "Automaticky ukládat sešity .xlsx po každé změně", "Workbook calculation" => "Výpočet sešitu", "Enable iterative calculation" => "Povolit iterativní výpočet", + "Maximum Iterations:" => "Maximální počet iterací:", + "Maximum Change:" => "Maximální změna:", + "Maximum Iterations must be a whole number from 1 to 32767." => "Maximální počet iterací musí být celé číslo od 1 do 32767.", + "Maximum Change must be a number of 0 or more." => "Maximální změna musí být číslo 0 nebo větší.", "Calculation settings are saved with the workbook." => "Nastavení výpočtů se ukládá se sešitem.", "100%" => "100 %", "Zoom to\nSelection" => "Přiblížit\nna výběr", diff --git a/crates/ui-egui/src/i18n/de.rs b/crates/ui-egui/src/i18n/de.rs index c20c7d1b36..efa20cac75 100644 --- a/crates/ui-egui/src/i18n/de.rs +++ b/crates/ui-egui/src/i18n/de.rs @@ -323,6 +323,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => ".xlsx-Arbeitsmappen nach jeder Änderung automatisch speichern", "Workbook calculation" => "Arbeitsmappenberechnung", "Enable iterative calculation" => "Iterative Berechnung aktivieren", + "Maximum Iterations:" => "Maximale Iterationszahl:", + "Maximum Change:" => "Maximale Änderung:", + "Maximum Iterations must be a whole number from 1 to 32767." => "Die maximale Iterationszahl muss eine ganze Zahl von 1 bis 32767 sein.", + "Maximum Change must be a number of 0 or more." => "Die maximale Änderung muss eine Zahl größer oder gleich 0 sein.", "Calculation settings are saved with the workbook." => "Berechnungseinstellungen werden mit der Arbeitsmappe gespeichert.", "Interface size" => "Größe der Benutzeroberfläche", "Auto" => "Automatisch", diff --git a/crates/ui-egui/src/i18n/es.rs b/crates/ui-egui/src/i18n/es.rs index 4ce231bcad..508f04945d 100644 --- a/crates/ui-egui/src/i18n/es.rs +++ b/crates/ui-egui/src/i18n/es.rs @@ -322,6 +322,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "Guardar automáticamente los libros .xlsx tras cada cambio", "Workbook calculation" => "Cálculo del libro", "Enable iterative calculation" => "Habilitar cálculo iterativo", + "Maximum Iterations:" => "Iteraciones máximas:", + "Maximum Change:" => "Cambio máximo:", + "Maximum Iterations must be a whole number from 1 to 32767." => "Las iteraciones máximas deben ser un número entero de 1 a 32767.", + "Maximum Change must be a number of 0 or more." => "El cambio máximo debe ser un número igual o mayor que 0.", "Calculation settings are saved with the workbook." => "La configuración de cálculo se guarda con el libro.", "Interface size" => "Tamaño de la interfaz", "Auto" => "Automático", diff --git a/crates/ui-egui/src/i18n/fr.rs b/crates/ui-egui/src/i18n/fr.rs index 5d20ab0ca2..dec45571b5 100644 --- a/crates/ui-egui/src/i18n/fr.rs +++ b/crates/ui-egui/src/i18n/fr.rs @@ -409,6 +409,12 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "Enregistrer automatiquement les classeurs .xlsx après chaque modification", "Workbook calculation" => "Calcul du classeur", "Enable iterative calculation" => "Activer le calcul itératif", + "Maximum Iterations:" => "Nombre maximal d'itérations :", + "Maximum Change:" => "Écart maximal :", + "Maximum Iterations must be a whole number from 1 to 32767." => { + "Le nombre maximal d'itérations doit être un entier compris entre 1 et 32767." + } + "Maximum Change must be a number of 0 or more." => "L'écart maximal doit être un nombre supérieur ou égal à 0.", "Calculation settings are saved with the workbook." => "Les paramètres de calcul sont enregistrés avec le classeur.", _ => return None, }) diff --git a/crates/ui-egui/src/i18n/ko.rs b/crates/ui-egui/src/i18n/ko.rs index 1902ad3015..566044d57a 100644 --- a/crates/ui-egui/src/i18n/ko.rs +++ b/crates/ui-egui/src/i18n/ko.rs @@ -409,6 +409,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "변경할 때마다 .xlsx 통합 문서를 자동으로 저장", "Workbook calculation" => "통합 문서 계산", "Enable iterative calculation" => "반복 계산 사용", + "Maximum Iterations:" => "최대 반복 횟수:", + "Maximum Change:" => "변화 한도:", + "Maximum Iterations must be a whole number from 1 to 32767." => "최대 반복 횟수는 1에서 32767 사이의 정수여야 합니다.", + "Maximum Change must be a number of 0 or more." => "변화 한도는 0 이상의 숫자여야 합니다.", "Calculation settings are saved with the workbook." => "계산 설정은 통합 문서와 함께 저장됩니다.", _ => return None, }) diff --git a/crates/ui-egui/src/i18n/pt.rs b/crates/ui-egui/src/i18n/pt.rs index fb9236cdb6..74d4c62688 100644 --- a/crates/ui-egui/src/i18n/pt.rs +++ b/crates/ui-egui/src/i18n/pt.rs @@ -409,6 +409,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "Salvar pastas de trabalho .xlsx automaticamente após cada alteração", "Workbook calculation" => "Cálculo da pasta de trabalho", "Enable iterative calculation" => "Habilitar cálculo iterativo", + "Maximum Iterations:" => "Número Máximo de Iterações:", + "Maximum Change:" => "Alteração Máxima:", + "Maximum Iterations must be a whole number from 1 to 32767." => "O número máximo de iterações deve ser um número inteiro de 1 a 32767.", + "Maximum Change must be a number of 0 or more." => "A alteração máxima deve ser um número maior ou igual a 0.", "Calculation settings are saved with the workbook." => "As configurações de cálculo são salvas com a pasta de trabalho.", "OK" => "OK", "Cancel" => "Cancelar", diff --git a/crates/ui-egui/src/i18n/ru.rs b/crates/ui-egui/src/i18n/ru.rs index 765f6bb326..f53611517a 100644 --- a/crates/ui-egui/src/i18n/ru.rs +++ b/crates/ui-egui/src/i18n/ru.rs @@ -409,6 +409,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "Автоматически сохранять книги .xlsx после каждого изменения", "Workbook calculation" => "Вычисления в книге", "Enable iterative calculation" => "Включить итеративные вычисления", + "Maximum Iterations:" => "Предельное число итераций:", + "Maximum Change:" => "Относительная погрешность:", + "Maximum Iterations must be a whole number from 1 to 32767." => "Предельное число итераций должно быть целым числом от 1 до 32767.", + "Maximum Change must be a number of 0 or more." => "Относительная погрешность должна быть числом не меньше 0.", "Calculation settings are saved with the workbook." => "Параметры вычислений сохраняются вместе с книгой.", _ => return None, }) diff --git a/crates/ui-egui/src/i18n/sr.rs b/crates/ui-egui/src/i18n/sr.rs index cbf18d4c46..8f724c22b3 100644 --- a/crates/ui-egui/src/i18n/sr.rs +++ b/crates/ui-egui/src/i18n/sr.rs @@ -401,6 +401,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "Automatski sačuvaj .xlsx radne sveske posle svake izmene", "Workbook calculation" => "Izračunavanje radne sveske", "Enable iterative calculation" => "Omogući iterativno izračunavanje", + "Maximum Iterations:" => "Maksimalan broj iteracija:", + "Maximum Change:" => "Maksimalna promena:", + "Maximum Iterations must be a whole number from 1 to 32767." => "Maksimalan broj iteracija mora biti ceo broj od 1 do 32767.", + "Maximum Change must be a number of 0 or more." => "Maksimalna promena mora biti broj veći ili jednak 0.", "Calculation settings are saved with the workbook." => "Podešavanja izračunavanja se čuvaju sa radnom sveskom.", "Left Margin" => "Leva margina", "Right Margin" => "Desna margina", diff --git a/crates/ui-egui/src/i18n/zh.rs b/crates/ui-egui/src/i18n/zh.rs index 00175925e6..8f77aeb6d7 100644 --- a/crates/ui-egui/src/i18n/zh.rs +++ b/crates/ui-egui/src/i18n/zh.rs @@ -401,6 +401,10 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Save .xlsx workbooks automatically after every change" => "每次更改后自动保存 .xlsx 工作簿", "Workbook calculation" => "工作簿计算", "Enable iterative calculation" => "启用迭代计算", + "Maximum Iterations:" => "最多迭代次数:", + "Maximum Change:" => "最大误差:", + "Maximum Iterations must be a whole number from 1 to 32767." => "最多迭代次数必须是 1 到 32767 之间的整数。", + "Maximum Change must be a number of 0 or more." => "最大误差必须是大于或等于 0 的数字。", "Calculation settings are saved with the workbook." => "计算设置随工作簿一起保存。", _ => return None, }) diff --git a/crates/ui-egui/tests/file_menu.rs b/crates/ui-egui/tests/file_menu.rs index 691eef3675..33059c14e7 100644 --- a/crates/ui-egui/tests/file_menu.rs +++ b/crates/ui-egui/tests/file_menu.rs @@ -2,7 +2,7 @@ use std::sync::{Arc, Mutex}; -use egui_kittest::kittest::Queryable as _; +use egui_kittest::kittest::{NodeT as _, Queryable as _}; use gridcraft_engine::{Session, core::CellRef, core::Value}; use gridcraft_ui_egui::{Services, SheetApp}; @@ -217,3 +217,89 @@ fn a_cancelled_export_pdf_picker_writes_nothing() { assert!(h.state().message.is_none()); assert!(h.state_mut().session.take_ui_requests().is_empty()); } + +/// Options › Formulas: Maximum Iterations and Maximum Change, with the checkbox that enables them. +#[test] +fn options_sets_maximum_iterations_and_change_only_while_iteration_is_on() { + use egui::accesskit::Role; + let mut session = Session::new(); + session.new_workbook(); + // Tall enough that the Formulas section is on screen without scrolling. + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 1400.0)).build_ui_state( + |ui, app: &mut SheetApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + SheetApp::new(session, Services::default()), + ); + h.run_steps(4); + h.state_mut().run("file.options", serde_json::json!({})).unwrap(); + h.run_steps(3); + let calc = |h: &egui_kittest::Harness<'static, SheetApp>| h.state().session.active().unwrap().wb.calc.clone(); + // The text field on the same row as `label`. + let field = |h: &egui_kittest::Harness<'static, SheetApp>, label: &str| { + let l = h.get_by_label(label).rect(); + h.get_all_by_role(Role::TextInput).map(|n| n.rect()).find(|r| r.left() > l.left() && (r.center().y - l.center().y).abs() < 8.0).unwrap() + }; + let disabled = |h: &egui_kittest::Harness<'static, SheetApp>, rect: egui::Rect| { + h.get_all_by_role(Role::TextInput).find(|n| n.rect() == rect).unwrap().accesskit_node().is_disabled() + }; + let retype = |h: &mut egui_kittest::Harness<'static, SheetApp>, rect: egui::Rect, text: &str| { + h.input_mut().events.push(egui::Event::PointerMoved(rect.center())); + h.run_steps(1); + h.get_all_by_role(Role::TextInput).find(|n| n.rect() == rect).unwrap().click(); + h.run_steps(2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.run_steps(1); + for c in text.chars() { + h.input_mut().events.push(egui::Event::Text(c.to_string())); + h.run_steps(2); + } + h.key_press(egui::Key::Enter); + h.run_steps(2); + }; + + // Iteration is off: the two fields show the defaults, greyed out. + let (iterations, change) = (field(&h, "Maximum Iterations:"), field(&h, "Maximum Change:")); + assert!(disabled(&h, iterations) && disabled(&h, change), "the fields are disabled while iteration is off"); + assert_eq!(h.get_all_by_role(Role::TextInput).find(|n| n.rect() == iterations).unwrap().value().as_deref(), Some("100")); + assert_eq!(h.get_all_by_role(Role::TextInput).find(|n| n.rect() == change).unwrap().value().as_deref(), Some("0.001")); + + h.get_by_role_and_label(Role::CheckBox, "Enable iterative calculation").click(); + h.run_steps(3); + assert!(calc(&h).iterative); + let (iterations, change) = (field(&h, "Maximum Iterations:"), field(&h, "Maximum Change:")); + assert!(!disabled(&h, iterations) && !disabled(&h, change)); + + retype(&mut h, iterations, "500"); + assert_eq!(calc(&h).max_iterations, 500); + retype(&mut h, change, "0.01"); + assert_eq!(calc(&h).max_change, 0.01); + assert!(h.state().message.is_none()); + + // Out-of-range entries are refused with a message and change nothing (the command alone would clamp them). + for bad in ["0", "32768", "12.5", "lots"] { + retype(&mut h, iterations, bad); + assert_eq!(calc(&h).max_iterations, 500, "{bad}"); + assert!(h.state_mut().message.take().is_some(), "{bad} is reported"); + h.run_steps(2); + } + retype(&mut h, iterations, "32767"); + assert_eq!(calc(&h).max_iterations, 32767); + for bad in ["-1", "x"] { + retype(&mut h, change, bad); + assert_eq!(calc(&h).max_change, 0.01, "{bad}"); + assert!(h.state_mut().message.take().is_some(), "{bad} is reported"); + h.run_steps(2); + } + retype(&mut h, change, "0"); + assert_eq!(calc(&h).max_change, 0.0); + + // Undo puts the old value back in the field. + h.state_mut().run("edit.undo", serde_json::json!({})).unwrap(); + h.run_steps(3); + assert_eq!(calc(&h).max_change, 0.01); + let change = field(&h, "Maximum Change:"); + assert_eq!(h.get_all_by_role(Role::TextInput).find(|n| n.rect() == change).unwrap().value().as_deref(), Some("0.01")); +} diff --git a/docs/function-parity.md b/docs/function-parity.md index 760ffd6ac3..7b2f5f03ea 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (USDOLLAR added as a compatibility function; MIRR, COUNTIF and INDIRECT edge cases match Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-15 · **Change:** minor (iterative calculation settings in File › Options) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -79,7 +79,7 @@ PIVOTBY, REGEXTEST…) are all present. | Implicit intersection `@`, legacy CSE array formulas | done; legacy array range fill (PR #156) | | Trim references `A1:.E10` (trailing), `A1.:E10` (leading), `A1.:.E10` (both), also on whole rows/columns (`A:.A`) | done; evaluated as `TRIMRANGE(range, code, code)`, adjusted on insert/delete; files store `_xlfn.TRIMRANGE(range,code,code)` (Excel's own file spelling unconfirmed in public docs), `_xlfn._TRO_ALL/_TRO_LEADING/_TRO_TRAILING(...)` read too | | LET, LAMBDA, named LAMBDAs, ISOMITTED, MAP/REDUCE/SCAN/BYROW/BYCOL/MAKEARRAY | done | -| Iterative calculation for circular references | done: with Enable Iterative Calculation on (`formulas.calculationOptions` `{iterative, maxIterations, maxChange}`, saved as ``), loops are evaluated again from their previous values (blank = 0) until no cell on a loop changes by Maximum Change or Maximum Iterations is reached; off, cycles give `#CIRC!`. Differences: no Options dialog yet (command only); GridCraft recalculates on open, so a loop that never settles (`=A1+1`) advances again each time the file is opened | +| Iterative calculation for circular references | done: with Enable Iterative Calculation on (`formulas.calculationOptions` `{iterative, maxIterations, maxChange}`, saved as ``), loops are evaluated again from their previous values (blank = 0) until no cell on a loop changes by Maximum Change or Maximum Iterations is reached; off, cycles give `#CIRC!`. Set in File › Options › Formulas (Enable iterative calculation, Maximum Iterations 1 to 32767, Maximum Change 0 or more; the two fields are disabled while iteration is off). Differences: GridCraft recalculates on open, so a loop that never settles (`=A1+1`) advances again each time the file is opened | | 15-significant-digit comparison semantics | **missing** (#57: `=0.1+0.2=0.3` is FALSE) | | External workbook references (`[Book.xlsx]Sheet1!A1`) | parsed; links dropped on open, no update | | Localized function names and `;` separators | **missing** (#45); en-US only | @@ -108,6 +108,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | Iterative calculation: Maximum Iterations and Maximum Change are set in File › Options › Formulas (dropped "no Options dialog yet") | | 2026-10-12 | minor | USDOLLAR added as a Compatibility function (not on Microsoft's list, so the counts don't change); MIRR with no positive flow is -1, COUNTIF's bare `"<="` / `">="` skip empty strings, INDIRECT with `a1` = FALSE gives `#REF!` for A1-style text (#738) | | 2026-10-11 | minor | TEXT / number formats fixed against Excel-cached results: fraction literals and zero fractions, `0` denominators zero padded, `?` pads the thousands separator, booleans use the text section (226 mismatches → 0 in 352 TEXT cells) | | 2026-10-11 | minor | Iterative calculation: circular references iterate up to Maximum Iterations / Maximum Change when enabled | diff --git a/docs/gaps.md b/docs/gaps.md index 03e8672c52..61b5f74220 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-15 · **Change:** minor (§6: Maximum Iterations and Maximum Change in File › Options) · **Target:** Microsoft Excel (Microsoft 365) Every known shortfall against Excel, one entry each, ranked by user impact (what blocks someone using GridCraft for real work first). This is the work list: pick from the top, check the open @@ -62,7 +62,8 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- - **Missing:** 15-significant-digit equality; spill updates after structural edits and cleared blockers; 3-D span deletion; qualified names after sheet rename. - **Evidence:** #57, #133, #139, #140, #134; PRs #148, #155, #156, #157. Iterative calculation - for circular references landed (no Options dialog yet). + for circular references landed, set in File › Options › Formulas (Maximum Iterations, Maximum + Change). - **Impact:** wrong numbers are worse than missing features. - **Estimate:** 10–15 h. [function-parity.md](function-parity.md#formula-language) @@ -168,6 +169,7 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | §6: Maximum Iterations and Maximum Change in File › Options › Formulas; dropped the stale "no Options dialog yet" | | 2026-10-12 | minor | rows auto-fit oversized fonts on open and whole-row/column formatting (#833) | | 2026-10-12 | minor | Page Layout view: dragging margins on the rulers (#792) removed from the §14 remainder | | 2026-10-12 | minor | §13: Forecast Sheet Forecast Start (hindcast) landed (#785) | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 22e221e698..34b6ed1e4c 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (horizontal scrollbar in the sheet tab bar, #840) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-15 · **Change:** minor (File › Options › Formulas: Maximum Iterations and Maximum Change) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -26,7 +26,7 @@ Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes | Task panes (Format Chart, Comments, Watch, Selection, PivotTable fields) | yes | yes | element-level Format pane for shapes and pictures missing | | Backstage (File) view: Info, New with templates, Open recent, Export, Options | yes | partial | Options dialog and template gallery missing | | Dark mode, system appearance | yes | yes | done (10-09); follows the system on first run, title-bar switch and File › Display theme (#194, #239) | -| Backstage (File) view: Info, New with templates, Open recent, Export, Options | yes | yes (#174, 10-11): the File tab opens a full-window view (Esc or the back arrow return) with Home, New (blank + the three samples), Open (recent + Browse; the recent list tells when each workbook was last changed, 10-12), Info (location, properties, Protect Workbook), Save, Save As, Print (page preview, active sheet or workbook, fit to page, Page Setup), Export (PDF, CSV, web page), Close and Options (`file.options`: theme, language, formula bar, AutoSave, calculation) | no Account, Share or template gallery beyond the samples; Options applies at once (no OK/Cancel dialog) and holds only the settings GridCraft has | +| Backstage (File) view: Info, New with templates, Open recent, Export, Options | yes | yes (#174, 10-11): the File tab opens a full-window view (Esc or the back arrow return) with Home, New (blank + the three samples), Open (recent + Browse; the recent list tells when each workbook was last changed, 10-12), Info (location, properties, Protect Workbook), Save, Save As, Print (page preview, active sheet or workbook, fit to page, Page Setup), Export (PDF, CSV, web page), Close and Options (`file.options`: theme, language, formula bar, AutoSave, calculation mode and iterative calculation with Maximum Iterations and Maximum Change, 10-15) | no Account, Share or template gallery beyond the samples; Options applies at once (no OK/Cancel dialog) and holds only the settings GridCraft has | | Dark mode, system appearance | yes | yes | done (10-09) | | Page Layout view (paper view with rulers and header editing) | yes | yes (#329, 10-11): per-sheet view saved in XLSX, pages with margins and gaps on the print pagination, "Click to add data" pages, rulers (inches or centimetres), headers and footers drawn and edited in place with a Header & Footer tab | margins drag on the rulers (#792, with header and footer margins); header/footer pictures, Different First Page / Odd & Even missing (1–3 h) | | Multiple windows per workbook | yes | no (#162) | see [hardware-parity.md](hardware-parity.md) | @@ -161,6 +161,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | File › Options › Formulas: Maximum Iterations (1 to 32767) and Maximum Change (0 or more) under Enable iterative calculation, disabled while it is off; invalid entries are refused with a message | | 2026-10-12 | minor | The File view's recent list tells when each workbook was last changed, read off the UI thread | | 2026-10-13 | minor | Horizontal scrollbar in the sheet tab bar: drag, track click pages, frozen columns excluded from the page (#840) | | 2026-10-12 | minor | Ctrl+wheel zoom steps along a ladder of percentages about 20% apart that never skips 100%, gliding to each step with animations on, so a notch out undoes a notch in (it drifted: 100 → 122 → 101); Excel steps 15 points, see decisions.md | From 6db94d154a14697cb8a0d3b85c77f8a5c4c33efb Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Thu, 15 Oct 2026 05:19:32 -0500 Subject: [PATCH 008/687] Calc: INDEX with one index on a 2-D reference is #REF!, as in Excel INDEX(A1:C3,2) returned the whole second row. Excel gives #REF! when a reference has several rows and columns and the column argument is omitted, so the error also carries into ranges built from it (A1:INDEX(A1:C3,2)), position arrays (INDEX(A1:C3,{1;2})) and outer functions. An empty column argument (INDEX(A1:C3,2,)) still gives the row, and arrays are unchanged: INDEX({1,2;3,4},2) is the row. Observed in Excel that a column array with one index, INDEX({10;20;30},2), is a 1x1 array (TYPE 64), which is what calc already returned; the new test pins it. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 4 ++- crates/calc/src/eval.rs | 8 ++++- crates/calc/src/tests.rs | 75 ++++++++++++++++++++++++++++++++++++++++ docs/function-parity.md | 3 +- 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..3292b595fe 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-15 · **Change:** minor (INDEX with one index on a 2-D reference is #REF! as in Excel) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-15 | Calc: `INDEX(A1:C3,2)` (one index on a reference of several rows and columns) is `#REF!` as in Excel, instead of the whole row; `INDEX(A1:C3,2,)` still gives the row | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | @@ -193,6 +194,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | Progress log: INDEX with one index on a 2-D reference is #REF! | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/crates/calc/src/eval.rs b/crates/calc/src/eval.rs index 0f9cfb03db..1f58c50be4 100644 --- a/crates/calc/src/eval.rs +++ b/crates/calc/src/eval.rs @@ -1251,7 +1251,13 @@ impl<'h> Evaluator<'h> { let area = usize::try_from((area_n as u64).saturating_sub(1)).ok(); let Some(a) = area.and_then(|i| areas.get(i)).copied() else { return err(CellError::Ref) }; let r = a.range; - // A single row or column with one index picks along it. + // A single row or column with one index picks along it. An area with several rows + // and columns needs both: `INDEX(A1:C3,2)` is #REF! in Excel (an empty third + // argument, `INDEX(A1:C3,2,)`, still means the whole row). Arrays differ: + // `INDEX({1,2;3,4},2)` is the row. + if two_args && r.height() > 1 && r.width() > 1 { + return err(CellError::Ref); + } let (row, col) = if two_args && r.height() == 1 { (1, row) } else { (row, col) }; if row as u64 > r.height() as u64 || col as u64 > r.width() as u64 { return err(CellError::Ref); diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de78..5abedec607 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -172,6 +172,81 @@ fn index_area_number_overflow() { } } +/// INDEX with one index, as observed in Excel: a 2-D reference needs both indexes (#REF!), a +/// column array gives a 1×1 array (TYPE 64), a row array or 1-D reference gives one value. +#[test] +fn index_with_one_index() { + let mut t = T::new(); + for (i, v) in [10, 20, 30].iter().enumerate() { + t.set(&format!("A{}", i + 1), &v.to_string()); + } + let mut k = 1; + for r in 5..=7 { + for col in ["A", "B", "C"] { + t.set(&format!("{col}{r}"), &k.to_string()); + k += 1; + } + } + for (row, (formula, expected)) in [ + // Column arrays keep Excel's 1×1 array; row arrays and explicit columns give a value. + ("=INDEX({10;20;30},2)", 20.0), + ("=TYPE(INDEX({10;20;30},2))", 64.0), + ("=TYPE(INDEX(SEQUENCE(3),2))", 64.0), + ("=TYPE(INDEX({10;20;30},2,1))", 1.0), + ("=TYPE(INDEX({10,20,30},2))", 1.0), + ("=TYPE(INDEX(INDEX({10;20;30},2),1))", 1.0), + ("=SUM(INDEX({10;20;30},2))", 20.0), + // 1-D references with one index are single-cell references. + ("=TYPE(INDEX(A1:A3,2))", 1.0), + ("=SUM(INDEX(A1:A3,1):INDEX(A1:A3,3))", 60.0), + ("=SUM(INDEX((A1:A3,A5:C7),2))", 20.0), + // A 2-D array with one index is the row. + ("=SUM(INDEX({1,2;3,4},2))", 7.0), + ("=SUM(INDEX({1,2;3,4},0))", 10.0), + // A 2-D reference with an empty (not omitted) column argument is the row. + ("=SUM(INDEX(A5:C7,2,))", 15.0), + ("=SUM(INDEX(A5:C7,2,,1))", 15.0), + ("=COLUMNS(INDEX((A1:A3,A5:C7),2,,2))", 3.0), + ("=INDEX(A5:C7,2,2)", 5.0), + // A 2-D reference with one index is #REF!, and the error carries through. + ("=TYPE(INDEX(A5:C7,2))", 16.0), + ("=ISREF(INDEX(A5:C7,2))", 0.0), + ] + .iter() + .enumerate() + { + let at = format!("J{}", row * 4 + 1); + t.set(&at, formula); + let got = match t.get(&at) { + Value::Bool(b) => f64::from(u8::from(b)), + v => v.as_f64().unwrap_or_else(|| panic!("{formula} = {v:?}")), + }; + assert_eq!(got, *expected, "{formula}"); + } + for formula in [ + "=INDEX(A5:C7,2)", + "=INDEX(A5:C7,0)", + "=INDEX(A5:C7,1.9)", + "=INDEX(A5:C7,)", + "=INDEX(A5:B6,1)", + "=INDEX(A5:C7,2)+1", + "=SUM(INDEX(A5:C7,{1;2}))", + "=ROWS(INDEX(INDEX(A5:C7,2),3))", + "=SUM(A5:INDEX(A5:C7,2))", + "=SUM(INDEX(A5:C7,2):C7)", + ] { + t.set("L1", formula); + assert_eq!(t.get("L1"), Value::Error(CellError::Ref), "{formula}"); + } + t.set("L1", "=INDEX(A5:C7,{1;2})"); + assert_eq!([t.get("L1"), t.get("L2")], [Value::Error(CellError::Ref), Value::Error(CellError::Ref)]); + t.set("L1", "=IFERROR(INDEX(A5:C7,2),\"e\")"); + assert_eq!(t.get("L1"), Value::from("e")); + // A bad position is still #VALUE!. + t.set("L1", "=INDEX(A5:C7,\"x\")"); + assert_eq!(t.get("L1"), Value::Error(CellError::Value)); +} + #[test] fn errors_and_cycles() { let mut t = T::new(); diff --git a/docs/function-parity.md b/docs/function-parity.md index 760ffd6ac3..12bef8ee9f 100644 --- a/docs/function-parity.md +++ b/docs/function-parity.md @@ -1,6 +1,6 @@ # Function parity: worksheet functions and formulas -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (USDOLLAR added as a compatibility function; MIRR, COUNTIF and INDIRECT edge cases match Excel 365) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-15 · **Change:** minor (INDEX with one index on a reference of several rows and columns is #REF! as in Excel) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Excel's worksheet functions, category by category, against GridCraft's function library. Part of [target-app-parity.md](target-app-parity.md); gaps feed [gaps.md](gaps.md). @@ -108,6 +108,7 @@ workbooks; results checked in as data, not Excel files) and fixing what it finds | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | INDEX with only a row index on a reference of several rows and columns (`INDEX(A1:C3,2)`, `INDEX(A1:C3,0)`) is `#REF!`, as observed in Excel, so it also fails as a range endpoint and inside other functions; an empty column argument (`INDEX(A1:C3,2,)`) still gives the row. Checked that a column array with one index stays a 1×1 array (`TYPE` 64), which is what Excel returns | | 2026-10-12 | minor | USDOLLAR added as a Compatibility function (not on Microsoft's list, so the counts don't change); MIRR with no positive flow is -1, COUNTIF's bare `"<="` / `">="` skip empty strings, INDIRECT with `a1` = FALSE gives `#REF!` for A1-style text (#738) | | 2026-10-11 | minor | TEXT / number formats fixed against Excel-cached results: fraction literals and zero fractions, `0` denominators zero padded, `?` pads the thousands separator, booleans use the text section (226 mismatches → 0 in 352 TEXT cells) | | 2026-10-11 | minor | Iterative calculation: circular references iterate up to Maximum Iterations / Maximum Change when enabled | From 672c102eb2eb375b3b2752f1e446147aebd522dc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 17 Oct 2026 15:12:02 -0700 Subject: [PATCH 009/687] Calc: a LAMBDA that calls itself without end answers #NUM! instead of overflowing the stack The evaluator stopped at MAX_DEPTH (400) nested eval calls. A LAMBDA call takes one of those levels but several stack frames (eval, eval_inner, call, builtin, apply_lambda, call_lambda_values), so on the 1 MiB main thread of the Windows build `=LET(f,LAMBDA(g,x,g(g,x)),f(f,1))` overflowed the stack before it reached the limit, and the process ended. eval now also stops once a formula has used 512 KiB of stack below its first eval. Both limits answer #NUM!, the error Excel documents for too many recursive calls, and a defined name that holds the recursion passes that error on instead of answering #NAME?. Fixes #1662 --- crates/calc/src/eval.rs | 37 +++++++++++++++++++++++++++++-------- crates/calc/src/tests.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/crates/calc/src/eval.rs b/crates/calc/src/eval.rs index 0f9cfb03db..2bbedd551a 100644 --- a/crates/calc/src/eval.rs +++ b/crates/calc/src/eval.rs @@ -49,6 +49,18 @@ fn err(e: CellError) -> Ev { Ev::V(Value::Error(e)) } +/// Native stack, in bytes, that one formula's evaluation may use below its first `eval`. Past it, +/// `eval` answers `#NUM!`. One level of `MAX_DEPTH` can take several stack frames. The Windows main +/// thread and the web build have 1 MiB of stack. +const STACK_BUDGET: usize = 512 * 1024; + +/// The address of a local variable, which shows how far down the native stack this call is. +#[inline(always)] +fn stack_position() -> usize { + let marker = 0u8; + std::hint::black_box(&marker) as *const u8 as usize +} + /// What the evaluator needs from its surroundings. pub trait Host { fn workbook(&self) -> &Workbook; @@ -84,6 +96,8 @@ pub struct Evaluator<'h> { pub at: CellRef, env: Vec<(String, Ev)>, depth: usize, + /// `stack_position()` at the outermost `eval`. + stack_top: usize, } struct FnCtx<'a, 'h> { @@ -121,14 +135,14 @@ impl Ctx for FnCtx<'_, '_> { _ => None, }) .collect(); - let mut ev = Evaluator { host: &mut *self.ev.host, sheet: area.sheet, at, env, depth: self.ev.depth }; + let mut ev = Evaluator { host: &mut *self.ev.host, sheet: area.sheet, at, env, depth: self.ev.depth, stack_top: self.ev.stack_top }; Some(matches!(ev.value(&shifted).scalar().to_bool(), Ok(true))) } } impl<'h> Evaluator<'h> { pub fn new(host: &'h mut dyn Host, sheet: usize, at: CellRef) -> Self { - Evaluator { host, sheet, at, env: Vec::new(), depth: 0 } + Evaluator { host, sheet, at, env: Vec::new(), depth: 0, stack_top: 0 } } /// Evaluates to a value (references are dereferenced; multi-cell → array). @@ -205,10 +219,16 @@ impl<'h> Evaluator<'h> { } pub fn eval(&mut self, e: &Expr) -> Ev { + let here = stack_position(); + if self.depth == 0 { + self.stack_top = here; + } else if self.stack_top.abs_diff(here) > STACK_BUDGET { + return err(CellError::Num); + } self.depth += 1; if self.depth > MAX_DEPTH { self.depth -= 1; - return err(CellError::Calc); + return err(CellError::Num); } let r = self.eval_inner(e); self.depth -= 1; @@ -492,7 +512,7 @@ impl<'h> Evaluator<'h> { } _ => { // A long chain such as A1+A2+…+A500 nests to the left: fold it in a loop instead - // of one level of recursion per operator (past the depth limit it was #CALC!). + // of one level of recursion per operator (past the depth limit it was an error). let mut rest = vec![(op, b)]; let mut left = a; while let Expr::Binary(o, l, r) = left @@ -801,10 +821,11 @@ impl<'h> Evaluator<'h> { fn builtin(&mut self, name: &str, args: &[Expr]) -> Ev { let Some(spec) = gridcraft_functions::lookup(name) else { // A defined name holding a LAMBDA, or a LET-bound lambda. - if let Ev::L(l) = self.name(name) { - return self.apply_lambda(&l, args); - } - return err(CellError::Name); + return match self.name(name) { + Ev::L(l) => self.apply_lambda(&l, args), + Ev::V(Value::Error(x)) => err(x), + _ => err(CellError::Name), + }; }; if args.len() < spec.min_args || args.len() > spec.max_args { return err(CellError::Value); diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 196c57de78..5cbbd5509c 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -643,6 +643,38 @@ fn lambda_argument_count() { assert_eq!(t.get("A9"), Value::Bool(true)); } +#[test] +fn a_lambda_that_calls_itself_without_end_is_num_on_a_small_stack() { + // 1 MiB is the main thread's stack on Windows, where commands, recalculation and file open run. + let handle = std::thread::Builder::new() + .stack_size(1 << 20) + .spawn(|| { + let mut t = T::new(); + t.wb.names.push(gridcraft_model::DefinedName { + name: "LOOP".into(), + scope: None, + formula: "LAMBDA(n,LOOP(n))".into(), + comment: String::new(), + hidden: false, + }); + t.calc.rebuild(&t.wb); + let endless = "=LET(f,LAMBDA(g,x,g(g,x)),f(f,1))"; + t.set("A1", endless); + t.set("A2", "=LOOP(1)"); + t.set("A3", "=LET(F,LAMBDA(me,n,IF(n<=0,0,1+SUM(MAP({1},LAMBDA(v,me(me,n-1)))))),F(F,100000))"); + t.set("A4", "=LET(F,LAMBDA(me,n,IF(n<=0,0,1+me(me,n-1))),F(F,50))"); + let evaluated = crate::evaluate(&t.wb, 0, c("B1"), endless); + [t.get("A1"), t.get("A2"), t.get("A3"), evaluated, t.get("A4")] + }) + .expect("spawn"); + let [endless, endless_name, through_map, evaluated, countdown] = handle.join().expect("recursion must not overflow the stack"); + assert_eq!(endless, Value::Error(CellError::Num)); + assert_eq!(endless_name, Value::Error(CellError::Num)); + assert_eq!(through_map, Value::Error(CellError::Num)); + assert_eq!(evaluated, Value::Error(CellError::Num)); + assert_eq!(countdown, Value::Number(50.0)); +} + #[test] fn names_and_sheets() { let mut t = T::new(); From 09973e7831ecdb03f8ee71ef9a3841cd26c6547f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 17 Oct 2026 15:12:19 -0700 Subject: [PATCH 010/687] Contributors: add dotCooCoo (Robert Lee) --- contributors/people.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/contributors/people.toml b/contributors/people.toml index d522b84e0b..96bead93ec 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -65,3 +65,7 @@ sync_github_name = true [people.Silvertree2010] display_name = "Netter" sync_github_name = false + +[people.dotCooCoo] +real_name = "Robert Lee" +sync_github_name = true From d0fdf948db4ad425b3fe19ec8ad8ee035d792059 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 18 Oct 2026 13:09:44 -0700 Subject: [PATCH 011/687] Calc: a recursive LAMBDA stops at a fixed count of 140 calls A LAMBDA recursion answered #NUM! once the formula had used 512 KiB of native stack, so the depth it reached depended on frame sizes. On a Windows release build the deepest countdown was F(F,94), where 0.7.0 reached F(F,131). The evaluator now counts the LAMBDA calls a formula has in progress, in call_lambda_values, which direct calls, defined names, MAP, REDUCE, SCAN, BYROW, BYCOL and MAKEARRAY all go through. The 141st call answers #NUM!. The countdown F(F,131), the deepest that the 400-level MAX_DEPTH allows, has 132 calls in progress and still runs. The stack check stays as a guard. Its budget rises to 896 KiB, which leaves 128 KiB of a 1 MiB thread for the frames above the formula. a_recursive_lambda_stops_at_the_same_call_on_any_stack_size runs recursions on each side of the limit on a 1 MiB and a 64 MiB thread and expects the same results. The 1 MiB test now runs the countdown F(F,131). --- crates/calc/src/eval.rs | 31 ++++++++++++++++++++++++++----- crates/calc/src/tests.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/crates/calc/src/eval.rs b/crates/calc/src/eval.rs index 2bbedd551a..bc60c5b7a3 100644 --- a/crates/calc/src/eval.rs +++ b/crates/calc/src/eval.rs @@ -49,10 +49,16 @@ fn err(e: CellError) -> Ev { Ev::V(Value::Error(e)) } +/// LAMBDA calls that one formula may have in progress at once, counted the same way on every build. +/// The next call answers `#NUM!`. A countdown from 131, the deepest that `MAX_DEPTH` allows, has 132 +/// calls in progress, so it still runs. +const MAX_LAMBDA_CALLS: usize = 140; + /// Native stack, in bytes, that one formula's evaluation may use below its first `eval`. Past it, -/// `eval` answers `#NUM!`. One level of `MAX_DEPTH` can take several stack frames. The Windows main -/// thread and the web build have 1 MiB of stack. -const STACK_BUDGET: usize = 512 * 1024; +/// `eval` answers `#NUM!`. It guards the stack and leaves 128 KiB of a 1 MiB thread for the frames +/// above the formula. A cell that another cell's formula computes on demand gets its own `Evaluator`, +/// which starts its own budget. +const STACK_BUDGET: usize = 896 * 1024; /// The address of a local variable, which shows how far down the native stack this call is. #[inline(always)] @@ -96,6 +102,8 @@ pub struct Evaluator<'h> { pub at: CellRef, env: Vec<(String, Ev)>, depth: usize, + /// LAMBDA calls in progress. + calls: usize, /// `stack_position()` at the outermost `eval`. stack_top: usize, } @@ -135,14 +143,22 @@ impl Ctx for FnCtx<'_, '_> { _ => None, }) .collect(); - let mut ev = Evaluator { host: &mut *self.ev.host, sheet: area.sheet, at, env, depth: self.ev.depth, stack_top: self.ev.stack_top }; + let mut ev = Evaluator { + host: &mut *self.ev.host, + sheet: area.sheet, + at, + env, + depth: self.ev.depth, + calls: self.ev.calls, + stack_top: self.ev.stack_top, + }; Some(matches!(ev.value(&shifted).scalar().to_bool(), Ok(true))) } } impl<'h> Evaluator<'h> { pub fn new(host: &'h mut dyn Host, sheet: usize, at: CellRef) -> Self { - Evaluator { host, sheet, at, env: Vec::new(), depth: 0, stack_top: 0 } + Evaluator { host, sheet, at, env: Vec::new(), depth: 0, calls: 0, stack_top: 0 } } /// Evaluates to a value (references are dereferenced; multi-cell → array). @@ -963,10 +979,15 @@ impl<'h> Evaluator<'h> { } fn call_lambda_values(&mut self, l: &Lambda, bound: Vec<(String, Ev)>) -> Ev { + if self.calls >= MAX_LAMBDA_CALLS { + return err(CellError::Num); + } + self.calls += 1; let saved = std::mem::replace(&mut self.env, l.env.clone()); self.env.extend(bound); let r = self.eval(&l.body); self.env = saved; + self.calls -= 1; r } diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 99ef117ce7..d56bf0c136 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -662,7 +662,7 @@ fn a_lambda_that_calls_itself_without_end_is_num_on_a_small_stack() { t.set("A1", endless); t.set("A2", "=LOOP(1)"); t.set("A3", "=LET(F,LAMBDA(me,n,IF(n<=0,0,1+SUM(MAP({1},LAMBDA(v,me(me,n-1)))))),F(F,100000))"); - t.set("A4", "=LET(F,LAMBDA(me,n,IF(n<=0,0,1+me(me,n-1))),F(F,50))"); + t.set("A4", "=LET(F,LAMBDA(me,n,IF(n<=0,0,1+me(me,n-1))),F(F,131))"); let evaluated = crate::evaluate(&t.wb, 0, c("B1"), endless); [t.get("A1"), t.get("A2"), t.get("A3"), evaluated, t.get("A4")] }) @@ -672,7 +672,35 @@ fn a_lambda_that_calls_itself_without_end_is_num_on_a_small_stack() { assert_eq!(endless_name, Value::Error(CellError::Num)); assert_eq!(through_map, Value::Error(CellError::Num)); assert_eq!(evaluated, Value::Error(CellError::Num)); - assert_eq!(countdown, Value::Number(50.0)); + assert_eq!(countdown, Value::Number(131.0)); +} + +#[test] +fn a_recursive_lambda_stops_at_the_same_call_on_any_stack_size() { + let run = |stack: usize| { + std::thread::Builder::new() + .stack_size(stack) + .spawn(|| { + let mut wb = Workbook::new(); + wb.names.push(gridcraft_model::DefinedName { + name: "CNT".into(), + scope: None, + formula: "LAMBDA(n,IF(n=0,0,1+CNT(n-1)))".into(), + comment: String::new(), + hidden: false, + }); + // F(F,n) has n + 1 calls in progress at its deepest; W adds one more call per step. + let tail = |n: u32| format!("=LET(F,LAMBDA(me,n,IF(n=0,0,me(me,n-1))),F(F,{n}))"); + let wrapped = |n: u32| format!("=LET(W,LAMBDA(f,k,f(f,k)),F,LAMBDA(me,n,IF(n<=0,0,1+W(me,n-1))),F(F,{n}))"); + [tail(139), tail(140), wrapped(69), wrapped(70), "=CNT(131)".to_string()].map(|f| crate::evaluate(&wb, 0, c("B1"), &f)) + }) + .expect("spawn") + .join() + .expect("recursion must not overflow the stack") + }; + let small = run(1 << 20); + assert_eq!(small, [Value::Number(0.0), Value::Error(CellError::Num), Value::Number(69.0), Value::Error(CellError::Num), Value::Number(131.0)]); + assert_eq!(run(64 << 20), small); } #[test] From c01e46619a76ddce2d4a717c3b212681bc2c4314 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 20 Oct 2026 03:36:51 +0200 Subject: [PATCH 012/687] fix(linux): keep font-free GNOME Wayland controls scoped to packaging and build --- Cargo.lock | 50 +++++++++++++++++++ NOTICE | 31 ++++++++++++ apps/gridcraft/Cargo.toml | 4 ++ packaging/env.sh | 2 +- .../flatpak/ai.storyteller.gridcraft.yml | 1 + packaging/linux/package.sh | 2 + 6 files changed, 89 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 17ebf848cc..3f70c6725d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -230,6 +230,12 @@ dependencies = [ "x11rb", ] +[[package]] +name = "arrayref" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb" + [[package]] name = "arrayvec" version = "0.7.8" @@ -1513,6 +1519,7 @@ dependencies = [ "pollster 1.0.1", "rfd", "serde_json", + "winit", "winresource", ] @@ -3101,6 +3108,17 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sctk-adwaita" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6277f0217056f77f1d8f49f2950ac6c278c0d607c45f5ee99328d792ede24ec" +dependencies = [ + "log", + "smithay-client-toolkit 0.19.2", + "tiny-skia", +] + [[package]] name = "self_cell" version = "1.3.0" @@ -3360,6 +3378,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strict-num" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" + [[package]] name = "syn" version = "2.0.119" @@ -3467,6 +3491,31 @@ dependencies = [ "zune-jpeg", ] +[[package]] +name = "tiny-skia" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83d13394d44dae3207b52a326c0c85a8bf87f1541f23b0d143811088497b09ab" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "tiny-skia-path", +] + +[[package]] +name = "tiny-skia-path" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9e7fc0c2e86a30b117d0462aa261b72b7a99b7ebd7deb3a14ceda95c5bdc93" +dependencies = [ + "arrayref", + "bytemuck", + "strict-num", +] + [[package]] name = "tokio" version = "1.53.2" @@ -4400,6 +4449,7 @@ dependencies = [ "raw-window-handle", "redox_syscall 0.4.1", "rustix 0.38.44", + "sctk-adwaita", "smithay-client-toolkit 0.19.2", "smol_str", "tracing", diff --git a/NOTICE b/NOTICE index 1e3668b9d0..7f9e28769c 100644 --- a/NOTICE +++ b/NOTICE @@ -14,3 +14,34 @@ list (author, source, license) is in ATTRIBUTION.md. None: every bundled asset is original to GridCraft. Builds made with craft-fonts (CRAFT_FONTS_DIR) embed the fonts listed in ATTRIBUTION.md under the SIL Open Font License 1.1. + +Linux Wayland window decorations +------------------------------- +sctk-adwaita 0.10.1 (https://github.com/PolyMeilex/sctk-adwaita, +commit d5f343c45c04d4a026db57f76760819b7325db00) is used by winit on Linux +for vector window controls. Its title-font features are disabled: no Cantarell +font is embedded by this dependency and no font file is added to this repo. + +sctk-adwaita's MIT licence: + +MIT License + +Copyright (c) 2022 Bartłomiej Maryńczak + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/apps/gridcraft/Cargo.toml b/apps/gridcraft/Cargo.toml index cd8311b345..17bf162613 100644 --- a/apps/gridcraft/Cargo.toml +++ b/apps/gridcraft/Cargo.toml @@ -52,3 +52,7 @@ objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std [[test]] name = "mac_menu_appkit" harness = false + +[target.'cfg(target_os = "linux")'.dependencies] +# GNOME/Wayland client-side controls without a bundled title font. +winit = { version = "0.30", default-features = false, features = ["wayland-csd-adwaita-notitle"] } diff --git a/packaging/env.sh b/packaging/env.sh index bacb167e19..eaa02e1789 100644 --- a/packaging/env.sh +++ b/packaging/env.sh @@ -47,7 +47,7 @@ warn() { # Copy licence and readme files that exist into a package directory. copy_docs() { local dest="$1" f - for f in README.md LICENSE LICENSE-MIT LICENSE-APACHE COPYRIGHT; do + for f in README.md LICENSE LICENSE-MIT LICENSE-APACHE COPYRIGHT NOTICE; do if [ -f "$ROOT/$f" ]; then cp "$ROOT/$f" "$dest/"; fi done copy_font_licences "$dest" diff --git a/packaging/linux/flatpak/ai.storyteller.gridcraft.yml b/packaging/linux/flatpak/ai.storyteller.gridcraft.yml index 8bde5db6d9..d1360dc298 100644 --- a/packaging/linux/flatpak/ai.storyteller.gridcraft.yml +++ b/packaging/linux/flatpak/ai.storyteller.gridcraft.yml @@ -46,6 +46,7 @@ modules: - cargo --offline build --release --locked -p gridcraft -p gridcraft-cli - install -Dm755 target/release/gridcraft /app/bin/gridcraft - install -Dm755 target/release/gridcraft-cli /app/bin/gridcraft-cli + - install -Dm644 NOTICE /app/share/doc/gridcraft/NOTICE - install -Dm644 packaging/linux/ai.storyteller.gridcraft.desktop /app/share/applications/ai.storyteller.gridcraft.desktop - install -Dm644 packaging/linux/ai.storyteller.gridcraft.mime.xml /app/share/mime/packages/ai.storyteller.gridcraft.xml - mkdir -p /app/share/icons && cp -R assets/app-icon/hicolor /app/share/icons/ diff --git a/packaging/linux/package.sh b/packaging/linux/package.sh index 9fde2c591a..402da5b8e6 100755 --- a/packaging/linux/package.sh +++ b/packaging/linux/package.sh @@ -102,6 +102,8 @@ if has appimage; then APPDIR="$WORK/GridCraft.AppDir" cp -R "$STAGE" "$APPDIR" mv "$APPDIR/usr/share/doc" "$WORK/doc-unused" + # Keep the runtime Adwaita MIT notice in the packaged AppImage. + install -Dm644 "$ROOT/NOTICE" "$APPDIR/usr/share/doc/gridcraft/NOTICE" ln -s usr/bin/gridcraft "$APPDIR/AppRun" cp "$HERE/$APP_ID.desktop" "$APPDIR/$APP_ID.desktop" cp "$ROOT/assets/app-icon/hicolor/256x256/apps/$APP_ID.png" "$APPDIR/$APP_ID.png" From acfd38f3092f4c91a2ff3cbe8d4aa0502b04d960 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:03:39 -0400 Subject: [PATCH 013/687] Engine: undo groups make several commands one undo step Session::grouped(label, f) runs f as one undo step on the active document. On success the group leaves exactly one undo entry holding the workbook from before the group; on error it restores the workbook, selection, undo and redo stacks as they were and returns the error. group_begin, group_end and group_rollback do the same across separate calls, for an action that runs one step per call. While a group is open on a document, undoable commands on it add no undo steps of their own, so a long group neither grows the history nor pushes older entries past the undo limit, and Undo and Redo are disabled there. Groups nest: the outermost one makes the step, and a failing inner group rolls back only its own changes. Closing a document drops its groups, and a command that panics drops the groups it opened. history.list reports whether a group is open. Insert Copied Cells folds its nested steps into one undo step by counting revisions; inside a group the nested steps push no entries, so it now leaves the history alone there instead of removing entries from before the group. --- crates/engine/src/cmd/inspect.rs | 10 +- crates/engine/src/cmd/mod.rs | 7 ++ crates/engine/src/cmd/sheet.rs | 8 +- crates/engine/src/lib.rs | 149 ++++++++++++++++++++++- crates/engine/src/tests.rs | 1 + crates/engine/src/tests/seams.rs | 203 +++++++++++++++++++++++++++++++ 6 files changed, 366 insertions(+), 12 deletions(-) create mode 100644 crates/engine/src/tests/seams.rs diff --git a/crates/engine/src/cmd/inspect.rs b/crates/engine/src/cmd/inspect.rs index f92b48bd9d..366d8e6e68 100644 --- a/crates/engine/src/cmd/inspect.rs +++ b/crates/engine/src/cmd/inspect.rs @@ -11,7 +11,7 @@ pub fn specs() -> Vec { cmd!(query "sheet.read", "Read Range", [], None, "{range?: \"A1:D10\" (default: used range), sheet?, formulas?: bool, formatted?: bool} → rows of values", has_doc, read), cmd!(query "cell.get", "Get Cell", [], None, "{cell?, sheet?} → value, formula, display text, style, comment, link", has_doc, cell_get), cmd!(query "selection.stats", "Selection Statistics", [], None, "{} → count, sum, average, min, max", has_doc, sel_stats), - cmd!(query "history.list", "Undo History", [], None, "{} → undo/redo labels", has_doc, history), + cmd!(query "history.list", "Undo History", [], None, "{} → undo/redo labels, grouping (an undo group is open)", has_doc, history), cmd!(query "app.commands", "List Commands", [], None, "{search?} → ids, labels, params", always, list_commands), ] } @@ -148,9 +148,11 @@ fn sel_stats(s: &mut Session, _: &Json) -> Result { fn history(s: &mut Session, _: &Json) -> Result { let d = s.doc()?; - Ok( - json!({"undo": d.undo.iter().rev().map(|e| e.label.clone()).collect::>(), "redo": d.redo.iter().rev().map(|e| e.label.clone()).collect::>()}), - ) + Ok(json!({ + "undo": d.undo.iter().rev().map(|e| e.label.clone()).collect::>(), + "redo": d.redo.iter().rev().map(|e| e.label.clone()).collect::>(), + "grouping": s.in_group(d.uid), + })) } fn list_commands(s: &mut Session, p: &Json) -> Result { diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 50865a2b7d..d8abfac61f 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -96,12 +96,19 @@ pub fn has_doc(s: &Session) -> std::result::Result<(), String> { } pub fn can_undo(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; + not_grouping(s)?; if s.active().is_some_and(|d| !d.undo.is_empty()) { Ok(()) } else { Err("nothing to undo".into()) } } pub fn can_redo(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; + not_grouping(s)?; if s.active().is_some_and(|d| !d.redo.is_empty()) { Ok(()) } else { Err("nothing to redo".into()) } } +/// Undo and Redo wait while an undo group is open on the active document (an action that spans +/// several calls is still running). +fn not_grouping(s: &Session) -> std::result::Result<(), String> { + if s.active().is_some_and(|d| s.in_group(d.uid)) { Err("finish or stop the running action first".into()) } else { Ok(()) } +} pub fn has_clipboard(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; if s.clipboard.is_some() { Ok(()) } else { Err("the clipboard is empty".into()) } diff --git a/crates/engine/src/cmd/sheet.rs b/crates/engine/src/cmd/sheet.rs index 7870dee4e9..c42b3abd40 100644 --- a/crates/engine/src/cmd/sheet.rs +++ b/crates/engine/src/cmd/sheet.rs @@ -554,11 +554,15 @@ fn insert_copied_cells(s: &mut Session, p: &Json) -> Result { } let revision = d.revision; let before = (d.wb.clone(), d.selection.clone()); + // Inside an undo group the nested steps add no undo entries of their own. + let nested_entries = !s.in_group(d.uid); let result = insert_copied_steps(s, &clip, sheet, at, shift, cut); let d = s.doc_mut()?; // The nested steps collapse into this command's single undo step. - for _ in 0..d.revision.saturating_sub(revision) { - d.undo.pop(); + if nested_entries { + for _ in 0..d.revision.saturating_sub(revision) { + d.undo.pop(); + } } match result { Ok((final_range, out)) => { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 258a1cb729..e8e3d73d6f 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -63,6 +63,36 @@ pub struct HistoryEntry { pub selection: Selection, } +/// An undo group opened with [`Session::group_begin`]. Hand it back to [`Session::group_end`] +/// (keep the changes as one undo step) or [`Session::group_rollback`] (undo them all). +#[derive(Debug, PartialEq, Eq)] +#[must_use = "an undo group stays open until it is ended or rolled back"] +pub struct GroupToken { + id: u64, + doc: u64, +} + +impl GroupToken { + /// The uid of the document the group belongs to. + pub fn doc(&self) -> u64 { + self.doc + } +} + +/// An open undo group: what runs on `doc` until it ends becomes one undo step. +#[derive(Clone, Debug)] +struct OpenGroup { + id: u64, + doc: u64, + label: String, + /// Undo depth when the group began; the group's own steps are cut back to it. + start: usize, + wb: Arc, + selection: Selection, + /// The redo stack when the group began (a rollback puts it back). + redo: Vec, +} + /// The clipboard: a block of cells (with formulas and styles) or marching-ants reference. #[derive(Clone, Debug)] pub struct Clipboard { @@ -256,6 +286,11 @@ pub struct Session { /// Review › Notes › Show All Notes: every note is displayed. An application display setting /// (like Excel's comment indicator option), so it's not saved in the file. pub show_all_notes: bool, + /// Open undo groups, innermost last. + groups: Vec, + next_group: u64, + /// Counts group begins and ends, so `execute` can tell that a command opened or closed one. + group_epoch: u64, } impl Session { @@ -292,7 +327,8 @@ impl Session { } pub fn close_document(&mut self, i: usize) { if i < self.docs.len() { - self.docs.remove(i); + let d = self.docs.remove(i); + self.groups.retain(|g| g.doc != d.uid); if self.active >= self.docs.len() { self.active = self.docs.len().saturating_sub(1); } @@ -313,7 +349,8 @@ impl Session { command_specs().iter().map(|c| c.info(self)).collect() } - /// Runs a command by id. Undoable commands record a history step when the workbook changed. + /// Runs a command by id. Undoable commands record a history step when the workbook changed, + /// unless an undo group is open on the document (the group then makes the step). /// Panics inside commands are caught and reported; the workbook is restored. pub fn execute(&mut self, id: &str, params: Json) -> Result { let spec = find_command(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; @@ -322,13 +359,15 @@ impl Session { } let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); let params = if params.is_null() { Json::Object(Default::default()) } else { params }; + let (first_group, group_epoch) = (self.next_group, self.group_epoch); let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, ¶ms))); let result = match result { Ok(r) => r, Err(p) => { let msg = p.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| p.downcast_ref::().cloned()).unwrap_or_else(|| "panic".into()); - // Restore the workbook as it was. + // Restore the workbook as it was, and drop the undo groups the command opened. + self.groups.retain(|g| g.id < first_group); if let Some((uid, wb, sel)) = &before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == *uid) { @@ -345,14 +384,18 @@ impl Session { self.journal.drain(..5_000); } } + // Inside an undo group, or when the command opened or closed one, the group owns the step. + let grouped = before.as_ref().is_some_and(|(uid, ..)| self.group_epoch != group_epoch || self.in_group(*uid)); if spec.undoable && let Some((uid, wb, sel)) = before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == uid) && !Arc::ptr_eq(&d.wb, &wb) { - d.undo.push(HistoryEntry { label: spec.label.trim_end_matches('…').to_string(), wb, selection: sel }); - if d.undo.len() > UNDO_LIMIT { - d.undo.remove(0); + if !grouped { + d.undo.push(HistoryEntry { label: spec.label.trim_end_matches('…').to_string(), wb, selection: sel }); + if d.undo.len() > UNDO_LIMIT { + d.undo.remove(0); + } } d.redo.clear(); d.revision += 1; @@ -360,6 +403,100 @@ impl Session { result } + /// Runs `f` as one undo step labelled `label` on the active document: whatever commands `f` + /// runs, one Undo brings back the workbook as it was before. If `f` fails, every change it made + /// is rolled back and its error is returned. Groups nest; the outermost one makes the step. + pub fn grouped(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result) -> Result { + let token = self.group_begin(label)?; + match f(self) { + Ok(r) => { + self.group_end(token)?; + Ok(r) + } + Err(e) => { + // The error from `f` is the one to report. A rollback fails only when the group + // has already ended, and then there is nothing left to undo. + let _ = self.group_rollback(token); + Err(e) + } + } + } + + /// Opens an undo group on the active document that can span several calls (for example a + /// debugger that runs one step per call). Until [`Session::group_end`] or + /// [`Session::group_rollback`], undoable commands on that document add no undo steps of their + /// own, and Undo and Redo are unavailable there. + pub fn group_begin(&mut self, label: &str) -> Result { + let d = self.doc()?; + let g = OpenGroup { + id: self.next_group, + doc: d.uid, + label: label.trim_end_matches('…').to_string(), + start: d.undo.len(), + wb: d.wb.clone(), + selection: d.selection.clone(), + redo: d.redo.clone(), + }; + let token = GroupToken { id: g.id, doc: g.doc }; + self.next_group = self.next_group.saturating_add(1); + self.group_epoch = self.group_epoch.wrapping_add(1); + self.groups.push(g); + Ok(token) + } + + /// Ends an undo group. If the workbook changed, the group becomes one undo step, unless it is + /// nested in another open group on the same document, which then owns the step. Groups opened + /// inside it and still open end with it. Returns whether the workbook changed. + pub fn group_end(&mut self, token: GroupToken) -> Result { + let g = self.take_group(&token)?; + let outer = self.in_group(g.doc); + let Some(d) = self.docs.iter_mut().find(|d| d.uid == g.doc) else { return Ok(false) }; + // Steps pushed directly while the group was open fold into the group's step. + d.undo.truncate(g.start); + let changed = !Arc::ptr_eq(&d.wb, &g.wb); + if changed && !outer { + d.undo.push(HistoryEntry { label: g.label, wb: g.wb, selection: g.selection }); + while d.undo.len() > UNDO_LIMIT { + d.undo.remove(0); + } + d.redo.clear(); + d.revision += 1; + } + Ok(changed) + } + + /// Ends an undo group by putting back the workbook, selection, undo and redo history as they + /// were when it began. + pub fn group_rollback(&mut self, token: GroupToken) -> Result<()> { + let g = self.take_group(&token)?; + let Some(d) = self.docs.iter_mut().find(|d| d.uid == g.doc) else { return Ok(()) }; + if !Arc::ptr_eq(&d.wb, &g.wb) { + d.wb = g.wb; + d.calc.rebuild(&d.wb); + } + d.selection = g.selection; + d.undo.truncate(g.start); + d.redo = g.redo; + d.revision += 1; + Ok(()) + } + + /// Whether an undo group is open on the document with this uid. + pub fn in_group(&self, doc: u64) -> bool { + self.groups.iter().any(|g| g.doc == doc) + } + + /// Removes the token's group and any group on the same document opened after it. + fn take_group(&mut self, token: &GroupToken) -> Result { + let ended = || EngineError::Other("That undo group has already ended.".into()); + let at = self.groups.iter().position(|g| g.id == token.id).ok_or_else(ended)?; + let mut later = self.groups.split_off(at).into_iter(); + let g = later.next().ok_or_else(ended)?; + self.groups.extend(later.filter(|o| o.doc != g.doc)); + self.group_epoch = self.group_epoch.wrapping_add(1); + Ok(g) + } + pub fn run(&mut self, id: &str, params: Json) -> std::result::Result { self.execute(id, params).map_err(|e| e.to_string()) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 2ac2906548..b5ac30d43f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3344,3 +3344,4 @@ fn csv_as_text_keeps_identifier_columns_as_written() { let saved = s.execute("file.saveBytes", json!({"format": "csv"})).unwrap(); assert_eq!(crate::io::base64_decode(saved["base64"].as_str().unwrap()).unwrap(), csv.replace('\n', "\r\n").as_bytes()); } +mod seams; diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs new file mode 100644 index 0000000000..aec2c85449 --- /dev/null +++ b/crates/engine/src/tests/seams.rs @@ -0,0 +1,203 @@ +//! Engine building blocks for multi-command actions such as macros: undo groups, the recording +//! buffer, document trust and chunked storage of workbook data in hidden names. + +use serde_json::json; + +use super::{s, v}; +use crate::{EngineError, Session}; + +fn undo_labels(s: &Session) -> Vec { + s.doc().unwrap().undo.iter().map(|e| e.label.clone()).collect() +} + +fn set(s: &mut Session, cell: &str, input: &str) { + s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap(); +} + +// ---------------------------------------------------------------- undo groups + +#[test] +fn grouped_commands_are_one_undo_step() { + let mut s = s(); + set(&mut s, "A1", "before"); + let r = s.grouped("Fill Three", |s| { + set(s, "A1", "1"); + set(s, "A2", "2"); + s.execute("home.bold", json!({"range": "A1:A2"}))?; + Ok(7) + }); + assert_eq!(r.unwrap(), 7); + assert_eq!(undo_labels(&s), ["Enter Cell", "Fill Three"]); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(v(&s, "A1").display(), "before"); + assert!(v(&s, "A2").is_empty()); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(v(&s, "A2").display(), "2"); + assert!(!s.in_group(s.doc().unwrap().uid)); +} + +#[test] +fn a_failing_group_rolls_everything_back() { + let mut s = s(); + set(&mut s, "B1", "base"); + set(&mut s, "A1", "undone"); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("selection.set", json!({"range": "C3"})).unwrap(); + let redo_before = s.doc().unwrap().redo.len(); + assert_eq!(redo_before, 1); + let r: crate::Result<()> = s.grouped("Doomed", |s| { + set(s, "B1", "changed"); + s.execute("selection.set", json!({"range": "D4"}))?; + set(s, "B2", "new"); + Err(EngineError::Other("step 3 failed".into())) + }); + assert!(r.unwrap_err().to_string().contains("step 3 failed")); + assert_eq!(v(&s, "B1").display(), "base"); + assert!(v(&s, "B2").is_empty()); + assert_eq!(s.doc().unwrap().selection.a1(), "C3"); + assert_eq!(undo_labels(&s), ["Enter Cell"]); + assert_eq!(s.doc().unwrap().redo.len(), redo_before, "the redo stack comes back too"); + // The recalculation follows the restored workbook. + set(&mut s, "C1", "=B1&\"!\""); + assert_eq!(v(&s, "C1").display(), "base!"); +} + +#[test] +fn groups_nest_and_the_outermost_makes_the_step() { + let mut s = s(); + let r = s.grouped("Outer", |s| { + set(s, "A1", "1"); + s.grouped("Inner", |s| { + set(s, "A2", "2"); + Ok(()) + })?; + // A failing inner group rolls back only its own changes; the outer one carries on. + let inner: crate::Result<()> = s.grouped("Inner fails", |s| { + set(s, "A3", "3"); + Err(EngineError::Other("no".into())) + }); + assert!(inner.is_err()); + assert!(v(s, "A3").is_empty()); + set(s, "A4", "4"); + Ok(()) + }); + r.unwrap(); + assert_eq!(undo_labels(&s), ["Outer"]); + assert_eq!(v(&s, "A2").display(), "2"); + s.execute("edit.undo", json!({})).unwrap(); + for c in ["A1", "A2", "A3", "A4"] { + assert!(v(&s, c).is_empty(), "{c}"); + } +} + +#[test] +fn an_unchanged_group_adds_no_step() { + let mut s = s(); + set(&mut s, "A1", "x"); + s.grouped("Look Around", |s| { + s.execute("selection.set", json!({"range": "B2"}))?; + s.execute("cell.get", json!({"cell": "A1"}))?; + Ok(()) + }) + .unwrap(); + assert_eq!(undo_labels(&s), ["Enter Cell"]); +} + +#[test] +fn a_group_spans_calls_and_blocks_undo_until_it_ends() { + let mut s = s(); + set(&mut s, "A1", "start"); + let token = s.group_begin("Debug Macro").unwrap(); + // Each step is its own call, as a debugger makes them. + set(&mut s, "A1", "step 1"); + set(&mut s, "A2", "step 2"); + let undo = s.execute("edit.undo", json!({})); + assert!(matches!(undo, Err(EngineError::Disabled(..))), "{undo:?}"); + assert!(matches!(s.execute("edit.redo", json!({})), Err(EngineError::Disabled(..)))); + assert_eq!(s.execute("history.list", json!({})).unwrap()["grouping"], json!(true)); + assert!(s.group_end(token).unwrap()); + assert_eq!(s.execute("history.list", json!({})).unwrap()["grouping"], json!(false)); + assert_eq!(undo_labels(&s), ["Enter Cell", "Debug Macro"]); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(v(&s, "A1").display(), "start"); + assert!(v(&s, "A2").is_empty()); +} + +#[test] +fn rolling_back_a_spanning_group_restores_the_start() { + let mut s = s(); + set(&mut s, "A1", "start"); + let token = s.group_begin("Debug Macro").unwrap(); + set(&mut s, "A1", "step 1"); + s.execute("home.insertSheet", json!({})).unwrap(); + s.group_rollback(token).unwrap(); + assert_eq!(v(&s, "A1").display(), "start"); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 1); + assert_eq!(undo_labels(&s), ["Enter Cell"]); +} + +#[test] +fn ending_an_outer_group_ends_the_inner_ones() { + let mut s = s(); + let outer = s.group_begin("Outer").unwrap(); + let inner = s.group_begin("Inner").unwrap(); + set(&mut s, "A1", "1"); + s.group_end(outer).unwrap(); + assert_eq!(undo_labels(&s), ["Outer"]); + assert!(s.group_end(inner).is_err(), "the inner group ended with its outer group"); + assert!(!s.in_group(s.doc().unwrap().uid)); +} + +#[test] +fn a_long_group_keeps_the_history_before_it() { + let mut s = s(); + for i in 0..100 { + set(&mut s, "A1", &i.to_string()); + } + let first = s.doc().unwrap().undo.first().map(|e| e.wb.clone()).unwrap(); + s.grouped("Many Steps", |s| { + for i in 0..250 { + set(s, "B1", &i.to_string()); + } + Ok(()) + }) + .unwrap(); + let d = s.doc().unwrap(); + assert_eq!(d.undo.len(), 100, "the undo limit still holds"); + assert_eq!(d.undo.last().map(|e| e.label.as_str()), Some("Many Steps")); + // Only the oldest entry made room for the group's one step. + assert!(!d.undo.iter().any(|e| std::sync::Arc::ptr_eq(&e.wb, &first))); + assert_eq!(d.undo.iter().filter(|e| e.label == "Enter Cell").count(), 99); +} + +#[test] +fn closing_a_document_drops_its_groups() { + let mut s = s(); + let token = s.group_begin("Debug Macro").unwrap(); + let uid = token.doc(); + s.close_document(s.active_index()); + assert!(!s.in_group(uid)); + assert!(s.group_end(token).is_err()); + assert!(matches!(s.group_begin("x"), Err(EngineError::NoDocument))); +} + +#[test] +fn a_group_owns_the_steps_of_commands_that_run_commands() { + // Insert Copied Cells runs nested steps and folds them into its own undo step; inside a group + // it must leave the history from before the group alone. + let mut s = s(); + set(&mut s, "A1", "kept"); + set(&mut s, "A5", "copied"); + s.execute("edit.copy", json!({"range": "5:5"})).unwrap(); + s.grouped("Insert Inside", |s| { + s.execute("selection.set", json!({"range": "A2"}))?; + s.execute("home.insertCopiedCells", json!({}))?; + Ok(()) + }) + .unwrap(); + assert_eq!(v(&s, "A2").display(), "copied"); + assert_eq!(undo_labels(&s), ["Enter Cell", "Enter Cell", "Insert Inside"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "A2").is_empty()); + assert_eq!(v(&s, "A1").display(), "kept"); +} From d4f1e61c994e80f860761c0a6a83bafaf199d2a6 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:04:33 -0400 Subject: [PATCH 014/687] Engine: a recording buffer of top-level commands, apart from the journal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A macro recorder needs more than a journal index: the journal is trimmed past 10,000 entries, it also holds the commands that other commands run (so playback would run them twice), and it keeps no selection, which relative references need. Session now tracks how deeply execute calls are nested. While recording, a buffer (record.rs) keeps each successful, journaled top-level call with the document uid, the active sheet (index and name) and the selection it ran in, then its id and parameters without secrets, as the journal stores them. Queries, failed commands and the command that starts recording are not kept. The buffer holds up to 100,000 steps and counts any it drops. Session::start_recording, stop_recording, is_recording and depth are the interface. Automate › Record Actions keeps recording through the journal as before. --- crates/engine/src/lib.rs | 44 ++++++++++++++- crates/engine/src/record.rs | 50 +++++++++++++++++ crates/engine/src/tests/seams.rs | 92 ++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/record.rs diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index e8e3d73d6f..03e710a450 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -18,6 +18,7 @@ pub mod outline; pub mod paper; pub mod pivot; pub mod pivot_calc; +pub mod record; pub mod sample; pub mod selection; pub mod tables; @@ -273,8 +274,13 @@ pub struct Session { pub last_find: Option, /// View › Show › Ruler is off (rulers in Page Layout view; an application option). pub hide_ruler: bool, - /// Journal position where action recording started. + /// Journal position where Automate › Record Actions started. pub recording: Option, + /// The recording buffer while a recorder runs ([`Session::start_recording`]): top-level + /// commands with the place they ran in, kept apart from the journal. + pub recorder: Option, + /// How many `execute` calls are running (0 between top-level commands). + depth: u32, /// Windows hidden with View › Hide. pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. @@ -360,7 +366,14 @@ impl Session { let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); let params = if params.is_null() { Json::Object(Default::default()) } else { params }; let (first_group, group_epoch) = (self.next_group, self.group_epoch); + // The recorder keeps top-level calls with the place they ran in. + let place = (self.depth == 0 && self.recorder.is_some()).then(|| match self.active() { + Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), + None => (0, 0, String::new(), Selection::default()), + }); + self.depth = self.depth.saturating_add(1); let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, ¶ms))); + self.depth = self.depth.saturating_sub(1); let result = match result { Ok(r) => r, Err(p) => { @@ -379,6 +392,13 @@ impl Session { } }; if spec.journal && result.is_ok() { + // A command that started recording is not part of the recording; one that stopped it + // finds no recorder. + if let Some((doc, sheet, sheet_name, selection)) = place + && let Some(rec) = self.recorder.as_mut() + { + rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params: cmd::journal_params(¶ms) }); + } self.journal.push((id.to_string(), cmd::journal_params(¶ms))); if self.journal.len() > 10_000 { self.journal.drain(..5_000); @@ -481,6 +501,28 @@ impl Session { Ok(()) } + /// Starts recording top-level commands into an empty buffer, replacing any recording in + /// progress. Queries, failed commands and commands run by other commands are not recorded. + pub fn start_recording(&mut self) { + self.recorder = Some(record::Recorder::default()); + } + + /// Stops recording and returns what was recorded (`None` when nothing was recording). + pub fn stop_recording(&mut self) -> Option { + self.recorder.take() + } + + /// Whether the recording buffer is collecting commands. + pub fn is_recording(&self) -> bool { + self.recorder.is_some() + } + + /// How many commands are running: 0 between top-level commands, 1 inside one, 2 inside a + /// command run by a command, and so on. + pub fn depth(&self) -> u32 { + self.depth + } + /// Whether an undo group is open on the document with this uid. pub fn in_group(&self, doc: u64) -> bool { self.groups.iter().any(|g| g.doc == doc) diff --git a/crates/engine/src/record.rs b/crates/engine/src/record.rs new file mode 100644 index 0000000000..dbbc1a8af7 --- /dev/null +++ b/crates/engine/src/record.rs @@ -0,0 +1,50 @@ +//! The recording buffer: the commands a person (or an agent) ran while recording, each with the +//! document, sheet and selection it ran in. +//! +//! Only top-level calls are kept. A command that runs other commands (Custom Views › Show, a +//! script) is one step, so playing the recording back does not run the inner commands twice. The +//! buffer is separate from the journal, so trimming the journal never loses recorded steps, and +//! secret parameters are left out as they are in the journal ([`crate::cmd::journal_params`]). + +use serde::Serialize; +use serde_json::Value as Json; + +use crate::Selection; + +/// The most steps one recording keeps; later ones are counted in [`Recorder::dropped`]. +pub const RECORD_LIMIT: usize = 100_000; + +/// One recorded command and where it ran. +#[derive(Clone, Debug, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RecordedStep { + /// Uid of the document that was active when the command ran (0 when none was open). + pub doc: u64, + /// That document's active sheet (index and name) just before the command ran. + pub sheet: usize, + pub sheet_name: String, + /// That document's selection just before the command ran. + pub selection: Selection, + /// The command id and its parameters, without secrets. + pub id: String, + pub params: Json, +} + +/// The steps recorded since recording started. +#[derive(Clone, Debug, Default)] +pub struct Recorder { + pub steps: Vec, + /// Steps not kept because the recording reached [`RECORD_LIMIT`]. + pub dropped: usize, +} + +impl Recorder { + /// Adds a step, or counts it as dropped once the buffer is full. + pub fn push(&mut self, step: RecordedStep) { + if self.steps.len() < RECORD_LIMIT { + self.steps.push(step); + } else { + self.dropped = self.dropped.saturating_add(1); + } + } +} diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index aec2c85449..005d700a6c 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -201,3 +201,95 @@ fn a_group_owns_the_steps_of_commands_that_run_commands() { assert!(v(&s, "A2").is_empty()); assert_eq!(v(&s, "A1").display(), "kept"); } + +// ---------------------------------------------------------------- recording buffer + +fn recorded_ids(s: &Session) -> Vec { + s.recorder.as_ref().unwrap().steps.iter().map(|st| st.id.clone()).collect() +} + +#[test] +fn the_recorder_keeps_top_level_calls_with_where_they_ran() { + let mut s = s(); + s.execute("home.insertSheet", json!({})).unwrap(); + s.execute("selection.set", json!({"range": "B2:C3"})).unwrap(); + s.start_recording(); + assert!(s.is_recording()); + assert_eq!(s.depth(), 0); + set(&mut s, "B2", "10"); + s.execute("cell.get", json!({"cell": "B2"})).unwrap(); // a query: not recorded + assert!(s.execute("sheet.move", json!({})).is_err()); // failed: not recorded + s.execute("view.customViews", json!({"save": "Mine"})).unwrap(); + // Show runs sheet.activate, selection.set and view.zoom inside it: one step. + s.execute("view.customViews", json!({"show": "Mine"})).unwrap(); + assert_eq!(recorded_ids(&s), ["cell.set", "view.customViews", "view.customViews"]); + let first = &s.recorder.as_ref().unwrap().steps[0]; + let d = s.doc().unwrap(); + assert_eq!((first.doc, first.sheet), (d.uid, d.wb.active_sheet)); + assert_eq!(first.sheet_name, d.wb.active().unwrap().name); + assert_eq!(first.selection.a1(), "B2:C3", "the selection before the command ran"); + assert_eq!(first.params, json!({"cell": "B2", "input": "10"})); + assert_eq!(s.depth(), 0); + let rec = s.stop_recording().unwrap(); + assert_eq!(rec.dropped, 0); + set(&mut s, "A1", "after"); + assert!(s.recorder.is_none() && !s.is_recording()); +} + +#[test] +fn the_recorder_survives_journal_trimming() { + let mut s = s(); + s.start_recording(); + for i in 0..10_050u32 { + s.execute("selection.set", json!({"range": format!("A{}", i % 50 + 1)})).unwrap(); + } + set(&mut s, "C1", "last"); + assert!(s.journal.len() < 10_051, "the journal was trimmed"); + let steps = &s.recorder.as_ref().unwrap().steps; + assert_eq!(steps.len(), 10_051); + assert_eq!(steps[0].params, json!({"range": "A1"})); + assert_eq!(steps[10_050].id, "cell.set"); +} + +#[test] +fn the_recorder_has_a_cap() { + let mut rec = crate::record::Recorder::default(); + let step = crate::record::RecordedStep { + doc: 1, + sheet: 0, + sheet_name: "Sheet1".into(), + selection: crate::Selection::default(), + id: "cell.set".into(), + params: json!({}), + }; + for _ in 0..crate::record::RECORD_LIMIT + 3 { + rec.push(step.clone()); + } + assert_eq!(rec.steps.len(), crate::record::RECORD_LIMIT); + assert_eq!(rec.dropped, 3); +} + +#[test] +fn passwords_stay_out_of_the_recording_buffer() { + const PW: &str = "synthetic-pw-5521"; + let mut s = s(); + s.start_recording(); + s.execute("review.protectSheet", json!({"password": PW, "formatCells": true})).unwrap(); + s.execute("review.unprotectSheet", json!({"password": PW})).unwrap(); + let rec = s.stop_recording().unwrap(); + let text = serde_json::to_string(&rec.steps).unwrap(); + assert!(!text.contains(PW), "{text}"); + assert_eq!(rec.steps[0].params, json!({"formatCells": true})); +} + +#[test] +fn record_actions_is_unchanged_by_the_buffer() { + // Automate › Record Actions keeps recording through the journal; the buffer is separate. + let mut s = s(); + s.execute("automate.recordActions", json!({})).unwrap(); + assert!(!s.is_recording()); + set(&mut s, "A1", "typed"); + let r = s.execute("automate.recordActions", json!({"stop": true, "name": "Typed"})).unwrap(); + assert_eq!(r["steps"], 1); + assert!(s.recorder.is_none()); +} From b7bce3bc09aa973f2128ef8a39725a38c30b6513 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:05:42 -0400 Subject: [PATCH 015/687] Engine: document trust for workbooks opened from files Active content such as macros must not run only because a workbook was opened. DocState gets a trust state: Local for workbooks made in the session (new workbooks and samples) and FromFile for anything opened with file.open. Active content in a FromFile workbook asks before it runs. A second window on a workbook shares its trust. The clipboard carries the trust of its source document. Pasting formulas, formats (conditional formats hold formulas) or validation from a FromFile workbook into another workbook makes that workbook FromFile too; pasting values, column widths or notes carries nothing active and changes nothing. Sheet moves and copies stay inside one workbook, so its trust is unchanged. document.inspect reports the trust. --- crates/engine/src/cmd/edit.rs | 8 +++- crates/engine/src/cmd/extra.rs | 1 + crates/engine/src/cmd/file.rs | 2 + crates/engine/src/cmd/inspect.rs | 3 +- crates/engine/src/lib.rs | 30 ++++++++++++++ crates/engine/src/tests/seams.rs | 67 +++++++++++++++++++++++++++++++- 6 files changed, 108 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index fe7f1fe8bc..496ca811a3 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -676,7 +676,7 @@ fn copy_impl(s: &mut Session, p: &Json, cut: bool) -> Result { // The HTML rendering is for the host clipboard (UI); it can reach 4 MB, so agents and // scripts get it only when they ask for it. let html = if p.get("html").and_then(Json::as_bool) == Some(true) { crate::io::range_html(&wb, sheet, range, &text) } else { None }; - let clip = Clipboard { wb, sheet, range, cut, doc_uid: d.uid, text: text.clone(), areas, selected }; + let clip = Clipboard { wb, sheet, range, cut, doc_uid: d.uid, text: text.clone(), areas, selected, trust: d.trust }; s.clipboard = Some(clip); let mut result = json!({"range": range.a1(), "text": text}); if let Some(html) = html { @@ -1183,6 +1183,12 @@ fn paste_special(s: &mut Session, p: &Json) -> Result { *cx.sel = crate::Selection { active: at, anchor: at, ranges: vec![RangeRef::new(at, end)] }; Ok(()) })?; + // Formulas, formats (conditional formats hold formulas) and validation from an untrusted file + // leave the destination untrusted; plain values, column widths and notes carry nothing active. + if !same_doc && !matches!(what, What::Values | What::ValuesAndNumberFormats | What::ColumnWidths | What::Comments) { + let d = s.doc_mut()?; + d.trust = d.trust.receive(clip.trust); + } if cut_move { s.clipboard = None; } diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index 01e2ac6e63..6d6516f50b 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -782,6 +782,7 @@ fn new_window(s: &mut Session, _: &Json) -> Result { let d = s.doc()?.clone(); let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title())); copy.selection = d.selection.clone(); + copy.trust = d.trust; let i = s.add_document(copy); Ok(json!({"index": i})) } diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 9ddc056113..b4dec84c19 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -80,6 +80,8 @@ fn open(s: &mut Session, p: &Json) -> Result { if crate::io::FileKind::from_path(&name).is_none() && gridcraft_xlsx::sniff(&bytes) == gridcraft_xlsx::Format::Xlsx { doc.content_kind = Some(crate::io::FileKind::Xlsx); } + // Someone else may have made the file: its active content asks before it runs. + doc.trust = crate::Trust::FromFile; let i = s.add_document(doc); // The decrypted-workbook notice is for the info bar, not a message box. let shown: Vec<&str> = warnings.iter().map(String::as_str).filter(|w| *w != crate::io::DECRYPTED_NOTICE).collect(); diff --git a/crates/engine/src/cmd/inspect.rs b/crates/engine/src/cmd/inspect.rs index 366d8e6e68..de65ac206b 100644 --- a/crates/engine/src/cmd/inspect.rs +++ b/crates/engine/src/cmd/inspect.rs @@ -7,7 +7,7 @@ use super::*; pub fn specs() -> Vec { vec![ - cmd!(query "document.inspect", "Inspect Workbook", [], None, "{} → sheets, selection, names, tables, charts, dirty, history", has_doc, inspect), + cmd!(query "document.inspect", "Inspect Workbook", [], None, "{} → sheets, selection, names, tables, charts, dirty, trust (local|fromFile), history", has_doc, inspect), cmd!(query "sheet.read", "Read Range", [], None, "{range?: \"A1:D10\" (default: used range), sheet?, formulas?: bool, formatted?: bool} → rows of values", has_doc, read), cmd!(query "cell.get", "Get Cell", [], None, "{cell?, sheet?} → value, formula, display text, style, comment, link", has_doc, cell_get), cmd!(query "selection.stats", "Selection Statistics", [], None, "{} → count, sum, average, min, max", has_doc, sel_stats), @@ -63,6 +63,7 @@ fn inspect(s: &mut Session, _: &Json) -> Result { "title": d.display_title(), "path": d.path, "dirty": d.is_dirty(), + "trust": d.trust, "activeSheet": wb.active_sheet, "sheets": sheets, "selection": d.selection.a1(), diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 03e710a450..b57c474a1d 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -112,6 +112,33 @@ pub struct Clipboard { /// A multi-area copy: the areas as selected (for the marquee). `wb` then holds them slid /// together into one block at `range`. Empty for a single-range copy. pub areas: Vec, + /// Trust of the source document: pasting content from a file someone else made carries it. + pub trust: Trust, +} + +/// Whether a document's active content (macros) may run without asking the person first. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum Trust { + /// Made in this session: a new workbook or a sample. + #[default] + Local, + /// Opened from a file, or holding content pasted from one: active content asks before it + /// runs. + FromFile, +} + +impl Trust { + /// Whether active content may run without asking. + pub fn allows_code(self) -> bool { + self == Trust::Local + } + + /// The trust of a document after it receives content from a document with trust `source`: + /// content from an untrusted file leaves the receiver untrusted. + pub fn receive(self, source: Trust) -> Trust { + if source == Trust::FromFile { Trust::FromFile } else { self } + } } /// In-cell edit mode as shown in the status bar. @@ -143,6 +170,8 @@ pub struct DocState { /// The format `path`'s content was recognised as when its extension doesn't name one (an /// XLSX called `budget.dat`): Save writes it back in that format. pub content_kind: Option, + /// Whether the workbook's active content may run without asking. + pub trust: Trust, } static NEXT_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); @@ -168,6 +197,7 @@ impl DocState { sheet_selections: vec![], unlocked_ranges: vec![], content_kind: None, + trust: Trust::Local, } } pub fn is_dirty(&self) -> bool { diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 005d700a6c..55c35d76c6 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -4,7 +4,7 @@ use serde_json::json; use super::{s, v}; -use crate::{EngineError, Session}; +use crate::{EngineError, Session, Trust}; fn undo_labels(s: &Session) -> Vec { s.doc().unwrap().undo.iter().map(|e| e.label.clone()).collect() @@ -293,3 +293,68 @@ fn record_actions_is_unchanged_by_the_buffer() { assert_eq!(r["steps"], 1); assert!(s.recorder.is_none()); } + +// ---------------------------------------------------------------- trust + +fn open_json(s: &mut Session, wb: &gridcraft_model::Workbook) { + let bytes = serde_json::to_vec(wb).unwrap(); + s.execute("file.open", json!({"name": "theirs.json", "base64": crate::io::base64_encode(&bytes)})).unwrap(); +} + +fn trust(s: &Session) -> Trust { + s.doc().unwrap().trust +} + +#[test] +fn documents_know_where_they_came_from() { + let mut s = s(); + assert_eq!(trust(&s), Trust::Local); + assert!(Trust::Local.allows_code() && !Trust::FromFile.allows_code()); + s.execute("file.new", json!({"sample": "sales"})).unwrap(); + assert_eq!(trust(&s), Trust::Local); + assert_eq!(s.execute("document.inspect", json!({})).unwrap()["trust"], "local"); + open_json(&mut s, &gridcraft_model::Workbook::new()); + assert_eq!(trust(&s), Trust::FromFile); + assert_eq!(s.execute("document.inspect", json!({})).unwrap()["trust"], "fromFile"); + // A second window on the same workbook is the same workbook. + s.execute("view.newWindow", json!({})).unwrap(); + assert_eq!(trust(&s), Trust::FromFile); + // Moving and copying sheets inside it changes nothing. + s.execute("sheet.move", json!({"to": 0, "copy": true})).unwrap(); + assert_eq!(trust(&s), Trust::FromFile); +} + +#[test] +fn pasting_from_a_file_carries_its_trust() { + let mut s = s(); // document 0: Local + open_json(&mut s, &gridcraft_model::Workbook::new()); // document 1: FromFile + let theirs = s.active_index(); + set(&mut s, "A1", "=1+1"); + s.execute("edit.copy", json!({"range": "A1"})).unwrap(); + s.new_workbook(); + let mine = s.active_index(); + // Values carry nothing that runs. + s.execute("edit.pasteSpecial", json!({"what": "values", "at": "B1"})).unwrap(); + assert_eq!(trust(&s), Trust::Local); + s.execute("edit.paste", json!({"at": "C1"})).unwrap(); + assert_eq!(trust(&s), Trust::FromFile); + assert_eq!(v(&s, "C1").display(), "2"); + + // Pasting within a workbook changes nothing, and content from a local workbook leaves the + // destination as it was. + s.set_active(theirs); + s.execute("edit.paste", json!({"at": "A3"})).unwrap(); + assert_eq!(trust(&s), Trust::FromFile, "pasting within a workbook"); + s.new_workbook(); + set(&mut s, "A1", "=3"); + s.execute("edit.copy", json!({"range": "A1"})).unwrap(); + s.set_active(mine); + s.execute("edit.paste", json!({"at": "D1"})).unwrap(); + assert_eq!(trust(&s), Trust::FromFile, "still untrusted from the earlier paste"); + s.new_workbook(); + s.execute("edit.paste", json!({"at": "D1"})).unwrap(); + assert_eq!(trust(&s), Trust::Local); + assert_eq!(Trust::Local.receive(Trust::FromFile), Trust::FromFile); + assert_eq!(Trust::FromFile.receive(Trust::Local), Trust::FromFile); + assert_eq!(Trust::Local.receive(Trust::Local), Trust::Local); +} From 7d1fee8c78e38ead757edd4cbba7b554066c9187 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:06:27 -0400 Subject: [PATCH 016/687] Engine: workbook data in chunked hidden names (cmd/stash.rs) GridCraft keeps workbook data in hidden defined names so it travels inside the xlsx file. The existing items write one name holding one string literal, which past Excel's 255-character literal and 8,192-character formula limits gives a file Excel does not accept. cmd/stash.rs stores an item `key` as hidden workbook-scope names _gc__0, _gc__1, ..., each a formula of string literals of at most 255 characters (UTF-16 units, as Excel counts them) joined with &, at most 8,192 characters per name. Reading concatenates the chunks in order and stops at a gap; it also accepts the older single _sc_ name. Names are matched case-insensitively and only at workbook scope. Keys are limited to letters, digits, _ and ., items to 4,194,304 characters, and junk around or inside the literals, duplicate or out-of-order chunks, non-JSON and over-deep JSON all read safely. keys(prefix) lists the stored items that start with a prefix. Custom views, scripts, the watch list and scenarios keep their current storage; nothing reads or writes the new names yet. docs/architecture.md describes the engine building blocks added in this series: undo groups, the recording buffer, document trust and this storage. --- crates/engine/src/cmd/mod.rs | 1 + crates/engine/src/cmd/stash.rs | 335 +++++++++++++++++++++++++++++++++ docs/architecture.md | 24 ++- 3 files changed, 359 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/cmd/stash.rs diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index d8abfac61f..1db65c20c7 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -19,6 +19,7 @@ pub mod sheet; pub mod slicer; pub mod solver; pub mod spelling; +pub mod stash; pub mod view; pub mod whatif; diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs new file mode 100644 index 0000000000..a31d587529 --- /dev/null +++ b/crates/engine/src/cmd/stash.rs @@ -0,0 +1,335 @@ +//! GridCraft data kept in hidden workbook-scope defined names, so that it travels inside the xlsx +//! file. +//! +//! An item `key` is stored as names `_gc__0`, `_gc__1`, …, each a formula of string +//! literals joined with `&`: `"…"&"…"`. Excel limits a string literal to 255 characters and a +//! formula to 8,192, so a long value is split over as many names as it needs and the file stays +//! one that Excel accepts. Older items (custom views, scripts, the watch list and scenarios) keep +//! one name each, `_sc_`; reading accepts that form as well, so those items can move to this +//! format later, and a write of the same key replaces it. + +use std::collections::BTreeMap; + +use gridcraft_model::{DefinedName, Workbook}; +use serde_json::Value as Json; + +use crate::{EngineError, Result}; + +const PREFIX: &str = "_gc_"; +const LEGACY_PREFIX: &str = "_sc_"; +/// Characters (UTF-16 units, as Excel counts them) in one string literal. +const LITERAL_UNITS: usize = 255; +/// Characters in one name's formula. +const FORMULA_UNITS: usize = 8_192; +/// The longest key: `_gc__` stays well inside Excel's 255-character names. +const MAX_KEY: usize = 200; +/// The longest item: 4,194,304 characters. +pub const MAX_ITEM_CHARS: usize = 4 * 1024 * 1024; + +/// The item text stored under `key`, or `None` when there is none (or it is over the size limit). +pub fn get(wb: &Workbook, key: &str) -> Option { + let mut chunks: BTreeMap = BTreeMap::new(); + let mut legacy = None; + for n in wb.names.iter().filter(|n| n.scope.is_none()) { + match parse_name(&n.name) { + Some((k, Some(i))) if k.eq_ignore_ascii_case(key) => { + chunks.entry(i).or_insert(&n.formula); + } + Some((k, None)) if k.eq_ignore_ascii_case(key) => legacy = legacy.or(Some(&n.formula)), + _ => {} + } + } + let mut out = String::new(); + if chunks.is_empty() { + out = string_literals(legacy?); + } else { + // Chunks run 0, 1, 2, …; anything after a gap isn't part of the item. + for (want, (i, f)) in chunks.iter().enumerate() { + if *i != want as u64 { + break; + } + out.push_str(&string_literals(f)); + if out.len() > MAX_ITEM_CHARS.saturating_mul(4) { + return None; + } + } + } + (out.chars().count() <= MAX_ITEM_CHARS).then_some(out) +} + +/// Stores `text` under `key`, replacing what was there (in either format). Empty text removes it. +pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { + check_key(key)?; + if text.chars().count() > MAX_ITEM_CHARS { + return Err(EngineError::Other(format!("The data is too large to keep in the workbook ({} MB at most).", MAX_ITEM_CHARS / (1024 * 1024)))); + } + remove(wb, key); + for (i, formula) in chunk_formulas(text).into_iter().enumerate() { + wb.names.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true }); + } + Ok(()) +} + +/// Removes the item stored under `key`. +pub fn remove(wb: &mut Workbook, key: &str) { + wb.names.retain(|n| n.scope.is_some() || !parse_name(&n.name).is_some_and(|(k, _)| k.eq_ignore_ascii_case(key))); +} + +/// The item under `key` as JSON (`None` when missing or not JSON). +pub fn get_json(wb: &Workbook, key: &str) -> Option { + serde_json::from_str(&get(wb, key)?).ok() +} + +pub fn set_json(wb: &mut Workbook, key: &str, v: &Json) -> Result<()> { + set(wb, key, &v.to_string()) +} + +/// The keys of the items stored in the workbook that start with `prefix` (`"macro_"`), sorted. +pub fn keys(wb: &Workbook, prefix: &str) -> Vec { + let mut keys: Vec = wb + .names + .iter() + .filter(|n| n.scope.is_none()) + .filter_map(|n| parse_name(&n.name).map(|(k, _)| k.to_ascii_lowercase())) + .filter(|k| k.starts_with(&prefix.to_ascii_lowercase())) + .collect(); + keys.sort(); + keys.dedup(); + keys +} + +/// The item a hidden name belongs to, lower-cased: `_gc__` and `_sc_` give ``. +pub fn item_key(name: &str) -> Option { + parse_name(name).map(|(k, _)| k.to_ascii_lowercase()) +} + +/// `_gc__` → (`key`, `Some(k)`); `_sc_` → (`key`, `None`). +fn parse_name(name: &str) -> Option<(&str, Option)> { + if let Some(rest) = strip_prefix_ci(name, PREFIX) { + let (key, k) = rest.rsplit_once('_')?; + let all_digits = !k.is_empty() && k.len() <= 9 && k.bytes().all(|b| b.is_ascii_digit()); + if key.is_empty() || !all_digits { + return None; + } + return k.parse().ok().map(|i| (key, Some(i))); + } + strip_prefix_ci(name, LEGACY_PREFIX).filter(|k| !k.is_empty()).map(|k| (k, None)) +} + +fn strip_prefix_ci<'a>(s: &'a str, prefix: &str) -> Option<&'a str> { + let head = s.get(..prefix.len())?; + head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..)).flatten() +} + +fn check_key(key: &str) -> Result<()> { + let ok = !key.is_empty() && key.len() <= MAX_KEY && key.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'.'); + if ok { Ok(()) } else { Err(EngineError::Other(format!("`{key}` can't name workbook data: use letters, digits, `_` and `.`"))) } +} + +/// `text` as name formulas: string literals of at most 255 characters joined with `&`, each formula +/// at most 8,192 characters. Concatenating [`string_literals`] of each gives `text` back. +pub fn chunk_formulas(text: &str) -> Vec { + let mut formulas = Vec::new(); + let (mut formula, mut units) = (String::new(), 0usize); + let (mut literal, mut lit_units) = (String::from("\""), 0usize); + let mut flush_literal = |literal: &mut String, lit_units: &mut usize, formula: &mut String, units: &mut usize| { + literal.push('"'); + let cost = literal.encode_utf16().count() + usize::from(!formula.is_empty()); + if *units + cost > FORMULA_UNITS && !formula.is_empty() { + formulas.push(std::mem::take(formula)); + *units = 0; + } + if !formula.is_empty() { + formula.push('&'); + *units += 1; + } + *units += literal.encode_utf16().count(); + formula.push_str(literal); + *literal = String::from("\""); + *lit_units = 0; + }; + for ch in text.chars() { + if lit_units + ch.len_utf16() > LITERAL_UNITS { + flush_literal(&mut literal, &mut lit_units, &mut formula, &mut units); + } + lit_units += ch.len_utf16(); + if ch == '"' { + literal.push('"'); + } + literal.push(ch); + } + if lit_units > 0 { + flush_literal(&mut literal, &mut lit_units, &mut formula, &mut units); + } + if !formula.is_empty() { + formulas.push(formula); + } + formulas +} + +/// Concatenates the string literals of a `"a"&"b""c"` formula (anything outside them is ignored). +pub fn string_literals(f: &str) -> String { + let mut out = String::new(); + let mut it = f.chars().peekable(); + let mut in_str = false; + while let Some(c) = it.next() { + if !in_str { + if c == '"' { + in_str = true; + } + continue; + } + if c == '"' { + if it.peek() == Some(&'"') { + it.next(); + out.push('"'); + } else { + in_str = false; + } + } else { + out.push(c); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn name(n: &str, formula: &str) -> DefinedName { + DefinedName { name: n.into(), scope: None, formula: formula.into(), comment: String::new(), hidden: true } + } + + fn excel_limits_hold(wb: &Workbook) { + for n in &wb.names { + assert!(n.formula.encode_utf16().count() <= FORMULA_UNITS, "{} is {} long", n.name, n.formula.len()); + // Every literal (quotes undoubled) is at most 255 characters. + for lit in n.formula.split("\"&\"") { + let inner = lit.trim_start_matches('"').trim_end_matches('"').replace("\"\"", "\""); + assert!(inner.encode_utf16().count() <= LITERAL_UNITS, "{}", inner.len()); + } + } + } + + #[test] + fn long_values_split_over_names_and_come_back() { + let mut wb = Workbook::new(); + // Quotes double when written; characters outside the BMP count twice. + let text: String = (0..30_000).map(|i| ['a', '"', 'é', '\u{1D11E}', '&', '\n'][i % 6]).collect(); + set(&mut wb, "big", &text).unwrap(); + let names: Vec<&str> = wb.names.iter().map(|n| n.name.as_str()).collect(); + assert!(names.len() > 5, "{names:?}"); + assert_eq!(names[0], "_gc_big_0"); + assert!(wb.names.iter().all(|n| n.hidden && n.scope.is_none())); + excel_limits_hold(&wb); + assert_eq!(get(&wb, "big").as_deref(), Some(text.as_str())); + assert_eq!(get(&wb, "BIG").as_deref(), Some(text.as_str()), "names are case-insensitive"); + // Shorter again: the extra chunks go. + set(&mut wb, "big", "short").unwrap(); + assert_eq!(wb.names.len(), 1); + assert_eq!(wb.names[0].formula, "\"short\""); + set(&mut wb, "big", "").unwrap(); + assert!(wb.names.is_empty()); + assert_eq!(get(&wb, "big"), None); + } + + #[test] + fn chunks_and_literals_respect_the_boundaries() { + for len in [0, 1, 254, 255, 256, 8_000, 8_192, 8_193, 20_000] { + let text = "q".repeat(len); + let fs = chunk_formulas(&text); + assert_eq!(fs.iter().map(|f| string_literals(f)).collect::(), text, "{len}"); + assert!(fs.iter().all(|f| f.len() <= FORMULA_UNITS), "{len}"); + } + // All quotes: each literal doubles in the formula. + let text = "\"".repeat(9_000); + let fs = chunk_formulas(&text); + assert_eq!(fs.iter().map(|f| string_literals(f)).collect::(), text); + assert!(fs.iter().all(|f| f.len() <= FORMULA_UNITS)); + } + + #[test] + fn the_old_single_name_still_reads_and_is_replaced_on_write() { + let mut wb = Workbook::new(); + wb.names.push(name("_sc_scripts", "\"{\"\"A\"\":[1]}\"")); + wb.names.push(name("_sc_scenarios", "\"[1,\"&\"2]\"")); + assert_eq!(get_json(&wb, "scripts"), Some(serde_json::json!({"A": [1]}))); + assert_eq!(get_json(&wb, "scenarios"), Some(serde_json::json!([1, 2]))); + set_json(&mut wb, "scripts", &serde_json::json!({"B": []})).unwrap(); + let names: Vec<&str> = wb.names.iter().map(|n| n.name.as_str()).collect(); + assert_eq!(names, ["_sc_scenarios", "_gc_scripts_0"]); + assert_eq!(get_json(&wb, "scripts"), Some(serde_json::json!({"B": []}))); + } + + #[test] + fn hostile_names_read_safely() { + let mut wb = Workbook::new(); + // A gap: chunk 2 without chunk 1. + wb.names.push(name("_gc_gap_0", "\"a\"")); + wb.names.push(name("_gc_gap_2", "\"c\"")); + assert_eq!(get(&wb, "gap").as_deref(), Some("a")); + // Chunks out of order, a duplicate, junk around the literals, an unterminated literal. + wb.names.push(name("_gc_mix_1", "\"b\"&A1&")); + wb.names.push(name("_gc_mix_0", "SUM(\"a\")")); + wb.names.push(name("_gc_mix_0", "\"ignored\"")); + wb.names.push(name("_gc_mix_2", "\"c")); + assert_eq!(get(&wb, "mix").as_deref(), Some("abc")); + // Names that only look like chunks. + for odd in ["_gc_", "_gc__0", "_gc_x_", "_gc_x_-1", "_gc_x_99999999999999999999", "_sc_", "_gcx_0"] { + wb.names.push(name(odd, "\"zzz\"")); + } + assert_eq!(get(&wb, "x"), None); + assert_eq!(get(&wb, ""), None); + // Not JSON, or JSON nested deeper than the parser allows. + wb.names.push(name("_gc_notjson_0", "\"{\"")); + assert_eq!(get_json(&wb, "notjson"), None); + let deep = format!("\"{}{}\"", "[".repeat(10_000), "]".repeat(10_000)); + wb.names.push(name("_gc_deep_0", &deep)); + assert_eq!(get_json(&wb, "deep"), None); + // Sheet-scoped names are someone else's. + wb.names.push(DefinedName { scope: Some(0), ..name("_gc_scoped_0", "\"s\"") }); + assert_eq!(get(&wb, "scoped"), None); + // Bad keys are refused. + for bad in ["", "has space", "quote\"", "a/b", &"k".repeat(201)] { + assert!(set(&mut wb, bad, "v").is_err(), "{bad}"); + } + assert!(set(&mut wb, "big", &"x".repeat(MAX_ITEM_CHARS + 1)).is_err()); + } + + #[test] + fn oversized_items_are_not_read() { + let mut wb = Workbook::new(); + let chunk = format!("\"{}\"", "x".repeat(250)); + let big = vec![chunk.as_str(); 32].join("&"); + for i in 0..(MAX_ITEM_CHARS / 8_000 + 2) { + wb.names.push(name(&format!("_gc_huge_{i}"), &big)); + } + assert_eq!(get(&wb, "huge"), None); + } + + #[test] + fn keys_list_items_by_prefix() { + let mut wb = Workbook::new(); + set(&mut wb, "macro_2", "b").unwrap(); + set(&mut wb, "macro_1", &"a".repeat(9_000)).unwrap(); + set(&mut wb, "scripts", "{}").unwrap(); + assert_eq!(keys(&wb, "macro_"), ["macro_1", "macro_2"]); + assert_eq!(item_key("_gc_macro_1_3").as_deref(), Some("macro_1")); + assert_eq!(item_key("_SC_Scripts").as_deref(), Some("scripts")); + assert_eq!(item_key("Total"), None); + } + + #[test] + fn items_round_trip_through_xlsx() { + let mut wb = Workbook::new(); + let text: String = (0..20_000).map(|i| ['x', '"', 'ü', '\u{1D11E}', ' '][i % 5]).collect(); + set(&mut wb, "blob", &text).unwrap(); + wb.names.push(name("_sc_watch", "\"[\"\"A1\"\"]\"")); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let (back, warnings) = crate::io::open_bytes("data.xlsx", &bytes).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(get(&back, "blob").as_deref(), Some(text.as_str())); + assert_eq!(get_json(&back, "watch"), Some(serde_json::json!(["A1"]))); + } +} diff --git a/docs/architecture.md b/docs/architecture.md index 5695036b24..446cbe4321 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # Architecture -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-10 · **Change:** major (created from the code on main; supersedes the local-only plan/architecture.md for committed docs) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (engine building blocks for multi-command actions) · **Target:** Microsoft Excel (Microsoft 365) How GridCraft is built today. Rules for contributors are in [`AGENTS.md`](../AGENTS.md). @@ -43,6 +43,27 @@ including tests, 2026-10-10. 5. **Files**: `engine/io.rs` sniffs content (XLSX, XLSB, ODS) before trusting the extension and dispatches to `crates/xlsx`; imports that lose data return warnings shown to the user. +## Engine building blocks for multi-command actions + +Actions made of many commands (macros, a debugger that runs one step per call) use these parts of +`Session` and the engine: + +- **Undo groups.** `Session::grouped(label, f)` runs `f` as one undo step and rolls every change + back if `f` fails; `group_begin`, `group_end` and `group_rollback` do the same across calls. + While a group is open on a document, its commands add no undo steps of their own and Undo and + Redo are disabled there. Groups nest; the outermost one makes the step. +- **Recording buffer** (`record.rs`). `Session::start_recording` collects each successful + top-level command with the document, sheet and selection it ran in, without secret parameters. + Commands run by other commands are not recorded, and the buffer is independent of the journal + (Automate › Record Actions still records through the journal). +- **Document trust.** `DocState::trust` is `Local` for workbooks made in the session and + `FromFile` for workbooks opened from a file, and for workbooks that received formulas, formats + or validation pasted from one. Active content in a `FromFile` workbook asks before it runs. +- **Workbook data in hidden names** (`cmd/stash.rs`). An item is split over hidden + workbook-scope names `_gc__0`, `_gc__1`, … of string literals within Excel's 255- and + 8,192-character limits, so it survives an xlsx round trip. The older single-name `_sc_` + items are read as well. + ## Agent control - Desktop control channel: `gridcraft --control 7979`, JSON lines on localhost @@ -68,4 +89,5 @@ rpm, tarball), FreeBSD, and web (WASM; WebGPU or WebGL2). Release workflows in | Date | Change | Summary | |---|---|---| +| 2026-10-11 | minor | Engine building blocks for multi-command actions: undo groups, recording buffer, document trust, chunked hidden-name storage | | 2026-10-10 | major | Created from the code on main: crates, layers, data flow, agent control, known gaps | From e165eaf3a65adcd67b890182a80face59827e865 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:18:15 -0400 Subject: [PATCH 017/687] Engine: a linked paste of values from a file carries the file's trust A linked paste writes formulas even when it pastes values, but the values exception skipped the trust update, so a Local workbook stayed Local after linking to a FromFile one. The exception now applies only to unlinked pastes. --- crates/engine/src/cmd/edit.rs | 6 ++++-- crates/engine/src/tests/seams.rs | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 496ca811a3..018dd2991d 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1184,8 +1184,10 @@ fn paste_special(s: &mut Session, p: &Json) -> Result { Ok(()) })?; // Formulas, formats (conditional formats hold formulas) and validation from an untrusted file - // leave the destination untrusted; plain values, column widths and notes carry nothing active. - if !same_doc && !matches!(what, What::Values | What::ValuesAndNumberFormats | What::ColumnWidths | What::Comments) { + // leave the destination untrusted; plain values, column widths and notes carry nothing active + // (a linked paste of values still writes formulas). + let passive = matches!(what, What::ColumnWidths | What::Comments) || (!link && matches!(what, What::Values | What::ValuesAndNumberFormats)); + if !same_doc && !passive { let d = s.doc_mut()?; d.trust = d.trust.receive(clip.trust); } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 55c35d76c6..cc082258e0 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -336,6 +336,10 @@ fn pasting_from_a_file_carries_its_trust() { // Values carry nothing that runs. s.execute("edit.pasteSpecial", json!({"what": "values", "at": "B1"})).unwrap(); assert_eq!(trust(&s), Trust::Local); + // A linked paste of values still writes formulas. + s.execute("edit.pasteSpecial", json!({"what": "values", "link": true, "at": "B2"})).unwrap(); + assert_eq!(trust(&s), Trust::FromFile); + s.new_workbook(); s.execute("edit.paste", json!({"at": "C1"})).unwrap(); assert_eq!(trust(&s), Trust::FromFile); assert_eq!(v(&s, "C1").display(), "2"); From 83ae63f79352bd0e473af0b4049b0dffdb2b0f2a Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:18:15 -0400 Subject: [PATCH 018/687] Engine: a stash item with only later chunks reads as missing and falls back to the legacy name With chunk _gc_k_2 but no _gc_k_0, get returned Some("") and never looked at _sc_k. It now takes the legacy path unless chunk 0 exists. --- crates/engine/src/cmd/stash.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index a31d587529..fd8612ad05 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -40,7 +40,7 @@ pub fn get(wb: &Workbook, key: &str) -> Option { } } let mut out = String::new(); - if chunks.is_empty() { + if !chunks.contains_key(&0) { out = string_literals(legacy?); } else { // Chunks run 0, 1, 2, …; anything after a gap isn't part of the item. @@ -269,6 +269,11 @@ mod tests { wb.names.push(name("_gc_gap_0", "\"a\"")); wb.names.push(name("_gc_gap_2", "\"c\"")); assert_eq!(get(&wb, "gap").as_deref(), Some("a")); + // Only a later chunk: no item, and the legacy name is still read. + wb.names.push(name("_gc_orphan_2", "\"c\"")); + assert_eq!(get(&wb, "orphan"), None); + wb.names.push(name("_sc_orphan", "\"old\"")); + assert_eq!(get(&wb, "orphan").as_deref(), Some("old")); // Chunks out of order, a duplicate, junk around the literals, an unterminated literal. wb.names.push(name("_gc_mix_1", "\"b\"&A1&")); wb.names.push(name("_gc_mix_0", "SUM(\"a\")")); From f4e05534a3aa46d28a686db12d5649c2e6d97b40 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:20:29 -0400 Subject: [PATCH 019/687] Engine: a command that runs its own undo groups makes one undo step for itself execute treated any change in the group epoch as 'the group owns the step'. A command that edited outside its own group, or whose group rolled back before it edited, left that edit with no undo step or only part of it. Now only a group that was open before the command (or is still open after) owns the step; a command that opened and closed its own groups replaces their steps with one entry for the command. --- crates/engine/src/cmd/mod.rs | 17 ++++++++++++ crates/engine/src/lib.rs | 27 +++++++++++++++++--- crates/engine/src/tests/seams.rs | 44 ++++++++++++++++++++++++++++++++ docs/architecture.md | 4 ++- 4 files changed, 88 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 1db65c20c7..fadb72b6c4 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -187,6 +187,23 @@ pub fn register_test_command(id: &'static str, run: Run) { test_commands().push(spec); } +/// Test-only: an undoable command, to exercise how `execute` records its step. +#[cfg(test)] +pub(crate) fn register_undoable_test_command(id: &'static str, run: Run) { + let spec: &'static CommandSpec = Box::leak(Box::new(CommandSpec { + id, + label: id, + menu: &[], + shortcut: None, + params: "", + enabled: |_| Ok(()), + run, + journal: false, + undoable: true, + })); + test_commands().push(spec); +} + fn test_commands() -> std::sync::MutexGuard<'static, Vec<&'static CommandSpec>> { #[allow(clippy::unwrap_used)] // a poisoned test registry has no meaningful recovery match TEST_COMMANDS.lock() { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b57c474a1d..e12335e882 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -327,6 +327,8 @@ pub struct Session { next_group: u64, /// Counts group begins and ends, so `execute` can tell that a command opened or closed one. group_epoch: u64, + /// The step the last group made: document, its undo length after the step, and the epoch. + group_step: Option<(u64, usize, u64)>, } impl Session { @@ -396,6 +398,8 @@ impl Session { let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); let params = if params.is_null() { Json::Object(Default::default()) } else { params }; let (first_group, group_epoch) = (self.next_group, self.group_epoch); + // Whether a group was open on the document before the command, and how long its history was. + let start = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (self.in_group(*uid), d.undo.len()))); // The recorder keeps top-level calls with the place they ran in. let place = (self.depth == 0 && self.recorder.is_some()).then(|| match self.active() { Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), @@ -434,15 +438,31 @@ impl Session { self.journal.drain(..5_000); } } - // Inside an undo group, or when the command opened or closed one, the group owns the step. - let grouped = before.as_ref().is_some_and(|(uid, ..)| self.group_epoch != group_epoch || self.in_group(*uid)); + // Inside an undo group, or when the command ended one that was open before it, the group + // owns the step. A command that opened and closed its own groups makes one step of its + // own, which replaces the steps those groups made. + let epoch_changed = self.group_epoch != group_epoch; + let in_group_now = before.as_ref().is_some_and(|(uid, ..)| self.in_group(*uid)); + let own_groups = epoch_changed && !in_group_now && start.is_some_and(|(was_open, _)| !was_open); + let grouped = !own_groups && (epoch_changed || in_group_now); if spec.undoable && let Some((uid, wb, sel)) = before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == uid) && !Arc::ptr_eq(&d.wb, &wb) { if !grouped { - d.undo.push(HistoryEntry { label: spec.label.trim_end_matches('…').to_string(), wb, selection: sel }); + let mut label = spec.label.trim_end_matches('…').to_string(); + if own_groups && let Some((_, len)) = start { + // A command whose only step is its group's keeps that name ("Run Macro: Totals"). + if d.undo.len() == len + 1 + && self.group_step.is_some_and(|(doc, n, epoch)| doc == uid && n == len + 1 && epoch != group_epoch) + && let Some(last) = d.undo.last() + { + label = last.label.clone(); + } + d.undo.truncate(len); + } + d.undo.push(HistoryEntry { label, wb, selection: sel }); if d.undo.len() > UNDO_LIMIT { d.undo.remove(0); } @@ -509,6 +529,7 @@ impl Session { while d.undo.len() > UNDO_LIMIT { d.undo.remove(0); } + self.group_step = Some((g.doc, d.undo.len(), self.group_epoch)); d.redo.clear(); d.revision += 1; } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index cc082258e0..60f84dcf20 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -202,6 +202,50 @@ fn a_group_owns_the_steps_of_commands_that_run_commands() { assert_eq!(v(&s, "A1").display(), "kept"); } +fn put(s: &mut Session, cell: &str, text: &str) -> crate::Result<()> { + s.execute("cell.set", json!({"cell": cell, "input": text})).map(|_| ()) +} + +#[test] +fn a_command_that_runs_its_own_group_makes_one_step() { + use crate::cmd::register_undoable_test_command as register; + // Edits before its group are part of the step. + register("test.editThenGroup", |s, _| { + put(s, "A1", "outside")?; + s.grouped("Inner", |s| put(s, "A2", "inside"))?; + Ok(json!({})) + }); + // A group that fails and rolls back, then an edit outside it. + register("test.failThenEdit", |s, _| { + let failed: crate::Result<()> = s.grouped("Doomed", |s| { + put(s, "B1", "gone")?; + Err(EngineError::Other("no".into())) + }); + assert!(failed.is_err()); + put(s, "B2", "kept")?; + Ok(json!({})) + }); + // A command that is one group keeps the group's name. + register("test.onlyGroup", |s, _| s.grouped("Named Step", |s| put(s, "D1", "x")).map(|_| json!({}))); + let mut s = s(); + s.execute("test.onlyGroup", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["Named Step"]); + s.execute("edit.undo", json!({})).unwrap(); + set(&mut s, "C1", "base"); + s.execute("test.editThenGroup", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["Enter Cell", "test.editThenGroup"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "A1").is_empty() && v(&s, "A2").is_empty()); + assert_eq!(v(&s, "C1").display(), "base"); + + s.execute("test.failThenEdit", json!({})).unwrap(); + assert!(v(&s, "B1").is_empty()); + assert_eq!(v(&s, "B2").display(), "kept"); + assert_eq!(undo_labels(&s), ["Enter Cell", "test.failThenEdit"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "B2").is_empty()); +} + // ---------------------------------------------------------------- recording buffer fn recorded_ids(s: &Session) -> Vec { diff --git a/docs/architecture.md b/docs/architecture.md index 446cbe4321..0f97f857b3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -51,7 +51,9 @@ Actions made of many commands (macros, a debugger that runs one step per call) u - **Undo groups.** `Session::grouped(label, f)` runs `f` as one undo step and rolls every change back if `f` fails; `group_begin`, `group_end` and `group_rollback` do the same across calls. While a group is open on a document, its commands add no undo steps of their own and Undo and - Redo are disabled there. Groups nest; the outermost one makes the step. + Redo are disabled there. Groups nest; the outermost one makes the step. A command that opens and + closes its own groups still makes one undo step for itself, covering its edits inside and outside + the groups. - **Recording buffer** (`record.rs`). `Session::start_recording` collects each successful top-level command with the document, sheet and selection it ran in, without secret parameters. Commands run by other commands are not recorded, and the buffer is independent of the journal From ab27d353865fe29c5187f99e44c5c36b065ae5f3 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:25:45 -0400 Subject: [PATCH 020/687] Engine: a full undo history keeps one step per command that runs its own undo groups When the history held 100 steps, group_end pushed its step and dropped the oldest one at once, so execute's collapse compared against a length that no longer matched. A command that was one group kept the group's step and added its own, losing two old steps. After test.editThenGroup, the second Undo brought back the intermediate state after the outside edit. The history limit now applies after the outermost command has collapsed its steps, and when a group ends outside any command. A document with an open group is trimmed when the group ends. --- crates/engine/src/lib.rs | 30 ++++++++-- crates/engine/src/tests/seams.rs | 96 +++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index e12335e882..fc859e16a9 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -422,6 +422,9 @@ impl Session { d.selection = sel.clone(); d.calc.rebuild(&d.wb); } + if self.depth == 0 { + self.trim_history(); + } return Err(EngineError::Internal(id.to_string(), msg)); } }; @@ -463,13 +466,15 @@ impl Session { d.undo.truncate(len); } d.undo.push(HistoryEntry { label, wb, selection: sel }); - if d.undo.len() > UNDO_LIMIT { - d.undo.remove(0); - } } d.redo.clear(); d.revision += 1; } + // The history limit applies once the outermost command has collapsed its steps, so the + // lengths it compared stay true. + if self.depth == 0 { + self.trim_history(); + } result } @@ -526,16 +531,29 @@ impl Session { let changed = !Arc::ptr_eq(&d.wb, &g.wb); if changed && !outer { d.undo.push(HistoryEntry { label: g.label, wb: g.wb, selection: g.selection }); - while d.undo.len() > UNDO_LIMIT { - d.undo.remove(0); - } self.group_step = Some((g.doc, d.undo.len(), self.group_epoch)); d.redo.clear(); d.revision += 1; } + // Inside a command, the outermost command applies the history limit when it finishes. + if self.depth == 0 { + self.trim_history(); + } Ok(changed) } + /// Drops the oldest undo steps past [`UNDO_LIMIT`]. A document with an open group waits for + /// the group to end, because the group remembers where its steps start. + fn trim_history(&mut self) { + let groups = &self.groups; + for d in self.docs.iter_mut().filter(|d| !groups.iter().any(|g| g.doc == d.uid)) { + let excess = d.undo.len().saturating_sub(UNDO_LIMIT); + if excess > 0 { + d.undo.drain(..excess); + } + } + } + /// Ends an undo group by putting back the workbook, selection, undo and redo history as they /// were when it began. pub fn group_rollback(&mut self, token: GroupToken) -> Result<()> { diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 60f84dcf20..e641fb0cde 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -206,8 +206,7 @@ fn put(s: &mut Session, cell: &str, text: &str) -> crate::Result<()> { s.execute("cell.set", json!({"cell": cell, "input": text})).map(|_| ()) } -#[test] -fn a_command_that_runs_its_own_group_makes_one_step() { +fn register_group_commands() { use crate::cmd::register_undoable_test_command as register; // Edits before its group are part of the step. register("test.editThenGroup", |s, _| { @@ -227,6 +226,18 @@ fn a_command_that_runs_its_own_group_makes_one_step() { }); // A command that is one group keeps the group's name. register("test.onlyGroup", |s, _| s.grouped("Named Step", |s| put(s, "D1", "x")).map(|_| json!({}))); + // An edit, then the end of the innermost group that was open before the command. + register("test.endOpenGroup", |s, _| { + put(s, "E1", "end")?; + let token = s.groups.last().map(|g| crate::GroupToken { id: g.id, doc: g.doc }); + s.group_end(token.ok_or_else(|| EngineError::Other("no group".into()))?)?; + Ok(json!({})) + }); +} + +#[test] +fn a_command_that_runs_its_own_group_makes_one_step() { + register_group_commands(); let mut s = s(); s.execute("test.onlyGroup", json!({})).unwrap(); assert_eq!(undo_labels(&s), ["Named Step"]); @@ -246,6 +257,87 @@ fn a_command_that_runs_its_own_group_makes_one_step() { assert!(v(&s, "B2").is_empty()); } +/// Fills the undo history to the limit with edits of Z1 ("v0" to "v99"), and returns its snapshots. +fn fill_history(s: &mut Session) -> Vec> { + for i in 0..crate::UNDO_LIMIT { + set(s, "Z1", &format!("v{i}")); + } + let d = s.doc().unwrap(); + assert_eq!(d.undo.len(), crate::UNDO_LIMIT); + d.undo.iter().map(|e| e.wb.clone()).collect() +} + +/// The history is still full, ends with one step labelled `label`, and kept every older step but +/// the oldest. +fn assert_one_step_replaced_the_oldest(s: &Session, before: &[std::sync::Arc], label: &str) { + let d = s.doc().unwrap(); + assert_eq!(d.undo.len(), crate::UNDO_LIMIT); + assert_eq!(d.undo.last().map(|e| e.label.as_str()), Some(label)); + assert_eq!(d.undo.iter().filter(|e| e.label == label).count(), 1); + let kept = d.undo.iter().take(crate::UNDO_LIMIT - 1); + assert!(kept.zip(before.iter().skip(1)).all(|(e, wb)| std::sync::Arc::ptr_eq(&e.wb, wb))); +} + +/// Undo goes back to the state before the step, then to the edit before it; Redo comes back. +fn assert_undo_redo(s: &mut Session, cells: &[(&str, &str)]) { + let after = |s: &Session| cells.iter().all(|(c, t)| v(s, c).display() == *t); + let gone = |s: &Session| cells.iter().all(|(c, _)| v(s, c).is_empty()); + assert!(after(s)); + s.execute("edit.undo", json!({})).unwrap(); + assert!(gone(s) && v(s, "Z1").display() == "v99", "the first Undo restores the state before the command"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(gone(s) && v(s, "Z1").display() == "v98", "the second Undo restores the edit before it"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(gone(s) && v(s, "Z1").display() == "v99"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(after(s) && v(s, "Z1").display() == "v99"); +} + +#[test] +fn a_group_only_command_makes_one_step_when_the_history_is_full() { + register_group_commands(); + let mut s = s(); + let before = fill_history(&mut s); + s.execute("test.onlyGroup", json!({})).unwrap(); + assert_one_step_replaced_the_oldest(&s, &before, "Named Step"); + assert_undo_redo(&mut s, &[("D1", "x")]); +} + +#[test] +fn an_edit_then_a_group_makes_one_step_when_the_history_is_full() { + register_group_commands(); + let mut s = s(); + let before = fill_history(&mut s); + s.execute("test.editThenGroup", json!({})).unwrap(); + assert_one_step_replaced_the_oldest(&s, &before, "test.editThenGroup"); + assert_undo_redo(&mut s, &[("A1", "outside"), ("A2", "inside")]); +} + +#[test] +fn a_group_ended_at_the_history_limit_replaces_only_the_oldest_step() { + register_group_commands(); + // A command ends a group that was open before it. + let mut s = s(); + let before = fill_history(&mut s); + let _open = s.group_begin("Debug Macro").unwrap(); + set(&mut s, "E2", "step"); + assert_eq!(s.doc().unwrap().undo.len(), crate::UNDO_LIMIT); + s.execute("test.endOpenGroup", json!({})).unwrap(); + assert!(!s.in_group(s.doc().unwrap().uid)); + assert_one_step_replaced_the_oldest(&s, &before, "Debug Macro"); + assert_undo_redo(&mut s, &[("E1", "end"), ("E2", "step")]); + + // A group ended directly, outside any command, across calls. + let mut s = super::s(); + let before = fill_history(&mut s); + let token = s.group_begin("Spanning").unwrap(); + set(&mut s, "F1", "one"); + set(&mut s, "F2", "two"); + assert!(s.group_end(token).unwrap()); + assert_one_step_replaced_the_oldest(&s, &before, "Spanning"); + assert_undo_redo(&mut s, &[("F1", "one"), ("F2", "two")]); +} + // ---------------------------------------------------------------- recording buffer fn recorded_ids(s: &Session) -> Vec { From 0457f102a04b53a3ca163918474e67ee129c0309 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:27:20 -0400 Subject: [PATCH 021/687] Engine: pasting only formats from a file leaves the destination's trust alone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Paste Special › Formats copies cell styles and merged areas. It copies no conditional formats, formulas or validation, so it carries nothing that runs. It still marked the destination FromFile: copying a formula cell from an opened file and pasting its formats into a new workbook made that workbook ask before running its own active content. Formats pastes now count as passive, like values, column widths and notes. The comment and docs/architecture.md no longer say that pasted formats carry trust. --- crates/engine/src/cmd/edit.rs | 9 +++++---- crates/engine/src/tests/seams.rs | 3 +++ docs/architecture.md | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 018dd2991d..179f35719b 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1183,10 +1183,11 @@ fn paste_special(s: &mut Session, p: &Json) -> Result { *cx.sel = crate::Selection { active: at, anchor: at, ranges: vec![RangeRef::new(at, end)] }; Ok(()) })?; - // Formulas, formats (conditional formats hold formulas) and validation from an untrusted file - // leave the destination untrusted; plain values, column widths and notes carry nothing active - // (a linked paste of values still writes formulas). - let passive = matches!(what, What::ColumnWidths | What::Comments) || (!link && matches!(what, What::Values | What::ValuesAndNumberFormats)); + // Formulas and validation from an untrusted file leave the destination untrusted; plain values, + // formats (cell styles and merged areas, no conditional formats), column widths and notes + // carry nothing active (a linked paste of values still writes formulas). + let passive = + matches!(what, What::Formats | What::ColumnWidths | What::Comments) || (!link && matches!(what, What::Values | What::ValuesAndNumberFormats)); if !same_doc && !passive { let d = s.doc_mut()?; d.trust = d.trust.receive(clip.trust); diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index e641fb0cde..16b2617d70 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -472,6 +472,9 @@ fn pasting_from_a_file_carries_its_trust() { // Values carry nothing that runs. s.execute("edit.pasteSpecial", json!({"what": "values", "at": "B1"})).unwrap(); assert_eq!(trust(&s), Trust::Local); + // Formats carry cell styles and merged areas, nothing that runs. + s.execute("edit.pasteSpecial", json!({"what": "formats", "at": "B3"})).unwrap(); + assert_eq!(trust(&s), Trust::Local); // A linked paste of values still writes formulas. s.execute("edit.pasteSpecial", json!({"what": "values", "link": true, "at": "B2"})).unwrap(); assert_eq!(trust(&s), Trust::FromFile); diff --git a/docs/architecture.md b/docs/architecture.md index 0f97f857b3..d815bc81c5 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -59,8 +59,8 @@ Actions made of many commands (macros, a debugger that runs one step per call) u Commands run by other commands are not recorded, and the buffer is independent of the journal (Automate › Record Actions still records through the journal). - **Document trust.** `DocState::trust` is `Local` for workbooks made in the session and - `FromFile` for workbooks opened from a file, and for workbooks that received formulas, formats - or validation pasted from one. Active content in a `FromFile` workbook asks before it runs. + `FromFile` for workbooks opened from a file, and for workbooks that received formulas or + validation pasted from one. Active content in a `FromFile` workbook asks before it runs. - **Workbook data in hidden names** (`cmd/stash.rs`). An item is split over hidden workbook-scope names `_gc__0`, `_gc__1`, … of string literals within Excel's 255- and 8,192-character limits, so it survives an xlsx round trip. The older single-name `_sc_` From 5392d95bf85d53eab9abedf78a8757e7d292d9ad Mon Sep 17 00:00:00 2001 From: Higgs32584 Date: Wed, 21 Oct 2026 21:13:13 -0400 Subject: [PATCH 022/687] scripts: require workbook trust before running embedded scripts (#2178) A workbook opened with GridCraft scripts (`_sc_scripts`) starts untrusted: automate.runScript refuses them until the user trusts the workbook for the session (Macros dialog > Trust Scripts, i.e. automate.trustScripts) or the caller passes `trusted: true` for that one run. Workbooks without scripts, and scripts written in the session, are unaffected. Whatever the trust, a script whose steps reach outside the workbook (file.*, link.*, or any `path` param: Get Data, From Text/CSV, Insert Picture) is refused before any step runs. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/extra.rs | 75 ++++++++++++++++++++++++++--- crates/engine/src/lib.rs | 7 +++ crates/engine/src/tests.rs | 87 ++++++++++++++++++++++++++++++++++ crates/ui-egui/src/dialogs.rs | 15 +++++- 4 files changed, 176 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index 01e2ac6e63..b88ce55928 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -193,7 +193,24 @@ pub fn specs() -> Vec { new_script ), cmd!(query "automate.allScripts", "All Scripts", ["Automate", "Scripting Tools"], None, "{} → saved scripts", has_doc, all_scripts), - cmd!("automate.runScript", "Run Script", [], None, "{name}", has_doc, run_script), + cmd!( + "automate.runScript", + "Run Script", + [], + None, + "{name, trusted?: bool (runs a script from an untrusted workbook this once)} — refuses scripts in a workbook opened from a file until it is trusted, and steps that reach outside the workbook (file.*, link.*, any `path` param)", + has_doc, + run_script + ), + cmd!( + noundo "automate.trustScripts", + "Trust Scripts", + ["Automate", "Scripting Tools"], + None, + "{trusted?: bool (default true)} → {trusted}: lets this workbook's scripts run for the rest of the session", + has_doc, + trust_scripts + ), cmd!("automate.deleteScript", "Delete Script", [], None, "{name}", has_doc, delete_script), // Cell controls, themes, borders, styles cmd!( @@ -782,6 +799,7 @@ fn new_window(s: &mut Session, _: &Json) -> Result { let d = s.doc()?.clone(); let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title())); copy.selection = d.selection.clone(); + copy.scripts_trusted = d.scripts_trusted; let i = s.add_document(copy); Ok(json!({"index": i})) } @@ -970,10 +988,13 @@ fn new_script(s: &mut Session, p: &Json) -> Result { } fn all_scripts(s: &mut Session, _: &Json) -> Result { - let wb = &s.doc()?.wb; + let d = s.doc()?; + let (wb, trusted) = (&d.wb, d.scripts_trusted); let all = stash_get(wb, SCRIPTS_NAME); - let mut list: Vec = - all.as_object().map(|o| o.iter().map(|(k, v)| json!({"name": k, "steps": v.as_array().map_or(0, Vec::len)})).collect()).unwrap_or_default(); + let mut list: Vec = all + .as_object() + .map(|o| o.iter().map(|(k, v)| json!({"name": k, "steps": v.as_array().map_or(0, Vec::len), "trusted": trusted})).collect()) + .unwrap_or_default(); // A preserved VBA project is reported alongside GridCraft's own scripts; it is carried on // save but never executed here, so it has no step count. if let Some(bytes) = &wb.vba_project { @@ -982,12 +1003,54 @@ fn all_scripts(s: &mut Session, _: &Json) -> Result { Ok(Json::Array(list)) } +/// Whether `wb` carries GridCraft scripts (a workbook opened with them starts untrusted). +pub(crate) fn has_scripts(wb: &Workbook) -> bool { + stash_map(wb, SCRIPTS_NAME).as_object().is_some_and(|o| !o.is_empty()) +} + +/// Why a script step is refused, whatever the workbook's trust: scripts stay inside the +/// workbook. Anything that reads or writes files (`file.*`, or any `path` parameter: Get Data, +/// From Text/CSV, Insert Picture) or leaves the app (`link.*` opens URLs) is the caller's to run +/// directly, not a stored script's. (Nested `automate.*` steps are skipped.) +fn denied_script_step(id: &str, params: &Json) -> Option { + if id.starts_with("file.") || id.starts_with("link.") { + return Some(format!("scripts can't run `{id}`: it reaches outside the workbook")); + } + if params.get("path").is_some() { + return Some(format!("scripts can't pass a file `path` to `{id}`")); + } + None +} + +fn trust_scripts(s: &mut Session, p: &Json) -> Result { + let trusted = bool_param(p, "trusted").unwrap_or(true); + s.doc_mut()?.scripts_trusted = trusted; + Ok(json!({"trusted": trusted})) +} + +/// Runs a stored script. Scripts in a workbook opened from a file run only once the workbook is +/// trusted for this session (`automate.trustScripts`, or `trusted: true` on this call); steps that +/// reach outside the workbook ([`denied_script_step`]) are refused before any step runs. fn run_script(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("automate.runScript", "missing `name`"))?; - let all = stash_get(&s.doc()?.wb, SCRIPTS_NAME); + let d = s.doc()?; + let all = stash_get(&d.wb, SCRIPTS_NAME); let steps = all.get(name).and_then(Json::as_array).cloned().ok_or_else(|| bad("automate.runScript", "no such script"))?; + if !d.scripts_trusted && !bool_param(p, "trusted").unwrap_or(false) { + return Err(bad( + "automate.runScript", + "scripts in this workbook came from a file and are disabled; trust the workbook first (Automate › Macros › Trust Scripts, or `trusted: true`)", + )); + } + let steps: Vec = steps.into_iter().take(100_000).collect(); + for st in &steps { + let id = st["command"].as_str().unwrap_or(""); + if let Some(why) = denied_script_step(id, &st["params"]) { + return Err(bad("automate.runScript", why)); + } + } let mut n = 0; - for st in steps.iter().take(100_000) { + for st in &steps { let id = st["command"].as_str().unwrap_or(""); if id.starts_with("automate.") { continue; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 258a1cb729..bf7dd633db 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -112,6 +112,11 @@ pub struct DocState { /// The format `path`'s content was recognised as when its extension doesn't name one (an /// XLSX called `budget.dat`): Save writes it back in that format. pub content_kind: Option, + /// Whether the scripts stored in this workbook may run (`automate.runScript`). A workbook + /// that arrives already carrying scripts (opened from a file) starts untrusted until the + /// user trusts it in this session (`automate.trustScripts`); one with no scripts starts + /// trusted, since any script it gains later was written here. + pub scripts_trusted: bool, } static NEXT_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); @@ -121,6 +126,7 @@ impl DocState { pub fn new(mut wb: Workbook, path: Option, title: String) -> DocState { let mut calc = Calc::new(); calc.recalc_all(&mut wb); + let scripts_trusted = !cmd::extra::has_scripts(&wb); let wb = Arc::new(wb); let sel = Selection::at(wb.active().map(|s| s.view_active).unwrap_or_default()); DocState { @@ -137,6 +143,7 @@ impl DocState { sheet_selections: vec![], unlocked_ranges: vec![], content_kind: None, + scripts_trusted, } } pub fn is_dirty(&self) -> bool { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 2ac2906548..357b925c1f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -940,6 +940,93 @@ fn protection_passwords_stay_out_of_the_journal_and_recorded_scripts() { s.execute("automate.runScript", json!({"name": "Lock"})).unwrap(); } +/// A session holding a workbook opened from file bytes whose `Fill` script sets B1 (#2178). +fn opened_with_script() -> Session { + let mut author = s(); + author + .execute("automate.newScript", json!({"name": "Fill", "commands": [{"command": "cell.set", "params": {"cell": "B1", "input": "ran"}}]})) + .unwrap(); + let bytes = crate::io::save_bytes(&author.doc().unwrap().wb, "book.xlsx").unwrap(); + let mut s = Session::new(); + s.execute("file.open", json!({"name": "downloaded.xlsx", "base64": crate::io::base64_encode(&bytes)})).unwrap(); + s +} + +#[test] +fn scripts_written_in_this_session_run_without_a_trust_step() { + let mut s = s(); + assert!(s.doc().unwrap().scripts_trusted); + s.execute("automate.newScript", json!({"name": "Fill", "commands": [{"command": "cell.set", "params": {"cell": "B1", "input": "ran"}}]})) + .unwrap(); + assert_eq!(s.execute("automate.runScript", json!({"name": "Fill"})).unwrap()["ran"], 1); + assert_eq!(v(&s, "B1"), Value::Text("ran".into())); +} + +#[test] +fn untrusted_workbook_scripts_are_refused_until_trusted() { + let mut s = opened_with_script(); + s.ui_requests.clear(); + assert!(!s.doc().unwrap().scripts_trusted, "a workbook opened with scripts starts untrusted"); + let listed = s.execute("automate.allScripts", json!({})).unwrap(); + assert_eq!(listed[0]["trusted"], false, "{listed}"); + let err = s.execute("automate.runScript", json!({"name": "Fill"})).unwrap_err().to_string(); + assert!(err.contains("trust"), "{err}"); + assert_eq!(v(&s, "B1"), Value::Empty, "no step of an untrusted script runs"); + // Trust can't be smuggled in as a string, and nothing opens a dialog. + assert!(s.execute("automate.runScript", json!({"name": "Fill", "trusted": "yes"})).is_err()); + assert!(s.ui_requests.is_empty(), "{:?}", s.ui_requests); + + // An explicit `trusted: true` runs it this once without trusting the workbook. + s.execute("automate.runScript", json!({"name": "Fill", "trusted": true})).unwrap(); + assert_eq!(v(&s, "B1"), Value::Text("ran".into())); + assert!(!s.doc().unwrap().scripts_trusted); + s.execute("cell.set", json!({"cell": "B1", "input": ""})).unwrap(); + assert!(s.execute("automate.runScript", json!({"name": "Fill"})).is_err()); + + // Trusting the workbook (the UI's Trust Scripts action) lets its scripts run this session. + assert_eq!(s.execute("automate.trustScripts", json!({})).unwrap()["trusted"], true); + s.execute("automate.runScript", json!({"name": "Fill"})).unwrap(); + assert_eq!(v(&s, "B1"), Value::Text("ran".into())); + // A second window on the same workbook keeps the decision; withdrawing it disables them again. + s.execute("view.newWindow", json!({})).unwrap(); + assert!(s.doc().unwrap().scripts_trusted); + s.execute("automate.trustScripts", json!({"trusted": false})).unwrap(); + assert!(s.execute("automate.runScript", json!({"name": "Fill"})).is_err()); +} + +#[test] +fn scripts_cannot_reach_outside_the_workbook_even_when_trusted() { + let dir = std::env::temp_dir().join(format!("gridcraft-script-deny-{}", std::process::id())); + let leak = dir.join("leak.xlsx"); + let leak = leak.to_string_lossy().to_string(); + let mut s = opened_with_script(); + s.ui_requests.clear(); + s.execute("automate.trustScripts", json!({})).unwrap(); + let denied = [ + json!({"command": "link.open", "params": {"url": "https://example.com/?d=secret"}}), + json!({"command": "file.saveAs", "params": {"path": leak}}), + json!({"command": "file.save", "params": {}}), + json!({"command": "file.exportPdf", "params": {"path": leak}}), + json!({"command": "file.open", "params": {"path": leak}}), + json!({"command": "data.getData", "params": {"path": leak}}), + json!({"command": "data.fromTextCsv", "params": {"path": leak}}), + json!({"command": "insert.picture", "params": {"path": leak}}), + ]; + for step in denied { + // The harmless first step proves the whole script is refused before anything runs. + let first = json!({"command": "cell.set", "params": {"cell": "C1", "input": "partial"}}); + s.execute("automate.newScript", json!({"name": "Leak", "commands": [first, step]})).unwrap(); + let r = s.execute("automate.runScript", json!({"name": "Leak", "trusted": true})); + assert!(r.is_err(), "{step} must be refused"); + assert_eq!(v(&s, "C1"), Value::Empty, "{step}: no step runs"); + } + assert!(!s.ui_requests.iter().any(|r| matches!(r, crate::UiRequest::OpenUrl(_))), "{:?}", s.ui_requests); + assert!(!std::path::Path::new(&leak).exists()); + // Data a script supplies itself is still fine. + s.execute("automate.newScript", json!({"name": "Csv", "commands": [{"command": "data.fromTextCsv", "params": {"text": "a,b\n1,2"}}]})).unwrap(); + s.execute("automate.runScript", json!({"name": "Csv"})).unwrap(); +} + /// A sheet with locked data in A1:B3, unlocked cells in D1:D3 and row 10, then protected. fn protected(extra: serde_json::Value) -> Session { let mut s = s(); diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index fb899c7026..10320476c8 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1077,7 +1077,7 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) { } }); } - "macros" => macros(ui, &mut d), + "macros" => macros(app, ui, &mut d), "statistics" | "accessibility" | "errorChecking" | "evaluateFormula" | "comments" => { let r = if d.name == "comments" { app.session.active().and_then(|doc| doc.wb.active().map(|s| json!(s.comments.iter().map(|(c, m)| json!({"cell": c.a1(), "author": m.author, "text": m.text})).collect::>()))) @@ -1496,7 +1496,7 @@ fn about(ui: &mut egui::Ui, d: &mut Dialog) { /// The Macros dialog: lists GridCraft's own recorded scripts and (if the file carries one) the /// preserved VBA project. Mirrors Excel's Developer ▸ Macros list, but is honest that VBA is /// preserved and not executed. -fn macros(ui: &mut egui::Ui, d: &mut Dialog) { +fn macros(app: &mut SheetApp, ui: &mut egui::Ui, d: &mut Dialog) { ui.label("Automation in this workbook. GridCraft runs its own scripts; a file's VBA project is preserved but not executed."); ui.add_space(6.0); let items: Vec = d.result.as_ref().and_then(Json::as_array).cloned().unwrap_or_default(); @@ -1504,6 +1504,17 @@ fn macros(ui: &mut egui::Ui, d: &mut Dialog) { ui.weak("This workbook has no scripts or macros."); return; } + // Scripts that came with a file stay disabled until the user trusts the workbook (#2178). + if items.iter().any(|it| it.get("trusted").and_then(Json::as_bool) == Some(false)) { + ui.horizontal(|ui| { + ui.weak("Scripts in this workbook came from a file and are disabled."); + if ui.button("Trust Scripts").on_hover_text("Let this workbook's scripts run for the rest of this session").clicked() { + let _ = app.run("automate.trustScripts", json!({"trusted": true})); + d.result = app.session.run("view.macros", json!({})).ok(); + } + }); + ui.add_space(6.0); + } egui::ScrollArea::vertical().max_height(300.0).show(ui, |ui| { for it in &items { let name = it.get("name").and_then(Json::as_str).unwrap_or("(unnamed)"); From b9801f3f810927d4dd597c0d38b5ba13729221eb Mon Sep 17 00:00:00 2001 From: Viorel Toader Date: Thu, 22 Oct 2026 07:55:04 +0300 Subject: [PATCH 023/687] Romanian: translate Options|Editing; 22 strings main added since (sheet tab menu, account, chart and error-checking labels) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JadgGo4G2GHaAMNb3qyygk --- crates/ui-egui/src/i18n/ro.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/i18n/ro.rs b/crates/ui-egui/src/i18n/ro.rs index 699e30ff39..9e0d92516f 100644 --- a/crates/ui-egui/src/i18n/ro.rs +++ b/crates/ui-egui/src/i18n/ro.rs @@ -1005,7 +1005,31 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Print job submitted: {receipt}" => "Lucrarea de imprimare a fost trimisă: {receipt}", "Print failed: {error}" => "Imprimarea a eșuat: {error}", "Print failed: the printing worker stopped." => "Imprimarea a eșuat: procesul de imprimare s-a oprit.", - "Options|Editing" => "Editing", + "Options|Editing" => "Editare", + "Duplicate" => "Duplicare", + "Rename" => "Redenumire", + "Product Information" => "Informații despre produs", + "Tab Color" => "Culoare filă", + "Select All Sheets" => "Selectare toate foile", + "Fit selection" => "Încadrare selecție", + "All sheets" => "Toate foile", + "Version" => "Versiune", + "Unhide…" => "Reafișare…", + "Platform" => "Platformă", + "Unprotect Sheet…" => "Deprotejare foaie…", + "AutoSave: save after every change (workbooks saved to a file)" => { + "Salvare automată: salvează după fiecare modificare (registrele salvate într-un fișier)" + } + "Licence" => "Licență", + "Account" => "Cont", + "No Color" => "Fără culoare", + "Move or Copy…" => "Mutare sau copiere…", + "Number Stored as Text" => "Număr stocat ca text", + "Select Data…" => "Selectare date…", + "Convert to Number" => "Conversie în număr", + "Format Chart Area…" => "Formatare zonă diagramă…", + "Error checking options" => "Opțiuni de verificare a erorilor", + "Paste formulas that refer to the copied cells" => "Lipire formule care fac referire la celulele copiate", _ => return None, }) } From 1794c389e58ce42e538a88468d7fe6a426dff387 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 05:01:47 +0000 Subject: [PATCH 024/687] Keep the cell draft in its own workbook; Switch Windows lists open workbooks A cell being typed when File > New ran (macOS menu, control channel) stayed open over the new workbook, and Enter wrote it there; the first workbook stayed clean and nothing in the UI reached it. - Every file.* and window command (window.activate, view.newWindow, view.hideWindow, view.unhideWindow, view.switchWindows with a doc) first commits the cell and text-box drafts as Enter would, or drops a draft its workbook refuses, still reporting why. Finder, argv and dropped opens go through file.open and get the same. - The cell editor records its workbook's uid; the grid draws a draft only over that workbook, and a commit always goes into it. - view.switchWindows takes a `doc` uid to bring that workbook forward and lists index, doc, title, dirty, active and hidden. Menu-style calls with no doc open the View tab's new Switch Windows dropdown. - macOS: the Window menu lists the open workbooks (unsaved bullet, active check), and Quit closes the window so unsaved workbooks are asked about. --- apps/gridcraft/src/main.rs | 2 +- apps/gridcraft/src/native_menu.rs | 68 ++++++++++++++-- crates/engine/src/cmd/extra.rs | 28 ++++++- crates/engine/src/lib.rs | 6 ++ crates/engine/src/tests.rs | 31 ++++++++ crates/ui-egui/src/editor.rs | 15 ++-- crates/ui-egui/src/grid.rs | 3 +- crates/ui-egui/src/lib.rs | 127 ++++++++++++++++++++++++------ crates/ui-egui/src/ribbon.rs | 19 +++++ crates/ui-egui/tests/file_menu.rs | 77 ++++++++++++++++++ 10 files changed, 337 insertions(+), 39 deletions(-) diff --git a/apps/gridcraft/src/main.rs b/apps/gridcraft/src/main.rs index 7c0eb04e6c..8aad9aeb8f 100644 --- a/apps/gridcraft/src/main.rs +++ b/apps/gridcraft/src/main.rs @@ -34,7 +34,7 @@ impl eframe::App for App { if self.1.is_none() && std::env::var_os("GRIDCRAFT_NO_NATIVE_MENU").is_none() { self.1 = Some(native_menu::NativeMenu::install(ctx)); } - if let Some(m) = &self.1 { + if let Some(m) = &mut self.1 { m.poll(&mut self.0, ctx); } apple_events::poll(&self.2, &mut self.0, ctx); diff --git a/apps/gridcraft/src/native_menu.rs b/apps/gridcraft/src/native_menu.rs index 6985af3c0f..2c28663791 100644 --- a/apps/gridcraft/src/native_menu.rs +++ b/apps/gridcraft/src/native_menu.rs @@ -4,7 +4,7 @@ use std::str::FromStr; use std::sync::mpsc::{Receiver, channel}; -use gridcraft_ui_egui::SheetApp; +use gridcraft_ui_egui::{OpenWindow, SheetApp}; use muda::accelerator::Accelerator; use muda::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu}; use serde_json::{Value, json}; @@ -168,7 +168,16 @@ fn tree() -> Vec<(&'static str, Vec)> { ("Calculate Now", "formulas.calculateNow", None), ], ), - ("Window", vec![("New Window", "view.newWindow", None), ("Next Sheet", "sheet.next", None), ("Previous Sheet", "sheet.previous", None)]), + // The open workbooks follow the separator (`NativeMenu::sync_windows`). + ( + "Window", + vec![ + ("New Window", "view.newWindow", None), + ("Next Sheet", "sheet.next", None), + ("Previous Sheet", "sheet.previous", None), + ("-", "", None), + ], + ), ( "Help", vec![ @@ -185,8 +194,19 @@ pub struct NativeMenu { _menu: Menu, events: Receiver, theme_items: Vec<(&'static str, CheckMenuItem)>, + window_menu: Option, + /// The Window menu's workbook items and what they last showed. + window_items: Vec, + windows: Vec, } +/// Menu id of the Window menu item that brings workbook `doc` forward. +const WINDOW_ITEM: &str = "window:doc:"; + +/// The Quit item: closing the window asks about unsaved workbooks first, which AppKit's own +/// Quit (terminate:) would skip. +const QUIT: &str = "app.quit"; + fn select_theme(app: &mut SheetApp, id: &str) -> bool { let mode = match id { "theme:system" => "system", @@ -212,7 +232,7 @@ impl NativeMenu { &PredefinedMenuItem::hide_others(None), &PredefinedMenuItem::show_all(None), &PredefinedMenuItem::separator(), - &PredefinedMenuItem::quit(None), + &MenuItem::with_id(QUIT, "Quit GridCraft", true, Accelerator::from_str("CMD+Q").ok()), ]); let _ = menu.append(&app_menu); let mut theme_items = Vec::new(); @@ -247,7 +267,7 @@ impl NativeMenu { // NSApp's windowsMenu is where macOS adds Move & Resize, Fill and Center, the targets of the // system tiling shortcuts (Fn+Ctrl+arrows, Fn+F). Only once the menu is NSApp's: muda looks // the submenu up in the installed main menu. - if let Some(window) = window_menu { + if let Some(window) = &window_menu { window.set_as_windows_menu_for_nsapp(); } let (tx, rx) = channel(); @@ -256,12 +276,47 @@ impl NativeMenu { let _ = tx.send(e); ctx.request_repaint(); })); - NativeMenu { _menu: menu, events: rx, theme_items } + NativeMenu { _menu: menu, events: rx, theme_items, window_menu, window_items: Vec::new(), windows: Vec::new() } } - pub fn poll(&self, app: &mut SheetApp, ctx: &egui::Context) { + /// Keeps the Window menu's list of open workbooks (title, unsaved bullet, active check) in + /// step with the session. + fn sync_windows(&mut self, app: &SheetApp) { + let windows = app.open_windows(); + if windows == self.windows { + return; + } + let Some(menu) = &self.window_menu else { return }; + let same_docs = windows.len() == self.windows.len() && windows.iter().zip(&self.windows).all(|(a, b)| a.doc == b.doc); + if same_docs { + for (item, w) in self.window_items.iter().zip(&windows) { + item.set_text(w.label()); + item.set_checked(w.active); + } + } else { + for item in self.window_items.drain(..) { + let _ = menu.remove(&item); + } + for w in &windows { + let item = CheckMenuItem::with_id(format!("{WINDOW_ITEM}{}", w.doc), w.label(), true, w.active, None); + let _ = menu.append(&item); + self.window_items.push(item); + } + } + self.windows = windows; + } + + pub fn poll(&mut self, app: &mut SheetApp, ctx: &egui::Context) { while let Ok(ev) = self.events.try_recv() { let id = ev.id.as_ref().to_string(); + if id == QUIT { + ctx.send_viewport_cmd(egui::ViewportCommand::Close); + continue; + } + if let Some(doc) = id.strip_prefix(WINDOW_ITEM).and_then(|d| d.parse::().ok()) { + app.run_or_alert("view.switchWindows", json!({"doc": doc})); + continue; + } // Text selection belongs to the inline editor, not the worksheet clipboard/undo. if app.text_box_editor.is_some() { let clipboard = match id.as_str() { @@ -321,6 +376,7 @@ impl NativeMenu { item.set_checked(checked); } } + self.sync_windows(app); } } diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index c44094cddb..debb62eeb9 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -145,7 +145,15 @@ pub fn specs() -> Vec { cmd!(noundo "view.arrangeAll", "Arrange All", ["View", "Window"], None, "{}", has_doc, |_, _| ok()), cmd!(noundo "view.hideWindow", "Hide", ["View", "Window"], None, "{}", has_doc, hide_window), cmd!(noundo "view.unhideWindow", "Unhide…", ["View", "Window"], None, "{index?}", has_doc, unhide_window), - cmd!(query "view.switchWindows", "Switch Windows", ["View", "Window"], None, "{} → open windows", always, switch_windows), + cmd!( + noundo "view.switchWindows", + "Switch Windows", + ["View", "Window"], + None, + "{doc?} → open workbooks [{index, doc, title, dirty, active, hidden}]; with `doc` (a workbook's `doc` id from the list) makes that workbook active first", + always, + switch_windows + ), cmd!(noundo "view.ruler", "Ruler", ["View", "Show"], None, "{on?} → {on}: rulers in Page Layout view (an application option)", has_doc, ruler), cmd!(query "view.macros", "Macros", ["View", "Macros"], None, "{} → scripts", has_doc, all_scripts), // Table tools @@ -808,12 +816,26 @@ fn unhide_window(s: &mut Session, p: &Json) -> Result { ok() } -fn switch_windows(s: &mut Session, _: &Json) -> Result { +fn switch_windows(s: &mut Session, p: &Json) -> Result { + if let Some(doc) = p.get("doc").filter(|v| !v.is_null()) { + let i = doc.as_u64().and_then(|uid| s.document_index(uid)).ok_or_else(|| bad("view.switchWindows", "no open workbook has that `doc` id"))?; + s.hidden_windows.retain(|x| *x != i); + s.set_active(i); + } Ok(Json::Array( s.documents() .iter() .enumerate() - .map(|(i, d)| json!({"index": i, "title": d.display_title(), "active": i == s.active_index(), "hidden": s.hidden_windows.contains(&i)})) + .map(|(i, d)| { + json!({ + "index": i, + "doc": d.uid, + "title": d.display_title(), + "dirty": d.is_dirty(), + "active": i == s.active_index(), + "hidden": s.hidden_windows.contains(&i), + }) + }) .collect(), )) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 10c6cb87cc..aeeaa9bdb0 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -106,6 +106,8 @@ pub struct DocState { pub path: Option, pub saved: Arc, pub revision: u64, + /// Identifies this open workbook for the rest of the session: it never changes while the + /// workbook is open (saving, renaming, reordering) and is never reused for another one. pub uid: u64, pub title: String, /// Per-sheet selection memory. @@ -336,6 +338,10 @@ impl Session { pub fn active(&self) -> Option<&DocState> { self.docs.get(self.active) } + /// Where the open workbook with this [`DocState::uid`] is in [`Session::documents`]. + pub fn document_index(&self, uid: u64) -> Option { + self.docs.iter().position(|d| d.uid == uid) + } pub fn active_mut(&mut self) -> Option<&mut DocState> { self.docs.get_mut(self.active) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index d6a99be91f..07a32209f0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -4459,3 +4459,34 @@ fn html_export_without_pictures_or_charts_has_no_fixed_geometry() { let html = crate::io::to_html(&s.doc().unwrap().wb, 0).unwrap(); assert!(!html.contains("") && !html.contains("table-layout") && !html.contains(" EditState { + pub fn new(doc: u64, sheet: usize, cell: CellRef, text: String, enter_mode: bool, from_formula_bar: bool) -> EditState { let caret = text.chars().count(); EditState { + doc, sheet, locale: Default::default(), cell, @@ -365,7 +368,7 @@ mod tests { #[test] fn point_mode() { - let mut e = EditState::new(0, CellRef::new(0, 0), "=SUM(".into(), true, false); + let mut e = EditState::new(1, 0, CellRef::new(0, 0), "=SUM(".into(), true, false); assert!(e.can_point()); e.insert_ref("A1"); assert_eq!(e.text, "=SUM(A1"); @@ -375,19 +378,19 @@ mod tests { e.caret += 1; e.point = None; assert!(e.can_point()); - let e2 = EditState::new(0, CellRef::new(0, 0), "hello".into(), true, false); + let e2 = EditState::new(1, 0, CellRef::new(0, 0), "hello".into(), true, false); assert!(!e2.can_point()); } #[test] fn autocomplete_and_hints() { let names: Vec = ["SUM", "SUMIF", "SUMPRODUCT", "AVERAGE"].iter().map(|s| s.to_string()).collect(); - let mut e = EditState::new(0, CellRef::new(0, 0), "=su".into(), true, false); + let mut e = EditState::new(1, 0, CellRef::new(0, 0), "=su".into(), true, false); e.update_autocomplete(&names); assert_eq!(e.autocomplete.len(), 3); assert!(e.accept_autocomplete()); assert_eq!(e.text, "=SUM("); - let e = EditState::new(0, CellRef::new(0, 0), "=IF(A1>0,SUM(B1,".into(), true, false); + let e = EditState::new(1, 0, CellRef::new(0, 0), "=IF(A1>0,SUM(B1,".into(), true, false); assert_eq!(e.current_function(), Some(("SUM".into(), 1))); } @@ -423,7 +426,7 @@ mod tests { #[test] fn inserting_a_union_keeps_each_reference_separately_pointed() { - let mut edit = EditState::new(0, CellRef::new(0, 0), "=SUM(".into(), true, false); + let mut edit = EditState::new(1, 0, CellRef::new(0, 0), "=SUM(".into(), true, false); edit.insert_ref("C:C"); edit.append_ref("E:E"); assert_eq!(edit.text, "=SUM(C:C,E:E"); diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index a0efbfae7a..145b9f583d 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -1744,6 +1744,7 @@ pub(crate) fn paint_selection( // Editing a formula: outline its references instead of the selection. if let Some(ed) = &app.editor && ed.is_formula() + && app.session.active().is_some_and(|d| d.uid == ed.doc) { let origin = app.session.active().and_then(|d| d.wb.sheet(ed.sheet)).map_or(sh.name.as_str(), |o| o.name.as_str()); for (_, _, r, sheet, idx) in crate::editor::formula_refs(&ed.text) { @@ -3045,7 +3046,7 @@ fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet, wb: &Workbook) { let Some(ed) = app.editor.as_ref() else { return }; - if ed.sheet != wb.active_sheet { + if app.view_key() != Some((ed.doc, ed.sheet)) { return; } let cell = ed.cell; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index faae9bfae8..001abaad50 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -166,6 +166,23 @@ pub struct Notice { pub action: Option<(String, String)>, } +/// One open workbook, as View › Switch Windows and the macOS Window menu list it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OpenWindow { + /// The workbook's `DocState::uid`, the `doc` param of `view.switchWindows`. + pub doc: u64, + pub title: String, + pub dirty: bool, + pub active: bool, +} + +impl OpenWindow { + /// The title, with a bullet after it while the workbook has unsaved changes. + pub fn label(&self) -> String { + if self.dirty { format!("{} •", self.title) } else { self.title.clone() } + } +} + /// Host services the platform shell provides (file pickers, opening URLs). #[derive(Default)] pub struct Services { @@ -296,6 +313,9 @@ pub struct SheetApp { /// Every unsaved workbook has been answered for quitting (or `app.quit` asked): the next /// window close goes through. pub allow_close: bool, + /// View › Switch Windows opens its list on the next frame the View tab draws (asked for by a + /// key tip, the command search or a menu, possibly while another tab was showing). + pub(crate) open_switch_windows: bool, } impl SheetApp { @@ -342,6 +362,7 @@ impl SheetApp { tab_run: None, started: now_ms(), allow_close: false, + open_switch_windows: false, } } @@ -387,30 +408,30 @@ impl SheetApp { } fn run_command(&mut self, id: &str, params: Json) -> Result { + let switches_window = id.starts_with("window.") + || matches!(id, "view.newWindow" | "view.hideWindow" | "view.unhideWindow") + || (id == "view.switchWindows" && params.get("doc").is_some_and(|d| !d.is_null())); + if id.starts_with("file.") || switches_window { + self.settle_drafts(); + } if matches!(id, "file.save" | "file.saveAs" | "file.saveBytes") { self.store_view_state(); } - if id.starts_with("file.") - || matches!( - id, - "window.activate" - | "sheet.activate" - | "sheet.next" - | "sheet.previous" - | "sheet.move" - | "sheet.hide" - | "sheet.unhide" - | "view.newWindow" - | "view.hideWindow" - | "view.unhideWindow" - | "home.insertSheet" - | "home.deleteSheet" - | "edit.undo" - | "edit.redo" - | "object.delete" - | "object.move" - ) - { + if matches!( + id, + "sheet.activate" + | "sheet.next" + | "sheet.previous" + | "sheet.move" + | "sheet.hide" + | "sheet.unhide" + | "home.insertSheet" + | "home.deleteSheet" + | "edit.undo" + | "edit.redo" + | "object.delete" + | "object.move" + ) { self.commit_text_box_edit()?; } if let Some(r) = self.run_ui_command(id, ¶ms) { @@ -546,6 +567,14 @@ impl SheetApp { if id == "insert.recommendedCharts" && params.get("type").is_none() { return self.open_dialog("recommendedCharts", json!({})); } + // Switch Windows with no workbook named (key tip, command search) opens the View tab's + // list to choose from; programmatic callers of `run` get the list back. + if id == "view.switchWindows" && params.get("doc").is_none_or(Json::is_null) { + self.ui.ribbon_tab = "View".into(); + self.ui.ribbon_collapsed = false; + self.open_switch_windows = true; + return; + } if let Err(e) = self.run(id, params) { self.alert(e); } @@ -882,6 +911,17 @@ impl SheetApp { } } + /// The open workbooks in order, for the Switch Windows lists. + pub fn open_windows(&self) -> Vec { + let active = self.session.active_index(); + self.session + .documents() + .iter() + .enumerate() + .map(|(i, d)| OpenWindow { doc: d.uid, title: d.display_title(), dirty: d.is_dirty(), active: i == active }) + .collect() + } + pub fn view_key(&self) -> Option<(u64, usize)> { self.session.active().map(|d| (d.uid, d.wb.active_sheet)) } @@ -961,7 +1001,7 @@ impl SheetApp { Some(t) => (t, true), None => (current.clone(), false), }; - let mut ed = editor::EditState::new(d.wb.active_sheet, at, text, replace, from_formula_bar); + let mut ed = editor::EditState::new(d.uid, d.wb.active_sheet, at, text, replace, from_formula_bar); ed.locale = locale; if replace && self.ui.autocomplete && !ed.is_formula() { ed.completion = editor::column_completion(sh, at, &ed.text); @@ -971,7 +1011,45 @@ impl SheetApp { } /// Commits the edit; `then` moves the selection (dr, dc) afterwards. + /// + /// The draft goes into the workbook it was typed in. If another workbook is showing, that + /// workbook stays active afterwards, and a draft its own workbook refuses (validation, a bad + /// formula) is dropped rather than left open over the wrong grid. pub fn commit_edit(&mut self, dr: i64, dc: i64, array: bool, fill_selection: bool) -> bool { + let Some(doc) = self.editor.as_ref().map(|ed| ed.doc) else { return true }; + let shown = self.session.active().map(|d| d.uid); + if shown == Some(doc) { + return self.commit_edit_here(dr, dc, array, fill_selection); + } + let Some(owner) = self.session.document_index(doc) else { + self.cancel_edit(); + return true; + }; + let back = self.session.active_index(); + self.session.set_active(owner); + if !self.commit_edit_here(dr, dc, array, fill_selection) { + self.cancel_edit(); + } + self.session.set_active(back); + true + } + + /// Settles the cell and text-box drafts before a command that can show another workbook or + /// write this one to a file: each is committed as Enter would, or dropped when it can't be + /// (the refusal is still reported), so a draft never lands in another workbook. + fn settle_drafts(&mut self) { + if !self.commit_edit(0, 0, false, false) { + self.cancel_edit(); + } + if let Err(e) = self.commit_text_box_edit() { + self.cancel_text_box_edit(); + if self.message.is_none() { + self.message = Some(("Text Box".into(), clean_error(&e))); + } + } + } + + fn commit_edit_here(&mut self, dr: i64, dc: i64, array: bool, fill_selection: bool) -> bool { let Some(ed) = self.editor.take() else { return true }; self.session.mode = gridcraft_engine::Mode::Ready; let text = match &ed.completion { @@ -1276,6 +1354,11 @@ impl SheetApp { } let t0 = now_ms(); text_box::before_ui(self, &ctx); + // Another workbook came forward without going through a command (which settles drafts + // first): the draft goes into its own workbook instead of floating over this one. + if self.editor.as_ref().is_some_and(|ed| self.session.active().map(|d| d.uid) != Some(ed.doc)) { + self.commit_edit(0, 0, false, false); + } // The language the widgets translate with this frame (see `i18n::current`). i18n::set_current(&ctx, self.ui.language); let t = theme::Tokens::get(&ctx); diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 356381d661..d90b642ea2 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -2336,6 +2336,7 @@ fn view(app: &mut SheetApp, ui: &mut Ui) { ], ); }); + switch_windows(app, ui); ui.vertical(|ui| { ui.label(lang.tr("Display theme")); let mode = app.ui.theme_mode(); @@ -2391,6 +2392,24 @@ fn view(app: &mut SheetApp, ui: &mut Ui) { }); } +/// View › Switch Windows: the open workbooks, the active one checked and unsaved ones marked; +/// choosing one brings it forward. +fn switch_windows(app: &mut SheetApp, ui: &mut Ui) { + let anchor = big_button(ui, Icon::Book, "Switch\nWindows", "Switch Windows", true); + anchor.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), "Switch Windows")); + control_keytip(app, ui, &anchor, "view.switchWindows"); + let forced = std::mem::take(&mut app.open_switch_windows); + egui::Popup::menu(&anchor).open_memory(popup_command(forced, anchor.clicked())).show(|ui| { + for w in app.open_windows() { + let label = format!("{}{}", if w.active { "✓ " } else { " " }, w.label()); + if ui.add(egui::Button::selectable(w.active, label).min_size(vec2(220.0, 22.0))).clicked() { + act(app, "view.switchWindows", json!({"doc": w.doc})); + ui.close(); + } + } + }); +} + fn automate(app: &mut SheetApp, ui: &mut Ui) { if big_button(ui, Icon::Script, "Macros", "Macros and scripts in this workbook (Alt+F8)", false).clicked() { app.open_dialog("macros", json!({})); diff --git a/crates/ui-egui/tests/file_menu.rs b/crates/ui-egui/tests/file_menu.rs index 047efe1b1e..415e099021 100644 --- a/crates/ui-egui/tests/file_menu.rs +++ b/crates/ui-egui/tests/file_menu.rs @@ -289,6 +289,83 @@ fn closing_the_window_can_save_first_and_a_saved_workbook_closes_at_once() { let _ = std::fs::remove_dir_all(&dir); } +#[test] +fn quitting_with_one_of_two_workbooks_unsaved_asks_once() { + let mut h = harness(Services::default()); + h.state_mut().run("cell.set", serde_json::json!({"cell": "A1", "input": "42"})).unwrap(); + h.state_mut().run("file.new", serde_json::json!({})).unwrap(); + h.run_steps(2); + + let sent = request_window_close(&mut h); + assert!(sent.contains(&egui::ViewportCommand::CancelClose), "{sent:?}"); + assert_eq!(h.state().dialog.as_ref().map(|d| d.name()), Some("saveChanges")); + assert_eq!(h.state().session.active_index(), 0, "the unsaved workbook comes forward"); + let sent = click_commands(&mut h, "Cancel"); + assert!(!sent.contains(&egui::ViewportCommand::Close), "Cancel stops the quit"); + assert_eq!(h.state().session.documents().len(), 2, "Cancel keeps both workbooks"); + + request_window_close(&mut h); + let sent = click_commands(&mut h, "Don't Save"); + assert!(sent.contains(&egui::ViewportCommand::Close), "the saved workbook isn't asked about: {sent:?}"); + assert!(h.state().dialog.is_none()); +} + +/// Book1 has a cell being typed when File › New runs (the macOS menu runs the command directly). +fn typing_in_book1_then_new(h: &mut egui_kittest::Harness<'static, SheetApp>) -> u64 { + h.input_mut().events.push(egui::Event::Text("hello".into())); + h.run_steps(2); + assert!(h.state().editor.is_some()); + let book1 = h.state().session.doc().unwrap().uid; + h.state_mut().run_or_alert("file.new", serde_json::json!({})); + h.run_steps(3); + book1 +} + +fn a1(h: &egui_kittest::Harness<'static, SheetApp>, doc: usize) -> Value { + h.state().session.documents()[doc].wb.active().unwrap().value(CellRef::new(0, 0)) +} + +#[test] +fn new_workbook_keeps_the_typed_cell_in_the_first_and_switch_windows_brings_it_back() { + let mut h = harness(Services::default()); + let book1 = typing_in_book1_then_new(&mut h); + assert!(h.state().editor.is_none(), "the draft doesn't follow into Book2"); + assert_eq!(h.state().session.documents().len(), 2); + assert_eq!(h.state().session.active_index(), 1); + assert_eq!(a1(&h, 0), Value::text("hello"), "the draft is committed into Book1, as Enter would"); + assert!(h.state().session.documents()[0].is_dirty(), "so quitting asks about Book1"); + assert_eq!(a1(&h, 1), Value::Empty); + h.key_press(egui::Key::Enter); + h.run_steps(2); + assert_eq!(a1(&h, 1), Value::Empty, "Enter in Book2 writes nothing left over"); + + // The key tip or command search opens the View tab's list; Book1 shows its unsaved bullet. + h.state_mut().run_or_alert("view.switchWindows", serde_json::json!({})); + h.run_steps(3); + assert_eq!(h.state().ui.ribbon_tab, "View"); + h.get_by_label_contains("Book1 •").click(); + h.run_steps(3); + assert_eq!(h.state().session.doc().unwrap().uid, book1); + assert_eq!(h.state().session.documents().len(), 2); + assert!(h.state().message.is_none()); +} + +#[test] +fn a_draft_its_workbook_refuses_is_dropped_when_switching_away() { + let mut h = harness(Services::default()); + let book1 = typing_in_book1_then_new(&mut h); + h.state_mut().session.execute("data.validation", serde_json::json!({"range": "A1", "type": "list", "formula1": "\"Red,Green\""})).unwrap(); + h.input_mut().events.push(egui::Event::Text("Purple".into())); + h.run_steps(2); + assert!(h.state().editor.is_some()); + h.state_mut().run_or_alert("view.switchWindows", serde_json::json!({"doc": book1})); + h.run_steps(2); + assert!(h.state().editor.is_none()); + assert_eq!(h.state().session.doc().unwrap().uid, book1, "the switch still happens"); + assert_eq!(a1(&h, 1), Value::Empty, "Book2 is unchanged"); + assert!(h.state().message.is_some(), "the refusal is reported"); +} + fn pdf_picker_harness(dir: &std::path::Path) -> (Arc>>, egui_kittest::Harness<'static, SheetApp>) { let asked = Arc::new(Mutex::new(Vec::::new())); let (capture, target) = (asked.clone(), dir.to_path_buf()); From a0d54b49d47c081b33dc18c44b65da74a3916413 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 05:12:23 +0000 Subject: [PATCH 025/687] A refused cell draft stops the command; an untouched F2 draft changes nothing - File and window commands stop when the workbook refuses the cell or text-box draft (validation Stop, bad formula, protected text box): the draft stays open in its cell with the reason shown, to fix or Esc. The per-frame backstop brings a refused draft's workbook back to the front. - An F2 draft committed with its text unchanged writes nothing: no undo step, and the workbook stays clean through Save, Print or New. - macOS Window menu: the active workbook's check is put back every poll, since choosing a check item flips it. --- apps/gridcraft/src/native_menu.rs | 39 ++++++++++++++++------------ crates/ui-egui/src/editor.rs | 5 ++++ crates/ui-egui/src/lib.rs | 43 ++++++++++++++++++------------- crates/ui-egui/tests/file_menu.rs | 38 ++++++++++++++++++++++++--- 4 files changed, 86 insertions(+), 39 deletions(-) diff --git a/apps/gridcraft/src/native_menu.rs b/apps/gridcraft/src/native_menu.rs index 2c28663791..877c82fd8a 100644 --- a/apps/gridcraft/src/native_menu.rs +++ b/apps/gridcraft/src/native_menu.rs @@ -283,27 +283,32 @@ impl NativeMenu { /// step with the session. fn sync_windows(&mut self, app: &SheetApp) { let windows = app.open_windows(); - if windows == self.windows { - return; + if windows != self.windows + && let Some(menu) = &self.window_menu + { + let same_docs = windows.len() == self.windows.len() && windows.iter().zip(&self.windows).all(|(a, b)| a.doc == b.doc); + if same_docs { + for (item, w) in self.window_items.iter().zip(&windows) { + item.set_text(w.label()); + } + } else { + for item in self.window_items.drain(..) { + let _ = menu.remove(&item); + } + for w in &windows { + let item = CheckMenuItem::with_id(format!("{WINDOW_ITEM}{}", w.doc), w.label(), true, w.active, None); + let _ = menu.append(&item); + self.window_items.push(item); + } + } + self.windows = windows; } - let Some(menu) = &self.window_menu else { return }; - let same_docs = windows.len() == self.windows.len() && windows.iter().zip(&self.windows).all(|(a, b)| a.doc == b.doc); - if same_docs { - for (item, w) in self.window_items.iter().zip(&windows) { - item.set_text(w.label()); + // AppKit flips a check item when it is chosen, so the active check is put back every poll. + for (item, w) in self.window_items.iter().zip(&self.windows) { + if item.is_checked() != w.active { item.set_checked(w.active); } - } else { - for item in self.window_items.drain(..) { - let _ = menu.remove(&item); - } - for w in &windows { - let item = CheckMenuItem::with_id(format!("{WINDOW_ITEM}{}", w.doc), w.label(), true, w.active, None); - let _ = menu.append(&item); - self.window_items.push(item); - } } - self.windows = windows; } pub fn poll(&mut self, app: &mut SheetApp, ctx: &egui::Context) { diff --git a/crates/ui-egui/src/editor.rs b/crates/ui-egui/src/editor.rs index eeeaf12da0..3f23e8bed2 100644 --- a/crates/ui-egui/src/editor.rs +++ b/crates/ui-egui/src/editor.rs @@ -45,11 +45,15 @@ pub struct EditState { pub completion: Option, /// Move the caret to the end on the next frame. pub caret_to_end: bool, + /// The cell's text when an F2-style edit began. Committing it unchanged leaves the cell alone + /// (no undo step, the workbook stays clean). `None` for an edit started by typing. + pub original: Option, } impl EditState { pub fn new(doc: u64, sheet: usize, cell: CellRef, text: String, enter_mode: bool, from_formula_bar: bool) -> EditState { let caret = text.chars().count(); + let original = (!enter_mode).then(|| text.clone()); EditState { doc, sheet, @@ -68,6 +72,7 @@ impl EditState { sync_caret: true, caret_to_end: true, completion: None, + original, } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 001abaad50..cc478168ef 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -412,7 +412,7 @@ impl SheetApp { || matches!(id, "view.newWindow" | "view.hideWindow" | "view.unhideWindow") || (id == "view.switchWindows" && params.get("doc").is_some_and(|d| !d.is_null())); if id.starts_with("file.") || switches_window { - self.settle_drafts(); + self.settle_drafts()?; } if matches!(id, "file.save" | "file.saveAs" | "file.saveBytes") { self.store_view_state(); @@ -575,8 +575,11 @@ impl SheetApp { self.open_switch_windows = true; return; } - if let Err(e) = self.run(id, params) { - self.alert(e); + match self.run(id, params) { + // A refused cell draft already shows its own message (with the rule's title). + Err(e) if self.message.as_ref().is_some_and(|(_, m)| *m == e) => {} + Err(e) => self.alert(e), + Ok(_) => {} } } @@ -1013,8 +1016,8 @@ impl SheetApp { /// Commits the edit; `then` moves the selection (dr, dc) afterwards. /// /// The draft goes into the workbook it was typed in. If another workbook is showing, that - /// workbook stays active afterwards, and a draft its own workbook refuses (validation, a bad - /// formula) is dropped rather than left open over the wrong grid. + /// workbook stays active afterwards; a draft its own workbook refuses (validation, a bad + /// formula) brings that workbook back to the front with the draft still open. pub fn commit_edit(&mut self, dr: i64, dc: i64, array: bool, fill_selection: bool) -> bool { let Some(doc) = self.editor.as_ref().map(|ed| ed.doc) else { return true }; let shown = self.session.active().map(|d| d.uid); @@ -1028,25 +1031,21 @@ impl SheetApp { let back = self.session.active_index(); self.session.set_active(owner); if !self.commit_edit_here(dr, dc, array, fill_selection) { - self.cancel_edit(); + // Refused: its workbook stays in front with the draft open and the reason shown. + return false; } self.session.set_active(back); true } - /// Settles the cell and text-box drafts before a command that can show another workbook or - /// write this one to a file: each is committed as Enter would, or dropped when it can't be - /// (the refusal is still reported), so a draft never lands in another workbook. - fn settle_drafts(&mut self) { + /// Commits the cell and text-box drafts, as Enter would, before a command that can show + /// another workbook or write this one to a file, so a draft never lands in another workbook. + /// A draft its workbook refuses stays open with the reason shown, and the command doesn't run. + fn settle_drafts(&mut self) -> Result<(), String> { if !self.commit_edit(0, 0, false, false) { - self.cancel_edit(); - } - if let Err(e) = self.commit_text_box_edit() { - self.cancel_text_box_edit(); - if self.message.is_none() { - self.message = Some(("Text Box".into(), clean_error(&e))); - } + return Err(self.message.as_ref().map(|(_, m)| m.clone()).unwrap_or_else(|| "The cell being edited can't be entered.".into())); } + self.commit_text_box_edit() } fn commit_edit_here(&mut self, dr: i64, dc: i64, array: bool, fill_selection: bool) -> bool { @@ -1056,6 +1055,13 @@ impl SheetApp { Some(full) if full.to_lowercase().starts_with(&ed.text.to_lowercase()) => full.clone(), _ => ed.text.clone(), }; + // F2 then Enter (or a file command) on an untouched draft: nothing to write. + if !fill_selection && !array && ed.original.as_ref() == Some(&text) { + if dr != 0 || dc != 0 { + self.move_after_enter(dr, dc); + } + return true; + } let text = match self.session.active().map(|d| formula_locale::canonical(&text, ed.locale, &d.wb, ed.sheet, ed.cell)) { Some(Ok(text)) => text, Some(Err(error)) => { @@ -1355,7 +1361,8 @@ impl SheetApp { let t0 = now_ms(); text_box::before_ui(self, &ctx); // Another workbook came forward without going through a command (which settles drafts - // first): the draft goes into its own workbook instead of floating over this one. + // first): the draft goes into its own workbook instead of floating over this one. If that + // workbook refuses it, it comes back to the front with the draft open. if self.editor.as_ref().is_some_and(|ed| self.session.active().map(|d| d.uid) != Some(ed.doc)) { self.commit_edit(0, 0, false, false); } diff --git a/crates/ui-egui/tests/file_menu.rs b/crates/ui-egui/tests/file_menu.rs index 415e099021..5c447cb633 100644 --- a/crates/ui-egui/tests/file_menu.rs +++ b/crates/ui-egui/tests/file_menu.rs @@ -351,19 +351,49 @@ fn new_workbook_keeps_the_typed_cell_in_the_first_and_switch_windows_brings_it_b } #[test] -fn a_draft_its_workbook_refuses_is_dropped_when_switching_away() { +fn a_draft_its_workbook_refuses_stops_the_switch_and_stays_open() { let mut h = harness(Services::default()); let book1 = typing_in_book1_then_new(&mut h); + let book2 = h.state().session.doc().unwrap().uid; h.state_mut().session.execute("data.validation", serde_json::json!({"range": "A1", "type": "list", "formula1": "\"Red,Green\""})).unwrap(); h.input_mut().events.push(egui::Event::Text("Purple".into())); h.run_steps(2); - assert!(h.state().editor.is_some()); h.state_mut().run_or_alert("view.switchWindows", serde_json::json!({"doc": book1})); h.run_steps(2); - assert!(h.state().editor.is_none()); - assert_eq!(h.state().session.doc().unwrap().uid, book1, "the switch still happens"); + assert_eq!(h.state().session.doc().unwrap().uid, book2, "the switch doesn't happen"); + assert_eq!(h.state().editor.as_ref().map(|ed| ed.text.as_str()), Some("Purple"), "the draft stays open to fix"); assert_eq!(a1(&h, 1), Value::Empty, "Book2 is unchanged"); assert!(h.state().message.is_some(), "the refusal is reported"); + + // Esc drops the draft; then the switch goes through. + h.state_mut().message = None; + h.state_mut().cancel_edit(); + h.state_mut().run_or_alert("view.switchWindows", serde_json::json!({"doc": book1})); + h.run_steps(2); + assert_eq!(h.state().session.doc().unwrap().uid, book1); + assert!(h.state().message.is_none()); +} + +#[test] +fn an_untouched_f2_draft_leaves_the_workbook_clean_on_a_file_command() { + let dir = std::env::temp_dir().join(format!("gridcraft-f2-draft-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("Book.xlsx").to_string_lossy().into_owned(); + let mut h = harness(Services::default()); + h.state_mut().run("cell.set", serde_json::json!({"cell": "A1", "input": "=1+2"})).unwrap(); + h.state_mut().run("file.save", serde_json::json!({"path": &path})).unwrap(); + h.run_steps(2); + let undo = h.state().session.doc().unwrap().undo.len(); + h.key_press(egui::Key::F2); + h.run_steps(2); + assert_eq!(h.state().editor.as_ref().map(|ed| ed.text.as_str()), Some("=1+2")); + h.state_mut().run_or_alert("file.new", serde_json::json!({})); + h.run_steps(2); + assert!(h.state().editor.is_none()); + let book1 = &h.state().session.documents()[0]; + assert!(!book1.is_dirty(), "nothing was typed, so nothing changed"); + assert_eq!(book1.undo.len(), undo, "and no undo step was added"); + let _ = std::fs::remove_dir_all(&dir); } fn pdf_picker_harness(dir: &std::path::Path) -> (Arc>>, egui_kittest::Harness<'static, SheetApp>) { From 4bafd4e8ee5f38b068720156b9057eed5ed82a80 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 05:12:23 +0000 Subject: [PATCH 026/687] Docs: Switch Windows and drafts kept in their workbook (UI parity, progress log) --- ROADMAP.md | 4 +++- docs/ui-parity.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 2e04e91232..9617ef1e68 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (Page Setup paper sizes: JIS B4/B5/B6, the Japanese postcards and envelopes, and A6; long flat operator chains up to Excel's formula limit) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -135,6 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-22 | Typing in a cell and then choosing File › New no longer strands the first workbook: the entry is committed into its own workbook before any File or window command (a refused entry stays open and stops the command), View › Switch Windows and the macOS Window menu list the open workbooks with unsaved markers, `view.switchWindows {doc}` switches by a stable workbook id, and the macOS Quit item asks about unsaved workbooks | | 2026-10-21 | Page Setup paper sizes match Japanese Excel: `B4`/`B5`/`B6` are the JIS sizes (ISO B4/B5/B6 stay reachable by name), so a Japanese B5 worksheet prints at 182×257 mm; Japanese Postcard (100×148 mm), Double Postcard (148×200 mm), Envelope #3/#4 (120×235/90×205 mm), Kaku #2/#3 (240×332/216×277 mm) and A6 are added, both sizes reach the PDF page (`file.exportPdf`/`file.print` MediaBox) and the size survives an XLSX round-trip; the Page Layout Size menu lists them. `B4`/`B5` used to be the ISO sizes, which is changed here | | 2026-10-20 | Japanese interface catalog covers every key the Brazilian Portuguese catalog translates: the 398-label shared core plus 545 dialog, message, Backstage and menu strings (943 total, ~43% of ~2,200). A test parses `pt.rs`'s source, so the two catalogs stay in step | | 2026-10-20 | Interface translated into Dutch (518 labels: ribbon, context menus, File view, dialog titles, Options); `nl`, `nl-NL`, `nl-BE` locale tags resolve to Dutch | @@ -220,6 +221,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Progress log: drafts stay in their workbook, Switch Windows, macOS Quit asks to save | | 2026-10-21 | minor | Page Setup paper sizes: JIS B4/B5/B6 (ISO by name), Japanese postcards and envelopes, A6; PDF page size follows, XLSX round-trips | | 2026-10-20 | minor | Dutch (nl) listed under other shipped languages; progress log row | | 2026-10-19 | minor | Progress log: long flat operator chains (#916) | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 07797d70d0..b70682ac3d 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Switch Windows lists the open workbooks; a cell being typed stays in its own workbook through New, Open and window switches; macOS Quit asks about unsaved workbooks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -34,6 +34,7 @@ Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes | Dark mode, system appearance | yes | yes | done (10-09) | | Page Layout view (paper view with rulers and header editing) | yes | yes (#329, 10-11): per-sheet view saved in XLSX, pages with margins and gaps on the print pagination, "Click to add data" pages, rulers (inches or centimetres), headers and footers drawn and edited in place with a Header & Footer tab | margins drag on the rulers (#792, with header and footer margins); header/footer pictures, Different First Page / Odd & Even missing (1–3 h) | | Multiple windows per workbook | yes | no (#162) | see [hardware-parity.md](hardware-parity.md) | +| Switch Windows, unsaved workbooks on quit | yes | yes (10-22): View › Switch Windows and the macOS Window menu list the open workbooks (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {doc}`); a cell being typed is entered into its own workbook before New, Open, Close, Save, Print and window switches, and a refused entry stays open and stops the command; quitting (window close, Cmd+Q, the macOS Quit item) asks Save / Don't Save / Cancel for each unsaved workbook | one OS window for all workbooks (#162) | ## Grid interaction @@ -176,6 +177,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | New workbook no longer loses the first one: the cell being typed is entered into its own workbook before any File or window command (an entry the workbook refuses stays open and the command doesn't run; an untouched F2 draft changes nothing), the editor is tied to its workbook, View › Switch Windows and the macOS Window menu list the open workbooks, and the macOS Quit item asks about unsaved workbooks like the close button | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | | 2026-10-21 | minor | Form dialogs (conditional-format quick rules, New Name, Remove Duplicates, Text to Columns and every other `Dialog::form`) fit their content: OK and Cancel sit in a row under the last field instead of a right-to-left layout that took the screen's whole height and left the buttons floating halfway down | From eb3eaa052db54ae3723569b7dcef2a0b89107846 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Thu, 22 Oct 2026 01:36:01 -0400 Subject: [PATCH 027/687] Engine: a command keeps its group's name only for a group it ended itself The record of the last group step (document, history length, epoch) outlived the Undo of that step. A later command that opened and closed its own groups at the same history length, after rolling one back, passed the epoch check (the record's epoch merely differed from the one the command started with) and took the name of the nested edit it had made instead of its own. The step must now come from a group ended during the command (an epoch past the command's starting one), and a rollback drops a record of a step it truncated away. --- crates/engine/src/lib.rs | 9 ++++++++- crates/engine/src/tests/seams.rs | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index fc859e16a9..1dc9196d42 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -457,8 +457,11 @@ impl Session { let mut label = spec.label.trim_end_matches('…').to_string(); if own_groups && let Some((_, len)) = start { // A command whose only step is its group's keeps that name ("Run Macro: Totals"). + // The step must have been made by a group this command ended (its epoch is + // past the one the command started with): an older record at the same length + // belongs to an earlier command, undone since. if d.undo.len() == len + 1 - && self.group_step.is_some_and(|(doc, n, epoch)| doc == uid && n == len + 1 && epoch != group_epoch) + && self.group_step.is_some_and(|(doc, n, epoch)| doc == uid && n == len + 1 && epoch > group_epoch) && let Some(last) = d.undo.last() { label = last.label.clone(); @@ -567,6 +570,10 @@ impl Session { d.undo.truncate(g.start); d.redo = g.redo; d.revision += 1; + // A step the rollback dropped is no longer anyone's to name. + if self.group_step.is_some_and(|(doc, n, _)| doc == g.doc && n > g.start) { + self.group_step = None; + } Ok(()) } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 16b2617d70..e1aaaba674 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -257,6 +257,27 @@ fn a_command_that_runs_its_own_group_makes_one_step() { assert!(v(&s, "B2").is_empty()); } +#[test] +fn a_stale_group_step_does_not_name_a_later_command() { + // The record of the last group step (document, length, epoch) survives the Undo of that step. + // A later command that ends a group of its own at the same history length, after rolling + // another back, must not take the name of the nested edit it made. + register_group_commands(); + let mut s = s(); + s.execute("test.onlyGroup", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["Named Step"]); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("test.failThenEdit", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["test.failThenEdit"]); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("test.failThenEdit", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["test.failThenEdit"]); + assert!(v(&s, "B1").is_empty()); + assert_eq!(v(&s, "B2").display(), "kept"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "B2").is_empty() && v(&s, "D1").is_empty()); +} + /// Fills the undo history to the limit with edits of Z1 ("v0" to "v99"), and returns its snapshots. fn fill_history(s: &mut Session) -> Vec> { for i in 0..crate::UNDO_LIMIT { From 2287f63a95139db4cb4d0d42e8c238e6f34319a5 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:00:54 -0400 Subject: [PATCH 028/687] Engine: a group rolled back inside a command keeps the redo history A command that changed the workbook cleared the redo history even when an open group owned its step. When the group was then rolled back inside the same command (the debugger's Stop with Undo Changes), the rollback put the redo history back and the command's bookkeeping cleared it again. Now only a command that makes a step of its own clears the redo history; a group clears it when it ends as a step, and a rollback restores it. --- crates/engine/src/lib.rs | 4 +++- crates/engine/src/tests/seams.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 1dc9196d42..80bcfd5ae4 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -469,8 +469,10 @@ impl Session { d.undo.truncate(len); } d.undo.push(HistoryEntry { label, wb, selection: sel }); + d.redo.clear(); } - d.redo.clear(); + // Inside a group the redo history waits for the group: `group_end` clears it when the + // group becomes a step, and `group_rollback` puts it back as it was. d.revision += 1; } // The history limit applies once the outermost command has collapsed its steps, so the diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index e1aaaba674..b3397c724e 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -226,6 +226,13 @@ fn register_group_commands() { }); // A command that is one group keeps the group's name. register("test.onlyGroup", |s, _| s.grouped("Named Step", |s| put(s, "D1", "x")).map(|_| json!({}))); + // An edit, then the rollback of the innermost group that was open before the command. + register("test.rollbackOpenGroup", |s, _| { + put(s, "E1", "gone")?; + let token = s.groups.last().map(|g| crate::GroupToken { id: g.id, doc: g.doc }); + s.group_rollback(token.ok_or_else(|| EngineError::Other("no group".into()))?)?; + Ok(json!({})) + }); // An edit, then the end of the innermost group that was open before the command. register("test.endOpenGroup", |s, _| { put(s, "E1", "end")?; @@ -278,6 +285,25 @@ fn a_stale_group_step_does_not_name_a_later_command() { assert!(v(&s, "B2").is_empty() && v(&s, "D1").is_empty()); } +#[test] +fn a_group_rolled_back_inside_a_command_keeps_the_redo_history() { + // The debugger's Stop with Undo Changes rolls its group back inside a command. The rollback + // put the redo history back, and the command's own bookkeeping then cleared it again. + register_group_commands(); + let mut s = s(); + set(&mut s, "A1", "first"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.doc().unwrap().redo.len(), 1); + let token = s.group_begin("Debug").unwrap(); + set(&mut s, "B1", "inside"); + s.execute("test.rollbackOpenGroup", json!({})).unwrap(); + assert!(!s.in_group(token.doc)); + assert!(v(&s, "B1").is_empty() && v(&s, "E1").is_empty()); + assert_eq!(s.doc().unwrap().redo.len(), 1, "the redo history survives the rolled-back group"); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(v(&s, "A1").display(), "first"); +} + /// Fills the undo history to the limit with edits of Z1 ("v0" to "v99"), and returns its snapshots. fn fill_history(s: &mut Session) -> Vec> { for i in 0..crate::UNDO_LIMIT { From 471114e29663ac231bd0e4f0bc63c7bd07d59d5b Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:14:55 -0400 Subject: [PATCH 029/687] Engine: a visible name of the item shape is not workbook data The item store read, listed and removed every workbook-scope name of the `_gc__` shape, visible or not, so saving an item removed a person's own visible name that happened to look like one. Items are the hidden names the store writes; a legacy `_sc_` name still reads. A visible `_gc_` name is left alone by every operation, and writing an item that would need that name is refused before anything changes: a hidden name beside it with the same name is the one an xlsx file drops, so the item would be lost on save. --- crates/engine/src/cmd/stash.rs | 54 ++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index fd8612ad05..9043262d22 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -30,8 +30,8 @@ pub const MAX_ITEM_CHARS: usize = 4 * 1024 * 1024; pub fn get(wb: &Workbook, key: &str) -> Option { let mut chunks: BTreeMap = BTreeMap::new(); let mut legacy = None; - for n in wb.names.iter().filter(|n| n.scope.is_none()) { - match parse_name(&n.name) { + for n in wb.names.iter() { + match item_of(n) { Some((k, Some(i))) if k.eq_ignore_ascii_case(key) => { chunks.entry(i).or_insert(&n.formula); } @@ -63,8 +63,19 @@ pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { if text.chars().count() > MAX_ITEM_CHARS { return Err(EngineError::Other(format!("The data is too large to keep in the workbook ({} MB at most).", MAX_ITEM_CHARS / (1024 * 1024)))); } + let formulas = chunk_formulas(text); + // A visible name of the item's shape is someone's own (see `item_of`); a hidden name beside + // it with the same name would be the one an xlsx file drops. Refuse before changing anything. + for i in 0..formulas.len() { + let name = format!("{PREFIX}{key}_{i}"); + if wb.names.iter().any(|n| n.scope.is_none() && !n.hidden && n.name.eq_ignore_ascii_case(&name)) { + return Err(EngineError::Other(format!( + "The workbook has a defined name '{name}', which GridCraft needs for its own data. Rename it first." + ))); + } + } remove(wb, key); - for (i, formula) in chunk_formulas(text).into_iter().enumerate() { + for (i, formula) in formulas.into_iter().enumerate() { wb.names.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true }); } Ok(()) @@ -72,7 +83,7 @@ pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { /// Removes the item stored under `key`. pub fn remove(wb: &mut Workbook, key: &str) { - wb.names.retain(|n| n.scope.is_some() || !parse_name(&n.name).is_some_and(|(k, _)| k.eq_ignore_ascii_case(key))); + wb.names.retain(|n| !item_of(n).is_some_and(|(k, _)| k.eq_ignore_ascii_case(key))); } /// The item under `key` as JSON (`None` when missing or not JSON). @@ -89,8 +100,7 @@ pub fn keys(wb: &Workbook, prefix: &str) -> Vec { let mut keys: Vec = wb .names .iter() - .filter(|n| n.scope.is_none()) - .filter_map(|n| parse_name(&n.name).map(|(k, _)| k.to_ascii_lowercase())) + .filter_map(|n| item_of(n).map(|(k, _)| k.to_ascii_lowercase())) .filter(|k| k.starts_with(&prefix.to_ascii_lowercase())) .collect(); keys.sort(); @@ -103,6 +113,16 @@ pub fn item_key(name: &str) -> Option { parse_name(name).map(|(k, _)| k.to_ascii_lowercase()) } +/// The item a name holds a part of: a hidden workbook-scope `_gc__` name, or a legacy +/// `_sc_` one. A visible name of the `_gc_` shape is someone's own name, not an item: it is +/// neither read, nor listed, nor removed on write. +fn item_of(n: &DefinedName) -> Option<(&str, Option)> { + if n.scope.is_some() { + return None; + } + parse_name(&n.name).filter(|(_, chunk)| chunk.is_none() || n.hidden) +} + /// `_gc__` → (`key`, `Some(k)`); `_sc_` → (`key`, `None`). fn parse_name(name: &str) -> Option<(&str, Option)> { if let Some(rest) = strip_prefix_ci(name, PREFIX) { @@ -313,6 +333,28 @@ mod tests { assert_eq!(get(&wb, "huge"), None); } + #[test] + fn a_visible_name_of_the_item_shape_is_not_an_item() { + // Someone's own visible `_gc_watch_0` is not storage: it is not read or listed, and + // writing or removing the item leaves it alone. + let mut wb = Workbook::new(); + let own = DefinedName { hidden: false, ..name("_gc_watch_0", "\"mine\"") }; + wb.names.push(own.clone()); + assert_eq!(get(&wb, "watch"), None); + assert!(keys(&wb, "").is_empty()); + // Writing the item would put a hidden name of the same name beside it, and an xlsx file + // keeps only one of them: refused, with the names as they were. + let err = set(&mut wb, "watch", "theirs").unwrap_err().to_string(); + assert!(err.contains("_gc_watch_0"), "{err}"); + assert_eq!(wb.names, vec![own.clone()]); + assert_eq!(get(&wb, "watch"), None); + set(&mut wb, "other", "theirs").unwrap(); + assert_eq!(get(&wb, "other").as_deref(), Some("theirs")); + remove(&mut wb, "watch"); + remove(&mut wb, "other"); + assert_eq!(wb.names, vec![own]); + } + #[test] fn keys_list_items_by_prefix() { let mut wb = Workbook::new(); From a4b4872602fae4a08530c1b875ad7e2984548f15 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:09:12 -0400 Subject: [PATCH 030/687] Model: macros as typed command lists, saved in the workbook A macro is plain data in the model (model/src/macros.rs): a name, a description, an optional shortcut, declared inputs (shape, header, column types, non-empty, default reference), steps, an output rule (anchor and what to do on a conflict) and the commands it may run that read or write files. A step runs a command (id and params) or is one of three bounded control steps: Repeat over the rows or columns of a range, If on one cell's value, and Stop with a message. Every step carries enabled, breakpoint and note flags. These are GridCraft macros, not VBA; a VBA project in a file stays preserved and is never run. Workbook::macros holds them. The JSON format keeps the field. In xlsx, io::save_bytes writes each macro as one chunked hidden-name item, _gc_macro_, and io::open_bytes takes the items back out of the names, so the names never linger in the open document. Limits are checked whenever a macro is read from a file or a command: at most 1,000 macros, 10,000 steps each, Repeat and If nested 4 deep, names and notes of bounded length, and at most 4,194,304 characters of JSON, so that saving a workbook never fails because of a macro; nesting is checked before the steps are deserialized. What is over or damaged is dropped with one warning, and clashing names get a suffix. Automate scripts keep their own storage (_sc_scripts) and are neither read nor changed by this. --- crates/engine/src/io.rs | 15 +- crates/engine/src/lib.rs | 1 + crates/engine/src/macros/mod.rs | 166 ++++++++++++++ crates/engine/src/macros/tests.rs | 195 +++++++++++++++++ crates/model/Cargo.toml | 4 +- crates/model/src/lib.rs | 2 + crates/model/src/macros.rs | 351 ++++++++++++++++++++++++++++++ crates/model/src/workbook.rs | 5 + 8 files changed, 734 insertions(+), 5 deletions(-) create mode 100644 crates/engine/src/macros/mod.rs create mode 100644 crates/engine/src/macros/tests.rs create mode 100644 crates/model/src/macros.rs diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 14717a3df8..4d19205c0c 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -118,8 +118,14 @@ fn decrypt_message(name: &str, e: gridcraft_xlsx::DecryptError) -> String { } } -/// Parses bytes that aren't encrypted. +/// Parses bytes that aren't encrypted, then takes the workbook's macros out of their hidden names. fn open_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook, Vec)> { + let (mut wb, mut warnings) = read_plain(name, bytes, csv_as_text)?; + warnings.extend(crate::macros::load(&mut wb)); + Ok((wb, warnings)) +} + +fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook, Vec)> { let kind = FileKind::from_path(name); let sniffed = gridcraft_xlsx::sniff(bytes); if sniffed == gridcraft_xlsx::Format::Xls { @@ -207,7 +213,12 @@ pub fn save_bytes_as(wb: &Workbook, path: &str, kind: FileKind) -> Result gridcraft_xlsx::write_xlsx_as(wb, gridcraft_xlsx::XlsxKind::from_path(path)).map_err(|e| EngineError::Other(e.to_string())), + // Macros travel in hidden names. + FileKind::Xlsx => { + let named = crate::macros::with_names(wb)?; + gridcraft_xlsx::write_xlsx_as(named.as_ref().unwrap_or(wb), gridcraft_xlsx::XlsxKind::from_path(path)) + .map_err(|e| EngineError::Other(e.to_string())) + } FileKind::Xls => { Err(EngineError::Other("Excel 97-2003 (.xls) is supported for import only. Save as .xlsx or another supported export format.".into())) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 80bcfd5ae4..7f63001d23 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -14,6 +14,7 @@ pub mod display; pub mod fill; pub mod io; pub mod links; +pub mod macros; pub mod outline; pub mod paper; pub mod pivot; diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs new file mode 100644 index 0000000000..1413236500 --- /dev/null +++ b/crates/engine/src/macros/mod.rs @@ -0,0 +1,166 @@ +//! GridCraft macros: typed, declarative command lists, not VBA. The model is plain data in +//! `gridcraft_model::Macro`; this module reads and writes it and keeps it within its limits. +//! +//! Storage: the JSON format keeps `Workbook::macros` as a field. In xlsx each macro is one item of +//! GridCraft's hidden workbook data ([`crate::cmd::stash`]), `_gc_macro__`, written when the +//! file is saved and taken back out of the names when it is opened, so the names never linger in +//! the open document. Scripts made with Automate › New Script or Record Actions are a separate, +//! older feature with their own storage; macros neither read nor change them. + +use gridcraft_model::{Macro, Workbook}; +use serde_json::{Value as Json, json}; + +use crate::cmd::stash; + +/// The hidden-data key prefix of a stored macro (`macro_`). +const MACRO_KEY: &str = "macro_"; + +/// Reads a macro from JSON, as `macro.save` and files give it. A command step may also use the +/// short form `{"command": "cell.set", "params": {…}}`. +pub fn parse_macro(v: &Json) -> Result { + let mut v = v.clone(); + if let Some(steps) = v.get_mut("steps") { + normalize_steps(steps, 1)?; + } + if v.get("v").is_none() + && let Some(o) = v.as_object_mut() + { + o.insert("v".into(), json!(gridcraft_model::MACRO_VERSION)); + } + let mut m: Macro = serde_json::from_value(v).map_err(|e| format!("This isn't a valid macro: {e}"))?; + m.name = m.name.trim().to_string(); + m.check_limits()?; + if m.name.chars().any(char::is_control) { + return Err("A macro name can't hold line breaks or other control characters.".into()); + } + check_size(&m)?; + Ok(m) +} + +/// Refuses a macro too large to keep in an xlsx file ([`stash::MAX_ITEM_CHARS`] characters of +/// JSON), so saving a workbook never fails because of one of its macros. +fn check_size(m: &Macro) -> Result<(), String> { + if macro_json(m).to_string().chars().count() > stash::MAX_ITEM_CHARS { + return Err(format!("This macro is too large to keep in the workbook ({} characters at most).", stash::MAX_ITEM_CHARS)); + } + Ok(()) +} + +/// Rewrites the short step form into the tagged one, in place, refusing nesting past the limit +/// before the steps are deserialized. +fn normalize_steps(steps: &mut Json, level: usize) -> Result<(), String> { + let Some(list) = steps.as_array_mut() else { return Err("`steps` must be a list.".into()) }; + if level > gridcraft_model::MAX_MACRO_DEPTH { + return Err(format!("Repeat and If steps can nest at most {} deep.", gridcraft_model::MAX_MACRO_DEPTH)); + } + for st in list { + let Some(o) = st.as_object_mut() else { return Err("Every step must be an object.".into()) }; + if !o.contains_key("type") + && let Some(id) = o.remove("command") + { + o.insert("type".into(), json!("command")); + o.insert("id".into(), id); + } + for key in ["body", "then", "else"] { + if let Some(body) = o.get_mut(key) { + normalize_steps(body, level + 1)?; + } + } + } + Ok(()) +} + +/// A macro as JSON (the shape `macro.get` returns and `macro.save` takes). +pub fn macro_json(m: &Macro) -> Json { + serde_json::to_value(m).unwrap_or(Json::Null) +} + +/// The index of the macro called `name` (case-insensitive). +pub fn find(wb: &Workbook, name: &str) -> Option { + let name = name.trim(); + wb.macros.iter().position(|m| m.name.eq_ignore_ascii_case(name)) +} + +/// A copy of `wb` for xlsx with each macro in hidden names (`_gc_macro__`) instead of the +/// field, or `None` when there is nothing to change. +pub fn with_names(wb: &Workbook) -> crate::Result> { + let stale = !stash::keys(wb, MACRO_KEY).is_empty(); + if wb.macros.is_empty() && !stale { + return Ok(None); + } + let mut out = wb.clone(); + for key in stash::keys(&out, MACRO_KEY) { + stash::remove(&mut out, &key); + } + for (i, m) in wb.macros.iter().enumerate() { + stash::set(&mut out, &format!("{MACRO_KEY}{i}"), ¯o_json(m).to_string())?; + } + out.macros.clear(); + Ok(Some(out)) +} + +/// After a file is opened: moves macros out of hidden names into `Workbook::macros` and drops +/// macros that are damaged or over the limits. Returns a warning for what was dropped. +pub fn load(wb: &mut Workbook) -> Vec { + let mut warnings = Vec::new(); + let mut bad = 0usize; + // Stored macros, in their saved order. + let mut keyed: Vec<(u64, String)> = + stash::keys(wb, MACRO_KEY).into_iter().filter_map(|k| k.strip_prefix(MACRO_KEY).and_then(|n| n.parse().ok()).map(|n| (n, k))).collect(); + keyed.sort(); + let mut found = Vec::new(); + for (_, key) in &keyed { + // Past the macro limit, the rest are counted without being read. + if found.len() >= gridcraft_model::MAX_MACROS { + bad += 1; + continue; + } + match stash::get_json(wb, key).ok_or_else(|| "unreadable".to_string()).and_then(|v| parse_macro(&v)) { + Ok(m) => found.push(m), + Err(_) => bad += 1, + } + } + for key in stash::keys(wb, MACRO_KEY) { + stash::remove(wb, &key); + } + // Macros that came as a field (the JSON format) go through the same checks. + let mut all = std::mem::take(&mut wb.macros); + all.extend(found); + for m in all { + if wb.macros.len() >= gridcraft_model::MAX_MACROS { + bad += 1; + continue; + } + match m.check_limits().and_then(|()| check_size(&m)) { + Ok(()) => { + let mut m = m; + m.name = unique_name(wb, m.name.trim()); + wb.macros.push(m); + } + Err(_) => bad += 1, + } + } + if bad > 0 { + warnings.push(format!( + "{bad} macro{} in the workbook couldn't be opened: {} damaged or over the limits ({} macros, {} steps each, Repeat and If nested {} deep).", + if bad == 1 { "" } else { "s" }, + if bad == 1 { "it is" } else { "they are" }, + gridcraft_model::MAX_MACROS, + gridcraft_model::MAX_MACRO_STEPS, + gridcraft_model::MAX_MACRO_DEPTH + )); + } + warnings +} + +/// `name`, or `name (2)`, `name (3)`… if a macro already has it. +pub fn unique_name(wb: &Workbook, name: &str) -> String { + let name = if name.is_empty() { "Macro" } else { name }; + if find(wb, name).is_none() { + return name.to_string(); + } + (2..).map(|n| format!("{name} ({n})")).find(|n| find(wb, n).is_none()).unwrap_or_else(|| name.to_string()) +} + +#[cfg(test)] +mod tests; diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs new file mode 100644 index 0000000000..1b417c8a04 --- /dev/null +++ b/crates/engine/src/macros/tests.rs @@ -0,0 +1,195 @@ +use gridcraft_model::*; +use serde_json::json; + +use super::*; + +fn sample_macro(name: &str) -> Macro { + parse_macro(&json!({ + "name": name, + "description": "Totals the amounts", + "shortcut": "Ctrl+Shift+T", + "inputs": [{"name": "Input", "rows": [1, 500], "cols": [2, 2], "header": true, "colTypes": ["text", "number"], "nonEmpty": true}], + "steps": [ + {"type": "command", "id": "cell.set", "params": {"cell": "Output", "input": "=SUM({{Input.col(2)}})"}, "breakpoint": true, "note": "the total"}, + {"type": "repeat", "over": "Input.rows(2..)", "of": "rows", "max": 100, "body": [ + {"type": "if", "cell": "Row.cell(1,2)", "op": "gt", "value": 100, "then": [ + {"command": "home.bold", "params": {"range": "Row"}} + ], "else": [{"type": "stop", "message": "done"}]} + ]}, + {"type": "command", "id": "home.italic", "enabled": false} + ], + "output": {"anchor": "Input.offset(0,3)", "onConflict": "shiftDown"}, + "permissions": ["data.getData"] + })) + .unwrap() +} + +#[test] +fn macros_read_from_json_in_both_step_forms() { + let m = sample_macro("Totals"); + assert_eq!(m.v, MACRO_VERSION); + assert_eq!(m.inputs[0].col_types, [ColType::Text, ColType::Number]); + assert_eq!(m.output.on_conflict, OnConflict::ShiftDown); + assert_eq!(m.step_count(), 6); + assert!(m.steps[0].breakpoint && m.steps[0].enabled && !m.steps[2].enabled); + let StepKind::Repeat { body, max, .. } = &m.steps[1].kind else { panic!("{:?}", m.steps[1]) }; + assert_eq!(*max, 100); + let StepKind::If { then, otherwise, op, .. } = &body[0].kind else { panic!() }; + assert_eq!(*op, CompareOp::Gt); + // The short form of earlier scripts reads as a command step. + assert_eq!(then[0].kind, StepKind::Command { id: "home.bold".into(), params: json!({"range": "Row"}) }); + assert!(matches!(&otherwise[0].kind, StepKind::Stop { message, rollback: false } if message == "done")); + // Writing and reading again gives the same macro; defaults stay out of the JSON. + let v = macro_json(&m); + assert_eq!(v["steps"][0]["type"], "command"); + assert!(v["steps"][1].get("enabled").is_none() && v["steps"][1].get("breakpoint").is_none()); + assert_eq!(v["steps"][2]["enabled"], false); + assert_eq!(parse_macro(&v).unwrap(), m); + // A minimal macro gets the defaults. + let m = parse_macro(&json!({"name": " Bare "})).unwrap(); + assert_eq!((m.name.as_str(), m.output.on_conflict, m.inputs.len()), ("Bare", OnConflict::Ask, 0)); +} + +#[test] +fn macros_over_the_limits_are_refused() { + let flat = |n: usize| json!({"name": "Big", "steps": vec![json!({"command": "cell.set"}); n]}); + assert!(parse_macro(&flat(MAX_MACRO_STEPS)).is_ok()); + assert!(parse_macro(&flat(MAX_MACRO_STEPS + 1)).unwrap_err().contains("10000 steps")); + // Nested Repeat steps: 4 levels are fine, 5 are not. + let nest = |levels: usize| { + let mut step = json!({"command": "cell.set"}); + for _ in 1..levels { + step = json!({"type": "repeat", "over": "Input", "body": [step]}); + } + json!({"name": "Deep", "steps": [step]}) + }; + assert!(parse_macro(&nest(4)).is_ok()); + assert!(parse_macro(&nest(5)).unwrap_err().contains("4 deep")); + // Far deeper than serde would allow is refused before it is read. + assert!(parse_macro(&nest(200)).is_err()); + for bad in [ + json!({"name": ""}), + json!({"name": "x".repeat(256)}), + json!({"name": "a\nb"}), + json!({"name": "A", "steps": {}}), + json!({"name": "A", "steps": [1]}), + json!({"name": "A", "steps": [{"type": "nope"}]}), + json!({"name": "A", "steps": [{"type": "if", "cell": "A1", "op": "between"}]}), + json!({"name": "A", "inputs": [{"name": ""}]}), + json!({"name": "A", "inputs": [{"name": "Has space"}]}), + json!({"name": "A", "inputs": [{"name": "row"}]}), + json!({"name": "A", "inputs": [{"name": "In"}, {"name": "IN"}]}), + json!({"name": "A", "output": {"onConflict": "explode"}}), + json!([]), + ] { + assert!(parse_macro(&bad).is_err(), "{bad}"); + } +} + +fn names_of(wb: &Workbook) -> Vec { + wb.names.iter().map(|n| n.name.clone()).collect() +} + +#[test] +fn macros_round_trip_through_xlsx_and_json() { + let mut wb = Workbook::new(); + wb.macros.push(sample_macro("Totals")); + // Large enough to need several hidden names. + let long: Vec = (0..300).map(|r| Step::command("cell.set", json!({"cell": format!("A{r}"), "input": "x\"y".repeat(10)}))).collect(); + wb.macros.push(Macro { name: "Long".into(), steps: long, ..Macro::default() }); + for file in ["book.xlsx", "book.json"] { + let bytes = crate::io::save_bytes(&wb, file).unwrap(); + let (back, warnings) = crate::io::open_bytes(file, &bytes).unwrap(); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + assert_eq!(back.macros, wb.macros, "{file}"); + assert!(back.names.is_empty(), "{file}: macros stay out of the names once opened: {:?}", names_of(&back)); + } + // In the xlsx file they are hidden names; the long one spans several. + let bytes = crate::io::save_bytes(&wb, "book.xlsx").unwrap(); + let (raw, _) = gridcraft_xlsx::read_xlsx(&bytes).unwrap(); + let names = names_of(&raw); + assert!(names.contains(&"_gc_macro_0_0".to_string()), "{names:?}"); + assert!(names.iter().filter(|n| n.starts_with("_gc_macro_1_")).count() >= 2, "{names:?}"); + assert!(raw.names.iter().all(|n| n.hidden)); + // Saving without macros writes no macro names, even if stale ones linger. + let mut empty = raw.clone(); + empty.macros.clear(); + let (again, _) = gridcraft_xlsx::read_xlsx(&crate::io::save_bytes(&empty, "x.xlsx").unwrap()).unwrap(); + assert!(again.names.iter().all(|n| !n.name.starts_with("_gc_macro_"))); + // The JSON format keeps the field (and leaves it out when there are no macros). + let json = String::from_utf8(crate::io::save_bytes(&wb, "book.json").unwrap()).unwrap(); + assert!(json.contains("\"macros\"")); + let plain = String::from_utf8(crate::io::save_bytes(&Workbook::new(), "book.json").unwrap()).unwrap(); + assert!(!plain.contains("\"macros\"")); +} + +fn hidden(name: &str, text: &str) -> DefinedName { + DefinedName { name: name.into(), scope: None, formula: format!("\"{}\"", text.replace('"', "\"\"")), comment: String::new(), hidden: true } +} + +#[test] +fn scripts_and_vba_stay_as_they_are() { + // Automate scripts keep their own storage, and a VBA project is carried through unchanged. + let mut wb = Workbook::new(); + let scripts = json!({"Fill": [{"command": "cell.set", "params": {"cell": "A1", "input": "x"}}]}); + wb.names.push(hidden("_sc_scripts", &scripts.to_string())); + wb.vba_project = Some(b"\xd0\xcf\x11\xe0 synthetic vbaProject".to_vec()); + wb.macros.push(sample_macro("Totals")); + let bytes = crate::io::save_bytes(&wb, "book.xlsm").unwrap(); + let (back, warnings) = crate::io::open_bytes("book.xlsm", &bytes).unwrap(); + // The only warning is the reader's note that the VBA project is kept and not run. + assert!(warnings.iter().all(|w| w.contains("VBA project")), "{warnings:?}"); + assert_eq!(back.macros, wb.macros); + assert_eq!(names_of(&back), ["_sc_scripts"], "the scripts item is untouched"); + assert_eq!(back.names[0].formula, wb.names[0].formula); + assert_eq!(back.vba_project, wb.vba_project); +} + +#[test] +fn damaged_or_oversized_macros_are_dropped_with_a_warning() { + let mut wb = Workbook::new(); + wb.names.push(hidden("_gc_macro_0_0", &json!({"name": "Good"}).to_string())); + wb.names.push(hidden("_gc_macro_1_0", "{not json")); + wb.names.push(hidden("_gc_macro_2_0", &json!({"name": "Huge", "steps": vec![json!({"command": "x"}); MAX_MACRO_STEPS + 1]}).to_string())); + wb.names.push(hidden("_gc_macro_3_0", &json!({"name": "good"}).to_string())); + wb.names.push(hidden("_gc_macro_10_0", &json!({"name": "Tenth"}).to_string())); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let (back, warnings) = crate::io::open_bytes("hostile.xlsx", &bytes).unwrap(); + let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect(); + // Saved order is by number (10 after 3); a clashing name gets a suffix. + assert_eq!(names, ["Good", "good (2)", "Tenth"]); + assert!(back.names.is_empty(), "{:?}", names_of(&back)); + assert_eq!(warnings.len(), 1, "{warnings:?}"); + assert!(warnings[0].starts_with("2 macros"), "{warnings:?}"); + + // The JSON format: more than the limit, and one over the step limit. + let mut wb = Workbook::new(); + wb.macros = (0..MAX_MACROS + 5).map(|i| Macro { name: format!("M{i}"), ..Macro::default() }).collect(); + wb.macros[0].steps = vec![Step::command("x", json!({})); MAX_MACRO_STEPS + 1]; + let bytes = crate::io::save_bytes(&wb, "many.json").unwrap(); + let (back, warnings) = crate::io::open_bytes("many.json", &bytes).unwrap(); + assert_eq!(back.macros.len(), MAX_MACROS); + assert_eq!(back.macros[0].name, "M1"); + assert!(warnings[0].starts_with("5 macros"), "{warnings:?}"); + + // A macro too large for the hidden names is refused, so a save never fails on it. + let huge = "x".repeat(crate::cmd::stash::MAX_ITEM_CHARS); + let err = parse_macro(&json!({"name": "Huge", "steps": [{"command": "cell.set", "params": {"input": huge}}]})).unwrap_err(); + assert!(err.contains("too large"), "{err}"); + let mut wb = Workbook::new(); + wb.macros.push(Macro { name: "Huge".into(), steps: vec![Step::command("cell.set", json!({"input": huge}))], ..Macro::default() }); + let bytes = crate::io::save_bytes(&wb, "huge.json").unwrap(); + let (back, warnings) = crate::io::open_bytes("huge.json", &bytes).unwrap(); + assert!(back.macros.is_empty() && warnings[0].starts_with("1 macro "), "{warnings:?}"); +} + +#[test] +fn names() { + let mut wb = Workbook::new(); + wb.macros.push(Macro { name: "Macro1".into(), ..Macro::default() }); + assert_eq!(unique_name(&wb, "macro1"), "macro1 (2)"); + assert_eq!(unique_name(&wb, "Other"), "Other"); + assert_eq!(unique_name(&wb, ""), "Macro"); + assert_eq!(find(&wb, " MACRO1 "), Some(0)); + assert_eq!(find(&wb, "Macro2"), None); +} diff --git a/crates/model/Cargo.toml b/crates/model/Cargo.toml index 761cf0093c..d7d7fff4dd 100644 --- a/crates/model/Cargo.toml +++ b/crates/model/Cargo.toml @@ -13,7 +13,5 @@ gridcraft-core = { workspace = true } sha2 = { workspace = true } gridcraft-formula = { workspace = true } serde = { workspace = true } -thiserror = { workspace = true } - -[dev-dependencies] serde_json = { workspace = true } +thiserror = { workspace = true } diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index a76f30fc3f..f304d5470c 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -8,6 +8,7 @@ mod b64; pub mod cell; mod cellmap; pub mod features; +pub mod macros; pub mod password; pub mod preserved; pub mod sheet; @@ -17,6 +18,7 @@ pub mod workbook; pub use cell::{Cell, CellPicture, Formula}; pub use features::*; +pub use macros::*; pub use password::PasswordHash; pub use preserved::{Preserved, PreservedPart, PreservedRel, PreservedSheet, PreservedXml}; pub use sheet::{DEFAULT_COL_WIDTH, DEFAULT_ROW_HEIGHT, LineIndex, Sheet, SheetViewMode, Visibility, points_to_chars}; diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs new file mode 100644 index 0000000000..fa898984d1 --- /dev/null +++ b/crates/model/src/macros.rs @@ -0,0 +1,351 @@ +//! GridCraft macros: typed, declarative command lists that run as one undo step. They are not VBA: +//! a VBA project in a file is carried through unchanged (`Workbook::vba_project`) and never run. + +use serde::{Deserialize, Serialize}; + +/// The macro format version written by this build. +pub const MACRO_VERSION: u32 = 1; +/// The most macros a workbook keeps. +pub const MAX_MACROS: usize = 1_000; +/// The most steps in one macro, counting the steps inside Repeat and If. +pub const MAX_MACRO_STEPS: usize = 10_000; +/// How deeply Repeat and If may nest (a top-level step is at depth 1). +pub const MAX_MACRO_DEPTH: usize = 4; +/// The longest macro or input name, in characters. +pub const MAX_MACRO_NAME: usize = 255; +/// The longest macro description or step note, in characters. +pub const MAX_MACRO_TEXT: usize = 4_096; + +/// A recorded or hand-made macro: a list of typed steps that runs as one undo step. Most steps run +/// a GridCraft command; Repeat, If and Stop add bounded control flow. References in step +/// parameters can be tokens (`"Input.col(2)"`) that are resolved against the declared inputs when +/// the macro runs, so a macro recorded on one block of data works on another. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct Macro { + /// Format version ([`MACRO_VERSION`]). + pub v: u32, + pub name: String, + pub description: String, + /// Keyboard shortcut (`Ctrl+Shift+M`) for a user interface to offer; the engine only stores it. + #[serde(skip_serializing_if = "Option::is_none")] + pub shortcut: Option, + /// The ranges the macro works on, checked before it runs. + pub inputs: Vec, + pub steps: Vec, + /// Where results go and what to do when they would land on existing data. + pub output: OutputRule, + /// Commands that need permission (reading or writing files) which the macro may run. + pub permissions: Vec, +} + +impl Default for Macro { + fn default() -> Self { + Macro { + v: MACRO_VERSION, + name: String::new(), + description: String::new(), + shortcut: None, + inputs: vec![], + steps: vec![], + output: OutputRule::default(), + permissions: vec![], + } + } +} + +impl Macro { + /// Steps in the macro, counting those inside Repeat and If. + pub fn step_count(&self) -> usize { + fn count(steps: &[Step]) -> usize { + steps.iter().map(|s| 1usize.saturating_add(s.kind.children().map(|b| count(b)).sum::())).sum() + } + count(&self.steps) + } + + /// Checks the size limits ([`MAX_MACRO_STEPS`], [`MAX_MACRO_DEPTH`], name and text lengths). + pub fn check_limits(&self) -> Result<(), String> { + let name = self.name.trim(); + if name.is_empty() { + return Err("A macro needs a name.".into()); + } + if name.chars().count() > MAX_MACRO_NAME { + return Err(format!("A macro name can be at most {MAX_MACRO_NAME} characters long.")); + } + if self.description.chars().count() > MAX_MACRO_TEXT { + return Err(format!("A macro description can be at most {MAX_MACRO_TEXT} characters long.")); + } + // Count without recursing past the depth limit (a hostile file can nest deeper). + fn walk(steps: &[Step], level: usize, count: &mut usize) -> Result<(), String> { + if level > MAX_MACRO_DEPTH { + return Err(format!("Repeat and If steps can nest at most {MAX_MACRO_DEPTH} deep.")); + } + for s in steps { + *count = count.saturating_add(1); + if *count > MAX_MACRO_STEPS { + return Err(format!("A macro can have at most {MAX_MACRO_STEPS} steps.")); + } + if s.note.chars().count() > MAX_MACRO_TEXT { + return Err(format!("A step note can be at most {MAX_MACRO_TEXT} characters long.")); + } + for body in s.kind.children() { + walk(body, level + 1, count)?; + } + } + Ok(()) + } + walk(&self.steps, 1, &mut 0)?; + if self.inputs.len() > 64 { + return Err("A macro can declare at most 64 inputs.".into()); + } + for (k, i) in self.inputs.iter().enumerate() { + let n = i.name.as_str(); + let ident = n.chars().next().is_some_and(char::is_alphabetic) && n.chars().all(|c| c.is_alphanumeric() || c == '_'); + if !ident || n.chars().count() > MAX_MACRO_NAME { + return Err(format!("'{n}' can't name an input: use letters, digits and _, starting with a letter.")); + } + if ["Output", "Row", "Column", "Name"].iter().any(|r| r.eq_ignore_ascii_case(n)) { + return Err(format!("'{n}' is a token of its own and can't name an input.")); + } + if self.inputs.iter().take(k).any(|o| o.name.eq_ignore_ascii_case(n)) { + return Err(format!("Two inputs are called '{n}'.")); + } + } + Ok(()) + } +} + +/// A range the macro works on, with the shape and content it expects. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct NamedInput { + /// The token that stands for it in step parameters (`Input`, `Rates`). + pub name: String, + pub kind: InputKind, + /// Allowed data rows (header excluded), `[min, max]`; a max of 0 means no limit. + pub rows: [u32; 2], + /// Allowed columns, `[min, max]`; a max of 0 means no limit. + pub cols: [u32; 2], + /// The first row holds column names. + pub header: bool, + /// What each column must hold, first column first; columns past the list may hold anything. + pub col_types: Vec, + /// Every data cell must have something in it. + pub non_empty: bool, + /// The range used when a run doesn't name one (else the current selection, for the first input). + #[serde(skip_serializing_if = "Option::is_none")] + pub default_ref: Option, +} + +impl Default for NamedInput { + fn default() -> Self { + NamedInput { + name: "Input".into(), + kind: InputKind::Range, + rows: [1, 0], + cols: [1, 0], + header: false, + col_types: vec![], + non_empty: false, + default_ref: None, + } + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum InputKind { + /// A single cell. + Cell, + #[default] + Range, +} + +/// What a column of an input must hold. Empty cells pass unless the input is `non_empty`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum ColType { + Number, + Text, + /// A number shown with a date or time format. + Date, + Bool, + #[default] + Any, +} + +impl ColType { + /// The plural noun used in messages ("must hold numbers"). + pub fn noun(self) -> &'static str { + match self { + ColType::Number => "numbers", + ColType::Text => "text", + ColType::Date => "dates", + ColType::Bool => "TRUE or FALSE", + ColType::Any => "anything", + } + } +} + +/// One macro step and its editor flags. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Step { + #[serde(flatten)] + pub kind: StepKind, + /// A disabled step is skipped. + #[serde(default = "yes", skip_serializing_if = "is_true")] + pub enabled: bool, + /// The debugger stops before this step. + #[serde(default, skip_serializing_if = "is_false")] + pub breakpoint: bool, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub note: String, +} + +fn yes() -> bool { + true +} +fn is_true(b: &bool) -> bool { + *b +} +fn is_false(b: &bool) -> bool { + !*b +} + +impl Step { + pub fn new(kind: StepKind) -> Step { + Step { kind, enabled: true, breakpoint: false, note: String::new() } + } + + /// A step that runs command `id` with `params`. + pub fn command(id: impl Into, params: serde_json::Value) -> Step { + Step::new(StepKind::Command { id: id.into(), params }) + } +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(tag = "type", rename_all = "camelCase")] +pub enum StepKind { + /// Runs a GridCraft command. String parameters may hold tokens, resolved when the step runs. + Command { + id: String, + #[serde(default)] + params: serde_json::Value, + }, + /// Runs `body` once for each row (or column) of the range `over`, a token or reference; inside + /// it, `Row` (or `Column`) stands for the current one. At most `max` times. + Repeat { + over: String, + #[serde(default)] + of: RepeatOf, + #[serde(default = "repeat_max")] + max: u32, + #[serde(default)] + body: Vec, + }, + /// Compares the value of `cell` (a token or reference to one cell) with `value` and runs + /// `then` or `else`. + If { + cell: String, + op: CompareOp, + #[serde(default)] + value: serde_json::Value, + #[serde(default)] + then: Vec, + #[serde(default, rename = "else")] + otherwise: Vec, + }, + /// Ends the run with a message. The changes made so far are kept, or undone with `rollback`. + Stop { + #[serde(default)] + message: String, + #[serde(default, skip_serializing_if = "is_false")] + rollback: bool, + }, +} + +fn repeat_max() -> u32 { + 10_000 +} + +impl StepKind { + /// The step lists inside this step (Repeat's body, If's branches). + pub fn children(&self) -> impl Iterator> { + let (a, b) = match self { + StepKind::Repeat { body, .. } => (Some(body), None), + StepKind::If { then, otherwise, .. } => (Some(then), Some(otherwise)), + _ => (None, None), + }; + a.into_iter().chain(b) + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum RepeatOf { + #[default] + Rows, + Columns, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum CompareOp { + Eq, + Ne, + Gt, + Ge, + Lt, + Le, + /// The cell is empty (`value` is ignored). + Empty, + NotEmpty, + /// The cell's text contains `value`'s text, ignoring case. + Contains, +} + +/// Where a macro's results go. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct OutputRule { + /// What the `Output` token stands for: a token (`Input.offset(0,4)`), a reference or `Name:X`. + #[serde(skip_serializing_if = "Option::is_none")] + pub anchor: Option, + /// What to do when the cells the macro writes, outside its inputs, already hold data. + pub on_conflict: OnConflict, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum OnConflict { + /// Ask first: overwrite, shift the data down or right, or cancel. Programmatic runs fail. + #[default] + Ask, + Fail, + Overwrite, + ShiftDown, + ShiftRight, +} + +impl OnConflict { + pub fn parse(s: &str) -> Option { + Some(match s.to_ascii_lowercase().replace(['_', '-', ' '], "").as_str() { + "ask" => OnConflict::Ask, + "fail" | "cancel" => OnConflict::Fail, + "overwrite" => OnConflict::Overwrite, + "shiftdown" => OnConflict::ShiftDown, + "shiftright" => OnConflict::ShiftRight, + _ => return None, + }) + } + + pub fn name(self) -> &'static str { + match self { + OnConflict::Ask => "ask", + OnConflict::Fail => "fail", + OnConflict::Overwrite => "overwrite", + OnConflict::ShiftDown => "shiftDown", + OnConflict::ShiftRight => "shiftRight", + } + } +} diff --git a/crates/model/src/workbook.rs b/crates/model/src/workbook.rs index 828ca692e7..b90186c40a 100644 --- a/crates/model/src/workbook.rs +++ b/crates/model/src/workbook.rs @@ -113,6 +113,10 @@ pub struct Workbook { /// separator back (a `;` file stays a `;` file). `None` = not read from CSV: CSV saves use `,`. #[serde(default, skip_serializing_if = "Option::is_none")] pub csv_delimiter: Option, + /// GridCraft macros (typed command lists, not VBA), in the order the macro list shows them. + /// The JSON format keeps them as this field; xlsx keeps them in hidden defined names. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub macros: Vec, /// Unmodelled file content kept for saving (package root and workbook level). Not in JSON. #[serde(skip)] pub preserved: Option>, @@ -146,6 +150,7 @@ impl Workbook { cell_styles: vec![], vba_project: None, csv_delimiter: None, + macros: vec![], preserved: None, source_dir: None, } From 54c08645b9335d3cc8d3cb9afbae455987b1beaf Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:15:12 -0400 Subject: [PATCH 031/687] Engine: macro.run and macro.check: input checks, footprint dry run, policy macro.run runs a macro as one undo step ("Run Macro: "); an error at any step rolls everything back and names the step and its path (3, 2.1 inside a Repeat, 4.then.1 inside an If). It works in four parts: - Policy (macro_policy). File, app, window, automate and macro commands and Undo and Redo never run in a macro. Reading or writing files (data.getData, data.fromTextCsv, insert.picture, Page Layout Background, or an export with a path) needs the command in the macro's permissions and the person's approval. A macro in a workbook opened from a file asks before it runs. Approval holds for the session until the macro's content changes; it is asked for with the new EngineError::Approve and given with approve: true. A step that would open a dialog or a web page fails instead. - Inputs. Each declared input is bound (given, its default, or the selection for the first one), whole columns shrink to the used range, and the shape and column types are checked with messages that name the cells: "Input column 2 (Amount) must hold numbers; C4 holds text". - Footprint. Unless the macro may overwrite, it first runs in dry-run mode on the real workbook inside an undo group that is rolled back, with the session's clipboard, journal, windows and the document's trust put back. CellStore::diff on every sheet, plus the cells commands wrote (DocState::touched), sheets and objects, gives what it would write outside its inputs; cells that only recalculated do not count. Writing over data, deleting a sheet or deleting an object is a conflict: ask (the new EngineError::Confirm, so programmatic runs fail unless they pass onConflict or overwrite), fail, overwrite, or shift the data down or right with Insert Cells and check again. A step that would fail, for example past the edge of the sheet, fails the run before anything changes. File steps and Solver stop the dry run early and are reported. - Steps run on a machine with an explicit frame stack: Repeat over the rows or columns of a range with Row or Column bound, If on one cell with Excel's comparisons, and Stop, which keeps the changes or rolls them back. A run executes at most 100,000 steps within a time limit (60 s by default, timeLimit to change; the web build has no clock, so only the step limit applies there). macro.check reports the bound inputs, the output anchor, the approval needed, the footprint and the conflicts without changing anything; nothing from an unapproved macro runs, not even as a dry run. --- crates/engine/src/cmd/macros.rs | 43 ++ crates/engine/src/cmd/mod.rs | 12 + crates/engine/src/lib.rs | 12 + crates/engine/src/macros/mod.rs | 10 + crates/engine/src/macros/policy.rs | 122 +++ crates/engine/src/macros/run.rs | 1134 ++++++++++++++++++++++++++++ crates/engine/src/macros/tests.rs | 5 + crates/engine/src/tests.rs | 1 + crates/engine/src/tests/macros.rs | 436 +++++++++++ 9 files changed, 1775 insertions(+) create mode 100644 crates/engine/src/cmd/macros.rs create mode 100644 crates/engine/src/macros/policy.rs create mode 100644 crates/engine/src/macros/run.rs create mode 100644 crates/engine/src/tests/macros.rs diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs new file mode 100644 index 0000000000..2aba053b03 --- /dev/null +++ b/crates/engine/src/cmd/macros.rs @@ -0,0 +1,43 @@ +//! Macros: typed command lists that run as one undo step. See [`crate::macros`] for the model, +//! tokens, runner and policy. Automate scripts (`automate.*`) are a separate, older feature. + +use serde_json::Value as Json; + +use super::*; + +pub fn specs() -> Vec { + vec![ + cmd!( + "macro.run", + "Run Macro", + [], + None, + "{name, inputs?: {Input: \"B2:D9\"} | \"B2:D9\", onConflict?: ask|fail|overwrite|shiftDown|shiftRight, overwrite?: bool, approve?: bool, timeLimit?: seconds} runs as one undo step; any error rolls it all back → {macro, ran, inputs, stopped?, shifted?}", + has_doc, + run + ), + cmd!( + query "macro.check", + "Check Macro", + [], + None, + "{name, inputs?} validates the inputs and dry-runs the macro on the workbook (then puts everything back) → {ok, errors?, needsApproval?, inputs, output?, conflicts, footprint}", + has_doc, + check + ), + ] +} + +fn name_param<'a>(cmd: &str, p: &'a Json) -> Result<&'a str> { + str_param(p, "name").filter(|n| !n.trim().is_empty()).ok_or_else(|| bad(cmd, "missing `name`")) +} + +fn run(s: &mut Session, p: &Json) -> Result { + let name = name_param("macro.run", p)?; + crate::macros::run::run(s, name, p) +} + +fn check(s: &mut Session, p: &Json) -> Result { + let name = name_param("macro.check", p)?; + crate::macros::run::check(s, name, p) +} diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index fadb72b6c4..6aa9cac58f 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -12,6 +12,7 @@ pub mod format; pub mod formulas; pub mod insert; pub mod inspect; +pub mod macros; pub mod pivot; pub mod print; pub mod review; @@ -143,6 +144,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(view::specs()); v.extend(inspect::specs()); v.extend(extra::specs()); + v.extend(macros::specs()); v.extend(draw::specs()); v.extend(spelling::specs()); v.extend(solver::specs()); @@ -365,6 +367,13 @@ pub(crate) fn commit(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result) if !structural { check_arrays(&d.wb, &wb)?; } + if let Some(log) = d.touched.as_mut() { + for (si, c) in changed.iter().take(TOUCH_LOG_LIMIT.saturating_sub(log.len())) { + if let Some(sh) = wb.sheet(*si) { + log.push((sh.name.clone(), *c)); + } + } + } if changed.len() <= 200_000 { fit_rows.extend(changed.iter().map(|(s, c)| (*s, c.row))); } @@ -392,6 +401,9 @@ pub(crate) fn commit(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result) Ok(r) } +/// The most written cells a macro's footprint check records. +const TOUCH_LOG_LIMIT: usize = 1_000_000; + /// Excel's message for an edit of a locked cell on a protected sheet. pub(crate) const PROTECTED: &str = "The cell or chart you're trying to change is on a protected sheet."; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 7f63001d23..d188161eea 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -52,6 +52,12 @@ pub enum EngineError { NoDocument, #[error("{0}")] Other(String), + /// The command would replace something; run it again with `overwrite: true` to go ahead. + #[error("confirm: {0} (run again with `overwrite: true`)")] + Confirm(String), + /// Active content (a macro) asks to run; run it again with `approve: true` to allow it. + #[error("approve: {0} (run again with `approve: true`)")] + Approve(String), #[error("internal error in `{0}` (the workbook was kept as it was): {1}")] Internal(String, String), } @@ -173,6 +179,9 @@ pub struct DocState { pub content_kind: Option, /// Whether the workbook's active content may run without asking. pub trust: Trust, + /// While a macro's footprint is measured: every cell a command wrote, by sheet name, even + /// where it wrote what was already there. + pub touched: Option>, } static NEXT_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); @@ -199,6 +208,7 @@ impl DocState { unlocked_ranges: vec![], content_kind: None, trust: Trust::Local, + touched: None, } } pub fn is_dirty(&self) -> bool { @@ -312,6 +322,8 @@ pub struct Session { pub recorder: Option, /// How many `execute` calls are running (0 between top-level commands). depth: u32, + /// Macros the person allowed to run in this session: (document uid, macro content hash). + pub macro_approvals: std::collections::HashSet<(u64, u64)>, /// Windows hidden with View › Hide. pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index fee5392018..55933c1acb 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -14,7 +14,9 @@ use serde_json::{Value as Json, json}; use crate::cmd::stash; +pub mod policy; pub mod resolve; +pub mod run; /// The hidden-data key prefix of a stored macro (`macro_`). const MACRO_KEY: &str = "macro_"; @@ -86,6 +88,14 @@ pub fn find(wb: &Workbook, name: &str) -> Option { wb.macros.iter().position(|m| m.name.eq_ignore_ascii_case(name)) } +/// A stable fingerprint of a macro's content: approval to run a macro from a file holds until +/// the macro changes. +pub fn hash(m: &Macro) -> u64 { + // FNV-1a over the macro's JSON. + let text = macro_json(m).to_string(); + text.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)) +} + /// A copy of `wb` for xlsx with each macro in hidden names (`_gc_macro__`) instead of the /// field, or `None` when there is nothing to change. pub fn with_names(wb: &Workbook) -> crate::Result> { diff --git a/crates/engine/src/macros/policy.rs b/crates/engine/src/macros/policy.rs new file mode 100644 index 0000000000..7329f567fe --- /dev/null +++ b/crates/engine/src/macros/policy.rs @@ -0,0 +1,122 @@ +//! What a macro step may run. Macros run commands, so this is the one place that keeps a macro +//! from saving, opening or closing files, driving the app, running other macros, or reading and +//! writing files without the person knowing. + +use serde_json::Value as Json; + +use crate::Trust; + +/// Whether a command may run as a macro step. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Policy { + Allow, + /// Allowed once the person approves the macro; the text says why it asks ("came with a + /// workbook you opened"). + Ask(String), + /// Reads or writes files: the macro must list the command in its permissions, and the person + /// approves the macro before it runs. The text says what it does ("reads a file"). + NeedsPermission(String), + /// Never runs in a macro; the text says why. + Deny(String), +} + +/// Prefixes of commands that never run in a macro, with the reason. +const DENIED_PREFIXES: &[(&str, &str)] = &[ + ("file.", "macros don't open, save, close or create workbooks"), + ("app.", "macros don't change the app"), + ("window.", "macros don't switch windows"), + ("automate.", "macros don't record or run scripts or other macros"), + ("macro.", "macros don't record or run other macros"), +]; + +/// Single commands that never run in a macro. +const DENIED: &[(&str, &str)] = + &[("edit.undo", "a macro is undone as one step after it runs"), ("edit.redo", "a macro is undone as one step after it runs")]; + +/// Commands under a denied prefix that only change the workbook. +const ALLOWED: &[&str] = &["file.properties"]; + +/// Commands that read a file when given a `path`. +const READS_FILES: &[&str] = &["data.getData", "data.fromTextCsv", "insert.picture", "pageLayout.background"]; + +/// File commands that only produce bytes unless given a `path` (then they write a file). +const EXPORTS: &[&str] = &["file.exportPdf", "file.exportCsv", "file.exportHtml", "file.print", "file.printPreview", "file.saveBytes"]; + +/// The policy for running command `id` with `params` as a macro step in a document with `trust`. +pub fn macro_policy(id: &str, params: &Json, trust: Trust) -> Policy { + let has_path = params.get("path").and_then(Json::as_str).is_some_and(|p| !p.trim().is_empty()); + let base = if EXPORTS.contains(&id) { + if has_path { Policy::NeedsPermission("writes a file".into()) } else { Policy::Allow } + } else if ALLOWED.contains(&id) { + Policy::Allow + } else if let Some((_, why)) = DENIED.iter().find(|(d, _)| *d == id) { + Policy::Deny((*why).into()) + } else if let Some((_, why)) = DENIED_PREFIXES.iter().find(|(p, _)| id.starts_with(p)) { + Policy::Deny((*why).into()) + } else if READS_FILES.contains(&id) && has_path { + Policy::NeedsPermission("reads a file".into()) + } else { + Policy::Allow + }; + match base { + Policy::Allow if !trust.allows_code() => Policy::Ask("came with a workbook you opened".into()), + other => other, + } +} + +/// Whether the footprint dry run can preview a command. Solver can take long, so a dry run stops +/// before it and reports the footprint as partial. +pub fn previewable(id: &str) -> bool { + id != "data.solver" +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn the_deny_and_permission_lists() { + let none = json!({}); + let path = json!({"path": "/tmp/x"}); + for id in [ + "file.saveAs", + "file.save", + "file.open", + "file.close", + "file.new", + "app.commands", + "window.activate", + "automate.runScript", + "automate.recordActions", + "macro.run", + "macro.record", + "edit.undo", + "edit.redo", + ] { + assert!(matches!(macro_policy(id, &none, Trust::Local), Policy::Deny(_)), "{id}"); + assert!(matches!(macro_policy(id, &path, Trust::FromFile), Policy::Deny(_)), "{id}"); + } + for id in ["data.getData", "data.fromTextCsv", "insert.picture", "pageLayout.background", "file.exportPdf", "file.print", "file.exportCsv"] { + assert!(matches!(macro_policy(id, &path, Trust::Local), Policy::NeedsPermission(_)), "{id}"); + assert!(matches!(macro_policy(id, &path, Trust::FromFile), Policy::NeedsPermission(_)), "{id}"); + } + // Without a path they touch no file. + for id in ["data.getData", "insert.picture", "file.exportPdf", "file.saveBytes", "file.printPreview"] { + assert_eq!(macro_policy(id, &none, Trust::Local), Policy::Allow, "{id}"); + assert_eq!(macro_policy(id, &json!({"path": " "}), Trust::Local), Policy::Allow, "{id}"); + } + for id in ["cell.set", "home.bold", "view.macros", "edit.copy", "file.properties"] { + assert_eq!(macro_policy(id, &none, Trust::Local), Policy::Allow, "{id}"); + assert!(matches!(macro_policy(id, &none, Trust::FromFile), Policy::Ask(_)), "{id}"); + } + assert!(!previewable("data.solver") && previewable("cell.set")); + } + + #[test] + fn every_listed_command_exists() { + for id in ALLOWED.iter().chain(READS_FILES).chain(EXPORTS).chain(DENIED.iter().map(|(id, _)| id)).chain(&["data.solver"]) { + assert!(crate::find_command(id).is_some(), "{id}"); + } + } +} diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs new file mode 100644 index 0000000000..3651deb0c5 --- /dev/null +++ b/crates/engine/src/macros/run.rs @@ -0,0 +1,1134 @@ +//! Running a macro. +//! +//! 1. **Gate**: every step's command goes through [`macro_policy`]. Denied commands stop the macro +//! before anything runs; commands that touch files must be listed in the macro's permissions; +//! a macro from a file someone else made, or one that touches files, runs only once the person +//! approves it (once per macro content, for the session). +//! 2. **Inputs**: each declared input is bound to a range (given, its default, or the selection) +//! and checked against its shape and column types, with messages that name the cells. +//! 3. **Footprint**: unless the macro may overwrite, it first runs on the real workbook in dry-run +//! mode, the changes are compared with `CellStore::diff`, and everything is put back. Cells it +//! would write outside its inputs that already hold data are conflicts, handled by the output +//! rule: ask, fail, overwrite, or shift the data down or right (and check again). +//! 4. **Run**: one undo step (`Session::grouped`); an error at any step rolls everything back and +//! names the step. +//! +//! The steps run on a small machine with an explicit frame stack, so the debugger can stop +//! between any two steps and pick up again on a later frame. + +use std::collections::BTreeSet; +use std::sync::Arc; + +use gridcraft_core::{CellRef, RangeRef, Value}; +use gridcraft_model::{Cell, CompareOp, InputKind, Macro, OnConflict, RepeatOf, Sheet, Step, StepKind, Workbook}; +use serde::Serialize; +use serde_json::{Value as Json, json}; + +use super::policy::{Policy, macro_policy, previewable}; +use super::resolve::{self, Env, Place, ResolveError}; +use crate::{EngineError, Result, Selection, Session, Trust, UiRequest}; + +/// The most steps one run executes (counting each pass through a Repeat). +pub const MAX_EXECUTED_STEPS: u64 = 100_000; +/// How long a run may take, in milliseconds. The web build has no clock; there only the step +/// limit applies. +pub const DEFAULT_TIME_LIMIT_MS: f64 = 60_000.0; +/// The most cells an input may have for its content to be checked. +pub const MAX_INPUT_CELLS: u64 = 1_000_000; +/// The most ranges a footprint lists per sheet (the counts stay exact). +const MAX_LISTED_RANGES: usize = 500; + +/// An input bound to a range: name, where, whether its first row is a header. +pub type Bound = (String, Place, bool); + +/// Where and why a step failed. +#[derive(Clone, Debug, PartialEq, Serialize)] +pub struct StepError { + /// The step's path: `3`, `2.1` inside a Repeat, `4.then.1` inside an If. + pub step: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub command: Option, + pub message: String, + /// The step ran past the edge of the sheet. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub edge: bool, +} + +impl StepError { + fn new(step: &str, command: Option<&str>, message: impl Into) -> StepError { + StepError { step: step.to_string(), command: command.map(str::to_string), message: message.into(), edge: false } + } + + fn resolve(step: &str, command: Option<&str>, e: ResolveError) -> StepError { + let edge = matches!(e, ResolveError::Edge(_)); + StepError { edge, ..StepError::new(step, command, e.message()) } + } + + /// `Step 3 (cell.set): …` + pub fn describe(&self) -> String { + match &self.command { + Some(c) => format!("Step {} ({c}): {}", self.step, self.message), + None => format!("Step {}: {}", self.step, self.message), + } + } + + pub fn into_engine(self, name: &str) -> EngineError { + EngineError::Other(format!("Macro '{name}' stopped. {} Nothing was changed.", self.describe())) + } +} + +// ---------------------------------------------------------------- gate + +/// Checks every enabled step against the policy: denied commands and undeclared file access are +/// errors; otherwise returns what the person must approve (empty when nothing). +pub fn gate(m: &Macro, trust: Trust) -> Result> { + let mut asks = BTreeSet::new(); + let mut err = None; + walk_enabled(&m.steps, "", &mut |path, id, params| { + if err.is_some() { + return; + } + match macro_policy(id, params, trust) { + Policy::Allow => {} + Policy::Ask(why) => { + asks.insert(why); + } + Policy::NeedsPermission(what) => { + if m.permissions.iter().any(|p| p == id) { + asks.insert(format!("runs {id}, which {what}")); + } else { + err = Some(format!("Step {path} ({id}) {what}. Add {id} to the macro's permissions to allow it.")); + } + } + Policy::Deny(why) => err = Some(format!("Step {path} ({id}) can't run in a macro: {why}.")), + } + }); + match err { + Some(e) => Err(EngineError::Other(format!("Macro '{}' can't run. {e}", m.name))), + None => Ok(asks.into_iter().collect()), + } +} + +/// Calls `f(path, id, params)` for each enabled command step (skipping disabled steps' bodies). +fn walk_enabled(steps: &[Step], prefix: &str, f: &mut impl FnMut(&str, &str, &Json)) { + for (i, st) in steps.iter().enumerate() { + if !st.enabled { + continue; + } + let path = child_path(prefix, i); + match &st.kind { + StepKind::Command { id, params } => f(&path, id, params), + StepKind::Repeat { body, .. } => walk_enabled(body, &path, f), + StepKind::If { then, otherwise, .. } => { + walk_enabled(then, &format!("{path}.then"), f); + walk_enabled(otherwise, &format!("{path}.else"), f); + } + StepKind::Stop { .. } => {} + } + } +} + +fn child_path(prefix: &str, i: usize) -> String { + if prefix.is_empty() { (i + 1).to_string() } else { format!("{prefix}.{}", i + 1) } +} + +/// Approves the macro for this session when `approve` is set, or asks (an +/// [`EngineError::Approve`]) when it needs approval it doesn't have. +pub fn approve(s: &mut Session, m: &Macro, doc: u64, asks: &[String], approve: bool) -> Result<()> { + if asks.is_empty() { + return Ok(()); + } + let key = (doc, super::hash(m)); + if s.macro_approvals.contains(&key) { + return Ok(()); + } + if approve { + s.macro_approvals.insert(key); + return Ok(()); + } + Err(EngineError::Approve(format!("Run macro '{}'? It {}.", m.name, join_and(asks)))) +} + +fn join_and(items: &[String]) -> String { + match items { + [] => String::new(), + [one] => one.clone(), + [init @ .., last] => format!("{} and {last}", init.join(", ")), + } +} + +// ---------------------------------------------------------------- inputs + +/// A macro ready to run: its inputs bound and checked, its output anchor resolved. +#[derive(Clone, Debug)] +pub struct Prepared { + pub mac: Macro, + pub doc: u64, + pub trust: Trust, + pub inputs: Vec, + pub output: Option, +} + +impl Prepared { + pub fn inputs_json(&self, wb: &Workbook) -> Json { + let env = Env { wb, active_sheet: wb.active_sheet, inputs: &[], output: None, item: None }; + Json::Object(self.inputs.iter().map(|(n, p, _)| (n.clone(), json!(resolve::a1(*p, &env)))).collect()) + } +} + +/// Binds the macro's inputs on the active document (`given`: `{"Input": "B2:D9"}`, or a reference +/// for the first input) and checks them. Errors list every problem found. +pub fn prepare(s: &Session, m: &Macro, given: Option<&Json>) -> Result { + let d = s.doc()?; + let wb = &d.wb; + let mut inputs: Vec = Vec::new(); + let mut problems = Vec::new(); + for (i, input) in m.inputs.iter().enumerate() { + let named = given.and_then(Json::as_object).and_then(|o| o.iter().find(|(k, _)| k.eq_ignore_ascii_case(&input.name)).map(|(_, v)| v)); + let text = named.and_then(Json::as_str).or_else(|| if i == 0 { given.and_then(Json::as_str) } else { None }).or(input.default_ref.as_deref()); + let place = match text { + Some(t) => { + let env = Env { wb, active_sheet: wb.active_sheet, inputs: &inputs, output: None, item: None }; + match resolve::resolve_ref(t, &env) { + Ok(p) => p, + Err(e) => { + problems.push(format!("{}: {}", input.name, e.message())); + continue; + } + } + } + None if i == 0 => Place { sheet: wb.active_sheet, range: d.selection.current() }, + None => { + problems.push(format!("{0} needs a range: pass inputs: {{\"{0}\": \"A1:B5\"}}.", input.name)); + continue; + } + }; + let place = trim(wb, place); + problems.extend(check_input(wb, input, place)); + inputs.push((input.name.clone(), place, input.header)); + } + if !problems.is_empty() { + return Err(EngineError::Other(format!("Macro '{}' can't run on these inputs. {}", m.name, problems.join(" ")))); + } + let output = match &m.output.anchor { + Some(a) if !a.trim().is_empty() => { + let env = Env { wb, active_sheet: wb.active_sheet, inputs: &inputs, output: None, item: None }; + Some( + resolve::resolve_ref(a, &env) + .map_err(|e| EngineError::Other(format!("Macro '{}': its output anchor {}", m.name, lower_first(e.message()))))?, + ) + } + _ => None, + }; + Ok(Prepared { mac: m.clone(), doc: d.uid, trust: d.trust, inputs, output }) +} + +fn lower_first(s: &str) -> String { + let mut c = s.chars(); + match c.next() { + Some(f) => f.to_lowercase().chain(c).collect(), + None => String::new(), + } +} + +/// Whole rows or columns shrink to the part of the sheet in use. +fn trim(wb: &Workbook, p: Place) -> Place { + if !(p.range.is_full_cols() || p.range.is_full_rows()) { + return p; + } + let used = wb.sheet(p.sheet).and_then(Sheet::used_range); + let range = used.and_then(|u| p.range.intersection(&u)).unwrap_or(RangeRef::cell(p.range.start)); + Place { sheet: p.sheet, range } +} + +/// What is wrong with `place` as `input`, one sentence each. +fn check_input(wb: &Workbook, input: &gridcraft_model::NamedInput, place: Place) -> Vec { + let mut out = Vec::new(); + let r = place.range; + let name = &input.name; + let at = r.a1(); + if input.kind == InputKind::Cell && !r.is_single() { + out.push(format!("{name} must be a single cell; {at} is {} cells.", r.count())); + return out; + } + let header = u32::from(input.header && r.height() > 0); + let rows = r.height().saturating_sub(header); + let between = |what: &str, n: u32, [min, max]: [u32; 2]| -> Option { + let plural = |k: u32| if k == 1 { what.to_string() } else { format!("{what}s") }; + if max > 0 && min == max && n != min { + Some(format!("exactly {min} {}", plural(min))) + } else if n < min { + Some(format!("at least {min} {}", plural(min))) + } else if max > 0 && n > max { + Some(format!("at most {max} {}", plural(max))) + } else { + None + } + }; + if let Some(want) = between("data row", rows, input.rows) { + out.push(format!("{name} needs {want}; {at} has {rows}.")); + } + if let Some(want) = between("column", r.width(), input.cols) { + out.push(format!("{name} needs {want}; {at} has {}.", r.width())); + } + if r.count() > MAX_INPUT_CELLS { + out.push(format!("{name} is too large to check ({} cells; at most {}).", r.count(), MAX_INPUT_CELLS)); + return out; + } + let Some(sh) = wb.sheet(place.sheet) else { return out }; + let mut blanks = Vec::new(); + for (ci, col) in (r.start.col..=r.end.col).enumerate() { + let ty = input.col_types.get(ci).copied().unwrap_or_default(); + let mut wrong = Vec::new(); + for row in r.start.row + header..=r.end.row { + let c = CellRef::new(row, col); + let v = sh.value(c); + if v.is_empty() || v.as_text() == Some("") { + if input.non_empty { + blanks.push(c); + } + continue; + } + let date = || wb.styles.get(sh.style_id(c)).num_fmt.as_str().to_string(); + let ok = match ty { + gridcraft_model::ColType::Any => true, + gridcraft_model::ColType::Number => v.is_number(), + gridcraft_model::ColType::Text => v.is_text(), + gridcraft_model::ColType::Bool => matches!(v, Value::Bool(_)), + gridcraft_model::ColType::Date => v.is_number() && crate::display::number_format(&date()).is_date(), + }; + if !ok { + wrong.push((c, holds(&v, ty))); + } + } + if !wrong.is_empty() { + let head = if input.header { + Some(crate::display::cell_text(wb, sh, CellRef::new(r.start.row, col))).filter(|h| !h.trim().is_empty()) + } else { + None + }; + let column = match head { + Some(h) => format!("{name} column {} ({})", ci + 1, h.trim()), + None => format!("{name} column {}", ci + 1), + }; + let listed: Vec = wrong.iter().take(3).map(|(c, what)| format!("{} holds {what}", c.a1())).collect(); + let more = if wrong.len() > 3 { format!(" and {} more cells don't", wrong.len() - 3) } else { String::new() }; + out.push(format!("{column} must hold {}; {}{more}.", ty.noun(), listed.join(", "))); + } + } + if let Some(first) = blanks.first() { + let more = if blanks.len() > 1 { format!(" (and {} more)", blanks.len() - 1) } else { String::new() }; + out.push(format!("{name} must have no blank cells; {} is blank{more}.", first.a1())); + } + out +} + +/// What a cell holds, for "C7 holds text". +fn holds(v: &Value, wanted: gridcraft_model::ColType) -> &'static str { + match v { + Value::Number(_) if wanted == gridcraft_model::ColType::Date => "a number that isn't a date", + Value::Number(_) => "a number", + Value::Text(_) => "text", + Value::Bool(_) => "TRUE or FALSE", + Value::Error(_) => "an error", + Value::Array(_) => "an array", + Value::Empty => "nothing", + } +} + +// ---------------------------------------------------------------- the machine + +/// A stopwatch for the time limit. `Instant::now` traps on wasm, so there it measures nothing. +#[derive(Clone, Copy, Debug)] +struct Stopwatch { + #[cfg(not(target_arch = "wasm32"))] + start: std::time::Instant, +} + +impl Stopwatch { + fn start() -> Stopwatch { + #[cfg(not(target_arch = "wasm32"))] + #[allow(clippy::disallowed_methods)] // never on wasm (see above) + let w = Stopwatch { start: std::time::Instant::now() }; + #[cfg(target_arch = "wasm32")] + let w = Stopwatch {}; + w + } + + /// Milliseconds since the start, or `None` where there is no clock. + fn elapsed_ms(&self) -> Option { + #[cfg(not(target_arch = "wasm32"))] + let ms = Some(self.start.elapsed().as_secs_f64() * 1000.0); + #[cfg(target_arch = "wasm32")] + let ms = None; + ms + } +} + +#[derive(Clone, Debug)] +struct Frame { + steps: Arc>, + next: usize, + /// Path of the step that opened this frame (`""` at the top, `3` for a Repeat's body, + /// `4.then` for an If's branch). + prefix: String, + repeat: Option, +} + +#[derive(Clone, Copy, Debug)] +struct Loop { + of: RepeatOf, + over: Place, + /// The current item (0-based) and how many there are. + at: u32, + count: u32, +} + +impl Loop { + fn item(&self) -> Place { + let r = self.over.range; + let range = match self.of { + RepeatOf::Rows => { + let row = r.start.row.saturating_add(self.at); + RangeRef::new(CellRef::new(row, r.start.col), CellRef::new(row, r.end.col)) + } + RepeatOf::Columns => { + let col = r.start.col.saturating_add(self.at); + RangeRef::new(CellRef::new(r.start.row, col), CellRef::new(r.end.row, col)) + } + }; + Place { sheet: self.over.sheet, range } + } +} + +/// A macro run in progress: which step is next, and what has run. +#[derive(Clone, Debug)] +pub struct Machine { + pub name: String, + pub doc: u64, + pub inputs: Vec, + pub output: Option, + permissions: Vec, + trust: Trust, + frames: Vec, + /// Steps executed so far (each pass through a Repeat counts). + pub executed: u64, + clock: Stopwatch, + pub time_limit_ms: f64, + /// Dry run (the footprint check): steps that would touch files or can't be previewed stop + /// the run early instead of running. + pub dry_run: bool, + /// A Stop step ended the run: its message, and whether it asked for a rollback. + pub stopped: Option<(String, bool)>, + /// A dry run stopped before this step (path, reason): what follows isn't in the footprint. + pub partial: Option<(String, String)>, + /// The last step's resolved parameters (for the debugger). + pub last_params: Option, +} + +impl Machine { + pub fn new(p: &Prepared, dry_run: bool) -> Machine { + let mut m = Machine { + name: p.mac.name.clone(), + doc: p.doc, + inputs: p.inputs.clone(), + output: p.output, + permissions: p.mac.permissions.clone(), + trust: p.trust, + frames: vec![Frame { steps: Arc::new(p.mac.steps.clone()), next: 0, prefix: String::new(), repeat: None }], + executed: 0, + clock: Stopwatch::start(), + time_limit_ms: DEFAULT_TIME_LIMIT_MS, + dry_run, + stopped: None, + partial: None, + last_params: None, + }; + m.settle(); + m + } + + /// Moves to the next enabled step: past disabled steps, finished frames and loop passes. + fn settle(&mut self) { + while let Some(f) = self.frames.last_mut() { + if let Some(st) = f.steps.get(f.next) { + if st.enabled { + return; + } + f.next += 1; + continue; + } + if let Some(l) = f.repeat.as_mut() + && l.at.saturating_add(1) < l.count + { + l.at += 1; + f.next = 0; + continue; + } + self.frames.pop(); + } + } + + pub fn is_done(&self) -> bool { + self.frames.is_empty() + } + + /// The step that runs next, with its path. + pub fn current(&self) -> Option<(String, &Step)> { + let f = self.frames.last()?; + f.steps.get(f.next).map(|st| (child_path(&f.prefix, f.next), st)) + } + + /// The innermost Repeat's pass (1-based) and count. + pub fn iteration(&self) -> Option<(u32, u32)> { + self.frames.iter().rev().find_map(|f| f.repeat).map(|l| (l.at + 1, l.count)) + } + + fn item(&self) -> Option<(RepeatOf, Place)> { + self.frames.iter().rev().find_map(|f| f.repeat).map(|l| (l.of, l.item())) + } + + /// Runs every remaining step. + pub fn run_to_end(&mut self, s: &mut Session) -> std::result::Result<(), StepError> { + while !self.is_done() { + self.step(s)?; + } + Ok(()) + } + + /// Runs the current step and moves to the next one. + pub fn step(&mut self, s: &mut Session) -> std::result::Result<(), StepError> { + let Some((path, st)) = self.current().map(|(p, st)| (p, st.clone())) else { return Ok(()) }; + if let Some(f) = self.frames.last_mut() { + f.next += 1; + } + let r = self.exec(s, &path, &st); + if r.is_err() { + self.frames.clear(); + } + self.settle(); + r + } + + fn exec(&mut self, s: &mut Session, path: &str, st: &Step) -> std::result::Result<(), StepError> { + self.executed += 1; + let at = |msg: String| StepError::new(path, None, msg); + if self.executed > MAX_EXECUTED_STEPS { + return Err(at(format!("The macro ran more than {MAX_EXECUTED_STEPS} steps and was stopped."))); + } + if self.clock.elapsed_ms().is_some_and(|ms| ms > self.time_limit_ms) { + return Err(at(format!("The macro ran longer than {} seconds and was stopped.", (self.time_limit_ms / 1000.0).round()))); + } + // Steps run on the macro's workbook, whichever one is in front. + let Some(i) = s.docs.iter().position(|d| d.uid == self.doc) else { return Err(at("The workbook was closed.".into())) }; + s.set_active(i); + match &st.kind { + StepKind::Command { id, params } => self.command(s, path, id, params), + StepKind::Repeat { over, of, max, body } => { + let place = { + let wb = &s.doc().map_err(|e| at(e.to_string()))?.wb; + let env = self.env(wb); + let p = resolve::resolve_ref(over, &env).map_err(|e| StepError::resolve(path, None, e))?; + trim(wb, p) + }; + let items = match of { + RepeatOf::Rows => place.range.height(), + RepeatOf::Columns => place.range.width(), + }; + let cap = (*max).min(MAX_EXECUTED_STEPS as u32); + if items > cap { + let what = if *of == RepeatOf::Rows { "rows" } else { "columns" }; + return Err(at(format!("Repeat over {} has {items} {what}, more than its limit of {cap}.", place.range.a1()))); + } + if items > 0 && body.iter().any(|b| b.enabled) { + let repeat = Some(Loop { of: *of, over: place, at: 0, count: items }); + self.frames.push(Frame { steps: Arc::new(body.clone()), next: 0, prefix: path.to_string(), repeat }); + } + Ok(()) + } + StepKind::If { cell, op, value, then, otherwise } => { + let v = { + let wb = &s.doc().map_err(|e| at(e.to_string()))?.wb; + let env = self.env(wb); + let p = resolve::resolve_ref(cell, &env).map_err(|e| StepError::resolve(path, None, e))?; + if !p.range.is_single() { + return Err(at(format!("If looks at one cell; {} is {} cells.", p.range.a1(), p.range.count()))); + } + wb.sheet(p.sheet).map(|sh| sh.value(p.range.start)).unwrap_or_default() + }; + let (branch, name) = if compare(&v, *op, value) { (then, "then") } else { (otherwise, "else") }; + if branch.iter().any(|b| b.enabled) { + self.frames.push(Frame { steps: Arc::new(branch.clone()), next: 0, prefix: format!("{path}.{name}"), repeat: None }); + } + Ok(()) + } + StepKind::Stop { message, rollback } => { + self.stopped = Some((message.clone(), *rollback)); + self.frames.clear(); + Ok(()) + } + } + } + + fn env<'a>(&'a self, wb: &'a Workbook) -> Env<'a> { + Env { wb, active_sheet: wb.active_sheet, inputs: &self.inputs, output: self.output, item: self.item() } + } + + fn command(&mut self, s: &mut Session, path: &str, id: &str, params: &Json) -> std::result::Result<(), StepError> { + let fail = |msg: String| StepError::new(path, Some(id), msg); + match macro_policy(id, params, self.trust) { + Policy::Deny(why) => return Err(fail(format!("{id} can't run in a macro: {why}."))), + Policy::NeedsPermission(what) => { + if !self.permissions.iter().any(|p| p == id) { + return Err(fail(format!("{id} {what}. Add it to the macro's permissions to allow it."))); + } + if self.dry_run { + self.partial = Some((path.to_string(), format!("{id} {what}"))); + self.frames.clear(); + return Ok(()); + } + } + Policy::Allow | Policy::Ask(_) => {} + } + if self.dry_run && !previewable(id) { + self.partial = Some((path.to_string(), format!("{id} can't be previewed"))); + self.frames.clear(); + return Ok(()); + } + let resolved = { + let wb = &s.doc().map_err(|e| fail(e.to_string()))?.wb; + resolve::resolve_params(params, &self.env(wb)).map_err(|e| StepError::resolve(path, Some(id), e))? + }; + self.last_params = Some(resolved.clone()); + let queued = s.ui_requests.len(); + let r = s.execute(id, resolved); + // Macros never open dialogs or web pages; messages for the person stay queued. + let mut blocked = None; + let asked: Vec = s.ui_requests.drain(queued.min(s.ui_requests.len())..).collect(); + for q in asked { + match q { + UiRequest::Dialog(name, _) => blocked = Some(format!("{id} would open the {name} dialog; give the step the parameters it needs.")), + UiRequest::OpenUrl(_) => blocked = Some(format!("{id} would open a web page, which macros don't do.")), + UiRequest::EditCell(_) => {} + m @ UiRequest::Message(_) => s.ui_requests.push(m), + } + } + r.map_err(|e| fail(e.to_string()))?; + if let Some(b) = blocked { + return Err(fail(b)); + } + Ok(()) + } +} + +/// If's comparison of a cell's value with a step's `value`, the way Excel's `=`, `<`… compare. +fn compare(v: &Value, op: CompareOp, want: &Json) -> bool { + let empty = v.is_empty() || v.as_text() == Some(""); + let want_text = || match want { + Json::String(t) => t.clone(), + Json::Null => String::new(), + other => other.to_string(), + }; + let ord = || -> Option { + let w = match want { + Json::Null => Value::Empty, + Json::Bool(b) => Value::Bool(*b), + Json::Number(n) => Value::number(n.as_f64()?), + // "5" compares as a number with a number in the cell. + Json::String(t) => match (v, t.trim().parse::()) { + (Value::Number(_), Ok(n)) => Value::number(n), + _ => Value::text(t.as_str()), + }, + _ => return None, + }; + match (v, &w) { + (Value::Error(e), Value::Text(t)) => { + Some(if e.as_str().eq_ignore_ascii_case(t.trim()) { std::cmp::Ordering::Equal } else { std::cmp::Ordering::Less }) + } + (Value::Error(_), _) | (_, Value::Error(_)) | (Value::Array(_), _) => None, + _ => Some(gridcraft_core::value::compare(v, &w)), + } + }; + use std::cmp::Ordering::*; + match op { + CompareOp::Empty => empty, + CompareOp::NotEmpty => !empty, + CompareOp::Contains => v.display().to_lowercase().contains(&want_text().to_lowercase()), + CompareOp::Eq => ord() == Some(Equal), + CompareOp::Ne => ord() != Some(Equal), + CompareOp::Gt => ord() == Some(Greater), + CompareOp::Ge => matches!(ord(), Some(Greater | Equal)), + CompareOp::Lt => ord() == Some(Less), + CompareOp::Le => matches!(ord(), Some(Less | Equal)), + } +} + +// ---------------------------------------------------------------- footprint + +/// What a macro would change, from a dry run on the real workbook. +#[derive(Clone, Debug, Default, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Footprint { + pub sheets: Vec, + pub sheets_added: Vec, + pub sheets_removed: Vec, + pub objects_added: usize, + pub objects_changed: usize, + pub objects_removed: usize, + /// The dry run stopped early before this step; later steps aren't in the footprint. + #[serde(skip_serializing_if = "Option::is_none")] + pub partial: Option, + /// The step the run would fail at. + #[serde(skip_serializing_if = "Option::is_none")] + pub error: Option, + /// A Stop step's message. + #[serde(skip_serializing_if = "Option::is_none")] + pub stopped: Option, +} + +/// The cells a macro would write on one sheet, outside its inputs. +#[derive(Clone, Debug, Default, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct SheetFootprint { + pub sheet: String, + #[serde(skip)] + pub index: usize, + /// The written cells as ranges (at most 500 listed), and how many cells. + pub cells: Vec, + pub changed: usize, + /// Written cells that hold data now. + pub conflicts: Vec, + pub conflict_count: usize, + /// The block that holds every written cell. + #[serde(skip_serializing_if = "Option::is_none")] + pub bounds: Option, + #[serde(skip)] + pub bounds_range: Option, +} + +impl Footprint { + /// Cells that hold data and would be written, plus sheets and objects that would go. + pub fn conflict_count(&self) -> usize { + self.sheets.iter().map(|s| s.conflict_count).sum::() + self.sheets_removed.len() + self.objects_removed + } + + /// "C2:C9 on Sheet1, the sheet Old, 2 objects". + pub fn describe_conflicts(&self) -> String { + let mut parts: Vec = self + .sheets + .iter() + .filter(|s| s.conflict_count > 0) + .map(|s| { + let mut listed = s.conflicts.iter().take(3).cloned().collect::>().join(", "); + if s.conflicts.len() > 3 { + listed.push_str(", …"); + } + format!("{listed} on {}", s.sheet) + }) + .collect(); + parts.extend(self.sheets_removed.iter().map(|n| format!("the sheet {n}"))); + if self.objects_removed > 0 { + parts.push(format!("{} object{}", self.objects_removed, if self.objects_removed == 1 { "" } else { "s" })); + } + parts.join("; ") + } +} + +/// Session state a dry run may touch, put back afterwards. The workbook, its selection and its +/// history are put back by the dry run's undo group. +struct Saved { + active: usize, + docs: Vec, + sheet_selections: Vec, + unlocked_ranges: Vec<(String, String)>, + trust: Trust, + clipboard: Option, + prefs: crate::Prefs, + mode: crate::Mode, + journal: usize, + recording: Option, + ui_requests: usize, + format_painter: Option<(Arc, usize, RangeRef, bool)>, + last_find: Option, + hide_ruler: bool, + hidden_windows: Vec, + draw: (String, String, f32), + comments_pane: bool, + show_all_notes: bool, +} + +impl Saved { + fn take(s: &mut Session, doc: u64) -> Saved { + let d = s.docs.iter().find(|d| d.uid == doc); + Saved { + active: s.active, + docs: s.docs.iter().map(|d| d.uid).collect(), + sheet_selections: d.map(|d| d.sheet_selections.clone()).unwrap_or_default(), + unlocked_ranges: d.map(|d| d.unlocked_ranges.clone()).unwrap_or_default(), + trust: d.map_or(Trust::Local, |d| d.trust), + clipboard: s.clipboard.clone(), + prefs: s.prefs.clone(), + mode: s.mode, + journal: s.journal.len(), + recording: s.recording, + ui_requests: s.ui_requests.len(), + format_painter: s.format_painter.clone(), + last_find: s.last_find.clone(), + hide_ruler: s.hide_ruler, + hidden_windows: s.hidden_windows.clone(), + draw: (s.draw_tool.clone(), s.draw_color.clone(), s.draw_width), + comments_pane: s.comments_pane, + show_all_notes: s.show_all_notes, + } + } + + fn restore(self, s: &mut Session, doc: u64) { + s.docs.retain(|d| self.docs.contains(&d.uid)); + if let Some(d) = s.docs.iter_mut().find(|d| d.uid == doc) { + d.sheet_selections = self.sheet_selections; + d.unlocked_ranges = self.unlocked_ranges; + d.trust = self.trust; + } + s.active = self.active.min(s.docs.len().saturating_sub(1)); + s.clipboard = self.clipboard; + s.prefs = self.prefs; + s.mode = self.mode; + s.journal.truncate(self.journal); + s.recording = self.recording; + s.ui_requests.truncate(self.ui_requests); + s.format_painter = self.format_painter; + s.last_find = self.last_find; + s.hide_ruler = self.hide_ruler; + s.hidden_windows = self.hidden_windows; + (s.draw_tool, s.draw_color, s.draw_width) = self.draw; + s.comments_pane = self.comments_pane; + s.show_all_notes = self.show_all_notes; + } +} + +/// Runs the macro in dry-run mode on its workbook, puts everything back, and says what it would +/// change. +pub fn footprint(s: &mut Session, p: &Prepared) -> Result { + let i = s.docs.iter().position(|d| d.uid == p.doc).ok_or(EngineError::NoDocument)?; + s.set_active(i); + let saved = Saved::take(s, p.doc); + let before = s.doc()?.wb.clone(); + let token = s.group_begin("Check Macro")?; + if let Some(d) = s.docs.iter_mut().find(|d| d.uid == p.doc) { + d.touched = Some(Vec::new()); + } + let mut m = Machine::new(p, true); + let outcome = m.run_to_end(s); + let (after, touched) = match s.docs.iter_mut().find(|d| d.uid == p.doc) { + Some(d) => (d.wb.clone(), d.touched.take().unwrap_or_default()), + None => (before.clone(), Vec::new()), + }; + let rolled = s.group_rollback(token); + saved.restore(s, p.doc); + rolled?; + let (after, touched) = if matches!(m.stopped, Some((_, true))) { (before.clone(), Vec::new()) } else { (after, touched) }; + let mut fp = diff(&before, &after, &p.inputs, &touched); + fp.error = outcome.err(); + fp.stopped = m.stopped.map(|(msg, _)| msg); + fp.partial = m.partial.map(|(step, reason)| json!({"step": step, "reason": reason})); + Ok(fp) +} + +/// Compares the workbook before and after a dry run. `touched` adds the cells commands wrote +/// without changing them (a formula written again), so a shift moves everything the macro writes. +fn diff(before: &Workbook, after: &Workbook, inputs: &[Bound], touched: &[(String, CellRef)]) -> Footprint { + let mut fp = Footprint::default(); + // Sheets are matched by name; what's left is matched in order (a renamed sheet). + let mut pairs: Vec<(usize, Option)> = before.sheets.iter().enumerate().map(|(bi, sh)| (bi, after.sheet_index(&sh.name))).collect(); + let matched: BTreeSet = pairs.iter().filter_map(|(_, a)| *a).collect(); + let mut spare = (0..after.sheets.len()).filter(|a| !matched.contains(a)); + for (_, a) in pairs.iter_mut().filter(|(_, a)| a.is_none()) { + *a = spare.next(); + } + fp.sheets_added = spare.filter_map(|a| after.sheet(a).map(|s| s.name.clone())).collect(); + for (bi, ai) in pairs { + let Some(bs) = before.sheets.get(bi) else { continue }; + let Some(ai) = ai else { + fp.sheets_removed.push(bs.name.clone()); + continue; + }; + let Some(after_sheet) = after.sheets.get(ai) else { continue }; + let rewritten: BTreeSet = touched.iter().filter(|(n, _)| *n == after_sheet.name).map(|(_, c)| *c).collect(); + if Arc::ptr_eq(bs, after_sheet) && rewritten.is_empty() { + continue; + } + let (added, changed, removed) = objects(bs, after_sheet); + fp.objects_added += added; + fp.objects_changed += changed; + fp.objects_removed += removed; + let in_input = |c: CellRef| inputs.iter().any(|(_, p, _)| p.sheet == bi && p.range.contains(c)); + let mut written = Vec::new(); + let mut conflicts = Vec::new(); + let mut cells: BTreeSet = bs.cells.diff(&after_sheet.cells).into_iter().collect(); + cells.extend(rewritten.iter().copied()); + for c in cells { + if in_input(c) { + continue; + } + let (b, a) = (bs.cell(c), after_sheet.cell(c)); + if recalculated_only(b, a) && !rewritten.contains(&c) { + continue; + } + written.push(c); + if b.is_some_and(has_content) && content_differs(b, a) { + conflicts.push(c); + } + } + let Some(first) = written.first() else { continue }; + let bounds = written.iter().fold(RangeRef::cell(*first), |r, c| r.union(&RangeRef::cell(*c))); + fp.sheets.push(SheetFootprint { + sheet: bs.name.clone(), + index: bi, + changed: written.len(), + conflict_count: conflicts.len(), + cells: ranges(&mut written), + conflicts: ranges(&mut conflicts), + bounds: Some(bounds.a1()), + bounds_range: Some(bounds), + }); + } + fp +} + +/// Only a formula's cached value changed (a recalculation, not a write). +fn recalculated_only(b: Option<&Cell>, a: Option<&Cell>) -> bool { + match (b, a) { + (Some(b), Some(a)) => match (&b.formula, &a.formula) { + (Some(x), Some(y)) => x.text == y.text && x.array == y.array && b.style == a.style, + _ => false, + }, + _ => false, + } +} + +fn has_content(c: &Cell) -> bool { + c.formula.is_some() || !(c.value.is_empty() || c.value.as_text() == Some("")) +} + +fn content_differs(b: Option<&Cell>, a: Option<&Cell>) -> bool { + match (b, a) { + (Some(b), Some(a)) => match (&b.formula, &a.formula) { + (Some(x), Some(y)) => x.text != y.text, + (None, None) => b.value != a.value, + _ => true, + }, + (Some(b), None) => has_content(b), + _ => false, + } +} + +/// Objects added, changed and removed between two versions of a sheet. +fn objects(b: &Sheet, a: &Sheet) -> (usize, usize, usize) { + fn count(b: &[T], a: &[T], id: impl Fn(&T) -> u32) -> (usize, usize, usize) { + let added = a.iter().filter(|x| !b.iter().any(|y| id(y) == id(x))).count(); + let removed = b.iter().filter(|x| !a.iter().any(|y| id(y) == id(x))).count(); + let changed = b.iter().filter(|x| a.iter().any(|y| id(y) == id(x) && y != *x)).count(); + (added, changed, removed) + } + let parts = [count(&b.charts, &a.charts, |o| o.id), count(&b.images, &a.images, |o| o.id), count(&b.shapes, &a.shapes, |o| o.id)]; + parts.iter().fold((0, 0, 0), |t, p| (t.0 + p.0, t.1 + p.1, t.2 + p.2)) +} + +/// Cells as A1 ranges: runs along each row, then runs with the same columns on following rows. +pub(crate) fn ranges(cells: &mut [CellRef]) -> Vec { + cells.sort_unstable_by_key(|c| (c.row, c.col)); + let mut runs: Vec = Vec::new(); + for c in cells.iter() { + match runs.last_mut() { + Some(r) if r.end.row == c.row && r.end.col.checked_add(1) == Some(c.col) => r.end.col = c.col, + _ => runs.push(RangeRef::cell(*c)), + } + } + let mut merged: Vec = Vec::new(); + for r in runs { + // The block above with the same columns that ends on the row before. + match merged + .iter_mut() + .rev() + .take(64) + .find(|m| m.start.col == r.start.col && m.end.col == r.end.col && m.end.row.checked_add(1) == Some(r.start.row)) + { + Some(m) => m.end.row = r.end.row, + None => merged.push(r), + } + } + merged.iter().take(MAX_LISTED_RANGES).map(RangeRef::a1).collect() +} + +/// Inserts blank cells where the footprint lands (moving what is there down or right), so the +/// macro writes into empty cells. +fn make_room(s: &mut Session, fp: &Footprint, down: bool) -> Result<()> { + if !fp.sheets_removed.is_empty() || fp.objects_removed > 0 { + return Err(EngineError::Other("Shifting can't make room: the macro deletes sheets or objects.".into())); + } + let wb = s.doc()?.wb.clone(); + for sh in fp.sheets.iter().filter(|s| s.conflict_count > 0) { + let Some(bounds) = sh.bounds_range else { continue }; + let env = Env { wb: &wb, active_sheet: usize::MAX, inputs: &[], output: None, item: None }; + let range = resolve::a1(Place { sheet: sh.index, range: bounds }, &env); + s.execute("home.insertCells", json!({"range": range, "shift": if down { "down" } else { "right" }}))?; + } + Ok(()) +} + +// ---------------------------------------------------------------- run + +/// A macro that passed its checks and is about to run. +pub struct Begun { + pub mac: Macro, + pub prepared: Prepared, + pub rule: OnConflict, + pub time_limit_ms: f64, + pub inputs: Json, +} + +/// The checks before a run, on the active workbook: the macro exists, the policy allows it, the +/// person approved it if it needs that, and its inputs are right. +pub fn begin(s: &mut Session, name: &str, p: &Json) -> Result { + let d = s.doc()?; + let (doc, trust) = (d.uid, d.trust); + let m = super::find(&d.wb, name) + .and_then(|i| d.wb.macros.get(i)) + .cloned() + .ok_or_else(|| EngineError::Other(format!("There is no macro called '{name}'.")))?; + let asks = gate(&m, trust)?; + approve(s, &m, doc, &asks, p.get("approve").and_then(Json::as_bool).unwrap_or(false))?; + let prepared = prepare(s, &m, p.get("inputs"))?; + let rule = conflict_rule(p, &m)?; + let time_limit_ms = p.get("timeLimit").and_then(Json::as_f64).map(|t| (t * 1000.0).clamp(1.0, 600_000.0)).unwrap_or(DEFAULT_TIME_LIMIT_MS); + let inputs = prepared.inputs_json(&s.doc()?.wb); + Ok(Begun { mac: m, prepared, rule, time_limit_ms, inputs }) +} + +/// The footprint check and the output rule, inside the run's undo group: an error when the macro +/// would fail or write over data it may not, after shifting the data if the rule says so. Returns +/// whether it shifted. +pub fn make_way(s: &mut Session, b: &Begun) -> Result { + let (m, rule) = (&b.mac, b.rule); + if rule == OnConflict::Overwrite { + return Ok(false); + } + let fp = footprint(s, &b.prepared)?; + if let Some(e) = fp.error { + return Err(e.into_engine(&m.name)); + } + if fp.conflict_count() == 0 { + return Ok(false); + } + let what = fp.describe_conflicts(); + match rule { + OnConflict::Ask => Err(EngineError::Confirm(format!( + "Macro '{}' would write over cells that hold data ({what}). Overwrite them, or run again with onConflict shiftDown, shiftRight or fail", + m.name + ))), + OnConflict::Fail | OnConflict::Overwrite => { + Err(EngineError::Other(format!("Macro '{}' would write over cells that hold data ({what}). Nothing was changed.", m.name))) + } + OnConflict::ShiftDown | OnConflict::ShiftRight => { + make_room(s, &fp, rule == OnConflict::ShiftDown)?; + let again = footprint(s, &b.prepared)?; + if let Some(e) = again.error { + return Err(e.into_engine(&m.name)); + } + if again.conflict_count() > 0 { + return Err(EngineError::Other(format!( + "Macro '{}': shifting the data didn't make room ({}). Nothing was changed.", + m.name, + again.describe_conflicts() + ))); + } + Ok(true) + } + } +} + +/// Runs macro `name` on the active workbook: `{inputs?, onConflict?, overwrite?, approve?, +/// timeLimit?}` (see `macro.run`). +pub fn run(s: &mut Session, name: &str, p: &Json) -> Result { + let b = begin(s, name, p)?; + let label = format!("Run Macro: {}", b.mac.name); + s.grouped(&label, |s| { + let shifted = make_way(s, &b)?; + let mut machine = Machine::new(&b.prepared, false); + machine.time_limit_ms = b.time_limit_ms; + let name = &b.mac.name; + machine.run_to_end(s).map_err(|e| e.into_engine(name))?; + match machine.stopped { + Some((msg, true)) => Err(EngineError::Other(format!("Macro '{name}' stopped: {msg} Nothing was changed."))), + stopped => { + let mut out = json!({"macro": name, "ran": machine.executed, "inputs": b.inputs}); + if let Some((msg, _)) = stopped { + out["stopped"] = json!(msg); + } + if shifted { + out["shifted"] = json!(b.rule.name()); + } + Ok(out) + } + } + }) +} + +/// The conflict rule for this run: `onConflict`, or `overwrite: true` (the answer to the +/// question), else the macro's own. +fn conflict_rule(p: &Json, m: &Macro) -> Result { + if let Some(t) = p.get("onConflict").and_then(Json::as_str) { + return OnConflict::parse(t) + .ok_or_else(|| EngineError::Other(format!("onConflict must be ask, fail, overwrite, shiftDown or shiftRight, not '{t}'."))); + } + if p.get("overwrite").and_then(Json::as_bool) == Some(true) { + return Ok(OnConflict::Overwrite); + } + Ok(m.output.on_conflict) +} + +/// `macro.check`: what running the macro would do, without changing anything. +pub fn check(s: &mut Session, name: &str, p: &Json) -> Result { + let d = s.doc()?; + let (doc, trust) = (d.uid, d.trust); + let m = super::find(&d.wb, name) + .and_then(|i| d.wb.macros.get(i)) + .cloned() + .ok_or_else(|| EngineError::Other(format!("There is no macro called '{name}'.")))?; + let mut out = json!({"macro": m.name, "ok": false, "onConflict": m.output.on_conflict.name()}); + let asks = match gate(&m, trust) { + Ok(a) => a, + Err(e) => { + out["errors"] = json!([e.to_string()]); + return Ok(out); + } + }; + let approved = s.macro_approvals.contains(&(doc, super::hash(&m))); + if !asks.is_empty() && !approved { + // Nothing from an unapproved macro runs, not even in a dry run. + out["needsApproval"] = json!(asks); + } + let prepared = match prepare(s, &m, p.get("inputs")) { + Ok(pr) => pr, + Err(e) => { + out["errors"] = json!([e.to_string()]); + return Ok(out); + } + }; + out["inputs"] = prepared.inputs_json(&s.doc()?.wb); + if let Some(o) = prepared.output { + let wb = &s.doc()?.wb; + out["output"] = json!(resolve::a1(o, &Env { wb, active_sheet: wb.active_sheet, inputs: &[], output: None, item: None })); + } + if !asks.is_empty() && !approved { + return Ok(out); + } + let fp = footprint(s, &prepared)?; + out["ok"] = json!(fp.error.is_none()); + out["conflicts"] = json!(fp.conflict_count()); + if let Some(e) = &fp.error { + out["errors"] = json!([e.describe()]); + } + out["footprint"] = serde_json::to_value(&fp).unwrap_or(Json::Null); + Ok(out) +} diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 9a28f14f90..f0845a54be 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -213,6 +213,11 @@ fn names() { assert_eq!(unique_name(&wb, &long).chars().count(), MAX_MACRO_NAME); assert_eq!(find(&wb, " MACRO1 "), Some(0)); assert_eq!(find(&wb, "Macro2"), None); + let a = sample_macro("A"); + let mut b = a.clone(); + assert_eq!(hash(&a), hash(&b)); + b.steps[0].note = "changed".into(); + assert_ne!(hash(&a), hash(&b)); } #[test] diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index b5ac30d43f..74b7621eb0 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3344,4 +3344,5 @@ fn csv_as_text_keeps_identifier_columns_as_written() { let saved = s.execute("file.saveBytes", json!({"format": "csv"})).unwrap(); assert_eq!(crate::io::base64_decode(saved["base64"].as_str().unwrap()).unwrap(), csv.replace('\n', "\r\n").as_bytes()); } +mod macros; mod seams; diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs new file mode 100644 index 0000000000..fcc418b566 --- /dev/null +++ b/crates/engine/src/tests/macros.rs @@ -0,0 +1,436 @@ +//! Macros: running, input checks, the footprint dry run, the policy and the limits. + +use std::sync::Arc; + +use serde_json::{Value as Json, json}; + +use super::{s, v}; +use crate::{EngineError, Session}; + +fn undo_labels(s: &Session) -> Vec { + s.doc().unwrap().undo.iter().map(|e| e.label.clone()).collect() +} + +fn set(s: &mut Session, cell: &str, input: &str) { + s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap(); +} + +/// Puts a macro into the active workbook (replacing one with the same name). +pub(super) fn put(s: &mut Session, m: Json) { + let m = crate::macros::parse_macro(&m).unwrap(); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + wb.macros.retain(|x| !x.name.eq_ignore_ascii_case(&m.name)); + wb.macros.push(m); + d.wb = Arc::new(wb); + d.saved = d.wb.clone(); +} + +/// Names in B2:B5 and amounts in C2:C5 under a header row. +fn data(s: &mut Session) { + for (cell, input) in [("B2", "Name"), ("C2", "Amount"), ("B3", "Ann"), ("C3", "120"), ("B4", "Bo"), ("C4", "80"), ("B5", "Cy"), ("C5", "200")] { + set(s, cell, input); + } +} + +/// A macro that writes a label and a total next to its input and bolds the header. +fn totals() -> Json { + json!({ + "name": "Totals", + "inputs": [{"name": "Input", "header": true, "cols": [2, 2], "colTypes": ["text", "number"]}], + "output": {"anchor": "Input.right(2).cell(1,2)"}, + "steps": [ + {"command": "cell.set", "params": {"cell": "Output", "input": "Total"}}, + {"command": "cell.set", "params": {"cell": "Output.offset(1,0)", "input": "=SUM({{Input.data.col(2)}})"}}, + {"command": "home.bold", "params": {"range": "Input.row(1)"}} + ] + }) +} + +fn bold(s: &Session, a: &str) -> bool { + let d = s.doc().unwrap(); + let sh = d.wb.active().unwrap(); + d.wb.styles.get(sh.style_id(gridcraft_core::CellRef::parse(a).unwrap())).font.bold +} + +#[test] +fn a_run_resolves_tokens_and_is_one_undo_step() { + let mut s = s(); + data(&mut s); + put(&mut s, totals()); + let before = undo_labels(&s); + let r = s.execute("macro.run", json!({"name": "totals", "inputs": {"Input": "B2:C5"}})).unwrap(); + assert_eq!(r["ran"], 3); + assert_eq!(r["inputs"], json!({"Input": "B2:C5"})); + assert_eq!(v(&s, "E2").display(), "Total"); + assert_eq!(v(&s, "E3").display(), "400"); + assert_eq!(s.doc().unwrap().wb.active().unwrap().input_text(gridcraft_core::CellRef::parse("E3").unwrap()), "=SUM(C3:C5)"); + assert!(bold(&s, "B2") && bold(&s, "C2")); + let after = undo_labels(&s); + assert_eq!(after.len(), before.len() + 1); + assert_eq!(after.last().unwrap(), "Run Macro: Totals"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "E2").is_empty() && v(&s, "E3").is_empty() && !bold(&s, "B2")); + assert_eq!(undo_labels(&s), before); + // The selection is the default input. + s.execute("selection.set", json!({"range": "B2:C5"})).unwrap(); + s.execute("macro.run", json!({"name": "Totals"})).unwrap(); + assert_eq!(v(&s, "E3").display(), "400"); +} + +#[test] +fn inputs_are_checked_with_messages_that_name_the_cells() { + let mut s = s(); + data(&mut s); + set(&mut s, "C4", "eighty"); + set(&mut s, "C5", "TRUE"); + put(&mut s, totals()); + let start = s.doc().unwrap().wb.clone(); + let err = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5"})).unwrap_err().to_string(); + assert!(err.contains("Input column 2 (Amount) must hold numbers; C4 holds text, C5 holds TRUE or FALSE."), "{err}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start), "nothing ran"); + + // Shape: width, rows, a single cell, blanks, dates. + let mut m = totals(); + m["inputs"][0] = json!({"name": "Input", "header": true, "rows": [1, 2], "cols": [3, 3], "nonEmpty": true, "colTypes": ["any", "date"]}); + put(&mut s, m); + set(&mut s, "B4", ""); + let err = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5"})).unwrap_err().to_string(); + for needle in [ + "Input needs at most 2 data rows; B2:C5 has 3.", + "Input needs exactly 3 columns; B2:C5 has 2.", + "Input column 2 (Amount) must hold dates; C3 holds a number that isn't a date, C4 holds text, C5 holds TRUE or FALSE.", + "Input must have no blank cells; B4 is blank.", + ] { + assert!(err.contains(needle), "missing `{needle}` in {err}"); + } + let mut m = totals(); + m["inputs"][0] = json!({"name": "Input", "kind": "cell"}); + put(&mut s, m); + let err = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5"})).unwrap_err().to_string(); + assert!(err.contains("Input must be a single cell; B2:C5 is 8 cells."), "{err}"); + // A second input with no range, and a bad reference. + put(&mut s, json!({"name": "Two", "inputs": [{"name": "Input"}, {"name": "Rates"}], "steps": []})); + let err = s.execute("macro.run", json!({"name": "Two"})).unwrap_err().to_string(); + assert!(err.contains("Rates needs a range"), "{err}"); + let err = s.execute("macro.run", json!({"name": "Two", "inputs": {"Rates": "Nowhere!A1"}})).unwrap_err().to_string(); + assert!(err.contains("Rates: There is no sheet named 'Nowhere'."), "{err}"); + // Whole columns shrink to the cells in use. + put( + &mut s, + json!({"name": "Col", "inputs": [{"name": "Input", "rows": [1, 10]}], "steps": [{"command": "cell.set", "params": {"cell": "Input.below", "input": "end"}}], "output": {"onConflict": "overwrite"}}), + ); + s.execute("macro.run", json!({"name": "Col", "inputs": "B:B"})).unwrap(); + assert_eq!(v(&s, "B6").display(), "end"); + assert!(s.execute("macro.run", json!({"name": "nope"})).unwrap_err().to_string().contains("no macro called 'nope'")); +} + +#[test] +fn an_error_rolls_everything_back_and_names_the_step() { + let mut s = s(); + data(&mut s); + let mut m = totals(); + m["output"]["onConflict"] = json!("overwrite"); + m["steps"][2] = json!({"type": "repeat", "over": "Input.data", "body": [ + {"command": "home.italic", "params": {"range": "Row"}}, + {"command": "cell.set", "params": {"cell": "Row.cell(1,9)", "input": "=1/"}} + ]}); + put(&mut s, m); + let (before, undo) = (s.doc().unwrap().wb.clone(), undo_labels(&s)); + let err = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5"})).unwrap_err().to_string(); + assert!(err.starts_with("Macro 'Totals' stopped. Step 3.2 (cell.set):"), "{err}"); + assert!(err.ends_with("Nothing was changed."), "{err}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &before)); + assert_eq!(undo_labels(&s), undo); + assert!(s.doc().unwrap().redo.is_empty()); +} + +#[test] +fn conflicts_ask_fail_or_overwrite() { + let mut s = s(); + data(&mut s); + set(&mut s, "E3", "keep me"); + // A formula that reads what the macro writes: its value changes, but it isn't written. + set(&mut s, "H1", "=E3"); + put(&mut s, totals()); + let start = s.doc().unwrap().wb.clone(); + let r = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5"})); + let Err(EngineError::Confirm(q)) = r else { panic!("{r:?}") }; + assert!(q.contains("would write over cells that hold data (E3 on Sheet1)"), "{q}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start), "programmatic runs default to failing"); + let err = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5", "onConflict": "fail"})).unwrap_err().to_string(); + assert!(err.contains("Nothing was changed"), "{err}"); + assert!(s.execute("macro.run", json!({"name": "Totals", "onConflict": "sideways"})).is_err()); + + // macro.check shows the footprint and the conflict, and changes nothing. + let journal = s.journal.len(); + let sel = s.doc().unwrap().selection.clone(); + let c = s.execute("macro.check", json!({"name": "Totals", "inputs": "B2:C5"})).unwrap(); + assert_eq!(c["ok"], true); + assert_eq!(c["conflicts"], 1); + assert_eq!(c["output"], "E2"); + assert_eq!(c["footprint"]["sheets"][0]["cells"], json!(["E2:E3"])); + assert_eq!(c["footprint"]["sheets"][0]["conflicts"], json!(["E3"])); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); + assert_eq!((s.journal.len(), &s.doc().unwrap().selection), (journal, &sel)); + + // The answer to the question overwrites. + s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5", "overwrite": true})).unwrap(); + assert_eq!(v(&s, "E3").display(), "400"); + assert_eq!(v(&s, "H1").display(), "400"); +} + +#[test] +fn shifting_makes_room_and_is_checked_again() { + for (rule, moved) in [("shiftDown", ["E4", "E5"]), ("shiftRight", ["F2", "F3"])] { + let mut s = s(); + data(&mut s); + set(&mut s, "E2", "old 1"); + set(&mut s, "E3", "old 2"); + put(&mut s, totals()); + let r = s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5", "onConflict": rule})).unwrap(); + assert_eq!(r["shifted"], rule); + assert_eq!((v(&s, "E2").display(), v(&s, "E3").display()), ("Total".into(), "400".into()), "{rule}"); + assert_eq!((v(&s, moved[0]).display(), v(&s, moved[1]).display()), ("old 1".into(), "old 2".into()), "{rule}"); + // The shift and the run are one undo step. + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(v(&s, "E2").display(), "old 1", "{rule}"); + assert!(v(&s, moved[1]).is_empty(), "{rule}"); + } + // A cell written with what it already holds isn't a conflict, but it moves with the others, + // so rows stay together. + let mut s = super::s(); + for (cell, input) in [("F2", "x"), ("G2", "b"), ("F3", "f3"), ("G3", "g3")] { + set(&mut s, cell, input); + } + put( + &mut s, + json!({"name": "Pair", "output": {"onConflict": "shiftDown"}, "steps": [ + {"command": "cell.set", "params": {"cell": "F2", "input": "a"}}, + {"command": "cell.set", "params": {"cell": "G2", "input": "b"}} + ]}), + ); + let c = s.execute("macro.check", json!({"name": "Pair"})).unwrap(); + assert_eq!((c["conflicts"].as_u64(), &c["footprint"]["sheets"][0]["cells"]), (Some(1), &json!(["F2:G2"]))); + s.execute("macro.run", json!({"name": "Pair"})).unwrap(); + let row = |s: &Session, r: u32| (v(s, &format!("F{r}")).display(), v(s, &format!("G{r}")).display()); + assert_eq!(row(&s, 2), ("a".into(), "b".into())); + assert_eq!(row(&s, 3), ("x".into(), "b".into())); + assert_eq!(row(&s, 4), ("f3".into(), "g3".into())); + // An anchor that moves with the shifted data still lands on it: the second check refuses. + let mut s = super::s(); + data(&mut s); + set(&mut s, "E2", "old"); + s.execute("formulas.defineName", json!({"name": "Target", "refersTo": "=Sheet1!$E$2"})).unwrap(); + put( + &mut s, + json!({"name": "Named", "steps": [{"command": "cell.set", "params": {"cell": "Name:Target", "input": "new"}}], "output": {"onConflict": "shiftDown"}}), + ); + let start = s.doc().unwrap().wb.clone(); + let err = s.execute("macro.run", json!({"name": "Named"})).unwrap_err().to_string(); + assert!(err.contains("shifting the data didn't make room (E3 on Sheet1)"), "{err}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); +} + +#[test] +fn the_footprint_covers_other_sheets_objects_and_edges() { + let mut s = s(); + data(&mut s); + s.execute("home.insertSheet", json!({"name": "Notes"})).unwrap(); + set(&mut s, "A1", "note"); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + s.execute("insert.chart", json!({"range": "B2:C5", "type": "column", "at": "H2"})).unwrap(); + let chart = s.doc().unwrap().wb.sheet(0).unwrap().charts[0].id; + put( + &mut s, + json!({"name": "Wide", "output": {"onConflict": "fail"}, "steps": [ + {"command": "cell.set", "params": {"cell": "Notes!A1", "input": "x"}}, + {"command": "cell.set", "params": {"cell": "Notes!B2", "input": "y"}}, + {"command": "home.insertSheet", "params": {"name": "Extra"}}, + {"command": "sheet.activate", "params": {"sheet": "Sheet1"}}, + {"command": "insert.shape", "params": {"kind": "ellipse", "at": "J2"}}, + {"command": "object.delete", "params": {"kind": "chart", "id": chart}} + ]}), + ); + let c = s.execute("macro.check", json!({"name": "Wide", "inputs": {}})).unwrap(); + let fp = &c["footprint"]; + assert_eq!( + fp["sheets"], + json!([{"sheet": "Notes", "cells": ["A1", "B2"], "changed": 2, "conflicts": ["A1"], "conflictCount": 1, "bounds": "A1:B2"}]) + ); + assert_eq!(fp["sheetsAdded"], json!(["Extra"])); + assert_eq!((fp["objectsAdded"].as_u64(), fp["objectsRemoved"].as_u64()), (Some(1), Some(1))); + assert_eq!(c["conflicts"], 2, "the deleted chart and the note"); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 2, "the dry run's new sheet went away"); + let err = s.execute("macro.run", json!({"name": "Wide"})).unwrap_err().to_string(); + assert!(err.contains("(A1 on Notes; 1 object)"), "{err}"); + + // Running past the edge of the sheet fails before anything changes. + put( + &mut s, + json!({"name": "Edge", "inputs": [{"name": "Input"}], "steps": [{"command": "cell.set", "params": {"cell": "Input.offset(-3,0)", "input": "x"}}]}), + ); + let err = s.execute("macro.run", json!({"name": "Edge", "inputs": "B2"})).unwrap_err().to_string(); + assert!(err.contains("Step 1 (cell.set): 'Input.offset(-3,0)' runs past the top of the sheet."), "{err}"); + let c = s.execute("macro.check", json!({"name": "Edge", "inputs": "B2"})).unwrap(); + assert_eq!((c["ok"].as_bool(), c["footprint"]["error"]["edge"].as_bool()), (Some(false), Some(true))); +} + +#[test] +fn repeat_if_and_stop() { + let mut s = s(); + data(&mut s); + put( + &mut s, + json!({"name": "Flag", "inputs": [{"name": "Input", "header": true}], "output": {"onConflict": "overwrite"}, "steps": [ + {"type": "repeat", "over": "Input.data", "body": [ + {"type": "if", "cell": "Row.cell(1,2)", "op": "gt", "value": 100, + "then": [{"command": "cell.set", "params": {"cell": "Row.right", "input": "big"}}], + "else": [{"command": "cell.set", "params": {"cell": "Row.right", "input": "small"}}]}, + {"type": "if", "cell": "Row.cell(1,1)", "op": "eq", "value": "bo", "then": [{"type": "stop", "message": "Found Bo."}]} + ]}, + {"command": "cell.set", "params": {"cell": "A1", "input": "not reached"}} + ]}), + ); + let r = s.execute("macro.run", json!({"name": "Flag", "inputs": "B2:C5"})).unwrap(); + assert_eq!(r["stopped"], "Found Bo."); + assert_eq!((v(&s, "D3").display(), v(&s, "D4").display()), ("big".into(), "small".into())); + assert!(v(&s, "D5").is_empty() && v(&s, "A1").is_empty()); + assert_eq!(undo_labels(&s).last().unwrap(), "Run Macro: Flag", "a stop keeps the changes"); + // A Stop that rolls back is an error that changes nothing. + put( + &mut s, + json!({"name": "Guard", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "A1", "input": "x"}}, + {"type": "if", "cell": "C3", "op": "notEmpty", "then": [{"type": "stop", "message": "C3 must be empty.", "rollback": true}]} + ]}), + ); + let err = s.execute("macro.run", json!({"name": "Guard"})).unwrap_err().to_string(); + assert_eq!(err, "Macro 'Guard' stopped: C3 must be empty. Nothing was changed."); + assert!(v(&s, "A1").is_empty()); + // Comparisons. + for (op, value, want) in [ + ("eq", json!(120), true), + ("eq", json!("120"), true), + ("ne", json!(120), false), + ("ge", json!(120), true), + ("lt", json!(120.5), true), + ("le", json!(100), false), + ("contains", json!("12"), true), + ("empty", json!(null), false), + ] { + put( + &mut s, + json!({"name": "Cmp", "output": {"onConflict": "overwrite"}, "steps": [ + {"type": "if", "cell": "C3", "op": op, "value": value, "then": [{"command": "cell.set", "params": {"cell": "A9", "input": "yes"}}], "else": [{"command": "cell.set", "params": {"cell": "A9", "input": "no"}}]} + ]}), + ); + s.execute("macro.run", json!({"name": "Cmp"})).unwrap(); + assert_eq!(v(&s, "A9").display(), if want { "yes" } else { "no" }, "{op} {value}"); + } + // If needs one cell. + put(&mut s, json!({"name": "Many", "steps": [{"type": "if", "cell": "A1:A2", "op": "empty"}]})); + assert!(s.execute("macro.run", json!({"name": "Many"})).unwrap_err().to_string().contains("If looks at one cell")); + // Disabled steps are skipped. + put( + &mut s, + json!({"name": "Off", "output": {"onConflict": "overwrite"}, "steps": [{"command": "cell.set", "params": {"cell": "A8", "input": "x"}, "enabled": false}]}), + ); + assert_eq!(s.execute("macro.run", json!({"name": "Off"})).unwrap()["ran"], 0); +} + +#[test] +fn macros_are_bounded() { + let mut s = s(); + put(&mut s, json!({"name": "Long", "steps": [{"type": "repeat", "over": "A1:A20", "max": 10, "body": [{"type": "stop"}]}]})); + let err = s.execute("macro.run", json!({"name": "Long"})).unwrap_err().to_string(); + assert!(err.contains("Repeat over A1:A20 has 20 rows, more than its limit of 10."), "{err}"); + // 400 × 400 passes is over the step limit. + put( + &mut s, + json!({"name": "Spin", "output": {"onConflict": "overwrite"}, "steps": [{"type": "repeat", "over": "A1:A400", "body": [ + {"type": "repeat", "over": "A1:A400", "body": [{"type": "if", "cell": "A1", "op": "empty"}]} + ]}]}), + ); + let err = s.execute("macro.run", json!({"name": "Spin"})).unwrap_err().to_string(); + assert!(err.contains("ran more than 100000 steps"), "{err}"); + let err = s.execute("macro.run", json!({"name": "Spin", "timeLimit": 0.001})).unwrap_err().to_string(); + assert!(err.contains("ran longer than"), "{err}"); +} + +#[test] +fn the_policy_keeps_macros_away_from_files_and_the_app() { + let mut s = s(); + for (id, params) in [ + ("file.saveAs", json!({"path": "/tmp/x.xlsx"})), + ("file.close", json!({})), + ("app.language.set", json!({"language": "de"})), + ("automate.runScript", json!({"name": "Script 1"})), + ("edit.undo", json!({})), + ("macro.run", json!({"name": "Self"})), + ] { + put( + &mut s, + json!({"name": "Bad", "steps": [{"command": "cell.set", "params": {"cell": "A1", "input": "x"}}, {"command": id, "params": params}]}), + ); + let err = s.execute("macro.run", json!({"name": "Bad"})).unwrap_err().to_string(); + assert!(err.contains(&format!("Step 2 ({id}) can't run in a macro")), "{err}"); + assert!(v(&s, "A1").is_empty(), "{id}: nothing ran"); + } + // A disabled step isn't checked. + put(&mut s, json!({"name": "Bad", "steps": [{"command": "file.close", "enabled": false}]})); + s.execute("macro.run", json!({"name": "Bad"})).unwrap(); + + // Reading a file needs the permission, then the person's approval (once per macro content). + let dir = std::env::temp_dir().join(format!("gc-macro-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let csv = dir.join("in.csv"); + std::fs::write(&csv, "a,b\n1,2\n").unwrap(); + let step = json!({"command": "data.fromTextCsv", "params": {"path": csv.to_string_lossy(), "sheet": "Imported"}}); + put(&mut s, json!({"name": "Import", "steps": [step]})); + let err = s.execute("macro.run", json!({"name": "Import"})).unwrap_err().to_string(); + assert!(err.contains("Step 1 (data.fromTextCsv) reads a file. Add data.fromTextCsv to the macro's permissions"), "{err}"); + put(&mut s, json!({"name": "Import", "steps": [step], "permissions": ["data.fromTextCsv"]})); + let r = s.execute("macro.run", json!({"name": "Import"})); + let Err(EngineError::Approve(q)) = &r else { panic!("{r:?}") }; + assert_eq!(q, "Run macro 'Import'? It runs data.fromTextCsv, which reads a file."); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 1); + let c = s.execute("macro.check", json!({"name": "Import"})).unwrap(); + assert_eq!(c["needsApproval"], json!(["runs data.fromTextCsv, which reads a file"])); + assert!(c.get("footprint").is_none(), "nothing unapproved runs, even dry"); + s.execute("macro.run", json!({"name": "Import", "approve": true})).unwrap(); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 2); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("macro.run", json!({"name": "Import"})).unwrap(); + // The dry run skips the file step and says so. + let c = s.execute("macro.check", json!({"name": "Import"})).unwrap(); + assert_eq!(c["footprint"]["partial"]["step"], "1"); + // Changing the macro asks again. + put(&mut s, json!({"name": "Import", "description": "changed", "steps": [step], "permissions": ["data.fromTextCsv"]})); + assert!(matches!(s.execute("macro.run", json!({"name": "Import"})), Err(EngineError::Approve(_)))); + let _ = std::fs::remove_dir_all(&dir); + + // Steps that would open a dialog fail instead. + put(&mut s, json!({"name": "Pic", "output": {"onConflict": "overwrite"}, "steps": [{"command": "insert.picture"}]})); + let err = s.execute("macro.run", json!({"name": "Pic"})).unwrap_err().to_string(); + assert!(err.contains("would open the insertPicture dialog"), "{err}"); + assert!(s.ui_requests.is_empty()); +} + +#[test] +fn macros_from_a_file_ask_before_running() { + let mut s = s(); + let mut wb = gridcraft_model::Workbook::new(); + wb.macros.push(crate::macros::parse_macro(&json!({"name": "Theirs", "output": {"onConflict": "overwrite"}, "steps": [{"command": "cell.set", "params": {"cell": "A1", "input": "ran"}}]})).unwrap()); + let bytes = serde_json::to_vec(&wb).unwrap(); + s.execute("file.open", json!({"name": "theirs.json", "base64": crate::io::base64_encode(&bytes)})).unwrap(); + assert_eq!(s.doc().unwrap().trust, crate::Trust::FromFile); + let r = s.execute("macro.run", json!({"name": "Theirs"})); + let Err(EngineError::Approve(q)) = &r else { panic!("{r:?}") }; + assert_eq!(q, "Run macro 'Theirs'? It came with a workbook you opened."); + assert!(v(&s, "A1").is_empty()); + s.execute("macro.run", json!({"name": "Theirs", "approve": true})).unwrap(); + assert_eq!(v(&s, "A1").display(), "ran"); + // Approved once per macro content: the second run doesn't ask. + s.execute("macro.run", json!({"name": "Theirs"})).unwrap(); +} From 20d2a3b99861cc4a0e89be6c07fa950f9dedf0ee Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:20:38 -0400 Subject: [PATCH 032/687] Model: a macro name with control characters is refused on every path Only parse_macro refused a name holding a line break or other control character. Macros that came from the macros field of a JSON workbook were checked with check_limits alone, so a file could bring in a macro named "a\nb" that macro.save and macro.rename would refuse. The check now lives in Macro::check_limits, so every path applies it, and such a macro is dropped with the usual warning when the file is opened. --- crates/engine/src/macros/mod.rs | 3 --- crates/engine/src/macros/tests.rs | 13 +++++++++++++ crates/model/src/macros.rs | 6 +++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 1413236500..6b3ccdbd88 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -30,9 +30,6 @@ pub fn parse_macro(v: &Json) -> Result { let mut m: Macro = serde_json::from_value(v).map_err(|e| format!("This isn't a valid macro: {e}"))?; m.name = m.name.trim().to_string(); m.check_limits()?; - if m.name.chars().any(char::is_control) { - return Err("A macro name can't hold line breaks or other control characters.".into()); - } check_size(&m)?; Ok(m) } diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 1b417c8a04..fea61f6c35 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -193,3 +193,16 @@ fn names() { assert_eq!(find(&wb, " MACRO1 "), Some(0)); assert_eq!(find(&wb, "Macro2"), None); } + +#[test] +fn json_field_macros_get_the_name_check_too() { + // A JSON workbook can't bring in a name that macro.save and macro.rename refuse. + let mut wb = Workbook::new(); + wb.macros.push(Macro { name: "a\nb".into(), ..Macro::default() }); + wb.macros.push(Macro { name: "Fine".into(), ..Macro::default() }); + let bytes = crate::io::save_bytes(&wb, "ctl.json").unwrap(); + let (back, warnings) = crate::io::open_bytes("ctl.json", &bytes).unwrap(); + let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect(); + assert_eq!(names, ["Fine"]); + assert!(warnings.len() == 1 && warnings[0].starts_with("1 macro "), "{warnings:?}"); +} diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs index fa898984d1..caee00805f 100644 --- a/crates/model/src/macros.rs +++ b/crates/model/src/macros.rs @@ -63,7 +63,8 @@ impl Macro { count(&self.steps) } - /// Checks the size limits ([`MAX_MACRO_STEPS`], [`MAX_MACRO_DEPTH`], name and text lengths). + /// Checks the size limits ([`MAX_MACRO_STEPS`], [`MAX_MACRO_DEPTH`], name and text lengths) and + /// that the name has no control characters. pub fn check_limits(&self) -> Result<(), String> { let name = self.name.trim(); if name.is_empty() { @@ -72,6 +73,9 @@ impl Macro { if name.chars().count() > MAX_MACRO_NAME { return Err(format!("A macro name can be at most {MAX_MACRO_NAME} characters long.")); } + if name.chars().any(char::is_control) { + return Err("A macro name can't hold line breaks or other control characters.".into()); + } if self.description.chars().count() > MAX_MACRO_TEXT { return Err(format!("A macro description can be at most {MAX_MACRO_TEXT} characters long.")); } From 23c0107511d516dcd5978594384ba850e7d95fba Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:01:51 -0400 Subject: [PATCH 033/687] Engine: macro policy checks a step's parameters after tokens resolve; any path counts as file access A path given as a token (`{"$ref": "Input"}` or `{{Input}}`) became a file name only when the step ran, after the policy had passed it, so a macro without permissions or approval could write a file, even from macro.check. Exports also wrote to blank paths the policy ignored. Any `path` other than null now needs the permission and approval, and the runner checks the policy again on the resolved parameters before the command runs. --- crates/engine/src/macros/policy.rs | 15 +++++++++-- crates/engine/src/macros/run.rs | 21 ++++++++++------ crates/engine/src/tests/macros.rs | 40 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/macros/policy.rs b/crates/engine/src/macros/policy.rs index 7329f567fe..84bbc497b8 100644 --- a/crates/engine/src/macros/policy.rs +++ b/crates/engine/src/macros/policy.rs @@ -43,8 +43,12 @@ const READS_FILES: &[&str] = &["data.getData", "data.fromTextCsv", "insert.pictu const EXPORTS: &[&str] = &["file.exportPdf", "file.exportCsv", "file.exportHtml", "file.print", "file.printPreview", "file.saveBytes"]; /// The policy for running command `id` with `params` as a macro step in a document with `trust`. +/// +/// Any `path` other than `null` counts as touching a file: a token such as `{"$ref": "Input"}` +/// becomes a path when the step runs, and exports write to `""` and `" "` as file names. The +/// runner checks the parameters both as written and as resolved. pub fn macro_policy(id: &str, params: &Json, trust: Trust) -> Policy { - let has_path = params.get("path").and_then(Json::as_str).is_some_and(|p| !p.trim().is_empty()); + let has_path = params.get("path").is_some_and(|p| !p.is_null()); let base = if EXPORTS.contains(&id) { if has_path { Policy::NeedsPermission("writes a file".into()) } else { Policy::Allow } } else if ALLOWED.contains(&id) { @@ -104,7 +108,14 @@ mod tests { // Without a path they touch no file. for id in ["data.getData", "insert.picture", "file.exportPdf", "file.saveBytes", "file.printPreview"] { assert_eq!(macro_policy(id, &none, Trust::Local), Policy::Allow, "{id}"); - assert_eq!(macro_policy(id, &json!({"path": " "}), Trust::Local), Policy::Allow, "{id}"); + assert_eq!(macro_policy(id, &json!({"path": null}), Trust::Local), Policy::Allow, "{id}"); + } + // A path that is blank or a token still touches a file once the step runs. + for path in [json!(""), json!(" "), json!({"$ref": "Input"}), json!("{{Input}}.csv"), json!(1)] { + for id in ["file.exportCsv", "data.fromTextCsv"] { + let p = json!({ "path": path }); + assert!(matches!(macro_policy(id, &p, Trust::Local), Policy::NeedsPermission(_)), "{id} {p}"); + } } for id in ["cell.set", "home.bold", "view.macros", "edit.copy", "file.properties"] { assert_eq!(macro_policy(id, &none, Trust::Local), Policy::Allow, "{id}"); diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 3651deb0c5..aebaa8c907 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -576,29 +576,34 @@ impl Machine { fn command(&mut self, s: &mut Session, path: &str, id: &str, params: &Json) -> std::result::Result<(), StepError> { let fail = |msg: String| StepError::new(path, Some(id), msg); - match macro_policy(id, params, self.trust) { - Policy::Deny(why) => return Err(fail(format!("{id} can't run in a macro: {why}."))), - Policy::NeedsPermission(what) => { + // The policy sees the parameters the command will get, after tokens became A1 text. + let resolved = { + let wb = &s.doc().map_err(|e| fail(e.to_string()))?.wb; + resolve::resolve_params(params, &self.env(wb)).map_err(|e| StepError::resolve(path, Some(id), e))? + }; + match (macro_policy(id, params, self.trust), macro_policy(id, &resolved, self.trust)) { + (_, Policy::Deny(why)) | (Policy::Deny(why), _) => return Err(fail(format!("{id} can't run in a macro: {why}."))), + (written, Policy::NeedsPermission(what)) => { if !self.permissions.iter().any(|p| p == id) { return Err(fail(format!("{id} {what}. Add it to the macro's permissions to allow it."))); } + // The gate asked the person about this step only if it saw the file access. + if !matches!(written, Policy::NeedsPermission(_)) { + return Err(fail(format!("{id} {what} only once its parameters are filled in, so it wasn't approved."))); + } if self.dry_run { self.partial = Some((path.to_string(), format!("{id} {what}"))); self.frames.clear(); return Ok(()); } } - Policy::Allow | Policy::Ask(_) => {} + (_, Policy::Allow | Policy::Ask(_)) => {} } if self.dry_run && !previewable(id) { self.partial = Some((path.to_string(), format!("{id} can't be previewed"))); self.frames.clear(); return Ok(()); } - let resolved = { - let wb = &s.doc().map_err(|e| fail(e.to_string()))?.wb; - resolve::resolve_params(params, &self.env(wb)).map_err(|e| StepError::resolve(path, Some(id), e))? - }; self.last_params = Some(resolved.clone()); let queued = s.ui_requests.len(); let r = s.execute(id, resolved); diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index fcc418b566..c3a2c44425 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -358,6 +358,46 @@ fn macros_are_bounded() { assert!(err.contains("ran longer than"), "{err}"); } +#[test] +fn a_token_in_a_path_needs_the_permission_and_approval() { + let mut s = s(); + let dir = std::env::temp_dir().join(format!("gc-macro-token-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let files = |dir: &std::path::Path| std::fs::read_dir(dir).unwrap().count(); + let in_dir = format!("{}/{{{{Input}}}}", dir.to_string_lossy()); + for path in [json!({"$ref": "Input"}), json!(in_dir), json!(" ")] { + let step = json!({"command": "file.exportCsv", "params": {"path": path}}); + put(&mut s, json!({"name": "Export", "inputs": [{"name": "Input"}], "steps": [step]})); + // Without the permission nothing runs, not even the check's dry run. + let err = s.execute("macro.run", json!({"name": "Export"})).unwrap_err().to_string(); + assert!(err.contains("Step 1 (file.exportCsv) writes a file. Add file.exportCsv to the macro's permissions"), "{path}: {err}"); + let c = s.execute("macro.check", json!({"name": "Export"})).unwrap(); + assert!(c["errors"][0].as_str().unwrap().contains("writes a file"), "{path}: {c}"); + assert!(c.get("footprint").is_none()); + // With it, the person approves first; the check doesn't run it before then. + put(&mut s, json!({"name": "Export", "inputs": [{"name": "Input"}], "steps": [step], "permissions": ["file.exportCsv"]})); + let r = s.execute("macro.run", json!({"name": "Export"})); + let Err(EngineError::Approve(q)) = &r else { panic!("{path}: {r:?}") }; + assert!(q.contains("file.exportCsv, which writes a file"), "{q}"); + let c = s.execute("macro.check", json!({"name": "Export"})).unwrap(); + assert_eq!(c["needsApproval"], json!(["runs file.exportCsv, which writes a file"]), "{path}"); + assert!(c.get("footprint").is_none()); + assert_eq!(files(&dir), 0, "{path}"); + assert!(!std::path::Path::new("A1.csv").exists() && !std::path::Path::new(" .csv").exists()); + } + // Once approved, the check's dry run stops before the step, and only the run writes the file. + let step = json!({"command": "file.exportCsv", "params": {"path": in_dir}}); + put(&mut s, json!({"name": "Export", "inputs": [{"name": "Input"}], "steps": [step], "permissions": ["file.exportCsv"]})); + s.execute("macro.run", json!({"name": "Export", "approve": true, "inputs": "B2"})).unwrap(); + assert_eq!(files(&dir), 1); + std::fs::remove_dir_all(&dir).unwrap(); + std::fs::create_dir_all(&dir).unwrap(); + let c = s.execute("macro.check", json!({"name": "Export", "inputs": "B2"})).unwrap(); + assert_eq!(c["footprint"]["partial"]["step"], "1", "{c}"); + assert_eq!(files(&dir), 0, "the check writes nothing"); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn the_policy_keeps_macros_away_from_files_and_the_app() { let mut s = s(); From b2740bdf339c1a77dfcabe7e0c285a79ef911a23 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:19:29 -0400 Subject: [PATCH 034/687] Engine: macro recorder and macro.list, get, save, delete, rename, record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commands: macro.list, macro.get, macro.save (a macro object or its fields; replace defaults to true), macro.delete, macro.rename and macro.record, beside macro.run, macro.check and macro.debug. Saving, renaming and deleting are undoable. The catalog gains View › Macros › Record Macro (parity 275 / 291). The recorder builds on the recording buffer of top-level commands, so nested commands are never recorded twice and trimming the journal loses nothing. When it stops, commands a macro may not run are left out and listed; a macro run while recording is replaced by its steps when it has no inputs, and an Automate script run by the script's steps; sheet.activate and selection.set steps are added where the sheet or selection changed between commands without one, and runs of them collapse to the last. With relative references (the default) the selection when recording started is the example input: references in and near it become tokens (Input.col(2), Input.rows(2..), Input.col(2).below, Input.row(4).right, Input.cell(r,c)), exact references in formulas become {{...}}, and the input's width, header and column types are inferred from the example. A recording replayed on a longer block of data writes in the matching places. A toggle such as Bold is recorded with the state it set ("on": true), as Excel records Bold = True, so a replay sets it instead of flipping it again. Automate scripts are unchanged: automate.* keep their commands, parameters, storage and results, and View › Macros still lists the scripts and a preserved VBA project. --- crates/engine/src/catalog.rs | 1 + crates/engine/src/cmd/extra.rs | 17 ++ crates/engine/src/cmd/macros.rs | 165 ++++++++++- crates/engine/src/lib.rs | 13 +- crates/engine/src/macros/mod.rs | 29 +- crates/engine/src/macros/recorder.rs | 391 +++++++++++++++++++++++++++ crates/engine/src/macros/tests.rs | 3 + crates/engine/src/tests/macros.rs | 186 +++++++++++++ docs/parity-checklist.md | 7 +- 9 files changed, 805 insertions(+), 7 deletions(-) create mode 100644 crates/engine/src/macros/recorder.rs diff --git a/crates/engine/src/catalog.rs b/crates/engine/src/catalog.rs index d6b67e1b45..aefbae29c3 100644 --- a/crates/engine/src/catalog.rs +++ b/crates/engine/src/catalog.rs @@ -269,6 +269,7 @@ pub const CATALOG: &[(&str, &str, &str, &str)] = &[ ("view.unhideWindow", "View", "Window", "Unhide"), ("view.switchWindows", "View", "Window", "Switch Windows"), ("view.macros", "View", "Macros", "Macros"), + ("macro.record", "View", "Macros", "Record Macro"), // Automate ("automate.newScript", "Automate", "Scripting Tools", "New Script"), ("automate.allScripts", "Automate", "Scripting Tools", "All Scripts"), diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index 6d6516f50b..08dfc0a64f 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -999,6 +999,23 @@ fn run_script(s: &mut Session, p: &Json) -> Result { Ok(json!({"ran": n})) } +/// The command steps of the Automate script called `name`, as `automate.runScript` runs them +/// (steps that run other scripts are left out), or `None` when there is no such script. +pub(crate) fn script_steps(wb: &Workbook, name: &str) -> Option> { + let all = stash_get(wb, SCRIPTS_NAME); + let steps = all.get(name)?.as_array()?; + Some( + steps + .iter() + .take(100_000) + .filter_map(|st| { + let id = st["command"].as_str()?; + (!id.starts_with("automate.")).then(|| (id.to_string(), st["params"].clone())) + }) + .collect(), + ) +} + fn delete_script(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("automate.deleteScript", "missing `name`"))?.to_string(); edit(s, |cx| { diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index a14fc28429..e68dd92e69 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -1,12 +1,27 @@ //! Macros: typed command lists that run as one undo step. See [`crate::macros`] for the model, -//! tokens, runner, debugger and policy. Automate scripts (`automate.*`) are a separate, older feature. +//! tokens, runner, debugger, recorder and policy. Automate scripts (`automate.*`) are a separate, +//! older feature with their own storage; these commands neither read nor change them. -use serde_json::Value as Json; +use serde_json::{Value as Json, json}; use super::*; +use crate::macros::recorder::RecordingStart; pub fn specs() -> Vec { vec![ + cmd!(query "macro.list", "List Macros", [], None, "{} → [{name, description, shortcut?, steps, inputs: [names], onConflict, permissions}]", has_doc, list), + cmd!(query "macro.get", "Get Macro", [], None, "{name} → the macro: {v, name, description, shortcut?, inputs, steps, output, permissions}", has_doc, get), + cmd!( + "macro.save", + "Save Macro", + [], + None, + "{macro: {name, description?, shortcut?, inputs?: [{name, kind: cell|range, rows: [min, max], cols: [min, max], header, colTypes: [number|text|date|bool|any], nonEmpty, defaultRef?}], steps: [{type: command, id, params, enabled?, breakpoint?, note?} | {type: repeat, over, of: rows|columns, max, body} | {type: if, cell, op: eq|ne|gt|ge|lt|le|empty|notEmpty|contains, value, then, else} | {type: stop, message, rollback?}], output?: {anchor?, onConflict: ask|fail|overwrite|shiftDown|shiftRight}, permissions?: [command ids]} (or those fields at the top level), replace?: bool (default true)} → {name, steps}", + has_doc, + save + ), + cmd!("macro.delete", "Delete Macro", [], None, "{name}", has_doc, delete), + cmd!("macro.rename", "Rename Macro", [], None, "{name, to}", has_doc, rename), cmd!( "macro.run", "Run Macro", @@ -25,6 +40,15 @@ pub fn specs() -> Vec { has_doc, check ), + cmd!( + noundo "macro.record", + "Record Macro", + ["View", "Macros"], + None, + "{stop?: bool, relative?: bool (default true; when starting), name?, description? (when stopping)} toggles recording; with relative references the selection when it starts is the example input → {recording, relative?, input?} or {recording: false, saved, steps, dropped, skipped, inputs}", + has_doc, + record + ), cmd!( "macro.debug", "Step Into", @@ -41,6 +65,93 @@ fn name_param<'a>(cmd: &str, p: &'a Json) -> Result<&'a str> { str_param(p, "name").filter(|n| !n.trim().is_empty()).ok_or_else(|| bad(cmd, "missing `name`")) } +/// The index of macro `name` in the active workbook, or an error that says there is none. +fn index_of(s: &Session, name: &str) -> Result { + crate::macros::find(&s.doc()?.wb, name).ok_or_else(|| EngineError::Other(format!("There is no macro called '{name}'."))) +} + +fn list(s: &mut Session, _: &Json) -> Result { + let wb = &s.doc()?.wb; + Ok(Json::Array( + wb.macros + .iter() + .map(|m| { + let mut o = json!({ + "name": m.name, + "description": m.description, + "steps": m.step_count(), + "inputs": m.inputs.iter().map(|i| i.name.clone()).collect::>(), + "onConflict": m.output.on_conflict.name(), + "permissions": m.permissions, + }); + if let Some(k) = &m.shortcut { + o["shortcut"] = json!(k); + } + o + }) + .collect(), + )) +} + +fn get(s: &mut Session, p: &Json) -> Result { + let i = index_of(s, name_param("macro.get", p)?)?; + let wb = &s.doc()?.wb; + Ok(wb.macros.get(i).map(crate::macros::macro_json).unwrap_or(Json::Null)) +} + +fn save(s: &mut Session, p: &Json) -> Result { + let v = p.get("macro").filter(|m| m.is_object()).unwrap_or(p); + let m = crate::macros::parse_macro(v).map_err(|e| bad("macro.save", e))?; + let replace = bool_param(p, "replace").unwrap_or(true); + let (name, steps) = (m.name.clone(), m.step_count()); + edit(s, |cx| { + match crate::macros::find(&cx.wb, &m.name) { + Some(_) if !replace => return Err(EngineError::Other(format!("A macro called '{}' already exists.", m.name))), + Some(i) => { + if let Some(old) = cx.wb.macros.get_mut(i) { + *old = m; + } + } + None if cx.wb.macros.len() >= gridcraft_model::MAX_MACROS => { + return Err(EngineError::Other(format!("A workbook can hold at most {} macros.", gridcraft_model::MAX_MACROS))); + } + None => cx.wb.macros.push(m), + } + Ok(()) + })?; + Ok(json!({"name": name, "steps": steps})) +} + +fn delete(s: &mut Session, p: &Json) -> Result { + let i = index_of(s, name_param("macro.delete", p)?)?; + edit(s, |cx| { + if i < cx.wb.macros.len() { + cx.wb.macros.remove(i); + } + Ok(Json::Null) + }) +} + +fn rename(s: &mut Session, p: &Json) -> Result { + let i = index_of(s, name_param("macro.rename", p)?)?; + let to = str_param(p, "to").map(str::trim).filter(|t| !t.is_empty()).ok_or_else(|| bad("macro.rename", "missing `to`"))?.to_string(); + if to.chars().any(char::is_control) { + return Err(bad("macro.rename", "a macro name can't hold line breaks or other control characters")); + } + if crate::macros::find(&s.doc()?.wb, &to).is_some_and(|j| j != i) { + return Err(EngineError::Other(format!("A macro called '{to}' already exists."))); + } + edit(s, |cx| { + let Some(m) = cx.wb.macros.get_mut(i) else { return Ok(()) }; + let mut renamed = m.clone(); + renamed.name = to.clone(); + renamed.check_limits().map_err(EngineError::Other)?; + *m = renamed; + Ok(()) + })?; + Ok(json!({"name": to})) +} + fn run(s: &mut Session, p: &Json) -> Result { let name = name_param("macro.run", p)?; crate::macros::run::run(s, name, p) @@ -50,3 +161,53 @@ fn check(s: &mut Session, p: &Json) -> Result { let name = name_param("macro.check", p)?; crate::macros::run::check(s, name, p) } + +fn record(s: &mut Session, p: &Json) -> Result { + if bool_param(p, "stop").unwrap_or(false) || s.is_recording() { + return stop_recording(s, p); + } + start_recording(s, bool_param(p, "relative").unwrap_or(true)) +} + +fn start_recording(s: &mut Session, relative: bool) -> Result { + let d = s.doc()?; + let start = RecordingStart { + doc: d.uid, + sheet: d.wb.active_sheet, + sheet_name: d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), + selection: d.selection.clone(), + relative, + }; + let input = start.selection.current().a1(); + s.start_recording(); + s.macro_recording = Some(start); + Ok(if relative { json!({"recording": true, "relative": true, "input": input}) } else { json!({"recording": true, "relative": false}) }) +} + +/// Stops recording and saves what was recorded as a macro. +fn stop_recording(s: &mut Session, p: &Json) -> Result { + let Some(rec) = s.stop_recording() else { return Ok(json!({"recording": false})) }; + let start = s.macro_recording.take(); + let d = s.doc()?; + let start = start.unwrap_or_else(|| RecordingStart { + doc: d.uid, + sheet: d.wb.active_sheet, + sheet_name: d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), + selection: d.selection.clone(), + relative: true, + }); + let wb = d.wb.clone(); + let name = match str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()) { + Some(n) => n.to_string(), + None => crate::macros::next_name(&wb), + }; + let mut converted = crate::macros::recorder::convert(&name, &start, &rec.steps, &wb); + converted.mac.steps.truncate(gridcraft_model::MAX_MACRO_STEPS); + if let Some(desc) = str_param(p, "description") { + converted.mac.description = desc.chars().take(gridcraft_model::MAX_MACRO_TEXT).collect(); + } + let steps = converted.mac.steps.len(); + let inputs: Vec = converted.mac.inputs.iter().map(|i| i.name.clone()).collect(); + s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac)}))?; + Ok(json!({"recording": false, "saved": name, "steps": steps, "dropped": rec.dropped, "skipped": converted.skipped, "inputs": inputs})) +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 242c86b97a..2f6c252fca 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -327,6 +327,8 @@ pub struct Session { pub macro_approvals: std::collections::HashSet<(u64, String)>, /// The macro being debugged, between debugger actions. pub macro_debug: Option, + /// Where the macro recorder started (beside `recorder`, its buffer). + pub macro_recording: Option, /// Windows hidden with View › Hide. pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. @@ -462,7 +464,16 @@ impl Session { if let Some((doc, sheet, sheet_name, selection)) = place && let Some(rec) = self.recorder.as_mut() { - rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params: cmd::journal_params(¶ms) }); + // A toggle (Bold) is recorded with the state it set, as Excel records + // `Bold = True`: playing it back sets that state rather than flipping it again. + let mut params = cmd::journal_params(¶ms); + if let (Ok(Json::Object(r)), Json::Object(p)) = (&result, &mut params) + && !p.contains_key("on") + && let Some(on @ Json::Bool(_)) = r.get("on") + { + p.insert("on".into(), on.clone()); + } + rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params }); } self.journal.push((id.to_string(), cmd::journal_params(¶ms))); if self.journal.len() > 10_000 { diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index b25f06cb18..872f80513c 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -9,13 +9,14 @@ use std::collections::{BTreeMap, HashMap, HashSet}; -use gridcraft_model::{DefinedName, Macro, Workbook}; +use gridcraft_model::{DefinedName, Macro, Step, StepKind, Workbook}; use serde_json::{Value as Json, json}; use crate::cmd::stash; pub mod debug; pub mod policy; +pub mod recorder; pub mod resolve; pub mod run; @@ -261,5 +262,31 @@ impl Taken { } } +/// The first free `Macro1`, `Macro2`… name. +pub fn next_name(wb: &Workbook) -> String { + (1..).map(|n| format!("Macro{n}")).find(|n| find(wb, n).is_none()).unwrap_or_else(|| "Macro".into()) +} + +/// Every command step of `steps` (inside Repeat and If too), with its path. +pub fn command_steps(steps: &[Step]) -> Vec<(String, &str, &Json)> { + fn walk<'a>(steps: &'a [Step], prefix: &str, out: &mut Vec<(String, &'a str, &'a Json)>) { + for (i, st) in steps.iter().enumerate() { + let path = if prefix.is_empty() { (i + 1).to_string() } else { format!("{prefix}.{}", i + 1) }; + match &st.kind { + StepKind::Command { id, params } => out.push((path, id.as_str(), params)), + StepKind::Repeat { body, .. } => walk(body, &path, out), + StepKind::If { then, otherwise, .. } => { + walk(then, &format!("{path}.then"), out); + walk(otherwise, &format!("{path}.else"), out); + } + StepKind::Stop { .. } => {} + } + } + } + let mut out = Vec::new(); + walk(steps, "", &mut out); + out +} + #[cfg(test)] mod tests; diff --git a/crates/engine/src/macros/recorder.rs b/crates/engine/src/macros/recorder.rs new file mode 100644 index 0000000000..56a64a72f5 --- /dev/null +++ b/crates/engine/src/macros/recorder.rs @@ -0,0 +1,391 @@ +//! Turning a recording into a macro. +//! +//! The recording buffer (`crate::record`) holds the top-level commands run while recording, each +//! with the sheet and selection it ran in. When recording stops: +//! +//! - commands a macro may not run (saving, opening files…) and other macros' commands are left +//! out; a macro run while recording is replaced by its steps when it has no inputs, and an +//! Automate script run while recording by the script's steps; +//! - a `sheet.activate` or `selection.set` step is added where the sheet or selection changed +//! between commands without one, and runs of `selection.set` collapse to the last; +//! - with relative references (the default), the selection when recording started is the example +//! input: references in, next to or near it become tokens (`Input.col(2)`, `Input.below`, +//! `Input.cell(1,4)`), also inside formulas (`{{Input.col(2)}}`), and the input's shape, +//! header and column types are inferred from it. + +use gridcraft_core::{CellRef, RangeRef, Value}; +use gridcraft_model::{ColType, InputKind, Macro, NamedInput, Step, Workbook}; +use serde_json::{Map, Value as Json, json}; + +use super::policy::{Policy, macro_policy}; +use crate::Selection; +use crate::record::RecordedStep; + +/// How far outside the example input a reference may be and still be made relative to it. +const NEAR_ROWS: u32 = 50; +const NEAR_COLS: u32 = 26; + +/// Where recording started, kept beside the buffer. +#[derive(Clone, Debug)] +pub struct RecordingStart { + pub doc: u64, + pub sheet: usize, + pub sheet_name: String, + pub selection: Selection, + pub relative: bool, +} + +/// A recording turned into a macro, with what was left out. +pub struct Converted { + pub mac: Macro, + /// Commands left out (ids), in order. + pub skipped: Vec, +} + +/// Builds a macro called `name` from the recorded steps. +pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: &Workbook) -> Converted { + let mut skipped = Vec::new(); + // Commands as they'll be played back: (id, params, sheet before, selection before, whether + // they came from another macro and stay as written). + let mut cmds: Vec<(String, Json, usize, String, Selection, bool)> = Vec::new(); + for st in steps.iter().filter(|st| st.doc == start.doc) { + let id = st.id.as_str(); + if id == "macro.run" { + // Another macro's steps, when they stand on their own. + let inner = st.params.get("name").and_then(Json::as_str).and_then(|n| super::find(wb, n)).and_then(|i| wb.macros.get(i)); + match inner { + Some(m) if m.inputs.is_empty() => { + for (_, cid, params) in super::command_steps(&m.steps) { + cmds.push((cid.to_string(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + } + } + _ => skipped.push(id.to_string()), + } + continue; + } + if id == "automate.runScript" { + // An Automate script's steps, as the script runs them. + match st.params.get("name").and_then(Json::as_str).and_then(|n| crate::cmd::extra::script_steps(wb, n)) { + Some(steps) => { + for (cid, params) in steps { + cmds.push((cid, params, st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + } + } + None => skipped.push(id.to_string()), + } + continue; + } + if matches!(macro_policy(id, &st.params, crate::Trust::Local), Policy::Deny(_) | Policy::NeedsPermission(_)) { + skipped.push(id.to_string()); + continue; + } + cmds.push((id.to_string(), st.params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), false)); + } + + // Sheet and selection changes made without a command: where a command ran on another sheet, + // or with another selection, than the one before it, say so (a command that moved the + // selection itself does it again when played back, so the extra step is harmless). + let mut out: Vec<(String, Json, bool)> = Vec::new(); + let (mut sheet, mut sel) = (start.sheet, start.selection.clone()); + for (id, params, at_sheet, sheet_name, at_sel, kept) in cmds { + if at_sheet != sheet { + out.push(("sheet.activate".into(), json!({"sheet": sheet_name}), false)); + } + // A recorded selection.set just before already put the selection there. + let selected = matches!(out.last(), Some((last, ..)) if last == "selection.set"); + if (at_sel != sel || at_sheet != sheet) && !selected { + out.push(("selection.set".into(), selection_params(&at_sel), false)); + } + (sheet, sel) = (at_sheet, at_sel); + out.push((id, params, kept)); + } + // Runs of selection.set (or sheet.activate): the last one wins. + let mut collapsed: Vec<(String, Json, bool)> = Vec::new(); + for (id, params, kept) in out { + if matches!(id.as_str(), "selection.set" | "sheet.activate") + && let Some((last, ..)) = collapsed.last() + && *last == id + { + collapsed.pop(); + } + collapsed.push((id, params, kept)); + } + + // Relative references. + let example = start.selection.current(); + let mut used_input = false; + let steps: Vec = collapsed + .into_iter() + .map(|(id, params, kept)| { + let params = if start.relative && !kept { relative(¶ms, example, &start.sheet_name, &mut used_input, None) } else { params }; + Step::command(id, params) + }) + .collect(); + let mut mac = Macro { name: name.to_string(), steps, ..Macro::default() }; + if start.relative && used_input { + mac.inputs = vec![infer_input(wb, start.sheet, example)]; + } + Converted { mac, skipped } +} + +fn selection_params(sel: &Selection) -> Json { + let ranges: Vec = sel.ranges.iter().map(RangeRef::a1).collect(); + json!({"range": ranges.join(","), "active": sel.active.a1()}) +} + +/// `params` with references near `example` made into tokens. +fn relative(v: &Json, example: RangeRef, sheet: &str, used: &mut bool, key: Option<&str>) -> Json { + match v { + Json::Object(o) => { + let mut out = Map::new(); + for (k, x) in o { + out.insert(k.clone(), relative(x, example, sheet, used, Some(k))); + } + Json::Object(out) + } + Json::Array(a) => Json::Array(a.iter().map(|x| relative(x, example, sheet, used, key)).collect()), + Json::String(t) if key.is_some_and(super::resolve::is_ref_key) => { + // A list of areas ("A1:B2,D4") stays as it is unless every part has a token. + let parts: Option> = t.split(',').map(|part| ref_token(part, example, sheet)).collect(); + match parts { + Some(p) if p.len() == 1 => { + *used = true; + json!(p.join("")) + } + _ => v.clone(), + } + } + Json::String(t) if t.starts_with('=') && example.count() > 1 => { + let (f, changed) = formula_tokens(t, example); + *used |= changed; + json!(f) + } + other => other.clone(), + } +} + +/// The token for an A1 reference (on the example's sheet) near the example input. +fn ref_token(text: &str, example: RangeRef, sheet: &str) -> Option { + let (on, body) = crate::cmd::split_sheet(text.trim()); + if on.is_some_and(|s| !s.eq_ignore_ascii_case(sheet)) { + return None; + } + let t = RangeRef::parse(body)?; + if t.is_full_cols() || t.is_full_rows() { + return None; + } + token(t, example) +} + +/// `t` as a token relative to `r`. +fn token(t: RangeRef, r: RangeRef) -> Option { + if t == r { + return Some("Input".into()); + } + let rel_row = |row: u32| i64::from(row) - i64::from(r.start.row) + 1; + let rel_col = |col: u32| i64::from(col) - i64::from(r.start.col) + 1; + let cols = |a: u32, b: u32| if a == b { format!("col({})", rel_col(a)) } else { format!("cols({}..{})", rel_col(a), rel_col(b)) }; + if r.contains_range(&t) { + let full_rows = t.start.row == r.start.row && t.end.row == r.end.row; + let full_cols = t.start.col == r.start.col && t.end.col == r.end.col; + return Some(if full_rows { + format!("Input.{}", cols(t.start.col, t.end.col)) + } else if full_cols && t.start.row == t.end.row { + format!("Input.row({})", rel_row(t.start.row)) + } else if full_cols && t.end.row == r.end.row { + format!("Input.rows({}..)", rel_row(t.start.row)) + } else if full_cols { + format!("Input.rows({}..{})", rel_row(t.start.row), rel_row(t.end.row)) + } else if t.is_single() { + format!("Input.cell({},{})", rel_row(t.start.row), rel_col(t.start.col)) + } else { + format!("Input.cell({},{}).resize({},{})", rel_row(t.start.row), rel_col(t.start.col), t.height(), t.width()) + }); + } + let near = t.start.row.saturating_add(NEAR_ROWS) >= r.start.row + && t.end.row <= r.end.row.saturating_add(NEAR_ROWS) + && t.start.col.saturating_add(NEAR_COLS) >= r.start.col + && t.end.col <= r.end.col.saturating_add(NEAR_COLS); + if !near { + return None; + } + // Just below the input, in its columns: follows the input's length. + if t.start.row == r.end.row + 1 && t.start.col >= r.start.col && t.end.col <= r.end.col { + let base = if t.start.col == r.start.col && t.end.col == r.end.col { + "Input".to_string() + } else { + format!("Input.{}", cols(t.start.col, t.end.col)) + }; + return Some(if t.height() == 1 { format!("{base}.below") } else { format!("{base}.below({})", t.height()) }); + } + // Just right of the input, in its rows: follows its width. + if t.start.col == r.end.col + 1 && t.start.row >= r.start.row && t.end.row <= r.end.row { + let base = if t.start.row == r.start.row && t.end.row == r.end.row { + "Input".to_string() + } else if t.start.row == t.end.row { + format!("Input.row({})", rel_row(t.start.row)) + } else { + format!("Input.rows({}..{})", rel_row(t.start.row), rel_row(t.end.row)) + }; + return Some(if t.width() == 1 { format!("{base}.right") } else { format!("{base}.right({})", t.width()) }); + } + let cell = format!("Input.cell({},{})", rel_row(t.start.row), rel_col(t.start.col)); + Some(if t.is_single() { cell } else { format!("{cell}.resize({},{})", t.height(), t.width()) }) +} + +/// Replaces references in a formula that are exactly the example input, one of its columns or +/// rows, or a column without its first row, with `{{token}}`. +fn formula_tokens(f: &str, r: RangeRef) -> (String, bool) { + let mut candidates: Vec<(String, String)> = vec![(r.a1(), "Input".into())]; + for (i, col) in (r.start.col..=r.end.col).enumerate() { + let n = i + 1; + candidates.push((RangeRef::new(CellRef::new(r.start.row, col), CellRef::new(r.end.row, col)).a1(), format!("Input.col({n})"))); + if r.height() > 2 { + candidates + .push((RangeRef::new(CellRef::new(r.start.row + 1, col), CellRef::new(r.end.row, col)).a1(), format!("Input.rows(2..).col({n})"))); + } + } + if r.width() > 1 { + for (i, row) in (r.start.row..=r.end.row).enumerate() { + candidates.push((RangeRef::new(CellRef::new(row, r.start.col), CellRef::new(row, r.end.col)).a1(), format!("Input.row({})", i + 1))); + } + } + // Longest first, so `B2:B10` isn't taken for a `B2` cell reference. + candidates.sort_by_key(|(a1, _)| std::cmp::Reverse(a1.len())); + let mut out = String::with_capacity(f.len()); + let mut changed = false; + let (mut i, mut in_text) = (0, false); + let bytes = f.as_bytes(); + while i < f.len() { + let Some(rest) = f.get(i..) else { break }; + let ch = rest.chars().next().unwrap_or(' '); + if ch == '"' { + in_text = !in_text; + } + let prev_ok = i == 0 || !bytes.get(i - 1).is_some_and(|b| b.is_ascii_alphanumeric() || matches!(b, b'$' | b'!' | b'_' | b'.' | b':')); + if !in_text && prev_ok { + let hit = candidates.iter().find(|(a1, _)| { + rest.starts_with(a1.as_str()) + && !rest + .get(a1.len()..) + .and_then(|x| x.chars().next()) + .is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, ':' | '!' | '(' | '_')) + }); + if let Some((a1, tok)) = hit { + out.push_str("{{"); + out.push_str(tok); + out.push_str("}}"); + i += a1.len(); + changed = true; + continue; + } + } + out.push(ch); + i += ch.len_utf8(); + } + (out, changed) +} + +/// The example input's shape: a header when the first row is all text over other kinds of +/// values, and each column's type when all its values agree. +fn infer_input(wb: &Workbook, sheet: usize, r: RangeRef) -> NamedInput { + let mut input = NamedInput::default(); + if r.is_single() { + input.kind = InputKind::Cell; + input.rows = [1, 1]; + input.cols = [1, 1]; + return input; + } + input.cols = [r.width(), r.width()]; + let Some(sh) = wb.sheet(sheet) else { return input }; + if r.count() > 100_000 { + return input; + } + let kind = |c: CellRef| -> Option { + let v = sh.value(c); + Some(match v { + Value::Empty => return None, + Value::Text(t) if t.is_empty() => return None, + Value::Number(_) if crate::display::number_format(wb.styles.get(sh.style_id(c)).num_fmt.as_str()).is_date() => ColType::Date, + Value::Number(_) => ColType::Number, + Value::Text(_) => ColType::Text, + Value::Bool(_) => ColType::Bool, + _ => ColType::Any, + }) + }; + let column = |col: u32, from: u32| -> ColType { + let mut seen: Option = None; + for row in from..=r.end.row { + match (seen, kind(CellRef::new(row, col))) { + (_, None) => {} + (None, k) => seen = k, + (Some(a), Some(b)) if a != b => return ColType::Any, + _ => {} + } + } + seen.unwrap_or(ColType::Any) + }; + let first_text = (r.start.col..=r.end.col).all(|c| kind(CellRef::new(r.start.row, c)) == Some(ColType::Text)); + let below = (r.start.col..=r.end.col).map(|c| column(c, r.start.row + 1)).collect::>(); + input.header = r.height() > 1 && first_text && below.iter().any(|k| !matches!(k, ColType::Text | ColType::Any)); + input.col_types = if input.header { below } else { (r.start.col..=r.end.col).map(|c| column(c, r.start.row)).collect() }; + if input.col_types.iter().all(|k| *k == ColType::Any) { + input.col_types.clear(); + } + input.rows = [1, 0]; + input +} + +#[cfg(test)] +mod tests { + use super::*; + + fn r(a1: &str) -> RangeRef { + RangeRef::parse(a1).unwrap() + } + + #[test] + fn references_near_the_example_become_tokens() { + let ex = r("B2:D11"); + for (t, want) in [ + ("B2:D11", Some("Input")), + ("C2:C11", Some("Input.col(2)")), + ("C2:D11", Some("Input.cols(2..3)")), + ("B2:D2", Some("Input.row(1)")), + ("B3:D11", Some("Input.rows(2..)")), + ("B3:D5", Some("Input.rows(2..4)")), + ("C4", Some("Input.cell(3,2)")), + ("C4:D5", Some("Input.cell(3,2).resize(2,2)")), + ("B12:D12", Some("Input.below")), + ("C12", Some("Input.col(2).below")), + ("C12:C13", Some("Input.col(2).below(2)")), + ("E2:E11", Some("Input.right")), + ("E5", Some("Input.row(4).right")), + ("E3:F4", Some("Input.rows(2..3).right(2)")), + ("G2", Some("Input.cell(1,6)")), + ("A1", Some("Input.cell(0,0)")), + ("Z500", None), + ("A:A", None), + ] { + assert_eq!(ref_token(t, ex, "Sheet1").as_deref(), want, "{t}"); + } + assert_eq!(ref_token("Sheet1!C4", ex, "Sheet1").as_deref(), Some("Input.cell(3,2)")); + assert_eq!(ref_token("Other!C4", ex, "Sheet1"), None); + } + + #[test] + fn formulas_get_tokens_for_exact_references() { + let ex = r("B2:C5"); + for (f, want) in [ + ("=SUM(C3:C5)", "=SUM({{Input.rows(2..).col(2)}})"), + ("=SUM(C2:C5)+COUNT(B2:C5)", "=SUM({{Input.col(2)}})+COUNT({{Input}})"), + ("=SUM($C$3:$C$5)", "=SUM($C$3:$C$5)"), + ("=SUM(Other!C3:C5)", "=SUM(Other!C3:C5)"), + ("=\"C3:C5\"&C3:C5", "=\"C3:C5\"&{{Input.rows(2..).col(2)}}"), + ("=SUM(AC3:C5)", "=SUM(AC3:C5)"), + ("=B2", "=B2"), + ] { + assert_eq!(formula_tokens(f, ex).0, want, "{f}"); + } + } +} diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 84792dc407..2602dc0f07 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -218,6 +218,9 @@ fn names() { assert_eq!(fingerprint(&a), fingerprint(&b)); b.steps[0].note = "changed".into(); assert_ne!(fingerprint(&a), fingerprint(&b)); + assert_eq!(next_name(&wb), "Macro2"); + let paths: Vec<(String, &str)> = command_steps(&a.steps).into_iter().map(|(p, id, _)| (p, id)).collect(); + assert_eq!(paths, [("1".into(), "cell.set"), ("2.1.then.1".into(), "home.bold"), ("3".into(), "home.italic")]); } #[test] diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 207e4c944c..941abe9063 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -752,6 +752,192 @@ fn a_failing_step_ends_the_debugged_run_and_rolls_it_back() { assert_eq!(debug(&mut s, json!({"name": "Empty"}))["state"], "finished"); } +// ---------------------------------------------------------------- commands and the recorder + +#[test] +fn macros_are_saved_listed_renamed_and_deleted() { + let mut s = s(); + s.execute("macro.save", json!({"macro": totals()})).unwrap(); + // The fields can also come at the top level. + let r = s + .execute("macro.save", json!({"name": "Second", "description": "two", "shortcut": "Ctrl+Shift+Q", "steps": [{"command": "home.bold"}]})) + .unwrap(); + assert_eq!(r, json!({"name": "Second", "steps": 1})); + let list = s.execute("macro.list", json!({})).unwrap(); + assert_eq!(list[0], json!({"name": "Totals", "description": "", "steps": 3, "inputs": ["Input"], "onConflict": "ask", "permissions": []})); + assert_eq!(list[1]["shortcut"], "Ctrl+Shift+Q"); + let got = s.execute("macro.get", json!({"name": "totals"})).unwrap(); + assert_eq!(crate::macros::parse_macro(&got).unwrap(), crate::macros::parse_macro(&totals()).unwrap()); + assert!(s.execute("macro.save", json!({"macro": totals(), "replace": false})).unwrap_err().to_string().contains("already exists")); + assert!(s.execute("macro.save", json!({"macro": {"name": "Bad", "steps": [{"type": "warp"}]}})).is_err()); + assert!(s.execute("macro.rename", json!({"name": "Second", "to": "TOTALS"})).unwrap_err().to_string().contains("already exists")); + s.execute("macro.rename", json!({"name": "Second", "to": "Third"})).unwrap(); + s.execute("macro.rename", json!({"name": "third", "to": "THIRD"})).unwrap(); + s.execute("macro.delete", json!({"name": "Totals"})).unwrap(); + let names: Vec = + s.execute("macro.list", json!({})).unwrap().as_array().unwrap().iter().map(|m| m["name"].as_str().unwrap().to_string()).collect(); + assert_eq!(names, ["THIRD"]); + assert!(s.execute("macro.delete", json!({"name": "Totals"})).unwrap_err().to_string().contains("no macro called")); + // Saving, renaming and deleting are undoable. + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.execute("macro.list", json!({})).unwrap().as_array().unwrap().len(), 2); + // Macros are kept apart from Automate scripts: View › Macros still lists the scripts. + assert_eq!(s.execute("view.macros", json!({})).unwrap(), json!([])); + assert!(s.execute("macro.rename", json!({"name": "THIRD", "to": "a\nb"})).is_err()); +} + +#[test] +fn macros_and_automate_scripts_live_side_by_side() { + let mut s = s(); + let steps = json!([{"command": "cell.set", "params": {"cell": "B1", "input": "from script"}}]); + s.execute("automate.newScript", json!({"name": "Inner", "commands": steps})).unwrap(); + s.execute("macro.save", json!({"name": "Inner", "steps": [{"command": "cell.set", "params": {"cell": "C1", "input": "from macro"}}], "output": {"onConflict": "overwrite"}})).unwrap(); + // Recording a script run keeps the script's steps, so the macro stands on its own. + s.execute("macro.record", json!({"relative": false})).unwrap(); + set(&mut s, "A1", "typed"); + s.execute("automate.runScript", json!({"name": "Inner"})).unwrap(); + s.execute("automate.recordActions", json!({})).unwrap(); + s.execute("automate.recordActions", json!({"stop": true, "name": "Empty"})).unwrap(); + let r = s.execute("macro.record", json!({"stop": true, "name": "Recorded"})).unwrap(); + assert_eq!(r["skipped"], json!(["automate.recordActions", "automate.recordActions"])); + let m = crate::macros::parse_macro(&s.execute("macro.get", json!({"name": "Recorded"})).unwrap()).unwrap(); + let ids: Vec = crate::macros::command_steps(&m.steps).iter().map(|(_, id, p)| format!("{id} {p}")).collect(); + assert_eq!(ids, ["cell.set {\"cell\":\"A1\",\"input\":\"typed\"}", "cell.set {\"cell\":\"B1\",\"input\":\"from script\"}"]); + // Both lists keep their own entries, under the same name if need be. + let scripts = s.execute("automate.allScripts", json!({})).unwrap(); + assert_eq!(scripts, json!([{"name": "Empty", "steps": 0}, {"name": "Inner", "steps": 1}])); + let macros: Vec = s.execute("macro.list", json!({})).unwrap().as_array().unwrap().iter().map(|m| m["name"].clone()).collect(); + assert_eq!(macros, [json!("Inner"), json!("Recorded")]); + s.execute("home.insertSheet", json!({})).unwrap(); + s.execute("automate.runScript", json!({"name": "Inner"})).unwrap(); + s.execute("macro.run", json!({"name": "Inner"})).unwrap(); + assert_eq!((v(&s, "B1").display(), v(&s, "C1").display()), ("from script".into(), "from macro".into())); + // Both survive an xlsx round trip unchanged. + let b64 = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap()["base64"].as_str().unwrap().to_string(); + s.execute("file.open", json!({"name": "both.xlsx", "base64": b64})).unwrap(); + assert_eq!(s.execute("automate.allScripts", json!({})).unwrap(), scripts); + assert_eq!(s.execute("macro.list", json!({})).unwrap().as_array().unwrap().len(), 2); +} + +#[test] +fn a_relative_recording_plays_back_on_another_block() { + let mut s = s(); + data(&mut s); + put( + &mut s, + json!({"name": "Plain", "steps": [{"command": "cell.set", "params": {"cell": "A20", "input": "plain"}}], "output": {"onConflict": "overwrite"}}), + ); + put(&mut s, json!({"name": "Shaped", "inputs": [{"name": "Input"}], "steps": []})); + s.execute("selection.set", json!({"range": "B2:C5"})).unwrap(); + let r = s.execute("macro.record", json!({})).unwrap(); + assert_eq!(r, json!({"recording": true, "relative": true, "input": "B2:C5"})); + // Clicking a cell and typing, a formula over the amounts, bold header, italic on a selection. + s.execute("selection.set", json!({"cell": "E2"})).unwrap(); + set(&mut s, "E2", "Total"); + set(&mut s, "E3", "=SUM(C3:C5)"); + s.execute("home.bold", json!({"range": "B2:C2"})).unwrap(); + s.execute("selection.set", json!({"cell": "C3"})).unwrap(); + s.execute("selection.set", json!({"cell": "C4"})).unwrap(); + s.execute("home.italic", json!({})).unwrap(); + set(&mut s, "C6", "end"); + // Not kept: commands macros don't run, and a macro with inputs; a plain macro's steps are + // inlined. + s.execute("window.activate", json!({"index": 0})).unwrap(); + s.execute("macro.run", json!({"name": "Plain"})).unwrap(); + s.execute("macro.run", json!({"name": "Shaped", "inputs": "A1"})).unwrap(); + // The journal can be trimmed meanwhile. + s.journal.clear(); + let r = s.execute("macro.record", json!({"stop": true, "name": "Sum it", "description": "made by recording"})).unwrap(); + assert!(!s.is_recording()); + assert_eq!(r["saved"], "Sum it"); + assert_eq!(r["skipped"], json!(["window.activate", "macro.run"])); + assert_eq!(r["inputs"], json!(["Input"])); + let m = crate::macros::parse_macro(&s.execute("macro.get", json!({"name": "Sum it"})).unwrap()).unwrap(); + assert_eq!(m.description, "made by recording"); + let input = &m.inputs[0]; + assert!(input.header); + assert_eq!((input.cols, input.col_types.clone()), ([2, 2], vec![gridcraft_model::ColType::Text, gridcraft_model::ColType::Number])); + let steps: Vec<(String, Json)> = m + .steps + .iter() + .map(|st| match &st.kind { + gridcraft_model::StepKind::Command { id, params } => (id.clone(), params.clone()), + other => panic!("{other:?}"), + }) + .collect(); + assert_eq!( + steps, + vec![ + ("selection.set".to_string(), json!({"cell": "Input.cell(1,4)"})), + ("cell.set".into(), json!({"cell": "Input.cell(1,4)", "input": "Total"})), + ("cell.set".into(), json!({"cell": "Input.cell(2,4)", "input": "=SUM({{Input.rows(2..).col(2)}})"})), + ("home.bold".into(), json!({"range": "Input.row(1)", "on": true})), + ("selection.set".into(), json!({"cell": "Input.cell(3,2)"})), + ("home.italic".into(), json!({"on": true})), + ("cell.set".into(), json!({"cell": "Input.col(2).below", "input": "end"})), + ("cell.set".into(), json!({"cell": "A20", "input": "plain"})), + ] + ); + // Play it back on a longer block somewhere else. + for (cell, input) in [ + ("G10", "Who"), + ("H10", "Paid"), + ("G11", "A"), + ("H11", "1"), + ("G12", "B"), + ("H12", "2"), + ("G13", "C"), + ("H13", "3"), + ("G14", "D"), + ("H14", "4"), + ] { + set(&mut s, cell, input); + } + s.execute("macro.run", json!({"name": "Sum it", "inputs": "G10:H14", "onConflict": "overwrite"})).unwrap(); + assert_eq!((v(&s, "J10").display(), v(&s, "J11").display()), ("Total".into(), "10".into())); + assert!(bold(&s, "G10") && bold(&s, "H10")); + assert_eq!(v(&s, "H15").display(), "end"); + // Toggles were recorded with the state they set: a second run keeps the header bold. + s.execute("macro.run", json!({"name": "Sum it", "inputs": "G10:H14", "onConflict": "overwrite"})).unwrap(); + assert!(bold(&s, "G10") && bold(&s, "H10")); + let d = s.doc().unwrap(); + assert!(d.wb.styles.get(d.wb.active().unwrap().style_id(gridcraft_core::CellRef::parse("H12").unwrap())).font.italic); + // The input is checked like any other: text in the amounts is refused. + set(&mut s, "H12", "two"); + let err = s.execute("macro.run", json!({"name": "Sum it", "inputs": "G10:H14"})).unwrap_err().to_string(); + assert!(err.contains("Input column 2 (Paid) must hold numbers; H12 holds text."), "{err}"); +} + +#[test] +fn an_absolute_recording_keeps_its_references() { + let mut s = s(); + s.execute("selection.set", json!({"range": "B2:C3"})).unwrap(); + s.execute("macro.record", json!({"relative": false})).unwrap(); + s.execute("home.insertSheet", json!({"name": "Two"})).unwrap(); + set(&mut s, "B2", "x"); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + set(&mut s, "C3", "y"); + let r = s.execute("macro.record", json!({})).unwrap(); + assert_eq!(r["saved"], "Macro1"); + let m = crate::macros::parse_macro(&s.execute("macro.get", json!({"name": "Macro1"})).unwrap()).unwrap(); + assert!(m.inputs.is_empty()); + let ids: Vec = crate::macros::command_steps(&m.steps).iter().map(|(_, id, p)| format!("{id} {p}")).collect(); + assert_eq!( + ids, + [ + "home.insertSheet {\"name\":\"Two\"}", + "sheet.activate {\"sheet\":\"Two\"}", + "selection.set {\"active\":\"A1\",\"range\":\"A1\"}", + "cell.set {\"cell\":\"B2\",\"input\":\"x\"}", + "sheet.activate {\"sheet\":\"Sheet1\"}", + "selection.set {\"active\":\"A1\",\"range\":\"A1\"}", + "cell.set {\"cell\":\"C3\",\"input\":\"y\"}", + ] + ); + // Stopping when nothing records says so. + assert_eq!(s.execute("macro.record", json!({"stop": true})).unwrap(), json!({"recording": false})); +} + #[test] fn only_the_workbooks_a_debugged_run_touched_wait() { let mut s = s(); diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index d218c8169d..dec0cf5572 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -4,7 +4,7 @@ Each entry is an Excel ribbon or menu command (feature names only). It counts as implemented when GridCraft registers a command with the same id. This measures breadth only; the overall assessment (depth, fidelity, effort) is in [target-app-parity.md](target-app-parity.md). -**Overall: 274 / 290 commands implemented (94.5%).** +**Overall: 275 / 291 commands implemented (94.5%).** | Tab | Implemented | Total | Parity | |---|---|---|---| @@ -16,7 +16,7 @@ Each entry is an Excel ribbon or menu command (feature names only). It counts as | Formulas | 23 | 23 | 100% | | Data | 23 | 28 | 82% | | Review | 15 | 18 | 83% | -| View | 20 | 20 | 100% | +| View | 21 | 21 | 100% | | Automate | 3 | 3 | 100% | | Table Design | 13 | 13 | 100% | | Chart Design | 8 | 8 | 100% | @@ -299,7 +299,7 @@ Missing: - `review.translate` Translate (Language) - `review.showChanges` Show Changes (Changes) -## View (20 / 20, 100%) +## View (21 / 21, 100%) Implemented: @@ -323,6 +323,7 @@ Implemented: - `view.unhideWindow` Unhide (Window) - `view.switchWindows` Switch Windows (Window) - `view.macros` Macros (Macros) +- `macro.record` Record Macro (Macros) ## Automate (3 / 3, 100%) From 7011395fcf06734110dee5d86a06325fa0665e08 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:21:33 -0400 Subject: [PATCH 035/687] Engine: a macro renamed on a name clash keeps its name within the length limit When a file held two macros with the same name, the second was renamed "name (2)" without room for the suffix. Two macros with the same 255-character name both opened, but the second name grew to 259 characters, was saved that way, and the next open dropped that macro as over the limits. unique_name now shortens the name to make room for the suffix, and load checks the macro again after renaming it. --- crates/engine/src/macros/mod.rs | 27 +++++++++++++++++++-------- crates/engine/src/macros/tests.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 6b3ccdbd88..68db8e249c 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -128,12 +128,14 @@ pub fn load(wb: &mut Workbook) -> Vec { bad += 1; continue; } - match m.check_limits().and_then(|()| check_size(&m)) { - Ok(()) => { - let mut m = m; - m.name = unique_name(wb, m.name.trim()); - wb.macros.push(m); - } + let mut m = m; + let checked = m.check_limits().and_then(|()| { + m.name = unique_name(wb, m.name.trim()); + // The macro as it is kept, renamed, must still pass, or the next save would lose it. + m.check_limits().and_then(|()| check_size(&m)) + }); + match checked { + Ok(()) => wb.macros.push(m), Err(_) => bad += 1, } } @@ -150,13 +152,22 @@ pub fn load(wb: &mut Workbook) -> Vec { warnings } -/// `name`, or `name (2)`, `name (3)`… if a macro already has it. +/// `name`, or `name (2)`, `name (3)`… if a macro already has it. A long name is shortened to make +/// room for the suffix, so the result stays within [`gridcraft_model::MAX_MACRO_NAME`] characters. pub fn unique_name(wb: &Workbook, name: &str) -> String { let name = if name.is_empty() { "Macro" } else { name }; if find(wb, name).is_none() { return name.to_string(); } - (2..).map(|n| format!("{name} ({n})")).find(|n| find(wb, n).is_none()).unwrap_or_else(|| name.to_string()) + (2..) + .map(|n| { + let suffix = format!(" ({n})"); + let keep = gridcraft_model::MAX_MACRO_NAME.saturating_sub(suffix.chars().count()); + let base: String = name.chars().take(keep).collect(); + format!("{}{suffix}", base.trim_end()) + }) + .find(|n| find(wb, n).is_none()) + .unwrap_or_else(|| name.to_string()) } #[cfg(test)] diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index fea61f6c35..e67479eabb 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -190,6 +190,13 @@ fn names() { assert_eq!(unique_name(&wb, "macro1"), "macro1 (2)"); assert_eq!(unique_name(&wb, "Other"), "Other"); assert_eq!(unique_name(&wb, ""), "Macro"); + // A long name is shortened to make room for the suffix. + let long = "y".repeat(MAX_MACRO_NAME); + wb.macros.push(Macro { name: long.clone(), ..Macro::default() }); + let renamed = unique_name(&wb, &long); + assert_eq!(renamed, format!("{} (2)", "y".repeat(MAX_MACRO_NAME - 4))); + wb.macros.push(Macro { name: renamed, ..Macro::default() }); + assert_eq!(unique_name(&wb, &long).chars().count(), MAX_MACRO_NAME); assert_eq!(find(&wb, " MACRO1 "), Some(0)); assert_eq!(find(&wb, "Macro2"), None); } @@ -206,3 +213,24 @@ fn json_field_macros_get_the_name_check_too() { assert_eq!(names, ["Fine"]); assert!(warnings.len() == 1 && warnings[0].starts_with("1 macro "), "{warnings:?}"); } + +#[test] +fn duplicate_names_at_the_length_limit_survive_save_and_reopen() { + // Two macros with the same 255-character name: the second is renamed within the limit, so a + // save and a second open keep both. + let long = "é".repeat(MAX_MACRO_NAME); + for file in ["dup.json", "dup.xlsx"] { + let mut wb = Workbook::new(); + wb.macros.push(Macro { name: long.clone(), ..Macro::default() }); + wb.macros.push(Macro { name: long.clone(), description: "second".into(), ..Macro::default() }); + let (first, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&wb, file).unwrap()).unwrap(); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + assert_eq!(first.macros.len(), 2, "{file}"); + let renamed = &first.macros[1].name; + assert_eq!(renamed.chars().count(), MAX_MACRO_NAME, "{file}"); + assert!(renamed.ends_with(" (2)") && renamed.starts_with("éé"), "{file}: {renamed}"); + let (again, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&first, file).unwrap()).unwrap(); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + assert_eq!(again.macros, first.macros, "{file}"); + } +} From d305566e57165f77abb8f925190fcaa0b797bcb2 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 14:09:24 -0400 Subject: [PATCH 036/687] Docs: running and checking macros --- docs/macros.md | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index dd14951bc0..424dd4f396 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -1,13 +1,15 @@ # Macros -> **Last updated:** 2026-10-21 · **Change:** minor (macro tokens) +> **Last updated:** 2026-10-21 · **Change:** minor (running and checking macros: `macro.run`, `macro.check`) GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro -declares the inputs it works on. +declares the inputs it works on, runs as one undo step, rolls everything back on an error, checks +where its results will land before it writes them, and does not touch files without permission. +Agents run macros with `macro.run` like any other command. -This page describes the macro format, the tokens its steps use and how macros are saved in a -workbook. The engine has no command that runs macros in this version. +This page describes the macro format, its tokens, and running and checking macros. The engine has +no macro debugger, recorder or management commands in this version, and no macro interface. ## A macro @@ -67,6 +69,30 @@ any string, such as a formula. Plain A1 references stay absolute; a `Name:` anch inserted rows. A reference with a sheet name is never a token, so `Output!A1` is a cell on a sheet called Output, even when a token or input has that name. +## Running + +`macro.run {name, inputs?, onConflict?, overwrite?, approve?, timeLimit?}`: + +1. **Policy.** File, app, window, automate and macro commands, and Undo and Redo, never run in a + macro. Reading or writing a file (`data.getData`, `data.fromTextCsv`, `insert.picture` or + `pageLayout.background` with a `path`, or an export or print with a `path`) needs the command + in the macro's `permissions`. Any `path` other than `null` counts, including a blank one and a + token, and the policy is checked again on the parameters as they are when the step runs. A macro that touches files, or that came with a workbook opened + from a file, runs once the person approves it: the call fails with `approve: …` and runs with + `approve: true`. Approval lasts for the session, until the macro changes. +2. **Footprint.** The macro first runs in dry-run mode on the real workbook, and everything is put + back. Cells it would write outside its inputs that hold data, and sheets and objects it would + delete, are conflicts, handled by `onConflict` (or the macro's `output.onConflict`): `ask` (the + call fails with `confirm: …`; `overwrite: true` answers it), `fail`, `overwrite`, `shiftDown` or + `shiftRight` (Insert Cells makes room, then the footprint is checked again). Steps that touch + files, and Solver, end the dry run early; the footprint then says it is partial. +3. **Run.** One undo step, "Run Macro: Totals". Any error rolls the whole run back and names the + step: `Step 3.1 (cell.set): …` (paths are 1-based; `2.1` is inside a Repeat, `4.then.1` inside + an If). + +`macro.check {name, inputs?}` reports the bound inputs, the output anchor, any approval needed and +the footprint, without changing anything. + ## Storage Macros are saved in the workbook: as a field in GridCraft's JSON format, and in xlsx as hidden From 749558f978e00b6ca49ac8dc5f69de30b76b2f04 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:34:26 -0400 Subject: [PATCH 037/687] Docs: recording macros and the management commands; roadmap, architecture and parity --- ROADMAP.md | 6 ++++-- docs/architecture.md | 8 +++++++- docs/macros.md | 22 ++++++++++++++++++---- docs/target-app-parity.md | 5 +++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..2c0ee33cd0 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (macro engine and `macro.*` commands) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -85,7 +85,7 @@ collaboration, mobile, AI). | Get & Transform, external data, data model | ~15% | ~10% | 60–100 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) | | Insert objects (pictures, shapes, ink, SmartArt, equations) | ~55% | ~45% | 25–40 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) | | Review, comments, protection, collaboration | ~55% | ~40% | 35–55 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) | -| Automation (recorded scripts; no VBA) | ~30% | ~20% | 12–20 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) | +| Automation (recorded scripts; typed macros in the engine, no interface yet; no VBA) | ~30% | ~20% | 12–20 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) | ## Languages @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-21 | Macro engine ([macros.md](docs/macros.md)): typed, declarative macros saved in the workbook (a JSON field; hidden names in xlsx); declared inputs checked with messages that name the cells; tokens (`Input.col(2)`, `Row`, `Output`, `Name:Total`, `{{…}}` in formulas); one undo step with full rollback; a footprint dry run with ask, fail, overwrite, shift down or shift right; a policy that keeps macros away from files and the app, with approval for macros in opened files; Repeat, If and Stop steps; a debugger; a relative recorder; `macro.*` commands beside the unchanged Automate scripts | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | @@ -193,6 +194,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-21 | minor | Macro engine and `macro.*` commands; catalog 274 / 290 → 275 / 291 (View › Macros › Record Macro); progress log row | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/docs/architecture.md b/docs/architecture.md index d815bc81c5..fa38b52ed7 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # Architecture -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (engine building blocks for multi-command actions) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (macro engine on the engine building blocks) · **Target:** Microsoft Excel (Microsoft 365) How GridCraft is built today. Rules for contributors are in [`AGENTS.md`](../AGENTS.md). @@ -66,6 +66,11 @@ Actions made of many commands (macros, a debugger that runs one step per call) u 8,192-character limits, so it survives an xlsx round trip. The older single-name `_sc_` items are read as well. +The macro engine (`engine/src/macros/`, commands in `cmd/macros.rs`, described in +[`macros.md`](macros.md)) is built on these: a run is an undo group, the recorder reads the +recording buffer, the policy asks before a macro in a `FromFile` workbook runs, and xlsx keeps +macros as chunked hidden names (`_gc_macro_`). + ## Agent control - Desktop control channel: `gridcraft --control 7979`, JSON lines on localhost @@ -91,5 +96,6 @@ rpm, tarball), FreeBSD, and web (WASM; WebGPU or WebGL2). Release workflows in | Date | Change | Summary | |---|---|---| +| 2026-10-21 | minor | Macro engine on the building blocks (`macros/`, `cmd/macros.rs`) | | 2026-10-11 | minor | Engine building blocks for multi-command actions: undo groups, recording buffer, document trust, chunked hidden-name storage | | 2026-10-10 | major | Created from the code on main: crates, layers, data flow, agent control, known gaps | diff --git a/docs/macros.md b/docs/macros.md index 4301989ae7..79e6c02c56 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -1,6 +1,6 @@ # Macros -> **Last updated:** 2026-10-21 · **Change:** minor (debugging macros: `macro.debug`) +> **Last updated:** 2026-10-21 · **Change:** minor (recording macros and the management commands) GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro @@ -9,8 +9,9 @@ error (a file it already wrote stays), checks where its results will land before and does not touch files without permission. Agents run macros with `macro.run` like any other command. -This page describes the macro format, its tokens, and running, checking and debugging macros. -The engine has no macro recorder or management commands in this version, and no macro interface. +This page describes the engine and its commands. The macro interface (a Macros list, a recorder +button and a step editor) is not part of it yet. + ## A macro @@ -115,7 +116,20 @@ command makes them (stopping Record Actions saves a script), and other workbooks editable. Each answer gives the next step with its parameters as they would run, the parameters of the step that just ran, and the cells changed so far. -## Storage +## Recording + +`macro.record` toggles recording (`relative: false` for absolute references; `name` and +`description` when stopping). With relative references the selection when recording starts is the +example input: references in and near it become tokens, and the input's shape, header and column +types are inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so +playing them back sets them rather than flipping them. Commands a macro may not run are left out +and listed in the result; a macro run while recording is replaced by its steps when it has no +inputs, and an Automate script run by the script's steps. + +## Other commands and storage + +`macro.list`, `macro.get`, `macro.save {macro, replace?}`, `macro.delete` and `macro.rename +{name, to}`. Saving, renaming and deleting are undoable. Macros are saved in the workbook: as a field in GridCraft's JSON format, and in xlsx as hidden defined names (`_gc_macro__`, the macro's JSON split into string literals within Excel's diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index b623124d9f..de57d10d8a 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1,6 +1,6 @@ # Target-app parity: GridCraft vs Microsoft Excel -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and row formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (automation: typed macros in the engine) · **Target:** Microsoft Excel (Microsoft 365) This is the authoritative parity assessment. [`ROADMAP.md`](../ROADMAP.md) summarizes it, [`gaps.md`](gaps.md) lists every shortfall, and the area checklists hold the detail: @@ -91,7 +91,7 @@ performance 8, stability 8, localization 4, hardware 2, platforms 2, ecosystem 2 | Get & Transform, external data, data model | 5 | ~15% | ~10% | 60–100 | Import of CSV/TSV, JSON, HTML. No Power Query editor, no From Web, database or folder sources, no connections or refresh, no relationships or data model, no external workbook links (dropped with a warning) | | Insert objects | 3 | ~55% | ~45% | 25–40 | Pictures, 8 shape kinds (Excel has ~170), text boxes, own icon set, ink with Ink to Shape, hyperlinks, checkboxes. Missing: SmartArt, WordArt, equations, signature line, objects, 3-D models, screenshot, pictures in cells (PR #105) | | Review, comments, protection, collaboration | 4 | ~55% | ~40% | 35–55 | Threaded comments, notes, spelling (system dictionary), thesaurus, sheet and workbook protection, Allow Edit Ranges with Excel-compatible password hashes (#307). Missing: comment navigation, Show Changes, notes-to-comments conversion, accessibility checker, co-authoring, opening password-encrypted files (refused with a message since #17) | -| Automation | 4 | ~30% | ~20% | 12–20 | Recorded, editable GridCraft scripts (Automate tab). VBA is out of scope by owner decision and is **dropped** when an `.xlsm` is opened (warning shown). No Office Scripts compatibility | +| Automation | 4 | ~30% | ~20% | 12–20 | Recorded, editable GridCraft scripts (Automate tab). Typed macros in the engine (`macro.*`: recorder, runner with input checks and a footprint dry run, debugger; [macros.md](macros.md)), with no interface yet. VBA is out of scope by owner decision and is **dropped** when an `.xlsm` is opened (warning shown). No Office Scripts compatibility | | **Weighted** | 100 | **~75%** | **~59%** | **300–485** | | ## Dimensions @@ -332,6 +332,7 @@ The inventory carried over from the 2026-10-07 ROADMAP.md, updated for what land | Date | Change | Summary | |---|---|---| +| 2026-10-21 | minor | Automation: typed macros in the engine and `macro.*` commands, no interface yet (no score change) | | 2026-10-12 | minor | Essentials AutoFit evidence: rows auto-fit oversized fonts on open and row formatting (#833) | | 2026-10-12 | minor | Sheet tabs: reveal the selected tab on navigation and resize while preserving manual scrolling (#681); no score change | | 2026-10-12 | minor | PivotTables: values on rows (Σ Values in Rows at any position, all three layouts; `pivot.moveField` and the field pane move it; `dataOnRows` round-trips) (no score change) | From 4966b9c86db8b9e8adbae2158fdb6e8a5474bc6e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:22:38 -0400 Subject: [PATCH 038/687] Engine: opening a file reads its hidden macro names in one bounded pass load listed every _gc_macro_ item, then read each one with a scan of all the workbook's names, and removed each with another full scan. Damaged items didn't count toward the macro limit, so a file with 20,000 damaged items took 20,000 scans of 20,000 names (seconds, and far more for a larger file). The names are now taken out of the workbook and grouped by item in one pass; only the first 1,000 items are read, and the rest are counted in the warning without being read. --- crates/engine/src/macros/mod.rs | 43 ++++++++++++++++++++----------- crates/engine/src/macros/tests.rs | 20 ++++++++++++++ 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 68db8e249c..155b9182f1 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -7,7 +7,9 @@ //! the open document. Scripts made with Automate › New Script or Record Actions are a separate, //! older feature with their own storage; macros neither read nor change them. -use gridcraft_model::{Macro, Workbook}; +use std::collections::BTreeMap; + +use gridcraft_model::{DefinedName, Macro, Workbook}; use serde_json::{Value as Json, json}; use crate::cmd::stash; @@ -101,25 +103,29 @@ pub fn with_names(wb: &Workbook) -> crate::Result> { pub fn load(wb: &mut Workbook) -> Vec { let mut warnings = Vec::new(); let mut bad = 0usize; - // Stored macros, in their saved order. - let mut keyed: Vec<(u64, String)> = - stash::keys(wb, MACRO_KEY).into_iter().filter_map(|k| k.strip_prefix(MACRO_KEY).and_then(|n| n.parse().ok()).map(|n| (n, k))).collect(); - keyed.sort(); - let mut found = Vec::new(); - for (_, key) in &keyed { - // Past the macro limit, the rest are counted without being read. - if found.len() >= gridcraft_model::MAX_MACROS { - bad += 1; - continue; + // One pass takes the hidden macro names out of the workbook, grouped by macro in saved order. + let mut stored: BTreeMap<(u64, String), Vec> = BTreeMap::new(); + let mut kept = Vec::new(); + for n in std::mem::take(&mut wb.names) { + match macro_item(&n) { + None => kept.push(n), + Some((key, Some(i))) => stored.entry((i, key)).or_default().push(n), + // A `macro_` item that isn't numbered isn't one this build wrote; it goes unread. + Some((_, None)) => {} } - match stash::get_json(wb, key).ok_or_else(|| "unreadable".to_string()).and_then(|v| parse_macro(&v)) { + } + wb.names = kept; + // Past the macro limit, the rest are counted without being read. + bad = bad.saturating_add(stored.len().saturating_sub(gridcraft_model::MAX_MACROS)); + let mut found = Vec::new(); + let mut item = Workbook::new(); + for ((_, key), names) in stored.into_iter().take(gridcraft_model::MAX_MACROS) { + item.names = names; + match stash::get_json(&item, &key).ok_or_else(|| "unreadable".to_string()).and_then(|v| parse_macro(&v)) { Ok(m) => found.push(m), Err(_) => bad += 1, } } - for key in stash::keys(wb, MACRO_KEY) { - stash::remove(wb, &key); - } // Macros that came as a field (the JSON format) go through the same checks. let mut all = std::mem::take(&mut wb.macros); all.extend(found); @@ -152,6 +158,13 @@ pub fn load(wb: &mut Workbook) -> Vec { warnings } +/// The item key (`macro_`, lower-cased) of a hidden macro name, and `n` when it is a number. +fn macro_item(n: &DefinedName) -> Option<(String, Option)> { + let key = stash::item_key(&n.name).filter(|k| n.scope.is_none() && k.starts_with(MACRO_KEY))?; + let i = key.strip_prefix(MACRO_KEY).and_then(|i| i.parse().ok()); + Some((key, i)) +} + /// `name`, or `name (2)`, `name (3)`… if a macro already has it. A long name is shortened to make /// room for the suffix, so the result stays within [`gridcraft_model::MAX_MACRO_NAME`] characters. pub fn unique_name(wb: &Workbook, name: &str) -> String { diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index e67479eabb..189bf752ef 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -234,3 +234,23 @@ fn duplicate_names_at_the_length_limit_survive_save_and_reopen() { assert_eq!(again.macros, first.macros, "{file}"); } } + +#[test] +#[allow(clippy::disallowed_methods)] // a native-only timing test: the clock is read off wasm +fn many_hidden_macro_names_are_read_in_one_pass() { + // A hostile file with many damaged macro items: only the first MAX_MACROS are read, and the + // names are gathered and removed in one pass instead of one scan of every name per item. + let mut wb = Workbook::new(); + let items = 20_000; + for i in 0..items { + wb.names.push(hidden(&format!("_gc_macro_{i}_0"), "{not json")); + } + wb.names.push(hidden("_gc_other_0", "kept")); + let start = std::time::Instant::now(); + let warnings = load(&mut wb); + let took = start.elapsed(); + assert!(wb.macros.is_empty()); + assert_eq!(names_of(&wb), ["_gc_other_0"]); + assert!(warnings.len() == 1 && warnings[0].starts_with(&format!("{items} macros")), "{warnings:?}"); + assert!(took < std::time::Duration::from_secs(3), "{took:?}"); +} From 3bc1d91636c15e5dce4897c0ea663360e672007d Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:09:52 -0400 Subject: [PATCH 039/687] Engine: macro tokens, resolved to A1 references when a step runs Step parameters can name where they act through tokens that are worked out when the step runs, so a macro made on one block of data works on another: a declared input (Input, or any other input's name), Output (the output rule's anchor), Row or Column (the current item of the Repeat step around the step) and Name:Total (a defined name or a table's data). Methods narrow or move the range: col(n) or col("Header"), row(n), cols(a..b), rows(2..), cell(r,c) relative to the top-left cell, offset(r,c), resize(h,w), data (without the header row), below(n) and right(n). Methods chain. A token is written as {"$ref": "..."} anywhere, as the whole string of a reference parameter (cell, range, target, keys ending in Cell or Range and similar), or as {{...}} inside any string, which is how a formula refers to its input. Other text parameters are never read as tokens, so a cell can still hold the word Row and a chart can still be of type column. Every token becomes A1 text, sheet-qualified when it is on another sheet, before the command runs, so no existing command changes. Plain A1 references in a step stay absolute. A reference that runs past the edge of the sheet is reported apart from other mistakes, so the footprint check can tell the two apart. --- crates/engine/src/macros/mod.rs | 2 + crates/engine/src/macros/resolve.rs | 650 ++++++++++++++++++++++++++++ 2 files changed, 652 insertions(+) create mode 100644 crates/engine/src/macros/resolve.rs diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 38016049d2..fee5392018 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -14,6 +14,8 @@ use serde_json::{Value as Json, json}; use crate::cmd::stash; +pub mod resolve; + /// The hidden-data key prefix of a stored macro (`macro_`). const MACRO_KEY: &str = "macro_"; diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs new file mode 100644 index 0000000000..8abf68ad4b --- /dev/null +++ b/crates/engine/src/macros/resolve.rs @@ -0,0 +1,650 @@ +//! Macro tokens: references in step parameters that are worked out when the step runs, so a +//! macro made on one block of data works on another. +//! +//! A token is a base followed by methods: +//! +//! - bases: a declared input (`Input`, `Rates`), `Output` (the output rule's anchor), `Row` or +//! `Column` (the current item of the Repeat step around it), and `Name:Total` (a defined name or +//! a table's data); +//! - methods, 1-based and chainable: `.col(n)` or `.col("Amount")` (by header), `.row(n)`, +//! `.cols(a..b)`, `.rows(a..)`, `.cell(r,c)` (relative to the top-left cell; any whole numbers, +//! so `cell(0,1)` is the cell above), `.offset(r,c)`, `.resize(h,w)`, `.data` (without the +//! header row), `.below(n)` and `.right(n)` (the n rows below or columns right of the range). +//! +//! In parameters a token is written three ways: `{"$ref": "Input.col(2)"}` anywhere; the whole +//! string `"Input.col(2)"` in a reference parameter (`cell`, `range`, `target`, `to`, keys ending +//! in `Cell` or `Range`…); or `{{Input.col(2)}}` inside any string (`"=SUM({{Input.col(2)}})"`). +//! Each becomes A1 text before the command runs, so commands never see a token. Plain A1 +//! references in steps are absolute: they don't follow row inserts (as in VBA); a `Name:` anchor +//! does. + +use gridcraft_core::{CellRef, MAX_COLS, MAX_ROWS, RangeRef}; +use gridcraft_model::{RepeatOf, Workbook}; +use serde_json::{Map, Value as Json}; + +/// A resolved reference: a block of cells on a sheet. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Place { + pub sheet: usize, + pub range: RangeRef, +} + +/// Why a reference couldn't be worked out. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ResolveError { + /// It runs past the edge of the sheet. + Edge(String), + /// Anything else (an unknown input, a malformed token). + Bad(String), +} + +impl ResolveError { + pub fn message(&self) -> &str { + match self { + ResolveError::Edge(m) | ResolveError::Bad(m) => m, + } + } +} + +impl std::fmt::Display for ResolveError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.message()) + } +} + +type R = Result; + +fn bad(msg: impl Into) -> R { + Err(ResolveError::Bad(msg.into())) +} + +/// What tokens refer to while a step runs. +#[derive(Clone, Copy, Debug)] +pub struct Env<'a> { + pub wb: &'a Workbook, + /// The sheet a reference without a sheet name is on. + pub active_sheet: usize, + /// Declared inputs, with whether each has a header row. + pub inputs: &'a [(String, Place, bool)], + pub output: Option, + /// The innermost Repeat step's current row or column. + pub item: Option<(RepeatOf, Place)>, +} + +/// Parameter keys that hold references, where a whole-string token is resolved. Elsewhere a +/// string is text (a cell may hold "Row", a chart's type is "column"), and a token needs +/// `{"$ref": …}` or `{{…}}`. +const REF_KEYS: &[&str] = &[ + "active", + "anchor", + "area", + "areas", + "at", + "cell", + "cells", + "changing", + "copyTo", + "destination", + "from", + "fromCell", + "location", + "objective", + "over", + "range", + "ranges", + "reference", + "resultCells", + "source", + "sources", + "target", + "to", + "variables", +]; + +/// Whether a parameter called `key` holds a reference. +pub fn is_ref_key(key: &str) -> bool { + let k = key.to_ascii_lowercase(); + REF_KEYS.iter().any(|r| r.eq_ignore_ascii_case(key)) || ["cell", "cells", "range", "ranges", "ref"].iter().any(|end| k.ends_with(end)) +} + +/// The most nesting [`resolve_params`] walks into. +const MAX_JSON_DEPTH: usize = 64; + +/// Whether `text` starts like a token (its base is one this environment knows). +pub fn is_token(text: &str, env: &Env) -> bool { + let t = text.trim(); + if t.get(..5).is_some_and(|h| h.eq_ignore_ascii_case("name:")) { + return true; + } + let n: usize = t.chars().take_while(|c| c.is_alphanumeric() || *c == '_').map(char::len_utf8).sum(); + let base = t.get(..n).unwrap_or(""); + is_builtin(base) || env.inputs.iter().any(|(n, ..)| n.eq_ignore_ascii_case(base)) +} + +fn is_builtin(base: &str) -> bool { + ["Output", "Row", "Column"].iter().any(|b| b.eq_ignore_ascii_case(base)) +} + +/// Resolves a token, or a plain reference (`B2`, `Sheet2!A1:C3`, a defined name). +pub fn resolve_ref(text: &str, env: &Env) -> R { + let t = text.trim(); + if is_token(t, env) { + return resolve_token(t, env); + } + plain_ref(t, env) +} + +/// `A1`, `'My Sheet'!A1:B2` or a defined name or table. +fn plain_ref(t: &str, env: &Env) -> R { + let (sheet, body) = crate::cmd::split_sheet(t); + let si = match &sheet { + Some(name) => env.wb.sheet_index(name).ok_or_else(|| ResolveError::Bad(format!("There is no sheet named '{name}'.")))?, + None => env.active_sheet, + }; + if let Some(range) = RangeRef::parse(body) { + return Ok(Place { sheet: si, range }); + } + if sheet.is_none() { + return named(t, env); + } + bad(format!("'{t}' isn't a reference.")) +} + +/// A defined name (by its formula) or a table (its data rows). +fn named(name: &str, env: &Env) -> R { + if let Some(n) = env.wb.name(name, env.active_sheet) { + let f = n.formula.trim().trim_start_matches('=').replace('$', ""); + let (sheet, body) = crate::cmd::split_sheet(&f); + let si = match sheet { + Some(s) => env.wb.sheet_index(&s), + None => n.scope.or(Some(env.active_sheet)), + }; + if let (Some(sheet), Some(range)) = (si, RangeRef::parse(body)) { + return Ok(Place { sheet, range }); + } + return bad(format!("The name '{name}' doesn't refer to a range.")); + } + if let Some((si, ti)) = env.wb.table(name) + && let Some(t) = env.wb.sheet(si).and_then(|sh| sh.tables.get(ti)) + { + return Ok(Place { sheet: si, range: t.data_range().unwrap_or(t.range) }); + } + bad(format!("There is no name or table called '{name}'.")) +} + +/// Resolves a token (see the module docs). +pub fn resolve_token(text: &str, env: &Env) -> R { + let t = text.trim(); + let mut p = Parser { s: t, at: 0 }; + let (mut place, mut header) = p.base(env)?; + while p.eat('.') { + let name = p.ident(); + if name.is_empty() { + return bad(format!("'{t}': expected a method name after '.'.")); + } + let args = if p.eat('(') { p.args(t)? } else { vec![] }; + let r = place.range; + let range = match (name.to_ascii_lowercase().as_str(), args.as_slice()) { + ("col", [Arg::Int(n)]) => sub(r, None, Some((*n, *n)), t)?, + ("col", [Arg::Text(h)]) => { + let c = header_col(env, place, h).ok_or_else(|| ResolveError::Bad(format!("'{t}': no column is headed '{h}'.")))?; + sub(r, None, Some((c, c)), t)? + } + ("row", [Arg::Int(n)]) => sub(r, Some((*n, *n)), None, t)?, + ("cols", [Arg::Span(a, b)]) => sub(r, None, Some(span(*a, *b, i64::from(r.width()))), t)?, + ("rows", [Arg::Span(a, b)]) => sub(r, Some(span(*a, *b, i64::from(r.height()))), None, t)?, + ("cell", [Arg::Int(row), Arg::Int(col)]) => { + let c = shift(r.start, row - 1, col - 1, t)?; + RangeRef::cell(c) + } + ("offset", [Arg::Int(dr), Arg::Int(dc)]) => RangeRef::new(shift(r.start, *dr, *dc, t)?, shift(r.end, *dr, *dc, t)?), + ("resize", [Arg::Int(h), Arg::Int(w)]) => { + if *h < 1 || *w < 1 { + return bad(format!("'{t}': resize needs at least 1 row and 1 column.")); + } + RangeRef::new(r.start, shift(r.start, h - 1, w - 1, t)?) + } + ("data", []) => { + if header && r.height() > 1 { + RangeRef::new(CellRef::new(r.start.row + 1, r.start.col), r.end) + } else { + r + } + } + ("below", []) | ("below", [Arg::Int(_)]) => { + let n = arg_or_one(&args); + let top = shift(CellRef::new(r.end.row, r.start.col), 1, 0, t)?; + RangeRef::new(top, shift(CellRef::new(r.end.row, r.end.col), n, 0, t)?) + } + ("right", []) | ("right", [Arg::Int(_)]) => { + let n = arg_or_one(&args); + let left = shift(CellRef::new(r.start.row, r.end.col), 0, 1, t)?; + RangeRef::new(left, shift(r.end, 0, n, t)?) + } + _ => { + return bad(format!( + "'{t}': '{name}' with these arguments isn't a token method (col, row, cols, rows, cell, offset, resize, data, below, right)." + )); + } + }; + // After a method the result is plain cells: `data` and `col("…")` no longer apply. + header = false; + place = Place { sheet: place.sheet, range }; + } + p.skip_ws(); + if p.at < p.s.len() { + return bad(format!("'{t}' isn't a token: unexpected '{}'.", p.s.get(p.at..).unwrap_or(""))); + } + Ok(place) +} + +fn arg_or_one(args: &[Arg]) -> i64 { + match args.first() { + Some(Arg::Int(n)) => (*n).max(1), + _ => 1, + } +} + +/// `a..b` with open ends filled from 1 and `len`. +fn span(a: Option, b: Option, len: i64) -> (i64, i64) { + (a.unwrap_or(1), b.unwrap_or(len)) +} + +/// Rows `rows` and columns `cols` (1-based, inclusive) of `r`; `None` keeps them all. +fn sub(r: RangeRef, rows: Option<(i64, i64)>, cols: Option<(i64, i64)>, t: &str) -> R { + let (h, w) = (i64::from(r.height()), i64::from(r.width())); + let (r0, r1) = rows.unwrap_or((1, h)); + let (c0, c1) = cols.unwrap_or((1, w)); + if r0 < 1 || c0 < 1 || r1 < r0 || c1 < c0 || r1 > h || c1 > w { + return bad(format!("'{t}' is outside the range it starts from ({} rows by {} columns).", h, w)); + } + Ok(RangeRef::new(shift(r.start, r0 - 1, c0 - 1, t)?, shift(r.start, r1 - 1, c1 - 1, t)?)) +} + +fn shift(c: CellRef, dr: i64, dc: i64, t: &str) -> R { + let row = i64::from(c.row).checked_add(dr); + let col = i64::from(c.col).checked_add(dc); + match (row, col) { + (Some(row), Some(col)) if (0..i64::from(MAX_ROWS)).contains(&row) && (0..i64::from(MAX_COLS)).contains(&col) => { + Ok(CellRef::new(row as u32, col as u32)) + } + (Some(row), Some(_)) if row < 0 => Err(ResolveError::Edge(format!("'{t}' runs past the top of the sheet."))), + (Some(row), Some(_)) if row >= i64::from(MAX_ROWS) => Err(ResolveError::Edge(format!("'{t}' runs past the last row of the sheet."))), + _ => Err(ResolveError::Edge(format!("'{t}' runs past the edge of the sheet."))), + } +} + +/// The 1-based column of `place` whose header (first row) reads `h`. +fn header_col(env: &Env, place: Place, h: &str) -> Option { + let sh = env.wb.sheet(place.sheet)?; + let r = place.range; + (r.start.col..=r.end.col) + .position(|c| crate::display::cell_text(env.wb, sh, CellRef::new(r.start.row, c)).trim().eq_ignore_ascii_case(h.trim())) + .map(|i| i as i64 + 1) +} + +#[derive(Debug)] +enum Arg { + Int(i64), + Span(Option, Option), + Text(String), +} + +struct Parser<'a> { + s: &'a str, + at: usize, +} + +impl Parser<'_> { + fn rest(&self) -> &str { + self.s.get(self.at..).unwrap_or("") + } + fn skip_ws(&mut self) { + let n = self.rest().len() - self.rest().trim_start().len(); + self.at += n; + } + fn eat(&mut self, c: char) -> bool { + self.skip_ws(); + if self.rest().starts_with(c) { + self.at += c.len_utf8(); + true + } else { + false + } + } + fn ident(&mut self) -> String { + self.skip_ws(); + let n: usize = self.rest().chars().take_while(|c| c.is_alphanumeric() || *c == '_').map(char::len_utf8).sum(); + let out = self.rest().get(..n).unwrap_or("").to_string(); + self.at += n; + out + } + fn int(&mut self) -> Option { + self.skip_ws(); + let neg = self.rest().starts_with('-'); + let start = self.at + usize::from(neg); + let digits: usize = self.s.get(start..).unwrap_or("").chars().take_while(char::is_ascii_digit).count(); + if digits == 0 || digits > 9 { + return None; + } + let n: i64 = self.s.get(start..start + digits)?.parse().ok()?; + self.at = start + digits; + Some(if neg { -n } else { n }) + } + /// The base of a token and whether it has a header row. + fn base(&mut self, env: &Env) -> R<(Place, bool)> { + self.skip_ws(); + if self.rest().get(..5).is_some_and(|h| h.eq_ignore_ascii_case("name:")) { + self.at += 5; + // A name runs to the first `.` followed by a method, or the end. + let n: usize = self.rest().chars().take_while(|c| c.is_alphanumeric() || matches!(c, '_' | '\\')).map(char::len_utf8).sum(); + let name = self.rest().get(..n).unwrap_or("").to_string(); + self.at += n; + if name.is_empty() { + return bad("'Name:' needs a defined name after it."); + } + return Ok((named(&name, env)?, false)); + } + let base = self.ident(); + if base.eq_ignore_ascii_case("Output") { + return env.output.map(|p| (p, false)).ok_or_else(|| ResolveError::Bad("'Output' is used, but the macro has no output anchor.".into())); + } + if base.eq_ignore_ascii_case("Row") || base.eq_ignore_ascii_case("Column") { + return match env.item { + Some((_, p)) => Ok((p, false)), + None => bad(format!("'{base}' only means something inside a Repeat step.")), + }; + } + match env.inputs.iter().find(|(n, ..)| n.eq_ignore_ascii_case(&base)) { + Some((_, p, header)) => Ok((*p, *header)), + None => bad(format!("'{base}' isn't an input of this macro.")), + } + } + fn args(&mut self, t: &str) -> R> { + let mut out = Vec::new(); + if self.eat(')') { + return Ok(out); + } + loop { + self.skip_ws(); + if self.rest().starts_with('"') { + self.at += 1; + let n = self.rest().find('"').ok_or_else(|| ResolveError::Bad(format!("'{t}': a quote isn't closed.")))?; + out.push(Arg::Text(self.rest().get(..n).unwrap_or("").to_string())); + self.at += n + 1; + } else { + let a = self.int(); + self.skip_ws(); + if self.rest().starts_with("..") { + self.at += 2; + let b = self.int(); + out.push(Arg::Span(a, b)); + } else { + out.push(Arg::Int(a.ok_or_else(|| ResolveError::Bad(format!("'{t}': expected a whole number.")))?)); + } + } + if out.len() > 4 { + return bad(format!("'{t}': too many arguments.")); + } + if self.eat(')') { + return Ok(out); + } + if !self.eat(',') { + return bad(format!("'{t}': expected ',' or ')'.")); + } + } + } +} + +/// `place` as A1 text, with the sheet name when it isn't the active sheet. +pub fn a1(place: Place, env: &Env) -> String { + if place.sheet == env.active_sheet { + return place.range.a1(); + } + let name = env.wb.sheet(place.sheet).map(|s| s.name.as_str()).unwrap_or(""); + format!("{}!{}", quote_sheet(name), place.range.a1()) +} + +/// `Sheet1` or `'My Sheet'` (quotes doubled). +pub fn quote_sheet(name: &str) -> String { + if !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') && !name.starts_with(|c: char| c.is_ascii_digit()) { + name.to_string() + } else { + format!("'{}'", name.replace('\'', "''")) + } +} + +/// Step parameters with every token replaced by A1 text. +pub fn resolve_params(params: &Json, env: &Env) -> R { + walk(params, None, env, 0) +} + +fn walk(v: &Json, key: Option<&str>, env: &Env, depth: usize) -> R { + if depth > MAX_JSON_DEPTH { + return bad("The step's parameters nest too deeply."); + } + Ok(match v { + Json::Object(o) => { + if o.len() == 1 + && let Some(Json::String(t)) = o.get("$ref") + { + return Ok(Json::String(a1(resolve_ref(t, env)?, env))); + } + let mut out = Map::new(); + for (k, x) in o { + out.insert(k.clone(), walk(x, Some(k), env, depth + 1)?); + } + Json::Object(out) + } + // Items of a list keep their key's kind (`ranges: ["Input", …]`). + Json::Array(a) => Json::Array(a.iter().map(|x| walk(x, key, env, depth + 1)).collect::>>()?), + Json::String(s) => { + if key.is_some_and(is_ref_key) && is_token(s, env) { + Json::String(a1(resolve_token(s, env)?, env)) + } else { + Json::String(interpolate(s, env)?) + } + } + other => other.clone(), + }) +} + +/// Replaces each `{{token}}` in `s`. +pub fn interpolate(s: &str, env: &Env) -> R { + if !s.contains("{{") { + return Ok(s.to_string()); + } + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(i) = rest.find("{{") { + out.push_str(rest.get(..i).unwrap_or("")); + let after = rest.get(i + 2..).unwrap_or(""); + let Some(j) = after.find("}}") else { + out.push_str(rest.get(i..).unwrap_or("")); + return Ok(out); + }; + let token = after.get(..j).unwrap_or(""); + out.push_str(&a1(resolve_ref(token, env)?, env)); + rest = after.get(j + 2..).unwrap_or(""); + } + out.push_str(rest); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn wb() -> Workbook { + let mut wb = Workbook::new(); + wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("My Data"))); + let sh = wb.sheet_mut(0).unwrap(); + for (i, h) in ["Name", "Amount", "Date"].iter().enumerate() { + sh.set_value(CellRef::new(1, 1 + i as u32), gridcraft_core::Value::text(*h)); + } + wb.names.push(gridcraft_model::DefinedName { + name: "Total".into(), + scope: None, + formula: "Sheet1!$F$20".into(), + comment: String::new(), + hidden: false, + }); + wb.names.push(gridcraft_model::DefinedName { + name: "Rates".into(), + scope: None, + formula: "'My Data'!$A$1:$A$5".into(), + comment: String::new(), + hidden: false, + }); + wb + } + + fn place(a1: &str) -> Place { + Place { sheet: 0, range: RangeRef::parse(a1).unwrap() } + } + + fn check(env: &Env, token: &str, want: &str) { + match resolve_ref(token, env) { + Ok(p) => assert_eq!(a1(p, env), want, "{token}"), + Err(e) => panic!("{token}: {e}"), + } + } + + #[test] + fn every_token_resolves() { + let wb = wb(); + // Input B2:D11 with a header row; Other on another sheet. + let inputs = vec![ + ("Input".to_string(), place("B2:D11"), true), + ("Other".to_string(), Place { sheet: 1, range: RangeRef::parse("C3:C4").unwrap() }, false), + ]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: Some(place("G2")), item: Some((RepeatOf::Rows, place("B5:D5"))) }; + for (token, want) in [ + ("Input", "B2:D11"), + ("input", "B2:D11"), + ("Input.col(2)", "C2:C11"), + ("Input.col(\"Amount\")", "C2:C11"), + ("Input.col(\"date\").data", "D2:D11"), + ("Input.data", "B3:D11"), + ("Input.data.col(2)", "C3:C11"), + ("Input.row(1)", "B2:D2"), + ("Input.rows(2..)", "B3:D11"), + ("Input.rows(2..4)", "B3:D5"), + ("Input.rows(..2)", "B2:D3"), + ("Input.cols(2..3)", "C2:D11"), + ("Input.cell(1,1)", "B2"), + ("Input.cell(3,2)", "C4"), + ("Input.cell(0,1)", "B1"), + ("Input.cell(12,3)", "D13"), + ("Input.offset(0,4)", "F2:H11"), + ("Input.offset(-1,-1)", "A1:C10"), + ("Input.cell(1,1).resize(2,3)", "B2:D3"), + ("Input.below()", "B12:D12"), + ("Input.below(2)", "B12:D13"), + ("Input.col(2).below", "C12"), + ("Input.right", "E2:E11"), + (" Input . col( 2 ) ", "C2:C11"), + ("Output", "G2"), + ("Output.offset(1,0)", "G3"), + ("Row", "B5:D5"), + ("Row.cell(1,2)", "C5"), + ("Column", "B5:D5"), + ("Name:Total", "F20"), + ("Name:Rates", "'My Data'!A1:A5"), + ("Name:Rates.cell(2,1)", "'My Data'!A2"), + ("Other", "'My Data'!C3:C4"), + ("B7", "B7"), + ("'My Data'!A1:B2", "'My Data'!A1:B2"), + ("Total", "F20"), + ] { + check(&env, token, want); + } + } + + #[test] + fn bad_tokens_say_why() { + let wb = wb(); + let inputs = vec![("Input".to_string(), place("B2:D11"), false)]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None }; + for (token, needle, edge) in [ + ("Input.col(4)", "outside the range", false), + ("Input.col(0)", "outside the range", false), + ("Input.rows(5..2)", "outside the range", false), + ("Input.col(\"Nope\")", "no column is headed", false), + ("Input.colour(1)", "isn't a token method", false), + ("Input.col(1", "expected", false), + ("Input.col(x)", "whole number", false), + ("Input.col(99999999999)", "whole number", false), + ("Input.", "method name", false), + ("Input junk", "unexpected", false), + ("Input.resize(0,1)", "at least 1 row", false), + ("Output", "no output anchor", false), + ("Row", "inside a Repeat", false), + ("Name:", "needs a defined name", false), + ("Name:Nothing", "no name or table", false), + ("Nowhere!A1", "no sheet named", false), + ("Input.offset(-2,0)", "past the top", true), + ("Input.offset(0,-2)", "edge of the sheet", true), + ("Input.offset(1048570,0)", "past the last row", true), + ("Input.cell(1,16384)", "edge of the sheet", true), + ] { + let e = resolve_ref(token, &env).unwrap_err(); + assert!(e.message().contains(needle), "{token}: {e}"); + assert_eq!(matches!(e, ResolveError::Edge(_)), edge, "{token}"); + } + } + + #[test] + fn params_resolve_tokens_in_reference_keys_and_braces_anywhere() { + let wb = wb(); + let inputs = vec![("Input".to_string(), place("B2:D11"), true)]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: Some(place("F2")), item: None }; + let p = json!({ + "cell": "Output", + "range": "Input.col(2)", + "ranges": ["Input.row(1)", "A1"], + "input": "Row", + "value": "Input", + "formula": "=SUM({{Input.data.col(2)}})/{{Name:Total}}", + "nested": {"target": {"$ref": "Input.cell(1,1)"}, "n": 3, "flag": true}, + "text": {"$ref": "Output"}, + "type": "column", + "sheet": "Input", + "rowInputCell": "Input.cell(1,1)", + "unclosed": "a {{b" + }); + let r = resolve_params(&p, &env).unwrap(); + assert_eq!( + r, + json!({ + "cell": "F2", + "range": "C2:C11", + "ranges": ["B2:D2", "A1"], + "input": "Row", + "value": "Input", + "formula": "=SUM(C3:C11)/F20", + "nested": {"target": "B2", "n": 3, "flag": true}, + "text": "F2", + "type": "column", + "sheet": "Input", + "rowInputCell": "B2", + "unclosed": "a {{b" + }) + ); + // Deeply nested parameters are refused, not followed. + let mut deep = json!(1); + for _ in 0..100 { + deep = json!([deep]); + } + assert!(resolve_params(&deep, &env).is_err()); + assert!(resolve_params(&json!({"cell": "Input.col(9)"}), &env).is_err()); + } + + #[test] + fn sheet_names_are_quoted_when_needed() { + for (name, want) in [("Sheet1", "Sheet1"), ("My Data", "'My Data'"), ("O'Hara", "'O''Hara'"), ("2024", "'2024'"), ("", "''")] { + assert_eq!(quote_sheet(name), want); + } + } +} From 4c6962a97c8ad812ffbe858f6a1583b43505391b Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:24:08 -0400 Subject: [PATCH 040/687] Engine: a partial macro footprint no longer clears later writes; the run checks each step The footprint dry run stops at an approved file step and at Solver, but make_way took a partial footprint with no conflicts as proof that the run was safe, also after a shift. A macro that exported a CSV and then set B1 ran under onConflict fail or ask and overwrote B1 without asking. When the footprint is partial and the rule is not overwrite, the run's machine now checks every step as it runs: a step that writes over cells, sheets or objects that held data when the run started stops the run and rolls it back, with a confirmation under ask and an error otherwise. make_way now returns the machine for the run along with whether it shifted. --- crates/engine/src/macros/run.rs | 198 +++++++++++++++++++++++------- crates/engine/src/tests/macros.rs | 60 +++++++++ docs/macros.md | 6 +- 3 files changed, 220 insertions(+), 44 deletions(-) diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index aebaa8c907..1f4f39f746 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -9,7 +9,9 @@ //! 3. **Footprint**: unless the macro may overwrite, it first runs on the real workbook in dry-run //! mode, the changes are compared with `CellStore::diff`, and everything is put back. Cells it //! would write outside its inputs that already hold data are conflicts, handled by the output -//! rule: ask, fail, overwrite, or shift the data down or right (and check again). +//! rule: ask, fail, overwrite, or shift the data down or right (and check again). A dry run +//! that stops early (a file step, Solver) proves nothing about later steps, so the run then +//! checks each step as it goes. //! 4. **Run**: one undo step (`Session::grouped`); an error at any step rolls everything back and //! names the step. //! @@ -52,11 +54,14 @@ pub struct StepError { /// The step ran past the edge of the sheet. #[serde(skip_serializing_if = "std::ops::Not::not")] pub edge: bool, + /// The step would write over data and the conflict rule is ask: overwriting answers it. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub confirm: bool, } impl StepError { fn new(step: &str, command: Option<&str>, message: impl Into) -> StepError { - StepError { step: step.to_string(), command: command.map(str::to_string), message: message.into(), edge: false } + StepError { step: step.to_string(), command: command.map(str::to_string), message: message.into(), edge: false, confirm: false } } fn resolve(step: &str, command: Option<&str>, e: ResolveError) -> StepError { @@ -73,7 +78,8 @@ impl StepError { } pub fn into_engine(self, name: &str) -> EngineError { - EngineError::Other(format!("Macro '{name}' stopped. {} Nothing was changed.", self.describe())) + let text = format!("Macro '{name}' stopped. {} Nothing was changed.", self.describe()); + if self.confirm { EngineError::Confirm(text) } else { EngineError::Other(text) } } } @@ -424,6 +430,85 @@ pub struct Machine { pub partial: Option<(String, String)>, /// The last step's resolved parameters (for the debugger). pub last_params: Option, + /// Set when the footprint was partial: each step is checked for writes over data as it runs. + guard: Option, +} + +/// Checks each step of a real run for writes over data the footprint couldn't see. +#[derive(Clone, Debug)] +struct Guard { + /// The workbook when the run started, and for each sheet now, its index there (`None` for a + /// sheet the run added). + start: Arc, + origin: Vec>, + /// Where the footprint stopped: "step 2 (file.exportCsv writes a file)". + partial: String, + /// The conflict rule is ask. + ask: bool, +} + +impl Guard { + fn new(start: Arc, partial: &Json, ask: bool) -> Guard { + let text = |k: &str| partial.get(k).and_then(Json::as_str).unwrap_or_default().to_string(); + let partial = format!("step {} ({})", text("step"), text("reason")); + Guard { origin: (0..start.sheets.len()).map(Some).collect(), start, partial, ask } + } + + /// What one step wrote over that held data when the run started: cells outside the inputs, + /// sheets and objects it deleted ("B1 on Sheet1; 1 object"), or `""`. + fn check(&mut self, prev: &Workbook, after: &Workbook, inputs: &[Bound]) -> String { + let (pairs, _) = pair_sheets(prev, after); + let mut origin = vec![None; after.sheets.len()]; + let mut parts = Vec::new(); + let mut objects = 0usize; + for (pi, ai) in pairs { + // A sheet the run added holds nothing of the person's. + let Some(si) = self.origin.get(pi).copied().flatten() else { continue }; + let Some(ss) = self.start.sheets.get(si) else { continue }; + let Some(ai) = ai else { + parts.push(format!("the sheet {}", ss.name)); + continue; + }; + if let Some(o) = origin.get_mut(ai) { + *o = Some(si); + } + let (Some(ps), Some(sa)) = (prev.sheets.get(pi), after.sheets.get(ai)) else { continue }; + if Arc::ptr_eq(ps, sa) { + continue; + } + let in_input = |c: CellRef| inputs.iter().any(|(_, p, _)| p.sheet == si && p.range.contains(c)); + let mut hit: Vec = ps + .cells + .diff(&sa.cells) + .into_iter() + .filter(|&c| !in_input(c) && !recalculated_only(ps.cell(c), sa.cell(c))) + .filter(|&c| ss.cell(c).is_some_and(has_content) && content_differs(ss.cell(c), sa.cell(c))) + .collect(); + if !hit.is_empty() { + let mut listed = ranges(&mut hit); + if listed.len() > 3 { + listed.truncate(3); + listed.push("…".into()); + } + parts.push(format!("{} on {}", listed.join(", "), sa.name)); + } + objects += deleted(&ss.charts, &ps.charts, &sa.charts, |o| o.id) + + deleted(&ss.images, &ps.images, &sa.images, |o| o.id) + + deleted(&ss.shapes, &ps.shapes, &sa.shapes, |o| o.id); + } + if objects > 0 { + parts.push(format!("{objects} object{}", if objects == 1 { "" } else { "s" })); + } + self.origin = origin; + parts.join("; ") + } +} + +/// Objects of `prev` that `after` no longer has and that were there at the start. +fn deleted(start: &[T], prev: &[T], after: &[T], id: impl Fn(&T) -> u32) -> usize { + let kept: BTreeSet = after.iter().map(&id).collect(); + let had: BTreeSet = start.iter().map(&id).collect(); + prev.iter().map(&id).filter(|i| !kept.contains(i) && had.contains(i)).count() } impl Machine { @@ -443,6 +528,7 @@ impl Machine { stopped: None, partial: None, last_params: None, + guard: None, }; m.settle(); m @@ -605,6 +691,7 @@ impl Machine { return Ok(()); } self.last_params = Some(resolved.clone()); + let prev = self.guard.as_ref().and_then(|_| s.docs.iter().find(|d| d.uid == self.doc)).map(|d| d.wb.clone()); let queued = s.ui_requests.len(); let r = s.execute(id, resolved); // Macros never open dialogs or web pages; messages for the person stay queued. @@ -622,6 +709,14 @@ impl Machine { if let Some(b) = blocked { return Err(fail(b)); } + if let (Some(g), Some(prev)) = (self.guard.as_mut(), prev) { + let after = s.docs.iter().find(|d| d.uid == self.doc).map(|d| d.wb.clone()).ok_or_else(|| fail("The workbook was closed.".into()))?; + let what = g.check(&prev, &after, &self.inputs); + if !what.is_empty() { + let msg = format!("It would write over data ({what}); the check before the run stopped at {} and couldn't see this.", g.partial); + return Err(StepError { confirm: g.ask, ..fail(msg) }); + } + } Ok(()) } } @@ -843,14 +938,8 @@ pub fn footprint(s: &mut Session, p: &Prepared) -> Result { /// without changing them (a formula written again), so a shift moves everything the macro writes. fn diff(before: &Workbook, after: &Workbook, inputs: &[Bound], touched: &[(String, CellRef)]) -> Footprint { let mut fp = Footprint::default(); - // Sheets are matched by name; what's left is matched in order (a renamed sheet). - let mut pairs: Vec<(usize, Option)> = before.sheets.iter().enumerate().map(|(bi, sh)| (bi, after.sheet_index(&sh.name))).collect(); - let matched: BTreeSet = pairs.iter().filter_map(|(_, a)| *a).collect(); - let mut spare = (0..after.sheets.len()).filter(|a| !matched.contains(a)); - for (_, a) in pairs.iter_mut().filter(|(_, a)| a.is_none()) { - *a = spare.next(); - } - fp.sheets_added = spare.filter_map(|a| after.sheet(a).map(|s| s.name.clone())).collect(); + let (pairs, added) = pair_sheets(before, after); + fp.sheets_added = added.into_iter().filter_map(|a| after.sheet(a).map(|s| s.name.clone())).collect(); for (bi, ai) in pairs { let Some(bs) = before.sheets.get(bi) else { continue }; let Some(ai) = ai else { @@ -900,6 +989,19 @@ fn diff(before: &Workbook, after: &Workbook, inputs: &[Bound], touched: &[(Strin fp } +/// Each sheet of `before` with its index in `after` (`None` when deleted), and the sheets of +/// `after` that are new. Sheets are matched by name; what's left is matched in order (a renamed +/// sheet). +fn pair_sheets(before: &Workbook, after: &Workbook) -> (Vec<(usize, Option)>, Vec) { + let mut pairs: Vec<(usize, Option)> = before.sheets.iter().enumerate().map(|(bi, sh)| (bi, after.sheet_index(&sh.name))).collect(); + let matched: BTreeSet = pairs.iter().filter_map(|(_, a)| *a).collect(); + let mut spare = (0..after.sheets.len()).filter(|a| !matched.contains(a)); + for (_, a) in pairs.iter_mut().filter(|(_, a)| a.is_none()) { + *a = spare.next(); + } + (pairs, spare.collect()) +} + /// Only a formula's cached value changed (a recalculation, not a write). fn recalculated_only(b: Option<&Cell>, a: Option<&Cell>) -> bool { match (b, a) { @@ -1012,44 +1114,56 @@ pub fn begin(s: &mut Session, name: &str, p: &Json) -> Result { /// The footprint check and the output rule, inside the run's undo group: an error when the macro /// would fail or write over data it may not, after shifting the data if the rule says so. Returns -/// whether it shifted. -pub fn make_way(s: &mut Session, b: &Begun) -> Result { +/// the machine for the run and whether it shifted. +/// +/// A partial footprint proves nothing about the steps it didn't reach, so then the machine +/// checks each step as it runs and stops the run at one that writes over data. +pub fn make_way(s: &mut Session, b: &Begun) -> Result<(Machine, bool)> { let (m, rule) = (&b.mac, b.rule); + let mut machine = Machine::new(&b.prepared, false); + machine.time_limit_ms = b.time_limit_ms; if rule == OnConflict::Overwrite { - return Ok(false); + return Ok((machine, false)); } - let fp = footprint(s, &b.prepared)?; - if let Some(e) = fp.error { + let mut fp = footprint(s, &b.prepared)?; + if let Some(e) = fp.error.take() { return Err(e.into_engine(&m.name)); } - if fp.conflict_count() == 0 { - return Ok(false); - } - let what = fp.describe_conflicts(); - match rule { - OnConflict::Ask => Err(EngineError::Confirm(format!( - "Macro '{}' would write over cells that hold data ({what}). Overwrite them, or run again with onConflict shiftDown, shiftRight or fail", - m.name - ))), - OnConflict::Fail | OnConflict::Overwrite => { - Err(EngineError::Other(format!("Macro '{}' would write over cells that hold data ({what}). Nothing was changed.", m.name))) - } - OnConflict::ShiftDown | OnConflict::ShiftRight => { - make_room(s, &fp, rule == OnConflict::ShiftDown)?; - let again = footprint(s, &b.prepared)?; - if let Some(e) = again.error { - return Err(e.into_engine(&m.name)); - } - if again.conflict_count() > 0 { - return Err(EngineError::Other(format!( - "Macro '{}': shifting the data didn't make room ({}). Nothing was changed.", - m.name, - again.describe_conflicts() + let mut shifted = false; + if fp.conflict_count() > 0 { + let what = fp.describe_conflicts(); + match rule { + OnConflict::Ask => { + return Err(EngineError::Confirm(format!( + "Macro '{}' would write over cells that hold data ({what}). Overwrite them, or run again with onConflict shiftDown, shiftRight or fail", + m.name ))); } - Ok(true) + OnConflict::Fail | OnConflict::Overwrite => { + return Err(EngineError::Other(format!("Macro '{}' would write over cells that hold data ({what}). Nothing was changed.", m.name))); + } + OnConflict::ShiftDown | OnConflict::ShiftRight => { + make_room(s, &fp, rule == OnConflict::ShiftDown)?; + fp = footprint(s, &b.prepared)?; + if let Some(e) = fp.error.take() { + return Err(e.into_engine(&m.name)); + } + if fp.conflict_count() > 0 { + return Err(EngineError::Other(format!( + "Macro '{}': shifting the data didn't make room ({}). Nothing was changed.", + m.name, + fp.describe_conflicts() + ))); + } + shifted = true; + } } } + if let Some(p) = &fp.partial { + let start = s.docs.iter().find(|d| d.uid == b.prepared.doc).ok_or(EngineError::NoDocument)?.wb.clone(); + machine.guard = Some(Guard::new(start, p, rule == OnConflict::Ask)); + } + Ok((machine, shifted)) } /// Runs macro `name` on the active workbook: `{inputs?, onConflict?, overwrite?, approve?, @@ -1058,9 +1172,7 @@ pub fn run(s: &mut Session, name: &str, p: &Json) -> Result { let b = begin(s, name, p)?; let label = format!("Run Macro: {}", b.mac.name); s.grouped(&label, |s| { - let shifted = make_way(s, &b)?; - let mut machine = Machine::new(&b.prepared, false); - machine.time_limit_ms = b.time_limit_ms; + let (mut machine, shifted) = make_way(s, &b)?; let name = &b.mac.name; machine.run_to_end(s).map_err(|e| e.into_engine(name))?; match machine.stopped { diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index c3a2c44425..5f483e31dd 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -474,3 +474,63 @@ fn macros_from_a_file_ask_before_running() { // Approved once per macro content: the second run doesn't ask. s.execute("macro.run", json!({"name": "Theirs"})).unwrap(); } + +#[test] +fn a_partial_footprint_doesnt_clear_later_writes() { + let dir = std::env::temp_dir().join(format!("gc-macro-partial-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let csv = dir.join("out.csv").to_string_lossy().to_string(); + let export = json!({"command": "file.exportCsv", "params": {"path": csv}}); + let solver = json!({"command": "data.solver", "params": {"objective": "C1", "variables": "A1", "constraints": ["A1 <= 5"], "method": "simplex", "saveModel": false}}); + let late = json!({"command": "cell.set", "params": {"cell": "B1", "input": "over"}}); + // The dry run stops at an approved file step or at Solver; the step after it writes over B1. + for first in [&export, &solver] { + let mut s = super::s(); + set(&mut s, "A1", "1"); + set(&mut s, "C1", "=A1"); + set(&mut s, "B1", "keep"); + put(&mut s, json!({"name": "Late", "inputs": [{"name": "Input"}], "permissions": ["file.exportCsv"], "steps": [first, late]})); + let start = s.doc().unwrap().wb.clone(); + let c = s.execute("macro.check", json!({"name": "Late", "inputs": "A1"})).unwrap(); + if c.get("needsApproval").is_none() { + assert_eq!((c["conflicts"].as_u64(), &c["footprint"]["partial"]["step"]), (Some(0), &json!("1")), "{c}"); + } + for rule in ["fail", "ask"] { + let r = s.execute("macro.run", json!({"name": "Late", "inputs": "A1", "onConflict": rule, "approve": true})); + let e = match (rule, r) { + ("fail", Err(EngineError::Other(e))) | ("ask", Err(EngineError::Confirm(e))) => e, + (_, r) => panic!("{first} {rule}: {r:?}"), + }; + assert!(e.contains("Step 2 (cell.set): It would write over data (B1 on Sheet1)"), "{e}"); + assert_eq!(v(&s, "B1").display(), "keep", "{first} {rule}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start), "{first} {rule}: rolled back"); + } + // The answer to the question overwrites. + s.execute("macro.run", json!({"name": "Late", "inputs": "A1", "overwrite": true})).unwrap(); + assert_eq!(v(&s, "B1").display(), "over"); + } + // A step that writes over what the macro itself wrote isn't a conflict. + let mut s = super::s(); + let again = |input: &str| json!({"command": "cell.set", "params": {"cell": "E1", "input": input}}); + put( + &mut s, + json!({"name": "Twice", "permissions": ["file.exportCsv"], "output": {"onConflict": "fail"}, "steps": [again("a"), export, again("b")]}), + ); + s.execute("macro.run", json!({"name": "Twice", "approve": true})).unwrap(); + assert_eq!(v(&s, "E1").display(), "b"); + // After a shift, the steps past the file step are checked too. + let mut s = super::s(); + set(&mut s, "C1", "old"); + set(&mut s, "D1", "keep"); + let step = |cell: &str| json!({"command": "cell.set", "params": {"cell": cell, "input": "new"}}); + put( + &mut s, + json!({"name": "Shift", "permissions": ["file.exportCsv"], "output": {"onConflict": "shiftDown"}, "steps": [step("C1"), export, step("D1")]}), + ); + let start = s.doc().unwrap().wb.clone(); + let err = s.execute("macro.run", json!({"name": "Shift", "approve": true})).unwrap_err().to_string(); + assert!(err.contains("Step 3 (cell.set): It would write over data (D1 on Sheet1)"), "{err}"); + assert_eq!((v(&s, "C1").display(), v(&s, "D1").display()), ("old".into(), "keep".into())); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start), "the shift is rolled back too"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/docs/macros.md b/docs/macros.md index 424dd4f396..2b17c6a50d 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -85,7 +85,11 @@ called Output, even when a token or input has that name. delete, are conflicts, handled by `onConflict` (or the macro's `output.onConflict`): `ask` (the call fails with `confirm: …`; `overwrite: true` answers it), `fail`, `overwrite`, `shiftDown` or `shiftRight` (Insert Cells makes room, then the footprint is checked again). Steps that touch - files, and Solver, end the dry run early; the footprint then says it is partial. + files, and Solver, end the dry run early; the footprint then says it is partial, and unless + the rule is `overwrite`, the run checks every step as it goes. A step that writes over cells, + sheets or objects that held data when the run started stops the run and rolls the workbook + back (a file already written stays): it asks (`confirm: …`) under `ask` and fails otherwise, + since shifting can't make room that late. 3. **Run.** One undo step, "Run Macro: Totals". Any error rolls the whole run back and names the step: `Step 3.1 (cell.set): …` (paths are 1-based; `2.1` is inside a Repeat, `4.then.1` inside an If). From 252ffa95b6a9ee8c67b1a738770348bc081295d4 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:21:46 -0400 Subject: [PATCH 041/687] Engine: macro recorder keeps a recording when saving it fails Stopping a recording took the recorded commands and where recording started before saving the macro, so a failed save threw both away. For example, stopping with a name that holds a control character, or in a workbook at its macro limit, ended the recording, and stopping again with a good name found nothing to save. A failed save now puts the recording back, so stopping again saves what was recorded. --- crates/engine/src/cmd/macros.rs | 12 ++++++++- crates/engine/src/tests/macros.rs | 43 +++++++++++++++++++++++++++++++ docs/macros.md | 14 +++++----- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index e68dd92e69..953be19824 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -184,10 +184,20 @@ fn start_recording(s: &mut Session, relative: bool) -> Result { Ok(if relative { json!({"recording": true, "relative": true, "input": input}) } else { json!({"recording": true, "relative": false}) }) } -/// Stops recording and saves what was recorded as a macro. +/// Stops recording and saves what was recorded as a macro. When saving fails (a bad name, a full +/// workbook) the recording goes on, so stopping again keeps what was recorded. fn stop_recording(s: &mut Session, p: &Json) -> Result { let Some(rec) = s.stop_recording() else { return Ok(json!({"recording": false})) }; let start = s.macro_recording.take(); + let saved = save_recording(s, p, &rec, start.clone()); + if saved.is_err() { + s.recorder = Some(rec); + s.macro_recording = start; + } + saved +} + +fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, start: Option) -> Result { let d = s.doc()?; let start = start.unwrap_or_else(|| RecordingStart { doc: d.uid, diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 941abe9063..c32fa2d433 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -967,3 +967,46 @@ fn only_the_workbooks_a_debugged_run_touched_wait() { debug(&mut s, json!({"action": "stop"})); set(&mut s, "A1", "free"); } + +/// The commands of saved macro `name`, as `id params`. +fn recorded(s: &mut Session, name: &str) -> Vec { + let m = crate::macros::parse_macro(&s.execute("macro.get", json!({"name": name})).unwrap()).unwrap(); + crate::macros::command_steps(&m.steps).iter().map(|(_, id, p)| format!("{id} {p}")).collect() +} + +#[test] +fn a_recording_is_kept_when_saving_it_fails() { + let mut s = s(); + s.execute("macro.record", json!({"relative": false})).unwrap(); + set(&mut s, "A1", "typed"); + // A name the macro check refuses: still recording, and a good name saves what was recorded. + assert!(s.execute("macro.record", json!({"stop": true, "name": "Bad\u{1}name"})).is_err()); + assert!(s.is_recording()); + set(&mut s, "A2", "more"); + let r = s.execute("macro.record", json!({"stop": true, "name": "Good"})).unwrap(); + assert_eq!(r["saved"], "Good"); + assert!(!s.is_recording()); + assert_eq!(recorded(&mut s, "Good"), ["cell.set {\"cell\":\"A1\",\"input\":\"typed\"}", "cell.set {\"cell\":\"A2\",\"input\":\"more\"}"]); + + // A workbook at its macro limit: freeing a slot and stopping again saves it. + s.execute("macro.record", json!({"relative": false})).unwrap(); + set(&mut s, "B1", "full"); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + let filler = wb.macros[0].clone(); + while wb.macros.len() < gridcraft_model::MAX_MACROS { + let mut m = filler.clone(); + m.name = format!("Filler{}", wb.macros.len()); + wb.macros.push(m); + } + d.wb = Arc::new(wb); + let err = s.execute("macro.record", json!({"stop": true, "name": "Late"})).unwrap_err().to_string(); + assert!(err.contains("at most"), "{err}"); + assert!(s.is_recording()); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + wb.macros.pop(); + d.wb = Arc::new(wb); + s.execute("macro.record", json!({"stop": true, "name": "Late"})).unwrap(); + assert_eq!(recorded(&mut s, "Late"), ["cell.set {\"cell\":\"B1\",\"input\":\"full\"}"]); +} diff --git a/docs/macros.md b/docs/macros.md index 79e6c02c56..8587ca877d 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -119,12 +119,14 @@ of the step that just ran, and the cells changed so far. ## Recording `macro.record` toggles recording (`relative: false` for absolute references; `name` and -`description` when stopping). With relative references the selection when recording starts is the -example input: references in and near it become tokens, and the input's shape, header and column -types are inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so -playing them back sets them rather than flipping them. Commands a macro may not run are left out -and listed in the result; a macro run while recording is replaced by its steps when it has no -inputs, and an Automate script run by the script's steps. +`description` when stopping). When saving the recording fails (a name the check refuses, a workbook +at its macro limit), recording goes on, so stopping again keeps what was recorded. With relative +references the selection when recording starts is the example input: references in and near it +become tokens, and the input's shape, header and column types are inferred. Toggles such as Bold +are recorded with the state they set (`"on": true`), so playing them back sets them rather than +flipping them. Commands a macro may not run are left out and listed in the result; a macro run +while recording is replaced by its steps when it has no inputs, and an Automate script run by the +script's steps. ## Other commands and storage From 709a9952909e036b573cc34096dcaa815cbb6109 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:25:10 -0400 Subject: [PATCH 042/687] Model: one damaged macro no longer stops a JSON workbook opening Workbook::macros was read straight into typed macros, so one entry this build couldn't read (a step type from a later build, an unknown onConflict value, an entry that isn't an object) failed the whole file with "not a GridCraft JSON workbook". The field is now read as raw entries into Workbook::macros_read, and macros::load reads each one with parse_macro, as for xlsx: short-form steps are accepted, the depth limit is checked before deserializing, and a bad entry is dropped with the same warning. Only the first 1,000 entries are read, as in xlsx. --- crates/engine/src/macros/mod.rs | 11 ++++++++++- crates/engine/src/macros/tests.rs | 28 ++++++++++++++++++++++++++-- crates/model/src/macros.rs | 10 ++++++++++ crates/model/src/workbook.rs | 8 +++++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 155b9182f1..6b2150e66e 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -103,6 +103,15 @@ pub fn with_names(wb: &Workbook) -> crate::Result> { pub fn load(wb: &mut Workbook) -> Vec { let mut warnings = Vec::new(); let mut bad = 0usize; + // Entries of the JSON format's field, each read on its own so a damaged one drops alone. + let read = std::mem::take(&mut wb.macros_read); + bad = bad.saturating_add(read.len().saturating_sub(gridcraft_model::MAX_MACROS)); + for v in read.iter().take(gridcraft_model::MAX_MACROS) { + match parse_macro(v) { + Ok(m) => wb.macros.push(m), + Err(_) => bad += 1, + } + } // One pass takes the hidden macro names out of the workbook, grouped by macro in saved order. let mut stored: BTreeMap<(u64, String), Vec> = BTreeMap::new(); let mut kept = Vec::new(); @@ -126,7 +135,7 @@ pub fn load(wb: &mut Workbook) -> Vec { Err(_) => bad += 1, } } - // Macros that came as a field (the JSON format) go through the same checks. + // All of them go through the same limits, and a clashing name gets a suffix. let mut all = std::mem::take(&mut wb.macros); all.extend(found); for m in all { diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 189bf752ef..9d5b90a37d 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -168,9 +168,10 @@ fn damaged_or_oversized_macros_are_dropped_with_a_warning() { wb.macros[0].steps = vec![Step::command("x", json!({})); MAX_MACRO_STEPS + 1]; let bytes = crate::io::save_bytes(&wb, "many.json").unwrap(); let (back, warnings) = crate::io::open_bytes("many.json", &bytes).unwrap(); - assert_eq!(back.macros.len(), MAX_MACROS); + // Only the first MAX_MACROS entries are read, as in xlsx. + assert_eq!(back.macros.len(), MAX_MACROS - 1); assert_eq!(back.macros[0].name, "M1"); - assert!(warnings[0].starts_with("5 macros"), "{warnings:?}"); + assert!(warnings[0].starts_with("6 macros"), "{warnings:?}"); // A macro too large for the hidden names is refused, so a save never fails on it. let huge = "x".repeat(crate::cmd::stash::MAX_ITEM_CHARS); @@ -254,3 +255,26 @@ fn many_hidden_macro_names_are_read_in_one_pass() { assert!(warnings.len() == 1 && warnings[0].starts_with(&format!("{items} macros")), "{warnings:?}"); assert!(took < std::time::Duration::from_secs(3), "{took:?}"); } + +#[test] +fn a_damaged_json_macro_drops_alone() { + // One macro this build can't read (a newer step type, an unknown value, the wrong shape) is + // dropped with the usual warning; the workbook and its other macros open. + let mut wb = Workbook::new(); + wb.sheet_mut(0).unwrap().set_value(gridcraft_core::CellRef::new(0, 0), gridcraft_core::Value::text("kept")); + let mut v: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&wb, "book.json").unwrap()).unwrap(); + v["macros"] = json!([ + {"name": "Good"}, + {"name": "Newer", "steps": [{"type": "fromALaterBuild"}]}, + {"name": "Odd", "output": {"onConflict": "explode"}}, + {"name": "Short", "steps": [{"command": "cell.set", "params": {"cell": "A1", "input": "x"}}]}, + "not a macro" + ]); + let (back, warnings) = crate::io::open_bytes("book.json", v.to_string().as_bytes()).unwrap(); + let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect(); + assert_eq!(names, ["Good", "Short"]); + assert!(matches!(&back.macros[1].steps[0].kind, StepKind::Command { id, .. } if id == "cell.set")); + assert!(back.macros_read.is_empty()); + assert!(warnings.len() == 1 && warnings[0].starts_with("3 macros"), "{warnings:?}"); + assert_eq!(back.sheet(0).unwrap().value(gridcraft_core::CellRef::new(0, 0)), gridcraft_core::Value::text("kept")); +} diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs index caee00805f..39f7996159 100644 --- a/crates/model/src/macros.rs +++ b/crates/model/src/macros.rs @@ -119,6 +119,16 @@ impl Macro { } } +/// Reads the `macros` field of a JSON workbook as raw entries, whatever they hold, so they can be +/// checked one at a time. +pub(crate) fn entries<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { + Ok(match serde_json::Value::deserialize(d)? { + serde_json::Value::Array(list) => list, + serde_json::Value::Null => vec![], + other => vec![other], + }) +} + /// A range the macro works on, with the shape and content it expects. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] diff --git a/crates/model/src/workbook.rs b/crates/model/src/workbook.rs index b90186c40a..4ac73a1aed 100644 --- a/crates/model/src/workbook.rs +++ b/crates/model/src/workbook.rs @@ -115,8 +115,13 @@ pub struct Workbook { pub csv_delimiter: Option, /// GridCraft macros (typed command lists, not VBA), in the order the macro list shows them. /// The JSON format keeps them as this field; xlsx keeps them in hidden defined names. - #[serde(default, skip_serializing_if = "Vec::is_empty")] + #[serde(default, skip_serializing_if = "Vec::is_empty", skip_deserializing)] pub macros: Vec, + /// The `macros` entries of a JSON file as read, before the engine checks each one on its own + /// and moves it into `macros`, so one damaged or newer macro doesn't stop the file opening. + /// Never written. + #[serde(rename = "macros", default, skip_serializing, deserialize_with = "crate::macros::entries")] + pub macros_read: Vec, /// Unmodelled file content kept for saving (package root and workbook level). Not in JSON. #[serde(skip)] pub preserved: Option>, @@ -151,6 +156,7 @@ impl Workbook { vba_project: None, csv_delimiter: None, macros: vec![], + macros_read: vec![], preserved: None, source_dir: None, } From 7a0f5917127299b633373d1351ba31de4c2acc4c Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:30:33 -0400 Subject: [PATCH 043/687] Docs: macro tokens --- docs/macros.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index 1c755e3295..2860aa33ef 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -1,14 +1,13 @@ # Macros -> **Last updated:** 2026-10-21 · **Change:** major (created: the macro format and storage) +> **Last updated:** 2026-10-21 · **Change:** minor (macro tokens) GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro declares the inputs it works on. -This page describes the macro format and how macros are saved in a workbook. Step parameters -can hold tokens such as `Input` and `Output`; the engine does not resolve or run them in this -version. +This page describes the macro format, the tokens its steps use and how macros are saved in a +workbook. The engine has no command that runs macros in this version. ## A macro @@ -43,6 +42,26 @@ version. "Input column 2 (Amount) must hold numbers; C4 holds text." - **Limits**: 1,000 macros a workbook, 10,000 steps a macro, and Repeat and If nested 4 deep. +## Tokens + +References in step parameters can be tokens, worked out when the step runs: + +| Token | Means | +|---|---| +| `Input`, or another input's name | the range the input is bound to | +| `Output` | the output rule's anchor | +| `Row`, `Column` | the current row or column of the Repeat around the step | +| `Name:Total` | a defined name, or a table's data | + +Methods narrow or move a range and chain: `col(2)` or `col("Amount")`, `row(1)`, `cols(2..3)`, +`rows(2..)`, `cell(r,c)` (relative to the top-left cell; `cell(0,1)` is the cell above), +`offset(r,c)`, `resize(h,w)`, `data` (without the header row), `below(n)` and `right(n)`. + +Write a token as the whole value of a reference parameter (`cell`, `range`, `target`, keys ending +in `Cell` or `Range`…), as `{"$ref": "Input.col(2)"}` anywhere, or as `{{Input.col(2)}}` inside +any string, such as a formula. Plain A1 references stay absolute; a `Name:` anchor follows +inserted rows. + ## Storage Macros are saved in the workbook: as a field in GridCraft's JSON format, and in xlsx as hidden From bf456c9ff8465b0c9b65ea921b19316b49d73dc2 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:25:34 -0400 Subject: [PATCH 044/687] Engine: a macro's footprint dry runs keep to the run's timeLimit footprint always ran its dry run with the default 60 second limit, so macro.run with timeLimit 5 could run for over two minutes (a dry run, then another after a shift) and fail with "ran longer than 60 seconds". footprint now takes the run's clock and limit. make_way starts the run's clock before the dry runs, so the checks and the run share one limit, and macro.check takes timeLimit too. --- crates/engine/src/cmd/macros.rs | 2 +- crates/engine/src/macros/run.rs | 25 ++++++++++++++++--------- crates/engine/src/tests/macros.rs | 11 +++++++++++ docs/macros.md | 8 +++++--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index 2aba053b03..53856e7204 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -21,7 +21,7 @@ pub fn specs() -> Vec { "Check Macro", [], None, - "{name, inputs?} validates the inputs and dry-runs the macro on the workbook (then puts everything back) → {ok, errors?, needsApproval?, inputs, output?, conflicts, footprint}", + "{name, inputs?, timeLimit?: seconds} validates the inputs and dry-runs the macro on the workbook (then puts everything back) → {ok, errors?, needsApproval?, inputs, output?, conflicts, footprint}", has_doc, check ), diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 1f4f39f746..57a70562e5 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -346,13 +346,13 @@ fn holds(v: &Value, wanted: gridcraft_model::ColType) -> &'static str { /// A stopwatch for the time limit. `Instant::now` traps on wasm, so there it measures nothing. #[derive(Clone, Copy, Debug)] -struct Stopwatch { +pub struct Stopwatch { #[cfg(not(target_arch = "wasm32"))] start: std::time::Instant, } impl Stopwatch { - fn start() -> Stopwatch { + pub fn start() -> Stopwatch { #[cfg(not(target_arch = "wasm32"))] #[allow(clippy::disallowed_methods)] // never on wasm (see above) let w = Stopwatch { start: std::time::Instant::now() }; @@ -907,8 +907,8 @@ impl Saved { } /// Runs the macro in dry-run mode on its workbook, puts everything back, and says what it would -/// change. -pub fn footprint(s: &mut Session, p: &Prepared) -> Result { +/// change. The dry run stops once `time_limit_ms` has passed on `clock`. +pub fn footprint(s: &mut Session, p: &Prepared, clock: Stopwatch, time_limit_ms: f64) -> Result { let i = s.docs.iter().position(|d| d.uid == p.doc).ok_or(EngineError::NoDocument)?; s.set_active(i); let saved = Saved::take(s, p.doc); @@ -918,6 +918,7 @@ pub fn footprint(s: &mut Session, p: &Prepared) -> Result { d.touched = Some(Vec::new()); } let mut m = Machine::new(p, true); + (m.clock, m.time_limit_ms) = (clock, time_limit_ms); let outcome = m.run_to_end(s); let (after, touched) = match s.docs.iter_mut().find(|d| d.uid == p.doc) { Some(d) => (d.wb.clone(), d.touched.take().unwrap_or_default()), @@ -1107,14 +1108,15 @@ pub fn begin(s: &mut Session, name: &str, p: &Json) -> Result { approve(s, &m, doc, &asks, p.get("approve").and_then(Json::as_bool).unwrap_or(false))?; let prepared = prepare(s, &m, p.get("inputs"))?; let rule = conflict_rule(p, &m)?; - let time_limit_ms = p.get("timeLimit").and_then(Json::as_f64).map(|t| (t * 1000.0).clamp(1.0, 600_000.0)).unwrap_or(DEFAULT_TIME_LIMIT_MS); + let time_limit_ms = time_limit(p); let inputs = prepared.inputs_json(&s.doc()?.wb); Ok(Begun { mac: m, prepared, rule, time_limit_ms, inputs }) } /// The footprint check and the output rule, inside the run's undo group: an error when the macro /// would fail or write over data it may not, after shifting the data if the rule says so. Returns -/// the machine for the run and whether it shifted. +/// the machine for the run and whether it shifted. The dry runs count towards the run's time +/// limit. /// /// A partial footprint proves nothing about the steps it didn't reach, so then the machine /// checks each step as it runs and stops the run at one that writes over data. @@ -1125,7 +1127,7 @@ pub fn make_way(s: &mut Session, b: &Begun) -> Result<(Machine, bool)> { if rule == OnConflict::Overwrite { return Ok((machine, false)); } - let mut fp = footprint(s, &b.prepared)?; + let mut fp = footprint(s, &b.prepared, machine.clock, b.time_limit_ms)?; if let Some(e) = fp.error.take() { return Err(e.into_engine(&m.name)); } @@ -1144,7 +1146,7 @@ pub fn make_way(s: &mut Session, b: &Begun) -> Result<(Machine, bool)> { } OnConflict::ShiftDown | OnConflict::ShiftRight => { make_room(s, &fp, rule == OnConflict::ShiftDown)?; - fp = footprint(s, &b.prepared)?; + fp = footprint(s, &b.prepared, machine.clock, b.time_limit_ms)?; if let Some(e) = fp.error.take() { return Err(e.into_engine(&m.name)); } @@ -1191,6 +1193,11 @@ pub fn run(s: &mut Session, name: &str, p: &Json) -> Result { }) } +/// The run's `timeLimit` (seconds) in milliseconds, or the default. +fn time_limit(p: &Json) -> f64 { + p.get("timeLimit").and_then(Json::as_f64).map(|t| (t * 1000.0).clamp(1.0, 600_000.0)).unwrap_or(DEFAULT_TIME_LIMIT_MS) +} + /// The conflict rule for this run: `onConflict`, or `overwrite: true` (the answer to the /// question), else the macro's own. fn conflict_rule(p: &Json, m: &Macro) -> Result { @@ -1240,7 +1247,7 @@ pub fn check(s: &mut Session, name: &str, p: &Json) -> Result { if !asks.is_empty() && !approved { return Ok(out); } - let fp = footprint(s, &prepared)?; + let fp = footprint(s, &prepared, Stopwatch::start(), time_limit(p))?; out["ok"] = json!(fp.error.is_none()); out["conflicts"] = json!(fp.conflict_count()); if let Some(e) = &fp.error { diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 5f483e31dd..2855f11e68 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -356,6 +356,17 @@ fn macros_are_bounded() { assert!(err.contains("ran more than 100000 steps"), "{err}"); let err = s.execute("macro.run", json!({"name": "Spin", "timeLimit": 0.001})).unwrap_err().to_string(); assert!(err.contains("ran longer than"), "{err}"); + // The footprint's dry run keeps to the time limit too, and so does the check's. + put( + &mut s, + json!({"name": "Spin", "output": {"onConflict": "fail"}, "steps": [{"type": "repeat", "over": "A1:A400", "body": [ + {"type": "repeat", "over": "A1:A400", "body": [{"type": "if", "cell": "A1", "op": "empty"}]} + ]}]}), + ); + let err = s.execute("macro.run", json!({"name": "Spin", "timeLimit": 0.001})).unwrap_err().to_string(); + assert!(err.contains("ran longer than"), "{err}"); + let c = s.execute("macro.check", json!({"name": "Spin", "timeLimit": 0.001})).unwrap(); + assert!(c["errors"][0].as_str().unwrap().contains("ran longer than"), "{c}"); } #[test] diff --git a/docs/macros.md b/docs/macros.md index 2b17c6a50d..1e1b873fb6 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -42,7 +42,9 @@ no macro debugger, recorder or management commands in this version, and no macro `nonEmpty` and `defaultRef`. A run binds each input to the range it is given, its default, or (for the first input) the selection, and refuses with messages that name the cells: "Input column 2 (Amount) must hold numbers; C4 holds text." -- **Limits**: 1,000 macros a workbook, 10,000 steps a macro, and Repeat and If nested 4 deep. +- **Limits**: 1,000 macros a workbook, 10,000 steps a macro, Repeat and If nested 4 deep, + 100,000 steps executed per run, and 60 seconds a run, footprint checks included (`timeLimit` + to change; the web build has no clock, so there only the step limit applies). ## Tokens @@ -94,8 +96,8 @@ called Output, even when a token or input has that name. step: `Step 3.1 (cell.set): …` (paths are 1-based; `2.1` is inside a Repeat, `4.then.1` inside an If). -`macro.check {name, inputs?}` reports the bound inputs, the output anchor, any approval needed and -the footprint, without changing anything. +`macro.check {name, inputs?, timeLimit?}` reports the bound inputs, the output anchor, any +approval needed and the footprint, without changing anything. ## Storage From 45e580ad915389186636aae24697517fac8e398e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:16:29 -0400 Subject: [PATCH 045/687] Engine: macro.debug steps through a macro as one undo step macro.debug runs a macro one step at a time. start checks the macro as macro.run does (policy, approval, inputs, footprint and output rule) and pauses before the first step; step runs one step; continue runs to the next breakpoint; runTo runs to a chosen step path, stopping at breakpoints on the way; stop ends the run and keeps its changes or rolls them back; state reports where it is. Breakpoints are the steps' own flags plus a list of paths given with any action (breakpointsOnly makes the list replace the flags), and they stop in every pass of a Repeat. The run lives on the session between calls, inside an undo group that spans them (group_begin and group_end), so a debugged run is one undo step "Run Macro: ", as a normal run is. A failing step ends the run and rolls it back; so does a Stop step that asks for a rollback. While a macro is being debugged, edits of the workbooks the run touched (its own, and any other a step changed) are refused at the top level with "Finish or stop the macro first"; other workbooks stay editable, and selecting and queries still work. Every answer gives the next step's path, kind, command, parameters as they would run now (tokens resolved), the Repeat pass, the parameters of the step that just ran, the steps executed and the cells changed so far. Each action gets its own time limit. If a command panics while a step runs, the session's panic guard rolls the debugged run back as well, so its undo group never stays open. --- crates/engine/src/cmd/macros.rs | 11 +- crates/engine/src/lib.rs | 9 + crates/engine/src/macros/debug.rs | 394 ++++++++++++++++++++++++++++++ crates/engine/src/macros/mod.rs | 1 + crates/engine/src/macros/run.rs | 43 ++++ crates/engine/src/tests/macros.rs | 225 +++++++++++++++++ 6 files changed, 682 insertions(+), 1 deletion(-) create mode 100644 crates/engine/src/macros/debug.rs diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index 53856e7204..a14fc28429 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -1,5 +1,5 @@ //! Macros: typed command lists that run as one undo step. See [`crate::macros`] for the model, -//! tokens, runner and policy. Automate scripts (`automate.*`) are a separate, older feature. +//! tokens, runner, debugger and policy. Automate scripts (`automate.*`) are a separate, older feature. use serde_json::Value as Json; @@ -25,6 +25,15 @@ pub fn specs() -> Vec { has_doc, check ), + cmd!( + "macro.debug", + "Step Into", + [], + None, + "{action: start|step|continue|runTo|stop|state, name? and inputs?, onConflict?, approve? (start), step: path (runTo), keep?: bool (stop), breakpoints?: [\"3\", \"2.1\"], breakpointsOnly?: bool (the list replaces the steps' own breakpoints)} debugs a macro one step at a time as one undo step; edits of the workbooks it touched wait until it finishes or stops (other workbooks stay editable) → {state: paused|finished|stopped|failed, current: {step, kind, command?, params?, iteration?}, lastParams, executed, changed, atBreakpoint?, error?}", + has_doc, + |s, p| crate::macros::debug::debug(s, p) + ), ] } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index caac006bcb..3d542469b5 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -325,6 +325,8 @@ pub struct Session { /// Macros the person allowed to run in this session: (document uid, the macro's content as /// canonical JSON, from [`macros::fingerprint`]). pub macro_approvals: std::collections::HashSet<(u64, String)>, + /// The macro being debugged, between debugger actions. + pub macro_debug: Option, /// Windows hidden with View › Hide. pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. @@ -409,6 +411,12 @@ impl Session { if let Err(why) = (spec.enabled)(self) { return Err(EngineError::Disabled(id.to_string(), why)); } + // While a macro is being debugged its undo group is open: other edits of the workbooks it + // touched wait. Other workbooks stay editable. + let locked = self.active().is_some_and(|d| self.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid))); + if self.depth == 0 && spec.undoable && id != "macro.debug" && locked { + return Err(EngineError::Disabled(id.to_string(), macros::debug::BUSY.into())); + } let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); let params = if params.is_null() { Json::Object(Default::default()) } else { params }; let (first_group, group_epoch) = (self.next_group, self.group_epoch); @@ -436,6 +444,7 @@ impl Session { d.selection = sel.clone(); d.calc.rebuild(&d.wb); } + macros::debug::recover(self); if self.depth == 0 { self.trim_history(); } diff --git a/crates/engine/src/macros/debug.rs b/crates/engine/src/macros/debug.rs new file mode 100644 index 0000000000..1e599ab012 --- /dev/null +++ b/crates/engine/src/macros/debug.rs @@ -0,0 +1,394 @@ +//! The macro debugger: a run that stops between steps. +//! +//! `macro.debug` starts a run that pauses before its first step, then steps, continues to the next +//! breakpoint, runs to a chosen step, or stops. The run lives on the session between calls (one +//! action per call) inside an undo group that spans them, so a debugged run is still +//! one undo step; Stop keeps the changes or rolls them back. While a macro is being debugged, +//! other edits of the workbooks it touched (its own, and any other its steps changed) are refused +//! ("Finish or stop the macro first"); other workbooks stay editable. + +use std::collections::BTreeSet; +use std::sync::Arc; + +use gridcraft_model::Workbook; +use serde_json::{Value as Json, json}; + +use super::run::{Machine, StepError}; +use crate::{EngineError, GroupToken, Result, Session}; + +/// A macro being debugged. +pub struct Debugger { + pub name: String, + pub doc: u64, + /// Out of the session only while a step runs. + machine: Option, + token: Option, + /// Breakpoints set for this run (step paths), on top of the steps' own. + pub breakpoints: BTreeSet, + /// Only `breakpoints` stop the run; the steps' own flags are ignored (an editor that shows + /// its own breakpoints sends them all, so clearing one takes effect at once). + pub only_listed: bool, + /// The workbook when the run started, for "changed so far". + start: Arc, + /// Every step path in the macro, for checking `runTo` targets. + paths: BTreeSet, + /// The workbooks the run has touched (by uid): its own, and any other a step changed. Edits + /// of these wait until the run ends. + touched: BTreeSet, +} + +impl Debugger { + /// Whether edits of the workbook with this uid wait for the run to end. + pub fn locks(&self, doc: u64) -> bool { + self.touched.contains(&doc) + } +} + +/// Each open workbook's uid and its current version, to see which ones a step changes. +fn versions(s: &Session) -> Vec<(u64, Arc)> { + s.docs.iter().map(|d| (d.uid, d.wb.clone())).collect() +} + +/// Adds to the run's touched log the workbooks that changed since `before`. +fn note_touched(s: &mut Session, before: &[(u64, Arc)]) { + let changed: Vec = + s.docs.iter().filter(|d| before.iter().any(|(uid, wb)| *uid == d.uid && !Arc::ptr_eq(wb, &d.wb))).map(|d| d.uid).collect(); + if let Some(dbg) = s.macro_debug.as_mut() { + dbg.touched.extend(changed); + } +} + +/// The message for edits refused while a macro is being debugged. +pub const BUSY: &str = "Finish or stop the macro first"; + +enum Mode { + Step, + Continue, + RunTo(String), +} + +/// `macro.debug {name?, inputs?, action: start|step|continue|runTo|stop|state, step?, keep?, +/// breakpoints?}`. +pub fn debug(s: &mut Session, p: &Json) -> Result { + let action = match p.get("action") { + Some(Json::String(a)) => a.clone(), + // `{"action": {"runTo": {"step": "3"}}}` + Some(Json::Object(o)) => o.keys().next().cloned().unwrap_or_default(), + _ if s.macro_debug.is_some() => "state".into(), + _ => "start".into(), + }; + if action != "start" + && let (Some(d), Some(bps)) = (s.macro_debug.as_mut(), p.get("breakpoints")) + { + d.breakpoints = paths_param(bps)?; + if let Some(only) = p.get("breakpointsOnly").and_then(Json::as_bool) { + d.only_listed = only; + } + } + match action.as_str() { + "start" => start(s, p), + "step" => advance(s, Mode::Step), + "continue" => advance(s, Mode::Continue), + "runTo" => { + let target = p + .get("step") + .or_else(|| p.pointer("/action/runTo/step")) + .and_then(path_of) + .ok_or_else(|| bad("`runTo` needs `step`: the path of the step to stop at"))?; + let d = s.macro_debug.as_ref().ok_or_else(not_debugging)?; + if !d.paths.contains(&target) { + return Err(bad(format!("macro '{}' has no step {target}", d.name))); + } + advance(s, Mode::RunTo(target)) + } + "stop" => stop(s, p.get("keep").and_then(Json::as_bool).unwrap_or(false)), + "state" => state(s), + other => Err(bad(format!("action must be start, step, continue, runTo, stop or state, not '{other}'"))), + } +} + +fn bad(msg: impl Into) -> EngineError { + crate::cmd::bad("macro.debug", msg) +} + +fn not_debugging() -> EngineError { + EngineError::Other("No macro is being debugged. Start one with action: start.".into()) +} + +/// A step path from JSON: `"2.1"` or `3`. +fn path_of(v: &Json) -> Option { + match v { + Json::String(t) if !t.trim().is_empty() => Some(t.trim().to_string()), + Json::Number(n) => n.as_u64().map(|n| n.to_string()), + _ => None, + } +} + +fn paths_param(v: &Json) -> Result> { + let list = v.as_array().ok_or_else(|| bad("`breakpoints` must be a list of step paths"))?; + list.iter().take(10_000).map(|x| path_of(x).ok_or_else(|| bad("a breakpoint is a step path such as \"3\" or \"2.1\""))).collect() +} + +fn start(s: &mut Session, p: &Json) -> Result { + if let Some(d) = &s.macro_debug { + return Err(EngineError::Other(format!("Macro '{}' is being debugged. Stop it first.", d.name))); + } + let name = p.get("name").and_then(Json::as_str).filter(|n| !n.trim().is_empty()).ok_or_else(|| bad("missing `name`"))?; + let b = super::run::begin(s, name, p)?; + let breakpoints = match p.get("breakpoints") { + Some(v) => paths_param(v)?, + None => BTreeSet::new(), + }; + let start_wb = s.doc()?.wb.clone(); + let token = s.group_begin(&format!("Run Macro: {}", b.mac.name))?; + // The run's machine: it checks each step for writes over data when the footprint was partial. + let mut machine = match super::run::make_way(s, &b) { + Ok((machine, _)) => machine, + Err(e) => { + let _ = s.group_rollback(token); + return Err(e); + } + }; + machine.restart_clock(); + let paths = all_paths(&b.mac.steps, ""); + s.macro_debug = Some(Debugger { + name: b.mac.name.clone(), + doc: b.prepared.doc, + machine: Some(machine), + token: Some(token), + breakpoints, + only_listed: p.get("breakpointsOnly").and_then(Json::as_bool).unwrap_or(false), + start: start_wb, + paths, + touched: BTreeSet::from([b.prepared.doc]), + }); + if s.macro_debug.as_ref().and_then(|d| d.machine.as_ref()).is_some_and(Machine::is_done) { + return finish(s, None); + } + state(s) +} + +fn all_paths(steps: &[gridcraft_model::Step], prefix: &str) -> BTreeSet { + let mut out = BTreeSet::new(); + for (i, st) in steps.iter().enumerate() { + let path = if prefix.is_empty() { (i + 1).to_string() } else { format!("{prefix}.{}", i + 1) }; + match &st.kind { + gridcraft_model::StepKind::Repeat { body, .. } => out.extend(all_paths(body, &path)), + gridcraft_model::StepKind::If { then, otherwise, .. } => { + out.extend(all_paths(then, &format!("{path}.then"))); + out.extend(all_paths(otherwise, &format!("{path}.else"))); + } + _ => {} + } + out.insert(path); + } + out +} + +/// Runs one step (`Step`), or steps until a breakpoint, the target step or the end. +fn advance(s: &mut Session, mode: Mode) -> Result { + let d = s.macro_debug.as_mut().ok_or_else(not_debugging)?; + let breakpoints = d.breakpoints.clone(); + let only_listed = d.only_listed; + let mut machine = d.machine.take().ok_or_else(not_debugging)?; + machine.restart_clock(); + let before = versions(s); + let mut first = true; + let mut hit = false; + let mut outcome = Ok(()); + while !machine.is_done() { + if !first { + let Some((path, st)) = machine.current() else { break }; + if matches!(mode, Mode::Step) { + break; + } + if (st.breakpoint && !only_listed) || breakpoints.contains(&path) { + hit = true; + break; + } + if let Mode::RunTo(target) = &mode + && *target == path + { + break; + } + } + first = false; + outcome = machine.step(s); + if outcome.is_err() { + break; + } + } + let done = machine.is_done(); + if let Some(d) = s.macro_debug.as_mut() { + d.machine = Some(machine); + } + note_touched(s, &before); + if let Err(e) = outcome { + return finish(s, Some(e)); + } + if done { + return finish(s, None); + } + let mut out = state(s)?; + out["atBreakpoint"] = json!(hit); + Ok(out) +} + +/// Ends the run after its last step (kept as one undo step, unless a Stop step asked for a +/// rollback) or a failed step (rolled back). +fn finish(s: &mut Session, error: Option) -> Result { + let Some(mut d) = s.macro_debug.take() else { return Err(not_debugging()) }; + let machine = d.machine.take(); + let changed = changed(s, &d); + let stopped = machine.as_ref().and_then(|m| m.stopped.clone()); + let executed = machine.as_ref().map_or(0, |m| m.executed); + let rollback = error.is_some() || matches!(stopped, Some((_, true))); + if let Some(token) = d.token.take() { + // The group is gone only if its workbook was closed; then there is nothing to end. + let _ = if rollback { s.group_rollback(token) } else { s.group_end(token).map(|_| ()) }; + } + let mut out = json!({"macro": d.name, "executed": executed}); + match (&error, &stopped) { + (Some(e), _) => { + out["state"] = json!("failed"); + out["error"] = serde_json::to_value(e).unwrap_or(Json::Null); + out["message"] = json!(format!("{} Nothing was changed.", e.describe())); + } + (None, Some((msg, rolled))) => { + out["state"] = json!("stopped"); + out["stopped"] = json!(msg); + out["kept"] = json!(!rolled); + } + (None, None) => out["state"] = json!("finished"), + } + if !rollback { + out["changed"] = json!(changed); + } + Ok(out) +} + +/// Stops the run: keeps what it did as one undo step, or rolls it back. +fn stop(s: &mut Session, keep: bool) -> Result { + let Some(mut d) = s.macro_debug.take() else { return Err(not_debugging()) }; + let changed = changed(s, &d); + if let Some(token) = d.token.take() { + let _ = if keep { s.group_end(token).map(|_| ()) } else { s.group_rollback(token) }; + } + let executed = d.machine.as_ref().map_or(0, |m| m.executed); + let mut out = json!({"state": "stopped", "macro": d.name, "kept": keep, "executed": executed}); + if keep { + out["changed"] = json!(changed); + } + Ok(out) +} + +/// Where the run is: the next step with its parameters as they would run, the step that just +/// ran, and the cells changed so far. +fn state(s: &mut Session) -> Result { + let d = s.macro_debug.as_ref().ok_or_else(not_debugging)?; + let machine = d.machine.as_ref().ok_or_else(not_debugging)?; + let wb = s.docs.iter().find(|x| x.uid == d.doc).map(|x| x.wb.clone()).ok_or(EngineError::NoDocument)?; + Ok(json!({ + "state": "paused", + "macro": d.name, + "current": machine.current_json(&wb), + "lastParams": machine.last_params, + "executed": machine.executed, + "breakpoints": d.breakpoints, + "changed": changed(s, d), + })) +} + +/// The cells changed since the run started, as `Sheet!A1:B2` ranges. +fn changed(s: &Session, d: &Debugger) -> Vec { + let Some(now) = s.docs.iter().find(|x| x.uid == d.doc).map(|x| x.wb.clone()) else { return vec![] }; + let mut out = Vec::new(); + for sh in &now.sheets { + let Some(before) = d.start.sheet_index(&sh.name).and_then(|i| d.start.sheets.get(i)) else { + out.push(format!("{} (new sheet)", sh.name)); + continue; + }; + if Arc::ptr_eq(before, sh) { + continue; + } + let mut cells = before.cells.diff(&sh.cells); + let q = super::resolve::quote_sheet(&sh.name); + out.extend(super::run::ranges(&mut cells).into_iter().map(|r| format!("{q}!{r}"))); + } + out +} + +/// After a command panicked while a macro was being debugged: if the run lost its place, roll +/// it back so its undo group doesn't stay open. +pub(crate) fn recover(s: &mut Session) { + if s.macro_debug.as_ref().is_some_and(|d| d.machine.is_none()) + && let Some(mut d) = s.macro_debug.take() + && let Some(token) = d.token.take() + { + let _ = s.group_rollback(token); + } +} + +#[cfg(test)] +mod tests { + use serde_json::json; + + use crate::Session; + + #[test] + fn a_run_that_lost_its_place_is_rolled_back() { + let mut s = Session::new(); + s.new_workbook(); + let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "A1", "input": "x"}}, + {"command": "cell.set", "params": {"cell": "A2", "input": "y"}} + ]})) + .unwrap(); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + wb.macros.push(m); + d.wb = std::sync::Arc::new(wb); + let start = s.doc().unwrap().wb.clone(); + s.execute("macro.debug", json!({"name": "M"})).unwrap(); + s.execute("macro.debug", json!({"action": "step"})).unwrap(); + // As if a step had panicked with the machine out of the session. + s.macro_debug.as_mut().unwrap().machine = None; + super::recover(&mut s); + assert!(s.macro_debug.is_none()); + assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().wb, &start)); + assert!(!s.in_group(s.doc().unwrap().uid)); + s.execute("cell.set", json!({"cell": "B1", "input": "free"})).unwrap(); + } + + #[test] + fn a_workbook_in_the_touched_log_waits_until_the_run_ends() { + let mut s = Session::new(); + s.new_workbook(); + let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "A1", "input": "x"}}, + {"command": "cell.set", "params": {"cell": "A2", "input": "y"}} + ]})) + .unwrap(); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + wb.macros.push(m); + d.wb = std::sync::Arc::new(wb); + s.new_workbook(); + let other = s.doc().unwrap().uid; + s.set_active(0); + s.execute("macro.debug", json!({"name": "M"})).unwrap(); + assert!(!s.macro_debug.as_ref().unwrap().locks(other)); + // As if a step had changed the other workbook too. + let before = super::versions(&s); + let d = s.docs.iter_mut().find(|d| d.uid == other).unwrap(); + let mut wb = (*d.wb).clone(); + wb.props.title = "changed by the macro".into(); + d.wb = std::sync::Arc::new(wb); + super::note_touched(&mut s, &before); + assert!(s.macro_debug.as_ref().unwrap().locks(other)); + s.set_active(1); + assert!(s.execute("cell.set", json!({"cell": "B1", "input": "no"})).is_err()); + s.execute("macro.debug", json!({"action": "stop", "keep": true})).unwrap(); + s.execute("cell.set", json!({"cell": "B1", "input": "free"})).unwrap(); + } +} diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 457f109875..b25f06cb18 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -14,6 +14,7 @@ use serde_json::{Value as Json, json}; use crate::cmd::stash; +pub mod debug; pub mod policy; pub mod resolve; pub mod run; diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 36fd1ac417..02a90b9155 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -559,6 +559,49 @@ impl Machine { self.frames.is_empty() } + /// Starts the time limit again (the debugger gives each action its own). + pub fn restart_clock(&mut self) { + self.clock = Stopwatch::start(); + } + + /// The current step for the debugger: its path, kind, command, parameters as they would run + /// now, and the Repeat pass. + pub fn current_json(&self, wb: &Workbook) -> Json { + let Some((path, st)) = self.current() else { return Json::Null }; + let env = self.env(wb); + let mut out = json!({"step": path, "breakpoint": st.breakpoint}); + if !st.note.is_empty() { + out["note"] = json!(st.note); + } + match &st.kind { + StepKind::Command { id, params } => { + out["kind"] = json!("command"); + out["command"] = json!(id); + match resolve::resolve_params(params, &env) { + Ok(p) => out["params"] = p, + Err(e) => out["paramsError"] = json!(e.message()), + } + } + StepKind::Repeat { over, of, .. } => { + out["kind"] = json!("repeat"); + out["over"] = json!(resolve::resolve_ref(over, &env).map(|p| resolve::a1(p, &env)).unwrap_or_else(|e| e.message().to_string())); + out["of"] = json!(if *of == RepeatOf::Rows { "rows" } else { "columns" }); + } + StepKind::If { cell, .. } => { + out["kind"] = json!("if"); + out["cell"] = json!(resolve::resolve_ref(cell, &env).map(|p| resolve::a1(p, &env)).unwrap_or_else(|e| e.message().to_string())); + } + StepKind::Stop { message, .. } => { + out["kind"] = json!("stop"); + out["message"] = json!(message); + } + } + if let Some((at, count)) = self.iteration() { + out["iteration"] = json!({"at": at, "of": count}); + } + out + } + /// The step that runs next, with its path. pub fn current(&self) -> Option<(String, &Step)> { let f = self.frames.last()?; diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index a42a60dd7e..207e4c944c 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -409,6 +409,27 @@ fn a_token_in_a_path_needs_the_permission_and_approval() { let _ = std::fs::remove_dir_all(&dir); } +#[test] +fn the_debugger_runs_a_token_path_only_with_the_permission_and_approval() { + let mut s = s(); + let dir = std::env::temp_dir().join(format!("gc-macro-debug-token-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let files = |dir: &std::path::Path| std::fs::read_dir(dir).unwrap().count(); + let step = json!({"command": "file.exportCsv", "params": {"path": format!("{}/{{{{Input}}}}", dir.to_string_lossy())}}); + put(&mut s, json!({"name": "Export", "inputs": [{"name": "Input"}], "steps": [step]})); + let err = s.execute("macro.debug", json!({"action": "start", "name": "Export"})).unwrap_err().to_string(); + assert!(err.contains("writes a file"), "{err}"); + put(&mut s, json!({"name": "Export", "inputs": [{"name": "Input"}], "steps": [step], "permissions": ["file.exportCsv"]})); + assert!(matches!(s.execute("macro.debug", json!({"action": "start", "name": "Export"})), Err(EngineError::Approve(_)))); + assert_eq!(files(&dir), 0); + // Approved, it pauses before the step and writes the file only when the step runs. + s.execute("macro.debug", json!({"action": "start", "name": "Export", "approve": true, "inputs": "B2", "breakpoints": ["1"]})).unwrap(); + assert_eq!(files(&dir), 0, "paused before the step"); + s.execute("macro.debug", json!({"action": "step"})).unwrap(); + assert_eq!(files(&dir), 1, "the debugger runs the approved step"); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn the_policy_keeps_macros_away_from_files_and_the_app() { let mut s = s(); @@ -556,3 +577,207 @@ fn a_partial_footprint_doesnt_clear_later_writes() { assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start), "the shift is rolled back too"); let _ = std::fs::remove_dir_all(&dir); } + +// ---------------------------------------------------------------- debugger + +fn debug(s: &mut Session, p: Json) -> Json { + s.execute("macro.debug", p).unwrap() +} + +/// Four steps: two plain ones, a Repeat over the data rows (with a breakpoint on its body), and +/// a final one. +fn stepper(s: &mut Session) { + data(s); + put( + s, + json!({"name": "Walk", "inputs": [{"name": "Input", "header": true}], "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "E1", "input": "start"}}, + {"command": "cell.set", "params": {"cell": "Input.right.cell(1,1)", "input": "flag"}}, + {"type": "repeat", "over": "Input.data", "body": [ + {"command": "cell.set", "params": {"cell": "Row.right", "input": "seen"}, "breakpoint": true} + ]}, + {"command": "cell.set", "params": {"cell": "E9", "input": "end"}} + ]}), + ); +} + +#[test] +fn the_debugger_steps_continues_and_runs_to_a_step_as_one_undo_step() { + let mut s = s(); + stepper(&mut s); + let undo = undo_labels(&s); + let st = debug(&mut s, json!({"name": "Walk", "inputs": "B2:C5", "action": "start"})); + assert_eq!(st["state"], "paused"); + assert_eq!( + st["current"], + json!({"step": "1", "kind": "command", "command": "cell.set", "params": {"cell": "E1", "input": "start"}, "breakpoint": false}) + ); + assert_eq!(st["executed"], 0); + + let st = debug(&mut s, json!({"action": "step"})); + assert_eq!(st["current"]["step"], "2"); + assert_eq!(st["current"]["params"]["cell"], "D2", "tokens resolve for the next step"); + assert_eq!(st["lastParams"], json!({"cell": "E1", "input": "start"})); + assert_eq!(st["changed"], json!(["Sheet1!E1"])); + assert_eq!(v(&s, "E1").display(), "start"); + + // Other edits wait; selecting cells and queries don't. + let r = s.execute("cell.set", json!({"cell": "A1", "input": "x"})); + assert!(matches!(&r, Err(EngineError::Disabled(_, why)) if why == "Finish or stop the macro first"), "{r:?}"); + assert!(s.execute("macro.run", json!({"name": "Walk"})).is_err()); + assert!(s.execute("edit.undo", json!({})).is_err()); + s.execute("selection.set", json!({"range": "A1"})).unwrap(); + assert!(s.execute("macro.debug", json!({"name": "Walk", "action": "start"})).unwrap_err().to_string().contains("is being debugged")); + + // Continue stops at the breakpoint inside the Repeat, once per row. + let st = debug(&mut s, json!({"action": "continue"})); + assert_eq!((st["current"]["step"].as_str(), st["atBreakpoint"].as_bool()), (Some("3.1"), Some(true))); + assert_eq!(st["current"]["iteration"], json!({"at": 1, "of": 3})); + assert_eq!(st["current"]["params"]["cell"], "D3"); + let st = debug(&mut s, json!({"action": "continue"})); + assert_eq!(st["current"]["iteration"], json!({"at": 2, "of": 3})); + // Run To stops at a breakpoint on the way, then at its step. + let st = debug(&mut s, json!({"action": "runTo", "step": "4", "breakpoints": []})); + assert_eq!((st["current"]["step"].as_str(), st["atBreakpoint"].as_bool()), (Some("3.1"), Some(true))); + assert_eq!(st["current"]["iteration"], json!({"at": 3, "of": 3})); + let st = debug(&mut s, json!({"action": "runTo", "step": "4"})); + assert_eq!((st["current"]["step"].as_str(), st["atBreakpoint"].as_bool()), (Some("4"), Some(false))); + assert_eq!((v(&s, "D4").display(), v(&s, "D5").display()), ("seen".into(), "seen".into())); + // Breakpoints from the call: none left before the end. + let st = debug(&mut s, json!({"action": "continue"})); + assert_eq!(st["state"], "finished"); + assert_eq!(st["executed"], 7); + assert!(st["changed"].as_array().unwrap().contains(&json!("Sheet1!E9"))); + assert_eq!(v(&s, "E9").display(), "end"); + let after = undo_labels(&s); + assert_eq!(after.len(), undo.len() + 1, "{after:?}"); + assert_eq!(after.last().unwrap(), "Run Macro: Walk"); + // Edits work again; one undo takes the whole run back. + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "E1").is_empty() && v(&s, "D3").is_empty() && v(&s, "E9").is_empty()); + set(&mut s, "A1", "free"); +} + +#[test] +fn a_breakpoint_list_can_replace_the_steps_own() { + let mut s = s(); + stepper(&mut s); + // Only step 4 stops the run: the flag on 3.1 is ignored. + let st = debug(&mut s, json!({"name": "Walk", "inputs": "B2:C5", "breakpoints": ["4"], "breakpointsOnly": true})); + assert_eq!(st["current"]["step"], "1"); + let st = debug(&mut s, json!({"action": "continue"})); + assert_eq!((st["current"]["step"].as_str(), st["atBreakpoint"].as_bool()), (Some("4"), Some(true))); + debug(&mut s, json!({"action": "stop"})); + // Clearing the list mid-run (an editor removing its last breakpoint) runs to the end. + debug(&mut s, json!({"name": "Walk", "inputs": "B2:C5", "breakpoints": ["3.1"], "breakpointsOnly": true})); + let st = debug(&mut s, json!({"action": "continue"})); + assert_eq!(st["current"]["step"], "3.1"); + let st = debug(&mut s, json!({"action": "continue", "breakpoints": [], "breakpointsOnly": true})); + assert_eq!(st["state"], "finished"); +} + +#[test] +fn stopping_the_debugger_keeps_or_rolls_back() { + for keep in [false, true] { + let mut s = s(); + stepper(&mut s); + let (start, undo) = (s.doc().unwrap().wb.clone(), undo_labels(&s)); + debug(&mut s, json!({"name": "Walk", "inputs": "B2:C5", "breakpoints": ["2"]})); + let st = debug(&mut s, json!({"action": "continue"})); + assert_eq!((st["current"]["step"].as_str(), st["atBreakpoint"].as_bool()), (Some("2"), Some(true))); + let st = debug(&mut s, json!({"action": "stop", "keep": keep})); + assert_eq!((st["state"].as_str(), st["kept"].as_bool()), (Some("stopped"), Some(keep))); + if keep { + assert_eq!(v(&s, "E1").display(), "start"); + assert_eq!(undo_labels(&s).len(), undo.len() + 1); + assert_eq!(st["changed"], json!(["Sheet1!E1"])); + } else { + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); + assert_eq!(undo_labels(&s), undo); + } + set(&mut s, "A1", "free"); + assert!(s.execute("macro.debug", json!({"action": "step"})).unwrap_err().to_string().contains("No macro is being debugged")); + } +} + +#[test] +fn a_debugged_run_after_a_partial_footprint_stops_before_writing_over_data() { + let mut s = s(); + set(&mut s, "A1", "1"); + set(&mut s, "C1", "=A1"); + set(&mut s, "B1", "keep"); + let solver = json!({"command": "data.solver", "params": {"objective": "C1", "variables": "A1", "constraints": ["A1 <= 5"], "method": "simplex", "saveModel": false}}); + let late = json!({"command": "cell.set", "params": {"cell": "B1", "input": "over"}}); + put(&mut s, json!({"name": "Late", "inputs": [{"name": "Input"}], "output": {"onConflict": "fail"}, "steps": [solver, late]})); + let start = s.doc().unwrap().wb.clone(); + debug(&mut s, json!({"name": "Late", "action": "start", "inputs": "A1"})); + debug(&mut s, json!({"action": "step"})); + let st = debug(&mut s, json!({"action": "step"})); + assert_eq!(st["state"], "failed", "{st}"); + assert!(st["message"].as_str().unwrap().contains("It would write over data (B1 on Sheet1)"), "{st}"); + assert_eq!(v(&s, "B1").display(), "keep"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); +} + +#[test] +fn a_failing_step_ends_the_debugged_run_and_rolls_it_back() { + let mut s = s(); + data(&mut s); + put( + &mut s, + json!({"name": "Breaks", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "A1", "input": "one"}}, + {"command": "cell.set", "params": {"cell": "Nowhere!A2", "input": "two"}}, + {"type": "stop", "message": "never"} + ]}), + ); + let start = s.doc().unwrap().wb.clone(); + debug(&mut s, json!({"name": "Breaks"})); + assert!(s.execute("macro.debug", json!({"action": "runTo", "step": "9"})).unwrap_err().to_string().contains("has no step 9")); + assert!(s.execute("macro.debug", json!({"action": "leap"})).is_err()); + debug(&mut s, json!({"action": "step"})); + let st = debug(&mut s, json!({"action": "step"})); + assert_eq!(st["state"], "failed"); + assert_eq!(st["error"]["step"], "2"); + assert!(st["message"].as_str().unwrap().starts_with("Step 2 (cell.set):"), "{st}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); + set(&mut s, "B1", "free again"); + // A run that ends at once (a Stop first) finishes on start. + put(&mut s, json!({"name": "Quick", "steps": [{"type": "stop", "message": "bye"}]})); + let st = debug(&mut s, json!({"name": "Quick"})); + assert_eq!(st["state"], "paused"); + let st = debug(&mut s, json!({"action": "step"})); + assert_eq!((st["state"].as_str(), st["stopped"].as_str()), (Some("stopped"), Some("bye"))); + put(&mut s, json!({"name": "Empty", "steps": []})); + assert_eq!(debug(&mut s, json!({"name": "Empty"}))["state"], "finished"); +} + +#[test] +fn only_the_workbooks_a_debugged_run_touched_wait() { + let mut s = s(); + stepper(&mut s); + debug(&mut s, json!({"name": "Walk", "inputs": "B2:C5"})); + debug(&mut s, json!({"action": "step"})); + // Another workbook stays editable, macros included. + s.new_workbook(); + set(&mut s, "A1", "elsewhere"); + put( + &mut s, + json!({"name": "Here", "output": {"onConflict": "overwrite"}, "steps": [{"command": "cell.set", "params": {"cell": "A2", "input": "ran"}}]}), + ); + s.execute("macro.run", json!({"name": "Here"})).unwrap(); + assert_eq!(v(&s, "A2").display(), "ran"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "A2").is_empty()); + // The debugged one waits. + s.set_active(0); + let r = s.execute("cell.set", json!({"cell": "A1", "input": "x"})); + assert!(matches!(&r, Err(EngineError::Disabled(_, why)) if why == "Finish or stop the macro first"), "{r:?}"); + // The run carries on in its own workbook whichever one is in front. + s.set_active(1); + let st = debug(&mut s, json!({"action": "continue", "breakpoints": []})); + assert_eq!(st["state"], "paused"); + assert_eq!(s.active_index(), 0); + debug(&mut s, json!({"action": "stop"})); + set(&mut s, "A1", "free"); +} From c6dda8562f81241d5dcb109e9036208e19ac0aa2 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:24:35 -0400 Subject: [PATCH 046/687] Engine: macro recorder keeps references on other sheets as they are The recorder made every unqualified reference relative to the example input, whatever sheet the step ran on. For example, starting a recording on Sheet1 with B2:C5 selected, activating Sheet2 and setting C4 recorded Input.cell(3,2), so playback wrote Sheet1 instead; a formula entered on Sheet2 was changed to refer to Sheet1's cells. Each step now keeps the sheet it ran on, and only references on the input's sheet become tokens. --- crates/engine/src/macros/recorder.rs | 62 ++++++++++++++++------------ crates/engine/src/tests/macros.rs | 35 ++++++++++++++++ docs/macros.md | 10 ++--- 3 files changed, 76 insertions(+), 31 deletions(-) diff --git a/crates/engine/src/macros/recorder.rs b/crates/engine/src/macros/recorder.rs index 56a64a72f5..b154898735 100644 --- a/crates/engine/src/macros/recorder.rs +++ b/crates/engine/src/macros/recorder.rs @@ -9,9 +9,9 @@ //! - a `sheet.activate` or `selection.set` step is added where the sheet or selection changed //! between commands without one, and runs of `selection.set` collapse to the last; //! - with relative references (the default), the selection when recording started is the example -//! input: references in, next to or near it become tokens (`Input.col(2)`, `Input.below`, -//! `Input.cell(1,4)`), also inside formulas (`{{Input.col(2)}}`), and the input's shape, -//! header and column types are inferred from it. +//! input: references on its sheet in, next to or near it become tokens (`Input.col(2)`, +//! `Input.below`, `Input.cell(1,4)`), also inside formulas (`{{Input.col(2)}}`), and the input's +//! shape, header and column types are inferred from it. use gridcraft_core::{CellRef, RangeRef, Value}; use gridcraft_model::{ColType, InputKind, Macro, NamedInput, Step, Workbook}; @@ -85,39 +85,42 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & // Sheet and selection changes made without a command: where a command ran on another sheet, // or with another selection, than the one before it, say so (a command that moved the // selection itself does it again when played back, so the extra step is harmless). - let mut out: Vec<(String, Json, bool)> = Vec::new(); + // Each step keeps the name of the sheet it runs on. + let mut out: Vec<(String, Json, String, bool)> = Vec::new(); let (mut sheet, mut sel) = (start.sheet, start.selection.clone()); for (id, params, at_sheet, sheet_name, at_sel, kept) in cmds { if at_sheet != sheet { - out.push(("sheet.activate".into(), json!({"sheet": sheet_name}), false)); + out.push(("sheet.activate".into(), json!({"sheet": sheet_name}), sheet_name.clone(), false)); } // A recorded selection.set just before already put the selection there. let selected = matches!(out.last(), Some((last, ..)) if last == "selection.set"); if (at_sel != sel || at_sheet != sheet) && !selected { - out.push(("selection.set".into(), selection_params(&at_sel), false)); + out.push(("selection.set".into(), selection_params(&at_sel), sheet_name.clone(), false)); } (sheet, sel) = (at_sheet, at_sel); - out.push((id, params, kept)); + out.push((id, params, sheet_name, kept)); } // Runs of selection.set (or sheet.activate): the last one wins. - let mut collapsed: Vec<(String, Json, bool)> = Vec::new(); - for (id, params, kept) in out { + let mut collapsed: Vec<(String, Json, String, bool)> = Vec::new(); + for (id, params, on, kept) in out { if matches!(id.as_str(), "selection.set" | "sheet.activate") && let Some((last, ..)) = collapsed.last() && *last == id { collapsed.pop(); } - collapsed.push((id, params, kept)); + collapsed.push((id, params, on, kept)); } - // Relative references. + // Relative references. Only references on the input's sheet become tokens: on another sheet + // `C4` is that sheet's C4. let example = start.selection.current(); let mut used_input = false; let steps: Vec = collapsed .into_iter() - .map(|(id, params, kept)| { - let params = if start.relative && !kept { relative(¶ms, example, &start.sheet_name, &mut used_input, None) } else { params }; + .map(|(id, params, on, kept)| { + let here = on.eq_ignore_ascii_case(&start.sheet_name); + let params = if start.relative && !kept { relative(¶ms, example, &start.sheet_name, here, &mut used_input, None) } else { params }; Step::command(id, params) }) .collect(); @@ -133,20 +136,21 @@ fn selection_params(sel: &Selection) -> Json { json!({"range": ranges.join(","), "active": sel.active.a1()}) } -/// `params` with references near `example` made into tokens. -fn relative(v: &Json, example: RangeRef, sheet: &str, used: &mut bool, key: Option<&str>) -> Json { +/// `params` with references near `example` (on `sheet`) made into tokens; `here` says whether the +/// step runs on that sheet, so its unqualified references are on it. +fn relative(v: &Json, example: RangeRef, sheet: &str, here: bool, used: &mut bool, key: Option<&str>) -> Json { match v { Json::Object(o) => { let mut out = Map::new(); for (k, x) in o { - out.insert(k.clone(), relative(x, example, sheet, used, Some(k))); + out.insert(k.clone(), relative(x, example, sheet, here, used, Some(k))); } Json::Object(out) } - Json::Array(a) => Json::Array(a.iter().map(|x| relative(x, example, sheet, used, key)).collect()), + Json::Array(a) => Json::Array(a.iter().map(|x| relative(x, example, sheet, here, used, key)).collect()), Json::String(t) if key.is_some_and(super::resolve::is_ref_key) => { // A list of areas ("A1:B2,D4") stays as it is unless every part has a token. - let parts: Option> = t.split(',').map(|part| ref_token(part, example, sheet)).collect(); + let parts: Option> = t.split(',').map(|part| ref_token(part, example, sheet, here)).collect(); match parts { Some(p) if p.len() == 1 => { *used = true; @@ -155,7 +159,7 @@ fn relative(v: &Json, example: RangeRef, sheet: &str, used: &mut bool, key: Opti _ => v.clone(), } } - Json::String(t) if t.starts_with('=') && example.count() > 1 => { + Json::String(t) if here && t.starts_with('=') && example.count() > 1 => { let (f, changed) = formula_tokens(t, example); *used |= changed; json!(f) @@ -164,11 +168,14 @@ fn relative(v: &Json, example: RangeRef, sheet: &str, used: &mut bool, key: Opti } } -/// The token for an A1 reference (on the example's sheet) near the example input. -fn ref_token(text: &str, example: RangeRef, sheet: &str) -> Option { +/// The token for an A1 reference on the example's sheet near the example input. `here` says whether +/// an unqualified reference is on that sheet. +fn ref_token(text: &str, example: RangeRef, sheet: &str, here: bool) -> Option { let (on, body) = crate::cmd::split_sheet(text.trim()); - if on.is_some_and(|s| !s.eq_ignore_ascii_case(sheet)) { - return None; + match on { + Some(on) if !on.eq_ignore_ascii_case(sheet) => return None, + None if !here => return None, + _ => {} } let t = RangeRef::parse(body)?; if t.is_full_cols() || t.is_full_rows() { @@ -367,10 +374,13 @@ mod tests { ("Z500", None), ("A:A", None), ] { - assert_eq!(ref_token(t, ex, "Sheet1").as_deref(), want, "{t}"); + assert_eq!(ref_token(t, ex, "Sheet1", true).as_deref(), want, "{t}"); } - assert_eq!(ref_token("Sheet1!C4", ex, "Sheet1").as_deref(), Some("Input.cell(3,2)")); - assert_eq!(ref_token("Other!C4", ex, "Sheet1"), None); + assert_eq!(ref_token("Sheet1!C4", ex, "Sheet1", true).as_deref(), Some("Input.cell(3,2)")); + assert_eq!(ref_token("Other!C4", ex, "Sheet1", true), None); + // On another sheet only a reference qualified with the input's sheet is on it. + assert_eq!(ref_token("C4", ex, "Sheet1", false), None); + assert_eq!(ref_token("Sheet1!C4", ex, "Sheet1", false).as_deref(), Some("Input.cell(3,2)")); } #[test] diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index c32fa2d433..5b0b2ddbb0 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1010,3 +1010,38 @@ fn a_recording_is_kept_when_saving_it_fails() { s.execute("macro.record", json!({"stop": true, "name": "Late"})).unwrap(); assert_eq!(recorded(&mut s, "Late"), ["cell.set {\"cell\":\"B1\",\"input\":\"full\"}"]); } + +#[test] +fn a_relative_recording_keeps_references_on_another_sheet() { + let mut s = s(); + s.execute("home.insertSheet", json!({"name": "Two"})).unwrap(); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + data(&mut s); + s.execute("selection.set", json!({"range": "B2:C5"})).unwrap(); + s.execute("macro.record", json!({})).unwrap(); + s.execute("home.bold", json!({"range": "B2:C2"})).unwrap(); + // On the other sheet, C4 and C3:C5 are that sheet's cells, not the input's. + s.execute("sheet.activate", json!({"sheet": "Two"})).unwrap(); + set(&mut s, "C4", "x"); + set(&mut s, "D2", "=SUM(C3:C5)"); + s.execute("macro.record", json!({"stop": true, "name": "Both"})).unwrap(); + let steps = recorded(&mut s, "Both"); + assert!(steps.contains(&"cell.set {\"cell\":\"C4\",\"input\":\"x\"}".to_string()), "{steps:?}"); + assert!(steps.contains(&"cell.set {\"cell\":\"D2\",\"input\":\"=SUM(C3:C5)\"}".to_string()), "{steps:?}"); + // Played back on another block of Sheet1, it writes Two's cells. + set(&mut s, "C4", ""); + set(&mut s, "D2", ""); + set(&mut s, "C3", "5"); + set(&mut s, "C5", "7"); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + for (cell, input) in [("G10", "Who"), ("H10", "Paid"), ("G11", "A"), ("H11", "100"), ("G12", "B"), ("H12", "200"), ("G13", "C"), ("H13", "300")] { + set(&mut s, cell, input); + } + s.execute("macro.run", json!({"name": "Both", "inputs": "G10:H13", "onConflict": "overwrite"})).unwrap(); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + assert!(bold(&s, "G10") && bold(&s, "H10")); + // Nothing was written into or beside the input. + assert_eq!((v(&s, "H12").display(), v(&s, "I10").display()), ("200".into(), String::new())); + s.execute("sheet.activate", json!({"sheet": "Two"})).unwrap(); + assert_eq!((v(&s, "C4").display(), v(&s, "D2").display()), ("x".into(), "12".into())); +} diff --git a/docs/macros.md b/docs/macros.md index 8587ca877d..6a30a1d56a 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -122,11 +122,11 @@ of the step that just ran, and the cells changed so far. `description` when stopping). When saving the recording fails (a name the check refuses, a workbook at its macro limit), recording goes on, so stopping again keeps what was recorded. With relative references the selection when recording starts is the example input: references in and near it -become tokens, and the input's shape, header and column types are inferred. Toggles such as Bold -are recorded with the state they set (`"on": true`), so playing them back sets them rather than -flipping them. Commands a macro may not run are left out and listed in the result; a macro run -while recording is replaced by its steps when it has no inputs, and an Automate script run by the -script's steps. +on its sheet become tokens (on another sheet `C4` stays that sheet's `C4`), and the input's shape, +header and column types are inferred. Toggles such as Bold are recorded with the state they set +(`"on": true`), so playing them back sets them rather than flipping them. Commands a macro may not +run are left out and listed in the result; a macro run while recording is replaced by its steps +when it has no inputs, and an Automate script run by the script's steps. ## Other commands and storage From ee7040a340e01ff50428c99b3531a7b6653694fc Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:29:54 -0400 Subject: [PATCH 047/687] Docs: macro format and storage --- docs/macros.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 docs/macros.md diff --git a/docs/macros.md b/docs/macros.md new file mode 100644 index 0000000000..99050b0aa5 --- /dev/null +++ b/docs/macros.md @@ -0,0 +1,60 @@ +# Macros + +> **Last updated:** 2026-10-21 · **Change:** major (created: the macro format and storage) + +GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft +command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro +declares the inputs it works on. + +This page describes the macro format and how macros are saved in a workbook. Step parameters +can hold tokens such as `Input` and `Output`; the engine does not resolve or run them in this +version. + +## A macro + +```json +{ + "name": "Totals", + "description": "Adds a total next to the amounts", + "inputs": [{"name": "Input", "header": true, "cols": [2, 2], "colTypes": ["text", "number"]}], + "output": {"anchor": "Input.right(2).cell(1,2)", "onConflict": "ask"}, + "steps": [ + {"type": "command", "id": "cell.set", "params": {"cell": "Output", "input": "Total"}}, + {"type": "command", "id": "cell.set", "params": {"cell": "Output.offset(1,0)", "input": "=SUM({{Input.data.col(2)}})"}}, + {"type": "repeat", "over": "Input.data", "of": "rows", "max": 1000, "body": [ + {"type": "if", "cell": "Row.cell(1,2)", "op": "gt", "value": 100, + "then": [{"type": "command", "id": "home.bold", "params": {"range": "Row"}}]} + ]}, + {"type": "stop", "message": "Done."} + ], + "permissions": [] +} +``` + +- **Steps**: `command` (`id`, `params`), `repeat` (each row or column of `over`, at most `max` + times), `if` (`cell` compared with `value` by `eq`, `ne`, `gt`, `ge`, `lt`, `le`, `empty`, + `notEmpty` or `contains`; `then` and `else` lists) and `stop` (`message`; `rollback: true` + undoes the run). Every step can carry `enabled`, `breakpoint` and `note`. The short form + `{"command": "cell.set", "params": {…}}` is accepted for a command step. +- **Inputs**: `kind` (`cell` or `range`), `rows` and `cols` as `[min, max]` (0 for no maximum; + rows exclude the header), `header`, `colTypes` (`number`, `text`, `date`, `bool`, `any`), + `nonEmpty` and `defaultRef`. A run binds each input to the range it is given, its default, or + (for the first input) the selection, and refuses with messages that name the cells: + "Input column 2 (Amount) must hold numbers; C4 holds text." +- **Limits**: 1,000 macros a workbook, 10,000 steps a macro, and Repeat and If nested 4 deep. + +## Storage + +Macros are saved in the workbook: as a field in GridCraft's JSON format, and in xlsx as hidden +defined names (`_gc_macro__`, the macro's JSON split into string literals within Excel's +limits), which other spreadsheet applications keep but do not run. + +## Automate scripts and VBA + +Automate scripts (`automate.recordActions`, `newScript`, `allScripts`, `runScript`, +`deleteScript`) are an older, separate feature. They keep their commands, parameters, storage +(`_sc_scripts`) and results, and View › Macros (`view.macros`) still lists them. Macros neither +read nor change them; a workbook can hold a script and a macro with the same name. + +A VBA project in an `.xlsm` or `.xltm` file is preserved on save and never run. GridCraft macros +are stored apart from it. From ac7c1ae639ec6ced58fec5f9b88e6ea01e2fb19e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:26:37 -0400 Subject: [PATCH 048/687] Engine: a reference on a sheet named like a token stays a reference is_token looked only at the leading identifier, so on a workbook with a sheet called Output (or Row, Column, or the name of a declared input), "Output!A1:B2" was read as the Output token, and the step failed with "the macro has no output anchor" or "unexpected '!A1:B2'". This hit reference keys, $ref and {{...}} alike, including references the recorder writes for other sheets. A base followed by '!' is now a sheet name, and the text resolves as a plain reference. --- crates/engine/src/macros/resolve.rs | 36 ++++++++++++++++++++++++++++- docs/macros.md | 3 ++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs index 8abf68ad4b..e8c7b6613f 100644 --- a/crates/engine/src/macros/resolve.rs +++ b/crates/engine/src/macros/resolve.rs @@ -110,7 +110,8 @@ pub fn is_ref_key(key: &str) -> bool { /// The most nesting [`resolve_params`] walks into. const MAX_JSON_DEPTH: usize = 64; -/// Whether `text` starts like a token (its base is one this environment knows). +/// Whether `text` starts like a token (its base is one this environment knows). A reference on a +/// sheet with such a name (`Output!A1`) is not a token. pub fn is_token(text: &str, env: &Env) -> bool { let t = text.trim(); if t.get(..5).is_some_and(|h| h.eq_ignore_ascii_case("name:")) { @@ -118,6 +119,9 @@ pub fn is_token(text: &str, env: &Env) -> bool { } let n: usize = t.chars().take_while(|c| c.is_alphanumeric() || *c == '_').map(char::len_utf8).sum(); let base = t.get(..n).unwrap_or(""); + if t.get(n..).unwrap_or("").trim_start().starts_with('!') { + return false; + } is_builtin(base) || env.inputs.iter().any(|(n, ..)| n.eq_ignore_ascii_case(base)) } @@ -641,6 +645,36 @@ mod tests { assert!(resolve_params(&json!({"cell": "Input.col(9)"}), &env).is_err()); } + #[test] + fn references_on_sheets_named_like_tokens_stay_references() { + // Sheets named after the built-in bases and after an input: `Output!A1` is a cell on the + // sheet Output, in a reference key, in `$ref` and in `{{…}}`. + let mut wb = Workbook::new(); + for name in ["Output", "Row", "Column", "Input", "Rates"] { + wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new(name))); + } + let inputs = vec![("Input".to_string(), place("B2:D11"), true), ("Rates".to_string(), place("F2:F5"), false)]; + for output in [None, Some(place("H2"))] { + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output, item: Some((RepeatOf::Rows, place("B5:D5"))) }; + for (sheet, i) in [("Output", 1), ("Row", 2), ("Column", 3), ("Input", 4), ("Rates", 5)] { + for text in [format!("{sheet}!A1:B2"), format!("'{sheet}'!A1:B2")] { + let want = Place { sheet: i, range: RangeRef::parse("A1:B2").unwrap() }; + assert_eq!(resolve_ref(&text, &env), Ok(want), "{text}"); + let a = a1(want, &env); + let p = json!({"range": text, "target": {"$ref": text}, "formula": format!("=SUM({{{{{text}}}}})")}); + let r = resolve_params(&p, &env).unwrap(); + // A plain reference in a reference key is passed on as written. + assert_eq!(r, json!({"range": text, "target": a, "formula": format!("=SUM({a})")}), "{text}"); + } + } + assert!(!is_token("Output !A1", &env)); + // The bare names are still tokens. + check(&env, "Input.col(2)", "C2:C11"); + check(&env, "Rates", "F2:F5"); + check(&env, "Row", "B5:D5"); + } + } + #[test] fn sheet_names_are_quoted_when_needed() { for (name, want) in [("Sheet1", "Sheet1"), ("My Data", "'My Data'"), ("O'Hara", "'O''Hara'"), ("2024", "'2024'"), ("", "''")] { diff --git a/docs/macros.md b/docs/macros.md index 2860aa33ef..44bf572cf0 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -60,7 +60,8 @@ Methods narrow or move a range and chain: `col(2)` or `col("Amount")`, `row(1)`, Write a token as the whole value of a reference parameter (`cell`, `range`, `target`, keys ending in `Cell` or `Range`…), as `{"$ref": "Input.col(2)"}` anywhere, or as `{{Input.col(2)}}` inside any string, such as a formula. Plain A1 references stay absolute; a `Name:` anchor follows -inserted rows. +inserted rows. A reference with a sheet name is never a token, so `Output!A1` is a cell on a sheet +called Output, even when a token or input has that name. ## Storage From b51d588cac747db7ba0cc37953a6b01fbf7f1eaf Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:26:30 -0400 Subject: [PATCH 049/687] Engine: macros can't open, hide or show windows The policy denies window.* commands, but the View tab's window commands are view.*: a macro could run view.newWindow, which adds a window that the run's rollback doesn't remove, or view.hideWindow and view.unhideWindow, which change the session's hidden windows. The policy now denies those three. Arrange All changes nothing in the engine and Switch Windows only lists windows, so both stay allowed. --- crates/engine/src/macros/policy.rs | 12 ++++++++++-- crates/engine/src/tests/macros.rs | 2 ++ docs/macros.md | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/macros/policy.rs b/crates/engine/src/macros/policy.rs index 84bbc497b8..8f23a5b407 100644 --- a/crates/engine/src/macros/policy.rs +++ b/crates/engine/src/macros/policy.rs @@ -30,8 +30,13 @@ const DENIED_PREFIXES: &[(&str, &str)] = &[ ]; /// Single commands that never run in a macro. -const DENIED: &[(&str, &str)] = - &[("edit.undo", "a macro is undone as one step after it runs"), ("edit.redo", "a macro is undone as one step after it runs")]; +const DENIED: &[(&str, &str)] = &[ + ("edit.undo", "a macro is undone as one step after it runs"), + ("edit.redo", "a macro is undone as one step after it runs"), + ("view.newWindow", "macros don't open, hide or show windows"), + ("view.hideWindow", "macros don't open, hide or show windows"), + ("view.unhideWindow", "macros don't open, hide or show windows"), +]; /// Commands under a denied prefix that only change the workbook. const ALLOWED: &[&str] = &["file.properties"]; @@ -97,6 +102,9 @@ mod tests { "macro.record", "edit.undo", "edit.redo", + "view.newWindow", + "view.hideWindow", + "view.unhideWindow", ] { assert!(matches!(macro_policy(id, &none, Trust::Local), Policy::Deny(_)), "{id}"); assert!(matches!(macro_policy(id, &path, Trust::FromFile), Policy::Deny(_)), "{id}"); diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 2855f11e68..a928e9b53b 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -419,6 +419,8 @@ fn the_policy_keeps_macros_away_from_files_and_the_app() { ("automate.runScript", json!({"name": "Script 1"})), ("edit.undo", json!({})), ("macro.run", json!({"name": "Self"})), + ("view.newWindow", json!({})), + ("view.hideWindow", json!({})), ] { put( &mut s, diff --git a/docs/macros.md b/docs/macros.md index 1e1b873fb6..cf93148d3c 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -75,8 +75,8 @@ called Output, even when a token or input has that name. `macro.run {name, inputs?, onConflict?, overwrite?, approve?, timeLimit?}`: -1. **Policy.** File, app, window, automate and macro commands, and Undo and Redo, never run in a - macro. Reading or writing a file (`data.getData`, `data.fromTextCsv`, `insert.picture` or +1. **Policy.** File, app, window (including New Window, Hide and Unhide), automate and macro + commands, and Undo and Redo, never run in a macro. Reading or writing a file (`data.getData`, `data.fromTextCsv`, `insert.picture` or `pageLayout.background` with a `path`, or an export or print with a `path`) needs the command in the macro's `permissions`. Any `path` other than `null` counts, including a blank one and a token, and the policy is checked again on the parameters as they are when the step runs. A macro that touches files, or that came with a workbook opened From ccea7a8db372a4fb553103f09268ffd3bb04200c Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:19:50 -0400 Subject: [PATCH 050/687] Engine: macro debugger reports a step that panics as the step's failure When a debugged step panicked, the nested execute rolled the run back and dropped the debugger before advance could reattach the machine, so the caller got "No macro is being debugged" instead of the failed step. For example, stepping into a step whose command panics lost the step path and the panic message. advance now sees that the run was already rolled back and returns the usual failed result with the step error. --- crates/engine/src/macros/debug.rs | 65 ++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/macros/debug.rs b/crates/engine/src/macros/debug.rs index 1e599ab012..04da198346 100644 --- a/crates/engine/src/macros/debug.rs +++ b/crates/engine/src/macros/debug.rs @@ -188,6 +188,7 @@ fn all_paths(steps: &[gridcraft_model::Step], prefix: &str) -> BTreeSet /// Runs one step (`Step`), or steps until a breakpoint, the target step or the end. fn advance(s: &mut Session, mode: Mode) -> Result { let d = s.macro_debug.as_mut().ok_or_else(not_debugging)?; + let name = d.name.clone(); let breakpoints = d.breakpoints.clone(); let only_listed = d.only_listed; let mut machine = d.machine.take().ok_or_else(not_debugging)?; @@ -219,8 +220,16 @@ fn advance(s: &mut Session, mode: Mode) -> Result { } } let done = machine.is_done(); - if let Some(d) = s.macro_debug.as_mut() { - d.machine = Some(machine); + let executed = machine.executed; + match s.macro_debug.as_mut() { + Some(d) => d.machine = Some(machine), + // A step panicked and `recover` has already rolled the run back. + None => { + return match outcome { + Err(e) => Ok(failed(&name, executed, &e)), + Ok(()) => Err(not_debugging()), + }; + } } note_touched(s, &before); if let Err(e) = outcome { @@ -247,19 +256,17 @@ fn finish(s: &mut Session, error: Option) -> Result { // The group is gone only if its workbook was closed; then there is nothing to end. let _ = if rollback { s.group_rollback(token) } else { s.group_end(token).map(|_| ()) }; } + if let Some(e) = &error { + return Ok(failed(&d.name, executed, e)); + } let mut out = json!({"macro": d.name, "executed": executed}); - match (&error, &stopped) { - (Some(e), _) => { - out["state"] = json!("failed"); - out["error"] = serde_json::to_value(e).unwrap_or(Json::Null); - out["message"] = json!(format!("{} Nothing was changed.", e.describe())); - } - (None, Some((msg, rolled))) => { + match &stopped { + Some((msg, rolled)) => { out["state"] = json!("stopped"); out["stopped"] = json!(msg); out["kept"] = json!(!rolled); } - (None, None) => out["state"] = json!("finished"), + None => out["state"] = json!("finished"), } if !rollback { out["changed"] = json!(changed); @@ -267,6 +274,17 @@ fn finish(s: &mut Session, error: Option) -> Result { Ok(out) } +/// The result of a run that a failed step rolled back. +fn failed(name: &str, executed: u64, e: &StepError) -> Json { + json!({ + "macro": name, + "executed": executed, + "state": "failed", + "error": serde_json::to_value(e).unwrap_or(Json::Null), + "message": format!("{} Nothing was changed.", e.describe()), + }) +} + /// Stops the run: keeps what it did as one undo step, or rolls it back. fn stop(s: &mut Session, keep: bool) -> Result { let Some(mut d) = s.macro_debug.take() else { return Err(not_debugging()) }; @@ -360,6 +378,33 @@ mod tests { s.execute("cell.set", json!({"cell": "B1", "input": "free"})).unwrap(); } + #[test] + fn a_step_that_panics_reports_the_step_failure() { + crate::register_test_command("test.debugStepPanics", |_, _| panic!("boom")); + let mut s = Session::new(); + s.new_workbook(); + let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "A1", "input": "x"}}, + {"command": "test.debugStepPanics", "params": {}} + ]})) + .unwrap(); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + wb.macros.push(m); + d.wb = std::sync::Arc::new(wb); + let start = s.doc().unwrap().wb.clone(); + s.execute("macro.debug", json!({"name": "M"})).unwrap(); + s.execute("macro.debug", json!({"action": "step"})).unwrap(); + let r = s.execute("macro.debug", json!({"action": "step"})).unwrap(); + assert_eq!(r["state"], "failed", "{r}"); + assert_eq!(r["macro"], "M"); + assert_eq!(r["error"]["step"], "2"); + assert!(r["message"].as_str().unwrap().contains("boom"), "{r}"); + assert!(s.macro_debug.is_none()); + assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().wb, &start)); + assert!(!s.in_group(s.doc().unwrap().uid)); + } + #[test] fn a_workbook_in_the_touched_log_waits_until_the_run_ends() { let mut s = Session::new(); From 3f9223c85f870a93bb1a49c38775331623289fea Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:25:28 -0400 Subject: [PATCH 051/687] Engine: macro recorder inlines a recorded macro run only when its steps stand alone A macro run while recording was replaced by every command in it, including both branches of an If and the body of a Repeat, whenever it had no inputs. For example, recording a run of a macro with an If played back both the then and the else commands without a condition, and commands using Row, Column or Output failed on playback. The run is now inlined only when the macro has no inputs, no output and only plain commands (its disabled steps are left out); otherwise it is left out and listed as skipped. --- crates/engine/src/macros/recorder.rs | 18 +++++++++------ crates/engine/src/tests/macros.rs | 33 ++++++++++++++++++++++++++++ docs/macros.md | 3 ++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/macros/recorder.rs b/crates/engine/src/macros/recorder.rs index b154898735..bb2e30f74e 100644 --- a/crates/engine/src/macros/recorder.rs +++ b/crates/engine/src/macros/recorder.rs @@ -4,8 +4,9 @@ //! with the sheet and selection it ran in. When recording stops: //! //! - commands a macro may not run (saving, opening files…) and other macros' commands are left -//! out; a macro run while recording is replaced by its steps when it has no inputs, and an -//! Automate script run while recording by the script's steps; +//! out; a macro run while recording is replaced by its steps when it has no inputs, no output +//! and only plain commands (otherwise it is left out), and an Automate script run while +//! recording by the script's steps; //! - a `sheet.activate` or `selection.set` step is added where the sheet or selection changed //! between commands without one, and runs of `selection.set` collapse to the last; //! - with relative references (the default), the selection when recording started is the example @@ -14,7 +15,7 @@ //! shape, header and column types are inferred from it. use gridcraft_core::{CellRef, RangeRef, Value}; -use gridcraft_model::{ColType, InputKind, Macro, NamedInput, Step, Workbook}; +use gridcraft_model::{ColType, InputKind, Macro, NamedInput, Step, StepKind, Workbook}; use serde_json::{Map, Value as Json, json}; use super::policy::{Policy, macro_policy}; @@ -51,12 +52,15 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & for st in steps.iter().filter(|st| st.doc == start.doc) { let id = st.id.as_str(); if id == "macro.run" { - // Another macro's steps, when they stand on their own. + // Another macro's steps, when they stand on their own: no inputs, no output and only + // plain commands (a Repeat, If or Stop means nothing outside its macro). let inner = st.params.get("name").and_then(Json::as_str).and_then(|n| super::find(wb, n)).and_then(|i| wb.macros.get(i)); match inner { - Some(m) if m.inputs.is_empty() => { - for (_, cid, params) in super::command_steps(&m.steps) { - cmds.push((cid.to_string(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + Some(m) if m.inputs.is_empty() && m.output.anchor.is_none() && m.steps.iter().all(|x| matches!(x.kind, StepKind::Command { .. })) => { + for x in m.steps.iter().filter(|x| x.enabled) { + if let StepKind::Command { id: cid, params } = &x.kind { + cmds.push((cid.clone(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + } } } _ => skipped.push(id.to_string()), diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 5b0b2ddbb0..f3640f08af 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1045,3 +1045,36 @@ fn a_relative_recording_keeps_references_on_another_sheet() { s.execute("sheet.activate", json!({"sheet": "Two"})).unwrap(); assert_eq!((v(&s, "C4").display(), v(&s, "D2").display()), ("x".into(), "12".into())); } + +#[test] +fn a_recorded_macro_run_is_inlined_only_when_its_steps_stand_alone() { + let mut s = s(); + put( + &mut s, + json!({"name": "Branchy", "output": {"onConflict": "overwrite"}, "steps": [ + {"type": "if", "cell": "A1", "op": "empty", + "then": [{"command": "cell.set", "params": {"cell": "B1", "input": "empty"}}], + "else": [{"command": "cell.set", "params": {"cell": "B1", "input": "full"}}]} + ]}), + ); + put( + &mut s, + json!({"name": "Placed", "output": {"anchor": "D1", "onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "Output", "input": "out"}} + ]}), + ); + put( + &mut s, + json!({"name": "Plain", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "C1", "input": "plain"}}, + {"command": "cell.set", "params": {"cell": "C2", "input": "off"}, "enabled": false} + ]}), + ); + s.execute("macro.record", json!({"relative": false})).unwrap(); + for name in ["Branchy", "Placed", "Plain"] { + s.execute("macro.run", json!({"name": name})).unwrap(); + } + let r = s.execute("macro.record", json!({"stop": true, "name": "Rec"})).unwrap(); + assert_eq!(r["skipped"], json!(["macro.run", "macro.run"])); + assert_eq!(recorded(&mut s, "Rec"), ["cell.set {\"cell\":\"C1\",\"input\":\"plain\"}"]); +} diff --git a/docs/macros.md b/docs/macros.md index 6a30a1d56a..674a35673f 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -126,7 +126,8 @@ on its sheet become tokens (on another sheet `C4` stays that sheet's `C4`), and header and column types are inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so playing them back sets them rather than flipping them. Commands a macro may not run are left out and listed in the result; a macro run while recording is replaced by its steps -when it has no inputs, and an Automate script run by the script's steps. +when it has no inputs, no output and only plain commands (otherwise it is left out and listed), and +an Automate script run by the script's steps. ## Other commands and storage From 7d31c5a4eda45cb7684991158aff28f786d916ef Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:25:53 -0400 Subject: [PATCH 052/687] Engine: a macro in a newer format is dropped with a warning, not saved back as version 1 Opening a workbook accepted a macro whose format version `v` was above MACRO_VERSION. Serde dropped the fields this build doesn't know, and the next save wrote the reduced macro back with `v: 2`, so a newer build would read it as a complete version 2 macro. A version above MACRO_VERSION is now refused by Macro::check_limits, and parse_macro checks the raw `v` before reading the steps. Such a macro takes the existing per-macro path: it is left out with the usual single warning (which now names a newer format), and the rest of the workbook opens. Tests cover JSON and xlsx hidden names, with current and missing versions as controls. --- crates/engine/src/macros/mod.rs | 8 +++++-- crates/engine/src/macros/tests.rs | 39 +++++++++++++++++++++++++++++++ crates/model/src/macros.rs | 14 +++++++++-- docs/macros.md | 6 +++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 6b2150e66e..b74fc7e99b 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -20,6 +20,10 @@ const MACRO_KEY: &str = "macro_"; /// Reads a macro from JSON, as `macro.save` and files give it. A command step may also use the /// short form `{"command": "cell.set", "params": {…}}`. pub fn parse_macro(v: &Json) -> Result { + // A newer format is refused before its steps are read, so the message says why. + if let Some(found) = v.get("v").and_then(Json::as_u64) { + gridcraft_model::check_version(found)?; + } let mut v = v.clone(); if let Some(steps) = v.get_mut("steps") { normalize_steps(steps, 1)?; @@ -99,7 +103,7 @@ pub fn with_names(wb: &Workbook) -> crate::Result> { } /// After a file is opened: moves macros out of hidden names into `Workbook::macros` and drops -/// macros that are damaged or over the limits. Returns a warning for what was dropped. +/// macros that are damaged, over the limits or in a newer format. Returns a warning for what was dropped. pub fn load(wb: &mut Workbook) -> Vec { let mut warnings = Vec::new(); let mut bad = 0usize; @@ -156,7 +160,7 @@ pub fn load(wb: &mut Workbook) -> Vec { } if bad > 0 { warnings.push(format!( - "{bad} macro{} in the workbook couldn't be opened: {} damaged or over the limits ({} macros, {} steps each, Repeat and If nested {} deep).", + "{bad} macro{} in the workbook couldn't be opened: {} damaged, made by a newer version of GridCraft, or over the limits ({} macros, {} steps each, Repeat and If nested {} deep).", if bad == 1 { "" } else { "s" }, if bad == 1 { "it is" } else { "they are" }, gridcraft_model::MAX_MACROS, diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 9d5b90a37d..63f8cef761 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -278,3 +278,42 @@ fn a_damaged_json_macro_drops_alone() { assert!(warnings.len() == 1 && warnings[0].starts_with("3 macros"), "{warnings:?}"); assert_eq!(back.sheet(0).unwrap().value(gridcraft_core::CellRef::new(0, 0)), gridcraft_core::Value::text("kept")); } + +#[test] +fn a_macro_in_a_newer_format_is_dropped_not_rewritten() { + // A newer build's macro (`v` 2, with a field this build doesn't know) would lose that field + // here and be saved back as if complete, so it is dropped with the usual warning instead. + // Version 1 and a missing version are read as before. + let future = json!({"name": "Future", "v": MACRO_VERSION + 1, "fromALaterBuild": {"x": 1}, "steps": [{"command": "cell.set"}]}); + let current = json!({"name": "Current", "v": MACRO_VERSION, "steps": [{"command": "cell.set"}]}); + let missing = json!({"name": "Missing"}); + let err = parse_macro(&future).unwrap_err(); + assert!(err.contains("newer version") && err.contains("format 2"), "{err}"); + assert!(parse_macro(&json!({"name": "Huge", "v": u64::from(u32::MAX) + 1})).unwrap_err().contains("newer version")); + // A macro built in code goes through the same check on every path. + assert!(Macro { name: "Built".into(), v: MACRO_VERSION + 1, ..Macro::default() }.check_limits().is_err()); + let check = |file: &str, back: &Workbook, warnings: &[String]| { + let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect(); + assert_eq!(names, ["Current", "Missing"], "{file}"); + assert!(back.macros.iter().all(|m| m.v == MACRO_VERSION), "{file}"); + assert!(warnings.len() == 1 && warnings[0].starts_with("1 macro ") && warnings[0].contains("newer version"), "{file}: {warnings:?}"); + // Saving and opening again leaves no reduced copy of it behind. + let (again, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(back, file).unwrap()).unwrap(); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + assert_eq!(again.macros, back.macros, "{file}"); + }; + // The JSON format's field. + let mut v: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&Workbook::new(), "book.json").unwrap()).unwrap(); + v["macros"] = json!([future, current, missing]); + let (back, warnings) = crate::io::open_bytes("book.json", v.to_string().as_bytes()).unwrap(); + check("book.json", &back, &warnings); + // Hidden names in xlsx. + let mut wb = Workbook::new(); + for (i, m) in [&future, ¤t, &missing].into_iter().enumerate() { + wb.names.push(hidden(&format!("_gc_macro_{i}_0"), &m.to_string())); + } + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let (back, warnings) = crate::io::open_bytes("book.xlsx", &bytes).unwrap(); + assert!(back.names.is_empty(), "{:?}", names_of(&back)); + check("book.xlsx", &back, &warnings); +} diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs index 39f7996159..8dc657f907 100644 --- a/crates/model/src/macros.rs +++ b/crates/model/src/macros.rs @@ -63,9 +63,10 @@ impl Macro { count(&self.steps) } - /// Checks the size limits ([`MAX_MACRO_STEPS`], [`MAX_MACRO_DEPTH`], name and text lengths) and - /// that the name has no control characters. + /// Checks the format version ([`check_version`]), the size limits ([`MAX_MACRO_STEPS`], + /// [`MAX_MACRO_DEPTH`], name and text lengths) and that the name has no control characters. pub fn check_limits(&self) -> Result<(), String> { + check_version(u64::from(self.v))?; let name = self.name.trim(); if name.is_empty() { return Err("A macro needs a name.".into()); @@ -119,6 +120,15 @@ impl Macro { } } +/// Refuses a format version newer than [`MACRO_VERSION`]: this build would drop the fields it +/// doesn't know, and a save would then write the rest back as if it were the whole macro. +pub fn check_version(v: u64) -> Result<(), String> { + if v > u64::from(MACRO_VERSION) { + return Err(format!("This macro was made by a newer version of GridCraft (macro format {v}); this version reads format {MACRO_VERSION}.")); + } + Ok(()) +} + /// Reads the `macros` field of a JSON workbook as raw entries, whatever they hold, so they can be /// checked one at a time. pub(crate) fn entries<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { diff --git a/docs/macros.md b/docs/macros.md index 99050b0aa5..1c755e3295 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -49,6 +49,12 @@ Macros are saved in the workbook: as a field in GridCraft's JSON format, and in defined names (`_gc_macro__`, the macro's JSON split into string literals within Excel's limits), which other spreadsheet applications keep but do not run. +Each macro carries its format version, `v` (1 in this version; a missing `v` reads as 1). When a +file is opened, a macro that is damaged, over the limits or in a newer format (a `v` above 1) is +left out with one warning, and the rest of the workbook and its other macros open. A newer +macro is never read in part, so a save can't write it back with its newer fields missing; a +macro that was left out is not in the saved file. + ## Automate scripts and VBA Automate scripts (`automate.recordActions`, `newScript`, `allScripts`, `runScript`, From 77150be4556c5b15a8e1f9dec456bee5ae82c3ba Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:27:28 -0400 Subject: [PATCH 053/687] Engine: col("Header") is refused on a range without a header row col("...") always matched against the first row of the range, whatever its header flag. On an input declared without a header row, Input.col("Amount") silently picked the first column whose first data cell read "Amount". After .data, the natural Input.data.col("Amount") failed, because the first row was then a data row. The header row is now carried through data, and col("...") is refused with a message when there is none (an input without a header, a Name: base, or after any other method). --- crates/engine/src/macros/resolve.rs | 40 ++++++++++++++++++++--------- docs/macros.md | 2 ++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs index e8c7b6613f..d71db8cd4d 100644 --- a/crates/engine/src/macros/resolve.rs +++ b/crates/engine/src/macros/resolve.rs @@ -6,10 +6,11 @@ //! - bases: a declared input (`Input`, `Rates`), `Output` (the output rule's anchor), `Row` or //! `Column` (the current item of the Repeat step around it), and `Name:Total` (a defined name or //! a table's data); -//! - methods, 1-based and chainable: `.col(n)` or `.col("Amount")` (by header), `.row(n)`, -//! `.cols(a..b)`, `.rows(a..)`, `.cell(r,c)` (relative to the top-left cell; any whole numbers, -//! so `cell(0,1)` is the cell above), `.offset(r,c)`, `.resize(h,w)`, `.data` (without the -//! header row), `.below(n)` and `.right(n)` (the n rows below or columns right of the range). +//! - methods, 1-based and chainable: `.col(n)` or `.col("Amount")` (by an input's header row, +//! also after `.data`), `.row(n)`, `.cols(a..b)`, `.rows(a..)`, `.cell(r,c)` (relative to the +//! top-left cell; any whole numbers, so `cell(0,1)` is the cell above), `.offset(r,c)`, +//! `.resize(h,w)`, `.data` (without the header row), `.below(n)` and `.right(n)` (the n rows +//! below or columns right of the range). //! //! In parameters a token is written three ways: `{"$ref": "Input.col(2)"}` anywhere; the whole //! string `"Input.col(2)"` in a reference parameter (`cell`, `range`, `target`, `to`, keys ending @@ -181,6 +182,8 @@ pub fn resolve_token(text: &str, env: &Env) -> R { let t = text.trim(); let mut p = Parser { s: t, at: 0 }; let (mut place, mut header) = p.base(env)?; + // The row of column names `col("…")` reads: the input's header row, kept through `data`. + let mut names_row = header.then_some(place.range.start.row); while p.eat('.') { let name = p.ident(); if name.is_empty() { @@ -188,10 +191,14 @@ pub fn resolve_token(text: &str, env: &Env) -> R { } let args = if p.eat('(') { p.args(t)? } else { vec![] }; let r = place.range; - let range = match (name.to_ascii_lowercase().as_str(), args.as_slice()) { + let method = name.to_ascii_lowercase(); + let range = match (method.as_str(), args.as_slice()) { ("col", [Arg::Int(n)]) => sub(r, None, Some((*n, *n)), t)?, ("col", [Arg::Text(h)]) => { - let c = header_col(env, place, h).ok_or_else(|| ResolveError::Bad(format!("'{t}': no column is headed '{h}'.")))?; + let Some(row) = names_row else { + return bad(format!("'{t}': col(\"{h}\") needs an input with a header row; use col(n).")); + }; + let c = header_col(env, place, row, h).ok_or_else(|| ResolveError::Bad(format!("'{t}': no column is headed '{h}'.")))?; sub(r, None, Some((c, c)), t)? } ("row", [Arg::Int(n)]) => sub(r, Some((*n, *n)), None, t)?, @@ -231,8 +238,12 @@ pub fn resolve_token(text: &str, env: &Env) -> R { )); } }; - // After a method the result is plain cells: `data` and `col("…")` no longer apply. + // After a method the result is plain cells: `data` no longer applies, and only `data` + // keeps the header row for `col("…")`. header = false; + if method != "data" { + names_row = None; + } place = Place { sheet: place.sheet, range }; } p.skip_ws(); @@ -278,12 +289,12 @@ fn shift(c: CellRef, dr: i64, dc: i64, t: &str) -> R { } } -/// The 1-based column of `place` whose header (first row) reads `h`. -fn header_col(env: &Env, place: Place, h: &str) -> Option { +/// The 1-based column of `place` whose header (in sheet row `row`) reads `h`. +fn header_col(env: &Env, place: Place, row: u32, h: &str) -> Option { let sh = env.wb.sheet(place.sheet)?; let r = place.range; (r.start.col..=r.end.col) - .position(|c| crate::display::cell_text(env.wb, sh, CellRef::new(r.start.row, c)).trim().eq_ignore_ascii_case(h.trim())) + .position(|c| crate::display::cell_text(env.wb, sh, CellRef::new(row, c)).trim().eq_ignore_ascii_case(h.trim())) .map(|i| i as i64 + 1) } @@ -532,6 +543,7 @@ mod tests { ("Input.col(\"date\").data", "D2:D11"), ("Input.data", "B3:D11"), ("Input.data.col(2)", "C3:C11"), + ("Input.data.col(\"Amount\")", "C3:C11"), ("Input.row(1)", "B2:D2"), ("Input.rows(2..)", "B3:D11"), ("Input.rows(2..4)", "B3:D5"), @@ -569,13 +581,17 @@ mod tests { #[test] fn bad_tokens_say_why() { let wb = wb(); - let inputs = vec![("Input".to_string(), place("B2:D11"), false)]; + // The same cells, read with and without a header row. + let inputs = vec![("Input".to_string(), place("B2:D11"), false), ("Headed".to_string(), place("B2:D11"), true)]; let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None }; for (token, needle, edge) in [ ("Input.col(4)", "outside the range", false), ("Input.col(0)", "outside the range", false), ("Input.rows(5..2)", "outside the range", false), - ("Input.col(\"Nope\")", "no column is headed", false), + ("Headed.col(\"Nope\")", "no column is headed", false), + ("Input.col(\"Name\")", "needs an input with a header row", false), + ("Headed.col(2).col(\"Amount\")", "needs an input with a header row", false), + ("Name:Rates.col(\"Name\")", "needs an input with a header row", false), ("Input.colour(1)", "isn't a token method", false), ("Input.col(1", "expected", false), ("Input.col(x)", "whole number", false), diff --git a/docs/macros.md b/docs/macros.md index 44bf572cf0..4a08e7228f 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -56,6 +56,8 @@ References in step parameters can be tokens, worked out when the step runs: Methods narrow or move a range and chain: `col(2)` or `col("Amount")`, `row(1)`, `cols(2..3)`, `rows(2..)`, `cell(r,c)` (relative to the top-left cell; `cell(0,1)` is the cell above), `offset(r,c)`, `resize(h,w)`, `data` (without the header row), `below(n)` and `right(n)`. +`col("Amount")` looks the name up in an input's header row, directly or after `data`; on a range +without a header row it is refused. Write a token as the whole value of a reference parameter (`cell`, `range`, `target`, keys ending in `Cell` or `Range`…), as `{"$ref": "Input.col(2)"}` anywhere, or as `{{Input.col(2)}}` inside From 06bbcb504aa2b777dfffc068aefdf8e17330559e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:27:29 -0400 Subject: [PATCH 054/687] Engine: macro approvals are keyed on the macro's canonical JSON, not a 64-bit hash Approvals were stored as (document, FNV-1a hash of the macro's JSON). FNV-1a is not collision resistant, so a crafted workbook could hold a harmless macro and a different one with the same hash, and approving the first would let the second run without asking. The key is now the macro's canonical JSON text itself (object keys sorted), so two different macros never share an approval. macros::hash is replaced by macros::fingerprint. --- crates/engine/src/lib.rs | 5 +++-- crates/engine/src/macros/mod.rs | 11 +++++------ crates/engine/src/macros/run.rs | 4 ++-- crates/engine/src/macros/tests.rs | 4 ++-- crates/engine/src/tests/macros.rs | 9 +++++++++ 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index d188161eea..caac006bcb 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -322,8 +322,9 @@ pub struct Session { pub recorder: Option, /// How many `execute` calls are running (0 between top-level commands). depth: u32, - /// Macros the person allowed to run in this session: (document uid, macro content hash). - pub macro_approvals: std::collections::HashSet<(u64, u64)>, + /// Macros the person allowed to run in this session: (document uid, the macro's content as + /// canonical JSON, from [`macros::fingerprint`]). + pub macro_approvals: std::collections::HashSet<(u64, String)>, /// Windows hidden with View › Hide. pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 55933c1acb..457f109875 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -88,12 +88,11 @@ pub fn find(wb: &Workbook, name: &str) -> Option { wb.macros.iter().position(|m| m.name.eq_ignore_ascii_case(name)) } -/// A stable fingerprint of a macro's content: approval to run a macro from a file holds until -/// the macro changes. -pub fn hash(m: &Macro) -> u64 { - // FNV-1a over the macro's JSON. - let text = macro_json(m).to_string(); - text.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)) +/// A macro's content as canonical JSON text (object keys sorted): approval to run a macro holds +/// until the macro changes. It is the text itself rather than a hash, so no two different macros +/// share it. +pub fn fingerprint(m: &Macro) -> String { + macro_json(m).to_string() } /// A copy of `wb` for xlsx with each macro in hidden names (`_gc_macro__`) instead of the diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 57a70562e5..36fd1ac417 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -144,7 +144,7 @@ pub fn approve(s: &mut Session, m: &Macro, doc: u64, asks: &[String], approve: b if asks.is_empty() { return Ok(()); } - let key = (doc, super::hash(m)); + let key = (doc, super::fingerprint(m)); if s.macro_approvals.contains(&key) { return Ok(()); } @@ -1227,7 +1227,7 @@ pub fn check(s: &mut Session, name: &str, p: &Json) -> Result { return Ok(out); } }; - let approved = s.macro_approvals.contains(&(doc, super::hash(&m))); + let approved = s.macro_approvals.contains(&(doc, super::fingerprint(&m))); if !asks.is_empty() && !approved { // Nothing from an unapproved macro runs, not even in a dry run. out["needsApproval"] = json!(asks); diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index f0845a54be..84792dc407 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -215,9 +215,9 @@ fn names() { assert_eq!(find(&wb, "Macro2"), None); let a = sample_macro("A"); let mut b = a.clone(); - assert_eq!(hash(&a), hash(&b)); + assert_eq!(fingerprint(&a), fingerprint(&b)); b.steps[0].note = "changed".into(); - assert_ne!(hash(&a), hash(&b)); + assert_ne!(fingerprint(&a), fingerprint(&b)); } #[test] diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index a928e9b53b..a42a60dd7e 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -486,6 +486,15 @@ fn macros_from_a_file_ask_before_running() { assert_eq!(v(&s, "A1").display(), "ran"); // Approved once per macro content: the second run doesn't ask. s.execute("macro.run", json!({"name": "Theirs"})).unwrap(); + // The approval holds the macro's whole content, not a hash two macros could share. + let m = s.doc().unwrap().wb.macros[0].clone(); + let doc = s.doc().unwrap().uid; + let key: (u64, String) = (doc, crate::macros::macro_json(&m).to_string()); + assert!(s.macro_approvals.contains(&key)); + // The JSON is canonical: the same macro with its keys in another order is the same macro. + let a = crate::macros::parse_macro(&json!({"name": "M", "steps": [{"command": "cell.set", "params": {"cell": "A1", "input": "x"}}]})).unwrap(); + let b = crate::macros::parse_macro(&json!({"steps": [{"params": {"input": "x", "cell": "A1"}, "command": "cell.set"}], "name": "M"})).unwrap(); + assert_eq!(crate::macros::fingerprint(&a), crate::macros::fingerprint(&b)); } #[test] From 62cececa63e2ebce48aeb2bf6c5296807fbcdbf1 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:33:53 -0400 Subject: [PATCH 055/687] Docs: debugging macros --- docs/macros.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index 7c7953ccf7..45b2b51f84 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -1,6 +1,6 @@ # Macros -> **Last updated:** 2026-10-21 · **Change:** minor (running and checking macros: `macro.run`, `macro.check`) +> **Last updated:** 2026-10-21 · **Change:** minor (debugging macros: `macro.debug`) GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro @@ -9,8 +9,8 @@ error (a file it already wrote stays), checks where its results will land before and does not touch files without permission. Agents run macros with `macro.run` like any other command. -This page describes the macro format, its tokens, and running and checking macros. The engine has -no macro debugger, recorder or management commands in this version, and no macro interface. +This page describes the macro format, its tokens, and running, checking and debugging macros. +The engine has no macro recorder or management commands in this version, and no macro interface. ## A macro @@ -104,6 +104,16 @@ called Output, even when a token or input has that name. `macro.check {name, inputs?, timeLimit?}` reports the bound inputs, the output anchor, any approval needed and the footprint, without changing anything. +## Debugging + +`macro.debug {action, …}` with `start` (`name`, `inputs`, `breakpoints`), `step`, `continue`, +`runTo` (`step`: a path), `stop` (`keep: true` keeps the changes) and `state`. `breakpoints` can +be given with any action; `breakpointsOnly: true` makes the list replace the steps' own flags. The +run stays one undo step; while it is open, edits of the workbooks it touched (its own, and any +other its steps changed) are refused with "Finish or stop the macro first", and other workbooks +stay editable. Each answer gives the next step with its parameters as they would run, the +parameters of the step that just ran, and the cells changed so far. + ## Storage Macros are saved in the workbook: as a field in GridCraft's JSON format, and in xlsx as hidden From efce63f75ff132c3ced76465530897b0bc5e8aa0 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:26:27 -0400 Subject: [PATCH 056/687] Engine: macro recorder applies the macro policy to the steps of a recorded script run An Automate script run while recording was replaced by all of its steps except automate.* ones, without the policy check that top-level commands get. For example, a script that runs window.activate or macro.list produced a recorded macro that the runner refused on playback. The steps of a recorded script run, and of an inlined macro run, now go through the same policy as top-level commands; refused ones are left out and listed as skipped. --- crates/engine/src/macros/recorder.rs | 16 ++++++++++++---- crates/engine/src/tests/macros.rs | 17 +++++++++++++++++ docs/macros.md | 7 ++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/macros/recorder.rs b/crates/engine/src/macros/recorder.rs index bb2e30f74e..210872475e 100644 --- a/crates/engine/src/macros/recorder.rs +++ b/crates/engine/src/macros/recorder.rs @@ -6,7 +6,7 @@ //! - commands a macro may not run (saving, opening files…) and other macros' commands are left //! out; a macro run while recording is replaced by its steps when it has no inputs, no output //! and only plain commands (otherwise it is left out), and an Automate script run while -//! recording by the script's steps; +//! recording by the script's steps, leaving out the steps a macro may not run; //! - a `sheet.activate` or `selection.set` step is added where the sheet or selection changed //! between commands without one, and runs of `selection.set` collapse to the last; //! - with relative references (the default), the selection when recording started is the example @@ -49,6 +49,7 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & // Commands as they'll be played back: (id, params, sheet before, selection before, whether // they came from another macro and stay as written). let mut cmds: Vec<(String, Json, usize, String, Selection, bool)> = Vec::new(); + let refused = |id: &str, params: &Json| matches!(macro_policy(id, params, crate::Trust::Local), Policy::Deny(_) | Policy::NeedsPermission(_)); for st in steps.iter().filter(|st| st.doc == start.doc) { let id = st.id.as_str(); if id == "macro.run" { @@ -58,9 +59,12 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & match inner { Some(m) if m.inputs.is_empty() && m.output.anchor.is_none() && m.steps.iter().all(|x| matches!(x.kind, StepKind::Command { .. })) => { for x in m.steps.iter().filter(|x| x.enabled) { - if let StepKind::Command { id: cid, params } = &x.kind { - cmds.push((cid.clone(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + let StepKind::Command { id: cid, params } = &x.kind else { continue }; + if refused(cid, params) { + skipped.push(cid.clone()); + continue; } + cmds.push((cid.clone(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); } } _ => skipped.push(id.to_string()), @@ -72,6 +76,10 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & match st.params.get("name").and_then(Json::as_str).and_then(|n| crate::cmd::extra::script_steps(wb, n)) { Some(steps) => { for (cid, params) in steps { + if refused(&cid, ¶ms) { + skipped.push(cid); + continue; + } cmds.push((cid, params, st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); } } @@ -79,7 +87,7 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & } continue; } - if matches!(macro_policy(id, &st.params, crate::Trust::Local), Policy::Deny(_) | Policy::NeedsPermission(_)) { + if refused(id, &st.params) { skipped.push(id.to_string()); continue; } diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index f3640f08af..e16e8be3e5 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1078,3 +1078,20 @@ fn a_recorded_macro_run_is_inlined_only_when_its_steps_stand_alone() { assert_eq!(r["skipped"], json!(["macro.run", "macro.run"])); assert_eq!(recorded(&mut s, "Rec"), ["cell.set {\"cell\":\"C1\",\"input\":\"plain\"}"]); } + +#[test] +fn a_recorded_script_run_leaves_out_the_steps_a_macro_may_not_run() { + let mut s = s(); + let steps = json!([ + {"command": "cell.set", "params": {"cell": "B1", "input": "kept"}}, + {"command": "window.activate", "params": {"index": 0}}, + {"command": "macro.list", "params": {}} + ]); + s.execute("automate.newScript", json!({"name": "Mixed", "commands": steps})).unwrap(); + s.execute("macro.record", json!({"relative": false})).unwrap(); + s.execute("automate.runScript", json!({"name": "Mixed"})).unwrap(); + let r = s.execute("macro.record", json!({"stop": true, "name": "Rec"})).unwrap(); + assert_eq!(r["skipped"], json!(["window.activate", "macro.list"])); + assert_eq!(recorded(&mut s, "Rec"), ["cell.set {\"cell\":\"B1\",\"input\":\"kept\"}"]); + s.execute("macro.run", json!({"name": "Rec", "onConflict": "overwrite"})).unwrap(); +} diff --git a/docs/macros.md b/docs/macros.md index 674a35673f..fab64144f9 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -125,9 +125,10 @@ references the selection when recording starts is the example input: references on its sheet become tokens (on another sheet `C4` stays that sheet's `C4`), and the input's shape, header and column types are inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so playing them back sets them rather than flipping them. Commands a macro may not -run are left out and listed in the result; a macro run while recording is replaced by its steps -when it has no inputs, no output and only plain commands (otherwise it is left out and listed), and -an Automate script run by the script's steps. +run are left out and listed in the result, also from the steps of a macro or script run while +recording; a macro run while recording is replaced by its steps when it has no inputs, no output +and only plain commands (otherwise it is left out and listed), and an Automate script run by the +script's steps. ## Other commands and storage From 3c74dfcbb0b7feec9bad4c4a91fe2caff6f4a9d0 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:27:10 -0400 Subject: [PATCH 057/687] Engine: renaming macros with clashing names on open takes near-linear work Opening a workbook gave each clashing macro name a suffix by trying " (2)", " (3)"... in turn, and each try scanned every macro accepted so far. A file with 1,000 macros of the same 255-character name made about 300 million name comparisons. Load now keeps a set of the names in use (ASCII case-insensitive, as find compares them) and, for each shortened name and suffix width, the next suffix to try, so each suffix is looked up about once. The result is unchanged: the smallest free suffix, within the name length limit, and the renamed macro still passes the final checks. unique_name keeps its signature. A test opens, saves and reopens 1,000 same-name macros in JSON and xlsx and keeps them all, and another counts lookups instead of timing them. --- crates/engine/src/macros/mod.rs | 75 ++++++++++++++++++++++++++----- crates/engine/src/macros/tests.rs | 50 +++++++++++++++++++++ 2 files changed, 113 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index b74fc7e99b..9a580c7911 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -7,7 +7,7 @@ //! the open document. Scripts made with Automate › New Script or Record Actions are a separate, //! older feature with their own storage; macros neither read nor change them. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap, HashSet}; use gridcraft_model::{DefinedName, Macro, Workbook}; use serde_json::{Value as Json, json}; @@ -142,6 +142,7 @@ pub fn load(wb: &mut Workbook) -> Vec { // All of them go through the same limits, and a clashing name gets a suffix. let mut all = std::mem::take(&mut wb.macros); all.extend(found); + let mut taken = Taken::default(); for m in all { if wb.macros.len() >= gridcraft_model::MAX_MACROS { bad += 1; @@ -149,12 +150,15 @@ pub fn load(wb: &mut Workbook) -> Vec { } let mut m = m; let checked = m.check_limits().and_then(|()| { - m.name = unique_name(wb, m.name.trim()); + m.name = taken.unique(m.name.trim()); // The macro as it is kept, renamed, must still pass, or the next save would lose it. m.check_limits().and_then(|()| check_size(&m)) }); match checked { - Ok(()) => wb.macros.push(m), + Ok(()) => { + taken.insert(&m.name); + wb.macros.push(m); + } Err(_) => bad += 1, } } @@ -181,19 +185,66 @@ fn macro_item(n: &DefinedName) -> Option<(String, Option)> { /// `name`, or `name (2)`, `name (3)`… if a macro already has it. A long name is shortened to make /// room for the suffix, so the result stays within [`gridcraft_model::MAX_MACRO_NAME`] characters. pub fn unique_name(wb: &Workbook, name: &str) -> String { - let name = if name.is_empty() { "Macro" } else { name }; - if find(wb, name).is_none() { - return name.to_string(); + let mut taken = Taken::default(); + for m in &wb.macros { + taken.insert(&m.name); } - (2..) - .map(|n| { + taken.unique(name) +} + +/// Macro names in use, compared as [`find`] compares them, and the next suffix to try for each +/// shortened name and suffix width, so naming many clashing macros takes near-linear work. +#[derive(Default)] +struct Taken { + names: HashSet, + next: HashMap<(String, usize), u64>, + /// Name lookups made, for tests. + probes: usize, +} + +impl Taken { + fn key(name: &str) -> String { + name.trim().to_ascii_lowercase() + } + + fn insert(&mut self, name: &str) { + self.names.insert(Taken::key(name)); + } + + fn contains(&mut self, name: &str) -> bool { + self.probes = self.probes.saturating_add(1); + self.names.contains(&Taken::key(name)) + } + + /// The first of `name`, `name (2)`, `name (3)`… not in use. + fn unique(&mut self, name: &str) -> String { + let name = if name.is_empty() { "Macro" } else { name }; + if !self.contains(name) { + return name.to_string(); + } + let mut n: u64 = 2; + loop { let suffix = format!(" ({n})"); let keep = gridcraft_model::MAX_MACRO_NAME.saturating_sub(suffix.chars().count()); let base: String = name.chars().take(keep).collect(); - format!("{}{suffix}", base.trim_end()) - }) - .find(|n| find(wb, n).is_none()) - .unwrap_or_else(|| name.to_string()) + let base = base.trim_end(); + // Every candidate for this base and suffix width below `next` is in use already + // (names are only added), so the search goes on from there. + let key = (Taken::key(base), suffix.len()); + let next = self.next.get(&key).copied().unwrap_or(n); + if next > n { + n = next; + continue; + } + let Some(after) = n.checked_add(1) else { return name.to_string() }; + self.next.insert(key, after); + let candidate = format!("{base}{suffix}"); + if !self.contains(&candidate) { + return candidate; + } + n = after; + } + } } #[cfg(test)] diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 63f8cef761..4bd2ac1bc5 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -317,3 +317,53 @@ fn a_macro_in_a_newer_format_is_dropped_not_rewritten() { assert!(back.names.is_empty(), "{:?}", names_of(&back)); check("book.xlsx", &back, &warnings); } + +#[test] +fn many_macros_with_one_name_all_survive_save_and_reopen() { + // MAX_MACROS macros with the same name, at the length limit, so the suffixes run from " (2)" + // to " (1000)" and the kept part of the name shrinks as they widen. + let long = "é".repeat(MAX_MACRO_NAME); + for file in ["same.json", "same.xlsx"] { + let mut wb = Workbook::new(); + wb.macros = (0..MAX_MACROS).map(|i| Macro { name: long.clone(), description: format!("{i}"), ..Macro::default() }).collect(); + let (first, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&wb, file).unwrap()).unwrap(); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + assert_eq!(first.macros.len(), MAX_MACROS, "{file}"); + let names: std::collections::HashSet = first.macros.iter().map(|m| m.name.to_lowercase()).collect(); + assert_eq!(names.len(), MAX_MACROS, "{file}: every name is different"); + assert!(first.macros.iter().all(|m| m.name.chars().count() <= MAX_MACRO_NAME && m.check_limits().is_ok()), "{file}"); + assert_eq!(first.macros[0].name, long); + assert_eq!(first.macros[9].name, format!("{} (10)", "é".repeat(MAX_MACRO_NAME - 5))); + assert_eq!(first.macros[MAX_MACROS - 1].name, format!("{} (1000)", "é".repeat(MAX_MACRO_NAME - 7))); + assert!(first.macros.iter().enumerate().all(|(i, m)| m.description == format!("{i}")), "{file}: order kept"); + let (again, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&first, file).unwrap()).unwrap(); + assert!(warnings.is_empty(), "{file}: {warnings:?}"); + assert_eq!(again.macros, first.macros, "{file}"); + } +} + +#[test] +fn naming_clashing_macros_takes_linear_work() { + // Each suffix is looked up about once, not once per earlier macro with the same name. + let mut taken = Taken::default(); + // Names a file may already use, in the way of the first suffixes. + for n in 2..=500 { + taken.insert(&format!("Same ({n})")); + } + let mut out = Vec::new(); + for _ in 0..MAX_MACROS { + let name = taken.unique("SAME"); + taken.insert(&name); + out.push(name); + } + assert_eq!(out[..3], ["SAME", "SAME (501)", "SAME (502)"]); + assert!(taken.probes <= 3 * MAX_MACROS, "{} lookups", taken.probes); + // The smallest free suffix is still the one used. + let mut taken = Taken::default(); + for name in ["A", "A (3)"] { + taken.insert(name); + } + assert_eq!(taken.unique("a"), "a (2)"); + taken.insert("a (2)"); + assert_eq!(taken.unique("a"), "a (4)"); +} From 0ea5d3ee336167432072c3fc24bfbf1e7c8e7f45 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:29:18 -0400 Subject: [PATCH 058/687] Engine: a Name: token reaches a defined name that holds periods `Name:` read a name only up to the first period, so `Name:Sales.Q1` looked up `Sales` and treated `Q1` as a method, and a workbook name called Sales.Q1 could not be used in a macro, alone or as `Name:Sales.Q1.cell(1,1)`. The name now runs through periods, and the longest defined name or table that exists and ends at a period (or the end) is used; what follows is read as methods. So `Name:Data.row` is a name called Data.row, and `Name:Sales.offset(1,0)` still moves Sales. When no name matches, the error names the text before the first method, and a bad method after a dotted name is still refused. Tests cover direct resolution, reference parameters, `$ref` and `{{...}}`, with and without a chained method. --- crates/engine/src/macros/resolve.rs | 102 +++++++++++++++++++++++++--- docs/macros.md | 4 +- 2 files changed, 96 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs index d71db8cd4d..47636bd61e 100644 --- a/crates/engine/src/macros/resolve.rs +++ b/crates/engine/src/macros/resolve.rs @@ -5,7 +5,8 @@ //! //! - bases: a declared input (`Input`, `Rates`), `Output` (the output rule's anchor), `Row` or //! `Column` (the current item of the Repeat step around it), and `Name:Total` (a defined name or -//! a table's data); +//! a table's data; a name can hold periods, and the longest one that exists is used, so +//! `Name:Sales.Q1.cell(1,1)` is a cell of `Sales.Q1`); //! - methods, 1-based and chainable: `.col(n)` or `.col("Amount")` (by an input's header row, //! also after `.data`), `.row(n)`, `.cols(a..b)`, `.rows(a..)`, `.cell(r,c)` (relative to the //! top-left cell; any whole numbers, so `cell(0,1)` is the cell above), `.offset(r,c)`, @@ -177,6 +178,33 @@ fn named(name: &str, env: &Env) -> R { bad(format!("There is no name or table called '{name}'.")) } +/// Token method names. +const METHODS: &[&str] = &["col", "row", "cols", "rows", "cell", "offset", "resize", "data", "below", "right"]; + +/// Where the name ends in `run`, the text after `Name:` up to the first character a name can't +/// hold: at the end or at a period, the longest choice that is a defined name or table. When +/// none is, the name ends before the first method (`Nope.cell`), so the error names it. +fn name_end(run: &str, env: &Env) -> usize { + // One pass over the names and tables, as a single lookup makes. + let fits = |name: &str| { + !name.is_empty() + && run.get(..name.len()).is_some_and(|head| head.eq_ignore_ascii_case(name)) + && run.get(name.len()..).is_some_and(|rest| rest.is_empty() || rest.starts_with('.')) + }; + let names = env.wb.names.iter().filter(|n| n.scope.is_none() || n.scope == Some(env.active_sheet)).map(|n| n.name.as_str()); + let tables = env.wb.sheets.iter().flat_map(|sh| sh.tables.iter().map(|t| t.name.as_str())); + if let Some(end) = names.chain(tables).filter(|n| fits(n)).map(str::len).max() { + return end; + } + run.match_indices('.') + .map(|(i, _)| i) + .find(|&i| { + let next = run.get(i + 1..).unwrap_or("").split('.').next().unwrap_or(""); + METHODS.iter().any(|m| m.eq_ignore_ascii_case(next)) + }) + .unwrap_or(run.len()) +} + /// Resolves a token (see the module docs). pub fn resolve_token(text: &str, env: &Env) -> R { let t = text.trim(); @@ -233,9 +261,7 @@ pub fn resolve_token(text: &str, env: &Env) -> R { RangeRef::new(left, shift(r.end, 0, n, t)?) } _ => { - return bad(format!( - "'{t}': '{name}' with these arguments isn't a token method (col, row, cols, rows, cell, offset, resize, data, below, right)." - )); + return bad(format!("'{t}': '{name}' with these arguments isn't a token method ({}).", METHODS.join(", "))); } }; // After a method the result is plain cells: `data` no longer applies, and only `data` @@ -351,14 +377,17 @@ impl Parser<'_> { self.skip_ws(); if self.rest().get(..5).is_some_and(|h| h.eq_ignore_ascii_case("name:")) { self.at += 5; - // A name runs to the first `.` followed by a method, or the end. - let n: usize = self.rest().chars().take_while(|c| c.is_alphanumeric() || matches!(c, '_' | '\\')).map(char::len_utf8).sum(); - let name = self.rest().get(..n).unwrap_or("").to_string(); - self.at += n; + // A name can hold periods (`Sales.Q1`), so it is the longest part of the run, up to + // a period or the end, that names something; methods follow it. + let n: usize = self.rest().chars().take_while(|c| c.is_alphanumeric() || matches!(c, '_' | '\\' | '.')).map(char::len_utf8).sum(); + let run = self.rest().get(..n).unwrap_or("").to_string(); + let end = name_end(&run, env); + let name = run.get(..end).unwrap_or(""); if name.is_empty() { return bad("'Name:' needs a defined name after it."); } - return Ok((named(&name, env)?, false)); + self.at += end; + return Ok((named(name, env)?, false)); } let base = self.ident(); if base.eq_ignore_ascii_case("Output") { @@ -691,6 +720,61 @@ mod tests { } } + #[test] + fn defined_names_can_hold_periods() { + // `Sales.Q1` is one defined name, beside `Sales`; `Data.row` has a segment that reads like + // a method. The longest name that exists is used, and methods follow it. + let mut wb = wb(); + for (name, formula) in [("Sales", "Sheet1!$A$1"), ("Sales.Q1", "Sheet1!$B$2:$C$5"), ("Data.row", "'My Data'!$E$1:$E$3")] { + wb.names.push(gridcraft_model::DefinedName { + name: name.into(), + scope: None, + formula: formula.into(), + comment: String::new(), + hidden: false, + }); + } + let env = Env { wb: &wb, active_sheet: 0, inputs: &[], output: None, item: None }; + for (token, want) in [ + ("Name:Sales.Q1", "B2:C5"), + ("Name:sales.q1", "B2:C5"), + ("Name:Sales.Q1.cell(1,1)", "B2"), + ("Name:Sales.Q1.col(2).row(4)", "C5"), + ("Name:Sales", "A1"), + ("Name:Sales.offset(1,0)", "A2"), + ("Name:Data.row", "'My Data'!E1:E3"), + ("Name:Data.row.row(2)", "'My Data'!E2"), + ("Name:Data.row.cell(1,1)", "'My Data'!E1"), + ("Sales.Q1", "B2:C5"), + ] { + check(&env, token, want); + } + // In a reference parameter, `$ref` and `{{…}}`, with and without a method. + let p = json!({ + "range": "Name:Sales.Q1", + "cell": "Name:Sales.Q1.cell(1,1)", + "a": {"$ref": "Name:Sales.Q1"}, + "b": {"$ref": "Name:Sales.Q1.cell(2,2)"}, + "formula": "=SUM({{Name:Sales.Q1}})+{{Name:Sales.Q1.cell(1,2)}}" + }); + let r = resolve_params(&p, &env).unwrap(); + assert_eq!(r, json!({"range": "B2:C5", "cell": "B2", "a": "B2:C5", "b": "C3", "formula": "=SUM(B2:C5)+C2"})); + // Unknown names and bad methods still fail, naming what is wrong. + for (token, needle) in [ + ("Name:Nope.Q1", "no name or table called 'Nope.Q1'"), + ("Name:Nope.cell(1,1)", "no name or table called 'Nope'"), + ("Name:Sales.Q1.bogus(1)", "'bogus' with these arguments isn't a token method"), + ("Name:Sales.Q1.cell(1", "expected"), + ("Name:Sales.Q1.", "method name"), + ("Name:.cell(1,1)", "needs a defined name"), + ] { + let e = resolve_ref(token, &env).unwrap_err(); + assert!(e.message().contains(needle), "{token}: {e}"); + let e = resolve_params(&json!({"formula": format!("={{{{{token}}}}}")}), &env).unwrap_err(); + assert!(e.message().contains(needle), "{token} in braces: {e}"); + } + } + #[test] fn sheet_names_are_quoted_when_needed() { for (name, want) in [("Sheet1", "Sheet1"), ("My Data", "'My Data'"), ("O'Hara", "'O''Hara'"), ("2024", "'2024'"), ("", "''")] { diff --git a/docs/macros.md b/docs/macros.md index 4a08e7228f..dd14951bc0 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -57,7 +57,9 @@ Methods narrow or move a range and chain: `col(2)` or `col("Amount")`, `row(1)`, `rows(2..)`, `cell(r,c)` (relative to the top-left cell; `cell(0,1)` is the cell above), `offset(r,c)`, `resize(h,w)`, `data` (without the header row), `below(n)` and `right(n)`. `col("Amount")` looks the name up in an input's header row, directly or after `data`; on a range -without a header row it is refused. +without a header row it is refused. A defined name can hold periods: `Name:Sales.Q1` is the name +`Sales.Q1`, and `Name:Sales.Q1.cell(1,1)` its first cell, because the longest name or table that +exists is used and methods follow it. Write a token as the whole value of a reference parameter (`cell`, `range`, `target`, keys ending in `Cell` or `Range`…), as `{"$ref": "Input.col(2)"}` anywhere, or as `{{Input.col(2)}}` inside From f1a46a2b53588c8138450cf97b5b5e7952ab03ae Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:27:48 -0400 Subject: [PATCH 059/687] Docs: the macro policy summary lists the file commands that do run The policy summary said file commands never run in a macro, but File > Properties runs, and exports and printing run: without a path they only produce bytes, and with one they need the permission and approval. The summary now names what is denied and what runs, and the paragraph is wrapped to the file's line length. --- docs/macros.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index cf93148d3c..dc0c044c77 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -75,12 +75,16 @@ called Output, even when a token or input has that name. `macro.run {name, inputs?, onConflict?, overwrite?, approve?, timeLimit?}`: -1. **Policy.** File, app, window (including New Window, Hide and Unhide), automate and macro - commands, and Undo and Redo, never run in a macro. Reading or writing a file (`data.getData`, `data.fromTextCsv`, `insert.picture` or - `pageLayout.background` with a `path`, or an export or print with a `path`) needs the command - in the macro's `permissions`. Any `path` other than `null` counts, including a blank one and a - token, and the policy is checked again on the parameters as they are when the step runs. A macro that touches files, or that came with a workbook opened - from a file, runs once the person approves it: the call fails with `approve: …` and runs with +1. **Policy.** File commands other than Properties, exports and printing (such as Open, Save + and Close), app, window (including New Window, Hide and Unhide), automate and macro commands, + and Undo and Redo, never run in a macro. File › Properties runs, and so do exports and + printing (`file.exportPdf`, `exportCsv`, `exportHtml`, `print`, `printPreview`, `saveBytes`) + that only produce bytes. Reading or writing a file (`data.getData`, `data.fromTextCsv`, + `insert.picture` or `pageLayout.background` with a `path`, or an export or print with a + `path`) needs the command in the macro's `permissions`. Any `path` other than `null` counts, + including a blank one and a token, and the policy is checked again on the parameters as they + are when the step runs. A macro that touches files, or that came with a workbook opened from + a file, runs once the person approves it: the call fails with `approve: …` and runs with `approve: true`. Approval lasts for the session, until the macro changes. 2. **Footprint.** The macro first runs in dry-run mode on the real workbook, and everything is put back. Cells it would write outside its inputs that hold data, and sheets and objects it would From f553619b333767e43f17a86769314c1dc0ac1baf Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:25:49 -0400 Subject: [PATCH 060/687] Engine: the macro debugger lock also refuses edits that another command runs Session::execute checked the debugger lock only for top-level undoable commands. A command that is not undoable could run edits as nested commands and skip it: stopping Record Actions while a macro in the same workbook was being debugged saved the script inside the run's undo group, and stopping the run without keeping its changes deleted the script again. The lock now applies at every depth, and only the debugged run's own steps go ahead. --- crates/engine/src/lib.rs | 7 ++++--- crates/engine/src/macros/debug.rs | 35 ++++++++++++++++++++++++++++++- docs/macros.md | 7 ++++--- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 3d542469b5..6807d1ccef 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -412,9 +412,10 @@ impl Session { return Err(EngineError::Disabled(id.to_string(), why)); } // While a macro is being debugged its undo group is open: other edits of the workbooks it - // touched wait. Other workbooks stay editable. - let locked = self.active().is_some_and(|d| self.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid))); - if self.depth == 0 && spec.undoable && id != "macro.debug" && locked { + // touched wait, also when another command runs them (a script, or a recording that stops + // and saves). Only the run's own steps go ahead. Other workbooks stay editable. + let locked = self.active().is_some_and(|d| self.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid) && !m.stepping())); + if spec.undoable && id != "macro.debug" && locked { return Err(EngineError::Disabled(id.to_string(), macros::debug::BUSY.into())); } let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); diff --git a/crates/engine/src/macros/debug.rs b/crates/engine/src/macros/debug.rs index 04da198346..b1e53cf9b9 100644 --- a/crates/engine/src/macros/debug.rs +++ b/crates/engine/src/macros/debug.rs @@ -42,6 +42,11 @@ impl Debugger { pub fn locks(&self, doc: u64) -> bool { self.touched.contains(&doc) } + + /// Whether one of the run's steps is running (its machine is out of the session). + pub fn stepping(&self) -> bool { + self.machine.is_none() + } } /// Each open workbook's uid and its current version, to see which ones a step changes. @@ -339,7 +344,7 @@ fn changed(s: &Session, d: &Debugger) -> Vec { /// After a command panicked while a macro was being debugged: if the run lost its place, roll /// it back so its undo group doesn't stay open. pub(crate) fn recover(s: &mut Session) { - if s.macro_debug.as_ref().is_some_and(|d| d.machine.is_none()) + if s.macro_debug.as_ref().is_some_and(Debugger::stepping) && let Some(mut d) = s.macro_debug.take() && let Some(token) = d.token.take() { @@ -436,4 +441,32 @@ mod tests { s.execute("macro.debug", json!({"action": "stop", "keep": true})).unwrap(); s.execute("cell.set", json!({"cell": "B1", "input": "free"})).unwrap(); } + + #[test] + fn edits_run_by_another_command_wait_too() { + let mut s = Session::new(); + s.new_workbook(); + let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [ + {"command": "cell.set", "params": {"cell": "A1", "input": "x"}}, + {"command": "cell.set", "params": {"cell": "A2", "input": "y"}} + ]})) + .unwrap(); + let d = s.doc_mut().unwrap(); + let mut wb = (*d.wb).clone(); + wb.macros.push(m); + d.wb = std::sync::Arc::new(wb); + s.execute("automate.recordActions", json!({})).unwrap(); + s.execute("cell.set", json!({"cell": "C1", "input": "1"})).unwrap(); + s.execute("macro.debug", json!({"name": "M"})).unwrap(); + s.execute("macro.debug", json!({"action": "step"})).unwrap(); + // Record Actions changes nothing itself; stopping it saves the script with a nested + // command, which would land in the run's undo group. + let e = s.execute("automate.recordActions", json!({"stop": true, "name": "S"})).unwrap_err().to_string(); + assert!(e.contains(super::BUSY), "{e}"); + assert!(s.execute("automate.allScripts", json!({})).unwrap().as_array().unwrap().is_empty()); + // The run's own steps still go ahead. + let r = s.execute("macro.debug", json!({"action": "step"})).unwrap(); + assert_eq!(r["executed"], 2, "{r}"); + assert!(s.macro_debug.is_none()); + } } diff --git a/docs/macros.md b/docs/macros.md index 45b2b51f84..4301989ae7 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -110,9 +110,10 @@ approval needed and the footprint, without changing anything. `runTo` (`step`: a path), `stop` (`keep: true` keeps the changes) and `state`. `breakpoints` can be given with any action; `breakpointsOnly: true` makes the list replace the steps' own flags. The run stays one undo step; while it is open, edits of the workbooks it touched (its own, and any -other its steps changed) are refused with "Finish or stop the macro first", and other workbooks -stay editable. Each answer gives the next step with its parameters as they would run, the -parameters of the step that just ran, and the cells changed so far. +other its steps changed) are refused with "Finish or stop the macro first", also when another +command makes them (stopping Record Actions saves a script), and other workbooks stay +editable. Each answer gives the next step with its parameters as they would run, the parameters +of the step that just ran, and the cells changed so far. ## Storage From 8797b7183c94cba8f1675d31d0111a032981fd56 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:27:37 -0400 Subject: [PATCH 061/687] Engine: stopping a macro recording saves it in the workbook where recording started Stopping a recording converted and saved it against the active workbook. For example, starting a recording in Book1 and switching to Book2 before stopping saved the macro into Book2, inferred the input's header and column types from Book2's cells and looked up inlined macros and scripts there. The recording is now converted and saved against the workbook where it started, and the active workbook stays active. If that workbook was closed, stopping returns an error and ends the recording. --- crates/engine/src/cmd/macros.rs | 27 +++++++++++++++++++++------ crates/engine/src/tests/macros.rs | 31 +++++++++++++++++++++++++++++++ docs/macros.md | 12 +++++++----- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index 953be19824..383471f535 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -184,12 +184,21 @@ fn start_recording(s: &mut Session, relative: bool) -> Result { Ok(if relative { json!({"recording": true, "relative": true, "input": input}) } else { json!({"recording": true, "relative": false}) }) } -/// Stops recording and saves what was recorded as a macro. When saving fails (a bad name, a full -/// workbook) the recording goes on, so stopping again keeps what was recorded. +/// Stops recording and saves what was recorded as a macro, in the workbook where recording +/// started. When saving fails (a bad name, a full workbook) the recording goes on, so stopping +/// again keeps what was recorded; when that workbook was closed, the recording ends unsaved. fn stop_recording(s: &mut Session, p: &Json) -> Result { let Some(rec) = s.stop_recording() else { return Ok(json!({"recording": false})) }; let start = s.macro_recording.take(); - let saved = save_recording(s, p, &rec, start.clone()); + let at = match &start { + Some(st) => s + .docs + .iter() + .position(|d| d.uid == st.doc) + .ok_or_else(|| EngineError::Other("The workbook where recording started was closed, so the recording was not saved.".into()))?, + None => s.active_index(), + }; + let saved = save_recording(s, p, &rec, start.clone(), at); if saved.is_err() { s.recorder = Some(rec); s.macro_recording = start; @@ -197,8 +206,9 @@ fn stop_recording(s: &mut Session, p: &Json) -> Result { saved } -fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, start: Option) -> Result { - let d = s.doc()?; +/// Saves the recording into the workbook at index `at`. +fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, start: Option, at: usize) -> Result { + let d = s.docs.get(at).ok_or(EngineError::NoDocument)?; let start = start.unwrap_or_else(|| RecordingStart { doc: d.uid, sheet: d.wb.active_sheet, @@ -218,6 +228,11 @@ fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, star } let steps = converted.mac.steps.len(); let inputs: Vec = converted.mac.inputs.iter().map(|i| i.name.clone()).collect(); - s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac)}))?; + // Saved into that workbook even when another one is active now. + let active = s.active_index(); + s.set_active(at); + let saved = s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac)})); + s.set_active(active); + saved?; Ok(json!({"recording": false, "saved": name, "steps": steps, "dropped": rec.dropped, "skipped": converted.skipped, "inputs": inputs})) } diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index e16e8be3e5..323b64ed41 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1095,3 +1095,34 @@ fn a_recorded_script_run_leaves_out_the_steps_a_macro_may_not_run() { assert_eq!(recorded(&mut s, "Rec"), ["cell.set {\"cell\":\"B1\",\"input\":\"kept\"}"]); s.execute("macro.run", json!({"name": "Rec", "onConflict": "overwrite"})).unwrap(); } + +#[test] +fn a_recording_is_saved_in_the_workbook_where_it_started() { + let mut s = s(); + data(&mut s); + let first = s.doc().unwrap().uid; + s.execute("selection.set", json!({"range": "B2:C5"})).unwrap(); + s.execute("macro.record", json!({})).unwrap(); + set(&mut s, "E2", "Total"); + s.new_workbook(); + let r = s.execute("macro.record", json!({"stop": true, "name": "Here"})).unwrap(); + assert_eq!(r["saved"], "Here"); + // The active workbook stays active and gets nothing. + assert_ne!(s.doc().unwrap().uid, first); + assert!(s.doc().unwrap().wb.macros.is_empty()); + s.set_active(0); + let m = crate::macros::parse_macro(&s.execute("macro.get", json!({"name": "Here"})).unwrap()).unwrap(); + // The input's shape comes from the workbook where recording started. + assert!(m.inputs[0].header); + assert_eq!(recorded(&mut s, "Here"), ["cell.set {\"cell\":\"Input.cell(1,4)\",\"input\":\"Total\"}"]); + + // When that workbook was closed, stopping says so and ends the recording. + s.execute("macro.record", json!({})).unwrap(); + set(&mut s, "E3", "x"); + s.set_active(1); + s.close_document(0); + let err = s.execute("macro.record", json!({"stop": true, "name": "Gone"})).unwrap_err().to_string(); + assert!(err.contains("was closed"), "{err}"); + assert!(!s.is_recording()); + assert!(s.doc().unwrap().wb.macros.is_empty()); +} diff --git a/docs/macros.md b/docs/macros.md index fab64144f9..235d1f2b44 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -119,11 +119,13 @@ of the step that just ran, and the cells changed so far. ## Recording `macro.record` toggles recording (`relative: false` for absolute references; `name` and -`description` when stopping). When saving the recording fails (a name the check refuses, a workbook -at its macro limit), recording goes on, so stopping again keeps what was recorded. With relative -references the selection when recording starts is the example input: references in and near it -on its sheet become tokens (on another sheet `C4` stays that sheet's `C4`), and the input's shape, -header and column types are inferred. Toggles such as Bold are recorded with the state they set +`description` when stopping). The macro is saved in the workbook where recording started, even if +another one is active; if that workbook was closed, stopping ends the recording without saving it. +When saving the recording fails (a name the check refuses, a workbook at its macro limit), +recording goes on, so stopping again keeps what was recorded. With relative references the +selection when recording starts is the example input: references in and near it on its sheet become +tokens (on another sheet `C4` stays that sheet's `C4`), and the input's shape, header and column +types are inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so playing them back sets them rather than flipping them. Commands a macro may not run are left out and listed in the result, also from the steps of a macro or script run while recording; a macro run while recording is replaced by its steps when it has no inputs, no output From 6312c7cb4067842838e0dd970b1809b5769eef4f Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:01:32 -0400 Subject: [PATCH 062/687] Engine: only hidden names are read as macro storage A visible workbook-scope name such as `_gc_macro_0_0` matched the storage key and was read as a macro and removed when the workbook opened. Macro storage is written hidden, so only hidden names are read back; a person's own name of that shape stays a name. --- crates/engine/src/macros/mod.rs | 4 +++- crates/engine/src/macros/tests.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 9a580c7911..38016049d2 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -177,7 +177,9 @@ pub fn load(wb: &mut Workbook) -> Vec { /// The item key (`macro_`, lower-cased) of a hidden macro name, and `n` when it is a number. fn macro_item(n: &DefinedName) -> Option<(String, Option)> { - let key = stash::item_key(&n.name).filter(|k| n.scope.is_none() && k.starts_with(MACRO_KEY))?; + // Only a hidden workbook-scope name is macro storage: a person's own visible name that + // happens to look like one stays a name. + let key = stash::item_key(&n.name).filter(|k| n.hidden && n.scope.is_none() && k.starts_with(MACRO_KEY))?; let i = key.strip_prefix(MACRO_KEY).and_then(|i| i.parse().ok()); Some((key, i)) } diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 4bd2ac1bc5..9a28f14f90 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -127,6 +127,19 @@ fn hidden(name: &str, text: &str) -> DefinedName { DefinedName { name: name.into(), scope: None, formula: format!("\"{}\"", text.replace('"', "\"\"")), comment: String::new(), hidden: true } } +#[test] +fn a_visible_name_that_looks_like_macro_storage_is_left_alone() { + // Macro storage is hidden workbook-scope names; a visible `_gc_macro_0_0` someone defined + // is neither read as a macro nor removed when the workbook opens. + let mut wb = Workbook::new(); + let name = DefinedName { hidden: false, ..hidden("_gc_macro_0_0", "x") }; + wb.names.push(name.clone()); + let warnings = load(&mut wb); + assert!(warnings.is_empty(), "{warnings:?}"); + assert!(wb.macros.is_empty()); + assert_eq!(wb.names, vec![name]); +} + #[test] fn scripts_and_vba_stay_as_they_are() { // Automate scripts keep their own storage, and a VBA project is carried through unchanged. From 045cf428f5cc8362ab8f82bab96456c106a326f7 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:03:18 -0400 Subject: [PATCH 063/687] Engine: a column header made by a formula matches its token while formulas are shown `col("Amount")` read the header through the cell's display text, which is the formula while Show Formulas is on, so a header such as ="Amo"&"unt" matched nothing. The header is now the cell's value as formatted, whatever the sheet shows. --- crates/engine/src/macros/resolve.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs index 47636bd61e..b3cf438fa1 100644 --- a/crates/engine/src/macros/resolve.rs +++ b/crates/engine/src/macros/resolve.rs @@ -320,7 +320,9 @@ fn header_col(env: &Env, place: Place, row: u32, h: &str) -> Option { let sh = env.wb.sheet(place.sheet)?; let r = place.range; (r.start.col..=r.end.col) - .position(|c| crate::display::cell_text(env.wb, sh, CellRef::new(row, c)).trim().eq_ignore_ascii_case(h.trim())) + // The header's value, however the sheet shows it: with Show Formulas on, the cell's + // display text is its formula. + .position(|c| crate::display::value_text(env.wb, sh, CellRef::new(row, c)).trim().eq_ignore_ascii_case(h.trim())) .map(|i| i as i64 + 1) } @@ -555,6 +557,20 @@ mod tests { } } + #[test] + fn a_formula_header_matches_while_formulas_are_shown() { + // The header was read through the cell's display text, which is the formula while Show + // Formulas is on, so `col("Amount")` found no column headed by a formula. + let mut wb = wb(); + let sh = wb.sheet_mut(0).unwrap(); + let formula = Some(std::sync::Arc::new(gridcraft_model::Formula::new("\"Amo\"&\"unt\""))); + sh.set_cell(CellRef::new(1, 2), gridcraft_model::Cell { value: gridcraft_core::Value::text("Amount"), formula, style: Default::default() }); + sh.show_formulas = true; + let inputs = vec![("Input".to_string(), place("B2:D11"), true)]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None }; + check(&env, "Input.col(\"Amount\")", "C2:C11"); + } + #[test] fn every_token_resolves() { let wb = wb(); From c0079aabe99be12b8edc928addf1cb6708de77bb Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:22:50 -0400 Subject: [PATCH 064/687] Docs: a failed macro run rolls back workbook changes; a file it already wrote stays The intro said a macro run rolls everything back on an error, but a step that exports or prints to a path writes the file before a later step can fail, and the Running section says that file stays. The intro now limits the guarantee to workbook changes. --- docs/macros.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index dc0c044c77..7c7953ccf7 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -4,8 +4,9 @@ GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro -declares the inputs it works on, runs as one undo step, rolls everything back on an error, checks -where its results will land before it writes them, and does not touch files without permission. +declares the inputs it works on, runs as one undo step, rolls all workbook changes back on an +error (a file it already wrote stays), checks where its results will land before it writes them, +and does not touch files without permission. Agents run macros with `macro.run` like any other command. This page describes the macro format, its tokens, and running and checking macros. The engine has From 6a004bf610feba45d3efc0b166d81963066b4238 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:31:17 -0400 Subject: [PATCH 065/687] Engine: stopping Record Actions waits while a macro in its workbook is being debugged Stopping Record Actions saves a script into the workbook, which the debugger lock refuses while a macro there is paused. The refusal came after Record Actions had taken its recording, so the recording was lost. Stopping is now refused before anything changes, and stopping again after the run saves the script. --- crates/engine/src/lib.rs | 6 +++++- crates/engine/src/macros/debug.rs | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 6807d1ccef..242c86b97a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -415,7 +415,11 @@ impl Session { // touched wait, also when another command runs them (a script, or a recording that stops // and saves). Only the run's own steps go ahead. Other workbooks stay editable. let locked = self.active().is_some_and(|d| self.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid) && !m.stepping())); - if spec.undoable && id != "macro.debug" && locked { + // Stopping Record Actions saves a script into the workbook; refuse it before it takes the + // recording, so stopping again after the run keeps what was recorded. + let stops_record_actions = + id == "automate.recordActions" && (params.get("stop").and_then(Json::as_bool) == Some(true) || self.recording.is_some()); + if (spec.undoable || stops_record_actions) && id != "macro.debug" && locked { return Err(EngineError::Disabled(id.to_string(), macros::debug::BUSY.into())); } let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); diff --git a/crates/engine/src/macros/debug.rs b/crates/engine/src/macros/debug.rs index b1e53cf9b9..8c5b7e2017 100644 --- a/crates/engine/src/macros/debug.rs +++ b/crates/engine/src/macros/debug.rs @@ -468,5 +468,10 @@ mod tests { let r = s.execute("macro.debug", json!({"action": "step"})).unwrap(); assert_eq!(r["executed"], 2, "{r}"); assert!(s.macro_debug.is_none()); + // The recording was kept: stopping now saves it. + let saved = s.execute("automate.recordActions", json!({"stop": true, "name": "S"})).unwrap(); + assert_eq!(saved["saved"], "S", "{saved}"); + let scripts = s.execute("automate.allScripts", json!({})).unwrap(); + assert_eq!(scripts[0]["name"], "S", "{scripts}"); } } From 20eb282e75bedcfee6dcb8cea6163ab22cc4af7a Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 15:28:02 -0400 Subject: [PATCH 066/687] Docs: catalog count 275 / 291 in the roadmap and parity summaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The catalog went from 274 / 290 to 275 / 291 with View › Macros › Record Macro, and parity-checklist.md says so, but the ROADMAP.md headline, ui-parity.md and target-app-parity.md still said 274 / 290 (and 290 catalog ids). They now match, with revision rows. --- ROADMAP.md | 2 +- docs/target-app-parity.md | 6 +++--- docs/ui-parity.md | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 2c0ee33cd0..de39b787ad 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -21,7 +21,7 @@ This page is the summary. The detail is in: | Number | Value | Kind | |---|---|---| -| **Feature breadth** (weighted) | **~75%** | estimated, anchored on two measurements: ribbon catalog 274 / 290 (94.5%), worksheet functions 513 / 523 (98.1%; 504 working, 96.4%) | +| **Feature breadth** (weighted) | **~75%** | estimated, anchored on two measurements: ribbon catalog 275 / 291 (94.5%), worksheet functions 513 / 523 (98.1%; 504 working, 96.4%) | | **Ready for real work** | **~50%** | estimated from written weights (51.6); [target-app-parity.md](docs/target-app-parity.md) | | **Mainstream practitioner** | **~50%** | estimated: typical analyst/finance user, weekly features, × 0.90 interaction × 0.92 stability × 0.88 file exchange; [method](docs/target-app-parity.md#mainstream-practitioner) | | **Essentials user** | **~63%** | estimated: core features only, × 0.92 launch/stability × 0.95 clarity × 0.90 opening sent files; [method](docs/target-app-parity.md#essentials-user) | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index de57d10d8a..479583f294 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -13,7 +13,7 @@ generated ribbon checklist is [`parity-checklist.md`](parity-checklist.md). | Number | Value | Kind | |---|---|---| | Feature breadth (weighted by use) | **~75%** | estimated from the area table below; anchored on two measurements | -| Ribbon and menu catalog | **274 / 290 (94.5%)** | measured: `cargo xtask parity` (`crates/engine/src/catalog.rs` ∩ registered command ids) | +| Ribbon and menu catalog | **275 / 291 (94.5%)** | measured: `cargo xtask parity` (`crates/engine/src/catalog.rs` ∩ registered command ids) | | Worksheet functions, by name | **513 / 523 (98.1%)** | measured: registered functions vs Microsoft's published function list (method below) | | Worksheet functions that do real work | **504 / 523 (96.4%)** | measured: excludes 9 stubs (7 CUBE functions, WEBSERVICE, FILTERXML) | | **Ready for real work** | **~50%** | estimated; written weights below (51.6 computed) | @@ -54,7 +54,7 @@ long `.xls` track, so about half parallelizes (≈30–50 wall-clock hours with COPILOT and PY: **523 functions**. Ours: every `f!(…)` spec in `crates/functions/src/*.rs` plus `SPECIAL_FUNCTIONS` in `crates/calc/src/eval.rs` (evaluator special forms), minus the test-only `ADD2`: **505**, all of them Excel names. -- **Ribbon catalog:** `crates/engine/src/catalog.rs` (290 unique ids across 15 tabs) against the +- **Ribbon catalog:** `crates/engine/src/catalog.rs` (291 unique ids across 15 tabs) against the 313 command ids registered in `crates/engine/src/cmd/*.rs`, recomputed from source by script and identical to the committed `parity-checklist.md`. The catalog is our own transcription of Excel's main ribbon tabs; it leaves out most contextual tabs (Picture Format, Shape Format, @@ -332,7 +332,7 @@ The inventory carried over from the 2026-10-07 ROADMAP.md, updated for what land | Date | Change | Summary | |---|---|---| -| 2026-10-21 | minor | Automation: typed macros in the engine and `macro.*` commands, no interface yet (no score change) | +| 2026-10-21 | minor | Automation: typed macros in the engine and `macro.*` commands, no interface yet (no score change); catalog 274 / 290 → 275 / 291 (View › Macros › Record Macro) | | 2026-10-12 | minor | Essentials AutoFit evidence: rows auto-fit oversized fonts on open and row formatting (#833) | | 2026-10-12 | minor | Sheet tabs: reveal the selected tab on navigation and resize while preserving manual scrolling (#681); no score change | | 2026-10-12 | minor | PivotTables: values on rows (Σ Values in Rows at any position, all three layouts; `pivot.moveField` and the field pane move it; `dataOnRows` round-trips) (no score change) | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 34cb502f54..b4d66c271a 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,10 +1,10 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-19 · **Change:** minor (Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (ribbon catalog count 275 / 291) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured -separately in [parity-checklist.md](parity-checklist.md) (274 / 290). +separately in [parity-checklist.md](parity-checklist.md) (275 / 291). Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes), `crates/engine/src/cmd` (shortcuts in command specs), the owner's black-box notes @@ -161,6 +161,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-21 | minor | Ribbon catalog count 274 / 290 → 275 / 291 (View › Macros › Record Macro) | | 2026-10-19 | minor | Ctrl+Enter (`range.fill`) gives each cell what typing the input there gives: a General cell gets the automatic number format (`5%`, `$5`, `3/4/2026`, `=TODAY()`) even when it already held a value, and a cell with its own format, a formatted row or column, or Text keeps it. Cells that held a value used to keep General (`5%` showed 0.05), and empty cells took the active cell's format | | 2026-10-18 | minor | Keytip letters typed with left Alt held continue the sequence (Alt+H, O, A / Alt+H, O, I ran nothing before: Alt+letter cancelled keytips); other Alt combinations, AltGr and macOS Option are unchanged (#1846) | | 2026-10-12 | minor | The File view's recent list tells when each workbook was last changed, read off the UI thread | From 3f5f6ea4f1fc9f1ab6bb66d45f85d2693cd16b8e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:59:29 -0400 Subject: [PATCH 067/687] Engine: a name given when a recording stops never replaces an existing macro Stopping a recording with a name that a macro already has (in any case) saved over that macro's steps, inputs and description, with only Undo to get them back. A taken name now gets a suffix ("Tidy (2)"), and the save passes replace: false as a backstop. --- crates/engine/src/cmd/macros.rs | 7 ++++--- crates/engine/src/tests/macros.rs | 15 +++++++++++++++ docs/macros.md | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index 383471f535..cd1f36c67a 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -45,7 +45,7 @@ pub fn specs() -> Vec { "Record Macro", ["View", "Macros"], None, - "{stop?: bool, relative?: bool (default true; when starting), name?, description? (when stopping)} toggles recording; with relative references the selection when it starts is the example input → {recording, relative?, input?} or {recording: false, saved, steps, dropped, skipped, inputs}", + "{stop?: bool, relative?: bool (default true; when starting), name?, description? (when stopping; a taken name gets a suffix)} toggles recording; with relative references the selection when it starts is the example input → {recording, relative?, input?} or {recording: false, saved, steps, dropped, skipped, inputs}", has_doc, record ), @@ -217,8 +217,9 @@ fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, star relative: true, }); let wb = d.wb.clone(); + // A name another macro has gets a suffix, so stopping never replaces a macro. let name = match str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()) { - Some(n) => n.to_string(), + Some(n) => crate::macros::unique_name(&wb, n), None => crate::macros::next_name(&wb), }; let mut converted = crate::macros::recorder::convert(&name, &start, &rec.steps, &wb); @@ -231,7 +232,7 @@ fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, star // Saved into that workbook even when another one is active now. let active = s.active_index(); s.set_active(at); - let saved = s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac)})); + let saved = s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac), "replace": false})); s.set_active(active); saved?; Ok(json!({"recording": false, "saved": name, "steps": steps, "dropped": rec.dropped, "skipped": converted.skipped, "inputs": inputs})) diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 323b64ed41..0339ad5eeb 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1126,3 +1126,18 @@ fn a_recording_is_saved_in_the_workbook_where_it_started() { assert!(!s.is_recording()); assert!(s.doc().unwrap().wb.macros.is_empty()); } + +#[test] +fn a_name_given_when_recording_stops_never_replaces_a_macro() { + let mut s = s(); + let original = json!({"command": "cell.set", "params": {"cell": "Z9", "input": "mine"}}); + put(&mut s, json!({"name": "Tidy", "description": "keep me", "steps": [original]})); + s.execute("macro.record", json!({"relative": false})).unwrap(); + set(&mut s, "A1", "recorded"); + let r = s.execute("macro.record", json!({"stop": true, "name": "tidy"})).unwrap(); + assert_eq!(r["saved"], "tidy (2)"); + let kept = crate::macros::parse_macro(&s.execute("macro.get", json!({"name": "Tidy"})).unwrap()).unwrap(); + assert_eq!((kept.name.as_str(), kept.description.as_str(), kept.steps.len()), ("Tidy", "keep me", 1)); + assert_eq!(recorded(&mut s, "Tidy"), ["cell.set {\"cell\":\"Z9\",\"input\":\"mine\"}"]); + assert_eq!(recorded(&mut s, "tidy (2)"), ["cell.set {\"cell\":\"A1\",\"input\":\"recorded\"}"]); +} diff --git a/docs/macros.md b/docs/macros.md index 235d1f2b44..9f1461fc26 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -119,7 +119,8 @@ of the step that just ran, and the cells changed so far. ## Recording `macro.record` toggles recording (`relative: false` for absolute references; `name` and -`description` when stopping). The macro is saved in the workbook where recording started, even if +`description` when stopping; a name that another macro has gets a suffix, such as `Tidy (2)`, so +stopping never replaces a macro). The macro is saved in the workbook where recording started, even if another one is active; if that workbook was closed, stopping ends the recording without saving it. When saving the recording fails (a name the check refuses, a workbook at its macro limit), recording goes on, so stopping again keeps what was recorded. With relative references the From ea691f703c5cdbcdacef3123fd553750fb7508c8 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:27:23 -0400 Subject: [PATCH 068/687] Engine: a stopped recording reports the steps cut at the macro step limit in dropped A recording keeps up to 100,000 commands, and inlined macro and script runs can add more, but a macro holds at most 10,000 steps. Stopping truncated the converted steps to that limit without saying so: dropped counted only the recorder's own overflow, so a 10,005-step recording saved a 10,000-step macro and reported nothing missing. The steps cut at the macro limit are now added to dropped. --- crates/engine/src/cmd/macros.rs | 6 +++++- crates/engine/src/tests/macros.rs | 22 ++++++++++++++++++++++ docs/macros.md | 9 +++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index cd1f36c67a..1af881587f 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -223,6 +223,8 @@ fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, star None => crate::macros::next_name(&wb), }; let mut converted = crate::macros::recorder::convert(&name, &start, &rec.steps, &wb); + // Steps past the macro limit are left out and counted with the ones the recorder dropped. + let cut = converted.mac.steps.len().saturating_sub(gridcraft_model::MAX_MACRO_STEPS); converted.mac.steps.truncate(gridcraft_model::MAX_MACRO_STEPS); if let Some(desc) = str_param(p, "description") { converted.mac.description = desc.chars().take(gridcraft_model::MAX_MACRO_TEXT).collect(); @@ -235,5 +237,7 @@ fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, star let saved = s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac), "replace": false})); s.set_active(active); saved?; - Ok(json!({"recording": false, "saved": name, "steps": steps, "dropped": rec.dropped, "skipped": converted.skipped, "inputs": inputs})) + Ok( + json!({"recording": false, "saved": name, "steps": steps, "dropped": rec.dropped.saturating_add(cut), "skipped": converted.skipped, "inputs": inputs}), + ) } diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 0339ad5eeb..3f0901c949 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1141,3 +1141,25 @@ fn a_name_given_when_recording_stops_never_replaces_a_macro() { assert_eq!(recorded(&mut s, "Tidy"), ["cell.set {\"cell\":\"Z9\",\"input\":\"mine\"}"]); assert_eq!(recorded(&mut s, "tidy (2)"), ["cell.set {\"cell\":\"A1\",\"input\":\"recorded\"}"]); } + +#[test] +fn steps_past_the_macro_limit_are_counted_as_dropped() { + let mut s = s(); + s.execute("macro.record", json!({"relative": false})).unwrap(); + let (doc, selection) = (s.doc().unwrap().uid, s.doc().unwrap().selection.clone()); + let rec = s.recorder.as_mut().unwrap(); + for i in 0..gridcraft_model::MAX_MACRO_STEPS + 5 { + rec.push(crate::record::RecordedStep { + doc, + sheet: 0, + sheet_name: "Sheet1".into(), + selection: selection.clone(), + id: "cell.set".into(), + params: json!({"cell": "A1", "input": i.to_string()}), + }); + } + rec.dropped = 3; + let r = s.execute("macro.record", json!({"stop": true, "name": "Long"})).unwrap(); + assert_eq!(r["steps"], gridcraft_model::MAX_MACRO_STEPS); + assert_eq!(r["dropped"], 8, "{r}"); +} diff --git a/docs/macros.md b/docs/macros.md index 9f1461fc26..6cc67d4e81 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -123,10 +123,11 @@ of the step that just ran, and the cells changed so far. stopping never replaces a macro). The macro is saved in the workbook where recording started, even if another one is active; if that workbook was closed, stopping ends the recording without saving it. When saving the recording fails (a name the check refuses, a workbook at its macro limit), -recording goes on, so stopping again keeps what was recorded. With relative references the -selection when recording starts is the example input: references in and near it on its sheet become -tokens (on another sheet `C4` stays that sheet's `C4`), and the input's shape, header and column -types are inferred. Toggles such as Bold are recorded with the state they set +recording goes on, so stopping again keeps what was recorded. A recording keeps up to 100,000 +commands and a macro up to 10,000 steps; steps past either limit are left out and counted in +`dropped`. With relative references the selection when recording starts is the example input: +references in and near it on its sheet become tokens (on another sheet `C4` stays that sheet's +`C4`), and the input's shape, header and column types are inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so playing them back sets them rather than flipping them. Commands a macro may not run are left out and listed in the result, also from the steps of a macro or script run while recording; a macro run while recording is replaced by its steps when it has no inputs, no output From bbf1bc441ba1c382187e5854cb4617c27b3004c0 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Wed, 21 Oct 2026 17:28:03 -0400 Subject: [PATCH 069/687] Engine: stopping a recording waits while a macro in its workbook is being debugged Stopping a recording saved the macro with a nested macro.save while a debugged run's undo group could be open on the same workbook. Stopping the run without keeping its changes then deleted the macro that macro.record had reported as saved. Stopping now fails with "Finish or stop the macro first" under macro.record before anything changes, and the recording goes on, as when a save fails for another reason, so stopping again after the run ends saves it. --- crates/engine/src/cmd/macros.rs | 9 +++++++-- crates/engine/src/tests/macros.rs | 19 +++++++++++++++++++ docs/macros.md | 22 +++++++++++----------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index 1af881587f..426d064928 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -185,8 +185,9 @@ fn start_recording(s: &mut Session, relative: bool) -> Result { } /// Stops recording and saves what was recorded as a macro, in the workbook where recording -/// started. When saving fails (a bad name, a full workbook) the recording goes on, so stopping -/// again keeps what was recorded; when that workbook was closed, the recording ends unsaved. +/// started. When saving fails (a bad name, a full workbook, a macro being debugged there) the +/// recording goes on, so stopping again keeps what was recorded; when that workbook was closed, +/// the recording ends unsaved. fn stop_recording(s: &mut Session, p: &Json) -> Result { let Some(rec) = s.stop_recording() else { return Ok(json!({"recording": false})) }; let start = s.macro_recording.take(); @@ -209,6 +210,10 @@ fn stop_recording(s: &mut Session, p: &Json) -> Result { /// Saves the recording into the workbook at index `at`. fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, start: Option, at: usize) -> Result { let d = s.docs.get(at).ok_or(EngineError::NoDocument)?; + // A macro being debugged there could still roll the saved macro back with its run. + if s.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid)) { + return Err(EngineError::Disabled("macro.record".into(), crate::macros::debug::BUSY.into())); + } let start = start.unwrap_or_else(|| RecordingStart { doc: d.uid, sheet: d.wb.active_sheet, diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 3f0901c949..df6e59606a 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1163,3 +1163,22 @@ fn steps_past_the_macro_limit_are_counted_as_dropped() { assert_eq!(r["steps"], gridcraft_model::MAX_MACRO_STEPS); assert_eq!(r["dropped"], 8, "{r}"); } + +#[test] +fn a_recording_waits_while_a_macro_in_its_workbook_is_debugged() { + let mut s = s(); + stepper(&mut s); + s.execute("macro.record", json!({"relative": false})).unwrap(); + set(&mut s, "Z1", "typed"); + debug(&mut s, json!({"name": "Walk", "inputs": "B2:C5"})); + debug(&mut s, json!({"action": "step"})); + // Saved now, the macro would go into the run's undo group, and stopping the run without + // keeping its changes would delete it again. + let r = s.execute("macro.record", json!({"stop": true, "name": "Typed"})); + assert!(matches!(&r, Err(EngineError::Disabled(id, why)) if id == "macro.record" && why == crate::macros::debug::BUSY), "{r:?}"); + assert!(s.is_recording()); + debug(&mut s, json!({"action": "stop"})); + let r = s.execute("macro.record", json!({"stop": true, "name": "Typed"})).unwrap(); + assert_eq!(r["saved"], "Typed"); + assert_eq!(recorded(&mut s, "Typed"), ["cell.set {\"cell\":\"Z1\",\"input\":\"typed\"}"]); +} diff --git a/docs/macros.md b/docs/macros.md index 6cc67d4e81..5030687074 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -122,17 +122,17 @@ of the step that just ran, and the cells changed so far. `description` when stopping; a name that another macro has gets a suffix, such as `Tidy (2)`, so stopping never replaces a macro). The macro is saved in the workbook where recording started, even if another one is active; if that workbook was closed, stopping ends the recording without saving it. -When saving the recording fails (a name the check refuses, a workbook at its macro limit), -recording goes on, so stopping again keeps what was recorded. A recording keeps up to 100,000 -commands and a macro up to 10,000 steps; steps past either limit are left out and counted in -`dropped`. With relative references the selection when recording starts is the example input: -references in and near it on its sheet become tokens (on another sheet `C4` stays that sheet's -`C4`), and the input's shape, header and column types are inferred. Toggles such as Bold are recorded with the state they set -(`"on": true`), so playing them back sets them rather than flipping them. Commands a macro may not -run are left out and listed in the result, also from the steps of a macro or script run while -recording; a macro run while recording is replaced by its steps when it has no inputs, no output -and only plain commands (otherwise it is left out and listed), and an Automate script run by the -script's steps. +When saving the recording fails (a name the check refuses, a workbook at its macro limit, a +macro being debugged in that workbook), recording goes on, so stopping again keeps what was +recorded. A recording keeps up to 100,000 commands and a macro up to 10,000 steps; steps past +either limit are left out and counted in `dropped`. With relative references the selection when +recording starts is the example input: references in and near it on its sheet become tokens (on +another sheet `C4` stays that sheet's `C4`), and the input's shape, header and column types are +inferred. Toggles such as Bold are recorded with the state they set (`"on": true`), so playing +them back sets them rather than flipping them. Commands a macro may not run are left out and +listed in the result, also from the steps of a macro or script run while recording; a macro run +while recording is replaced by its steps when it has no inputs, no output and only plain commands +(otherwise it is left out and listed), and an Automate script run by the script's steps. ## Other commands and storage From e177bf1c44581da8bf29ebf7e261b8a988449c05 Mon Sep 17 00:00:00 2001 From: Tyrel Haveman <1434399+tyrel@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:41:28 -0700 Subject: [PATCH 070/687] calc: hardening: overlong digits, NaN indexes, REGEXREPLACE output (#2163, #2162) - Text coercion: digits too many for a finite number are not a number, and a fraction must come out finite, so "1 <400 digits>/<400 digits>" (inf/inf) no longer coerces to NaN (test overlong_digits_stay_text). - VLOOKUP/HLOOKUP and the D-functions' field index refuse NaN, which passed their "< 1" checks and became index 0, then underflowed (test nan_index_is_a_value_error; hostile_inputs_never_panic now also passes NaN and infinity to every function). - REGEXREPLACE replaces match by match and gives up once the result can only be too long, instead of building every replacement first: the 32,767 x 32,767 worst case went from 1.05 GB peak to 7 MB (test regexreplace_bounds_its_result). #2165 (MAP's size check) is already fixed on main by the broadcast cap (836b2698, #2182), whose test covers MAP. Also adds my contributors/people.toml entry. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019Nnsfu3QGqjD1aFZbBgUWP --- ROADMAP.md | 4 +++- contributors/people.toml | 3 +++ crates/core/src/parse.rs | 19 +++++++++++++++++-- crates/functions/src/database.rs | 5 +++-- crates/functions/src/lib.rs | 31 +++++++++++++++++++++++++++++++ crates/functions/src/lookup.rs | 5 +++-- crates/functions/src/text.rs | 16 +++++++++++++++- 7 files changed, 75 insertions(+), 8 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 2e04e91232..1faf680ca3 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (Page Setup paper sizes: JIS B4/B5/B6, the Japanese postcards and envelopes, and A6; long flat operator chains up to Excel's formula limit) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** trivial (progress log: formula hardening, #2163/#2162; previously minor: Page Setup paper sizes: JIS B4/B5/B6, the Japanese postcards and envelopes, and A6; long flat operator chains up to Excel's formula limit) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -135,6 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-22 | Formula hardening: digits too many for a finite number are text, so `"1 <400 digits>/<400 digits>"` no longer coerces to NaN; a NaN column index in VLOOKUP, HLOOKUP and the D-functions is `#VALUE!` instead of an index underflow; REGEXREPLACE stops building a result as soon as it can only be too long (a worst case went from 1 GB to 7 MB) (#2163, #2162) | | 2026-10-21 | Page Setup paper sizes match Japanese Excel: `B4`/`B5`/`B6` are the JIS sizes (ISO B4/B5/B6 stay reachable by name), so a Japanese B5 worksheet prints at 182×257 mm; Japanese Postcard (100×148 mm), Double Postcard (148×200 mm), Envelope #3/#4 (120×235/90×205 mm), Kaku #2/#3 (240×332/216×277 mm) and A6 are added, both sizes reach the PDF page (`file.exportPdf`/`file.print` MediaBox) and the size survives an XLSX round-trip; the Page Layout Size menu lists them. `B4`/`B5` used to be the ISO sizes, which is changed here | | 2026-10-20 | Japanese interface catalog covers every key the Brazilian Portuguese catalog translates: the 398-label shared core plus 545 dialog, message, Backstage and menu strings (943 total, ~43% of ~2,200). A test parses `pt.rs`'s source, so the two catalogs stay in step | | 2026-10-20 | Interface translated into Dutch (518 labels: ribbon, context menus, File view, dialog titles, Options); `nl`, `nl-NL`, `nl-BE` locale tags resolve to Dutch | @@ -220,6 +221,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-22 | trivial | Progress log: formula hardening (#2163, #2162) | | 2026-10-21 | minor | Page Setup paper sizes: JIS B4/B5/B6 (ISO by name), Japanese postcards and envelopes, A6; PDF page size follows, XLSX round-trips | | 2026-10-20 | minor | Dutch (nl) listed under other shipped languages; progress log row | | 2026-10-19 | minor | Progress log: long flat operator chains (#916) | diff --git a/contributors/people.toml b/contributors/people.toml index 62635c8ae3..88e496fc7b 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -93,3 +93,6 @@ sync_github_name = false [people.bilalmughal] sync_github_name = true + +[people.tyrel] +real_name = "Tyrel Haveman" diff --git a/crates/core/src/parse.rs b/crates/core/src/parse.rs index b58586aef0..3f2b97a8ad 100644 --- a/crates/core/src/parse.rs +++ b/crates/core/src/parse.rs @@ -167,7 +167,8 @@ fn parse_plain(t: &str) -> Option<(f64, Option<&'static str>)> { } /// A plain ASCII number: digits, with an optional `.digits` fraction when `frac` is set. Rejects -/// `nan`, `inf`, signs and exponents that `f64::from_str` would accept. +/// `nan`, `inf`, signs and exponents that `f64::from_str` would accept, and digits too many for a +/// finite number (#2163). fn plain_number(s: &str, frac: bool) -> Option { let (int, dec) = match s.split_once('.') { Some((i, d)) if frac => (i, Some(d)), @@ -177,7 +178,7 @@ fn plain_number(s: &str, frac: bool) -> Option { if !digits(int) || dec.is_some_and(|d| !digits(d)) { return None; } - s.parse().ok() + s.parse().ok().filter(|x: &f64| x.is_finite()) } /// `1/2` (only with a whole part, else it's a date), `1 1/2`, `0 3/4`. @@ -192,6 +193,9 @@ fn parse_fraction(t: &str) -> Option { return None; } let v = w + a / b; + if !v.is_finite() { + return None; + } Some(if neg { -v } else { v }) } @@ -366,6 +370,17 @@ mod tests { parse_input(s, DateSystem::D1900) } + /// #2163: digits too many for a finite number don't make one (∞/∞ was NaN). + #[test] + fn overlong_digits_stay_text() { + let big = "9".repeat(400); + for s in [format!("1 {big}/{big}"), format!("1 {big}/2"), format!("1 1/{big}0"), format!("{big}:00"), format!("1:00:{big}.5")] { + assert_eq!(p(&s).value, Value::text(s.as_str()), "{s}"); + assert_eq!(parse_number_text(&s), None, "{s}"); + } + assert_eq!(parse_number_text("1 1/2"), Some(1.5)); + } + #[test] fn non_digit_time_and_fraction_parts_stay_text() { for s in ["1:nan", "nan:30", "1:30:NaN", "1 nan/2", "1 1/nan", "inf:00", "1e1:00", "+5:00"] { diff --git a/crates/functions/src/database.rs b/crates/functions/src/database.rs index 67132e98fe..455b8a1d66 100644 --- a/crates/functions/src/database.rs +++ b/crates/functions/src/database.rs @@ -35,10 +35,11 @@ fn field_index(db: &Array, a: &[Arg]) -> R> { match v { Value::Number(n) => { let i = n.trunc(); - if i < 1.0 || i > db.cols as f64 { + // NaN too: it would pass both comparisons and become index 0 (#2163). + if i.is_nan() || i < 1.0 || i > db.cols as f64 { return Err(CellError::Value); } - Ok(Some(i as usize - 1)) + Ok(Some((i as usize).saturating_sub(1))) } Value::Text(t) => find_column(db, &t).map(Some).ok_or(CellError::Value), Value::Bool(_) => Err(CellError::Value), diff --git a/crates/functions/src/lib.rs b/crates/functions/src/lib.rs index d7ccc87264..f870f3fe2f 100644 --- a/crates/functions/src/lib.rs +++ b/crates/functions/src/lib.rs @@ -264,6 +264,34 @@ mod tests { } } + /// #2163: a NaN column index is refused (it used to become 0 and underflow to `usize::MAX`). + #[test] + fn nan_index_is_a_value_error() { + use crate::util::testutil::*; + use gridcraft_core::CellError; + let table = rf(arr(vec![vec![tv("Field"), tv("Other")], vec![nv(1.0), nv(2.0)]])); + let nan = || av(Value::Number(f64::NAN)); + assert_eq!(ev("VLOOKUP", vec![n(1.0), table.clone(), nan()]), Value::Error(CellError::Value)); + assert_eq!(ev("HLOOKUP", vec![t("Field"), table.clone(), nan()]), Value::Error(CellError::Value)); + assert_eq!(ev("DSUM", vec![table.clone(), nan(), table.clone()]), Value::Error(CellError::Value)); + assert_eq!(ev("VLOOKUP", vec![n(1.0), table, n(2.0)]), Value::Number(2.0)); + } + + /// #2162: REGEXREPLACE gives up on a too-long result while building it, and otherwise replaces + /// as before (every match, `$n` groups). + #[test] + fn regexreplace_bounds_its_result() { + use crate::util::testutil::*; + use gridcraft_core::CellError; + let many = "a".repeat(32_767); + assert_eq!(ev("REGEXREPLACE", vec![t(&many), t("."), t(&"b".repeat(32_767))]), Value::Error(CellError::Value)); + assert_eq!(ev("REGEXREPLACE", vec![t(&many), t("a"), t("b")]), Value::text("b".repeat(32_767))); + assert_eq!(ev("REGEXREPLACE", vec![t("a1b22c"), t("[0-9]+"), t("#")]), Value::text("a#b#c")); + assert_eq!(ev("REGEXREPLACE", vec![t("2026-10-22"), t("(\\d+)-(\\d+)-(\\d+)"), t("$3/$2/$1")]), Value::text("22/10/2026")); + assert_eq!(ev("REGEXREPLACE", vec![t("abc"), t("x*"), t("-")]), Value::text("-a-b-c-")); + assert_eq!(ev("REGEXREPLACE", vec![t("a1b2"), t("[0-9]"), t("#"), n(2.0)]), Value::text("a1b#")); + } + #[test] fn hostile_inputs_never_panic() { use crate::util::testutil::*; @@ -279,6 +307,9 @@ mod tests { n(-1e308), n(1e10), n(2.5), + // Numbers no formula should make, but code can (#2163). + av(Value::Number(f64::NAN)), + av(Value::Number(f64::INFINITY)), t(""), t("abc"), t("*?~"), diff --git a/crates/functions/src/lookup.rs b/crates/functions/src/lookup.rs index f9c0b6f44a..acb23f2eaf 100644 --- a/crates/functions/src/lookup.rs +++ b/crates/functions/src/lookup.rs @@ -134,10 +134,11 @@ fn vh_lookup(args: &[Arg], vertical: bool) -> R { return Err(*e); } let idx = num(args, 2)?; - if idx < 1.0 { + // NaN too: it would pass `idx < 1.0` and become index 0 (#2163). + if idx.is_nan() || idx < 1.0 { return Err(CellError::Value); } - let idx = idx.trunc() as usize - 1; + let idx = (idx.trunc() as usize).saturating_sub(1); let approx = opt_bool(args, 3, true)?; let (keys, len) = if vertical { (column(&table, 0), table.cols) } else { (row(&table, 0), table.rows) }; if idx >= len { diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index 71a6ccb4bc..096e329903 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -769,7 +769,21 @@ fn regexreplace(a: &[Arg], _c: &mut dyn Ctx) -> R { let occ = opt_int(a, 3, 0)?; let re = build_regex(&text(a, 1)?, case_arg(a, 4)?)?; if occ == 0 { - return check_len(re.replace_all(&s, rep.as_str()).into_owned()); + // Match by match, giving up once the text can only be too long (a character is at most + // four bytes) rather than building every replacement first (#2162). + let mut out = String::new(); + let mut last = 0; + for cap in re.captures_iter(&s) { + let Some(m) = cap.get(0) else { continue }; + out.push_str(s.get(last..m.start()).unwrap_or_default()); + cap.expand(&rep, &mut out); + last = m.end(); + if out.len() > MAX_TEXT * 4 { + return Err(CellError::Value); + } + } + out.push_str(s.get(last..).unwrap_or_default()); + return check_len(out); } let caps: Vec = re.captures_iter(&s).collect(); let k = occ.unsigned_abs() as usize; From 742a6e8535dde599073ddffb9e55a3f2b58cd83e Mon Sep 17 00:00:00 2001 From: Tyrel Haveman <1434399+tyrel@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:45:50 -0700 Subject: [PATCH 071/687] functions: REGEXREPLACE bounds each replacement as it expands it (#2162) From the local CodeRabbit review: the result was bounded per match, but one match's expansion was built whole first, so a replacement repeating $0 10,000 times over a 32,767-character match allocated about 330 MB before the check. The replacement is now split into pieces as regex's Captures::expand reads it (template_expands_like_regex checks they agree) and appended piece by piece within the bound: 327 MB peak became 7 MB (regexreplace_bounds_its_result). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019Nnsfu3QGqjD1aFZbBgUWP --- crates/functions/src/lib.rs | 3 ++ crates/functions/src/text.rs | 100 ++++++++++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 12 deletions(-) diff --git a/crates/functions/src/lib.rs b/crates/functions/src/lib.rs index f870f3fe2f..e4f7a1f248 100644 --- a/crates/functions/src/lib.rs +++ b/crates/functions/src/lib.rs @@ -290,6 +290,9 @@ mod tests { assert_eq!(ev("REGEXREPLACE", vec![t("2026-10-22"), t("(\\d+)-(\\d+)-(\\d+)"), t("$3/$2/$1")]), Value::text("22/10/2026")); assert_eq!(ev("REGEXREPLACE", vec![t("abc"), t("x*"), t("-")]), Value::text("-a-b-c-")); assert_eq!(ev("REGEXREPLACE", vec![t("a1b2"), t("[0-9]"), t("#"), n(2.0)]), Value::text("a1b#")); + // One match whose replacement repeats it 10,000 times: refused before it is built. + assert_eq!(ev("REGEXREPLACE", vec![t(&many), t("^.*$"), t(&"$0".repeat(10_000))]), Value::Error(CellError::Value)); + assert_eq!(ev("REGEXREPLACE", vec![t(&many), t("^.*$"), t(&"$0".repeat(10_000)), n(1.0)]), Value::Error(CellError::Value)); } #[test] diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index 096e329903..95eb6784d9 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -763,24 +763,87 @@ fn convert_replacement(r: &str) -> String { out } +/// A piece of a replacement: literal text or a capture group (by number or name). +enum Piece { + Text(String), + Index(usize), + Name(String), +} + +/// A replacement split into pieces as `regex::Captures::expand` reads it: `$$` is `$`, `$name` takes +/// the longest run of `[_0-9A-Za-z]`, `${name}` ends at `}` (`${}` is empty), a name of digits is a +/// group number, and a `$` that starts no reference is itself. +fn template(rep: &str) -> Vec { + let mut out = Vec::new(); + let mut lit = String::new(); + let mut rest = rep; + while let Some(i) = rest.find('$') { + lit.push_str(&rest[..i]); + rest = &rest[i + 1..]; + if let Some(r) = rest.strip_prefix('$') { + lit.push('$'); + rest = r; + continue; + } + // A braced name is a reference even when empty (it expands to nothing). + let braced = rest.strip_prefix('{').and_then(|b| b.find('}').map(|j| (&b[..j], &b[j + 1..]))); + let (name, after) = braced.unwrap_or_else(|| { + let j = rest.find(|c: char| !(c == '_' || c.is_ascii_alphanumeric())).unwrap_or(rest.len()); + (&rest[..j], &rest[j..]) + }); + if braced.is_none() && name.is_empty() { + lit.push('$'); + continue; + } + if !lit.is_empty() { + out.push(Piece::Text(std::mem::take(&mut lit))); + } + out.push(match name.parse::() { + Ok(n) => Piece::Index(n), + Err(_) => Piece::Name(name.to_string()), + }); + rest = after; + } + lit.push_str(rest); + if !lit.is_empty() { + out.push(Piece::Text(lit)); + } + out +} + +/// Append a match's replacement to `out`, piece by piece; `None` once `out` would pass `limit` +/// bytes, before anything that long is built (#2162). +fn expand_capped(cap: ®ex::Captures, pieces: &[Piece], out: &mut String, limit: usize) -> Option<()> { + for p in pieces { + let s = match p { + Piece::Text(t) => t.as_str(), + Piece::Index(i) => cap.get(*i).map_or("", |m| m.as_str()), + Piece::Name(n) => cap.name(n).map_or("", |m| m.as_str()), + }; + if out.len().saturating_add(s.len()) > limit { + return None; + } + out.push_str(s); + } + Some(()) +} + fn regexreplace(a: &[Arg], _c: &mut dyn Ctx) -> R { let s = text(a, 0)?; - let rep = convert_replacement(&text(a, 2)?); + let rep = template(&convert_replacement(&text(a, 2)?)); let occ = opt_int(a, 3, 0)?; let re = build_regex(&text(a, 1)?, case_arg(a, 4)?)?; + // Built match by match within a bound past which the text can only be too long (a character + // is at most four bytes), instead of building every replacement first (#2162). + let limit = MAX_TEXT * 4; + let mut out = String::new(); if occ == 0 { - // Match by match, giving up once the text can only be too long (a character is at most - // four bytes) rather than building every replacement first (#2162). - let mut out = String::new(); let mut last = 0; for cap in re.captures_iter(&s) { let Some(m) = cap.get(0) else { continue }; out.push_str(s.get(last..m.start()).unwrap_or_default()); - cap.expand(&rep, &mut out); + expand_capped(&cap, &rep, &mut out, limit).ok_or(CellError::Value)?; last = m.end(); - if out.len() > MAX_TEXT * 4 { - return Err(CellError::Value); - } } out.push_str(s.get(last..).unwrap_or_default()); return check_len(out); @@ -790,10 +853,9 @@ fn regexreplace(a: &[Arg], _c: &mut dyn Ctx) -> R { let idx = if occ > 0 { k.checked_sub(1) } else { caps.len().checked_sub(k) }; let Some(cap) = idx.and_then(|i| caps.get(i)) else { return text_val(s) }; let Some(m) = cap.get(0) else { return text_val(s) }; - let mut out = String::new(); - out.push_str(&s[..m.start()]); - cap.expand(&rep, &mut out); - out.push_str(&s[m.end()..]); + out.push_str(s.get(..m.start()).unwrap_or_default()); + expand_capped(cap, &rep, &mut out, limit).ok_or(CellError::Value)?; + out.push_str(s.get(m.end()..).unwrap_or_default()); check_len(out) } @@ -971,6 +1033,20 @@ pub(crate) fn specs() -> Vec { mod tests { use gridcraft_core::{CellError, Value}; + /// The pieces expand exactly as `regex::Captures::expand` does. + #[test] + fn template_expands_like_regex() { + let re = regex::Regex::new(r"(?\d+)-(\d+)").unwrap(); + let cap = re.captures("x 2026-10 y").unwrap(); + for rep in ["$1", "${1}", "$1a", "${1}a", "$y", "${y}!", "$$", "$$1", "a$", "$}", "${", "${}", "${y", "$0|$2|$9", "$-1", "€$1€", ""] { + let mut want = String::new(); + cap.expand(rep, &mut want); + let mut got = String::new(); + assert!(super::expand_capped(&cap, &super::template(rep), &mut got, usize::MAX).is_some()); + assert_eq!(got, want, "{rep:?}"); + } + } + use crate::util::testutil::*; #[test] From c39d2ad197e00fe4bc55572e075bf1702b1cab40 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:47:21 +0200 Subject: [PATCH 072/687] Keep '!' in structured-reference columns intact on XLSX save Found while testing crates/xlsx/src/fmla.rs:101 (with crates/formula/src/printer.rs:181) Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/formula/src/printer.rs | 24 +++++++++++++++++++++++- crates/xlsx/src/fmla.rs | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/formula/src/printer.rs b/crates/formula/src/printer.rs index 055b0f1458..e693511681 100644 --- a/crates/formula/src/printer.rs +++ b/crates/formula/src/printer.rs @@ -85,6 +85,28 @@ pub fn struct_ref_file(s: &StructRef) -> String { write_struct_ref(s, false) } +/// Splits a sheet-qualified name at its last `!` outside brackets. A `!` inside brackets +/// belongs to a structured reference's column (`Table1[Done!]`, `Table1[#REF!]`). +fn sheet_split(n: &str) -> Option<(&str, &str)> { + let mut depth = 0usize; + let mut escaped = false; + let mut at = None; + for (i, c) in n.char_indices() { + if escaped { + escaped = false; + continue; + } + match c { + '\'' if depth > 0 => escaped = true, + '[' => depth = depth.saturating_add(1), + ']' => depth = depth.saturating_sub(1), + '!' if depth == 0 => at = Some(i), + _ => {} + } + } + at.and_then(|i| Some((n.get(..i)?, n.get(i + 1..)?))) +} + fn write_struct_ref(s: &StructRef, at_shorthand: bool) -> String { let esc = |c: &str| c.replace('\'', "''").replace('[', "'[").replace(']', "']").replace('#', "'#"); let item = |i: &StructItem| match i { @@ -178,7 +200,7 @@ fn write_expr(s: &mut String, e: &Expr, r1c1: Option) { Some(at) => s.push_str(&reference_r1c1(r, at)), None => s.push_str(&reference_a1(r)), }, - Expr::Name(n) => match n.rsplit_once('!') { + Expr::Name(n) => match sheet_split(n) { // A sheet-qualified name is stored with the bare sheet name (`My Sheet!Total`): quote it so it parses back. Some((sheet, name)) => { s.push_str("e_sheet(sheet)); diff --git a/crates/xlsx/src/fmla.rs b/crates/xlsx/src/fmla.rs index bef43a5aad..c5e1f686c9 100644 --- a/crates/xlsx/src/fmla.rs +++ b/crates/xlsx/src/fmla.rs @@ -183,6 +183,18 @@ mod tests { } } + #[test] + fn struct_ref_column_with_bang() { + // A `!` inside the brackets is part of the column name, not a sheet separator. + assert_eq!(to_file(&Formula::new("SUM(Table1[Done!])")), "SUM(Table1[Done!])"); + assert_eq!(text_to_file("=Table1[[#This Row],[a!b]]"), "Table1[[#This Row],[a!b]]"); + // `Table1[#REF!]` was found by the command-sequence fuzzer (seed 40). + for text in ["SUM(Table1[Done!])", "Table1[#REF!]", "Table1[@[x!]]", "Sheet2!A1+Table1[[#All],[a!b]]"] { + let f = Formula::new(text); + assert_eq!(from_file(&to_file(&f)), f.text); + } + } + #[test] fn prefixes() { assert_eq!(to_file(&Formula::new("XLOOKUP(1,A:A,B:B)+SUM(A1)")), "_xlfn.XLOOKUP(1,A:A,B:B)+SUM(A1)"); From 8307d3cb0526c7af66645cf9d7c79f01e77be632 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 22 Oct 2026 08:25:54 +0000 Subject: [PATCH 073/687] Treat empty instance_num in TEXTBEFORE/TEXTAFTER as 1 Found while testing crates/functions/src/text.rs:553 Co-Authored-By: Claude Opus 5.5 --- crates/calc/src/tests.rs | 13 +++++++++++++ crates/functions/src/text.rs | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index e5e287d381..622ed6cc9e 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -1768,3 +1768,16 @@ fn a_dynamic_array_of_one_value_still_anchors_a_spill() { t.set("E11", "=ROWS(D1#)"); assert_eq!(t.get("E11"), Value::Error(CellError::Ref)); } + +#[test] +fn text_before_after_empty_instance_defaults_to_one() { + let mut t = T::new(); + t.set("A1", "=TEXTBEFORE(\"Little Red Riding Hood\",\"red\",,1)"); + assert_eq!(t.get("A1"), Value::Text("Little ".into())); + t.set("A2", "=TEXTAFTER(\"a-b\",\"-\",,,,\"nf\")"); + assert_eq!(t.get("A2"), Value::Text("b".into())); + t.set("A3", "=TEXTAFTER(\"a-b\",\"x\",,,,\"nf\")"); + assert_eq!(t.get("A3"), Value::Text("nf".into())); + t.set("A4", "=TEXTBEFORE(\"a-b\",\"-\",0)"); + assert_eq!(t.get("A4"), Value::Error(CellError::Value)); +} diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index 71a6ccb4bc..baa86693aa 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -5,8 +5,8 @@ use gridcraft_core::{CellError, Value, number_to_text}; use crate::criteria::wildcard_find; use crate::util::{ - A, MAX_CELLS, MAX_TEXT, R, S, arg, array_val, as_array, boolean, has, num, num_val, opt_bool, opt_int, opt_num, round_half_away, scalar, text, - text_val, + A, MAX_CELLS, MAX_TEXT, R, S, arg, array_val, as_array, boolean, has, int, num, num_val, opt_bool, opt_int, opt_num, round_half_away, scalar, + text, text_val, }; use crate::{Arg, Ctx, FnSpec, VAR}; @@ -550,7 +550,7 @@ fn find_matches(text: &[char], delims: &[Vec], insensitive: bool, from_end fn text_before_after(a: &[Arg], before: bool) -> R { let s = chars(&text(a, 0)?); let delims = delimiters(&arg(a, 1)?.value)?; - let inst = opt_int(a, 2, 1)?; + let inst = if has(a, 2) { int(a, 2)? } else { 1 }; let mode = opt_int(a, 3, 0)?; let match_end = opt_bool(a, 4, false)?; if !(0..=1).contains(&mode) || inst == 0 || inst.unsigned_abs() as usize > s.len().max(1) { From 7ed494691cbd272c5dac69f768317fc699fcaa81 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Thu, 22 Oct 2026 17:57:56 +0700 Subject: [PATCH 074/687] README: the app icon above the title --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 2c224559cb..d9dc5eb363 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@

+

+ GridCraft icon: a spreadsheet page on a green tile +

+

GridCraft

From 56207f2d3cc55f5f0574415998ebcbaf61c90216 Mon Sep 17 00:00:00 2001 From: Hamza Date: Thu, 22 Oct 2026 17:50:09 +0500 Subject: [PATCH 075/687] run --script: a line's unknown key stops the run (#2256) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A script line whose parameters sat under a key the runner does not read ran the command with no parameters at all, and the run still exited 0: {"command":"home.insertSheet","args":{"name":"Report"}} {"command":"home.insertSheet","name":"Report2"} made Sheet2 and Sheet3 instead of Report and Report2, and said nothing. A typo in a script produced a valid, wrong workbook. A line is read for `command` (or `id`) and `params`; any other key is a usage error now, naming the keys and the line, and the run stops before the workbook is written: gridcraft-cli: s.jsonl:2: unknown key `args`: a line is {"command": "id", "params": {…}} The usage text says what a line looks like, which is the other half of the report — the format was not written down anywhere, so the reporter found it by trying `args`, a flat object and `id` in turn. Tested at both levels: `parse_command` for the keys, their sorted list when there are several, and the forms that are read (`id`, `params`, `id={json}`, comments); and a run of a four-line script that stops at line 2 without writing the file, then the same script corrected, whose sheets come out named. Without the check the mistaken line parses again. An unknown key *inside* `params` is still ignored — the rest of #2256 — and needs the command's parameter list, which the engine does not expose yet. Noted on the issue. --- apps/gridcraft-cli/src/main.rs | 33 ++++++++++++++++++++++++++++ apps/gridcraft-cli/tests/cli.rs | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/apps/gridcraft-cli/src/main.rs b/apps/gridcraft-cli/src/main.rs index 0b763e3784..972c023fae 100644 --- a/apps/gridcraft-cli/src/main.rs +++ b/apps/gridcraft-cli/src/main.rs @@ -43,6 +43,8 @@ const USAGE: &str = "usage: cached-value calc oracle + XLSX round-trip fidelity (exit 1 on any difference) gridcraft-cli run [--in FILE | --sample NAME] [--cmd 'id={json}']... [--script FILE.jsonl] [--out FILE] [--print RANGE] [--quiet] + a script line is id={json} or {\"command\": \"id\", \"params\": {…}}; + blank lines and lines starting with # or // are skipped gridcraft-cli commands [--json] [--search X] gridcraft-cli functions [--json] [--search X] [--category C] gridcraft-cli mcp [--connect PORT] [--in FILE | --sample NAME] @@ -375,6 +377,17 @@ fn parse_command(text: &str) -> Result, String> { } if t.starts_with('{') { let v: Value = serde_json::from_str(t).map_err(|e| format!("bad JSON: {e}"))?; + // A key this does not read is a mistake, not a comment: `{"command": …, "args": {…}}` and + // `{"command": …, "name": "Report"}` ran the command with no parameters at all and the + // script still exited 0, so a typo made a valid, wrong workbook (#2256). + if let Some(o) = v.as_object() { + let mut unknown: Vec<&str> = o.keys().map(String::as_str).filter(|k| !matches!(*k, "command" | "id" | "params")).collect(); + if !unknown.is_empty() { + unknown.sort_unstable(); + let (plural, list) = (if unknown.len() == 1 { "key" } else { "keys" }, unknown.join("`, `")); + return Err(format!("unknown {plural} `{list}`: a line is {{\"command\": \"id\", \"params\": {{…}}}}")); + } + } let id = v.get("command").or_else(|| v.get("id")).and_then(Value::as_str).ok_or("JSON command needs a `command` field")?; return Ok(Some((id.to_string(), v.get("params").cloned().unwrap_or_else(|| json!({}))))); } @@ -567,6 +580,26 @@ mod tests { assert_eq!(parse_command("{\"command\":\"edit.redo\"}").unwrap(), Some(("edit.redo".into(), json!({})))); assert_eq!(parse_command(" # comment").unwrap(), None); assert!(parse_command("x={bad").is_err()); + // A key this does not read is a mistake: the command used to run with no parameters and + // the script still exited 0, so a typo made a valid, wrong workbook (#2256). + for (line, named) in [ + (r#"{"command":"home.insertSheet","args":{"name":"R"}}"#, "`args`"), + (r#"{"command":"home.insertSheet","name":"R"}"#, "`name`"), + (r#"{"command":"home.insertSheet","params":{},"note":"why"}"#, "`note`"), + ] { + let e = parse_command(line).expect_err("refused"); + assert!(e.contains("unknown key") && e.contains(named), "{line}: {e}"); + assert!(e.contains(r#"{"command": "id", "params": {…}}"#), "{line}: {e}"); + } + // Several at once are listed in one message, sorted. + let e = parse_command(r#"{"command":"x","name":"R","args":{}}"#).expect_err("refused"); + assert!(e.contains("unknown keys `args`, `name`"), "{e}"); + // `id` is still the accepted alternative to `command`, and `params` still carries them. + assert_eq!(parse_command(r#"{"id":"edit.undo"}"#).unwrap(), Some(("edit.undo".into(), json!({})))); + assert_eq!( + parse_command(r#"{"command":"home.insertSheet","params":{"name":"R"}}"#).unwrap(), + Some(("home.insertSheet".into(), json!({"name": "R"}))) + ); } #[test] diff --git a/apps/gridcraft-cli/tests/cli.rs b/apps/gridcraft-cli/tests/cli.rs index 3105819ec8..5295368e3d 100644 --- a/apps/gridcraft-cli/tests/cli.rs +++ b/apps/gridcraft-cli/tests/cli.rs @@ -206,3 +206,41 @@ fn check_reports_a_depth_limited_scan_while_checking_readable_files() { assert!(report["files"].as_array().unwrap().iter().any(|r| r["error"].as_str().is_some_and(|e| e.contains("depth limit")))); let _ = std::fs::remove_dir_all(dir); } + +/// A script line whose parameters are under a key `run` does not read used to run the command +/// with no parameters at all, and the run still exited 0: three mistaken lines made Sheet2, +/// Sheet3 and Sheet4 instead of the named sheets, and nothing said so (#2256). +#[test] +fn a_script_line_with_an_unknown_key_stops_the_run() { + let dir = tmpdir("script-keys"); + let script = dir.join("s.jsonl"); + let out = dir.join("t.xlsx"); + let write = |lines: &[&str]| std::fs::write(&script, lines.join("\n")).unwrap(); + + write(&[r#"{"command":"file.new"}"#, r#"{"command":"home.insertSheet","args":{"name":"Report"}}"#]); + let o = run(&["run", "--script", script.to_str().unwrap(), "--out", out.to_str().unwrap()]); + let err = String::from_utf8_lossy(&o.stderr); + assert!(!o.status.success(), "the run must stop: {err}"); + assert!(err.contains("s.jsonl:2"), "it names the line: {err}"); + assert!(err.contains("unknown key `args`"), "and the key: {err}"); + assert!(!out.exists(), "and writes no workbook: {err}"); + + // The forms that are read still work, in one script: `params`, `id=json`, and a comment. + write(&[ + r#"{"command":"file.new"}"#, + r#"{"command":"home.insertSheet","params":{"name":"Report"}}"#, + "# a comment", + r#"home.insertSheet={"name":"Report2"}"#, + ]); + let o = run(&["run", "--script", script.to_str().unwrap(), "--out", out.to_str().unwrap()]); + assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); + let info = ok(&["info", out.to_str().unwrap()]); + assert!(info.contains("Report:") && info.contains("Report2:"), "the sheets are named: {info}"); + assert!(!info.contains("Sheet2"), "and no sheet was added with a default name: {info}"); + + // The usage text says what a line looks like, which is how this was found. + let usage = String::from_utf8_lossy(&run(&[]).stderr).into_owned() + &String::from_utf8_lossy(&run(&["--help"]).stdout); + assert!(usage.contains(r#"{"command": "id", "params": {…}}"#), "{usage}"); + + let _ = std::fs::remove_dir_all(&dir); +} From 843c9b84049738eaab5cf2fc71d22a31ea86100b Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 22 Oct 2026 09:50:06 +0200 Subject: [PATCH 076/687] ui: a too-wide number in a repeat-fill format shows #### A number too wide for its column is drawn as ####. With a repeat fill (`*` in the format, as in the Accounting format) the fill step then rebuilt the text from the formatted value, so it overflowed and, being right-aligned, lost its leading digits: 2,509.00 CHF in a narrow column was drawn as "509.00 CHF". The fill now leaves #### alone. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/grid.rs | 7 ++- crates/ui-egui/tests/accounting_fill.rs | 77 +++++++++++++++++++++++++ docs/ui-parity.md | 3 +- 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/tests/accounting_fill.rs diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index a0efbfae7a..87aaa3c738 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -1268,13 +1268,16 @@ pub(crate) fn paint_quadrant( let mut galley = p.layout_no_wrap(first_line.clone(), font.clone(), color); let mut w = galley.size().x; // Numbers and dates never overflow: they show #### when too narrow. - if !shrunk && (numeric || matches!(v, Value::Number(_))) && w > avail_w - 2.0 * pad && st.align.h != HAlign::Fill { + let hashed = !shrunk && (numeric || matches!(v, Value::Number(_))) && w > avail_w - 2.0 * pad && st.align.h != HAlign::Fill; + if hashed { let n = ((avail_w - 2.0 * pad) / char_w).floor().max(1.0) as usize; galley = p.layout_no_wrap("#".repeat(n), font.clone(), color); w = galley.size().x; } - // Repeat fill (`*` in number formats) / Fill alignment. + // Repeat fill (`*` in number formats) / Fill alignment. A number already shown as #### keeps + // it: rebuilding it from the text would overflow and clip its leading digits. if let Some((ch, pos)) = fill_char + && !hashed && pos <= first_line.len() { let base_w = w; diff --git a/crates/ui-egui/tests/accounting_fill.rs b/crates/ui-egui/tests/accounting_fill.rs new file mode 100644 index 0000000000..492f581cd7 --- /dev/null +++ b/crates/ui-egui/tests/accounting_fill.rs @@ -0,0 +1,77 @@ +//! Repeat fill (`*` in a number format, as in the Accounting format): a number that fits is drawn +//! across its cell, and one too wide for its column shows #### instead of losing its leading digits. + +use egui::Shape; +use gridcraft_engine::Session; +use gridcraft_engine::core::CellRef; +use gridcraft_ui_egui::{SheetApp, grid::Geo}; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +const ACCOUNTING: &str = r#"_-* #,##0.00\ "CHF"_-;\-* #,##0.00\ "CHF"_-;_-* "-"??\ "CHF"_-;_-@_-"#; + +fn harness() -> Harness { + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "A1:B2", "values": [[344.75, 344.75], [2509, 2509]]})).unwrap(); + s.execute("home.formatCells", json!({"range": "A1:B2", "style": {"num_fmt": ACCOUNTING}})).unwrap(); + // Column A is too narrow for 2,509.00 CHF; column B fits both. + s.execute("home.columnWidth", json!({"cols": "A:A", "chars": 7})).unwrap(); + s.execute("home.columnWidth", json!({"cols": "B:B", "chars": 20})).unwrap(); + s.execute("selection.set", json!({"range": "H20"})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + SheetApp::new(s, Default::default()), + ); + h.run_steps(4); + h +} + +/// Painted text shapes whose text satisfies `f`: (text, left x, width). +fn texts(h: &Harness, f: impl Fn(&str) -> bool) -> Vec<(String, f32, f32)> { + h.output() + .shapes + .iter() + .filter_map(|s| match &s.shape { + Shape::Text(t) if f(&t.galley.job.text) => Some((t.galley.job.text.clone(), t.pos.x, t.galley.size().x)), + _ => None, + }) + .collect() +} + +#[test] +fn too_wide_fill_number_shows_hashes_not_a_clipped_value() { + let h = harness(); + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + let a2 = geo.cell_rect(sheet, CellRef::parse("A2").unwrap()); + let b2 = geo.cell_rect(sheet, CellRef::parse("B2").unwrap()); + // A2 is drawn as #### inside its cell. + let hashes = texts(&h, |t| !t.is_empty() && t.chars().all(|c| c == '#')); + assert!(hashes.iter().any(|(_, x, w)| *x >= a2.left() && x + w <= a2.right() + 0.5), "A2 shows ####: {hashes:?}"); + // The only 2,509.00 drawn is B2's, whole and inside column B. + let values = texts(&h, |t| t.contains("509.00")); + assert_eq!(values.len(), 1, "{values:?}"); + let (text, x, w) = &values[0]; + assert!(text.contains("2,509.00 CHF"), "{text:?}"); + assert!(*x >= b2.left() && x + w <= b2.right() + 0.5, "B2's value stays in its cell: {values:?}"); +} + +#[test] +fn fill_spreads_a_fitting_number_across_its_cell() { + let h = harness(); + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + let b1 = geo.cell_rect(sheet, CellRef::parse("B1").unwrap()); + let drawn = texts(&h, |t| t.contains("344.75 CHF")); + let (_, x, w) = drawn.iter().find(|(_, x, _)| *x >= b1.left()).expect("B1 is drawn"); + // The fill pushes the value to the right edge (less the trailing `_-` space). + assert!(b1.right() - (x + w) < 6.0, "B1 ends at its right edge: {drawn:?} in {b1:?}"); + assert!(*x - b1.left() < 6.0, "the fill starts at the left edge: {drawn:?} in {b1:?}"); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 07797d70d0..601e19d9d3 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (a too-wide number in a repeat-fill format such as Accounting shows #### instead of losing its leading digits; Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -176,6 +176,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | A number in a repeat-fill format (`*`, as in the Accounting format) that is too wide for its column shows #### like any other number, instead of the fill rebuilding the value so its leading digits were cut off (2,509.00 CHF drawn as "509.00 CHF") | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | | 2026-10-21 | minor | Form dialogs (conditional-format quick rules, New Name, Remove Duplicates, Text to Columns and every other `Dialog::form`) fit their content: OK and Cancel sit in a row under the last field instead of a right-to-left layout that took the screen's whole height and left the buttons floating halfway down | From 99f92f4a28e0c02d661050198499f5daeb6efece Mon Sep 17 00:00:00 2001 From: Hamza Date: Thu, 22 Oct 2026 18:05:53 +0500 Subject: [PATCH 077/687] range.setValues: `parse: false` stores a string as the text it is (#2255) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every string in `values` went through typed input, which reads it with en-US rules, and nothing said so: "05.04.2026" (dd.mm.yyyy) → the date 4 May 2026 "02/11/2026" (dd/mm/yyyy) → the date 11 February 2026 "2.500" (thousands) → the number 2.5 "4.25 %" (text) → 0.0425, formatted 0.00% A workbook built from Turkish-format exports therefore held different values than the script put in it, and the only way to notice was to read the saved file back cell by cell. `parse: false` stores every string as the text it is. A formula is still a formula — `=` means a formula in a cell, and the ticket asks for that — and numbers, booleans and null are unaffected either way. A leading apostrophe is part of the text rather than the marker that makes it text, since nothing is being marked. The default is unchanged: `parse` is absent in every existing call, and those calls behave exactly as before. A `parse` that is neither true nor false is a usage error rather than a quiet default. The test pins both sides — the swapped date, the decimal point and the percentage under today's default; each of the six strings stored verbatim with `parse: false`, with `=1+1` still calculating 2; numbers and booleans either way; and the refused `parse` values. Without the change the first string comes back as a date. --- crates/engine/src/cmd/edit.rs | 62 ++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index e537407127..67920264d0 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -16,7 +16,7 @@ pub fn specs() -> Vec { "Set Values", [], None, - "{range?: \"A1\", values: [[...]] (rows of numbers/strings/bools/null; strings starting with = are formulas)}", + "{range?: \"A1\", values: [[...]] (rows of numbers/strings/bools/null; strings starting with = are formulas), parse?: bool (default true: a string goes through typed input, so \"05.04.2026\" becomes a date and \"2.500\" a number, by en-US rules; false stores every other string as the text it is, so a dd.mm.yyyy date or a thousands separator survives)}", has_doc, range_set_values ), @@ -299,6 +299,15 @@ fn range_set_values(s: &mut Session, p: &Json) -> Result { let sheet = target_sheet(s, p)?; let start = target_range(s, p)?.start; let Some(rows) = p.get("values").and_then(Json::as_array) else { return Err(bad("range.setValues", "missing `values` (array of rows)")) }; + // A string is run through typed input by default, which reads it with en-US rules: a caller + // sending "05.04.2026" from a dd.mm.yyyy export got 4 May, and "2.500" became 2.5, in silence + // (#2255). `parse: false` stores a string as the text it is; numbers, booleans and formulas + // are unaffected either way. + let parse = match p.get("parse") { + None | Some(Json::Null) => true, + Some(Json::Bool(b)) => *b, + Some(other) => return Err(bad("range.setValues", format!("parse {other}: true or false"))), + }; if rows.len() > MAX_ROWS as usize { return Err(bad("range.setValues", "too many rows")); } @@ -321,6 +330,14 @@ fn range_set_values(s: &mut Session, p: &Json) -> Result { Json::Bool(b) => { Some(Cell { value: Value::Bool(*b), formula: None, style: cx.wb.sheet(sheet).map(|sh| sh.style_id(at)).unwrap_or_default() }) } + // Text as given: no date or number reading, and a leading `'` is part of the + // text rather than the marker that makes it text. A formula is still a + // formula, which is what the ticket asks for and what `=` means in a cell. + Json::String(text) if !parse && !text.starts_with('=') => Some(Cell { + value: Value::Text(text.as_str().into()), + formula: None, + style: cx.wb.sheet(sheet).map(|sh| sh.style_id(at)).unwrap_or_default(), + }), _ => input_to_cell(&input, sheet, at, &mut cx.wb)?, }; let sh = cx.sheet_mut(sheet)?; @@ -1769,6 +1786,49 @@ mod tests { assert!(s.execute("cell.set", json!({"cell": "A5"})).is_err()); } + /// Every string in `values` went through typed input, read with en-US rules, in silence: a + /// caller sending "05.04.2026" from a dd.mm.yyyy export stored 4 May, "2.500" became 2.5 and + /// "4.25 %" became a percentage (#2255). `parse: false` stores a string as the text it is. + #[test] + fn set_values_can_store_strings_as_the_text_they_are() { + let mut s = Session::new(); + s.new_workbook(); + let strings = ["05.04.2026", "02/11/2026", "2.500", "4.25 %", "=1+1", "'keep"]; + let row: Vec = strings.iter().map(|v| json!(v)).collect(); + let get = |s: &mut Session, a: &str| s.execute("cell.get", json!({"cell": a})).unwrap(); + + // The default is unchanged: the dates swap, the thousands separator is a decimal point. + s.execute("range.setValues", json!({"range": "A1", "values": [row.clone()]})).unwrap(); + assert_eq!(get(&mut s, "A1")["text"], "5/4/2026", "dd.mm.yyyy read as mm.dd.yyyy"); + assert_eq!(get(&mut s, "C1")["value"], json!(2.5), "\"2.500\" read as a decimal"); + assert_eq!(get(&mut s, "D1")["value"], json!(0.0425), "\"4.25 %\" read as a percentage"); + + // With `parse: false` each string is the text it was given, and a formula is still a + // formula. + s.execute("range.setValues", json!({"range": "A3", "parse": false, "values": [row.clone()]})).unwrap(); + for (i, want) in strings.iter().enumerate() { + let cell = format!("{}3", (b'A' + i as u8) as char); + let got = get(&mut s, &cell); + if *want == "=1+1" { + assert_eq!(got["value"], json!(2.0), "{cell} is still a formula: {got}"); + assert_eq!(got["formula"], "=1+1", "{got}"); + } else { + assert_eq!(got["value"], json!(*want), "{cell} is the text it was given: {got}"); + assert!(got["formula"].is_null(), "{cell} is not a formula: {got}"); + } + } + // Numbers and booleans are untouched either way. + s.execute("range.setValues", json!({"range": "A5", "parse": false, "values": [[12.5, true, null]]})).unwrap(); + assert_eq!(get(&mut s, "A5")["value"], json!(12.5)); + assert_eq!(get(&mut s, "B5")["value"], json!(true)); + assert_eq!(get(&mut s, "C5")["text"], ""); + // `parse` is a flag, and anything else is a usage error rather than a quiet default. + s.execute("range.setValues", json!({"range": "A7", "parse": true, "values": [["2.500"]]})).unwrap(); + assert_eq!(get(&mut s, "A7")["value"], json!(2.5)); + assert!(s.execute("range.setValues", json!({"range": "A8", "parse": "no", "values": [["x"]]})).is_err()); + assert!(s.execute("range.setValues", json!({"range": "A8", "parse": 0, "values": [["x"]]})).is_err()); + } + #[test] fn legacy_array_entry() { let mut s = Session::new(); From 840ba5c3a5107b642ad243800d53ef095702434b Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 22 Oct 2026 15:54:06 +0200 Subject: [PATCH 078/687] view: Show Outline Symbols (Ctrl+8), read and written A sheet can hide its outline's level buttons and brackets while keeping the outline (Excel's Ctrl+8, saved as showOutlineSymbols="0"). It wasn't modelled, so such sheets showed a wide outline band Excel doesn't. - model: Sheet::show_outline_symbols (default on, also for older JSON). - xlsx: read and written on the sheet view. - engine: view.showOutlineSymbols {on?} (Ctrl+8), undoable. - ui: no outline bands while it's off. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/view.rs | 2 ++ crates/engine/src/tests.rs | 15 +++++++++++++++ crates/model/src/sheet.rs | 9 +++++++++ crates/ui-egui/src/grid.rs | 28 ++++++++++++++++++++++++---- crates/xlsx/src/sheet_read.rs | 1 + crates/xlsx/src/sheet_write.rs | 3 +++ crates/xlsx/src/tests/malformed.rs | 13 +++++++++++++ docs/ui-parity.md | 3 ++- 8 files changed, 69 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/view.rs b/crates/engine/src/cmd/view.rs index f0b2f77d39..b42aec9a61 100644 --- a/crates/engine/src/cmd/view.rs +++ b/crates/engine/src/cmd/view.rs @@ -35,6 +35,7 @@ pub fn specs() -> Vec { cmd!("view.gridlines", "Gridlines", ["View", "Show"], None, "{on?}", has_doc, |s, p| sheet_flag(s, p, "gridlines")), cmd!("view.headings", "Headings", ["View", "Show"], None, "{on?}", has_doc, |s, p| sheet_flag(s, p, "headings")), cmd!("view.showZeros", "Show Zeros", [], None, "{on?}", has_doc, |s, p| sheet_flag(s, p, "zeros")), + cmd!("view.showOutlineSymbols", "Show Outline Symbols", [], Some("Ctrl+8"), "{on?}", has_doc, |s, p| sheet_flag(s, p, "outlineSymbols")), cmd!("view.showObjects", "Show Objects", [], Some("Ctrl+6"), "{on?}", has_doc, show_objects), cmd!("view.rightToLeft", "Sheet Right-to-Left", ["Page Layout", "Sheet Options"], None, "{on?}", has_doc, |s, p| sheet_flag(s, p, "rtl")), cmd!("view.zoom", "Zoom", ["View", "Zoom"], None, "{percent: 10..400}", has_doc, zoom), @@ -166,6 +167,7 @@ fn sheet_flag(s: &mut Session, p: &Json, which: &str) -> Result { "gridlines" => &mut sh.show_gridlines, "headings" => &mut sh.show_headings, "zeros" => &mut sh.show_zeros, + "outlineSymbols" => &mut sh.show_outline_symbols, "rtl" => &mut sh.right_to_left, "printGridlines" => &mut sh.print.gridlines, _ => &mut sh.print.headings, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index d6a99be91f..81441c8273 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2595,6 +2595,21 @@ fn file_options_reports_settings_and_asks_for_the_options_page() { assert_eq!(s.active().unwrap().wb, before, "Options never changes the workbook"); } +/// Ctrl+8 hides and shows the outline symbols; it's undoable and a sheet setting. +#[test] +fn show_outline_symbols_toggles() { + let mut s = s(); + let on = |s: &Session| s.active().unwrap().wb.active().unwrap().show_outline_symbols; + assert!(on(&s)); + assert_eq!(s.execute("view.showOutlineSymbols", json!({})).unwrap(), json!({"on": false})); + assert!(!on(&s)); + s.execute("edit.undo", json!({})).unwrap(); + assert!(on(&s)); + s.execute("view.showOutlineSymbols", json!({"on": false})).unwrap(); + reopen(&mut s); + assert!(!on(&s)); +} + fn reopen(s: &mut Session) { let r = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap(); let b64 = r["base64"].as_str().unwrap().to_string(); diff --git a/crates/model/src/sheet.rs b/crates/model/src/sheet.rs index 1901d8048e..f7ef8f3be4 100644 --- a/crates/model/src/sheet.rs +++ b/crates/model/src/sheet.rs @@ -76,6 +76,10 @@ pub struct Sheet { pub show_headings: bool, pub show_formulas: bool, pub show_zeros: bool, + /// The outline's level buttons and brackets (Excel's Show Outline Symbols, Ctrl+8); the + /// outline itself stays. + #[serde(default = "yes")] + pub show_outline_symbols: bool, pub right_to_left: bool, pub zoom: u16, pub cond_formats: Vec, @@ -113,6 +117,10 @@ pub struct Sheet { pub preserved: Option>, } +fn yes() -> bool { + true +} + /// Default width of a column and height of a row (pixels at 96 dpi) for the default font. pub const DEFAULT_COL_WIDTH: f32 = 64.0; pub const DEFAULT_ROW_HEIGHT: f32 = 20.0; @@ -140,6 +148,7 @@ impl Sheet { tab_color: None, visibility: Visibility::Visible, show_gridlines: true, + show_outline_symbols: true, show_headings: true, show_formulas: false, show_zeros: true, diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index a0efbfae7a..78e558cce1 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -235,13 +235,15 @@ impl Geo { let frozen_h = (ri.start(fr) as f32) * z; let row_outline = Arc::new(crate::outline::Axis::new(&sh.rows)); let col_outline = Arc::new(crate::outline::Axis::new(&sh.cols)); - let mut band_w = crate::outline::band(row_outline.levels, zoom); - let mut band_h = crate::outline::band(col_outline.levels, zoom); + // Show Outline Symbols off: no bands (the outline's hidden lines stay hidden). + let symbols = sh.show_outline_symbols; + let mut band_w = if symbols { crate::outline::band(row_outline.levels, zoom) } else { 0.0 }; + let mut band_h = if symbols { crate::outline::band(col_outline.levels, zoom) } else { 0.0 }; // Without headings the level buttons still need a corner to sit in. - if row_outline.levels > 0 && header_h == 0.0 { + if symbols && row_outline.levels > 0 && header_h == 0.0 { band_h = band_h.max(crate::outline::lane(zoom) + 3.0); } - if col_outline.levels > 0 && header_w == 0.0 { + if symbols && col_outline.levels > 0 && header_w == 0.0 { band_w = band_w.max(crate::outline::lane(zoom) + 3.0); } let cells = Rect::from_min_max(pos2(rect.left() + band_w + header_w, rect.top() + band_h + header_h), rect.max); @@ -4216,6 +4218,24 @@ mod pane_tests { assert!(range_in_pane(sel, (2, 20), (2, 10))); } + #[test] + fn hidden_outline_symbols_take_no_room() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let mut sh = Sheet::new("S"); + sh.rows.entry(3).or_default().outline = 1; + sh.cols.entry(2).or_default().outline = 2; + let shown = Geo::new(&sh, rect, vec2(0.0, 0.0)); + assert!(shown.band_w > 0.0 && shown.band_h > 0.0); + sh.show_outline_symbols = false; + let hidden = Geo::new(&sh, rect, vec2(0.0, 0.0)); + assert_eq!((hidden.band_w, hidden.band_h), (0.0, 0.0)); + assert!(hidden.cells.left() < shown.cells.left() && hidden.cells.top() < shown.cells.top()); + // Without headings too. + sh.show_headings = false; + let bare = Geo::new(&sh, rect, vec2(0.0, 0.0)); + assert_eq!((bare.band_w, bare.band_h), (0.0, 0.0)); + } + /// Two frozen rows and columns, scrolled right so that columns C:E lie under the frozen columns. fn frozen_geo(sh: &mut Sheet) -> Geo { sh.freeze = Some((2, 2)); diff --git a/crates/xlsx/src/sheet_read.rs b/crates/xlsx/src/sheet_read.rs index 83955ee1fe..5af3aeab37 100644 --- a/crates/xlsx/src/sheet_read.rs +++ b/crates/xlsx/src/sheet_read.rs @@ -274,6 +274,7 @@ fn read_views(e: &El, sheet: &mut Sheet) { sheet.show_headings = v.flag("showRowColHeaders", true); sheet.show_formulas = v.flag("showFormulas", false); sheet.show_zeros = v.flag("showZeros", true); + sheet.show_outline_symbols = v.flag("showOutlineSymbols", true); sheet.right_to_left = v.flag("rightToLeft", false); sheet.view = v.attr("view").and_then(gridcraft_model::SheetViewMode::parse).unwrap_or_default(); if let Some(z) = v.attr_u32("zoomScale") { diff --git a/crates/xlsx/src/sheet_write.rs b/crates/xlsx/src/sheet_write.rs index daac03daaa..e0e863ec34 100644 --- a/crates/xlsx/src/sheet_write.rs +++ b/crates/xlsx/src/sheet_write.rs @@ -172,6 +172,9 @@ pub fn write_sheet(wb: &Workbook, si: usize, selected: bool, out: &mut Out) -> ( if !sheet.show_zeros { s.push_str(" showZeros=\"0\""); } + if !sheet.show_outline_symbols { + s.push_str(" showOutlineSymbols=\"0\""); + } if sheet.right_to_left { s.push_str(" rightToLeft=\"1\""); } diff --git a/crates/xlsx/src/tests/malformed.rs b/crates/xlsx/src/tests/malformed.rs index e746b01dec..91b5d4b9ca 100644 --- a/crates/xlsx/src/tests/malformed.rs +++ b/crates/xlsx/src/tests/malformed.rs @@ -330,3 +330,16 @@ fn ordinary_overlapping_column_ranges_keep_last_declaration() { assert!(last.hidden); assert_eq!(wb.styles.get(last.style.unwrap()).num_fmt.as_str(), "0.00"); } + +/// Show Outline Symbols off (`showOutlineSymbols="0"`) is read and written back. +#[test] +fn outline_symbols_flag_round_trips() { + let body = r#""#; + let (wb, _) = read_xlsx(&minimal(body, &[], "", "")).unwrap(); + assert!(!wb.sheet(0).unwrap().show_outline_symbols); + let (back, _) = read_xlsx(&crate::write_xlsx(&wb).unwrap()).unwrap(); + assert!(!back.sheet(0).unwrap().show_outline_symbols); + // Absent means shown. + let (wb, _) = read_xlsx(&minimal("", &[], "", "")).unwrap(); + assert!(wb.sheet(0).unwrap().show_outline_symbols); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 07797d70d0..404875b5d4 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Show Outline Symbols, Ctrl+8; Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -176,6 +176,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Show Outline Symbols (`view.showOutlineSymbols`, Ctrl+8): a sheet can hide the outline's level buttons and brackets while keeping its outline, as Excel saves with `showOutlineSymbols="0"` (read and written); the grid then gives the bands no room | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | | 2026-10-21 | minor | Form dialogs (conditional-format quick rules, New Name, Remove Duplicates, Text to Columns and every other `Dialog::form`) fit their content: OK and Cancel sit in a row under the last field instead of a right-to-left layout that took the screen's whole height and left the buttons floating halfway down | From 5bcbaacce7a9de54c4bd2960f25998a2ecc6a8bb Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 22 Oct 2026 16:02:58 +0200 Subject: [PATCH 079/687] Snapshot: catalog installed fonts before rendering The offscreen snapshot never started the background font scan the app runs from its first frame, so a workbook's installed fonts (Helvetica Neue, say) were drawn in the Calibri substitute. Catalog them up front and run a few more frames so they bind, as in the app. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/examples/snapshot.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/examples/snapshot.rs b/crates/ui-egui/examples/snapshot.rs index dcee71173b..9eb3cc69c0 100644 --- a/crates/ui-egui/examples/snapshot.rs +++ b/crates/ui-egui/examples/snapshot.rs @@ -79,6 +79,9 @@ fn main() { } i += 1; } + // Catalog the installed fonts first (the app scans in the background from its first frame), so + // the workbook's fonts are drawn in their own faces, not a substitute. + let _ = gridcraft_ui_egui::fonts::catalog(); let mut session = Session::new(); if let Some(p) = &input { session.execute("file.open", json!({"path": p})).expect("open"); @@ -127,7 +130,9 @@ fn main() { app }, ); - harness.run_steps(8); + // The workbook's installed fonts bind once the font scan is done; frames past the first few + // apply them, so the snapshot shows them as the app does. + harness.run_steps(14); let img = harness.render().expect("render"); img.save(&out).expect("save png"); eprintln!("wrote {out} ({}x{})", img.width(), img.height()); From e6ffca60bb2356e87d64f94e6c59b2af626e64c5 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 22 Oct 2026 16:09:29 +0200 Subject: [PATCH 080/687] view: hidden outline symbols answer no clicks; parity rows updated With headings hidden too, a level button sat across the empty column band at the grid's top edge, so a click there still ran it. Outline hit testing now stops when both bands are empty. The parity doc no longer lists Ctrl+8 as missing. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/grid.rs | 10 ++++++++++ crates/ui-egui/src/outline.rs | 4 ++++ docs/ui-parity.md | 6 +++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 78e558cce1..78116b4ecb 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -4234,6 +4234,16 @@ mod pane_tests { sh.show_headings = false; let bare = Geo::new(&sh, rect, vec2(0.0, 0.0)); assert_eq!((bare.band_w, bare.band_h), (0.0, 0.0)); + // No button answers a click on the empty bands' edges (the grid's left and top), where the + // level buttons would sit with no headings to hold them. + for geo in [&hidden, &bare] { + for i in 0..400 { + let t = i as f32 * 0.5; + for p in [pos2(geo.rect.left(), geo.rect.top() + t), pos2(geo.rect.left() + t, geo.rect.top())] { + assert_eq!(crate::outline::hit(geo, &sh, p), None, "{p:?}"); + } + } + } } /// Two frozen rows and columns, scrolled right so that columns C:E lie under the frozen columns. diff --git a/crates/ui-egui/src/outline.rs b/crates/ui-egui/src/outline.rs index 61c40811c6..19fd884970 100644 --- a/crates/ui-egui/src/outline.rs +++ b/crates/ui-egui/src/outline.rs @@ -224,6 +224,10 @@ pub fn layout(geo: &Geo, sh: &Sheet) -> Layout { /// The button under `p` (logical coordinates), if any. pub fn hit(geo: &Geo, sh: &Sheet, p: Pos2) -> Option { + // Hidden symbols (or no outline) leave empty bands: a click on their edge is a cell click. + if geo.band_w <= 0.0 && geo.band_h <= 0.0 { + return None; + } let (rows, cols) = bands(geo); if !rows.contains(p) && !cols.contains(p) { return None; diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 404875b5d4..d35ed50b77 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -60,7 +60,7 @@ this logic, with one undo step. | AutoComplete in columns, function autocomplete and argument tips | done; cell helpers clip to the worksheet and hide when the editor scrolls out (#200) | | Header resize and double-click AutoFit | done (#42, #88 for header dividers) | | Data › Advanced Filter | dialog offers in-place or copy-to filtering, list and optional criteria ranges, and unique records; Alt+A,Q opens it (#1622). The engine's copy-to path clears only the result footprint and rejects worksheet overflow (#1031). | -| Outline symbols for grouped rows / columns (band beside the headers, brackets, − / +, level buttons 1…n+1) | done (#373): clicks run `data.hideDetail` / `data.showDetail` / `data.showLevels`, frozen panes and right-to-left sheets included; summary rows/columns are always below/right (the "Summary rows below detail" setting isn't modelled), no "Show outline symbols" option yet, and a block of cells still groups rows without the Rows / Columns dialog | +| Outline symbols for grouped rows / columns (band beside the headers, brackets, − / +, level buttons 1…n+1) | done (#373): clicks run `data.hideDetail` / `data.showDetail` / `data.showLevels`, frozen panes and right-to-left sheets included; summary rows/columns are always below/right (the "Summary rows below detail" setting isn't modelled), View › Show Outline Symbols (Ctrl+8) hides the bands and is saved with the sheet, and a block of cells still groups rows without the Rows / Columns dialog | | Reorder selected whole columns by dragging their headers | done (10-11) | | Freeze panes, split panes | freeze done; split to verify | | A Table's header names and filter buttons in the column headings once its header row scrolls away | done (#1423): with the active cell in the Table, as in Excel; the button opens the column's filter menu, the rest of the heading still selects the column. Plain AutoFilter ranges keep their letters (Excel does the same) | @@ -135,9 +135,9 @@ otherwise. Number-format and border keys match the physical key (Ctrl+Shift+4, w | F5, Shift+F5 (Mac), F7, Shift+F9, Ctrl+Alt+F9, F12, Ctrl+F4 | same | Go To, Find, Spelling, calculate sheet / all, Save As, close | **added** | | Shift+F10, Alt+Shift+F10, Ctrl+F1 | Shift+F10, Cmd+Option+R | context menu, Error Checking, ribbon | **added** | | F1, F3, F4 (repeat), F6 / Shift+F6, F8 / Shift+F8, F11, End mode | same | help, Paste Name, Repeat, pane cycling, extend/add mode, chart sheet | missing (no such feature yet) | -| Ctrl+8, Ctrl+Q, Ctrl+Shift+A, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | outline symbols, Quick Analysis, argument names, Function Arguments, formula bar height, tooltips | missing | +| Ctrl+Q, Ctrl+Shift+A, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | Quick Analysis, argument names, Function Arguments, formula bar height, tooltips | missing | | Ctrl+Shift+A after a function name | Control+Shift+A | argument names: `=SUMIF(` becomes `=SUMIF(range,criteria,sum_range)`, the `(` added when missing; one Undo takes them back | **added** | -| Ctrl+6, Ctrl+8, Ctrl+Q, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | objects, outline symbols, Quick Analysis, Function Arguments, formula bar height, tooltips | missing | +| Ctrl+6, Ctrl+Q, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | objects, Quick Analysis, Function Arguments, formula bar height, tooltips | missing | | Ctrl+Shift+Page Up / Down, Ctrl+Alt+5, Ctrl+Shift+F1, Ctrl+F5–F10, Alt+F8 / F11 / F12 | same | group sheets, shape cycling, full screen, window keys, macros, VBA, Power Query | missing | | — | Cmd+= (calculate), Cmd+T (references while editing; F4 works), Shift+F7, Cmd+Shift+L | Mac-only extras | missing (Cmd+= is egui's zoom key; no thesaurus or Modify Cell Style) | From 74f29a743f51292c9c418af12cc27bd4caaccbaa Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Thu, 22 Oct 2026 16:18:45 +0200 Subject: [PATCH 081/687] docs: Ctrl+Shift+A (argument names) is no longer listed as missing Co-Authored-By: Claude Opus 5.5 --- docs/ui-parity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ui-parity.md b/docs/ui-parity.md index d35ed50b77..0fd350aeed 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -135,7 +135,7 @@ otherwise. Number-format and border keys match the physical key (Ctrl+Shift+4, w | F5, Shift+F5 (Mac), F7, Shift+F9, Ctrl+Alt+F9, F12, Ctrl+F4 | same | Go To, Find, Spelling, calculate sheet / all, Save As, close | **added** | | Shift+F10, Alt+Shift+F10, Ctrl+F1 | Shift+F10, Cmd+Option+R | context menu, Error Checking, ribbon | **added** | | F1, F3, F4 (repeat), F6 / Shift+F6, F8 / Shift+F8, F11, End mode | same | help, Paste Name, Repeat, pane cycling, extend/add mode, chart sheet | missing (no such feature yet) | -| Ctrl+Q, Ctrl+Shift+A, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | Quick Analysis, argument names, Function Arguments, formula bar height, tooltips | missing | +| Ctrl+Q, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | Quick Analysis, Function Arguments, formula bar height, tooltips | missing | | Ctrl+Shift+A after a function name | Control+Shift+A | argument names: `=SUMIF(` becomes `=SUMIF(range,criteria,sum_range)`, the `(` added when missing; one Undo takes them back | **added** | | Ctrl+6, Ctrl+Q, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | objects, Quick Analysis, Function Arguments, formula bar height, tooltips | missing | | Ctrl+Shift+Page Up / Down, Ctrl+Alt+5, Ctrl+Shift+F1, Ctrl+F5–F10, Alt+F8 / F11 / F12 | same | group sheets, shape cycling, full screen, window keys, macros, VBA, Power Query | missing | From 45d1d1acc0dc87d0104d0634c42deae009721775 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 14:50:53 +0000 Subject: [PATCH 082/687] G2: windows on workbooks in the engine; New Window shares the workbook Session keeps a window list (stable ids, per-window selection, active sheet and per-sheet selections swapped into the workbook without dirtying it) in place of the index-based hidden list and the copying New Window. Titles read Name:1 / Name:2 for a workbook with several windows. File > Close closes one window of several; window.close, window.list and window ids for window.activate, view.switchWindows, view.hideWindow and view.unhideWindow. Control channel and MCP tools take window/doc ids; scroll positions and cell drafts belong to their window. --- ROADMAP.md | 4 +- apps/gridcraft/src/native_menu.rs | 16 +- crates/engine/src/cmd/extra.rs | 72 ++-- crates/engine/src/cmd/file.rs | 50 ++- crates/engine/src/lib.rs | 39 ++- crates/engine/src/tests.rs | 114 ++++++- crates/engine/src/windows.rs | 342 +++++++++++++++++++ crates/engine/tests/command_sequence_fuzz.rs | 3 +- crates/mcp/src/headless.rs | 24 +- crates/mcp/src/tests.rs | 29 ++ crates/mcp/src/tools.rs | 64 +++- crates/ui-egui/src/control.rs | 44 ++- crates/ui-egui/src/dialogs.rs | 4 +- crates/ui-egui/src/editor.rs | 4 + crates/ui-egui/src/lib.rs | 84 +++-- crates/ui-egui/src/ribbon.rs | 2 +- crates/ui-egui/tests/file_menu.rs | 62 ++++ docs/control-protocol.md | 20 +- docs/mcp.md | 5 + docs/ui-parity.md | 5 +- 20 files changed, 872 insertions(+), 115 deletions(-) create mode 100644 crates/engine/src/windows.rs diff --git a/ROADMAP.md b/ROADMAP.md index 9617ef1e68..bf25cc3a99 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save; New Window shares the workbook) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -135,6 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-22 | View › New Window opens a second window on the same workbook (it used to open a copy whose Save overwrote the original): each window has its own selection, active sheet and scroll, titles read "Book1:1" / "Book1:2", and Switch Windows, Hide, Unhide and Close act per window. The control channel, CLI `send` and MCP tools take an optional `window` or `doc` id and default to the active window; `window.list` (MCP `list_windows`) lists the windows | | 2026-10-22 | Typing in a cell and then choosing File › New no longer strands the first workbook: the entry is committed into its own workbook before any File or window command (a refused entry stays open and stops the command), View › Switch Windows and the macOS Window menu list the open workbooks with unsaved markers, `view.switchWindows {doc}` switches by a stable workbook id, and the macOS Quit item asks about unsaved workbooks | | 2026-10-21 | Page Setup paper sizes match Japanese Excel: `B4`/`B5`/`B6` are the JIS sizes (ISO B4/B5/B6 stay reachable by name), so a Japanese B5 worksheet prints at 182×257 mm; Japanese Postcard (100×148 mm), Double Postcard (148×200 mm), Envelope #3/#4 (120×235/90×205 mm), Kaku #2/#3 (240×332/216×277 mm) and A6 are added, both sizes reach the PDF page (`file.exportPdf`/`file.print` MediaBox) and the size survives an XLSX round-trip; the Page Layout Size menu lists them. `B4`/`B5` used to be the ISO sizes, which is changed here | | 2026-10-20 | Japanese interface catalog covers every key the Brazilian Portuguese catalog translates: the 398-label shared core plus 545 dialog, message, Backstage and menu strings (943 total, ~43% of ~2,200). A test parses `pt.rs`'s source, so the two catalogs stay in step | @@ -221,6 +222,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Progress log: New Window shares the workbook; window ids for scripts and MCP | | 2026-10-22 | minor | Progress log: drafts stay in their workbook, Switch Windows, macOS Quit asks to save | | 2026-10-21 | minor | Page Setup paper sizes: JIS B4/B5/B6 (ISO by name), Japanese postcards and envelopes, A6; PDF page size follows, XLSX round-trips | | 2026-10-20 | minor | Dutch (nl) listed under other shipped languages; progress log row | diff --git a/apps/gridcraft/src/native_menu.rs b/apps/gridcraft/src/native_menu.rs index 877c82fd8a..826a12b311 100644 --- a/apps/gridcraft/src/native_menu.rs +++ b/apps/gridcraft/src/native_menu.rs @@ -195,13 +195,13 @@ pub struct NativeMenu { events: Receiver, theme_items: Vec<(&'static str, CheckMenuItem)>, window_menu: Option, - /// The Window menu's workbook items and what they last showed. + /// The Window menu's window items and what they last showed. window_items: Vec, windows: Vec, } /// Menu id of the Window menu item that brings workbook `doc` forward. -const WINDOW_ITEM: &str = "window:doc:"; +const WINDOW_ITEM: &str = "window:id:"; /// The Quit item: closing the window asks about unsaved workbooks first, which AppKit's own /// Quit (terminate:) would skip. @@ -279,15 +279,15 @@ impl NativeMenu { NativeMenu { _menu: menu, events: rx, theme_items, window_menu, window_items: Vec::new(), windows: Vec::new() } } - /// Keeps the Window menu's list of open workbooks (title, unsaved bullet, active check) in + /// Keeps the Window menu's list of open windows (title, unsaved bullet, active check) in /// step with the session. fn sync_windows(&mut self, app: &SheetApp) { let windows = app.open_windows(); if windows != self.windows && let Some(menu) = &self.window_menu { - let same_docs = windows.len() == self.windows.len() && windows.iter().zip(&self.windows).all(|(a, b)| a.doc == b.doc); - if same_docs { + let same_windows = windows.len() == self.windows.len() && windows.iter().zip(&self.windows).all(|(a, b)| a.window == b.window); + if same_windows { for (item, w) in self.window_items.iter().zip(&windows) { item.set_text(w.label()); } @@ -296,7 +296,7 @@ impl NativeMenu { let _ = menu.remove(&item); } for w in &windows { - let item = CheckMenuItem::with_id(format!("{WINDOW_ITEM}{}", w.doc), w.label(), true, w.active, None); + let item = CheckMenuItem::with_id(format!("{WINDOW_ITEM}{}", w.window), w.label(), true, w.active, None); let _ = menu.append(&item); self.window_items.push(item); } @@ -318,8 +318,8 @@ impl NativeMenu { ctx.send_viewport_cmd(egui::ViewportCommand::Close); continue; } - if let Some(doc) = id.strip_prefix(WINDOW_ITEM).and_then(|d| d.parse::().ok()) { - app.run_or_alert("view.switchWindows", json!({"doc": doc})); + if let Some(window) = id.strip_prefix(WINDOW_ITEM).and_then(|w| w.parse::().ok()) { + app.run_or_alert("view.switchWindows", json!({"window": window})); continue; } // Text selection belongs to the inline editor, not the worksheet clipboard/undo. diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index debb62eeb9..3953455432 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -141,16 +141,16 @@ pub fn specs() -> Vec { ), cmd!(noundo "view.customViews", "Custom Views…", ["View", "Workbook Views"], None, "{save?: name, show?: name, delete?: name} → list", has_doc, custom_views), cmd!(noundo "view.split", "Split", ["View", "Window"], None, "{} (toggles a split at the active cell; shown as frozen panes in this version)", has_doc, split), - cmd!(noundo "view.newWindow", "New Window", ["View", "Window"], None, "{} opens a second window on the same workbook (a linked copy)", has_doc, new_window), + cmd!(noundo "view.newWindow", "New Window", ["View", "Window"], None, "{} → {window, doc, title}: opens another window on the active workbook (the same workbook: saving from either window saves it)", has_doc, new_window), cmd!(noundo "view.arrangeAll", "Arrange All", ["View", "Window"], None, "{}", has_doc, |_, _| ok()), - cmd!(noundo "view.hideWindow", "Hide", ["View", "Window"], None, "{}", has_doc, hide_window), - cmd!(noundo "view.unhideWindow", "Unhide…", ["View", "Window"], None, "{index?}", has_doc, unhide_window), + cmd!(noundo "view.hideWindow", "Hide", ["View", "Window"], None, "{window?, doc?} (the active window by default)", has_doc, hide_window), + cmd!(noundo "view.unhideWindow", "Unhide…", ["View", "Window"], None, "{window?, doc?, index?} (the most recently hidden window by default)", has_doc, unhide_window), cmd!( noundo "view.switchWindows", "Switch Windows", ["View", "Window"], None, - "{doc?} → open workbooks [{index, doc, title, dirty, active, hidden}]; with `doc` (a workbook's `doc` id from the list) makes that workbook active first", + "{window?, doc?} → open windows [{window, doc, index, title, dirty, active, hidden}]; with `window` (an id from the list), or `doc` (a workbook id: its most recent window), shows and activates that window first", always, switch_windows ), @@ -790,54 +790,46 @@ fn split(s: &mut Session, _: &Json) -> Result { } fn new_window(s: &mut Session, _: &Json) -> Result { - let d = s.doc()?.clone(); - let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title())); - copy.selection = d.selection.clone(); - let i = s.add_document(copy); - Ok(json!({"index": i})) + let id = s.new_window()?; + let doc = s.window(id).map(|w| w.doc_uid); + Ok(json!({"window": id, "doc": doc, "title": s.window_title(id)})) } -fn hide_window(s: &mut Session, _: &Json) -> Result { - s.hidden_windows.push(s.active_index()); - let n = s.documents().len(); - if n > 1 { - let next = (0..n).find(|i| !s.hidden_windows.contains(i)).unwrap_or(0); - s.set_active(next); - } +/// The window a window command names: `window` (an id), else `doc` (a workbook id: its most +/// recent window), else the active window. +pub(crate) fn window_param(s: &Session, cmd: &str, p: &Json) -> Result { + let t = crate::Target::from_json(p).map_err(|e| bad(cmd, e.to_string()))?; + s.target_window(t).map_err(|e| bad(cmd, e.to_string())) +} + +fn hide_window(s: &mut Session, p: &Json) -> Result { + let id = window_param(s, "view.hideWindow", p)?; + s.hide_window(id); ok() } fn unhide_window(s: &mut Session, p: &Json) -> Result { - let i = u32_param(p, "index").map(|v| v as usize).or_else(|| s.hidden_windows.last().copied()); - if let Some(i) = i { - s.hidden_windows.retain(|x| *x != i); - s.set_active(i); + let id = if p.get("window").is_some_and(|v| !v.is_null()) || p.get("doc").is_some_and(|v| !v.is_null()) { + Some(window_param(s, "view.unhideWindow", p)?) + } else if let Some(i) = u32_param(p, "index") { + // A workbook's position in the document list. + let uid = s.documents().get(i as usize).map(|d| d.uid).ok_or_else(|| bad("view.unhideWindow", "no such window"))?; + s.windows().iter().filter(|w| w.doc_uid == uid && w.hidden).map(|w| w.id).next_back().or_else(|| s.doc_window(uid)) + } else { + s.last_hidden_window() + }; + if let Some(id) = id { + s.unhide_window(id); } ok() } fn switch_windows(s: &mut Session, p: &Json) -> Result { - if let Some(doc) = p.get("doc").filter(|v| !v.is_null()) { - let i = doc.as_u64().and_then(|uid| s.document_index(uid)).ok_or_else(|| bad("view.switchWindows", "no open workbook has that `doc` id"))?; - s.hidden_windows.retain(|x| *x != i); - s.set_active(i); + if p.get("window").is_some_and(|v| !v.is_null()) || p.get("doc").is_some_and(|v| !v.is_null()) { + let id = window_param(s, "view.switchWindows", p)?; + s.unhide_window(id); } - Ok(Json::Array( - s.documents() - .iter() - .enumerate() - .map(|(i, d)| { - json!({ - "index": i, - "doc": d.uid, - "title": d.display_title(), - "dirty": d.is_dirty(), - "active": i == s.active_index(), - "hidden": s.hidden_windows.contains(&i), - }) - }) - .collect(), - )) + Ok(s.window_list()) } fn table_resize(s: &mut Session, p: &Json) -> Result { diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index a61c6298f8..1f50b908bc 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -12,7 +12,7 @@ pub fn specs() -> Vec { cmd!(noundo "file.save", "Save", ["File"], Some("Cmd+S"), "{path?} (xlsx by default; .csv/.tsv/.json/.html/.pdf by extension)", has_doc, save), cmd!(noundo "file.saveAs", "Save As…", ["File"], Some("Cmd+Shift+S"), "{path} (.xlsx, .csv, .tsv, .json, .html, or .pdf: a PDF of the active sheet)", has_doc, save_as), cmd!(query "file.saveBytes", "Encode Workbook", [], None, "{format?: xlsx|csv|tsv|json|html} → {base64}", has_doc, save_bytes), - cmd!(noundo "file.close", "Close", ["File"], Some("Cmd+W"), "{force?: bool}", has_doc, close), + cmd!(noundo "file.close", "Close", ["File"], Some("Cmd+W"), "{force?: bool, document?: bool} (closes the active window, or with `document` the workbook and all its windows; the workbook closes with its last window)", has_doc, close), cmd!(noundo "file.exportCsv", "Export as CSV", ["File", "Export"], None, "{path, sheet?}", has_doc, |s, p| export(s, p, "csv")), cmd!(noundo "file.exportHtml", "Save as Web Page", ["File", "Export"], None, "{path}", has_doc, |s, p| export(s, p, "html")), cmd!("file.properties", "Properties", ["File"], None, "{title?, subject?, author?, company?, keywords?, description?}", has_doc, properties), @@ -25,7 +25,9 @@ pub fn specs() -> Vec { always, options ), - cmd!(noundo "window.activate", "Switch Window", ["Window"], None, "{index}", has_doc, activate_window), + cmd!(noundo "window.activate", "Switch Window", ["Window"], None, "{window} | {doc} | {index} (a window id, a workbook id, or the workbook's position in the list)", has_doc, activate_window), + cmd!(noundo "window.close", "Close Window", ["Window"], None, "{window?, doc?, force?: bool} → {closed, document}: closes a window (the active one by default); its workbook closes with its last window, asking first when it has unsaved changes unless `force`", has_doc, close_window), + cmd!(query "window.list", "Windows", ["Window"], None, "{} → [{window, doc, index, title, dirty, active, hidden}]", always, |s, _| Ok(s.window_list())), ] } @@ -159,7 +161,16 @@ fn save_bytes(s: &mut Session, p: &Json) -> Result { Ok(json!({"name": name, "base64": crate::io::base64_encode(&bytes), "bytes": bytes.len()})) } +/// File › Close closes the active window; the workbook closes with its last window. fn close(s: &mut Session, p: &Json) -> Result { + if !bool_param(p, "document").unwrap_or(false) + && let Some(id) = s.active_window() + && let Some(doc) = s.window(id).map(|w| w.doc_uid) + && s.windows().iter().filter(|w| w.doc_uid == doc).count() > 1 + { + s.close_window(id); + return Ok(json!({"closed": true, "document": false})); + } let force = bool_param(p, "force").unwrap_or(false); let d = s.doc()?; if d.is_dirty() && !force { @@ -220,10 +231,35 @@ fn options(s: &mut Session, _: &Json) -> Result { } fn activate_window(s: &mut Session, p: &Json) -> Result { - let i = u32_param(p, "index").ok_or_else(|| bad("window.activate", "missing `index`"))? as usize; - if i >= s.documents().len() { - return Err(bad("window.activate", "no such window")); - } - s.set_active(i); + let id = match u32_param(p, "index").filter(|_| p.get("window").is_none_or(Json::is_null) && p.get("doc").is_none_or(Json::is_null)) { + Some(i) => { + let uid = s.documents().get(i as usize).map(|d| d.uid).ok_or_else(|| bad("window.activate", "no such window"))?; + s.doc_window(uid).ok_or_else(|| bad("window.activate", "no such window"))? + } + None if p.get("window").is_none_or(Json::is_null) && p.get("doc").is_none_or(Json::is_null) => { + return Err(bad("window.activate", "missing `window`")); + } + None => super::extra::window_param(s, "window.activate", p)?, + }; + s.unhide_window(id); ok() } + +fn close_window(s: &mut Session, p: &Json) -> Result { + let id = super::extra::window_param(s, "window.close", p)?; + let doc = s.window(id).map(|w| w.doc_uid).unwrap_or_default(); + if s.windows().iter().filter(|w| w.doc_uid == doc).count() > 1 { + s.close_window(id); + return Ok(json!({"closed": true, "document": false})); + } + let force = bool_param(p, "force").unwrap_or(false); + let d = s.documents().iter().find(|d| d.uid == doc).ok_or(crate::EngineError::NoDocument)?; + if d.is_dirty() && !force { + let title = d.display_title(); + s.activate_window(id); + s.ui_requests.push(crate::UiRequest::Dialog("saveChanges".into(), json!({"title": title}))); + return Ok(json!({"closed": false, "dirty": true})); + } + s.close_window(id); + Ok(json!({"closed": true, "document": true})) +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index aeeaa9bdb0..74924d542a 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -22,6 +22,7 @@ pub mod pivot_calc; pub mod sample; pub mod selection; pub mod tables; +pub mod windows; use std::sync::Arc; @@ -38,6 +39,7 @@ pub use gridcraft_formula as formula; pub use gridcraft_model as model; pub use gridcraft_numfmt as numfmt; pub use selection::Selection; +pub use windows::{Target, TargetGuard, WindowState, WindowView}; #[derive(Debug, thiserror::Error)] pub enum EngineError { @@ -286,7 +288,15 @@ pub type TextWidth = Arc f32 + Send + Sync>; #[derive(Default)] pub struct Session { docs: Vec, + /// Index in `docs` of the active window's workbook. active: usize, + /// Windows on the open workbooks ([`windows`]); every workbook has at least one. + windows: Vec, + active_window: Option, + /// Last window id handed out. + next_window: u64, + /// Window activation counter (most recently used order). + activation: u64, pub clipboard: Option, pub prefs: Prefs, pub mode: Mode, @@ -304,8 +314,6 @@ pub struct Session { pub hide_ruler: bool, /// Journal position where action recording started. pub recording: Option, - /// Windows hidden with View › Hide. - pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. pub draw_tool: String, pub draw_color: String, @@ -330,9 +338,15 @@ impl Session { pub fn active_index(&self) -> usize { self.active } + /// Makes the workbook at `i` active, in its most recently active window (unless the active + /// window already shows it). pub fn set_active(&mut self, i: usize) { - if i < self.docs.len() { - self.active = i; + let Some(uid) = self.docs.get(i).map(|d| d.uid) else { return }; + if self.active == i && self.active_window.and_then(|w| self.window(w)).is_some_and(|w| w.doc_uid == uid) { + return; + } + if let Some(w) = self.doc_window(uid) { + self.activate_window(w); } } pub fn active(&self) -> Option<&DocState> { @@ -351,16 +365,23 @@ impl Session { pub fn doc_mut(&mut self) -> Result<&mut DocState> { self.active_mut().ok_or(EngineError::NoDocument) } + /// Opens a workbook in a new window, which becomes the active one. pub fn add_document(&mut self, d: DocState) -> usize { + let uid = d.uid; self.docs.push(d); - self.active = self.docs.len() - 1; - self.active + let w = self.push_window(uid); + self.activate_window(w); + self.docs.len() - 1 } + /// Closes a workbook and its windows; the most recently active remaining window comes forward. pub fn close_document(&mut self, i: usize) { if i < self.docs.len() { - self.docs.remove(i); - if self.active >= self.docs.len() { - self.active = self.docs.len().saturating_sub(1); + let d = self.docs.remove(i); + self.windows.retain(|w| w.doc_uid != d.uid); + if self.active_window.is_some_and(|w| self.window(w).is_none()) { + self.activate_recent(); + } else if let Some(di) = self.active_window.and_then(|w| self.window(w)).and_then(|w| self.document_index(w.doc_uid)) { + self.active = di; } } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 07a32209f0..6f7f7f4bcb 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -2501,7 +2501,10 @@ fn every_command_survives_empty_params() { let mut s = Session::new(); s.execute("file.new", json!({"sample": "sales"})).unwrap(); for spec in crate::command_specs() { - if matches!(spec.id, "file.close" | "file.open" | "file.save" | "file.saveAs" | "file.exportCsv" | "file.exportHtml" | "insert.picture") { + if matches!( + spec.id, + "file.close" | "window.close" | "file.open" | "file.save" | "file.saveAs" | "file.exportCsv" | "file.exportHtml" | "insert.picture" + ) { continue; } let _ = s.execute(spec.id, json!({})); @@ -4490,3 +4493,112 @@ fn switch_windows_lists_workbooks_by_stable_id_and_switches_to_one() { assert!(s.execute("view.switchWindows", json!({"doc": "Book1"})).is_err()); assert_eq!(s.doc().unwrap().uid, book1, "a bad id leaves the active workbook alone"); } + +#[test] +fn new_window_shares_the_workbook_and_closes_window_by_window() { + let mut s = s(); + let book = s.doc().unwrap().uid; + assert_eq!(s.windows().len(), 1, "a new workbook opens in one window"); + let first = s.active_window().unwrap(); + assert_eq!(s.window_title(first).as_deref(), Some("Book1")); + + let r = s.execute("view.newWindow", json!({})).unwrap(); + let second = r["window"].as_u64().unwrap(); + assert_eq!(s.documents().len(), 1, "no copy of the workbook"); + assert_eq!((s.windows().len(), s.active_window()), (2, Some(second))); + assert_eq!(s.window_title(first).as_deref(), Some("Book1:1")); + assert_eq!(s.window_title(second).as_deref(), Some("Book1:2")); + assert!(!s.doc().unwrap().is_dirty()); + + // An edit in window 2 is in window 1's workbook, and saving from window 2 saves it. + s.execute("cell.set", json!({"cell": "A1", "input": "shared"})).unwrap(); + let path = std::env::temp_dir().join(format!("gridcraft-windows-{}.xlsx", std::process::id())); + s.execute("file.save", json!({"path": path})).unwrap(); + s.execute("window.activate", json!({"window": first})).unwrap(); + assert_eq!((s.doc().unwrap().uid, v(&s, "A1")), (book, Value::text("shared"))); + assert!(!s.doc().unwrap().is_dirty(), "saved from the other window"); + assert_eq!(s.doc().unwrap().path.as_deref(), path.to_str()); + let _ = std::fs::remove_file(&path); + + // Closing one window keeps the workbook and renumbers the titles; the last one closes it. + s.execute("cell.set", json!({"cell": "A2", "input": "unsaved"})).unwrap(); + let r = s.execute("file.close", json!({})).unwrap(); + assert_eq!((r["closed"].as_bool(), r["document"].as_bool()), (Some(true), Some(false)), "no prompt for one of two windows"); + assert_eq!((s.windows().len(), s.active_window()), (1, Some(second))); + assert_eq!(s.window_title(second).as_deref(), Some(s.doc().unwrap().display_title().as_str())); + let r = s.execute("window.close", json!({})).unwrap(); + assert_eq!(r["closed"].as_bool(), Some(false), "the last window asks about unsaved changes"); + assert_eq!(s.documents().len(), 1); + s.execute("window.close", json!({"force": true})).unwrap(); + assert!(s.documents().is_empty() && s.windows().is_empty() && s.active_window().is_none()); +} + +#[test] +fn switching_windows_swaps_their_views_without_dirtying_the_workbook() { + let mut s = s(); + s.execute("home.insertSheet", json!({})).unwrap(); + let path = std::env::temp_dir().join(format!("gridcraft-window-views-{}.xlsx", std::process::id())); + s.execute("file.save", json!({"path": path})).unwrap(); + let _ = std::fs::remove_file(&path); + s.execute("sheet.activate", json!({"sheet": 0})).unwrap(); + s.execute("selection.set", json!({"range": "B2:C3"})).unwrap(); + let first = s.active_window().unwrap(); + let second = s.execute("view.newWindow", json!({})).unwrap()["window"].as_u64().unwrap(); + s.execute("sheet.activate", json!({"sheet": 1})).unwrap(); + s.execute("selection.set", json!({"range": "D4"})).unwrap(); + assert!(!s.doc().unwrap().is_dirty()); + + s.execute("window.activate", json!({"window": first})).unwrap(); + let d = s.doc().unwrap(); + assert_eq!((d.wb.active_sheet, d.selection.a1()), (0, "B2:C3".to_string())); + assert!(!d.is_dirty(), "a window's sheet and selection are not document changes"); + s.execute("view.switchWindows", json!({"window": second})).unwrap(); + let d = s.doc().unwrap(); + assert_eq!((d.wb.active_sheet, d.selection.a1()), (1, "D4".to_string())); + assert!(!d.is_dirty()); + + // A routed command acts in its window and leaves the active one showing. + s.execute_in(crate::Target::Window(first), "cell.set", json!({"cell": "A1", "input": "in window 1"})).unwrap(); + assert_eq!(s.active_window(), Some(second)); + assert_eq!(s.doc().unwrap().wb.sheets[0].value(CellRef::parse("A1").unwrap()), Value::text("in window 1")); + assert_eq!(s.doc().unwrap().selection.a1(), "D4"); +} + +#[test] +fn hide_unhide_and_switch_by_window_id() { + let mut s = s(); + let book1 = s.doc().unwrap().uid; + let w1 = s.active_window().unwrap(); + let w2 = s.execute("view.newWindow", json!({})).unwrap()["window"].as_u64().unwrap(); + s.execute("file.new", json!({})).unwrap(); + let w3 = s.active_window().unwrap(); + s.execute("view.hideWindow", json!({})).unwrap(); + assert_eq!(s.active_window(), Some(w2), "the most recent visible window comes forward"); + let list = s.execute("window.list", json!({})).unwrap(); + let rows = list.as_array().unwrap(); + assert_eq!(rows.len(), 3); + assert_eq!((rows[0]["window"].as_u64(), rows[0]["title"].as_str(), rows[0]["doc"].as_u64()), (Some(w1), Some("Book1:1"), Some(book1))); + assert_eq!((rows[2]["title"].as_str(), rows[2]["hidden"].as_bool()), (Some("Book2"), Some(true))); + assert_eq!(rows[1]["active"].as_bool(), Some(true)); + s.execute("view.unhideWindow", json!({})).unwrap(); + assert_eq!(s.active_window(), Some(w3)); + assert!(!s.window(w3).unwrap().hidden); + // A workbook id goes to that workbook's most recent window. + s.execute("view.switchWindows", json!({"doc": book1})).unwrap(); + assert_eq!(s.active_window(), Some(w2)); + assert!(s.execute("window.activate", json!({"window": 999})).is_err()); + assert_eq!(s.active_window(), Some(w2)); +} + +#[test] +fn open_replaces_an_untouched_blank_workbook_and_its_windows() { + let mut s = s(); + s.execute("view.newWindow", json!({})).unwrap(); + let path = std::env::temp_dir().join(format!("gridcraft-open-blank-{}.csv", std::process::id())); + std::fs::write(&path, "a,b\n1,2\n").unwrap(); + s.execute("file.open", json!({"path": path})).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(s.documents().len(), 1); + assert_eq!(s.windows().len(), 1, "the blank workbook's windows closed with it"); + assert_eq!(s.windows()[0].doc_uid, s.doc().unwrap().uid); +} diff --git a/crates/engine/src/windows.rs b/crates/engine/src/windows.rs new file mode 100644 index 0000000000..8dcb920113 --- /dev/null +++ b/crates/engine/src/windows.rs @@ -0,0 +1,342 @@ +//! Windows on open workbooks. +//! +//! Every open workbook has one or more windows (View › New Window adds another on the same +//! workbook). A window has its own selection, active sheet and per-sheet selections. The active +//! window's view lives in its [`DocState`] (`selection`, `wb.active_sheet`, `sheet_selections`), +//! where every command reads it; each other window keeps its view in [`WindowState`] until it is +//! activated. Swapping a view in or out never makes the workbook dirty: per-window view state is +//! not a document change. + +use serde_json::{Value as Json, json}; + +use crate::{DocState, EngineError, Result, Selection, Session}; + +/// The view state one window keeps while another window on its workbook is showing. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct WindowView { + pub selection: Selection, + pub active_sheet: usize, + /// The active sheet's name, so a sheet inserted, deleted or moved meanwhile in another + /// window still finds it. + pub sheet_name: String, + pub sheet_selections: Vec, +} + +/// One window on an open workbook. +#[derive(Clone, Debug)] +pub struct WindowState { + /// Identifies the window for the rest of the session; never reused. + pub id: u64, + /// The [`DocState::uid`] of the workbook it shows. + pub doc_uid: u64, + /// View › Hide. + pub hidden: bool, + /// `None` while this window's view is the one in the document (the active window, or the + /// workbook's most recently active window); exactly one window per workbook has `None`. + view: Option, + /// Activation order: higher was active more recently. + last_active: u64, +} + +impl WindowView { + fn capture(d: &DocState) -> WindowView { + WindowView { + selection: d.selection.clone(), + active_sheet: d.wb.active_sheet, + sheet_name: d.wb.sheets.get(d.wb.active_sheet).map(|s| s.name.clone()).unwrap_or_default(), + sheet_selections: d.sheet_selections.clone(), + } + } + + fn apply(self, d: &mut DocState) { + let sheets = &d.wb.sheets; + let at = if sheets.get(self.active_sheet).is_some_and(|s| s.name == self.sheet_name) { + self.active_sheet + } else { + sheets.iter().position(|s| s.name == self.sheet_name).unwrap_or(self.active_sheet) + }; + let at = at.min(sheets.len().saturating_sub(1)); + if d.wb.active_sheet != at { + let dirty = d.is_dirty(); + std::sync::Arc::make_mut(&mut d.wb).active_sheet = at; + if !dirty { + d.saved = d.wb.clone(); + } + } + d.selection = self.selection; + d.sheet_selections = self.sheet_selections; + } +} + +/// Which window a command from a script or agent acts on. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Target { + /// The active window. + #[default] + Active, + /// A window by [`WindowState::id`]. + Window(u64), + /// A workbook by [`DocState::uid`]: its most recently active window. + Doc(u64), +} + +impl Target { + /// Reads `window` or `doc` from a JSON object (control channel envelopes, MCP arguments). + /// Neither present is [`Target::Active`]; a value that isn't an id is an error. + pub fn from_json(p: &Json) -> Result { + let id = |k: &str| -> Result> { + match p.get(k) { + None | Some(Json::Null) => Ok(None), + Some(v) => v.as_u64().map(Some).ok_or_else(|| EngineError::Other(format!("`{k}` must be a window or workbook id number"))), + } + }; + Ok(match (id("window")?, id("doc")?) { + (Some(w), _) => Target::Window(w), + (None, Some(d)) => Target::Doc(d), + (None, None) => Target::Active, + }) + } +} + +/// What [`Session::leave_target`] restores. +#[derive(Debug)] +#[must_use] +pub struct TargetGuard { + id: u64, + back: Option, +} + +impl Session { + /// The open windows, in the order they were opened. + pub fn windows(&self) -> &[WindowState] { + &self.windows + } + + /// The active window's id (`None` with no workbook open). + pub fn active_window(&self) -> Option { + self.active_window + } + + pub fn window(&self, id: u64) -> Option<&WindowState> { + self.windows.iter().find(|w| w.id == id) + } + + /// A workbook's windows in opening order. + fn windows_of(&self, doc: u64) -> impl Iterator { + self.windows.iter().filter(move |w| w.doc_uid == doc) + } + + /// The window title: the workbook's name when it has one window, "Name:1", "Name:2"… in + /// opening order when it has several. + pub fn window_title(&self, id: u64) -> Option { + let w = self.window(id)?; + let d = self.docs.iter().find(|d| d.uid == w.doc_uid)?; + let name = d.display_title(); + let mut n = 0; + let mut at = 0; + for x in self.windows_of(w.doc_uid) { + n += 1; + if x.id == id { + at = n; + } + } + Some(if n > 1 { format!("{name}:{at}") } else { name }) + } + + /// The most recently active window of a workbook, preferring one that isn't hidden. + pub fn doc_window(&self, doc: u64) -> Option { + self.windows_of(doc).max_by_key(|w| (!w.hidden, w.last_active)).map(|w| w.id) + } + + /// Resolves a [`Target`] to a window id. + pub fn target_window(&self, t: Target) -> Result { + match t { + Target::Active => self.active_window.ok_or(EngineError::NoDocument), + Target::Window(id) => self.window(id).map(|w| w.id).ok_or_else(|| EngineError::Other(format!("no open window has id {id}"))), + Target::Doc(uid) => self.doc_window(uid).ok_or_else(|| EngineError::Other(format!("no open workbook has id {uid}"))), + } + } + + pub(crate) fn push_window(&mut self, doc_uid: u64) -> u64 { + self.next_window = self.next_window.saturating_add(1); + let id = self.next_window; + self.windows.push(WindowState { id, doc_uid, hidden: false, view: None, last_active: 0 }); + id + } + + /// Makes a window active, swapping its view into its workbook. Unknown ids are ignored. + pub fn activate_window(&mut self, id: u64) { + let Some(wi) = self.windows.iter().position(|w| w.id == id) else { return }; + let Some(w) = self.windows.get(wi) else { return }; + let doc = w.doc_uid; + if w.view.is_some() + && let Some(di) = self.document_index(doc) + { + let live = self.windows.iter().position(|x| x.doc_uid == doc && x.view.is_none()); + let incoming = self.windows.get_mut(wi).and_then(|w| w.view.take()); + if let Some(d) = self.docs.get_mut(di) { + if let Some(li) = live + && let Some(lw) = self.windows.get_mut(li) + { + lw.view = Some(WindowView::capture(d)); + } + if let Some(v) = incoming { + v.apply(d); + } + } + } + self.activation = self.activation.saturating_add(1); + let stamp = self.activation; + if let Some(w) = self.windows.get_mut(wi) { + w.last_active = stamp; + } + self.active_window = Some(id); + if let Some(di) = self.document_index(doc) { + self.active = di; + } + } + + /// Activates the most recently active window that isn't hidden, else any window, else none. + pub(crate) fn activate_recent(&mut self) { + let next = self.windows.iter().max_by_key(|w| (!w.hidden, w.last_active)).map(|w| w.id); + match next { + Some(id) => self.activate_window(id), + None => { + self.active_window = None; + self.active = 0; + } + } + } + + /// View › New Window: another window on the active window's workbook, showing what it shows. + pub fn new_window(&mut self) -> Result { + let current = self.active_window.ok_or(EngineError::NoDocument)?; + let doc = self.window(current).map(|w| w.doc_uid).ok_or(EngineError::NoDocument)?; + let view = self.doc().map(WindowView::capture)?; + // The new window takes over the live view (identical at this point); the old one keeps a copy. + if let Some(w) = self.windows.iter_mut().find(|w| w.doc_uid == doc && w.view.is_none()) { + w.view = Some(view); + } + let id = self.push_window(doc); + self.activate_window(id); + Ok(id) + } + + /// Closes one window. The workbook stays open while it has another window; closing its last + /// window closes the workbook (unsaved changes are the caller's to ask about). + pub fn close_window(&mut self, id: u64) { + let Some(wi) = self.windows.iter().position(|w| w.id == id) else { return }; + let doc = self.windows.get(wi).map(|w| w.doc_uid).unwrap_or_default(); + if self.windows_of(doc).count() <= 1 { + if let Some(di) = self.document_index(doc) { + self.close_document(di); + } + return; + } + let closed = self.windows.remove(wi); + if closed.view.is_none() { + // Its view was the one in the document: the workbook's next window takes it over. + if let Some(next) = self.windows.iter().filter(|w| w.doc_uid == doc).max_by_key(|w| w.last_active).map(|w| w.id) { + let incoming = self.windows.iter_mut().find(|w| w.id == next).and_then(|w| w.view.take()); + if let Some(v) = incoming + && let Some(d) = self.docs.iter_mut().find(|d| d.uid == doc) + { + v.apply(d); + } + } + } + if self.active_window == Some(id) { + self.activate_recent(); + } + } + + /// View › Hide on a window; the most recent visible window comes forward. + pub fn hide_window(&mut self, id: u64) { + let Some(w) = self.windows.iter_mut().find(|w| w.id == id) else { return }; + w.hidden = true; + if self.active_window == Some(id) && self.windows.iter().any(|w| !w.hidden) { + self.activate_recent(); + } + } + + /// View › Unhide: shows a hidden window and activates it. + pub fn unhide_window(&mut self, id: u64) { + let Some(w) = self.windows.iter_mut().find(|w| w.id == id) else { return }; + w.hidden = false; + self.activate_window(id); + } + + /// The most recently hidden window. + pub fn last_hidden_window(&self) -> Option { + self.windows.iter().filter(|w| w.hidden).max_by_key(|w| w.last_active).map(|w| w.id) + } + + /// The windows as scripts and the Switch Windows lists see them: + /// `[{window, doc, index, title, dirty, active, hidden}]`, `index` being the workbook's + /// position in [`Session::documents`]. + pub fn window_list(&self) -> Json { + Json::Array( + self.windows + .iter() + .filter_map(|w| { + let index = self.document_index(w.doc_uid)?; + let d = self.docs.get(index)?; + Some(json!({ + "window": w.id, + "doc": w.doc_uid, + "index": index, + "title": self.window_title(w.id).unwrap_or_default(), + "dirty": d.is_dirty(), + "active": self.active_window == Some(w.id), + "hidden": w.hidden, + })) + }) + .collect(), + ) + } + + /// Runs `f` with the target window active, then brings back the window that was active, so a + /// script acting on another window doesn't change what the user is looking at. When `f` + /// itself switched windows (`window.activate`, `file.new`, a close), its choice stands. + pub fn with_target(&mut self, t: Target, f: impl FnOnce(&mut Session) -> T) -> Result { + let guard = self.enter_target(t)?; + let r = f(self); + self.leave_target(guard); + Ok(r) + } + + /// The first half of [`Session::with_target`], for callers whose closure can't borrow the + /// session alone (the UI runs commands through its app): activates the target window and + /// returns what [`Session::leave_target`] needs to bring the previous one back. + pub fn enter_target(&mut self, t: Target) -> Result> { + if t == Target::Active { + return Ok(None); + } + let id = self.target_window(t)?; + let back = self.active_window; + self.activate_window(id); + Ok(Some(TargetGuard { id, back })) + } + + pub fn leave_target(&mut self, guard: Option) { + let Some(TargetGuard { id, back: Some(back) }) = guard else { return }; + if self.active_window == Some(id) && back != id && self.window(back).is_some() { + self.activate_window(back); + } + } + + /// [`Session::execute`] on a target window. + pub fn execute_in(&mut self, t: Target, id: &str, params: Json) -> Result { + self.with_target(t, |s| s.execute(id, params))? + } + + /// [`Session::execute_headless`] on a target window. + pub fn execute_headless_in(&mut self, t: Target, id: &str, params: Json) -> Result { + self.with_target(t, |s| s.execute_headless(id, params))? + } +} + +/// Commands that take `window`/`doc` as their own parameters rather than as a target. +pub fn is_window_command(id: &str) -> bool { + id.starts_with("window.") || matches!(id, "view.newWindow" | "view.hideWindow" | "view.unhideWindow" | "view.switchWindows") +} diff --git a/crates/engine/tests/command_sequence_fuzz.rs b/crates/engine/tests/command_sequence_fuzz.rs index f8cb603cba..1724b9b9d2 100644 --- a/crates/engine/tests/command_sequence_fuzz.rs +++ b/crates/engine/tests/command_sequence_fuzz.rs @@ -59,7 +59,8 @@ const EXCLUDED: &[(&str, &str)] = &[ ("file.exportPdf", "renders a PDF: slow, and may write files"), ("file.print", "renders a PDF: slow"), ("window.activate", "switches documents"), - ("view.newWindow", "opens another document"), + ("window.close", "closes the document under test"), + ("view.newWindow", "opens another window, whose view swaps with the first"), ("insert.picture", "reads files"), ("pageLayout.background", "reads files"), ("formulas.calculationOptions", "manual and iterative calculation differ from a full recalc by design"), diff --git a/crates/mcp/src/headless.rs b/crates/mcp/src/headless.rs index 5f96fbd250..28a5cecce5 100644 --- a/crates/mcp/src/headless.rs +++ b/crates/mcp/src/headless.rs @@ -1,6 +1,6 @@ //! An in-process engine session that answers the engine-level control-channel methods itself. -use gridcraft_engine::Session; +use gridcraft_engine::{Session, Target}; use serde_json::{Value, json}; use crate::backend::Backend; @@ -33,9 +33,9 @@ impl Headless { Self { session } } - fn execute(&mut self, id: &str, params: Value) -> Result { + fn execute(&mut self, target: Target, id: &str, params: Value) -> Result { // Programmatic calls never open dialogs; anything a command queued for a UI is dropped. - self.session.execute_headless(id, params).map_err(|e| e.to_string()) + self.session.execute_headless_in(target, id, params).map_err(|e| e.to_string()) } fn commands(&self, params: &Value) -> Value { @@ -68,14 +68,28 @@ impl Backend for Headless { if !(p.is_object() || p.is_null()) { return Err(format!("engine.execute: `params` of `{id}` must be an object")); } - self.execute(&id, p) + let target = Target::from_json(¶ms).map_err(|e| e.to_string())?; + self.execute(target, &id, p) } "engine.commands" => Ok(self.commands(¶ms)), "engine.journal" => Ok(Value::Array(self.session.journal.iter().map(|(id, p)| json!({"command": id, "params": p})).collect())), m if m.starts_with("ui.") || m.starts_with("app.screenshot") => Err(format!("{m}: {NO_UI}")), // Read-only engine methods (`document.inspect`, `sheet.read`, `cell.get`, …) are // commands with the same id. - m if gridcraft_engine::find_command(m).is_some() => self.execute(m, params), + // Their params may name a `window` or `doc` to act in, except for the window commands, + // whose own params those are. + m if gridcraft_engine::find_command(m).is_some() => { + if gridcraft_engine::windows::is_window_command(m) { + return self.execute(Target::Active, m, params); + } + let target = Target::from_json(¶ms).map_err(|e| e.to_string())?; + let mut params = params; + if let Some(o) = params.as_object_mut() { + o.remove("window"); + o.remove("doc"); + } + self.execute(target, m, params) + } other => Err(format!("unknown method `{other}`")), } } diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rs index c699ccf691..5ceeea0ce1 100644 --- a/crates/mcp/src/tests.rs +++ b/crates/mcp/src/tests.rs @@ -106,6 +106,7 @@ fn tools_list_has_schemas() { "redo", "screenshot", "list_functions", + "list_windows", ] { assert!(names.contains(&want), "missing tool {want}"); } @@ -548,3 +549,31 @@ fn invalid_sort_keys_are_invalid_params() { } ok(&mut s, "sort_range", json!({"range": "A1:A3", "keys": ["A", {"column": "A", "order": "desc"}]})); } + +#[test] +fn a_window_argument_routes_tools_to_that_window() { + let mut s = server(); + ok(&mut s, "set_cell", json!({"cell": "A1", "input": "book one"})); + ok(&mut s, "new_workbook", json!({})); + let windows = ok(&mut s, "list_windows", json!({})); + let rows = windows.as_array().unwrap(); + assert_eq!(rows.len(), 2, "{windows}"); + let (w1, book1) = (rows[0]["window"].as_u64().unwrap(), rows[0]["doc"].as_u64().unwrap()); + assert_eq!((rows[0]["title"].as_str(), rows[1]["active"].as_bool()), (Some("Book1"), Some(true))); + + // Book2 stays active; reads and writes go to Book1's window. + ok(&mut s, "set_cell", json!({"cell": "B1", "input": "=A1", "window": w1})); + let got = ok(&mut s, "get_cell", json!({"cell": "B1", "doc": book1})); + assert!(got.to_string().contains("book one"), "{got}"); + let book2 = ok(&mut s, "read_range", json!({"range": "A1:B1"})); + assert!(!book2.to_string().contains("book one"), "{book2}"); + let info = ok(&mut s, "inspect_workbook", json!({"window": w1})); + assert!(info.to_string().contains("Book1"), "{info}"); + assert!(fails(&mut s, "get_cell", json!({"cell": "A1", "window": 999})).contains("no open window")); + + // A second window on Book1 gets a numbered title. + ok(&mut s, "execute_command", json!({"command": "view.newWindow", "window": w1})); + let titles: Vec = + ok(&mut s, "list_windows", json!({})).as_array().unwrap().iter().map(|r| r["title"].as_str().unwrap().to_string()).collect(); + assert_eq!(titles, ["Book1:1", "Book2", "Book1:2"]); +} diff --git a/crates/mcp/src/tools.rs b/crates/mcp/src/tools.rs index a6879b3ff1..b422b5f346 100644 --- a/crates/mcp/src/tools.rs +++ b/crates/mcp/src/tools.rs @@ -106,8 +106,26 @@ const BORDER_PRESETS: &[&str] = &[ "diagonalUp", ]; -/// All tools, in `tools/list` order. +/// Tools that don't act on one workbook, so they take no `window`/`doc` target. +const UNTARGETED: &[&str] = &["list_commands", "list_functions", "list_windows", "new_workbook", "open_workbook", "screenshot"]; + +/// All tools, in `tools/list` order. Tools that act on a workbook take an optional `window` or +/// `doc` id (from list_windows) and default to the active window. pub fn tool_definitions() -> Vec { + let mut tools = base_tool_definitions(); + for t in &mut tools { + if UNTARGETED.iter().any(|n| t["name"] == *n) { + continue; + } + if let Some(props) = t["inputSchema"]["properties"].as_object_mut() { + props.insert("window".into(), int("Window id from list_windows (default: the active window)")); + props.insert("doc".into(), int("Workbook id from list_windows: its most recent window (default: the active window)")); + } + } + tools +} + +fn base_tool_definitions() -> Vec { let empty = || obj(json!({}), &[]); vec![ // ----- generic ----- @@ -125,6 +143,14 @@ pub fn tool_definitions() -> Vec { ), false, ), + tool( + "list_windows", + "List windows", + "List the open windows: window id, workbook (`doc`) id, title (\"Book1\", or \"Book1:2\" for a workbook's second \ + window), unsaved changes, active and hidden. Pass a `window` or `doc` id to other tools to act there.", + empty(), + true, + ), tool( "list_commands", "List commands", @@ -608,6 +634,7 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result exec(b, cmd, p) } "list_commands" => b.call("engine.commands", pick(a, &["search"])), + "list_windows" => exec(b, "window.list", json!({})), "inspect_workbook" => b.call("document.inspect", json!({})), "read_range" => b.call("sheet.read", pick(a, &["range", "sheet", "formulas", "formatted"])), "write_range" => exec(b, "range.setValues", pick(a, &["range", "values"])), @@ -638,6 +665,35 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result } } +/// Sends every engine call of one tool to the window its `window`/`doc` argument names. +struct Targeted<'a> { + inner: &'a mut dyn Backend, + target: Map, +} + +impl Backend for Targeted<'_> { + fn call(&mut self, method: &str, params: Value) -> Result { + let envelope = if method == "engine.execute" { + params + } else if gridcraft_engine::find_command(method).is_some() { + json!({"command": method, "params": params}) + } else { + return self.inner.call(method, params); + }; + let mut envelope = envelope; + if let Some(o) = envelope.as_object_mut() { + o.extend(self.target.clone()); + } + self.inner.call("engine.execute", envelope) + } + fn has_ui(&self) -> bool { + self.inner.has_ui() + } + fn describe(&self) -> String { + self.inner.describe() + } +} + /// Runs one tool. Failures become `isError` results with a readable message. pub fn call_tool(b: &mut dyn Backend, name: &str, args: &Value) -> ToolResult { let empty = Map::new(); @@ -646,7 +702,11 @@ pub fn call_tool(b: &mut dyn Backend, name: &str, args: &Value) -> ToolResult { Value::Null => &empty, _ => return ToolResult::error("tool arguments must be a JSON object"), }; - match dispatch(b, name, a) { + let target: Map = + ["window", "doc"].iter().filter_map(|k| a.get(*k).filter(|v| !v.is_null()).map(|v| (k.to_string(), v.clone()))).collect(); + let r = + if target.is_empty() || UNTARGETED.contains(&name) { dispatch(b, name, a) } else { dispatch(&mut Targeted { inner: b, target }, name, a) }; + match r { Ok(v) => ToolResult::json(&v), Err(e) => ToolResult::error(format!("{name}: {e}")), } diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 11d535ac41..bf1dedac10 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -104,6 +104,38 @@ fn run(app: &mut SheetApp, id: &str, params: Json) -> Result { r } +/// [`run`] in another window (`window` or `doc` from the request), bringing back the active +/// window afterwards unless the command itself switched windows. +fn run_in(app: &mut SheetApp, target: gridcraft_engine::Target, id: &str, params: Json) -> Result { + let guard = app.session.enter_target(target).map_err(|e| e.to_string())?; + let r = run(app, id, params); + app.session.leave_target(guard); + r +} + +/// A command called by its id as the method. Its params may name a `window` or `doc` to act in, +/// except for the window commands, whose own params those are. +fn direct(app: &mut SheetApp, id: &str, p: &Json, via_app: bool) -> Result { + let mut params = p.clone(); + let target = if gridcraft_engine::windows::is_window_command(id) { + gridcraft_engine::Target::Active + } else { + let t = gridcraft_engine::Target::from_json(p).map_err(|e| e.to_string())?; + if let Some(o) = params.as_object_mut() { + o.remove("window"); + o.remove("doc"); + } + t + }; + if via_app { + return run_in(app, target, id, params); + } + let guard = app.session.enter_target(target).map_err(|e| e.to_string())?; + let r = app.session.run(id, params); + app.session.leave_target(guard); + r +} + pub fn inspect(app: &SheetApp, ctx: &egui::Context) -> Json { let r = ctx.content_rect(); let v = app.view(); @@ -120,6 +152,8 @@ pub fn inspect(app: &SheetApp, ctx: &egui::Context) -> Json { "mode": format!("{:?}", app.session.mode), "documents": app.session.documents().iter().map(|d| json!({"title": d.display_title(), "dirty": d.is_dirty()})).collect::>(), "activeDocument": app.session.active_index(), + "windows": app.session.window_list(), + "activeWindow": app.session.active_window(), "perf": {"frameMs": app.perf.frame_ms, "gridMs": app.perf.grid_ms, "fps": app.perf.fps}, }) } @@ -222,7 +256,11 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) - "engine.execute" | "command" | "ui.menu.invoke" => { let Some(id) = s("command").or(s("id")) else { return err("missing `command`") }; let params = p.get("params").cloned().unwrap_or(json!({})); - wrap(run(app, id, if params.is_null() { json!({}) } else { params })) + let target = match gridcraft_engine::Target::from_json(p) { + Ok(t) => t, + Err(e) => return err(e), + }; + wrap(run_in(app, target, id, if params.is_null() { json!({}) } else { params })) } "engine.commands" => { let mut v: Vec = app.session.commands().into_iter().map(|c| serde_json::to_value(c).unwrap_or_default()).collect(); @@ -256,7 +294,7 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) - ok(Json::Array(filter_commands(v, s("search")))) } "engine.journal" => ok(json!(app.session.journal.iter().map(|(id, p)| json!({"command": id, "params": p})).collect::>())), - "document.inspect" | "sheet.read" | "cell.get" | "selection.stats" => wrap(app.session.run(method, p.clone())), + "document.inspect" | "sheet.read" | "cell.get" | "selection.stats" => wrap(direct(app, method, p, false)), "ui.inspect" => ok(inspect(app, ctx)), "ui.ribbon" => { app.ui.ribbon_tab = s("tab").unwrap_or("Home").to_string(); @@ -440,7 +478,7 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) - ok(Json::Null) } // Anything else that names a command runs it. - other if gridcraft_engine::find_command(other).is_some() => wrap(run(app, other, p.clone())), + other if gridcraft_engine::find_command(other).is_some() => wrap(direct(app, other, p, true)), other => err(format!("unknown method `{other}`")), }; ctx.request_repaint(); diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 612ca93b27..be474d37f2 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1327,7 +1327,7 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) { let quitting = d.values.get("quit").and_then(Json::as_bool) == Some(true); ui.horizontal(|ui| { if ui.button("Don't Save").clicked() { - app.run_or_alert("file.close", json!({"force": true})); + app.run_or_alert("file.close", json!({"force": true, "document": true})); if quitting { app.continue_quit(ui.ctx()); } @@ -1340,7 +1340,7 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) { if ui.button("Save").clicked() && app.commit_edit(0, 0, false, false) { app.run_or_alert("file.save", json!({})); if app.session.active().is_some_and(|x| !x.is_dirty()) { - app.run_or_alert("file.close", json!({"force": true})); + app.run_or_alert("file.close", json!({"force": true, "document": true})); if quitting { app.continue_quit(ui.ctx()); } diff --git a/crates/ui-egui/src/editor.rs b/crates/ui-egui/src/editor.rs index 3f23e8bed2..95e74d9594 100644 --- a/crates/ui-egui/src/editor.rs +++ b/crates/ui-egui/src/editor.rs @@ -20,6 +20,9 @@ pub const REF_COLORS: [Color32; 8] = [ pub struct EditState { /// The workbook (`DocState::uid`) the draft belongs to: it is drawn and committed only there. pub doc: u64, + /// The window it was typed in: switching to another window, even on the same workbook, + /// commits it there first (`None` until the app sets it). + pub window: Option, pub sheet: usize, /// Syntax of this edit, captured when it starts (prevents a language switch reinterpreting it). pub locale: gridcraft_engine::formula::FormulaLocale, @@ -56,6 +59,7 @@ impl EditState { let original = (!enter_mode).then(|| text.clone()); EditState { doc, + window: None, sheet, locale: Default::default(), cell, diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index cc478168ef..d330d1fcf8 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -166,10 +166,12 @@ pub struct Notice { pub action: Option<(String, String)>, } -/// One open workbook, as View › Switch Windows and the macOS Window menu list it. +/// One open window, as View › Switch Windows and the macOS Window menu list it. #[derive(Clone, Debug, PartialEq, Eq)] pub struct OpenWindow { - /// The workbook's `DocState::uid`, the `doc` param of `view.switchWindows`. + /// The window's id, the `window` param of `view.switchWindows`. + pub window: u64, + /// Its workbook's `DocState::uid`. pub doc: u64, pub title: String, pub dirty: bool, @@ -177,7 +179,8 @@ pub struct OpenWindow { } impl OpenWindow { - /// The title, with a bullet after it while the workbook has unsaved changes. + /// The title ("Book1", or "Book1:2" for a workbook's second window), with a bullet after it + /// while the workbook has unsaved changes. pub fn label(&self) -> String { if self.dirty { format!("{} •", self.title) } else { self.title.clone() } } @@ -256,6 +259,7 @@ pub struct SheetApp { pub ui: UiState, /// The window chrome to draw (see [`Chrome`]). pub chrome: Chrome, + /// Scroll position per (window id, sheet index). pub views: HashMap<(u64, usize), SheetView>, pub editor: Option, pub text_box_editor: Option, @@ -410,7 +414,7 @@ impl SheetApp { fn run_command(&mut self, id: &str, params: Json) -> Result { let switches_window = id.starts_with("window.") || matches!(id, "view.newWindow" | "view.hideWindow" | "view.unhideWindow") - || (id == "view.switchWindows" && params.get("doc").is_some_and(|d| !d.is_null())); + || (id == "view.switchWindows" && ["window", "doc"].iter().any(|k| params.get(*k).is_some_and(|d| !d.is_null()))); if id.starts_with("file.") || switches_window { self.settle_drafts()?; } @@ -567,9 +571,9 @@ impl SheetApp { if id == "insert.recommendedCharts" && params.get("type").is_none() { return self.open_dialog("recommendedCharts", json!({})); } - // Switch Windows with no workbook named (key tip, command search) opens the View tab's + // Switch Windows with no window named (key tip, command search) opens the View tab's // list to choose from; programmatic callers of `run` get the list back. - if id == "view.switchWindows" && params.get("doc").is_none_or(Json::is_null) { + if id == "view.switchWindows" && ["window", "doc"].iter().all(|k| params.get(*k).is_none_or(Json::is_null)) { self.ui.ribbon_tab = "View".into(); self.ui.ribbon_collapsed = false; self.open_switch_windows = true; @@ -914,27 +918,36 @@ impl SheetApp { } } - /// The open workbooks in order, for the Switch Windows lists. + /// The open windows in order, for the Switch Windows lists. pub fn open_windows(&self) -> Vec { - let active = self.session.active_index(); + let active = self.session.active_window(); self.session - .documents() + .windows() .iter() - .enumerate() - .map(|(i, d)| OpenWindow { doc: d.uid, title: d.display_title(), dirty: d.is_dirty(), active: i == active }) + .filter_map(|w| { + let d = self.session.documents().iter().find(|d| d.uid == w.doc_uid)?; + let title = self.session.window_title(w.id)?; + Some(OpenWindow { window: w.id, doc: d.uid, title, dirty: d.is_dirty(), active: active == Some(w.id) }) + }) .collect() } + /// The active workbook and sheet: what drafts, text boxes and picture targets belong to. pub fn view_key(&self) -> Option<(u64, usize)> { self.session.active().map(|d| (d.uid, d.wb.active_sheet)) } + /// The active window and sheet: each window scrolls on its own. + fn scroll_key(&self) -> Option<(u64, usize)> { + Some((self.session.active_window()?, self.session.active()?.wb.active_sheet)) + } + pub fn view(&self) -> SheetView { - self.view_key().and_then(|k| self.views.get(&k).copied()).unwrap_or_else(|| self.saved_view()) + self.scroll_key().and_then(|k| self.views.get(&k).copied()).unwrap_or_else(|| self.saved_view()) } pub fn view_mut(&mut self) -> Option<&mut SheetView> { - let k = self.view_key()?; + let k = self.scroll_key()?; let saved = self.saved_view(); Some(self.views.entry(k).or_insert(saved)) } @@ -956,10 +969,11 @@ impl SheetApp { /// cell to the engine so a save writes them (`sheetView topLeftCell`). Normal view only: /// Page Layout view scrolls over pages, not cells. pub fn store_view_state(&mut self) { + let window = self.session.active_window(); let Some(d) = self.session.active_mut() else { return }; let mut tops = Vec::new(); - for (&(uid, si), v) in &self.views { - let Some(sh) = d.wb.sheets.get(si).filter(|sh| uid == d.uid && sh.view == gridcraft_engine::model::SheetViewMode::Normal) else { + for (&(w, si), v) in &self.views { + let Some(sh) = d.wb.sheets.get(si).filter(|sh| Some(w) == window && sh.view == gridcraft_engine::model::SheetViewMode::Normal) else { continue; }; let (fr, fc) = sh.freeze.unwrap_or((0, 0)); @@ -1005,6 +1019,7 @@ impl SheetApp { None => (current.clone(), false), }; let mut ed = editor::EditState::new(d.uid, d.wb.active_sheet, at, text, replace, from_formula_bar); + ed.window = self.session.active_window(); ed.locale = locale; if replace && self.ui.autocomplete && !ed.is_formula() { ed.completion = editor::column_completion(sh, at, &ed.text); @@ -1015,29 +1030,38 @@ impl SheetApp { /// Commits the edit; `then` moves the selection (dr, dc) afterwards. /// - /// The draft goes into the workbook it was typed in. If another workbook is showing, that - /// workbook stays active afterwards; a draft its own workbook refuses (validation, a bad - /// formula) brings that workbook back to the front with the draft still open. + /// The draft goes into the window and workbook it was typed in. If another window is + /// showing, that window stays active afterwards; a draft its own workbook refuses + /// (validation, a bad formula) brings its window back to the front with the draft still open. pub fn commit_edit(&mut self, dr: i64, dc: i64, array: bool, fill_selection: bool) -> bool { - let Some(doc) = self.editor.as_ref().map(|ed| ed.doc) else { return true }; - let shown = self.session.active().map(|d| d.uid); - if shown == Some(doc) { + let Some((doc, window)) = self.editor.as_ref().map(|ed| (ed.doc, ed.window)) else { return true }; + if !self.draft_elsewhere() { return self.commit_edit_here(dr, dc, array, fill_selection); } - let Some(owner) = self.session.document_index(doc) else { + // Its window, or (when that window closed) its workbook's most recent one. + let Some(owner) = window.filter(|w| self.session.window(*w).is_some()).or_else(|| self.session.doc_window(doc)) else { self.cancel_edit(); return true; }; - let back = self.session.active_index(); - self.session.set_active(owner); + let back = self.session.active_window(); + self.session.activate_window(owner); if !self.commit_edit_here(dr, dc, array, fill_selection) { - // Refused: its workbook stays in front with the draft open and the reason shown. + // Refused: its window stays in front with the draft open and the reason shown. return false; } - self.session.set_active(back); + if let Some(back) = back { + self.session.activate_window(back); + } true } + /// The cell draft belongs to a window or workbook other than the one showing. + fn draft_elsewhere(&self) -> bool { + self.editor.as_ref().is_some_and(|ed| { + self.session.active().map(|d| d.uid) != Some(ed.doc) || ed.window.is_some_and(|w| self.session.active_window() != Some(w)) + }) + } + /// Commits the cell and text-box drafts, as Enter would, before a command that can show /// another workbook or write this one to a file, so a draft never lands in another workbook. /// A draft its workbook refuses stays open with the reason shown, and the command doesn't run. @@ -1360,10 +1384,10 @@ impl SheetApp { } let t0 = now_ms(); text_box::before_ui(self, &ctx); - // Another workbook came forward without going through a command (which settles drafts - // first): the draft goes into its own workbook instead of floating over this one. If that - // workbook refuses it, it comes back to the front with the draft open. - if self.editor.as_ref().is_some_and(|ed| self.session.active().map(|d| d.uid) != Some(ed.doc)) { + // Another window came forward without going through a command (which settles drafts + // first): the draft goes into its own window instead of floating over this one. If its + // workbook refuses it, its window comes back to the front with the draft open. + if self.draft_elsewhere() { self.commit_edit(0, 0, false, false); } // The language the widgets translate with this frame (see `i18n::current`). diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index d90b642ea2..6bd45e18f0 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -2403,7 +2403,7 @@ fn switch_windows(app: &mut SheetApp, ui: &mut Ui) { for w in app.open_windows() { let label = format!("{}{}", if w.active { "✓ " } else { " " }, w.label()); if ui.add(egui::Button::selectable(w.active, label).min_size(vec2(220.0, 22.0))).clicked() { - act(app, "view.switchWindows", json!({"doc": w.doc})); + act(app, "view.switchWindows", json!({"window": w.window})); ui.close(); } } diff --git a/crates/ui-egui/tests/file_menu.rs b/crates/ui-egui/tests/file_menu.rs index 5c447cb633..fa9350f7a1 100644 --- a/crates/ui-egui/tests/file_menu.rs +++ b/crates/ui-egui/tests/file_menu.rs @@ -484,3 +484,65 @@ fn save_a_copy_with_a_bare_name_asks_where_to_save_it() { assert_eq!(asked.lock().unwrap().len(), 1); let _ = std::fs::remove_dir_all(&dir); } + +#[test] +fn new_window_shows_the_same_workbook_and_a_draft_settles_in_its_own_window() { + let mut h = harness(Services::default()); + h.state_mut().run("selection.set", serde_json::json!({"range": "B2"})).unwrap(); + let first = h.state().session.active_window().unwrap(); + h.state_mut().run_or_alert("view.newWindow", serde_json::json!({})); + h.run_steps(2); + let second = h.state().session.active_window().unwrap(); + let book = h.state().session.doc().unwrap().uid; + assert_ne!(first, second); + assert_eq!(h.state().session.documents().len(), 1, "one workbook, two windows"); + let titles: Vec = h.state().open_windows().iter().map(|w| w.title.clone()).collect(); + assert_eq!(titles, ["Book1:1", "Book1:2"]); + + // A draft typed in window 2 is entered in window 2's cell before window 1 comes forward. + h.state_mut().run("selection.set", serde_json::json!({"range": "D4"})).unwrap(); + h.input_mut().events.push(egui::Event::Text("typed".into())); + h.run_steps(2); + h.state_mut().run_or_alert("view.switchWindows", serde_json::json!({"window": first})); + h.run_steps(2); + assert!(h.state().editor.is_none(), "the draft doesn't follow into window 1"); + assert_eq!(h.state().session.active_window(), Some(first)); + let d = h.state().session.doc().unwrap(); + assert_eq!(d.wb.active().unwrap().value(CellRef::parse("D4").unwrap()), Value::text("typed")); + assert_eq!(d.selection.active, CellRef::parse("B2").unwrap(), "window 1 keeps its own selection"); + + // Quitting asks once about the workbook, whichever window shows it. + request_window_close(&mut h); + let sent = click_commands(&mut h, "Don't Save"); + assert!(sent.contains(&egui::ViewportCommand::Close), "{sent:?}"); + assert!(h.state().session.document_index(book).is_none(), "both windows closed with the workbook"); +} + +#[test] +fn control_requests_route_to_a_window_and_list_windows() { + use gridcraft_ui_egui::control::{Outcome, handle}; + let mut h = harness(Services::default()); + let first = h.state().session.active_window().unwrap(); + h.state_mut().run_or_alert("file.new", serde_json::json!({})); + h.run_steps(2); + let ctx = h.ctx.clone(); + let call = |h: &mut egui_kittest::Harness<'static, SheetApp>, method: &str, p: serde_json::Value| match handle(h.state_mut(), &ctx, method, &p) { + Outcome::Done(v) => v, + Outcome::Screenshot { .. } => serde_json::Value::Null, + }; + let r = call(&mut h, "engine.execute", serde_json::json!({"command": "cell.set", "params": {"cell": "A1", "input": "one"}, "window": first})); + assert_eq!(r["ok"], true, "{r}"); + assert_eq!(a1(&h, 0), Value::text("one")); + assert_eq!(a1(&h, 1), Value::Empty); + assert_eq!(h.state().session.active_index(), 1, "Book2 stays in front"); + let r = call(&mut h, "cell.get", serde_json::json!({"cell": "A1", "window": first})); + assert!(r["result"].to_string().contains("one"), "{r}"); + + let r = call(&mut h, "window.list", serde_json::json!({})); + let rows = r["result"].as_array().unwrap(); + assert_eq!(rows.len(), 2, "{r}"); + assert_eq!((rows[0]["window"].as_u64(), rows[0]["title"].as_str(), rows[0]["dirty"].as_bool()), (Some(first), Some("Book1"), Some(true))); + assert_eq!(rows[1]["active"].as_bool(), Some(true)); + let r = call(&mut h, "engine.execute", serde_json::json!({"command": "cell.get", "params": {"cell": "A1"}, "window": 999})); + assert_eq!(r["ok"], false, "{r}"); +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 0c24e92524..00bbc8fc58 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -23,13 +23,14 @@ connection; nothing sent after it runs. Blank lines are skipped. | Method | Params | What it does | |---|---|---| -| `engine.execute` | `{command, params}` | Runs any engine command (see `engine.commands` or `gridcraft-cli commands`). `edit.goTo`, `edit.goToSpecial`, `selection.set` and `selection.move` scroll the active cell into view, as Go To does in the window | +| `engine.execute` | `{command, params, window?, doc?}` | Runs any engine command (see `engine.commands` or `gridcraft-cli commands`) in the active window, or in the window named by `window` (a window id) or `doc` (a workbook id: its most recent window). The active window stays in front afterwards unless the command itself switches windows. `edit.goTo`, `edit.goToSpecial`, `selection.set` and `selection.move` scroll the active cell into view, as Go To does in the window | +| `window.list` | | The open windows: `[{window, doc, index, title, dirty, active, hidden}]`. `title` is the workbook name, or "Book1:1", "Book1:2"… when a workbook has several windows; `index` is the workbook's position among the open workbooks | | `engine.commands` | `{search?}` | Every command: id, label, ribbon path, shortcut, params doc, enabled; `search` keeps those whose id, label or ribbon path contains it (case-insensitive) | | `engine.journal` | | Commands run so far (replayable). Passwords (`password`, `…Password` params) are left out, so a replayed Protect Sheet has no password. Capped at 10,000 entries and 64 MB (serialized); past either cap the oldest entries go | | `document.inspect` | | Workbook summary: sheets, used ranges, tables, charts, names, selection, undo labels | | `sheet.read` | `{range?, sheet?, formulas?, formatted?}` | Values (or formulas / displayed text) of a range | | `cell.get` | `{cell?}` | One cell: value, formula, displayed text, style, merge, comment, link | -| `ui.inspect` | | UI state: ribbon tab, editor, open dialog, Backstage page, message, grid rect, scroll, perf | +| `ui.inspect` | | UI state: ribbon tab, editor, open dialog, Backstage page, message, grid rect, scroll, perf, `windows` (as `window.list`) and `activeWindow` | | `ui.click` / `ui.drag` / `ui.move` | `{x, y, toX?, toY?, button?, count?, shift?, cmd?, alt?}` | Real pointer input in window points; refused with an error if it would leave more than 10,000 pointer events queued | | `ui.cell.click` / `ui.cell.drag` | `{cell}` / `{from, to}` | Pointer input at a cell's centre (works in point mode while editing); same 10,000-event queue limit | | `ui.key` / `ui.text` | `{key, shift?, cmd?, alt?, ctrl?}` / `{text}` | Keyboard input (typing in the grid starts editing) | @@ -42,7 +43,20 @@ connection; nothing sent after it runs. Blank lines are skipped. | `ui.resize` / `ui.focus` | `{width, height}` | Window control | | `app.open` / `app.save` / `app.quit` | `{path, password?}` / `{path?}` | Files and lifetime. `password` opens a password-protected workbook; without it, such a workbook returns an error (never a dialog) | -Any other method name that is a command id (e.g. `home.bold`) runs that command. +Any other method name that is a command id (e.g. `home.bold`) runs that command. Its params may +include `window` or `doc` to run it in that window, as with `engine.execute`; the window commands +(`window.*`, `view.newWindow`, `view.hideWindow`, `view.unhideWindow`, `view.switchWindows`) read +them as their own parameters. + +### Windows + +Every open workbook has one or more windows; View › New Window (`view.newWindow`) adds another +on the same workbook, and saving from any of them saves that workbook. Each window has its own +selection, active sheet and scroll position; switching windows never marks the workbook as +changed. `window.activate {window}`, `view.switchWindows {window}` and `view.unhideWindow +{window}` bring a window forward; `window.close {window?, force?}` closes one (the workbook +closes with its last window, asking about unsaved changes unless `force`); `file.close` closes +the active window, or with `{document: true}` the workbook and all its windows. Use `engine.execute` with `{command: "view.theme", params: {mode: "system"}}` to choose the display theme; `mode` accepts `"system"`, `"light"`, or `"dark"`. diff --git a/docs/mcp.md b/docs/mcp.md index 1877cf2289..8c45182c94 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -41,6 +41,7 @@ with a file open. |---|---|---| | `execute_command` | `command`, `params?` | Runs **any** engine command (`cell.set`, `home.bold`, `data.removeDuplicates`, …). | | `list_commands` | `search?` | Command ids, labels, ribbon paths, shortcuts and parameter docs. | +| `list_windows` | — | Open windows: window id, workbook (`doc`) id, title ("Book1", or "Book1:2" for a workbook's second window), unsaved changes, active, hidden. | | `inspect_workbook` | — | Sheets, used ranges, tables, charts, names, selection, history. | | `read_range` | `range?`, `sheet?`, `formulas?`, `formatted?` | Rows of values (default: the used range). | | `write_range` | `range`, `values` (2-D) | `range.setValues`; strings starting with `=` are formulas. | @@ -57,6 +58,10 @@ with a file open. | `screenshot` | `path` | PNG of the window (Remote only). | | `list_functions` | `search?`, `category?` | Worksheet functions with signatures. | +Every tool that acts on a workbook (all but `list_commands`, `list_functions`, `list_windows`, `open_workbook`, +`new_workbook` and `screenshot`) also takes `window` or `doc`, an id from `list_windows`, and acts in that window +(for `doc`, the workbook's most recent window) without bringing it forward. Without them it acts in the active window. + Results are one text block holding pretty JSON; failures set `isError: true` with a readable message. Unknown tools, missing required arguments and wrong argument types are JSON-RPC `-32602` errors; malformed JSON is `-32700`; unknown methods `-32601`. diff --git a/docs/ui-parity.md b/docs/ui-parity.md index b70682ac3d..839984959d 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Switch Windows lists the open workbooks; a cell being typed stays in its own workbook through New, Open and window switches; macOS Quit asks about unsaved workbooks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (New Window opens a second window on the same workbook, titled Name:1 / Name:2; Switch Windows lists the open windows; a cell being typed stays in its own window through New, Open and window switches; macOS Quit asks about unsaved workbooks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -34,7 +34,7 @@ Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes | Dark mode, system appearance | yes | yes | done (10-09) | | Page Layout view (paper view with rulers and header editing) | yes | yes (#329, 10-11): per-sheet view saved in XLSX, pages with margins and gaps on the print pagination, "Click to add data" pages, rulers (inches or centimetres), headers and footers drawn and edited in place with a Header & Footer tab | margins drag on the rulers (#792, with header and footer margins); header/footer pictures, Different First Page / Odd & Even missing (1–3 h) | | Multiple windows per workbook | yes | no (#162) | see [hardware-parity.md](hardware-parity.md) | -| Switch Windows, unsaved workbooks on quit | yes | yes (10-22): View › Switch Windows and the macOS Window menu list the open workbooks (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {doc}`); a cell being typed is entered into its own workbook before New, Open, Close, Save, Print and window switches, and a refused entry stays open and stops the command; quitting (window close, Cmd+Q, the macOS Quit item) asks Save / Don't Save / Cancel for each unsaved workbook | one OS window for all workbooks (#162) | +| Switch Windows, New Window, unsaved workbooks on quit | yes | yes (10-22): View › New Window opens another window on the same workbook (no copy: saving from either saves it), each with its own selection, active sheet and scroll; a workbook with several windows titles them "Book1:1", "Book1:2"; File › Close closes one of them without asking and asks only for the last; View › Switch Windows and the macOS Window menu list the open windows (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {window}`, or `{doc}` for a workbook's most recent window); a cell being typed is entered into its own window and workbook before New, Open, Close, Save, Print and window switches (also between two windows on one workbook), and a refused entry stays open and stops the command; quitting (window close, Cmd+Q, the macOS Quit item) asks Save / Don't Save / Cancel for each unsaved workbook | one OS window for all workbooks (#162) | ## Grid interaction @@ -177,6 +177,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | New Window opens a second window on the same workbook instead of a copy that saved over the original; windows keep their own selection, sheet and scroll; titles Name:1 / Name:2; Switch Windows, Hide and Unhide work by window; File › Close closes one window of several without asking | | 2026-10-22 | minor | New workbook no longer loses the first one: the cell being typed is entered into its own workbook before any File or window command (an entry the workbook refuses stays open and the command doesn't run; an untouched F2 draft changes nothing), the editor is tied to its workbook, View › Switch Windows and the macOS Window menu list the open workbooks, and the macOS Quit item asks about unsaved workbooks like the close button | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | From 5bd5d241a0825a4af8551cc28c6c97b9b31f2d66 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 16:21:59 +0000 Subject: [PATCH 083/687] G3: a native window per workbook window Each engine window is drawn in its own native window through craft-window's immediate-viewport driver (desktop only; the web app keeps one window). The root viewport shows one window and adopts another when its window closes. - WindowUi: the cell draft, text-box draft, dialog, Backstage, message, toast, notice, grid state, name box, key tips, selected chart, Tab run and edit mode are per window. SheetApp's fields hold the active window's; the others are parked and swapped in when a window is drawn or activated, and after any command that switched windows. - Session::show_window makes a window current for drawing without counting as an activation. - The ribbon in each window acts on it; menu bar commands and scripts go to the window focused last. Leaving a window enters its draft as Enter would. - A window's close button runs window.close: one of several windows on a workbook closes without asking, a workbook's last asks when unsaved. SheetApp::request_quit (macOS Quit item, Dock quit) asks about each unsaved workbook in its window; Cancel stops. Save Changes answers moved to SheetApp::answer_save_changes. - The last window closed quits on Windows and Linux; macOS hides the root and keeps running until New or Open. - Fonts are bound for every open workbook, since each may be showing. craft-window is pinned to the aiongg/craft-libs window-list branch until it merges into storytold/craft-libs. --- Cargo.lock | 10 + Cargo.toml | 4 + ROADMAP.md | 4 +- apps/gridcraft/src/apple_events.rs | 3 +- apps/gridcraft/src/main.rs | 20 +- apps/gridcraft/src/native_menu.rs | 14 +- crates/engine/src/windows.rs | 20 +- crates/ui-egui/Cargo.toml | 1 + crates/ui-egui/src/dialogs.rs | 25 +-- crates/ui-egui/src/doc_windows.rs | 227 ++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 148 +++++++++++++-- crates/ui-egui/src/window_ui.rs | 133 +++++++++++++ crates/ui-egui/tests/doc_windows.rs | 284 ++++++++++++++++++++++++++++ docs/hardware-parity.md | 5 +- docs/ui-parity.md | 7 +- 15 files changed, 840 insertions(+), 65 deletions(-) create mode 100644 crates/ui-egui/src/doc_windows.rs create mode 100644 crates/ui-egui/src/window_ui.rs create mode 100644 crates/ui-egui/tests/doc_windows.rs diff --git a/Cargo.lock b/Cargo.lock index e041aafbc8..77b3b801a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -758,6 +758,15 @@ dependencies = [ "egui", ] +[[package]] +name = "craft-window" +version = "0.1.0" +source = "git+https://github.com/aiongg/craft-libs?rev=4fbdea5fcf86ba893d9bc03685d4531cfdd4efc2#4fbdea5fcf86ba893d9bc03685d4531cfdd4efc2" +dependencies = [ + "egui", + "wayland-client", +] + [[package]] name = "crc32fast" version = "1.5.2" @@ -1636,6 +1645,7 @@ name = "gridcraft-ui-egui" version = "0.10.0" dependencies = [ "craft-ui", + "craft-window", "egui", "egui_kittest", "epaint_default_fonts", diff --git a/Cargo.toml b/Cargo.toml index e8a6dc67e6..b28d7eb33e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,6 +54,10 @@ sys-locale = "0.3" skrifa = { version = "0.44", default-features = false, features = ["std"] } # Real font bytes for tests (Ubuntu Light, compiled into epaint). epaint_default_fonts = "0.36" +# One native window per workbook: the window list, Window menu titles and the immediate-viewport +# driver (craft-libs). Pinned to the aiongg fork's `window-list` branch; repins to +# storytold/craft-libs once that branch merges there. +craft-window = { version = "0.1.0", git = "https://github.com/aiongg/craft-libs", rev = "4fbdea5fcf86ba893d9bc03685d4531cfdd4efc2", default-features = false, features = ["egui"] } [workspace.lints.rust] unsafe_code = "forbid" diff --git a/ROADMAP.md b/ROADMAP.md index bf25cc3a99..38c394fb06 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save; New Window shares the workbook) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (a native window per workbook window; no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save; New Window shares the workbook) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -135,6 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-22 | On the desktop every workbook window is a native window of its own: New, Open and New Window open one, each keeps its own cell draft, dialog and ribbon, and the macOS menu bar acts on the window focused last. Closing one of several windows on a workbook doesn't ask; closing a workbook's last window, or quitting, asks about unsaved changes in that window. The last window closed quits on Windows and Linux; macOS keeps running with the menu bar. First craft-libs `craft-window` use (window list and viewport driver) | | 2026-10-22 | View › New Window opens a second window on the same workbook (it used to open a copy whose Save overwrote the original): each window has its own selection, active sheet and scroll, titles read "Book1:1" / "Book1:2", and Switch Windows, Hide, Unhide and Close act per window. The control channel, CLI `send` and MCP tools take an optional `window` or `doc` id and default to the active window; `window.list` (MCP `list_windows`) lists the windows | | 2026-10-22 | Typing in a cell and then choosing File › New no longer strands the first workbook: the entry is committed into its own workbook before any File or window command (a refused entry stays open and stops the command), View › Switch Windows and the macOS Window menu list the open workbooks with unsaved markers, `view.switchWindows {doc}` switches by a stable workbook id, and the macOS Quit item asks about unsaved workbooks | | 2026-10-21 | Page Setup paper sizes match Japanese Excel: `B4`/`B5`/`B6` are the JIS sizes (ISO B4/B5/B6 stay reachable by name), so a Japanese B5 worksheet prints at 182×257 mm; Japanese Postcard (100×148 mm), Double Postcard (148×200 mm), Envelope #3/#4 (120×235/90×205 mm), Kaku #2/#3 (240×332/216×277 mm) and A6 are added, both sizes reach the PDF page (`file.exportPdf`/`file.print` MediaBox) and the size survives an XLSX round-trip; the Page Layout Size menu lists them. `B4`/`B5` used to be the ISO sizes, which is changed here | @@ -222,6 +223,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Progress log: a native window per workbook window | | 2026-10-22 | minor | Progress log: New Window shares the workbook; window ids for scripts and MCP | | 2026-10-22 | minor | Progress log: drafts stay in their workbook, Switch Windows, macOS Quit asks to save | | 2026-10-21 | minor | Page Setup paper sizes: JIS B4/B5/B6 (ISO by name), Japanese postcards and envelopes, A6; PDF page size follows, XLSX round-trips | diff --git a/apps/gridcraft/src/apple_events.rs b/apps/gridcraft/src/apple_events.rs index 405ee89a04..4d596f9571 100644 --- a/apps/gridcraft/src/apple_events.rs +++ b/apps/gridcraft/src/apple_events.rs @@ -43,7 +43,8 @@ pub fn poll(inbox: &Inbox, app: &mut SheetApp, ctx: &egui::Context) { app.open_path(p); } } - Event::Quit => ctx.send_viewport_cmd(egui::ViewportCommand::Close), + // Quit from the Dock or the system: each unsaved workbook is asked about in its window. + Event::Quit => app.request_quit(ctx), } } } diff --git a/apps/gridcraft/src/main.rs b/apps/gridcraft/src/main.rs index 8aad9aeb8f..c0b50038dc 100644 --- a/apps/gridcraft/src/main.rs +++ b/apps/gridcraft/src/main.rs @@ -22,24 +22,27 @@ use std::cell::RefCell; use std::rc::Rc; use gridcraft_engine::Session; +use gridcraft_ui_egui::doc_windows::DocWindows; use gridcraft_ui_egui::{Services, SheetApp}; use serde_json::json; -struct App(SheetApp, #[cfg(target_os = "macos")] Option, #[cfg(target_os = "macos")] fmv_macos_events::Inbox); +/// The app, its native window per workbook window, and (macOS) the menu bar and Finder events. +struct App(SheetApp, DocWindows, #[cfg(target_os = "macos")] Option, #[cfg(target_os = "macos")] fmv_macos_events::Inbox); impl eframe::App for App { fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { #[cfg(target_os = "macos")] { - if self.1.is_none() && std::env::var_os("GRIDCRAFT_NO_NATIVE_MENU").is_none() { - self.1 = Some(native_menu::NativeMenu::install(ctx)); + if self.2.is_none() && std::env::var_os("GRIDCRAFT_NO_NATIVE_MENU").is_none() { + self.2 = Some(native_menu::NativeMenu::install(ctx)); } - if let Some(m) = &mut self.1 { - m.poll(&mut self.0, ctx); + if let Some(m) = &mut self.2 { + m.poll(&mut self.0, ctx, self.1.focused_viewport()); } - apple_events::poll(&self.2, &mut self.0, ctx); + apple_events::poll(&self.3, &mut self.0, ctx); } self.0.logic(ctx); + self.1.logic(&mut self.0, ctx); self.0.poll_printing(ctx); if ctx.input(|input| input.viewport().close_requested()) && self.0.printing.busy() { ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); @@ -55,7 +58,7 @@ impl eframe::App for App { } } fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { - self.0.ui(ui); + self.1.show(&mut self.0, ui); } fn on_exit(&mut self) { print::finish_pending_jobs(); @@ -320,6 +323,7 @@ fn main() -> eframe::Result<()> { if let Some(i) = icon() { viewport = viewport.with_icon(i); } + let window_builder = viewport.clone(); let options = eframe::NativeOptions { viewport, ..Default::default() }; // Before eframe creates the wgpu instance: Windows starts on DirectX 12, or OpenGL when // DirectX 12 has no GPU driver, and never touches Vulkan unless asked (see graphics.rs). @@ -357,8 +361,10 @@ fn main() -> eframe::Result<()> { Err(e) => log::error!("control channel on port {port}: {e}"), } } + let windows = DocWindows::new(&mut app, window_builder.clone(), DocWindows::platform_quits_with_last_window()); Ok(Box::new(App( app, + windows, #[cfg(target_os = "macos")] None, #[cfg(target_os = "macos")] diff --git a/apps/gridcraft/src/native_menu.rs b/apps/gridcraft/src/native_menu.rs index 826a12b311..9559c47d73 100644 --- a/apps/gridcraft/src/native_menu.rs +++ b/apps/gridcraft/src/native_menu.rs @@ -203,8 +203,8 @@ pub struct NativeMenu { /// Menu id of the Window menu item that brings workbook `doc` forward. const WINDOW_ITEM: &str = "window:id:"; -/// The Quit item: closing the window asks about unsaved workbooks first, which AppKit's own -/// Quit (terminate:) would skip. +/// The Quit item: asks about each unsaved workbook in its window first +/// (`SheetApp::request_quit`), which AppKit's own Quit (terminate:) would skip. const QUIT: &str = "app.quit"; fn select_theme(app: &mut SheetApp, id: &str) -> bool { @@ -311,11 +311,13 @@ impl NativeMenu { } } - pub fn poll(&mut self, app: &mut SheetApp, ctx: &egui::Context) { + /// Runs the chosen menu items. Commands go to the active window, the most recently focused; + /// `viewport` is its egui viewport, for the items that act on its input. + pub fn poll(&mut self, app: &mut SheetApp, ctx: &egui::Context, viewport: egui::ViewportId) { while let Ok(ev) = self.events.try_recv() { let id = ev.id.as_ref().to_string(); if id == QUIT { - ctx.send_viewport_cmd(egui::ViewportCommand::Close); + app.request_quit(ctx); continue; } if let Some(window) = id.strip_prefix(WINDOW_ITEM).and_then(|w| w.parse::().ok()) { @@ -331,11 +333,11 @@ impl NativeMenu { _ => None, }; if let Some(command) = clipboard { - ctx.send_viewport_cmd(command); + ctx.send_viewport_cmd_to(viewport, command); continue; } if id == "edit.undo" || id == "edit.redo" { - ctx.input_mut(|i| { + ctx.input_mut_for(viewport, |i| { i.events.push(egui::Event::Key { key: egui::Key::Z, physical_key: None, diff --git a/crates/engine/src/windows.rs b/crates/engine/src/windows.rs index 8dcb920113..4441166db1 100644 --- a/crates/engine/src/windows.rs +++ b/crates/engine/src/windows.rs @@ -166,6 +166,16 @@ impl Session { /// Makes a window active, swapping its view into its workbook. Unknown ids are ignored. pub fn activate_window(&mut self, id: u64) { + self.enter_window(id, true); + } + + /// Makes a window the one commands act on while the interface draws it, without counting as + /// an activation: the most recently active window of each workbook stays the same. + pub fn show_window(&mut self, id: u64) { + self.enter_window(id, false); + } + + fn enter_window(&mut self, id: u64, recent: bool) { let Some(wi) = self.windows.iter().position(|w| w.id == id) else { return }; let Some(w) = self.windows.get(wi) else { return }; let doc = w.doc_uid; @@ -185,10 +195,12 @@ impl Session { } } } - self.activation = self.activation.saturating_add(1); - let stamp = self.activation; - if let Some(w) = self.windows.get_mut(wi) { - w.last_active = stamp; + if recent { + self.activation = self.activation.saturating_add(1); + let stamp = self.activation; + if let Some(w) = self.windows.get_mut(wi) { + w.last_active = stamp; + } } self.active_window = Some(id); if let Some(di) = self.document_index(doc) { diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 85a24dda97..626d5e7fa2 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -33,6 +33,7 @@ gridcraft-xlsx = { workspace = true, features = ["test-support"] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # The desktop's preferred language for the first-run interface language. sys-locale = { workspace = true } +craft-window = { workspace = true } [target.'cfg(target_arch = "wasm32")'.dependencies] # Read the browser's preferred language for the first-run web interface language. diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index be474d37f2..55c0819df3 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1323,29 +1323,14 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) { } "saveChanges" => { ui.label(format!("Do you want to save the changes you made to {}?", d.values.get("title").and_then(Json::as_str).unwrap_or("this workbook"))); - // Asked while quitting: once answered, the next unsaved workbook is asked about. let quitting = d.values.get("quit").and_then(Json::as_bool) == Some(true); ui.horizontal(|ui| { - if ui.button("Don't Save").clicked() { - app.run_or_alert("file.close", json!({"force": true, "document": true})); - if quitting { - app.continue_quit(ui.ctx()); + for (label, answer) in + [("Don't Save", crate::SaveAnswer::DontSave), ("Cancel", crate::SaveAnswer::Cancel), ("Save", crate::SaveAnswer::Save)] + { + if ui.button(label).clicked() && app.answer_save_changes(answer, quitting, ui.ctx()) { + open = false; } - open = false; - } - if ui.button("Cancel").clicked() { - open = false; - } - // File operations must include the cell the user is still editing (as the File > Save menu item already does). - if ui.button("Save").clicked() && app.commit_edit(0, 0, false, false) { - app.run_or_alert("file.save", json!({})); - if app.session.active().is_some_and(|x| !x.is_dirty()) { - app.run_or_alert("file.close", json!({"force": true, "document": true})); - if quitting { - app.continue_quit(ui.ctx()); - } - } - open = false; } }); } diff --git a/crates/ui-egui/src/doc_windows.rs b/crates/ui-egui/src/doc_windows.rs new file mode 100644 index 0000000000..88ba3967a9 --- /dev/null +++ b/crates/ui-egui/src/doc_windows.rs @@ -0,0 +1,227 @@ +//! Desktop: each workbook window is a native window of its own. +//! +//! The engine owns the windows ([`Session::windows`]); [`DocWindows`] mirrors the visible ones in +//! a [`WindowList`] and draws each through [`Viewports`]: the root viewport shows one window and +//! every other window is an immediate viewport. Before a window draws, it becomes the engine's +//! window and its interface state is swapped in ([`SheetApp::show_window`]); after the frame the +//! focused window is active again, so menu commands, scripts and shortcuts outside a window's own +//! input go to the window the user worked in last. +//! +//! Close and quit: +//! - A window's close button closes that window (`window.close`): one of several windows on a +//! workbook closes without asking; a workbook's last window asks Save / Don't Save / Cancel when +//! it has unsaved changes. +//! - Quit ([`SheetApp::request_quit`]) asks about each unsaved workbook in its window; Cancel stops. +//! - When the root's window closes while others are open, another window moves into the root. +//! - With no window left, the app quits, or (macOS) keeps running with its root hidden and the +//! menu bar showing until a workbook is created or opened. +//! +//! [`Session::windows`]: gridcraft_engine::Session::windows + +use craft_window::{Frame, Viewports, WindowEvent, WindowId, WindowList}; +use serde_json::json; + +use crate::SheetApp; + +/// The native windows (see the module docs). +pub struct DocWindows { + /// Engine window id per native window. + list: WindowList, + viewports: Viewports, + /// The builder new windows start from: the root window's size, icon and frame settings. + base: egui::ViewportBuilder, + /// Quit when the last window closes (Windows, Linux); macOS keeps running without a window. + quit_with_last_window: bool, + /// The root viewport is hidden because no window is open. + root_hidden: bool, +} + +impl DocWindows { + /// Takes over `app`'s windows. `base` is the root window's builder, used for new windows. + pub fn new(app: &mut SheetApp, base: egui::ViewportBuilder, quit_with_last_window: bool) -> DocWindows { + app.native_windows = true; + // The root window keeps the desktop's frame, so every window does. + DocWindows { list: WindowList::new(), viewports: Viewports::new(Frame::Desktop), base, quit_with_last_window, root_hidden: false } + } + + /// The platform default for the last window closing: macOS keeps running, others quit. + pub fn platform_quits_with_last_window() -> bool { + !cfg!(target_os = "macos") + } + + /// The native window showing engine window `window`. + fn find(&self, window: u64) -> Option { + self.list.find(|w| *w == window) + } + + /// The engine window drawn in the root viewport. + pub fn root_window(&self) -> Option { + self.viewports.root().and_then(|w| self.list.get(w).copied()) + } + + /// The engine windows that have a native window, in opening order. + pub fn shown_windows(&self) -> Vec { + self.list.iter().map(|(_, w)| *w).collect() + } + + /// The egui viewport showing engine window `window`. + pub fn viewport_of(&self, window: u64) -> Option { + self.find(window).map(|id| self.viewports.viewport_id(id)) + } + + /// The egui viewport of the focused window, where menu commands that act on input (copy, + /// paste, undo in a text box) go. + pub fn focused_viewport(&self) -> egui::ViewportId { + self.list.active().map(|w| self.viewports.viewport_id(w)).unwrap_or(egui::ViewportId::ROOT) + } + + /// Call each frame after [`SheetApp::logic`], also while the root is hidden (eframe runs only + /// the logic then): shows windows created or opened by menu commands, files and scripts. + pub fn logic(&mut self, app: &mut SheetApp, ctx: &egui::Context) { + self.sync(app, ctx); + } + + /// Mirrors the engine's visible windows: closed or hidden windows lose their native window + /// (the root adopting another), new ones get one, and a window a command activated comes to + /// the front. With no window left, quits or hides the root. + fn sync(&mut self, app: &mut SheetApp, ctx: &egui::Context) { + let visible = visible_windows(app); + let gone: Vec = self.list.iter().filter(|(_, w)| !visible.contains(w)).map(|(id, _)| id).collect(); + let mut root_closed = false; + for id in gone { + root_closed |= self.viewports.root() == Some(id); + self.list.close(id); + } + if root_closed { + // The window the engine brought forward, else the most recently focused. + let next = app.session.active_window().and_then(|w| self.find(w)).or_else(|| self.list.active()); + match next { + Some(id) => self.viewports.adopt(ctx, &self.list, id), + None => self.viewports.set_root(None), + } + } + for w in visible { + if self.find(w).is_none() { + self.list.open(w); + } + } + if self.viewports.root().is_none() { + let first = app.session.active_window().and_then(|w| self.find(w)).or_else(|| self.list.active()); + self.viewports.set_root(first); + } + if self.list.is_empty() { + if self.quit_with_last_window { + if !app.allow_close { + app.allow_close = true; + ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Close); + } + } else if !self.root_hidden { + self.root_hidden = true; + ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Visible(false)); + } + return; + } + if self.root_hidden { + self.root_hidden = false; + ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Visible(true)); + ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Focus); + } + // A command or script activated another window: bring its native window forward. + if let Some(id) = app.session.active_window().and_then(|w| self.find(w)) + && self.list.active() != Some(id) + { + self.list.focus(id); + self.viewports.focus(ctx, id); + } + } + + /// Draws every window. Call from the root viewport's UI each frame, in place of + /// [`SheetApp::ui`]. + pub fn show(&mut self, app: &mut SheetApp, ui: &mut egui::Ui) { + let ctx = ui.ctx().clone(); + self.sync(app, &ctx); + let focused = self.list.active().and_then(|id| self.list.get(id).copied()); + app.focus_window = focused; + let titles: std::collections::HashMap = self.list.iter().map(|(_, w)| (*w, window_title(app, *w))).collect(); + let base = &self.base; + // The window a command run in some window's UI activated (File › New, Switch Windows, a close). + let mut requested: Option = None; + let events = self.viewports.show( + ui, + &mut self.list, + |_, w| base.clone().with_title(titles.get(w).cloned().unwrap_or_default()), + |ui, _, w| { + app.show_window(*w); + // Files dropped on a window open as workbooks (the root's arrive through the app's + // raw input hook; a child window's input reaches only its own pass). + let dropped: Vec = ui + .ctx() + .input(|i| i.raw.dropped_files.iter().filter_map(|f| f.path().to_str().map(str::to_string)).filter(|p| !p.is_empty()).collect()); + for path in dropped { + app.open_path(&path); + } + app.ui(ui); + if app.session.active_window() != Some(*w) { + requested = app.session.active_window(); + } + }, + ); + for event in events { + match event { + WindowEvent::Focused(_) => {} + WindowEvent::CloseRequested(id) => { + if let Some(w) = self.close_requested(app, &ctx, id) { + requested = Some(w); + } + } + } + } + let target = requested.or_else(|| self.list.active().and_then(|id| self.list.get(id).copied())); + if let Some(target) = target.filter(|w| app.session.window(*w).is_some()) { + if requested.is_none() + && let Some(previous) = focused.filter(|p| *p != target && app.session.window(*p).is_some()) + { + // The user moved to another window: the previous window's draft is entered there, + // as Enter would. One its workbook refuses stays open in that window. + app.enter_window(previous); + let _ = app.settle_drafts(); + } + app.enter_window(target); + } + self.sync(app, &ctx); + app.focus_window = self.list.active().and_then(|id| self.list.get(id).copied()); + } + + /// A window's close button. Returns the window to bring forward when the close needs an + /// answer (or moved focus); `None` when nothing changed. + fn close_requested(&mut self, app: &mut SheetApp, ctx: &egui::Context, id: WindowId) -> Option { + let is_root = self.viewports.root() == Some(id); + if app.allow_close { + // Quitting: every workbook has been answered for; the root's close ends the app. + return None; + } + if is_root { + // eframe would end the app with the root window; closing it is the app's decision. + ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::CancelClose); + } + let window = *self.list.get(id)?; + app.enter_window(window); + app.run_or_alert("window.close", json!({"window": window})); + app.session.active_window() + } +} + +/// The windows that get a native window: every window not hidden (View › Hide), or the active one +/// when all are hidden, so the app still has a window to unhide them from. +fn visible_windows(app: &SheetApp) -> Vec { + let shown: Vec = app.session.windows().iter().filter(|w| !w.hidden).map(|w| w.id).collect(); + if shown.is_empty() { app.session.active_window().into_iter().collect() } else { shown } +} + +/// A native window's title: the window's ("Book1", "Book1:2"), marked while its workbook has +/// unsaved changes. +fn window_title(app: &SheetApp, window: u64) -> String { + let title = app.session.window_title(window).unwrap_or_default(); + let dirty = app.session.window(window).and_then(|w| app.session.documents().iter().find(|d| d.uid == w.doc_uid)).is_some_and(|d| d.is_dirty()); + if dirty { format!("{title} — Edited") } else { title } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d330d1fcf8..f405a3d185 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -13,6 +13,8 @@ pub mod chartview; pub mod control; pub mod credits; pub mod dialogs; +#[cfg(not(target_arch = "wasm32"))] +pub mod doc_windows; pub mod editor; mod floating_pictures; pub mod fonts; @@ -34,6 +36,9 @@ pub mod tabs; pub mod text_box; pub mod theme; pub mod widgets; +mod window_ui; + +pub use window_ui::WindowUi; use std::collections::HashMap; @@ -166,6 +171,14 @@ pub struct Notice { pub action: Option<(String, String)>, } +/// An answer to the Save Changes question. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SaveAnswer { + Save, + DontSave, + Cancel, +} + /// One open window, as View › Switch Windows and the macOS Window menu list it. #[derive(Clone, Debug, PartialEq, Eq)] pub struct OpenWindow { @@ -282,8 +295,8 @@ pub struct SheetApp { fonts_set: bool, /// Workbook fonts bound into the current font set; a change rebinds (installed fonts only). fonts_bound: Vec, - /// The (doc uid, revision) `fonts_bound` was computed for, so the style table is walked only - /// when the workbook changes. + /// The open workbooks' (uid, revision), folded, that `fonts_bound` was computed for, so the + /// style tables are walked only when a workbook changes. fonts_seen: (u64, u64), /// Set while a font-set change is pending activation: skip this frame's paint, like the /// startup `fonts_ready` gate (egui applies new definitions at the next pass). @@ -320,6 +333,17 @@ pub struct SheetApp { /// View › Switch Windows opens its list on the next frame the View tab draws (asked for by a /// key tip, the command search or a menu, possibly while another tab was showing). pub(crate) open_switch_windows: bool, + /// The window whose interface state the per-window fields above hold (see [`WindowUi`]). + ui_window: Option, + /// The other windows' interface state, by window id. + parked: HashMap, + /// Each window is a native window of its own ([`doc_windows`] on the desktop): a window's + /// close button closes that window, and quitting is asked for separately. Without it (web, + /// tests, embedding hosts) one window shows the active workbook window, and its close button + /// quits. + pub native_windows: bool, + /// The native window that has focus, which menu commands and screenshots go to. + pub(crate) focus_window: Option, } impl SheetApp { @@ -367,6 +391,10 @@ impl SheetApp { started: now_ms(), allow_close: false, open_switch_windows: false, + ui_window: None, + parked: HashMap::new(), + native_windows: false, + focus_window: None, } } @@ -385,11 +413,12 @@ impl SheetApp { theme::apply(ctx, dark); } - /// The font names the active workbook uses: every style's font plus the theme's heading/body - /// fonts. These are the families [`fonts`] binds an installed file for. + /// The font names the open workbooks use: every style's font plus the theme's heading/body + /// fonts. These are the families [`fonts`] binds an installed file for. Every workbook's, as + /// each may be showing in a window of its own. pub fn used_fonts(&self) -> Vec { let mut names: Vec = Vec::new(); - if let Some(d) = self.session.active() { + for d in self.session.documents() { names.extend(d.wb.styles.iter().map(|(_, s)| s.font.name.clone())); names.push(d.wb.theme.major_font.clone()); names.push(d.wb.theme.minor_font.clone()); @@ -441,7 +470,14 @@ impl SheetApp { if let Some(r) = self.run_ui_command(id, ¶ms) { return r; } + let before = self.session.active_window(); let r = self.session.run(id, params); + if id.starts_with("file.") && self.session.active_window() != before { + // File › New or Open from the Backstage view: the window it was in goes back to its sheet. + self.backstage = None; + } + // Requests the command made (a dialog, a draft) belong to the window it left active. + self.follow_active_window(); self.after_engine(); // The new chart is selected, as after inserting any object, so Delete removes it, not its data. if id == "insert.chart" @@ -1065,7 +1101,7 @@ impl SheetApp { /// Commits the cell and text-box drafts, as Enter would, before a command that can show /// another workbook or write this one to a file, so a draft never lands in another workbook. /// A draft its workbook refuses stays open with the reason shown, and the command doesn't run. - fn settle_drafts(&mut self) -> Result<(), String> { + pub(crate) fn settle_drafts(&mut self) -> Result<(), String> { if !self.commit_edit(0, 0, false, false) { return Err(self.message.as_ref().map(|(_, m)| m.clone()).unwrap_or_else(|| "The cell being edited can't be entered.".into())); } @@ -1216,7 +1252,7 @@ impl SheetApp { /// Holds back a window close (the close button, Alt+F4, the OS quitting the app) while a /// workbook has unsaved changes, and asks about them first. fn guard_window_close(&mut self, ctx: &egui::Context) { - if self.allow_close || !ctx.input(|i| i.viewport().close_requested()) { + if self.native_windows || self.allow_close || !ctx.input(|i| i.viewport().close_requested()) { return; } // A cell still being edited is part of the workbook, as for File › Close. @@ -1237,21 +1273,75 @@ impl SheetApp { } } - /// Asks about the next unsaved workbook before quitting, or closes the window once none is left. + /// Asks about the next unsaved workbook before quitting, in its window, or closes the app + /// once none is left. pub(crate) fn continue_quit(&mut self, ctx: &egui::Context) { - match self.session.documents().iter().position(|d| d.is_dirty()) { - Some(i) => { - self.session.set_active(i); + let dirty = self.session.documents().iter().find(|d| d.is_dirty()).map(|d| d.uid); + match dirty.and_then(|uid| self.session.doc_window(uid)) { + Some(window) => { + // Its window comes forward (the native window too: see `doc_windows`). + self.enter_window(window); let title = self.session.active().map(|d| d.display_title()).unwrap_or_default(); self.open_dialog("saveChanges", json!({"title": title, "quit": true})); } None => { self.allow_close = true; - ctx.send_viewport_cmd(egui::ViewportCommand::Close); + ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::Close); } } } + /// Answers the Save Changes question about the active workbook (its buttons). While quitting, + /// the next unsaved workbook is asked about once this one is answered; Cancel stops the quit. + /// Returns whether the question is settled: Save keeps it open when the cell being edited + /// can't be entered. + pub fn answer_save_changes(&mut self, answer: SaveAnswer, quitting: bool, ctx: &egui::Context) -> bool { + match answer { + SaveAnswer::Cancel => {} + SaveAnswer::DontSave => { + self.run_or_alert("file.close", json!({"force": true, "document": true})); + if quitting { + self.continue_quit(ctx); + } + } + SaveAnswer::Save => { + // File operations include the cell still being edited, as File › Save does. + if !self.commit_edit(0, 0, false, false) { + return false; + } + self.run_or_alert("file.save", json!({})); + if self.session.active().is_some_and(|x| !x.is_dirty()) { + self.run_or_alert("file.close", json!({"force": true, "document": true})); + if quitting { + self.continue_quit(ctx); + } + } + } + } + true + } + + /// Quit (the macOS app menu, ⌘Q): commits every window's cell draft as Enter would, then asks + /// about each unsaved workbook in its window; Cancel on any of them stops the quit. A draft its + /// workbook refuses stops the quit too, with its window in front and the reason shown. + pub fn request_quit(&mut self, ctx: &egui::Context) { + while let Some(window) = self.parked_window_with_draft() { + self.enter_window(window); + if self.settle_drafts().is_err() { + return; + } + } + if self.settle_drafts().is_err() { + return; + } + if let Some(d) = self.dialog.as_mut().filter(|d| d.name() == "saveChanges") { + // Already asking about a workbook (File › Close): answering it goes on to quit. + d.values.insert("quit".into(), json!(true)); + return; + } + self.continue_quit(ctx); + } + /// Per-frame logic (control channel, screenshots). Call before `ui`. pub fn logic(&mut self, ctx: &egui::Context) { if self.session.text_width.is_none() { @@ -1288,7 +1378,11 @@ impl SheetApp { // Bind the workbook's installed fonts once the background scan has finished. Adding // fonts re-applies the font set, which egui activates at the next pass, so skip this // frame's paint (same one-frame cost as startup). Only when the workbook changed. - let seen = self.session.active().map(|d| (d.uid, d.revision)).unwrap_or((0, 0)); + let seen = self + .session + .documents() + .iter() + .fold((0u64, 0u64), |(uid, rev), d| (uid.wrapping_mul(31).wrapping_add(d.uid), rev.wrapping_mul(31).wrapping_add(d.revision))); if seen != self.fonts_seen { let used = self.used_fonts(); self.fonts_seen = seen; @@ -1322,9 +1416,10 @@ impl SheetApp { ctx.options_mut(|o| o.zoom_with_keyboard = false); } // egui closes the window on Ctrl+Q (and with Shift or Alt held too) without asking to save - // (#1443). Excel for Windows has no such key (Ctrl+Q is Quick Analysis); macOS keeps Cmd+Q, - // the app's Quit there. - if ctx.os() != egui::os::OperatingSystem::Mac && ctx.options(|o| !o.quit_shortcuts.is_empty()) { + // (#1443). Excel for Windows has no such key (Ctrl+Q is Quick Analysis). With a native + // window per workbook egui's Cmd+Q would close only the window it was typed in: the menu + // bar's Quit item has the key instead (it asks about unsaved workbooks first). + if (ctx.os() != egui::os::OperatingSystem::Mac || self.native_windows) && ctx.options(|o| !o.quit_shortcuts.is_empty()) { ctx.options_mut(|o| o.quit_shortcuts.clear()); } let zoom = self.ui.zoom_factor(); @@ -1357,9 +1452,12 @@ impl SheetApp { } self.after_engine(); } - if self.session.active().is_none() { + // One window always shows a workbook, unless each workbook has a native window of its own + // (`doc_windows` decides what having none left means). + if self.session.active().is_none() && !self.native_windows { self.session.new_workbook(); } + self.follow_active_window(); } pub fn raw_input_hook(&mut self, raw: &mut egui::RawInput) { @@ -1383,6 +1481,10 @@ impl SheetApp { return; } let t0 = now_ms(); + // A script may have switched windows since the last frame. + self.follow_active_window(); + // Native windows: menu commands and screenshots go to the focused one. + let focused = !self.native_windows || self.focus_window == self.ui_window; text_box::before_ui(self, &ctx); // Another window came forward without going through a command (which settles drafts // first): the draft goes into its own window instead of floating over this one. If its @@ -1428,8 +1530,10 @@ impl SheetApp { dialogs::show(self, &ctx); widgets::message_box(self, &ctx); widgets::toast(self, &ctx); - control::issue_screenshots(self, &ctx); - control::collect_screenshots(self, &ctx); + if focused { + control::issue_screenshots(self, &ctx); + control::collect_screenshots(self, &ctx); + } if !ctx.input(|i| i.focused) { // Nothing else to do. } @@ -1453,8 +1557,10 @@ impl SheetApp { self.store_view_state(); let _ = self.session.run("file.save", json!({})); } - // Window title. - if let Some(d) = self.session.active() { + // Window title (native windows get theirs from `doc_windows`). + if !self.native_windows + && let Some(d) = self.session.active() + { let title = format!("{}{}", d.display_title(), if d.is_dirty() { " — Edited" } else { "" }); if self.grid.last_title.as_deref() != Some(title.as_str()) { ctx.send_viewport_cmd(egui::ViewportCommand::Title(title.clone())); diff --git a/crates/ui-egui/src/window_ui.rs b/crates/ui-egui/src/window_ui.rs new file mode 100644 index 0000000000..96aa83c79d --- /dev/null +++ b/crates/ui-egui/src/window_ui.rs @@ -0,0 +1,133 @@ +//! The interface state each window keeps for itself. +//! +//! [`SheetApp`]'s per-window fields (the cell draft, dialog, Backstage view, grid gestures…) hold +//! the state of one window: the engine's active window. Every other window's state is parked in a +//! [`WindowUi`] and swapped in when that window is activated or drawn, the way the engine swaps a +//! window's selection and sheet into its workbook. So a draft, a dialog or a message stays in the +//! window it belongs to. + +use gridcraft_engine::core::CellRef; + +use crate::{Notice, SheetApp, backstage, dialogs, editor, grid, keytips, text_box}; + +/// One window's parked interface state (see the module docs). +#[derive(Default)] +pub struct WindowUi { + editor: Option, + text_box_editor: Option, + dialog: Option, + backstage: Option, + message: Option<(String, String)>, + toast: Option<(String, f64)>, + notice: Option, + grid: grid::GridState, + name_box: Option, + keytips: keytips::KeyTips, + selected_chart: Option, + tab_run: Option<(u64, usize, u32, CellRef)>, + open_switch_windows: bool, + /// The status bar's Ready/Enter/Edit, which follows the window's cell draft. + mode: gridcraft_engine::Mode, +} + +impl WindowUi { + /// A new window's state. The last border, fill and font colour picked carry over, as they are + /// the user's recent choices rather than the window's. + fn fresh(like: &grid::GridState) -> WindowUi { + WindowUi { + grid: grid::GridState { + last_border: like.last_border.clone(), + last_fill: like.last_fill.clone(), + last_font_color: like.last_font_color.clone(), + ..Default::default() + }, + ..Default::default() + } + } +} + +impl SheetApp { + /// Exchanges the per-window fields with `w`. Destructured in full, so a field added to + /// [`WindowUi`] can't be left out of the swap. + fn swap_window_ui(&mut self, w: &mut WindowUi) { + let WindowUi { + editor, + text_box_editor, + dialog, + backstage, + message, + toast, + notice, + grid, + name_box, + keytips, + selected_chart, + tab_run, + open_switch_windows, + mode, + } = w; + std::mem::swap(&mut self.editor, editor); + std::mem::swap(&mut self.text_box_editor, text_box_editor); + std::mem::swap(&mut self.dialog, dialog); + std::mem::swap(&mut self.backstage, backstage); + std::mem::swap(&mut self.message, message); + std::mem::swap(&mut self.toast, toast); + std::mem::swap(&mut self.notice, notice); + std::mem::swap(&mut self.grid, grid); + std::mem::swap(&mut self.name_box, name_box); + std::mem::swap(&mut self.keytips, keytips); + std::mem::swap(&mut self.selected_chart, selected_chart); + std::mem::swap(&mut self.tab_run, tab_run); + std::mem::swap(&mut self.open_switch_windows, open_switch_windows); + std::mem::swap(&mut self.session.mode, mode); + } + + /// The window whose interface state is in the per-window fields. + pub fn ui_window(&self) -> Option { + self.ui_window + } + + /// Brings the per-window fields in line with the engine's active window, after a command (or + /// a script) switched windows: the previous window's state is parked, the active one's swapped + /// in (a window never seen before starts fresh). A closed window's state is dropped. + pub fn follow_active_window(&mut self) { + let active = self.session.active_window(); + if active == self.ui_window { + return; + } + let Some(previous) = self.ui_window else { + // The first window: the fields are already its state. + self.ui_window = active; + return; + }; + let mut outgoing = WindowUi::fresh(&self.grid); + self.swap_window_ui(&mut outgoing); + if self.session.window(previous).is_some() { + self.parked.insert(previous, outgoing); + } + if let Some(mut incoming) = active.and_then(|w| self.parked.remove(&w)) { + self.swap_window_ui(&mut incoming); + } + self.ui_window = active; + let session = &self.session; + self.parked.retain(|w, _| session.window(*w).is_some()); + } + + /// Activates window `id` (it becomes the most recently active) with its interface state. + pub fn enter_window(&mut self, id: u64) { + self.session.activate_window(id); + self.follow_active_window(); + } + + /// Makes window `id` and its interface state current while it is drawn, without changing + /// which window counts as most recently active. + pub fn show_window(&mut self, id: u64) { + self.session.show_window(id); + self.follow_active_window(); + } + + /// Whether a window other than the current one has a cell or text-box draft open. + pub fn parked_window_with_draft(&self) -> Option { + self.parked.iter().filter(|(_, w)| w.editor.is_some() || w.text_box_editor.is_some()).map(|(id, _)| *id).min() + } +} diff --git a/crates/ui-egui/tests/doc_windows.rs b/crates/ui-egui/tests/doc_windows.rs new file mode 100644 index 0000000000..ff8e2d6318 --- /dev/null +++ b/crates/ui-egui/tests/doc_windows.rs @@ -0,0 +1,284 @@ +//! One native window per workbook window: each window keeps its own interface state, and closing +//! or quitting asks about unsaved workbooks only when a workbook would close. +//! +//! The windows are drawn through a test backend that, like eframe, runs each immediate viewport +//! as a nested pass with its own input, so a close button can be pressed on one window. +#![cfg(not(target_arch = "wasm32"))] + +use std::cell::RefCell; + +use egui::{Context, RawInput, ViewportCommand, ViewportEvent, ViewportId, ViewportInfo}; +use gridcraft_engine::{Session, core::CellRef, core::Value}; +use gridcraft_ui_egui::doc_windows::DocWindows; +use gridcraft_ui_egui::{SaveAnswer, SheetApp}; +use serde_json::json; + +thread_local! { + /// Each viewport's input for the next frame (focus, close button). + static INFOS: RefCell> = RefCell::new(egui::ViewportIdMap::default()); + /// The viewport commands the app sent this frame. + static SENT: RefCell> = const { RefCell::new(Vec::new()) }; +} + +fn record(mut out: egui::FullOutput) { + out.textures_delta.clear(); // not painted + SENT.with(|s| { + let mut s = s.borrow_mut(); + for (id, v) in &out.viewport_output { + s.extend(v.commands.iter().map(|c| (*id, c.clone()))); + } + }); +} + +struct Desk { + ctx: Context, + app: SheetApp, + windows: DocWindows, +} + +impl Desk { + fn new(quit_with_last_window: bool) -> Desk { + INFOS.with(|i| i.borrow_mut().clear()); + let ctx = Context::default(); + ctx.set_embed_viewports(false); + Context::set_immediate_viewport_renderer(|ctx, viewport| { + let id = viewport.ids.this; + let mut input = RawInput { viewport_id: id, ..RawInput::default() }; + input.viewports = INFOS.with(|i| i.borrow().clone()); + input.viewports.entry(id).or_default().parent = Some(viewport.ids.parent); + let mut cb = viewport.viewport_ui_cb; + record(ctx.run_ui(input, |ui| cb(ui))); + }); + let mut session = Session::new(); + session.new_workbook(); + let mut app = SheetApp::new(session, Default::default()); + let windows = DocWindows::new(&mut app, egui::ViewportBuilder::default(), quit_with_last_window); + let mut desk = Desk { ctx, app, windows }; + desk.frames(3); + desk + } + + /// One frame as eframe runs it; `act` runs first, inside the frame. Returns the viewport + /// commands sent. + fn frame_with(&mut self, act: impl FnOnce(&mut SheetApp, &Context)) -> Vec<(ViewportId, ViewportCommand)> { + SENT.with(|s| s.borrow_mut().clear()); + let mut input = RawInput { viewport_id: ViewportId::ROOT, ..RawInput::default() }; + input.viewports = INFOS.with(|i| i.borrow().clone()); + input.viewports.entry(ViewportId::ROOT).or_default(); + let Desk { ctx, app, windows } = self; + let mut act = Some(act); + let out = ctx.clone().run_ui(input, |ui| { + let ctx = ui.ctx().clone(); + if let Some(act) = act.take() { + act(app, &ctx); + } + app.logic(&ctx); + windows.logic(app, &ctx); + windows.show(app, ui); + }); + record(out); + // Close buttons are one-shot events. + INFOS.with(|i| i.borrow_mut().values_mut().for_each(|v| v.events.clear())); + SENT.with(|s| s.borrow().clone()) + } + + fn frames(&mut self, n: usize) -> Vec<(ViewportId, ViewportCommand)> { + (0..n).flat_map(|_| self.frame_with(|_, _| {})).collect() + } + + fn viewport(&self, window: u64) -> ViewportId { + self.windows.viewport_of(window).unwrap_or_else(|| panic!("window {window} has no native window")) + } + + /// The user presses window `window`'s close button. + fn close(&mut self, window: u64) -> Vec<(ViewportId, ViewportCommand)> { + let vp = self.viewport(window); + INFOS.with(|i| i.borrow_mut().entry(vp).or_default().events.push(ViewportEvent::Close)); + let mut sent = self.frame_with(|_, _| {}); + sent.extend(self.frames(2)); + sent + } + + /// Answers the Save Changes question showing in the active window, as its button does. + fn answer(&mut self, answer: SaveAnswer) -> Vec<(ViewportId, ViewportCommand)> { + let mut sent = self.frame_with(|app, ctx| { + let d = app.dialog.take().unwrap_or_else(|| panic!("no question is showing")); + assert_eq!(d.name(), "saveChanges"); + let quitting = d.values.get("quit").and_then(serde_json::Value::as_bool) == Some(true); + app.answer_save_changes(answer, quitting, ctx); + }); + sent.extend(self.frames(2)); + sent + } + + fn window_of(&self, doc: usize) -> u64 { + let uid = self.app.session.documents()[doc].uid; + self.app.session.doc_window(uid).unwrap_or_default() + } +} + +fn sent_to(sent: &[(ViewportId, ViewportCommand)], vp: ViewportId, cmd: &ViewportCommand) -> bool { + sent.iter().any(|(id, c)| *id == vp && c == cmd) +} + +#[test] +fn each_window_keeps_its_own_selection_and_draft() { + let mut desk = Desk::new(true); + desk.app.run("file.new", json!({})).unwrap(); + desk.frames(3); + let (w1, w2) = (desk.window_of(0), desk.window_of(1)); + assert_eq!(desk.windows.shown_windows(), [w1, w2], "a native window per workbook"); + assert_ne!(desk.viewport(w1), desk.viewport(w2)); + + desk.app.enter_window(w1); + desk.app.run("selection.set", json!({"range": "B2"})).unwrap(); + desk.app.begin_edit(Some("one".into()), false); + desk.frames(2); + desk.app.enter_window(w2); + desk.app.run("selection.set", json!({"range": "D4"})).unwrap(); + desk.app.begin_edit(Some("two".into()), false); + desk.frames(3); + + for (w, cell, text) in [(w1, "B2", "one"), (w2, "D4", "two")] { + desk.app.enter_window(w); + assert_eq!(desk.app.editor.as_ref().map(|ed| ed.text.as_str()), Some(text), "window {w} keeps its draft"); + assert!(desk.app.grid.rect.is_some(), "window {w} drew its own grid"); + assert_eq!(desk.app.session.doc().unwrap().selection.active, CellRef::parse(cell).unwrap(), "window {w} keeps its selection"); + } + assert!(desk.app.session.documents().iter().all(|d| !d.is_dirty()), "drawing the other window commits no draft"); +} + +#[test] +fn closing_one_of_two_windows_on_a_workbook_does_not_ask() { + let mut desk = Desk::new(true); + desk.app.run("cell.set", json!({"cell": "A1", "input": "42"})).unwrap(); + desk.app.run("view.newWindow", json!({})).unwrap(); + desk.frames(3); + let windows = desk.windows.shown_windows(); + assert_eq!(windows.len(), 2, "New Window opens a native window"); + let child = windows.iter().copied().find(|w| Some(*w) != desk.windows.root_window()).unwrap(); + + let sent = desk.close(child); + assert!(desk.app.dialog.is_none(), "the workbook stays open in its other window: nothing to ask"); + assert_eq!(desk.app.session.documents().len(), 1); + assert_eq!(desk.app.session.windows().len(), 1); + assert!(desk.app.session.documents()[0].is_dirty(), "its changes are kept"); + assert_eq!(desk.windows.shown_windows().len(), 1); + assert!(!sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "{sent:?}"); +} + +#[test] +fn closing_the_last_window_of_an_unsaved_workbook_asks_and_the_app_quits_after() { + let mut desk = Desk::new(true); + desk.app.run("cell.set", json!({"cell": "A1", "input": "42"})).unwrap(); + desk.frames(2); + let w = desk.window_of(0); + let sent = desk.close(w); + assert!(sent_to(&sent, ViewportId::ROOT, &ViewportCommand::CancelClose), "the window stays open to ask: {sent:?}"); + assert_eq!(desk.app.dialog.as_ref().map(|d| d.name()), Some("saveChanges")); + assert_eq!(desk.app.session.documents().len(), 1); + + let sent = desk.answer(SaveAnswer::Cancel); + assert!(!sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "Cancel keeps it open"); + assert_eq!(desk.app.session.documents().len(), 1); + + desk.close(w); + let sent = desk.answer(SaveAnswer::DontSave); + assert!(desk.app.session.documents().is_empty()); + assert!(sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "the last window closed: the app quits: {sent:?}"); + assert!(desk.app.allow_close); +} + +#[test] +fn without_quitting_on_the_last_window_the_app_hides_and_new_shows_it_again() { + let mut desk = Desk::new(false); + let w = desk.window_of(0); + let sent = desk.close(w); + assert!(desk.app.session.documents().is_empty(), "a clean workbook closes without asking"); + assert!(sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Visible(false)), "{sent:?}"); + assert!(!sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "the app keeps running: {sent:?}"); + assert!(desk.app.session.documents().is_empty(), "and doesn't make a workbook of its own"); + + let sent = desk.frame_with(|app, _| app.run_or_alert("file.new", json!({}))); + assert!(sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Visible(true)), "{sent:?}"); + assert_eq!(desk.windows.root_window(), Some(desk.window_of(0))); +} + +#[test] +fn quit_asks_about_each_unsaved_workbook_in_its_window_and_cancel_stops() { + let mut desk = Desk::new(true); + desk.app.run("cell.set", json!({"cell": "A1", "input": "1"})).unwrap(); + desk.app.run("file.new", json!({})).unwrap(); + desk.app.run("cell.set", json!({"cell": "A1", "input": "2"})).unwrap(); + desk.frames(3); + let (w1, w2) = (desk.window_of(0), desk.window_of(1)); + assert_eq!(desk.app.session.active_window(), Some(w2)); + + let sent = desk.frame_with(|app, ctx| app.request_quit(ctx)); + assert_eq!(desk.app.session.active_window(), Some(w1), "Book1's window comes forward to ask"); + assert!(sent_to(&sent, desk.viewport(w1), &ViewportCommand::Focus), "{sent:?}"); + assert_eq!(desk.app.dialog.as_ref().map(|d| d.name()), Some("saveChanges")); + let sent = desk.answer(SaveAnswer::Cancel); + assert!(!sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "Cancel stops the quit: {sent:?}"); + assert_eq!(desk.app.session.documents().len(), 2); + assert!(desk.app.dialog.is_none()); + + desk.frame_with(|app, ctx| app.request_quit(ctx)); + let sent = desk.answer(SaveAnswer::DontSave); + assert!(!sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "Book2 is still unsaved: {sent:?}"); + assert_eq!(desk.app.session.active_window(), Some(w2), "then Book2's window asks"); + assert_eq!(desk.app.dialog.as_ref().map(|d| d.name()), Some("saveChanges")); + let sent = desk.answer(SaveAnswer::DontSave); + assert!(sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "both answered: the app quits: {sent:?}"); +} + +#[test] +fn the_root_adopts_the_remaining_window_with_its_workbook_and_state() { + let mut desk = Desk::new(true); + desk.app.run("file.new", json!({})).unwrap(); + desk.frames(3); + let (w1, w2) = (desk.window_of(0), desk.window_of(1)); + assert_eq!(desk.windows.root_window(), Some(w1)); + let book2 = desk.app.session.documents()[1].uid; + desk.app.enter_window(w2); + desk.app.run("selection.set", json!({"range": "C3"})).unwrap(); + desk.app.begin_edit(Some("kept".into()), false); + desk.frames(2); + + let sent = desk.close(w1); + assert!(sent_to(&sent, ViewportId::ROOT, &ViewportCommand::CancelClose), "closing the root's window doesn't end the app: {sent:?}"); + assert!(!sent_to(&sent, ViewportId::ROOT, &ViewportCommand::Close), "{sent:?}"); + assert_eq!(desk.windows.root_window(), Some(w2), "Book2's window moves into the root"); + assert_eq!(desk.windows.shown_windows(), [w2]); + assert_eq!(desk.app.session.documents().len(), 1); + assert_eq!(desk.app.session.doc().unwrap().uid, book2); + assert_eq!(desk.app.editor.as_ref().map(|ed| ed.text.as_str()), Some("kept"), "its draft is still open"); + assert_eq!(desk.app.session.doc().unwrap().selection.active, CellRef::parse("C3").unwrap()); + assert_eq!(desk.app.session.doc().unwrap().wb.active().unwrap().value(CellRef::parse("C3").unwrap()), Value::Empty); +} + +#[test] +fn menu_commands_follow_the_focused_window_and_leaving_a_window_enters_its_draft() { + let mut desk = Desk::new(true); + desk.app.run("file.new", json!({})).unwrap(); + desk.frames(3); + let (w1, w2) = (desk.window_of(0), desk.window_of(1)); + desk.app.begin_edit(Some("typed".into()), false); + desk.frames(2); + assert_eq!(desk.app.session.active_window(), Some(w2)); + + // The user clicks into Book1's window. + let (v1, v2) = (desk.viewport(w1), desk.viewport(w2)); + INFOS.with(|i| { + let mut i = i.borrow_mut(); + i.entry(v1).or_default().focused = Some(true); + i.entry(v2).or_default().focused = Some(false); + }); + desk.frames(2); + assert_eq!(desk.app.session.active_window(), Some(w1), "commands from the menu bar go to the window focused last"); + assert!(desk.app.editor.is_none(), "Book1's window has no draft"); + let book2 = &desk.app.session.documents()[1]; + assert_eq!(book2.wb.active().unwrap().value(CellRef::new(0, 0)), Value::text("typed"), "the draft was entered in its own window"); + desk.app.run("cell.set", json!({"cell": "B1", "input": "menu"})).unwrap(); + assert_eq!(desk.app.session.documents()[0].wb.active().unwrap().value(CellRef::new(0, 1)), Value::text("menu")); +} diff --git a/docs/hardware-parity.md b/docs/hardware-parity.md index 3f9135c490..f19b4a49dd 100644 --- a/docs/hardware-parity.md +++ b/docs/hardware-parity.md @@ -1,6 +1,6 @@ # Hardware parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (direct desktop printing through craft-print) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (a native window per workbook window) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 The hardware Excel uses, per platform, against GridCraft. Part of [target-app-parity.md](target-app-parity.md). **Hardware: ~40% ready, 25–40 h** (estimated). @@ -11,7 +11,7 @@ The hardware Excel uses, per platform, against GridCraft. Part of | GPU rendering of the grid and charts | DirectX / Metal accelerated drawing | wgpu: Metal · DirectX 12 (default since 10-08; OpenGL when DirectX 12 has no GPU driver or fails to start; Vulkan only on request, so the Intel UHD `igvk64.dll` crash (#120) can't happen; shaders compiled with FXC, so no DXC runtime is needed, #339) · Vulkan/GL · Vulkan/GL · WebGPU or WebGL2 | At parity in kind. Driver problems: screen flashing on Windows 11 (#76): 2–4 h | | Pen and touch (Draw tab) | Pen pressure and tilt, eraser end, touch mode, Ink to Shape, ink replay | Pen, highlighter, eraser, Ink to Shape with mouse/pen input; **no pressure or tilt**, no touch mode | Pressure-sensitive strokes where winit reports force; touch-friendly ribbon spacing: 4–6 h | | Trackpad and mouse | Smooth scrolling, pinch zoom, horizontal scroll, Magic Mouse | Smooth scrolling and zoom via egui | Verify pinch zoom on each platform: 1–2 h | -| Multiple windows and monitors | One window per workbook, New Window for the same workbook, per-monitor DPI | One window; multiple workbooks switch inside it (#162; PR #171) | Native window per workbook, New Window, Arrange All across monitors: 6–8 h | +| Multiple windows and monitors | One window per workbook, New Window for the same workbook, per-monitor DPI | A native window per workbook window on the desktop, New Window for the same workbook; new windows cascade from the last one on its monitor (web: one window) | Arrange All across monitors; verify per-monitor DPI with windows on two monitors: 2–3 h | | High-DPI and display scaling | yes | yes (display scale matched to desktop density; View › Interface size 75–200 % on top, #230) | — | | Accessibility hardware (screen readers, braille displays, switch control) | Narrator, JAWS, NVDA, VoiceOver | AccessKit enabled in eframe; grid cells and ribbon not yet exposed as an accessible tree | Counted under UI ([ui-parity.md](ui-parity.md)) | | Printers | System print with driver options | Desktop submits through craft-print with printer choice, copies, duplex and grayscale; web browser printing | Counted under print | @@ -20,6 +20,7 @@ The hardware Excel uses, per platform, against GridCraft. Part of | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Multiple windows: each workbook window is a native window of its own (desktop). | | 2026-10-12 | minor | Desktop File > Print now discovers printers and submits PDF jobs through pinned craft-print with printer choice, copies, duplex and grayscale; browser printing and PDF export remain available. Remaining print parity includes page ranges and driver-specific tray/options UI; physical printer fidelity needs hardware validation. | | 2026-10-11 | trivial | Windows: DirectX 12 shaders compiled with FXC, no DXC runtime needed (#339, #354) | | 2026-10-11 | trivial | Windows: OpenGL fallback when DirectX 12 has no GPU or fails to start; Vulkan never by default (#120) | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 839984959d..817b5b9151 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (New Window opens a second window on the same workbook, titled Name:1 / Name:2; Switch Windows lists the open windows; a cell being typed stays in its own window through New, Open and window switches; macOS Quit asks about unsaved workbooks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (every workbook window is a native window of its own on the desktop, with its own draft, dialog and ribbon; closing one of several windows on a workbook doesn't ask; Quit asks about each unsaved workbook in its window; the last window closed quits on Windows and Linux, macOS keeps running) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -33,8 +33,8 @@ Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes | Backstage (File) view: Info, New with templates, Open recent, Export, Options | yes | yes (#174, 10-11): the File tab opens a full-window view (Esc or the back arrow return) with Home, New (blank + the three samples), Open (recent + Browse; the recent list tells when each workbook was last changed, 10-12), Info (location, properties, Protect Workbook), Save, Save As, Print (page preview, active sheet or workbook, fit to page, Page Setup), Export (PDF, CSV, web page), Close and Options (`file.options`: theme, language, formula bar, AutoSave, calculation) | no Account, Share or template gallery beyond the samples; Options applies at once (no OK/Cancel dialog) and holds only the settings GridCraft has | | Dark mode, system appearance | yes | yes | done (10-09) | | Page Layout view (paper view with rulers and header editing) | yes | yes (#329, 10-11): per-sheet view saved in XLSX, pages with margins and gaps on the print pagination, "Click to add data" pages, rulers (inches or centimetres), headers and footers drawn and edited in place with a Header & Footer tab | margins drag on the rulers (#792, with header and footer margins); header/footer pictures, Different First Page / Odd & Even missing (1–3 h) | -| Multiple windows per workbook | yes | no (#162) | see [hardware-parity.md](hardware-parity.md) | -| Switch Windows, New Window, unsaved workbooks on quit | yes | yes (10-22): View › New Window opens another window on the same workbook (no copy: saving from either saves it), each with its own selection, active sheet and scroll; a workbook with several windows titles them "Book1:1", "Book1:2"; File › Close closes one of them without asking and asks only for the last; View › Switch Windows and the macOS Window menu list the open windows (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {window}`, or `{doc}` for a workbook's most recent window); a cell being typed is entered into its own window and workbook before New, Open, Close, Save, Print and window switches (also between two windows on one workbook), and a refused entry stays open and stops the command; quitting (window close, Cmd+Q, the macOS Quit item) asks Save / Don't Save / Cancel for each unsaved workbook | one OS window for all workbooks (#162) | +| Multiple windows per workbook | yes | yes (10-22, desktop): each workbook window is a native window of its own (New, Open and New Window open one; titles "Book1", or "Book1:1" / "Book1:2" for a workbook's second window, with " — Edited" while unsaved). Each window keeps its own cell draft, dialog, Backstage view, message and grid state, and its ribbon acts on it; on macOS the menu bar acts on the window focused last. Leaving a window enters its draft as Enter would. A window's close button closes that window: one of several on a workbook closes without asking, a workbook's last asks Save / Don't Save / Cancel when unsaved. Quit asks about each unsaved workbook in its window, and Cancel stops it. The last window closed quits on Windows and Linux; macOS keeps running with the menu bar until New or Open. Web: one window | no Arrange All; a hidden window (View › Hide) has no native window; every window repaints when one does; see [hardware-parity.md](hardware-parity.md) | +| Switch Windows, New Window, unsaved workbooks on quit | yes | yes (10-22): View › New Window opens another window on the same workbook (no copy: saving from either saves it), each with its own selection, active sheet and scroll; a workbook with several windows titles them "Book1:1", "Book1:2"; File › Close closes one of them without asking and asks only for the last; View › Switch Windows and the macOS Window menu list the open windows (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {window}`, or `{doc}` for a workbook's most recent window); a cell being typed is entered into its own window and workbook before New, Open, Close, Save, Print and window switches (also between two windows on one workbook), and a refused entry stays open and stops the command; quitting (Cmd+Q, the macOS Quit item, the Dock) asks Save / Don't Save / Cancel for each unsaved workbook | | ## Grid interaction @@ -177,6 +177,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | One native window per workbook window on the desktop: per-window drafts, dialogs and ribbons; menu bar commands go to the focused window; closing one of several windows on a workbook doesn't ask; Quit asks in each unsaved workbook's window; the last window closed quits (Windows, Linux) or leaves macOS running | | 2026-10-22 | minor | New Window opens a second window on the same workbook instead of a copy that saved over the original; windows keep their own selection, sheet and scroll; titles Name:1 / Name:2; Switch Windows, Hide and Unhide work by window; File › Close closes one window of several without asking | | 2026-10-22 | minor | New workbook no longer loses the first one: the cell being typed is entered into its own workbook before any File or window command (an entry the workbook refuses stays open and the command doesn't run; an untouched F2 draft changes nothing), the editor is tied to its workbook, View › Switch Windows and the macOS Window menu list the open workbooks, and the macOS Quit item asks about unsaved workbooks like the close button | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | From d4db58cde467f7a412293e55861d390a7f77b4ad Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 16:29:24 +0000 Subject: [PATCH 084/687] G3: feedback from a command that switched windows shows in the window it left active after_engine, show_open_warnings and alert bring the per-window state in line with the engine's active window first, so a notice, message or toast from opening a workbook (also by a script on the session) lands in the opened workbook's window instead of being parked with the previous one. --- crates/ui-egui/src/lib.rs | 7 +++++++ crates/ui-egui/tests/doc_windows.rs | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f405a3d185..19f69a90fa 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -648,6 +648,8 @@ impl SheetApp { /// Shows a command error: a toast for an unavailable command, else a message box. fn alert(&mut self, e: String) { + // Shown in the window the user ends up in, also after a command that switched windows. + self.follow_active_window(); if e.contains("is not available right now") { self.toast = Some((e, now_ms())); } else { @@ -799,6 +801,8 @@ impl SheetApp { /// Picks up requests commands made (dialogs, edit mode…). pub fn after_engine(&mut self) { + // The requests belong to the window the command left active. + self.follow_active_window(); for req in self.session.take_ui_requests() { match req { UiRequest::Dialog(name, params) => self.open_dialog(&name, params), @@ -924,6 +928,9 @@ impl SheetApp { /// most important one (a preserved VBA project outranks, say, a skipped optional part). /// Kept deliberately generic: any open-time warning can reach the user this way. pub fn show_open_warnings(&mut self, r: &Json) { + // The opened workbook's window shows them, however it was opened (a script may have run + // `file.open` on the session directly). + self.follow_active_window(); let Some(list) = r.get("warnings").and_then(Json::as_array) else { self.notice = None; return; diff --git a/crates/ui-egui/tests/doc_windows.rs b/crates/ui-egui/tests/doc_windows.rs index ff8e2d6318..b6d518ce2e 100644 --- a/crates/ui-egui/tests/doc_windows.rs +++ b/crates/ui-egui/tests/doc_windows.rs @@ -282,3 +282,28 @@ fn menu_commands_follow_the_focused_window_and_leaving_a_window_enters_its_draft desk.app.run("cell.set", json!({"cell": "B1", "input": "menu"})).unwrap(); assert_eq!(desk.app.session.documents()[0].wb.active().unwrap().value(CellRef::new(0, 1)), Value::text("menu")); } + +#[test] +fn feedback_from_opening_a_workbook_shows_in_its_new_window() { + let mut desk = Desk::new(true); + desk.app.run("cell.set", json!({"cell": "A1", "input": "kept"})).unwrap(); + desk.frames(2); + let book1 = desk.window_of(0); + let mut wb = gridcraft_engine::model::Workbook::new(); + wb.vba_project = Some(b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1 fake OLE2 vbaProject".to_vec().into()); + let b64 = gridcraft_engine::io::base64_encode(&gridcraft_engine::io::save_bytes(&wb, "macros.xlsm").unwrap()); + desk.frame_with(|app, _| { + // A script opening the file on the session, then the interface reporting on it. + let r = app.session.run("file.open", json!({"name": "macros.xlsm", "base64": b64})).unwrap(); + app.show_open_warnings(&r); + app.run_or_alert("no.such.command", json!({})); + }); + desk.frames(2); + let opened = desk.window_of(1); + assert_eq!(desk.windows.shown_windows(), [book1, opened]); + assert_eq!(desk.app.session.active_window(), Some(opened)); + assert_eq!(desk.app.notice.as_ref().map(|n| n.kind), Some(gridcraft_ui_egui::NoticeKind::Warning), "the macro notice shows in the opened window"); + assert!(desk.app.message.is_some(), "and so does the alert"); + desk.app.enter_window(book1); + assert!(desk.app.notice.is_none() && desk.app.message.is_none(), "Book1's window shows neither"); +} From b5b3969377b125cd782768ace8cfbaee06f57bc9 Mon Sep 17 00:00:00 2001 From: Higgs32584 Date: Thu, 22 Oct 2026 12:43:48 -0400 Subject: [PATCH 085/687] scripts: trust gate review fixes (#2178) - automate.trustScripts: only a missing `trusted` defaults to true; a non-boolean value is an error and leaves the workbook untrusted. - The refusal message points at Trust Scripts in View > Macros (Alt+F8) and at automate.trustScripts. - Catalog row for automate.trustScripts; parity checklist regenerated. - Script steps run inside an io::ScriptSandbox (thread-local) so read_file/write_file refuse whatever the parameter is called, and a step that raises OpenUrl is dropped and stops the script. The id/param deny list stays as the up-front refusal. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/catalog.rs | 1 + crates/engine/src/cmd/extra.rs | 28 ++++++++++++--- crates/engine/src/io.rs | 62 ++++++++++++++++++++++++++++++++++ crates/engine/src/tests.rs | 38 +++++++++++++++++++++ docs/parity-checklist.md | 7 ++-- 5 files changed, 129 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/catalog.rs b/crates/engine/src/catalog.rs index d6b67e1b45..007cbf4bc0 100644 --- a/crates/engine/src/catalog.rs +++ b/crates/engine/src/catalog.rs @@ -273,6 +273,7 @@ pub const CATALOG: &[(&str, &str, &str, &str)] = &[ ("automate.newScript", "Automate", "Scripting Tools", "New Script"), ("automate.allScripts", "Automate", "Scripting Tools", "All Scripts"), ("automate.recordActions", "Automate", "Scripting Tools", "Record Actions"), + ("automate.trustScripts", "Automate", "Scripting Tools", "Trust Scripts"), // Table Design ("table.rename", "Table Design", "Properties", "Table Name"), ("table.resize", "Table Design", "Properties", "Resize Table"), diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index b2d8c352df..f248c59f2d 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -1017,7 +1017,10 @@ pub(crate) fn has_scripts(wb: &Workbook) -> bool { /// Why a script step is refused, whatever the workbook's trust: scripts stay inside the /// workbook. Anything that reads or writes files (`file.*`, or any `path` parameter: Get Data, /// From Text/CSV, Insert Picture) or leaves the app (`link.*` opens URLs) is the caller's to run -/// directly, not a stored script's. (Nested `automate.*` steps are skipped.) +/// directly, not a stored script's. (Nested `automate.*` steps are skipped.) This check goes by +/// command id and parameter name, so it is only the up-front refusal: the steps also run inside +/// an [`crate::io::ScriptSandbox`] (file access refused whatever the parameter is called) and +/// [`run_script`] drops any `OpenUrl` request a step raises. fn denied_script_step(id: &str, params: &Json) -> Option { if id.starts_with("file.") || id.starts_with("link.") { return Some(format!("scripts can't run `{id}`: it reaches outside the workbook")); @@ -1029,7 +1032,12 @@ fn denied_script_step(id: &str, params: &Json) -> Option { } fn trust_scripts(s: &mut Session, p: &Json) -> Result { - let trusted = bool_param(p, "trusted").unwrap_or(true); + // Only a missing `trusted` means "trust"; `"false"`, `0` or `null` are mistakes, not consent. + let trusted = match p.get("trusted") { + None => true, + Some(Json::Bool(b)) => *b, + Some(_) => return Err(bad("automate.trustScripts", "`trusted` must be true or false")), + }; s.doc_mut()?.scripts_trusted = trusted; Ok(json!({"trusted": trusted})) } @@ -1045,7 +1053,7 @@ fn run_script(s: &mut Session, p: &Json) -> Result { if !d.scripts_trusted && !bool_param(p, "trusted").unwrap_or(false) { return Err(bad( "automate.runScript", - "scripts in this workbook came from a file and are disabled; trust the workbook first (Automate › Macros › Trust Scripts, or `trusted: true`)", + "scripts in this workbook came from a file and are disabled; trust the workbook first (Trust Scripts in View › Macros, Alt+F8; `automate.trustScripts`; or `trusted: true`)", )); } let steps: Vec = steps.into_iter().take(100_000).collect(); @@ -1055,13 +1063,25 @@ fn run_script(s: &mut Session, p: &Json) -> Result { return Err(bad("automate.runScript", why)); } } + let _sandbox = crate::io::ScriptSandbox::enter(); let mut n = 0; for st in &steps { let id = st["command"].as_str().unwrap_or(""); if id.starts_with("automate.") { continue; } - s.execute(id, st["params"].clone())?; + let before = s.ui_requests.len(); + let ran = s.execute(id, st["params"].clone()); + // A step that asks the host to open a URL leaves the app: drop the request and stop. + if s.ui_requests.get(before..).is_some_and(|new| new.iter().any(|r| matches!(r, crate::UiRequest::OpenUrl(_)))) { + let mut i = 0; + s.ui_requests.retain(|r| { + i += 1; + i <= before || !matches!(r, crate::UiRequest::OpenUrl(_)) + }); + return Err(bad("automate.runScript", format!("scripts can't open links (`{id}`)"))); + } + ran?; n += 1; } Ok(json!({"ran": n})) diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 5c0bf800b5..b18e8577bf 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -556,8 +556,41 @@ pub fn to_html(wb: &Workbook, sheet: usize) -> Result { Ok(out) } +thread_local! { + /// How many [`ScriptSandbox`]es are alive on this thread. + static SCRIPT_SANDBOX: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// While one is alive, [`read_file`] and [`write_file`] on this thread refuse every path: a +/// stored script's steps run inside one (#2178), so no command reaches the file system from a +/// script whatever its parameters are called. Dropping it (also on unwind) lifts the refusal. +pub(crate) struct ScriptSandbox(()); + +impl ScriptSandbox { + pub(crate) fn enter() -> Self { + let _ = SCRIPT_SANDBOX.try_with(|c| c.set(c.get().saturating_add(1))); + ScriptSandbox(()) + } +} + +impl Drop for ScriptSandbox { + fn drop(&mut self) { + let _ = SCRIPT_SANDBOX.try_with(|c| c.set(c.get().saturating_sub(1))); + } +} + +/// Refuses file access while a [`ScriptSandbox`] is alive (and, failing closed, while the +/// thread is being torn down). +fn script_sandbox_check(path: &str) -> Result<()> { + if SCRIPT_SANDBOX.try_with(|c| c.get() > 0).unwrap_or(true) { + return Err(EngineError::Other(format!("{path}: scripts can't read or write files"))); + } + Ok(()) +} + #[cfg(not(target_arch = "wasm32"))] pub fn read_file(path: &str) -> Result> { + script_sandbox_check(path)?; let meta = std::fs::metadata(path).map_err(|e| EngineError::Other(format!("{path}: {e}")))?; if meta.len() > 2 * 1024 * 1024 * 1024 { return Err(EngineError::Other("files larger than 2 GB aren't supported".into())); @@ -567,11 +600,13 @@ pub fn read_file(path: &str) -> Result> { #[cfg(target_arch = "wasm32")] pub fn read_file(path: &str) -> Result> { + script_sandbox_check(path)?; Err(EngineError::Other(format!("{path}: no file system in the browser"))) } #[cfg(not(target_arch = "wasm32"))] pub fn write_file(path: &str, bytes: &[u8]) -> Result<()> { + script_sandbox_check(path)?; // Write to a temporary file, then rename: a crash never leaves a half-written workbook. let tmp = format!("{path}.sctmp"); std::fs::write(&tmp, bytes).and_then(|()| std::fs::rename(&tmp, path)).map_err(|e| { @@ -584,6 +619,7 @@ pub fn write_file(path: &str, bytes: &[u8]) -> Result<()> { #[cfg(target_arch = "wasm32")] pub fn write_file(path: &str, _bytes: &[u8]) -> Result<()> { + script_sandbox_check(path)?; Err(EngineError::Other(format!("{path}: no file system in the browser"))) } @@ -902,6 +938,32 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// While a script runs (#2178), file reads and writes are refused whatever command or + /// parameter asked for them; nesting and dropping the sandbox restore access. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn script_sandbox_refuses_file_access_until_dropped() { + let dir = std::env::temp_dir().join(format!("gridcraft-sandbox-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let existing = dir.join("in.csv"); + std::fs::write(&existing, b"a,b\n").unwrap(); + let (existing, out) = (existing.to_string_lossy().to_string(), dir.join("out.xlsx").to_string_lossy().to_string()); + { + let _outer = ScriptSandbox::enter(); + { + let _inner = ScriptSandbox::enter(); + assert!(read_file(&existing).is_err()); + } + assert!(read_file(&existing).is_err(), "the outer sandbox still holds"); + assert!(write_file(&out, b"leak").is_err()); + assert!(!std::path::Path::new(&out).exists()); + } + assert_eq!(read_file(&existing).unwrap(), b"a,b\n"); + write_file(&out, b"ok").unwrap(); + let _ = std::fs::remove_dir_all(&dir); + } + /// A minimal compound (CFB/OLE2) file — FAT in sector 0, directory in sector 1 — holding a /// root entry and the given stream names. fn cfb(streams: &[&str]) -> Vec { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 567148d41b..dcab1d2046 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1147,6 +1147,44 @@ fn untrusted_workbook_scripts_are_refused_until_trusted() { assert!(s.execute("automate.runScript", json!({"name": "Fill"})).is_err()); } +#[test] +fn trust_scripts_only_defaults_to_trusted_when_the_flag_is_missing() { + let mut s = opened_with_script(); + // A `trusted` that isn't a boolean is an error and leaves the workbook untrusted. + for wrong in [json!("false"), json!("true"), json!(0), json!(1), json!(null), json!([true])] { + let err = s.execute("automate.trustScripts", json!({"trusted": wrong})).unwrap_err().to_string(); + assert!(err.contains("trusted"), "{wrong}: {err}"); + assert!(!s.doc().unwrap().scripts_trusted, "{wrong} must not trust the workbook"); + assert!(s.execute("automate.runScript", json!({"name": "Fill"})).is_err()); + } + // The refusal names where the Trust Scripts button actually is. + let err = s.execute("automate.runScript", json!({"name": "Fill"})).unwrap_err().to_string(); + assert!(err.contains("View › Macros") && err.contains("automate.trustScripts"), "{err}"); + assert_eq!(s.execute("automate.trustScripts", json!({"trusted": false})).unwrap()["trusted"], false); + assert!(!s.doc().unwrap().scripts_trusted); + assert_eq!(s.execute("automate.trustScripts", json!({})).unwrap()["trusted"], true); + assert!(s.doc().unwrap().scripts_trusted); +} + +#[test] +fn scripts_leave_file_access_working_after_they_run() { + // The sandbox a script runs in is lifted afterwards, also when a step fails. + let dir = std::env::temp_dir().join(format!("gridcraft-script-sandbox-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let csv = dir.join("in.csv"); + std::fs::write(&csv, b"a,b\n1,2\n").unwrap(); + let csv = csv.to_string_lossy().to_string(); + let mut s = s(); + let bad_step = json!({"command": "cell.set", "params": {"cell": "not a cell", "input": "x"}}); + s.execute("automate.newScript", json!({"name": "Bad", "commands": [bad_step]})).unwrap(); + assert!(s.execute("automate.runScript", json!({"name": "Bad"})).is_err()); + s.execute("automate.newScript", json!({"name": "Ok", "commands": [{"command": "cell.set", "params": {"cell": "A1", "input": "1"}}]})).unwrap(); + s.execute("automate.runScript", json!({"name": "Ok"})).unwrap(); + s.execute("data.fromTextCsv", json!({"path": csv})).unwrap(); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn scripts_cannot_reach_outside_the_workbook_even_when_trusted() { let dir = std::env::temp_dir().join(format!("gridcraft-script-deny-{}", std::process::id())); diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index d218c8169d..ee8bcfb754 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -4,7 +4,7 @@ Each entry is an Excel ribbon or menu command (feature names only). It counts as implemented when GridCraft registers a command with the same id. This measures breadth only; the overall assessment (depth, fidelity, effort) is in [target-app-parity.md](target-app-parity.md). -**Overall: 274 / 290 commands implemented (94.5%).** +**Overall: 275 / 291 commands implemented (94.5%).** | Tab | Implemented | Total | Parity | |---|---|---|---| @@ -17,7 +17,7 @@ Each entry is an Excel ribbon or menu command (feature names only). It counts as | Data | 23 | 28 | 82% | | Review | 15 | 18 | 83% | | View | 20 | 20 | 100% | -| Automate | 3 | 3 | 100% | +| Automate | 4 | 4 | 100% | | Table Design | 13 | 13 | 100% | | Chart Design | 8 | 8 | 100% | | Format | 3 | 3 | 100% | @@ -324,13 +324,14 @@ Implemented: - `view.switchWindows` Switch Windows (Window) - `view.macros` Macros (Macros) -## Automate (3 / 3, 100%) +## Automate (4 / 4, 100%) Implemented: - `automate.newScript` New Script (Scripting Tools) - `automate.allScripts` All Scripts (Scripting Tools) - `automate.recordActions` Record Actions (Scripting Tools) +- `automate.trustScripts` Trust Scripts (Scripting Tools) ## Table Design (13 / 13, 100%) From 2ed3df2bd64d2022d28ce588e6275babd6d654ce Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 22 Oct 2026 16:50:10 +0000 Subject: [PATCH 086/687] G4: two windows on one workbook without copying it; Arrange All - The workbook's active sheet is an atomic set in place (Workbook::active_sheet / set_active_sheet), so a window swap or sheet switch never clones the Arc or marks it dirty. Selections already live in DocState and the window views. Save records the sheet of the most recently active window. - A dialog taken out of its window while it runs goes back to that window when a command run from it switched windows (dialogs::show, ui.dialog.confirm). - view.arrangeAll {arrange?: tiled|horizontal|vertical|cascade, activeWorkbook?} returns the visible windows, active first; the desktop moves and sizes the native windows over the main window's monitor. New Window and Arrange All buttons on the View ribbon; Arrange Windows dialog. - Docs: ui-parity, hardware-parity, gaps #20, target-app-parity, ROADMAP. --- ROADMAP.md | 4 +- crates/engine/src/cmd/analysis.rs | 2 +- crates/engine/src/cmd/data.rs | 18 +-- crates/engine/src/cmd/draw.rs | 4 +- crates/engine/src/cmd/edit.rs | 27 ++--- crates/engine/src/cmd/extra.rs | 51 ++++++-- crates/engine/src/cmd/file.rs | 4 +- crates/engine/src/cmd/forecast.rs | 10 +- crates/engine/src/cmd/format.rs | 2 +- crates/engine/src/cmd/formulas.rs | 16 +-- crates/engine/src/cmd/insert.rs | 22 ++-- crates/engine/src/cmd/inspect.rs | 2 +- crates/engine/src/cmd/mod.rs | 6 +- crates/engine/src/cmd/pivot.rs | 16 +-- crates/engine/src/cmd/print.rs | 6 +- crates/engine/src/cmd/review.rs | 6 +- crates/engine/src/cmd/sheet.rs | 20 ++-- crates/engine/src/cmd/slicer.rs | 6 +- crates/engine/src/cmd/solver.rs | 12 +- crates/engine/src/cmd/view.rs | 4 +- crates/engine/src/cmd/whatif.rs | 14 +-- crates/engine/src/io.rs | 2 +- crates/engine/src/lib.rs | 4 +- crates/engine/src/sample.rs | 2 +- crates/engine/src/tests.rs | 75 +++++++++++- crates/engine/src/windows.rs | 30 ++--- crates/engine/tests/command_sequence_fuzz.rs | 6 +- .../engine/tests/hidden_sheet_navigation.rs | 12 +- crates/model/src/workbook.rs | 68 ++++++++++- crates/ui-egui/examples/snapshot.rs | 2 +- crates/ui-egui/src/control.rs | 3 +- crates/ui-egui/src/dialogs.rs | 30 ++++- crates/ui-egui/src/doc_windows.rs | 74 +++++++++++- crates/ui-egui/src/formula_bar.rs | 6 +- crates/ui-egui/src/grid.rs | 33 ++--- crates/ui-egui/src/lib.rs | 27 +++-- crates/ui-egui/src/panes.rs | 2 +- crates/ui-egui/src/paper_view.rs | 8 +- crates/ui-egui/src/ribbon.rs | 12 +- crates/ui-egui/src/tabs.rs | 10 +- crates/ui-egui/src/text_box.rs | 2 +- crates/ui-egui/src/window_ui.rs | 13 ++ crates/ui-egui/tests/commit_after_pointing.rs | 4 +- crates/ui-egui/tests/doc_windows.rs | 113 ++++++++++++++++++ crates/ui-egui/tests/popup_dismissal.rs | 2 +- crates/ui-egui/tests/sheet_navigator.rs | 4 +- crates/ui-egui/tests/sheet_tabs.rs | 6 +- crates/ui-egui/tests/trace_navigation.rs | 4 +- crates/xlsx/src/read.rs | 2 +- crates/xlsx/src/write.rs | 4 +- crates/xlsx/src/xls/mod.rs | 2 +- crates/xlsx/src/xlsb.rs | 2 +- docs/gaps.md | 7 +- docs/hardware-parity.md | 5 +- docs/target-app-parity.md | 9 +- docs/ui-parity.md | 7 +- 56 files changed, 620 insertions(+), 224 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 38c394fb06..782c00e3a2 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (a native window per workbook window; no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save; New Window shares the workbook) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (Arrange All; a native window per workbook window; no lost workbook after New: drafts stay in their workbook, Switch Windows, macOS Quit asks to save; New Window shares the workbook) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -135,6 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-22 | View › New Window and Arrange All on the ribbon. Arrange All tiles, stacks side by side or top to bottom, or cascades the native windows over the main window's monitor, the active one first. Two windows on one workbook show different sheets without copying the workbook or marking it changed; Save records the sheet of the window used last. A dialog stays in the window it was opened in | | 2026-10-22 | On the desktop every workbook window is a native window of its own: New, Open and New Window open one, each keeps its own cell draft, dialog and ribbon, and the macOS menu bar acts on the window focused last. Closing one of several windows on a workbook doesn't ask; closing a workbook's last window, or quitting, asks about unsaved changes in that window. The last window closed quits on Windows and Linux; macOS keeps running with the menu bar. First craft-libs `craft-window` use (window list and viewport driver) | | 2026-10-22 | View › New Window opens a second window on the same workbook (it used to open a copy whose Save overwrote the original): each window has its own selection, active sheet and scroll, titles read "Book1:1" / "Book1:2", and Switch Windows, Hide, Unhide and Close act per window. The control channel, CLI `send` and MCP tools take an optional `window` or `doc` id and default to the active window; `window.list` (MCP `list_windows`) lists the windows | | 2026-10-22 | Typing in a cell and then choosing File › New no longer strands the first workbook: the entry is committed into its own workbook before any File or window command (a refused entry stays open and stops the command), View › Switch Windows and the macOS Window menu list the open workbooks with unsaved markers, `view.switchWindows {doc}` switches by a stable workbook id, and the macOS Quit item asks about unsaved workbooks | @@ -223,6 +224,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Progress log: Arrange All; two windows on one workbook without copying it | | 2026-10-22 | minor | Progress log: a native window per workbook window | | 2026-10-22 | minor | Progress log: New Window shares the workbook; window ids for scripts and MCP | | 2026-10-22 | minor | Progress log: drafts stay in their workbook, Switch Windows, macOS Quit asks to save | diff --git a/crates/engine/src/cmd/analysis.rs b/crates/engine/src/cmd/analysis.rs index 8ff7e35828..6225b4934a 100644 --- a/crates/engine/src/cmd/analysis.rs +++ b/crates/engine/src/cmd/analysis.rs @@ -1182,7 +1182,7 @@ mod tests { fn v(s: &Session, a: &str) -> Value { let d = s.doc().unwrap(); let (sh, body) = crate::cmd::split_sheet(a); - let si = sh.map(|n| d.wb.sheet_index(&n).unwrap()).unwrap_or(d.wb.active_sheet); + let si = sh.map(|n| d.wb.sheet_index(&n).unwrap()).unwrap_or(d.wb.active_sheet()); d.wb.sheet(si).unwrap().value(CellRef::parse(body).unwrap()) } fn n(s: &Session, a: &str) -> f64 { diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index 52a716cff4..8ffab2b516 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -158,7 +158,7 @@ fn data_target(s: &Session, p: &Json, cmd: &str) -> Result<(usize, RangeRef)> { { return Err(bad(cmd, format!("`{t}` names a sheet other than `sheet`"))); } - if sheet != d.wb.active_sheet && p.get("range").is_none() { + if sheet != d.wb.active_sheet() && p.get("range").is_none() { return Err(bad(cmd, "missing `range`: the selection is on the active sheet")); } Ok((sheet, data_range(s, p)?)) @@ -210,7 +210,7 @@ fn sort_target(s: &Session, p: &Json) -> Result<(usize, RangeRef)> { { return Err(bad("data.sort", format!("`{t}` names a sheet other than `sheet`"))); } - if sheet != d.wb.active_sheet && p.get("range").is_none() { + if sheet != d.wb.active_sheet() && p.get("range").is_none() { return Err(bad("data.sort", "missing `range`: the selection is on the active sheet")); } Ok((sheet, data_range(s, p)?)) @@ -502,7 +502,7 @@ fn toggle_filter(s: &mut Session, p: &Json) -> Result { let (sheet, r) = data_target(s, p, "data.filter")?; super::refuse_unless_allowed(s, sheet, "Filtering", |p| p.autofilter)?; // The active cell belongs to the active sheet: on another sheet the table is the one at `range`. - let at = if sheet == s.doc()?.wb.active_sheet { s.doc()?.selection.active } else { r.start }; + let at = if sheet == s.doc()?.wb.active_sheet() { s.doc()?.selection.active } else { r.start }; let has = s.doc()?.wb.sheet(sheet).is_some_and(|sh| sh.autofilter.is_some()); let in_table = s.doc()?.wb.sheet(sheet).and_then(|sh| sh.table_at(at).map(|t| t.name.clone())); if let Some(name) = in_table { @@ -554,7 +554,7 @@ fn install_filter(s: &mut Session, p: &Json) -> Result { } fn filter_by(s: &mut Session, p: &Json) -> Result { - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); super::refuse_unless_allowed(s, sheet, "Filtering", |p| p.autofilter)?; let Some(sh) = s.doc()?.wb.sheet(sheet) else { return Err(EngineError::NoDocument) }; let af = match &sh.autofilter { @@ -741,7 +741,7 @@ pub(crate) fn apply_filter(wb: &mut Workbook, sheet: usize) { } fn clear_filter(s: &mut Session, _: &Json) -> Result { - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { let sh = cx.sheet_mut(sheet)?; if let Some(a) = sh.autofilter.as_mut() { @@ -754,7 +754,7 @@ fn clear_filter(s: &mut Session, _: &Json) -> Result { } fn reapply(s: &mut Session, _: &Json) -> Result { - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { apply_filter(&mut cx.wb, sheet); cx.structural = true; @@ -1258,7 +1258,7 @@ fn validate_cmd(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; let at = cell_param(s, p, "cell")?.unwrap_or(d.selection.active); let input = str_param(p, "input").unwrap_or(""); - match check_validation(&d.wb, d.wb.active_sheet, at, input) { + match check_validation(&d.wb, d.wb.active_sheet(), at, input) { None => Ok(json!({"ok": true})), Some((dv, msg)) => Ok(json!({"ok": false, "message": msg, "title": dv.error_title, "style": format!("{:?}", dv.error_style)})), } @@ -1266,7 +1266,7 @@ fn validate_cmd(s: &mut Session, p: &Json) -> Result { fn circle_invalid(s: &mut Session, _: &Json) -> Result { let d = s.doc()?; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); let Some(sh) = d.wb.sheet(sheet) else { return Err(EngineError::NoDocument) }; let mut bad_cells = Vec::new(); for dv in &sh.validations { @@ -1305,7 +1305,7 @@ fn outline_lines(s: &Session, p: &Json) -> Result<(bool, u32, u32)> { fn group(s: &mut Session, p: &Json, on: bool) -> Result { let si = target_sheet(s, p)?; // The selection belongs to the active sheet, so another sheet needs its lines. - if si != s.doc()?.wb.active_sheet && p.get("rows").is_none() && p.get("cols").is_none() { + if si != s.doc()?.wb.active_sheet() && p.get("rows").is_none() && p.get("cols").is_none() { return Err(bad(if on { "data.group" } else { "data.ungroup" }, "missing `rows` or `cols`: the selection is on the active sheet")); } let (rows, a, b) = outline_lines(s, p)?; diff --git a/crates/engine/src/cmd/draw.rs b/crates/engine/src/cmd/draw.rs index af3e65e281..a86be30995 100644 --- a/crates/engine/src/cmd/draw.rs +++ b/crates/engine/src/cmd/draw.rs @@ -72,7 +72,7 @@ fn stroke(s: &mut Session, p: &Json) -> Result { str_param(p, "color").map(str::to_string).unwrap_or_else(|| if s.draw_color.is_empty() { "#1F5FC9".into() } else { s.draw_color.clone() }); let width = f64_param(p, "width").map(|w| w as f32).unwrap_or(if s.draw_width <= 0.0 { 2.0 } else { s.draw_width }).clamp(0.5, 40.0); let d = s.doc()?; - let si = d.wb.active_sheet; + let si = d.wb.active_sheet(); let sh = d.wb.sheet(si).ok_or(EngineError::NoDocument)?; let cell = CellRef::new(sh.row_at(miny), sh.col_at(minx)); let dx = (minx - sh.col_left(cell.col)) as f32; @@ -115,7 +115,7 @@ pub fn ink_points(sh: &Shape) -> Vec<[f32; 2]> { } fn ink_to_shape(s: &mut Session, p: &Json) -> Result { - let si = s.doc()?.wb.active_sheet; + let si = s.doc()?.wb.active_sheet(); let id = u32_param(p, "id"); edit(s, |cx| { let sh = cx.sheet_mut(si)?; diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index e537407127..77c2fb8a0d 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -237,7 +237,7 @@ fn cell_set(s: &mut Session, p: &Json) -> Result { { return Err(EngineError::Other(m)); } - let active_sheet = s.doc()?.wb.active_sheet; + let active_sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { if array { // Legacy Ctrl+Shift+Enter array formula over the selection, when the selection holds @@ -414,23 +414,18 @@ fn selection_set(s: &mut Session, p: &Json) -> Result { impl crate::DocState { /// Switches the active sheet, remembering each sheet's selection. pub fn wb_switch_sheet(&mut self, i: usize) { - if i >= self.wb.sheets.len() || i == self.wb.active_sheet { + if i >= self.wb.sheets.len() || i == self.wb.active_sheet() { return; } - let cur = self.wb.active_sheet; + let cur = self.wb.active_sheet(); if self.sheet_selections.len() < self.wb.sheets.len() { self.sheet_selections.resize(self.wb.sheets.len(), crate::Selection::default()); } if let Some(slot) = self.sheet_selections.get_mut(cur) { *slot = self.selection.clone(); } - // The active sheet is view state: change it without making the workbook dirty. - let dirty = self.is_dirty(); - let wb = std::sync::Arc::make_mut(&mut self.wb); - wb.active_sheet = i; - if !dirty { - self.saved = self.wb.clone(); - } + // View state: set in place, so the workbook is neither copied nor made dirty. + self.wb.set_active_sheet(i); self.selection = self.sheet_selections.get(i).cloned().unwrap_or_default(); } } @@ -558,7 +553,7 @@ pub(super) fn go_to(s: &mut Session, p: &Json) -> Result { // A defined name or table? let d = s.doc()?; let resolved = if RangeRef::parse(split_sheet(reference).1).is_none() { - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); if let Some(n) = d.wb.name(reference, sheet) { n.formula.clone() } else if let Some((si, ti)) = d.wb.table(reference) { @@ -1535,7 +1530,7 @@ fn find(s: &mut Session, p: &Json) -> Result { let workbook = str_param(p, "within") == Some("workbook"); s.last_find = Some(p.clone()); let d = s.doc()?; - let sheets: Vec = if workbook { (0..d.wb.sheets.len()).collect() } else { vec![d.wb.active_sheet] }; + let sheets: Vec = if workbook { (0..d.wb.sheets.len()).collect() } else { vec![d.wb.active_sheet()] }; let mut hits: Vec<(usize, CellRef, String)> = Vec::new(); for si in sheets { let Some(sh) = d.wb.sheet(si) else { continue }; @@ -1565,7 +1560,7 @@ fn find(s: &mut Session, p: &Json) -> Result { return Ok(json!({"count": list.len(), "results": list})); } // Next hit after the active cell. - let cur = (d.wb.active_sheet, d.selection.active); + let cur = (d.wb.active_sheet(), d.selection.active); let key = |si: usize, c: CellRef| if by_cols { (si, c.col, c.row) } else { (si, c.row, c.col) }; let next = hits.iter().find(|(si, c, _)| key(*si, *c) > key(cur.0, cur.1)).or_else(|| hits.first()).cloned(); let Some((si, c, t)) = next else { return Err(EngineError::Other("not found".into())) }; @@ -1587,7 +1582,7 @@ fn replace(s: &mut Session, p: &Json) -> Result { let workbook = str_param(p, "within") == Some("workbook"); let active = s.doc()?.selection.active; let n = edit(s, |cx| { - let sheets: Vec = if workbook { (0..cx.wb.sheets.len()).collect() } else { vec![cx.wb.active_sheet] }; + let sheets: Vec = if workbook { (0..cx.wb.sheets.len()).collect() } else { vec![cx.wb.active_sheet()] }; let mut n = 0; for si in sheets { let cells: Vec<(CellRef, String)> = @@ -1659,7 +1654,7 @@ fn replace_ci(text: &str, what: &str, with: &str) -> String { fn format_painter(s: &mut Session, p: &Json) -> Result { if let Some(dest) = str_param(p, "apply").and_then(RangeRef::parse) { let Some((wb, sheet, src, sticky)) = s.format_painter.clone() else { return Err(EngineError::Other("Pick up a format first.".into())) }; - let dest_sheet = s.doc()?.wb.active_sheet; + let dest_sheet = s.doc()?.wb.active_sheet(); let same = std::sync::Arc::ptr_eq(&wb, &s.doc()?.wb) || s.doc()?.wb.styles == wb.styles; edit(s, |cx| { let Some(src_sheet) = wb.sheet(sheet) else { return Ok(()) }; @@ -1739,7 +1734,7 @@ fn format_painter(s: &mut Session, p: &Json) -> Result { } let sticky = bool_param(p, "sticky").unwrap_or(false); let d = s.doc()?; - s.format_painter = Some((d.wb.clone(), d.wb.active_sheet, d.selection.current(), sticky)); + s.format_painter = Some((d.wb.clone(), d.wb.active_sheet(), d.selection.current(), sticky)); ok() } diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index 3953455432..506f56443d 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -142,7 +142,15 @@ pub fn specs() -> Vec { cmd!(noundo "view.customViews", "Custom Views…", ["View", "Workbook Views"], None, "{save?: name, show?: name, delete?: name} → list", has_doc, custom_views), cmd!(noundo "view.split", "Split", ["View", "Window"], None, "{} (toggles a split at the active cell; shown as frozen panes in this version)", has_doc, split), cmd!(noundo "view.newWindow", "New Window", ["View", "Window"], None, "{} → {window, doc, title}: opens another window on the active workbook (the same workbook: saving from either window saves it)", has_doc, new_window), - cmd!(noundo "view.arrangeAll", "Arrange All", ["View", "Window"], None, "{}", has_doc, |_, _| ok()), + cmd!( + noundo "view.arrangeAll", + "Arrange All", + ["View", "Window"], + None, + "{arrange?: tiled|horizontal|vertical|cascade, activeWorkbook?: bool} → {arrange, windows}: lays out the visible windows (tiled by default; `activeWorkbook` only the active workbook's), the active window first", + has_doc, + arrange_all + ), cmd!(noundo "view.hideWindow", "Hide", ["View", "Window"], None, "{window?, doc?} (the active window by default)", has_doc, hide_window), cmd!(noundo "view.unhideWindow", "Unhide…", ["View", "Window"], None, "{window?, doc?, index?} (the most recently hidden window by default)", has_doc, unhide_window), cmd!( @@ -276,7 +284,7 @@ pub fn specs() -> Vec { fn find_chart(s: &Session, p: &Json) -> Result<(usize, usize)> { let d = s.doc()?; - let si = d.wb.active_sheet; + let si = d.wb.active_sheet(); let sh = d.wb.sheet(si).ok_or(EngineError::NoDocument)?; let idx = match u32_param(p, "chart") { Some(id) => sh.charts.iter().position(|c| c.id == id), @@ -442,7 +450,7 @@ fn select_data(s: &mut Session, p: &Json) -> Result { let (sheet_name, body) = split_sheet(range); let src_si = match sheet_name { Some(n) => d.wb.sheet_index(&n).ok_or_else(|| bad("chart.selectData", "no such sheet"))?, - None => d.wb.active_sheet, + None => d.wb.active_sheet(), }; let r = RangeRef::parse(body).ok_or_else(|| bad("chart.selectData", "not a range"))?; if r.count() > 1_000_000 { @@ -464,7 +472,7 @@ fn select_data(s: &mut Session, p: &Json) -> Result { let (sheet_name, body) = split_sheet(t); let si = match sheet_name { Some(n) => d.wb.sheet_index(&n).ok_or_else(|| bad("chart.selectData", "no such sheet"))?, - None => d.wb.active_sheet, + None => d.wb.active_sheet(), }; let r = RangeRef::parse(body).ok_or_else(|| bad("chart.selectData", "`categories` is not a range"))?; let name = d.wb.sheet(si).map(|sh| sh.name.clone()).ok_or(EngineError::NoDocument)?; @@ -546,7 +554,7 @@ fn shape_outline(s: &mut Session, p: &Json) -> Result { } fn with_shape(s: &mut Session, p: &Json, f: impl FnOnce(&mut Shape)) -> Result { - let si = s.doc()?.wb.active_sheet; + let si = s.doc()?.wb.active_sheet(); let id = u32_param(p, "id"); edit(s, |cx| { let shapes = &mut cx.sheet_mut(si)?.shapes; @@ -564,7 +572,7 @@ fn arrange(s: &mut Session, p: &Json, forward: bool) -> Result { let id = u32_param(p, "id").ok_or_else(|| bad("arrange", "missing `id`"))?; let kind = str_param(p, "kind").unwrap_or("shape").to_string(); let extreme = bool_param(p, "toFront").or(bool_param(p, "toBack")).unwrap_or(false); - let si = s.doc()?.wb.active_sheet; + let si = s.doc()?.wb.active_sheet(); fn reorder(v: &mut Vec, i: usize, forward: bool, extreme: bool) { if i >= v.len() { return; @@ -630,7 +638,7 @@ fn align(s: &mut Session, p: &Json) -> Result { let ids: Vec = p.get("ids").and_then(Json::as_array).map(|a| a.iter().filter_map(|v| v.as_u64().map(|x| x as u32)).collect()).unwrap_or_default(); let how = str_param(p, "align").unwrap_or("left").to_string(); - let si = s.doc()?.wb.active_sheet; + let si = s.doc()?.wb.active_sheet(); edit(s, |cx| { let sheet = cx.sheet_mut(si)?; let geo: Vec<(u32, f64, f64, f32, f32)> = { @@ -743,7 +751,7 @@ fn custom_views(s: &mut Session, p: &Json) -> Result { let mut views = stash_map(&d.wb, VIEWS_NAME); if let Some(name) = str_param(p, "save") { let sh = d.wb.active().ok_or(EngineError::NoDocument)?; - views[name] = json!({"sheet": d.wb.active_sheet, "selection": d.selection.a1(), "zoom": sh.zoom, "freeze": sh.freeze}); + views[name] = json!({"sheet": d.wb.active_sheet(), "selection": d.selection.a1(), "zoom": sh.zoom, "freeze": sh.freeze}); let v = views.clone(); edit(s, |cx| { stash_set(&mut cx.wb, VIEWS_NAME, &v); @@ -802,6 +810,25 @@ pub(crate) fn window_param(s: &Session, cmd: &str, p: &Json) -> Result { s.target_window(t).map_err(|e| bad(cmd, e.to_string())) } +/// View › Arrange All. The engine has no window geometry: it picks the windows and the layout, +/// and the interface moves its native windows ([`Session::window_list`] order, active first). +fn arrange_all(s: &mut Session, p: &Json) -> Result { + let arrange = match str_param(p, "arrange").unwrap_or("tiled") { + a @ ("tiled" | "horizontal" | "vertical" | "cascade") => a, + other => return Err(bad("view.arrangeAll", format!("`arrange` must be tiled, horizontal, vertical or cascade, not `{other}`"))), + }; + let active = s.active_window(); + let doc = active.and_then(|w| s.window(w)).map(|w| w.doc_uid); + let only_active = p.get("activeWorkbook").and_then(Json::as_bool).unwrap_or(false); + let mut windows: Vec = s.windows().iter().filter(|w| !w.hidden && (!only_active || Some(w.doc_uid) == doc)).map(|w| w.id).collect(); + if let Some(at) = windows.iter().position(|w| Some(*w) == active) + && let Some(first) = windows.get_mut(..=at) + { + first.rotate_right(1); + } + Ok(json!({"arrange": arrange, "windows": windows})) +} + fn hide_window(s: &mut Session, p: &Json) -> Result { let id = window_param(s, "view.hideWindow", p)?; s.hide_window(id); @@ -835,7 +862,7 @@ fn switch_windows(s: &mut Session, p: &Json) -> Result { fn table_resize(s: &mut Session, p: &Json) -> Result { let r = str_param(p, "range").and_then(RangeRef::parse).ok_or_else(|| bad("table.resize", "missing `range`"))?; let d = s.doc()?; - let si = d.wb.active_sheet; + let si = d.wb.active_sheet(); let sh = d.wb.sheet(si).ok_or(EngineError::NoDocument)?; let ti = match str_param(p, "table") { Some(n) => sh.tables.iter().position(|t| t.name.eq_ignore_ascii_case(n)), @@ -944,7 +971,7 @@ fn from_table_range(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; let sh = d.wb.active().ok_or(EngineError::NoDocument)?; let r = if r.is_single() { crate::selection::current_region(sh, r.start) } else { r }; - let text = crate::display::range_text(&d.wb, d.wb.active_sheet, r); + let text = crate::display::range_text(&d.wb, d.wb.active_sheet(), r); from_csv(s, &json!({"text": text, "delimiter": "\t", "sheet": format!("{} (2)", sh.name).chars().take(31).collect::()})) } @@ -1117,7 +1144,7 @@ fn theme_fonts(s: &mut Session, p: &Json) -> Result { fn background(s: &mut Session, p: &Json) -> Result { if bool_param(p, "clear").unwrap_or(false) { - let si = s.doc()?.wb.active_sheet; + let si = s.doc()?.wb.active_sheet(); return edit(s, |cx| { cx.sheet_mut(si)?.images.retain(|i| i.alt != "Sheet background"); Ok(Json::Null) @@ -1210,7 +1237,7 @@ fn watch_window(s: &mut Session, p: &Json) -> Result { .iter() .map(|w| { let (sheet, body) = split_sheet(w); - let si = sheet.and_then(|n| d.wb.sheet_index(&n)).unwrap_or(d.wb.active_sheet); + let si = sheet.and_then(|n| d.wb.sheet_index(&n)).unwrap_or(d.wb.active_sheet()); let c = CellRef::parse(body).unwrap_or_default(); let sh = d.wb.sheet(si); json!({"cell": w, "value": sh.map(|s| s.value(c).display()), "formula": sh.and_then(|s| s.cell(c)).and_then(|x| x.formula.as_ref()).map(|f| format!("={}", f.text))}) diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 1f50b908bc..f704714ccd 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -185,9 +185,9 @@ fn close(s: &mut Session, p: &Json) -> Result { fn export(s: &mut Session, p: &Json, ext: &str) -> Result { let path = str_param(p, "path").ok_or_else(|| bad("file.export", "missing `path`"))?.to_string(); let d = s.doc()?; - let mut wb = (*d.wb).clone(); + let wb = (*d.wb).clone(); if p.get("sheet").is_some() { - wb.active_sheet = target_sheet(s, p)?; + wb.set_active_sheet(target_sheet(s, p)?); } let target = if path.ends_with(&format!(".{ext}")) { path.clone() } else { format!("{path}.{ext}") }; let bytes = crate::io::save_bytes(&wb, &target)?; diff --git a/crates/engine/src/cmd/forecast.rs b/crates/engine/src/cmd/forecast.rs index 139d6fc461..b7472fd22c 100644 --- a/crates/engine/src/cmd/forecast.rs +++ b/crates/engine/src/cmd/forecast.rs @@ -68,7 +68,7 @@ fn range_on(s: &Session, t: &str) -> Result<(usize, RangeRef)> { let (sh, body) = split_sheet(t); let si = match sh { Some(n) => d.wb.sheet_index(&n).ok_or_else(|| bad(CMD, format!("no sheet named `{n}`")))?, - None => d.wb.active_sheet, + None => d.wb.active_sheet(), }; let r = RangeRef::parse(body.trim()).ok_or_else(|| bad(CMD, format!("not a range: `{t}`")))?; let r = if r.is_full_cols() || r.is_full_rows() { @@ -101,7 +101,7 @@ fn source(s: &Session, p: &Json) -> Result<(usize, Vec, Vec)> None => { let sh = d.wb.active().ok_or(EngineError::NoDocument)?; let sel = d.selection.current(); - (d.wb.active_sheet, if sel.is_single() { current_region(sh, d.selection.active) } else { sel }) + (d.wb.active_sheet(), if sel.is_single() { current_region(sh, d.selection.active) } else { sel }) } }; if r.width() < 2 { @@ -307,7 +307,7 @@ fn forecast_sheet(s: &mut Session, p: &Json) -> Result { let stats_col = width + 1; let chart_col = if opts.statistics { stats_col + 3 } else { width + 1 }; - let at = s.doc()?.wb.active_sheet; + let at = s.doc()?.wb.active_sheet(); let index = edit(s, |cx| { let at = at.min(cx.wb.sheets.len()); let bold = cx.wb.styles.derive(StyleId::DEFAULT, |st| st.font.bold = true); @@ -440,7 +440,7 @@ fn forecast_sheet(s: &mut Session, p: &Json) -> Result { *sc += 1; } } - cx.wb.active_sheet = at; + cx.wb.set_active_sheet(at); *cx.sel = crate::Selection::at(CellRef::new(0, 0)); cx.structural = true; Ok(at) @@ -502,7 +502,7 @@ mod tests { assert_eq!(r["periods"], 4); let wb = &s.doc().unwrap().wb; // Inserted before the data sheet and activated. - assert_eq!(wb.active_sheet, 0); + assert_eq!(wb.active_sheet(), 0); let sh = wb.sheet(0).unwrap(); assert_eq!(sh.name, r["sheet"].as_str().unwrap()); assert_eq!(value(&s, 0, "C1"), Value::text("Forecast(Sales)")); diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index cd28cfbfa3..84bc2f3ccb 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -1011,7 +1011,7 @@ fn line_targets(s: &Session, p: &Json, rows: bool) -> Result<(usize, Vec<(u32, u return Ok((sheet, lines)); } let d = s.doc()?; - if sheet != d.wb.active_sheet { + if sheet != d.wb.active_sheet() { return Err(bad(key, format!("missing `{key}`: the selection is on the active sheet"))); } Ok((sheet, d.selection.ranges.iter().map(|r| if rows { (r.start.row, r.end.row) } else { (r.start.col, r.end.col) }).collect())) diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index 5334784fae..22db2abd0a 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -102,7 +102,7 @@ fn auto_sum(s: &mut Session, p: &Json) -> Result { (CellRef::new(out_row, c), format!("={func}({})", RangeRef::new(CellRef::new(sel.start.row, c), CellRef::new(data_end_row, c)).a1())) }) .collect(); - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); edit(s, |cx| { for (c, f) in &writes { let cell = super::edit::input_to_cell(f, sheet, *c, &mut cx.wb)?.unwrap_or_default(); @@ -400,7 +400,7 @@ fn name_manager(s: &mut Session, _: &Json) -> Result { .iter() .filter(|n| !n.hidden) .map(|n| { - let sheet = n.scope.unwrap_or(d.wb.active_sheet); + let sheet = n.scope.unwrap_or(d.wb.active_sheet()); // parse_name, not evaluate's parse: a name's top-level commas are unions. let v = gridcraft_formula::parse_name(&n.formula) .map_or(Value::Error(CellError::Name), |e| gridcraft_calc::recalc::evaluate_expr(&d.wb, sheet, CellRef::default(), &e)); @@ -477,7 +477,7 @@ fn create_from_selection(s: &mut Session, p: &Json) -> Result { fn trace_precedents(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; let at = cell_param(s, p, "cell")?.unwrap_or(d.selection.active); - let areas = d.calc.graph.precedents_of((d.wb.active_sheet, at)); + let areas = d.calc.graph.precedents_of((d.wb.active_sheet(), at)); let v: Vec = areas.iter().map(|a| json!({"sheet": d.wb.sheet(a.sheet).map(|s| s.name.clone()), "range": a.range.a1()})).collect(); Ok(Json::Array(v)) } @@ -486,7 +486,7 @@ fn trace_dependents(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; let at = cell_param(s, p, "cell")?.unwrap_or(d.selection.active); let mut deps = Vec::new(); - d.calc.graph.dependents(d.wb.active_sheet, at, &mut deps); + d.calc.graph.dependents(d.wb.active_sheet(), at, &mut deps); deps.sort(); deps.dedup(); let v: Vec = deps.iter().map(|(si, c)| json!({"sheet": d.wb.sheet(*si).map(|s| s.name.clone()), "cell": c.a1()})).collect(); @@ -499,7 +499,7 @@ fn select_trace_cells(s: &mut Session, p: &Json, precedents: bool) -> Result = BTreeSet::new(); @@ -570,7 +570,7 @@ fn select_dependents(s: &mut Session, p: &Json) -> Result { } fn show_formulas(s: &mut Session, p: &Json) -> Result { - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); let cur = s.doc()?.wb.sheet(sheet).is_some_and(|sh| sh.show_formulas); let on = bool_param(p, "on").unwrap_or(!cur); edit(s, |cx| { @@ -636,7 +636,7 @@ fn error_checking(s: &mut Session, _: &Json) -> Result { fn evaluate_formula(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; let at = cell_param(s, p, "cell")?.unwrap_or(d.selection.active); - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); let text = match str_param(p, "formula") { Some(f) => f.trim_start_matches('=').to_string(), None => { @@ -675,7 +675,7 @@ fn evaluate(s: &mut Session, p: &Json) -> Result { let at = cell_param(s, p, "cell")?.unwrap_or(d.selection.active); let expr = crate::cmd::edit::parse_typed_formula(f.strip_prefix('=').unwrap_or(f))?; crate::cmd::edit::check_arg_counts(&expr)?; - let v = gridcraft_calc::recalc::evaluate_expr(&d.wb, d.wb.active_sheet, at, &expr); + let v = gridcraft_calc::recalc::evaluate_expr(&d.wb, d.wb.active_sheet(), at, &expr); Ok(crate::cmd::inspect::value_json(&v)) } diff --git a/crates/engine/src/cmd/insert.rs b/crates/engine/src/cmd/insert.rs index aae5a9faa1..08369a773a 100644 --- a/crates/engine/src/cmd/insert.rs +++ b/crates/engine/src/cmd/insert.rs @@ -356,7 +356,7 @@ fn find_table(s: &Session, p: &Json) -> Result<(usize, usize)> { if let Some(n) = str_param(p, "table") { return d.wb.table(n).ok_or_else(|| bad("table", format!("no table named `{n}`"))); } - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); let sh = d.wb.sheet(sheet).ok_or(EngineError::NoDocument)?; let ti = sh .tables @@ -819,7 +819,7 @@ fn insert_chart(s: &mut Session, p: &Json) -> Result { fn find_chart(s: &Session, p: &Json) -> Result<(usize, usize)> { let d = s.doc()?; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); let sh = d.wb.sheet(sheet).ok_or(EngineError::NoDocument)?; let idx = match u32_param(p, "chart") { Some(id) => sh.charts.iter().position(|c| c.id == id), @@ -968,7 +968,7 @@ fn insert_sparkline(s: &mut Session, p: &Json) -> Result { _ => SparklineKind::Line, }; let markers = bool_param(p, "markers").unwrap_or(false); - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { let sh = cx.sheet_mut(sheet)?; sh.sparklines.retain(|x| x.cell != loc); @@ -993,7 +993,7 @@ fn insert_picture(s: &mut Session, p: &Json) -> Result { if placement == "cell" { let mime = validate_cell_picture(&data)?; let d = s.doc()?; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); let sh = d.wb.active().ok_or(EngineError::NoDocument)?; let at = match str_param(p, "at") { Some(a) => CellRef::parse(a).ok_or_else(|| bad("insert.picture", "invalid cell address"))?, @@ -1037,7 +1037,7 @@ fn insert_picture(s: &mut Session, p: &Json) -> Result { mime, alt: str_param(p, "alt").unwrap_or("").into(), }; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); edit(s, |cx| { cx.sheet_mut(sheet)?.images.push(img); Ok(json!({"image": id})) @@ -1107,7 +1107,7 @@ fn insert_shape(s: &mut Session, p: &Json) -> Result { text: str_param(p, "text").unwrap_or("").into(), format: Default::default(), }; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); edit(s, |cx| { cx.sheet_mut(sheet)?.shapes.push(shape); Ok(json!({"shape": id})) @@ -1125,7 +1125,7 @@ fn shape_set_text(s: &mut Session, p: &Json) -> Result { return Err(bad("shape.setText", "`text` is longer than 32,767 characters")); } let d = s.doc()?; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); let sh = d.wb.active().ok_or_else(|| bad("shape.setText", "no such text box"))?; let shape = sh.shapes.iter().find(|shape| shape.id == id).ok_or_else(|| bad("shape.setText", "no such text box"))?; if shape.kind != ShapeKind::TextBox { @@ -1148,7 +1148,7 @@ fn shape_set_text(s: &mut Session, p: &Json) -> Result { fn delete_object(s: &mut Session, p: &Json) -> Result { let id = u32_param(p, "id").ok_or_else(|| bad("object.delete", "missing `id`"))?; let kind = str_param(p, "kind").unwrap_or("").to_string(); - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { let sh = cx.sheet_mut(sheet)?; match kind.as_str() { @@ -1164,7 +1164,7 @@ fn delete_object(s: &mut Session, p: &Json) -> Result { fn move_object(s: &mut Session, p: &Json) -> Result { let id = u32_param(p, "id").ok_or_else(|| bad("object.move", "missing `id`"))?; let kind = str_param(p, "kind").unwrap_or("").to_string(); - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); let at = cell_param(s, p, "at")?; edit(s, |cx| { let sh = cx.sheet_mut(sheet)?; @@ -1205,7 +1205,7 @@ fn set_anchor_mode(s: &mut Session, p: &Json) -> Result { return Err(bad("object.setAnchorMode", "kind must be chart, image or shape")); } let mode = anchor_mode_param(p).ok_or_else(|| bad("object.setAnchorMode", "mode must be moveAndSize, moveOnly or absolute"))?; - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { let sh = cx.sheet_mut(sheet)?; let anchor = match kind.as_str() { @@ -1703,7 +1703,7 @@ fn insert_icon(s: &mut Session, p: &Json) -> Result { text: name, format: Default::default(), }; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); edit(s, |cx| { cx.sheet_mut(sheet)?.shapes.push(shape); Ok(json!({"shape": id})) diff --git a/crates/engine/src/cmd/inspect.rs b/crates/engine/src/cmd/inspect.rs index f373de4897..c322f4388b 100644 --- a/crates/engine/src/cmd/inspect.rs +++ b/crates/engine/src/cmd/inspect.rs @@ -63,7 +63,7 @@ fn inspect(s: &mut Session, _: &Json) -> Result { "title": d.display_title(), "path": d.path, "dirty": d.is_dirty(), - "activeSheet": wb.active_sheet, + "activeSheet": wb.active_sheet(), "sheets": sheets, "selection": d.selection.a1(), "activeCell": d.selection.active.a1(), diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 1f15ddfb8b..3d1b404d0f 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -271,7 +271,7 @@ pub(crate) fn target_sheet(s: &Session, p: &Json) -> Result { { return d.wb.sheet_index(&sh).ok_or_else(|| bad("range", format!("no sheet named `{sh}`"))); } - Ok(d.wb.active_sheet) + Ok(d.wb.active_sheet()) } pub(crate) use gridcraft_core::split_sheet; @@ -280,7 +280,7 @@ pub(crate) use gridcraft_core::split_sheet; /// a cell reference, or that names a sheet other than the active one, is an error rather than a /// silent fallback to some other cell. pub(crate) fn cell_param(s: &Session, p: &Json, key: &str) -> Result> { - cell_param_on(s, p, key, s.doc()?.wb.active_sheet) + cell_param_on(s, p, key, s.doc()?.wb.active_sheet()) } /// Like [`cell_param`], for a command that operates on `sheet` (a `Sheet!` prefix must name it). @@ -562,7 +562,7 @@ pub fn auto_row_height(wb: &mut Workbook, si: usize, row: u32) { impl Ctx<'_> { pub fn sheet_index(&self) -> usize { - self.wb.active_sheet + self.wb.active_sheet() } pub fn sheet_mut(&mut self, i: usize) -> Result<&mut gridcraft_model::Sheet> { self.wb.sheet_mut(i).ok_or_else(|| EngineError::Other("no such sheet".into())) diff --git a/crates/engine/src/cmd/pivot.rs b/crates/engine/src/cmd/pivot.rs index 43c3aa4b5e..3d3c28b440 100644 --- a/crates/engine/src/cmd/pivot.rs +++ b/crates/engine/src/cmd/pivot.rs @@ -206,7 +206,7 @@ fn other(msg: impl Into) -> EngineError { /// (sheet, index) of the pivot a call targets. fn find_pivot(s: &Session, p: &Json) -> Result<(usize, usize)> { let d = s.doc()?; - let active = d.wb.active_sheet; + let active = d.wb.active_sheet(); if let Some(v) = p.get("pivot").filter(|v| !v.is_null()) { let mut order: Vec = vec![active]; order.extend((0..d.wb.sheets.len()).filter(|i| *i != active)); @@ -616,7 +616,7 @@ fn insert_pivot(s: &mut Session, p: &Json) -> Result { return Err(other("The workbook is protected and cannot be changed.")); } let source = source_param(s, p)?; - let active = s.doc()?.wb.active_sheet; + let active = s.doc()?.wb.active_sheet(); // Validate the source now (header names, size). let src = pv::read_source(&s.doc()?.wb, &source, active).map_err(other)?; let name = match str_param(p, "name") { @@ -654,7 +654,7 @@ fn insert_pivot(s: &mut Session, p: &Json) -> Result { let (si, anchor) = match target { Some(t) => t, None => { - let at = cx.wb.active_sheet.min(cx.wb.sheets.len()); + let at = cx.wb.active_sheet().min(cx.wb.sheets.len()); let nm = cx.wb.next_sheet_name(); cx.wb.sheets.insert(at, Arc::new(Sheet::new(nm))); for n in cx.wb.names.iter_mut() { @@ -704,7 +704,7 @@ fn insert_pivot(s: &mut Session, p: &Json) -> Result { sh.pivots.push(pt); let pi = sh.pivots.len() - 1; let area = pv::refresh(&mut cx.wb, si, pi, replace).map_err(other)?; - cx.wb.active_sheet = si; + cx.wb.set_active_sheet(si); *cx.sel = crate::Selection::at(anchor); cx.structural = true; let sheet_name = cx.wb.sheet(si).map(|s| s.name.clone()).unwrap_or_default(); @@ -1395,7 +1395,7 @@ fn chart_pivot(s: &Session, p: &Json) -> Result<(usize, usize)> { let sh = gridcraft_core::unquote_sheet(sh); (d.wb.sheet_index(&sh), a) } - None => (Some(d.wb.active_sheet), t), + None => (Some(d.wb.active_sheet()), t), }; if let (Some(si), Some(c)) = (sheet, CellRef::parse(addr.trim().replace('$', "").as_str())) { let hit = d.wb.sheet(si).and_then(|sh| sh.pivots.iter().position(|pt| pt.last_range.is_some_and(|r| r.contains(c)) || pt.anchor == c)); @@ -1529,7 +1529,7 @@ fn suggestions(wb: &Workbook, src: &pv::Source) -> Vec { fn recommended(s: &mut Session, p: &Json) -> Result { let source = source_param(s, p)?; - let active = s.doc()?.wb.active_sheet; + let active = s.doc()?.wb.active_sheet(); let src = pv::read_source(&s.doc()?.wb, &source, active).map_err(other)?; let list = suggestions(&s.doc()?.wb, &src); let Some(apply) = p.get("apply").and_then(Json::as_u64) else { @@ -1612,7 +1612,7 @@ mod tests { assert_eq!(r["fields"], json!(["Region", "Product", "Date", "Units", "Sales"])); let d = s.doc().unwrap(); assert_eq!(d.wb.sheets.len(), 2); - assert_eq!(d.wb.active_sheet, 0, "new sheet goes before the current one"); + assert_eq!(d.wb.active_sheet(), 0, "new sheet goes before the current one"); assert_eq!(d.wb.active().unwrap().name, "Sheet2"); assert!(text(&s, "A3").starts_with("PivotTable1")); assert_eq!(pt(&s).source, "Sheet1!$A$1:$E$13"); @@ -1993,7 +1993,7 @@ mod tests { } fn plotted(s: &Session) -> Vec>> { let d = s.doc().unwrap(); - let data = gridcraft_chart::resolve(&d.wb, d.wb.active_sheet, &pivot_chart(s)); + let data = gridcraft_chart::resolve(&d.wb, d.wb.active_sheet(), &pivot_chart(s)); data.series.into_iter().map(|x| x.values).collect() } diff --git a/crates/engine/src/cmd/print.rs b/crates/engine/src/cmd/print.rs index 4c80f2fa90..35225cbe96 100644 --- a/crates/engine/src/cmd/print.rs +++ b/crates/engine/src/cmd/print.rs @@ -370,13 +370,13 @@ pub fn layouts(wb: &Workbook, opts: &PrintOptions) -> Vec { let sheets: Vec = if opts.all_sheets { (0..wb.sheets.len()).filter(|i| wb.sheet(*i).is_some_and(|s| s.visibility == Visibility::Visible)).collect() } else { - vec![wb.active_sheet] + vec![wb.active_sheet()] }; for si in sheets { if pages >= MAX_PAGES { break; } - let ovr = if si == wb.active_sheet { opts.range } else { None }; + let ovr = if si == wb.active_sheet() { opts.range } else { None }; if opts.all_sheets && ovr.is_none() && wb.sheet(si).is_some_and(|s| print_range(s).is_none()) { continue; } @@ -387,7 +387,7 @@ pub fn layouts(wb: &Workbook, opts: &PrintOptions) -> Vec { } } if out.is_empty() && opts.all_sheets { - out.extend(layout_sheet(wb, wb.active_sheet, opts, None)); + out.extend(layout_sheet(wb, wb.active_sheet(), opts, None)); } out } diff --git a/crates/engine/src/cmd/review.rs b/crates/engine/src/cmd/review.rs index 5464abba50..9811fde9e7 100644 --- a/crates/engine/src/cmd/review.rs +++ b/crates/engine/src/cmd/review.rs @@ -90,7 +90,7 @@ fn has_comments(s: &Session) -> std::result::Result<(), String> { fn step_comment(s: &mut Session, forward: bool) -> Result { let list = comment_cells(s); let d = s.doc()?; - let here = (d.wb.active_sheet, d.selection.active); + let here = (d.wb.active_sheet(), d.selection.active); let target = if forward { list.iter().find(|c| **c > here).or_else(|| list.first()) } else { @@ -374,7 +374,7 @@ mod tests { fn at(s: &Session) -> (usize, String) { let d = s.doc().unwrap(); - (d.wb.active_sheet, d.selection.active.a1()) + (d.wb.active_sheet(), d.selection.active.a1()) } #[test] @@ -443,7 +443,7 @@ mod tests { ); let prot: SheetProtection = serde_json::from_str(&old).unwrap(); assert_eq!((prot.legacy_password_hash, prot.password.is_none()), (Some(password_hash("old")), true)); - let i = s.doc().unwrap().wb.active_sheet; + let i = s.doc().unwrap().wb.active_sheet(); let wb = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb); std::sync::Arc::make_mut(&mut wb.sheets[i]).protection = Some(prot); assert!(s.execute("review.unprotectSheet", json!({"password": "new"})).is_err()); diff --git a/crates/engine/src/cmd/sheet.rs b/crates/engine/src/cmd/sheet.rs index 555c2fb796..e68bf60708 100644 --- a/crates/engine/src/cmd/sheet.rs +++ b/crates/engine/src/cmd/sheet.rs @@ -687,7 +687,7 @@ fn insert_sheet(s: &mut Session, p: &Json) -> Result { let before = p.get("before").and_then(Json::as_u64).map(|v| v as usize); let idx = edit_doc(d, |cx| { // Excel inserts before the active sheet; the "+" button appends after it. - let at = before.unwrap_or(cx.wb.active_sheet + 1).min(cx.wb.sheets.len()); + let at = before.unwrap_or(cx.wb.active_sheet() + 1).min(cx.wb.sheets.len()); cx.wb.sheets.insert(at, Arc::new(Sheet::new(name.clone()))); // Names scoped to later sheets shift. for n in cx.wb.names.iter_mut() { @@ -697,7 +697,7 @@ fn insert_sheet(s: &mut Session, p: &Json) -> Result { *sc += 1; } } - cx.wb.active_sheet = at; + cx.wb.set_active_sheet(at); *cx.sel = crate::Selection::default(); cx.structural = true; Ok(at) @@ -785,8 +785,8 @@ fn delete_sheet(s: &mut Session, p: &Json) -> Result { } } } - if cx.wb.active_sheet >= cx.wb.sheets.len() || cx.wb.active_sheet > i { - cx.wb.active_sheet = cx.wb.active_sheet.saturating_sub(1).min(cx.wb.sheets.len().saturating_sub(1)); + if cx.wb.active_sheet() >= cx.wb.sheets.len() || cx.wb.active_sheet() > i { + cx.wb.set_active_sheet(cx.wb.active_sheet().saturating_sub(1).min(cx.wb.sheets.len().saturating_sub(1))); } *cx.sel = crate::Selection::default(); cx.structural = true; @@ -900,12 +900,12 @@ fn move_sheet(s: &mut Session, p: &Json) -> Result { } cx.wb.names.extend(copies); cx.wb.sheets.insert(at, Arc::new(sh)); - cx.wb.active_sheet = at; + cx.wb.set_active_sheet(at); } else { let sh = cx.wb.sheets.remove(i); let at = to.min(n - 1); cx.wb.sheets.insert(at, sh); - cx.wb.active_sheet = at; + cx.wb.set_active_sheet(at); // Sheet-level names follow their sheet. for nm in cx.wb.names.iter_mut() { if let Some(sc) = nm.scope.as_mut() { @@ -942,8 +942,8 @@ fn hide_sheet(s: &mut Session, p: &Json) -> Result { } edit(s, |cx| { cx.sheet_mut(i)?.visibility = Visibility::Hidden; - if cx.wb.active_sheet == i { - cx.wb.active_sheet = cx.wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0); + if cx.wb.active_sheet() == i { + cx.wb.set_active_sheet(cx.wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0)); } Ok(Json::Null) }) @@ -964,7 +964,7 @@ fn unhide_sheet(s: &mut Session, p: &Json) -> Result { let i = target_sheet(s, p)?; edit(s, |cx| { cx.sheet_mut(i)?.visibility = Visibility::Visible; - cx.wb.active_sheet = i; + cx.wb.set_active_sheet(i); Ok(Json::Null) }) } @@ -978,7 +978,7 @@ fn activate(s: &mut Session, p: &Json) -> Result { fn step_sheet(s: &mut Session, dir: isize) -> Result { let d = s.doc()?; - let mut i = d.wb.active_sheet; + let mut i = d.wb.active_sheet(); while let Some(next) = i.checked_add_signed(dir) { let Some(sheet) = d.wb.sheet(next) else { break }; if sheet.visibility == Visibility::Visible { diff --git a/crates/engine/src/cmd/slicer.rs b/crates/engine/src/cmd/slicer.rs index cb72ded71c..10e3cf13c9 100644 --- a/crates/engine/src/cmd/slicer.rs +++ b/crates/engine/src/cmd/slicer.rs @@ -165,7 +165,7 @@ fn slicer_toggle(s: &mut Session, p: &Json) -> Result { let (idx, id) = find_slicer(s, p)?; let value = str_param(p, "value").ok_or_else(|| bad("slicer.toggle", "missing `value`"))?.to_string(); let multi = bool_param(p, "multi").unwrap_or(false); - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); let (table, column, current) = { let d = s.doc()?; let sl = d.wb.active().and_then(|sh| sh.slicers.get(idx)).ok_or_else(|| bad("slicer.toggle", "no slicer"))?; @@ -204,7 +204,7 @@ fn slicer_toggle(s: &mut Session, p: &Json) -> Result { fn slicer_select_all(s: &mut Session, p: &Json) -> Result { let (idx, id) = find_slicer(s, p)?; - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); edit(s, |cx| { if let Some(sl) = cx.sheet_mut(sheet)?.slicers.get_mut(idx) { sl.selected = None; @@ -217,7 +217,7 @@ fn slicer_select_all(s: &mut Session, p: &Json) -> Result { fn slicer_delete(s: &mut Session, p: &Json) -> Result { let (idx, id) = find_slicer(s, p)?; - let sheet = s.doc()?.wb.active_sheet; + let sheet = s.doc()?.wb.active_sheet(); // Absolute column of the slicer, so its filter criterion can be removed too. let abs_col = s.doc().ok().and_then(|d| { let sh = d.wb.active()?; diff --git a/crates/engine/src/cmd/solver.rs b/crates/engine/src/cmd/solver.rs index ffd426f5f9..bf78b4d4c7 100644 --- a/crates/engine/src/cmd/solver.rs +++ b/crates/engine/src/cmd/solver.rs @@ -299,7 +299,7 @@ fn parse_model(s: &Session, p: &Json) -> Result { let d = s.doc()?; let wb = &d.wb; let obj_text = str_param(p, "objective").or_else(|| str_param(p, "setObjective")).or_else(|| str_param(p, "set")).unwrap_or(""); - let (sheet, obj) = cells_of(wb, obj_text, wb.active_sheet, 1).map_err(|_| EngineError::Other("The objective must be a single cell.".into()))?; + let (sheet, obj) = cells_of(wb, obj_text, wb.active_sheet(), 1).map_err(|_| EngineError::Other("The objective must be a single cell.".into()))?; let objective = *obj.first().ok_or_else(|| EngineError::Other("The objective must be a single cell.".into()))?; let sh = wb.sheet(sheet).ok_or(EngineError::NoDocument)?; if sh.cell(objective).is_none_or(|c| c.formula.is_none()) { @@ -1913,8 +1913,8 @@ fn write_reports(cx: &mut Ctx, at: usize, built: Vec<(String, Lines)>) -> Vec= at { - cx.wb.active_sheet += count; + if cx.wb.active_sheet() >= at { + cx.wb.set_active_sheet(cx.wb.active_sheet().saturating_add(count)); } cx.structural = true; names @@ -1939,7 +1939,7 @@ fn report(s: &mut Session, p: &Json) -> Result { let params = match p.get("model") { Some(m @ Json::Object(_)) => m.clone(), Some(Json::Null) | None => { - let mut params = saved_model(&d.wb, d.wb.active_sheet).unwrap_or_else(|| json!({})); + let mut params = saved_model(&d.wb, d.wb.active_sheet()).unwrap_or_else(|| json!({})); if let (Some(base), Some(over)) = (params.as_object_mut(), p.as_object()) { for (k, v) in over { base.insert(k.clone(), v.clone()); @@ -1988,7 +1988,7 @@ fn report(s: &mut Session, p: &Json) -> Result { fn run(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; - let active = d.wb.active_sheet; + let active = d.wb.active_sheet(); let mut params = p.clone(); if bool_param(p, "saved").unwrap_or(false) { let saved = saved_model(&d.wb, active).ok_or_else(|| EngineError::Other("This sheet has no saved Solver model.".into()))?; @@ -2145,7 +2145,7 @@ mod tests { /// The first saved-model name scoped to the active sheet. fn saved(s: &Session) -> String { let d = s.doc().unwrap(); - d.wb.names.iter().find(|n| n.scope == Some(d.wb.active_sheet)).map(|n| n.name.clone()).unwrap_or_default() + d.wb.names.iter().find(|n| n.scope == Some(d.wb.active_sheet())).map(|n| n.name.clone()).unwrap_or_default() } fn set(s: &mut Session, cells: &[(&str, &str)]) { for (c, v) in cells { diff --git a/crates/engine/src/cmd/view.rs b/crates/engine/src/cmd/view.rs index f0b2f77d39..e79d79630c 100644 --- a/crates/engine/src/cmd/view.rs +++ b/crates/engine/src/cmd/view.rs @@ -139,7 +139,7 @@ fn freeze_panes(s: &mut Session, p: &Json) -> Result { } let at = match cell_param_on(s, p, "cell", si)? { Some(c) => c, - None if si == d.wb.active_sheet => d.selection.active, + None if si == d.wb.active_sheet() => d.selection.active, // The selection belongs to the active sheet. None => return Err(bad("view.freezePanes", "missing `cell`: the selection is on the active sheet")), }; @@ -190,7 +190,7 @@ fn zoom(s: &mut Session, p: &Json) -> Result { let d = s.doc_mut()?; let dirty = d.is_dirty(); let wb = std::sync::Arc::make_mut(&mut d.wb); - let si = wb.active_sheet; + let si = wb.active_sheet(); if let Some(sh) = wb.sheet_mut(si) { sh.zoom = z; } diff --git a/crates/engine/src/cmd/whatif.rs b/crates/engine/src/cmd/whatif.rs index cec845b7a6..53b0dc3cc2 100644 --- a/crates/engine/src/cmd/whatif.rs +++ b/crates/engine/src/cmd/whatif.rs @@ -101,7 +101,7 @@ fn cell_arg(s: &Session, p: &Json, key: &str, cmd: &str) -> Result<(usize, CellR let d = s.doc()?; let t = str_param(p, key).ok_or_else(|| bad(cmd, format!("missing `{key}`")))?; let (sh, body) = split_sheet(t); - let si = sheet_named(&d.wb, sh, d.wb.active_sheet, cmd)?; + let si = sheet_named(&d.wb, sh, d.wb.active_sheet(), cmd)?; let c = CellRef::parse(body.trim()).ok_or_else(|| bad(cmd, format!("`{key}` is not a cell: `{t}`")))?; Ok((si, c)) } @@ -110,7 +110,7 @@ fn cell_arg(s: &Session, p: &Json, key: &str, cmd: &str) -> Result<(usize, CellR /// are trimmed to the used range. pub(crate) fn parse_range(wb: &Workbook, t: &str, cmd: &str) -> Result<(usize, RangeRef)> { let (sh, body) = split_sheet(t); - let si = sheet_named(wb, sh, wb.active_sheet, cmd)?; + let si = sheet_named(wb, sh, wb.active_sheet(), cmd)?; let r = RangeRef::parse(body.trim()).ok_or_else(|| bad(cmd, format!("not a range: `{t}`")))?; Ok((si, trim_to_used(wb, si, r))) } @@ -152,7 +152,7 @@ fn cell_list(wb: &Workbook, t: &str, cmd: &str, cap: usize) -> Result<(usize, Ve return Err(bad(cmd, format!("too many cells (at most {cap})"))); } } - Ok((sheet.unwrap_or(wb.active_sheet), out)) + Ok((sheet.unwrap_or(wb.active_sheet()), out)) } /// What a JSON value typed into a cell is, as a constant. @@ -525,7 +525,7 @@ fn scenario_delete(s: &mut Session, p: &Json) -> Result { fn scenario_summary(s: &mut Session, p: &Json) -> Result { const CMD: &str = "data.scenarioSummary"; let d = s.doc()?; - let si = d.wb.active_sheet; + let si = d.wb.active_sheet(); let sh = d.wb.sheet(si).ok_or(EngineError::NoDocument)?; let scenarios: Vec = load_scenarios(&d.wb).into_iter().filter(|x| x.sheet.eq_ignore_ascii_case(&sh.name)).collect(); if scenarios.is_empty() { @@ -799,7 +799,7 @@ fn advanced_filter(s: &mut Session, p: &Json) -> Result { Some(t) => parse_range(&d.wb, t, CMD)?, None => { let sh = d.wb.active().ok_or(EngineError::NoDocument)?; - (d.wb.active_sheet, crate::selection::current_region(sh, d.selection.active)) + (d.wb.active_sheet(), crate::selection::current_region(sh, d.selection.active)) } }; let criteria = str_param(p, "criteria").filter(|t| !t.trim().is_empty()).map(|t| parse_range(&d.wb, t, CMD)).transpose()?; @@ -1024,7 +1024,7 @@ fn consolidate(s: &mut Session, p: &Json) -> Result { let (si, r) = parse_range(&d.wb, t, CMD)?; (si, r.start) } - None => (d.wb.active_sheet, d.selection.active), + None => (d.wb.active_sheet(), d.selection.active), }; let mut row_keys: Vec = Vec::new(); let mut col_keys: Vec = Vec::new(); @@ -1146,7 +1146,7 @@ mod tests { fn v(s: &Session, a: &str) -> Value { let d = s.doc().unwrap(); let (sh, body) = super::split_sheet(a); - let si = sh.map(|n| d.wb.sheet_index(&n).unwrap()).unwrap_or(d.wb.active_sheet); + let si = sh.map(|n| d.wb.sheet_index(&n).unwrap()).unwrap_or(d.wb.active_sheet()); d.wb.sheet(si).unwrap().value(CellRef::parse(body).unwrap()) } fn n(s: &Session, a: &str) -> f64 { diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index 5c0bf800b5..ef1cc7bb57 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -234,7 +234,7 @@ pub fn save_bytes(wb: &Workbook, path: &str) -> Result> { /// Encodes a workbook as `kind`, whatever `path`'s extension says. pub fn save_bytes_as(wb: &Workbook, path: &str, kind: FileKind) -> Result> { - let sheet = wb.active_sheet; + let sheet = wb.active_sheet(); match kind { // The extension picks the package kind; only the macro-enabled kinds (.xlsm, .xltm) carry // the VBA project, so any other name saves without it, as Excel does. diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 74924d542a..f55fba080b 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -194,14 +194,14 @@ impl DocState { !Arc::ptr_eq(&self.wb, &self.saved) } pub fn sheet_index(&self) -> usize { - self.wb.active_sheet + self.wb.active_sheet() } /// Records the view state that is saved with the file (`sheetView topLeftCell`, the active /// cell): the first visible cell of the scrolling area per sheet index in `tops`, and the /// active sheet's active cell. View state never makes the workbook dirty. pub fn store_view(&mut self, tops: &[(usize, CellRef)]) { let active = self.selection.active; - let si = self.wb.active_sheet; + let si = self.wb.active_sheet(); let changed = tops.iter().any(|(i, c)| self.wb.sheets.get(*i).is_some_and(|sh| sh.view_top_left != *c)) || self.wb.sheets.get(si).is_some_and(|sh| sh.view_active != active); if !changed { diff --git a/crates/engine/src/sample.rs b/crates/engine/src/sample.rs index 129fe2171b..d5f5b81d06 100644 --- a/crates/engine/src/sample.rs +++ b/crates/engine/src/sample.rs @@ -37,7 +37,7 @@ pub fn build(name: &str) -> Option { return None; } let mut wb = (*s.doc().ok()?.wb).clone(); - wb.active_sheet = 0; + wb.set_active_sheet(0); if let Some(sh) = wb.sheet_mut(0) { sh.view_active = gridcraft_core::CellRef::default(); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 6f7f7f4bcb..73ea967430 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -188,7 +188,7 @@ fn chart_category_axis_from_blank_corner_and_select_data() { assert_eq!(series(&s), [(Some("Sheet1!$D$2:$D$4".to_string()), "Sheet1!$E$2:$E$4".to_string())]); let d = s.doc().unwrap(); let sh = d.wb.active().unwrap(); - let data = gridcraft_chart::resolve(&d.wb, d.wb.active_sheet, sh.charts.last().unwrap()); + let data = gridcraft_chart::resolve(&d.wb, d.wb.active_sheet(), sh.charts.last().unwrap()); assert_eq!(data.categories, ["2020", "2021", "2022"]); // The last series can't be removed; "" clears the labels. s.execute("chart.selectData", json!({"categories": "", "removeSeries": [0]})).unwrap(); @@ -1608,7 +1608,7 @@ fn slicer_finds_a_named_table_on_another_sheet() { s.execute("insert.table", json!({"range": "A1:B3", "name": "Sales"})).unwrap(); s.execute("home.insertSheet", json!({"name": "Report"})).unwrap(); let report = s.doc().unwrap().wb.sheet_index("Report").unwrap(); - assert_eq!(s.doc().unwrap().wb.active_sheet, report); + assert_eq!(s.doc().unwrap().wb.active_sheet(), report); // With another sheet active, `table` still names the table, and the slicer goes on the table's sheet. let r = s.execute("insert.slicer", json!({"table": "Sales", "column": "Region", "at": "D2"})).unwrap(); @@ -3843,7 +3843,7 @@ fn line_sizes_and_freeze_panes_take_a_sheet() { assert!(data.cols.get(&4).is_some_and(|c| c.hidden)); assert_eq!(data.default_col_width, 80.0); assert_eq!(sheet(&s, 0), before, "the active sheet is untouched"); - assert_eq!(s.doc().unwrap().wb.active_sheet, 0); + assert_eq!(s.doc().unwrap().wb.active_sheet(), 0); s.execute("view.freezePanes", json!({"cell": "Data!B3"})).unwrap(); assert_eq!(sheet(&s, 1).freeze, Some((2, 1))); @@ -4550,11 +4550,11 @@ fn switching_windows_swaps_their_views_without_dirtying_the_workbook() { s.execute("window.activate", json!({"window": first})).unwrap(); let d = s.doc().unwrap(); - assert_eq!((d.wb.active_sheet, d.selection.a1()), (0, "B2:C3".to_string())); + assert_eq!((d.wb.active_sheet(), d.selection.a1()), (0, "B2:C3".to_string())); assert!(!d.is_dirty(), "a window's sheet and selection are not document changes"); s.execute("view.switchWindows", json!({"window": second})).unwrap(); let d = s.doc().unwrap(); - assert_eq!((d.wb.active_sheet, d.selection.a1()), (1, "D4".to_string())); + assert_eq!((d.wb.active_sheet(), d.selection.a1()), (1, "D4".to_string())); assert!(!d.is_dirty()); // A routed command acts in its window and leaves the active one showing. @@ -4564,6 +4564,71 @@ fn switching_windows_swaps_their_views_without_dirtying_the_workbook() { assert_eq!(s.doc().unwrap().selection.a1(), "D4"); } +#[test] +fn window_swaps_share_the_workbook_and_save_records_the_latest_window_sheet() { + let mut s = s(); + s.execute("home.insertSheet", json!({})).unwrap(); + let path = std::env::temp_dir().join(format!("gridcraft-window-arc-{}.xlsx", std::process::id())); + s.execute("file.save", json!({"path": path})).unwrap(); + s.execute("sheet.activate", json!({"sheet": 0})).unwrap(); + s.execute("selection.set", json!({"range": "B2"})).unwrap(); + let first = s.active_window().unwrap(); + let second = s.execute("view.newWindow", json!({})).unwrap()["window"].as_u64().unwrap(); + s.execute("sheet.activate", json!({"sheet": 1})).unwrap(); + s.execute("selection.set", json!({"range": "C3:D4"})).unwrap(); + + // Swapping windows, as the interface does before drawing each one, never copies the workbook. + let before = s.doc().unwrap().wb.clone(); + for _ in 0..3 { + s.show_window(first); + assert_eq!((s.doc().unwrap().wb.active_sheet(), s.doc().unwrap().selection.a1()), (0, "B2".to_string())); + s.activate_window(second); + assert_eq!((s.doc().unwrap().wb.active_sheet(), s.doc().unwrap().selection.a1()), (1, "C3:D4".to_string())); + s.execute("sheet.activate", json!({"sheet": 0})).unwrap(); + s.execute("sheet.activate", json!({"sheet": 1})).unwrap(); + } + let d = s.doc().unwrap(); + assert!(std::sync::Arc::ptr_eq(&before, &d.wb), "a window swap or sheet switch copied the workbook"); + assert!(!d.is_dirty()); + + // A command run in window 2 edits window 2's sheet at its selection. + s.execute("cell.set", json!({"input": "two"})).unwrap(); + let d = s.doc().unwrap(); + assert_eq!(d.wb.sheets[1].value(CellRef::parse("C3").unwrap()), Value::text("two")); + assert_eq!(d.wb.sheets[0].value(CellRef::parse("B2").unwrap()), Value::Empty); + s.show_window(first); + assert_eq!(s.doc().unwrap().wb.active_sheet(), 0); + + // The most recently active window is window 2: the file records its sheet. + s.execute_in(crate::Target::Window(second), "file.save", json!({"path": path})).unwrap(); + let mut t = Session::new(); + t.execute("file.open", json!({"path": path})).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(t.doc().unwrap().wb.active_sheet(), 1); + s.activate_window(first); + s.execute("file.save", json!({"path": path})).unwrap(); + let mut t = Session::new(); + t.execute("file.open", json!({"path": path})).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(t.doc().unwrap().wb.active_sheet(), 0); +} + +#[test] +fn arrange_all_picks_the_visible_windows_active_first() { + let mut s = s(); + let book1 = s.active_window().unwrap(); + let book1b = s.execute("view.newWindow", json!({})).unwrap()["window"].as_u64().unwrap(); + s.execute("file.new", json!({})).unwrap(); + let book2 = s.active_window().unwrap(); + let r = s.execute("view.arrangeAll", json!({})).unwrap(); + assert_eq!(r, json!({"arrange": "tiled", "windows": [book2, book1, book1b]})); + s.execute("window.activate", json!({"window": book1b})).unwrap(); + s.execute("view.hideWindow", json!({"window": book2})).unwrap(); + let r = s.execute("view.arrangeAll", json!({"arrange": "cascade", "activeWorkbook": true})).unwrap(); + assert_eq!(r, json!({"arrange": "cascade", "windows": [book1b, book1]})); + assert!(s.execute("view.arrangeAll", json!({"arrange": "diagonal"})).is_err()); +} + #[test] fn hide_unhide_and_switch_by_window_id() { let mut s = s(); diff --git a/crates/engine/src/windows.rs b/crates/engine/src/windows.rs index 4441166db1..df43b7d320 100644 --- a/crates/engine/src/windows.rs +++ b/crates/engine/src/windows.rs @@ -2,10 +2,11 @@ //! //! Every open workbook has one or more windows (View › New Window adds another on the same //! workbook). A window has its own selection, active sheet and per-sheet selections. The active -//! window's view lives in its [`DocState`] (`selection`, `wb.active_sheet`, `sheet_selections`), -//! where every command reads it; each other window keeps its view in [`WindowState`] until it is -//! activated. Swapping a view in or out never makes the workbook dirty: per-window view state is -//! not a document change. +//! window's view lives in its [`DocState`] (`selection`, `sheet_selections`) and in the workbook's +//! active sheet ([`gridcraft_model::workbook::Workbook::active_sheet`]), where every command reads +//! it; each other window keeps its view in [`WindowState`] until it is activated. Swapping a view +//! in or out never copies the workbook or makes it dirty: the active sheet is set in place through +//! the shared `Arc`, and selections live outside the workbook. use serde_json::{Value as Json, json}; @@ -15,7 +16,7 @@ use crate::{DocState, EngineError, Result, Selection, Session}; #[derive(Clone, Debug, Default, PartialEq)] pub struct WindowView { pub selection: Selection, - pub active_sheet: usize, + pub sheet: usize, /// The active sheet's name, so a sheet inserted, deleted or moved meanwhile in another /// window still finds it. pub sheet_name: String, @@ -42,27 +43,20 @@ impl WindowView { fn capture(d: &DocState) -> WindowView { WindowView { selection: d.selection.clone(), - active_sheet: d.wb.active_sheet, - sheet_name: d.wb.sheets.get(d.wb.active_sheet).map(|s| s.name.clone()).unwrap_or_default(), + sheet: d.wb.active_sheet(), + sheet_name: d.wb.sheets.get(d.wb.active_sheet()).map(|s| s.name.clone()).unwrap_or_default(), sheet_selections: d.sheet_selections.clone(), } } fn apply(self, d: &mut DocState) { let sheets = &d.wb.sheets; - let at = if sheets.get(self.active_sheet).is_some_and(|s| s.name == self.sheet_name) { - self.active_sheet + let at = if sheets.get(self.sheet).is_some_and(|s| s.name == self.sheet_name) { + self.sheet } else { - sheets.iter().position(|s| s.name == self.sheet_name).unwrap_or(self.active_sheet) + sheets.iter().position(|s| s.name == self.sheet_name).unwrap_or(self.sheet) }; - let at = at.min(sheets.len().saturating_sub(1)); - if d.wb.active_sheet != at { - let dirty = d.is_dirty(); - std::sync::Arc::make_mut(&mut d.wb).active_sheet = at; - if !dirty { - d.saved = d.wb.clone(); - } - } + d.wb.set_active_sheet(at.min(sheets.len().saturating_sub(1))); d.selection = self.selection; d.sheet_selections = self.sheet_selections; } diff --git a/crates/engine/tests/command_sequence_fuzz.rs b/crates/engine/tests/command_sequence_fuzz.rs index 1724b9b9d2..fd06c70b3d 100644 --- a/crates/engine/tests/command_sequence_fuzz.rs +++ b/crates/engine/tests/command_sequence_fuzz.rs @@ -373,8 +373,8 @@ fn structure_problem(wb: &Workbook, sel: &gridcraft_engine::Selection) -> Option if wb.sheets.is_empty() { return Some("no sheets left".into()); } - if wb.active_sheet >= wb.sheets.len() { - return Some(format!("active sheet {} of {}", wb.active_sheet, wb.sheets.len())); + if wb.active_sheet() >= wb.sheets.len() { + return Some(format!("active sheet {} of {}", wb.active_sheet(), wb.sheets.len())); } let mut tables = BTreeSet::new(); for (i, sh) in wb.sheets.iter().enumerate() { @@ -490,7 +490,7 @@ fn watch_list(wb: &Workbook) -> Option<&str> { /// reset: undo restores view state as it was, but switching sheets isn't an undoable edit. fn canonical(wb: &Workbook) -> Workbook { let mut wb = wb.clone(); - wb.active_sheet = 0; + wb.set_active_sheet(0); wb.names.retain(|n| n.name != WATCH_NAME); for sh in wb.sheets.iter_mut() { let sh = Arc::make_mut(sh); diff --git a/crates/engine/tests/hidden_sheet_navigation.rs b/crates/engine/tests/hidden_sheet_navigation.rs index f457f4bdbf..8869d52ba8 100644 --- a/crates/engine/tests/hidden_sheet_navigation.rs +++ b/crates/engine/tests/hidden_sheet_navigation.rs @@ -25,7 +25,7 @@ fn next_stays_on_the_last_visible_sheet() { for _ in 0..2 { session.execute("sheet.next", json!({})).unwrap(); let doc = session.active().unwrap(); - assert_eq!(doc.wb.active_sheet, 1); + assert_eq!(doc.wb.active_sheet(), 1); assert_eq!(doc.selection.active.a1(), "C7"); } } @@ -36,7 +36,7 @@ fn previous_stays_on_the_first_visible_sheet() { for _ in 0..2 { session.execute("sheet.previous", json!({})).unwrap(); let doc = session.active().unwrap(); - assert_eq!(doc.wb.active_sheet, 2); + assert_eq!(doc.wb.active_sheet(), 2); assert_eq!(doc.selection.active.a1(), "C7"); } } @@ -45,10 +45,10 @@ fn previous_stays_on_the_first_visible_sheet() { fn navigation_skips_hidden_sheets_in_both_directions() { let mut session = workbook(&[1, 2], 0); session.execute("sheet.next", json!({})).unwrap(); - assert_eq!(session.active().unwrap().wb.active_sheet, 3); + assert_eq!(session.active().unwrap().wb.active_sheet(), 3); session.execute("sheet.previous", json!({})).unwrap(); let doc = session.active().unwrap(); - assert_eq!(doc.wb.active_sheet, 0); + assert_eq!(doc.wb.active_sheet(), 0); assert_eq!(doc.selection.active.a1(), "C7"); } @@ -58,7 +58,7 @@ fn navigation_with_one_visible_sheet_is_a_noop() { for command in ["sheet.previous", "sheet.next"] { session.execute(command, json!({})).unwrap(); let doc = session.active().unwrap(); - assert_eq!(doc.wb.active_sheet, 1); + assert_eq!(doc.wb.active_sheet(), 1); assert_eq!(doc.selection.active.a1(), "C7"); } } @@ -73,6 +73,6 @@ fn very_hidden_sheets_are_skipped_at_interior_and_boundary_positions() { } for (command, expected) in [("sheet.next", 2), ("sheet.next", 2), ("sheet.previous", 0)] { session.execute(command, json!({})).unwrap(); - assert_eq!(session.active().unwrap().wb.active_sheet, expected); + assert_eq!(session.active().unwrap().wb.active_sheet(), expected); } } diff --git a/crates/model/src/workbook.rs b/crates/model/src/workbook.rs index 3b943d295b..ac6b3131b9 100644 --- a/crates/model/src/workbook.rs +++ b/crates/model/src/workbook.rs @@ -1,6 +1,7 @@ //! Workbooks. use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; use gridcraft_core::DateSystem; use gridcraft_formula::Expr; @@ -147,7 +148,8 @@ pub struct Workbook { pub calc: CalcSettings, #[serde(default, skip_serializing_if = "is_all")] pub show_objects: ShowObjects, - pub active_sheet: usize, + /// Read and set with [`Workbook::active_sheet`] and [`Workbook::set_active_sheet`]. + pub active_sheet: ActiveSheet, pub props: DocProps, /// Workbook structure protection. pub protected_structure: bool, @@ -205,6 +207,57 @@ impl Default for Workbook { } } +/// The active sheet index of a [`Workbook`]. +/// +/// Every other part of a workbook value is immutable once shared (the engine keeps versions as +/// `Arc` and tells unsaved changes by pointer). The active sheet is the exception: it +/// is the view state of the window showing the workbook, and several windows on one workbook +/// swap their own sheet into it whenever one of them becomes the one commands act on. Keeping it +/// in an atomic lets that swap happen through the shared `Arc` without cloning the workbook. +/// Copies (undo versions, a cloned workbook) take the value at the time of the copy. +#[derive(Default, Serialize, Deserialize)] +#[serde(from = "usize", into = "usize")] +pub struct ActiveSheet(AtomicUsize); + +impl ActiveSheet { + pub fn get(&self) -> usize { + self.0.load(Ordering::Relaxed) + } + pub fn set(&self, i: usize) { + self.0.store(i, Ordering::Relaxed); + } +} + +impl From for ActiveSheet { + fn from(i: usize) -> ActiveSheet { + ActiveSheet(AtomicUsize::new(i)) + } +} + +impl From for usize { + fn from(a: ActiveSheet) -> usize { + a.get() + } +} + +impl Clone for ActiveSheet { + fn clone(&self) -> ActiveSheet { + ActiveSheet::from(self.get()) + } +} + +impl PartialEq for ActiveSheet { + fn eq(&self, other: &ActiveSheet) -> bool { + self.get() == other.get() + } +} + +impl std::fmt::Debug for ActiveSheet { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.get().fmt(f) + } +} + impl Workbook { /// A new workbook with one sheet, "Sheet1". pub fn new() -> Workbook { @@ -216,7 +269,7 @@ impl Workbook { date_system: DateSystem::D1900, calc: CalcSettings::default(), show_objects: ShowObjects::All, - active_sheet: 0, + active_sheet: ActiveSheet::default(), props: DocProps::default(), protected_structure: false, structure_password: None, @@ -239,8 +292,17 @@ impl Workbook { pub fn sheet_index(&self, name: &str) -> Option { self.sheets.iter().position(|s| s.name.eq_ignore_ascii_case(name)) } + /// The index of the sheet the workbook's window shows (the file's active tab). + pub fn active_sheet(&self) -> usize { + self.active_sheet.get() + } + /// Changes the active sheet in place, also through a shared `Arc`: it is view state, + /// so switching sheets or windows neither copies the workbook nor makes it a new version. + pub fn set_active_sheet(&self, i: usize) { + self.active_sheet.set(i); + } pub fn active(&self) -> Option<&Sheet> { - self.sheet(self.active_sheet) + self.sheet(self.active_sheet()) } /// A fresh sheet name `SheetN` not in use. pub fn next_sheet_name(&self) -> String { diff --git a/crates/ui-egui/examples/snapshot.rs b/crates/ui-egui/examples/snapshot.rs index dcee71173b..3fe935732b 100644 --- a/crates/ui-egui/examples/snapshot.rs +++ b/crates/ui-egui/examples/snapshot.rs @@ -119,7 +119,7 @@ fn main() { app.open_dialog(name, p); } // Page Layout view: edit the first page's centre header (or footer) section. - let active = app.session.active().and_then(|d| d.wb.active().cloned().map(|s| (d.wb.active_sheet, s))); + let active = app.session.active().and_then(|d| d.wb.active().cloned().map(|s| (d.wb.active_sheet(), s))); if let (Some(part), Some((si, sh))) = (&hf_edit, active) { let zone = gridcraft_engine::paper::HfZone { footer: part == "footer", section: 1 }; gridcraft_ui_egui::paper_view::begin(&mut app, si, &sh, (0, 0), zone); diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index bf1dedac10..ec04f327b7 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -343,9 +343,10 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) - Some(d) => match d.command { Some(cmd) => { let params = crate::dialogs::build_params(&d); + let home = app.ui_window(); let r = app.run(cmd, params); if r.is_err() { - app.dialog = Some(d); + app.return_dialog(home, d); } wrap(r) } diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 55c0819df3..d463d86709 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -134,6 +134,20 @@ impl Dialog { }], json!({"shift": "down"}), ), + "arrangeAll" => Dialog::form( + "arrangeAll", + "Arrange Windows", + "view.arrangeAll", + vec![ + Choice { + key: "arrange", + label: "Arrange", + options: vec![("tiled", "Tiled"), ("horizontal", "Horizontal"), ("vertical", "Vertical"), ("cascade", "Cascade")], + }, + Check { key: "activeWorkbook", label: "Windows of active workbook" }, + ], + json!({"arrange": "tiled", "activeWorkbook": false}), + ), "insertPaste" => Dialog::form( "insertPaste", "Insert Paste", @@ -540,7 +554,7 @@ impl Dialog { } "solver" => { // Starts from the sheet's saved model, else the active cell as objective. - let saved = app.session.active().and_then(|doc| gridcraft_engine::cmd::solver::saved_model(&doc.wb, doc.wb.active_sheet)); + let saved = app.session.active().and_then(|doc| gridcraft_engine::cmd::solver::saved_model(&doc.wb, doc.wb.active_sheet())); let mut values = json!({"objective": active, "goal": "max", "value": "0", "variables": "", "constraints": [], "nonNegative": true, "method": "grg"}); if let (Some(m), Some(v)) = (saved.as_ref().and_then(Json::as_object), values.as_object_mut()) { for (k, x) in m { @@ -776,7 +790,7 @@ impl Dialog { // Chart Design › Select Data: data range, series to keep, category axis labels. let id = p("chart").as_u64(); let found = app.session.active().and_then(|doc| { - let si = doc.wb.active_sheet; + let si = doc.wb.active_sheet(); let sh = doc.wb.sheet(si)?; let ch = sh.charts.iter().find(|c| Some(u64::from(c.id)) == id)?; let data = gridcraft_chart::resolve(&doc.wb, si, ch); @@ -1139,6 +1153,7 @@ pub(crate) fn build_params(d: &Dialog) -> Json { pub fn show(app: &mut SheetApp, ctx: &egui::Context) { let Some(mut d) = app.dialog.take() else { return }; + let home = app.ui_window(); if d.name == "pasteSpecial" { paste_special_shortcuts(&mut d, ctx); } @@ -1415,6 +1430,15 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) { } else if confirm { open = false; } + if app.ui_window() != home { + // A command run from the dialog switched windows: whatever it opened shows in the new + // window, and this dialog (or the one it returns to) stays in its own. + let keep = if open { Some(d) } else { d.back.take().map(|b| *b) }; + if let Some(keep) = keep { + app.return_dialog(home, keep); + } + return; + } if let Some(child) = app.dialog.as_mut() { // The body opened another dialog (Name Manager › New…). It takes over; this one waits // behind it and comes back when it closes, as Excel returns to the Name Manager. Go To @@ -2032,7 +2056,7 @@ fn insert_function(app: &mut SheetApp, ui: &mut egui::Ui, d: &mut Dialog, confir let mut open = true; ok_cancel(ui, confirm, &mut open); if *confirm && let Some(n) = hits.get(d.list_index).and_then(|f| f["name"].as_str()) { - let n = app.session.active().map(|doc| crate::formula_locale::completion_name(n, locale, &doc.wb, doc.wb.active_sheet)).unwrap_or(n); + let n = app.session.active().map(|doc| crate::formula_locale::completion_name(n, locale, &doc.wb, doc.wb.active_sheet())).unwrap_or(n); let cur = app.editor.as_ref().map(|e| e.text.clone()); let text = match cur { Some(t) if t.starts_with('=') => format!("{t}{n}("), diff --git a/crates/ui-egui/src/doc_windows.rs b/crates/ui-egui/src/doc_windows.rs index 88ba3967a9..f4dcc21075 100644 --- a/crates/ui-egui/src/doc_windows.rs +++ b/crates/ui-egui/src/doc_windows.rs @@ -18,7 +18,7 @@ //! //! [`Session::windows`]: gridcraft_engine::Session::windows -use craft_window::{Frame, Viewports, WindowEvent, WindowId, WindowList}; +use craft_window::{CASCADE_STEP, Frame, Rect, Viewports, WindowEvent, WindowId, WindowList, cascade}; use serde_json::json; use crate::SheetApp; @@ -79,6 +79,33 @@ impl DocWindows { /// the logic then): shows windows created or opened by menu commands, files and scripts. pub fn logic(&mut self, app: &mut SheetApp, ctx: &egui::Context) { self.sync(app, ctx); + if let Some((arrange, windows)) = app.arrange_windows.take() { + self.arrange(ctx, &arrange, &windows); + } + } + + /// View › Arrange All: moves and sizes the native windows of `windows` (engine ids, the first + /// on top or at the top left) over the root window's monitor. + fn arrange(&self, ctx: &egui::Context, arrange: &str, windows: &[u64]) { + let ids: Vec = windows.iter().filter_map(|w| self.find(*w)).collect(); + let (monitor, infos) = ctx.input(|i| (i.viewport().monitor_size, i.raw.viewports.clone())); + // Platforms that don't report the monitor (Wayland, tests) get a common laptop screen. + let size = monitor.filter(|m| m.x > 0.0 && m.y > 0.0).unwrap_or(egui::vec2(1280.0, 800.0)); + let mut rects = arrange_rects(arrange, ids.len(), Rect::new(0.0, 0.0, size.x, size.y)); + if arrange == "cascade" { + // The active window goes in front: the last step of the cascade. + rects.reverse(); + } + for (id, r) in ids.iter().zip(rects) { + let vp = self.viewports.viewport_id(*id); + // Inner size is what can be set: take off the frame the window has now. + let frame = infos.get(&vp).and_then(|v| Some(v.outer_rect?.size() - v.inner_rect?.size())).unwrap_or(egui::Vec2::ZERO); + ctx.send_viewport_cmd_to(vp, egui::ViewportCommand::OuterPosition(egui::pos2(r.x, r.y))); + ctx.send_viewport_cmd_to(vp, egui::ViewportCommand::InnerSize((egui::vec2(r.width, r.height) - frame).max(egui::vec2(240.0, 160.0)))); + } + if let Some(first) = ids.first() { + self.viewports.focus(ctx, *first); + } } /// Mirrors the engine's visible windows: closed or hidden windows lose their native window @@ -225,3 +252,48 @@ fn window_title(app: &SheetApp, window: u64) -> String { let dirty = app.session.window(window).and_then(|w| app.session.documents().iter().find(|d| d.uid == w.doc_uid)).is_some_and(|d| d.is_dirty()); if dirty { format!("{title} — Edited") } else { title } } + +/// Outer rectangles for `n` windows arranged within `bounds` as View › Arrange All's `arrange` +/// says: `tiled` (a grid, the last row's windows widened to fill it), `horizontal` (stacked top +/// to bottom), `vertical` (side by side) or `cascade` (each one title bar below and right of the +/// previous, wrapping at the edges). +pub fn arrange_rects(arrange: &str, n: usize, bounds: Rect) -> Vec { + if n == 0 { + return vec![]; + } + let count = n as f32; + match arrange { + "horizontal" => { + let h = bounds.height / count; + (0..n).map(|i| Rect::new(bounds.x, bounds.y + h * i as f32, bounds.width, h)).collect() + } + "vertical" => { + let w = bounds.width / count; + (0..n).map(|i| Rect::new(bounds.x + w * i as f32, bounds.y, w, bounds.height)).collect() + } + "cascade" => { + let shift = CASCADE_STEP * (count - 1.0); + let size = [(bounds.width - shift).max(bounds.width * 0.5), (bounds.height - shift).max(bounds.height * 0.5)]; + let mut previous = Rect::new(bounds.x - CASCADE_STEP, bounds.y - CASCADE_STEP, size[0], size[1]); + (0..n) + .map(|_| { + previous = cascade(previous, size, bounds, CASCADE_STEP); + previous + }) + .collect() + } + _ => { + let cols = (count.sqrt().ceil() as usize).max(1); + let rows = n.div_ceil(cols); + let h = bounds.height / rows as f32; + (0..n) + .map(|i| { + let (row, col) = (i / cols, i % cols); + let in_row = if row + 1 == rows { n - row * cols } else { cols }; + let w = bounds.width / in_row as f32; + Rect::new(bounds.x + w * col as f32, bounds.y + h * row as f32, w, h) + }) + .collect() + } + } +} diff --git a/crates/ui-egui/src/formula_bar.rs b/crates/ui-egui/src/formula_bar.rs index b88699f571..5ca1edd12a 100644 --- a/crates/ui-egui/src/formula_bar.rs +++ b/crates/ui-egui/src/formula_bar.rs @@ -83,7 +83,7 @@ fn active_input(app: &SheetApp) -> String { } return sh .cell(anchor) - .map(|c| crate::formula_locale::display(&c.input_text(), app.ui.language.formula_locale(), &d.wb, d.wb.active_sheet)) + .map(|c| crate::formula_locale::display(&c.input_text(), app.ui.language.formula_locale(), &d.wb, d.wb.active_sheet())) .unwrap_or_default(); } if let Some(c) = sh.cell(a) { @@ -91,7 +91,7 @@ fn active_input(app: &SheetApp) -> String { return String::new(); } return if let Some(f) = &c.formula { - let shown = crate::formula_locale::display(&c.input_text(), app.ui.language.formula_locale(), &d.wb, d.wb.active_sheet); + let shown = crate::formula_locale::display(&c.input_text(), app.ui.language.formula_locale(), &d.wb, d.wb.active_sheet()); // A legacy array formula shows in braces (`{=A1:A2*2}`), as `Cell::bar_text` does. if f.array.is_some() { format!("{{{shown}}}") } else { shown } } else { @@ -126,7 +126,7 @@ fn name_box(app: &mut SheetApp, ui: &mut egui::Ui, t: &Tokens) { if !target.is_empty() && target != shown { // A new name for the selection, or a place to go. let is_ref = gridcraft_engine::core::RangeRef::parse(target.split('!').next_back().unwrap_or("")).is_some(); - let known = app.session.active().is_some_and(|d| d.wb.name(&target, d.wb.active_sheet).is_some() || d.wb.table(&target).is_some()); + let known = app.session.active().is_some_and(|d| d.wb.name(&target, d.wb.active_sheet()).is_some() || d.wb.table(&target).is_some()); let r = if is_ref || known { app.run("edit.goTo", json!({"reference": target})) } else { diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 145b9f583d..37d12759d6 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -563,7 +563,7 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { app.grid.rect = Some(rect); let Some(d) = app.session.active() else { return }; let wb: Arc = d.wb.clone(); - let si = wb.active_sheet; + let si = wb.active_sheet(); let Some(sh) = wb.sheet(si) else { return }; let sel = d.selection.clone(); let place = (d.uid, si); @@ -1803,7 +1803,7 @@ pub(crate) fn paint_selection( } // Marching ants around the copied range. if let Some(clip) = &app.session.clipboard - && app.session.active().is_some_and(|d| d.uid == clip.doc_uid && d.wb.active_sheet == clip.sheet) + && app.session.active().is_some_and(|d| d.uid == clip.doc_uid && d.wb.active_sheet() == clip.sheet) && range_in_pane(clip.range, rows, cols) { // A multi-area copy marks each area as selected. @@ -2109,7 +2109,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & if primary_clicked && !resp.drag_started() && let Some(p) = pos - && let Some((id, act)) = crate::chartview::slicer_click(geo, sh, wb, wb.active_sheet, p) + && let Some((id, act)) = crate::chartview::slicer_click(geo, sh, wb, wb.active_sheet(), p) { app.selected_chart = Some(id); match act { @@ -2307,13 +2307,13 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & && ed.can_point() { let anchor = if mods.shift && ed.point_at_caret() { - ed.point_anchor.filter(|(sheet, _)| *sheet == wb.active_sheet).map(|(_, cell)| cell).unwrap_or(c) + ed.point_anchor.filter(|(sheet, _)| *sheet == wb.active_sheet()).map(|(_, cell)| cell).unwrap_or(c) } else { c }; let range = RangeRef::new(anchor, c); let text = if range.is_single() { c.a1() } else { range.a1() }; - let sheet_name = (ed.sheet != wb.active_sheet).then(|| sh.name.clone()); + let sheet_name = (ed.sheet != wb.active_sheet()).then(|| sh.name.clone()); let r = match sheet_name { Some(n) => format!("{}!{text}", gridcraft_engine::formula::quote_sheet(&n)), None => text, @@ -2325,7 +2325,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & } ed.request_focus = true; ed.point_cell = Some(c); - ed.point_anchor = Some((wb.active_sheet, anchor)); + ed.point_anchor = Some((wb.active_sheet(), anchor)); app.grid.drag = Drag::Select; app.grid.drag_select = true; if resp.drag_started() @@ -2614,7 +2614,7 @@ fn format_painter_apply(app: &mut SheetApp) { fn point_header(app: &mut SheetApp, wb: &Workbook, sh: &Sheet, range: RangeRef, extend: bool, add: bool) -> bool { let Some(ed) = app.editor.as_mut().filter(|ed| ed.can_point()) else { return false }; let edit_sheet = ed.sheet; - let active_sheet = wb.active_sheet; + let active_sheet = wb.active_sheet(); let range = if extend { ed.point .and_then(|point| crate::editor::formula_refs(&ed.text).into_iter().find(|(start, end, ..)| (*start, *end) == point)) @@ -2636,7 +2636,8 @@ fn point_header(app: &mut SheetApp, wb: &Workbook, sh: &Sheet, range: RangeRef, range }; let reference = range.a1(); - let reference = if ed.sheet != wb.active_sheet { format!("{}!{reference}", gridcraft_engine::formula::quote_sheet(&sh.name)) } else { reference }; + let reference = + if ed.sheet != wb.active_sheet() { format!("{}!{reference}", gridcraft_engine::formula::quote_sheet(&sh.name)) } else { reference }; if add { ed.append_ref(&reference); } else { @@ -2656,13 +2657,13 @@ fn select_to_pointer(app: &mut SheetApp, geo: &Geo, sh: &Sheet, wb: &Workbook, p if let Some(ed) = app.editor.as_mut() && ed.point.is_some() { - if let Some(start) = ed.point_anchor.filter(|(sheet, _)| *sheet == wb.active_sheet).map(|(_, cell)| cell).or(ed.point_cell) { + if let Some(start) = ed.point_anchor.filter(|(sheet, _)| *sheet == wb.active_sheet()).map(|(_, cell)| cell).or(ed.point_cell) { let range = RangeRef::new(start, cell); let text = if range.is_single() { range.start.a1() } else { range.a1() }; - let text = if ed.sheet != wb.active_sheet { format!("{}!{text}", gridcraft_engine::formula::quote_sheet(&sh.name)) } else { text }; + let text = if ed.sheet != wb.active_sheet() { format!("{}!{text}", gridcraft_engine::formula::quote_sheet(&sh.name)) } else { text }; ed.insert_ref(&text); ed.point_cell = Some(cell); - ed.point_anchor = Some((wb.active_sheet, start)); + ed.point_anchor = Some((wb.active_sheet(), start)); } } else if let Some(d) = app.session.active() { // Read the current anchor: starting a drag may have just replaced the selection. @@ -3117,7 +3118,7 @@ fn filter_menu(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, was_open: bool) let Some((col, row, at)) = app.grid.filter_menu else { return }; let Some(d) = app.session.active() else { return }; let wb = d.wb.clone(); - let si = wb.active_sheet; + let si = wb.active_sheet(); let Some(sh) = wb.sheet(si) else { return }; let _ = geo; // Tables get an autofilter on demand. @@ -3460,12 +3461,12 @@ pub fn point_move(app: &mut SheetApp, ed: &mut EditState, dr: i64, dc: i64, exte ) }) .unwrap_or(from); - ed.point_anchor.filter(|(sheet, _)| *sheet == d.wb.active_sheet).map(|(_, cell)| cell).unwrap_or(inferred_anchor) + ed.point_anchor.filter(|(sheet, _)| *sheet == d.wb.active_sheet()).map(|(_, cell)| cell).unwrap_or(inferred_anchor) } else { to }; let text = if extend { RangeRef::new(anchor, to).a1() } else { to.a1() }; - let sheet = d.wb.active_sheet; + let sheet = d.wb.active_sheet(); ed.insert_ref(&text); ed.point_cell = Some(to); ed.point_anchor = Some((sheet, anchor)); @@ -3606,7 +3607,7 @@ fn list_picker(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, was_open: bool) let Some(c) = app.grid.list_picker else { return }; let Some(d) = app.session.active() else { return }; let wb = d.wb.clone(); - let si = wb.active_sheet; + let si = wb.active_sheet(); let Some(sh) = wb.sheet(si) else { return }; let Some(dv) = sh.validations.iter().find(|x| x.ranges.iter().any(|r| r.contains(c))).cloned() else { app.grid.list_picker = None; @@ -3743,7 +3744,7 @@ fn is_checkbox(sh: &Sheet, app: &SheetApp) -> bool { fn paint_pages(app: &mut SheetApp, p: &Painter, geo: &Geo, sh: &Sheet) { let Some(d) = app.session.active() else { return }; - let key = (d.uid, d.revision, d.wb.active_sheet); + let key = (d.uid, d.revision, d.wb.active_sheet()); if app.grid.pages.as_ref().is_none_or(|(k, _)| *k != key) { let pages: Vec<(RangeRef, u32)> = app .session diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 19f69a90fa..973b12de5a 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -342,6 +342,8 @@ pub struct SheetApp { /// tests, embedding hosts) one window shows the active workbook window, and its close button /// quits. pub native_windows: bool, + /// View › Arrange All's layout and windows, waiting for the native windows to move. + pub arrange_windows: Option<(String, Vec)>, /// The native window that has focus, which menu commands and screenshots go to. pub(crate) focus_window: Option, } @@ -394,6 +396,7 @@ impl SheetApp { ui_window: None, parked: HashMap::new(), native_windows: false, + arrange_windows: None, focus_window: None, } } @@ -479,6 +482,14 @@ impl SheetApp { // Requests the command made (a dialog, a draft) belong to the window it left active. self.follow_active_window(); self.after_engine(); + if id == "view.arrangeAll" + && self.native_windows + && let Ok(r) = &r + { + let arrange = r.get("arrange").and_then(Json::as_str).unwrap_or("tiled").to_string(); + let windows = r.get("windows").and_then(Json::as_array).map(|a| a.iter().filter_map(Json::as_u64).collect()).unwrap_or_default(); + self.arrange_windows = Some((arrange, windows)); + } // The new chart is selected, as after inserting any object, so Delete removes it, not its data. if id == "insert.chart" && let Ok(r) = &r @@ -811,7 +822,7 @@ impl SheetApp { let text = text.map(|text| { self.session .active() - .map(|d| formula_locale::display_template(&text, self.ui.language.formula_locale(), &d.wb, d.wb.active_sheet)) + .map(|d| formula_locale::display_template(&text, self.ui.language.formula_locale(), &d.wb, d.wb.active_sheet())) .unwrap_or(text) }); self.begin_edit(text, false); @@ -977,12 +988,12 @@ impl SheetApp { /// The active workbook and sheet: what drafts, text boxes and picture targets belong to. pub fn view_key(&self) -> Option<(u64, usize)> { - self.session.active().map(|d| (d.uid, d.wb.active_sheet)) + self.session.active().map(|d| (d.uid, d.wb.active_sheet())) } /// The active window and sheet: each window scrolls on its own. fn scroll_key(&self) -> Option<(u64, usize)> { - Some((self.session.active_window()?, self.session.active()?.wb.active_sheet)) + Some((self.session.active_window()?, self.session.active()?.wb.active_sheet())) } pub fn view(&self) -> SheetView { @@ -1053,7 +1064,7 @@ impl SheetApp { String::new() } else { match sh.cell(at) { - Some(c) if c.formula.is_some() => formula_locale::display(&c.input_text(), locale, &d.wb, d.wb.active_sheet), + Some(c) if c.formula.is_some() => formula_locale::display(&c.input_text(), locale, &d.wb, d.wb.active_sheet()), _ => gridcraft_engine::display::edit_text(&d.wb, sh, at), } }; @@ -1061,7 +1072,7 @@ impl SheetApp { Some(t) => (t, true), None => (current.clone(), false), }; - let mut ed = editor::EditState::new(d.uid, d.wb.active_sheet, at, text, replace, from_formula_bar); + let mut ed = editor::EditState::new(d.uid, d.wb.active_sheet(), at, text, replace, from_formula_bar); ed.window = self.session.active_window(); ed.locale = locale; if replace && self.ui.autocomplete && !ed.is_formula() { @@ -1153,7 +1164,7 @@ impl SheetApp { self.toast = Some((msg, now_ms())); } // Point mode can leave another sheet active; the edit belongs to the sheet it started on. - if self.session.active().is_some_and(|d| d.wb.active_sheet != ed.sheet) + if self.session.active().is_some_and(|d| d.wb.active_sheet() != ed.sheet) && let Err(e) = self.session.run("sheet.activate", json!({"sheet": ed.sheet})) { self.message = Some(("GridCraft".into(), clean_error(&e))); @@ -1214,7 +1225,7 @@ impl SheetApp { let multi = self.session.active().is_some_and(|d| !d.selection.is_single_cell()); // Any other move (arrows, clicks, another sheet) leaves the selection off the cell the // last Tab landed on, which ends the run. - let here = self.session.active().map(|d| (d.uid, d.wb.active_sheet, d.selection.active)); + let here = self.session.active().map(|d| (d.uid, d.wb.active_sheet(), d.selection.active)); let run = self.tab_run.take().filter(|&(uid, sheet, _, at)| here == Some((uid, sheet, at))); if multi { let fwd = dr > 0 || dc > 0; @@ -1227,7 +1238,7 @@ impl SheetApp { if dr == 0 && dc != 0 { let start = run.map(|r| r.2); self.tab_run = self.session.active().and_then(|d| { - let (uid, sheet) = (d.uid, d.wb.active_sheet); + let (uid, sheet) = (d.uid, d.wb.active_sheet()); let col = start.or(here.map(|h| h.2.col))?; Some((uid, sheet, col, d.selection.active)) }); diff --git a/crates/ui-egui/src/panes.rs b/crates/ui-egui/src/panes.rs index a8a7e35f48..623803e2af 100644 --- a/crates/ui-egui/src/panes.rs +++ b/crates/ui-egui/src/panes.rs @@ -230,7 +230,7 @@ fn format_chart(app: &mut SheetApp, ui: &mut Ui) { }); }); ui.collapsing("Series", |ui| { - let data = app.session.active().map(|d| gridcraft_chart::resolve(&d.wb, d.wb.active_sheet, &chart)); + let data = app.session.active().map(|d| gridcraft_chart::resolve(&d.wb, d.wb.active_sheet(), &chart)); for (i, s) in chart.series.iter().enumerate() { let name = data.as_ref().and_then(|d| d.series.get(i)).map(|x| x.name.clone()).unwrap_or_else(|| format!("Series {}", i + 1)); let col = diff --git a/crates/ui-egui/src/paper_view.rs b/crates/ui-egui/src/paper_view.rs index 621a979033..c3bc3312d9 100644 --- a/crates/ui-egui/src/paper_view.rs +++ b/crates/ui-egui/src/paper_view.rs @@ -279,14 +279,14 @@ pub(crate) fn paint_headers_footers(app: &SheetApp, ui: &egui::Ui, p: &Painter, pub(crate) fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: &Geo, sh: &Sheet, wb: &Workbook) { // Leaving the sheet or the view commits. if let Some(e) = &app.grid.hf_edit - && (e.sheet != wb.active_sheet || sh.view != SheetViewMode::PageLayout || geo.paper.is_none()) + && (e.sheet != wb.active_sheet() || sh.view != SheetViewMode::PageLayout || geo.paper.is_none()) { commit(app); } let Some(pg) = geo.paper.clone() else { return }; - ruler_margins(app, ui, geo, &pg, sh, wb.active_sheet); + ruler_margins(app, ui, geo, &pg, sh, wb.active_sheet()); if std::mem::take(&mut app.grid.hf_start) { - begin(app, wb.active_sheet, sh, (pg.data_rows.0, pg.data_cols.0), HfZone { footer: false, section: 1 }); + begin(app, wb.active_sheet(), sh, (pg.data_rows.0, pg.data_cols.0), HfZone { footer: false, section: 1 }); } if resp.clicked_by(egui::PointerButton::Primary) && let Some(p) = ui.input(|i| i.pointer.interact_pos()) @@ -294,7 +294,7 @@ pub(crate) fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Respo let (x, y) = (geo.scroll.x as f64 + ((p.x - geo.cells.left()) / geo.z) as f64, geo.scroll.y as f64 + ((p.y - geo.cells.top()) / geo.z) as f64); match pg.hf_zone_at(x, y) { - Some((page, zone)) if geo.cells.contains(p) => begin(app, wb.active_sheet, sh, page, zone), + Some((page, zone)) if geo.cells.contains(p) => begin(app, wb.active_sheet(), sh, page, zone), _ => { // A click anywhere else ends the edit; the grid then handles the click. if app.grid.hf_edit.is_some() { diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 6bd45e18f0..bea9e71912 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -1930,7 +1930,7 @@ fn formulas(app: &mut SheetApp, ui: &mut Ui) { { let locale = app.ui.language.formula_locale(); let name = - app.session.active().map(|d| crate::formula_locale::completion_name(n, locale, &d.wb, d.wb.active_sheet)).unwrap_or(n); + app.session.active().map(|d| crate::formula_locale::completion_name(n, locale, &d.wb, d.wb.active_sheet())).unwrap_or(n); let desc = crate::formula_locale::description(n, locale, app.ui.language).unwrap_or_default(); if ui.button(name).on_hover_text(desc).clicked() { app.run_or_alert("formulas.insertFunction", json!({"name": n})); @@ -2318,6 +2318,16 @@ fn view(app: &mut SheetApp, ui: &mut Ui) { act(app, "view.zoomToSelection", json!({"viewWidth": r.width(), "viewHeight": r.height()})); } sep(ui); + let __k = big_button(ui, Icon::NewFile, "New\nWindow", "New Window", false); + control_keytip(app, ui, &__k, "view.newWindow"); + if __k.clicked() { + act(app, "view.newWindow", json!({})); + } + let __k = big_button(ui, Icon::Table, "Arrange\nAll", "Arrange All", false); + control_keytip(app, ui, &__k, "view.arrangeAll"); + if __k.clicked() { + app.open_dialog("arrangeAll", json!({})); + } let fp = big_button(ui, Icon::Freeze, "Freeze\nPanes", "Freeze Panes", true); for sequence in keytip_table::group_extension(app.keytips.prefix().unwrap_or_default(), &["view.freezePanes", "view.freezeTopRow", "view.freezeFirstColumn"]) diff --git a/crates/ui-egui/src/tabs.rs b/crates/ui-egui/src/tabs.rs index bc6fcc1d8f..14d2dfc6ae 100644 --- a/crates/ui-egui/src/tabs.rs +++ b/crates/ui-egui/src/tabs.rs @@ -72,7 +72,7 @@ pub fn sheet_tabs(app: &mut SheetApp, ui: &mut Ui) { let all = crate::widgets::icon_button(ui, Icon::SheetList, t.text_dim, app.ui.language.tr("All sheets"), vec2(24.0, 24.0)); all.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, app.ui.language.tr("All sheets"))); let items = app.session.active().map(|d| navigator_items(&d.wb.sheets)).unwrap_or_default(); - let active = app.session.active().map(|d| d.wb.active_sheet.to_string()).unwrap_or_default(); + let active = app.session.active().map(|d| d.wb.active_sheet().to_string()).unwrap_or_default(); let rows: Vec<(&str, String)> = items.iter().map(|(i, name)| (i.as_str(), name.clone())).collect(); if let crate::ribbon::ComboResult::Picked(i) = crate::ribbon::editable_combo(ui, &all, "sheet_navigator", &active, &rows, false) && let Ok(i) = i.parse::() @@ -86,7 +86,7 @@ pub fn sheet_tabs(app: &mut SheetApp, ui: &mut Ui) { .iter() .enumerate() .filter(|(_, s)| s.visibility == Visibility::Visible) - .map(|(i, s)| (i, s.name.clone(), s.tab_color.and_then(|c| c.resolve(&d.wb.theme)), i == d.wb.active_sheet)) + .map(|(i, s)| (i, s.name.clone(), s.tab_color.and_then(|c| c.resolve(&d.wb.theme)), i == d.wb.active_sheet())) .collect(); // Keep worksheet tabs in their own horizontally scrollable viewport. As in Excel, the // tabs get up to 60% of the bar and the grid's horizontal scrollbar the rest; a strip @@ -94,7 +94,7 @@ pub fn sheet_tabs(app: &mut SheetApp, ui: &mut Ui) { let tabs_width = (ui.available_width() * 0.6).max(100.0); // Reveal the selected sheet after navigation or layout changes, but // allow browsing other tabs without snapping back on every frame. - let reveal_key = egui::Id::new((d.uid, d.wb.active_sheet, &sheets, tabs_width.to_bits())); + let reveal_key = egui::Id::new((d.uid, d.wb.active_sheet(), &sheets, tabs_width.to_bits())); let reveal_id = ui.id().with("worksheet_tabs_reveal"); let reveal_active = ui.ctx().data_mut(|data| { let changed = data.get_temp::(reveal_id) != Some(reveal_key); @@ -451,11 +451,11 @@ mod tests { let mut app = crate::SheetApp::new(session, Default::default()); app.begin_edit(Some("99".into()), false); activate_sheet(&mut app, 1); - assert_eq!(app.session.active().unwrap().wb.active_sheet, 0, "a value the validation stops must not leave its sheet"); + assert_eq!(app.session.active().unwrap().wb.active_sheet(), 0, "a value the validation stops must not leave its sheet"); assert!(app.editor.is_some(), "the edit stays open to be corrected"); app.editor.as_mut().unwrap().text = "7".into(); activate_sheet(&mut app, 1); - assert_eq!(app.session.active().unwrap().wb.active_sheet, 1, "the corrected value commits and the sheet changes"); + assert_eq!(app.session.active().unwrap().wb.active_sheet(), 1, "the corrected value commits and the sheet changes"); let a1 = gridcraft_engine::core::CellRef::parse("A1").unwrap(); assert!(app.session.active().unwrap().wb.sheet(0).unwrap().cell(a1).is_some(), "the value lands on the first sheet"); } diff --git a/crates/ui-egui/src/text_box.rs b/crates/ui-egui/src/text_box.rs index a0b30c0252..229f4ea5f0 100644 --- a/crates/ui-egui/src/text_box.rs +++ b/crates/ui-egui/src/text_box.rs @@ -44,7 +44,7 @@ impl SheetApp { id, text: shape.text.clone(), uid: d.uid, - sheet: d.wb.active_sheet, + sheet: d.wb.active_sheet(), original: shape.text.clone(), request_focus: true, rect: None, diff --git a/crates/ui-egui/src/window_ui.rs b/crates/ui-egui/src/window_ui.rs index 96aa83c79d..bc6f31a3da 100644 --- a/crates/ui-egui/src/window_ui.rs +++ b/crates/ui-egui/src/window_ui.rs @@ -126,6 +126,19 @@ impl SheetApp { self.follow_active_window(); } + /// Puts back a dialog that was taken out of window `home`'s fields while it ran. A command + /// run from the dialog may have switched windows meanwhile; the dialog stays in the window it + /// was opened in (dropped if that window closed). + pub(crate) fn return_dialog(&mut self, home: Option, d: dialogs::Dialog) { + if home == self.ui_window { + self.dialog = Some(d); + } else if let Some(w) = home.and_then(|h| self.parked.get_mut(&h)) + && w.dialog.is_none() + { + w.dialog = Some(d); + } + } + /// Whether a window other than the current one has a cell or text-box draft open. pub fn parked_window_with_draft(&self) -> Option { self.parked.iter().filter(|(_, w)| w.editor.is_some() || w.text_box_editor.is_some()).map(|(id, _)| *id).min() diff --git a/crates/ui-egui/tests/commit_after_pointing.rs b/crates/ui-egui/tests/commit_after_pointing.rs index 89e1ccd29b..3b29eb7a66 100644 --- a/crates/ui-egui/tests/commit_after_pointing.rs +++ b/crates/ui-egui/tests/commit_after_pointing.rs @@ -34,7 +34,7 @@ fn commit_after_pointing_on_another_sheet_writes_to_the_origin_sheet() { assert!(h.state_mut().commit_edit(0, 0, false, false)); let at = CellRef::parse("F8").unwrap(); let wb = &h.state().session.active().unwrap().wb; - assert_eq!(wb.active_sheet, 0, "the origin sheet is shown again"); + assert_eq!(wb.active_sheet(), 0, "the origin sheet is shown again"); assert!(wb.sheet(0).unwrap().cell(at).is_some_and(|c| c.formula.is_some()), "formula written on the origin sheet"); assert!(wb.sheet(1).unwrap().cell(at).is_none(), "the pointed-at sheet is untouched"); } @@ -44,7 +44,7 @@ fn ctrl_enter_after_pointing_on_another_sheet_fills_the_origin_selection() { let mut h = pointing_on_second_sheet("B2:C3"); assert!(h.state_mut().commit_edit(0, 0, false, true)); let wb = &h.state().session.active().unwrap().wb; - assert_eq!(wb.active_sheet, 0, "the origin sheet is shown again"); + assert_eq!(wb.active_sheet(), 0, "the origin sheet is shown again"); for a1 in ["B2", "C2", "B3", "C3"] { let at = CellRef::parse(a1).unwrap(); assert!(wb.sheet(0).unwrap().cell(at).is_some_and(|c| c.formula.is_some()), "{a1} filled on the origin sheet"); diff --git a/crates/ui-egui/tests/doc_windows.rs b/crates/ui-egui/tests/doc_windows.rs index b6d518ce2e..46427f3bfb 100644 --- a/crates/ui-egui/tests/doc_windows.rs +++ b/crates/ui-egui/tests/doc_windows.rs @@ -307,3 +307,116 @@ fn feedback_from_opening_a_workbook_shows_in_its_new_window() { desk.app.enter_window(book1); assert!(desk.app.notice.is_none() && desk.app.message.is_none(), "Book1's window shows neither"); } + +/// Book1 with two sheets, saved, shown in two windows: window 1 on Sheet1 at B2, window 2 on +/// Sheet2 at D4. Returns the window ids. +fn two_windows_on_one_workbook(desk: &mut Desk) -> (u64, u64) { + desk.app.run("home.insertSheet", json!({})).unwrap(); + let path = std::env::temp_dir().join(format!("gridcraft-two-windows-{}-{:?}.xlsx", std::process::id(), std::thread::current().id())); + desk.app.run("file.save", json!({"path": path})).unwrap(); + let _ = std::fs::remove_file(&path); + desk.app.run("sheet.activate", json!({"sheet": 0})).unwrap(); + desk.app.run("selection.set", json!({"range": "B2"})).unwrap(); + desk.frames(2); + let w1 = desk.app.session.active_window().unwrap(); + desk.app.run("view.newWindow", json!({})).unwrap(); + desk.frames(3); + let w2 = desk.app.session.active_window().unwrap(); + desk.app.run("sheet.activate", json!({"sheet": 1})).unwrap(); + desk.app.run("selection.set", json!({"range": "D4"})).unwrap(); + desk.frames(3); + (w1, w2) +} + +#[test] +fn two_windows_on_one_workbook_show_their_own_sheet_and_each_others_edits() { + let mut desk = Desk::new(true); + let (w1, w2) = two_windows_on_one_workbook(&mut desk); + assert_eq!(desk.windows.shown_windows(), [w1, w2], "New Window opens a native window"); + assert_ne!(desk.viewport(w1), desk.viewport(w2)); + let titles: Vec = [w1, w2].iter().map(|w| desk.app.session.window_title(*w).unwrap()).collect(); + assert!(titles[0].ends_with(".xlsx:1") && titles[1].ends_with(".xlsx:2"), "{titles:?}"); + + // Drawing both windows every frame swaps their views without copying the workbook. + let wb = desk.app.session.doc().unwrap().wb.clone(); + desk.frames(3); + assert!(std::sync::Arc::ptr_eq(&wb, &desk.app.session.doc().unwrap().wb), "a window swap copied the workbook"); + assert!(!desk.app.session.doc().unwrap().is_dirty()); + for (w, sheet, cell) in [(w1, 0, "B2"), (w2, 1, "D4")] { + desk.app.enter_window(w); + let d = desk.app.session.doc().unwrap(); + assert_eq!((d.wb.active_sheet(), d.selection.active), (sheet, CellRef::parse(cell).unwrap()), "window {w}'s sheet and selection"); + assert!(desk.app.grid.rect.is_some(), "window {w} drew its grid"); + } + + // Window 1 refers to Sheet2!D4; window 2 types there; window 1 shows it on the next frame. + desk.app.enter_window(w1); + desk.app.run("cell.set", json!({"cell": "B2", "input": "=Sheet2!D4*2"})).unwrap(); + desk.frames(1); + desk.app.enter_window(w2); + desk.app.run("cell.set", json!({"input": "21"})).unwrap(); + desk.frames(1); + desk.app.enter_window(w1); + let d = desk.app.session.doc().unwrap(); + assert_eq!(d.wb.active_sheet(), 0); + assert_eq!(d.wb.active().unwrap().value(CellRef::parse("B2").unwrap()), Value::Number(42.0)); +} + +#[test] +fn a_dialog_stays_with_its_window_across_a_switch() { + let mut desk = Desk::new(true); + let (w1, w2) = two_windows_on_one_workbook(&mut desk); + desk.app.enter_window(w1); + desk.app.open_dialog("zoom", json!({})); + desk.frames(2); + assert_eq!(desk.app.dialog.as_ref().map(|d| d.name()), Some("zoom")); + + // From the menu bar. + desk.app.run("view.switchWindows", json!({"window": w2})).unwrap(); + desk.frames(3); + assert_eq!(desk.app.session.active_window(), Some(w2)); + assert!(desk.app.dialog.is_none(), "window 2 shows no dialog"); + + // The user clicks into window 1, then back into window 2. + for (w, other) in [(w1, w2), (w2, w1)] { + let (v, o) = (desk.viewport(w), desk.viewport(other)); + INFOS.with(|i| { + let mut i = i.borrow_mut(); + i.entry(v).or_default().focused = Some(true); + i.entry(o).or_default().focused = Some(false); + }); + desk.frames(2); + assert_eq!(desk.app.session.active_window(), Some(w)); + let expected = if w == w1 { Some("zoom") } else { None }; + assert_eq!(desk.app.dialog.as_ref().map(|d| d.name()), expected, "window {w}'s dialog"); + } + + // A script switching windows while the dialog is open. + desk.frame_with(|app, ctx| { + let _ = gridcraft_ui_egui::control::handle(app, ctx, "engine.execute", &json!({"command": "window.activate", "params": {"window": w1}})); + }); + desk.frames(2); + assert_eq!(desk.app.dialog.as_ref().map(|d| d.name()), Some("zoom")); + desk.app.enter_window(w2); + assert!(desk.app.dialog.is_none()); +} + +#[test] +fn arrange_all_lays_out_the_native_windows_active_first() { + let mut desk = Desk::new(true); + let (w1, w2) = two_windows_on_one_workbook(&mut desk); + INFOS.with(|i| i.borrow_mut().entry(ViewportId::ROOT).or_default().monitor_size = Some(egui::vec2(1000.0, 800.0))); + desk.app.run("view.arrangeAll", json!({"arrange": "vertical"})).unwrap(); + let sent = desk.frames(1); + let at = |x: f32| ViewportCommand::OuterPosition(egui::pos2(x, 0.0)); + assert!(sent_to(&sent, desk.viewport(w2), &at(0.0)), "the active window goes left: {sent:?}"); + assert!(sent_to(&sent, desk.viewport(w1), &at(500.0))); + assert!(sent_to(&sent, desk.viewport(w1), &ViewportCommand::InnerSize(egui::vec2(500.0, 800.0)))); + + // Without parameters, as a script calls it: tiled. + let r = desk.app.run("view.arrangeAll", json!({})).unwrap(); + assert_eq!(r["arrange"], "tiled"); + let tiled = gridcraft_ui_egui::doc_windows::arrange_rects("tiled", 3, craft_window::Rect::new(0.0, 0.0, 1000.0, 800.0)); + let r = |x, y, w, h| craft_window::Rect::new(x, y, w, h); + assert_eq!(tiled, [r(0.0, 0.0, 500.0, 400.0), r(500.0, 0.0, 500.0, 400.0), r(0.0, 400.0, 1000.0, 400.0)]); +} diff --git a/crates/ui-egui/tests/popup_dismissal.rs b/crates/ui-egui/tests/popup_dismissal.rs index 24d9fafbcc..8ea1ec9b19 100644 --- a/crates/ui-egui/tests/popup_dismissal.rs +++ b/crates/ui-egui/tests/popup_dismissal.rs @@ -106,7 +106,7 @@ fn filter_closes_when_clicking_another_table_or_sheet() { click(&mut h, outside, PointerButton::Primary); assert!(!is_open(&h, Menu::Filter), "filter stayed open after switching table/sheet"); if sheet { - assert_eq!(h.state().session.active().unwrap().wb.active_sheet, 1); + assert_eq!(h.state().session.active().unwrap().wb.active_sheet(), 1); } else { assert_eq!(h.state().session.active().unwrap().selection.active.a1(), "H2"); } diff --git a/crates/ui-egui/tests/sheet_navigator.rs b/crates/ui-egui/tests/sheet_navigator.rs index a16e306ede..d8ee2eae10 100644 --- a/crates/ui-egui/tests/sheet_navigator.rs +++ b/crates/ui-egui/tests/sheet_navigator.rs @@ -32,7 +32,7 @@ fn harness() -> egui_kittest::Harness<'static, SheetApp> { #[test] fn typing_a_name_and_enter_activates_that_sheet() { let mut h = harness(); - assert_eq!(h.state().session.active().unwrap().wb.active_sheet, 0); + assert_eq!(h.state().session.active().unwrap().wb.active_sheet(), 0); h.get_by_label("All sheets").click(); h.run_steps(4); h.input_mut().events.push(Event::Text("summ".into())); @@ -40,5 +40,5 @@ fn typing_a_name_and_enter_activates_that_sheet() { h.input_mut().events.push(Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }); h.input_mut().events.push(Event::Key { key: Key::Enter, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE }); h.run_steps(4); - assert_eq!(h.state().session.active().unwrap().wb.active_sheet, 3, "Enter on the filtered row must activate Summary"); + assert_eq!(h.state().session.active().unwrap().wb.active_sheet(), 3, "Enter on the filtered row must activate Summary"); } diff --git a/crates/ui-egui/tests/sheet_tabs.rs b/crates/ui-egui/tests/sheet_tabs.rs index c3e5d13fb0..8b419ecc75 100644 --- a/crates/ui-egui/tests/sheet_tabs.rs +++ b/crates/ui-egui/tests/sheet_tabs.rs @@ -80,7 +80,7 @@ fn navigation_arrows_reveal_the_selected_tab() { for _ in 0..29 { click(&mut h, next); } - assert_eq!(h.state().session.doc().unwrap().wb.active_sheet, 29); + assert_eq!(h.state().session.doc().unwrap().wb.active_sheet(), 29); assert_visible(&h, "Tab29"); } @@ -141,7 +141,7 @@ fn hidden_sheets_long_names_and_window_resize_keep_active_tab_visible() { h.set_size(vec2(1000.0, 120.0)); h.run_steps(20); assert_visible(&h, name); - assert_eq!(h.state().session.doc().unwrap().wb.active_sheet, 29); + assert_eq!(h.state().session.doc().unwrap().wb.active_sheet(), 29); } #[test] @@ -185,7 +185,7 @@ fn right_clicking_a_tab_activates_its_sheet_so_the_menu_acts_on_it() { h.step(); h.input_mut().events.push(Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::NONE }); h.run_steps(20); - assert_eq!(h.state().session.doc().unwrap().wb.active_sheet, 3, "right-clicking Tab03 should activate it"); + assert_eq!(h.state().session.doc().unwrap().wb.active_sheet(), 3, "right-clicking Tab03 should activate it"); // The menu now offers Unprotect for the protected sheet that was clicked, not Protect for the one that was active. let texts: Vec = h .output() diff --git a/crates/ui-egui/tests/trace_navigation.rs b/crates/ui-egui/tests/trace_navigation.rs index 11f79fcfff..8a74ec87e4 100644 --- a/crates/ui-egui/tests/trace_navigation.rs +++ b/crates/ui-egui/tests/trace_navigation.rs @@ -107,11 +107,11 @@ fn cross_sheet_precedents_jump_to_the_other_sheet() { s.execute("cell.set", json!({"cell": "A1", "input": "=Src!B2*2"})).unwrap(); // Back on the first sheet, Ctrl+[ from A1 jumps to Src!B2. let d = s.active().unwrap(); - let first = d.wb.active_sheet; + let first = d.wb.active_sheet(); s.execute("selection.set", json!({"range": "A1"})).unwrap(); let mut h = harness(s); key(&mut h, Key::OpenBracket, Modifiers::CTRL); let d = h.state().session.active().unwrap(); - assert_ne!(d.wb.active_sheet, first, "selection jumped to the precedent's sheet"); + assert_ne!(d.wb.active_sheet(), first, "selection jumped to the precedent's sheet"); assert_eq!(d.selection.active, CellRef { row: 1, col: 1 }, "B2 on the Src sheet"); } diff --git a/crates/xlsx/src/read.rs b/crates/xlsx/src/read.rs index 774567bf8e..9381e010eb 100644 --- a/crates/xlsx/src/read.rs +++ b/crates/xlsx/src/read.rs @@ -297,7 +297,7 @@ pub fn read_xlsx(bytes: &[u8]) -> Result<(Workbook, ReadReport), IoError> { cx.warn("the workbook has no worksheets; added an empty sheet"); wb.sheets.push(Arc::new(Sheet::new("Sheet1"))); } - wb.active_sheet = file_to_model.get(active_tab).copied().flatten().unwrap_or(0); + wb.set_active_sheet(file_to_model.get(active_tab).copied().flatten().unwrap_or(0)); // Defined names. Chartex parts refer to their data through hidden `_xlchart.*` names. let mut chart_names: Vec<(String, String)> = vec![]; diff --git a/crates/xlsx/src/write.rs b/crates/xlsx/src/write.rs index cf208d6cda..d26dddea40 100644 --- a/crates/xlsx/src/write.rs +++ b/crates/xlsx/src/write.rs @@ -244,8 +244,8 @@ pub fn write_xlsx_named(wb: &Workbook, kind: XlsxKind, book_name: Option<&str>) // Which sheet is active (must be visible), and at least one visible sheet. let any_visible = wb.sheets.iter().any(|s| s.visibility == Visibility::Visible); - let active = if wb.sheets.get(wb.active_sheet).is_some_and(|s| s.visibility == Visibility::Visible) { - wb.active_sheet + let active = if wb.sheets.get(wb.active_sheet()).is_some_and(|s| s.visibility == Visibility::Visible) { + wb.active_sheet() } else { wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0) }; diff --git a/crates/xlsx/src/xls/mod.rs b/crates/xlsx/src/xls/mod.rs index ceaf3e26d3..d46b058cbd 100644 --- a/crates/xlsx/src/xls/mod.rs +++ b/crates/xlsx/src/xls/mod.rs @@ -198,7 +198,7 @@ pub(crate) fn read_xls_with(bytes: &[u8], mut budget: Budget) -> Result<(Workboo } wb.calc.precision_as_displayed = g.precision_as_displayed; defined_names(&g, &model_index, &mut wb, &mut report); - wb.active_sheet = wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0); + wb.set_active_sheet(wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0)); // The overview comes first. report.warnings.insert( 0, diff --git a/crates/xlsx/src/xlsb.rs b/crates/xlsx/src/xlsb.rs index 3c8454749a..39d9fd9f61 100644 --- a/crates/xlsx/src/xlsb.rs +++ b/crates/xlsx/src/xlsb.rs @@ -120,7 +120,7 @@ pub fn read_xlsb(bytes: &[u8]) -> Result<(Workbook, ReadReport), IoError> { if wb.sheets.is_empty() { return Err(invalid("no supported worksheets")); } - wb.active_sheet = wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0); + wb.set_active_sheet(wb.sheets.iter().position(|s| s.visibility == Visibility::Visible).unwrap_or(0)); Ok((wb, report)) } diff --git a/docs/gaps.md b/docs/gaps.md index 7e2e40dc06..da47033266 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-20 · **Change:** minor (the Japanese catalog covers the dialogs, messages and menus) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (§20: a native window per workbook window, New Window and Arrange All) · **Target:** Microsoft Excel (Microsoft 365) Every known shortfall against Excel, one entry each, ranked by user impact (what blocks someone using GridCraft for real work first). This is the work list: pick from the top, check the open @@ -156,7 +156,9 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- **Estimate:** 15–23 h. ### 20. Multiple windows; pen pressure (H) -- One window (#162, PR #171); no pressure or tilt in ink. **Estimate:** 10–14 h. +- Desktop windows done: a native window per workbook window, New Window on the same workbook, + Switch Windows, Arrange All. Left: Arrange All across monitors, one window on the web, no + pressure or tilt in ink. **Estimate:** 5–8 h. [hardware-parity.md](hardware-parity.md) ### 21. Automation and add-ins (F) @@ -178,6 +180,7 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | §20: a native window per workbook window, New Window on the same workbook and Arrange All on the desktop; left: Arrange All across monitors, the web's one window, pen pressure (estimate 10–14 h → 5–8 h) | | 2026-10-20 | minor | §11: the Japanese catalog covers the dialogs, messages and menus — 943 keys (~43%), matching the pt-BR coverage; a test keeps the two catalogs in step | | 2026-10-17 | minor | §6: the five reports it tracked are closed and their PRs merged (#57, #133, #139, #140, #134; PRs #148, #155, #156, #157); the entry stays open for §15's oracle suite | | 2026-10-15 | minor | §13: Solver prepares reports before committing the solution, model and report sheets together; report refusal preserves the workbook and Undo/Redo (#1050) | diff --git a/docs/hardware-parity.md b/docs/hardware-parity.md index f19b4a49dd..71cf001d53 100644 --- a/docs/hardware-parity.md +++ b/docs/hardware-parity.md @@ -1,6 +1,6 @@ # Hardware parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (a native window per workbook window) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Arrange All) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 The hardware Excel uses, per platform, against GridCraft. Part of [target-app-parity.md](target-app-parity.md). **Hardware: ~40% ready, 25–40 h** (estimated). @@ -11,7 +11,7 @@ The hardware Excel uses, per platform, against GridCraft. Part of | GPU rendering of the grid and charts | DirectX / Metal accelerated drawing | wgpu: Metal · DirectX 12 (default since 10-08; OpenGL when DirectX 12 has no GPU driver or fails to start; Vulkan only on request, so the Intel UHD `igvk64.dll` crash (#120) can't happen; shaders compiled with FXC, so no DXC runtime is needed, #339) · Vulkan/GL · Vulkan/GL · WebGPU or WebGL2 | At parity in kind. Driver problems: screen flashing on Windows 11 (#76): 2–4 h | | Pen and touch (Draw tab) | Pen pressure and tilt, eraser end, touch mode, Ink to Shape, ink replay | Pen, highlighter, eraser, Ink to Shape with mouse/pen input; **no pressure or tilt**, no touch mode | Pressure-sensitive strokes where winit reports force; touch-friendly ribbon spacing: 4–6 h | | Trackpad and mouse | Smooth scrolling, pinch zoom, horizontal scroll, Magic Mouse | Smooth scrolling and zoom via egui | Verify pinch zoom on each platform: 1–2 h | -| Multiple windows and monitors | One window per workbook, New Window for the same workbook, per-monitor DPI | A native window per workbook window on the desktop, New Window for the same workbook; new windows cascade from the last one on its monitor (web: one window) | Arrange All across monitors; verify per-monitor DPI with windows on two monitors: 2–3 h | +| Multiple windows and monitors | One window per workbook, New Window for the same workbook, per-monitor DPI | A native window per workbook window on the desktop, New Window for the same workbook; new windows cascade from the last one on its monitor; View › Arrange All tiles, stacks or cascades them over the main window's monitor (web: one window) | Arrange All across monitors; verify per-monitor DPI with windows on two monitors: 2–3 h | | High-DPI and display scaling | yes | yes (display scale matched to desktop density; View › Interface size 75–200 % on top, #230) | — | | Accessibility hardware (screen readers, braille displays, switch control) | Narrator, JAWS, NVDA, VoiceOver | AccessKit enabled in eframe; grid cells and ribbon not yet exposed as an accessible tree | Counted under UI ([ui-parity.md](ui-parity.md)) | | Printers | System print with driver options | Desktop submits through craft-print with printer choice, copies, duplex and grayscale; web browser printing | Counted under print | @@ -20,6 +20,7 @@ The hardware Excel uses, per platform, against GridCraft. Part of | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | View › Arrange All over the main window's monitor. | | 2026-10-22 | minor | Multiple windows: each workbook window is a native window of its own (desktop). | | 2026-10-12 | minor | Desktop File > Print now discovers printers and submits PDF jobs through pinned craft-print with printer choice, copies, duplex and grayscale; browser printing and PDF export remain available. Remaining print parity includes page ranges and driver-specific tray/options UI; physical printer fidelity needs hardware validation. | | 2026-10-11 | trivial | Windows: DirectX 12 shaders compiled with FXC, no DXC runtime needed (#339, #354) | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 08a9124bd5..f15a13f42f 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1,6 +1,6 @@ # Target-app parity: GridCraft vs Microsoft Excel -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (Page Setup paper sizes match Japanese Excel: B4/B5/B6 are the JIS sizes, with ISO B4/B5/B6 by name and the Japanese postcards, envelopes and A6 added; no score change) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-22 · **Change:** minor (several windows: a native window per workbook window, New Window on the same workbook, Arrange All; no score change) · **Target:** Microsoft Excel (Microsoft 365) This is the authoritative parity assessment. [`ROADMAP.md`](../ROADMAP.md) summarizes it, [`gaps.md`](gaps.md) lists every shortfall, and the area checklists hold the detail: @@ -102,11 +102,11 @@ performance 8, stability 8, localization 4, hardware 2, platforms 2, ecosystem 2 |---|---|---|---|---| | Features | 45 | ~59% | 300–485 | Table above | | File formats | 15 | ~50% | 80–120 | XLSX read/write is broad but unproven on a real corpus and in Excel; XLSB and ODS are values-only import; no XLS, XML Spreadsheet 2003, SYLK, DIF, PRN; VBA dropped; 100 MB XLSX fails (#175). [file-format-parity.md](file-format-parity.md) | -| UI/UX fidelity | 12 | ~55% | 50–80 | Excel-style ribbon, galleries, dialogs, formula bar, status bar; 59 commands with shortcuts vs Excel's 200+; key tips on part of the Home tab only; short context menu; single window. [ui-parity.md](ui-parity.md) | +| UI/UX fidelity | 12 | ~55% | 50–80 | Excel-style ribbon, galleries, dialogs, formula bar, status bar; 59 commands with shortcuts vs Excel's 200+; key tips on part of the Home tab only; short context menu; a native window per workbook window on the desktop (one on the web). [ui-parity.md](ui-parity.md) | | Performance | 8 | ~35% | 35–55 | [Performance](#performance) | | Stability | 8 | ~60% | 15–25 | Never-crash rules enforced by lint, every command fuzzed with hostile params, random command sequences fuzzed against undo/redo, XLSX round-trip and full-recalc invariants, last-resort guard in `Session::execute`. Field reports: Windows Intel GPU crash (#120), error on close (#117), freeze opening from Finder (#166), screen flashing (#76) | | Localization | 4 | ~10% | 100–165 | Ribbon-only catalog in five languages (ja, zh, ko, pt-BR, ru; ~12% of strings each), CJK/Thai/Hebrew font fallback, no shaping or RTL. [localization-parity.md](localization-parity.md) | -| Hardware | 2 | ~40% | 25–40 | GPU UI via wgpu; single-threaded calc; no pen pressure; one window. [hardware-parity.md](hardware-parity.md) | +| Hardware | 2 | ~40% | 25–40 | GPU UI via wgpu; single-threaded calc; no pen pressure; a native window per workbook window, Arrange All on one monitor. [hardware-parity.md](hardware-parity.md) | | Platforms | 2 | ~80% | 30–60 | [Platforms](#platforms) | | Ecosystem / add-ins | 2 | ~10% | 25–40 | [Ecosystem and AI](#ecosystem-and-ai) | | AI features | 2 | ~15% | 30–50 | [Ecosystem and AI](#ecosystem-and-ai) | @@ -144,7 +144,7 @@ Discounts for what still stops real work: | Discount | Factor | Evidence | |---|---|---| -| Interaction fidelity | × 0.90 | 59 command shortcuts vs Excel's 200+; key tips only on part of the Home tab (#43); rectangular selection bug (#28); charts can't be resized (#190); one window (#162); missing horizontal scrollbar report (#176) | +| Interaction fidelity | × 0.90 | 59 command shortcuts vs Excel's 200+; key tips only on part of the Home tab (#43); rectangular selection bug (#28); charts can't be resized (#190); missing horizontal scrollbar report (#176) | | Stability on real machines | × 0.92 | Never-crash lints, fuzzed commands and the `Session::execute` guard; but field reports of a startup crash on Intel UHD (#120), an error on close (#117), a freeze opening from Finder (#166) and screen flashing (#76) | | File exchange with Excel users | × 0.88 | Excel opening our output is unverified across features; unmodelled parts dropped on save; advanced chart kinds lose their type; 100 MB files fail (#175); `.xls` and encrypted files are refused with a clear message (#17) rather than opened. Harsher than a drawing app's discount because spreadsheets are exchanged constantly | @@ -334,6 +334,7 @@ The inventory carried over from the 2026-10-07 ROADMAP.md, updated for what land | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | Several windows on the desktop: a native window per workbook window, New Window on the same workbook, Switch Windows and Arrange All; "one window" removed from UI/UX fidelity, Hardware and the interaction-fidelity discount; no score change | | 2026-10-21 | minor | Page Setup paper sizes: `B4`/`B5`/`B6` are the JIS sizes Japanese Excel uses (ISO B4/B5/B6 by name), and Japanese Postcard, Japanese Double Postcard, the chou/kaku envelopes and A6 are added; PDF MediaBox and `file.print` follow, and the Size menu lists them | | 2026-10-18 | trivial | Stale chart notes: all chart kinds survive XLSX round-trip (PRs #158, #161) and trendlines landed | | 2026-10-16 | minor | Advanced Filter copy-to clears only its actual output and rejects overflow without changing cells (#1031) | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 817b5b9151..d34b8c464f 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (every workbook window is a native window of its own on the desktop, with its own draft, dialog and ribbon; closing one of several windows on a workbook doesn't ask; Quit asks about each unsaved workbook in its window; the last window closed quits on Windows and Linux, macOS keeps running) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (View › Arrange All lays out the native windows; two windows on one workbook show different sheets without copying it; a dialog stays in its window) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -33,8 +33,8 @@ Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes | Backstage (File) view: Info, New with templates, Open recent, Export, Options | yes | yes (#174, 10-11): the File tab opens a full-window view (Esc or the back arrow return) with Home, New (blank + the three samples), Open (recent + Browse; the recent list tells when each workbook was last changed, 10-12), Info (location, properties, Protect Workbook), Save, Save As, Print (page preview, active sheet or workbook, fit to page, Page Setup), Export (PDF, CSV, web page), Close and Options (`file.options`: theme, language, formula bar, AutoSave, calculation) | no Account, Share or template gallery beyond the samples; Options applies at once (no OK/Cancel dialog) and holds only the settings GridCraft has | | Dark mode, system appearance | yes | yes | done (10-09) | | Page Layout view (paper view with rulers and header editing) | yes | yes (#329, 10-11): per-sheet view saved in XLSX, pages with margins and gaps on the print pagination, "Click to add data" pages, rulers (inches or centimetres), headers and footers drawn and edited in place with a Header & Footer tab | margins drag on the rulers (#792, with header and footer margins); header/footer pictures, Different First Page / Odd & Even missing (1–3 h) | -| Multiple windows per workbook | yes | yes (10-22, desktop): each workbook window is a native window of its own (New, Open and New Window open one; titles "Book1", or "Book1:1" / "Book1:2" for a workbook's second window, with " — Edited" while unsaved). Each window keeps its own cell draft, dialog, Backstage view, message and grid state, and its ribbon acts on it; on macOS the menu bar acts on the window focused last. Leaving a window enters its draft as Enter would. A window's close button closes that window: one of several on a workbook closes without asking, a workbook's last asks Save / Don't Save / Cancel when unsaved. Quit asks about each unsaved workbook in its window, and Cancel stops it. The last window closed quits on Windows and Linux; macOS keeps running with the menu bar until New or Open. Web: one window | no Arrange All; a hidden window (View › Hide) has no native window; every window repaints when one does; see [hardware-parity.md](hardware-parity.md) | -| Switch Windows, New Window, unsaved workbooks on quit | yes | yes (10-22): View › New Window opens another window on the same workbook (no copy: saving from either saves it), each with its own selection, active sheet and scroll; a workbook with several windows titles them "Book1:1", "Book1:2"; File › Close closes one of them without asking and asks only for the last; View › Switch Windows and the macOS Window menu list the open windows (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {window}`, or `{doc}` for a workbook's most recent window); a cell being typed is entered into its own window and workbook before New, Open, Close, Save, Print and window switches (also between two windows on one workbook), and a refused entry stays open and stops the command; quitting (Cmd+Q, the macOS Quit item, the Dock) asks Save / Don't Save / Cancel for each unsaved workbook | | +| Multiple windows per workbook | yes | yes (10-22, desktop): each workbook window is a native window of its own (New, Open and New Window open one; titles "Book1", or "Book1:1" / "Book1:2" for a workbook's second window, with " — Edited" while unsaved). Each window keeps its own cell draft, dialog, Backstage view, message and grid state, and its ribbon acts on it; on macOS the menu bar acts on the window focused last. Leaving a window enters its draft as Enter would. A window's close button closes that window: one of several on a workbook closes without asking, a workbook's last asks Save / Don't Save / Cancel when unsaved. Quit asks about each unsaved workbook in its window, and Cancel stops it. The last window closed quits on Windows and Linux; macOS keeps running with the menu bar until New or Open. View › Arrange All (dialog: Tiled, Horizontal, Vertical, Cascade, Windows of active workbook; `view.arrangeAll` tiles by default) lays the visible windows out over the main window's monitor, the active one first. Web: one window | Arrange All uses one monitor; a hidden window (View › Hide) has no native window; every window repaints when one does; see [hardware-parity.md](hardware-parity.md) | +| Switch Windows, New Window, unsaved workbooks on quit | yes | yes (10-22): View › New Window opens another window on the same workbook (no copy: saving from either saves it), each with its own selection, active sheet and scroll (showing different sheets copies nothing and leaves the workbook unchanged; Save records the sheet of the window used last); a workbook with several windows titles them "Book1:1", "Book1:2"; File › Close closes one of them without asking and asks only for the last; View › Switch Windows and the macOS Window menu list the open windows (active one checked, unsaved ones marked •) and bring the chosen one forward (`view.switchWindows {window}`, or `{doc}` for a workbook's most recent window); a cell being typed is entered into its own window and workbook before New, Open, Close, Save, Print and window switches (also between two windows on one workbook), and a refused entry stays open and stops the command; quitting (Cmd+Q, the macOS Quit item, the Dock) asks Save / Don't Save / Cancel for each unsaved workbook | | ## Grid interaction @@ -177,6 +177,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-22 | minor | View › New Window and Arrange All on the View ribbon; Arrange All tiles, stacks or cascades the native windows; two windows on one workbook keep their own sheet without copying the workbook; a dialog opened in one window stays there when a command run from it switches windows | | 2026-10-22 | minor | One native window per workbook window on the desktop: per-window drafts, dialogs and ribbons; menu bar commands go to the focused window; closing one of several windows on a workbook doesn't ask; Quit asks in each unsaved workbook's window; the last window closed quits (Windows, Linux) or leaves macOS running | | 2026-10-22 | minor | New Window opens a second window on the same workbook instead of a copy that saved over the original; windows keep their own selection, sheet and scroll; titles Name:1 / Name:2; Switch Windows, Hide and Unhide work by window; File › Close closes one window of several without asking | | 2026-10-22 | minor | New workbook no longer loses the first one: the cell being typed is entered into its own workbook before any File or window command (an entry the workbook refuses stays open and the command doesn't run; an untouched F2 draft changes nothing), the editor is tied to its workbook, View › Switch Windows and the macOS Window menu list the open workbooks, and the macOS Quit item asks about unsaved workbooks like the close button | From 9cbb674c550e1bdca3a2be9a008e8a455b00f3f8 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:34:06 +0200 Subject: [PATCH 087/687] Print numbers near f64::MAX in a form that parses back Found while testing crates/formula/src/printer.rs:134 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/formula/src/printer.rs | 11 ++++++++++- crates/formula/tests/printer_roundtrip.rs | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 crates/formula/tests/printer_roundtrip.rs diff --git a/crates/formula/src/printer.rs b/crates/formula/src/printer.rs index 055b0f1458..ba659c188a 100644 --- a/crates/formula/src/printer.rs +++ b/crates/formula/src/printer.rs @@ -132,7 +132,16 @@ fn write_struct_ref(s: &StructRef, at_shorthand: bool) -> String { } fn number(n: f64) -> String { - number_to_text(n) + let text = number_to_text(n); + // 15 significant digits can round past f64::MAX, which no longer parses. + if text.parse::().is_ok_and(f64::is_finite) { + return text; + } + let exact = format!("{:E}", n); + match exact.split_once('E') { + Some((m, e)) if !e.starts_with('-') => format!("{m}E+{e}"), + _ => exact, + } } /// Prints an expression in A1 notation. diff --git a/crates/formula/tests/printer_roundtrip.rs b/crates/formula/tests/printer_roundtrip.rs new file mode 100644 index 0000000000..206e5efff1 --- /dev/null +++ b/crates/formula/tests/printer_roundtrip.rs @@ -0,0 +1,15 @@ +use gridcraft_formula::{Expr, parse, print}; + +#[test] +fn number_near_f64_max_round_trips() { + for src in ["=1.7976931348623157E308", "=-1.7976931348623157E308"] { + let e = parse(src).unwrap(); + let printed = print(&e); + let back = parse(&printed).unwrap_or_else(|err| panic!("{printed}: {err:?}")); + assert_eq!(print(&back), printed); + match (&e, &back) { + (Expr::Number(a), Expr::Number(b)) => assert_eq!(a, b), + _ => assert_eq!(e, back), + } + } +} From a8e2ed31b110bf60e70ba7c860ab798fbd28ffe9 Mon Sep 17 00:00:00 2001 From: coolvitto <160416811+coolvitto@users.noreply.github.com> Date: Fri, 23 Oct 2026 03:05:42 +0900 Subject: [PATCH 088/687] Add extra Japanese translations for UI elements Added extra labels for ribbon menus and pop-ups in Japanese. --- crates/ui-egui/src/i18n/ja.rs | 164 +++++++++++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 254ff68f90..09461b4bdf 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -980,10 +980,160 @@ pub(super) const DIALOGS: &[(&str, &str)] = &[ ("Unprotect Sheet…", "シート保護の解除…"), ]; +/// Labels of the ribbon menus, pop-ups and palettes that the reference catalog does not cover (the +/// Portuguese catalog has no entry for them either). Kept apart so the two catalogs above keep the +/// same key set; it never overlaps them. +pub(super) const EXTRA: &[(&str, &str)] = &[ + ("Cut (⌘X)", "切り取り (⌘X)"), + ("Copy (⌘C)", "コピー (⌘C)"), + ("Bold (⌘B)", "太字 (⌘B)"), + ("Italic (⌘I)", "斜体 (⌘I)"), + ("Underline (⌘U)", "下線 (⌘U)"), + ("AutoSum (⌘⇧T)", "オート SUM (⌘⇧T)"), + ("Filter (⌘⇧F)", "フィルター (⌘⇧F)"), + ("Flash Fill (⌘E)", "フラッシュ フィル (⌘E)"), + ("Insert Link (⌘K)", "リンクの挿入 (⌘K)"), + ("Paste (⌘V)", "貼り付け (⌘V)"), + ("Redo (⌘Y)", "やり直し (⌘Y)"), + ("Save (⌘S)", "上書き保存 (⌘S)"), + ("Search commands (⌘⇧Space)", "コマンドの検索 (⌘⇧Space)"), + ("Table (⌘T)", "テーブル (⌘T)"), + ("Undo (⌘Z)", "元に戻す (⌘Z)"), + ("New Workbook", "新しいブック"), + ("Save As…", "名前を付けて保存…"), + ("Print…", "印刷…"), + ("Export as PDF…", "PDF としてエクスポート…"), + ("Save a Copy as Excel Workbook (.xlsx)…", "Excel ブック (.xlsx) としてコピーを保存…"), + ("Export as CSV…", "CSV としてエクスポート…"), + ("Export as Web Page (.html)…", "Web ページ (.html) としてエクスポート…"), + ("Bottom Border", "下罫線"), + ("Top Border", "上罫線"), + ("Left Border", "左罫線"), + ("Right Border", "右罫線"), + ("No Border", "枠なし"), + ("All Borders", "格子"), + ("Outside Borders", "外枠"), + ("Thick Outside Borders", "太い外枠"), + ("Bottom Double Border", "下二重罫線"), + ("Thick Bottom Border", "下太罫線"), + ("Top and Bottom Border", "上下罫線"), + ("Top and Thick Bottom Border", "上罫線 + 下太罫線"), + ("Top and Double Bottom Border", "上罫線 + 下二重罫線"), + ("Inside Borders", "内側罫線"), + ("More Borders…", "その他の罫線…"), + ("More Functions…", "その他の関数…"), + ("No Fill", "塗りつぶしなし"), + ("Theme Colors", "テーマの色"), + ("Standard Colors", "標準の色"), + ("Highlight Cells Rules", "セルの強調表示ルール"), + ("Greater Than…", "指定の値より大きい…"), + ("Less Than…", "指定の値より小さい…"), + ("Between…", "指定の範囲内…"), + ("Equal To…", "指定の値に等しい…"), + ("Text that Contains…", "文字列…"), + ("A Date Occurring…", "日付…"), + ("Duplicate Values…", "重複する値…"), + ("Top/Bottom Rules", "上位/下位ルール"), + ("Blue", "青"), + ("Green", "緑"), + ("Red", "赤"), + ("Orange", "オレンジ"), + ("Light Blue", "水色"), + ("Purple", "紫"), + ("Green - Yellow - Red", "緑 - 黄 - 赤"), + ("Red - Yellow - Green", "赤 - 黄 - 緑"), + ("Green - White - Red", "緑 - 白 - 赤"), + ("Blue - White - Red", "青 - 白 - 赤"), + ("White - Green", "白 - 緑"), + ("White - Red", "白 - 赤"), + ("3 Arrows", "3 つの矢印"), + ("3 Traffic Lights", "3 つの信号"), + ("3 Symbols", "3 つの記号"), + ("4 Ratings", "4 段階評価"), + ("5 Arrows", "5 つの矢印"), + ("5 Quarters", "5 つの四分円"), + ("New Rule…", "新しいルール…"), + ("Clear Rules", "ルールのクリア"), + ("Manage Rules…", "ルールの管理…"), + ("Good, Bad and Neutral", "良い、悪い、どちらでもない"), + ("Bad", "悪い"), + ("Good", "良い"), + ("Neutral", "どちらでもない"), + ("Data and Model", "データとモデル"), + ("Calculation", "計算"), + ("Check Cell", "チェック セル"), + ("Explanatory Text", "説明文"), + ("Input", "入力"), + ("Linked Cell", "リンク セル"), + ("Note", "メモ"), + ("Output", "出力"), + ("Warning Text", "警告文"), + ("Titles and Headings", "タイトルと見出し"), + ("Heading 1", "見出し 1"), + ("Heading 2", "見出し 2"), + ("Heading 3", "見出し 3"), + ("Heading 4", "見出し 4"), + ("Total", "集計"), + ("Themed Cell Styles", "テーマのセルのスタイル"), + ("Number Format", "表示形式"), + ("Comma [0]", "桁区切り [0]"), + ("Currency [0]", "通貨表示形式 [0]"), + ("Percent", "パーセント"), + ("Place in Cell", "セル内に配置"), + ("Place over Cells", "セルの上に配置"), + ("Rectangle", "四角形"), + ("Rounded Rectangle", "角丸四角形"), + ("Oval", "楕円"), + ("Triangle", "三角形"), + ("Arrow", "矢印"), + ("Column", "縦棒"), + ("Win/Loss", "勝敗"), + ("Type to filter…", "入力して絞り込み…"), + ("Sort Ascending", "昇順"), + ("Sort Descending", "降順"), + ("Search", "検索"), + ("Apply", "適用"), + ("20% - Accent1", "20% - アクセント 1"), + ("20% - Accent2", "20% - アクセント 2"), + ("20% - Accent3", "20% - アクセント 3"), + ("20% - Accent4", "20% - アクセント 4"), + ("20% - Accent5", "20% - アクセント 5"), + ("20% - Accent6", "20% - アクセント 6"), + ("40% - Accent1", "40% - アクセント 1"), + ("40% - Accent2", "40% - アクセント 2"), + ("40% - Accent3", "40% - アクセント 3"), + ("40% - Accent4", "40% - アクセント 4"), + ("40% - Accent5", "40% - アクセント 5"), + ("40% - Accent6", "40% - アクセント 6"), + ("60% - Accent1", "60% - アクセント 1"), + ("60% - Accent2", "60% - アクセント 2"), + ("60% - Accent3", "60% - アクセント 3"), + ("60% - Accent4", "60% - アクセント 4"), + ("60% - Accent5", "60% - アクセント 5"), + ("60% - Accent6", "60% - アクセント 6"), + ("Accent1", "アクセント 1"), + ("Accent2", "アクセント 2"), + ("Accent3", "アクセント 3"), + ("Accent4", "アクセント 4"), + ("Accent5", "アクセント 5"), + ("Accent6", "アクセント 6"), + ("Recent Colors", "最近使用した色"), + ("More Colors…", "その他の色…"), + ("Hex", "16進"), + ("Page Break Preview", "改ページ プレビュー"), + ("Bubble", "バブル"), + ("Box & Whisker", "箱ひげ図"), + ("Treemap", "ツリーマップ"), + ("Sunburst", "サンバースト"), + ("Stock", "株価"), + ("Bar", "横棒"), + ("Drag to fill cells. Drag from inside a cell to select a range.", "ドラッグしてセルを塗りつぶします。セルの内側からドラッグすると範囲を選択します。"), +]; + /// Translates an interface label; `None` for a label the catalog does not know, so `i18n.rs` can /// fall back to English. Dialog strings come first, the shared core second. pub(super) fn translate(text: &str) -> Option<&'static str> { - DIALOGS.iter().chain(CORE).find(|(english, _)| *english == text).map(|(_, translated)| *translated) + DIALOGS.iter().chain(CORE).chain(EXTRA).find(|(english, _)| *english == text).map(|(_, translated)| *translated) } #[cfg(test)] @@ -1088,6 +1238,18 @@ mod tests { assert_eq!(catalog_keys().count(), 954); } + #[test] + fn extra_labels_are_translated_and_do_not_overlap_the_catalog() { + let catalog: HashSet<&str> = catalog_keys().collect(); + let mut seen = HashSet::new(); + for (english, japanese) in EXTRA { + assert!(!japanese.trim().is_empty(), "empty translation for {english:?}"); + assert!(!catalog.contains(english), "{english:?} is already in CORE or DIALOGS"); + assert!(seen.insert(*english), "duplicate EXTRA key: {english:?}"); + assert_eq!(translate(english), Some(*japanese)); + } + } + #[test] fn covers_every_key_the_portuguese_catalog_translates() { let portuguese = portuguese_keys(); From 450f457535e3c8537e45986deff5b602cff00e76 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 22 Oct 2026 11:06:01 -0700 Subject: [PATCH 089/687] Calc: a formula calculated inside another one shares its stack budget A cell that another cell's formula calculates on demand got its own evaluator, which started a new 896 KiB stack budget wherever it was called. Two cells that each count down from 131 with a LAMBDA, one read at the deepest call of the other, overflowed a 1 MiB thread. The budget of a formula calculated on demand is now counted from where the outermost formula started. When it runs out there, the formula is calculated again from the top, as a formula needed at the nesting limit is, so its value does not depend on the order cells are calculated in. --- crates/calc/src/eval.rs | 19 ++++++++++++++----- crates/calc/src/recalc.rs | 31 ++++++++++++++++++++++++++++++- crates/calc/src/tests.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/crates/calc/src/eval.rs b/crates/calc/src/eval.rs index 1d39fd818e..f2f5f5ef4d 100644 --- a/crates/calc/src/eval.rs +++ b/crates/calc/src/eval.rs @@ -60,13 +60,13 @@ const MAX_LAMBDA_CALLS: usize = 140; /// Native stack, in bytes, that one formula's evaluation may use below its first `eval`. Past it, /// `eval` answers `#NUM!`. It guards the stack and leaves 128 KiB of a 1 MiB thread for the frames -/// above the formula. A cell that another cell's formula computes on demand gets its own `Evaluator`, -/// which starts its own budget. +/// above the formula. A cell that another cell's formula computes on demand shares the budget of the +/// outermost formula ([`Host::stack_top`]). const STACK_BUDGET: usize = 896 * 1024; /// The address of a local variable, which shows how far down the native stack this call is. #[inline(always)] -fn stack_position() -> usize { +pub(crate) fn stack_position() -> usize { let marker = 0u8; std::hint::black_box(&marker) as *const u8 as usize } @@ -98,6 +98,13 @@ pub trait Host { fn visit_range(&mut self, _sheet: usize, _range: RangeRef, _f: &mut dyn FnMut(&Value) -> bool) -> bool { false } + /// While a formula is calculated on demand inside another one: `stack_position()` where the + /// outermost one started, which the stack budget is counted from. + fn stack_top(&self) -> Option { + None + } + /// Called when a formula's evaluation runs past the stack budget. + fn stack_exhausted(&mut self) {} } pub struct Evaluator<'h> { @@ -265,8 +272,10 @@ impl<'h> Evaluator<'h> { pub fn eval(&mut self, e: &Expr) -> Ev { let here = stack_position(); if self.depth == 0 { - self.stack_top = here; - } else if self.stack_top.abs_diff(here) > STACK_BUDGET { + self.stack_top = self.host.stack_top().unwrap_or(here); + } + if self.stack_top.abs_diff(here) > STACK_BUDGET { + self.host.stack_exhausted(); return err(CellError::Num); } self.depth += 1; diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 66374bed2c..eabc293149 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -44,7 +44,7 @@ use gridcraft_core::{Array, CellError, CellRef, RangeRef, Value}; use gridcraft_formula::Expr; use gridcraft_model::{CalcMode, Workbook}; -use crate::eval::{Area, Evaluator, Host, precedents}; +use crate::eval::{Area, Evaluator, Host, precedents, stack_position}; pub type Key = (usize, CellRef); @@ -432,6 +432,10 @@ struct PassHost<'a> { tainted: HashSet, /// Whether the formula being evaluated has read a previous value. taint: bool, + /// `stack_position()` where the outermost formula being calculated started. + stack_top: usize, + /// Whether an evaluation ran past the stack budget since the innermost formula started. + out_of_stack: bool, } impl<'a> PassHost<'a> { @@ -462,6 +466,8 @@ impl<'a> PassHost<'a> { members: HashSet::default(), tainted: HashSet::default(), taint: false, + stack_top: 0, + out_of_stack: false, } } } @@ -580,6 +586,9 @@ impl PassHost<'_> { self.suspend = Some(k); return Value::Error(CellError::Calc); } + if self.depth == 0 { + self.stack_top = stack_position(); + } self.in_progress.insert(k); self.depth += 1; let outer_taint = std::mem::replace(&mut self.taint, false); @@ -587,10 +596,24 @@ impl PassHost<'_> { self.stack.push(k); } let expr = std::sync::Arc::clone(expr); + let outer_out_of_stack = std::mem::replace(&mut self.out_of_stack, false); let v = { let mut ev = Evaluator::new(self, k.0, k.1); ev.value(&expr) }; + let out_of_stack = std::mem::replace(&mut self.out_of_stack, outer_out_of_stack); + if out_of_stack && self.depth > 1 && self.suspend.is_none() { + // It ran out of the stack left below the formulas that read it: it is calculated + // from the top instead, as a formula needed at `MAX_NESTED` levels deep is. + self.suspend = Some(k); + self.depth -= 1; + self.in_progress.remove(&k); + if self.iterate { + self.stack.pop(); + } + self.taint = outer_taint; + return Value::Error(CellError::Calc); + } if self.suspend.is_some() { // Abandoned: `calculate` evaluates this formula again once the one it needs is done. self.depth -= 1; @@ -745,6 +768,12 @@ impl Host for PassHost<'_> { fn workbook(&self) -> &Workbook { self.wb } + fn stack_top(&self) -> Option { + (self.depth > 1).then_some(self.stack_top) + } + fn stack_exhausted(&mut self) { + self.out_of_stack = true; + } fn spill_range(&mut self, sheet: usize, anchor: CellRef) -> Option { let k = (sheet, anchor); if !(self.pending.contains(&k) || self.results.contains_key(&k)) { diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 608af93201..6106f1bfc9 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -834,6 +834,40 @@ fn a_recursive_lambda_stops_at_the_same_call_on_any_stack_size() { assert_eq!(run(64 << 20), small); } +#[test] +fn a_recursive_lambda_that_reads_another_cells_recursive_lambda_shares_the_stack() { + let run = |stack: usize| { + std::thread::Builder::new() + .stack_size(stack) + .spawn(|| { + // Row-major order calculates A1 first, so each cell below is calculated on demand + // at the deepest call of the one above it. + let countdown = |then: &str| format!("=LET(F,LAMBDA(me,n,IF(n<=0,{then},1+me(me,n-1))),F(F,131))"); + let mut wb = Workbook::new(); + let sheet = wb.sheet_mut(0).expect("sheet"); + for (at, f) in [ + ("A1", countdown("A2")), + ("A2", countdown("A3")), + ("A3", countdown("0")), + ("B1", countdown("B2")), + ("B2", "=LET(f,LAMBDA(g,x,g(g,x)),f(f,1))".to_string()), + ] { + sheet.cells.set(c(at), Cell::formula(Formula::new(&f))); + } + let mut calc = Calc::new(); + calc.recalc_all(&mut wb); + ["A1", "A2", "A3", "B1", "B2"].map(|at| wb.sheet(0).map(|s| s.value(c(at))).unwrap_or_default()) + }) + .expect("spawn") + .join() + .expect("recursion must not overflow the stack") + }; + let small = run(1 << 20); + let num = Value::Error(CellError::Num); + assert_eq!(small, [Value::Number(393.0), Value::Number(262.0), Value::Number(131.0), num.clone(), num]); + assert_eq!(run(64 << 20), small); +} + #[test] fn names_and_sheets() { let mut t = T::new(); From 09f78cb3a8dd116d524b3850608312c8e062be89 Mon Sep 17 00:00:00 2001 From: "D. Fender" <26173007+postfen@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:12:09 -0600 Subject: [PATCH 090/687] Tables: Format as Table inside a table restyles it Home > Format as Table on a cell or range inside an existing table failed with "A table can't overlap another table." Excel applies the picked style to that table instead. The command now returns that table and its range, as it does for a new table. A range crossing a table's edge is still rejected. Without a range, the active area of the selection decides, as it does for Insert > Table. The native Format menu's Format as Table now runs home.formatAsTable instead of insert.table. --- apps/gridcraft/src/native_menu.rs | 2 +- crates/engine/src/cmd/insert.rs | 19 +++++- .../engine/tests/format_as_table_restyle.rs | 61 +++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 crates/engine/tests/format_as_table_restyle.rs diff --git a/apps/gridcraft/src/native_menu.rs b/apps/gridcraft/src/native_menu.rs index 6985af3c0f..7dc549cb78 100644 --- a/apps/gridcraft/src/native_menu.rs +++ b/apps/gridcraft/src/native_menu.rs @@ -125,7 +125,7 @@ fn tree() -> Vec<(&'static str, Vec)> { ("Unhide Sheet…", "sheet.unhide", None), ("-", "", None), ("Conditional Formatting…", "dialog:manageRules", None), - ("Format as Table", "insert.table", None), + ("Format as Table", "home.formatAsTable", None), ], ), ( diff --git a/crates/engine/src/cmd/insert.rs b/crates/engine/src/cmd/insert.rs index aae5a9faa1..2388d1f25d 100644 --- a/crates/engine/src/cmd/insert.rs +++ b/crates/engine/src/cmd/insert.rs @@ -26,9 +26,9 @@ pub fn specs() -> Vec { "Format as Table", ["Home", "Styles"], None, - "{range?, style: \"TableStyleMedium2\", header?}", + "{range?, style: \"TableStyleMedium2\", header?} (inside a table: restyles it)", has_doc, - insert_table + format_as_table ), cmd!("table.totalRow", "Total Row", ["Table", "Table Style Options"], None, "{table?, on?: bool}", has_doc, |s, p| table_flag( s, p, "totals" @@ -351,6 +351,21 @@ fn insert_table(s: &mut Session, p: &Json) -> Result { }) } +/// Format as Table inside a table restyles that table, as in Excel; elsewhere it makes a new one. +fn format_as_table(s: &mut Session, p: &Json) -> Result { + // Without a range, the active area decides (as in insert_table), not the first one. + let r = if p.get("range").is_some() { target_range(s, p)? } else { s.doc()?.selection.current() }; + let sheet = target_sheet(s, p)?; + let inside = s.doc()?.wb.sheet(sheet).and_then(|sh| sh.tables.iter().find(|t| t.range.contains_range(&r))).map(|t| (t.name.clone(), t.range)); + match inside { + Some((table, range)) => { + table_style(s, &with(p, "table", json!(table)))?; + Ok(json!({"table": table, "range": range.a1()})) + } + None => insert_table(s, p), + } +} + fn find_table(s: &Session, p: &Json) -> Result<(usize, usize)> { let d = s.doc()?; if let Some(n) = str_param(p, "table") { diff --git a/crates/engine/tests/format_as_table_restyle.rs b/crates/engine/tests/format_as_table_restyle.rs new file mode 100644 index 0000000000..0419027c6f --- /dev/null +++ b/crates/engine/tests/format_as_table_restyle.rs @@ -0,0 +1,61 @@ +use gridcraft_engine::Session; +use serde_json::json; + +fn table() -> Session { + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range":"A1", "values":[["Item", "Qty"], ["x", "7"], ["y", "8"]]})).unwrap(); + s.execute("insert.table", json!({"range":"A1:B3", "header":true, "name":"T"})).unwrap(); + s +} + +fn styles(s: &Session) -> Vec { + s.doc().unwrap().wb.active().unwrap().tables.iter().map(|t| t.style.clone()).collect() +} + +#[test] +fn format_as_table_inside_a_table_restyles_it() { + let mut s = table(); + s.execute("selection.set", json!({"range":"B2"})).unwrap(); + s.execute("home.formatAsTable", json!({"style":"TableStyleLight9"})).unwrap(); + assert_eq!(styles(&s), ["TableStyleLight9"]); +} + +#[test] +fn format_as_table_on_a_range_inside_a_table_restyles_it() { + let mut s = table(); + s.execute("home.formatAsTable", json!({"range":"A2:B3", "style":"TableStyleDark3"})).unwrap(); + assert_eq!(styles(&s), ["TableStyleDark3"]); +} + +#[test] +fn format_as_table_across_a_table_edge_is_rejected() { + let mut s = table(); + let e = s.execute("home.formatAsTable", json!({"range":"A1:C3", "style":"TableStyleLight9"})).unwrap_err(); + assert!(e.to_string().contains("overlap"), "{e}"); + assert_eq!(styles(&s), ["TableStyleMedium2"]); +} + +#[test] +fn format_as_table_inside_a_table_returns_that_table() { + let mut s = table(); + let r = s.execute("home.formatAsTable", json!({"range":"B2", "style":"TableStyleLight9"})).unwrap(); + assert_eq!(r, json!({"table":"T", "range":"A1:B3"})); +} + +#[test] +fn format_as_table_uses_the_active_area_of_a_multi_area_selection() { + let mut s = table(); + s.execute("selection.set", json!({"range":"D10:E11,B2"})).unwrap(); + s.execute("home.formatAsTable", json!({"style":"TableStyleLight9"})).unwrap(); + assert_eq!(styles(&s), ["TableStyleLight9"]); +} + +#[test] +fn format_as_table_inside_a_table_with_data_beside_it_restyles_it() { + let mut s = table(); + s.execute("range.setValues", json!({"range":"C1", "values":[["Note"], ["a"], ["b"]]})).unwrap(); + s.execute("selection.set", json!({"range":"B2"})).unwrap(); + s.execute("home.formatAsTable", json!({"style":"TableStyleLight9"})).unwrap(); + assert_eq!(styles(&s), ["TableStyleLight9"]); +} From 73af8c6a6dab27e9d19666ac33e16631a7570132 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 22 Oct 2026 15:38:06 -0400 Subject: [PATCH 091/687] Release 0.11.0 Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 32 ++++++++++++++++---------------- Cargo.toml | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e041aafbc8..abc4720088 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1496,7 +1496,7 @@ dependencies = [ [[package]] name = "gridcraft" -version = "0.10.0" +version = "0.11.0" dependencies = [ "arboard", "craft-print", @@ -1518,7 +1518,7 @@ dependencies = [ [[package]] name = "gridcraft-calc" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-core", "gridcraft-formula", @@ -1530,7 +1530,7 @@ dependencies = [ [[package]] name = "gridcraft-chart" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-calc", "gridcraft-core", @@ -1541,7 +1541,7 @@ dependencies = [ [[package]] name = "gridcraft-cli" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-engine", "gridcraft-mcp", @@ -1553,7 +1553,7 @@ dependencies = [ [[package]] name = "gridcraft-core" -version = "0.10.0" +version = "0.11.0" dependencies = [ "js-sys", "serde", @@ -1563,7 +1563,7 @@ dependencies = [ [[package]] name = "gridcraft-engine" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-calc", "gridcraft-chart", @@ -1584,7 +1584,7 @@ dependencies = [ [[package]] name = "gridcraft-formula" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-core", "serde", @@ -1593,7 +1593,7 @@ dependencies = [ [[package]] name = "gridcraft-functions" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-core", "regex", @@ -1601,7 +1601,7 @@ dependencies = [ [[package]] name = "gridcraft-mcp" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-engine", "log", @@ -1610,7 +1610,7 @@ dependencies = [ [[package]] name = "gridcraft-model" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-core", "gridcraft-formula", @@ -1622,18 +1622,18 @@ dependencies = [ [[package]] name = "gridcraft-numfmt" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-core", ] [[package]] name = "gridcraft-pdf" -version = "0.10.0" +version = "0.11.0" [[package]] name = "gridcraft-ui-egui" -version = "0.10.0" +version = "0.11.0" dependencies = [ "craft-ui", "egui", @@ -1655,7 +1655,7 @@ dependencies = [ [[package]] name = "gridcraft-web" -version = "0.10.0" +version = "0.11.0" dependencies = [ "eframe", "egui", @@ -1672,7 +1672,7 @@ dependencies = [ [[package]] name = "gridcraft-xlsx" -version = "0.10.0" +version = "0.11.0" dependencies = [ "aes", "gridcraft-core", @@ -4526,7 +4526,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" [[package]] name = "xtask" -version = "0.10.0" +version = "0.11.0" dependencies = [ "gridcraft-engine", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index e8a6dc67e6..11e52c6837 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/gridcraft", "apps/gridcraft-cli", "xtask"] [workspace.package] -version = "0.10.0" +version = "0.11.0" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.90" From 10fb7d566eb8f1afe215117349881e1b6be78ca0 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 22 Oct 2026 16:16:17 -0400 Subject: [PATCH 092/687] macOS packaging: retry stapler when Apple's CloudKit drops the connection stapler staple/validate fetch the notarization ticket from CloudKit, which sometimes fails with "The network connection was lost" (Error 68). That failed this release's macOS job twice in a row. Both steps are idempotent, so retry up to 5 times with backoff, like hdiutil_retry. Co-Authored-By: Claude Opus 5.5 --- packaging/macos/package.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packaging/macos/package.sh b/packaging/macos/package.sh index b128c3c7f3..d8b4b097b0 100755 --- a/packaging/macos/package.sh +++ b/packaging/macos/package.sh @@ -80,6 +80,20 @@ sign() { codesign --force --sign "$IDENTITY" ${kc[@]+"${kc[@]}"} "${ts[@]}" "$@" } +# stapler fetches the ticket from Apple's CloudKit, which sometimes drops the connection ("The +# network connection was lost", Error 68). Stapling and validating are idempotent, so retry. +stapler_retry() { + local attempt + for attempt in 1 2 3 4 5; do + if xcrun stapler "$@"; then + return 0 + fi + [ "$attempt" = 5 ] && { echo "stapler $1 failed 5 times" >&2; return 1; } + warn "stapler $1 failed (attempt $attempt of 5); retrying in $((attempt * 15))s" + sleep $((attempt * 15)) + done +} + notarize() { local file="$1" out id status echo "==> notarizing $(basename "$file") (this can take a few minutes)" @@ -120,8 +134,8 @@ codesign --verify --strict --deep --verbose=2 "$APP" if [ "$NOTARIZE" = 1 ]; then ditto -c -k --keepParent "$APP" "$WORK/GridCraft-notarize.zip" notarize "$WORK/GridCraft-notarize.zip" - xcrun stapler staple "$APP" - xcrun stapler validate "$APP" + stapler_retry staple "$APP" + stapler_retry validate "$APP" spctl --assess --type execute -vvv "$APP" fi @@ -146,8 +160,8 @@ sign "$DMG" codesign --verify --strict --verbose=2 "$DMG" if [ "$NOTARIZE" = 1 ]; then notarize "$DMG" - xcrun stapler staple "$DMG" - xcrun stapler validate "$DMG" + stapler_retry staple "$DMG" + stapler_retry validate "$DMG" spctl --assess --type open --context context:primary-signature -vvv "$DMG" fi From ac8da7d49cbeb4be6aa1ad5869f3d29fe2da8cd9 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 22 Oct 2026 16:16:52 -0400 Subject: [PATCH 093/687] Release 0.11.1 Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 32 ++++++++++++++++---------------- Cargo.toml | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index abc4720088..c7e3560b60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1496,7 +1496,7 @@ dependencies = [ [[package]] name = "gridcraft" -version = "0.11.0" +version = "0.11.1" dependencies = [ "arboard", "craft-print", @@ -1518,7 +1518,7 @@ dependencies = [ [[package]] name = "gridcraft-calc" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-core", "gridcraft-formula", @@ -1530,7 +1530,7 @@ dependencies = [ [[package]] name = "gridcraft-chart" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-calc", "gridcraft-core", @@ -1541,7 +1541,7 @@ dependencies = [ [[package]] name = "gridcraft-cli" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-engine", "gridcraft-mcp", @@ -1553,7 +1553,7 @@ dependencies = [ [[package]] name = "gridcraft-core" -version = "0.11.0" +version = "0.11.1" dependencies = [ "js-sys", "serde", @@ -1563,7 +1563,7 @@ dependencies = [ [[package]] name = "gridcraft-engine" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-calc", "gridcraft-chart", @@ -1584,7 +1584,7 @@ dependencies = [ [[package]] name = "gridcraft-formula" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-core", "serde", @@ -1593,7 +1593,7 @@ dependencies = [ [[package]] name = "gridcraft-functions" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-core", "regex", @@ -1601,7 +1601,7 @@ dependencies = [ [[package]] name = "gridcraft-mcp" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-engine", "log", @@ -1610,7 +1610,7 @@ dependencies = [ [[package]] name = "gridcraft-model" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-core", "gridcraft-formula", @@ -1622,18 +1622,18 @@ dependencies = [ [[package]] name = "gridcraft-numfmt" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-core", ] [[package]] name = "gridcraft-pdf" -version = "0.11.0" +version = "0.11.1" [[package]] name = "gridcraft-ui-egui" -version = "0.11.0" +version = "0.11.1" dependencies = [ "craft-ui", "egui", @@ -1655,7 +1655,7 @@ dependencies = [ [[package]] name = "gridcraft-web" -version = "0.11.0" +version = "0.11.1" dependencies = [ "eframe", "egui", @@ -1672,7 +1672,7 @@ dependencies = [ [[package]] name = "gridcraft-xlsx" -version = "0.11.0" +version = "0.11.1" dependencies = [ "aes", "gridcraft-core", @@ -4526,7 +4526,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" [[package]] name = "xtask" -version = "0.11.0" +version = "0.11.1" dependencies = [ "gridcraft-engine", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 11e52c6837..bf8eb381e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/gridcraft", "apps/gridcraft-cli", "xtask"] [workspace.package] -version = "0.11.0" +version = "0.11.1" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.90" From d046ba6aa73cd1b5ce2c7e3f1c8e89e0e5dcba68 Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 00:44:34 +0200 Subject: [PATCH 094/687] Hyperlinks: Ctrl+click on a link follows it instead of extending the selection (#1198) A Ctrl+click went to the add-to-selection branch, so the link under the pointer was never followed. It now goes through follow_link like a plain click (asks before opening web addresses, jumps to references). --- crates/ui-egui/src/grid.rs | 10 ++- crates/ui-egui/tests/hyperlink_ctrl_click.rs | 70 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 crates/ui-egui/tests/hyperlink_ctrl_click.rs diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index a0efbfae7a..386f151a3e 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2364,7 +2364,15 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & d.selection.anchor = sel.anchor; } } else if mods.command { - select_ranges(app, RangeRef::cell(c), true, c); + // Ctrl+click on a hyperlink's text follows it instead of adding the cell to the selection. + if let Some(h) = sh.hyperlinks.get(&c).cloned() + && primary_clicked + && p.x < geo.cell_rect(sh, c).left() + geo.cell_rect(sh, c).width() * 0.8 + { + follow_link(app, &h.target); + } else { + select_ranges(app, RangeRef::cell(c), true, c); + } } else { let _ = app.session.run("selection.set", json!({"cell": c.a1()})); if primary_clicked && wb.styles.get(sh.style_id(c)).num_fmt.as_str() == "checkbox" { diff --git a/crates/ui-egui/tests/hyperlink_ctrl_click.rs b/crates/ui-egui/tests/hyperlink_ctrl_click.rs new file mode 100644 index 0000000000..34e110225c --- /dev/null +++ b/crates/ui-egui/tests/hyperlink_ctrl_click.rs @@ -0,0 +1,70 @@ +//! Ctrl+click on a hyperlink cell follows the link (#1198), like a plain click does. + +use egui::{Event, Modifiers, PointerButton, Pos2}; +use gridcraft_engine::Session; +use gridcraft_engine::core::CellRef; +use gridcraft_ui_egui::{SheetApp, grid::Geo}; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +fn harness() -> Harness { + let mut session = Session::new(); + session.new_workbook(); + session.execute("cell.set", json!({"cell": "B2", "input": "web"})).unwrap(); + session.execute("insert.link", json!({"cell": "B2", "target": "https://example.org/a"})).unwrap(); + session.execute("cell.set", json!({"cell": "B4", "input": "jump"})).unwrap(); + session.execute("insert.link", json!({"cell": "B4", "target": "Sheet1!F9"})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + SheetApp::new(session, Default::default()), + ); + h.run_steps(4); + h +} + +fn left_of_cell(h: &Harness, cell: &str) -> Pos2 { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + let r = geo.cell_rect(sheet, CellRef::parse(cell).unwrap()); + egui::pos2(r.left() + r.width() * 0.3, r.center().y) +} + +fn click(h: &mut Harness, cell: &str, modifiers: Modifiers) { + let pos = left_of_cell(h, cell); + h.input_mut().events.push(Event::ModifiersChanged(modifiers)); + h.input_mut().events.extend([ + Event::PointerMoved(pos), + Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers }, + Event::PointerButton { pos, button: PointerButton::Primary, pressed: false, modifiers }, + ]); + h.run_steps(3); + h.input_mut().events.push(Event::ModifiersChanged(Modifiers::NONE)); + h.run_steps(1); +} + +#[test] +fn ctrl_click_on_a_web_link_asks_to_open_it() { + let mut h = harness(); + click(&mut h, "B2", Modifiers::COMMAND); + let d = h.state().dialog.as_ref().expect("Ctrl+click on a web link opens the confirmation"); + assert_eq!(d.name, "openLink"); +} + +#[test] +fn ctrl_click_on_an_internal_link_goes_there() { + let mut h = harness(); + click(&mut h, "B4", Modifiers::COMMAND); + assert_eq!(h.state().session.active().unwrap().selection.active.a1(), "F9"); +} + +#[test] +fn plain_click_still_follows() { + let mut h = harness(); + click(&mut h, "B4", Modifiers::NONE); + assert_eq!(h.state().session.active().unwrap().selection.active.a1(), "F9"); +} From 6febf5442f747e32a4b332c967340ab7e23fcba4 Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 01:25:21 +0200 Subject: [PATCH 095/687] Fill handle: Auto Fill Options button after a drag offers Copy Cells, Fill Series and the formatting modes (#1403) A left-drag of the fill handle filled at once and offered no choice. A button now appears below the filled range; its menu undoes that fill and reruns edit.autoFill in the chosen mode. It goes away when the workbook changes or the selection moves. --- crates/ui-egui/src/grid.rs | 105 +++++++++-- crates/ui-egui/tests/fill_options_button.rs | 182 ++++++++++++++++++++ 2 files changed, 277 insertions(+), 10 deletions(-) create mode 100644 crates/ui-egui/tests/fill_options_button.rs diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index a0efbfae7a..e8250dde46 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -102,6 +102,19 @@ pub enum Drag { }, } +/// The fill a left-drag of the handle just made, which the Auto Fill Options button can redo +/// another way. It only applies while nothing else changed the workbook (`revision`) and the +/// filled cells are still selected. +#[derive(Clone, Debug, PartialEq)] +pub struct FillTag { + pub src: RangeRef, + pub target: RangeRef, + pub uid: u64, + pub sheet: usize, + pub revision: u64, + pub undo_len: usize, +} + #[derive(Default)] pub struct GridState { pub drag: Drag, @@ -120,6 +133,8 @@ pub struct GridState { pub context_menu_frame: u64, /// After a right-drag of the fill handle: the source, the dragged range and where to open the menu. pub fill_menu: Option<(RangeRef, RangeRef, Pos2)>, + /// After a left-drag of the fill handle: the Auto Fill Options button beside the filled cells. + pub fill_tag: Option, /// The right-clicked chart, picture, shape or slicer: where to open its menu, its kind and id. pub object_menu: Option<(Pos2, &'static str, u32)>, pub marching_phase: f32, @@ -790,6 +805,7 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { header_menu(app, ui, previous_popups.2 == app.grid.header_menu); context_menu(app, ui); fill_menu(app, ui, previous_popups.3 == app.grid.fill_menu); + fill_options_tag(app, ui, &geo, sh); object_menu(app, ui); error_check_tag(app, ui, &geo, sh); let track_x = if rtl { (available.left(), rect.left()) } else { (rect.right(), available.right()) }; @@ -2533,8 +2549,9 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & if t != src { // Ctrl (Cmd on macOS) swaps copying and extending, as in Excel. let mode = if mods.command { "alternate" } else { "series" }; - if let Err(e) = app.run("edit.autoFill", json!({"source": src.a1(), "target": t.a1(), "mode": mode})) { - app.message = Some(("GridCraft".into(), crate::clean_error(&e))); + match app.run("edit.autoFill", json!({"source": src.a1(), "target": t.a1(), "mode": mode})) { + Ok(_) => set_fill_tag(app, src, t), + Err(e) => app.message = Some(("GridCraft".into(), crate::clean_error(&e))), } } } @@ -3317,6 +3334,81 @@ fn context_menu(app: &mut SheetApp, ui: &mut egui::Ui) { } } +/// The ways to fill a dragged range, as the fill menu and the Auto Fill Options button list them. +const FILL_MODES: [(&str, &str); 4] = + [("Copy Cells", "copy"), ("Fill Series", "extend"), ("Fill Formatting Only", "formats"), ("Fill Without Formatting", "values")]; + +/// Remembers the fill just made so the Auto Fill Options button can offer the other ways. +fn set_fill_tag(app: &mut SheetApp, src: RangeRef, target: RangeRef) { + app.grid.fill_tag = + app.session.active().map(|d| FillTag { src, target, uid: d.uid, sheet: d.wb.active_sheet, revision: d.revision, undo_len: d.undo.len() }); +} + +/// The Auto Fill Options button below the bottom-right corner of cells a left-drag of the fill +/// handle just filled. Its menu redoes that fill as Copy Cells, Fill Series, Fill Formatting Only +/// or Fill Without Formatting: it undoes the drag's fill, then runs `edit.autoFill` in that mode. +/// The button goes away as soon as anything else changes the workbook or the selection moves. +fn fill_options_tag(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet) { + let Some(tag) = app.grid.fill_tag.clone() else { return }; + let current = app.session.active().is_some_and(|d| { + d.uid == tag.uid + && d.wb.active_sheet == tag.sheet + && d.revision == tag.revision + && d.undo.len() == tag.undo_len + && d.selection.ranges.len() == 1 + && d.selection.ranges.first().is_some_and(|r| *r == tag.target) + }); + if !current { + app.grid.fill_tag = None; + return; + } + if app.editor.is_some() || app.dialog.is_some() || app.grid.drag != Drag::None { + return; + } + let size = vec2(18.0, 18.0); + let corner = geo.range_rect(sh, tag.target).right_bottom(); + let button = geo.mirror_rect(Rect::from_min_size(corner + vec2(2.0, 2.0), size)); + if !geo.cells.contains_rect(button) { + return; + } + let lang = crate::i18n::current(ui.ctx()); + let resp = ui.interact(button, egui::Id::new("gridcraft.fill_options_tag"), Sense::click()).on_hover_text(lang.tr("Auto Fill Options")); + resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, lang.tr("Auto Fill Options"))); + let t = Tokens::get(ui.ctx()); + let p = ui.painter(); + p.rect_filled(button, 3.0, if resp.hovered() { t.hover } else { t.window }); + p.rect_stroke(button, 3.0, Stroke::new(1.0, t.separator), egui::StrokeKind::Inside); + // Two small cells and a drop-down arrow. + let c = button.center(); + p.rect_stroke(Rect::from_min_size(pos2(c.x - 6.0, c.y - 6.0), vec2(6.0, 6.0)), 0.0, Stroke::new(1.0, t.text), egui::StrokeKind::Inside); + p.rect_stroke(Rect::from_min_size(pos2(c.x - 3.0, c.y - 3.0), vec2(6.0, 6.0)), 0.0, Stroke::new(1.0, t.text), egui::StrokeKind::Inside); + p.add(egui::Shape::convex_polygon( + vec![pos2(c.x + 1.0, c.y + 5.0), pos2(c.x + 7.0, c.y + 5.0), pos2(c.x + 4.0, c.y + 8.0)], + t.text, + Stroke::NONE, + )); + let mut chosen = None; + egui::Popup::menu(&resp).show(|ui| { + ui.set_min_width(190.0); + for (label, mode) in FILL_MODES { + if ui.add(egui::Button::new(lang.tr(label)).frame(false).min_size(vec2(180.0, 20.0))).clicked() { + chosen = Some(mode); + ui.close(); + } + } + }); + if let Some(mode) = chosen { + let _ = app.run("edit.undo", json!({})); + match app.run("edit.autoFill", json!({"source": tag.src.a1(), "target": tag.target.a1(), "mode": mode})) { + Ok(_) => set_fill_tag(app, tag.src, tag.target), + Err(e) => { + app.grid.fill_tag = None; + app.message = Some(("GridCraft".into(), crate::clean_error(&e))); + } + } + } +} + /// The menu a right-drag of the fill handle opens on release, as in Excel. fn fill_menu(app: &mut SheetApp, ui: &mut egui::Ui, was_open: bool) { let Some((src, target, at)) = app.grid.fill_menu else { return }; @@ -3324,14 +3416,7 @@ fn fill_menu(app: &mut SheetApp, ui: &mut egui::Ui, was_open: bool) { let response = egui::Area::new(egui::Id::new("fill_menu")).fixed_pos(at).order(egui::Order::Foreground).show(ui.ctx(), |ui| { egui::Frame::popup(ui.style()).show(ui, |ui| { ui.set_min_width(190.0); - let items = [ - ("Copy Cells", "copy"), - ("Fill Series", "extend"), - ("Fill Formatting Only", "formats"), - ("Fill Without Formatting", "values"), - ("-", ""), - ("Series…", "ui:series"), - ]; + let items = [FILL_MODES[0], FILL_MODES[1], FILL_MODES[2], FILL_MODES[3], ("-", ""), ("Series…", "ui:series")]; let lang = crate::i18n::current(ui.ctx()); for (label, mode) in items { if label == "-" { diff --git a/crates/ui-egui/tests/fill_options_button.rs b/crates/ui-egui/tests/fill_options_button.rs new file mode 100644 index 0000000000..35c3147077 --- /dev/null +++ b/crates/ui-egui/tests/fill_options_button.rs @@ -0,0 +1,182 @@ +//! After a left-drag of the fill handle, the Auto Fill Options button redoes the fill as Copy Cells, +//! Fill Series, Fill Formatting Only or Fill Without Formatting (#1403). + +use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::kittest::Queryable; +use gridcraft_engine::Session; +use gridcraft_engine::core::{CellRef, Value}; +use gridcraft_ui_egui::{SheetApp, grid::Geo}; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +fn harness(setup: impl FnOnce(&mut Session)) -> Harness { + let mut session = Session::new(); + session.new_workbook(); + setup(&mut session); + let mut h = egui_kittest::Harness::builder().with_step_dt(1.0 / 60.0).with_size(vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + SheetApp::new(session, Default::default()), + ); + h.run_steps(4); + h +} + +/// B1:B2 holds 1, 2 and is selected. +fn one_two(s: &mut Session) { + s.execute("range.setValues", json!({"range": "B1", "values": [[1], [2]]})).unwrap(); + s.execute("selection.set", json!({"range": "B1:B2"})).unwrap(); +} + +fn corner(h: &Harness, cell: &str) -> Pos2 { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + geo.cell_rect(sheet, CellRef::parse(cell).unwrap()).right_bottom() - vec2(1.0, 1.0) +} + +fn center(h: &Harness, cell: &str) -> Pos2 { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + geo.cell_rect(sheet, CellRef::parse(cell).unwrap()).center() +} + +fn press(h: &mut Harness, pos: Pos2, button: PointerButton) { + h.input_mut().events.extend([Event::PointerMoved(pos), Event::PointerButton { pos, button, pressed: true, modifiers: Modifiers::NONE }]); + h.step(); +} + +fn move_to(h: &mut Harness, from: Pos2, to: Pos2) { + for i in 1..=10 { + h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (i as f32 / 10.0))); + h.step(); + } +} + +fn release(h: &mut Harness, pos: Pos2, button: PointerButton) { + h.input_mut().events.push(Event::PointerButton { pos, button, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +/// Drags the handle of the current selection (whose bottom-right cell is `from`) to `to`. +fn drag_handle(h: &mut Harness, from: &str, to: &str, button: PointerButton) { + let (start, end) = (corner(h, from), center(h, to)); + press(h, start, button); + move_to(h, start, end); + release(h, end, button); +} + +fn value(h: &Harness, cell: &str) -> Value { + h.state().session.active().unwrap().wb.active().unwrap().value(CellRef::parse(cell).unwrap()) +} + +fn values(h: &Harness, cells: &[&str]) -> Vec { + cells.iter().map(|c| value(h, c)).collect() +} + +fn bold(h: &Harness, cell: &str) -> bool { + let d = h.state().session.active().unwrap(); + let sh = d.wb.active().unwrap(); + d.wb.styles.get(sh.style_id(CellRef::parse(cell).unwrap())).font.bold +} + +fn italic(h: &Harness, cell: &str) -> bool { + let d = h.state().session.active().unwrap(); + let sh = d.wb.active().unwrap(); + d.wb.styles.get(sh.style_id(CellRef::parse(cell).unwrap())).font.italic +} + +fn selection(h: &Harness) -> String { + h.state().session.active().unwrap().selection.a1() +} + +fn n(x: f64) -> Value { + Value::Number(x) +} + +#[test] +fn left_drag_shows_the_auto_fill_options_button() { + let mut h = harness(one_two); + assert!(h.query_by_label("Auto Fill Options").is_none()); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + + assert_eq!(values(&h, &["B3", "B6"]), [n(3.0), n(6.0)]); + assert!(h.query_by_label("Auto Fill Options").is_some()); +} + +#[test] +fn copy_cells_replaces_the_series_the_drag_made() { + let mut h = harness(one_two); + let steps_before = h.state().session.active().unwrap().undo.len(); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + h.get_by_label("Auto Fill Options").click(); + h.run_steps(3); + h.get_by_label("Copy Cells").click(); + h.run_steps(3); + + assert_eq!(values(&h, &["B3", "B4", "B5", "B6"]), [n(1.0), n(2.0), n(1.0), n(2.0)]); + assert_eq!(selection(&h), "B1:B6"); + assert_eq!(h.state().session.active().unwrap().undo.len(), steps_before + 1, "one fill step, not two"); +} + +#[test] +fn a_lone_number_copies_on_drag_and_counts_up_with_fill_series() { + let mut h = harness(|s| { + s.execute("cell.set", json!({"cell": "B1", "input": "5"})).unwrap(); + s.execute("selection.set", json!({"cell": "B1"})).unwrap(); + }); + drag_handle(&mut h, "B1", "B4", PointerButton::Primary); + assert_eq!(values(&h, &["B2", "B3", "B4"]), [n(5.0), n(5.0), n(5.0)]); + h.get_by_label("Auto Fill Options").click(); + h.run_steps(3); + h.get_by_label("Fill Series").click(); + h.run_steps(3); + + assert_eq!(values(&h, &["B2", "B3", "B4"]), [n(6.0), n(7.0), n(8.0)]); +} + +fn formatted(s: &mut Session) { + s.execute("range.setValues", json!({"range": "B1", "values": [[1], [2], [null], [9]]})).unwrap(); + s.execute("home.bold", json!({"range": "B1:B2", "on": true})).unwrap(); + s.execute("home.italic", json!({"range": "B4", "on": true})).unwrap(); + s.execute("selection.set", json!({"range": "B1:B2"})).unwrap(); +} + +#[test] +fn fill_without_formatting_keeps_the_targets_formats() { + let mut h = harness(formatted); + drag_handle(&mut h, "B2", "B4", PointerButton::Primary); + h.get_by_label("Auto Fill Options").click(); + h.run_steps(3); + h.get_by_label("Fill Without Formatting").click(); + h.run_steps(3); + + assert_eq!(values(&h, &["B3", "B4"]), [n(3.0), n(4.0)]); + assert!(!bold(&h, "B3") && !bold(&h, "B4")); + assert!(italic(&h, "B4")); +} + +#[test] +fn the_button_goes_away_once_the_selection_moves() { + let mut h = harness(one_two); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + assert!(h.query_by_label("Auto Fill Options").is_some()); + let away = center(&h, "E9"); + h.input_mut().events.push(Event::PointerMoved(away)); + h.input_mut().events.push(Event::PointerButton { pos: away, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.input_mut().events.push(Event::PointerButton { pos: away, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); + + assert!(h.query_by_label("Auto Fill Options").is_none()); +} + +#[test] +fn right_drag_makes_no_button() { + let mut h = harness(one_two); + drag_handle(&mut h, "B2", "B6", PointerButton::Secondary); + assert!(h.query_by_label("Auto Fill Options").is_none()); +} From d19f65601427d4aacf522d95e57169b6b89c088a Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 02:17:38 +0200 Subject: [PATCH 096/687] Fill handle: test the Auto Fill Options button's workbook-changed rule, report an undo error, document it (#1403) --- crates/ui-egui/src/grid.rs | 6 +++++- crates/ui-egui/tests/fill_options_button.rs | 12 ++++++++++++ docs/ui-parity.md | 5 +++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index e8250dde46..ab5874111f 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -3398,7 +3398,11 @@ fn fill_options_tag(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet } }); if let Some(mode) = chosen { - let _ = app.run("edit.undo", json!({})); + if let Err(e) = app.run("edit.undo", json!({})) { + app.grid.fill_tag = None; + app.message = Some(("GridCraft".into(), crate::clean_error(&e))); + return; + } match app.run("edit.autoFill", json!({"source": tag.src.a1(), "target": tag.target.a1(), "mode": mode})) { Ok(_) => set_fill_tag(app, tag.src, tag.target), Err(e) => { diff --git a/crates/ui-egui/tests/fill_options_button.rs b/crates/ui-egui/tests/fill_options_button.rs index 35c3147077..8d5a21a183 100644 --- a/crates/ui-egui/tests/fill_options_button.rs +++ b/crates/ui-egui/tests/fill_options_button.rs @@ -180,3 +180,15 @@ fn right_drag_makes_no_button() { drag_handle(&mut h, "B2", "B6", PointerButton::Secondary); assert!(h.query_by_label("Auto Fill Options").is_none()); } + +#[test] +fn the_button_goes_away_once_the_workbook_changes() { + let mut h = harness(one_two); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + assert!(h.query_by_label("Auto Fill Options").is_some()); + h.state_mut().session.execute("cell.set", json!({"cell": "D1", "input": "x"})).unwrap(); + h.run_steps(3); + + assert_eq!(selection(&h), "B1:B6", "the selection did not move"); + assert!(h.query_by_label("Auto Fill Options").is_none(), "its undo would take the unrelated edit with it"); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 07797d70d0..f1c7d4d37e 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (Auto Fill Options button after a left-drag of the fill handle (#1403); Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -50,7 +50,7 @@ this logic, with one undo step. | Click/drag/Shift/Ctrl selection, multi-area selection | rectangular drag works in one direction only in some cases (#28) | | Cursor while dragging cells, headers, freeze handles and objects | stable for the active drag (#936); Draw tools keep their crosshair; header arrows and a custom cell cursor remain unimplemented | | Selection outline glides to a new selection (Excel: "Provide feedback with animation") | done (10-11): 0.12 s, decelerating, with the fill and fill handle; View ▸ Animations (`view.animations`) turns it off. The timing is an estimate, not yet measured against Excel; the system's reduced-motion setting isn't followed yet | -| Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series); right-drag menu partial: Copy Cells, Fill Series, Fill Formatting Only, Fill Without Formatting, Series…; Fill Days/Weekdays/Months/Years, Linear/Growth Trend and Flash Fill missing | +| Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series); right-drag menu partial: Copy Cells, Fill Series, Fill Formatting Only, Fill Without Formatting, Series…; a left-drag leaves an Auto Fill Options button with the first four (gone once the workbook changes or the selection moves); Fill Days/Weekdays/Months/Years, Linear/Growth Trend and Flash Fill missing | | Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series); month patterns use the workbook's 1900 or 1904 date system; right-drag menu missing | | Drag-move and drag-copy cells (border drag, Ctrl/Option, Shift to insert) | done (M3) | | In-cell editing, Enter/Tab movement, Alt/Option+Enter line breaks | done | @@ -176,6 +176,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | After a left-drag of the fill handle an Auto Fill Options button sits below the filled cells; its menu redoes the fill as Copy Cells, Fill Series, Fill Formatting Only or Fill Without Formatting (undoes the drag's fill, then `edit.autoFill` in that mode). It goes away when the workbook changes or the selection moves (#1403) | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | | 2026-10-21 | minor | Form dialogs (conditional-format quick rules, New Name, Remove Duplicates, Text to Columns and every other `Dialog::form`) fit their content: OK and Cancel sit in a row under the last field instead of a right-to-left layout that took the screen's whole height and left the buttons floating halfway down | From 60776ff4de75da9fc05633aa7fbea0a301cd1b26 Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 02:23:37 +0200 Subject: [PATCH 097/687] Hyperlinks: test that Ctrl+click beside the link text still extends the selection (#1198) --- crates/ui-egui/tests/hyperlink_ctrl_click.rs | 21 +++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/tests/hyperlink_ctrl_click.rs b/crates/ui-egui/tests/hyperlink_ctrl_click.rs index 34e110225c..2e557e12e1 100644 --- a/crates/ui-egui/tests/hyperlink_ctrl_click.rs +++ b/crates/ui-egui/tests/hyperlink_ctrl_click.rs @@ -26,16 +26,20 @@ fn harness() -> Harness { h } -fn left_of_cell(h: &Harness, cell: &str) -> Pos2 { +fn at_fraction(h: &Harness, cell: &str, fraction: f32) -> Pos2 { let app = h.state(); let sheet = app.session.active().unwrap().wb.active().unwrap(); let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); let r = geo.cell_rect(sheet, CellRef::parse(cell).unwrap()); - egui::pos2(r.left() + r.width() * 0.3, r.center().y) + egui::pos2(r.left() + r.width() * fraction, r.center().y) } fn click(h: &mut Harness, cell: &str, modifiers: Modifiers) { - let pos = left_of_cell(h, cell); + click_at(h, cell, 0.3, modifiers); +} + +fn click_at(h: &mut Harness, cell: &str, fraction: f32, modifiers: Modifiers) { + let pos = at_fraction(h, cell, fraction); h.input_mut().events.push(Event::ModifiersChanged(modifiers)); h.input_mut().events.extend([ Event::PointerMoved(pos), @@ -68,3 +72,14 @@ fn plain_click_still_follows() { click(&mut h, "B4", Modifiers::NONE); assert_eq!(h.state().session.active().unwrap().selection.active.a1(), "F9"); } + +#[test] +fn ctrl_click_beside_the_link_text_still_extends_the_selection() { + let mut h = harness(); + click_at(&mut h, "B2", 0.9, Modifiers::NONE); + click_at(&mut h, "B4", 0.9, Modifiers::COMMAND); + let d = h.state().session.active().unwrap(); + let ranges: Vec = d.selection.ranges.iter().map(|r| r.a1()).collect(); + assert_eq!(ranges, ["B2", "B4"], "the hyperlink cell joins the selection"); + assert!(h.state().dialog.is_none(), "no link was followed"); +} From 7c1ec1df7dbd27cacccc9b8333cb361a939f3d42 Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 02:37:49 +0200 Subject: [PATCH 098/687] Fill handle: test the Auto Fill Options button on a right-to-left sheet, merge the duplicate fill handle row (#1403) --- crates/ui-egui/tests/fill_options_button.rs | 21 +++++++++++++++++++-- docs/ui-parity.md | 3 +-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/tests/fill_options_button.rs b/crates/ui-egui/tests/fill_options_button.rs index 8d5a21a183..2cb209fa93 100644 --- a/crates/ui-egui/tests/fill_options_button.rs +++ b/crates/ui-egui/tests/fill_options_button.rs @@ -35,14 +35,14 @@ fn corner(h: &Harness, cell: &str) -> Pos2 { let app = h.state(); let sheet = app.session.active().unwrap().wb.active().unwrap(); let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); - geo.cell_rect(sheet, CellRef::parse(cell).unwrap()).right_bottom() - vec2(1.0, 1.0) + geo.mirror_pos(geo.cell_rect(sheet, CellRef::parse(cell).unwrap()).right_bottom() - vec2(1.0, 1.0)) } fn center(h: &Harness, cell: &str) -> Pos2 { let app = h.state(); let sheet = app.session.active().unwrap().wb.active().unwrap(); let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); - geo.cell_rect(sheet, CellRef::parse(cell).unwrap()).center() + geo.screen_cell_rect(sheet, CellRef::parse(cell).unwrap()).center() } fn press(h: &mut Harness, pos: Pos2, button: PointerButton) { @@ -192,3 +192,20 @@ fn the_button_goes_away_once_the_workbook_changes() { assert_eq!(selection(&h), "B1:B6", "the selection did not move"); assert!(h.query_by_label("Auto Fill Options").is_none(), "its undo would take the unrelated edit with it"); } + +#[test] +fn the_button_stays_inside_the_grid_on_a_right_to_left_sheet() { + let mut h = harness(|s| { + one_two(s); + s.execute("view.rightToLeft", json!({"on": true})).unwrap(); + }); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + + let button = h.get_by_label("Auto Fill Options").rect(); + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + assert!(geo.cells.contains_rect(button), "{button:?} outside {:?}", geo.cells); + let filled = geo.screen_cell_rect(sheet, CellRef::parse("B6").unwrap()); + assert!(button.center().x < filled.left() && button.top() >= filled.bottom() - 1.0, "below-left of the mirrored range's corner"); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index f1c7d4d37e..cf0ab377d0 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -50,8 +50,7 @@ this logic, with one undo step. | Click/drag/Shift/Ctrl selection, multi-area selection | rectangular drag works in one direction only in some cases (#28) | | Cursor while dragging cells, headers, freeze handles and objects | stable for the active drag (#936); Draw tools keep their crosshair; header arrows and a custom cell cursor remain unimplemented | | Selection outline glides to a new selection (Excel: "Provide feedback with animation") | done (10-11): 0.12 s, decelerating, with the fill and fill handle; View ▸ Animations (`view.animations`) turns it off. The timing is an estimate, not yet measured against Excel; the system's reduced-motion setting isn't followed yet | -| Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series); right-drag menu partial: Copy Cells, Fill Series, Fill Formatting Only, Fill Without Formatting, Series…; a left-drag leaves an Auto Fill Options button with the first four (gone once the workbook changes or the selection moves); Fill Days/Weekdays/Months/Years, Linear/Growth Trend and Flash Fill missing | -| Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series); month patterns use the workbook's 1900 or 1904 date system; right-drag menu missing | +| Fill handle: drag to fill series, double-click to fill down, right-drag menu | fill done (Ctrl+drag swaps copy and series; month patterns use the workbook's 1900 or 1904 date system); right-drag menu partial: Copy Cells, Fill Series, Fill Formatting Only, Fill Without Formatting, Series…; a left-drag leaves an Auto Fill Options button with the first four (gone once the workbook changes or the selection moves); Fill Days/Weekdays/Months/Years, Linear/Growth Trend and Flash Fill missing | | Drag-move and drag-copy cells (border drag, Ctrl/Option, Shift to insert) | done (M3) | | In-cell editing, Enter/Tab movement, Alt/Option+Enter line breaks | done | | Point mode (click/drag/arrow references), reference colouring, F4 | done; Command/Ctrl-click adds separate arguments with the edit's locale separator, and Shift-click extends the current reference from a fixed anchor on the same sheet. Pointer-event regressions cover repeated/reversed selections, manual separators, sheet switches and keyboard continuation. | From 8eda57754596533dc473436478e81b37a31f5737 Mon Sep 17 00:00:00 2001 From: RedBearAK <64876997+RedBearAK@users.noreply.github.com> Date: Thu, 22 Oct 2026 23:27:56 -0800 Subject: [PATCH 099/687] Page Up into frozen rows scrolls the rows below back to the top With row 1 frozen, Page Up could land on A1, which is always on screen, while the scrolled rows stayed a page down (row 10, Page Down, Page Up, Page Up ends on row 1 with rows 2-9 hidden). The same held for Ctrl+Up into frozen rows and Ctrl+Left / Alt+Page Up into frozen columns. Arrow and paging keys now record the cell they moved from (`GridState::reveal_from`). When such a move goes from the scrolled rows (columns) into the frozen ones, the scrolled area goes back to its top (left). Other reveals (clicks, Find, Go To, Name Box, Undo, the control channel, formula pointing) leave it unset and keep the view where the user scrolled it. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/grid.rs | 14 +++ crates/ui-egui/src/keymap.rs | 2 + crates/ui-egui/tests/selection_scroll.rs | 148 +++++++++++++++++++++++ docs/ui-parity.md | 3 +- 4 files changed, 166 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index a0efbfae7a..d42ef180b3 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -109,6 +109,10 @@ pub struct GridState { /// With `ensure_visible`: scroll to the moving end of the selection instead of the active cell /// (Shift/Ctrl+Shift+arrows keep the active cell on the anchor while the far corner moves). pub reveal_moving_end: bool, + /// With `ensure_visible`: the cell an arrow or paging key moved from (the moving end when + /// extending), set only by those keys. A move from the scrolled rows or columns into the + /// frozen ones scrolls them back to their top or left. + pub reveal_from: Option, pub last_title: Option, pub drag_select: bool, pub rect: Option, @@ -681,6 +685,7 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { if app.grid.ensure_visible { app.grid.ensure_visible = false; let cell = if std::mem::take(&mut app.grid.reveal_moving_end) { sel.moving_end() } else { sel.active }; + let from = app.grid.reveal_from.take(); let target = app.editor.as_ref().and_then(|e| e.point_cell).unwrap_or(cell); if paper { crate::paper_view::ensure_covers(app, &wb, si, target); @@ -694,6 +699,11 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { } else if cr.bottom() > area.bottom() - 2.0 { view.scroll.y += (cr.bottom() - area.bottom() + 2.0) / geo.z; } + } else if from.is_some_and(|f| f.row >= geo.fr) { + // A move from the scrolled rows into the frozen ones (Page Up or Ctrl+Up onto a frozen + // row 1) brings the scrolled rows back to their top; the rows it passed over would + // otherwise stay out of sight. + view.scroll.y = 0.0; } if target.col >= geo.fc { if cr.left() < area.left() { @@ -701,6 +711,8 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { } else if cr.right() > area.right() - 2.0 { view.scroll.x += ((cr.right() - area.right() + 2.0) / geo.z).min(cr.left() - area.left()); } + } else if from.is_some_and(|f| f.col >= geo.fc) { + view.scroll.x = 0.0; } view.scroll = view.scroll.max(egui::Vec2::ZERO); geo = make_geo(app, view.scroll); @@ -2967,9 +2979,11 @@ fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: let cmd = m.command; let shift = m.shift; let mv = |app: &mut SheetApp, dr: i64, dc: i64| { + let from = app.session.active().map(|d| if shift { d.selection.moving_end() } else { d.selection.active }); let _ = app.session.run("selection.move", json!({"dr": dr, "dc": dc, "extend": shift, "jump": cmd})); app.grid.ensure_visible = true; app.grid.reveal_moving_end = shift; + app.grid.reveal_from = from; }; match key { Key::ArrowUp => mv(app, -1, 0), diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs index fea791b69e..5840036a16 100644 --- a/crates/ui-egui/src/keymap.rs +++ b/crates/ui-egui/src/keymap.rs @@ -223,9 +223,11 @@ pub(crate) fn dispatch(app: &mut SheetApp, ctx: &egui::Context, key: Key, physic let width = ((geo.cells.width() - geo.frozen_w) / geo.z).max(1.0); let cols = (width / sh.default_col_width.max(1.0)).floor().max(1.0) as i64; let dc = if shortcut == Shortcut::ScreenRight { cols } else { -cols }; + let from = app.session.active().map(|d| if m.shift { d.selection.moving_end() } else { d.selection.active }); let _ = app.session.run("selection.move", json!({"dr": 0, "dc": dc, "extend": m.shift})); app.grid.ensure_visible = true; app.grid.reveal_moving_end = m.shift; + app.grid.reveal_from = from; } Shortcut::Edit => app.begin_edit(None, false), Shortcut::ContextMenu => app.grid.context_menu = Some(geo.cell_rect(sh, active).center()), diff --git a/crates/ui-egui/tests/selection_scroll.rs b/crates/ui-egui/tests/selection_scroll.rs index 719f1a0f26..9a78f9b9fe 100644 --- a/crates/ui-egui/tests/selection_scroll.rs +++ b/crates/ui-egui/tests/selection_scroll.rs @@ -142,3 +142,151 @@ fn select_over_the_control_channel_scrolls_the_active_cell_into_view() { assert_eq!(active(&h), CellRef::parse("BZ150").unwrap()); assert!(visible(&h, "BZ150") && !visible(&h, "A1"), "selecting a far range scrolls its active cell into view"); } + +fn scroll(h: &Harness) -> egui::Vec2 { + h.state().view().scroll +} + +#[test] +fn page_down_and_back_up_from_row_10_shows_rows_2_to_9_again() { + // The reported steps: row 1 frozen, row 10 selected, Page Down, Page Up, Page Up. + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + h.state_mut().session.execute("selection.set", json!({"cell": "A10"})).unwrap(); + h.run_steps(2); + key(&mut h, Key::PageDown, Modifiers::NONE); + key(&mut h, Key::PageUp, Modifiers::NONE); + assert_eq!(active(&h), CellRef::parse("A10").unwrap()); + key(&mut h, Key::PageUp, Modifiers::NONE); + assert_eq!(active(&h), CellRef::parse("A1").unwrap()); + assert_eq!(scroll(&h).y, 0.0); + assert!(visible(&h, "A2") && visible(&h, "A9")); +} + +#[test] +fn page_up_into_a_frozen_top_row_scrolls_the_rows_below_it_back_to_the_top() { + // Row 1 frozen: Page Up from far down ends on A1, and rows 2 onwards show under it again + // instead of the view staying a page down. + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + key(&mut h, Key::ArrowDown, CTRL); + assert!(!visible(&h, "A2")); + for _ in 0..20 { + key(&mut h, Key::PageUp, Modifiers::NONE); + } + assert_eq!(active(&h), CellRef::parse("A1").unwrap()); + assert_eq!(scroll(&h).y, 0.0, "the scrolled rows are back at the top"); + assert!(visible(&h, "A2")); + // Ctrl+Up from the bottom lands on A1 the same way. + key(&mut h, Key::ArrowDown, CTRL); + assert!(!visible(&h, "A2")); + key(&mut h, Key::ArrowUp, CTRL); + assert_eq!(active(&h), CellRef::parse("A1").unwrap()); + assert!(visible(&h, "A2")); +} + +#[test] +fn ctrl_left_into_a_frozen_first_column_scrolls_the_columns_beside_it_back() { + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "B1"})).unwrap(); + key(&mut h, Key::ArrowRight, CTRL); + assert!(!visible(&h, "B1")); + key(&mut h, Key::ArrowLeft, CTRL); + assert_eq!(active(&h), CellRef::parse("A1").unwrap()); + assert_eq!(scroll(&h).x, 0.0); + assert!(visible(&h, "B1")); +} + +#[test] +fn moving_within_a_frozen_row_leaves_the_scrolled_rows_where_they_are() { + // Selecting in row 1 while the rows below are scrolled down (without a reveal, as a click + // does), then moving along the frozen row, must not jump the view back to the top. + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + key(&mut h, Key::ArrowDown, CTRL); + let down = scroll(&h).y; + assert!(down > 0.0); + h.state_mut().session.execute("selection.set", json!({"cell": "A1"})).unwrap(); + h.run_steps(2); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + assert_eq!(active(&h), CellRef::parse("B1").unwrap()); + assert_eq!(scroll(&h).y, down, "the view stays scrolled down"); +} + +#[test] +fn alt_page_up_into_a_frozen_first_column_scrolls_the_columns_beside_it_back() { + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "B1"})).unwrap(); + key(&mut h, Key::ArrowRight, CTRL); + assert!(!visible(&h, "B1")); + for _ in 0..20 { + key(&mut h, Key::PageUp, Modifiers::ALT); + } + assert_eq!(active(&h), CellRef::parse("A1").unwrap()); + assert_eq!(scroll(&h).x, 0.0); + assert!(visible(&h, "B1")); +} + +#[test] +fn extending_up_into_a_frozen_top_row_scrolls_the_rows_below_it_back_to_the_top() { + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + key(&mut h, Key::ArrowDown, CTRL); + assert!(!visible(&h, "A2")); + key(&mut h, Key::ArrowUp, CTRL_SHIFT); + assert_extended(&h, "A200", "A1"); + assert!(visible(&h, "A2")); +} + +#[test] +fn other_reveals_onto_a_frozen_cell_leave_the_scrolled_rows_where_they_are() { + // Only arrow and paging keys bring the scrolled rows back; selecting a header cell from the + // control channel (as Find, Go To or Undo would) keeps the place the user scrolled to. + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + key(&mut h, Key::ArrowDown, CTRL); + let down = scroll(&h).y; + control(&mut h, "engine.execute", json!({"command": "selection.set", "params": {"cell": "B1"}})); + assert_eq!(active(&h), CellRef::parse("B1").unwrap()); + assert_eq!(scroll(&h).y, down); +} + +#[test] +fn pointing_along_a_frozen_row_keeps_the_edited_cell_in_view() { + // Editing a formula far down and pointing at header cells with the arrows must not scroll + // the cell being edited away. + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + key(&mut h, Key::ArrowDown, CTRL); + let down = scroll(&h).y; + h.state_mut().begin_edit(Some("=SUM(".into()), false); + h.run_steps(2); + let ed = h.state_mut().editor.as_mut().expect("editing"); + ed.caret = ed.text.chars().count(); + ed.point_cell = Some(CellRef::parse("B1").unwrap()); + h.run_steps(2); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + assert_eq!(h.state().editor.as_ref().and_then(|e| e.point_cell), Some(CellRef::parse("C1").unwrap()), "the arrow moved the point"); + assert_eq!(scroll(&h).y, down); + assert!(visible(&h, "A200"), "the edited cell stays in view"); +} + +#[test] +fn down_from_a_frozen_header_row_goes_to_row_2_at_the_top_from_far_down() { + // Excel's quick way back to the top: select a cell in the frozen header row while far down, + // press Down, and the view jumps to row 2 under the header. Selecting the header cell alone + // doesn't scroll. + let mut h = harness(); + h.state_mut().session.execute("view.freezePanes", json!({"cell": "A2"})).unwrap(); + h.state_mut().session.execute("selection.set", json!({"cell": "A100000"})).unwrap(); + h.state_mut().grid.ensure_visible = true; + h.run_steps(2); + assert!(!visible(&h, "A2") && visible(&h, "A100000")); + h.state_mut().session.execute("selection.set", json!({"cell": "A1"})).unwrap(); + h.run_steps(2); + assert!(visible(&h, "A100000")); + key(&mut h, Key::ArrowDown, Modifiers::NONE); + assert_eq!(active(&h), CellRef::parse("A2").unwrap()); + assert_eq!(scroll(&h).y, 0.0); + assert!(visible(&h, "A2")); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 07797d70d0..0b4ee83f2a 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (Page Up and Ctrl+Up into frozen rows, Ctrl+Left and Alt+Page Up into frozen columns bring the scrolled area back) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -176,6 +176,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | Frozen panes: Page Up, Ctrl+Up and Shift variants that move from the scrolled rows into the frozen ones bring the scrolled rows back to the top, so the rows in between are seen again (Page Up from far down used to end on row 1 with the view still a page down); Ctrl+Left and Alt+Page Up do the same for frozen columns. Other ways of selecting a frozen cell (click, Find, Go To, Undo, formula pointing) leave the view where it is | | 2026-10-21 | minor | Background error checking, first part (#1994): a constant whose text would be a number if typed (signs, thousands, currency, percent, exponent, parenthesised negative) gets Excel's green corner triangle; the active cell then shows a warning button whose menu names the problem and offers Convert to Number for the selection (`formulas.convertToNumber`). Dates, times and fractions kept as text and formula results are not flagged, as in Excel | | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | | 2026-10-21 | minor | Form dialogs (conditional-format quick rules, New Name, Remove Duplicates, Text to Columns and every other `Dialog::form`) fit their content: OK and Cancel sit in a row under the last field instead of a right-to-left layout that took the screen's whole height and left the buttons floating halfway down | From f1e29fa82868fe1a099fc90740a3849f6c8b8ff3 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 23 Oct 2026 10:14:10 -0400 Subject: [PATCH 100/687] Review fixes for #2267 and #2292 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - range.setValues with parse: false still turns on Wrap Text for a line break (CodeRabbit). - Japanese: the fill-handle hint says AutoFill (オートフィル), not paint (CodeRabbit). Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/edit.rs | 19 ++++++++++++++----- crates/ui-egui/src/i18n/ja.rs | 5 ++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 67920264d0..1998f0b2c6 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -333,11 +333,14 @@ fn range_set_values(s: &mut Session, p: &Json) -> Result { // Text as given: no date or number reading, and a leading `'` is part of the // text rather than the marker that makes it text. A formula is still a // formula, which is what the ticket asks for and what `=` means in a cell. - Json::String(text) if !parse && !text.starts_with('=') => Some(Cell { - value: Value::Text(text.as_str().into()), - formula: None, - style: cx.wb.sheet(sheet).map(|sh| sh.style_id(at)).unwrap_or_default(), - }), + Json::String(text) if !parse && !text.starts_with('=') => { + let mut style = cx.wb.sheet(sheet).map(|sh| sh.style_id(at)).unwrap_or_default(); + // Line breaks still turn on Wrap Text, as typed input does. + if text.contains('\n') && !cx.wb.styles.get(style).align.wrap { + style = cx.wb.styles.derive(style, |s| s.align.wrap = true); + } + Some(Cell { value: Value::Text(text.as_str().into()), formula: None, style }) + } _ => input_to_cell(&input, sheet, at, &mut cx.wb)?, }; let sh = cx.sheet_mut(sheet)?; @@ -1827,6 +1830,12 @@ mod tests { assert_eq!(get(&mut s, "A7")["value"], json!(2.5)); assert!(s.execute("range.setValues", json!({"range": "A8", "parse": "no", "values": [["x"]]})).is_err()); assert!(s.execute("range.setValues", json!({"range": "A8", "parse": 0, "values": [["x"]]})).is_err()); + // A line break still turns on Wrap Text. + s.execute("range.setValues", json!({"range": "A9", "parse": false, "values": [["one\ntwo"]]})).unwrap(); + assert_eq!(get(&mut s, "A9")["value"], json!("one\ntwo")); + let d = s.doc().unwrap(); + let sh = d.wb.active().unwrap(); + assert!(d.wb.styles.get(sh.style_id(CellRef::parse("A9").unwrap())).align.wrap); } #[test] diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 09461b4bdf..482852d8d7 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -1127,7 +1127,10 @@ pub(super) const EXTRA: &[(&str, &str)] = &[ ("Sunburst", "サンバースト"), ("Stock", "株価"), ("Bar", "横棒"), - ("Drag to fill cells. Drag from inside a cell to select a range.", "ドラッグしてセルを塗りつぶします。セルの内側からドラッグすると範囲を選択します。"), + ( + "Drag to fill cells. Drag from inside a cell to select a range.", + "ドラッグしてセルをオートフィルします。セルの内側からドラッグすると範囲を選択します。", + ), ]; /// Translates an interface label; `None` for a label the catalog does not know, so `i18n.rs` can From 9135c7f6f013de8d1608c39e5d65f1c96815255a Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 17:07:42 +0200 Subject: [PATCH 101/687] Ribbon: fall back to Home when the active contextual tab is gone (fixes #2329) --- crates/ui-egui/src/ribbon.rs | 7 ++ crates/ui-egui/tests/contextual_ribbon_tab.rs | 74 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 crates/ui-egui/tests/contextual_ribbon_tab.rs diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 356381d661..45856565f4 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -204,6 +204,11 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui) { let t = Tokens::get(ui.ctx()); let collapsed = app.ui.ribbon_collapsed; let h = if collapsed { 34.0 } else { 34.0 + 92.0 }; + // A contextual tab exists only while its object is selected (Excel hides Table Design once the + // cell leaves the table); a remembered one that is gone would draw an empty ribbon. + if CONTEXTUAL_TABS.contains(&app.ui.ribbon_tab.as_str()) && !contextual_tabs(app).contains(&app.ui.ribbon_tab.as_str()) { + app.ui.ribbon_tab = "Home".to_string(); + } egui::Panel::top("ribbon") .exact_size(h) .frame(egui::Frame::NONE.fill(t.window).inner_margin(egui::Margin { left: 10, right: 10, top: 0, bottom: 6 })) @@ -322,6 +327,8 @@ fn theme_choices(app: &mut SheetApp, ui: &mut Ui) { } } +const CONTEXTUAL_TABS: [&str; 3] = ["Table Design", "Chart Design", crate::paper_view::TAB]; + fn contextual_tabs(app: &SheetApp) -> Vec<&'static str> { let mut v = Vec::new(); if let Some(d) = app.session.active() diff --git a/crates/ui-egui/tests/contextual_ribbon_tab.rs b/crates/ui-egui/tests/contextual_ribbon_tab.rs new file mode 100644 index 0000000000..6e1626dd57 --- /dev/null +++ b/crates/ui-egui/tests/contextual_ribbon_tab.rs @@ -0,0 +1,74 @@ +//! A contextual ribbon tab (Table Design, Chart Design) only exists while its object is selected. +//! The tab remembered in the saved UI state, or left behind when the selection moves away, must +//! not leave the ribbon blank: it falls back to Home. + +use egui::vec2; +use gridcraft_engine::Session; +use gridcraft_ui_egui::SheetApp; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +fn harness(session: Session) -> Harness { + let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + SheetApp::new(session, Default::default()), + ); + h.run_steps(4); + h +} + +fn session_with_table() -> Session { + let mut session = Session::new(); + session.new_workbook(); + session.execute("range.setValues", json!({"range": "A1", "values": [["Name", "Qty"], ["a", 1], ["b", 2]]})).unwrap(); + session.execute("insert.table", json!({"range": "A1:B3", "header": true})).unwrap(); + session.execute("selection.set", json!({"range": "A2"})).unwrap(); + session +} + +fn app_restored_on(tab: &str, session: Session) -> Harness { + let mut app = SheetApp::new(session, Default::default()); + app.ui.ribbon_tab = tab.into(); + let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + app, + ); + h.run_steps(4); + h +} + +#[test] +fn a_saved_table_design_tab_without_a_table_opens_on_home() { + let mut session = Session::new(); + session.new_workbook(); + let h = app_restored_on("Table Design", session); + assert_eq!(h.state().ui.ribbon_tab, "Home"); +} + +#[test] +fn a_saved_chart_design_tab_without_a_chart_opens_on_home() { + let mut session = Session::new(); + session.new_workbook(); + let h = app_restored_on("Chart Design", session); + assert_eq!(h.state().ui.ribbon_tab, "Home"); +} + +#[test] +fn leaving_the_table_while_on_table_design_returns_to_home() { + let mut h = harness(session_with_table()); + h.state_mut().ui.ribbon_tab = "Table Design".into(); + h.run_steps(2); + assert_eq!(h.state().ui.ribbon_tab, "Table Design", "the tab stays while the cell is in the table"); + h.state_mut().session.execute("selection.set", json!({"range": "D10"})).unwrap(); + h.run_steps(2); + assert_eq!(h.state().ui.ribbon_tab, "Home"); +} From a0ceea1468a72baf4654950cf9d84611e649dd91 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 17:16:30 +0200 Subject: [PATCH 102/687] Open on the Home tab every launch, as in Excel (fixes #2329) --- apps/gridcraft-web/src/prefs.rs | 2 +- apps/gridcraft/src/main.rs | 2 +- crates/ui-egui/src/lib.rs | 8 +++++++ crates/ui-egui/tests/contextual_ribbon_tab.rs | 22 +++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/apps/gridcraft-web/src/prefs.rs b/apps/gridcraft-web/src/prefs.rs index 7d8c0abf86..987343c568 100644 --- a/apps/gridcraft-web/src/prefs.rs +++ b/apps/gridcraft-web/src/prefs.rs @@ -22,7 +22,7 @@ pub fn restore(app: &mut SheetApp, ui: Option<&str>, prefs: Option<&str>) { { // No usable saved language: keep the browser's, as the desktop keeps the system's. let language = gridcraft_ui_egui::i18n::saved_language(&json).unwrap_or(app.ui.language); - app.ui = state; + app.ui = state.at_startup(); app.ui.language = language; } if let Some(s) = prefs.filter(readable) diff --git a/apps/gridcraft/src/main.rs b/apps/gridcraft/src/main.rs index 7c0eb04e6c..6c0d2f6764 100644 --- a/apps/gridcraft/src/main.rs +++ b/apps/gridcraft/src/main.rs @@ -111,7 +111,7 @@ fn load_prefs(app: &mut SheetApp) { { let saved = serde_json::from_slice(&b).ok().and_then(|v| gridcraft_ui_egui::i18n::saved_language(&v)); let language = saved.unwrap_or(app.ui.language); - app.ui = ui; + app.ui = ui.at_startup(); app.ui.language = language; } if let Ok(b) = std::fs::read(dir.join("prefs.json")) diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index faae9bfae8..6763593555 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -117,6 +117,14 @@ impl Default for UiState { } impl UiState { + /// The state saved by an earlier run, as the app starts with it. The ribbon tab is written to + /// the preferences but not restored: Excel opens on Home every launch, so a contextual tab + /// (Table Design) cannot come back with nothing selected to justify it. + pub fn at_startup(mut self) -> Self { + self.ribbon_tab = "Home".into(); + self + } + pub fn theme_preference(&self) -> egui::ThemePreference { if self.system_theme { egui::ThemePreference::System diff --git a/crates/ui-egui/tests/contextual_ribbon_tab.rs b/crates/ui-egui/tests/contextual_ribbon_tab.rs index 6e1626dd57..bdd8b5c938 100644 --- a/crates/ui-egui/tests/contextual_ribbon_tab.rs +++ b/crates/ui-egui/tests/contextual_ribbon_tab.rs @@ -72,3 +72,25 @@ fn leaving_the_table_while_on_table_design_returns_to_home() { h.run_steps(2); assert_eq!(h.state().ui.ribbon_tab, "Home"); } + +#[test] +fn startup_opens_on_home_not_the_saved_tab() { + let mut saved = gridcraft_ui_egui::UiState::default(); + saved.ribbon_tab = "Table Design".into(); + saved.dark = true; + let json = serde_json::to_string(&saved).unwrap(); + assert!(json.contains(r#""ribbonTab":"Table Design""#), "{json}"); + let restored = serde_json::from_str::(&json).unwrap().at_startup(); + assert_eq!(restored.ribbon_tab, "Home", "the saved tab is not restored"); + assert!(restored.dark, "the other preferences are"); +} + +#[test] +fn an_ordinary_tab_stays_put_in_a_session() { + let mut session = Session::new(); + session.new_workbook(); + let mut h = harness(session); + h.state_mut().ui.ribbon_tab = "Data".into(); + h.run_steps(3); + assert_eq!(h.state().ui.ribbon_tab, "Data"); +} From 168acc58898d0f358964703f2826f57ca440418d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 23 Oct 2026 11:17:38 -0400 Subject: [PATCH 103/687] packaging: shell, PowerShell and web page fixes (SonarCloud S7688, S7679, S7682, S8677, S7926) (#2323) - Shell: [[ ]] for conditional tests, positional parameters copied into named locals, explicit return at the end of the env.sh helpers. - PowerShell: the desktop-shortcut smoke test writes its progress with Write-Output instead of Write-Host. - Web: the viewport no longer sets user-scalable=no, so people can zoom the page. --- apps/gridcraft-web/index.html | 2 +- packaging/env.sh | 19 ++++++++------ packaging/freebsd/package.sh | 4 +-- packaging/icons.sh | 5 +++- packaging/linux/flatpak-bundle.sh | 8 +++--- packaging/linux/package.sh | 21 +++++++++------- packaging/macos/import-cert.sh | 6 ++--- packaging/macos/package.sh | 28 ++++++++++----------- packaging/web/package.sh | 6 ++--- packaging/windows/test-desktop-shortcut.ps1 | 6 ++--- 10 files changed, 58 insertions(+), 47 deletions(-) diff --git a/apps/gridcraft-web/index.html b/apps/gridcraft-web/index.html index 5fdd1ea54a..321aa302ee 100644 --- a/apps/gridcraft-web/index.html +++ b/apps/gridcraft-web/index.html @@ -2,7 +2,7 @@ - + GridCraft diff --git a/packaging/env.sh b/packaging/env.sh index bacb167e19..6f36506b60 100644 --- a/packaging/env.sh +++ b/packaging/env.sh @@ -19,10 +19,11 @@ workspace_version() { /^\[/ { in_pkg = ($0 == "[workspace.package]") ; next } in_pkg && $1 == "version" { gsub(/[" ]/, "", $3); print $3; exit } ' "$ROOT/Cargo.toml" + return } VERSION="${GRIDCRAFT_VERSION:-$(workspace_version)}" -if [ -z "$VERSION" ]; then +if [[ -z "$VERSION" ]]; then echo "error: could not read [workspace.package] version from $ROOT/Cargo.toml" >&2 exit 1 fi @@ -32,7 +33,7 @@ DIST="${DIST:-$ROOT/dist/release}" mkdir -p "$DIST" export DIST -if [ -z "${GRIDCRAFT_BUILD_SHA:-}" ]; then +if [[ -z "${GRIDCRAFT_BUILD_SHA:-}" ]]; then GRIDCRAFT_BUILD_SHA="$(git -C "$ROOT" rev-parse HEAD 2>/dev/null || true)" fi export GRIDCRAFT_BUILD_SHA @@ -41,29 +42,33 @@ export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$ROOT/target}" # Emit a GitHub Actions warning (plain stderr outside Actions). warn() { - if [ -n "${GITHUB_ACTIONS:-}" ]; then echo "::warning::$*"; else echo "warning: $*" >&2; fi + if [[ -n "${GITHUB_ACTIONS:-}" ]]; then echo "::warning::$*"; else echo "warning: $*" >&2; fi + return } # Copy licence and readme files that exist into a package directory. copy_docs() { local dest="$1" f for f in README.md LICENSE LICENSE-MIT LICENSE-APACHE COPYRIGHT; do - if [ -f "$ROOT/$f" ]; then cp "$ROOT/$f" "$dest/"; fi + if [[ -f "$ROOT/$f" ]]; then cp "$ROOT/$f" "$dest/"; fi done copy_font_licences "$dest" + return } # Builds made with craft-fonts (CRAFT_FONTS_DIR, set by the release workflow) embed its fonts, so # the package carries their licences as OFL-.txt. Nothing without CRAFT_FONTS_DIR. copy_font_licences() { local dest="$1" f - [ -n "${CRAFT_FONTS_DIR:-}" ] || return 0 + [[ -n "${CRAFT_FONTS_DIR:-}" ]] || return 0 for f in "$CRAFT_FONTS_DIR"/fonts/*/OFL.txt; do - if [ -f "$f" ]; then cp "$f" "$dest/OFL-$(basename "$(dirname "$f")").txt"; fi + if [[ -f "$f" ]]; then cp "$f" "$dest/OFL-$(basename "$(dirname "$f")").txt"; fi done } # Portable SHA-256 of a file (prints just the hash). sha256() { - if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi + local file="$1" + if command -v sha256sum >/dev/null; then sha256sum "$file" | cut -d' ' -f1; else shasum -a 256 "$file" | cut -d' ' -f1; fi + return } diff --git a/packaging/freebsd/package.sh b/packaging/freebsd/package.sh index e4ee9da7bc..ab98e98001 100755 --- a/packaging/freebsd/package.sh +++ b/packaging/freebsd/package.sh @@ -16,7 +16,7 @@ APP_ID=ai.storyteller.gridcraft LINUX="$ROOT/packaging/linux" SKIP_BUILD=0 -while [ $# -gt 0 ]; do +while [[ $# -gt 0 ]]; do case "$1" in --skip-build) SKIP_BUILD=1; shift ;; -h | --help) sed -n '2,11p' "$0"; exit 0 ;; @@ -34,7 +34,7 @@ BASENAME="gridcraft-$VERSION-freebsd-$ARCH" echo "==> GridCraft $VERSION for FreeBSD $ARCH" -if [ "$SKIP_BUILD" = 0 ]; then +if [[ "$SKIP_BUILD" = 0 ]]; then (cd "$ROOT" && cargo build --release --locked -p gridcraft -p gridcraft-cli) fi BIN="$CARGO_TARGET_DIR/release" diff --git a/packaging/icons.sh b/packaging/icons.sh index 2adcb9a9f7..c6f325e3dd 100755 --- a/packaging/icons.sh +++ b/packaging/icons.sh @@ -21,7 +21,10 @@ MAC="$TMP/mac.svg" sed 's/viewBox="0 0 512 512"/viewBox="-62.136 -62.136 636.272 636.272"/' "$SVG" >"$MAC" grep -q 'viewBox="-62.136' "$MAC" || { echo "error: expected viewBox=\"0 0 512 512\" in $SVG" >&2; exit 1; } -render() { resvg -w "$2" -h "$2" "$1" "$3" &2; exit 1; } +[[ -f "$TARBALL" ]] || { echo "error: $TARBALL not found (run packaging/linux/package.sh --formats tar first)" >&2; exit 1; } for tool in flatpak flatpak-builder; do command -v "$tool" >/dev/null || { echo "error: $tool not found (apt install flatpak flatpak-builder librsvg2-common)" >&2; exit 1; } done @@ -49,7 +49,7 @@ WORK="$CARGO_TARGET_DIR/flatpak-bundle" rm -rf "$WORK" mkdir -p "$WORK/stage" tar -xzf "$TARBALL" -C "$WORK/stage" --strip-components=1 -[ -x "$WORK/stage/bin/gridcraft" ] || { echo "error: $TARBALL has no bin/gridcraft" >&2; exit 1; } +[[ -x "$WORK/stage/bin/gridcraft" ]] || { echo "error: $TARBALL has no bin/gridcraft" >&2; exit 1; } cp "$HERE/flatpak/$APP_ID.bundle.yml" "$WORK/$APP_ID.yml" flatpak remote-add --user --if-not-exists flathub "$FLATHUB" @@ -62,7 +62,7 @@ OUT="$DIST/$BASENAME.flatpak" flatpak build-bundle --runtime-repo="$FLATHUB" "$WORK/repo" "$OUT" "$APP_ID" stable echo "wrote $OUT" -if [ "$TEST" = 1 ]; then +if [[ "$TEST" = 1 ]]; then flatpak install --user -y --noninteractive --reinstall "$OUT" flatpak info --user "$APP_ID" flatpak run --command=gridcraft-cli "$APP_ID" --version diff --git a/packaging/linux/package.sh b/packaging/linux/package.sh index 105ac744fa..f8c53d6b30 100755 --- a/packaging/linux/package.sh +++ b/packaging/linux/package.sh @@ -21,7 +21,7 @@ APP_ID=ai.storyteller.gridcraft SKIP_BUILD=0 FORMATS="appimage deb rpm tar" -while [ $# -gt 0 ]; do +while [[ $# -gt 0 ]]; do case "$1" in --skip-build) SKIP_BUILD=1; shift ;; --formats) FORMATS="$2"; shift 2 ;; @@ -42,8 +42,8 @@ BASENAME="gridcraft-$VERSION-linux-$ARCH" echo "==> GridCraft $VERSION for Linux $ARCH ($FORMATS)" -if [ "$SKIP_BUILD" = 0 ]; then - if [ -n "${CROSS_TARGET:-}" ]; then +if [[ "$SKIP_BUILD" = 0 ]]; then + if [[ -n "${CROSS_TARGET:-}" ]]; then (cd "$ROOT" && cargo build --release --locked -p gridcraft -p gridcraft-cli --target "$CROSS_TARGET") else (cd "$ROOT" && cargo build --release --locked -p gridcraft -p gridcraft-cli) @@ -75,7 +75,10 @@ if command -v appstreamcli >/dev/null; then appstreamcli validate --no-net --explain "$STAGE/usr/share/metainfo/$APP_ID.metainfo.xml" fi -has() { case " $FORMATS " in *" $1 "*) return 0 ;; *) return 1 ;; esac; } +has() { + local format="$1" + case " $FORMATS " in *" $format "*) return 0 ;; *) return 1 ;; esac +} # ---- .tar.gz ------------------------------------------------------------------------------------ if has tar; then @@ -108,9 +111,9 @@ if has appimage; then ln -s "$APP_ID.png" "$APPDIR/.DirIcon" TOOL="${APPIMAGETOOL:-$(command -v appimagetool || true)}" - if [ -z "$TOOL" ]; then + if [[ -z "$TOOL" ]]; then TOOL="$CARGO_TARGET_DIR/appimagetool-$ARCH.AppImage" - if [ ! -x "$TOOL" ]; then + if [[ ! -x "$TOOL" ]]; then curl --proto '=https' -fsSL -o "$TOOL" "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-$ARCH.AppImage" chmod +x "$TOOL" fi @@ -133,16 +136,16 @@ if has appimage; then # package) appimagetool also writes the .zsync, into its working directory, hence the cd. (cd "$DIST" && ARCH="$ARCH" APPIMAGE_EXTRACT_AND_RUN=1 "$TOOL" --no-appstream -u "$UPDATE_INFO" "$APPDIR" "$OUT") echo "wrote $OUT" - if [ -s "$OUT.zsync" ]; then + if [[ -s "$OUT.zsync" ]]; then echo "wrote $OUT.zsync" else warn "zsyncmake not found, so $OUT.zsync was not written; AppImage delta updates need it" fi fi -if [ -z "${CROSS_TARGET:-}" ]; then +if [[ -z "${CROSS_TARGET:-}" ]]; then "$STAGE/usr/bin/gridcraft-cli" --version -elif [ -n "${EMULATOR:-}" ]; then +elif [[ -n "${EMULATOR:-}" ]]; then "$EMULATOR" "$STAGE/usr/bin/gridcraft-cli" --version fi echo "==> done" diff --git a/packaging/macos/import-cert.sh b/packaging/macos/import-cert.sh index 41d9538bfa..4611ffe055 100755 --- a/packaging/macos/import-cert.sh +++ b/packaging/macos/import-cert.sh @@ -12,7 +12,7 @@ umask 077 # shellcheck source=../env.sh . "$(dirname "${BASH_SOURCE[0]}")/../env.sh" -if [ -z "${APPLE_CERTIFICATE:-}" ]; then +if [[ -z "${APPLE_CERTIFICATE:-}" ]]; then warn "APPLE_CERTIFICATE is not set: macOS builds will be ad-hoc signed and not notarized" exit 0 fi @@ -36,13 +36,13 @@ security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$KC_PASS" security list-keychains -d user -s "$KEYCHAIN" $(security list-keychains -d user | tr -d '"') IDENTITY="$(security find-identity -v -p codesigning "$KEYCHAIN" | awk '/Developer ID Application/ { print $2; exit }')" -if [ -z "$IDENTITY" ]; then +if [[ -z "$IDENTITY" ]]; then security find-identity -v -p codesigning "$KEYCHAIN" >&2 echo "error: no 'Developer ID Application' identity in APPLE_CERTIFICATE" >&2 exit 1 fi echo "Imported signing identity $IDENTITY" -if [ -n "${GITHUB_ENV:-}" ]; then +if [[ -n "${GITHUB_ENV:-}" ]]; then { echo "MACOS_SIGN_IDENTITY=$IDENTITY"; echo "MACOS_KEYCHAIN=$KEYCHAIN"; } >>"$GITHUB_ENV" else echo "MACOS_SIGN_IDENTITY=$IDENTITY" diff --git a/packaging/macos/package.sh b/packaging/macos/package.sh index d8b4b097b0..b45164ee09 100755 --- a/packaging/macos/package.sh +++ b/packaging/macos/package.sh @@ -19,7 +19,7 @@ HERE="$ROOT/packaging/macos" ARCH=universal SKIP_BUILD=0 -while [ $# -gt 0 ]; do +while [[ $# -gt 0 ]]; do case "$1" in --arch) ARCH="$2"; shift 2 ;; --skip-build) SKIP_BUILD=1; shift ;; @@ -44,9 +44,9 @@ DMG="$DIST/gridcraft-$VERSION-macos-$ARCH.dmg" CLI_ZIP="$DIST/gridcraft-cli-$VERSION-macos-$ARCH.zip" NOTARIZE=0 -if [ "$IDENTITY" = "-" ]; then +if [[ "$IDENTITY" = "-" ]]; then warn "macOS: ad-hoc signing (no MACOS_SIGN_IDENTITY); artifacts are not notarized" -elif [ -n "${APPLE_ID:-}" ] && [ -n "${APPLE_PASSWORD:-}" ] && [ -n "${APPLE_TEAM_ID:-}" ]; then +elif [[ -n "${APPLE_ID:-}" ]] && [[ -n "${APPLE_PASSWORD:-}" ]] && [[ -n "${APPLE_TEAM_ID:-}" ]]; then NOTARIZE=1 else warn "macOS: APPLE_ID / APPLE_PASSWORD / APPLE_TEAM_ID incomplete; signed but not notarized" @@ -55,7 +55,7 @@ fi echo "==> GridCraft $VERSION for macOS ($ARCH), identity: $IDENTITY, notarize: $NOTARIZE" # ---- build ------------------------------------------------------------------------------------- -if [ "$SKIP_BUILD" = 0 ]; then +if [[ "$SKIP_BUILD" = 0 ]]; then args=() for t in "${TARGETS[@]}"; do args+=(--target "$t"); done (cd "$ROOT" && cargo build --release --locked -p gridcraft -p gridcraft-cli "${args[@]}") @@ -74,22 +74,22 @@ done sign() { # Hardened runtime + secure timestamp for a real identity; ad-hoc can't be timestamped. local ts=(--timestamp) - [ "$IDENTITY" = "-" ] && ts=(--timestamp=none) + [[ "$IDENTITY" = "-" ]] && ts=(--timestamp=none) local kc=() - [ -n "${MACOS_KEYCHAIN:-}" ] && kc=(--keychain "$MACOS_KEYCHAIN") + [[ -n "${MACOS_KEYCHAIN:-}" ]] && kc=(--keychain "$MACOS_KEYCHAIN") codesign --force --sign "$IDENTITY" ${kc[@]+"${kc[@]}"} "${ts[@]}" "$@" } # stapler fetches the ticket from Apple's CloudKit, which sometimes drops the connection ("The # network connection was lost", Error 68). Stapling and validating are idempotent, so retry. stapler_retry() { - local attempt + local action="$1" attempt for attempt in 1 2 3 4 5; do if xcrun stapler "$@"; then return 0 fi - [ "$attempt" = 5 ] && { echo "stapler $1 failed 5 times" >&2; return 1; } - warn "stapler $1 failed (attempt $attempt of 5); retrying in $((attempt * 15))s" + [[ "$attempt" = 5 ]] && { echo "stapler $action failed 5 times" >&2; return 1; } + warn "stapler $action failed (attempt $attempt of 5); retrying in $((attempt * 15))s" sleep $((attempt * 15)) done } @@ -102,8 +102,8 @@ notarize() { echo "$out" id="$(printf '%s' "$out" | plutil -extract id raw -o - - 2>/dev/null || true)" status="$(printf '%s' "$out" | plutil -extract status raw -o - - 2>/dev/null || true)" - if [ "$status" != "Accepted" ]; then - if [ -n "$id" ]; then + if [[ "$status" != "Accepted" ]]; then + if [[ -n "$id" ]]; then xcrun notarytool log "$id" --apple-id "$APPLE_ID" --password "$APPLE_PASSWORD" --team-id "$APPLE_TEAM_ID" || true fi echo "error: notarization of $(basename "$file") failed (status: ${status:-unknown})" >&2 @@ -131,7 +131,7 @@ sign --options runtime --entitlements "$HERE/entitlements.plist" "$APP/Contents/ sign --options runtime --entitlements "$HERE/entitlements.plist" "$APP" codesign --verify --strict --deep --verbose=2 "$APP" -if [ "$NOTARIZE" = 1 ]; then +if [[ "$NOTARIZE" = 1 ]]; then ditto -c -k --keepParent "$APP" "$WORK/GridCraft-notarize.zip" notarize "$WORK/GridCraft-notarize.zip" stapler_retry staple "$APP" @@ -158,7 +158,7 @@ hdiutil convert "$WORK/raw.dmg" -format UDZO -imagekey zlib-level=9 -o "$DMG" rm -f "$WORK/raw.dmg" sign "$DMG" codesign --verify --strict --verbose=2 "$DMG" -if [ "$NOTARIZE" = 1 ]; then +if [[ "$NOTARIZE" = 1 ]]; then notarize "$DMG" stapler_retry staple "$DMG" stapler_retry validate "$DMG" @@ -176,7 +176,7 @@ codesign --verify --strict --verbose=2 "$CLI_DIR/gridcraft-cli" rm -f "$CLI_ZIP" ditto -c -k --keepParent "$CLI_DIR" "$CLI_ZIP" # A bare Mach-O can't carry a stapled ticket; Gatekeeper looks the notarization up online. -if [ "$NOTARIZE" = 1 ]; then notarize "$CLI_ZIP"; fi +if [[ "$NOTARIZE" = 1 ]]; then notarize "$CLI_ZIP"; fi "$WORK/bin/gridcraft-cli" --version echo "==> done" diff --git a/packaging/web/package.sh b/packaging/web/package.sh index 8b1bca63bb..7c5be61346 100755 --- a/packaging/web/package.sh +++ b/packaging/web/package.sh @@ -12,15 +12,15 @@ set -euo pipefail HERE="$ROOT/packaging/web" SITE="$ROOT/dist/web" -if [ "${1:-}" != "--skip-build" ]; then +if [[ "${1:-}" != "--skip-build" ]]; then command -v trunk >/dev/null || { echo "error: trunk not found (cargo install trunk --locked)" >&2; exit 1; } - [ -f "$ROOT/apps/gridcraft-web/index.html" ] || { echo "error: apps/gridcraft-web/index.html missing (the web app isn't set up yet)" >&2; exit 1; } + [[ -f "$ROOT/apps/gridcraft-web/index.html" ]] || { echo "error: apps/gridcraft-web/index.html missing (the web app isn't set up yet)" >&2; exit 1; } # --dist/--public-url here, so the output doesn't depend on Trunk.toml (which should agree: # public_url = "./"). (cd "$ROOT/apps/gridcraft-web" && trunk build --release --dist "$SITE" --public-url ./) fi -[ -f "$SITE/index.html" ] || { echo "error: $SITE/index.html missing; run without --skip-build" >&2; exit 1; } +[[ -f "$SITE/index.html" ]] || { echo "error: $SITE/index.html missing; run without --skip-build" >&2; exit 1; } # Paths must be relative so the site works under any prefix (public_url = "./" in Trunk.toml). if grep -Eq '(src|href)="/[^/]' "$SITE/index.html"; then echo "error: $SITE/index.html has root-absolute URLs; it would break when served from a sub-path" >&2 diff --git a/packaging/windows/test-desktop-shortcut.ps1 b/packaging/windows/test-desktop-shortcut.ps1 index 9a5b4cb043..f3c25b2c8a 100644 --- a/packaging/windows/test-desktop-shortcut.ps1 +++ b/packaging/windows/test-desktop-shortcut.ps1 @@ -72,10 +72,10 @@ function Invoke-Msi([string] $Name, [string[]] $Arguments) { throw "$Name exceeded two minutes; see $log" } if ($process.ExitCode -ne 0) { - if (Test-Path $log) { Get-Content $log -Tail 60 | Write-Host } + if (Test-Path $log) { Get-Content $log -Tail 60 } throw "$Name exited $($process.ExitCode); see $log" } - Write-Host "ok $Name" + Write-Output "ok $Name" } finally { $process.Dispose() } @@ -146,7 +146,7 @@ function Uninstall-AsSystem($Package) { $info = Get-ScheduledTaskInfo -TaskName $taskName if ($info.LastRunTime -ge $started.AddSeconds(-1) -and $task.State -ne 'Running' -and $info.LastTaskResult -ne 267009) { Assert-That ($info.LastTaskResult -eq 0) "Cross-account uninstall failed with $($info.LastTaskResult); see $log" - Write-Host 'ok uninstall-as-system' + Write-Output 'ok uninstall-as-system' return } } while ((Get-Date) -lt $deadline) From ba980399887494f0539d30cceaf52a5b8b12d05c Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:33:31 +0200 Subject: [PATCH 104/687] Keep the in-cell editor wide enough for the next character at high zoom (#2336) --- crates/ui-egui/src/grid.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 40ef2c8e9f..acef3f8cb7 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -3087,6 +3087,13 @@ fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: } } +/// Room the in-cell editor keeps right of its text. It is sized from the text of the last frame, so +/// the next typed character must still fit: a fixed 14 px was less than one glyph at high zoom, +/// and the field scrolled and snapped back on every key (#2336). `size` is the font size in px. +fn editor_slack(size: f32) -> f32 { + 14.0 + size +} + fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet, wb: &Workbook) { let Some(ed) = app.editor.as_ref() else { return }; if ed.sheet != wb.active_sheet { @@ -3113,7 +3120,7 @@ fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet, let text_w = ui.painter().layout_no_wrap(ed.text.clone(), font.clone(), Color32::BLACK).size().x; // Excel keeps the cell's height for one line and only grows for extra lines. let h = if lines > 1.0 { (size * 1.3 * lines + 4.0).max(rect.height()) } else { rect.height() }; - ((text_w + 14.0).max(rect.width()).min(geo.cells.right() - rect.left()), h) + ((text_w + editor_slack(size)).max(rect.width()).min(geo.cells.right() - rect.left()), h) }; // Grows to the left on a right-to-left sheet. let erect = geo.mirror_rect(Rect::from_min_size(rect.min, vec2(w, h))); @@ -4002,6 +4009,15 @@ pub(crate) fn heading_font_size(zoom: f32) -> f32 { #[cfg(test)] mod tests { + #[test] + fn editor_slack_fits_one_more_glyph_at_high_zoom() { + use super::editor_slack; + // A glyph is at most about one em wide; at 400% an em is ~60 px, far over the old fixed 14 px. + for size in [14.7_f32, 25.0, 60.0] { + assert!(editor_slack(size) - 4.0 >= size, "slack {} too small for a {size} px glyph", editor_slack(size)); + } + } + #[test] fn explicit_left_and_right_keep_their_side_on_a_right_to_left_sheet() { use super::{HAlign, painted_halign}; From 599762f534bb47e6505fa6044d6aa2c17bc8fa4e Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:33:55 +0200 Subject: [PATCH 105/687] AutoFilter: grow the range when a value is entered in the row below it (#2342) --- crates/engine/src/cmd/data.rs | 42 +++++++++++++++++++++++++++++++++++ crates/engine/src/cmd/edit.rs | 6 ++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index 52a716cff4..8fe74779c3 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -652,6 +652,24 @@ fn filter_blank(v: &Value) -> bool { v.is_empty() || matches!(v, Value::Text(t) if t.is_empty()) } +/// Excel grows an AutoFilter range when a value is entered in the row right below it, in one of its columns (#2342). +/// A table's own filter is left to the table, and a row typed after a blank row does not count. +/// A formula counts before it is calculated: this runs inside the edit, ahead of the recalc. +pub(crate) fn grow_autofilter(sh: &mut Sheet, at: CellRef) { + if !sh.cells.get(at).is_some_and(|c| !c.value.is_empty() || c.formula.is_some()) { + return; + } + let Some(af) = sh.autofilter.as_ref() else { return }; + let r = af.range; + if r.end.row.checked_add(1) != Some(at.row) || at.col < r.start.col || at.col > r.end.col || sh.tables.iter().any(|t| t.range == r) { + return; + } + let grown = RangeRef::new(r.start, CellRef::new(at.row, r.end.col)); + if let Some(af) = sh.autofilter.as_mut() { + af.range = grown; + } +} + /// Hides rows of the autofilter range that fail any criterion. pub(crate) fn apply_filter(wb: &mut Workbook, sheet: usize) { let Some(sh) = wb.sheet(sheet) else { return }; @@ -1554,6 +1572,30 @@ mod tests { } } + #[test] + fn autofilter_range_grows_when_a_value_is_entered_in_the_row_below() { + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "A1", "values": [["Name", "Qty"], ["d", 4], ["b", 2]]})).unwrap(); + s.execute("data.filter", json!({"range": "A1:B3"})).unwrap(); + let range = |s: &Session| s.doc().unwrap().wb.active().unwrap().autofilter.as_ref().map(|a| a.range.a1()); + s.execute("cell.set", json!({"cell": "A4", "input": "e"})).unwrap(); + assert_eq!(range(&s), Some("A1:B4".to_string())); + s.execute("range.setValues", json!({"range": "A5", "values": [["f", 6]]})).unwrap(); + assert_eq!(range(&s), Some("A1:B5".to_string())); + // A row typed after a blank row does not grow it. + s.execute("cell.set", json!({"cell": "A7", "input": "x"})).unwrap(); + assert_eq!(range(&s), Some("A1:B5".to_string())); + // A cell outside the filter's columns does not either. + s.execute("cell.set", json!({"cell": "C6", "input": "x"})).unwrap(); + assert_eq!(range(&s), Some("A1:B5".to_string())); + // A formula and Ctrl+Enter (range.fill) are entries too. + s.execute("cell.set", json!({"cell": "B6", "input": "=1+1"})).unwrap(); + assert_eq!(range(&s), Some("A1:B6".to_string())); + s.execute("range.fill", json!({"range": "A7:B7", "input": "g"})).unwrap(); + assert_eq!(range(&s), Some("A1:B7".to_string())); + } + #[test] fn list_validation_without_formula1_is_an_error() { let mut s = Session::new(); diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 1998f0b2c6..352797eab2 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -272,6 +272,7 @@ fn cell_set(s: &mut Session, p: &Json) -> Result { sh.remove_cell(at); } } + super::data::grow_autofilter(sh, at); cx.touch(sheet, at); Ok(Json::Null) })?; @@ -350,6 +351,7 @@ fn range_set_values(s: &mut Session, p: &Json) -> Result { sh.remove_cell(at); } } + super::data::grow_autofilter(sh, at); cx.touch(sheet, at); count += 1; } @@ -390,7 +392,9 @@ fn range_fill(s: &mut Session, p: &Json) -> Result { let shifted = gridcraft_formula::adjust::shift_relative(e, at.row as i64 - origin.row as i64, at.col as i64 - origin.col as i64); cell.formula = Some(std::sync::Arc::new(Formula::from_expr(shifted))); } - cx.sheet_mut(sheet)?.set_cell(at, cell); + let sh = cx.sheet_mut(sheet)?; + sh.set_cell(at, cell); + super::data::grow_autofilter(sh, at); cx.touch(sheet, at); } } From bdf8b8cc755ae6dfc2266d23b3ede43329114339 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 23 Oct 2026 11:48:02 -0400 Subject: [PATCH 106/687] Use chrono instead of hand-rolled UTC conversion (#2121) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use chrono for UTC/calendar conversion instead of hand-rolled copies Replaces the hand-rolled civil_from_days / days_from_civil copies and the is_leap / days_in_month calendar helpers with chrono as a workspace dependency (default-features = false, std only, so wasm is unaffected). The spreadsheet serial-date semantics are untouched: Excel's 1900 fictitious leap day (serial 60 = 1900-02-29), the 1899-12-30 / 1904-01-01 serial offsets, Excel's weekday numbering and the 1900/1904 systems keep their explicit special cases in serial_from_ymd / datetime_from_serial / days_in_month_in. chrono only backs the plain proleptic-Gregorian day-number <-> date conversion and the leap-year / month-length helpers those build on; a small fallback keeps the old answer for dates outside chrono's NaiveDate range. The duplicate local is_leap in the financial functions now uses the core one. Output is byte-identical and pinned by the existing date, parse, financial and logging tests. AGENTS.md now says time goes through chrono. Follow-up to storytold/cadcraft#1785 * Never panic on out-of-range clocks or years; keep calendar results outside chrono's range - logging::timestamp: chrono's From unwraps, so a clock past year 262143 panicked inside the logger. Use the fallible DateTime::from_timestamp and read such a clock as the epoch, like the old saturating code. Years past 9999 are written with {:04} (not %Y, which prefixes a '+'). - date::is_leap: restore the Gregorian leap-year rule for years outside chrono's NaiveDate range (e.g. is_leap(400000) is true again) via direct arithmetic. - date::days_in_month: compute the December boundary with checked_add so days_in_month(i32::MAX, 12) no longer overflows, and compute month lengths through days_from_civil's fallback so out-of-range years keep their real lengths (e.g. days_in_month(400000, 2) is 29). Invalid months still return 0. - civil_from_days: add missing docstring. - AGENTS.md: note the narrow out-of-range fallback exception to the chrono rule. * Don't build an unrepresentable SystemTime in the far-future test UNIX_EPOCH + Duration::from_secs(u64::MAX / 2) overflows Windows' SystemTime and panicked before timestamp() ran, failing the Windows test job. Build it with checked_add and only assert where it exists. * Route out-of-range dates through chrono's 400-year cycle, not hand-rolled copies The PR kept hand-rolled Hinnant `days_from_civil_fallback`/`civil_from_days_fallback` copies for dates outside chrono's ±262143-year range, plus a hand-rolled Gregorian leap rule in `is_leap` and an i32::MAX special-case in `days_in_month`. AGENTS.md carved a "narrow fallback" exception for them, which left calendar arithmetic outside chrono after all. Replace all of it with the Gregorian 400-year cycle (146_097 days): chrono converts the day/year within one cycle and whole cycles are added back via i128 (so the i32/i64 extremes can't overflow). `is_leap` and `days_in_month` reduce the year with `rem_euclid(400)` (leap years and month lengths repeat every 400 years), which also removes the December `i32::MAX` overflow special-case. chrono is now the only calendar logic, and every output is unchanged: verified the cycle result equals the old formula over wide ranges and at the i32/i64 extremes with a scratch chrono program. The Excel 1900 leap bug (serial 60 = fictitious 1900-02-29) and 1904 semantics are untouched -- `serial_from_ymd`/ `datetime_from_serial` only ever hand the converters in-range years, so their serials are byte-for-byte the pre-chrono ones (pinned by a new test). New tests pin `days_from_civil`/`civil_from_days` past chrono's range on both sides of the epoch, round-tripping, with values from the old formula; the existing leap/month-length-beyond-range test still passes unchanged. Removed the AGENTS.md "narrow fallback" exception. Tested: cargo test -p gridcraft-core -p gridcraft-functions; fmt --check; clippy --all-targets -D warnings. * time: preserve out-of-range years in the log timestamp instead of the epoch `DateTime::from_timestamp(..).unwrap_or_default()` changed the existing output: a clock set far ahead (an input the old hand-rolled civil-from-days converted) collapsed to `1970-01-01`. Restore the old conversion domain with a cycle-based helper: reduce by whole 400-year Gregorian cycles (146_097 days), convert the in-range remainder with chrono, and carry the year back as i128. Only the year leaves chrono's range; month/day/time stay exact. Pre-1970 still clamps to the epoch as before. chrono stays the only calendar logic (no new features). apps/gridcraft logging.rs `timestamp` (SystemTime, u64 secs), pinned via a platform-safe `UNIX_EPOCH.checked_add`: u64::MAX/2 s → "292277026596-12-04T15:30:07.000Z" (was "1970-01-01..."), 10e12 s → "318857-05-20T17:46:40.000Z", 253_402_300_800 s → "10000-01-01...". * Name the 400-year range extension as the one exception to the time rule The rule said never to hand-roll calendar math, but the range fix (shifting by whole 400-year Gregorian cycles so dates past chrono's year range keep their old output) is cycle arithmetic, so the rule and the code disagreed. The rule now names that exception: the library still computes month, day and time, and only whole cycles are carried in the year. * Say the library computes month and day; time-of-day is plain arithmetic Narrows the range-extension wording: the date library computes the calendar fields, and splitting seconds into hours, minutes and seconds is ordinary arithmetic, which is what the code does. --- AGENTS.md | 1 + Cargo.lock | 11 +++ Cargo.toml | 2 + apps/gridcraft/Cargo.toml | 1 + apps/gridcraft/src/logging.rs | 55 +++++++++----- crates/core/Cargo.toml | 1 + crates/core/src/date.rs | 135 ++++++++++++++++++++++++++++------ 7 files changed, 163 insertions(+), 43 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 963fc6d748..ab73047ef4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -37,6 +37,7 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr - **Programmatic calls never open dialogs**: a command with empty params runs with defaults or returns an error; only menu-style invocation opens a dialog (`UiRequest::Dialog`). - **Layering** is enforced by `cargo xtask layers`: `core`/`numfmt` (L0) → `formula`/`functions` (L1) → `model`/`calc`/`xlsx` (L2) → `chart` (L3) → `engine` (L5) → `ui-egui`/`mcp` (L6) → `apps/*` (L7). Nothing below L6 depends on egui/eframe/winit/rfd. **The UI crate is swappable.** - **The UI is thin**: panels read engine state and act through `app.run(id, params)`. Colours come from `theme::Tokens`. +- **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math). A crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic). - **Rust only** (no handwritten JS/TS). **Never break wasm** (`cargo xtask wasm`). - **Quality gates** before every commit: `cargo xtask ci` (fmt, clippy -D warnings, tests, assets, layers, wasm). One task id per commit (`M2.1: borders gallery`). diff --git a/Cargo.lock b/Cargo.lock index c7e3560b60..7f52031881 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -619,6 +619,15 @@ dependencies = [ "libc", ] +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "num-traits", +] + [[package]] name = "cipher" version = "0.5.2" @@ -1499,6 +1508,7 @@ name = "gridcraft" version = "0.11.1" dependencies = [ "arboard", + "chrono", "craft-print", "eframe", "egui", @@ -1555,6 +1565,7 @@ dependencies = [ name = "gridcraft-core" version = "0.11.1" dependencies = [ + "chrono", "js-sys", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index bf8eb381e2..9994d44e3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,8 @@ thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = { version = "1", features = ["float_roundtrip"] } log = "0.4" +# All date/time conversion goes through chrono (no hand-rolled calendar code beyond the 400-year range extension; AGENTS.md). +chrono = { version = "0.4.45", default-features = false, features = ["std"] } rayon = "1" regex = { version = "1", default-features = false, features = ["std", "unicode", "perf"] } quick-xml = "0.41" diff --git a/apps/gridcraft/Cargo.toml b/apps/gridcraft/Cargo.toml index cd8311b345..94cf3fe9b2 100644 --- a/apps/gridcraft/Cargo.toml +++ b/apps/gridcraft/Cargo.toml @@ -18,6 +18,7 @@ egui = { workspace = true } serde_json = { workspace = true } log = { workspace = true } image = { workspace = true } +chrono = { workspace = true } [build-dependencies] # Icon + VERSIONINFO for gridcraft.exe; build.rs only uses it when the target is Windows. diff --git a/apps/gridcraft/src/logging.rs b/apps/gridcraft/src/logging.rs index e3b56a8cb5..a0f62d164a 100644 --- a/apps/gridcraft/src/logging.rs +++ b/apps/gridcraft/src/logging.rs @@ -92,29 +92,35 @@ fn matches(name: &str, target: &str) -> bool { } } -/// `2026-10-08T07:59:17.728Z` (UTC, milliseconds). A clock before 1970 reads as the epoch. +/// `2026-10-08T07:59:17.728Z` (UTC, milliseconds). A clock before 1970 reads as the epoch; a clock +/// set absurdly far ahead keeps its real year (years far outside chrono's own range are preserved) +/// instead of panicking. pub fn timestamp(t: SystemTime) -> String { let since = t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default(); - let secs = since.as_secs(); - let (days, rem) = (secs / 86_400, secs % 86_400); - let (y, m, d) = civil_from_days(days); - format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}.{:03}Z", rem / 3600, rem % 3600 / 60, rem % 60, since.subsec_millis()) + let (y, dt) = utc_parts(since.as_secs(), since.subsec_nanos()); + // `{:04}` rather than `%Y`, which writes a `+` before years past 9999. + format!("{y:04}-{}", dt.format("%m-%dT%H:%M:%S%.3fZ")) } -/// Days since 1970-01-01 to a proleptic Gregorian (year, month, day) (Howard Hinnant's -/// `civil_from_days`, unsigned because `timestamp` clamps at the epoch). Saturating, so no clock -/// value can overflow it. -fn civil_from_days(days: u64) -> (u64, u64, u64) { - let z = days.saturating_add(719_468); - let era = z / 146_097; - let doe = z % 146_097; - let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; - let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); - let mp = (5 * doy + 2) / 153; - let d = doy - (153 * mp + 2) / 5 + 1; - let m = if mp < 10 { mp + 3 } else { mp - 9 }; - let y = era.saturating_mul(400).saturating_add(yoe).saturating_add(u64::from(m <= 2)); - (y, m, d) +/// Unix seconds (+ nanos) → (proleptic Gregorian year, the same wall-clock instant moved into +/// chrono's range by whole 400-year cycles). The Gregorian calendar repeats every 400 years +/// (146_097 days), so month, day and time of day come from chrono and only the year is carried, +/// which keeps every input exact instead of collapsing out-of-range years to the epoch. `secs` is +/// `u64` (the clock is clamped at the epoch before 1970), so `days` always fits an `i64`. +fn utc_parts(secs: u64, nanos: u32) -> (i64, chrono::NaiveDateTime) { + use chrono::Datelike; + let (days, sod) = ((secs / 86_400) as i64, (secs % 86_400) as i64); + let (cycles, rest) = (days.div_euclid(146_097), days.rem_euclid(146_097)); + // `rest + 719_163` (1970-01-01 is CE day 719_163) is always a valid CE day, and `sod` < 86_400. + let date = i32::try_from(rest + 719_163).ok().and_then(chrono::NaiveDate::from_num_days_from_ce_opt); + let time = u32::try_from(sod).ok().and_then(|s| chrono::NaiveTime::from_num_seconds_from_midnight_opt(s, nanos.min(999_999_999))); + match (date, time) { + (Some(d), Some(t)) => { + let y = i128::from(d.year()) + i128::from(cycles) * 400; + (i64::try_from(y).unwrap_or(i64::MAX), d.and_time(t)) + } + _ => (1970, chrono::NaiveDateTime::default()), // unreachable + } } fn numbered(dir: &Path, n: usize) -> PathBuf { @@ -393,6 +399,17 @@ mod tests { assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(4_107_542_400)), "2100-03-01T00:00:00.000Z"); // Before the epoch (a clock set wrong) clamps instead of panicking. assert_eq!(timestamp(UNIX_EPOCH - Duration::from_secs(5)), "1970-01-01T00:00:00.000Z"); + // A clock set absurdly far ahead (past chrono's own range) keeps its real year rather than + // collapsing to the epoch. Windows' SystemTime can't hold such an instant, so it is built + // with a checked add and only asserted where it exists. + if let Some(far) = UNIX_EPOCH.checked_add(Duration::from_secs(u64::MAX / 2)) { + assert_eq!(timestamp(far), "292277026596-12-04T15:30:07.000Z"); + } + if let Some(far) = UNIX_EPOCH.checked_add(Duration::from_secs(10_000_000_000_000)) { + assert_eq!(timestamp(far), "318857-05-20T17:46:40.000Z"); + } + // Years past 9999 keep the plain zero-padded year (no `+`). + assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(253_402_300_800)), "10000-01-01T00:00:00.000Z"); } #[test] diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 1decf6257b..e7d3695524 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -11,6 +11,7 @@ workspace = true [dependencies] serde = { workspace = true } thiserror = { workspace = true } +chrono = { workspace = true } [target.'cfg(target_arch = "wasm32")'.dependencies] js-sys = "0.3" diff --git a/crates/core/src/date.rs b/crates/core/src/date.rs index b61892f6c1..b42c068a00 100644 --- a/crates/core/src/date.rs +++ b/crates/core/src/date.rs @@ -28,15 +28,22 @@ pub struct DateTime { pub weekday: u32, } -/// Days from 0000-03-01 style civil algorithm (Howard Hinnant), days since 1970-01-01. +/// Days since 1970-01-01 for a proleptic Gregorian date, exact for every `i32` year. +/// +/// `NaiveDate` only covers about ±262143 years, but the Gregorian calendar repeats every 400 +/// years (146_097 days): chrono converts the year within its cycle and whole cycles are added +/// back as days (an i128 so the i32 extremes can't overflow). 1970-01-01 is `num_days_from_ce` +/// 719_163; the result matches the proleptic Gregorian count the old hand-rolled code gave. pub fn days_from_civil(y: i32, m: u32, d: u32) -> i64 { - let y = if m <= 2 { y as i64 - 1 } else { y as i64 }; - let era = if y >= 0 { y } else { y - 399 } / 400; - let yoe = y - era * 400; - let m = m as i64; - let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + d as i64 - 1; - let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; - era * 146097 + doe - 719468 + let (cycles, year) = (i128::from(i64::from(y).div_euclid(400)), i64::from(y).rem_euclid(400)); + i32::try_from(year) + .ok() + .and_then(|year| chrono::NaiveDate::from_ymd_opt(year, m, d)) + .and_then(|date| { + let days = i128::from(chrono::Datelike::num_days_from_ce(&date)) - 719_163 + cycles * 146_097; + i64::try_from(days).ok() + }) + .unwrap_or(0) } /// Milliseconds since the Unix epoch (UTC) from the system clock; `None` when the clock is unavailable. @@ -67,32 +74,50 @@ pub fn serial_in_system(sys: DateSystem, serial_1900: f64) -> f64 { } } +/// Proleptic Gregorian `(year, month, day)` for a day count `z` since 1970-01-01 (the inverse of +/// [`days_from_civil`]). +/// +/// The calendar repeats every 400 years (146_097 days): chrono converts the day within its cycle +/// and whole cycles go back into the year (an i128 so no `z` overflows), which matches the +/// proleptic Gregorian answer the old hand-rolled code gave past `NaiveDate`'s ±262143 range. +/// 1970-01-01 is `num_days_from_ce` 719_163. pub fn civil_from_days(z: i64) -> (i32, u32, u32) { - let z = z + 719468; - let era = if z >= 0 { z } else { z - 146096 } / 146097; - let doe = z - era * 146097; - let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; - let y = yoe + era * 400; - let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); - let mp = (5 * doy + 2) / 153; - let d = (doy - (153 * mp + 2) / 5 + 1) as u32; - let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32; - ((if m <= 2 { y + 1 } else { y }) as i32, m, d) + use chrono::Datelike; + let (cycles, rest) = (z.div_euclid(146_097), z.rem_euclid(146_097)); + match i32::try_from(rest + 719_163).ok().and_then(chrono::NaiveDate::from_num_days_from_ce_opt) { + Some(d) => { + let y = i128::from(d.year()) + i128::from(cycles) * 400; + (i32::try_from(y).unwrap_or(if y < 0 { i32::MIN } else { i32::MAX }), d.month(), d.day()) + } + // Unreachable: `rest + 719_163` is always a valid CE day. + None => (1970, 1, 1), + } } const UNIX_1899_12_30: i64 = -25569; // days_from_civil(1899, 12, 30) const UNIX_1904_01_01: i64 = -24107; +/// Whether `y` is a Gregorian leap year, for every `i32` year. pub fn is_leap(y: i32) -> bool { - (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 + // A year is a leap year iff 29 February exists in it. Leap years repeat every 400 years, so + // the year within its Gregorian cycle (`rem_euclid(400)`) decides it and always fits chrono. + let y = i64::from(y).rem_euclid(400) as i32; + chrono::NaiveDate::from_ymd_opt(y, 2, 29).is_some() } +/// Number of days in month `m` of year `y` (1–31), or `0` for an invalid month number, for every +/// `i32` year. pub fn days_in_month(y: i32, m: u32) -> u32 { - match m { - 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, - 4 | 6 | 9 | 11 => 30, - 2 if is_leap(y) => 29, - 2 => 28, + // The length of a month is the day before the first of the next month. Month lengths repeat + // every 400 years, so the year within its Gregorian cycle (`rem_euclid(400)`) is enough and + // always fits chrono (so December can't overflow `y + 1`). An invalid month keeps the old `0`. + if !(1..=12).contains(&m) { + return 0; + } + let y = i64::from(y).rem_euclid(400) as i32; + let first = |y: i32, m: u32| chrono::NaiveDate::from_ymd_opt(y, m, 1); + match (first(y, m), if m == 12 { first(y + 1, 1) } else { first(y, m + 1) }) { + (Some(a), Some(b)) => u32::try_from((b - a).num_days()).unwrap_or(0), _ => 0, } } @@ -196,6 +221,68 @@ pub const WEEKDAYS: [&str; 7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Th mod tests { use super::*; + #[test] + fn leap_and_month_length_outside_chrono_range() { + // In range, the ordinary Gregorian rule holds. + assert!(is_leap(2000)); + assert!(!is_leap(1900)); + assert!(is_leap(2024)); + assert!(!is_leap(2023)); + // Outside chrono's NaiveDate range the Gregorian rule is still applied. + assert!(is_leap(400_000)); // divisible by 400 + assert!(!is_leap(300_100)); // century not divisible by 400 + assert!(is_leap(i32::MAX - 3)); // 2147483644 is divisible by 4, not a century + assert!(!is_leap(i32::MAX)); // 2147483647 is odd + // days_in_month keeps calendar lengths out of range and 0 for invalid months. + assert_eq!(days_in_month(400_000, 2), 29); + assert_eq!(days_in_month(300_100, 2), 28); + assert_eq!(days_in_month(400_000, 1), 31); + assert_eq!(days_in_month(400_000, 4), 30); + assert_eq!(days_in_month(2024, 2), 29); + assert_eq!(days_in_month(2023, 2), 28); + assert_eq!(days_in_month(2023, 0), 0); + assert_eq!(days_in_month(2023, 13), 0); + // December at i32::MAX does not overflow and still reports 31 days. + assert_eq!(days_in_month(i32::MAX, 12), 31); + assert_eq!(days_in_month(i32::MAX, 2), 28); // 2147483647 is not a leap year + } + + #[test] + fn civil_conversions_exact_beyond_chrono_range() { + assert_eq!(days_from_civil(1970, 1, 1), 0); + assert_eq!(civil_from_days(0), (1970, 1, 1)); + // Past `NaiveDate`'s ±262143 the 400-year cycles carry the result, exactly, both sides of + // the epoch; values round-trip through the proleptic Gregorian converters. + for (y, m, d, z) in [ + (262_144i32, 1u32, 1u32, 95_026_602i64), + (300_000, 1, 1, 108_853_222), + (400_000, 2, 29, 145_377_531), + (-262_145, 3, 1, -96_465_964), + (-300_000, 6, 15, -110_292_112), + ] { + assert_eq!(days_from_civil(y, m, d), z, "{y}-{m}-{d}"); + assert_eq!(civil_from_days(z), (y, m, d), "{z}"); + } + // The extremes must not overflow or panic (i128 intermediates). + let _ = (days_from_civil(i32::MAX, 12, 31), days_from_civil(i32::MIN, 1, 1), civil_from_days(i64::MAX), civil_from_days(i64::MIN)); + } + + #[test] + fn serials_unchanged_through_cycle_conversion() { + // The 1900 leap bug and 1904 semantics must stay exact; the converters only ever see + // in-range years here, so the serial outputs are byte-for-byte the pre-chrono ones. + let s = DateSystem::D1900; + assert_eq!(serial_from_ymd(s, 1900, 2, 29), Some(60.0)); // Excel's fictitious leap day + assert_eq!(serial_from_ymd(s, 1900, 3, 1), Some(61.0)); + assert_eq!(serial_from_ymd(s, 9999, 12, 31), Some(2958465.0)); + assert_eq!(days_in_month_in(s, 1900, 2), 29); + let s4 = DateSystem::D1904; + assert_eq!(serial_from_ymd(s4, 1904, 1, 1), Some(0.0)); // serial 0 is 1904-01-01 + assert_eq!(serial_from_ymd(s4, 1904, 2, 29), Some(59.0)); // 1904 is a real leap year + assert_eq!(serial_from_ymd(s4, 1900, 2, 29), None); // no fictitious day in 1904 system + assert_eq!(days_in_month_in(s4, 1900, 2), 28); + } + #[test] fn serial_from_unix_time() { assert_eq!(serial_from_unix_ms(0.0), 25569.0); From 8f2a60d17638aeee39d5152fc48df9a6be7ba292 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:56:16 +0200 Subject: [PATCH 107/687] Keep the in-cell editor the cell's width while the text fits (#2336) --- crates/ui-egui/src/grid.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index acef3f8cb7..859cf10530 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -3094,6 +3094,12 @@ fn editor_slack(size: f32) -> f32 { 14.0 + size } +/// Width of the one-line in-cell editor. While the text fits the cell (with the usual 14 px margin) the +/// editor is exactly the cell wide, as in Excel; once it overflows, the extra `editor_slack` applies. +fn editor_width(text_w: f32, cell_w: f32, size: f32) -> f32 { + if text_w + 14.0 <= cell_w { cell_w } else { text_w + editor_slack(size) } +} + fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet, wb: &Workbook) { let Some(ed) = app.editor.as_ref() else { return }; if ed.sheet != wb.active_sheet { @@ -3120,7 +3126,7 @@ fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet, let text_w = ui.painter().layout_no_wrap(ed.text.clone(), font.clone(), Color32::BLACK).size().x; // Excel keeps the cell's height for one line and only grows for extra lines. let h = if lines > 1.0 { (size * 1.3 * lines + 4.0).max(rect.height()) } else { rect.height() }; - ((text_w + editor_slack(size)).max(rect.width()).min(geo.cells.right() - rect.left()), h) + (editor_width(text_w, rect.width(), size).min(geo.cells.right() - rect.left()), h) }; // Grows to the left on a right-to-left sheet. let erect = geo.mirror_rect(Rect::from_min_size(rect.min, vec2(w, h))); @@ -4009,6 +4015,14 @@ pub(crate) fn heading_font_size(zoom: f32) -> f32 { #[cfg(test)] mod tests { + #[test] + fn editor_matches_the_cell_while_the_text_fits() { + use super::editor_width; + assert_eq!(editor_width(10.0, 80.0, 7.0), 80.0); + assert_eq!(editor_width(66.0, 80.0, 7.0), 80.0); + assert_eq!(editor_width(67.0, 80.0, 7.0), 88.0); + } + #[test] fn editor_slack_fits_one_more_glyph_at_high_zoom() { use super::editor_slack; From 45ec9dfbfe6b1a0a82f4c6ddf92fbe68a3e6e608 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 18:44:23 +0200 Subject: [PATCH 108/687] Build the saved UiState in the test with struct update syntax (clippy field_reassign_with_default) --- crates/ui-egui/tests/contextual_ribbon_tab.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/ui-egui/tests/contextual_ribbon_tab.rs b/crates/ui-egui/tests/contextual_ribbon_tab.rs index bdd8b5c938..5221f2d60d 100644 --- a/crates/ui-egui/tests/contextual_ribbon_tab.rs +++ b/crates/ui-egui/tests/contextual_ribbon_tab.rs @@ -75,9 +75,7 @@ fn leaving_the_table_while_on_table_design_returns_to_home() { #[test] fn startup_opens_on_home_not_the_saved_tab() { - let mut saved = gridcraft_ui_egui::UiState::default(); - saved.ribbon_tab = "Table Design".into(); - saved.dark = true; + let saved = gridcraft_ui_egui::UiState { ribbon_tab: "Table Design".into(), dark: true, ..Default::default() }; let json = serde_json::to_string(&saved).unwrap(); assert!(json.contains(r#""ribbonTab":"Table Design""#), "{json}"); let restored = serde_json::from_str::(&json).unwrap().at_startup(); From a7608ada6dc91f9caf53f9eb7c2cdb3231ab1e75 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Fri, 23 Oct 2026 12:59:46 -0400 Subject: [PATCH 109/687] CI: one test binary per crate, nextest, line tables only (#2372) * CI: dev builds keep line tables only * CI: one integration-test binary per crate * CI: xtask runs tests with nextest, doctests apart, --keep-going on clippy and wasm cargo xtask test runs the workspace tests (--lib --bins --tests) with cargo-nextest when it is installed, else cargo test, then the doctests. cargo xtask ci uses the same two steps. * CI: docs name cargo xtask test * CI: agent guides name the moved fuzz test * CI: tests added on main join their crate's test binary --- AGENTS.md | 2 +- CLAUDE.md | 2 +- Cargo.toml | 2 + apps/gridcraft-cli/tests/{ => it}/cli.rs | 2 - apps/gridcraft-cli/tests/it/main.rs | 5 + .../tests/{ => it}/text_to_columns_quotes.rs | 0 .../tests/{ => it}/autofit_filter_buttons.rs | 0 .../tests/{ => it}/cf_formula_validate.rs | 0 .../tests/{ => it}/chart_workbook_names.rs | 0 .../tests/{ => it}/command_sequence_fuzz.rs | 4 +- .../engine/tests/{ => it}/cross_sheet_cut.rs | 0 .../tests/{ => it}/csv_export_budget.rs | 0 .../tests/{ => it}/custom_filter_blanks.rs | 0 .../tests/{ => it}/custom_view_freeze.rs | 0 .../engine/tests/{ => it}/dynamicmetadata.rs | 0 .../tests/{ => it}/extreme_selection_moves.rs | 0 .../tests/{ => it}/format_as_table_restyle.rs | 0 .../tests/{ => it}/hidden_sheet_navigation.rs | 0 crates/engine/tests/{ => it}/ink_roundtrip.rs | 0 .../tests/{ => it}/line_count_overflow.rs | 0 .../long_operator_chains.rs} | 0 crates/engine/tests/it/main.rs | 29 ++++++ .../engine/tests/{ => it}/offset_overflow.rs | 0 crates/engine/tests/{ => it}/percent_entry.rs | 0 .../tests/{ => it}/pivot_unknown_func.rs | 0 .../engine/tests/{ => it}/pivotchart_link.rs | 0 .../tests/{ => it}/range_fill_formats.rs | 0 crates/engine/tests/{ => it}/row_autofit.rs | 0 .../tests/{ => it}/sheet_name_quoting.rs | 0 .../{ => it}/sparse_clear_invalidation.rs | 0 .../tests/{ => it}/stored_list_shape.rs | 0 .../tests/{ => it}/table_resize_overlap.rs | 0 .../{ => it}/table_total_row_last_row.rs | 0 .../engine/tests/{ => it}/wrap_row_height.rs | 0 crates/formula/tests/it/main.rs | 4 + .../tests/{ => it}/printer_roundtrip.rs | 0 .../formula/tests/{ => it}/spanish_input.rs | 0 .../ui-egui/tests/{ => it}/accounting_fill.rs | 0 .../ui-egui/tests/{ => it}/advanced_filter.rs | 0 .../ui-egui/tests/{ => it}/argument_names.rs | 0 .../tests/{ => it}/autosave_persist.rs | 0 crates/ui-egui/tests/{ => it}/borders.rs | 0 .../tests/{ => it}/cell_editor_caret.rs | 0 .../ui-egui/tests/{ => it}/cell_pictures.rs | 0 crates/ui-egui/tests/{ => it}/chart_resize.rs | 0 crates/ui-egui/tests/{ => it}/clipboard.rs | 0 .../ui-egui/tests/{ => it}/comments_pane.rs | 0 .../tests/{ => it}/commit_after_pointing.rs | 0 .../tests/{ => it}/committed_text_fields.rs | 0 .../tests/{ => it}/context_menu_stays_open.rs | 0 .../ui-egui/tests/{ => it}/control_timeout.rs | 0 .../tests/{ => it}/dialog_button_row.rs | 0 .../ui-egui/tests/{ => it}/dialog_confirm.rs | 0 crates/ui-egui/tests/{ => it}/dialog_focus.rs | 0 crates/ui-egui/tests/{ => it}/dialog_move.rs | 0 crates/ui-egui/tests/{ => it}/drag_cursor.rs | 0 crates/ui-egui/tests/{ => it}/file_menu.rs | 0 .../{ => it}/fill_handle_double_click.rs | 0 .../tests/{ => it}/fill_handle_right_drag.rs | 0 .../tests/{ => it}/fill_options_button.rs | 0 .../tests/{ => it}/filter_list_scroll.rs | 0 .../tests/{ => it}/filter_menu_enter.rs | 0 .../tests/{ => it}/find_dialog_focus.rs | 0 .../tests/{ => it}/floating_pictures.rs | 0 .../ui-egui/tests/{ => it}/font_size_box.rs | 0 .../ui-egui/tests/{ => it}/format_painter.rs | 0 .../ui-egui/tests/{ => it}/formula_locale.rs | 0 .../tests/{ => it}/formula_point_headers.rs | 0 .../ui-egui/tests/{ => it}/formula_popup.rs | 0 .../ui-egui/tests/{ => it}/freeze_handles.rs | 0 .../ui-egui/tests/{ => it}/header_autofit.rs | 0 .../tests/{ => it}/header_column_drag.rs | 0 crates/ui-egui/tests/{ => it}/host_chrome.rs | 0 .../tests/{ => it}/hyperlink_ctrl_click.rs | 0 .../tests/{ => it}/in_cell_editor_size.rs | 0 .../tests/{ => it}/in_cell_editor_wrap.rs | 0 .../tests/{ => it}/insert_copied_cells.rs | 0 crates/ui-egui/tests/{ => it}/interaction.rs | 0 .../ui-egui/tests/{ => it}/interface_scale.rs | 0 .../tests/{ => it}/keyboard_shortcuts.rs | 0 .../tests/{ => it}/keytips_generated.rs | 0 crates/ui-egui/tests/it/main.rs | 91 +++++++++++++++++++ crates/ui-egui/tests/{ => it}/menu_hover.rs | 0 crates/ui-egui/tests/{ => it}/merged_cells.rs | 0 crates/ui-egui/tests/{ => it}/more_colors.rs | 0 crates/ui-egui/tests/{ => it}/native_print.rs | 0 .../tests/{ => it}/nowrap_line_breaks.rs | 0 .../tests/{ => it}/number_stored_as_text.rs | 0 .../ui-egui/tests/{ => it}/open_encrypted.rs | 0 crates/ui-egui/tests/{ => it}/outline.rs | 0 .../tests/{ => it}/page_layout_view.rs | 0 .../tests/{ => it}/page_setup_margins.rs | 0 .../tests/{ => it}/paste_special_dialog.rs | 0 crates/ui-egui/tests/{ => it}/pivot_pane.rs | 0 .../ui-egui/tests/{ => it}/popup_dismissal.rs | 0 .../tests/{ => it}/protect_password.rs | 0 .../ui-egui/tests/{ => it}/right_to_left.rs | 0 .../ui-egui/tests/{ => it}/scroll_position.rs | 0 .../tests/{ => it}/scrollbar_column_fill.rs | 0 crates/ui-egui/tests/{ => it}/selection.rs | 0 .../ui-egui/tests/{ => it}/selection_fill.rs | 0 .../ui-egui/tests/{ => it}/selection_glide.rs | 0 .../tests/{ => it}/selection_scroll.rs | 0 .../ui-egui/tests/{ => it}/sheet_navigator.rs | 0 .../tests/{ => it}/sheet_tab_rename.rs | 0 crates/ui-egui/tests/{ => it}/sheet_tabs.rs | 0 crates/ui-egui/tests/{ => it}/show_objects.rs | 0 .../ui-egui/tests/{ => it}/shrink_to_fit.rs | 0 .../tests/{ => it}/sort_dialog_columns.rs | 0 .../ui-egui/tests/{ => it}/symbol_glyphs.rs | 0 crates/ui-egui/tests/{ => it}/system_theme.rs | 0 .../ui-egui/tests/{ => it}/table_headings.rs | 0 .../tests/{ => it}/text_box_editing.rs | 0 .../tests/{ => it}/text_overflow_scroll.rs | 0 crates/ui-egui/tests/{ => it}/touch_pan.rs | 0 .../tests/{ => it}/trace_navigation.rs | 0 .../tests/{ => it}/translated_form_fields.rs | 0 crates/ui-egui/tests/{ => it}/ui_clock.rs | 0 .../tests/{ => it}/validation_alert.rs | 0 .../{ => it}/validation_input_message.rs | 0 crates/ui-egui/tests/{ => it}/wheel_zoom.rs | 0 .../tests/{ => it}/whole_line_shortcuts.rs | 0 crates/ui-egui/tests/{ => it}/widgets.rs | 0 crates/ui-egui/tests/{ => it}/zoom_dialog.rs | 0 docs/roadmap.md | 6 +- docs/target-app-parity.md | 2 +- xtask/src/keytips.rs | 4 +- xtask/src/main.rs | 60 ++++++++---- 128 files changed, 186 insertions(+), 27 deletions(-) rename apps/gridcraft-cli/tests/{ => it}/cli.rs (99%) create mode 100644 apps/gridcraft-cli/tests/it/main.rs rename apps/gridcraft-cli/tests/{ => it}/text_to_columns_quotes.rs (100%) rename crates/engine/tests/{ => it}/autofit_filter_buttons.rs (100%) rename crates/engine/tests/{ => it}/cf_formula_validate.rs (100%) rename crates/engine/tests/{ => it}/chart_workbook_names.rs (100%) rename crates/engine/tests/{ => it}/command_sequence_fuzz.rs (99%) rename crates/engine/tests/{ => it}/cross_sheet_cut.rs (100%) rename crates/engine/tests/{ => it}/csv_export_budget.rs (100%) rename crates/engine/tests/{ => it}/custom_filter_blanks.rs (100%) rename crates/engine/tests/{ => it}/custom_view_freeze.rs (100%) rename crates/engine/tests/{ => it}/dynamicmetadata.rs (100%) rename crates/engine/tests/{ => it}/extreme_selection_moves.rs (100%) rename crates/engine/tests/{ => it}/format_as_table_restyle.rs (100%) rename crates/engine/tests/{ => it}/hidden_sheet_navigation.rs (100%) rename crates/engine/tests/{ => it}/ink_roundtrip.rs (100%) rename crates/engine/tests/{ => it}/line_count_overflow.rs (100%) rename crates/engine/tests/{long-operator-chains.rs => it/long_operator_chains.rs} (100%) create mode 100644 crates/engine/tests/it/main.rs rename crates/engine/tests/{ => it}/offset_overflow.rs (100%) rename crates/engine/tests/{ => it}/percent_entry.rs (100%) rename crates/engine/tests/{ => it}/pivot_unknown_func.rs (100%) rename crates/engine/tests/{ => it}/pivotchart_link.rs (100%) rename crates/engine/tests/{ => it}/range_fill_formats.rs (100%) rename crates/engine/tests/{ => it}/row_autofit.rs (100%) rename crates/engine/tests/{ => it}/sheet_name_quoting.rs (100%) rename crates/engine/tests/{ => it}/sparse_clear_invalidation.rs (100%) rename crates/engine/tests/{ => it}/stored_list_shape.rs (100%) rename crates/engine/tests/{ => it}/table_resize_overlap.rs (100%) rename crates/engine/tests/{ => it}/table_total_row_last_row.rs (100%) rename crates/engine/tests/{ => it}/wrap_row_height.rs (100%) create mode 100644 crates/formula/tests/it/main.rs rename crates/formula/tests/{ => it}/printer_roundtrip.rs (100%) rename crates/formula/tests/{ => it}/spanish_input.rs (100%) rename crates/ui-egui/tests/{ => it}/accounting_fill.rs (100%) rename crates/ui-egui/tests/{ => it}/advanced_filter.rs (100%) rename crates/ui-egui/tests/{ => it}/argument_names.rs (100%) rename crates/ui-egui/tests/{ => it}/autosave_persist.rs (100%) rename crates/ui-egui/tests/{ => it}/borders.rs (100%) rename crates/ui-egui/tests/{ => it}/cell_editor_caret.rs (100%) rename crates/ui-egui/tests/{ => it}/cell_pictures.rs (100%) rename crates/ui-egui/tests/{ => it}/chart_resize.rs (100%) rename crates/ui-egui/tests/{ => it}/clipboard.rs (100%) rename crates/ui-egui/tests/{ => it}/comments_pane.rs (100%) rename crates/ui-egui/tests/{ => it}/commit_after_pointing.rs (100%) rename crates/ui-egui/tests/{ => it}/committed_text_fields.rs (100%) rename crates/ui-egui/tests/{ => it}/context_menu_stays_open.rs (100%) rename crates/ui-egui/tests/{ => it}/control_timeout.rs (100%) rename crates/ui-egui/tests/{ => it}/dialog_button_row.rs (100%) rename crates/ui-egui/tests/{ => it}/dialog_confirm.rs (100%) rename crates/ui-egui/tests/{ => it}/dialog_focus.rs (100%) rename crates/ui-egui/tests/{ => it}/dialog_move.rs (100%) rename crates/ui-egui/tests/{ => it}/drag_cursor.rs (100%) rename crates/ui-egui/tests/{ => it}/file_menu.rs (100%) rename crates/ui-egui/tests/{ => it}/fill_handle_double_click.rs (100%) rename crates/ui-egui/tests/{ => it}/fill_handle_right_drag.rs (100%) rename crates/ui-egui/tests/{ => it}/fill_options_button.rs (100%) rename crates/ui-egui/tests/{ => it}/filter_list_scroll.rs (100%) rename crates/ui-egui/tests/{ => it}/filter_menu_enter.rs (100%) rename crates/ui-egui/tests/{ => it}/find_dialog_focus.rs (100%) rename crates/ui-egui/tests/{ => it}/floating_pictures.rs (100%) rename crates/ui-egui/tests/{ => it}/font_size_box.rs (100%) rename crates/ui-egui/tests/{ => it}/format_painter.rs (100%) rename crates/ui-egui/tests/{ => it}/formula_locale.rs (100%) rename crates/ui-egui/tests/{ => it}/formula_point_headers.rs (100%) rename crates/ui-egui/tests/{ => it}/formula_popup.rs (100%) rename crates/ui-egui/tests/{ => it}/freeze_handles.rs (100%) rename crates/ui-egui/tests/{ => it}/header_autofit.rs (100%) rename crates/ui-egui/tests/{ => it}/header_column_drag.rs (100%) rename crates/ui-egui/tests/{ => it}/host_chrome.rs (100%) rename crates/ui-egui/tests/{ => it}/hyperlink_ctrl_click.rs (100%) rename crates/ui-egui/tests/{ => it}/in_cell_editor_size.rs (100%) rename crates/ui-egui/tests/{ => it}/in_cell_editor_wrap.rs (100%) rename crates/ui-egui/tests/{ => it}/insert_copied_cells.rs (100%) rename crates/ui-egui/tests/{ => it}/interaction.rs (100%) rename crates/ui-egui/tests/{ => it}/interface_scale.rs (100%) rename crates/ui-egui/tests/{ => it}/keyboard_shortcuts.rs (100%) rename crates/ui-egui/tests/{ => it}/keytips_generated.rs (100%) create mode 100644 crates/ui-egui/tests/it/main.rs rename crates/ui-egui/tests/{ => it}/menu_hover.rs (100%) rename crates/ui-egui/tests/{ => it}/merged_cells.rs (100%) rename crates/ui-egui/tests/{ => it}/more_colors.rs (100%) rename crates/ui-egui/tests/{ => it}/native_print.rs (100%) rename crates/ui-egui/tests/{ => it}/nowrap_line_breaks.rs (100%) rename crates/ui-egui/tests/{ => it}/number_stored_as_text.rs (100%) rename crates/ui-egui/tests/{ => it}/open_encrypted.rs (100%) rename crates/ui-egui/tests/{ => it}/outline.rs (100%) rename crates/ui-egui/tests/{ => it}/page_layout_view.rs (100%) rename crates/ui-egui/tests/{ => it}/page_setup_margins.rs (100%) rename crates/ui-egui/tests/{ => it}/paste_special_dialog.rs (100%) rename crates/ui-egui/tests/{ => it}/pivot_pane.rs (100%) rename crates/ui-egui/tests/{ => it}/popup_dismissal.rs (100%) rename crates/ui-egui/tests/{ => it}/protect_password.rs (100%) rename crates/ui-egui/tests/{ => it}/right_to_left.rs (100%) rename crates/ui-egui/tests/{ => it}/scroll_position.rs (100%) rename crates/ui-egui/tests/{ => it}/scrollbar_column_fill.rs (100%) rename crates/ui-egui/tests/{ => it}/selection.rs (100%) rename crates/ui-egui/tests/{ => it}/selection_fill.rs (100%) rename crates/ui-egui/tests/{ => it}/selection_glide.rs (100%) rename crates/ui-egui/tests/{ => it}/selection_scroll.rs (100%) rename crates/ui-egui/tests/{ => it}/sheet_navigator.rs (100%) rename crates/ui-egui/tests/{ => it}/sheet_tab_rename.rs (100%) rename crates/ui-egui/tests/{ => it}/sheet_tabs.rs (100%) rename crates/ui-egui/tests/{ => it}/show_objects.rs (100%) rename crates/ui-egui/tests/{ => it}/shrink_to_fit.rs (100%) rename crates/ui-egui/tests/{ => it}/sort_dialog_columns.rs (100%) rename crates/ui-egui/tests/{ => it}/symbol_glyphs.rs (100%) rename crates/ui-egui/tests/{ => it}/system_theme.rs (100%) rename crates/ui-egui/tests/{ => it}/table_headings.rs (100%) rename crates/ui-egui/tests/{ => it}/text_box_editing.rs (100%) rename crates/ui-egui/tests/{ => it}/text_overflow_scroll.rs (100%) rename crates/ui-egui/tests/{ => it}/touch_pan.rs (100%) rename crates/ui-egui/tests/{ => it}/trace_navigation.rs (100%) rename crates/ui-egui/tests/{ => it}/translated_form_fields.rs (100%) rename crates/ui-egui/tests/{ => it}/ui_clock.rs (100%) rename crates/ui-egui/tests/{ => it}/validation_alert.rs (100%) rename crates/ui-egui/tests/{ => it}/validation_input_message.rs (100%) rename crates/ui-egui/tests/{ => it}/wheel_zoom.rs (100%) rename crates/ui-egui/tests/{ => it}/whole_line_shortcuts.rs (100%) rename crates/ui-egui/tests/{ => it}/widgets.rs (100%) rename crates/ui-egui/tests/{ => it}/zoom_dialog.rs (100%) diff --git a/AGENTS.md b/AGENTS.md index ab73047ef4..7fd1629cf5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -26,7 +26,7 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr - **Input-derived numbers are hostile** (files, formulas, commands, MCP/control params): `get()` instead of indexing, checked/saturating arithmetic, cap input-sized allocations (huge ranges, `REPT`, `SEQUENCE`, whole-column references). - **Bound recursion** (formula nesting, dependency chains, names referring to names). - **Last-resort guard:** `Session::execute` catches escaped panics and restores the workbook. It's a safety net, not a licence. -- **Prove it:** every crash fix lands with a regression test (`tests::every_command_survives_empty_params` fuzzes every command; `crates/engine/tests/command_sequence_fuzz.rs` fuzzes random command sequences against undo/redo, XLSX round-trip and full-recalc invariants; soak it with `GRIDCRAFT_FUZZ_SEEDS`/`GRIDCRAFT_FUZZ_STEPS`, replay with `GRIDCRAFT_FUZZ_SEED`). +- **Prove it:** every crash fix lands with a regression test (`tests::every_command_survives_empty_params` fuzzes every command; `crates/engine/tests/it/command_sequence_fuzz.rs` fuzzes random command sequences against undo/redo, XLSX round-trip and full-recalc invariants; soak it with `GRIDCRAFT_FUZZ_SEEDS`/`GRIDCRAFT_FUZZ_STEPS`, replay with `GRIDCRAFT_FUZZ_SEED`). - Crates carry `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`. ## Non-negotiables diff --git a/CLAUDE.md b/CLAUDE.md index 280fe8bcb0..1d1a77d1f5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -26,7 +26,7 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr - **Input-derived numbers are hostile** (files, formulas, commands, MCP/control params): `get()` instead of indexing, checked/saturating arithmetic, cap input-sized allocations (huge ranges, `REPT`, `SEQUENCE`, whole-column references). - **Bound recursion** (formula nesting, dependency chains, names referring to names). - **Last-resort guard:** `Session::execute` catches escaped panics and restores the workbook. It's a safety net, not a licence. -- **Prove it:** every crash fix lands with a regression test (`tests::every_command_survives_empty_params` fuzzes every command; `crates/engine/tests/command_sequence_fuzz.rs` fuzzes random command sequences against undo/redo, XLSX round-trip and full-recalc invariants; soak it with `GRIDCRAFT_FUZZ_SEEDS`/`GRIDCRAFT_FUZZ_STEPS`, replay with `GRIDCRAFT_FUZZ_SEED`). +- **Prove it:** every crash fix lands with a regression test (`tests::every_command_survives_empty_params` fuzzes every command; `crates/engine/tests/it/command_sequence_fuzz.rs` fuzzes random command sequences against undo/redo, XLSX round-trip and full-recalc invariants; soak it with `GRIDCRAFT_FUZZ_SEEDS`/`GRIDCRAFT_FUZZ_STEPS`, replay with `GRIDCRAFT_FUZZ_SEED`). - Crates carry `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`. ## Non-negotiables diff --git a/Cargo.toml b/Cargo.toml index 9994d44e3a..4d3fd4b83e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,9 +68,11 @@ type_complexity = "allow" [profile.dev.package."*"] opt-level = 2 +debug = false [profile.dev] opt-level = 1 +debug = "line-tables-only" [profile.release] opt-level = 3 diff --git a/apps/gridcraft-cli/tests/cli.rs b/apps/gridcraft-cli/tests/it/cli.rs similarity index 99% rename from apps/gridcraft-cli/tests/cli.rs rename to apps/gridcraft-cli/tests/it/cli.rs index 5295368e3d..dde018ca8d 100644 --- a/apps/gridcraft-cli/tests/cli.rs +++ b/apps/gridcraft-cli/tests/it/cli.rs @@ -1,5 +1,3 @@ -#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] - use std::io::Write; use std::path::PathBuf; use std::process::{Command, Output, Stdio}; diff --git a/apps/gridcraft-cli/tests/it/main.rs b/apps/gridcraft-cli/tests/it/main.rs new file mode 100644 index 0000000000..17f8db87f4 --- /dev/null +++ b/apps/gridcraft-cli/tests/it/main.rs @@ -0,0 +1,5 @@ +//! The crate's integration tests, one module per file, built as one test binary. + +#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] +mod cli; +mod text_to_columns_quotes; diff --git a/apps/gridcraft-cli/tests/text_to_columns_quotes.rs b/apps/gridcraft-cli/tests/it/text_to_columns_quotes.rs similarity index 100% rename from apps/gridcraft-cli/tests/text_to_columns_quotes.rs rename to apps/gridcraft-cli/tests/it/text_to_columns_quotes.rs diff --git a/crates/engine/tests/autofit_filter_buttons.rs b/crates/engine/tests/it/autofit_filter_buttons.rs similarity index 100% rename from crates/engine/tests/autofit_filter_buttons.rs rename to crates/engine/tests/it/autofit_filter_buttons.rs diff --git a/crates/engine/tests/cf_formula_validate.rs b/crates/engine/tests/it/cf_formula_validate.rs similarity index 100% rename from crates/engine/tests/cf_formula_validate.rs rename to crates/engine/tests/it/cf_formula_validate.rs diff --git a/crates/engine/tests/chart_workbook_names.rs b/crates/engine/tests/it/chart_workbook_names.rs similarity index 100% rename from crates/engine/tests/chart_workbook_names.rs rename to crates/engine/tests/it/chart_workbook_names.rs diff --git a/crates/engine/tests/command_sequence_fuzz.rs b/crates/engine/tests/it/command_sequence_fuzz.rs similarity index 99% rename from crates/engine/tests/command_sequence_fuzz.rs rename to crates/engine/tests/it/command_sequence_fuzz.rs index f8cb603cba..012757f697 100644 --- a/crates/engine/tests/command_sequence_fuzz.rs +++ b/crates/engine/tests/it/command_sequence_fuzz.rs @@ -23,7 +23,7 @@ //! //! Knobs (environment variables): //! - `GRIDCRAFT_FUZZ_SEEDS` (default 6) and `GRIDCRAFT_FUZZ_STEPS` (default 40): the budget. Soak with -//! `GRIDCRAFT_FUZZ_SEEDS=200 GRIDCRAFT_FUZZ_STEPS=200 cargo test --release -p gridcraft-engine --test command_sequence_fuzz`. +//! `GRIDCRAFT_FUZZ_SEEDS=200 GRIDCRAFT_FUZZ_STEPS=200 cargo test --release -p gridcraft-engine --test it command_sequence_fuzz::`. //! - `GRIDCRAFT_FUZZ_SEED=17`: replay that one seed (the failure message prints the command line). //! - `GRIDCRAFT_FUZZ_SHRINK=1`: on failure, shrink the sequence to a minimal list of commands that //! still fails the same way, printed as `execute` calls ready for a regression test. @@ -138,7 +138,7 @@ fn report(seed: u64, steps: usize, sample: &str, sequence: &[Step], f: &Failure) None => format!("end-of-run checks after {} steps", sequence.len()), }; let mut out = format!( - "command-sequence fuzz failure: seed {seed} (sample `{sample}`), {at}\n invariant [{}]: {}\n replay: GRIDCRAFT_FUZZ_SEED={seed} GRIDCRAFT_FUZZ_STEPS={steps} cargo test -p gridcraft-engine --test command_sequence_fuzz\n", + "command-sequence fuzz failure: seed {seed} (sample `{sample}`), {at}\n invariant [{}]: {}\n replay: GRIDCRAFT_FUZZ_SEED={seed} GRIDCRAFT_FUZZ_STEPS={steps} cargo test -p gridcraft-engine --test it command_sequence_fuzz::\n", f.kind, f.what ); let end = (f.step + 1).min(sequence.len()); diff --git a/crates/engine/tests/cross_sheet_cut.rs b/crates/engine/tests/it/cross_sheet_cut.rs similarity index 100% rename from crates/engine/tests/cross_sheet_cut.rs rename to crates/engine/tests/it/cross_sheet_cut.rs diff --git a/crates/engine/tests/csv_export_budget.rs b/crates/engine/tests/it/csv_export_budget.rs similarity index 100% rename from crates/engine/tests/csv_export_budget.rs rename to crates/engine/tests/it/csv_export_budget.rs diff --git a/crates/engine/tests/custom_filter_blanks.rs b/crates/engine/tests/it/custom_filter_blanks.rs similarity index 100% rename from crates/engine/tests/custom_filter_blanks.rs rename to crates/engine/tests/it/custom_filter_blanks.rs diff --git a/crates/engine/tests/custom_view_freeze.rs b/crates/engine/tests/it/custom_view_freeze.rs similarity index 100% rename from crates/engine/tests/custom_view_freeze.rs rename to crates/engine/tests/it/custom_view_freeze.rs diff --git a/crates/engine/tests/dynamicmetadata.rs b/crates/engine/tests/it/dynamicmetadata.rs similarity index 100% rename from crates/engine/tests/dynamicmetadata.rs rename to crates/engine/tests/it/dynamicmetadata.rs diff --git a/crates/engine/tests/extreme_selection_moves.rs b/crates/engine/tests/it/extreme_selection_moves.rs similarity index 100% rename from crates/engine/tests/extreme_selection_moves.rs rename to crates/engine/tests/it/extreme_selection_moves.rs diff --git a/crates/engine/tests/format_as_table_restyle.rs b/crates/engine/tests/it/format_as_table_restyle.rs similarity index 100% rename from crates/engine/tests/format_as_table_restyle.rs rename to crates/engine/tests/it/format_as_table_restyle.rs diff --git a/crates/engine/tests/hidden_sheet_navigation.rs b/crates/engine/tests/it/hidden_sheet_navigation.rs similarity index 100% rename from crates/engine/tests/hidden_sheet_navigation.rs rename to crates/engine/tests/it/hidden_sheet_navigation.rs diff --git a/crates/engine/tests/ink_roundtrip.rs b/crates/engine/tests/it/ink_roundtrip.rs similarity index 100% rename from crates/engine/tests/ink_roundtrip.rs rename to crates/engine/tests/it/ink_roundtrip.rs diff --git a/crates/engine/tests/line_count_overflow.rs b/crates/engine/tests/it/line_count_overflow.rs similarity index 100% rename from crates/engine/tests/line_count_overflow.rs rename to crates/engine/tests/it/line_count_overflow.rs diff --git a/crates/engine/tests/long-operator-chains.rs b/crates/engine/tests/it/long_operator_chains.rs similarity index 100% rename from crates/engine/tests/long-operator-chains.rs rename to crates/engine/tests/it/long_operator_chains.rs diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs new file mode 100644 index 0000000000..385ec76caa --- /dev/null +++ b/crates/engine/tests/it/main.rs @@ -0,0 +1,29 @@ +//! The crate's integration tests, one module per file, built as one test binary. + +mod autofit_filter_buttons; +mod cf_formula_validate; +mod chart_workbook_names; +mod command_sequence_fuzz; +mod cross_sheet_cut; +mod csv_export_budget; +mod custom_filter_blanks; +mod custom_view_freeze; +mod dynamicmetadata; +mod extreme_selection_moves; +mod format_as_table_restyle; +mod hidden_sheet_navigation; +mod ink_roundtrip; +mod line_count_overflow; +mod long_operator_chains; +mod offset_overflow; +mod percent_entry; +mod pivot_unknown_func; +mod pivotchart_link; +mod range_fill_formats; +mod row_autofit; +mod sheet_name_quoting; +mod sparse_clear_invalidation; +mod stored_list_shape; +mod table_resize_overlap; +mod table_total_row_last_row; +mod wrap_row_height; diff --git a/crates/engine/tests/offset_overflow.rs b/crates/engine/tests/it/offset_overflow.rs similarity index 100% rename from crates/engine/tests/offset_overflow.rs rename to crates/engine/tests/it/offset_overflow.rs diff --git a/crates/engine/tests/percent_entry.rs b/crates/engine/tests/it/percent_entry.rs similarity index 100% rename from crates/engine/tests/percent_entry.rs rename to crates/engine/tests/it/percent_entry.rs diff --git a/crates/engine/tests/pivot_unknown_func.rs b/crates/engine/tests/it/pivot_unknown_func.rs similarity index 100% rename from crates/engine/tests/pivot_unknown_func.rs rename to crates/engine/tests/it/pivot_unknown_func.rs diff --git a/crates/engine/tests/pivotchart_link.rs b/crates/engine/tests/it/pivotchart_link.rs similarity index 100% rename from crates/engine/tests/pivotchart_link.rs rename to crates/engine/tests/it/pivotchart_link.rs diff --git a/crates/engine/tests/range_fill_formats.rs b/crates/engine/tests/it/range_fill_formats.rs similarity index 100% rename from crates/engine/tests/range_fill_formats.rs rename to crates/engine/tests/it/range_fill_formats.rs diff --git a/crates/engine/tests/row_autofit.rs b/crates/engine/tests/it/row_autofit.rs similarity index 100% rename from crates/engine/tests/row_autofit.rs rename to crates/engine/tests/it/row_autofit.rs diff --git a/crates/engine/tests/sheet_name_quoting.rs b/crates/engine/tests/it/sheet_name_quoting.rs similarity index 100% rename from crates/engine/tests/sheet_name_quoting.rs rename to crates/engine/tests/it/sheet_name_quoting.rs diff --git a/crates/engine/tests/sparse_clear_invalidation.rs b/crates/engine/tests/it/sparse_clear_invalidation.rs similarity index 100% rename from crates/engine/tests/sparse_clear_invalidation.rs rename to crates/engine/tests/it/sparse_clear_invalidation.rs diff --git a/crates/engine/tests/stored_list_shape.rs b/crates/engine/tests/it/stored_list_shape.rs similarity index 100% rename from crates/engine/tests/stored_list_shape.rs rename to crates/engine/tests/it/stored_list_shape.rs diff --git a/crates/engine/tests/table_resize_overlap.rs b/crates/engine/tests/it/table_resize_overlap.rs similarity index 100% rename from crates/engine/tests/table_resize_overlap.rs rename to crates/engine/tests/it/table_resize_overlap.rs diff --git a/crates/engine/tests/table_total_row_last_row.rs b/crates/engine/tests/it/table_total_row_last_row.rs similarity index 100% rename from crates/engine/tests/table_total_row_last_row.rs rename to crates/engine/tests/it/table_total_row_last_row.rs diff --git a/crates/engine/tests/wrap_row_height.rs b/crates/engine/tests/it/wrap_row_height.rs similarity index 100% rename from crates/engine/tests/wrap_row_height.rs rename to crates/engine/tests/it/wrap_row_height.rs diff --git a/crates/formula/tests/it/main.rs b/crates/formula/tests/it/main.rs new file mode 100644 index 0000000000..01bff98c13 --- /dev/null +++ b/crates/formula/tests/it/main.rs @@ -0,0 +1,4 @@ +//! The crate's integration tests, one module per file, built as one test binary. + +mod printer_roundtrip; +mod spanish_input; diff --git a/crates/formula/tests/printer_roundtrip.rs b/crates/formula/tests/it/printer_roundtrip.rs similarity index 100% rename from crates/formula/tests/printer_roundtrip.rs rename to crates/formula/tests/it/printer_roundtrip.rs diff --git a/crates/formula/tests/spanish_input.rs b/crates/formula/tests/it/spanish_input.rs similarity index 100% rename from crates/formula/tests/spanish_input.rs rename to crates/formula/tests/it/spanish_input.rs diff --git a/crates/ui-egui/tests/accounting_fill.rs b/crates/ui-egui/tests/it/accounting_fill.rs similarity index 100% rename from crates/ui-egui/tests/accounting_fill.rs rename to crates/ui-egui/tests/it/accounting_fill.rs diff --git a/crates/ui-egui/tests/advanced_filter.rs b/crates/ui-egui/tests/it/advanced_filter.rs similarity index 100% rename from crates/ui-egui/tests/advanced_filter.rs rename to crates/ui-egui/tests/it/advanced_filter.rs diff --git a/crates/ui-egui/tests/argument_names.rs b/crates/ui-egui/tests/it/argument_names.rs similarity index 100% rename from crates/ui-egui/tests/argument_names.rs rename to crates/ui-egui/tests/it/argument_names.rs diff --git a/crates/ui-egui/tests/autosave_persist.rs b/crates/ui-egui/tests/it/autosave_persist.rs similarity index 100% rename from crates/ui-egui/tests/autosave_persist.rs rename to crates/ui-egui/tests/it/autosave_persist.rs diff --git a/crates/ui-egui/tests/borders.rs b/crates/ui-egui/tests/it/borders.rs similarity index 100% rename from crates/ui-egui/tests/borders.rs rename to crates/ui-egui/tests/it/borders.rs diff --git a/crates/ui-egui/tests/cell_editor_caret.rs b/crates/ui-egui/tests/it/cell_editor_caret.rs similarity index 100% rename from crates/ui-egui/tests/cell_editor_caret.rs rename to crates/ui-egui/tests/it/cell_editor_caret.rs diff --git a/crates/ui-egui/tests/cell_pictures.rs b/crates/ui-egui/tests/it/cell_pictures.rs similarity index 100% rename from crates/ui-egui/tests/cell_pictures.rs rename to crates/ui-egui/tests/it/cell_pictures.rs diff --git a/crates/ui-egui/tests/chart_resize.rs b/crates/ui-egui/tests/it/chart_resize.rs similarity index 100% rename from crates/ui-egui/tests/chart_resize.rs rename to crates/ui-egui/tests/it/chart_resize.rs diff --git a/crates/ui-egui/tests/clipboard.rs b/crates/ui-egui/tests/it/clipboard.rs similarity index 100% rename from crates/ui-egui/tests/clipboard.rs rename to crates/ui-egui/tests/it/clipboard.rs diff --git a/crates/ui-egui/tests/comments_pane.rs b/crates/ui-egui/tests/it/comments_pane.rs similarity index 100% rename from crates/ui-egui/tests/comments_pane.rs rename to crates/ui-egui/tests/it/comments_pane.rs diff --git a/crates/ui-egui/tests/commit_after_pointing.rs b/crates/ui-egui/tests/it/commit_after_pointing.rs similarity index 100% rename from crates/ui-egui/tests/commit_after_pointing.rs rename to crates/ui-egui/tests/it/commit_after_pointing.rs diff --git a/crates/ui-egui/tests/committed_text_fields.rs b/crates/ui-egui/tests/it/committed_text_fields.rs similarity index 100% rename from crates/ui-egui/tests/committed_text_fields.rs rename to crates/ui-egui/tests/it/committed_text_fields.rs diff --git a/crates/ui-egui/tests/context_menu_stays_open.rs b/crates/ui-egui/tests/it/context_menu_stays_open.rs similarity index 100% rename from crates/ui-egui/tests/context_menu_stays_open.rs rename to crates/ui-egui/tests/it/context_menu_stays_open.rs diff --git a/crates/ui-egui/tests/control_timeout.rs b/crates/ui-egui/tests/it/control_timeout.rs similarity index 100% rename from crates/ui-egui/tests/control_timeout.rs rename to crates/ui-egui/tests/it/control_timeout.rs diff --git a/crates/ui-egui/tests/dialog_button_row.rs b/crates/ui-egui/tests/it/dialog_button_row.rs similarity index 100% rename from crates/ui-egui/tests/dialog_button_row.rs rename to crates/ui-egui/tests/it/dialog_button_row.rs diff --git a/crates/ui-egui/tests/dialog_confirm.rs b/crates/ui-egui/tests/it/dialog_confirm.rs similarity index 100% rename from crates/ui-egui/tests/dialog_confirm.rs rename to crates/ui-egui/tests/it/dialog_confirm.rs diff --git a/crates/ui-egui/tests/dialog_focus.rs b/crates/ui-egui/tests/it/dialog_focus.rs similarity index 100% rename from crates/ui-egui/tests/dialog_focus.rs rename to crates/ui-egui/tests/it/dialog_focus.rs diff --git a/crates/ui-egui/tests/dialog_move.rs b/crates/ui-egui/tests/it/dialog_move.rs similarity index 100% rename from crates/ui-egui/tests/dialog_move.rs rename to crates/ui-egui/tests/it/dialog_move.rs diff --git a/crates/ui-egui/tests/drag_cursor.rs b/crates/ui-egui/tests/it/drag_cursor.rs similarity index 100% rename from crates/ui-egui/tests/drag_cursor.rs rename to crates/ui-egui/tests/it/drag_cursor.rs diff --git a/crates/ui-egui/tests/file_menu.rs b/crates/ui-egui/tests/it/file_menu.rs similarity index 100% rename from crates/ui-egui/tests/file_menu.rs rename to crates/ui-egui/tests/it/file_menu.rs diff --git a/crates/ui-egui/tests/fill_handle_double_click.rs b/crates/ui-egui/tests/it/fill_handle_double_click.rs similarity index 100% rename from crates/ui-egui/tests/fill_handle_double_click.rs rename to crates/ui-egui/tests/it/fill_handle_double_click.rs diff --git a/crates/ui-egui/tests/fill_handle_right_drag.rs b/crates/ui-egui/tests/it/fill_handle_right_drag.rs similarity index 100% rename from crates/ui-egui/tests/fill_handle_right_drag.rs rename to crates/ui-egui/tests/it/fill_handle_right_drag.rs diff --git a/crates/ui-egui/tests/fill_options_button.rs b/crates/ui-egui/tests/it/fill_options_button.rs similarity index 100% rename from crates/ui-egui/tests/fill_options_button.rs rename to crates/ui-egui/tests/it/fill_options_button.rs diff --git a/crates/ui-egui/tests/filter_list_scroll.rs b/crates/ui-egui/tests/it/filter_list_scroll.rs similarity index 100% rename from crates/ui-egui/tests/filter_list_scroll.rs rename to crates/ui-egui/tests/it/filter_list_scroll.rs diff --git a/crates/ui-egui/tests/filter_menu_enter.rs b/crates/ui-egui/tests/it/filter_menu_enter.rs similarity index 100% rename from crates/ui-egui/tests/filter_menu_enter.rs rename to crates/ui-egui/tests/it/filter_menu_enter.rs diff --git a/crates/ui-egui/tests/find_dialog_focus.rs b/crates/ui-egui/tests/it/find_dialog_focus.rs similarity index 100% rename from crates/ui-egui/tests/find_dialog_focus.rs rename to crates/ui-egui/tests/it/find_dialog_focus.rs diff --git a/crates/ui-egui/tests/floating_pictures.rs b/crates/ui-egui/tests/it/floating_pictures.rs similarity index 100% rename from crates/ui-egui/tests/floating_pictures.rs rename to crates/ui-egui/tests/it/floating_pictures.rs diff --git a/crates/ui-egui/tests/font_size_box.rs b/crates/ui-egui/tests/it/font_size_box.rs similarity index 100% rename from crates/ui-egui/tests/font_size_box.rs rename to crates/ui-egui/tests/it/font_size_box.rs diff --git a/crates/ui-egui/tests/format_painter.rs b/crates/ui-egui/tests/it/format_painter.rs similarity index 100% rename from crates/ui-egui/tests/format_painter.rs rename to crates/ui-egui/tests/it/format_painter.rs diff --git a/crates/ui-egui/tests/formula_locale.rs b/crates/ui-egui/tests/it/formula_locale.rs similarity index 100% rename from crates/ui-egui/tests/formula_locale.rs rename to crates/ui-egui/tests/it/formula_locale.rs diff --git a/crates/ui-egui/tests/formula_point_headers.rs b/crates/ui-egui/tests/it/formula_point_headers.rs similarity index 100% rename from crates/ui-egui/tests/formula_point_headers.rs rename to crates/ui-egui/tests/it/formula_point_headers.rs diff --git a/crates/ui-egui/tests/formula_popup.rs b/crates/ui-egui/tests/it/formula_popup.rs similarity index 100% rename from crates/ui-egui/tests/formula_popup.rs rename to crates/ui-egui/tests/it/formula_popup.rs diff --git a/crates/ui-egui/tests/freeze_handles.rs b/crates/ui-egui/tests/it/freeze_handles.rs similarity index 100% rename from crates/ui-egui/tests/freeze_handles.rs rename to crates/ui-egui/tests/it/freeze_handles.rs diff --git a/crates/ui-egui/tests/header_autofit.rs b/crates/ui-egui/tests/it/header_autofit.rs similarity index 100% rename from crates/ui-egui/tests/header_autofit.rs rename to crates/ui-egui/tests/it/header_autofit.rs diff --git a/crates/ui-egui/tests/header_column_drag.rs b/crates/ui-egui/tests/it/header_column_drag.rs similarity index 100% rename from crates/ui-egui/tests/header_column_drag.rs rename to crates/ui-egui/tests/it/header_column_drag.rs diff --git a/crates/ui-egui/tests/host_chrome.rs b/crates/ui-egui/tests/it/host_chrome.rs similarity index 100% rename from crates/ui-egui/tests/host_chrome.rs rename to crates/ui-egui/tests/it/host_chrome.rs diff --git a/crates/ui-egui/tests/hyperlink_ctrl_click.rs b/crates/ui-egui/tests/it/hyperlink_ctrl_click.rs similarity index 100% rename from crates/ui-egui/tests/hyperlink_ctrl_click.rs rename to crates/ui-egui/tests/it/hyperlink_ctrl_click.rs diff --git a/crates/ui-egui/tests/in_cell_editor_size.rs b/crates/ui-egui/tests/it/in_cell_editor_size.rs similarity index 100% rename from crates/ui-egui/tests/in_cell_editor_size.rs rename to crates/ui-egui/tests/it/in_cell_editor_size.rs diff --git a/crates/ui-egui/tests/in_cell_editor_wrap.rs b/crates/ui-egui/tests/it/in_cell_editor_wrap.rs similarity index 100% rename from crates/ui-egui/tests/in_cell_editor_wrap.rs rename to crates/ui-egui/tests/it/in_cell_editor_wrap.rs diff --git a/crates/ui-egui/tests/insert_copied_cells.rs b/crates/ui-egui/tests/it/insert_copied_cells.rs similarity index 100% rename from crates/ui-egui/tests/insert_copied_cells.rs rename to crates/ui-egui/tests/it/insert_copied_cells.rs diff --git a/crates/ui-egui/tests/interaction.rs b/crates/ui-egui/tests/it/interaction.rs similarity index 100% rename from crates/ui-egui/tests/interaction.rs rename to crates/ui-egui/tests/it/interaction.rs diff --git a/crates/ui-egui/tests/interface_scale.rs b/crates/ui-egui/tests/it/interface_scale.rs similarity index 100% rename from crates/ui-egui/tests/interface_scale.rs rename to crates/ui-egui/tests/it/interface_scale.rs diff --git a/crates/ui-egui/tests/keyboard_shortcuts.rs b/crates/ui-egui/tests/it/keyboard_shortcuts.rs similarity index 100% rename from crates/ui-egui/tests/keyboard_shortcuts.rs rename to crates/ui-egui/tests/it/keyboard_shortcuts.rs diff --git a/crates/ui-egui/tests/keytips_generated.rs b/crates/ui-egui/tests/it/keytips_generated.rs similarity index 100% rename from crates/ui-egui/tests/keytips_generated.rs rename to crates/ui-egui/tests/it/keytips_generated.rs diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs new file mode 100644 index 0000000000..b89253bb96 --- /dev/null +++ b/crates/ui-egui/tests/it/main.rs @@ -0,0 +1,91 @@ +//! The crate's integration tests, one module per file, built as one test binary. +//! +//! `header_autofit_fonts.rs` and `keytips.rs` stay separate binaries: they set process-wide state +//! (the installed-font catalog, a registered test command) that would leak into these tests. + +mod accounting_fill; +mod advanced_filter; +mod argument_names; +mod autosave_persist; +mod borders; +mod cell_editor_caret; +mod cell_pictures; +mod chart_resize; +mod clipboard; +mod comments_pane; +mod commit_after_pointing; +mod committed_text_fields; +mod context_menu_stays_open; +mod control_timeout; +mod dialog_button_row; +mod dialog_confirm; +mod dialog_focus; +mod dialog_move; +mod drag_cursor; +mod file_menu; +mod fill_handle_double_click; +mod fill_handle_right_drag; +mod fill_options_button; +mod filter_list_scroll; +mod filter_menu_enter; +mod find_dialog_focus; +mod floating_pictures; +mod font_size_box; +mod format_painter; +mod formula_locale; +mod formula_point_headers; +mod formula_popup; +mod freeze_handles; +mod header_autofit; +mod header_column_drag; +mod host_chrome; +mod hyperlink_ctrl_click; +mod in_cell_editor_size; +mod in_cell_editor_wrap; +mod insert_copied_cells; +mod interaction; +mod interface_scale; +mod keyboard_shortcuts; +mod keytips_generated; +mod menu_hover; +mod merged_cells; +mod more_colors; +mod native_print; +mod nowrap_line_breaks; +mod number_stored_as_text; +mod open_encrypted; +mod outline; +mod page_layout_view; +mod page_setup_margins; +mod paste_special_dialog; +mod pivot_pane; +mod popup_dismissal; +mod protect_password; +mod right_to_left; +mod scroll_position; +mod scrollbar_column_fill; +mod selection; +mod selection_fill; +mod selection_glide; +mod selection_scroll; +mod sheet_navigator; +mod sheet_tab_rename; +mod sheet_tabs; +mod show_objects; +mod shrink_to_fit; +mod sort_dialog_columns; +mod symbol_glyphs; +mod system_theme; +mod table_headings; +mod text_box_editing; +mod text_overflow_scroll; +mod touch_pan; +mod trace_navigation; +mod translated_form_fields; +mod ui_clock; +mod validation_alert; +mod validation_input_message; +mod wheel_zoom; +mod whole_line_shortcuts; +mod widgets; +mod zoom_dialog; diff --git a/crates/ui-egui/tests/menu_hover.rs b/crates/ui-egui/tests/it/menu_hover.rs similarity index 100% rename from crates/ui-egui/tests/menu_hover.rs rename to crates/ui-egui/tests/it/menu_hover.rs diff --git a/crates/ui-egui/tests/merged_cells.rs b/crates/ui-egui/tests/it/merged_cells.rs similarity index 100% rename from crates/ui-egui/tests/merged_cells.rs rename to crates/ui-egui/tests/it/merged_cells.rs diff --git a/crates/ui-egui/tests/more_colors.rs b/crates/ui-egui/tests/it/more_colors.rs similarity index 100% rename from crates/ui-egui/tests/more_colors.rs rename to crates/ui-egui/tests/it/more_colors.rs diff --git a/crates/ui-egui/tests/native_print.rs b/crates/ui-egui/tests/it/native_print.rs similarity index 100% rename from crates/ui-egui/tests/native_print.rs rename to crates/ui-egui/tests/it/native_print.rs diff --git a/crates/ui-egui/tests/nowrap_line_breaks.rs b/crates/ui-egui/tests/it/nowrap_line_breaks.rs similarity index 100% rename from crates/ui-egui/tests/nowrap_line_breaks.rs rename to crates/ui-egui/tests/it/nowrap_line_breaks.rs diff --git a/crates/ui-egui/tests/number_stored_as_text.rs b/crates/ui-egui/tests/it/number_stored_as_text.rs similarity index 100% rename from crates/ui-egui/tests/number_stored_as_text.rs rename to crates/ui-egui/tests/it/number_stored_as_text.rs diff --git a/crates/ui-egui/tests/open_encrypted.rs b/crates/ui-egui/tests/it/open_encrypted.rs similarity index 100% rename from crates/ui-egui/tests/open_encrypted.rs rename to crates/ui-egui/tests/it/open_encrypted.rs diff --git a/crates/ui-egui/tests/outline.rs b/crates/ui-egui/tests/it/outline.rs similarity index 100% rename from crates/ui-egui/tests/outline.rs rename to crates/ui-egui/tests/it/outline.rs diff --git a/crates/ui-egui/tests/page_layout_view.rs b/crates/ui-egui/tests/it/page_layout_view.rs similarity index 100% rename from crates/ui-egui/tests/page_layout_view.rs rename to crates/ui-egui/tests/it/page_layout_view.rs diff --git a/crates/ui-egui/tests/page_setup_margins.rs b/crates/ui-egui/tests/it/page_setup_margins.rs similarity index 100% rename from crates/ui-egui/tests/page_setup_margins.rs rename to crates/ui-egui/tests/it/page_setup_margins.rs diff --git a/crates/ui-egui/tests/paste_special_dialog.rs b/crates/ui-egui/tests/it/paste_special_dialog.rs similarity index 100% rename from crates/ui-egui/tests/paste_special_dialog.rs rename to crates/ui-egui/tests/it/paste_special_dialog.rs diff --git a/crates/ui-egui/tests/pivot_pane.rs b/crates/ui-egui/tests/it/pivot_pane.rs similarity index 100% rename from crates/ui-egui/tests/pivot_pane.rs rename to crates/ui-egui/tests/it/pivot_pane.rs diff --git a/crates/ui-egui/tests/popup_dismissal.rs b/crates/ui-egui/tests/it/popup_dismissal.rs similarity index 100% rename from crates/ui-egui/tests/popup_dismissal.rs rename to crates/ui-egui/tests/it/popup_dismissal.rs diff --git a/crates/ui-egui/tests/protect_password.rs b/crates/ui-egui/tests/it/protect_password.rs similarity index 100% rename from crates/ui-egui/tests/protect_password.rs rename to crates/ui-egui/tests/it/protect_password.rs diff --git a/crates/ui-egui/tests/right_to_left.rs b/crates/ui-egui/tests/it/right_to_left.rs similarity index 100% rename from crates/ui-egui/tests/right_to_left.rs rename to crates/ui-egui/tests/it/right_to_left.rs diff --git a/crates/ui-egui/tests/scroll_position.rs b/crates/ui-egui/tests/it/scroll_position.rs similarity index 100% rename from crates/ui-egui/tests/scroll_position.rs rename to crates/ui-egui/tests/it/scroll_position.rs diff --git a/crates/ui-egui/tests/scrollbar_column_fill.rs b/crates/ui-egui/tests/it/scrollbar_column_fill.rs similarity index 100% rename from crates/ui-egui/tests/scrollbar_column_fill.rs rename to crates/ui-egui/tests/it/scrollbar_column_fill.rs diff --git a/crates/ui-egui/tests/selection.rs b/crates/ui-egui/tests/it/selection.rs similarity index 100% rename from crates/ui-egui/tests/selection.rs rename to crates/ui-egui/tests/it/selection.rs diff --git a/crates/ui-egui/tests/selection_fill.rs b/crates/ui-egui/tests/it/selection_fill.rs similarity index 100% rename from crates/ui-egui/tests/selection_fill.rs rename to crates/ui-egui/tests/it/selection_fill.rs diff --git a/crates/ui-egui/tests/selection_glide.rs b/crates/ui-egui/tests/it/selection_glide.rs similarity index 100% rename from crates/ui-egui/tests/selection_glide.rs rename to crates/ui-egui/tests/it/selection_glide.rs diff --git a/crates/ui-egui/tests/selection_scroll.rs b/crates/ui-egui/tests/it/selection_scroll.rs similarity index 100% rename from crates/ui-egui/tests/selection_scroll.rs rename to crates/ui-egui/tests/it/selection_scroll.rs diff --git a/crates/ui-egui/tests/sheet_navigator.rs b/crates/ui-egui/tests/it/sheet_navigator.rs similarity index 100% rename from crates/ui-egui/tests/sheet_navigator.rs rename to crates/ui-egui/tests/it/sheet_navigator.rs diff --git a/crates/ui-egui/tests/sheet_tab_rename.rs b/crates/ui-egui/tests/it/sheet_tab_rename.rs similarity index 100% rename from crates/ui-egui/tests/sheet_tab_rename.rs rename to crates/ui-egui/tests/it/sheet_tab_rename.rs diff --git a/crates/ui-egui/tests/sheet_tabs.rs b/crates/ui-egui/tests/it/sheet_tabs.rs similarity index 100% rename from crates/ui-egui/tests/sheet_tabs.rs rename to crates/ui-egui/tests/it/sheet_tabs.rs diff --git a/crates/ui-egui/tests/show_objects.rs b/crates/ui-egui/tests/it/show_objects.rs similarity index 100% rename from crates/ui-egui/tests/show_objects.rs rename to crates/ui-egui/tests/it/show_objects.rs diff --git a/crates/ui-egui/tests/shrink_to_fit.rs b/crates/ui-egui/tests/it/shrink_to_fit.rs similarity index 100% rename from crates/ui-egui/tests/shrink_to_fit.rs rename to crates/ui-egui/tests/it/shrink_to_fit.rs diff --git a/crates/ui-egui/tests/sort_dialog_columns.rs b/crates/ui-egui/tests/it/sort_dialog_columns.rs similarity index 100% rename from crates/ui-egui/tests/sort_dialog_columns.rs rename to crates/ui-egui/tests/it/sort_dialog_columns.rs diff --git a/crates/ui-egui/tests/symbol_glyphs.rs b/crates/ui-egui/tests/it/symbol_glyphs.rs similarity index 100% rename from crates/ui-egui/tests/symbol_glyphs.rs rename to crates/ui-egui/tests/it/symbol_glyphs.rs diff --git a/crates/ui-egui/tests/system_theme.rs b/crates/ui-egui/tests/it/system_theme.rs similarity index 100% rename from crates/ui-egui/tests/system_theme.rs rename to crates/ui-egui/tests/it/system_theme.rs diff --git a/crates/ui-egui/tests/table_headings.rs b/crates/ui-egui/tests/it/table_headings.rs similarity index 100% rename from crates/ui-egui/tests/table_headings.rs rename to crates/ui-egui/tests/it/table_headings.rs diff --git a/crates/ui-egui/tests/text_box_editing.rs b/crates/ui-egui/tests/it/text_box_editing.rs similarity index 100% rename from crates/ui-egui/tests/text_box_editing.rs rename to crates/ui-egui/tests/it/text_box_editing.rs diff --git a/crates/ui-egui/tests/text_overflow_scroll.rs b/crates/ui-egui/tests/it/text_overflow_scroll.rs similarity index 100% rename from crates/ui-egui/tests/text_overflow_scroll.rs rename to crates/ui-egui/tests/it/text_overflow_scroll.rs diff --git a/crates/ui-egui/tests/touch_pan.rs b/crates/ui-egui/tests/it/touch_pan.rs similarity index 100% rename from crates/ui-egui/tests/touch_pan.rs rename to crates/ui-egui/tests/it/touch_pan.rs diff --git a/crates/ui-egui/tests/trace_navigation.rs b/crates/ui-egui/tests/it/trace_navigation.rs similarity index 100% rename from crates/ui-egui/tests/trace_navigation.rs rename to crates/ui-egui/tests/it/trace_navigation.rs diff --git a/crates/ui-egui/tests/translated_form_fields.rs b/crates/ui-egui/tests/it/translated_form_fields.rs similarity index 100% rename from crates/ui-egui/tests/translated_form_fields.rs rename to crates/ui-egui/tests/it/translated_form_fields.rs diff --git a/crates/ui-egui/tests/ui_clock.rs b/crates/ui-egui/tests/it/ui_clock.rs similarity index 100% rename from crates/ui-egui/tests/ui_clock.rs rename to crates/ui-egui/tests/it/ui_clock.rs diff --git a/crates/ui-egui/tests/validation_alert.rs b/crates/ui-egui/tests/it/validation_alert.rs similarity index 100% rename from crates/ui-egui/tests/validation_alert.rs rename to crates/ui-egui/tests/it/validation_alert.rs diff --git a/crates/ui-egui/tests/validation_input_message.rs b/crates/ui-egui/tests/it/validation_input_message.rs similarity index 100% rename from crates/ui-egui/tests/validation_input_message.rs rename to crates/ui-egui/tests/it/validation_input_message.rs diff --git a/crates/ui-egui/tests/wheel_zoom.rs b/crates/ui-egui/tests/it/wheel_zoom.rs similarity index 100% rename from crates/ui-egui/tests/wheel_zoom.rs rename to crates/ui-egui/tests/it/wheel_zoom.rs diff --git a/crates/ui-egui/tests/whole_line_shortcuts.rs b/crates/ui-egui/tests/it/whole_line_shortcuts.rs similarity index 100% rename from crates/ui-egui/tests/whole_line_shortcuts.rs rename to crates/ui-egui/tests/it/whole_line_shortcuts.rs diff --git a/crates/ui-egui/tests/widgets.rs b/crates/ui-egui/tests/it/widgets.rs similarity index 100% rename from crates/ui-egui/tests/widgets.rs rename to crates/ui-egui/tests/it/widgets.rs diff --git a/crates/ui-egui/tests/zoom_dialog.rs b/crates/ui-egui/tests/it/zoom_dialog.rs similarity index 100% rename from crates/ui-egui/tests/zoom_dialog.rs rename to crates/ui-egui/tests/it/zoom_dialog.rs diff --git a/docs/roadmap.md b/docs/roadmap.md index 4894d77b11..62e1735f77 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,6 +1,6 @@ # Roadmap detail -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-11 · **Change:** minor (PivotTable calculated fields) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-23 · **Change:** minor (test command) · **Target:** Microsoft Excel (Microsoft 365) Forward-looking plan. The one-page summary is [`ROADMAP.md`](../ROADMAP.md); the ranked work list is [`gaps.md`](gaps.md); numbers and method are in @@ -100,7 +100,8 @@ to check our files, and Windows machines with the reported GPU drivers. - `cargo xtask parity` regenerates [`parity-checklist.md`](parity-checklist.md) (ribbon catalog vs registered commands); `cargo xtask ci` checks it is current. -- `cargo test --workspace`: engine, formula, function, XLSX and UI behaviour. +- `cargo xtask test`: engine, formula, function, XLSX and UI behaviour (cargo-nextest when + installed, else `cargo test`), then the doctests. - `cargo run --release -p gridcraft-ui-egui --example snapshot -- --sample sales out.png`: look at the UI offscreen. - Function coverage: the script method in [function-parity.md](function-parity.md). A @@ -112,6 +113,7 @@ to check our files, and Windows machines with the reported GPU drivers. | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | Measuring: tests run through `cargo xtask test` | | 2026-10-11 | minor | PivotTable calculated fields landed | | 2026-10-10 | minor | Current focus and M15 updated for the 10-10 merges | | 2026-10-10 | minor | Added the alpha gate table (six core workflows; passes, stays alpha) | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 08a9124bd5..99a1e0717a 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -341,7 +341,7 @@ The inventory carried over from the 2026-10-07 ROADMAP.md, updated for what land | 2026-10-12 | minor | Essentials AutoFit evidence: rows auto-fit oversized fonts on open and row formatting (#833) | | 2026-10-12 | minor | Sheet tabs: reveal the selected tab on navigation and resize while preserving manual scrolling (#681); no score change | | 2026-10-12 | minor | PivotTables: values on rows (Σ Values in Rows at any position, all three layouts; `pivot.moveField` and the field pane move it; `dataOnRows` round-trips) (no score change) | -| 2026-10-11 | minor | Stability: command-sequence fuzz harness (`crates/engine/tests/command_sequence_fuzz.rs`) checks random command sequences against undo/redo, XLSX round-trip, full-recalc and structure invariants; the sequence bugs it found are fixed (no score change) | +| 2026-10-11 | minor | Stability: command-sequence fuzz harness (`crates/engine/tests/it/command_sequence_fuzz.rs`) checks random command sequences against undo/redo, XLSX round-trip, full-recalc and structure invariants; the sequence bugs it found are fixed (no score change) | | 2026-10-11 | minor | Password-encrypted workbooks open (#11): Essentials file-opening work 24–36 h | | 2026-10-11 | minor | Worksheet functions 505 / 523 (96.6%; 496 working): ODDFPRICE, ODDFYIELD, ODDLPRICE, ODDLYIELD | | 2026-10-11 | minor | Worksheet functions 503 / 523 (96.2%; 494 working): PERCENTOF, EUROCONVERT | diff --git a/xtask/src/keytips.rs b/xtask/src/keytips.rs index 63b1554a03..f6df981b7d 100644 --- a/xtask/src/keytips.rs +++ b/xtask/src/keytips.rs @@ -1,6 +1,6 @@ //! Regenerate the keytip sequence table and its per-sequence test suite from `keytips.json`. //! -//! `cargo xtask keytips` regenerate the table + `tests/keytips_generated.rs` +//! `cargo xtask keytips` regenerate the table + `tests/it/keytips_generated.rs` //! `cargo xtask keytips --check` fail if either generated file differs from the JSON source //! //! Requires the default `engine` feature: the generator probes the live command @@ -92,7 +92,7 @@ pub fn run(root: &Path, rest: &[&str]) -> Result<(), String> { { let json_path = root.join("crates/ui-egui/src/keytips/keytips.json"); let table_path = root.join("crates/ui-egui/src/keytips/keytip_table/generated.rs"); - let tests_path = root.join("crates/ui-egui/tests/keytips_generated.rs"); + let tests_path = root.join("crates/ui-egui/tests/it/keytips_generated.rs"); let source = fs::read_to_string(&json_path).map_err(|e| format!("read {}: {e}", json_path.display()))?; let (entries, probe) = parse_and_probe(&source)?; let generated = diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 14e9af32a6..0c9f5547db 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -15,17 +15,19 @@ mod stats; mod version; use std::path::{Path, PathBuf}; -use std::process::{Command, ExitCode}; +use std::process::{Command, ExitCode, Stdio}; const USAGE: &str = "\ usage: cargo xtask commands: - ci fmt --check, clippy -D warnings, test, assets, layers, wasm, parity --check - (stops at the first failure; the parity check runs only if docs/parity-checklist.md exists) + ci fmt --check, clippy -D warnings, test, doctest, assets, layers, wasm, parity --check, + keytips --check (stops at the first failure; the parity check runs only if + docs/parity-checklist.md exists) + test workspace tests (cargo nextest when installed, else cargo test), then doctests assets check that every icon/image/font/sample file is attributed in ATTRIBUTION.md layers enforce the crate dependency layering (AGENTS.md \"Layering\") - wasm cargo check --target wasm32-unknown-unknown for the L0-L5 crates (+ gridcraft-web) + wasm cargo check --target wasm32-unknown-unknown for the L0-L5 crates (+ gridcraft-web), one call parity [--check] [--out PATH] write docs/parity-checklist.md from gridcraft_engine::catalog (implemented vs missing ids) keytips [--check] @@ -44,6 +46,7 @@ fn main() -> ExitCode { Some("assets") => assets::run(&root()), Some("layers") => cmd_layers(), Some("wasm") => cmd_wasm(), + Some("test") => cmd_test().and_then(|()| cmd_doctest()), Some("ci") => cmd_ci(), Some("stats") => stats::run(&root(), rest.contains(&"--exact")), Some("parity") => parity::run(&root(), &rest), @@ -139,17 +142,19 @@ fn wasm_set() -> Result, String> { .collect()) } +/// One `cargo check` for the whole set; `--keep-going` reports every failing crate, not just the +/// first. fn cmd_wasm() -> Result<(), String> { let set = wasm_set()?; if set.is_empty() { return Err("no wasm crates found in the workspace".into()); } let mut c = cargo(); - c.args(["check", "--target", "wasm32-unknown-unknown"]); + c.args(["check", "--target", "wasm32-unknown-unknown", "--keep-going"]); for p in &set { c.args(["-p", p]); } - let r = run(c, &format!("cargo check --target wasm32-unknown-unknown -p {}", set.join(" -p "))); + let r = run(c, &format!("cargo check --target wasm32-unknown-unknown --keep-going -p {}", set.join(" -p "))); println!("\nwasm32-unknown-unknown check ({}): {}", set.join(", "), if r.is_ok() { "ok" } else { "FAIL" }); if r.is_err() { println!("(if the target is missing: rustup target add wasm32-unknown-unknown)"); @@ -157,6 +162,35 @@ fn cmd_wasm() -> Result<(), String> { r } +/// Targets for the test build. Examples are left out so they aren't linked; clippy +/// `--all-targets` still compiles them. +const TEST_TARGETS: [&str; 4] = ["--workspace", "--lib", "--bins", "--tests"]; + +fn nextest_available() -> bool { + cargo().args(["nextest", "--version"]).stdout(Stdio::null()).stderr(Stdio::null()).status().is_ok_and(|s| s.success()) +} + +fn cmd_test() -> Result<(), String> { + let mut c = cargo(); + let tool = if nextest_available() { + c.args(["nextest", "run"]); + "nextest run" + } else { + eprintln!("note: cargo-nextest is not installed, running cargo test (cargo install cargo-nextest --locked)"); + c.arg("test"); + "test" + }; + c.args(TEST_TARGETS); + run(c, &format!("cargo {tool} {}", TEST_TARGETS.join(" "))) +} + +/// nextest does not run doctests. +fn cmd_doctest() -> Result<(), String> { + let mut c = cargo(); + c.args(["test", "--workspace", "--doc"]); + run(c, "cargo test --workspace --doc") +} + fn cmd_ci() -> Result<(), String> { type Step = (&'static str, Box Result<(), String>>); let steps: Vec = vec![ @@ -172,18 +206,12 @@ fn cmd_ci() -> Result<(), String> { "clippy", Box::new(|| { let mut c = cargo(); - c.args(["clippy", "--workspace", "--all-targets", "--", "-D", "warnings"]); - run(c, "cargo clippy --workspace --all-targets -- -D warnings") - }), - ), - ( - "test", - Box::new(|| { - let mut c = cargo(); - c.args(["test", "--workspace"]); - run(c, "cargo test --workspace") + c.args(["clippy", "--workspace", "--all-targets", "--keep-going", "--", "-D", "warnings"]); + run(c, "cargo clippy --workspace --all-targets --keep-going -- -D warnings") }), ), + ("test", Box::new(cmd_test)), + ("doctest", Box::new(cmd_doctest)), ("assets", Box::new(|| assets::run(&root()))), ("layers", Box::new(cmd_layers)), ("wasm", Box::new(cmd_wasm)), From 33a45130a1786bd9dce1aa2f8b431339a56ed8ee Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:02:16 -0400 Subject: [PATCH 110/687] Packaging: pin the signing client and appimagetool, and check their hashes (#2073) * Packaging: pin the Windows signing client and check its hash * Packaging: pin appimagetool and check its hash * Packaging: pin the AppImage runtime and stop clearly without a pinned tool * Packaging: shellcheck-clean empty hashes --- packaging/linux/package.sh | 66 +++++++++++++++++++++++++++++++------- packaging/windows/sign.ps1 | 13 ++++++-- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/packaging/linux/package.sh b/packaging/linux/package.sh index f8c53d6b30..4de3aaf801 100755 --- a/packaging/linux/package.sh +++ b/packaging/linux/package.sh @@ -9,8 +9,9 @@ # # Usage: packaging/linux/package.sh [--skip-build] [--formats "appimage deb rpm tar"] # -# Needs: cargo; nfpm for deb/rpm (https://nfpm.goreleaser.com); appimagetool for the AppImage -# (downloaded into $CARGO_TARGET_DIR if missing). Build on an old distro (CI: Ubuntu 22.04, +# Needs: cargo; nfpm for deb/rpm (https://nfpm.goreleaser.com); appimagetool and the AppImage +# runtime for the AppImage (pinned downloads into $CARGO_TARGET_DIR, or APPIMAGETOOL and +# APPIMAGE_RUNTIME, e.g. on riscv64, which has no pinned ones). Build on an old distro (CI: Ubuntu 22.04, # glibc 2.35) so the binaries run on newer ones. Optional: desktop-file-validate, appstreamcli, # zsyncmake (the zsync package) for the AppImage's .zsync. set -euo pipefail @@ -25,7 +26,7 @@ while [[ $# -gt 0 ]]; do case "$1" in --skip-build) SKIP_BUILD=1; shift ;; --formats) FORMATS="$2"; shift 2 ;; - -h | --help) sed -n '2,17p' "$0"; exit 0 ;; + -h | --help) sed -n '2,18p' "$0"; exit 0 ;; *) echo "unknown argument: $1" >&2; exit 2 ;; esac done @@ -102,6 +103,32 @@ fi # ---- AppImage ----------------------------------------------------------------------------------- if has appimage; then + APPIMAGETOOL_VERSION=1.9.1 + # appimagetool would otherwise embed type2-runtime's unchecked `continuous` build. + APPIMAGE_RUNTIME_VERSION=20251108 + case "$ARCH" in + x86_64) + APPIMAGETOOL_SHA256=ed4ce84f0d9caff66f50bcca6ff6f35aae54ce8135408b3fa33abfc3cb384eb0 + APPIMAGE_RUNTIME_SHA256=2fca8b443c92510f1483a883f60061ad09b46b978b2631c807cd873a47ec260d + ;; + aarch64) + APPIMAGETOOL_SHA256=f0837e7448a0c1e4e650a93bb3e85802546e60654ef287576f46c71c126a9158 + APPIMAGE_RUNTIME_SHA256=00cbdfcf917cc6c0ff6d3347d59e0ca1f7f45a6df1a428a0d6d8a78664d87444 + ;; + *) APPIMAGETOOL_SHA256='' APPIMAGE_RUNTIME_SHA256='' ;; + esac + # Downloads a pinned file into the cache once and checks its hash on every run. + fetch_pinned() { + local path="$1" url="$2" sha256="$3" + if [[ ! -x "$path" ]]; then + curl --proto '=https' -fsSL -o "$path.download" "$url" + printf '%s %s\n' "$sha256" "$path.download" | sha256sum -c - + chmod 755 "$path.download" + mv "$path.download" "$path" + fi + printf '%s %s\n' "$sha256" "$path" | sha256sum -c - + return + } APPDIR="$WORK/GridCraft.AppDir" cp -R "$STAGE" "$APPDIR" mv "$APPDIR/usr/share/doc" "$WORK/doc-unused" @@ -111,18 +138,33 @@ if has appimage; then ln -s "$APP_ID.png" "$APPDIR/.DirIcon" TOOL="${APPIMAGETOOL:-$(command -v appimagetool || true)}" - if [[ -z "$TOOL" ]]; then - TOOL="$CARGO_TARGET_DIR/appimagetool-$ARCH.AppImage" - if [[ ! -x "$TOOL" ]]; then - curl --proto '=https' -fsSL -o "$TOOL" "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-$ARCH.AppImage" - chmod +x "$TOOL" - fi + if [[ -z "$TOOL" ]] && [[ -z "$APPIMAGETOOL_SHA256" ]]; then + echo "no pinned appimagetool for $ARCH; set APPIMAGETOOL or put appimagetool on PATH" >&2 + exit 2 fi - # Absolute, because appimagetool runs in $DIST below (CARGO_TARGET_DIR or APPIMAGETOOL may be - # relative, e.g. target/agent-). + RUNTIME="${APPIMAGE_RUNTIME:-}" + if [[ -z "$RUNTIME" ]] && [[ -z "$APPIMAGE_RUNTIME_SHA256" ]]; then + echo "no pinned AppImage runtime for $ARCH; set APPIMAGE_RUNTIME" >&2 + exit 2 + fi + if [[ -z "$TOOL" ]]; then + TOOL="$CARGO_TARGET_DIR/appimagetool-$APPIMAGETOOL_VERSION-$ARCH.AppImage" + fetch_pinned "$TOOL" \ + "https://github.com/AppImage/appimagetool/releases/download/$APPIMAGETOOL_VERSION/appimagetool-$ARCH.AppImage" \ + "$APPIMAGETOOL_SHA256" + fi + if [[ -z "$RUNTIME" ]]; then + RUNTIME="$CARGO_TARGET_DIR/appimage-runtime-$APPIMAGE_RUNTIME_VERSION-$ARCH" + fetch_pinned "$RUNTIME" \ + "https://github.com/AppImage/type2-runtime/releases/download/$APPIMAGE_RUNTIME_VERSION/runtime-$ARCH" \ + "$APPIMAGE_RUNTIME_SHA256" + fi + # Absolute, because appimagetool runs in $DIST below (CARGO_TARGET_DIR, APPIMAGETOOL or + # APPIMAGE_RUNTIME may be relative, e.g. target/agent-). OUT="$(cd "$DIST" && pwd)/$BASENAME.AppImage" APPDIR="$(cd "$APPDIR" && pwd)" TOOL="$(cd "$(dirname "$TOOL")" && pwd)/$(basename "$TOOL")" + RUNTIME="$(cd "$(dirname "$RUNTIME")" && pwd)/$(basename "$RUNTIME")" # A .zsync left by an earlier run would hide a missing zsyncmake and describe another file. rm -f "$OUT.zsync" # Update information: AppImageUpdate, AppImageLauncher and the like read it from the file and @@ -134,7 +176,7 @@ if has appimage; then # Extract-and-run: works without FUSE (containers, CI). The output embeds the static runtime, # so users don't need libfuse2 either. With zsyncmake on the host (CI installs the zsync # package) appimagetool also writes the .zsync, into its working directory, hence the cd. - (cd "$DIST" && ARCH="$ARCH" APPIMAGE_EXTRACT_AND_RUN=1 "$TOOL" --no-appstream -u "$UPDATE_INFO" "$APPDIR" "$OUT") + (cd "$DIST" && ARCH="$ARCH" APPIMAGE_EXTRACT_AND_RUN=1 "$TOOL" --no-appstream --runtime-file "$RUNTIME" -u "$UPDATE_INFO" "$APPDIR" "$OUT") echo "wrote $OUT" if [[ -s "$OUT.zsync" ]]; then echo "wrote $OUT.zsync" diff --git a/packaging/windows/sign.ps1 b/packaging/windows/sign.ps1 index a886a8de6d..222d5705b2 100644 --- a/packaging/windows/sign.ps1 +++ b/packaging/windows/sign.ps1 @@ -56,8 +56,8 @@ if (-not $haveCert -and -not $haveAzure) { $script:SignTool = Find-SignTool $common = @('sign', '/v', '/fd', 'SHA256', '/td', 'SHA256', '/d', 'GridCraft', '/du', 'https://github.com/storytold/gridcraft') -$tmp = Join-Path ([IO.Path]::GetTempPath()) "gridcraft-sign-$PID" -New-Item -ItemType Directory -Force -Path $tmp | Out-Null +$tmp = Join-Path ([IO.Path]::GetTempPath()) "gridcraft-sign-$([IO.Path]::GetRandomFileName())" +New-Item -ItemType Directory -Path $tmp | Out-Null try { if ($haveCert) { @@ -73,8 +73,15 @@ try { Write-Output "Signing with Azure Trusted Signing ($env:AZURE_SIGNING_ACCOUNT / $env:AZURE_CERT_PROFILE): $($Files -join ', ')" # The dlib authenticates with DefaultAzureCredential, which reads AZURE_TENANT_ID, # AZURE_CLIENT_ID and AZURE_CLIENT_SECRET from the environment. + $trustedSigningVersion = '1.0.95' + $trustedSigningSha256 = '3bfcf1e0a3cb42af1692f0a8ed45c15de070c2de86f28a59b2795d904d8a920f' $pkg = Join-Path $tmp 'trusted-signing.zip' - Invoke-WebRequest -Uri 'https://www.nuget.org/api/v2/package/Microsoft.Trusted.Signing.Client' -OutFile $pkg + $uri = "https://api.nuget.org/v3-flatcontainer/microsoft.trusted.signing.client/$trustedSigningVersion/microsoft.trusted.signing.client.$trustedSigningVersion.nupkg" + Invoke-WebRequest -Uri $uri -OutFile $pkg + $actualHash = (Get-FileHash -Path $pkg -Algorithm SHA256).Hash.ToLowerInvariant() + if ($actualHash -ne $trustedSigningSha256) { + throw "Microsoft.Trusted.Signing.Client $trustedSigningVersion checksum mismatch" + } Expand-Archive -Path $pkg -DestinationPath (Join-Path $tmp 'client') -Force $dlib = Get-ChildItem -Path (Join-Path $tmp 'client') -Recurse -Filter Azure.CodeSigning.Dlib.dll | Where-Object { $_.FullName -match '\\x64\\' } | Select-Object -First 1 From 39450b80d1eebfd78b4a0729fb4d57c2b1da832f Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:48:45 +0200 Subject: [PATCH 111/687] Hyperlinks: Ctrl+Alt+click on a link extends the selection again (#2368) --- crates/ui-egui/src/grid.rs | 3 ++- crates/ui-egui/tests/it/hyperlink_ctrl_click.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 40ef2c8e9f..75858ad4eb 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2397,9 +2397,10 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & d.selection.anchor = sel.anchor; } } else if mods.command { - // Ctrl+click on a hyperlink's text follows it instead of adding the cell to the selection. + // Ctrl+click (without Alt) on a hyperlink's text follows it instead of adding the cell to the selection. if let Some(h) = sh.hyperlinks.get(&c).cloned() && primary_clicked + && !mods.alt && p.x < geo.cell_rect(sh, c).left() + geo.cell_rect(sh, c).width() * 0.8 { follow_link(app, &h.target); diff --git a/crates/ui-egui/tests/it/hyperlink_ctrl_click.rs b/crates/ui-egui/tests/it/hyperlink_ctrl_click.rs index 2e557e12e1..a61350d3d1 100644 --- a/crates/ui-egui/tests/it/hyperlink_ctrl_click.rs +++ b/crates/ui-egui/tests/it/hyperlink_ctrl_click.rs @@ -83,3 +83,15 @@ fn ctrl_click_beside_the_link_text_still_extends_the_selection() { assert_eq!(ranges, ["B2", "B4"], "the hyperlink cell joins the selection"); assert!(h.state().dialog.is_none(), "no link was followed"); } + +#[test] +fn ctrl_alt_click_on_the_link_text_still_extends_the_selection() { + let mut h = harness(); + click_at(&mut h, "B2", 0.9, Modifiers::NONE); + click_at(&mut h, "B4", 0.3, Modifiers { alt: true, ..Modifiers::COMMAND }); + let d = h.state().session.active().unwrap(); + let ranges: Vec = d.selection.ranges.iter().map(|r| r.a1()).collect(); + assert_eq!(ranges, ["B2", "B4"], "Ctrl+Alt+click adds the hyperlink cell to the selection"); + assert_eq!(d.selection.active.a1(), "B4", "the link was not followed"); + assert!(h.state().dialog.is_none(), "no link dialog"); +} From 49f26bdd79f88ee12637a6728022557ac749f327 Mon Sep 17 00:00:00 2001 From: xc4pr <88853037+xc4pr@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:51:53 +0200 Subject: [PATCH 112/687] Fill handle: the Auto Fill Options button on a right-to-left sheet is checked in screen space (#2368) --- crates/ui-egui/src/grid.rs | 3 +- .../ui-egui/tests/it/fill_options_button.rs | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 75858ad4eb..ee9eb600b3 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -3396,7 +3396,8 @@ fn fill_options_tag(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet let size = vec2(18.0, 18.0); let corner = geo.range_rect(sh, tag.target).right_bottom(); let button = geo.mirror_rect(Rect::from_min_size(corner + vec2(2.0, 2.0), size)); - if !geo.cells.contains_rect(button) { + // `button` is in screen space, so compare it with the mirrored cells area. + if !geo.mirror_rect(geo.cells).contains_rect(button) { return; } let lang = crate::i18n::current(ui.ctx()); diff --git a/crates/ui-egui/tests/it/fill_options_button.rs b/crates/ui-egui/tests/it/fill_options_button.rs index 2cb209fa93..d735f846a4 100644 --- a/crates/ui-egui/tests/it/fill_options_button.rs +++ b/crates/ui-egui/tests/it/fill_options_button.rs @@ -209,3 +209,65 @@ fn the_button_stays_inside_the_grid_on_a_right_to_left_sheet() { let filled = geo.screen_cell_rect(sheet, CellRef::parse("B6").unwrap()); assert!(button.center().x < filled.left() && button.top() >= filled.bottom() - 1.0, "below-left of the mirrored range's corner"); } + +/// Right-to-left sheet with column A made `chars` wide (so column B's right edge, where the fill +/// range B1:B6 ends, sits far enough right to be scrolled to either edge of the cells area). +fn rtl_wide_a(chars: u32) -> Harness { + harness(|s| { + one_two(s); + s.execute("home.columnWidth", json!({"cols": "A:A", "chars": chars})).unwrap(); + s.execute("view.rightToLeft", json!({"on": true})).unwrap(); + }) +} + +/// Logical right edge of the filled column B (scroll-dependent) and the current geometry. +fn b_right(h: &Harness) -> (f32, Geo) { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + (geo.cell_rect(sheet, CellRef::parse("B6").unwrap()).right(), geo) +} + +fn scroll_b_right_to(h: &mut Harness, logical_x: f32) { + let (now, geo) = b_right(h); + h.state_mut().view_mut().unwrap().scroll.x += (now - logical_x) / geo.z; + h.run_steps(3); + let got = b_right(h).0; + assert!((got - logical_x).abs() < 1.0, "setup: column B ends at logical x {got}, wanted {logical_x}"); +} + +/// Logical x of the range's corner is `cells.right() - 25`: the 18 px button (at corner + 2 .. +/// corner + 20) fits in the cells area, so on screen it sits at the left edge of the mirrored cells +/// area, left of the filled range. `fill_options_tag` tested the mirrored button against the +/// unmirrored cells, i.e. against the row-heading strip it does not own, and hid it. +#[test] +fn the_button_shows_near_the_left_screen_edge_on_a_right_to_left_sheet() { + let mut h = rtl_wide_a(220); + let (_, geo) = b_right(&h); + scroll_b_right_to(&mut h, geo.cells.right() - 25.0); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + + let (_, geo) = b_right(&h); + let cells = h.state().grid.cells_rect.unwrap(); + assert!(geo.header_w > 20.0 && geo.cells.right() - 25.0 + 20.0 > geo.cells.right() - geo.header_w, "setup: in the zone the old check hides"); + let button = h.query_by_label("Auto Fill Options").expect("the button fits inside the cells area, so it shows").rect(); + assert!(cells.contains_rect(button), "{button:?} outside {cells:?}"); +} + +/// The corner is 10 px left of `cells.left()` (scrolled partly behind the row headings, which are +/// on the right on screen). The button would start 8 px behind the headings, so it must not be +/// drawn there. The old check let it through because the mirrored button fits the unmirrored cells. +#[test] +fn the_button_stays_off_the_row_headings_on_a_right_to_left_sheet() { + let mut h = rtl_wide_a(20); + drag_handle(&mut h, "B2", "B6", PointerButton::Primary); + assert!(h.query_by_label("Auto Fill Options").is_some(), "setup: the button shows before scrolling"); + let (_, geo) = b_right(&h); + scroll_b_right_to(&mut h, geo.cells.left() - 10.0); + + let cells = h.state().grid.cells_rect.unwrap(); + if let Some(b) = h.query_by_label("Auto Fill Options") { + let button = b.rect(); + assert!(cells.contains_rect(button), "{button:?} overlaps the row headings, cells area is {cells:?}"); + } +} From aa8399be9507ebc789063117c71d02efb5fb7251 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:14:15 -0400 Subject: [PATCH 113/687] Grid: #### is sized by the width of #, so it fits its cell in every font (#2378) A number too wide for its column is drawn as ####, with as many # as fit. The count used the width of 0, and in fonts where # is wider than 0 the hashes ran past the cell edge. accounting_fill::too_wide_fill_number_shows_hashes_not_a_clipped_value caught it on FreeBSD and on Linux machines without the CI font set. --- crates/ui-egui/src/grid.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 40ef2c8e9f..5902949041 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -1300,7 +1300,9 @@ pub(crate) fn paint_quadrant( // Numbers and dates never overflow: they show #### when too narrow. let hashed = !shrunk && (numeric || matches!(v, Value::Number(_))) && w > avail_w - 2.0 * pad && st.align.h != HAlign::Fill; if hashed { - let n = ((avail_w - 2.0 * pad) / char_w).floor().max(1.0) as usize; + // Measured on `#` itself: it is wider than `0` in some fonts, and #### must fit its cell. + let hash_w = p.layout_no_wrap("#".to_string(), font.clone(), color).size().x.max(1.0); + let n = ((avail_w - 2.0 * pad) / hash_w).floor().max(1.0) as usize; galley = p.layout_no_wrap("#".repeat(n), font.clone(), color); w = galley.size().x; } From 0c14f595550ca0970a10fa963bc35f3f3cf60e2f Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 20:21:27 +0200 Subject: [PATCH 114/687] PivotTables: Show Details, double-click a value cell to list its source records on a new sheet (#2364) --- crates/engine/src/cmd/pivot.rs | 207 +++++++++++++++++- crates/engine/src/pivot.rs | 32 +++ crates/ui-egui/src/grid.rs | 7 + crates/ui-egui/tests/it/main.rs | 1 + crates/ui-egui/tests/it/pivot_show_details.rs | 76 +++++++ docs/gaps.md | 6 +- 6 files changed, 317 insertions(+), 12 deletions(-) create mode 100644 crates/ui-egui/tests/it/pivot_show_details.rs diff --git a/crates/engine/src/cmd/pivot.rs b/crates/engine/src/cmd/pivot.rs index 43c3aa4b5e..80f8d0205d 100644 --- a/crates/engine/src/cmd/pivot.rs +++ b/crates/engine/src/cmd/pivot.rs @@ -179,6 +179,15 @@ pub fn specs() -> Vec { has_doc, change_source ), + cmd!( + "pivot.showDetails", + "Show Details", + [], + None, + "{cell?: \"B5\" (a value, subtotal or grand total cell of a PivotTable on the active sheet; default: the active cell)} puts the source records behind that value on a new sheet before the current one → {sheet, records}", + has_doc, + show_details + ), cmd!( "pivot.delete", "Delete PivotTable", @@ -654,16 +663,7 @@ fn insert_pivot(s: &mut Session, p: &Json) -> Result { let (si, anchor) = match target { Some(t) => t, None => { - let at = cx.wb.active_sheet.min(cx.wb.sheets.len()); - let nm = cx.wb.next_sheet_name(); - cx.wb.sheets.insert(at, Arc::new(Sheet::new(nm))); - for n in cx.wb.names.iter_mut() { - if let Some(sc) = n.scope.as_mut() - && *sc >= at - { - *sc += 1; - } - } + let at = insert_sheet_before_active(cx); new_sheet_at = Some(at); (at, CellRef::new(2, 0)) } @@ -726,6 +726,104 @@ fn insert_pivot(s: &mut Session, p: &Json) -> Result { Ok(res) } +/// Inserts a new, empty sheet before the active one (Excel's place for a new PivotTable or a +/// Show Details sheet); returns its index. +fn insert_sheet_before_active(cx: &mut Ctx) -> usize { + let at = cx.wb.active_sheet.min(cx.wb.sheets.len()); + let nm = cx.wb.next_sheet_name(); + cx.wb.sheets.insert(at, Arc::new(Sheet::new(nm))); + for n in cx.wb.names.iter_mut() { + if let Some(sc) = n.scope.as_mut() + && *sc >= at + { + *sc += 1; + } + } + at +} + +/// Show Details (double-click a value cell): the source records behind it, as values with their +/// column's number format, under the source headers on a new sheet. +fn show_details(s: &mut Session, p: &Json) -> Result { + let d = s.doc()?; + let at = cell_param(s, p, "cell")?.unwrap_or(d.selection.active); + let active = d.wb.active_sheet; + let sh = d.wb.active().ok_or(EngineError::NoDocument)?; + let pt = sh + .pivots + .iter() + .find(|pt| pt.last_range.is_some_and(|r| r.contains(at))) + .ok_or_else(|| EngineError::Disabled("pivot.showDetails".into(), "select a value cell in a PivotTable".into()))?; + let origin = pt.last_range.map(|r| r.start).unwrap_or_default(); + let found = pv::detail_records(&d.wb, pt, active, at.row.saturating_sub(origin.row), at.col.saturating_sub(origin.col)).map_err(other)?; + let (src, records) = found.ok_or_else(|| EngineError::Disabled("pivot.showDetails".into(), "select a value cell in a PivotTable".into()))?; + if d.wb.protected_structure { + return Err(other("The workbook is protected and cannot be changed.")); + } + let count = records.len(); + let mut new_sheet_at: Option = None; + let res = edit(s, |cx| { + let si = insert_sheet_before_active(cx); + new_sheet_at = Some(si); + let base = cx.wb.styles.get(StyleId::default()).clone(); + let fmts: Vec = src + .formats + .iter() + .map(|f| { + let mut st = base.clone(); + st.num_fmt = NumFmt::new(f); + cx.wb.styles.intern(st) + }) + .collect(); + // Columns wide enough for their text, as the PivotTable report's are (dates would show + // as ####); the first rows are enough to judge a column. + let font = base.font.clone(); + let widths: Vec = src + .headers + .iter() + .enumerate() + .map(|(c, h)| { + let fmt = src.formats.get(c).map(String::as_str).unwrap_or("General"); + let texts = records.iter().take(1_000).filter_map(|i| src.rows.get(*i).and_then(|r| r.get(c))); + let widest = texts + .map(|v| crate::cmd::format::approx_text_width(&crate::display::format(v, fmt, &cx.wb).text, font.size, false)) + .fold(crate::cmd::format::approx_text_width(h, font.size, false), f32::max); + (widest + 14.0).min(320.0) + }) + .collect(); + let sh = cx.sheet_mut(si)?; + for (c, w) in widths.into_iter().enumerate() { + if w > sh.col_width(c as u32) { + sh.cols.entry(c as u32).or_default().size = Some(w); + } + } + for (c, h) in src.headers.iter().enumerate() { + sh.set_cell(CellRef::new(0, c as u32), Cell { value: gridcraft_core::Value::text(h.as_str()), formula: None, style: StyleId::default() }); + } + for (r, i) in records.iter().enumerate() { + let Some(rec) = src.rows.get(*i) else { continue }; + for (c, v) in rec.iter().enumerate() { + let style = fmts.get(c).copied().unwrap_or_default(); + if v.is_empty() && style == StyleId::default() { + continue; + } + sh.set_cell(CellRef::new(r as u32 + 1, c as u32), Cell { value: v.clone(), formula: None, style }); + } + } + let name = sh.name.clone(); + cx.wb.active_sheet = si; + *cx.sel = crate::Selection::at(CellRef::new(0, 0)); + cx.structural = true; + Ok(json!({"sheet": name, "records": count})) + })?; + if let Some(at) = new_sheet_at + && let Ok(d) = s.doc_mut() + { + d.sheet_selections.insert(at.min(d.sheet_selections.len()), crate::Selection::default()); + } + Ok(res) +} + fn summarize_table(s: &mut Session, p: &Json) -> Result { let d = s.doc()?; let name = match str_param(p, "table") { @@ -2236,6 +2334,7 @@ mod tests { "data.refreshAll", "pivot.changeSource", "pivot.delete", + "pivot.showDetails", ]; let hostile = [ json!({}), @@ -2363,4 +2462,92 @@ mod tests { assert_eq!(l[0]["anchor"], "A3"); s.execute("pivot.refresh", json!({"pivot": "PivotTable1"})).unwrap(); } + + /// The active sheet's first five columns as shown, down to the first blank row. + fn sheet_rows(s: &mut Session) -> Vec> { + let mut out = Vec::new(); + for r in 0..30u32 { + let row: Vec = (0..5) + .map(|c| s.execute("cell.get", json!({"cell": CellRef::new(r, c).a1()})).unwrap()["text"].as_str().unwrap_or("").to_string()) + .collect(); + if row.iter().all(String::is_empty) { + break; + } + out.push(row); + } + out + } + + #[test] + fn show_details_lists_a_value_cells_records_on_a_new_sheet() { + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Region"], "columns": ["Product"], "values": ["Sales"]})).unwrap(); + // B5 is East × Apples (100 + 60); the active cell is the default. + s.execute("selection.set", json!({"range": "B5"})).unwrap(); + let r = s.execute("pivot.showDetails", json!({})).unwrap(); + assert_eq!(r["records"], 2); + let d = s.doc().unwrap(); + assert_eq!(d.wb.sheets.len(), 3); + assert_eq!(d.wb.active_sheet, 0, "the new sheet goes before the PivotTable's"); + assert_eq!(d.wb.active().unwrap().name, "Sheet3"); + assert_eq!( + sheet_rows(&mut s), + vec![ + vec!["Region", "Product", "Date", "Units", "Sales"], + vec!["East", "Apples", "1/15/2026", "10", "100"], + vec!["East", "Apples", "9/9/2026", "6", "60"], + ] + ); + // The dates fit their column instead of showing ####. + let sh = s.doc().unwrap().wb.active().unwrap().clone(); + assert!(sh.col_width(2) > sh.col_width(5), "{} vs {}", sh.col_width(2), sh.col_width(5)); + // One undo step takes the sheet away. + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 2); + assert_eq!(val(&s, "B5"), Value::Number(160.0)); + } + + #[test] + fn show_details_on_a_total_takes_every_record_it_adds_up() { + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Region"], "columns": ["Product"], "values": ["Sales"]})).unwrap(); + let records = |s: &mut Session, cell: &str| { + let n = s.execute("pivot.showDetails", json!({"cell": cell})).unwrap()["records"].as_u64().unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + n + }; + assert_eq!(records(&mut s, "E5"), 5, "East's row total"); + assert_eq!(records(&mut s, "C8"), 4, "Pears' column total"); + assert_eq!(records(&mut s, "E8"), 12, "the grand total"); + // A subtotal at the top of its group, in a two-level row axis. + s.execute("pivot.moveField", json!({"field": "Product", "from": "columns", "to": "rows"})).unwrap(); + assert_eq!(text(&s, "A4"), "East"); + assert_eq!(records(&mut s, "B4"), 5); + assert_eq!(text(&s, "A6"), "Pears"); + assert_eq!(records(&mut s, "B6"), 2); + } + + #[test] + fn show_details_leaves_out_the_records_the_report_filters_out() { + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Product"], "values": ["Sales"], "filters": ["Region"]})).unwrap(); + s.execute("pivot.filter", json!({"field": "Region", "selected": ["East"]})).unwrap(); + s.execute("pivot.filter", json!({"field": "Product", "selected": ["Apples"]})).unwrap(); + let gt = find_row(&s, "Grand Total").unwrap(); + let r = s.execute("pivot.showDetails", json!({"cell": CellRef::new(gt, 1).a1()})).unwrap(); + assert_eq!(r["records"], 2); + let rows = sheet_rows(&mut s); + assert_eq!(rows.len(), 3); + assert!(rows.iter().skip(1).all(|row| row[0] == "East" && row[1] == "Apples"), "{rows:?}"); + } + + #[test] + fn show_details_needs_a_value_cell() { + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Region"], "columns": ["Product"], "values": ["Sales"]})).unwrap(); + for cell in ["A3", "B3", "A4", "B4", "A5", "A8", "H20"] { + assert!(s.execute("pivot.showDetails", json!({"cell": cell})).is_err(), "{cell}"); + } + assert_eq!(s.doc().unwrap().wb.sheets.len(), 2); + } } diff --git a/crates/engine/src/pivot.rs b/crates/engine/src/pivot.rs index 08ec15dfb9..43a77c11ad 100644 --- a/crates/engine/src/pivot.rs +++ b/crates/engine/src/pivot.rs @@ -803,6 +803,30 @@ fn data_cols_exceed_sheet(col_groups: usize, grand_total: bool, values: usize) - /// Computes a PivotTable report from the workbook. pub fn compute(wb: &Workbook, pt: &PivotTable, pivot_sheet: usize) -> Result { let src = read_source(wb, &pt.source, pivot_sheet)?; + compute_from(wb, pt, &src, None) +} + +/// Show Details: the source list and the indices of its records behind the value cell at +/// (`row`, `col`) of the report grid (relative to its top-left), in source order. `None` when +/// that cell holds no value (a label, a header, a blank). +pub fn detail_records(wb: &Workbook, pt: &PivotTable, pivot_sheet: usize, row: u32, col: u32) -> Result)>, String> { + let src = read_source(wb, &pt.source, pivot_sheet)?; + let mut probe = Probe { at: (row as usize, col as usize), hit: None, paths: Vec::new() }; + compute_from(wb, pt, &src, Some(&mut probe))?; + let Some((rp, cp)) = probe.hit else { return Ok(None) }; + let records = probe.paths.into_iter().filter(|(_, r, c)| r.starts_with(&rp) && c.starts_with(&cp)).map(|(i, _, _)| i).collect(); + Ok(Some((src, records))) +} + +/// What [`detail_records`] asks of a report computation: the row and column item paths of the +/// value cell at `at`, and the paths of every record the filters let through. +struct Probe { + at: (usize, usize), + hit: Option<(Vec, Vec)>, + paths: Vec<(usize, Vec, Vec)>, +} + +fn compute_from(wb: &Workbook, pt: &PivotTable, src: &Source, mut probe: Option<&mut Probe>) -> Result { let fmt_of = |c: usize| src.formats.get(c).cloned().unwrap_or_else(|| "General".into()); let rows: Vec<(usize, &PivotField)> = pt.rows.iter().filter_map(|f| src.col(&f.source_col).map(|c| (c, f))).collect(); let cols: Vec<(usize, &PivotField)> = pt.columns.iter().filter_map(|f| src.col(&f.source_col).map(|c| (c, f))).collect(); @@ -914,6 +938,9 @@ pub fn compute(wb: &Workbook, pt: &PivotTable, pivot_sheet: usize) -> Result Result raw, }, }; + if let Some(pr) = probe.as_deref_mut() + && pr.at == (r, l + j) + { + pr.hit = Some((rp.to_vec(), cp.to_vec())); + } g.set(r, l + j, shown, ck.1, 0, data_fmt.get(*vi).cloned().flatten()); } } diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 40ef2c8e9f..1b72576e55 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2386,6 +2386,13 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & && !mods.shift { app.grid.last_click = None; + // A PivotTable's value cell shows its source records on a new sheet (Excel's Show + // Details); its labels and headers still edit. + if sh.pivots.iter().any(|pt| pt.last_range.is_some_and(|r| r.contains(c))) + && app.run("pivot.showDetails", json!({"cell": c.a1()})).is_ok() + { + return; + } // Following a hyperlink requires a single click in Excel; double-click edits. app.begin_edit(None, false); return; diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index b89253bb96..9b04d3869e 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -59,6 +59,7 @@ mod page_layout_view; mod page_setup_margins; mod paste_special_dialog; mod pivot_pane; +mod pivot_show_details; mod popup_dismissal; mod protect_password; mod right_to_left; diff --git a/crates/ui-egui/tests/it/pivot_show_details.rs b/crates/ui-egui/tests/it/pivot_show_details.rs new file mode 100644 index 0000000000..e78ca53ecc --- /dev/null +++ b/crates/ui-egui/tests/it/pivot_show_details.rs @@ -0,0 +1,76 @@ +//! Double-clicking a PivotTable's value cell shows its source records on a new sheet (Excel's +//! Show Details); a label of the report still opens the cell for editing. + +use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; +use gridcraft_engine::Session; +use gridcraft_engine::core::CellRef; +use gridcraft_ui_egui::{SheetApp, grid::Geo}; +use serde_json::json; + +fn harness() -> egui_kittest::Harness<'static, SheetApp> { + let mut session = Session::new(); + session.new_workbook(); + session.execute("range.setValues", json!({"range": "A1", "values": [["Region", "Sales"], ["East", 100], ["West", 80], ["East", 30]]})).unwrap(); + // Report at D3: D4 East, E4 its Sum of Sales. + session + .execute("insert.pivotTable", json!({"source": "Sheet1!A1:B4", "destination": "Sheet1!D3", "rows": ["Region"], "values": ["Sales"]})) + .unwrap(); + session.execute("selection.set", json!({"cell": "A1"})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_step_dt(1.0 / 60.0).with_size(vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + SheetApp::new(session, Default::default()), + ); + h.run_steps(4); + h +} + +fn center(h: &egui_kittest::Harness<'_, SheetApp>, cell: &str) -> Pos2 { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + geo.cell_rect(sheet, CellRef::parse(cell).unwrap()).center() +} + +fn double_click(h: &mut egui_kittest::Harness<'_, SheetApp>, pos: Pos2) { + for _ in 0..2 { + h.input_mut().events.extend([ + Event::PointerMoved(pos), + Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }, + Event::PointerButton { pos, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }, + ]); + h.run_steps(2); + } +} + +#[test] +fn double_click_on_a_value_cell_shows_its_records_on_a_new_sheet() { + let mut h = harness(); + let pos = center(&h, "E4"); + double_click(&mut h, pos); + + let app = h.state(); + assert!(app.editor.is_none(), "the value cell must not open for editing"); + let wb = &app.session.active().unwrap().wb; + assert_eq!(wb.sheets.len(), 2); + let sheet = wb.active().unwrap(); + assert_eq!(sheet.name, "Sheet2"); + let text = |a: &str| sheet.value(CellRef::parse(a).unwrap()).display(); + assert_eq!( + [text("A1"), text("B1"), text("A2"), text("B2"), text("A3"), text("B3"), text("A4")], + ["Region", "Sales", "East", "100", "East", "30", ""] + ); +} + +#[test] +fn double_click_on_a_row_label_still_edits() { + let mut h = harness(); + let pos = center(&h, "D4"); + double_click(&mut h, pos); + + let app = h.state(); + assert!(app.editor.is_some()); + assert_eq!(app.session.active().unwrap().wb.sheets.len(), 1); +} diff --git a/docs/gaps.md b/docs/gaps.md index 7e2e40dc06..b077279912 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-20 · **Change:** minor (the Japanese catalog covers the dialogs, messages and menus) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-23 · **Change:** minor (§8: PivotTable Show Details) · **Target:** Microsoft Excel (Microsoft 365) Every known shortfall against Excel, one entry each, ranked by user impact (what blocks someone using GridCraft for real work first). This is the work list: pick from the top, check the open @@ -84,7 +84,8 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- ### 8. PivotTables: no slicers, calculated items, PivotCharts; unverified in Excel (F) - **Evidence:** `parity-checklist.md` missing `insert.slicer`, `insert.timeline`, `insert.pivotChart`, `table.insertSlicer`; calculated items and GETPIVOTDATA missing (calculated fields - and values on rows done). + and values on rows done). Show Details (double-click a value, subtotal or grand total cell) lists + its source records on a new sheet; the "Enable show details" option isn't modelled yet. - **Impact:** pivots are the analyst's main tool. - **Estimate:** 20–30 h for beta (25–40 h full). @@ -178,6 +179,7 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | §8: PivotTable Show Details: double-clicking a value cell lists its source records on a new sheet (#2364) | | 2026-10-20 | minor | §11: the Japanese catalog covers the dialogs, messages and menus — 943 keys (~43%), matching the pt-BR coverage; a test keeps the two catalogs in step | | 2026-10-17 | minor | §6: the five reports it tracked are closed and their PRs merged (#57, #133, #139, #140, #134; PRs #148, #155, #156, #157); the entry stays open for §15's oracle suite | | 2026-10-15 | minor | §13: Solver prepares reports before committing the solution, model and report sheets together; report refusal preserves the workbook and Undo/Redo (#1050) | From 08820cb4b27c274258bd55576caa621ac31ffbb2 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:32:15 -0400 Subject: [PATCH 115/687] CI: run the gate through craft-common-actions' shared gate.yml; FreeBSD nightly and on release (#2338) ci.yml calls the shared workflow (cached apt packages, nextest, main-only cache saves, Cargo.lock check). FreeBSD runs nightly and on pushes to release instead of on pull requests. Depends on storytold/craft-common-actions#69. --- .github/workflows/ci.yml | 44 +++++++++-------------------------- .github/workflows/freebsd.yml | 5 ++++ 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77cde0cec9..52068cea1d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,8 +1,7 @@ -# The AGENTS.md quality gate (`cargo xtask ci`: fmt, clippy -D warnings, tests, assets, layers, -# wasm, parity --check) on every pull request and every push to main, so a merge burst can't land -# a red tree that only the next agent session discovers. name: CI +# Quality gates for every push to main and every pull request (`cargo xtask ci`), through the +# suite's shared gate workflow in craft-common-actions. on: push: branches: [main] @@ -12,37 +11,16 @@ on: permissions: contents: read +# A newer push to a PR branch cancels its older run; every push to main runs to the end. concurrency: - group: ci-${{ github.ref }} - cancel-in-progress: true + group: ci-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: gate: - name: cargo xtask ci - runs-on: ubuntu-24.04 - timeout-minutes: 60 - env: - CARGO_TERM_COLOR: always - CARGO_INCREMENTAL: "0" - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - - name: Install Linux build dependencies (winit, wgpu, rfd) - run: | - sudo apt-get update - sudo apt-get install -y --no-install-recommends \ - pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev \ - libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev mesa-vulkan-drivers - - - name: Install Rust (stable + clippy, rustfmt, wasm32) - run: | - rustup toolchain install stable --profile minimal --component clippy,rustfmt \ - --target wasm32-unknown-unknown - rustup default stable - - - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 - with: - save-if: ${{ github.ref == 'refs/heads/main' }} - - - name: Quality gate - run: cargo xtask ci + # Tracks @main so suite-wide CI fixes reach every app without a PR in each repository. + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + packages: >- + pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev libxi-dev + libgtk-3-dev libfontconfig1-dev libfreetype-dev mesa-vulkan-drivers diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml index 6cdde6e47b..0544913220 100644 --- a/.github/workflows/freebsd.yml +++ b/.github/workflows/freebsd.yml @@ -2,6 +2,9 @@ # (GitHub has no FreeBSD runners). GPU tests skip themselves when the VM has no adapter. name: FreeBSD +# The VM build is slow, so it runs nightly on main and on pushes to `release`, not on every pull +# request: a drifted dependency still shows up within a day, and always before a release ships. +# PRs that change the FreeBSD workflow or packaging itself still run it. on: push: branches: [release] @@ -10,6 +13,8 @@ on: - .github/workflows/freebsd.yml - packaging/freebsd/** - packaging/env.sh + schedule: + - cron: "57 2 * * *" workflow_dispatch: concurrency: From 7111812b8793dbf0cd04aeed8c8e2da02722d691 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 20:41:47 +0200 Subject: [PATCH 116/687] PivotTables: Show Details refuses an out-of-date report, and reports its errors instead of editing the cell --- crates/engine/src/cmd/pivot.rs | 20 +++++++++++++++-- crates/engine/src/pivot.rs | 22 +++++++++++++++++-- crates/ui-egui/src/grid.rs | 15 +++++++++---- crates/ui-egui/tests/it/pivot_show_details.rs | 14 ++++++++++++ 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/pivot.rs b/crates/engine/src/cmd/pivot.rs index 80f8d0205d..3b6ed83a40 100644 --- a/crates/engine/src/cmd/pivot.rs +++ b/crates/engine/src/cmd/pivot.rs @@ -753,10 +753,10 @@ fn show_details(s: &mut Session, p: &Json) -> Result { .pivots .iter() .find(|pt| pt.last_range.is_some_and(|r| r.contains(at))) - .ok_or_else(|| EngineError::Disabled("pivot.showDetails".into(), "select a value cell in a PivotTable".into()))?; + .ok_or_else(|| EngineError::Disabled("pivot.showDetails".into(), pv::NOT_A_VALUE_CELL.into()))?; let origin = pt.last_range.map(|r| r.start).unwrap_or_default(); let found = pv::detail_records(&d.wb, pt, active, at.row.saturating_sub(origin.row), at.col.saturating_sub(origin.col)).map_err(other)?; - let (src, records) = found.ok_or_else(|| EngineError::Disabled("pivot.showDetails".into(), "select a value cell in a PivotTable".into()))?; + let (src, records) = found.ok_or_else(|| EngineError::Disabled("pivot.showDetails".into(), pv::NOT_A_VALUE_CELL.into()))?; if d.wb.protected_structure { return Err(other("The workbook is protected and cannot be changed.")); } @@ -2541,6 +2541,22 @@ mod tests { assert!(rows.iter().skip(1).all(|row| row[0] == "East" && row[1] == "Apples"), "{rows:?}"); } + #[test] + fn show_details_refuses_a_report_its_source_has_moved_on_from() { + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Region"], "columns": ["Product"], "values": ["Sales"]})).unwrap(); + // B5 still shows East × Apples (160), but the first East record is now Central's. + s.execute("range.setValues", json!({"range": "Sheet1!A2", "values": [["Central"]]})).unwrap(); + assert_eq!(val(&s, "B5"), Value::Number(160.0)); + let e = s.execute("pivot.showDetails", json!({"cell": "B5"})).unwrap_err().to_string(); + assert!(e.contains("out of date"), "{e}"); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 2); + // After a refresh East × Apples (now B6) has the one record left. + s.execute("pivot.refresh", json!({})).unwrap(); + assert_eq!(text(&s, "A6"), "East"); + assert_eq!(s.execute("pivot.showDetails", json!({"cell": "B6"})).unwrap()["records"], 1); + } + #[test] fn show_details_needs_a_value_cell() { let mut s = sales(); diff --git a/crates/engine/src/pivot.rs b/crates/engine/src/pivot.rs index 43a77c11ad..45c4eb0a5e 100644 --- a/crates/engine/src/pivot.rs +++ b/crates/engine/src/pivot.rs @@ -806,14 +806,32 @@ pub fn compute(wb: &Workbook, pt: &PivotTable, pivot_sheet: usize) -> Result Result)>, String> { let src = read_source(wb, &pt.source, pivot_sheet)?; let mut probe = Probe { at: (row as usize, col as usize), hit: None, paths: Vec::new() }; - compute_from(wb, pt, &src, Some(&mut probe))?; + let out = compute_from(wb, pt, &src, Some(&mut probe))?; let Some((rp, cp)) = probe.hit else { return Ok(None) }; + // Excel reads its PivotTable cache; without one, the report written by the last refresh + // must still be what the source gives. + let sh = wb.sheet(pivot_sheet).ok_or("There's no such sheet.")?; + let current = pt.last_range.is_some_and(|r| { + r.height() == out.height().max(1) + && r.width() == out.width.max(1) + && out.grid.iter().enumerate().all(|(ri, line)| { + line.iter().enumerate().all(|(ci, oc)| sh.value(CellRef::new(r.start.row + ri as u32, r.start.col + ci as u32)) == oc.value) + }) + }); + if !current { + return Err("The PivotTable report is out of date. Refresh it first.".into()); + } let records = probe.paths.into_iter().filter(|(_, r, c)| r.starts_with(&rp) && c.starts_with(&cp)).map(|(i, _, _)| i).collect(); Ok(Some((src, records))) } diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 1b72576e55..9fbdfc9117 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2388,10 +2388,17 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & app.grid.last_click = None; // A PivotTable's value cell shows its source records on a new sheet (Excel's Show // Details); its labels and headers still edit. - if sh.pivots.iter().any(|pt| pt.last_range.is_some_and(|r| r.contains(c))) - && app.run("pivot.showDetails", json!({"cell": c.a1()})).is_ok() - { - return; + if sh.pivots.iter().any(|pt| pt.last_range.is_some_and(|r| r.contains(c))) { + let not_a_value_cell = + gridcraft_engine::EngineError::Disabled("pivot.showDetails".into(), gridcraft_engine::pivot::NOT_A_VALUE_CELL.into()).to_string(); + match app.run("pivot.showDetails", json!({"cell": c.a1()})) { + Ok(_) => return, + Err(e) if e == not_a_value_cell => {} + Err(e) => { + app.message = Some(("GridCraft".into(), crate::clean_error(&e))); + return; + } + } } // Following a hyperlink requires a single click in Excel; double-click edits. app.begin_edit(None, false); diff --git a/crates/ui-egui/tests/it/pivot_show_details.rs b/crates/ui-egui/tests/it/pivot_show_details.rs index e78ca53ecc..629774b042 100644 --- a/crates/ui-egui/tests/it/pivot_show_details.rs +++ b/crates/ui-egui/tests/it/pivot_show_details.rs @@ -74,3 +74,17 @@ fn double_click_on_a_row_label_still_edits() { assert!(app.editor.is_some()); assert_eq!(app.session.active().unwrap().wb.sheets.len(), 1); } + +#[test] +fn double_click_on_a_value_cell_of_a_protected_workbook_says_why_and_does_not_edit() { + let mut h = harness(); + h.state_mut().session.execute("review.protectWorkbook", json!({"on": true})).unwrap(); + h.run_steps(2); + let pos = center(&h, "E4"); + double_click(&mut h, pos); + + let app = h.state(); + assert!(app.editor.is_none(), "the value cell must not open for editing"); + assert!(app.message.as_ref().is_some_and(|(_, m)| m.contains("protected")), "{:?}", app.message); + assert_eq!(app.session.active().unwrap().wb.sheets.len(), 1); +} From 530a8cc810ea82daf318575a560d99f5c48ac3e0 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Fri, 23 Oct 2026 14:47:33 -0400 Subject: [PATCH 117/687] CI: the gate builds with the release craft-fonts pin (#2381) --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 52068cea1d..c704eb4d8a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,8 @@ jobs: # Tracks @main so suite-wide CI fixes reach every app without a PR in each repository. uses: storytold/craft-common-actions/.github/workflows/gate.yml@main with: + # The same craft-fonts commit as release.yml and freebsd.yml, so the gate tests the fonts that ship. + craft-fonts-ref: abb83316d96aa59c1cf64784289e378fe9fa5695 packages: >- pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcursor-dev libxrandr-dev libxi-dev libgtk-3-dev libfontconfig1-dev libfreetype-dev mesa-vulkan-drivers From 43a5a3c435f1c8829c974a8180800ba4bb4685d5 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 21:00:07 +0200 Subject: [PATCH 118/687] PivotTables: Show Details checks the report is up to date before looking at the cell --- crates/engine/src/cmd/pivot.rs | 14 ++++++++++++++ crates/engine/src/pivot.rs | 13 ++++++++----- crates/ui-egui/tests/it/pivot_show_details.rs | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/pivot.rs b/crates/engine/src/cmd/pivot.rs index 3b6ed83a40..229ff22b65 100644 --- a/crates/engine/src/cmd/pivot.rs +++ b/crates/engine/src/cmd/pivot.rs @@ -2557,6 +2557,20 @@ mod tests { assert_eq!(s.execute("pivot.showDetails", json!({"cell": "B6"})).unwrap()["records"], 1); } + #[test] + fn show_details_refuses_a_report_that_would_now_be_shorter() { + let mut s = sales(); + s.execute("insert.pivotTable", json!({"rows": ["Region"], "columns": ["Product"], "values": ["Sales"]})).unwrap(); + // North goes away: the source now gives one row fewer, so row 8 is no longer the Grand Total. + s.execute("range.setValues", json!({"range": "Sheet1!A5", "values": [["East"]]})).unwrap(); + s.execute("range.setValues", json!({"range": "Sheet1!A8", "values": [["East"]]})).unwrap(); + s.execute("range.setValues", json!({"range": "Sheet1!A11", "values": [["East"]]})).unwrap(); + assert_eq!(text(&s, "A8"), "Grand Total"); + let e = s.execute("pivot.showDetails", json!({"cell": "E8"})).unwrap_err().to_string(); + assert!(e.contains("out of date"), "{e}"); + assert_eq!(s.doc().unwrap().wb.sheets.len(), 2); + } + #[test] fn show_details_needs_a_value_cell() { let mut s = sales(); diff --git a/crates/engine/src/pivot.rs b/crates/engine/src/pivot.rs index 45c4eb0a5e..397dd5ad2d 100644 --- a/crates/engine/src/pivot.rs +++ b/crates/engine/src/pivot.rs @@ -812,26 +812,29 @@ pub const NOT_A_VALUE_CELL: &str = "select a value cell in a PivotTable"; /// Show Details: the source list and the indices of its records behind the value cell at /// (`row`, `col`) of the report grid (relative to its top-left), in source order. `None` when -/// that cell holds no value (a label, a header, a blank). An error when the report on the sheet -/// is no longer what the source computes: the cell's records would be another cell's. +/// that cell holds no value (a label, a header, a blank). An error, for any cell, when the report +/// on the sheet is no longer what the source computes: the cell's records would be another cell's. pub fn detail_records(wb: &Workbook, pt: &PivotTable, pivot_sheet: usize, row: u32, col: u32) -> Result)>, String> { let src = read_source(wb, &pt.source, pivot_sheet)?; let mut probe = Probe { at: (row as usize, col as usize), hit: None, paths: Vec::new() }; let out = compute_from(wb, pt, &src, Some(&mut probe))?; - let Some((rp, cp)) = probe.hit else { return Ok(None) }; // Excel reads its PivotTable cache; without one, the report written by the last refresh - // must still be what the source gives. + // must still be what the source gives. Checked first: in a stale report even a cell that + // computes as a label may show a value. let sh = wb.sheet(pivot_sheet).ok_or("There's no such sheet.")?; let current = pt.last_range.is_some_and(|r| { r.height() == out.height().max(1) && r.width() == out.width.max(1) && out.grid.iter().enumerate().all(|(ri, line)| { - line.iter().enumerate().all(|(ci, oc)| sh.value(CellRef::new(r.start.row + ri as u32, r.start.col + ci as u32)) == oc.value) + line.iter().enumerate().all(|(ci, oc)| { + sh.value_ref(CellRef::new(r.start.row + ri as u32, r.start.col + ci as u32)).unwrap_or(&Value::Empty) == &oc.value + }) }) }); if !current { return Err("The PivotTable report is out of date. Refresh it first.".into()); } + let Some((rp, cp)) = probe.hit else { return Ok(None) }; let records = probe.paths.into_iter().filter(|(_, r, c)| r.starts_with(&rp) && c.starts_with(&cp)).map(|(i, _, _)| i).collect(); Ok(Some((src, records))) } diff --git a/crates/ui-egui/tests/it/pivot_show_details.rs b/crates/ui-egui/tests/it/pivot_show_details.rs index 629774b042..9f11b29ebf 100644 --- a/crates/ui-egui/tests/it/pivot_show_details.rs +++ b/crates/ui-egui/tests/it/pivot_show_details.rs @@ -88,3 +88,19 @@ fn double_click_on_a_value_cell_of_a_protected_workbook_says_why_and_does_not_ed assert!(app.message.as_ref().is_some_and(|(_, m)| m.contains("protected")), "{:?}", app.message); assert_eq!(app.session.active().unwrap().wb.sheets.len(), 1); } + +#[test] +fn double_click_in_an_out_of_date_report_asks_for_a_refresh() { + let mut h = harness(); + // West goes away without a refresh: E6, the Grand Total on the sheet, has no cell in the + // report the source now gives. + h.state_mut().session.execute("range.setValues", json!({"range": "Sheet1!A3", "values": [["East"]]})).unwrap(); + h.run_steps(2); + let pos = center(&h, "E6"); + double_click(&mut h, pos); + + let app = h.state(); + assert!(app.editor.is_none(), "the cell must not open for editing"); + assert!(app.message.as_ref().is_some_and(|(_, m)| m.contains("out of date")), "{:?}", app.message); + assert_eq!(app.session.active().unwrap().wb.sheets.len(), 1); +} From 15a705e23457d502f053e2b7b9fac1876830d403 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:39:59 -0400 Subject: [PATCH 119/687] i18n: missing translations are reported, never a failure (#2383) A string a catalog lacks shows in English. Coverage, key-parity and per-language count tests and tests of one language's exact wording are removed; UI wiring tests take their expected text from the catalog. cargo xtask i18n-report lists the missing strings per language and always passes; cargo xtask ci runs it first. --- crates/ui-egui/src/backstage.rs | 5 +- crates/ui-egui/src/formula_locale.rs | 7 - crates/ui-egui/src/i18n.rs | 210 ++--------------- crates/ui-egui/src/i18n/ja.rs | 132 +---------- crates/ui-egui/tests/it/file_menu.rs | 24 +- crates/ui-egui/tests/it/formula_locale.rs | 5 - crates/ui-egui/tests/it/interaction.rs | 50 ++-- .../tests/it/translated_form_fields.rs | 17 +- docs/localization-parity.md | 8 +- xtask/Cargo.toml | 2 +- xtask/src/i18n_report.rs | 218 ++++++++++++++++++ xtask/src/main.rs | 11 +- 12 files changed, 297 insertions(+), 392 deletions(-) create mode 100644 xtask/src/i18n_report.rs diff --git a/crates/ui-egui/src/backstage.rs b/crates/ui-egui/src/backstage.rs index c2acf025bf..f5bdc20ab9 100644 --- a/crates/ui-egui/src/backstage.rs +++ b/crates/ui-egui/src/backstage.rs @@ -1084,11 +1084,8 @@ mod tests { } assert!(age(Language::En, f64::MAX).ends_with(" years ago")); // Other languages put the count where their own words want it. - assert_eq!(age(Language::Ja, 3.0 * DAY), "3 日前"); - assert_eq!(age(Language::De, 3.0 * DAY), "vor 3 Tagen"); - assert_eq!(age(Language::Fr, 90.0), "il y a 1 min"); - assert_eq!(age(Language::It, 3.0 * DAY), "3 giorni fa"); for lang in Language::ALL { + assert!(age(lang, 3.0 * DAY).contains('3'), "{lang:?}: {}", age(lang, 3.0 * DAY)); for seconds in [10.0, 600.0, 7200.0, 1.5 * DAY, 5.0 * DAY, 30.0 * DAY, 200.0 * DAY, 2000.0 * DAY] { let words = age(lang, seconds); assert!(!words.is_empty() && !words.contains('{'), "{lang:?} {seconds}: {words}"); diff --git a/crates/ui-egui/src/formula_locale.rs b/crates/ui-egui/src/formula_locale.rs index 6991679349..d53c5fa341 100644 --- a/crates/ui-egui/src/formula_locale.rs +++ b/crates/ui-egui/src/formula_locale.rs @@ -105,13 +105,6 @@ fn split_signature(signature: &str) -> Option<(&str, Vec<&str>)> { Some((head, if inner.is_empty() { Vec::new() } else { inner.split(", ").collect() })) } -/// The catalog names of a signature's arguments (`number` for `[number2]`), without `...`; the -/// function help tables are checked against them. -#[cfg(test)] -pub(crate) fn argument_names(signature: &str) -> Vec<&str> { - split_signature(signature).map(|(_, args)| args.into_iter().map(argument_name).filter(|name| *name != "...").collect()).unwrap_or_default() -} - /// `[number2]` -> `number`. A bracket can open or close on its own (SWITCH's /// `[default_or_value2, result2]`, LAMBDA's `[parameter1, ...]`). fn argument_name(arg: &str) -> &str { diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 4156c2cd3b..345037b52c 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -321,21 +321,14 @@ fn name_sort_key(name: &str) -> String { mod tests { use super::*; - /// Help tables are sorted, unique, and keyed by text the function catalog still has. + /// Help tables are sorted and unique: lookups binary-search them. #[test] - fn function_help_is_sorted_and_keyed_by_current_catalog_text() { - // Lookups binary-search the tables. A key that no longer matches the catalog (an English - // description reworded, an argument renamed) would quietly show English again. - let catalog = gridcraft_engine::cmd::formulas::function_list(); - let descriptions: std::collections::HashSet<&str> = catalog.iter().filter_map(|f| f["description"].as_str()).collect(); - let arguments: std::collections::HashSet<&str> = - catalog.iter().filter_map(|f| f["signature"].as_str()).flat_map(crate::formula_locale::argument_names).collect(); + fn function_help_tables_are_sorted_and_unique() { for language in Language::ALL { let Some((description_table, argument_table)) = language.function_help() else { continue }; - for (table, catalog_text) in [(description_table, &descriptions), (argument_table, &arguments)] { + for table in [description_table, argument_table] { assert!(table.windows(2).all(|pair| pair[0].0 < pair[1].0), "{language:?} function help is not sorted and unique"); for (english, translated) in table { - assert!(catalog_text.contains(english), "{language:?} translates {english:?}, which the function catalog doesn't use"); assert!(!translated.is_empty(), "{language:?}: empty translation of {english:?}"); } } @@ -343,105 +336,27 @@ mod tests { } #[test] - fn translations_are_unique_and_preserve_unknown_text() { - for (i, (en, ja)) in ja::CORE.iter().enumerate() { - assert!(!ja.is_empty()); - assert!(ja::CORE.iter().take(i).all(|(other, _)| en != other)); - assert_eq!(Language::En.tr(en), en.rsplit_once('|').map_or(*en, |(_, label)| label)); - } + fn untranslated_text_falls_back_to_english() { + let label = |key: &'static str| key.rsplit_once('|').map_or(key, |(_, label)| label); for (english, _) in ja::CORE { - assert!(zh::translate(english).is_some_and(|text| !text.is_empty()), "missing Simplified Chinese: {english}"); - assert!(ko::translate(english).is_some_and(|text| !text.is_empty()), "missing Korean: {english}"); - assert!(ru::translate(english).is_some_and(|text| !text.is_empty()), "missing Russian: {english}"); - assert!(pt::translate(english).is_some_and(|text| !text.is_empty()), "missing Portuguese: {english}"); - assert!(es::translate(english).is_some_and(|text| !text.is_empty()), "missing Spanish: {english}"); - assert!(el::translate(english).is_some_and(|text| !text.is_empty()), "missing Greek: {english}"); - assert!(de::translate(english).is_some_and(|text| !text.is_empty()), "missing German: {english}"); - assert!(cs::translate(english).is_some_and(|text| !text.is_empty()), "missing Czech: {english}"); - assert!(fr::translate(english).is_some_and(|text| !text.is_empty()), "missing French: {english}"); - assert!(sr::translate(english).is_some_and(|text| !text.is_empty()), "missing Serbian: {english}"); - assert!(tr::translate(english).is_some_and(|text| !text.is_empty()), "missing Turkish: {english}"); - assert!(pl::translate(english).is_some_and(|text| !text.is_empty()), "missing Polish: {english}"); - assert!(it::translate(english).is_some_and(|text| !text.is_empty()), "missing Italian: {english}"); - assert!(uk::translate(english).is_some_and(|text| !text.is_empty()), "missing Ukrainian: {english}"); - assert!(vi::translate(english).is_some_and(|text| !text.is_empty()), "missing Vietnamese: {english}"); - assert!(nl::translate(english).is_some_and(|text| !text.is_empty()), "missing Dutch: {english}"); - assert!(ro::translate(english).is_some_and(|text| !text.is_empty()), "missing Romanian: {english}"); + assert_eq!(Language::En.tr(english), label(english)); } - assert_eq!(Language::Ja.tr("Data"), "データ"); - assert_eq!(Language::Zh.tr("Data"), "数据"); - assert_eq!(Language::Ko.tr("Data"), "데이터"); - assert_eq!(Language::Ru.tr("Data"), "Данные"); - assert_eq!(Language::PtBr.tr("Data"), "Dados"); - assert_eq!(Language::PtBr.tr_error("Select a chart first."), "Selecione um gráfico primeiro."); - assert_eq!(Language::PtBr.tr_error("Sheet|Unknown failure"), "Sheet|Unknown failure"); - assert_eq!(Language::PtBr.tr_error("command `chart`: Select a chart first."), "Selecione um gráfico primeiro."); - assert_eq!(Language::PtBr.tr_error("Printer Office: permission denied"), "Printer Office: permission denied"); - assert_eq!(Language::Es.tr("Data"), "Datos"); - assert_eq!(Language::El.tr("Data"), "Δεδομένα"); - assert_eq!(Language::El.tr("Page Layout|Orientation"), "Προσανατολισμός σελίδας"); - assert_eq!(Language::El.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::El.tr("Page {page} of {pages}"), "Σελίδα {page} από {pages}"); - assert_eq!(Language::Es.tr("Freeze Panes"), "Inmovilizar paneles"); - assert_eq!(Language::Es.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::De.tr("Data"), "Daten"); - assert_eq!(Language::De.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Fr.tr("Data"), "Données"); - assert_eq!(Language::Fr.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Tr.tr("Data"), "Veri"); - assert_eq!(Language::Tr.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::PtBr.tr("Date"), "Data"); - assert_eq!(Language::PtBr.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Ja.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Zh.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Ko.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Ru.tr("Sheet1!A1"), "Sheet1!A1"); - // Czech "Data" is spelled as in English, so probe it with a label that differs. - assert_eq!(Language::Cs.tr("Data"), "Data"); - assert_eq!(Language::Cs.tr("Formulas"), "Vzorce"); - assert_eq!(Language::Cs.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Tr.tr("Formulas"), "Formüller"); - // A `context|label` key shows only its label in English and splits the translation. + for language in Language::ALL { + assert_eq!(language.tr("Sheet1!A1"), "Sheet1!A1"); + assert_eq!(language.tr("Not in any catalog|Untranslated"), "Untranslated"); + } + // A `context|label` key shows only its label in English. assert_eq!(Language::En.tr("Page Layout|Orientation"), "Orientation"); - assert_eq!(Language::Ja.tr("Page Layout|Orientation"), "印刷の向き"); - assert_eq!(Language::Ja.tr("Orientation"), "方向"); - assert_eq!(Language::Cs.tr("Page Layout|Orientation"), "Orientace"); - assert_eq!(Language::Cs.tr("Orientation"), "Orientace textu"); - assert_eq!(Language::Sr.tr("Data"), "Podaci"); - assert_eq!(Language::Sr.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Sr.tr("Page Layout|Orientation"), "Orijentacija"); - assert_eq!(Language::Sr.tr("Orientation"), "Orijentacija"); - assert_eq!(Language::Tr.tr("Page Layout|Orientation"), "Sayfa Yönü"); - assert_eq!(Language::Tr.tr("Orientation"), "Yönlendirme"); - assert_eq!(Language::Pl.tr("Data"), "Dane"); - assert_eq!(Language::Pl.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Pl.tr("Page Layout|Orientation"), "Orientacja"); - assert_eq!(Language::It.tr("Data"), "Dati"); - assert_eq!(Language::It.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::It.tr("Freeze Panes"), "Blocca riquadri"); - assert_eq!(Language::It.tr("Page Layout|Orientation"), "Orientamento"); - assert_eq!(Language::It.tr("Orientation"), "Orientamento"); - assert_eq!(Language::Uk.tr("Data"), "Дані"); - assert_eq!(Language::Uk.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Vi.tr("Data"), "Dữ liệu"); - assert_eq!(Language::Vi.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Vi.tr("Page Layout|Orientation"), "Hướng giấy"); - assert_eq!(Language::Vi.tr("Orientation"), "Hướng chữ"); - assert_eq!(Language::Nl.tr("Data"), "Gegevens"); - assert_eq!(Language::Nl.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Nl.tr("Page Layout|Orientation"), "Paginastand"); - assert_eq!(Language::Nl.tr("Orientation"), "Richting"); - assert_eq!(Language::Ro.tr("Data"), "Date"); - assert_eq!(Language::Ro.tr("Sheet1!A1"), "Sheet1!A1"); - assert_eq!(Language::Ro.tr("Page Layout|Orientation"), "Orientare"); - assert_eq!(Language::Ro.tr("Orientation"), "Orientare text"); + // Errors drop the `command `id`:` prefix before translating; an untranslated one stays whole. + assert_eq!(Language::PtBr.tr_error("command `chart`: Select a chart first."), Language::PtBr.tr("Select a chart first.")); + assert_eq!(Language::PtBr.tr_error("Sheet|Unknown failure"), "Sheet|Unknown failure"); + assert_eq!(Language::PtBr.tr_error("Printer Office: permission denied"), "Printer Office: permission denied"); assert_eq!(Language::parse("xx"), None); } /// A catalog whose translations slip onto their neighbours' keys still covers every key, so /// check the shape of each translation against its English: placeholders survive, and a - /// one-line label (a menu item, a tooltip) doesn't turn into a two-line ribbon label. French - /// once shipped with over a hundred translations shifted this way ("Close" read "Société"). + /// one-line label (a menu item, a tooltip) doesn't turn into a two-line ribbon label. #[test] fn translations_keep_the_shape_of_their_english() { let placeholders = @@ -457,106 +372,19 @@ mod tests { } } } - for (english, french) in [ - ("Close", "Fermer"), - ("File", "Fichier"), - ("Save", "Enregistrer"), - ("Animations", "Animations"), - ("Company", "Société"), - ("Show Detail", "Afficher le détail"), - ("Solver", "Solveur"), - ] { - assert_eq!(Language::Fr.tr(english), french); - } - } - - #[test] - fn german_cell_and_header_context_menus_are_fully_translated() { - for label in [ - "Cut", - "Copy", - "Paste", - "Paste Special…", - "Insert…", - "Delete…", - "Clear Contents", - "Filter by Selected Cell's Value", - "Sort A to Z", - "Sort Z to A", - "New Comment", - "New Note", - "Format Cells…", - "Pick From Drop-down List…", - "Define Name…", - "Insert Copied Cells", - "Insert Copied Cells…", - "Insert Cut Cells", - "Insert", - "Delete", - "Row Height…", - "AutoFit Row Height", - "Column Width…", - "AutoFit Column Width", - "Hide", - "Unhide", - "Group", - ] { - assert_ne!(Language::De.tr(label), label, "German context menu label missing: {label}"); - } - } - - #[test] - fn sheet_tab_menu_and_autosave_labels_are_translated() { - let labels = [ - "Insert Sheet", - "Delete", - "Rename", - "Move or Copy…", - "Duplicate", - "Tab Color", - "No Color", - "Hide", - "Unhide…", - "Protect Sheet…", - "Unprotect Sheet…", - "Select All Sheets", - "AutoSave", - "AutoSave: save after every change (workbooks saved to a file)", - "Previous sheet", - "Next sheet", - "All sheets", - "New sheet (⇧F11)", - ]; - for language in [Language::Es, Language::De, Language::Fr, Language::Ko, Language::Ru, Language::Zh, Language::Sr] { - for label in labels { - assert_ne!(language.tr(label), label, "{} lacks a translation of {label:?}", language.code()); - } - } - assert_eq!(Language::Es.tr("AutoSave"), "Autoguardado"); } #[test] fn translations_never_show_the_context_prefix() { // `tr` returns a found translation as it is, so a catalog that keeps `context|` in its // value shows it on screen (File › Options read "Options|Général" in French). - for language in Language::ALL { - for (english, _) in ja::CORE { + let keys = ja::CORE.iter().map(|(english, _)| *english).chain(["Options|General", "Options|Save", "Sheet Options|View"]); + for english in keys { + for language in Language::ALL { let shown = language.tr(english); assert!(!shown.contains('|'), "{} shows a context prefix for {english}: {shown}", language.code()); } } - assert_eq!(Language::Fr.tr("Options|General"), "Général"); - assert_eq!(Language::Fr.tr("Options|Save"), "Enregistrer"); - assert_eq!(Language::Fr.tr("Sheet Options|View"), "Affichage"); - assert_eq!(Language::Sr.tr("Options|General"), "Opšte"); - assert_eq!(Language::Sr.tr("Options|Save"), "Sačuvaj"); - assert_eq!(Language::Sr.tr("Sheet Options|View"), "Prikaz"); - assert_eq!(Language::Nl.tr("Options|General"), "Algemeen"); - assert_eq!(Language::Nl.tr("Options|Save"), "Opslaan"); - assert_eq!(Language::Nl.tr("Sheet Options|View"), "Weergeven"); - assert_eq!(Language::Ro.tr("Options|General"), "General"); - assert_eq!(Language::Ro.tr("Options|Save"), "Salvare"); - assert_eq!(Language::Ro.tr("Sheet Options|View"), "Vizualizare"); } #[test] diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 482852d8d7..2df4cfc11a 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -1,12 +1,12 @@ //! Japanese translations for the interface strings in `i18n.rs`; labels follow Excel for Mac's //! Japanese wording, and anything absent falls back to English. //! -//! [`CORE`] is the shared reference catalog every language translates: the ribbon, sheet tabs, -//! widgets and the most common dialog labels. [`DIALOGS`] adds the dialog titles and labels, -//! message boxes, Backstage/File-view items, template names and native-menu strings the Brazilian -//! Portuguese catalog (`pt.rs`) covers on top of it. The two lists never overlap. +//! [`CORE`] is the shared reference set: the ribbon, sheet tabs, widgets and the most common dialog +//! labels. [`DIALOGS`] adds the dialog titles and labels, message boxes, Backstage/File-view items, +//! template names and native-menu strings. The two lists never overlap. A string a language doesn't +//! translate shows in English; `cargo xtask i18n-report` lists them per language. -/// English label → Japanese, the shared reference set every catalog covers. +/// English label → Japanese, the shared reference set. pub(super) const CORE: &[(&str, &str)] = &[ ("Paste", "貼り付け"), ("Paste Values", "値の貼り付け"), @@ -1150,99 +1150,17 @@ mod tests { DIALOGS.iter().chain(CORE).map(|(english, _)| *english) } - /// The decoded value of the Rust string literal starting at byte `start` (its opening `"`), and - /// the byte index just past its closing quote. Escapes (`\n`, `\"`, `\\`, `\t`, `\u{…}`) are - /// decoded, so the keys compare equal to the ones in the tables above. - fn string_literal(source: &str, start: usize) -> Option<(String, usize)> { - if !source[start..].starts_with('"') { - return None; - } - let mut value = String::new(); - let mut chars = source[start + 1..].char_indices(); - while let Some((offset, c)) = chars.next() { - match c { - '"' => return Some((value, start + 1 + offset + 1)), - '\\' => match chars.next().map(|(_, c)| c) { - Some('n') => value.push('\n'), - Some('t') => value.push('\t'), - Some('r') => value.push('\r'), - Some('0') => value.push('\0'), - Some('\\') => value.push('\\'), - Some('"') => value.push('"'), - Some('\'') => value.push('\''), - Some('u') => { - let mut hex = String::new(); - for (_, c) in chars.by_ref() { - if c == '}' { - break; - } - if c != '{' { - hex.push(c); - } - } - if let Some(c) = u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32) { - value.push(c); - } - } - // A line continuation (backslash, newline, indentation) adds nothing. - _ => {} - }, - _ => value.push(c), - } - } - None - } - - /// Every key `pt.rs` translates, read out of its source with a small scanner: a match arm is a - /// string literal whose `=>` is followed by another string literal, which covers the block form - /// (`"key" => { "value" }`) as well. Parsing the real source keeps the two catalogs from - /// drifting apart again instead of checking a remembered count. - fn portuguese_keys() -> Vec { - let source = include_str!("pt.rs"); - let mut literals: Vec<(String, usize, usize)> = Vec::new(); - let mut i = 0; - while i < source.len() { - let rest = &source[i..]; - if rest.starts_with("//") { - i += rest.find('\n').unwrap_or(rest.len()); - continue; - } - if rest.starts_with('"') { - let (value, end) = string_literal(source, i).unwrap(); - literals.push((value, i, end)); - i = end; - continue; - } - i += rest.chars().next().unwrap().len_utf8(); - } - let mut keys = Vec::new(); - for pair in literals.windows(2) { - let (key, _, end) = &pair[0]; - let start = pair[1].1; - let gap = source[*end..start].trim(); - if gap == "=>" || gap == "=> {" { - keys.push(key.clone()); - } - } - keys - } - #[test] - fn keys_are_unique_and_every_value_is_translated() { + fn keys_are_unique_and_no_value_is_blank() { let mut seen = HashSet::new(); for (english, japanese) in DIALOGS.iter().chain(CORE) { - assert!(!japanese.is_empty(), "empty translation for {english:?}"); assert!(japanese.chars().any(|c| !c.is_whitespace()), "blank translation for {english:?}"); assert!(seen.insert(*english), "duplicate key across CORE and DIALOGS: {english:?}"); } - assert_eq!(seen.len(), catalog_keys().count()); - assert_eq!(CORE.len(), 392, "the shared reference set every catalog covers"); - assert_eq!(DIALOGS.len(), 562, "the dialog, message and menu strings beyond it"); - assert_eq!(catalog_keys().count(), 954); } #[test] - fn extra_labels_are_translated_and_do_not_overlap_the_catalog() { + fn extra_labels_do_not_overlap_the_catalog() { let catalog: HashSet<&str> = catalog_keys().collect(); let mut seen = HashSet::new(); for (english, japanese) in EXTRA { @@ -1252,40 +1170,4 @@ mod tests { assert_eq!(translate(english), Some(*japanese)); } } - - #[test] - fn covers_every_key_the_portuguese_catalog_translates() { - let portuguese = portuguese_keys(); - assert_eq!(portuguese.len(), 954, "pt.rs's arms changed: add the new keys here and update the count"); - let japanese: HashSet<&str> = catalog_keys().collect(); - for key in &portuguese { - assert!(japanese.contains(key.as_str()), "the Portuguese catalog translates {key:?}, the Japanese one does not"); - } - assert_eq!(japanese.len(), portuguese.len(), "the two catalogs hold the same key set"); - } - - #[test] - fn high_value_strings_use_excel_japanese_wording() { - for (english, japanese) in [ - ("OK", "OK"), - ("Cancel", "キャンセル"), - ("Don't Save", "保存しない"), - ("Text Box", "テキスト ボックス"), - ("Row Height", "行の高さ"), - ("Column Width", "列の幅"), - ("Sort", "並べ替え"), - ("Filter", "フィルター"), - ("Conditional Formatting", "条件付き書式"), - ("Do you want to save the changes you made to {title}?", "{title} への変更を保存しますか?"), - ("Save a copy as .{format}", ".{format} としてコピーを保存"), - ("Format Cells|Fill", "塗りつぶし"), - ("Insert\nSlicer", "スライサーの\n挿入"), - ] { - assert_eq!(translate(english), Some(japanese), "{english:?}"); - assert_eq!(crate::i18n::Language::Ja.tr(english), japanese, "{english:?}"); - } - // A `context|label` key shows only its label in English; an unknown label keeps the text. - assert_eq!(crate::i18n::Language::En.tr("Format Cells|Fill"), "Fill"); - assert_eq!(crate::i18n::Language::Ja.tr("Not in any catalog"), "Not in any catalog"); - } } diff --git a/crates/ui-egui/tests/it/file_menu.rs b/crates/ui-egui/tests/it/file_menu.rs index 047efe1b1e..092876b8da 100644 --- a/crates/ui-egui/tests/it/file_menu.rs +++ b/crates/ui-egui/tests/it/file_menu.rs @@ -4,6 +4,7 @@ use std::sync::{Arc, Mutex}; use egui_kittest::kittest::Queryable as _; use gridcraft_engine::{Session, core::CellRef, core::Value}; +use gridcraft_ui_egui::i18n::Language; use gridcraft_ui_egui::{Services, SheetApp}; fn harness(services: Services) -> egui_kittest::Harness<'static, SheetApp> { @@ -134,30 +135,25 @@ fn file_options_opens_the_options_page_and_pages_are_reachable_by_command() { #[test] fn translated_options_headings_show_no_context_prefix() { - for (code, headings, prefixed, (view, view_prefixed)) in [ - ("fr", ["Général", "Enregistrer"], ["Options|Général", "Options|Enregistrer"], ("Affichage", "Options de la feuille|Affichage")), - ("sr", ["Opšte", "Sačuvaj"], ["Opcije|Opšte", "Opcije|Sačuvaj"], ("Prikaz", "Opcije lista|Prikaz")), - ("nl", ["Algemeen", "Opslaan"], ["Opties|Algemeen", "Opties|Opslaan"], ("Weergeven", "Bladopties|Weergeven")), - ("ro", ["General", "Salvare"], ["Opțiuni|General", "Opțiuni|Salvare"], ("Vizualizare", "Opțiuni foaie|Vizualizare")), - ] { + for language in [Language::Fr, Language::Sr, Language::Nl, Language::Ro] { let mut h = harness(Services::default()); - h.state_mut().run("app.language.set", serde_json::json!({"language": code})).unwrap(); + h.state_mut().run("app.language.set", serde_json::json!({"language": language.code()})).unwrap(); h.state_mut().run("file.options", serde_json::json!({})).unwrap(); h.run_steps(3); assert_eq!(page(&h), Some("options")); - for heading in headings { - assert!(h.query_all_by_label(heading).next().is_some(), "{code}: no {heading} heading"); - } - for label in prefixed { - assert!(h.query_by_label(label).is_none(), "{code}: {label} shows its context"); + for key in ["Options|General", "Options|Save"] { + let heading = language.tr(key); + assert!(!heading.contains('|'), "{language:?}: {key} shows its context: {heading}"); + assert!(h.query_all_by_label(heading).next().is_some(), "{language:?}: no {heading} heading"); } // Page Layout › Sheet Options: the Gridlines and Headings "View" checkboxes had the same // prefix. The View tab may share the word, so count at least the two checkboxes. h.state_mut().run("ui.backstage", serde_json::json!({"page": "close"})).unwrap(); h.state_mut().ui.ribbon_tab = "Page Layout".into(); h.run_steps(3); - assert!(h.query_by_label(view_prefixed).is_none(), "{code}: {view_prefixed} shows its context"); - assert!(h.query_all_by_label(view).count() >= 2, "{code}: the Sheet Options checkboxes don't read {view}"); + let view = language.tr("Sheet Options|View"); + assert!(!view.contains('|'), "{language:?}: Sheet Options|View shows its context: {view}"); + assert!(h.query_all_by_label(view).count() >= 2, "{language:?}: the Sheet Options checkboxes don't read {view}"); } } diff --git a/crates/ui-egui/tests/it/formula_locale.rs b/crates/ui-egui/tests/it/formula_locale.rs index 16f4dac15c..98ab70d576 100644 --- a/crates/ui-egui/tests/it/formula_locale.rs +++ b/crates/ui-egui/tests/it/formula_locale.rs @@ -176,14 +176,12 @@ fn portuguese_interface_retains_its_canonical_formula_fallback() { let mut app = app(); app.run("app.language.set", json!({"code":"pt-BR"})).unwrap(); assert_eq!(app.ui.language, Language::PtBr); - assert_eq!(app.ui.language.tr("Data"), "Dados"); assert!(enter(&mut app, "=SUM(1.5,2.25)")); app.begin_edit(None, true); assert_eq!(app.editor.as_ref().unwrap().text, "=SUM(1.5,2.25)"); assert!(!enter(&mut app, "=SUMA(1;2)")); let restored: UiState = serde_json::from_str(&serde_json::to_string(&app.ui).unwrap()).unwrap(); assert_eq!(restored.language, Language::PtBr); - assert_eq!(Language::Es.tr("Page Layout|Orientation"), "Orientación"); } #[test] @@ -193,7 +191,6 @@ fn czech_interface_retains_its_canonical_formula_fallback() { let mut app = app(); app.run("app.language.set", json!({"code":"cs-CZ"})).unwrap(); assert_eq!(app.ui.language, Language::Cs); - assert_eq!(app.ui.language.tr("Formulas"), "Vzorce"); assert!(enter(&mut app, "=SUM(1.5,2.25)")); app.begin_edit(None, true); assert_eq!(app.editor.as_ref().unwrap().text, "=SUM(1.5,2.25)"); @@ -225,8 +222,6 @@ fn turkish_interface_retains_its_canonical_formula_fallback() { let mut app = app(); app.run("app.language.set", json!({"code":"tr-TR"})).unwrap(); assert_eq!(app.ui.language, Language::Tr); - assert_eq!(app.ui.language.tr("Data"), "Veri"); - assert_eq!(app.ui.language.tr("Formulas"), "Formüller"); assert!(enter(&mut app, "=SUM(1.5,2.25)")); app.begin_edit(None, true); assert_eq!(app.editor.as_ref().unwrap().text, "=SUM(1.5,2.25)"); diff --git a/crates/ui-egui/tests/it/interaction.rs b/crates/ui-egui/tests/it/interaction.rs index 46e981d0aa..94fefe6d3d 100644 --- a/crates/ui-egui/tests/it/interaction.rs +++ b/crates/ui-egui/tests/it/interaction.rs @@ -4,6 +4,7 @@ use egui::{Event, Key, Modifiers, PointerButton}; use egui_kittest::kittest::Queryable as _; use gridcraft_engine::Session; use gridcraft_engine::core::{CellRef, Value}; +use gridcraft_ui_egui::i18n::Language; use gridcraft_ui_egui::{SheetApp, grid::Geo}; use serde_json::json; @@ -400,8 +401,7 @@ fn renders_every_ribbon_tab_without_panicking() { } #[test] -fn language_switch_translates_the_ribbon_and_persists() { - use gridcraft_ui_egui::i18n::Language; +fn language_switch_renders_the_ribbon_and_persists() { let mut h = harness(blank()); // English by default whatever the host's locale: only the desktop app reads the system language. assert_eq!(h.state().ui.language, Language::En); @@ -415,31 +415,13 @@ fn language_switch_translates_the_ribbon_and_persists() { for id in ["app.language.set", "app.language.english", "app.language.japanese"] { assert!(ids.contains(&id), "engine.commands lists {id}"); } - for (language, expected) in [ - (Language::Zh, "数据"), - (Language::Ja, "データ"), - (Language::Ko, "데이터"), - (Language::Ru, "Данные"), - (Language::PtBr, "Dados"), - (Language::De, "Daten"), - (Language::Fr, "Données"), - (Language::Tr, "Veri"), - (Language::It, "Dati"), - ] { + for language in Language::ALL { h.state_mut().run("app.language.set", json!({"language": language.code()})).unwrap(); h.run_steps(2); // every locale renders the ribbon without panicking assert_eq!(h.state().ui.language, language); - assert_eq!(language.tr("Data"), expected); let restored: gridcraft_ui_egui::UiState = serde_json::from_str(&serde_json::to_string(&h.state().ui).unwrap()).unwrap(); assert_eq!(restored.language, language); } - // Czech spells "Data" as English does, so the Czech table is checked on a label that differs. - h.state_mut().run("app.language.set", json!({"language": "cs"})).unwrap(); - h.run_steps(2); - assert_eq!(h.state().ui.language, Language::Cs); - assert_eq!(Language::Cs.tr("Formulas"), "Vzorce"); - let restored: gridcraft_ui_egui::UiState = serde_json::from_str(&serde_json::to_string(&h.state().ui).unwrap()).unwrap(); - assert_eq!(restored.language, Language::Cs); h.state_mut().run("app.language.set", json!({"language": "en"})).unwrap(); assert_eq!(h.state().ui.language, Language::En); } @@ -452,16 +434,21 @@ fn pivot_fields_pane_follows_the_interface_language() { h.state_mut().run("insert.pivotTable", json!({"source": "Sheet1!$A$1:$B$3", "rows": ["Region"]})).unwrap(); h.state_mut().run("app.language.set", json!({"language": "es"})).unwrap(); h.run_steps(4); - for label in ["Campos de tabla dinámica", "Filas", "Valores", "Actualizar", "Campo calculado…"] { - assert!(h.query_by_label(label).is_some(), "the pane shows `{label}`"); + let es = |english| Language::Es.tr(english); + for english in ["PivotTable Fields", "Rows", "Values", "Refresh", "Calculated Field…"] { + assert!(h.query_by_label(es(english)).is_some(), "the pane shows `{}`", es(english)); + } + if es("PivotTable Fields") != "PivotTable Fields" { + assert!(h.query_by_label("PivotTable Fields").is_none()); } - assert!(h.query_by_label("PivotTable Fields").is_none()); h.get_by_label("Region ▾").click(); h.run_steps(2); - for label in ["Mover a rótulos de columna", "Agrupar por años", "Quitar campo"] { - assert!(h.query_by_label(label).is_some(), "the field menu shows `{label}`"); + for english in ["Move to Column Labels", "Group by Years", "Remove Field"] { + assert!(h.query_by_label(es(english)).is_some(), "the field menu shows `{}`", es(english)); + } + if es("Remove Field") != "Remove Field" { + assert!(h.query_by_label("Remove Field").is_none()); } - assert!(h.query_by_label("Remove Field").is_none()); } #[test] @@ -492,9 +479,12 @@ fn format_cells_draws_its_labels_in_the_interface_language() { h.state_mut().run("app.language.set", json!({"language": "de"})).unwrap(); h.state_mut().open_dialog("formatCells", json!({"tab": "Number"})); h.run_steps(2); - assert!(h.query_by_label("Wissenschaftlich").is_some(), "the Number tab's categories are translated"); - assert!(h.query_by_label("Scientific").is_none(), "no English category is left"); - h.get_by_label("Wissenschaftlich").click(); + let scientific = Language::De.tr("Scientific"); + assert!(h.query_by_label(scientific).is_some(), "the Number tab's categories are translated"); + if scientific != "Scientific" { + assert!(h.query_by_label("Scientific").is_none(), "no English category is left"); + } + h.get_by_label(scientific).click(); h.run_steps(2); let style = &h.state().dialog.as_ref().unwrap().values["style"]; assert_eq!(style["num_fmt"], json!("0.00E+00"), "a translated category still applies its code"); diff --git a/crates/ui-egui/tests/it/translated_form_fields.rs b/crates/ui-egui/tests/it/translated_form_fields.rs index 35847a4259..b284bf7867 100644 --- a/crates/ui-egui/tests/it/translated_form_fields.rs +++ b/crates/ui-egui/tests/it/translated_form_fields.rs @@ -4,6 +4,7 @@ use egui_kittest::kittest::Queryable as _; use gridcraft_engine::Session; use gridcraft_ui_egui::SheetApp; +use gridcraft_ui_egui::i18n::Language; use serde_json::json; fn harness() -> egui_kittest::Harness<'static, SheetApp> { @@ -23,19 +24,17 @@ fn harness() -> egui_kittest::Harness<'static, SheetApp> { #[test] fn form_fields_are_translated() { - for (dialog, translated, english) in [ - ("pageSetup", ["Margem esquerda (pol.):", "Rodapé (pol.):"], ["Left margin (in):", "Footer (in):"]), - ("insertPaste", ["Deslocar células para a direita", "Deslocar células para baixo"], ["Shift cells right", "Shift cells down"]), - ] { + for (dialog, labels) in [("pageSetup", ["Left margin (in):", "Footer (in):"]), ("insertPaste", ["Shift cells right", "Shift cells down"])] { let mut h = harness(); h.state_mut().run("app.language.set", json!({"language": "pt"})).unwrap(); h.state_mut().open_dialog(dialog, json!({})); h.run_steps(3); - for label in translated { - assert!(h.query_by_label_contains(label).is_some(), "{dialog}: no {label}"); - } - for label in english { - assert!(h.query_by_label_contains(label).is_none(), "{dialog}: {label} is untranslated"); + for english in labels { + let translated = Language::PtBr.tr(english); + assert!(h.query_by_label_contains(translated).is_some(), "{dialog}: no {translated}"); + if translated != english { + assert!(h.query_by_label_contains(english).is_none(), "{dialog}: {english} is untranslated"); + } } } } diff --git a/docs/localization-parity.md b/docs/localization-parity.md index 979ac1b810..39a9b3f91e 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (the Japanese catalog covers the dialogs, messages and menus; Dutch interface language; Romanian interface catalog) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Per-language status against Excel, which ships its UI in dozens of languages (Windows language packs cover 100+), with localized function names, argument separators, number and date formats, @@ -96,8 +96,9 @@ language (none so far) and of localized function names. ## Infrastructure (do first) 1. Extend the catalog from the ribbon to every user-visible string (dialogs, menus, task panes, - status and error messages, command labels, function descriptions), with a missing-key check - in `cargo xtask ci`. Language switching and the system-language default already exist (#31). + status and error messages, command labels, function descriptions). A string a catalog lacks + shows in English and never fails a check: `cargo xtask i18n-report` lists the missing strings + per language, and `cargo xtask ci` runs it first. Language switching and the system-language default already exist (#31). 2. Locale-aware formulas: localized function-name tables and separators at the formula-bar boundary only (files keep en-US invariant names), locale number and date formats, list separators in CSV. @@ -109,6 +110,7 @@ language (none so far) and of localized function names. | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | Missing translations are reported, never a failure: `cargo xtask i18n-report` prints, per language, the strings other catalogs translate and it lacks (plus Spanish function help rows keyed by English the function catalog no longer has) and always passes; `cargo xtask ci` runs it first. Coverage and per-language parity tests and tests of one language's exact wording are gone; UI tests take the expected text from the catalog. Duplicate-key, placeholder, context-prefix and line-shape checks stay | | 2026-10-21 | minor | Romanian interface (`ro`, Română): 970 labels covering every key of the other catalogs (`crates/ui-egui/src/i18n/ro.rs`), `ro-*` system locales, the View-tab switcher and `app.language.set`; context keys show no prefix (File › Options test). English formulas and formats; AI-assisted draft pending native-speaker review | | 2026-10-20 | minor | Japanese: furigana (`rPh` phonetic runs and `` in `xl/sharedStrings.xml`) is no longer deleted by an open + save; it is kept on the workbook and written back (a `` without runs is kept too, while duplicate texts with different readings keep only the first, reported on open). Display of the readings is still missing (`crates/xlsx/src/tests/phonetic_tests.rs`) | | 2026-10-20 | minor | Japanese catalog (`i18n/ja.rs`) covers every key the Brazilian Portuguese catalog translates: the 398-label shared core moved verbatim plus 545 dialog, message, Backstage and menu strings — 943 keys, ~12% → ~43%. A test parses `pt.rs`'s source, so the two catalogs can't drift apart again. Portuguese re-measured at 943 (was 923) | diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index cc3da3bb28..c40c976973 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -4,7 +4,7 @@ version.workspace = true edition.workspace = true license.workspace = true publish = false -description = "GridCraft workspace tooling: cargo xtask ci | layers | assets | wasm | parity | version | stats | ico" +description = "GridCraft workspace tooling: cargo xtask ci | layers | assets | wasm | parity | i18n-report | version | stats | ico" [lints] workspace = true diff --git a/xtask/src/i18n_report.rs b/xtask/src/i18n_report.rs new file mode 100644 index 0000000000..d72a887fad --- /dev/null +++ b/xtask/src/i18n_report.rs @@ -0,0 +1,218 @@ +//! `cargo xtask i18n-report`: the interface strings each language doesn't translate yet, read from +//! the catalogs in `crates/ui-egui/src/i18n/`. A string a catalog lacks shows in English, so the +//! report always succeeds; `cargo xtask ci` runs it first so the list shows in the gate log. +//! +//! The reference set is every key any catalog translates. A catalog is a source file with a +//! `fn translate`; its keys are the string literals that start a match arm (`"key" => …`) or open a +//! table row (`("key", …)`). With the `engine` feature it also lists function help rows keyed by +//! English the function catalog no longer has: those show the English help. + +use std::collections::{BTreeMap, BTreeSet}; +use std::fmt::Write as _; +use std::path::Path; + +/// Missing keys listed per language; the rest are counted. +const LIST_CAP: usize = 50; + +pub fn run(root: &Path) -> Result<(), String> { + let dir = root.join("crates/ui-egui/src/i18n"); + let mut catalogs = BTreeMap::new(); + let mut help = BTreeMap::new(); + let entries = match std::fs::read_dir(&dir) { + Ok(entries) => entries, + Err(e) => { + println!("i18n-report: can't read {}: {e}", dir.display()); + return Ok(()); + } + }; + for entry in entries.flatten() { + let path = entry.path(); + let Some(stem) = path.file_stem().and_then(|s| s.to_str()).map(str::to_owned) else { continue }; + if path.extension().and_then(|e| e.to_str()) != Some("rs") { + continue; + } + let Ok(source) = std::fs::read_to_string(&path) else { continue }; + if let Some(language) = stem.strip_suffix("_functions") { + help.insert(language.to_owned(), catalog_keys(&source)); + } else if source.contains("fn translate(") { + catalogs.insert(stem, catalog_keys(&source)); + } + } + print!("{}", render(&catalogs)); + print!("{}", function_help(&help)); + Ok(()) +} + +/// Per language: how many reference keys it lacks, then the first [`LIST_CAP`] of them. +pub fn render(catalogs: &BTreeMap>) -> String { + let reference: BTreeSet<&String> = catalogs.values().flatten().collect(); + let mut out = + format!("i18n-report: {} interface strings across {} catalogs; a string a catalog lacks shows in English\n", reference.len(), catalogs.len()); + for (language, keys) in catalogs { + let missing: Vec<&&String> = reference.iter().filter(|key| !keys.contains(key.as_str())).collect(); + let _ = writeln!(out, " {language}: {} missing", missing.len()); + list(&mut out, missing.iter().map(|key| key.as_str()), missing.len()); + } + out +} + +fn list<'a>(out: &mut String, keys: impl Iterator, count: usize) { + for key in keys.take(LIST_CAP) { + let _ = writeln!(out, " {key:?}"); + } + if count > LIST_CAP { + let _ = writeln!(out, " … and {} more", count - LIST_CAP); + } +} + +/// Function help rows whose English the function catalog no longer has (an English description +/// reworded, an argument renamed). +#[cfg(feature = "engine")] +fn function_help(help: &BTreeMap>) -> String { + let catalog = gridcraft_engine::cmd::formulas::function_list(); + let mut known: BTreeSet<&str> = catalog.iter().filter_map(|f| f["description"].as_str()).collect(); + known.extend(catalog.iter().filter_map(|f| f["signature"].as_str()).flat_map(argument_names)); + let mut out = String::new(); + for (language, keys) in help { + let stale: Vec<&str> = keys.iter().map(String::as_str).filter(|key| !known.contains(key)).collect(); + let _ = writeln!(out, " {language} function help: {} rows, {} keyed by English the function catalog no longer has", keys.len(), stale.len()); + list(&mut out, stale.iter().copied(), stale.len()); + } + out +} + +#[cfg(not(feature = "engine"))] +fn function_help(_: &BTreeMap>) -> String { + String::new() +} + +/// `SUM(number1, [number2], ...)` -> `number`, `number`: brackets, index and `...` dropped. +#[cfg(feature = "engine")] +fn argument_names(signature: &str) -> Vec<&str> { + let Some(inner) = signature.split_once('(').and_then(|(_, rest)| rest.strip_suffix(')')) else { return Vec::new() }; + inner + .split(", ") + .map(|arg| { + let arg = arg.strip_prefix('[').unwrap_or(arg); + let arg = arg.strip_suffix(']').unwrap_or(arg); + arg.trim_end_matches(|c: char| c.is_ascii_digit()) + }) + .filter(|name| !name.is_empty() && *name != "...") + .collect() +} + +/// The keys of one catalog source: string literals that start a match arm or open a table row. +/// Comments and the test module are skipped. +pub fn catalog_keys(source: &str) -> BTreeSet { + let source = source.split("#[cfg(test)]").next().unwrap_or(source); + let mut keys = BTreeSet::new(); + for (value, start, end) in literals(source) { + let before = source.get(..start).unwrap_or("").trim_end(); + let after = source.get(end..).unwrap_or("").trim_start(); + if after.starts_with("=>") || (before.ends_with('(') && after.starts_with(',')) { + keys.insert(value); + } + } + keys +} + +/// Every string literal outside `//` comments: its decoded value, and the byte range from its +/// opening quote to just past its closing one. +fn literals(source: &str) -> Vec<(String, usize, usize)> { + let mut out = Vec::new(); + let mut chars = source.char_indices().peekable(); + while let Some((i, c)) = chars.next() { + if c == '/' && source.get(i..).is_some_and(|rest| rest.starts_with("//")) { + for (_, c) in chars.by_ref() { + if c == '\n' { + break; + } + } + continue; + } + if c != '"' { + continue; + } + let mut value = String::new(); + let mut end = source.len(); + while let Some((j, c)) = chars.next() { + match c { + '"' => { + end = j + 1; + break; + } + '\\' => match chars.next().map(|(_, c)| c) { + Some('n') => value.push('\n'), + Some('t') => value.push('\t'), + Some('r') => value.push('\r'), + Some('0') => value.push('\0'), + Some(c @ ('\\' | '"' | '\'')) => value.push(c), + Some('u') => { + let mut hex = String::new(); + for (_, c) in chars.by_ref() { + match c { + '}' => break, + '{' => {} + c => hex.push(c), + } + } + if let Some(c) = u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32) { + value.push(c); + } + } + // A line continuation skips the newline and the next line's indentation. + Some('\n') => { + while chars.peek().is_some_and(|(_, c)| c.is_whitespace()) { + chars.next(); + } + } + _ => {} + }, + c => value.push(c), + } + } + out.push((value, i, end)); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_match_arms_and_table_rows_and_lists_what_each_catalog_lacks() { + let matched = r#" +pub(super) fn translate(text: &str) -> Option<&'static str> { + Some(match text { + "Paste" => "Einfügen", // "Commented" => "x", + "Line\nbreak \"quoted\"" => { + "Zeile" + } + _ => return None, + }) +} +#[cfg(test)] +mod tests { const T: &[(&str, &str)] = &[("Test only", "x")]; } +"#; + let table = r#" +pub(super) const CORE: &[(&str, &str)] = &[ + ("Paste", "貼り付け"), + ( + "Long \u{2026}", + "長い", + ), +]; +pub(super) fn translate(text: &str) -> Option<&'static str> { None } +"#; + let de = catalog_keys(matched); + assert_eq!(de.iter().map(String::as_str).collect::>(), ["Line\nbreak \"quoted\"", "Paste"]); + let ja = catalog_keys(table); + assert_eq!(ja.iter().map(String::as_str).collect::>(), ["Long …", "Paste"]); + + let report = render(&BTreeMap::from([("de".to_owned(), de), ("ja".to_owned(), ja)])); + assert!(report.starts_with("i18n-report: 3 interface strings across 2 catalogs"), "{report}"); + assert!(report.contains(" de: 1 missing\n \"Long …\"\n"), "{report}"); + assert!(report.contains(" ja: 1 missing\n \"Line\\nbreak \\\"quoted\\\"\"\n"), "{report}"); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 0c9f5547db..fe54b0f4e8 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -6,6 +6,7 @@ #![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used, clippy::panic))] mod assets; +mod i18n_report; mod ico; mod keytips; mod layers; @@ -21,11 +22,12 @@ const USAGE: &str = "\ usage: cargo xtask commands: - ci fmt --check, clippy -D warnings, test, doctest, assets, layers, wasm, parity --check, - keytips --check (stops at the first failure; the parity check runs only if - docs/parity-checklist.md exists) + ci i18n-report, fmt --check, clippy -D warnings, test, doctest, assets, layers, wasm, + parity --check, keytips --check (stops at the first failure; the parity check runs + only if docs/parity-checklist.md exists) test workspace tests (cargo nextest when installed, else cargo test), then doctests assets check that every icon/image/font/sample file is attributed in ATTRIBUTION.md + i18n-report list the interface strings each language doesn't translate yet (always passes) layers enforce the crate dependency layering (AGENTS.md \"Layering\") wasm cargo check --target wasm32-unknown-unknown for the L0-L5 crates (+ gridcraft-web), one call parity [--check] [--out PATH] @@ -44,6 +46,7 @@ fn main() -> ExitCode { let rest: Vec<&str> = args.iter().skip(1).map(String::as_str).collect(); let result = match args.first().map(String::as_str) { Some("assets") => assets::run(&root()), + Some("i18n-report") => i18n_report::run(&root()), Some("layers") => cmd_layers(), Some("wasm") => cmd_wasm(), Some("test") => cmd_test().and_then(|()| cmd_doctest()), @@ -194,6 +197,8 @@ fn cmd_doctest() -> Result<(), String> { fn cmd_ci() -> Result<(), String> { type Step = (&'static str, Box Result<(), String>>); let steps: Vec = vec![ + // Missing translations never fail the gate; the list goes first so every log shows it. + ("i18n-report", Box::new(|| i18n_report::run(&root()))), ( "fmt", Box::new(|| { From c746eea8764eb5919b1b0ff4ffc55e02940d5407 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:43:22 +0200 Subject: [PATCH 120/687] Fix MOD returning wrong sign and magnitude for tiny divisors Found while testing crates/functions/src/math.rs:305-319 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/functions/src/math.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/functions/src/math.rs b/crates/functions/src/math.rs index 7611dc410e..998a1c069a 100644 --- a/crates/functions/src/math.rs +++ b/crates/functions/src/math.rs @@ -306,8 +306,8 @@ fn modulo(x: f64, d: f64) -> R { if d == 0.0 { return Err(CellError::Div0); } - let q = (x / d).floor(); - let r = x - d * q; + // `%` is an exact remainder (truncated), so |r| < |d| even for tiny divisors. + let r = x % d; // Keep the sign of the divisor and avoid returning |d| from rounding. if r != 0.0 && (r < 0.0) != (d < 0.0) { return Ok(r + d); @@ -1442,6 +1442,19 @@ mod tests { close(ev1("ODD", 0.0), 1.0); } + #[test] + fn mod_tiny_divisor_stays_in_range() { + for (x, d) in [(3.0, 1e-300), (-1.0, 1e-300), (3.0, -1e-300), (-1.0, -1e-300)] { + match ev2("MOD", x, d) { + Value::Number(r) => { + assert!(r == 0.0 || (r < 0.0) == (d < 0.0), "MOD({x}, {d}) = {r}"); + assert!(r.abs() < d.abs(), "MOD({x}, {d}) = {r}"); + } + other => panic!("MOD({x}, {d}) = {other:?}"), + } + } + } + #[test] fn arithmetic() { close(ev2("MOD", 3.0, 2.0), 1.0); From d5aa6fbcbcabaf195c904202bcf3c44a5b094f9e Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 21:56:39 +0200 Subject: [PATCH 121/687] Conditional formatting: New Rule's formula box takes references clicked or dragged on the grid (#2348) --- crates/ui-egui/src/dialogs.rs | 84 +++++++++++++ crates/ui-egui/src/grid.rs | 22 +++- crates/ui-egui/tests/it/cf_rule_pick.rs | 160 ++++++++++++++++++++++++ crates/ui-egui/tests/it/main.rs | 1 + docs/ui-parity.md | 6 +- 5 files changed, 270 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/tests/it/cf_rule_pick.rs diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index bfb203c168..8be7f55cb7 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -7,6 +7,7 @@ use egui::{Color32, Key, Sense, Stroke, StrokeKind, vec2}; use crate::icons::{self, Icon}; +use gridcraft_engine::core::{CellRef, RangeRef}; use serde_json::{Map, Value as Json, json}; use crate::SheetApp; @@ -44,6 +45,24 @@ pub struct Dialog { /// The dialog this one was opened from (Name Manager › New…): it comes back when this closes. back: Option>, focus_initialized: bool, + /// The formula box that takes references clicked or dragged on the grid (New Formatting Rule). + pick: Option, +} + +/// A formula box in point mode, as in Excel: a click on the grid writes the cell's absolute +/// reference at the caret instead of selecting it, and a drag grows it into a range. +#[derive(Clone, Debug)] +struct RefPick { + key: &'static str, + /// The sheet the dialog was opened on: references to another sheet carry its name. + sheet: usize, + /// Caret (char index), mirrored from the text box while it has the keyboard. + caret: usize, + /// The reference last inserted (byte span), replaced by the next click or the drag. + point: Option<(usize, usize)>, + anchor: CellRef, + /// Give the box the keyboard back, caret after the reference, on the next frame. + refocus: bool, } impl Dialog { @@ -77,6 +96,43 @@ impl Dialog { self.values.insert(key.into(), value); } + /// Whether a click on the grid goes to a formula box of this dialog rather than the selection. + pub fn picks_references(&self) -> bool { + self.pick.is_some() + } + + /// A click (`extend` false) or a drag reaching `cell`: writes the absolute reference at the + /// formula box's caret, replacing the reference the press inserted, as in the formula bar's + /// point mode. An empty box starts with `=`. After an operand there is no place for a + /// reference, so nothing changes. `active_sheet` names the sheet shown when it is not the + /// dialog's own. + pub fn pick_reference(&mut self, cell: CellRef, active_sheet: (usize, &str), extend: bool) { + let Some(pick) = self.pick.as_mut() else { return }; + let text = self.values.get(pick.key).and_then(Json::as_str).unwrap_or("").to_string(); + let mut ed = crate::editor::EditState::new(pick.sheet, cell, text, true, false); + ed.caret = pick.caret.min(ed.text.chars().count()); + ed.point = pick.point; + if ed.text.trim().is_empty() { + ed.text = "=".into(); + ed.caret = 1; + ed.point = None; + } + if !ed.can_point() { + return; + } + if !extend { + pick.anchor = cell; + } + let range = abs(&RangeRef::new(pick.anchor, cell)); + let reference = + if active_sheet.0 == pick.sheet { range } else { format!("{}!{range}", gridcraft_engine::formula::quote_sheet(active_sheet.1)) }; + ed.insert_ref(&reference); + pick.caret = ed.caret; + pick.point = ed.point; + pick.refocus = true; + self.values.insert(pick.key.into(), json!(ed.text)); + } + fn form(name: &str, title: &str, command: &'static str, fields: Vec, defaults: Json) -> Dialog { Dialog { name: name.into(), @@ -94,6 +150,7 @@ impl Dialog { picture_target: None, back: None, focus_initialized: false, + pick: None, } } @@ -114,6 +171,7 @@ impl Dialog { picture_target: None, back: None, focus_initialized: false, + pick: None, } } @@ -690,6 +748,10 @@ impl Dialog { fields, json!({"preset": "lightRedFillDarkRedText", "period": "today"}), ); + if ty == "expression" { + let sheet = app.session.active().map(|doc| doc.wb.active_sheet).unwrap_or(0); + d.pick = Some(RefPick { key: "formula", sheet, caret: 0, point: None, anchor: CellRef::new(0, 0), refocus: false }); + } d.values.insert("type".into(), json!(ty)); d.values.insert("operator".into(), p("operator")); d @@ -1634,6 +1696,25 @@ fn take_first_focus(d: &mut Dialog, r: &egui::Response) { d.focus_first_field = false; } +/// Keeps a picking formula box's caret, and gives it the keyboard back after a grid click put a +/// reference in it (the click took the focus), its caret after the reference. +fn track_pick(pick: &mut RefPick, r: &egui::Response, typed: bool) { + if typed { + pick.point = None; + } + if pick.refocus { + let mut state = egui::text_edit::TextEditState::load(&r.ctx, r.id).unwrap_or_default(); + state.cursor.set_char_range(Some(egui::text::CCursorRange::one(egui::text::CCursor::new(pick.caret)))); + state.store(&r.ctx, r.id); + r.request_focus(); + pick.refocus = false; + } else if r.has_focus() + && let Some(range) = egui::text_edit::TextEditState::load(&r.ctx, r.id).and_then(|state| state.cursor.char_range()) + { + pick.caret = range.primary.index.into(); + } +} + /// Draws a dialog's `fields`, editing its values. fn form_fields(ui: &mut egui::Ui, d: &mut Dialog) { // Field labels, choices and notes are English source strings, translated as they are drawn. @@ -1659,6 +1740,9 @@ fn form_fields(ui: &mut egui::Ui, d: &mut Dialog) { if r.changed() { d.values.insert(key.into(), json!(s)); } + if let Some(pick) = d.pick.as_mut().filter(|pick| pick.key == key) { + track_pick(pick, &r, r.changed()); + } }); } Field::Password { key, label } => { diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 5902949041..157f608fe2 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2336,6 +2336,19 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & app.grid.filter_menu = Some((c.col, c.row, geo.mirror_pos(p))); return; } + // A dialog's formula box in point mode (New Formatting Rule) takes the reference; the + // selection, which the dialog acts on, stays. + if let Some(d) = app.dialog.as_mut().filter(|d| d.picks_references()) { + d.pick_reference(c, (wb.active_sheet, &sh.name), false); + app.grid.drag = Drag::Select; + app.grid.drag_select = true; + if resp.drag_started() + && let Some(p) = pos + { + select_to_pointer(app, geo, sh, wb, p); + } + return; + } // Point mode while editing a formula: clicking inserts a reference. if let Some(ed) = app.editor.as_mut() && ed.can_point() @@ -2643,7 +2656,10 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & /// An armed Format Painter pastes onto the cells just clicked or dragged over (not while /// pointing a formula reference, which also drags a Select). fn format_painter_apply(app: &mut SheetApp) { - if app.session.format_painter.is_some() && !app.editor.as_ref().is_some_and(|ed| ed.can_point()) { + if app.session.format_painter.is_some() + && !app.editor.as_ref().is_some_and(|ed| ed.can_point()) + && !app.dialog.as_ref().is_some_and(|d| d.picks_references()) + { let dest = app.session.active().map(|d| d.selection.current().a1()); if let Some(dest) = dest && let Err(e) = app.run("edit.formatPainter", json!({"apply": dest})) @@ -2696,7 +2712,9 @@ fn select_to_pointer(app: &mut SheetApp, geo: &Geo, sh: &Sheet, wb: &Workbook, p geo.row_at(sh, p.y.clamp(geo.cells.top() + 1.0, geo.cells.bottom() - 1.0)), geo.col_at(sh, p.x.clamp(geo.cells.left() + 1.0, geo.cells.right() - 1.0)), ); - if let Some(ed) = app.editor.as_mut() + if let Some(d) = app.dialog.as_mut().filter(|d| d.picks_references()) { + d.pick_reference(cell, (wb.active_sheet, &sh.name), true); + } else if let Some(ed) = app.editor.as_mut() && ed.point.is_some() { if let Some(start) = ed.point_anchor.filter(|(sheet, _)| *sheet == wb.active_sheet).map(|(_, cell)| cell).or(ed.point_cell) { diff --git a/crates/ui-egui/tests/it/cf_rule_pick.rs b/crates/ui-egui/tests/it/cf_rule_pick.rs new file mode 100644 index 0000000000..dc004ff254 --- /dev/null +++ b/crates/ui-egui/tests/it/cf_rule_pick.rs @@ -0,0 +1,160 @@ +//! New Formatting Rule ▸ formula: clicking or dragging on the grid writes the absolute reference +//! into the formula box at its caret, as in Excel, and leaves the selection the rule applies to alone. + +use egui::{Event, Key, Modifiers, PointerButton, Pos2, pos2, vec2}; +use gridcraft_engine::Session; +use gridcraft_ui_egui::{SheetApp, grid::Geo}; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +fn harness() -> Harness { + let mut session = Session::new(); + session.new_workbook(); + session.execute("home.insertSheet", json!({})).unwrap(); + session.execute("sheet.activate", json!({"sheet": 0})).unwrap(); + session.execute("selection.set", json!({"range": "F8:G9"})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_step_dt(1.0 / 60.0).with_size(vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + SheetApp::new(session, Default::default()), + ); + h.run_steps(4); + h.state_mut().open_dialog("cfQuick", json!({"type": "expression", "title": "New Formatting Rule"})); + h.run_steps(6); + h +} + +fn formula(h: &Harness) -> String { + h.state().dialog.as_ref().and_then(|d| d.values.get("formula")).and_then(|v| v.as_str()).unwrap_or("").to_string() +} + +fn selection(h: &Harness) -> String { + h.state().session.active().unwrap().selection.current().a1() +} + +fn type_text(h: &mut Harness, text: &str) { + for c in text.chars() { + h.input_mut().events.push(Event::Text(c.to_string())); + h.run_steps(1); + } +} + +fn cell_center(h: &Harness, a1: &str) -> Pos2 { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + let c = gridcraft_engine::core::CellRef::parse(a1).unwrap(); + pos2(geo.x(sheet, c.col) + sheet.col_width(c.col) * geo.z * 0.5, geo.y(sheet, c.row) + sheet.row_height(c.row) * geo.z * 0.5) +} + +fn click(h: &mut Harness, a1: &str) { + let pos = cell_center(h, a1); + for pressed in [true, false] { + h.input_mut() + .events + .extend([Event::PointerMoved(pos), Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }]); + h.step(); + } + h.run_steps(3); +} + +fn drag(h: &mut Harness, from: &str, to: &str) { + let (a, b) = (cell_center(h, from), cell_center(h, to)); + h.input_mut() + .events + .extend([Event::PointerMoved(a), Event::PointerButton { pos: a, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }]); + h.step(); + for i in 1..=6 { + let p = a + (b - a) * (i as f32 / 6.0); + h.input_mut().events.push(Event::PointerMoved(p)); + h.step(); + } + h.input_mut().events.push(Event::PointerButton { pos: b, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.step(); + h.run_steps(3); +} + +#[test] +fn clicking_a_cell_after_equals_inserts_its_absolute_reference() { + let mut h = harness(); + type_text(&mut h, "="); + assert_eq!(formula(&h), "=", "the formula box had the keyboard"); + click(&mut h, "B2"); + assert_eq!(formula(&h), "=$B$2"); + assert_eq!(selection(&h), "F8:G9", "the range the rule applies to is still selected"); +} + +#[test] +fn clicking_a_cell_in_an_empty_box_starts_the_formula() { + let mut h = harness(); + click(&mut h, "C3"); + assert_eq!(formula(&h), "=$C$3"); + assert_eq!(selection(&h), "F8:G9"); +} + +#[test] +fn a_second_click_replaces_the_reference_just_inserted() { + let mut h = harness(); + type_text(&mut h, "="); + click(&mut h, "B2"); + click(&mut h, "D5"); + assert_eq!(formula(&h), "=$D$5"); +} + +#[test] +fn dragging_inserts_a_range() { + let mut h = harness(); + type_text(&mut h, "="); + drag(&mut h, "B2", "C4"); + assert_eq!(formula(&h), "=$B$2:$C$4"); + assert_eq!(selection(&h), "F8:G9"); +} + +#[test] +fn the_reference_goes_at_the_caret_and_typing_continues_after_it() { + let mut h = harness(); + type_text(&mut h, "=SUM()"); + h.key_press(Key::ArrowLeft); + h.run_steps(2); + click(&mut h, "B2"); + assert_eq!(formula(&h), "=SUM($B$2)"); + type_text(&mut h, "+1"); + assert_eq!(formula(&h), "=SUM($B$2+1)", "the box has the keyboard back, its caret after the reference"); +} + +#[test] +fn a_click_after_an_operand_changes_nothing() { + let mut h = harness(); + type_text(&mut h, "=A1>5"); + click(&mut h, "B2"); + assert_eq!(formula(&h), "=A1>5"); + assert_eq!(selection(&h), "F8:G9"); +} + +#[test] +fn ok_applies_the_picked_formula_to_the_selection() { + let mut h = harness(); + type_text(&mut h, "="); + click(&mut h, "B2"); + type_text(&mut h, ">0"); + h.key_press(Key::Enter); + h.run_steps(4); + assert!(h.state().dialog.is_none(), "Enter confirmed the rule"); + let sheet = h.state().session.active().unwrap().wb.active().unwrap(); + let cf = sheet.cond_formats.last().expect("a rule was added"); + assert_eq!(cf.ranges.iter().map(|r| r.a1()).collect::>(), ["F8:G9"]); + assert!(format!("{:?}", cf.rule).contains("$B$2>0"), "rule: {:?}", cf.rule); +} + +#[test] +fn other_dialogs_leave_grid_clicks_alone() { + let mut h = harness(); + h.state_mut().dialog = None; + h.state_mut().open_dialog("cfQuick", json!({"type": "containsText", "title": "Text that Contains"})); + h.run_steps(6); + click(&mut h, "B2"); + assert_eq!(selection(&h), "B2", "a box that is not a formula does not take references"); +} diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index b89253bb96..94d6540893 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -10,6 +10,7 @@ mod autosave_persist; mod borders; mod cell_editor_caret; mod cell_pictures; +mod cf_rule_pick; mod chart_resize; mod clipboard; mod comments_pane; diff --git a/docs/ui-parity.md b/docs/ui-parity.md index c7cbc349ee..4795fb1965 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Page Up and Ctrl+Up into frozen rows, Ctrl+Left and Alt+Page Up into frozen columns bring the scrolled area back; Auto Fill Options button after a left-drag of the fill handle (#1403); Show Outline Symbols, Ctrl+8; a too-wide number in a repeat-fill format such as Accounting shows #### instead of losing its leading digits; Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (New Formatting Rule's formula box takes references clicked or dragged on the grid, #2348; Page Up and Ctrl+Up into frozen rows, Ctrl+Left and Alt+Page Up into frozen columns bring the scrolled area back; Auto Fill Options button after a left-drag of the fill handle (#1403); Show Outline Symbols, Ctrl+8; a too-wide number in a repeat-fill format such as Accounting shows #### instead of losing its leading digits; Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -152,6 +152,9 @@ Seek, Scenario Manager, Data Analysis, Forecast Sheet and others exist. Missing Solver, Power Query editor, Insert Object, Equation editor, Protect/Allow Edit Ranges, Workbook Links, Accessibility checker. Like Excel's, every dialog opens centred and moves by its title bar, keeping its place while it is open (#1250). +New Formatting Rule's formula box points like the formula bar: a click on the grid writes the +cell's absolute reference at the caret (`=$B$2`) and a drag a range, leaving the selection the rule +applies to as it is (#2348). Other dialogs' reference fields don't take clicks yet. ## Accessibility @@ -175,6 +178,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | Conditional Formatting ▸ New Rule, formula: clicking a cell on the grid writes its absolute reference at the formula box's caret (`=$B$2`, an empty box starts with `=`), a drag writes a range (`$B$2:$C$4`) and a second click replaces the reference just written, as in Excel ("Selecting cells on the worksheet inserts absolute cell references"). The selection the rule applies to doesn't move, and the box keeps the keyboard. After an operand (`=A1>5`) the click does nothing (#2348) | | 2026-10-23 | minor | Frozen panes: Page Up, Ctrl+Up and Shift variants that move from the scrolled rows into the frozen ones bring the scrolled rows back to the top, so the rows in between are seen again (Page Up from far down used to end on row 1 with the view still a page down); Ctrl+Left and Alt+Page Up do the same for frozen columns. Other ways of selecting a frozen cell (click, Find, Go To, Undo, formula pointing) leave the view where it is | | 2026-10-23 | minor | After a left-drag of the fill handle an Auto Fill Options button sits below the filled cells; its menu redoes the fill as Copy Cells, Fill Series, Fill Formatting Only or Fill Without Formatting (undoes the drag's fill, then `edit.autoFill` in that mode). It goes away when the workbook changes or the selection moves (#1403) | | 2026-10-22 | minor | Show Outline Symbols (`view.showOutlineSymbols`, Ctrl+8): a sheet can hide the outline's level buttons and brackets while keeping its outline, as Excel saves with `showOutlineSymbols="0"` (read and written); the grid then gives the bands no room | From 45f06f50da6c32fd2f76b8a9de0673d5dd2e3675 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 22:05:24 +0200 Subject: [PATCH 122/687] Conditional formatting: New Rule keeps the range it was opened for when pointing on another sheet (#2348) --- crates/ui-egui/src/dialogs.rs | 15 +++++++++++++-- crates/ui-egui/tests/it/cf_rule_pick.rs | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 8be7f55cb7..95855e56a8 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -63,6 +63,9 @@ struct RefPick { anchor: CellRef, /// Give the box the keyboard back, caret after the reference, on the next frame. refocus: bool, + /// The selection the rule applies to, fixed when the dialog opened as Excel's "Applies to": + /// pointing on another sheet must not move the rule there. + applies_to: String, } impl Dialog { @@ -749,8 +752,12 @@ impl Dialog { json!({"preset": "lightRedFillDarkRedText", "period": "today"}), ); if ty == "expression" { - let sheet = app.session.active().map(|doc| doc.wb.active_sheet).unwrap_or(0); - d.pick = Some(RefPick { key: "formula", sheet, caret: 0, point: None, anchor: CellRef::new(0, 0), refocus: false }); + let (sheet, applies_to) = app + .session + .active() + .map(|doc| (doc.wb.active_sheet, doc.selection.ranges.iter().map(RangeRef::a1).collect::>().join(","))) + .unwrap_or_default(); + d.pick = Some(RefPick { key: "formula", sheet, caret: 0, point: None, anchor: CellRef::new(0, 0), refocus: false, applies_to }); } d.values.insert("type".into(), json!(ty)); d.values.insert("operator".into(), p("operator")); @@ -1177,6 +1184,10 @@ pub(crate) fn build_params(d: &Dialog) -> Json { rule.insert(k.clone(), val.clone()); } v = json!({"rule": Json::Object(rule)}); + if let Some(pick) = d.pick.as_ref().filter(|pick| !pick.applies_to.is_empty()) { + v["sheet"] = json!(pick.sheet); + v["range"] = json!(pick.applies_to); + } } "series" => { if let Some(s) = d.values.get("stop").and_then(Json::as_str).and_then(|s| s.parse::().ok()) { diff --git a/crates/ui-egui/tests/it/cf_rule_pick.rs b/crates/ui-egui/tests/it/cf_rule_pick.rs index dc004ff254..642a91a180 100644 --- a/crates/ui-egui/tests/it/cf_rule_pick.rs +++ b/crates/ui-egui/tests/it/cf_rule_pick.rs @@ -158,3 +158,20 @@ fn other_dialogs_leave_grid_clicks_alone() { click(&mut h, "B2"); assert_eq!(selection(&h), "B2", "a box that is not a formula does not take references"); } + +#[test] +fn pointing_on_another_sheet_keeps_the_rule_on_the_sheet_it_was_opened_for() { + let mut h = harness(); + type_text(&mut h, "="); + h.state_mut().session.execute("sheet.activate", json!({"sheet": 1})).unwrap(); + h.run_steps(3); + click(&mut h, "A1"); + assert_eq!(formula(&h), "=Sheet2!$A$1"); + h.key_press(Key::Enter); + h.run_steps(4); + assert!(h.state().dialog.is_none(), "Enter confirmed the rule"); + let wb = &h.state().session.active().unwrap().wb; + assert!(wb.sheet(1).unwrap().cond_formats.is_empty(), "nothing lands on the sheet pointed at"); + let cf = wb.sheet(0).unwrap().cond_formats.last().expect("the rule is on the sheet the dialog was opened on"); + assert_eq!(cf.ranges.iter().map(|r| r.a1()).collect::>(), ["F8:G9"]); +} From 7cf44d0fbbb853617a09a608d2fe988b64f5ccac Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 23 Oct 2026 16:05:43 -0400 Subject: [PATCH 123/687] Integration and review fixes for #2367, #2369 and #2387 - #2369: its test joins the single ui-egui integration-test binary (tests/it), as main now builds them. - #2367: a cell set to empty text is a blank entry and doesn't grow the AutoFilter (CodeRabbit). - #2387: MOD's range check also covers the sign-corrected remainder (CodeRabbit). Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/data.rs | 5 ++++- crates/functions/src/math.rs | 4 +--- crates/ui-egui/tests/{ => it}/contextual_ribbon_tab.rs | 0 crates/ui-egui/tests/it/main.rs | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) rename crates/ui-egui/tests/{ => it}/contextual_ribbon_tab.rs (100%) diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index 8fe74779c3..8f7c47fb8f 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -656,7 +656,7 @@ fn filter_blank(v: &Value) -> bool { /// A table's own filter is left to the table, and a row typed after a blank row does not count. /// A formula counts before it is calculated: this runs inside the edit, ahead of the recalc. pub(crate) fn grow_autofilter(sh: &mut Sheet, at: CellRef) { - if !sh.cells.get(at).is_some_and(|c| !c.value.is_empty() || c.formula.is_some()) { + if !sh.cells.get(at).is_some_and(|c| !filter_blank(&c.value) || c.formula.is_some()) { return; } let Some(af) = sh.autofilter.as_ref() else { return }; @@ -1589,6 +1589,9 @@ mod tests { // A cell outside the filter's columns does not either. s.execute("cell.set", json!({"cell": "C6", "input": "x"})).unwrap(); assert_eq!(range(&s), Some("A1:B5".to_string())); + // Empty text is a blank entry, not a value. + s.execute("range.setValues", json!({"range": "A6", "parse": false, "values": [[""]]})).unwrap(); + assert_eq!(range(&s), Some("A1:B5".to_string())); // A formula and Ctrl+Enter (range.fill) are entries too. s.execute("cell.set", json!({"cell": "B6", "input": "=1+1"})).unwrap(); assert_eq!(range(&s), Some("A1:B6".to_string())); diff --git a/crates/functions/src/math.rs b/crates/functions/src/math.rs index 998a1c069a..53a92f00f5 100644 --- a/crates/functions/src/math.rs +++ b/crates/functions/src/math.rs @@ -309,9 +309,7 @@ fn modulo(x: f64, d: f64) -> R { // `%` is an exact remainder (truncated), so |r| < |d| even for tiny divisors. let r = x % d; // Keep the sign of the divisor and avoid returning |d| from rounding. - if r != 0.0 && (r < 0.0) != (d < 0.0) { - return Ok(r + d); - } + let r = if r != 0.0 && (r < 0.0) != (d < 0.0) { r + d } else { r }; if r.abs() >= d.abs() { return Ok(0.0); } diff --git a/crates/ui-egui/tests/contextual_ribbon_tab.rs b/crates/ui-egui/tests/it/contextual_ribbon_tab.rs similarity index 100% rename from crates/ui-egui/tests/contextual_ribbon_tab.rs rename to crates/ui-egui/tests/it/contextual_ribbon_tab.rs diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 9b04d3869e..fc00fb5201 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -16,6 +16,7 @@ mod comments_pane; mod commit_after_pointing; mod committed_text_fields; mod context_menu_stays_open; +mod contextual_ribbon_tab; mod control_timeout; mod dialog_button_row; mod dialog_confirm; From 4b2ee2e2dd8c4e71793fa8a658242627e356fd22 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo Date: Fri, 23 Oct 2026 22:07:22 +0200 Subject: [PATCH 124/687] Conditional formatting: New Rule remembers its sheet by name, so moving or inserting sheets while it is open keeps the rule there (#2348) --- crates/ui-egui/src/dialogs.rs | 27 ++++++++++++++++--------- crates/ui-egui/src/grid.rs | 4 ++-- crates/ui-egui/tests/it/cf_rule_pick.rs | 20 ++++++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 95855e56a8..e83db2fa1a 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -54,8 +54,9 @@ pub struct Dialog { #[derive(Clone, Debug)] struct RefPick { key: &'static str, - /// The sheet the dialog was opened on: references to another sheet carry its name. - sheet: usize, + /// The sheet the dialog was opened on, by name: its index changes when sheets are inserted or + /// moved while the dialog is open. References to another sheet carry that sheet's name. + sheet: String, /// Caret (char index), mirrored from the text box while it has the keyboard. caret: usize, /// The reference last inserted (byte span), replaced by the next click or the drag. @@ -109,10 +110,10 @@ impl Dialog { /// point mode. An empty box starts with `=`. After an operand there is no place for a /// reference, so nothing changes. `active_sheet` names the sheet shown when it is not the /// dialog's own. - pub fn pick_reference(&mut self, cell: CellRef, active_sheet: (usize, &str), extend: bool) { + pub fn pick_reference(&mut self, cell: CellRef, active_sheet: &str, extend: bool) { let Some(pick) = self.pick.as_mut() else { return }; let text = self.values.get(pick.key).and_then(Json::as_str).unwrap_or("").to_string(); - let mut ed = crate::editor::EditState::new(pick.sheet, cell, text, true, false); + let mut ed = crate::editor::EditState::new(0, cell, text, true, false); ed.caret = pick.caret.min(ed.text.chars().count()); ed.point = pick.point; if ed.text.trim().is_empty() { @@ -127,8 +128,11 @@ impl Dialog { pick.anchor = cell; } let range = abs(&RangeRef::new(pick.anchor, cell)); - let reference = - if active_sheet.0 == pick.sheet { range } else { format!("{}!{range}", gridcraft_engine::formula::quote_sheet(active_sheet.1)) }; + let reference = if active_sheet.eq_ignore_ascii_case(&pick.sheet) { + range + } else { + format!("{}!{range}", gridcraft_engine::formula::quote_sheet(active_sheet)) + }; ed.insert_ref(&reference); pick.caret = ed.caret; pick.point = ed.point; @@ -755,7 +759,12 @@ impl Dialog { let (sheet, applies_to) = app .session .active() - .map(|doc| (doc.wb.active_sheet, doc.selection.ranges.iter().map(RangeRef::a1).collect::>().join(","))) + .map(|doc| { + ( + doc.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), + doc.selection.ranges.iter().map(RangeRef::a1).collect::>().join(","), + ) + }) .unwrap_or_default(); d.pick = Some(RefPick { key: "formula", sheet, caret: 0, point: None, anchor: CellRef::new(0, 0), refocus: false, applies_to }); } @@ -1184,8 +1193,8 @@ pub(crate) fn build_params(d: &Dialog) -> Json { rule.insert(k.clone(), val.clone()); } v = json!({"rule": Json::Object(rule)}); - if let Some(pick) = d.pick.as_ref().filter(|pick| !pick.applies_to.is_empty()) { - v["sheet"] = json!(pick.sheet); + if let Some(pick) = d.pick.as_ref().filter(|pick| !pick.applies_to.is_empty() && !pick.sheet.is_empty()) { + v["sheet"] = json!(pick.sheet.as_str()); v["range"] = json!(pick.applies_to); } } diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 157f608fe2..87ac5787e7 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2339,7 +2339,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & // A dialog's formula box in point mode (New Formatting Rule) takes the reference; the // selection, which the dialog acts on, stays. if let Some(d) = app.dialog.as_mut().filter(|d| d.picks_references()) { - d.pick_reference(c, (wb.active_sheet, &sh.name), false); + d.pick_reference(c, &sh.name, false); app.grid.drag = Drag::Select; app.grid.drag_select = true; if resp.drag_started() @@ -2713,7 +2713,7 @@ fn select_to_pointer(app: &mut SheetApp, geo: &Geo, sh: &Sheet, wb: &Workbook, p geo.col_at(sh, p.x.clamp(geo.cells.left() + 1.0, geo.cells.right() - 1.0)), ); if let Some(d) = app.dialog.as_mut().filter(|d| d.picks_references()) { - d.pick_reference(cell, (wb.active_sheet, &sh.name), true); + d.pick_reference(cell, &sh.name, true); } else if let Some(ed) = app.editor.as_mut() && ed.point.is_some() { diff --git a/crates/ui-egui/tests/it/cf_rule_pick.rs b/crates/ui-egui/tests/it/cf_rule_pick.rs index 642a91a180..a523c9c6d3 100644 --- a/crates/ui-egui/tests/it/cf_rule_pick.rs +++ b/crates/ui-egui/tests/it/cf_rule_pick.rs @@ -175,3 +175,23 @@ fn pointing_on_another_sheet_keeps_the_rule_on_the_sheet_it_was_opened_for() { let cf = wb.sheet(0).unwrap().cond_formats.last().expect("the rule is on the sheet the dialog was opened on"); assert_eq!(cf.ranges.iter().map(|r| r.a1()).collect::>(), ["F8:G9"]); } + +#[test] +fn the_rule_follows_its_sheet_when_sheets_move_while_the_dialog_is_open() { + let mut h = harness(); + type_text(&mut h, "="); + click(&mut h, "B2"); + h.state_mut().session.execute("sheet.move", json!({"sheet": 0, "to": 1})).unwrap(); + h.run_steps(3); + let names = |h: &Harness| h.state().session.active().unwrap().wb.sheets.iter().map(|s| s.name.clone()).collect::>(); + assert_eq!(names(&h), ["Sheet2", "Sheet1"], "Sheet1 now sits at index 1"); + h.key_press(Key::Enter); + h.run_steps(4); + assert!(h.state().dialog.is_none(), "Enter confirmed the rule"); + let wb = &h.state().session.active().unwrap().wb; + let sheet1 = wb.sheets.iter().find(|s| s.name == "Sheet1").unwrap(); + let sheet2 = wb.sheets.iter().find(|s| s.name == "Sheet2").unwrap(); + assert!(sheet2.cond_formats.is_empty(), "nothing lands on the sheet that took index 0"); + let cf = sheet1.cond_formats.last().expect("the rule stays on Sheet1"); + assert_eq!(cf.ranges.iter().map(|r| r.a1()).collect::>(), ["F8:G9"]); +} From 18ec913c4e84a426700942ec96a4c3f2c44a4ba0 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:17:57 -0400 Subject: [PATCH 125/687] Engine: a command keeps its group's name only for a step that is exactly the group's The record of the last group step held the document, the history length and a group epoch, and a command reused the group's label when the lengths matched and the epoch was newer. A direct edit after the group (no nested command, so the length still matches) then left the step named after the group alone. The record now holds the workbook the group's step led to (a weak reference, so it keeps nothing alive). A command keeps the group's name only when the one entry it added starts from the command's own starting workbook and ends at its result, which is provenance by identity rather than by position: an entry a nested command made, a step of an earlier command undone since, and a group step the command edited past all name the step after the command. A later group that rolled back changes nothing, and the rollback no longer needs to clear the record. --- crates/engine/src/lib.rs | 32 +++++++++++++-------- crates/engine/src/tests/seams.rs | 48 ++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 8614bc094c..588a8a2a2b 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -97,6 +97,17 @@ struct OpenGroup { redo: Vec, } +/// The undo step the last [`Session::group_end`] made: its document and the workbook it led to. +/// The entry itself holds the workbook it started from, so a command can check that the step is +/// exactly its own (the same start and end as the command's) and keep the group's name for it. +/// A weak reference: the record keeps no workbook alive, and a workbook that is gone can't be +/// a command's result. +#[derive(Debug)] +struct GroupStep { + doc: u64, + after: std::sync::Weak, +} + /// The clipboard: a block of cells (with formulas and styles) or marching-ants reference. #[derive(Clone, Debug)] pub struct Clipboard { @@ -405,8 +416,9 @@ pub struct Session { next_group: u64, /// Counts group begins and ends, so `execute` can tell that a command opened or closed one. group_epoch: u64, - /// The step the last group made: document, its undo length after the step, and the epoch. - group_step: Option<(u64, usize, u64)>, + /// The step the last `group_end` made, so `execute` can tell that a command's step is exactly + /// its group's. + group_step: Option, /// Measures cell text for AutoFit Column Width. Without it (CLI, MCP) AutoFit uses /// [`cmd::format::approx_text_width`]. pub text_width: Option, @@ -567,12 +579,14 @@ impl Session { let mut label = spec.label.trim_end_matches('…').to_string(); if own_groups && let Some((_, len)) = start { // A command whose only step is its group's keeps that name ("Run Macro: Totals"). - // The step must have been made by a group this command ended (its epoch is - // past the one the command started with): an older record at the same length - // belongs to an earlier command, undone since. + // That is so only when the one entry the command added is a group's step from + // the command's own start to its own result: an entry a nested command made, + // a group step of an earlier command (undone since), or a group's step that + // the command edited past all name the step after the command instead. if d.undo.len() == len + 1 - && self.group_step.is_some_and(|(doc, n, epoch)| doc == uid && n == len + 1 && epoch > group_epoch) && let Some(last) = d.undo.last() + && Arc::ptr_eq(&last.wb, &wb) + && self.group_step.as_ref().is_some_and(|g| g.doc == uid && g.after.upgrade().is_some_and(|after| Arc::ptr_eq(&after, &d.wb))) { label = last.label.clone(); } @@ -646,7 +660,7 @@ impl Session { let changed = !Arc::ptr_eq(&d.wb, &g.wb); if changed && !outer { push_entry(&mut d.undo, g.label, g.wb, &d.wb, g.selection); - self.group_step = Some((g.doc, d.undo.len(), self.group_epoch)); + self.group_step = Some(GroupStep { doc: g.doc, after: Arc::downgrade(&d.wb) }); d.redo.clear(); d.revision += 1; } @@ -679,10 +693,6 @@ impl Session { d.undo.truncate(g.start); d.redo = g.redo; d.revision += 1; - // A step the rollback dropped is no longer anyone's to name. - if self.group_step.is_some_and(|(doc, n, _)| doc == g.doc && n > g.start) { - self.group_step = None; - } Ok(()) } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index b3397c724e..a598bb7c71 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -226,6 +226,27 @@ fn register_group_commands() { }); // A command that is one group keeps the group's name. register("test.onlyGroup", |s, _| s.grouped("Named Step", |s| put(s, "D1", "x")).map(|_| json!({}))); + // A group, then an edit the command makes itself (no nested command, so no undo entry). + register("test.groupThenEdit", |s, _| { + s.grouped("Named Step", |s| put(s, "D1", "x"))?; + crate::cmd::edit(s, |cx| { + let at = gridcraft_core::CellRef::parse("D2").ok_or_else(|| EngineError::Other("D2".into()))?; + cx.sheet_mut(0)?.set_value(at, gridcraft_core::Value::Text("y".into())); + cx.touch(0, at); + Ok(()) + })?; + Ok(json!({})) + }); + // A group with a change, then one that is rolled back: the step is still the first group's. + register("test.groupThenRolledBack", |s, _| { + s.grouped("Named Step", |s| put(s, "D1", "x"))?; + let failed: crate::Result<()> = s.grouped("Doomed", |s| { + put(s, "D3", "gone")?; + Err(EngineError::Other("no".into())) + }); + assert!(failed.is_err()); + Ok(json!({})) + }); // An edit, then the rollback of the innermost group that was open before the command. register("test.rollbackOpenGroup", |s, _| { put(s, "E1", "gone")?; @@ -266,9 +287,9 @@ fn a_command_that_runs_its_own_group_makes_one_step() { #[test] fn a_stale_group_step_does_not_name_a_later_command() { - // The record of the last group step (document, length, epoch) survives the Undo of that step. - // A later command that ends a group of its own at the same history length, after rolling - // another back, must not take the name of the nested edit it made. + // The record of the last group step survives the Undo of that step. A later command that + // ends a group of its own at the same history length, after rolling another back, must not + // take the name of the nested edit it made. register_group_commands(); let mut s = s(); s.execute("test.onlyGroup", json!({})).unwrap(); @@ -285,6 +306,27 @@ fn a_stale_group_step_does_not_name_a_later_command() { assert!(v(&s, "B2").is_empty() && v(&s, "D1").is_empty()); } +#[test] +fn a_command_keeps_its_groups_name_only_for_exactly_that_step() { + // The name comes from the group only when the group's step is the command's whole step: an + // edit after the group (no nested command, so the history length matches) makes it the + // command's own, while a later group that rolled back changes nothing. + register_group_commands(); + let mut s = s(); + s.execute("test.groupThenEdit", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["test.groupThenEdit"]); + assert_eq!((v(&s, "D1").display(), v(&s, "D2").display()), ("x".into(), "y".into())); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "D1").is_empty() && v(&s, "D2").is_empty()); + assert_eq!(undo_labels(&s), Vec::::new()); + s.execute("test.groupThenRolledBack", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["Named Step"]); + assert_eq!(v(&s, "D1").display(), "x"); + assert!(v(&s, "D3").is_empty()); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "D1").is_empty()); +} + #[test] fn a_group_rolled_back_inside_a_command_keeps_the_redo_history() { // The debugger's Stop with Undo Changes rolls its group back inside a command. The rollback From a8e8bf851e7d7996c5321f21f3ce42d1e1a97280 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:17:57 -0400 Subject: [PATCH 126/687] Engine: the recording buffer is bounded in bytes and keeps the start of a recording The buffer kept at most 100,000 steps but each step keeps its full parameters, and a file.open step holds the whole file as base64, so a recording of a few opens of a large workbook kept tens of MB alive. Like the journal, the buffer now also stops at 64 MB of steps serialized as JSON. A step that does not fit is counted as dropped, and once a step is dropped every later one is too, so what the buffer holds is always how the recording began, never a sequence with gaps. --- crates/engine/src/record.rs | 15 +++++++++++++-- crates/engine/src/tests/seams.rs | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/record.rs b/crates/engine/src/record.rs index dbbc1a8af7..fefd072347 100644 --- a/crates/engine/src/record.rs +++ b/crates/engine/src/record.rs @@ -13,6 +13,10 @@ use crate::Selection; /// The most steps one recording keeps; later ones are counted in [`Recorder::dropped`]. pub const RECORD_LIMIT: usize = 100_000; +/// The most bytes (steps serialized as JSON) one recording keeps. A step keeps its full +/// parameters so it can be played back, and a `file.open` holds the whole file as base64, so +/// without a size cap a recording of a few opens of a large workbook keeps tens of MB alive. +pub const RECORD_MAX_BYTES: usize = 64 * 1024 * 1024; /// One recorded command and where it ran. #[derive(Clone, Debug, PartialEq, Serialize)] @@ -34,14 +38,21 @@ pub struct RecordedStep { #[derive(Clone, Debug, Default)] pub struct Recorder { pub steps: Vec, - /// Steps not kept because the recording reached [`RECORD_LIMIT`]. + /// Steps not kept because the recording reached [`RECORD_LIMIT`] or [`RECORD_MAX_BYTES`]. + /// Once a step is dropped, every later one is too, so `steps` is always the beginning of + /// what ran, never a sequence with gaps. pub dropped: usize, + /// The size of `steps` serialized as JSON. + bytes: usize, } impl Recorder { /// Adds a step, or counts it as dropped once the buffer is full. pub fn push(&mut self, step: RecordedStep) { - if self.steps.len() < RECORD_LIMIT { + let size = serde_json::to_vec(&step).map_or(0, |v| v.len()); + let fits = self.steps.len() < RECORD_LIMIT && self.bytes.saturating_add(size) <= RECORD_MAX_BYTES; + if fits && self.dropped == 0 { + self.bytes = self.bytes.saturating_add(size); self.steps.push(step); } else { self.dropped = self.dropped.saturating_add(1); diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index a598bb7c71..c524b397d8 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -494,6 +494,31 @@ fn the_recorder_has_a_cap() { assert_eq!(rec.dropped, 3); } +#[test] +fn the_recorder_has_a_byte_cap_and_keeps_a_prefix() { + // A `file.open` step holds the whole file as base64: the buffer is bounded in bytes too, and + // once a step is dropped every later one is, so what is kept is always how the recording began. + let mut rec = crate::record::Recorder::default(); + let step = |n: usize| crate::record::RecordedStep { + doc: 1, + sheet: 0, + sheet_name: "Sheet1".into(), + selection: crate::Selection::default(), + id: "file.open".into(), + params: json!({"base64": "A".repeat(n)}), + }; + let big = crate::record::RECORD_MAX_BYTES / 2; + rec.push(step(big)); + assert_eq!((rec.steps.len(), rec.dropped), (1, 0)); + rec.push(step(big)); + assert_eq!((rec.steps.len(), rec.dropped), (1, 1), "the second does not fit beside the first"); + rec.push(step(1)); + assert_eq!((rec.steps.len(), rec.dropped), (1, 2), "a small step after a dropped one is dropped too"); + let mut rec = crate::record::Recorder::default(); + rec.push(step(crate::record::RECORD_MAX_BYTES + 1)); + assert_eq!((rec.steps.len(), rec.dropped), (0, 1), "a step larger than the whole budget is not kept"); +} + #[test] fn passwords_stay_out_of_the_recording_buffer() { const PW: &str = "synthetic-pw-5521"; From 524826687ccc8b9abb700e7c55c42f03c379a45a Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:17:57 -0400 Subject: [PATCH 127/687] Docs: the architecture revision row carries the date the change lands; document trust beside script trust The header and the revision row said 2026-10-11, older than the row above them. The document trust paragraph now says how DocState::trust (provenance) differs from DocState::scripts_trusted (per-session consent for stored scripts, #2178). --- docs/architecture.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index b656afc667..f457745081 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # Architecture -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (engine building blocks for multi-command actions) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (engine building blocks for multi-command actions) · **Target:** Microsoft Excel (Microsoft 365) How GridCraft is built today. Rules for contributors are in [`AGENTS.md`](../AGENTS.md). @@ -63,7 +63,9 @@ Actions made of many commands (macros, a debugger that runs one step per call) u (Automate › Record Actions still records through the journal). - **Document trust.** `DocState::trust` is `Local` for workbooks made in the session and `FromFile` for workbooks opened from a file, and for workbooks that received formulas or - validation pasted from one. Active content in a `FromFile` workbook asks before it runs. + validation pasted from one. Active content in a `FromFile` workbook asks before it runs. This + is provenance, which no decision changes; `DocState::scripts_trusted` is the separate + per-session consent for stored `automate.*` scripts (`automate.trustScripts`). - **Workbook data in hidden names** (`cmd/stash.rs`). An item is split over hidden workbook-scope names `_gc__0`, `_gc__1`, … of string literals within Excel's 255- and 8,192-character limits, so it survives an xlsx round trip. The older single-name `_sc_` @@ -94,6 +96,6 @@ rpm, tarball), FreeBSD, and web (WASM; WebGPU or WebGL2). Release workflows in | Date | Change | Summary | |---|---|---| +| 2026-10-23 | minor | Engine building blocks for multi-command actions: undo groups, recording buffer, document trust, chunked hidden-name storage | | 2026-10-14 | minor | Document Undo's retained-cell-storage budget and latest-step guarantee | -| 2026-10-11 | minor | Engine building blocks for multi-command actions: undo groups, recording buffer, document trust, chunked hidden-name storage | | 2026-10-10 | major | Created from the code on main: crates, layers, data flow, agent control, known gaps | From 5ca886507c91f8de8ef90a3743e9be299ea6f62c Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:21:48 -0400 Subject: [PATCH 128/687] Engine: writing a stash item checks for a clashing visible name in one pass over the names The check ran once per chunk, each a scan of every defined name: an item of many chunks in a workbook with thousands of names compared every pair. One pass now parses each visible workbook-scope name and refuses when it is a chunk of this key below the chunk count. The behaviour and its test are unchanged. --- crates/engine/src/cmd/stash.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index 9043262d22..683c42c2dd 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -65,14 +65,17 @@ pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { } let formulas = chunk_formulas(text); // A visible name of the item's shape is someone's own (see `item_of`); a hidden name beside - // it with the same name would be the one an xlsx file drops. Refuse before changing anything. - for i in 0..formulas.len() { - let name = format!("{PREFIX}{key}_{i}"); - if wb.names.iter().any(|n| n.scope.is_none() && !n.hidden && n.name.eq_ignore_ascii_case(&name)) { - return Err(EngineError::Other(format!( - "The workbook has a defined name '{name}', which GridCraft needs for its own data. Rename it first." - ))); - } + // it with the same name would be the one an xlsx file drops. Refuse before changing anything, + // in one pass over the names (a file can carry many thousands). + let chunks = formulas.len() as u64; + let taken = |n: &&DefinedName| { + n.scope.is_none() && !n.hidden && parse_name(&n.name).is_some_and(|(k, i)| k.eq_ignore_ascii_case(key) && i.is_some_and(|i| i < chunks)) + }; + if let Some(n) = wb.names.iter().find(taken) { + return Err(EngineError::Other(format!( + "The workbook has a defined name '{}', which GridCraft needs for its own data. Rename it first.", + n.name + ))); } remove(wb, key); for (i, formula) in formulas.into_iter().enumerate() { From 114088d589bf661ecff010a9aa162470bbd2e11e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:33:42 -0400 Subject: [PATCH 129/687] Engine: a command's changes after ending a group, or before opening one it leaves open, are not lost or a step of their own Two boundaries of the group rules were wrong. A command that ended a group open before it was treated as grouped whatever it did next: with an unchanged group (no step) its later edit had no undo entry at all, and with nested edits after the end the entry was the nested command's. The group's step now stays the group's, and the command makes one step of its own, under its name, for what changed after the end, from the workbook the group ended at. A command that edited and then opened a group it leaves open (a debugger's start) left its nested entries in the history, so one Undo after the group ended brought back a state from inside the command. Such a group now takes the command's start as its own, and its step covers the command's earlier edits. The record of where the last group ended now holds the workbook and selection it ended at, the history length then and whether it made a step; it lives until the outermost command finishes. A rollback records where it ended too, unless the document is back exactly where the last recorded step left it. --- crates/engine/src/lib.rs | 73 ++++++++++++++++++++++++-------- crates/engine/src/tests/seams.rs | 63 +++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 588a8a2a2b..b44ed08521 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -97,15 +97,19 @@ struct OpenGroup { redo: Vec, } -/// The undo step the last [`Session::group_end`] made: its document and the workbook it led to. -/// The entry itself holds the workbook it started from, so a command can check that the step is -/// exactly its own (the same start and end as the command's) and keep the group's name for it. -/// A weak reference: the record keeps no workbook alive, and a workbook that is gone can't be -/// a command's result. +/// Where the last [`Session::group_end`] or [`Session::group_rollback`] left its document: the +/// workbook and selection it ended at, the undo length then, and whether it made a step. A +/// command that ended the group tells its own later changes from the group's step by it, and +/// keeps the group's name only when the group's step is exactly its own (the entry holds the +/// workbook the step started from). Dropped when the outermost command finishes, so it keeps no +/// workbook alive between commands. #[derive(Debug)] struct GroupStep { doc: u64, - after: std::sync::Weak, + after: Arc, + selection: Selection, + len: usize, + stepped: bool, } /// The clipboard: a block of cells (with formulas and styles) or marching-ants reference. @@ -416,8 +420,7 @@ pub struct Session { next_group: u64, /// Counts group begins and ends, so `execute` can tell that a command opened or closed one. group_epoch: u64, - /// The step the last `group_end` made, so `execute` can tell that a command's step is exactly - /// its group's. + /// Where the last group ended, so `execute` can tell a command's own changes from its group's. group_step: Option, /// Measures cell text for AutoFit Column Width. Without it (CLI, MCP) AutoFit uses /// [`cmd::format::approx_text_width`]. @@ -549,6 +552,7 @@ impl Session { } if self.depth == 0 { self.trim_history(); + self.group_step = None; } return Err(EngineError::Internal(id.to_string(), msg)); } @@ -563,21 +567,44 @@ impl Session { } self.push_journal(id, cmd::journal_params(¶ms)); } - // Inside an undo group, or when the command ended one that was open before it, the group - // owns the step. A command that opened and closed its own groups makes one step of its - // own, which replaces the steps those groups made. + // Inside an undo group the group owns the step. A command that ended a group open before + // it leaves the group its step and makes one of its own for what it changed after the end. + // A command that opened and closed its own groups makes one step of its own, which + // replaces the steps those groups made. let epoch_changed = self.group_epoch != group_epoch; let in_group_now = before.as_ref().is_some_and(|(uid, ..)| self.in_group(*uid)); - let own_groups = epoch_changed && !in_group_now && start.is_some_and(|(was_open, _)| !was_open); - let grouped = !own_groups && (epoch_changed || in_group_now); + let was_open = start.is_some_and(|(was_open, _)| was_open); if spec.undoable && let Some((uid, wb, sel)) = before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == uid) && !Arc::ptr_eq(&d.wb, &wb) { - if !grouped { - let mut label = spec.label.trim_end_matches('…').to_string(); - if own_groups && let Some((_, len)) = start { + let label = spec.label.trim_end_matches('…').to_string(); + if in_group_now { + // A group this command opened and left open (a debugger's start) takes the + // command's start as its own, so what the command changed before opening it is + // part of the group's step and not a step of its own. + if !was_open + && let Some((_, len)) = start + && let Some(g) = self.groups.iter_mut().filter(|g| g.doc == uid && g.id >= first_group).min_by_key(|g| g.id) + { + g.start = len; + g.wb = wb; + g.selection = sel; + } + } else if epoch_changed && was_open { + // The group it ended made its step; changes after the end, whether direct or by + // nested commands, are the command's own step from where the group ended. + if let Some(step) = self.group_step.as_ref().filter(|g| g.doc == uid) + && (d.undo.len() > step.len || !Arc::ptr_eq(&step.after, &d.wb)) + { + d.undo.truncate(step.len); + push_entry(&mut d.undo, label, step.after.clone(), &d.wb, step.selection.clone()); + d.redo.clear(); + } + } else { + let mut label = label; + if epoch_changed && let Some((_, len)) = start { // A command whose only step is its group's keeps that name ("Run Macro: Totals"). // That is so only when the one entry the command added is a group's step from // the command's own start to its own result: an entry a nested command made, @@ -586,7 +613,7 @@ impl Session { if d.undo.len() == len + 1 && let Some(last) = d.undo.last() && Arc::ptr_eq(&last.wb, &wb) - && self.group_step.as_ref().is_some_and(|g| g.doc == uid && g.after.upgrade().is_some_and(|after| Arc::ptr_eq(&after, &d.wb))) + && self.group_step.as_ref().is_some_and(|g| g.doc == uid && g.stepped && Arc::ptr_eq(&g.after, &d.wb)) { label = last.label.clone(); } @@ -603,6 +630,7 @@ impl Session { // lengths it compared stay true. if self.depth == 0 { self.trim_history(); + self.group_step = None; } result } @@ -660,10 +688,13 @@ impl Session { let changed = !Arc::ptr_eq(&d.wb, &g.wb); if changed && !outer { push_entry(&mut d.undo, g.label, g.wb, &d.wb, g.selection); - self.group_step = Some(GroupStep { doc: g.doc, after: Arc::downgrade(&d.wb) }); d.redo.clear(); d.revision += 1; } + if !outer { + self.group_step = + Some(GroupStep { doc: g.doc, after: d.wb.clone(), selection: d.selection.clone(), len: d.undo.len(), stepped: changed }); + } // Inside a command, the outermost command applies the history limit when it finishes. if self.depth == 0 { self.trim_history(); @@ -693,6 +724,12 @@ impl Session { d.undo.truncate(g.start); d.redo = g.redo; d.revision += 1; + // The record of the last group step stays when the document is back exactly where that + // step left it (this group began there); otherwise the rollback is where the group ended. + let kept = self.group_step.as_ref().is_some_and(|st| st.doc == g.doc && st.len == g.start && Arc::ptr_eq(&st.after, &d.wb)); + if !kept && !self.groups.iter().any(|o| o.doc == g.doc) { + self.group_step = Some(GroupStep { doc: g.doc, after: d.wb.clone(), selection: d.selection.clone(), len: g.start, stepped: false }); + } Ok(()) } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index c524b397d8..1f83bd67d6 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -261,6 +261,20 @@ fn register_group_commands() { s.group_end(token.ok_or_else(|| EngineError::Other("no group".into()))?)?; Ok(json!({})) }); + // The end of the innermost group that was open before the command, then an edit. + register("test.endOpenGroupThenEdit", |s, _| { + let token = s.groups.last().map(|g| crate::GroupToken { id: g.id, doc: g.doc }); + s.group_end(token.ok_or_else(|| EngineError::Other("no group".into()))?)?; + put(s, "E1", "after")?; + Ok(json!({})) + }); + // An edit, then a group the command leaves open (a debugger's start). + register("test.editThenOpenGroup", |s, _| { + put(s, "A1", "first")?; + let _open = s.group_begin("Debug")?; + put(s, "A2", "second")?; + Ok(json!({})) + }); } #[test] @@ -327,6 +341,55 @@ fn a_command_keeps_its_groups_name_only_for_exactly_that_step() { assert!(v(&s, "D1").is_empty()); } +#[test] +fn what_a_command_changes_after_ending_a_group_is_its_own_step() { + // Ending a group that was open before the command leaves the group its step (if it made one) + // and makes a step of the command's own for what it changed after the end. + register_group_commands(); + let mut s = s(); + // The group made no step: the command's edit must still be undoable, under its own name. + let _unchanged = s.group_begin("Debug").unwrap(); + s.execute("test.endOpenGroupThenEdit", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["test.endOpenGroupThenEdit"]); + assert_eq!(v(&s, "E1").display(), "after"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "E1").is_empty()); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(v(&s, "E1").display(), "after"); + // The group made a step: two steps, the group's and the command's. + s.execute("edit.undo", json!({})).unwrap(); + let _changed = s.group_begin("Debug").unwrap(); + set(&mut s, "B1", "inside"); + s.execute("test.endOpenGroupThenEdit", json!({})).unwrap(); + assert_eq!(undo_labels(&s), ["Debug", "test.endOpenGroupThenEdit"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "E1").is_empty()); + assert_eq!(v(&s, "B1").display(), "inside"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "B1").is_empty()); +} + +#[test] +fn a_group_a_command_leaves_open_starts_where_the_command_did() { + // A command that edits and then opens a group it leaves open has no undo step of its own: + // the group's step, made when it ends, covers the command's edit too, so no state from + // inside the command can come back. + register_group_commands(); + let mut s = s(); + set(&mut s, "C1", "base"); + s.execute("test.editThenOpenGroup", json!({})).unwrap(); + let d = s.doc().unwrap(); + assert!(s.in_group(d.uid)); + let token = s.groups.last().map(|g| crate::GroupToken { id: g.id, doc: g.doc }).unwrap(); + assert!(s.group_end(token).unwrap()); + assert_eq!(undo_labels(&s), ["Enter Cell", "Debug"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "A1").is_empty() && v(&s, "A2").is_empty()); + assert_eq!(v(&s, "C1").display(), "base"); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!((v(&s, "A1").display(), v(&s, "A2").display()), ("first".into(), "second".into())); +} + #[test] fn a_group_rolled_back_inside_a_command_keeps_the_redo_history() { // The debugger's Stop with Undo Changes rolls its group back inside a command. The rollback From 6ab8d97118a89eed61c289c333a00693c59e35e8 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:35:45 -0400 Subject: [PATCH 130/687] Engine: the undo steps made inside a command are bounded while it runs The history limit is applied once the outermost command has finished, so the lengths its bookkeeping compares stay true (round 3). That left the steps nested commands make unbounded until then: a script of up to 100,000 steps, or a subtotal with a step per group, kept every one of them alive, each with the cells it retains, and before this branch every push trimmed the history at once. Each nested command now bounds the steps made since the latest mark on its document to the undo limit and byte budget, dropping the oldest of them. The marks are the starts of the running commands, the starts of the open groups and the end of the last group: the positions a command or group collapses its steps by when it finishes, so nothing before them moves. What the bound drops is only what the final trim would drop. --- crates/engine/src/lib.rs | 31 ++++++++++++++++++++++++++++++- crates/engine/src/tests/seams.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b44ed08521..6e37058dd6 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -422,6 +422,10 @@ pub struct Session { group_epoch: u64, /// Where the last group ended, so `execute` can tell a command's own changes from its group's. group_step: Option, + /// For each running command, innermost last: its document and that document's undo length + /// when it began. Steps before that length are not touched while the command runs (see + /// [`Session::bound_tail`]). + marks: Vec<(u64, usize)>, /// Measures cell text for AutoFit Column Width. Without it (CLI, MCP) AutoFit uses /// [`cmd::format::approx_text_width`]. pub text_width: Option, @@ -533,9 +537,15 @@ impl Session { Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), None => (0, 0, String::new(), Selection::default()), }); + let doc_uid = before.as_ref().map(|(uid, ..)| *uid); + let marked = doc_uid.zip(start).map(|(uid, (_, len))| (uid, len)); + self.marks.extend(marked); self.depth = self.depth.saturating_add(1); let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, ¶ms))); self.depth = self.depth.saturating_sub(1); + if marked.is_some() { + self.marks.pop(); + } let result = match result { Ok(r) => r, Err(p) => { @@ -627,10 +637,12 @@ impl Session { d.revision += 1; } // The history limit applies once the outermost command has collapsed its steps, so the - // lengths it compared stay true. + // lengths it compared stay true. Meanwhile the steps made inside it are bounded on their own. if self.depth == 0 { self.trim_history(); self.group_step = None; + } else if let Some(uid) = doc_uid { + self.bound_tail(uid); } result } @@ -702,6 +714,23 @@ impl Session { Ok(changed) } + /// Bounds the undo steps pushed while a command runs (a script of many steps, a subtotal per + /// group): at most [`UNDO_LIMIT`] of them within [`UNDO_BYTES_LIMIT`], dropping the oldest. + /// Only steps after the latest mark go: a running command's start, an open group's start and + /// the end of the last group are positions the command or group collapses its steps by when + /// it finishes, so what is before them stays as it is until [`Session::trim_history`]. + fn bound_tail(&mut self, uid: u64) { + let Some(d) = self.docs.iter_mut().find(|d| d.uid == uid) else { return }; + let marks = self.marks.iter().filter(|(doc, _)| *doc == uid).map(|(_, len)| *len); + let groups = self.groups.iter().filter(|g| g.doc == uid).map(|g| g.start); + let step = self.group_step.as_ref().filter(|g| g.doc == uid).map(|g| g.len); + let mark = marks.chain(groups).chain(step).max().unwrap_or(0).min(d.undo.len()); + let mut bytes = d.undo.iter().skip(mark).fold(0usize, |sum, e| sum.saturating_add(e.retained_bytes)); + while d.undo.len() > mark + 1 && (d.undo.len() - mark > UNDO_LIMIT || bytes > UNDO_BYTES_LIMIT) { + bytes = bytes.saturating_sub(d.undo.remove(mark).retained_bytes); + } + } + /// Drops the oldest undo steps past [`UNDO_LIMIT`] or [`UNDO_BYTES_LIMIT`]. A document with /// an open group waits for the group to end, because the group remembers where its steps start. fn trim_history(&mut self) { diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 1f83bd67d6..3216cfd97a 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -268,6 +268,19 @@ fn register_group_commands() { put(s, "E1", "after")?; Ok(json!({})) }); + // Many nested edits: the steps they make are bounded while the command runs. (Not undoable + // itself: like a script, its nested steps are the undo steps.) + crate::cmd::register_test_command("test.manyNestedEdits", |s, _| { + let start = s.doc()?.undo.len(); + for i in 0..crate::UNDO_LIMIT + 50 { + put(s, "A1", &i.to_string())?; + } + let len = s.doc()?.undo.len(); + if len > start + crate::UNDO_LIMIT { + return Err(EngineError::Other(format!("{} steps kept inside the command", len - start))); + } + Ok(json!({})) + }); // An edit, then a group the command leaves open (a debugger's start). register("test.editThenOpenGroup", |s, _| { put(s, "A1", "first")?; @@ -390,6 +403,25 @@ fn a_group_a_command_leaves_open_starts_where_the_command_did() { assert_eq!((v(&s, "A1").display(), v(&s, "A2").display()), ("first".into(), "second".into())); } +#[test] +fn steps_made_inside_a_command_are_bounded_while_it_runs() { + // The history limit applies when the outermost command finishes, so a command's bookkeeping + // holds; but a command that runs thousands of nested commands (a script, a subtotal) must not + // keep thousands of steps alive until then. Each nested step bounds the steps made since the + // command began, and leaves the history from before it alone. + register_group_commands(); + let mut s = s(); + let before = fill_history(&mut s); + s.execute("test.manyNestedEdits", json!({})).unwrap(); + let d = s.doc().unwrap(); + assert_eq!(d.undo.len(), crate::UNDO_LIMIT); + assert!(d.undo.iter().all(|e| e.label == "Enter Cell")); + assert!(!d.undo.iter().any(|e| before.iter().any(|w| std::sync::Arc::ptr_eq(w, &e.wb))), "the older history gave way to the newest steps"); + assert_eq!(v(&s, "A1").display(), "149"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(v(&s, "A1").display(), "148"); +} + #[test] fn a_group_rolled_back_inside_a_command_keeps_the_redo_history() { // The debugger's Stop with Undo Changes rolls its group back inside a command. The rollback From 080b6d993b1682010a613ca41389dc953a22e6b1 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:36:34 -0400 Subject: [PATCH 131/687] Engine: a grouped action that closes its document succeeds Session::grouped ended its group after the action, and when the action had closed the group's document (file.close, or file.open taking the place of an untouched new workbook) the group had gone with it, so the action was reported as failed although it had done what it was asked. A group that is gone because its document is gone now counts as ended; a group ended by someone else while the document is open is still an error. --- crates/engine/src/lib.rs | 12 ++++++++---- crates/engine/src/tests/seams.rs | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 6e37058dd6..ad740e3f63 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -652,11 +652,15 @@ impl Session { /// is rolled back and its error is returned. Groups nest; the outermost one makes the step. pub fn grouped(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result) -> Result { let token = self.group_begin(label)?; + let doc = token.doc(); match f(self) { - Ok(r) => { - self.group_end(token)?; - Ok(r) - } + Ok(r) => match self.group_end(token) { + Ok(_) => Ok(r), + // `f` closed the group's document (`file.close`, or `file.open` taking the place of + // an untouched new workbook), and the group went with it: nothing is left to undo. + Err(_) if !self.docs.iter().any(|d| d.uid == doc) => Ok(r), + Err(e) => Err(e), + }, Err(e) => { // The error from `f` is the one to report. A rollback fails only when the group // has already ended, and then there is nothing left to undo. diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 3216cfd97a..a5cd0d8685 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -170,6 +170,27 @@ fn a_long_group_keeps_the_history_before_it() { assert_eq!(d.undo.iter().filter(|e| e.label == "Enter Cell").count(), 99); } +#[test] +fn a_grouped_action_that_closes_its_document_succeeds() { + // The group goes with the document; the action did what it was asked. + let mut s = s(); + let uid = s.doc().unwrap().uid; + s.grouped("Close", |s| s.execute("file.close", json!({}))).unwrap(); + assert!(s.docs.iter().all(|d| d.uid != uid) && !s.in_group(uid)); + // Opening a file into an untouched new workbook takes its place the same way. + s.new_workbook(); + let uid = s.doc().unwrap().uid; + let bytes = serde_json::to_vec(&gridcraft_model::Workbook::new()).unwrap(); + s.grouped("Open", |s| s.execute("file.open", json!({"name": "theirs.json", "base64": crate::io::base64_encode(&bytes)}))).unwrap(); + assert!(s.docs.iter().all(|d| d.uid != uid) && !s.in_group(uid) && s.groups.is_empty()); + // A group ended by someone else while its document is still open is still an error. + let err = s.grouped("Ended", |s| { + let token = s.groups.last().map(|g| crate::GroupToken { id: g.id, doc: g.doc }); + s.group_end(token.ok_or_else(|| EngineError::Other("no group".into()))?).map(|_| ()) + }); + assert!(err.unwrap_err().to_string().contains("already ended")); +} + #[test] fn closing_a_document_drops_its_groups() { let mut s = s(); From 7e383f460a069e2eb44140d9026d46351815e0d7 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:36:52 -0400 Subject: [PATCH 132/687] Docs: undo groups at a command's boundaries, and the bound on steps made inside a command --- docs/architecture.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/architecture.md b/docs/architecture.md index f457745081..e44e800839 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -56,7 +56,10 @@ Actions made of many commands (macros, a debugger that runs one step per call) u While a group is open on a document, its commands add no undo steps of their own and Undo and Redo are disabled there. Groups nest; the outermost one makes the step. A command that opens and closes its own groups still makes one undo step for itself, covering its edits inside and outside - the groups. + the groups. A command that ends a group open before it leaves the group its step and makes one + of its own for what it changes after the end; a group a command opens and leaves open starts + where the command did. The history limit is applied when the outermost command finishes; until + then the steps made inside it are bounded on their own. - **Recording buffer** (`record.rs`). `Session::start_recording` collects each successful top-level command with the document, sheet and selection it ran in, without secret parameters. Commands run by other commands are not recorded, and the buffer is independent of the journal From 2329507a8e14e31fd559d44035329052d4768d28 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 23 Oct 2026 16:51:16 -0400 Subject: [PATCH 133/687] ci(sonar): don't cancel superseded runs on main, so coverage is published --- .github/workflows/sonarcloud.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index ecee45a6d8..e78fe1ea3b 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -11,7 +11,8 @@ permissions: concurrency: group: sonarcloud-${{ github.ref }} - cancel-in-progress: true + # A push to main is the run that publishes coverage; superseded runs there would never finish. + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} jobs: sonarcloud: From 1ce4781d3e059e14696cd4a351f61e4242062767 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:24:18 -0500 Subject: [PATCH 134/687] Accuracy: PMT to Excel's last bits via ln_1p/exp_m1 PMT computed (1 + rate)^nper with powf, which rounds 1 + rate first and drops the low bits of a small rate. Over 400 synthetic probes that left PMT up to about 1000 ULPs from Excel (59 bit-exact); the ln_1p/exp_m1 form is within 4 ULPs (244 bit-exact). A loan schedule guarded by IF(p - SUM(principal) > 0, ...) turns on those bits: in 3000 synthetic schedules main disagreed with Excel on the number of live periods in 1382, the fix in 823. Also follows Excel at the edges: a rate of -1 or less is #NUM!, a huge nper gives -pv*rate instead of #NUM!, a tiny rate no longer collapses, and a small negative rate keeps its payment. IPMT, PPMT, CUMIPMT and CUMPRINC pick this up through pmt_calc. FV and PV are unchanged: Excel's own FV/PV sit further from the exact value than the current powf form does. The tests allow the one-ULP libm difference between platforms. --- crates/calc/src/tests.rs | 37 ++++++++++++++++++++ crates/functions/src/financial.rs | 58 +++++++++++++++++++++++++++++-- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 622ed6cc9e..e71015213c 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -1781,3 +1781,40 @@ fn text_before_after_empty_instance_defaults_to_one() { t.set("A4", "=TEXTBEFORE(\"a-b\",\"-\",0)"); assert_eq!(t.get("A4"), Value::Error(CellError::Value)); } + +#[test] +fn loan_schedule_residue_keeps_excels_sign() { + // A synthetic monthly loan schedule: interest on the opening balance, principal = PMT - interest, + // each later period guarded by IF(p - SUM(principal so far) > 0, ...). After the 12th payment + // Excel is left with 2^-30 > 0, so it takes a 13th, tiny-interest period. That last bit comes + // from PMT; the old (1 + rate)^nper form left -2.8e-8 and returned "" instead. + let mut t = T::new(); + t.set("B1", "4653300"); + t.set("B2", "1"); + t.set("B3", "0.141"); + let col = |i: u8| char::from(b'A' + i).to_string(); + t.set("C8", "=$B$1*($B$3/12)"); + t.set("C7", "=PMT($B$3/12,$B$2*12,-$B$1)-C8"); + t.set("D8", "=($B1-C7)*($B$3/12)"); + t.set("D7", "=PMT($B$3/12,$B$2*12,-$B$1)-D8"); + for i in 4..16 { + let (c, p) = (col(i), col(i - 1)); + t.set(&format!("{c}8"), &format!("=IF($B1-SUM($C7:{p}7)>0,($B1-SUM($C7:{p}7))*($B$3/12),\"\")")); + t.set(&format!("{c}7"), &format!("=IF($B1-SUM($C7:{p}7)>0,PMT($B$3/12,$B$2*12,-$B$1)-{c}8,\"\")")); + } + t.set("N4", "=IF($B$1-SUM($C7:N7)>0,$B$1-SUM($C7:N7),\"\")"); + // Excel's values (Value2) for the same synthetic inputs. + // The leftover (2^-30 in Excel) is 12 payments' worth of rounding, so a one-ULP libm difference + // moves it by as much as its own size: only its sign is checked, and the 13th period must exist. + let positive = |cell: &str| match t.get(cell) { + Value::Number(x) => assert!(x > 0.0 && x < 1e-6, "{cell}: {x}"), + other => panic!("{cell}: expected a small positive number, got {other:?}"), + }; + positive("N4"); + positive("O8"); + match t.get("O7") { + Value::Number(x) => assert!((x - 418025.37230211904).abs() < 1e-3, "O7: {x}"), + other => panic!("O7: expected a number, got {other:?}"), + } + assert_eq!(t.get("P7"), Value::Text("".into())); +} diff --git a/crates/functions/src/financial.rs b/crates/functions/src/financial.rs index af28326e7a..043c09ed67 100644 --- a/crates/functions/src/financial.rs +++ b/crates/functions/src/financial.rs @@ -13,12 +13,31 @@ use crate::{Arg, Ctx, FnSpec, VAR}; // Time value of money // --------------------------------------------------------------------------------------------- +/// The payment per period. `(1 + rate)^nper` comes from `nper * ln_1p(rate)` and `p - 1` from +/// `exp_m1`: rounding `1 + rate` first throws away the low bits of a small rate, which left PMT up to +/// about 1000 ULPs from Excel (observed over 400 synthetic probes; this form is within 4). A loan +/// schedule's last `IF(p - SUM(principal) > 0, ...)` turns on those bits. Excel gives #NUM! for a +/// rate of -1 or less, and -pv*rate (no divide) once `(1 + rate)^nper` overflows. fn pmt_calc(rate: f64, nper: f64, pv: f64, fv: f64, typ: f64) -> f64 { if rate == 0.0 { return -(pv + fv) / nper; } - let p = (1.0 + rate).powf(nper); - -(rate * (fv + pv * p)) / ((1.0 + rate * typ) * (p - 1.0)) + if rate <= -1.0 { + return f64::NAN; + } + let l = nper * rate.ln_1p(); + let m = l.exp_m1(); + let k = 1.0 + rate * typ; + if m > 0.0 { + // (fv + pv*p) / (p - 1) = pv + (pv + fv) / (p - 1), without the cancellation in p - 1. + -(pv + (pv + fv) / m) * rate / k + } else if m > -0.5 { + // p near 1 from below: fv + pv*p = (pv + fv) + pv*m keeps the small terms that rounding p would drop. + -(rate * (pv + fv + pv * m)) / (k * m) + } else { + // p well below 1: m nears -1 and pv*m would swallow pv + fv, so use p itself. + -(rate * (fv + pv * l.exp())) / (k * m) + } } fn fv_calc(rate: f64, nper: f64, pmt: f64, pv: f64, typ: f64) -> f64 { @@ -1790,6 +1809,41 @@ mod tests { is_err(ev("CUMIPMT", vec![n(0.01), n(360.0), n(125000.0), n(1.0), n(1.0), n(2.0)]), CellError::Num); } + /// PMT within about 4 ULPs of Excel's (Value2 of synthetic probes; libm differs by an ULP across + /// platforms). The old `(1 + rate)^nper` form was 25 to 91 ULPs off on these, enough to flip a loan schedule's final `IF(p - SUM(r) > 0, ...)`. + #[test] + fn pmt_matches_excel_within_a_few_ulps() { + for (rate, nper, pv, fv, typ, excel) in [ + (0.141 / 12.0, 12.0, -4653300.0, 0.0, 0.0, 418025.37230211904), + (0.005716666666666666, 26.0, 42678411.0, -26010.62, 0.0, -1770235.3828507778), + (0.012164, 29.0, 16722649.0, 0.0, 1.0, -679516.0784762775), + (0.009916666666666666, 5.0, 8590842.8, 0.0, 0.0, -1769620.32624324), + (0.011158333333333333, 36.0, 21457108.0, 49218.26, 1.0, -720095.3912155787), + (-0.5, 2.5, 100.0, 0.0, 0.0, -10.736861692729645), + ] { + let got = ev("PMT", vec![n(rate), n(nper), n(pv), n(fv), n(typ)]); + close_tol(got, excel, 1e-15); + } + } + + /// Edge cases observed in Excel: a rate of -1 or less is #NUM!, a huge nper leaves -pv*rate, and a + /// tiny rate no longer loses its low digits in 1 + rate. + #[test] + fn pmt_edge_cases_follow_excel() { + is_err(ev("PMT", vec![n(-2.0), n(3.0), n(100.0)]), CellError::Num); + is_err(ev("PMT", vec![n(-1.0), n(3.0), n(100.0)]), CellError::Num); + is_err(ev("PMT", vec![n(-2.0), n(2.5), n(100.0)]), CellError::Num); + assert_eq!(ev("PMT", vec![n(0.1), n(10000.0), n(100.0)]), Value::Number(-10.0)); + assert_eq!(ev("PMT", vec![n(5.0), n(1000.0), n(100.0)]), Value::Number(-500.0)); + assert_eq!(ev("PMT", vec![n(1e-300), n(10.0), n(100.0)]), Value::Number(-10.0)); + close_tol(ev("PMT", vec![n(1e-12), n(10.0), n(100.0)]), -10.000000000055001, 1e-15); + close_tol(ev("PMT", vec![n(-0.999), n(5.0), n(100.0)]), -9.990_000_000_000_092e-14, 1e-15); + close_tol(ev("PMT", vec![n(0.05), n(-10.0), n(100.0)]), 7.950_457_496_545_667, 1e-15); + close_tol(ev("PMT", vec![n(-1e-17), n(1.0), n(1e18), n(-1e18)]), 10.0, 1e-9); + close_tol(ev("PMT", vec![n(-0.5), n(60.0), n(1e18)]), -0.433_680_868_994_201_8, 1e-6); + close_tol(ev("PMT", vec![n(0.05), n(10.0), n(100.0), n(50.0), n(1.0)]), -16.119701185541427, 1e-15); + } + #[test] fn cash_flows() { close_tol(ev("NPV", vec![n(0.1), n(-10000.0), n(3000.0), n(4200.0), n(6800.0)]), 1188.443412, 1e-9); From 2c052d03d51ba6da710648ca3d7c9babbcc35fa4 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:32:15 -0500 Subject: [PATCH 135/687] Read cell formatting from .xls files .xls (Excel 97-2003) import used only each cell's number format. It now also reads the BIFF8 formatting records (Font, XF, Palette and Style), so opened sheets keep their fonts, solid and pattern fills, borders (including diagonals), alignment (rotation, indent, wrap, shrink), locked and hidden protection, the workbook's custom colour palette, its Normal font as the default, and its user-defined cell styles. The decoding lives in a new xls/styles.rs; xls/mod.rs gains the record arms and the build call. Short or malformed formatting records keep the fields present and add one warning instead of refusing the workbook. Tests build the records in code, including truncation and byte mutation. Also merges origin/main, keeping its dialog-sheet, WINDOW2 and XF number-format handling; the XF arm now goes through the new styles module, which covers the number format too. --- ROADMAP.md | 2 + crates/xlsx/src/tests/mod.rs | 1 + crates/xlsx/src/tests/xls_style_tests.rs | 322 +++++++++++++++++++ crates/xlsx/src/xls/mod.rs | 45 ++- crates/xlsx/src/xls/styles.rs | 390 +++++++++++++++++++++++ docs/file-format-parity.md | 7 +- docs/gaps.md | 4 +- 7 files changed, 741 insertions(+), 30 deletions(-) create mode 100644 crates/xlsx/src/tests/xls_style_tests.rs create mode 100644 crates/xlsx/src/xls/styles.rs diff --git a/ROADMAP.md b/ROADMAP.md index 1faf680ca3..4c22fe0770 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -148,6 +148,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-15 | Function help follows the interface language: argument hints, autocomplete, Insert Function (search and readable categories) and the Formulas-tab menus read per-language tables of descriptions and argument names; Spanish keeps its 10 functions in the new format | | 2026-10-15 | Row auto-fit counts a cell's line breaks only when the cell wraps, as Excel does: a file whose unwrapped cells hold multi-line text no longer opens with tall rows (typing a break still turns wrap on and grows the row) | | 2026-10-14 | Protect Sheet on a protected sheet: Home › Format, its keytips (HOP, RPS) and the tab menu now read Unprotect Sheet… and unprotect (straight away without a password); a protected sheet that forbids selecting locked (or unlocked) cells is enforced for clicks, arrows and Go To, and the Protect Sheet dialog has the select options (#1346) | +| 2026-10-14 | `.xls` (Excel 97-2003) files open with their formatting: fonts, solid and pattern fills, borders (with diagonals), alignment (rotation, indent, wrap, shrink), locked and hidden cells, the workbook's custom colour palette, its Normal font and its user-defined cell styles | | 2026-10-14 | XLSX: formulas opened with dynamic-array metadata (`cm`) keep it on save, with `xl/metadata.xml`. Before, a formula returning one value lost it (11 of 40 corpus files that have the part lost it entirely), and Excel recalculated some of them with implicit intersection (`SUMPRODUCT(IFERROR(...))` changed value) | | 2026-10-14 | Recalculation: a dynamic array's anchor given a new formula that reads its old spill area (`A1` was `=SEQUENCE(3)`, becomes `=SUM(A2:A3)+1` or `=SEQUENCE(1,A2+1)`) sees that area empty instead of the old values | | 2026-10-14 | Recalculation: when two arrays of one recalculation want the same empty cells, the formulas that read the one laid out second (at its anchor or over the cells it would have spilled into) read `#SPILL!` and empty cells instead of its values | @@ -230,6 +231,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-16 | minor | Progress log: floating pictures' textures bounded and kept per workbook | | 2026-10-16 | trivial | Progress log: web preferences kept between visits (#1562) | | 2026-10-16 | minor | Progress log: interface fonts held once instead of twice | +| 2026-10-14 | minor | Progress log: `.xls` import reads cell formatting | | 2026-10-14 | minor | XLSX dynamic-array `cm` metadata kept on save; progress log row | | 2026-10-14 | minor | Progress log: spill anchor reading its old spill area | | 2026-10-14 | minor | Progress log: formulas reading an array that loses a spill conflict | diff --git a/crates/xlsx/src/tests/mod.rs b/crates/xlsx/src/tests/mod.rs index c8f199a47a..754525f148 100644 --- a/crates/xlsx/src/tests/mod.rs +++ b/crates/xlsx/src/tests/mod.rs @@ -25,6 +25,7 @@ mod shared_media; mod sheet_names; mod smooth_roundtrip; mod trendline_roundtrip; +mod xls_style_tests; mod xls_tests; mod xlsb_tests; mod zip_order; diff --git a/crates/xlsx/src/tests/xls_style_tests.rs b/crates/xlsx/src/tests/xls_style_tests.rs new file mode 100644 index 0000000000..d944e89155 --- /dev/null +++ b/crates/xlsx/src/tests/xls_style_tests.rs @@ -0,0 +1,322 @@ +//! Legacy `.xls` (BIFF8) cell formatting: Font, XF, Palette and Style records. Every workbook is +//! assembled here from the [MS-XLS] structures; no Excel-produced file is used. + +use gridcraft_core::{CellRef, Value}; +use gridcraft_model::style::Protection; +use gridcraft_model::{BorderLine, BorderStyle, Color, Fill, HAlign, PatternType, Style, StyleId, Underline, VAlign, VertAlign, Workbook}; + +use super::xls_tests::compound; +use crate::tables::INDEXED; +use crate::{read_xls, read_xlsx, write_xlsx}; + +fn rec(id: u16, payload: &[u8]) -> Vec { + let mut v = id.to_le_bytes().to_vec(); + v.extend_from_slice(&(payload.len() as u16).to_le_bytes()); + v.extend_from_slice(payload); + v +} + +fn u16s(v: &[u16]) -> Vec { + v.iter().flat_map(|x| x.to_le_bytes()).collect() +} + +fn bof(dt: u16) -> Vec { + let mut p = u16s(&[0x0600, dt, 0x0DBB, 0x07CC]); + p.extend_from_slice(&[0; 8]); + rec(0x0809, &p) +} + +fn eof() -> Vec { + rec(0x000A, &[]) +} + +/// Font: height in twips, flags, colour, weight, script, underline, then a UTF-16 name. +fn font(twips: u16, flags: u16, icv: u16, weight: u16, script: u16, underline: u8, name: &str) -> Vec { + let mut p = u16s(&[twips, flags, icv, weight, script]); + p.extend_from_slice(&[underline, 0, 0, 0]); + p.push(name.encode_utf16().count() as u8); + p.push(1); + p.extend(u16s(&name.encode_utf16().collect::>())); + rec(0x0031, &p) +} + +/// The CellXF fields an XF record carries after ifnt, ifmt and the protection flags. +#[derive(Clone, Copy, Default)] +struct XfData { + alc: u8, + wrap: bool, + alc_v: u8, + trot: u8, + indent: u8, + shrink: bool, + /// dgLeft, dgRight, dgTop, dgBottom, dgDiag. + dg: [u32; 5], + /// icvLeft, icvRight, icvTop, icvBottom, icvDiag. + icv: [u32; 5], + diag: u32, + fls: u32, + fore: u16, + back: u16, +} + +fn xf(ifnt: u16, ifmt: u16, flags: u16, d: XfData) -> Vec { + let mut p = u16s(&[ifnt, ifmt, flags]); + p.push(d.alc | (u8::from(d.wrap) << 3) | (d.alc_v << 4)); + p.push(d.trot); + p.push(d.indent | (u8::from(d.shrink) << 4)); + p.push(0xFC); // fAtr* bits: no bearing on the values read + let b1 = d.dg[0] | (d.dg[1] << 4) | (d.dg[2] << 8) | (d.dg[3] << 12) | (d.icv[0] << 16) | (d.icv[1] << 23) | (d.diag << 30); + let b2 = d.icv[2] | (d.icv[3] << 7) | (d.icv[4] << 14) | (d.dg[4] << 21) | (d.fls << 26); + p.extend_from_slice(&b1.to_le_bytes()); + p.extend_from_slice(&b2.to_le_bytes()); + p.extend_from_slice(&(d.fore | (d.back << 7)).to_le_bytes()); + rec(0x00E0, &p) +} + +/// What Excel writes for unformatted cells: bottom aligned, automatic fill colours. +fn plain() -> XfData { + XfData { alc_v: 2, fore: 0x40, back: 0x41, ..XfData::default() } +} + +/// Locked, a cell XF inheriting from style XF 0. +const CELL: u16 = 0x0001; +/// Locked, a style XF with no parent. +const STYLE: u16 = 0xFFF5; + +fn number(r: u16, c: u16, ixfe: u16, n: f64) -> Vec { + let mut p = u16s(&[r, c, ixfe]); + p.extend_from_slice(&n.to_le_bytes()); + rec(0x0203, &p) +} + +fn blank(r: u16, c: u16, ixfe: u16) -> Vec { + rec(0x0201, &u16s(&[r, c, ixfe])) +} + +/// A one-sheet workbook: `formatting` records in the globals, `cells` in the sheet. +fn workbook(formatting: &[Vec], cells: &[Vec]) -> Vec { + let boundsheet = |pos: u32| { + let mut p = pos.to_le_bytes().to_vec(); + p.extend_from_slice(&[0, 0, 6, 0]); + p.extend_from_slice(b"Sheet1"); + rec(0x0085, &p) + }; + let globals = |pos: u32| [vec![bof(0x0005)], formatting.to_vec(), vec![boundsheet(pos), eof()]].concat().concat(); + let sheet = [vec![bof(0x0010)], cells.to_vec(), vec![eof()]].concat().concat(); + let len = globals(0).len() as u32; + [globals(len), sheet].concat() +} + +fn style_at<'a>(wb: &'a Workbook, a1: &str) -> &'a Style { + wb.styles.get(wb.sheets[0].style_id(CellRef::parse(a1).unwrap())) +} + +/// Four default fonts (Arial 10 and its bold/italic variants), as Excel writes them. +fn default_fonts() -> Vec> { + vec![ + font(200, 0, 0x7FFF, 400, 0, 0, "Arial"), + font(200, 0, 0x7FFF, 700, 0, 0, "Arial"), + font(200, 2, 0x7FFF, 400, 0, 0, "Arial"), + font(200, 2, 0x7FFF, 700, 0, 0, "Arial"), + ] +} + +/// Fonts, a palette, style XFs, cell XFs and named styles covering every decoded field. +fn formatted() -> Vec { + let mut f = default_fonts(); + // ifnt 5 (index 4 is never written): 14 pt italic, struck, bold, double underline, + // superscript, red (colour 10). + f.push(font(280, 0x000A, 10, 700, 1, 2, "Courier New")); + // ifnt 6: single accounting underline, subscript, a custom palette colour. + f.push(font(220, 0, 9, 400, 2, 0x21, "Times New Roman")); + // Palette: the first two of the 56 custom entries (colour indexes 8 and 9) replaced. + let mut pal = u16s(&[56]); + for (i, c) in INDEXED.iter().enumerate().skip(8) { + let c = match i { + 8 => 0x123456, + 9 => 0xABCDEF, + _ => *c, + }; + pal.extend_from_slice(&[(c >> 16) as u8, (c >> 8) as u8, c as u8, 0]); + } + f.push(rec(0x0092, &pal)); + f.push(rec(0x041E, &[&164u16.to_le_bytes()[..], &[4u8, 0, 0][..], &b"0.0%"[..]].concat())); + // XF 0: the Normal style. + f.push(xf(0, 0, STYLE, plain())); + // XF 1: everything at once. + f.push(xf( + 5, + 164, + 0x0002, // hidden, not locked + XfData { + alc: 2, + wrap: true, + alc_v: 1, + trot: 135, + indent: 3, + shrink: true, + dg: [1, 0, 7, 2, 3], + icv: [9, 0, 8, 0x40, 10], + diag: 3, + fls: 1, + fore: 8, + back: 0x41, + }, + )); + // XF 2: a 12.5% gray pattern on yellow, font index 4 (invalid: the default font). + f.push(xf(4, 0, CELL, XfData { alc: 7, alc_v: 4, trot: 255, fls: 0x11, fore: 10, back: 13, ..plain() })); + // XF 3: a font index past the end, right aligned, diagonal-up only. + f.push(xf(200, 0, CELL, XfData { alc: 3, alc_v: 0, trot: 90, dg: [0, 13, 0, 0, 5], icv: [0, 12, 0, 0, 16], diag: 2, ..plain() })); + // XF 4: an ordinary cell: the default style. + f.push(xf(0, 0, CELL, plain())); + // XF 5: font ifnt 6. + f.push(xf(6, 0, CELL, plain())); + // Styles: built-in Normal, a user style on XF 1 and a duplicate name (ignored), a built-in + // style (Comma) left to GridCraft's own definition. + f.push(rec(0x0293, &[&u16s(&[0x8000])[..], &[0u8, 0xFF][..]].concat())); + f.push(rec(0x0293, &[&u16s(&[1, 5])[..], &[0u8][..], &b"Fancy"[..]].concat())); + f.push(rec(0x0293, &[&u16s(&[2, 5])[..], &[0u8][..], &b"FANCY"[..]].concat())); + f.push(rec(0x0293, &[&u16s(&[0x8002])[..], &[3u8, 0xFF][..]].concat())); + workbook( + &f, + &[ + number(0, 0, 1, 0.5), + blank(0, 1, 1), + number(1, 0, 2, 2.0), + number(2, 0, 3, 3.0), + number(3, 0, 4, 4.0), + number(4, 0, 999, 5.0), + number(5, 0, 5, 6.0), + blank(6, 0, 4), + ], + ) +} + +#[test] +fn reads_fonts_fills_borders_alignment_and_protection() { + let (wb, report) = read_xls(&compound("Workbook", &formatted())).unwrap(); + assert!(report.warnings[0].contains("cell formatting"), "{:?}", report.warnings); + assert!(!report.warnings.iter().any(|w| w.contains("shorter than expected")), "{:?}", report.warnings); + // The Normal style's font is the workbook default. + let normal = wb.styles.get(StyleId::DEFAULT); + assert_eq!((normal.font.name.as_str(), normal.font.size, normal.font.bold), ("Arial", 10.0, false)); + assert_eq!(normal.font.color, Color::Auto); + + let s = style_at(&wb, "A1"); + let f = &s.font; + assert_eq!((f.name.as_str(), f.size), ("Courier New", 14.0)); + assert!(f.bold && f.italic && f.strike); + assert_eq!((f.underline, f.vert, f.color), (Underline::Double, VertAlign::Superscript, Color::Rgb(0xFF0000))); + assert_eq!(s.num_fmt.as_str(), "0.0%"); + assert_eq!(s.protection, Protection { locked: false, hidden: true }); + assert_eq!((s.align.h, s.align.v, s.align.wrap, s.align.shrink), (HAlign::Center, VAlign::Center, true, true)); + assert_eq!((s.align.rotation, s.align.indent), (-45, 3)); + // Colour index 8 and 9 come from the Palette record. + assert_eq!(s.fill, Fill::solid(Color::Rgb(0x123456))); + assert_eq!(s.borders.left, BorderLine { style: BorderStyle::Thin, color: Color::Rgb(0xABCDEF) }); + assert_eq!(s.borders.right, BorderLine::default()); + assert_eq!(s.borders.top, BorderLine { style: BorderStyle::Hair, color: Color::Rgb(0x123456) }); + assert_eq!(s.borders.bottom, BorderLine { style: BorderStyle::Medium, color: Color::Auto }); + let dashed = BorderLine { style: BorderStyle::Dashed, color: Color::Rgb(0xFF0000) }; + assert_eq!((s.borders.diag_down, s.borders.diag_up), (dashed, dashed)); + // A formatted blank cell keeps its formatting. + assert_eq!(wb.sheets[0].style_id(CellRef::parse("B1").unwrap()), wb.sheets[0].style_id(CellRef::parse("A1").unwrap())); + assert_eq!(wb.sheets[0].value(CellRef::parse("B1").unwrap()), Value::Empty); + + let s = style_at(&wb, "A2"); + assert_eq!(s.font, normal.font, "font index 4 falls back to the default font"); + assert_eq!(s.fill, Fill { pattern: PatternType::Gray125, fg: Color::Rgb(0xFF0000), bg: Color::Rgb(0xFFFF00) }); + assert_eq!((s.align.h, s.align.v, s.align.rotation), (HAlign::Distributed, VAlign::Distributed, 255)); + + let s = style_at(&wb, "A3"); + assert_eq!(s.font, normal.font, "a font index past the end falls back to the default font"); + assert_eq!((s.align.h, s.align.v, s.align.rotation), (HAlign::Right, VAlign::Top, 90)); + assert_eq!(s.borders.right, BorderLine { style: BorderStyle::SlantDashDot, color: Color::Rgb(0x0000FF) }); + assert_eq!(s.borders.diag_up, BorderLine { style: BorderStyle::Thick, color: Color::Rgb(0x800000) }); + assert_eq!(s.borders.diag_down, BorderLine::default()); + + // An ordinary cell XF is the default style; so is an XF index past the end. + assert_eq!(wb.sheets[0].style_id(CellRef::parse("A4").unwrap()), StyleId::DEFAULT); + assert_eq!(wb.sheets[0].style_id(CellRef::parse("A5").unwrap()), StyleId::DEFAULT); + // A blank cell in the default style isn't stored. + assert!(wb.sheets[0].cell(CellRef::parse("A7").unwrap()).is_none()); + + let s = style_at(&wb, "A6"); + assert_eq!((s.font.name.as_str(), s.font.size, s.font.color), ("Times New Roman", 11.0, Color::Rgb(0xABCDEF))); + assert_eq!((s.font.underline, s.font.vert), (Underline::SingleAccounting, VertAlign::Subscript)); + + // User-defined styles are kept (first of a name wins); built-in ones are GridCraft's own. + assert_eq!(wb.cell_styles.len(), 1, "{:?}", wb.cell_styles.iter().map(|(n, _)| n).collect::>()); + assert_eq!(wb.cell_styles[0].0, "Fancy"); + assert_eq!(&wb.cell_styles[0].1, style_at(&wb, "A1")); + + // The formatting survives a save as .xlsx. + let (back, _) = read_xlsx(&write_xlsx(&wb).unwrap()).unwrap(); + for a1 in ["A1", "A2", "A3", "A6"] { + assert_eq!(style_at(&back, a1), style_at(&wb, a1), "{a1}"); + } + assert_eq!(back.styles.get(StyleId::DEFAULT).font.name, "Arial"); + assert_eq!(back.cell_styles.iter().map(|(n, _)| n.as_str()).collect::>(), ["Fancy"]); +} + +#[test] +fn without_a_palette_the_default_colours_apply() { + let mut f = default_fonts(); + f.push(xf(0, 0, STYLE, plain())); + f.push(xf(0, 0, CELL, XfData { fls: 1, fore: 8, ..plain() })); + f.push(xf(0, 0, CELL, XfData { fls: 1, fore: 63, ..plain() })); + // Out-of-range colour indexes are automatic. + f.push(xf(0, 0, CELL, XfData { fls: 1, fore: 0x7F, dg: [1, 0, 0, 0, 0], icv: [0x50, 0, 0, 0, 0], ..plain() })); + let (wb, _) = read_xls(&compound("Workbook", &workbook(&f, &[number(0, 0, 1, 1.0), number(1, 0, 2, 1.0), number(2, 0, 3, 1.0)]))).unwrap(); + assert_eq!(style_at(&wb, "A1").fill, Fill::solid(Color::Rgb(0x000000))); + assert_eq!(style_at(&wb, "A2").fill, Fill::solid(Color::Rgb(0x333333))); + let s = style_at(&wb, "A3"); + assert_eq!(s.fill, Fill::solid(Color::Auto)); + assert_eq!(s.borders.left, BorderLine { style: BorderStyle::Thin, color: Color::Auto }); +} + +#[test] +fn short_formatting_records_keep_what_is_there() { + let mut f = default_fonts(); + // A truncated font (no name): the default font keeps the numbering of the next one. + f.push(rec(0x0031, &u16s(&[240, 0]))); + f.push(font(300, 0, 0x7FFF, 700, 0, 0, "Georgia")); + // A palette claiming more entries than it holds. + f.push(rec(0x0092, &[&u16s(&[56])[..], &[1u8, 2, 3, 0][..]].concat())); + f.push(xf(0, 0, STYLE, plain())); + // XF 1: only ifnt and ifmt and flags: number format kept, no alignment. + f.push(rec(0x00E0, &u16s(&[6, 10, 0x0001]))); + // XF 2: two bytes. + f.push(rec(0x00E0, &u16s(&[6]))); + // XF 3: palette colour 8 (overridden) as a fill. + f.push(xf(0, 0, CELL, XfData { fls: 1, fore: 8, ..plain() })); + // Style records cut short, and a style naming a missing XF. + f.push(rec(0x0293, &u16s(&[0x8000]))); + f.push(rec(0x0293, &[&u16s(&[1, 40])[..], &[0u8][..], &b"Cut"[..]].concat())); + f.push(rec(0x0293, &[&u16s(&[900, 4])[..], &[0u8][..], &b"Gone"[..]].concat())); + let book = workbook(&f, &[number(0, 0, 1, 1.0), number(1, 0, 2, 1.0), number(2, 0, 3, 1.0)]); + let (wb, report) = read_xls(&compound("Workbook", &book)).unwrap(); + assert!(report.warnings.iter().any(|w| w.contains("shorter than expected")), "{:?}", report.warnings); + let s = style_at(&wb, "A1"); + assert_eq!((s.font.name.as_str(), s.font.bold), ("Georgia", true)); + assert_eq!(s.num_fmt.as_str(), "0.00%"); + assert_eq!((s.align.v, s.protection.locked), (VAlign::Bottom, true)); + assert_eq!(wb.sheets[0].style_id(CellRef::parse("A2").unwrap()), StyleId::DEFAULT); + assert_eq!(style_at(&wb, "A3").fill, Fill::solid(Color::Rgb(0x010203))); + assert!(wb.cell_styles.is_empty()); +} + +#[test] +fn hostile_formatting_records_never_panic() { + let stream = formatted(); + for at in 0..stream.len() { + for x in [0x00u8, 0xFF, 0x80] { + let mut s = stream.clone(); + s[at] = x; + let _ = read_xls(&compound("Workbook", &s)); + } + } + for cut in 0..stream.len() { + let _ = read_xls(&compound("Workbook", &stream[..cut])); + } +} diff --git a/crates/xlsx/src/xls/mod.rs b/crates/xlsx/src/xls/mod.rs index ceaf3e26d3..7f1df11973 100644 --- a/crates/xlsx/src/xls/mod.rs +++ b/crates/xlsx/src/xls/mod.rs @@ -3,24 +3,27 @@ //! . //! //! Read: worksheets, shared strings, cell values, formulas (text and cached results, including -//! shared and array formulas), number formats, the 1904 date system, merged cells, column -//! widths, row heights, defined names, print areas, print titles and the calculation settings -//! (mode, iteration, precision as displayed). Everything else (fonts, fills, borders, charts, -//! pictures, comments, conditional formats, validation, macros) is skipped with a warning. +//! shared and array formulas), cell formatting (number formats, fonts, fills, borders, +//! alignment, protection, the colour palette and user-defined cell styles), the 1904 date +//! system, merged cells, column widths, row heights, defined names, print areas, print titles +//! and the calculation settings (mode, iteration, precision as displayed). Everything else +//! (charts, pictures, comments, conditional formats, validation, macros) is skipped with a +//! warning. //! Encrypted workbooks and pre-BIFF8 versions are refused. mod formula; mod ftab; mod records; +mod styles; use std::collections::HashMap; use std::sync::Arc; use gridcraft_core::{CellRef, DateSystem, MAX_COLS, MAX_ROWS, RangeRef, Value}; -use gridcraft_model::{CalcMode, CalcSettings, Cell, DefinedName, Formula, LineInfo, NumFmt, Sheet, Style, StyleId, Visibility, Workbook}; +use gridcraft_model::{CalcMode, CalcSettings, Cell, DefinedName, Formula, LineInfo, Sheet, StyleId, Visibility, Workbook}; use crate::cfb::Compound; -use crate::tables::{builtin_format, col_width_to_px, pt_to_px}; +use crate::tables::{col_width_to_px, pt_to_px}; use crate::{IoError, ReadReport}; use formula::{Links, Mode, SupBook, Unsupported, Xti, cell_error, decode}; use records::{Cursor, Record, Records}; @@ -123,7 +126,7 @@ struct Globals { sheets: Vec, date1904: bool, formats: HashMap, - xf_formats: Vec, + styles: styles::StyleRecords, sst: Vec>, links: Links, names: Vec, @@ -153,19 +156,11 @@ pub(crate) fn read_xls_with(bytes: &[u8], mut budget: Budget) -> Result<(Workboo let mut wb = Workbook::new(); wb.sheets.clear(); wb.date_system = if g.date1904 { DateSystem::D1904 } else { DateSystem::D1900 }; - // One model style per XF: only the number format is imported. - let styles: Vec = g - .xf_formats - .iter() - .map(|ifmt| { - let code = g.formats.get(ifmt).map(String::as_str).or_else(|| builtin_format(u32::from(*ifmt))).unwrap_or("General"); - if code.eq_ignore_ascii_case("General") { - StyleId::DEFAULT - } else { - wb.styles.intern(Style { num_fmt: NumFmt::new(code), ..Style::default() }) - } - }) - .collect(); + // One model style per XF. + let styles = styles::build(&g.styles, &g.formats, &mut wb); + if g.styles.short { + report.warn("Some cell formatting records were shorter than expected; the formatting present was kept."); + } // BoundSheet8 index → model sheet index. let mut model_index: Vec> = Vec::with_capacity(g.sheets.len()); @@ -202,7 +197,7 @@ pub(crate) fn read_xls_with(bytes: &[u8], mut budget: Budget) -> Result<(Workboo // The overview comes first. report.warnings.insert( 0, - "Imported from an Excel 97-2003 (.xls) workbook: values, formulas, number formats, merged cells, column widths, row heights and defined names were read. Fonts, fills, borders, charts, pictures, comments and other features are not imported yet. Save as .xlsx; saving .xls is not supported.".into(), + "Imported from an Excel 97-2003 (.xls) workbook: values, formulas, cell formatting, merged cells, column widths, row heights and defined names were read. Charts, pictures, comments and other features are not imported yet. Save as .xlsx; saving .xls is not supported.".into(), ); Ok((wb, report)) } @@ -272,10 +267,10 @@ fn globals(stream: &[u8], budget: &mut Budget, report: &mut ReadReport) -> Resul budget.text(code.len())?; g.formats.insert(ifmt, code); } - 0x00E0 if g.xf_formats.len() < MAX_XFS => { - c.u16()?; - g.xf_formats.push(c.u16()?); - } + 0x00E0 => g.styles.xf(&mut c, MAX_XFS), + 0x0031 => g.styles.font(&mut c), + 0x0092 => g.styles.palette(&mut c), + 0x0293 => g.styles.style(&mut c), 0x00FC => g.sst = shared_strings(&mut c, budget)?, 0x01AE => { let _ctab = c.u16()?; diff --git a/crates/xlsx/src/xls/styles.rs b/crates/xlsx/src/xls/styles.rs new file mode 100644 index 0000000000..6e254c20ca --- /dev/null +++ b/crates/xlsx/src/xls/styles.rs @@ -0,0 +1,390 @@ +//! Cell formatting in a BIFF8 Workbook stream, from [MS-XLS]: Font (2.4.122), XF (2.4.353) with +//! CellXF (2.5.91), Palette (2.4.188) and Style (2.4.269). Every XF is +//! decoded on its own: a cell XF stores its complete formatting (the fAtr* bits only say whether +//! later changes to the parent style propagate), so nothing is inherited from `ixfParent`. + +use std::collections::HashMap; + +use gridcraft_model::style::Protection; +use gridcraft_model::{ + Alignment, BorderLine, BorderStyle, Borders, Color, Fill, Font, HAlign, NumFmt, PatternType, Style, StyleId, StyleTable, Underline, VAlign, + VertAlign, Workbook, +}; + +use super::records::Cursor; +use crate::tables::{INDEXED, builtin_format}; + +/// Font records kept at most ([MS-XLS] FontIndex allows indexes up to 1022). +const MAX_FONTS: usize = 1024; +/// Style records kept at most. +const MAX_STYLES: usize = 65_536; +/// Characters kept from a style name (Excel allows 255). +const MAX_STYLE_NAME: usize = 255; + +/// The automatic font colour (IcvFont 0x7FFF) and the system foreground/background colours. +const ICV_FONT_AUTO: u16 = 0x7FFF; +const ICV_DEFAULT_FG: u16 = 0x40; +const ICV_DEFAULT_BG: u16 = 0x41; + +/// One XF record, decoded as far as its bytes allow. +#[derive(Clone, Debug, Default)] +pub(super) struct Xf { + ifnt: u16, + ifmt: u16, + flags: u16, + /// The CellXF/StyleXF data: 14 bytes when complete. + data: [u8; 14], + /// Whether all 20 bytes were present. + complete: bool, +} + +/// A Style record: the XF it names and its name. +#[derive(Clone, Debug)] +pub(super) struct NamedStyle { + ixf: u16, + /// `Some(id)` for a built-in style (BuiltInStyle.istyBuiltIn), `None` for a user style. + builtin: Option, + /// A user style's name (empty for built-in styles). + name: String, +} + +/// Formatting records collected from the globals substream. +#[derive(Debug, Default)] +pub(super) struct StyleRecords { + fonts: Vec, + font_colors: Vec, + xfs: Vec, + /// Palette entries for colour indexes 8 and up (RRGGBB), when a Palette record is present. + palette: Option>, + styles: Vec, + /// Records that were shorter than their structure; the parts present were used. + pub(super) short: bool, +} + +impl StyleRecords { + /// A Font record. The colour is resolved later, once the palette is known. + pub(super) fn font(&mut self, c: &mut Cursor<'_, '_>) { + if self.fonts.len() >= MAX_FONTS { + return; + } + match read_font(c) { + Some((font, icv)) => { + self.fonts.push(font); + self.font_colors.push(icv); + } + None => { + // Keep the numbering of the fonts after it. + self.short = true; + self.fonts.push(Font::default()); + self.font_colors.push(ICV_FONT_AUTO); + } + } + } + + /// An XF record. A short record keeps the fields it has. + pub(super) fn xf(&mut self, c: &mut Cursor<'_, '_>, max: usize) { + if self.xfs.len() >= max { + return; + } + let mut xf = Xf::default(); + let head = (|| Some((c.u16().ok()?, c.u16().ok()?, c.u16().ok()?)))(); + match head { + Some((ifnt, ifmt, flags)) => { + xf.ifnt = ifnt; + xf.ifmt = ifmt; + xf.flags = flags; + xf.complete = true; + for b in xf.data.iter_mut() { + match c.u8() { + Ok(v) => *b = v, + Err(_) => { + xf.complete = false; + break; + } + } + } + } + None => { + // Not even the font and format: an unformatted, locked cell. + xf.flags = 1; + } + } + if !xf.complete { + self.short = true; + } + self.xfs.push(xf); + } + + /// A Palette record: colours for indexes 8 onwards. + pub(super) fn palette(&mut self, c: &mut Cursor<'_, '_>) { + let Ok(ccv) = c.u16() else { + self.short = true; + return; + }; + let n = usize::from(ccv).min(56); + if usize::from(ccv) > c.remaining() / 4 { + self.short = true; + } + let mut colors = Vec::with_capacity(n); + for _ in 0..n { + let (Ok(r), Ok(g), Ok(b), Ok(_)) = (c.u8(), c.u8(), c.u8(), c.u8()) else { break }; + colors.push((u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b)); + } + self.palette = Some(colors); + } + + /// A Style record. + pub(super) fn style(&mut self, c: &mut Cursor<'_, '_>) { + if self.styles.len() >= MAX_STYLES { + return; + } + let Ok(word) = c.u16() else { + self.short = true; + return; + }; + let ixf = word & 0x0FFF; + if word & 0x8000 != 0 { + let Ok(id) = c.u8() else { + self.short = true; + return; + }; + // BuiltInStyle: the style's number (0 is Normal) and outline level. + self.styles.push(NamedStyle { ixf, builtin: Some(id), name: String::new() }); + } else { + match c.string() { + Ok(name) => { + let name: String = name.chars().filter(|ch| *ch != '\0').take(MAX_STYLE_NAME).collect(); + if !name.is_empty() { + self.styles.push(NamedStyle { ixf, builtin: None, name }); + } + } + Err(_) => self.short = true, + } + } + } + + /// The 64-entry colour table: the built-in eight, then the palette (or the default one). + fn color_table(&self) -> [u32; 64] { + let mut table = INDEXED; + if let Some(p) = &self.palette { + for (slot, c) in table.iter_mut().skip(8).zip(p.iter()) { + *slot = *c; + } + } + table + } +} + +/// Font record fields up to the name; `None` if the record is too short for them. +fn read_font(c: &mut Cursor<'_, '_>) -> Option<(Font, u16)> { + let height = c.u16().ok()?; + let flags = c.u16().ok()?; + let icv = c.u16().ok()?; + let weight = c.u16().ok()?; + let script = c.u16().ok()?; + let underline = c.u8().ok()?; + c.skip(3).ok()?; // family, character set, unused + let name = c.short_string().ok()?; + let mut f = Font::default(); + let name: String = name.chars().filter(|ch| *ch != '\0').take(255).collect(); + if !name.trim().is_empty() { + f.name = name; + } + if height > 0 { + f.size = (f32::from(height) / 20.0).clamp(1.0, 409.0); + } + f.italic = flags & 0x0002 != 0; + f.strike = flags & 0x0008 != 0; + // Weights run 100-1000; Excel writes 400 for normal and 700 for bold. + f.bold = weight >= 600; + f.vert = match script { + 1 => VertAlign::Superscript, + 2 => VertAlign::Subscript, + _ => VertAlign::Baseline, + }; + f.underline = match underline { + 0x01 => Underline::Single, + 0x02 => Underline::Double, + 0x21 => Underline::SingleAccounting, + 0x22 => Underline::DoubleAccounting, + _ => Underline::None, + }; + Some((f, icv)) +} + +/// A colour index in an XF (IcvXF) or Font (IcvFont): 0-63 from the colour table, the system +/// and automatic colours as automatic. +fn color(icv: u16, table: &[u32; 64]) -> Color { + match icv { + ICV_DEFAULT_FG | ICV_DEFAULT_BG | ICV_FONT_AUTO => Color::Auto, + _ => table.get(usize::from(icv)).map_or(Color::Auto, |c| Color::Rgb(*c)), + } +} + +fn border_style(dg: u32) -> BorderStyle { + match dg { + 1 => BorderStyle::Thin, + 2 => BorderStyle::Medium, + 3 => BorderStyle::Dashed, + 4 => BorderStyle::Dotted, + 5 => BorderStyle::Thick, + 6 => BorderStyle::Double, + 7 => BorderStyle::Hair, + 8 => BorderStyle::MediumDashed, + 9 => BorderStyle::DashDot, + 10 => BorderStyle::MediumDashDot, + 11 => BorderStyle::DashDotDot, + 12 => BorderStyle::MediumDashDotDot, + 13 => BorderStyle::SlantDashDot, + _ => BorderStyle::None, + } +} + +fn pattern(fls: u16) -> PatternType { + match fls { + 0x01 => PatternType::Solid, + 0x02 => PatternType::Gray50, + 0x03 => PatternType::Gray75, + 0x04 => PatternType::Gray25, + 0x05 => PatternType::DarkHorizontal, + 0x06 => PatternType::DarkVertical, + 0x07 => PatternType::DarkDown, + 0x08 => PatternType::DarkUp, + 0x09 => PatternType::DarkGrid, + 0x0A => PatternType::DarkTrellis, + 0x0B => PatternType::LightHorizontal, + 0x0C => PatternType::LightVertical, + 0x0D => PatternType::LightDown, + 0x0E => PatternType::LightUp, + 0x0F => PatternType::LightGrid, + 0x10 => PatternType::LightTrellis, + 0x11 => PatternType::Gray125, + 0x12 => PatternType::Gray0625, + _ => PatternType::None, + } +} + +fn line(dg: u32, icv: u32, table: &[u32; 64]) -> BorderLine { + let style = border_style(dg); + if style == BorderStyle::None { + return BorderLine::default(); + } + // Colour 0 means "not specified": automatic. + let color = if icv == 0 { Color::Auto } else { color(icv as u16, table) }; + BorderLine { style, color } +} + +/// The font an XF names (FontIndex: index 4 is never written, so 5 and up count from one). +fn xf_font(ifnt: u16, fonts: &[Font]) -> Font { + let i = match ifnt { + 0..=3 => usize::from(ifnt), + 4 => return fonts.first().cloned().unwrap_or_default(), + _ => usize::from(ifnt) - 1, + }; + fonts.get(i).or_else(|| fonts.first()).cloned().unwrap_or_default() +} + +fn num_fmt(ifmt: u16, formats: &HashMap) -> NumFmt { + let code = formats.get(&ifmt).map(String::as_str).or_else(|| builtin_format(u32::from(ifmt))).unwrap_or("General"); + if code.eq_ignore_ascii_case("General") { NumFmt::default() } else { NumFmt::new(code) } +} + +/// The model style of one XF. +fn xf_style(xf: &Xf, fonts: &[Font], formats: &HashMap, table: &[u32; 64]) -> Style { + let mut s = Style { + font: xf_font(xf.ifnt, fonts), + num_fmt: num_fmt(xf.ifmt, formats), + protection: Protection { locked: xf.flags & 0x0001 != 0, hidden: xf.flags & 0x0002 != 0 }, + ..Style::default() + }; + if !xf.complete { + return s; + } + let d = &xf.data; + let byte = |i: usize| d.get(i).copied().unwrap_or(0); + let word32 = |i: usize| u32::from_le_bytes([byte(i), byte(i + 1), byte(i + 2), byte(i + 3)]); + let (a0, rot, a2) = (byte(0), byte(1), byte(2)); + s.align = Alignment { + h: match a0 & 0x07 { + 1 => HAlign::Left, + 2 => HAlign::Center, + 3 => HAlign::Right, + 4 => HAlign::Fill, + 5 => HAlign::Justify, + 6 => HAlign::CenterAcross, + 7 => HAlign::Distributed, + _ => HAlign::General, + }, + wrap: a0 & 0x08 != 0, + v: match (a0 >> 4) & 0x07 { + 0 => VAlign::Top, + 1 => VAlign::Center, + 3 => VAlign::Justify, + 4 => VAlign::Distributed, + _ => VAlign::Bottom, + }, + rotation: match rot { + 0..=90 => i16::from(rot), + 91..=180 => -(i16::from(rot) - 90), + 255 => 255, + _ => 0, + }, + indent: a2 & 0x0F, + shrink: a2 & 0x10 != 0, + }; + let b1 = word32(4); + let b2 = word32(8); + let mut borders = Borders { + left: line(b1 & 0x0F, (b1 >> 16) & 0x7F, table), + right: line((b1 >> 4) & 0x0F, (b1 >> 23) & 0x7F, table), + top: line((b1 >> 8) & 0x0F, b2 & 0x7F, table), + bottom: line((b1 >> 12) & 0x0F, (b2 >> 7) & 0x7F, table), + ..Borders::default() + }; + let diag = line((b2 >> 21) & 0x0F, (b2 >> 14) & 0x7F, table); + let which = (b1 >> 30) & 0x03; + if which & 1 != 0 { + borders.diag_down = diag; + } + if which & 2 != 0 { + borders.diag_up = diag; + } + s.borders = borders; + let fill = u16::from_le_bytes([byte(12), byte(13)]); + let fls = ((b2 >> 26) & 0x3F) as u16; + let fg = color(fill & 0x7F, table); + let bg = color((fill >> 7) & 0x7F, table); + s.fill = match pattern(fls) { + PatternType::None => Fill::default(), + PatternType::Solid => Fill::solid(fg), + p => Fill { pattern: p, fg, bg }, + }; + s +} + +/// Imports the formatting: sets the workbook's default (Normal) font and named cell styles, and +/// returns the model style of every XF in file order. +pub(super) fn build(r: &StyleRecords, formats: &HashMap, wb: &mut Workbook) -> Vec { + let table = r.color_table(); + let fonts: Vec = r.fonts.iter().zip(r.font_colors.iter()).map(|(f, icv)| Font { color: color(*icv, &table), ..f.clone() }).collect(); + // The Normal style's font is what every unformatted cell shows. + let normal_xf = r.styles.iter().find(|s| s.builtin == Some(0)).map_or(0, |s| usize::from(s.ixf)); + let normal_font = r.xfs.get(normal_xf).map(|xf| xf_font(xf.ifnt, &fonts)).or_else(|| fonts.first().cloned()); + if let Some(font) = normal_font { + wb.styles = StyleTable::with_default(Style { font, ..Style::default() }); + } + let out = r.xfs.iter().map(|xf| wb.styles.intern(xf_style(xf, &fonts, formats, &table))).collect(); + // User-defined named styles. Excel 2007 and later write every built-in style into each .xls + // (about fifty, their colours approximated to the palette); GridCraft defines those itself. + let mut named: Vec<(String, Style)> = Vec::new(); + for ns in &r.styles { + if ns.builtin.is_some() || ns.name.eq_ignore_ascii_case("Normal") || named.iter().any(|(n, _)| n.eq_ignore_ascii_case(&ns.name)) { + continue; + } + if let Some(xf) = r.xfs.get(usize::from(ns.ixf)) { + named.push((ns.name.clone(), xf_style(xf, &fonts, formats, &table))); + } + } + wb.cell_styles = named; + out +} diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index cf567f8bd6..b1109b678f 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (.xls dialog sheets are skipped instead of read as worksheets; `.xls` import reads each sheet's display options from WINDOW2: right-to-left, gridlines, headings, zeros, formulas; XLSX text boxes and shapes keep their paragraphs' right-to-left direction on save; CSV/TSV/TXT can open with every field kept as text via `file.open {csvAsText}`; formulas over Excel's 8,192 characters, and `%` or call chains past the nesting limit, are parse errors instead of stack overflows when a file is opened; XLSX PivotTables read and write `rowGrandTotals`/`colGrandTotals` the way Excel does, so a table saved with only one grand total opens with the right one; HTML export keeps right-to-left; CSV and text import detects likely Windows-1256 Arabic when invalid UTF-8 has Arabic byte runs; other invalid UTF-8 remains Windows-1252; HTML export places pictures and charts, as inline SVG, at their anchors) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-21 · **Change:** minor (`.xls` import reads cell formatting: fonts, fills, borders, alignment, protection, palette, user cell styles; .xls dialog sheets are skipped instead of read as worksheets; `.xls` import reads each sheet's display options from WINDOW2: right-to-left, gridlines, headings, zeros, formulas; XLSX text boxes and shapes keep their paragraphs' right-to-left direction on save; CSV/TSV/TXT can open with every field kept as text via `file.open {csvAsText}`; formulas over Excel's 8,192 characters, and `%` or call chains past the nesting limit, are parse errors instead of stack overflows when a file is opened; XLSX PivotTables read and write `rowGrandTotals`/`colGrandTotals` the way Excel does, so a table saved with only one grand total opens with the right one; HTML export keeps right-to-left; CSV and text import detects likely Windows-1256 Arabic when invalid UTF-8 has Arabic byte runs; other invalid UTF-8 remains Windows-1252; HTML export places pictures and charts, as inline SVG, at their anchors) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Every format Excel opens or saves, with GridCraft's read and write support, fidelity and tests. Part of [target-app-parity.md](target-app-parity.md). **File formats: ~50% ready, 80–120 h** @@ -54,7 +54,7 @@ values) is still dropped. | `.xlsm` Macro-Enabled Workbook | R/W | yes | yes, macros kept, not run | The VBA project is kept byte-for-byte and written back only into `.xlsm`/`.xltm` (#6); macros never run. Saved with the macro-enabled workbook content type | `roundtrip.rs` (`vba_project_roundtrip`, `package_kind_sets_workbook_content_type`), engine `io.rs` (`macros_are_saved_only_into_xlsm`) | | `.xltx` / `.xltm` templates | R/W | yes | partial | Saved with the template (and macro-enabled template) content type, chosen by extension; `.xltm` keeps a VBA project, `.xltx` drops it; "new from template" behaviour missing | `roundtrip.rs` (`package_kind_sets_workbook_content_type`), engine `io.rs` | | `.xlsb` Binary Workbook | R/W | **values only** (#101) | no | Names, visibility, numbers, text, booleans, errors, merges, cached formula results; no formulas, formatting, charts, names | `xlsb_tests.rs` | -| `.xls` Excel 97-2003 (BIFF8) | R/W | **yes, partial** (#121) | no (Save As `.xlsx`) | Clean-room from [MS-CFB] and [MS-XLS]. Read: worksheets and visibility, shared strings, numbers, text, booleans, errors, formulas (text for the common tokens, shared and array formulas, 3-D refs, names, newer functions called by name) with cached results, number formats (dates display as dates), 1904 date system, merges, column widths, row heights, defined names, print area and titles, calculation settings (mode, iteration with its maximum iterations and change, precision as displayed), sheet display options (right-to-left, gridlines, headings, zero values, formulas; a truncated WINDOW2 is ignored). Not read: fonts, fills, borders, alignment, charts, pictures, comments, CF, validation, hyperlinks, freeze panes, external links (formula kept as its value). Chart and macro sheets skipped with a warning; encrypted (FILEPASS) and BIFF5 (Excel 5.0/95) files refused with a message. Opens as a new unsaved workbook. Unverified on a real-world corpus | `xls_tests.rs` | +| `.xls` Excel 97-2003 (BIFF8) | R/W | **yes, partial** (#121) | no (Save As `.xlsx`) | Clean-room from [MS-CFB] and [MS-XLS]. Read: worksheets and visibility, shared strings, numbers, text, booleans, errors, formulas (text for the common tokens, shared and array formulas, 3-D refs, names, newer functions called by name) with cached results, number formats (dates display as dates), cell formatting (fonts, solid and pattern fills, borders with diagonals, alignment with rotation, indent, wrap and shrink, locked and hidden, the custom colour palette, the Normal font as the workbook default, user-defined cell styles), 1904 date system, merges, column widths, row heights, defined names, print area and titles, calculation settings (mode, iteration with its maximum iterations and change, precision as displayed), sheet display options (right-to-left, gridlines, headings, zero values, formulas; a truncated WINDOW2 is ignored). Not read: exact colours beyond the 56-colour palette (XFExt: theme colours and tints show as the palette colour Excel stored next to them), rich-text runs inside a cell, charts, pictures, comments, CF, validation, hyperlinks, freeze panes, external links (formula kept as its value). Chart and macro sheets skipped with a warning; encrypted (FILEPASS) and BIFF5 (Excel 5.0/95) files refused with a message. Opens as a new unsaved workbook. Unverified on a real-world corpus | `xls_tests.rs`, `xls_style_tests.rs` | | `.xlt` Excel 97-2003 template | R/W | yes (read as `.xls`) | no | | — | | `.xlam` / `.xla` add-ins | R/W | no | no | VBA; out of scope | — | | `.xml` XML Spreadsheet 2003 | R/W | no | no | Simple to support | — | @@ -100,7 +100,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. | Real-world XLSX corpus run (craftrules test-corpora standard), and confirm Excel opens our output with no repair prompt (owner runs Excel) | 15–25 | | Preserve the rest of the unmodelled XLSX content (slicers, timelines, data model, table query tables, `extLst` entries); pass-through landed for the rest (#330) | 6–10 | | Streaming XLSX reader for very large files (#175), part-size limits that degrade instead of failing | 10–15 | -| Legacy `.xls` (BIFF8): styles (fonts, fills, borders, alignment), comments, CF, validation, pictures and charts on import; corpus run; then write | 10–15 | +| Legacy `.xls` (BIFF8): exact theme colours (XFExt), comments, CF, validation, pictures and charts on import; corpus run; then write | 10–15 | | Encrypted workbooks: save with password (#11) | 4–6 | | XLSB and ODS: formulas and formatting on import; then write | 10–15 | | CSV/text: encoding choice, `.prn`, `.dif`, `.slk`, XML Spreadsheet 2003 | 4–7 | @@ -132,6 +132,7 @@ refresh. Counted under features (Get & Transform, ~10% ready), not here. | 2026-10-15 | minor | XLSX: formulas and PivotTable sources quote sheet names like Excel (a leading `.`, R1C1-like and cell-like names), so `'.x'!A1` no longer saves as `.x!A1`, which Excel could not open (#1458) | | 2026-10-15 | minor | XLSX text preserves CR and CRLF across save/reopen; shared strings use `_x000D_` and other XML text uses ` ` (#1488) | | 2026-10-15 | minor | XLSX save keeps legacy `INDEX`/`MATCH` formulas scalar instead of marking them as dynamic arrays (#1505) | +| 2026-10-14 | minor | `.xls` import reads cell formatting from the Font, XF, Palette and Style records: fonts, fills, borders, alignment, protection, the custom palette, the Normal font and user cell styles | | 2026-10-14 | trivial | Formula parser: text longer than Excel's 8,192 characters, and postfix chains (`1%%%…`, `f(1)(1)…`) past the 256-level nesting limit, are parse errors (the cell keeps its formula text and cached value) instead of trees deep enough to overflow the stack when a hostile file is opened, saved or dropped | | 2026-10-14 | minor | XLSX formulas read with dynamic-array metadata (`cm`) keep it on save, so Excel does not recalculate them with implicit intersection | | 2026-10-14 | minor | PDF export writes clickable annotations for safe cell URLs and destinations on printed pages; CLI `convert` accepts `.pdf` output (#954) | diff --git a/docs/gaps.md b/docs/gaps.md index b077279912..cfee087e35 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -72,14 +72,14 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- ### 7. Legacy `.xls` only partly read; encrypted workbooks can't be saved with a password (FF) - **Evidence:** #121: Excel 97-2003 (BIFF8) `.xls` files now open (values, formulas, number - formats, merges, sizes, names); fonts, fills, borders, charts, pictures, comments, conditional + formats, merges, sizes, names, and cell formatting: fonts, fills, borders, alignment); exact theme colours (XFExt), charts, pictures, comments, conditional formats, validation and BIFF5 files are not read, and `.xls` is never written. Password-encrypted `.xlsx` workbooks (Agile and Standard encryption) open with their password since #11, but are saved without it: GridCraft can't write an encrypted file yet. Encrypted `.xls` (FILEPASS) and RC4-encrypted files are still refused with a message. - **Impact:** common in finance, government and anything older than 2007; encrypted files are routine in business. -- **Estimate:** 14–21 h (`.xls` styles and objects 10–15, encryption 4–6). [file-format-parity.md](file-format-parity.md) +- **Estimate:** 12–18 h (`.xls` objects 8–12, encryption 4–6). [file-format-parity.md](file-format-parity.md) ### 8. PivotTables: no slicers, calculated items, PivotCharts; unverified in Excel (F) - **Evidence:** `parity-checklist.md` missing `insert.slicer`, `insert.timeline`, From 1062604fee0a6f4bf9394dd9d578f3595ede44a5 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:32:50 -0500 Subject: [PATCH 136/687] i18n: Russian Maximum Change reads as a change limit The Russian labels for Maximum Change said relative error, which is a different quantity. They now say maximum change. --- crates/ui-egui/src/i18n/ru.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/i18n/ru.rs b/crates/ui-egui/src/i18n/ru.rs index 3101d4b35e..97033fe4ef 100644 --- a/crates/ui-egui/src/i18n/ru.rs +++ b/crates/ui-egui/src/i18n/ru.rs @@ -418,9 +418,9 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Workbook calculation" => "Вычисления в книге", "Enable iterative calculation" => "Включить итеративные вычисления", "Maximum Iterations:" => "Предельное число итераций:", - "Maximum Change:" => "Относительная погрешность:", + "Maximum Change:" => "Максимальное изменение:", "Maximum Iterations must be a whole number from 1 to 32767." => "Предельное число итераций должно быть целым числом от 1 до 32767.", - "Maximum Change must be a number of 0 or more." => "Относительная погрешность должна быть числом не меньше 0.", + "Maximum Change must be a number of 0 or more." => "Максимальное изменение должно быть числом не меньше 0.", "Calculation settings are saved with the workbook." => "Параметры вычислений сохраняются вместе с книгой.", "Insert Sheet" => "Вставить лист", "Rename" => "Переименовать", From ad2e3ac9de1a1ce011229a2c77924c8a201d8d51 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:37:19 -0500 Subject: [PATCH 137/687] Check duplicate .xls style names in linear time --- crates/xlsx/src/xls/styles.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/xls/styles.rs b/crates/xlsx/src/xls/styles.rs index 6e254c20ca..8bd4ca9f8d 100644 --- a/crates/xlsx/src/xls/styles.rs +++ b/crates/xlsx/src/xls/styles.rs @@ -377,11 +377,18 @@ pub(super) fn build(r: &StyleRecords, formats: &HashMap, wb: &mut W // User-defined named styles. Excel 2007 and later write every built-in style into each .xls // (about fifty, their colours approximated to the palette); GridCraft defines those itself. let mut named: Vec<(String, Style)> = Vec::new(); + // Names are compared ASCII-case-insensitively; a set keeps a hostile file's many names linear. + let mut seen: std::collections::HashSet = std::collections::HashSet::from(["normal".to_string()]); for ns in &r.styles { - if ns.builtin.is_some() || ns.name.eq_ignore_ascii_case("Normal") || named.iter().any(|(n, _)| n.eq_ignore_ascii_case(&ns.name)) { + if ns.builtin.is_some() { + continue; + } + let key = ns.name.to_ascii_lowercase(); + if seen.contains(&key) { continue; } if let Some(xf) = r.xfs.get(usize::from(ns.ixf)) { + seen.insert(key); named.push((ns.name.clone(), xf_style(xf, &fonts, formats, &table))); } } From a80c7ff570ed25a72a86d921461f6abf3a5bd94e Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:43:24 -0500 Subject: [PATCH 138/687] i18n: Japanese labels for the iteration settings live in ja.rs The merge from main moved the Japanese catalog into i18n/ja.rs, but the feature's old table stayed in i18n.rs and was never used, which failed clippy's dead-code lint. Drop the table and add the four new labels and messages to ja.rs. --- crates/ui-egui/src/i18n.rs | 421 ---------------------------------- crates/ui-egui/src/i18n/ja.rs | 4 + 2 files changed, 4 insertions(+), 421 deletions(-) diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 5d3a831b1a..345037b52c 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -296,427 +296,6 @@ pub fn set_current(ctx: &egui::Context, language: Language) { ctx.data_mut(|d| d.insert_temp(lang_key(), language)); } -/// English label → Japanese. Keys are the exact interface strings (command ids, file names and -/// document text are never translated); anything absent falls back to English. -const JAPANESE: &[(&str, &str)] = &[ - ("Paste", "貼り付け"), - ("Paste Values", "値の貼り付け"), - ("Paste Formulas", "数式の貼り付け"), - ("Paste Formatting", "書式の貼り付け"), - ("Transpose", "行列を入れ替え"), - ("Paste Link", "リンクの貼り付け"), - ("Paste Special…", "形式を選択して貼り付け…"), - ("Insert Copied Cells", "コピーしたセルの挿入"), - ("Insert Copied Cells…", "コピーしたセルの挿入…"), - ("Insert Cut Cells", "切り取ったセルの挿入"), - ("Insert Paste", "挿入貼り付け"), - ("Cut (⌘X)", "切り取り (⌘X)"), - ("Copy (⌘C)", "コピー (⌘C)"), - ("Format Painter (double-click to keep it on)", "書式のコピー/貼り付け (ダブルクリックで固定)"), - ("Bold (⌘B)", "太字 (⌘B)"), - ("Italic (⌘I)", "斜体 (⌘I)"), - ("Underline (⌘U)", "下線 (⌘U)"), - ("Double Underline", "二重下線"), - ("Strikethrough", "取り消し線"), - ("Underline", "下線"), - ("Increase Font Size", "フォントの拡大"), - ("Decrease Font Size", "フォントの縮小"), - ("Wrap Text", "折り返して全体を表示"), - ("Merge & Center", "セルを結合して中央揃え"), - ("Merge Cells", "セルの結合"), - ("Merge Across", "横方向に結合"), - ("Unmerge Cells", "セル結合の解除"), - ("Fill Color", "塗りつぶしの色"), - ("Font Color", "フォントの色"), - ("Borders", "罫線"), - ("Top\nAlign", "上揃え"), - ("Middle\nAlign", "中央揃え (縦)"), - ("Bottom\nAlign", "下揃え"), - ("Left", "左"), - ("Right", "右"), - ("Center", "中央"), - ("Horizontal", "水平"), - ("Vertical Text", "縦書き"), - ("Decrease Indent", "インデントを減らす"), - ("Increase Indent", "インデントを増やす"), - ("Orientation", "方向"), - ("Page Layout|Orientation", "印刷の向き"), - ("Angle Counterclockwise", "左回りに回転"), - ("Angle Clockwise", "右回りに回転"), - ("Rotate Text Up", "文字列を上に回転"), - ("Rotate Text Down", "文字列を下に回転"), - ("Accounting Number Format", "通貨表示形式"), - ("Percent Style", "パーセント表示"), - ("Comma Style", "桁区切りスタイル"), - ("Increase Decimal", "小数点以下の桁数を増やす"), - ("Decrease Decimal", "小数点以下の桁数を減らす"), - ("More Accounting Formats…", "その他の通貨表示形式…"), - ("Conditional\nFormatting", "条件付き書式"), - ("Conditional Formatting", "条件付き書式"), - ("Format\nas Table", "テーブルとして書式設定"), - ("Cell\nStyles", "セルのスタイル"), - ("Format Cells…", "セルの書式設定…"), - ("Format Cell Alignment…", "セルの配置…"), - ("Format\nPane", "書式ウィンドウ"), - ("Format", "書式"), - ("Insert", "挿入"), - ("Insert Cells…", "セルの挿入…"), - ("Delete", "削除"), - ("Delete Cells…", "セルの削除…"), - ("Insert Sheet Rows", "シートの行を挿入"), - ("Insert Sheet Columns", "シートの列を挿入"), - ("Delete Sheet Rows", "シートの行を削除"), - ("Delete Sheet Columns", "シートの列を削除"), - ("Row Height…", "行の高さ…"), - ("AutoFit Row Height", "行の高さを自動調整"), - ("Column Width…", "列の幅…"), - ("AutoFit Column Width", "列の幅を自動調整"), - ("Default Width…", "標準の幅…"), - ("Hide Rows", "行を表示しない"), - ("Unhide Rows", "再表示"), - ("Hide Columns", "列の表示/非表示"), - ("Unhide Columns", "再表示"), - ("Lock Cell", "セルのロック"), - ("Clear", "クリア"), - ("Clear All", "すべてクリア"), - ("Clear Formats", "書式のクリア"), - ("Clear Contents", "数式と値のクリア"), - ("Clear Comments and Notes", "コメントとメモのクリア"), - ("Clear Hyperlinks", "ハイパーリンクのクリア"), - ("AutoSum", "オート SUM"), - ("AutoSum (⌘⇧T)", "オート SUM (⌘⇧T)"), - ("Flash\nFill", "フラッシュ フィル"), - ("Flash Fill", "フラッシュ フィル"), - ("Fill", "フィル"), - ("Clear Print Area", "印刷範囲のクリア"), - ("Find &\nSelect", "検索と選択"), - ("Find…", "検索…"), - ("Replace…", "置換…"), - ("Go To…", "ジャンプ…"), - ("Go To Special…", "セルを選択…"), - ("PivotTable", "ピボットテーブル"), - ("Recommended\nCharts", "推奨グラフ"), - ("Table", "テーブル"), - ("Pictures", "画像"), - ("Shapes", "図形"), - ("Icons", "アイコン"), - ("Text\nBox", "テキスト ボックス"), - ("Comment", "コメント"), - ("New\nComment", "新しいコメント"), - ("New Note", "新しいメモ"), - ("Show/Hide Note", "メモの表示/非表示"), - ("Header &\nFooter", "ヘッダーとフッター"), - ("Text to\nColumns", "区切り位置"), - ("Link", "リンク"), - ("Symbol", "記号"), - ("Sparklines", "スパークライン"), - ("Checkbox", "チェックボックス"), - ("Insert checkboxes in the selected cells", "選択したセルにチェックボックスを挿入"), - ("Header & Footer", "ヘッダーとフッター"), - ("Draw with ink", "インクで描画"), - ("Eraser", "消しゴム"), - ("Ink to\nShape", "インクを図形に変換"), - ("Select\nObjects", "オブジェクトの選択"), - ("Themes", "テーマ"), - ("Margins", "余白"), - ("Left Margin", "左余白"), - ("Right Margin", "右余白"), - ("Top Margin", "上余白"), - ("Bottom Margin", "下余白"), - ("Header Margin", "ヘッダーの余白"), - ("Footer Margin", "フッターの余白"), - ("Size", "サイズ"), - ("Print\nArea", "印刷範囲"), - ("Set Print Area", "印刷範囲の設定"), - ("Breaks", "改ページ"), - ("Insert Page Break", "改ページの挿入"), - ("Remove Page Break", "改ページの解除"), - ("Reset All Page Breaks", "すべての改ページを解除"), - ("Print\nTitles", "印刷タイトル"), - ("Sheet\nRight-to-Left", "シートを右から左へ"), - ("Sheet Right-to-Left", "シートを右から左へ"), - ("Narrow", "狭い"), - ("Wide", "広い"), - ("Normal", "標準"), - ("Portrait", "縦"), - ("Landscape", "横"), - ("Custom Margins…", "ユーザー設定の余白…"), - ("Insert\nFunction", "関数の挿入"), - ("Name\nManager", "名前の管理"), - ("Define Name", "名前の定義"), - ("Use in Formula", "数式で使用"), - ("Create from Selection", "選択範囲から作成"), - ("Trace Precedents", "参照元のトレース"), - ("Trace Dependents", "参照先のトレース"), - ("Remove Arrows", "矢印の解除"), - ("Show Formulas", "数式の表示"), - ("Error Checking", "エラーチェック"), - ("Evaluate Formula", "数式の検証"), - ("Watch Window", "ウォッチ ウィンドウ"), - ("Calculation\nOptions", "計算方法の設定"), - ("Calculate Now", "再計算実行"), - ("Calculate Sheet", "シートの再計算"), - ("Automatic", "自動"), - ("Automatic Except for Data Tables", "データ テーブル以外は自動"), - ("Manual", "手動"), - ("Sort &\nFilter", "並べ替えとフィルター"), - ("Sort", "並べ替え"), - ("Sort A to Z", "昇順"), - ("Sort Z to A", "降順"), - ("Custom Sort…", "ユーザー設定の並べ替え…"), - ("Filter", "フィルター"), - ("Reapply", "再適用"), - ("Get Data\n(Text/CSV)", "データの取得 (テキスト/CSV)"), - ("Remove\nDuplicates", "重複の削除"), - ("Data\nValidation", "データの入力規則"), - ("Data Validation", "データの入力規則"), - ("Data Validation…", "データの入力規則…"), - ("Circle Invalid Data", "無効なデータをマーク"), - ("Clear Validation", "入力規則のクリア"), - ("Group", "グループ化"), - ("Ungroup", "グループ解除"), - ("Show Detail", "詳細データの表示"), - ("Hide Detail", "詳細を表示しない"), - ("Subtotal", "小計"), - ("Data\nAnalysis", "データ分析"), - ("Data Analysis", "データ分析"), - ("Solver", "ソルバー"), - ("What-If\nAnalysis", "What-If 分析"), - ("Data Table…", "データ テーブル…"), - ("Scenario Manager…", "シナリオ マネージャー…"), - ("Goal Seek…", "ゴール シーク…"), - ("Forecast\nSheet", "予測シート"), - ("Spelling", "スペルチェック"), - ("Check\nAccessibility", "アクセシビリティ チェック"), - ("Threaded Comments", "スレッド化されたコメント"), - ("Show\nComments", "コメントの表示"), - ("Previous", "前へ"), - ("Next", "次へ"), - ("Previous Comment", "前のコメント"), - ("Next Comment", "次のコメント"), - ("Show All Notes", "すべてのメモを表示"), - ("Convert to Comments", "コメントに変換"), - ("Protect\nWorkbook", "ブックの保護"), - ("Allow Edit\nRanges", "範囲の編集を許可"), - ("Allow Edit Ranges", "範囲の編集を許可"), - ("Unprotect\nSheet", "シート保護の解除"), - ("Protect Sheet…", "シートの保護…"), - ("Comments", "コメント"), - ("Notes", "メモ"), - ("Page Break\nPreview", "改ページ プレビュー"), - ("Page\nLayout", "ページ レイアウト"), - ("Formula Bar", "数式バー"), - ("Gridlines", "目盛線"), - ("Headings", "見出し"), - ("Zoom", "ズーム"), - ("100%", "100%"), - ("Zoom to\nSelection", "選択範囲を拡大"), - ("Freeze\nPanes", "ウィンドウ枠の固定"), - ("Freeze Panes", "ウィンドウ枠の固定"), - ("Freeze Top Row", "最上行の固定"), - ("Freeze First Column", "最左列の固定"), - ("Unfreeze Panes", "ウィンドウ枠固定の解除"), - ("Dark Mode", "ダーク モード"), - ("Display theme", "表示テーマ"), - ("Animations", "アニメーション"), - ( - "Glide the selection outline to the cell you move to instead of jumping there", - "選択範囲の枠を、移動先のセルまで滑らかに動かします(瞬時には切り替えません)", - ), - ("System", "システム"), - ("Light", "ライト"), - ("Dark", "ダーク"), - ("Interface language", "表示言語"), - ("Interface size", "表示サイズ"), - ("Auto", "自動"), - ("Drag to freeze columns", "ドラッグして列を固定"), - ("Drag to freeze rows", "ドラッグして行を固定"), - ("Command\nPalette", "コマンド パレット"), - ("Agent\nControl", "エージェント制御"), - ("Action\nJournal", "操作ジャーナル"), - ("About\nGridCraft", "GridCraft について"), - ("Share", "共有"), - ("Add Chart\nElement", "グラフ要素を追加"), - ("Change\nChart Type", "グラフの種類の変更"), - ("Switch\nRow/Column", "行/列の切り替え"), - ("Select\nData", "データの選択"), - ("Data Labels: None", "データ ラベル: なし"), - ("Data Labels: Show", "データ ラベル: 表示"), - ("Legend: None", "凡例: なし"), - ("Legend: Bottom", "凡例: 下"), - ("Legend: Top", "凡例: 上"), - ("Legend: Right", "凡例: 右"), - ("Gridlines: None", "目盛線: なし"), - ("Gridlines: Show", "目盛線: 表示"), - ("Chart\nTitle", "グラフ タイトル"), - ("Delete\nChart", "グラフの削除"), - ("Convert\nto Range", "範囲に変換"), - ("Table\nStyles", "テーブル スタイル"), - ("Top 10 Items", "上位 10 項目"), - ("Top 10%", "上位 10%"), - ("Bottom 10 Items", "下位 10 項目"), - ("Bottom 10%", "下位 10%"), - ("Above Average", "平均より上"), - ("Below Average", "平均より下"), - ("Clear Rules from Entire Sheet", "シート全体からルールをクリア"), - ("Clear Rules from Selected Cells", "選択したセルからルールをクリア"), - ("Constants", "定数"), - ("Data Bars", "データ バー"), - ("Color Scales", "カラー スケール"), - ("Icon Sets", "アイコン セット"), - ("Home", "ホーム"), - ("Draw", "描画"), - ("Page Layout", "ページ レイアウト"), - ("Formulas", "数式"), - ("Data", "データ"), - ("Review", "レビュー"), - ("View", "表示"), - ("Automate", "自動化"), - ("Table Design", "テーブル デザイン"), - ("Chart Design", "グラフ デザイン"), - ("Ready", "準備完了"), - ("Enter", "入力"), - ("Edit", "編集"), - ("Point", "ポイント"), - ("Calculate", "計算"), - ("Select destination and press Enter or choose Paste", "貼り付け先を選び、Enter キーを押すか [貼り付け] を選びます"), - ("General", "標準"), - ("Number", "数値"), - ("Currency", "通貨"), - ("Accounting", "会計"), - ("Date", "日付"), - ("Time", "時刻"), - ("Percentage", "パーセンテージ"), - ("Fraction", "分数"), - ("Scientific", "指数"), - ("Text", "文字列"), - ("Special", "特殊"), - ("Custom", "ユーザー設定"), - ("Short Date", "日付"), - ("Long Date", "長い日付"), - ("More Number Formats…", "その他の表示形式…"), - ("Financial", "財務"), - ("Logical", "論理"), - ("Date &\nTime", "日付/時刻"), - ("Lookup &\nReference", "検索/行列"), - ("Math &\nTrig", "数学/三角"), - ("More\nFunctions", "その他の関数"), - ("Sheet Options|View", "画面"), - ("Print", "印刷"), - // Page Layout view and the Header & Footer tab. - ("Click to add data", "クリックしてデータを追加"), - ("Add header", "ヘッダーの追加"), - ("Add footer", "フッターの追加"), - ("(none)", "(なし)"), - ("Header", "ヘッダー"), - ("Footer", "フッター"), - ("Page\nNumber", "ページ\n番号"), - ("Number\nof Pages", "ページ数"), - ("Current\nDate", "現在の\n日付"), - ("Current\nTime", "現在の\n時刻"), - ("File\nPath", "ファイルの\nパス"), - ("File\nName", "ファイル名"), - ("Sheet\nName", "シート名"), - ("Go to\nHeader", "ヘッダーに\n移動"), - ("Go to\nFooter", "フッターに\n移動"), - ("Page Number", "ページ番号"), - ("Number of Pages", "ページ数"), - ("Current Date", "現在の日付"), - ("Current Time", "現在の時刻"), - ("File Path", "ファイルのパス"), - ("File Name", "ファイル名"), - ("Sheet Name", "シート名"), - ("Go to Header", "ヘッダーに移動"), - ("Different First Page", "先頭ページのみ別指定"), - ("Different Odd & Even Pages", "奇数/偶数ページ別指定"), - ("Go to Footer", "フッターに移動"), - ("Ruler", "ルーラー"), - // File › Backstage view. - ("File", "ファイル"), - ("Back", "戻る"), - ("New", "新規"), - ("Open", "開く"), - ("Info", "情報"), - ("Save", "上書き保存"), - ("Save As", "名前を付けて保存"), - ("Export", "エクスポート"), - ("Close", "閉じる"), - ("Options", "オプション"), - ("Blank workbook", "空白のブック"), - ("Recent", "最近使ったアイテム"), - ("No recent workbooks yet.", "最近使ったブックはまだありません。"), - ("More workbooks", "その他のブック"), - ("Modified", "更新日時"), - ("Not available", "利用できません"), - ("Just now", "たった今"), - ("Yesterday", "昨日"), - ("{n} min ago", "{n} 分前"), - ("{n} h ago", "{n} 時間前"), - ("{n} days ago", "{n} 日前"), - ("{n} weeks ago", "{n} 週間前"), - ("{n} months ago", "{n} か月前"), - ("{n} years ago", "{n} 年前"), - ("Browse…", "参照…"), - ("Not saved yet", "まだ保存されていません"), - ("Location", "場所"), - ("Sheets", "シート"), - ("Protect Workbook", "ブックの保護"), - ( - "Sheet structure is locked: sheets can't be added, moved, renamed or deleted.", - "シートの構成はロックされています。シートの追加、移動、名前の変更、削除はできません。", - ), - ( - "Lock the sheet structure so sheets can't be added, moved, renamed or deleted.", - "シートの構成をロックして、シートの追加、移動、名前の変更、削除をできないようにします。", - ), - ("Properties", "プロパティ"), - ("Title", "タイトル"), - ("Subject", "件名"), - ("Author", "作成者"), - ("Company", "会社名"), - ("Keywords", "キーワード"), - ("Description", "説明"), - ("No workbook is open.", "ブックが開かれていません。"), - ("Save as type", "ファイルの種類"), - ("Excel Workbook (*.xlsx)", "Excel ブック (*.xlsx)"), - ("CSV UTF-8 (Comma delimited) (*.csv)", "CSV UTF-8 (コンマ区切り) (*.csv)"), - ("Web Page (*.html)", "Web ページ (*.html)"), - ("PDF (*.pdf)", "PDF ファイル (*.pdf)"), - ("Download", "ダウンロード"), - ( - "A CSV file, web page or PDF is a copy: the workbook keeps its own file.", - "CSV ファイル、Web ページ、PDF はコピーです。ブックは元のファイルのままです。", - ), - ("Save the workbook under another name or in another folder.", "ブックを別の名前または別のフォルダーに保存します。"), - ("Print Active Sheets", "作業中のシートを印刷"), - ("Print Entire Workbook", "ブック全体を印刷"), - ("Fit Sheet on One Page", "シートを 1 ページに印刷"), - ("Page Setup…", "ページ設定…"), - ("Page {page} of {pages}", "{page} / {pages} ページ"), - ("Nothing to print.", "印刷する内容がありません。"), - ("Previous page", "前のページ"), - ("Next page", "次のページ"), - ("Create PDF", "PDF の作成"), - ("Save the sheet as a PDF document that keeps its page layout.", "ページ レイアウトを保ったまま、シートを PDF ドキュメントとして保存します。"), - ("Change File Type", "ファイルの種類の変更"), - ("CSV (Comma delimited)", "CSV (コンマ区切り)"), - ("Save the active sheet as comma-separated text.", "作業中のシートをコンマ区切りのテキストとして保存します。"), - ("Web Page (.html)", "Web ページ (.html)"), - ("Save the workbook as a web page.", "ブックを Web ページとして保存します。"), - ("Options|General", "全般"), - ("Options|Save", "保存"), - ("Show formula bar", "数式バーを表示する"), - ("Enable AutoComplete for cell values", "セルの値にオートコンプリートを使用する"), - ("Save .xlsx workbooks automatically after every change", "変更のたびに .xlsx ブックを自動的に保存する"), - ("Workbook calculation", "ブックの計算"), - ("Enable iterative calculation", "反復計算を行う"), - ("Maximum Iterations:", "最大反復回数:"), - ("Maximum Change:", "変化の最大値:"), - ("Maximum Iterations must be a whole number from 1 to 32767.", "最大反復回数には 1 から 32767 までの整数を指定してください。"), - ("Maximum Change must be a number of 0 or more.", "変化の最大値には 0 以上の数値を指定してください。"), - ("Calculation settings are saved with the workbook.", "計算の設定はブックと共に保存されます。"), -]; - /// `name` lowercased with the Latin accents that occur in language names folded away, so a /// byte-wise sort reads alphabetically. Other scripts are left as they are. fn name_sort_key(name: &str) -> String { diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rs index 2df4cfc11a..1a9ea50d45 100644 --- a/crates/ui-egui/src/i18n/ja.rs +++ b/crates/ui-egui/src/i18n/ja.rs @@ -411,6 +411,10 @@ pub(super) const CORE: &[(&str, &str)] = &[ ("Save .xlsx workbooks automatically after every change", "変更のたびに .xlsx ブックを自動的に保存する"), ("Workbook calculation", "ブックの計算"), ("Enable iterative calculation", "反復計算を行う"), + ("Maximum Iterations:", "最大反復回数:"), + ("Maximum Change:", "変化の最大値:"), + ("Maximum Iterations must be a whole number from 1 to 32767.", "最大反復回数には 1 から 32767 までの整数を指定してください。"), + ("Maximum Change must be a number of 0 or more.", "変化の最大値には 0 以上の数値を指定してください。"), ("Calculation settings are saved with the workbook.", "計算の設定はブックと共に保存されます。"), ]; From 66e69cb9466095f8b87380c5bcc47049c26d3f0f Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:01:21 -0400 Subject: [PATCH 139/687] Engine: a linked paste carries the file's trust whatever it pastes Paste Link writes a formula into every cell whatever `what` says, so a linked paste of formats from a file now makes the destination FromFile as a paste of formulas does. Column widths and notes write no cells, linked or not, and still carry nothing. Regression test for the three kinds. --- crates/engine/src/cmd/edit.rs | 5 +++-- crates/engine/src/tests/seams.rs | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 5e30f14f52..f3ac6270af 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1262,9 +1262,10 @@ fn paste_special(s: &mut Session, p: &Json) -> Result { })?; // Formulas and validation from an untrusted file leave the destination untrusted; plain values, // formats (cell styles and merged areas, no conditional formats), column widths and notes - // carry nothing active (a linked paste of values still writes formulas). + // carry nothing active. A linked paste writes a formula into every cell whatever `what` says, + // except for column widths and notes, which write no cells at all. let passive = - matches!(what, What::Formats | What::ColumnWidths | What::Comments) || (!link && matches!(what, What::Values | What::ValuesAndNumberFormats)); + matches!(what, What::ColumnWidths | What::Comments) || (!link && matches!(what, What::Formats | What::Values | What::ValuesAndNumberFormats)); if !same_doc && !passive { let d = s.doc_mut()?; d.trust = d.trust.receive(clip.trust); diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index a5cd0d8685..d3e9f78db8 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -731,3 +731,25 @@ fn pasting_from_a_file_carries_its_trust() { assert_eq!(Trust::FromFile.receive(Trust::Local), Trust::FromFile); assert_eq!(Trust::Local.receive(Trust::Local), Trust::Local); } + +#[test] +fn a_linked_paste_carries_the_files_trust_whatever_it_pastes() { + // Paste Link writes a formula into every cell whatever `what` says, so a linked paste of + // formats from a file carries the file's trust like one of formulas. Column widths and notes + // write no cells, linked or not. + let mut s = s(); + open_json(&mut s, &gridcraft_model::Workbook::new()); + set(&mut s, "A1", "=1+1"); + s.execute("edit.copy", json!({"range": "A1"})).unwrap(); + let b1 = |s: &Session| s.doc().unwrap().wb.sheet(0).unwrap().cell(gridcraft_core::CellRef::parse("B1").unwrap()).cloned(); + for what in ["columnWidths", "comments"] { + s.new_workbook(); + s.execute("edit.pasteSpecial", json!({"what": what, "link": true, "at": "B1"})).unwrap(); + assert_eq!(trust(&s), Trust::Local, "{what}"); + assert!(b1(&s).is_none(), "a linked paste of {what} writes no cell"); + } + s.new_workbook(); + s.execute("edit.pasteSpecial", json!({"what": "formats", "link": true, "at": "B1"})).unwrap(); + assert!(b1(&s).is_some_and(|c| c.formula.is_some()), "a linked paste of formats writes a formula"); + assert_eq!(trust(&s), Trust::FromFile); +} From 6b7c48f1191e6de61e82ca6c4cf6517e3de7f977 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:01:21 -0400 Subject: [PATCH 140/687] Engine: a stash chunk number with leading zeros names someone else's name `_gc_watch_00` parsed as chunk 0, so a hidden one was read as the item's first chunk and a visible one stopped `_gc_watch_0` from being written. Only the chunk number as `set` writes it counts now. Regression test. --- crates/engine/src/cmd/stash.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index 683c42c2dd..3c401a4dd6 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -130,8 +130,10 @@ fn item_of(n: &DefinedName) -> Option<(&str, Option)> { fn parse_name(name: &str) -> Option<(&str, Option)> { if let Some(rest) = strip_prefix_ci(name, PREFIX) { let (key, k) = rest.rsplit_once('_')?; - let all_digits = !k.is_empty() && k.len() <= 9 && k.bytes().all(|b| b.is_ascii_digit()); - if key.is_empty() || !all_digits { + // Only the chunk number as `set` writes it: `_gc_watch_00` is someone else's name, not + // chunk 0 under another spelling, so it is neither read nor in the way of `_gc_watch_0`. + let canonical = !k.is_empty() && k.len() <= 9 && k.bytes().all(|b| b.is_ascii_digit()) && (k == "0" || !k.starts_with('0')); + if key.is_empty() || !canonical { return None; } return k.parse().ok().map(|i| (key, Some(i))); @@ -358,6 +360,23 @@ mod tests { assert_eq!(wb.names, vec![own]); } + #[test] + fn a_chunk_number_with_leading_zeros_is_another_name() { + // `_gc_watch_00` is not chunk 0 under another spelling: a hidden one is not read, and a + // visible one is not in the way of writing `_gc_watch_0`. + let mut wb = Workbook::new(); + wb.names.push(name("_gc_watch_00", "\"theirs\"")); + assert_eq!(get(&wb, "watch"), None); + assert!(keys(&wb, "").is_empty()); + let own = DefinedName { hidden: false, ..name("_gc_watch_00", "\"mine\"") }; + wb.names.push(own.clone()); + set(&mut wb, "watch", "ours").unwrap(); + assert_eq!(get(&wb, "watch").as_deref(), Some("ours")); + assert_eq!(wb.names.len(), 3, "both other names stay"); + remove(&mut wb, "watch"); + assert_eq!(wb.names, vec![name("_gc_watch_00", "\"theirs\""), own]); + } + #[test] fn keys_list_items_by_prefix() { let mut wb = Workbook::new(); From a3b5f8e45c0fc82f21ea56dfdb8909dd7143ba47 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:08:40 -0400 Subject: [PATCH 141/687] Engine: a command that panics leaves the undo history as it found it The panic guard restored the workbook and selection but not the history, so a nested edit made before the panic left a no-op undo step and the redo history it had cleared stayed lost. The guard now truncates the steps made during the command and puts the redo history back. Regression test, and the architecture note says what the guard restores. --- crates/engine/src/lib.rs | 12 +++++++++++- crates/engine/src/tests/seams.rs | 22 ++++++++++++++++++++++ docs/architecture.md | 3 ++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ad740e3f63..9f79dd37ff 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -532,6 +532,9 @@ impl Session { let (first_group, group_epoch) = (self.next_group, self.group_epoch); // Whether a group was open on the document before the command, and how long its history was. let start = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (self.in_group(*uid), d.undo.len()))); + // A command that panics leaves the history as it found it: the steps commands nested in it + // made go with the workbook, and the redo history they cleared comes back. + let redo_before = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| d.redo.clone())); // The recorder keeps top-level calls with the place they ran in. let place = (self.depth == 0 && self.recorder.is_some()).then(|| match self.active() { Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), @@ -551,13 +554,20 @@ impl Session { Err(p) => { let msg = p.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| p.downcast_ref::().cloned()).unwrap_or_else(|| "panic".into()); - // Restore the workbook as it was, and drop the undo groups the command opened. + // Restore the workbook and its history as they were, and drop the undo groups the + // command opened. self.groups.retain(|g| g.id < first_group); if let Some((uid, wb, sel)) = &before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == *uid) { d.wb = wb.clone(); d.selection = sel.clone(); + if let Some((_, len)) = start { + d.undo.truncate(len); + } + if let Some(redo) = redo_before { + d.redo = redo; + } d.calc.rebuild(&d.wb); } if self.depth == 0 { diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index d3e9f78db8..dedd5c67b9 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -543,6 +543,28 @@ fn a_group_ended_at_the_history_limit_replaces_only_the_oldest_step() { assert_undo_redo(&mut s, &[("F1", "one"), ("F2", "two")]); } +#[test] +fn a_command_that_panics_leaves_the_history_as_it_found_it() { + // A command that edits through a nested command and then panics: the nested step is gone + // with the workbook it changed, and the redo history the nested step cleared comes back. + crate::cmd::register_undoable_test_command("test.editThenPanic", |s, _| { + put(s, "A1", "half done")?; + panic!("after an edit"); + }); + let mut s = s(); + set(&mut s, "A1", "1"); + set(&mut s, "A1", "2"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!((undo_labels(&s).len(), s.doc().unwrap().redo.len()), (1, 1)); + let err = s.execute("test.editThenPanic", json!({})).unwrap_err().to_string(); + assert!(err.contains("after an edit"), "{err}"); + assert_eq!(v(&s, "A1").display(), "1", "the workbook is restored"); + assert_eq!(undo_labels(&s), ["Enter Cell"], "no step for the nested edit"); + assert_eq!(s.doc().unwrap().redo.len(), 1, "the redo history is back"); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(v(&s, "A1").display(), "2"); +} + // ---------------------------------------------------------------- recording buffer fn recorded_ids(s: &Session) -> Vec { diff --git a/docs/architecture.md b/docs/architecture.md index e44e800839..aa5e1e64d0 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -33,7 +33,8 @@ including tests, 2026-10-10. `Session::execute(id, params)`. Commands are `CommandSpec { id, label, menu, shortcut, params, enabled, run, journal, undoable }`; ids follow Excel's ribbon (`home.bold`, `insert.chart`, `data.sortAscending`) plus primitives (`cell.set`, `selection.set`, `range.setValues`). - `Session::execute` catches escaped panics and restores the workbook (never-crash guard). + `Session::execute` catches escaped panics and restores the workbook, its selection and its + undo and redo history as they were before the command (never-crash guard). 2. **Edits** change the model through copy-on-write bands; undo keeps the previous snapshot. Undo retains at most 100 steps and trims older steps when its estimated retained cell storage exceeds 128 MiB. The newest step remains undoable even if that step alone exceeds the budget. From 456cf73886f361a25f5ff30503fe833ff75a9b3a Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:08:40 -0400 Subject: [PATCH 142/687] Engine: a headless call that fails for want of a file is not recorded file.open with no file succeeds by queuing a dialog, which execute_headless turns into an error for callers with no UI; the recording kept the step. Recorder::truncate forgets it and keeps the byte count right. Regression test. --- crates/engine/src/lib.rs | 7 +++++++ crates/engine/src/record.rs | 9 +++++++++ crates/engine/src/tests/seams.rs | 13 +++++++++++++ 3 files changed, 29 insertions(+) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 9f79dd37ff..bf11000824 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -826,10 +826,17 @@ impl Session { /// queued a file dialog because it got no file (`file.open`, `file.saveAs`, `file.save` on a workbook with no /// file yet, `data.getData`) is an error instead of a silent success. pub fn execute_headless(&mut self, id: &str, params: Json) -> Result { + let recorded = self.recorder.as_ref().map(|r| (r.steps.len(), r.dropped)); let r = self.execute(id, params); let requests = self.take_ui_requests(); let asked_for_file = requests.iter().any(|q| matches!(q, UiRequest::Dialog(name, _) if name == "open" || name == "saveAs")); if r.is_ok() && asked_for_file { + // The caller gets an error, so the recording keeps no step for the command. + if let Some((steps, dropped)) = recorded + && let Some(rec) = self.recorder.as_mut() + { + rec.truncate(steps, dropped); + } let what = match id { "file.open" => "`path` or `base64`", "data.getData" | "data.fromTextCsv" => "`path` or `text`", diff --git a/crates/engine/src/record.rs b/crates/engine/src/record.rs index fefd072347..5b8100ed43 100644 --- a/crates/engine/src/record.rs +++ b/crates/engine/src/record.rs @@ -58,4 +58,13 @@ impl Recorder { self.dropped = self.dropped.saturating_add(1); } } + + /// Forgets the steps after the first `steps` and puts the dropped count back to `dropped`, + /// for a command the caller received as failed after it was recorded. + pub fn truncate(&mut self, steps: usize, dropped: usize) { + for step in self.steps.drain(steps.min(self.steps.len())..) { + self.bytes = self.bytes.saturating_sub(serde_json::to_vec(&step).map_or(0, |v| v.len())); + } + self.dropped = dropped; + } } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index dedd5c67b9..8a87279d3f 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -571,6 +571,19 @@ fn recorded_ids(s: &Session) -> Vec { s.recorder.as_ref().unwrap().steps.iter().map(|st| st.id.clone()).collect() } +#[test] +fn a_headless_call_that_fails_for_want_of_a_file_is_not_recorded() { + // `file.open` with no file queues a dialog and succeeds; a headless caller gets an error for + // it instead, so the recording keeps no step for it either. + let mut s = s(); + s.start_recording(); + set(&mut s, "A1", "1"); + assert!(s.execute_headless("file.open", json!({})).is_err()); + assert_eq!(recorded_ids(&s), ["cell.set"]); + let rec = s.stop_recording().unwrap(); + assert_eq!(rec.dropped, 0); +} + #[test] fn the_recorder_keeps_top_level_calls_with_where_they_ran() { let mut s = s(); From d1e51a4af4d991522f221b1a8dac5ebc156db5dc Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:08:40 -0400 Subject: [PATCH 143/687] Engine: a later stash chunk on its own is not a listed item stash::keys reported a key that get read as missing, because only a later chunk of it existed. keys now counts chunk 0 and legacy names only, as get does. Regression assertions in the hostile-names test. --- crates/engine/src/cmd/stash.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index 3c401a4dd6..bbef7f3ec3 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -103,7 +103,8 @@ pub fn keys(wb: &Workbook, prefix: &str) -> Vec { let mut keys: Vec = wb .names .iter() - .filter_map(|n| item_of(n).map(|(k, _)| k.to_ascii_lowercase())) + // A later chunk on its own is no item, as `get` reads it. + .filter_map(|n| item_of(n).filter(|(_, chunk)| chunk.is_none_or(|i| i == 0)).map(|(k, _)| k.to_ascii_lowercase())) .filter(|k| k.starts_with(&prefix.to_ascii_lowercase())) .collect(); keys.sort(); @@ -294,11 +295,13 @@ mod tests { wb.names.push(name("_gc_gap_0", "\"a\"")); wb.names.push(name("_gc_gap_2", "\"c\"")); assert_eq!(get(&wb, "gap").as_deref(), Some("a")); - // Only a later chunk: no item, and the legacy name is still read. + // Only a later chunk: no item, not listed, and the legacy name is still read. wb.names.push(name("_gc_orphan_2", "\"c\"")); assert_eq!(get(&wb, "orphan"), None); + assert_eq!(keys(&wb, "orphan"), Vec::::new()); wb.names.push(name("_sc_orphan", "\"old\"")); assert_eq!(get(&wb, "orphan").as_deref(), Some("old")); + assert_eq!(keys(&wb, "orphan"), ["orphan"]); // Chunks out of order, a duplicate, junk around the literals, an unterminated literal. wb.names.push(name("_gc_mix_1", "\"b\"&A1&")); wb.names.push(name("_gc_mix_0", "SUM(\"a\")")); From 8440427c530cb4bc0b2e1ec43e3a4b5ceb03ffad Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:15:05 -0400 Subject: [PATCH 144/687] Tests: a linked paste of column widths or notes copies its metadata and no cell The source now carries a note and a column width, and the test checks that the destination received them while its trust stays Local and no cell is written. --- crates/engine/src/tests/seams.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 8a87279d3f..cb0902cdfe 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -775,16 +775,26 @@ fn a_linked_paste_carries_the_files_trust_whatever_it_pastes() { let mut s = s(); open_json(&mut s, &gridcraft_model::Workbook::new()); set(&mut s, "A1", "=1+1"); + s.execute("review.newComment", json!({"cell": "A1", "text": "theirs"})).unwrap(); + let wide = gridcraft_model::LineInfo { size: Some(123.0), ..Default::default() }; + std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).sheet_mut(0).unwrap().cols.insert(0, wide); s.execute("edit.copy", json!({"range": "A1"})).unwrap(); - let b1 = |s: &Session| s.doc().unwrap().wb.sheet(0).unwrap().cell(gridcraft_core::CellRef::parse("B1").unwrap()).cloned(); + let b1 = gridcraft_core::CellRef::parse("B1").unwrap(); + let sheet = |s: &Session| s.doc().unwrap().wb.sheet(0).unwrap().clone(); for what in ["columnWidths", "comments"] { s.new_workbook(); s.execute("edit.pasteSpecial", json!({"what": what, "link": true, "at": "B1"})).unwrap(); assert_eq!(trust(&s), Trust::Local, "{what}"); - assert!(b1(&s).is_none(), "a linked paste of {what} writes no cell"); + let sh = sheet(&s); + assert!(sh.cell(b1).is_none(), "a linked paste of {what} writes no cell"); + if what == "comments" { + assert_eq!(sh.comments.get(&b1).map(|c| c.text.as_str()), Some("theirs")); + } else { + assert_eq!(sh.cols.get(&1).and_then(|c| c.size), Some(123.0)); + } } s.new_workbook(); s.execute("edit.pasteSpecial", json!({"what": "formats", "link": true, "at": "B1"})).unwrap(); - assert!(b1(&s).is_some_and(|c| c.formula.is_some()), "a linked paste of formats writes a formula"); + assert!(sheet(&s).cell(b1).is_some_and(|c| c.formula.is_some()), "a linked paste of formats writes a formula"); assert_eq!(trust(&s), Trust::FromFile); } From d7c3c18345f5e172340906c3bb0529db17dc31a3 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 20:47:40 -0400 Subject: [PATCH 145/687] Engine: the panic guard restores entries a nested Undo removed Truncating the undo history to its length at the command's start cannot bring back an entry that a nested edit.undo popped. The guard now keeps the undo and redo history as they were and puts both back. The regression test gains the undo-then-panic case. --- crates/engine/src/lib.rs | 11 +++++------ crates/engine/src/tests/seams.rs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index bf11000824..aaa8db4f61 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -533,8 +533,9 @@ impl Session { // Whether a group was open on the document before the command, and how long its history was. let start = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (self.in_group(*uid), d.undo.len()))); // A command that panics leaves the history as it found it: the steps commands nested in it - // made go with the workbook, and the redo history they cleared comes back. - let redo_before = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| d.redo.clone())); + // made or undid, and the redo history they cleared, come back. A length is not enough, + // because a nested `edit.undo` removes entries. + let history_before = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (d.undo.clone(), d.redo.clone()))); // The recorder keeps top-level calls with the place they ran in. let place = (self.depth == 0 && self.recorder.is_some()).then(|| match self.active() { Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), @@ -562,10 +563,8 @@ impl Session { { d.wb = wb.clone(); d.selection = sel.clone(); - if let Some((_, len)) = start { - d.undo.truncate(len); - } - if let Some(redo) = redo_before { + if let Some((undo, redo)) = history_before { + d.undo = undo; d.redo = redo; } d.calc.rebuild(&d.wb); diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index cb0902cdfe..9c9aeb6ad4 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -563,6 +563,20 @@ fn a_command_that_panics_leaves_the_history_as_it_found_it() { assert_eq!(s.doc().unwrap().redo.len(), 1, "the redo history is back"); s.execute("edit.redo", json!({})).unwrap(); assert_eq!(v(&s, "A1").display(), "2"); + + // A command that undoes through a nested command and then panics: the entry Undo removed + // comes back, and the redo entry it made goes. + crate::cmd::register_undoable_test_command("test.undoThenPanic", |s, _| { + s.execute("edit.undo", json!({}))?; + panic!("after an undo"); + }); + assert_eq!((undo_labels(&s).len(), s.doc().unwrap().redo.len()), (2, 0)); + assert!(s.execute("test.undoThenPanic", json!({})).is_err()); + assert_eq!(v(&s, "A1").display(), "2", "the workbook is restored"); + assert_eq!(undo_labels(&s), ["Enter Cell", "Enter Cell"], "the undone step is back"); + assert_eq!(s.doc().unwrap().redo.len(), 0); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(v(&s, "A1").display(), "1"); } // ---------------------------------------------------------------- recording buffer From 378ce3a5a08c69b22d0897afc918c06b9077dd31 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:28:05 -0400 Subject: [PATCH 146/687] Docs: the macro page describes this version The inputs paragraph described how a run binds inputs and the message it shows, which the runner adds in a later pull request. It now says what the declaration is for and that this version stores and checks it. --- docs/macros.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index 1c755e3295..65160fad4a 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -1,6 +1,6 @@ # Macros -> **Last updated:** 2026-10-21 · **Change:** major (created: the macro format and storage) +> **Last updated:** 2026-10-24 · **Change:** major (created: the macro format and storage) GridCraft macros are typed, declarative lists of commands, not VBA. Every step runs a GridCraft command (the same ids the ribbon, CLI and MCP use) or one of three bounded control steps. A macro @@ -38,9 +38,8 @@ version. `{"command": "cell.set", "params": {…}}` is accepted for a command step. - **Inputs**: `kind` (`cell` or `range`), `rows` and `cols` as `[min, max]` (0 for no maximum; rows exclude the header), `header`, `colTypes` (`number`, `text`, `date`, `bool`, `any`), - `nonEmpty` and `defaultRef`. A run binds each input to the range it is given, its default, or - (for the first input) the selection, and refuses with messages that name the cells: - "Input column 2 (Amount) must hold numbers; C4 holds text." + `nonEmpty` and `defaultRef`. These declare what a range must look like for the macro to work + on it. This version stores and checks the declaration; it does not bind inputs or run macros. - **Limits**: 1,000 macros a workbook, 10,000 steps a macro, and Repeat and If nested 4 deep. ## Storage From 3ca12525715a7feee8e265152ac3cb4def8e6995 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:36:09 -0400 Subject: [PATCH 147/687] Engine: a group a panicking command ended keeps its step A group open before a command and ended or rolled back inside it lost its undo step when the command then panicked: the restored history held no entry for the group's changes, so Undo skipped the state before the group. The panic path now pushes the outermost such group's step from its start to the restored workbook. The group stays ended, because its token went with the command. Regression test for a group ended and one rolled back. --- crates/engine/src/lib.rs | 18 +++++++++++++++ crates/engine/src/tests/seams.rs | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index aaa8db4f61..09e39523be 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -536,6 +536,13 @@ impl Session { // made or undid, and the redo history they cleared, come back. A length is not enough, // because a nested `edit.undo` removes entries. let history_before = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (d.undo.clone(), d.redo.clone()))); + // The groups open on the document before the command (usually none), for the same reason. + let groups_before: Vec<(u64, String, Arc, Selection)> = match &before { + Some((uid, ..)) => { + self.groups.iter().filter(|g| g.doc == *uid).map(|g| (g.id, g.label.clone(), g.wb.clone(), g.selection.clone())).collect() + } + None => Vec::new(), + }; // The recorder keeps top-level calls with the place they ran in. let place = (self.depth == 0 && self.recorder.is_some()).then(|| match self.active() { Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), @@ -558,6 +565,7 @@ impl Session { // Restore the workbook and its history as they were, and drop the undo groups the // command opened. self.groups.retain(|g| g.id < first_group); + let group_open = before.as_ref().is_some_and(|(uid, ..)| self.groups.iter().any(|g| g.doc == *uid)); if let Some((uid, wb, sel)) = &before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == *uid) { @@ -567,6 +575,16 @@ impl Session { d.undo = undo; d.redo = redo; } + // A group open before the command and ended or rolled back inside it stays + // ended (its token went with the command), and keeps a step from its start to + // the restored workbook, so Undo still returns to the state before the group. + if !group_open + && let Some((_, label, wb, selection)) = groups_before.into_iter().min_by_key(|g| g.0) + && !Arc::ptr_eq(&wb, &d.wb) + { + push_entry(&mut d.undo, label, wb, &d.wb, selection); + d.redo.clear(); + } d.calc.rebuild(&d.wb); } if self.depth == 0 { diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 9c9aeb6ad4..48105da25f 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -579,6 +579,45 @@ fn a_command_that_panics_leaves_the_history_as_it_found_it() { assert_eq!(v(&s, "A1").display(), "1"); } +#[test] +fn a_group_ended_by_a_command_that_then_panics_keeps_its_step() { + // The command takes a token the test left for it, ends or rolls back that group, and panics. + // The group's token went with the command, so the group stays ended; its step runs from the + // group's start to the restored workbook, so Undo still returns to the state before the group. + static TOKEN: std::sync::Mutex> = std::sync::Mutex::new(None); + crate::cmd::register_undoable_test_command("test.endGroupThenPanic", |s, _| { + let token = TOKEN.lock().map_err(|_| EngineError::Other("lock".into()))?.take(); + if let Some(token) = token { + s.group_end(token)?; + } + panic!("after ending a group"); + }); + crate::cmd::register_undoable_test_command("test.rollBackGroupThenPanic", |s, _| { + let token = TOKEN.lock().map_err(|_| EngineError::Other("lock".into()))?.take(); + if let Some(token) = token { + s.group_rollback(token)?; + } + panic!("after rolling back a group"); + }); + for cmd in ["test.endGroupThenPanic", "test.rollBackGroupThenPanic"] { + let mut s = s(); + set(&mut s, "A1", "before"); + let token = s.group_begin("Outer").unwrap(); + set(&mut s, "A1", "inside"); + set(&mut s, "A2", "inside too"); + *TOKEN.lock().unwrap() = Some(token); + assert!(s.execute(cmd, json!({})).is_err()); + let uid = s.doc().unwrap().uid; + assert!(!s.in_group(uid), "{cmd}: the group stays ended"); + assert_eq!(undo_labels(&s), ["Enter Cell", "Outer"], "{cmd}"); + assert_eq!(v(&s, "A1").display(), "inside", "{cmd}: the workbook is as the command found it"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!((v(&s, "A1").display(), v(&s, "A2").display()), ("before".into(), String::new()), "{cmd}"); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(v(&s, "A2").display(), "inside too", "{cmd}"); + } +} + // ---------------------------------------------------------------- recording buffer fn recorded_ids(s: &Session) -> Vec { From 7ee9d77483c124e3ee4bcbf6135b7ca73762169f Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:36:09 -0400 Subject: [PATCH 148/687] Docs: list every paste that carries a file's trust The architecture note named formulas and validation only; it now lists the Paste Special kinds and linked pastes that write formulas, and the kinds that carry nothing. --- docs/architecture.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index aa5e1e64d0..f51f3a61bf 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -66,8 +66,11 @@ Actions made of many commands (macros, a debugger that runs one step per call) u Commands run by other commands are not recorded, and the buffer is independent of the journal (Automate › Record Actions still records through the journal). - **Document trust.** `DocState::trust` is `Local` for workbooks made in the session and - `FromFile` for workbooks opened from a file, and for workbooks that received formulas or - validation pasted from one. Active content in a `FromFile` workbook asks before it runs. This + `FromFile` for workbooks opened from a file, and for workbooks that received a paste from one + that writes formulas or validation: Paste, Paste Special › All, All Except Borders, Formulas, + Formulas and Number Formats or Validation, and any linked paste other than column widths or + notes. Values, formats, column widths and notes carry nothing active. Active content in a + `FromFile` workbook asks before it runs. This is provenance, which no decision changes; `DocState::scripts_trusted` is the separate per-session consent for stored `automate.*` scripts (`automate.trustScripts`). - **Workbook data in hidden names** (`cmd/stash.rs`). An item is split over hidden From 259b6e67e88324780815a75912dc4159be5b472a Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:45:54 -0400 Subject: [PATCH 149/687] Model: Repeat and If nest four deep, as the limit says The depth check refused any step list at the fifth level, so a fourth Repeat or If could not even hold an empty body: three levels nested, not the four MAX_MACRO_DEPTH and the docs state. The check now counts the Repeat or If steps themselves, in the model's limits and in the parser's pass over the JSON. The test asks for four and refuses a fifth. --- crates/engine/src/macros/mod.rs | 8 +++++--- crates/engine/src/macros/tests.rs | 19 ++++++++++++++----- crates/model/src/macros.rs | 7 ++++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 38016049d2..2aa45e12ce 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -53,9 +53,6 @@ fn check_size(m: &Macro) -> Result<(), String> { /// before the steps are deserialized. fn normalize_steps(steps: &mut Json, level: usize) -> Result<(), String> { let Some(list) = steps.as_array_mut() else { return Err("`steps` must be a list.".into()) }; - if level > gridcraft_model::MAX_MACRO_DEPTH { - return Err(format!("Repeat and If steps can nest at most {} deep.", gridcraft_model::MAX_MACRO_DEPTH)); - } for st in list { let Some(o) = st.as_object_mut() else { return Err("Every step must be an object.".into()) }; if !o.contains_key("type") @@ -64,6 +61,11 @@ fn normalize_steps(steps: &mut Json, level: usize) -> Result<(), String> { o.insert("type".into(), json!("command")); o.insert("id".into(), id); } + // A step with a body is a Repeat or an If: one at the top is nested 1 deep. + let nested = ["body", "then", "else"].iter().any(|key| o.contains_key(*key)); + if nested && level > gridcraft_model::MAX_MACRO_DEPTH { + return Err(format!("Repeat and If steps can nest at most {} deep.", gridcraft_model::MAX_MACRO_DEPTH)); + } for key in ["body", "then", "else"] { if let Some(body) = o.get_mut(key) { normalize_steps(body, level + 1)?; diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 7b498f0b88..5fbc6d38c7 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -55,16 +55,25 @@ fn macros_over_the_limits_are_refused() { let flat = |n: usize| json!({"name": "Big", "steps": vec![json!({"command": "cell.set"}); n]}); assert!(parse_macro(&flat(MAX_MACRO_STEPS)).is_ok()); assert!(parse_macro(&flat(MAX_MACRO_STEPS + 1)).unwrap_err().contains("10000 steps")); - // Nested Repeat steps: 4 levels are fine, 5 are not. - let nest = |levels: usize| { + // Nested Repeat steps: 4 Repeats one inside the other are fine (the innermost holds a + // command, or nothing), a 5th is not. + let nest = |repeats: usize| { let mut step = json!({"command": "cell.set"}); - for _ in 1..levels { + for _ in 0..repeats { step = json!({"type": "repeat", "over": "Input", "body": [step]}); } json!({"name": "Deep", "steps": [step]}) }; - assert!(parse_macro(&nest(4)).is_ok()); - assert!(parse_macro(&nest(5)).unwrap_err().contains("4 deep")); + assert!(parse_macro(&nest(MAX_MACRO_DEPTH)).is_ok()); + assert_eq!(parse_macro(&nest(MAX_MACRO_DEPTH)).unwrap().step_count(), MAX_MACRO_DEPTH + 1); + assert!(parse_macro(&nest(MAX_MACRO_DEPTH + 1)).unwrap_err().contains("4 deep")); + let mut empty = json!({"type": "repeat", "over": "Input", "body": []}); + for _ in 1..MAX_MACRO_DEPTH { + empty = json!({"type": "if", "cell": "A1", "op": "eq", "value": 1, "then": [empty], "else": []}); + } + let deep = parse_macro(&json!({"name": "Deep", "steps": [empty]})).unwrap(); + assert_eq!(deep.step_count(), MAX_MACRO_DEPTH); + assert!(deep.check_limits().is_ok(), "the model's check agrees with the parser"); // Far deeper than serde would allow is refused before it is read. assert!(parse_macro(&nest(200)).is_err()); for bad in [ diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs index 8dc657f907..03cb03cace 100644 --- a/crates/model/src/macros.rs +++ b/crates/model/src/macros.rs @@ -81,10 +81,8 @@ impl Macro { return Err(format!("A macro description can be at most {MAX_MACRO_TEXT} characters long.")); } // Count without recursing past the depth limit (a hostile file can nest deeper). + // A Repeat or If at the top is nested 1 deep; one inside it, 2 deep. fn walk(steps: &[Step], level: usize, count: &mut usize) -> Result<(), String> { - if level > MAX_MACRO_DEPTH { - return Err(format!("Repeat and If steps can nest at most {MAX_MACRO_DEPTH} deep.")); - } for s in steps { *count = count.saturating_add(1); if *count > MAX_MACRO_STEPS { @@ -93,6 +91,9 @@ impl Macro { if s.note.chars().count() > MAX_MACRO_TEXT { return Err(format!("A step note can be at most {MAX_MACRO_TEXT} characters long.")); } + if s.kind.children().next().is_some() && level > MAX_MACRO_DEPTH { + return Err(format!("Repeat and If steps can nest at most {MAX_MACRO_DEPTH} deep.")); + } for body in s.kind.children() { walk(body, level + 1, count)?; } From 8219c18b921d573192f01863a602a373f512ea35 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:45:54 -0400 Subject: [PATCH 150/687] Engine: macro storage shares the list, saves in one pass, keeps unknown names, checks once - Workbook::macros is a shared list (Macros, a copy-on-write newtype): a workbook copy made for a cell edit or an undo snapshot holds the same list, and the undo budget charges a list once a snapshot owns its own copy. Before, every mutating command copied every step of every macro, and a hundred undo entries kept a hundred copies the budget never saw. - Saving to xlsx drops the last save's items and writes the macros in one pass each (stash::set_many), instead of a scan of every name per macro. - A hidden _gc_macro__ name with a key this build did not write is kept on open and on save, as a visible one is, instead of being dropped without a warning. - macros::check is the one check for every path that adds a macro (JSON, a file, built in code): the model's limits and the xlsx size limit. Regression tests for each. --- crates/engine/src/cmd/stash.rs | 46 ++++++++++++-------- crates/engine/src/lib.rs | 4 ++ crates/engine/src/macros/mod.rs | 40 +++++++++++------- crates/engine/src/macros/tests.rs | 70 +++++++++++++++++++++++++++++++ crates/model/src/macros.rs | 69 ++++++++++++++++++++++++++++++ crates/model/src/workbook.rs | 6 +-- 6 files changed, 200 insertions(+), 35 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index bbef7f3ec3..e36635aff9 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -8,7 +8,7 @@ //! one name each, `_sc_`; reading accepts that form as well, so those items can move to this //! format later, and a write of the same key replaces it. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashSet}; use gridcraft_model::{DefinedName, Workbook}; use serde_json::Value as Json; @@ -59,28 +59,40 @@ pub fn get(wb: &Workbook, key: &str) -> Option { /// Stores `text` under `key`, replacing what was there (in either format). Empty text removes it. pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { - check_key(key)?; - if text.chars().count() > MAX_ITEM_CHARS { - return Err(EngineError::Other(format!("The data is too large to keep in the workbook ({} MB at most).", MAX_ITEM_CHARS / (1024 * 1024)))); + set_many(wb, &[(key.to_string(), text.to_string())]) +} + +/// Stores every `(key, text)` of `items` as [`set`] would, with the checks done before anything +/// changes and one pass over the names for all of them, so writing a thousand items into a +/// workbook with many thousands of names stays near-linear. +pub fn set_many(wb: &mut Workbook, items: &[(String, String)]) -> Result<()> { + let mut planned = Vec::new(); + let mut keys: HashSet = HashSet::new(); + for (key, text) in items { + check_key(key)?; + if text.chars().count() > MAX_ITEM_CHARS { + return Err(EngineError::Other(format!( + "The data is too large to keep in the workbook ({} MB at most).", + MAX_ITEM_CHARS / (1024 * 1024) + ))); + } + keys.insert(key.to_ascii_lowercase()); + for (i, formula) in chunk_formulas(text).into_iter().enumerate() { + planned.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true }); + } } - let formulas = chunk_formulas(text); - // A visible name of the item's shape is someone's own (see `item_of`); a hidden name beside - // it with the same name would be the one an xlsx file drops. Refuse before changing anything, - // in one pass over the names (a file can carry many thousands). - let chunks = formulas.len() as u64; - let taken = |n: &&DefinedName| { - n.scope.is_none() && !n.hidden && parse_name(&n.name).is_some_and(|(k, i)| k.eq_ignore_ascii_case(key) && i.is_some_and(|i| i < chunks)) - }; - if let Some(n) = wb.names.iter().find(taken) { + // A visible name of an item's shape is someone's own (see `item_of`); a hidden name beside + // it with the same name would be the one an xlsx file drops. Refuse before changing anything. + let names: HashSet = planned.iter().map(|n| n.name.to_ascii_lowercase()).collect(); + if let Some(n) = wb.names.iter().find(|n| n.scope.is_none() && !n.hidden && names.contains(&n.name.to_ascii_lowercase())) { return Err(EngineError::Other(format!( "The workbook has a defined name '{}', which GridCraft needs for its own data. Rename it first.", n.name ))); } - remove(wb, key); - for (i, formula) in formulas.into_iter().enumerate() { - wb.names.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true }); - } + // What was there under these keys goes, in either format, then the new chunks come. + wb.names.retain(|n| !item_of(n).is_some_and(|(k, _)| keys.contains(&k.to_ascii_lowercase()))); + wb.names.extend(planned); Ok(()) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 886e845ce4..4dad22d028 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -224,6 +224,10 @@ fn history_retained_bytes(older: &Workbook, newer: &Workbook) -> usize { let next = newer.sheets.get(index).map_or(&empty, |next| &next.cells); bytes = bytes.saturating_add(sheet.cells.retained_bytes_against(next)); } + // Snapshots share the macro list until a change to it; a snapshot with its own list keeps it. + if !older.macros.shares(&newer.macros) { + bytes = bytes.saturating_add(older.macros.estimated_bytes()); + } bytes } diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 2aa45e12ce..5a779758d8 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -35,11 +35,18 @@ pub fn parse_macro(v: &Json) -> Result { } let mut m: Macro = serde_json::from_value(v).map_err(|e| format!("This isn't a valid macro: {e}"))?; m.name = m.name.trim().to_string(); - m.check_limits()?; - check_size(&m)?; + check(&m)?; Ok(m) } +/// Every check a macro must pass before it is kept in a workbook: the model's limits +/// ([`Macro::check_limits`]) and the size an xlsx file can hold. Every path that adds a macro, +/// from JSON, from a file or built in code, goes through this one check. +pub fn check(m: &Macro) -> Result<(), String> { + m.check_limits()?; + check_size(m) +} + /// Refuses a macro too large to keep in an xlsx file ([`stash::MAX_ITEM_CHARS`] characters of /// JSON), so saving a workbook never fails because of one of its macros. fn check_size(m: &Macro) -> Result<(), String> { @@ -89,17 +96,18 @@ pub fn find(wb: &Workbook, name: &str) -> Option { /// A copy of `wb` for xlsx with each macro in hidden names (`_gc_macro__`) instead of the /// field, or `None` when there is nothing to change. pub fn with_names(wb: &Workbook) -> crate::Result> { - let stale = !stash::keys(wb, MACRO_KEY).is_empty(); - if wb.macros.is_empty() && !stale { + // Only the numbered items are this build's: a hidden `_gc_macro__` name with some + // other key stays, as `load` leaves it. + let stored = |n: &DefinedName| macro_item(n).is_some_and(|(_, i)| i.is_some()); + if wb.macros.is_empty() && !wb.names.iter().any(stored) { return Ok(None); } let mut out = wb.clone(); - for key in stash::keys(&out, MACRO_KEY) { - stash::remove(&mut out, &key); - } - for (i, m) in wb.macros.iter().enumerate() { - stash::set(&mut out, &format!("{MACRO_KEY}{i}"), ¯o_json(m).to_string())?; - } + // One pass drops the items of the last save, and one writes the macros (a file can hold a + // thousand macros and many thousands of names). + out.names.retain(|n| !stored(n)); + let items: Vec<(String, String)> = wb.macros.iter().enumerate().map(|(i, m)| (format!("{MACRO_KEY}{i}"), macro_json(m).to_string())).collect(); + stash::set_many(&mut out, &items)?; out.macros.clear(); Ok(Some(out)) } @@ -125,8 +133,9 @@ pub fn load(wb: &mut Workbook) -> Vec { match macro_item(&n) { None => kept.push(n), Some((key, Some(i))) => stored.entry((i, key)).or_default().push(n), - // A `macro_` item that isn't numbered isn't one this build wrote; it goes unread. - Some((_, None)) => {} + // A `macro_` item that isn't numbered isn't one this build wrote: it stays as it is, + // unread, and a save keeps it. + Some((_, None)) => kept.push(n), } } wb.names = kept; @@ -154,7 +163,7 @@ pub fn load(wb: &mut Workbook) -> Vec { let checked = m.check_limits().and_then(|()| { m.name = taken.unique(m.name.trim()); // The macro as it is kept, renamed, must still pass, or the next save would lose it. - m.check_limits().and_then(|()| check_size(&m)) + check(&m) }); match checked { Ok(()) => { @@ -186,8 +195,9 @@ fn macro_item(n: &DefinedName) -> Option<(String, Option)> { Some((key, i)) } -/// `name`, or `name (2)`, `name (3)`… if a macro already has it. A long name is shortened to make -/// room for the suffix, so the result stays within [`gridcraft_model::MAX_MACRO_NAME`] characters. +/// `name`, or `name (2)`, `name (3)`… if a macro already has it. A free name comes back as it is; +/// a renamed one is shortened to make room for the suffix, so it stays within +/// [`gridcraft_model::MAX_MACRO_NAME`] characters. The result still goes through [`check`]. pub fn unique_name(wb: &Workbook, name: &str) -> String { let mut taken = Taken::default(); for m in &wb.macros { diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 5fbc6d38c7..77e9fb1069 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -149,6 +149,76 @@ fn a_visible_name_that_looks_like_macro_storage_is_left_alone() { assert_eq!(wb.names, vec![name]); } +#[test] +fn a_hidden_macro_name_this_build_did_not_write_is_kept() { + // `_gc_macro_index_0` is not a numbered item: it is neither read nor dropped on open, and a + // save with macros keeps it beside the items written. + let mut wb = Workbook::new(); + let theirs = hidden("_gc_macro_index_0", "{}"); + wb.names.push(theirs.clone()); + wb.macros.push(sample_macro("Totals")); + let (raw, _) = gridcraft_xlsx::read_xlsx(&crate::io::save_bytes(&wb, "book.xlsx").unwrap()).unwrap(); + let names = names_of(&raw); + assert!(names.contains(&"_gc_macro_index_0".to_string()) && names.contains(&"_gc_macro_0_0".to_string()), "{names:?}"); + let (back, warnings) = crate::io::open_bytes("book.xlsx", &crate::io::save_bytes(&wb, "book.xlsx").unwrap()).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(back.macros, wb.macros); + assert_eq!(names_of(&back), ["_gc_macro_index_0"]); +} + +#[test] +fn workbook_snapshots_share_the_macro_list_until_it_changes() { + // An undo snapshot or a copy made for a cell edit holds the same list, not a copy of every + // step; the undo budget charges a list only once it is a snapshot's own. + let mut wb = Workbook::new(); + wb.macros.push(sample_macro("Totals")); + let snapshot = wb.clone(); + assert!(wb.macros.shares(&snapshot.macros)); + assert_eq!(crate::history_retained_bytes(&snapshot, &wb), std::mem::size_of::()); + wb.macros.push(sample_macro("More")); + assert!(!wb.macros.shares(&snapshot.macros), "a write copies the list first"); + assert_eq!(snapshot.macros.len(), 1); + assert!(crate::history_retained_bytes(&snapshot, &wb) > std::mem::size_of::()); + // The copy is per list, not per workbook copy: cloning again shares the new list. + let again = wb.clone(); + assert!(wb.macros.shares(&again.macros)); + let rebuilt: Macros = wb.macros.iter().cloned().collect(); + assert_eq!(rebuilt, wb.macros); + assert_eq!(rebuilt.into_iter().count(), 2); +} + +#[test] +fn a_macro_built_in_code_goes_through_the_size_check_too() { + // `check` is the one check for every path, so a macro that passes it never makes a save fail. + let big = Macro { + name: "Big".into(), + steps: vec![Step::command("cell.set", json!({"input": "x".repeat(crate::cmd::stash::MAX_ITEM_CHARS)}))], + ..Macro::default() + }; + assert!(big.check_limits().is_ok(), "the model's limits say nothing about the size"); + assert!(check(&big).unwrap_err().contains("too large")); + let mut wb = Workbook::new(); + wb.macros.push(big); + assert!(crate::io::save_bytes(&wb, "book.xlsx").is_err(), "a macro that fails `check` fails the save"); +} + +#[test] +fn a_save_refuses_a_clashing_visible_name_before_changing_anything() { + let mut wb = Workbook::new(); + wb.macros.push(sample_macro("A")); + wb.macros.push(sample_macro("B")); + let own = DefinedName { hidden: false, ..hidden("_GC_MACRO_1_0", "mine") }; + wb.names.push(own.clone()); + wb.names.push(hidden("_gc_macro_7_0", "stale")); + let err = crate::io::save_bytes(&wb, "book.xlsx").unwrap_err().to_string(); + assert!(err.contains("_GC_MACRO_1_0"), "{err}"); + assert_eq!(wb.names.len(), 2, "nothing changed"); + wb.names.remove(0); + let (raw, _) = gridcraft_xlsx::read_xlsx(&crate::io::save_bytes(&wb, "book.xlsx").unwrap()).unwrap(); + let names = names_of(&raw); + assert_eq!(names, ["_gc_macro_0_0", "_gc_macro_1_0"], "the stale item went in the same pass"); +} + #[test] fn scripts_and_vba_stay_as_they_are() { // Automate scripts keep their own storage, and a VBA project is carried through unchanged. diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs index 03cb03cace..92afac61ad 100644 --- a/crates/model/src/macros.rs +++ b/crates/model/src/macros.rs @@ -121,6 +121,75 @@ impl Macro { } } +/// The macros of a workbook. Workbook snapshots share the list until one of them changes it, so +/// an undo entry or a copy made for a cell edit holds the same list rather than a copy of every +/// step. Reads go through `Deref` to the list; a write through `DerefMut` first copies the list +/// when another snapshot shares it. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Macros(std::sync::Arc>); + +impl Macros { + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + /// Whether both hold the one list, with no copy made between them. + pub fn shares(&self, other: &Macros) -> bool { + std::sync::Arc::ptr_eq(&self.0, &other.0) + } + + /// A rough size of the list in memory, for the undo budget. + pub fn estimated_bytes(&self) -> usize { + self.0.iter().fold(0usize, |bytes, m| { + let text = m.name.len().saturating_add(m.description.len()); + let steps = m.step_count().saturating_mul(std::mem::size_of::().saturating_add(64)); + bytes.saturating_add(std::mem::size_of::()).saturating_add(text).saturating_add(steps) + }) + } +} + +impl From> for Macros { + fn from(list: Vec) -> Macros { + Macros(std::sync::Arc::new(list)) + } +} + +impl std::ops::Deref for Macros { + type Target = Vec; + fn deref(&self) -> &Vec { + &self.0 + } +} + +impl std::ops::DerefMut for Macros { + fn deref_mut(&mut self) -> &mut Vec { + std::sync::Arc::make_mut(&mut self.0) + } +} + +impl IntoIterator for Macros { + type Item = Macro; + type IntoIter = std::vec::IntoIter; + fn into_iter(self) -> Self::IntoIter { + std::sync::Arc::unwrap_or_clone(self.0).into_iter() + } +} + +impl<'a> IntoIterator for &'a Macros { + type Item = &'a Macro; + type IntoIter = std::slice::Iter<'a, Macro>; + fn into_iter(self) -> Self::IntoIter { + self.0.iter() + } +} + +impl FromIterator for Macros { + fn from_iter>(iter: I) -> Macros { + Macros(std::sync::Arc::new(iter.into_iter().collect())) + } +} + /// Refuses a format version newer than [`MACRO_VERSION`]: this build would drop the fields it /// doesn't know, and a save would then write the rest back as if it were the whole macro. pub fn check_version(v: u64) -> Result<(), String> { diff --git a/crates/model/src/workbook.rs b/crates/model/src/workbook.rs index 57acd2a8db..5a47b75ed7 100644 --- a/crates/model/src/workbook.rs +++ b/crates/model/src/workbook.rs @@ -181,8 +181,8 @@ pub struct Workbook { pub csv_delimiter: Option, /// GridCraft macros (typed command lists, not VBA), in the order the macro list shows them. /// The JSON format keeps them as this field; xlsx keeps them in hidden defined names. - #[serde(default, skip_serializing_if = "Vec::is_empty", skip_deserializing)] - pub macros: Vec, + #[serde(default, skip_serializing_if = "crate::macros::Macros::is_empty", skip_deserializing)] + pub macros: crate::macros::Macros, /// The `macros` entries of a JSON file as read, before the engine checks each one on its own /// and moves it into `macros`, so one damaged or newer macro doesn't stop the file opening. /// Never written. @@ -234,7 +234,7 @@ impl Workbook { phonetics: vec![], vba_project: None, csv_delimiter: None, - macros: vec![], + macros: crate::macros::Macros::default(), macros_read: vec![], preserved: None, source_dir: None, From 712bb766ecdf8063d28f74f94303bd5efa63ce6e Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:46:25 -0400 Subject: [PATCH 151/687] Engine: a grouped action that panics is rolled back before the panic goes on Session::grouped ran its action without a panic guard, so a caller outside a command that caught the panic found the group still open: Undo and Redo disabled, the partial edits kept, and the next grouped action nested in the abandoned group. The group is now rolled back and the panic resumed. Regression test. --- crates/engine/src/lib.rs | 12 +++++++++--- crates/engine/src/tests/seams.rs | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 09e39523be..8a8d8bcfa6 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -680,20 +680,26 @@ impl Session { pub fn grouped(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result) -> Result { let token = self.group_begin(label)?; let doc = token.doc(); - match f(self) { - Ok(r) => match self.group_end(token) { + // A panic in `f` rolls the group back before it goes on, so a caller that catches it + // (outside `execute`, which drops a command's groups itself) finds no group left open. + match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self))) { + Ok(Ok(r)) => match self.group_end(token) { Ok(_) => Ok(r), // `f` closed the group's document (`file.close`, or `file.open` taking the place of // an untouched new workbook), and the group went with it: nothing is left to undo. Err(_) if !self.docs.iter().any(|d| d.uid == doc) => Ok(r), Err(e) => Err(e), }, - Err(e) => { + Ok(Err(e)) => { // The error from `f` is the one to report. A rollback fails only when the group // has already ended, and then there is nothing left to undo. let _ = self.group_rollback(token); Err(e) } + Err(panic) => { + let _ = self.group_rollback(token); + std::panic::resume_unwind(panic) + } } } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 48105da25f..0f1bc30722 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -579,6 +579,31 @@ fn a_command_that_panics_leaves_the_history_as_it_found_it() { assert_eq!(v(&s, "A1").display(), "1"); } +#[test] +fn a_grouped_action_that_panics_is_rolled_back_before_the_panic_goes_on() { + // Outside a command, a caller that catches the panic finds the group rolled back and no + // group left open, so Undo is enabled and the next grouped action does not nest. + let mut s = s(); + set(&mut s, "A1", "before"); + let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + s.grouped::<()>("Doomed", |s| { + set(s, "A1", "half done"); + panic!("inside the group"); + }) + })); + assert!(caught.is_err(), "the panic goes on to the caller"); + let uid = s.doc().unwrap().uid; + assert!(!s.in_group(uid)); + assert_eq!(v(&s, "A1").display(), "before"); + assert_eq!(undo_labels(&s), ["Enter Cell"]); + s.grouped("Next", |s| { + set(s, "A1", "after"); + Ok(()) + }) + .unwrap(); + assert_eq!(undo_labels(&s), ["Enter Cell", "Next"]); +} + #[test] fn a_group_ended_by_a_command_that_then_panics_keeps_its_step() { // The command takes a token the test left for it, ends or rolls back that group, and panics. From b47c83f6aa3b730c9bc2fce74913ee1121f199cf Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:48:12 +0000 Subject: [PATCH 152/687] Reject unmatched '.' and ',' in NUMBERVALUE text Found while testing crates/functions/src/text.rs:326-340 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/functions/src/text.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/functions/src/text.rs b/crates/functions/src/text.rs index b9b06e608f..2d7fca13ae 100644 --- a/crates/functions/src/text.rs +++ b/crates/functions/src/text.rs @@ -334,6 +334,8 @@ fn numbervalue(a: &[Arg], c: &mut dyn Ctx) -> R { if seen_dec { return Err(CellError::Value); } + } else if c == '.' || c == ',' { + return Err(CellError::Value); } else { out.push(c); } @@ -1216,6 +1218,8 @@ mod tests { close(ev("NUMBERVALUE", vec![t(" 1 000 ")]), 1000.0); close(ev("NUMBERVALUE", vec![t("")]), 0.0); is_err(ev("NUMBERVALUE", vec![t("1.2.3")]), CellError::Value); + is_err(ev("NUMBERVALUE", vec![t("1.5"), t(","), t(" ")]), CellError::Value); + is_err(ev("NUMBERVALUE", vec![t("1,5"), t("."), t(" ")]), CellError::Value); is_text(ev("FIXED", vec![n(1234.567), n(1.0)]), "1,234.6"); is_text(ev("FIXED", vec![n(1234.567), n(-1.0)]), "1,230"); is_text(ev("FIXED", vec![n(-1234.567), n(1.0), b(true)]), "-1234.6"); From 4672b6da51b5cf89093e919f7fe1720c69dc4fd9 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 21:48:53 -0400 Subject: [PATCH 153/687] Model: an input name that reads as a cell reference is refused A macro could declare an input called Q1, AB12 or Sum1, and a step written with plain A1 text, as a recorded one is, would then resolve to the input's range instead of the cell. check_limits refuses such names; the docs say so. Regression test. --- crates/engine/src/macros/tests.rs | 14 ++++++++++++++ crates/model/src/macros.rs | 4 ++++ docs/macros.md | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/macros/tests.rs b/crates/engine/src/macros/tests.rs index 77e9fb1069..99235ec687 100644 --- a/crates/engine/src/macros/tests.rs +++ b/crates/engine/src/macros/tests.rs @@ -149,6 +149,20 @@ fn a_visible_name_that_looks_like_macro_storage_is_left_alone() { assert_eq!(wb.names, vec![name]); } +#[test] +fn input_names_that_read_as_cell_references_are_refused() { + // A recorded step holds plain A1 text; an input called `Q1` would take its place. + let with_input = |name: &str| json!({"name": "M", "inputs": [{"name": name}]}); + for name in ["Q1", "AB12", "Sum1", "a1"] { + let err = parse_macro(&with_input(name)).unwrap_err(); + assert!(err.contains("reads as a cell reference"), "{name}: {err}"); + } + for name in ["Amount", "Q", "Sales_Q1", "Total1a"] { + assert!(parse_macro(&with_input(name)).is_ok(), "{name}"); + } + assert!(parse_macro(&with_input("Row")).unwrap_err().contains("token of its own")); +} + #[test] fn a_hidden_macro_name_this_build_did_not_write_is_kept() { // `_gc_macro_index_0` is not a numbered item: it is neither read nor dropped on open, and a diff --git a/crates/model/src/macros.rs b/crates/model/src/macros.rs index 92afac61ad..54f975b040 100644 --- a/crates/model/src/macros.rs +++ b/crates/model/src/macros.rs @@ -113,6 +113,10 @@ impl Macro { if ["Output", "Row", "Column", "Name"].iter().any(|r| r.eq_ignore_ascii_case(n)) { return Err(format!("'{n}' is a token of its own and can't name an input.")); } + // A step written with plain A1 text (a recorded one) must keep meaning the cell. + if gridcraft_core::RangeRef::parse(n).is_some() { + return Err(format!("'{n}' reads as a cell reference and can't name an input.")); + } if self.inputs.iter().take(k).any(|o| o.name.eq_ignore_ascii_case(n)) { return Err(format!("Two inputs are called '{n}'.")); } diff --git a/docs/macros.md b/docs/macros.md index 65160fad4a..f98435bb53 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -39,7 +39,10 @@ version. - **Inputs**: `kind` (`cell` or `range`), `rows` and `cols` as `[min, max]` (0 for no maximum; rows exclude the header), `header`, `colTypes` (`number`, `text`, `date`, `bool`, `any`), `nonEmpty` and `defaultRef`. These declare what a range must look like for the macro to work - on it. This version stores and checks the declaration; it does not bind inputs or run macros. + on it. An input's name is letters, digits and `_`, starting with a letter, and is neither a + token of its own (`Output`, `Row`, `Column`, `Name`) nor something that reads as a cell + reference (`Q1`), so a step written with plain A1 text keeps meaning the cell. This version + stores and checks the declaration; it does not bind inputs or run macros. - **Limits**: 1,000 macros a workbook, 10,000 steps a macro, and Repeat and If nested 4 deep. ## Storage From 00b63468e6f529ae32d3e9f551e877b34a33d7e6 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:02:45 -0400 Subject: [PATCH 154/687] Engine: macro tokens resolve only where they mean a reference, and refuse instead of guessing Six fixes from a review of the resolver, each with a regression test that fails without it: - A bare Row or Column under a general reference key (to, from, at, area, source, target, destination, location) stays text: Change Spelling's replacement word and a pivot field's area were being rewritten into the current row's A1 range. Row.col(2), {{Row}} and {"$ref": "Row"} still resolve there, and the bare word still resolves under cell and range keys. - {{{{ in a string is a literal {{, so text that needs the braces can be written; an unknown token is still an error. - Input.data on a range that is only a header row, and Name: of a table with no data rows, are refused instead of reading the header. - below(0), below(-3) and right(0) are refused like resize(0, 1) is, instead of becoming 1. - A defined name on a sheet whose name holds a dollar sign resolves: the dollar anchors are stripped from the reference part only. - quote_sheet is the engine's one rule (gridcraft_core::quote_sheet), which quotes a sheet named like a cell reference, an R1C1 token or a boolean and leaves a period unquoted. docs/macros.md says which keys take a bare token and how to write a literal brace. --- crates/engine/src/macros/resolve.rs | 167 ++++++++++++++++++++++++---- docs/macros.md | 11 +- 2 files changed, 156 insertions(+), 22 deletions(-) diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs index b3cf438fa1..03b9988da9 100644 --- a/crates/engine/src/macros/resolve.rs +++ b/crates/engine/src/macros/resolve.rs @@ -109,6 +109,19 @@ pub fn is_ref_key(key: &str) -> bool { REF_KEYS.iter().any(|r| r.eq_ignore_ascii_case(key)) || ["cell", "cells", "range", "ranges", "ref"].iter().any(|end| k.ends_with(end)) } +/// Reference keys that other commands use for plain words: `to` is a replacement word in +/// Review › Change Spelling, `area` is `row` or `column` in a pivot field move. Under these a +/// bare `Row` or `Column` stays text; `Row.col(2)`, `{{Row}}` or `{"$ref": "Row"}` is the token. +fn is_general_key(key: &str) -> bool { + ["area", "areas", "at", "destination", "from", "location", "source", "sources", "target", "to"].iter().any(|k| k.eq_ignore_ascii_case(key)) +} + +/// `Row` or `Column` on its own, with no method or sheet after it. +fn is_bare_item_word(text: &str) -> bool { + let t = text.trim(); + t.eq_ignore_ascii_case("row") || t.eq_ignore_ascii_case("column") +} + /// The most nesting [`resolve_params`] walks into. const MAX_JSON_DEPTH: usize = 64; @@ -159,13 +172,15 @@ fn plain_ref(t: &str, env: &Env) -> R { /// A defined name (by its formula) or a table (its data rows). fn named(name: &str, env: &Env) -> R { if let Some(n) = env.wb.name(name, env.active_sheet) { - let f = n.formula.trim().trim_start_matches('=').replace('$', ""); - let (sheet, body) = crate::cmd::split_sheet(&f); + // The sheet part keeps its characters (a sheet may be called `US$`); only the reference + // loses its `$` anchors. + let f = n.formula.trim().trim_start_matches('='); + let (sheet, body) = crate::cmd::split_sheet(f); let si = match sheet { Some(s) => env.wb.sheet_index(&s), None => n.scope.or(Some(env.active_sheet)), }; - if let (Some(sheet), Some(range)) = (si, RangeRef::parse(body)) { + if let (Some(sheet), Some(range)) = (si, RangeRef::parse(&body.replace('$', ""))) { return Ok(Place { sheet, range }); } return bad(format!("The name '{name}' doesn't refer to a range.")); @@ -173,7 +188,12 @@ fn named(name: &str, env: &Env) -> R { if let Some((si, ti)) = env.wb.table(name) && let Some(t) = env.wb.sheet(si).and_then(|sh| sh.tables.get(ti)) { - return Ok(Place { sheet: si, range: t.data_range().unwrap_or(t.range) }); + // A table stands for its data rows; one with none (a header, perhaps a totals row) has + // nothing to give. + let Some(range) = t.data_range() else { + return bad(format!("The table '{name}' has no data rows.")); + }; + return Ok(Place { sheet: si, range }); } bad(format!("There is no name or table called '{name}'.")) } @@ -244,19 +264,23 @@ pub fn resolve_token(text: &str, env: &Env) -> R { RangeRef::new(r.start, shift(r.start, h - 1, w - 1, t)?) } ("data", []) => { - if header && r.height() > 1 { + if header { + // A header row with nothing under it has no data rows to give. + if r.height() < 2 { + return bad(format!("'{t}': the range is only a header row, with no data under it.")); + } RangeRef::new(CellRef::new(r.start.row + 1, r.start.col), r.end) } else { r } } ("below", []) | ("below", [Arg::Int(_)]) => { - let n = arg_or_one(&args); + let n = count_arg(&args, t)?; let top = shift(CellRef::new(r.end.row, r.start.col), 1, 0, t)?; RangeRef::new(top, shift(CellRef::new(r.end.row, r.end.col), n, 0, t)?) } ("right", []) | ("right", [Arg::Int(_)]) => { - let n = arg_or_one(&args); + let n = count_arg(&args, t)?; let left = shift(CellRef::new(r.start.row, r.end.col), 0, 1, t)?; RangeRef::new(left, shift(r.end, 0, n, t)?) } @@ -279,10 +303,12 @@ pub fn resolve_token(text: &str, env: &Env) -> R { Ok(place) } -fn arg_or_one(args: &[Arg]) -> i64 { +/// The count `below(n)` and `right(n)` take: 1 when left out, and never less than 1. +fn count_arg(args: &[Arg], t: &str) -> R { match args.first() { - Some(Arg::Int(n)) => (*n).max(1), - _ => 1, + Some(Arg::Int(n)) if *n < 1 => bad(format!("'{t}': the count must be at least 1.")), + Some(Arg::Int(n)) => Ok(*n), + _ => Ok(1), } } @@ -453,11 +479,9 @@ pub fn a1(place: Place, env: &Env) -> String { /// `Sheet1` or `'My Sheet'` (quotes doubled). pub fn quote_sheet(name: &str) -> String { - if !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_') && !name.starts_with(|c: char| c.is_ascii_digit()) { - name.to_string() - } else { - format!("'{}'", name.replace('\'', "''")) - } + // The engine's one rule: a name that reads as a cell reference, an R1C1 token or a boolean + // is quoted too, or `Q1!A1` would be read as a reference to cell Q1. + gridcraft_core::quote_sheet(name) } /// Step parameters with every token replaced by A1 text. @@ -485,7 +509,7 @@ fn walk(v: &Json, key: Option<&str>, env: &Env, depth: usize) -> R { // Items of a list keep their key's kind (`ranges: ["Input", …]`). Json::Array(a) => Json::Array(a.iter().map(|x| walk(x, key, env, depth + 1)).collect::>>()?), Json::String(s) => { - if key.is_some_and(is_ref_key) && is_token(s, env) { + if key.is_some_and(|k| is_ref_key(k) && !(is_general_key(k) && is_bare_item_word(s))) && is_token(s, env) { Json::String(a1(resolve_token(s, env)?, env)) } else { Json::String(interpolate(s, env)?) @@ -495,7 +519,8 @@ fn walk(v: &Json, key: Option<&str>, env: &Env, depth: usize) -> R { }) } -/// Replaces each `{{token}}` in `s`. +/// Replaces each `{{token}}` in `s`. `{{{{` is a literal `{{`, for text that needs the braces +/// themselves (`"Dear {{{{name}}"` writes `Dear {{name}}`). pub fn interpolate(s: &str, env: &Env) -> R { if !s.contains("{{") { return Ok(s.to_string()); @@ -504,6 +529,11 @@ pub fn interpolate(s: &str, env: &Env) -> R { let mut rest = s; while let Some(i) = rest.find("{{") { out.push_str(rest.get(..i).unwrap_or("")); + if rest.get(i..i + 4) == Some("{{{{") { + out.push_str("{{"); + rest = rest.get(i + 4..).unwrap_or(""); + continue; + } let after = rest.get(i + 2..).unwrap_or(""); let Some(j) = after.find("}}") else { out.push_str(rest.get(i..).unwrap_or("")); @@ -793,8 +823,107 @@ mod tests { #[test] fn sheet_names_are_quoted_when_needed() { - for (name, want) in [("Sheet1", "Sheet1"), ("My Data", "'My Data'"), ("O'Hara", "'O''Hara'"), ("2024", "'2024'"), ("", "''")] { - assert_eq!(quote_sheet(name), want); + // The engine's rule: a name that reads as a cell reference, an R1C1 token or a boolean + // is quoted too, and a period on its own is not a reason to quote. + for (name, want) in [ + ("Sheet1", "Sheet1"), + ("My Data", "'My Data'"), + ("O'Hara", "'O''Hara'"), + ("2024", "'2024'"), + ("", "''"), + ("Q1", "'Q1'"), + ("R1C1", "'R1C1'"), + ("TRUE", "'TRUE'"), + ("Sales.Q1", "Sales.Q1"), + ] { + assert_eq!(quote_sheet(name), want, "{name}"); + } + } + + #[test] + fn a_bare_row_or_column_under_a_general_key_is_text() { + // `to` is a replacement word in Change Spelling and `area` is `row` or `column` in a + // pivot field move, so a bare `Row` or `Column` there stays text, inside a Repeat or not; + // the token forms still resolve there, and the bare word resolves under `range`. + let wb = wb(); + let inputs = vec![("Input".to_string(), place("B2:D11"), true)]; + for item in [None, Some((RepeatOf::Rows, place("B5:D5")))] { + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: Some(place("F2")), item }; + let p = json!({"word": "Colum", "to": "Column", "area": "row", "from": "Input", "at": "Output"}); + let r = resolve_params(&p, &env).unwrap(); + assert_eq!(r, json!({"word": "Colum", "to": "Column", "area": "row", "from": "B2:D11", "at": "F2"}), "{item:?}"); + } + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: Some((RepeatOf::Rows, place("B5:D5"))) }; + let p = json!({"to": "Row.col(2)", "range": "Row", "area": {"$ref": "Row"}, "text": "{{Row}}"}); + let r = resolve_params(&p, &env).unwrap(); + assert_eq!(r, json!({"to": "C5", "range": "B5:D5", "area": "B5:D5", "text": "B5:D5"})); + } + + #[test] + fn a_header_row_alone_and_a_table_without_data_rows_are_refused() { + let mut wb = wb(); + let sh = wb.sheet_mut(0).unwrap(); + sh.tables.push(gridcraft_model::Table { + id: 1, + name: "Empty".into(), + range: RangeRef::parse("H1:J1").unwrap(), + header_row: true, + totals_row: false, + columns: Vec::new(), + style: String::new(), + banded_rows: false, + banded_cols: false, + first_col: false, + last_col: false, + filter_button: false, + filter: Vec::new(), + table_type: None, + connection_id: None, + }); + let inputs = vec![("Input".to_string(), place("B2:D2"), true), ("Plain".to_string(), place("B2:D2"), false)]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None }; + assert!(resolve_ref("Input.data", &env).unwrap_err().to_string().contains("only a header row")); + assert!(resolve_ref("Input.data.col(2)", &env).unwrap_err().to_string().contains("only a header row")); + check(&env, "Plain.data", "B2:D2"); + check(&env, "Input.col(2)", "C2"); + assert!(resolve_ref("Name:Empty", &env).unwrap_err().to_string().contains("no data rows")); + } + + #[test] + fn doubled_braces_are_literal_braces() { + let wb = wb(); + let inputs = vec![("Input".to_string(), place("B2:D11"), true)]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None }; + assert_eq!(interpolate("Dear {{{{name}},", &env).unwrap(), "Dear {{name}},"); + assert_eq!(interpolate("{{{{ {{Input.cell(1,1)}} }}", &env).unwrap(), "{{ B2 }}"); + assert!(interpolate("Dear {{name}},", &env).is_err(), "an unknown token is still an error"); + } + + #[test] + fn a_defined_name_on_a_sheet_with_a_dollar_sign_resolves() { + let mut wb = wb(); + wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("US$"))); + wb.names.push(gridcraft_model::DefinedName { + name: "Dollars".into(), + scope: None, + formula: "'US$'!$F$20".into(), + comment: String::new(), + hidden: false, + }); + let env = Env { wb: &wb, active_sheet: 0, inputs: &[], output: None, item: None }; + check(&env, "Name:Dollars", "'US$'!F20"); + } + + #[test] + fn below_and_right_refuse_a_count_under_one() { + let wb = wb(); + let inputs = vec![("Input".to_string(), place("B2:D11"), true)]; + let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None }; + check(&env, "Input.below", "B12:D12"); + check(&env, "Input.below(2)", "B12:D13"); + check(&env, "Input.right(1)", "E2:E11"); + for bad in ["Input.below(0)", "Input.below(-3)", "Input.right(0)"] { + assert!(resolve_ref(bad, &env).unwrap_err().to_string().contains("at least 1"), "{bad}"); } } } diff --git a/docs/macros.md b/docs/macros.md index dd14951bc0..6daf54c9ed 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -63,9 +63,14 @@ exists is used and methods follow it. Write a token as the whole value of a reference parameter (`cell`, `range`, `target`, keys ending in `Cell` or `Range`…), as `{"$ref": "Input.col(2)"}` anywhere, or as `{{Input.col(2)}}` inside -any string, such as a formula. Plain A1 references stay absolute; a `Name:` anchor follows -inserted rows. A reference with a sheet name is never a token, so `Output!A1` is a cell on a sheet -called Output, even when a token or input has that name. +any string, such as a formula; `{{{{` in a string is a literal `{{`. Under the general keys that +other commands use for plain words (`to`, `from`, `at`, `area`, `source`, `target`, +`destination`, `location`), a bare `Row` or `Column` stays text (a replacement word, a pivot +area); write `Row.col(2)`, `{{Row}}` or `{"$ref": "Row"}` there. Plain A1 references stay +absolute; a `Name:` anchor follows inserted rows. A reference with a sheet name is never a token, +so `Output!A1` is a cell on a sheet called Output, even when a token or input has that name. +`Input.data` on a range that is only a header row, and `Name:` of a table with no data rows, are +refused rather than read as the header. ## Storage From b389d9067a46539540bc6007b7bb81630d80bff9 Mon Sep 17 00:00:00 2001 From: Helen Kwok <107752876+helenkwok@users.noreply.github.com> Date: Sat, 24 Oct 2026 10:17:32 +0800 Subject: [PATCH 155/687] XLSX: write accounting formats 42 and 44 with their code builtin_id already wrote the currency formats 5-8 as custom formats because readers resolve those ids by locale. The accounting formats with a currency, 42 and 44, are resolved the same way but were written as bare ids, so LibreOffice showed a 44 cell holding -0.14 as "- 0.14" where US Excel shows "$ (0.14)". Treat them like 5-8. --- crates/xlsx/src/tables.rs | 7 +-- crates/xlsx/src/tests/locale_numfmt.rs | 61 ++++++++++++++++++++++++++ crates/xlsx/src/tests/mod.rs | 1 + 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 crates/xlsx/src/tests/locale_numfmt.rs diff --git a/crates/xlsx/src/tables.rs b/crates/xlsx/src/tables.rs index 141d8673a2..23c936c4a2 100644 --- a/crates/xlsx/src/tables.rs +++ b/crates/xlsx/src/tables.rs @@ -46,10 +46,11 @@ pub fn builtin_format(id: u32) -> Option<&'static str> { BUILTIN_FORMATS.iter().find(|(i, _)| *i == id).map(|(_, c)| *c) } -/// Id to write for a format code when it is a locale-independent built-in (currency formats 5–8 -/// are written as custom formats because their meaning depends on the reader's locale). +/// Id to write for a format code when it is a locale-independent built-in. The currency formats +/// 5–8 and the accounting formats with a currency, 42 and 44, are written as custom formats because +/// their meaning depends on the reader's locale. pub fn builtin_id(code: &str) -> Option { - BUILTIN_FORMATS.iter().find(|(i, c)| *c == code && !(5..=8).contains(i)).map(|(i, _)| *i) + BUILTIN_FORMATS.iter().find(|(i, c)| *c == code && !matches!(i, 5..=8 | 42 | 44)).map(|(i, _)| *i) } pub use gridcraft_core::color::INDEXED; diff --git a/crates/xlsx/src/tests/locale_numfmt.rs b/crates/xlsx/src/tests/locale_numfmt.rs new file mode 100644 index 0000000000..3259a3657e --- /dev/null +++ b/crates/xlsx/src/tests/locale_numfmt.rs @@ -0,0 +1,61 @@ +//! Number formats whose meaning depends on the reader's locale are written with their code: the +//! currency formats 5–8 and the accounting formats with a currency, 42 and 44. Written as a bare +//! built-in id, LibreOffice and Excel in other locales show their own version (a 44 cell holding +//! -0.14 reads `$ (0.14)` in US Excel but `- 0.14` in LibreOffice). + +use std::io::Read; +use std::sync::Arc; + +use gridcraft_core::{CellRef, Value}; +use gridcraft_model::*; + +use crate::tables::builtin_format; +use crate::{read_xlsx, write_xlsx}; + +fn styles_xml(bytes: &[u8]) -> String { + let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap(); + let mut s = String::new(); + zip.by_name("xl/styles.xml").unwrap().read_to_string(&mut s).unwrap(); + s +} + +/// Writes one cell formatted with `code`; returns the `numFmtId` its cell format points at, the +/// `styles.xml` and the code read back. +fn round_trip(code: &str) -> (u32, String, String) { + let mut wb = Workbook::new(); + let style = wb.styles.derive(StyleId::DEFAULT, |s| s.num_fmt = NumFmt::new(code)); + let mut sheet = Sheet::new("Sheet1"); + sheet.cells.set(CellRef::new(0, 0), Cell { value: Value::Number(-0.14), formula: None, style }); + wb.sheets = vec![Arc::new(sheet)]; + let bytes = write_xlsx(&wb).unwrap(); + let xml = styles_xml(&bytes); + let xfs = &xml[xml.find("().unwrap()) + .find(|&id| id != 0) + .unwrap(); + let (back, _) = read_xlsx(&bytes).unwrap(); + let st = back.styles.get(back.sheet(0).unwrap().cell(CellRef::new(0, 0)).unwrap().style); + (id, xml, st.num_fmt.as_str().to_string()) +} + +#[test] +fn currency_and_accounting_formats_are_written_with_their_code() { + for builtin in [5, 6, 7, 8, 42, 44] { + let code = builtin_format(builtin).unwrap(); + let (id, xml, back) = round_trip(code); + assert!(id >= 164, "format {builtin} written as built-in id {id}"); + assert!(xml.contains(&format!(" for id {id}"); + assert_eq!(back, code, "format {builtin}"); + } +} + +#[test] +fn locale_independent_built_ins_keep_their_id() { + for builtin in [3, 4, 10, 14, 41, 43, 49] { + let (id, _, back) = round_trip(builtin_format(builtin).unwrap()); + assert_eq!(id, builtin); + assert_eq!(back, builtin_format(builtin).unwrap()); + } +} diff --git a/crates/xlsx/src/tests/mod.rs b/crates/xlsx/src/tests/mod.rs index c8f199a47a..a7f6f1eb71 100644 --- a/crates/xlsx/src/tests/mod.rs +++ b/crates/xlsx/src/tests/mod.rs @@ -11,6 +11,7 @@ mod encrypted_tests; mod fixtures; mod ink_roundtrip; mod literal_category_roundtrip; +mod locale_numfmt; mod malformed; mod multilevel_category_roundtrip; mod note_box; From e93d15b50a00d87ff70ab1523bf4d604b3ead149 Mon Sep 17 00:00:00 2001 From: Helen Kwok <107752876+helenkwok@users.noreply.github.com> Date: Sat, 24 Oct 2026 10:17:32 +0800 Subject: [PATCH 156/687] Contributors: credit helenkwok as Helen Kwok --- contributors/people.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contributors/people.toml b/contributors/people.toml index 88e496fc7b..43252507dd 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -96,3 +96,6 @@ sync_github_name = true [people.tyrel] real_name = "Tyrel Haveman" + +[people.helenkwok] +real_name = "Helen Kwok" From bbf36000d4ec11d85daa8c0104aacd7ad74475d5 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:08:36 -0400 Subject: [PATCH 157/687] Engine: the recorder counts a dropped step without serializing it Once a step has been dropped, or the step count is full, push serialized each later step only to count it; a file.open step holds a whole file. The checks that need no size come first. --- crates/engine/src/record.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/record.rs b/crates/engine/src/record.rs index 5b8100ed43..7206c21a33 100644 --- a/crates/engine/src/record.rs +++ b/crates/engine/src/record.rs @@ -49,9 +49,14 @@ pub struct Recorder { impl Recorder { /// Adds a step, or counts it as dropped once the buffer is full. pub fn push(&mut self, step: RecordedStep) { + // Once a step has been dropped, or the step count is full, the rest are counted without + // being serialized (a `file.open` step holds a whole file). + if self.dropped > 0 || self.steps.len() >= RECORD_LIMIT { + self.dropped = self.dropped.saturating_add(1); + return; + } let size = serde_json::to_vec(&step).map_or(0, |v| v.len()); - let fits = self.steps.len() < RECORD_LIMIT && self.bytes.saturating_add(size) <= RECORD_MAX_BYTES; - if fits && self.dropped == 0 { + if self.bytes.saturating_add(size) <= RECORD_MAX_BYTES { self.bytes = self.bytes.saturating_add(size); self.steps.push(step); } else { From 43b0e39e8e5ed89c16928454ad2d91b766e22d6b Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:08:49 -0400 Subject: [PATCH 158/687] Engine: set_many refuses one key given twice Two items under one key, in any case, would leave two hidden names of one name, of which an xlsx file keeps one, and get would mix their chunks. A caller's mistake, refused before anything changes. Regression test. --- crates/engine/src/cmd/stash.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index e36635aff9..d714bf6c8e 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -76,7 +76,11 @@ pub fn set_many(wb: &mut Workbook, items: &[(String, String)]) -> Result<()> { MAX_ITEM_CHARS / (1024 * 1024) ))); } - keys.insert(key.to_ascii_lowercase()); + // Two items under one key (names compare without case) would leave two hidden names of + // one name, of which an xlsx file keeps one: a caller's mistake, refused. + if !keys.insert(key.to_ascii_lowercase()) { + return Err(EngineError::Other(format!("`{key}` is given twice."))); + } for (i, formula) in chunk_formulas(text).into_iter().enumerate() { planned.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true }); } @@ -375,6 +379,18 @@ mod tests { assert_eq!(wb.names, vec![own]); } + #[test] + fn set_many_refuses_one_key_twice() { + // Two items under one key, in any case, would leave two hidden names of one name. + let mut wb = Workbook::new(); + wb.names.push(name("_gc_other_0", "\"stays\"")); + let err = set_many(&mut wb, &[("k".into(), "a".into()), ("K".into(), "b".into())]).unwrap_err().to_string(); + assert!(err.contains("given twice"), "{err}"); + assert_eq!(wb.names.len(), 1, "nothing changed"); + set_many(&mut wb, &[("k".into(), "a".into()), ("k2".into(), "b".into())]).unwrap(); + assert_eq!((get(&wb, "k").as_deref(), get(&wb, "K2").as_deref(), get(&wb, "other").as_deref()), (Some("a"), Some("b"), Some("stays"))); + } + #[test] fn a_chunk_number_with_leading_zeros_is_another_name() { // `_gc_watch_00` is not chunk 0 under another spelling: a hidden one is not read, and a From 551eda7e8eed3fe5db97dc1b2742bd93c612bae9 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:08:49 -0400 Subject: [PATCH 159/687] Engine: the xlsx copy gets an empty macro list, not a copy of the shared one cleared out.macros shares its list with the workbook being saved, so clear() copied every macro first. --- crates/engine/src/macros/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/macros/mod.rs b/crates/engine/src/macros/mod.rs index 5a779758d8..f0c83e549d 100644 --- a/crates/engine/src/macros/mod.rs +++ b/crates/engine/src/macros/mod.rs @@ -108,7 +108,8 @@ pub fn with_names(wb: &Workbook) -> crate::Result> { out.names.retain(|n| !stored(n)); let items: Vec<(String, String)> = wb.macros.iter().enumerate().map(|(i, m)| (format!("{MACRO_KEY}{i}"), macro_json(m).to_string())).collect(); stash::set_many(&mut out, &items)?; - out.macros.clear(); + // An empty list of its own, not a copy of the shared one cleared. + out.macros = gridcraft_model::Macros::default(); Ok(Some(out)) } From 461395d0a058f512a10e7293248b13b9ca0437e4 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:09:02 -0400 Subject: [PATCH 160/687] Engine: col and cols keep the header state, so a later data takes the header off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Input.col("date").data resolved to the whole column, header included, because every method cleared the header state. col and cols keep it; the other methods still clear it, and only data keeps the header row for col("…"). The token test asks for D3:D11, cols(2..3).data and col(2).data.col(1). --- crates/engine/src/macros/resolve.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/macros/resolve.rs b/crates/engine/src/macros/resolve.rs index 03b9988da9..4e84c09659 100644 --- a/crates/engine/src/macros/resolve.rs +++ b/crates/engine/src/macros/resolve.rs @@ -288,9 +288,12 @@ pub fn resolve_token(text: &str, env: &Env) -> R { return bad(format!("'{t}': '{name}' with these arguments isn't a token method ({}).", METHODS.join(", "))); } }; - // After a method the result is plain cells: `data` no longer applies, and only `data` - // keeps the header row for `col("…")`. - header = false; + // After a method the result is plain cells, except that `col` and `cols` keep the input's top + // row, so a later `data` still takes the header off; only `data` keeps the header row for + // `col("…")`. + if !matches!(method.as_str(), "col" | "cols") { + header = false; + } if method != "data" { names_row = None; } @@ -615,7 +618,9 @@ mod tests { ("input", "B2:D11"), ("Input.col(2)", "C2:C11"), ("Input.col(\"Amount\")", "C2:C11"), - ("Input.col(\"date\").data", "D2:D11"), + ("Input.col(\"date\").data", "D3:D11"), + ("Input.cols(2..3).data", "C3:D11"), + ("Input.col(2).data.col(1)", "C3:C11"), ("Input.data", "B3:D11"), ("Input.data.col(2)", "C3:C11"), ("Input.data.col(\"Amount\")", "C3:C11"), From 74ff8ac6254c681ae50cd7657119cae9d54c6da9 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:13:18 -0400 Subject: [PATCH 161/687] Engine: macro runs refuse a shift that moves an input, stay clean when nothing changed, take the answer however ask was given, and share one step budget Four fixes from a review of the runner, each with a regression test that fails without it: - A shift inserts cells over the block of every written cell, which moves that block's columns (or rows) from there on. When a bound input lay in the way, its data moved and sheared while the run kept the old coordinates and reported success. The shift is now refused, naming the input and its place. The output is not counted: it is where the macro writes, and the second footprint catches data a shift moved into it. - A dry run or a refused run left a clean workbook dirty: view.zoom, sheet.activate and the view modes move the saved mark to the current workbook on a clean document, and the rollback put the workbook back but not the mark. Saved keeps and restores it. - overwrite: true answers the question however ask was given; with an explicit onConflict: ask the answer was ignored and the question came back forever. - The dry runs and the run share one step budget as they share one clock; each phase counted its own 100,000 steps before. ran reports the run's own steps. --- crates/engine/src/macros/run.rs | 79 +++++++++++++++++------ crates/engine/src/tests/macros.rs | 104 ++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 36fd1ac417..4699a5e7c9 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -769,6 +769,9 @@ fn compare(v: &Value, op: CompareOp, want: &Json) -> bool { #[derive(Clone, Debug, Default, Serialize)] #[serde(rename_all = "camelCase")] pub struct Footprint { + /// Steps executed by the end of the dry run that made this footprint (the run's one budget). + #[serde(skip)] + pub executed: u64, pub sheets: Vec, pub sheets_added: Vec, pub sheets_removed: Vec, @@ -839,6 +842,9 @@ impl Footprint { struct Saved { active: usize, docs: Vec, + /// The workbook as last saved: `sheet.activate` and the view commands move it to the + /// current workbook on a clean document, and the rollback puts the workbook back but not this. + saved: Option>, sheet_selections: Vec, unlocked_ranges: Vec<(String, String)>, trust: Trust, @@ -863,6 +869,7 @@ impl Saved { Saved { active: s.active, docs: s.docs.iter().map(|d| d.uid).collect(), + saved: d.map(|d| d.saved.clone()), sheet_selections: d.map(|d| d.sheet_selections.clone()).unwrap_or_default(), unlocked_ranges: d.map(|d| d.unlocked_ranges.clone()).unwrap_or_default(), trust: d.map_or(Trust::Local, |d| d.trust), @@ -885,6 +892,9 @@ impl Saved { fn restore(self, s: &mut Session, doc: u64) { s.docs.retain(|d| self.docs.contains(&d.uid)); if let Some(d) = s.docs.iter_mut().find(|d| d.uid == doc) { + if let Some(saved) = self.saved { + d.saved = saved; + } d.sheet_selections = self.sheet_selections; d.unlocked_ranges = self.unlocked_ranges; d.trust = self.trust; @@ -908,7 +918,7 @@ impl Saved { /// Runs the macro in dry-run mode on its workbook, puts everything back, and says what it would /// change. The dry run stops once `time_limit_ms` has passed on `clock`. -pub fn footprint(s: &mut Session, p: &Prepared, clock: Stopwatch, time_limit_ms: f64) -> Result { +pub fn footprint(s: &mut Session, p: &Prepared, clock: Stopwatch, time_limit_ms: f64, executed: u64) -> Result { let i = s.docs.iter().position(|d| d.uid == p.doc).ok_or(EngineError::NoDocument)?; s.set_active(i); let saved = Saved::take(s, p.doc); @@ -918,7 +928,7 @@ pub fn footprint(s: &mut Session, p: &Prepared, clock: Stopwatch, time_limit_ms: d.touched = Some(Vec::new()); } let mut m = Machine::new(p, true); - (m.clock, m.time_limit_ms) = (clock, time_limit_ms); + (m.clock, m.time_limit_ms, m.executed) = (clock, time_limit_ms, executed); let outcome = m.run_to_end(s); let (after, touched) = match s.docs.iter_mut().find(|d| d.uid == p.doc) { Some(d) => (d.wb.clone(), d.touched.take().unwrap_or_default()), @@ -929,6 +939,7 @@ pub fn footprint(s: &mut Session, p: &Prepared, clock: Stopwatch, time_limit_ms: rolled?; let (after, touched) = if matches!(m.stopped, Some((_, true))) { (before.clone(), Vec::new()) } else { (after, touched) }; let mut fp = diff(&before, &after, &p.inputs, &touched); + fp.executed = m.executed; fp.error = outcome.err(); fp.stopped = m.stopped.map(|(msg, _)| msg); fp.partial = m.partial.map(|(step, reason)| json!({"step": step, "reason": reason})); @@ -1069,16 +1080,38 @@ pub(crate) fn ranges(cells: &mut [CellRef]) -> Vec { } /// Inserts blank cells where the footprint lands (moving what is there down or right), so the -/// macro writes into empty cells. -fn make_room(s: &mut Session, fp: &Footprint, down: bool) -> Result<()> { +/// macro writes into empty cells. The block is every written cell's, so a row the macro writes +/// stays together. A shift that would move one of the macro's inputs is refused: the inputs were +/// checked and bound where they are, and only the block's columns (or rows) would move. +fn make_room(s: &mut Session, fp: &Footprint, p: &Prepared, down: bool) -> Result<()> { if !fp.sheets_removed.is_empty() || fp.objects_removed > 0 { return Err(EngineError::Other("Shifting can't make room: the macro deletes sheets or objects.".into())); } let wb = s.doc()?.wb.clone(); + let env = Env { wb: &wb, active_sheet: usize::MAX, inputs: &[], output: None, item: None }; + let mut moves = Vec::new(); for sh in fp.sheets.iter().filter(|s| s.conflict_count > 0) { - let Some(bounds) = sh.bounds_range else { continue }; - let env = Env { wb: &wb, active_sheet: usize::MAX, inputs: &[], output: None, item: None }; - let range = resolve::a1(Place { sheet: sh.index, range: bounds }, &env); + let Some(block) = sh.bounds_range else { continue }; + // What the insertion moves: the block's columns from its top row down, or its rows from + // its left column rightwards. + let moved = |r: &RangeRef| { + if down { + r.start.col <= block.end.col && r.end.col >= block.start.col && r.end.row >= block.start.row + } else { + r.start.row <= block.end.row && r.end.row >= block.start.row && r.end.col >= block.start.col + } + }; + // The output is where the macro writes, so moving what is there is the point; the second + // footprint catches data the shift moved into it. + if let Some((name, pl)) = p.inputs.iter().map(|(n, pl, _)| (n.as_str(), *pl)).find(|(_, pl)| pl.sheet == sh.index && moved(&pl.range)) { + return Err(EngineError::Other(format!( + "Shifting the data would move {name} ({}). Run with onConflict overwrite or fail, or make room first.", + resolve::a1(pl, &env) + ))); + } + moves.push(resolve::a1(Place { sheet: sh.index, range: block }, &env)); + } + for range in moves { s.execute("home.insertCells", json!({"range": range, "shift": if down { "down" } else { "right" }}))?; } Ok(()) @@ -1127,7 +1160,9 @@ pub fn make_way(s: &mut Session, b: &Begun) -> Result<(Machine, bool)> { if rule == OnConflict::Overwrite { return Ok((machine, false)); } - let mut fp = footprint(s, &b.prepared, machine.clock, b.time_limit_ms)?; + // The dry runs and the run share one step budget as they share one clock. + let mut fp = footprint(s, &b.prepared, machine.clock, b.time_limit_ms, machine.executed)?; + machine.executed = fp.executed; if let Some(e) = fp.error.take() { return Err(e.into_engine(&m.name)); } @@ -1145,8 +1180,9 @@ pub fn make_way(s: &mut Session, b: &Begun) -> Result<(Machine, bool)> { return Err(EngineError::Other(format!("Macro '{}' would write over cells that hold data ({what}). Nothing was changed.", m.name))); } OnConflict::ShiftDown | OnConflict::ShiftRight => { - make_room(s, &fp, rule == OnConflict::ShiftDown)?; - fp = footprint(s, &b.prepared, machine.clock, b.time_limit_ms)?; + make_room(s, &fp, &b.prepared, rule == OnConflict::ShiftDown)?; + fp = footprint(s, &b.prepared, machine.clock, b.time_limit_ms, machine.executed)?; + machine.executed = fp.executed; if let Some(e) = fp.error.take() { return Err(e.into_engine(&m.name)); } @@ -1176,11 +1212,13 @@ pub fn run(s: &mut Session, name: &str, p: &Json) -> Result { s.grouped(&label, |s| { let (mut machine, shifted) = make_way(s, &b)?; let name = &b.mac.name; + // `ran` counts the run's own steps; the budget also holds the dry runs'. + let before_run = machine.executed; machine.run_to_end(s).map_err(|e| e.into_engine(name))?; match machine.stopped { Some((msg, true)) => Err(EngineError::Other(format!("Macro '{name}' stopped: {msg} Nothing was changed."))), stopped => { - let mut out = json!({"macro": name, "ran": machine.executed, "inputs": b.inputs}); + let mut out = json!({"macro": name, "ran": machine.executed.saturating_sub(before_run), "inputs": b.inputs}); if let Some((msg, _)) = stopped { out["stopped"] = json!(msg); } @@ -1198,17 +1236,18 @@ fn time_limit(p: &Json) -> f64 { p.get("timeLimit").and_then(Json::as_f64).map(|t| (t * 1000.0).clamp(1.0, 600_000.0)).unwrap_or(DEFAULT_TIME_LIMIT_MS) } -/// The conflict rule for this run: `onConflict`, or `overwrite: true` (the answer to the -/// question), else the macro's own. +/// The conflict rule for this run: `onConflict`, else the macro's own. `overwrite: true` is the +/// answer to the question, so it turns `ask` into `overwrite` whichever way `ask` was given. fn conflict_rule(p: &Json, m: &Macro) -> Result { - if let Some(t) = p.get("onConflict").and_then(Json::as_str) { - return OnConflict::parse(t) - .ok_or_else(|| EngineError::Other(format!("onConflict must be ask, fail, overwrite, shiftDown or shiftRight, not '{t}'."))); - } - if p.get("overwrite").and_then(Json::as_bool) == Some(true) { + let rule = match p.get("onConflict").and_then(Json::as_str) { + Some(t) => OnConflict::parse(t) + .ok_or_else(|| EngineError::Other(format!("onConflict must be ask, fail, overwrite, shiftDown or shiftRight, not '{t}'.")))?, + None => m.output.on_conflict, + }; + if rule == OnConflict::Ask && p.get("overwrite").and_then(Json::as_bool) == Some(true) { return Ok(OnConflict::Overwrite); } - Ok(m.output.on_conflict) + Ok(rule) } /// `macro.check`: what running the macro would do, without changing anything. @@ -1247,7 +1286,7 @@ pub fn check(s: &mut Session, name: &str, p: &Json) -> Result { if !asks.is_empty() && !approved { return Ok(out); } - let fp = footprint(s, &prepared, Stopwatch::start(), time_limit(p))?; + let fp = footprint(s, &prepared, Stopwatch::start(), time_limit(p), 0)?; out["ok"] = json!(fp.error.is_none()); out["conflicts"] = json!(fp.conflict_count()); if let Some(e) = &fp.error { diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index a42a60dd7e..d1babe4eb9 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -232,6 +232,110 @@ fn shifting_makes_room_and_is_checked_again() { assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); } +#[test] +fn a_shift_that_would_move_an_input_is_refused() { + // A shift inserts cells over the conflicting block, which moves the block's columns (or rows) + // from there on. When one of the macro's own inputs lies in the way, its data would move and + // shear while the run keeps the old coordinates, so the shift is refused and nothing changes. + let mut s = s(); + data(&mut s); + set(&mut s, "B1", "Old title"); + put( + &mut s, + json!({"name": "Title", "inputs": [{"name": "Input", "header": true}], "steps": [ + {"command": "cell.set", "params": {"cell": "Input.cell(0,1)", "input": "Report"}} + ]}), + ); + let start = s.doc().unwrap().wb.clone(); + let err = s.execute("macro.run", json!({"name": "Title", "inputs": "B2:C5", "onConflict": "shiftDown"})).unwrap_err().to_string(); + assert!(err.contains("would move Input (") && err.contains("B2:C5"), "{err}"); + assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start), "nothing changed"); + assert_eq!((v(&s, "B1").display(), v(&s, "B3").display()), ("Old title".into(), "Ann".into())); + // Writing left of the input and shifting right would move it too. + put( + &mut s, + json!({"name": "Left", "inputs": [{"name": "Input", "header": true}], "steps": [ + {"command": "cell.set", "params": {"cell": "Input.offset(1,-1).cell(1,1)", "input": "x"}} + ]}), + ); + set(&mut s, "A3", "in the way"); + let err = s.execute("macro.run", json!({"name": "Left", "inputs": "B2:C5", "onConflict": "shiftRight"})).unwrap_err().to_string(); + assert!(err.contains("would move Input"), "{err}"); + // The output counts as well. + put( + &mut s, + json!({"name": "Out", "inputs": [{"name": "Input", "header": true}], "output": {"anchor": "Input.below(1)"}, "steps": [ + {"command": "cell.set", "params": {"cell": "Input.cell(0,1)", "input": "Report"}} + ]}), + ); + let err = s.execute("macro.run", json!({"name": "Out", "inputs": "B2:C5", "onConflict": "shiftDown"})).unwrap_err().to_string(); + assert!(err.contains("would move Input"), "{err}"); + // A shift that moves nothing of the macro's own still works, over the block of every written + // cell, so a row the macro writes stays together. + set(&mut s, "H1", "elsewhere"); + put( + &mut s, + json!({"name": "Wide", "inputs": [{"name": "Input", "header": true}], "steps": [ + {"command": "cell.set", "params": {"cell": "H1", "input": "over"}}, + {"command": "cell.set", "params": {"cell": "H9", "input": "far"}} + ]}), + ); + s.execute("macro.run", json!({"name": "Wide", "inputs": "B2:C5", "onConflict": "shiftDown"})).unwrap(); + assert_eq!((v(&s, "H1").display(), v(&s, "H9").display(), v(&s, "H10").display()), ("over".into(), "far".into(), "elsewhere".into())); + assert_eq!(v(&s, "B3").display(), "Ann", "the input did not move"); +} + +#[test] +fn a_dry_run_leaves_a_clean_workbook_clean() { + // `view.zoom` and `sheet.activate` move the saved mark to the current workbook on a clean + // document; the dry run's rollback puts the workbook back, so the mark must come back too. + let mut s = s(); + data(&mut s); + let d = s.doc_mut().unwrap(); + d.saved = d.wb.clone(); + assert!(!s.doc().unwrap().is_dirty()); + put(&mut s, json!({"name": "Look", "steps": [{"command": "view.zoom", "params": {"percent": 120}}]})); + s.execute("macro.check", json!({"name": "Look"})).unwrap(); + assert!(!s.doc().unwrap().is_dirty(), "a dry run changed nothing"); + // A run refused by the conflict rule changed nothing either. + put(&mut s, totals()); + set(&mut s, "E3", "taken"); + let d = s.doc_mut().unwrap(); + d.saved = d.wb.clone(); + assert!(s.execute("macro.run", json!({"name": "Totals", "inputs": "B2:C5", "onConflict": "fail"})).is_err()); + assert!(!s.doc().unwrap().is_dirty()); +} + +#[test] +fn the_answer_to_the_question_counts_however_ask_was_given() { + let mut s = s(); + data(&mut s); + put(&mut s, totals()); + set(&mut s, "E3", "taken"); + let p = json!({"name": "Totals", "inputs": "B2:C5", "onConflict": "ask"}); + assert!(matches!(s.execute("macro.run", p.clone()), Err(EngineError::Confirm(_)))); + let mut answered = p; + answered["overwrite"] = json!(true); + s.execute("macro.run", answered).unwrap(); + assert_eq!(v(&s, "E3").display(), "400"); +} + +#[test] +fn the_step_limit_is_one_budget_across_the_dry_run_and_the_run() { + // 300 x 300 passes is under the limit for one phase; the dry run and the run together are not. + let mut s = s(); + put( + &mut s, + json!({"name": "Spin", "steps": [{"type": "repeat", "over": "A1:A300", "body": [ + {"type": "repeat", "over": "A1:A300", "body": [{"type": "if", "cell": "A1", "op": "empty"}]} + ]}]}), + ); + let err = s.execute("macro.run", json!({"name": "Spin"})).unwrap_err().to_string(); + assert!(err.contains("more than 100000 steps"), "{err}"); + // With no dry run (overwrite), the same macro fits. + s.execute("macro.run", json!({"name": "Spin", "onConflict": "overwrite"})).unwrap(); +} + #[test] fn the_footprint_covers_other_sheets_objects_and_edges() { let mut s = s(); From fb4acb04a2d70a88db7ec0fd0c998f08775cca0c Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:30:14 -0400 Subject: [PATCH 162/687] Engine: closing the debugged workbook ends the debugger file.close is not an edit, so the debugger's lock let it through, and the session kept the debugger with a token for a group that went with the document: the next workbook could not start a debugger, and the state reported that no document was open. close_document (also the path that replaces an untouched blank workbook on file.open) now ends a debugger whose workbook it closes. Regression test. --- crates/engine/src/lib.rs | 4 ++++ crates/engine/src/tests/macros.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 399bb1c885..c1de0c4011 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -454,6 +454,10 @@ impl Session { if i < self.docs.len() { let d = self.docs.remove(i); self.groups.retain(|g| g.doc != d.uid); + // A macro being debugged in this workbook ends with it; its group just went. + if self.macro_debug.as_ref().is_some_and(|m| m.doc == d.uid) { + self.macro_debug = None; + } if self.active >= self.docs.len() { self.active = self.docs.len().saturating_sub(1); } diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 207e4c944c..95e3b632e2 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -232,6 +232,26 @@ fn shifting_makes_room_and_is_checked_again() { assert!(Arc::ptr_eq(&s.doc().unwrap().wb, &start)); } +#[test] +fn closing_the_debugged_workbook_ends_the_debugger() { + // `file.close` is not an edit, so the debugger's lock lets it through; the group went with + // the document, and so must the debugger, or the next workbook could not debug anything and + // the state would report that no document is open. + let mut s = s(); + data(&mut s); + put(&mut s, totals()); + s.execute("macro.debug", json!({"action": "start", "name": "Totals", "inputs": "B2:C5", "breakpoints": ["1"]})).unwrap(); + assert!(s.macro_debug.is_some()); + s.execute("file.close", json!({"force": true})).unwrap(); + assert!(s.macro_debug.is_none(), "the debugger ended with its workbook"); + s.new_workbook(); + data(&mut s); + put(&mut s, totals()); + s.execute("macro.debug", json!({"action": "start", "name": "Totals", "inputs": "B2:C5", "breakpoints": ["1"]})).unwrap(); + assert!(s.execute("macro.debug", json!({})).is_ok(), "the new workbook can be debugged"); + s.execute("macro.debug", json!({"action": "stop"})).unwrap(); +} + #[test] fn the_footprint_covers_other_sheets_objects_and_edges() { let mut s = s(); From a626705c958040ecf0c39af748c97351443e6ab7 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:30:35 -0400 Subject: [PATCH 163/687] Engine: a dry run puts the journal back even after the journal trimmed itself Every nested command a dry run executes is journaled, so a long macro pushes the journal past its limit and the journal drops its oldest entries. The restore cut the journal back to its old length, which then kept entries from the dry run and pointed Record Actions at the wrong place. The session now counts the entries its journal has dropped (journal_dropped) and truncates with its size bookkeeping (truncate_journal); the restore keeps the old length less what was dropped meanwhile and moves the recording's start back by the same count. Regression test with 12,000 nested steps. --- crates/engine/src/lib.rs | 19 +++++++++++++++++++ crates/engine/src/macros/run.rs | 11 +++++++++-- crates/engine/src/tests/macros.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 80084f7ca5..1a89aa2b7c 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -405,6 +405,9 @@ pub struct Session { /// Serialized size of each journal entry, measured once when it is pushed, and their sum. journal_sizes: Vec, journal_bytes: usize, + /// Entries the journal has dropped from its front to stay within its limits, ever; a dry run + /// that puts the journal back needs to know how many went while it ran. + journal_dropped: u64, pub ui_requests: Vec, /// Format painter source style (sticky when double-clicked). pub format_painter: Option<(Arc, usize, RangeRef, bool)>, @@ -529,10 +532,26 @@ impl Session { self.journal.drain(..n); self.journal_sizes.drain(..n); self.journal_bytes = kept; + self.journal_dropped = self.journal_dropped.saturating_add(n as u64); self.recording = self.recording.map(|r| r.saturating_sub(n)); } } + /// Entries dropped from the journal's front so far, for a caller that puts the journal back. + pub(crate) fn journal_dropped(&self) -> u64 { + self.journal_dropped + } + + /// Cuts the journal back to its first `keep` entries, with its size bookkeeping. + pub(crate) fn truncate_journal(&mut self, keep: usize) { + if self.journal_sizes.len() == self.journal.len() { + self.journal_sizes.truncate(keep); + self.journal_bytes = self.journal_sizes.iter().sum(); + } + self.journal.truncate(keep); + self.recording = self.recording.map(|r| r.min(self.journal.len())); + } + pub fn commands(&self) -> Vec { command_specs().iter().map(|c| c.info(self)).collect() } diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 4699a5e7c9..889d3dd0f1 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -851,7 +851,10 @@ struct Saved { clipboard: Option, prefs: crate::Prefs, mode: crate::Mode, + /// The journal's length and how many entries it had dropped from its front; a dry run that + /// pushes past the journal's limits drops more, which a length alone cannot see. journal: usize, + journal_dropped: u64, recording: Option, ui_requests: usize, format_painter: Option<(Arc, usize, RangeRef, bool)>, @@ -877,6 +880,7 @@ impl Saved { prefs: s.prefs.clone(), mode: s.mode, journal: s.journal.len(), + journal_dropped: s.journal_dropped(), recording: s.recording, ui_requests: s.ui_requests.len(), format_painter: s.format_painter.clone(), @@ -903,8 +907,11 @@ impl Saved { s.clipboard = self.clipboard; s.prefs = self.prefs; s.mode = self.mode; - s.journal.truncate(self.journal); - s.recording = self.recording; + // Entries the dry run pushed go; entries the journal dropped meanwhile are gone for good, + // and the recording's start moves back with them. + let dropped = usize::try_from(s.journal_dropped().saturating_sub(self.journal_dropped)).unwrap_or(usize::MAX); + s.truncate_journal(self.journal.saturating_sub(dropped)); + s.recording = self.recording.map(|r| r.saturating_sub(dropped)); s.ui_requests.truncate(self.ui_requests); s.format_painter = self.format_painter; s.last_find = self.last_find; diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index d1babe4eb9..d2aa786d87 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -306,6 +306,36 @@ fn a_dry_run_leaves_a_clean_workbook_clean() { assert!(!s.doc().unwrap().is_dirty()); } +#[test] +fn a_dry_run_puts_the_journal_back_even_after_it_trimmed_it() { + // Every nested command a dry run executes is journaled, so a long macro pushes the journal + // past its limit and the journal drops its oldest entries. Putting the journal back by its + // old length would then keep entries from the dry run and point a recording at the wrong + // place; the restore counts what was dropped. + let mut s = s(); + data(&mut s); + s.execute("selection.set", json!({"range": "A1"})).unwrap(); + let journal = s.journal.clone(); + s.recording = Some(journal.len().saturating_sub(1)); + put( + &mut s, + json!({"name": "Long", "steps": [{"type": "repeat", "over": "A1:A200", "body": [ + {"type": "repeat", "over": "A1:A60", "body": [{"command": "cell.set", "params": {"cell": "Row.cell(1,1)", "input": "x"}}]} + ]}]}), + ); + s.execute("macro.check", json!({"name": "Long"})).unwrap(); + let sets = |j: &[(String, Json)]| j.iter().filter(|(id, _)| id == "cell.set").count(); + assert!(sets(&s.journal) <= sets(&journal), "no dry-run entry stays"); + assert!(s.journal.len() <= journal.len(), "{} entries, had {}", s.journal.len(), journal.len()); + assert!(journal.ends_with(&s.journal), "what stays is the journal's own tail"); + assert!(s.recording.is_some_and(|r| r <= s.journal.len()), "{:?}", s.recording); + // A short dry run leaves the journal exactly as it was. + put(&mut s, json!({"name": "Short", "steps": [{"command": "cell.set", "params": {"cell": "H1", "input": "x"}}]})); + let (journal, recording) = (s.journal.clone(), s.recording); + s.execute("macro.check", json!({"name": "Short"})).unwrap(); + assert_eq!((&s.journal, s.recording), (&journal, recording)); +} + #[test] fn the_answer_to_the_question_counts_however_ask_was_given() { let mut s = s(); From 879b96f858dfc12e10662392d181f44e83ba5d09 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:30:35 -0400 Subject: [PATCH 164/687] Docs: the footprint dry run is skipped when the conflict rule is overwrite --- docs/macros.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/macros.md b/docs/macros.md index b757d26cbd..a9f7f13408 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -96,8 +96,9 @@ refused rather than read as the header. are when the step runs. A macro that touches files, or that came with a workbook opened from a file, runs once the person approves it: the call fails with `approve: …` and runs with `approve: true`. Approval lasts for the session, until the macro changes. -2. **Footprint.** The macro first runs in dry-run mode on the real workbook, and everything is put - back. Cells it would write outside its inputs that hold data, and sheets and objects it would +2. **Footprint.** Unless the conflict rule is `overwrite`, the macro first runs in dry-run mode on + the real workbook, and everything is put back. Cells it would write outside its + inputs that hold data, and sheets and objects it would delete, are conflicts, handled by `onConflict` (or the macro's `output.onConflict`): `ask` (the call fails with `confirm: …`; `overwrite: true` answers it), `fail`, `overwrite`, `shiftDown` or `shiftRight` (Insert Cells makes room, then the footprint is checked again). Steps that touch From 2215934c9d3b105c5f22b7d519caf3563285505d Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:55:39 -0400 Subject: [PATCH 165/687] Engine: a shift is refused when the footprint cannot follow every write The touch log keeps at most a million writes; past that, a write that kept a cell's value is missing from the footprint, so a shift derived from the block of written cells could split a row the macro writes. The footprint now says when the log filled up (logFull in macro.check), and a shift is refused then, as it is when the macro deletes sheets or objects. Regression test with a million-cell fill in a dry run. --- crates/engine/src/cmd/mod.rs | 4 +++- crates/engine/src/macros/run.rs | 8 ++++++++ crates/engine/src/tests/macros.rs | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 7527b37aaf..ec73e2e2a7 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -401,7 +401,9 @@ pub(crate) fn commit(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result) } /// The most written cells a macro's footprint check records. -const TOUCH_LOG_LIMIT: usize = 1_000_000; +/// The most cells a dry run's touch log keeps; a footprint whose log is full may be missing writes +/// that kept a cell's value, so a shift is refused then. +pub(crate) const TOUCH_LOG_LIMIT: usize = 1_000_000; /// Excel's message for an edit of a locked cell on a protected sheet. pub(crate) const PROTECTED: &str = "The cell or chart you're trying to change is on a protected sheet."; diff --git a/crates/engine/src/macros/run.rs b/crates/engine/src/macros/run.rs index 889d3dd0f1..ab67a9ce2b 100644 --- a/crates/engine/src/macros/run.rs +++ b/crates/engine/src/macros/run.rs @@ -772,6 +772,10 @@ pub struct Footprint { /// Steps executed by the end of the dry run that made this footprint (the run's one budget). #[serde(skip)] pub executed: u64, + /// The dry run wrote more cells than the touch log keeps, so writes that kept a cell's value + /// may be missing from `cells`: a shift cannot trust the block. + #[serde(skip_serializing_if = "std::ops::Not::not")] + pub log_full: bool, pub sheets: Vec, pub sheets_added: Vec, pub sheets_removed: Vec, @@ -946,6 +950,7 @@ pub fn footprint(s: &mut Session, p: &Prepared, clock: Stopwatch, time_limit_ms: rolled?; let (after, touched) = if matches!(m.stopped, Some((_, true))) { (before.clone(), Vec::new()) } else { (after, touched) }; let mut fp = diff(&before, &after, &p.inputs, &touched); + fp.log_full = touched.len() >= crate::cmd::TOUCH_LOG_LIMIT; fp.executed = m.executed; fp.error = outcome.err(); fp.stopped = m.stopped.map(|(msg, _)| msg); @@ -1094,6 +1099,9 @@ fn make_room(s: &mut Session, fp: &Footprint, p: &Prepared, down: bool) -> Resul if !fp.sheets_removed.is_empty() || fp.objects_removed > 0 { return Err(EngineError::Other("Shifting can't make room: the macro deletes sheets or objects.".into())); } + if fp.log_full { + return Err(EngineError::Other("Shifting can't make room: the macro writes more cells than the footprint can follow.".into())); + } let wb = s.doc()?.wb.clone(); let env = Env { wb: &wb, active_sheet: usize::MAX, inputs: &[], output: None, item: None }; let mut moves = Vec::new(); diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index d2aa786d87..285716b831 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -285,6 +285,28 @@ fn a_shift_that_would_move_an_input_is_refused() { assert_eq!(v(&s, "B3").display(), "Ann", "the input did not move"); } +#[test] +fn a_shift_is_refused_when_the_footprint_cannot_follow_every_write() { + // The touch log keeps at most a million writes; past that, a write that kept a cell's value + // is missing from the footprint, so a shift could split a row the macro writes. The footprint + // says so and the shift is refused (overwrite, with no dry run, still runs). + let mut s = s(); + set(&mut s, "A1", "x"); + set(&mut s, "E2", "old"); + put( + &mut s, + json!({"name": "Flood", "steps": [ + {"command": "edit.fillDown", "params": {"range": "A1:A1000001"}}, + {"command": "cell.set", "params": {"cell": "E2", "input": "new"}} + ]}), + ); + let c = s.execute("macro.check", json!({"name": "Flood"})).unwrap(); + assert_eq!(c["footprint"]["logFull"], true, "{c}"); + let err = s.execute("macro.run", json!({"name": "Flood", "onConflict": "shiftDown"})).unwrap_err().to_string(); + assert!(err.contains("more cells than the footprint can follow"), "{err}"); + assert_eq!(v(&s, "E2").display(), "old", "nothing changed"); +} + #[test] fn a_dry_run_leaves_a_clean_workbook_clean() { // `view.zoom` and `sheet.activate` move the saved mark to the current workbook on a clean From bb3db53489b50da833c4ae89516f12e56830fdfa Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Fri, 23 Oct 2026 23:56:32 -0400 Subject: [PATCH 166/687] Engine: the macro recorder keeps a step's own sheet, stays bounded, and can be cancelled Six fixes from a review of the recorder, each with a regression test (four fail against the old recorder; the other two bound work that grew with the input): - A step that names another sheet with its sheet parameter (cell.set and about fifty other commands) was tokenized as if on the input's sheet, so a recorded cell.set {sheet: Two, cell: C4} ran on Two at the input's relative place. Such a step keeps its references, like a sheet-qualified one. - Inlining recorded runs of other macros and scripts stops at the macro's step limit and counts the rest in dropped, instead of expanding every run first (a thousand runs of a long macro made millions of steps). - macro.record {cancel: true} ends a recording without saving it: the way out of a recording whose macro is too large for the workbook, which could neither be saved nor ended. - Formula tokenization reads each reference in the formula and matches it, instead of listing every column and row of the example (an example as large as a sheet made a million candidates per formula). - A selection of several areas keeps its active cell as it is; a token for the active cell alone put it outside the areas on playback. - Steps recorded in another workbook are counted in dropped. --- crates/engine/src/cmd/macros.rs | 14 +++- crates/engine/src/macros/recorder.rs | 119 ++++++++++++++++++--------- crates/engine/src/tests/macros.rs | 98 ++++++++++++++++++++++ docs/macros.md | 3 +- 4 files changed, 189 insertions(+), 45 deletions(-) diff --git a/crates/engine/src/cmd/macros.rs b/crates/engine/src/cmd/macros.rs index 426d064928..6cfd069f7a 100644 --- a/crates/engine/src/cmd/macros.rs +++ b/crates/engine/src/cmd/macros.rs @@ -45,7 +45,7 @@ pub fn specs() -> Vec { "Record Macro", ["View", "Macros"], None, - "{stop?: bool, relative?: bool (default true; when starting), name?, description? (when stopping; a taken name gets a suffix)} toggles recording; with relative references the selection when it starts is the example input → {recording, relative?, input?} or {recording: false, saved, steps, dropped, skipped, inputs}", + "{stop?: bool, cancel?: bool (ends a recording without saving it), relative?: bool (default true; when starting), name?, description? (when stopping; a taken name gets a suffix)} toggles recording; with relative references the selection when it starts is the example input → {recording, relative?, input?} or {recording: false, saved, steps, dropped, skipped, inputs}", has_doc, record ), @@ -163,6 +163,13 @@ fn check(s: &mut Session, p: &Json) -> Result { } fn record(s: &mut Session, p: &Json) -> Result { + // `cancel` ends a recording without saving it: the way out of a recording whose macro + // cannot be saved (too large for the workbook, for one). + if bool_param(p, "cancel").unwrap_or(false) { + let was = s.stop_recording().is_some(); + s.macro_recording = None; + return Ok(json!({"recording": false, "cancelled": was})); + } if bool_param(p, "stop").unwrap_or(false) || s.is_recording() { return stop_recording(s, p); } @@ -228,8 +235,9 @@ fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, star None => crate::macros::next_name(&wb), }; let mut converted = crate::macros::recorder::convert(&name, &start, &rec.steps, &wb); - // Steps past the macro limit are left out and counted with the ones the recorder dropped. - let cut = converted.mac.steps.len().saturating_sub(gridcraft_model::MAX_MACRO_STEPS); + // Steps past the macro limit, and ones recorded in another workbook, are left out and counted + // with the ones the recorder dropped. + let cut = converted.cut.saturating_add(converted.mac.steps.len().saturating_sub(gridcraft_model::MAX_MACRO_STEPS)); converted.mac.steps.truncate(gridcraft_model::MAX_MACRO_STEPS); if let Some(desc) = str_param(p, "description") { converted.mac.description = desc.chars().take(gridcraft_model::MAX_MACRO_TEXT).collect(); diff --git a/crates/engine/src/macros/recorder.rs b/crates/engine/src/macros/recorder.rs index 210872475e..257278b023 100644 --- a/crates/engine/src/macros/recorder.rs +++ b/crates/engine/src/macros/recorder.rs @@ -15,7 +15,7 @@ //! shape, header and column types are inferred from it. use gridcraft_core::{CellRef, RangeRef, Value}; -use gridcraft_model::{ColType, InputKind, Macro, NamedInput, Step, StepKind, Workbook}; +use gridcraft_model::{ColType, InputKind, MAX_MACRO_STEPS, Macro, NamedInput, Step, StepKind, Workbook}; use serde_json::{Map, Value as Json, json}; use super::policy::{Policy, macro_policy}; @@ -41,15 +41,28 @@ pub struct Converted { pub mac: Macro, /// Commands left out (ids), in order. pub skipped: Vec, + /// Steps not kept: past the macro's step limit (a recorded run of a long macro adds all of + /// its steps) or recorded in another workbook. + pub cut: usize, } /// Builds a macro called `name` from the recorded steps. pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: &Workbook) -> Converted { let mut skipped = Vec::new(); // Commands as they'll be played back: (id, params, sheet before, selection before, whether - // they came from another macro and stay as written). + // they came from another macro and stay as written). At most the macro's step limit are + // gathered: a recording of a thousand runs of a long macro would otherwise expand to + // millions of steps before the limit cut them back. let mut cmds: Vec<(String, Json, usize, String, Selection, bool)> = Vec::new(); + let mut cut = steps.iter().filter(|st| st.doc != start.doc).count(); let refused = |id: &str, params: &Json| matches!(macro_policy(id, params, crate::Trust::Local), Policy::Deny(_) | Policy::NeedsPermission(_)); + let mut push = |cmds: &mut Vec<(String, Json, usize, String, Selection, bool)>, cmd: (String, Json, usize, String, Selection, bool)| { + if cmds.len() < MAX_MACRO_STEPS { + cmds.push(cmd); + } else { + cut = cut.saturating_add(1); + } + }; for st in steps.iter().filter(|st| st.doc == start.doc) { let id = st.id.as_str(); if id == "macro.run" { @@ -64,7 +77,7 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & skipped.push(cid.clone()); continue; } - cmds.push((cid.clone(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + push(&mut cmds, (cid.clone(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); } } _ => skipped.push(id.to_string()), @@ -80,7 +93,7 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & skipped.push(cid); continue; } - cmds.push((cid, params, st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); + push(&mut cmds, (cid, params, st.sheet, st.sheet_name.clone(), st.selection.clone(), true)); } } None => skipped.push(id.to_string()), @@ -91,7 +104,7 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & skipped.push(id.to_string()); continue; } - cmds.push((id.to_string(), st.params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), false)); + push(&mut cmds, (id.to_string(), st.params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), false)); } // Sheet and selection changes made without a command: where a command ran on another sheet, @@ -131,7 +144,14 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & let steps: Vec = collapsed .into_iter() .map(|(id, params, on, kept)| { - let here = on.eq_ignore_ascii_case(&start.sheet_name); + // A step that names another sheet with its `sheet` parameter runs there, whatever + // sheet was active: its unqualified references are that sheet's cells. + let names_other_sheet = params.get("sheet").is_some_and(|v| match v { + Json::String(n) => !n.eq_ignore_ascii_case(&start.sheet_name), + Json::Number(i) => i.as_u64() != Some(start.sheet as u64), + _ => false, + }); + let here = on.eq_ignore_ascii_case(&start.sheet_name) && !names_other_sheet; let params = if start.relative && !kept { relative(¶ms, example, &start.sheet_name, here, &mut used_input, None) } else { params }; Step::command(id, params) }) @@ -140,7 +160,7 @@ pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], wb: & if start.relative && used_input { mac.inputs = vec![infer_input(wb, start.sheet, example)]; } - Converted { mac, skipped } + Converted { mac, skipped, cut } } fn selection_params(sel: &Selection) -> Json { @@ -153,22 +173,28 @@ fn selection_params(sel: &Selection) -> Json { fn relative(v: &Json, example: RangeRef, sheet: &str, here: bool, used: &mut bool, key: Option<&str>) -> Json { match v { Json::Object(o) => { + // A selection of several areas stays as it is, its active cell with it: a token for + // the active cell alone would put it outside the areas on playback. + let several_areas = o.get("range").and_then(Json::as_str).is_some_and(|r| r.contains(',')); let mut out = Map::new(); for (k, x) in o { - out.insert(k.clone(), relative(x, example, sheet, here, used, Some(k))); + let x = if several_areas && k == "active" { x.clone() } else { relative(x, example, sheet, here, used, Some(k)) }; + out.insert(k.clone(), x); } Json::Object(out) } Json::Array(a) => Json::Array(a.iter().map(|x| relative(x, example, sheet, here, used, key)).collect()), Json::String(t) if key.is_some_and(super::resolve::is_ref_key) => { - // A list of areas ("A1:B2,D4") stays as it is unless every part has a token. - let parts: Option> = t.split(',').map(|part| ref_token(part, example, sheet, here)).collect(); - match parts { - Some(p) if p.len() == 1 => { + // One area becomes a token; a list of areas ("A1:B2,D4") stays as it is. + if t.contains(',') { + return v.clone(); + } + match ref_token(t, example, sheet, here) { + Some(token) => { *used = true; - json!(p.join("")) + json!(token) } - _ => v.clone(), + None => v.clone(), } } Json::String(t) if here && t.starts_with('=') && example.count() > 1 => { @@ -255,22 +281,9 @@ fn token(t: RangeRef, r: RangeRef) -> Option { /// Replaces references in a formula that are exactly the example input, one of its columns or /// rows, or a column without its first row, with `{{token}}`. fn formula_tokens(f: &str, r: RangeRef) -> (String, bool) { - let mut candidates: Vec<(String, String)> = vec![(r.a1(), "Input".into())]; - for (i, col) in (r.start.col..=r.end.col).enumerate() { - let n = i + 1; - candidates.push((RangeRef::new(CellRef::new(r.start.row, col), CellRef::new(r.end.row, col)).a1(), format!("Input.col({n})"))); - if r.height() > 2 { - candidates - .push((RangeRef::new(CellRef::new(r.start.row + 1, col), CellRef::new(r.end.row, col)).a1(), format!("Input.rows(2..).col({n})"))); - } - } - if r.width() > 1 { - for (i, row) in (r.start.row..=r.end.row).enumerate() { - candidates.push((RangeRef::new(CellRef::new(row, r.start.col), CellRef::new(row, r.end.col)).a1(), format!("Input.row({})", i + 1))); - } - } - // Longest first, so `B2:B10` isn't taken for a `B2` cell reference. - candidates.sort_by_key(|(a1, _)| std::cmp::Reverse(a1.len())); + // Each reference in the formula text is read and matched on its own, so the work is the + // formula's length whatever the example's size (an example of a whole sheet has a million + // rows, which no list of candidates may grow with). let mut out = String::with_capacity(f.len()); let mut changed = false; let (mut i, mut in_text) = (0, false); @@ -282,19 +295,20 @@ fn formula_tokens(f: &str, r: RangeRef) -> (String, bool) { in_text = !in_text; } let prev_ok = i == 0 || !bytes.get(i - 1).is_some_and(|b| b.is_ascii_alphanumeric() || matches!(b, b'$' | b'!' | b'_' | b'.' | b':')); - if !in_text && prev_ok { - let hit = candidates.iter().find(|(a1, _)| { - rest.starts_with(a1.as_str()) - && !rest - .get(a1.len()..) - .and_then(|x| x.chars().next()) - .is_some_and(|c| c.is_ascii_alphanumeric() || matches!(c, ':' | '!' | '(' | '_')) - }); - if let Some((a1, tok)) = hit { + if !in_text && prev_ok && ch.is_ascii_alphabetic() { + // The longest run of reference characters here: letters, digits and one `:`. + let n = rest.bytes().take_while(|b| b.is_ascii_alphanumeric() || *b == b':').count(); + let text = rest.get(..n).unwrap_or(""); + let followed = rest.get(n..).and_then(|x| x.chars().next()).is_some_and(|c| matches!(c, '!' | '(' | '_' | '$')); + if !followed + && !text.contains('$') + && let Some(t) = RangeRef::parse(text) + && let Some(tok) = formula_token(t, r) + { out.push_str("{{"); - out.push_str(tok); + out.push_str(&tok); out.push_str("}}"); - i += a1.len(); + i += n; changed = true; continue; } @@ -305,6 +319,29 @@ fn formula_tokens(f: &str, r: RangeRef) -> (String, bool) { (out, changed) } +/// The token for a reference inside a formula: exactly the example input, one of its columns or +/// rows, or a column without its first row. Other references stay as they are. +fn formula_token(t: RangeRef, r: RangeRef) -> Option { + if t == r { + return Some("Input".into()); + } + if !r.contains_range(&t) { + return None; + } + let col = usize::try_from(t.start.col.checked_sub(r.start.col)?).ok()?.checked_add(1)?; + let row = usize::try_from(t.start.row.checked_sub(r.start.row)?).ok()?.checked_add(1)?; + if t.start.col == t.end.col && t.start.row == r.start.row && t.end.row == r.end.row { + return Some(format!("Input.col({col})")); + } + if t.start.col == t.end.col && r.height() > 2 && t.start.row == r.start.row.checked_add(1)? && t.end.row == r.end.row { + return Some(format!("Input.rows(2..).col({col})")); + } + if r.width() > 1 && t.start.row == t.end.row && t.start.col == r.start.col && t.end.col == r.end.col { + return Some(format!("Input.row({row})")); + } + None +} + /// The example input's shape: a header when the first row is all text over other kinds of /// values, and each column's type when all its values agree. fn infer_input(wb: &Workbook, sheet: usize, r: RangeRef) -> NamedInput { diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index df6e59606a..3e246ee717 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -1046,6 +1046,104 @@ fn a_relative_recording_keeps_references_on_another_sheet() { assert_eq!((v(&s, "C4").display(), v(&s, "D2").display()), ("x".into(), "12".into())); } +#[test] +fn a_step_that_names_another_sheet_keeps_its_references() { + // `sheet` names where the step runs whatever sheet is active, so its C4 is that sheet's C4. + let mut s = s(); + s.execute("home.insertSheet", json!({"name": "Two"})).unwrap(); + s.execute("sheet.activate", json!({"sheet": "Sheet1"})).unwrap(); + data(&mut s); + s.execute("selection.set", json!({"range": "B2:C5"})).unwrap(); + s.execute("macro.record", json!({})).unwrap(); + s.execute("cell.set", json!({"sheet": "Two", "cell": "C4", "input": "x"})).unwrap(); + s.execute("cell.set", json!({"sheet": "Sheet1", "cell": "C4", "input": "90"})).unwrap(); + s.execute("macro.record", json!({"stop": true, "name": "Named"})).unwrap(); + let steps = recorded(&mut s, "Named"); + assert!(steps.contains(&"cell.set {\"cell\":\"C4\",\"input\":\"x\",\"sheet\":\"Two\"}".to_string()), "{steps:?}"); + assert!(steps.contains(&"cell.set {\"cell\":\"Input.cell(3,2)\",\"input\":\"90\",\"sheet\":\"Sheet1\"}".to_string()), "{steps:?}"); +} + +#[test] +fn inlining_recorded_runs_stops_at_the_step_limit() { + // A recorded run of a long macro adds all of its steps; the conversion gathers at most the + // macro's step limit and counts the rest, instead of expanding every run first. + let mut s = s(); + let steps: Vec = (0..6_000).map(|_| json!({"command": "cell.set", "params": {"cell": "A1", "input": "x"}})).collect(); + put(&mut s, json!({"name": "Big", "output": {"onConflict": "overwrite"}, "steps": steps})); + s.execute("macro.record", json!({})).unwrap(); + s.execute("macro.run", json!({"name": "Big"})).unwrap(); + s.execute("macro.run", json!({"name": "Big"})).unwrap(); + let r = s.execute("macro.record", json!({"stop": true, "name": "Twice"})).unwrap(); + assert_eq!((r["steps"].as_u64(), r["dropped"].as_u64()), (Some(10_000), Some(2_000)), "{r}"); +} + +#[test] +fn a_recording_can_be_cancelled() { + let mut s = s(); + data(&mut s); + let before = s.execute("macro.list", json!({})).unwrap(); + s.execute("macro.record", json!({})).unwrap(); + set(&mut s, "E2", "x"); + let r = s.execute("macro.record", json!({"cancel": true})).unwrap(); + assert_eq!(r, json!({"recording": false, "cancelled": true})); + assert!(!s.is_recording() && s.macro_recording.is_none()); + assert_eq!(s.execute("macro.list", json!({})).unwrap(), before, "nothing was saved"); + assert_eq!(s.execute("macro.record", json!({"cancel": true})).unwrap()["cancelled"], false); + // Recording works again afterwards. + s.execute("macro.record", json!({})).unwrap(); + assert!(s.is_recording()); + s.execute("macro.record", json!({"cancel": true})).unwrap(); +} + +#[test] +fn formulas_are_tokenized_by_reading_their_references() { + // An example as large as a sheet has a million rows: each reference in the formula is read + // and matched on its own, instead of a list of every column and row of the example. + let mut s = s(); + s.execute("selection.set", json!({"range": "A1:Z1000000"})).unwrap(); + s.execute("macro.record", json!({})).unwrap(); + s.execute("cell.set", json!({"cell": "AB1", "input": "=SUM(A1:A1000000)+SUM(A1:A3)+B2"})).unwrap(); + s.execute("cell.set", json!({"cell": "AB2", "input": "=SUM(A1:Z1000000)&\"A1:A1000000\""})).unwrap(); + s.execute("macro.record", json!({"stop": true, "name": "Wide"})).unwrap(); + let steps = recorded(&mut s, "Wide"); + // A single cell inside a formula stays as it is: only the input, its columns and its rows + // become tokens there. + assert!(steps.iter().any(|st| st.contains("=SUM({{Input.col(1)}})+SUM(A1:A3)+B2")), "{steps:?}"); + assert!(steps.iter().any(|st| st.contains("=SUM({{Input}})&\\\"A1:A1000000\\\"")), "{steps:?}"); +} + +#[test] +fn a_selection_of_several_areas_keeps_its_active_cell_as_it_is() { + let mut s = s(); + data(&mut s); + s.execute("selection.set", json!({"range": "B2:C5"})).unwrap(); + s.execute("macro.record", json!({})).unwrap(); + // Several areas picked with the mouse: no command, so the recording adds a selection.set + // with the active cell before the next step. + let (a, b) = (gridcraft_core::RangeRef::parse("B2:C5").unwrap(), gridcraft_core::RangeRef::parse("E2:E5").unwrap()); + s.doc_mut().unwrap().selection = crate::Selection { active: b.start, anchor: b.start, ranges: vec![a, b] }; + s.execute("home.bold", json!({})).unwrap(); + s.execute("macro.record", json!({"stop": true, "name": "Areas"})).unwrap(); + let steps = recorded(&mut s, "Areas"); + assert!(steps.contains(&"selection.set {\"active\":\"E2\",\"range\":\"B2:C5,E2:E5\"}".to_string()), "{steps:?}"); + assert!(steps.iter().all(|st| !st.contains("\"active\":\"Input")), "{steps:?}"); +} + +#[test] +fn steps_recorded_in_another_workbook_are_counted() { + let mut s = s(); + data(&mut s); + let home = s.active_index(); + s.execute("macro.record", json!({})).unwrap(); + s.new_workbook(); + set(&mut s, "A1", "elsewhere"); + set(&mut s, "A2", "too"); + s.set_active(home); + set(&mut s, "E2", "here"); + let r = s.execute("macro.record", json!({"stop": true, "name": "Home"})).unwrap(); + assert_eq!((r["steps"].as_u64(), r["dropped"].as_u64()), (Some(1), Some(2)), "{r}"); +} + #[test] fn a_recorded_macro_run_is_inlined_only_when_its_steps_stand_alone() { let mut s = s(); diff --git a/docs/macros.md b/docs/macros.md index 5030687074..22742ac85f 100644 --- a/docs/macros.md +++ b/docs/macros.md @@ -120,7 +120,8 @@ of the step that just ran, and the cells changed so far. `macro.record` toggles recording (`relative: false` for absolute references; `name` and `description` when stopping; a name that another macro has gets a suffix, such as `Tidy (2)`, so -stopping never replaces a macro). The macro is saved in the workbook where recording started, even if +stopping never replaces a macro; `cancel: true` ends a recording without saving it, the way out of +one whose macro is too large for the workbook). The macro is saved in the workbook where recording started, even if another one is active; if that workbook was closed, stopping ends the recording without saving it. When saving the recording fails (a name the check refuses, a workbook at its macro limit, a macro being debugged in that workbook), recording goes on, so stopping again keeps what was From 359cce4bc3d4860c10406cf123ae49d5b37f9124 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:36:36 -0400 Subject: [PATCH 167/687] Tests and docs: merge fallout from main and the lower branches automate.allScripts reports trusted since #2178, and scripts reopened from a file are not trusted yet, so the side-by-side test expects the flag and compares names and steps after the round trip. cargo xtask parity gives 276 / 292 now (upstream added a command), so the ROADMAP and ui-parity summaries say so, and this branch's revision rows (275 / 291 to 276 / 292) sit at the top of their tables with the date they land. --- ROADMAP.md | 4 ++-- crates/engine/src/tests/macros.rs | 6 ++++-- docs/parity-checklist.md | 4 ++-- docs/ui-parity.md | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index b039998b73..59603f727f 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -22,7 +22,7 @@ This page is the summary. The detail is in: | Number | Value | Kind | |---|---|---| | 2026-10-14 | Editing a cell with text that would read back as a number, boolean, date or formula (entered with a leading apostrophe) shows the apostrophe in the editor and formula bar, so Enter unchanged keeps it text (#1362) | -| **Feature breadth** (weighted) | **~75%** | estimated, anchored on two measurements: ribbon catalog 275 / 291 (94.5%), worksheet functions 513 / 523 (98.1%; 504 working, 96.4%) | +| **Feature breadth** (weighted) | **~75%** | estimated, anchored on two measurements: ribbon catalog 276 / 292 (94.5%), worksheet functions 513 / 523 (98.1%; 504 working, 96.4%) | | **Ready for real work** | **~50%** | estimated from written weights (51.6); [target-app-parity.md](docs/target-app-parity.md) | | **Mainstream practitioner** | **~50%** | estimated: typical analyst/finance user, weekly features, × 0.90 interaction × 0.92 stability × 0.88 file exchange; [method](docs/target-app-parity.md#mainstream-practitioner) | | **Essentials user** | **~63%** | estimated: core features only, × 0.92 launch/stability × 0.95 clarity × 0.90 opening sent files; [method](docs/target-app-parity.md#essentials-user) | @@ -222,6 +222,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-24 | minor | Macro engine and `macro.*` commands; catalog 275 / 291 → 276 / 292 (View › Macros › Record Macro); progress log row | | 2026-10-22 | trivial | Progress log: formula hardening (#2163, #2162) | | 2026-10-21 | minor | Page Setup paper sizes: JIS B4/B5/B6 (ISO by name), Japanese postcards and envelopes, A6; PDF page size follows, XLSX round-trips | | 2026-10-20 | minor | Dutch (nl) listed under other shipped languages; progress log row | @@ -239,7 +240,6 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-13 | trivial | Progress log: shared pictures written once on XLSX export (#985) | | 2026-10-13 | minor | Progress log: conditional-format lookups no longer scan the range for rules without statistics (#1098) | | 2026-10-12 | minor | Progress log: hidden-sheet navigation at workbook boundaries | -| 2026-10-21 | minor | Macro engine and `macro.*` commands; catalog 274 / 290 → 275 / 291 (View › Macros › Record Macro); progress log row | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/crates/engine/src/tests/macros.rs b/crates/engine/src/tests/macros.rs index 2c0b557dea..8459bbcb1f 100644 --- a/crates/engine/src/tests/macros.rs +++ b/crates/engine/src/tests/macros.rs @@ -981,7 +981,7 @@ fn macros_and_automate_scripts_live_side_by_side() { assert_eq!(ids, ["cell.set {\"cell\":\"A1\",\"input\":\"typed\"}", "cell.set {\"cell\":\"B1\",\"input\":\"from script\"}"]); // Both lists keep their own entries, under the same name if need be. let scripts = s.execute("automate.allScripts", json!({})).unwrap(); - assert_eq!(scripts, json!([{"name": "Empty", "steps": 0}, {"name": "Inner", "steps": 1}])); + assert_eq!(scripts, json!([{"name": "Empty", "steps": 0, "trusted": true}, {"name": "Inner", "steps": 1, "trusted": true}])); let macros: Vec = s.execute("macro.list", json!({})).unwrap().as_array().unwrap().iter().map(|m| m["name"].clone()).collect(); assert_eq!(macros, [json!("Inner"), json!("Recorded")]); s.execute("home.insertSheet", json!({})).unwrap(); @@ -991,7 +991,9 @@ fn macros_and_automate_scripts_live_side_by_side() { // Both survive an xlsx round trip unchanged. let b64 = s.execute("file.saveBytes", json!({"format": "xlsx"})).unwrap()["base64"].as_str().unwrap().to_string(); s.execute("file.open", json!({"name": "both.xlsx", "base64": b64})).unwrap(); - assert_eq!(s.execute("automate.allScripts", json!({})).unwrap(), scripts); + // Opened from a file, the scripts are not trusted yet (#2178); their names and steps are the same. + let names_and_steps = |v: &Json| v.as_array().unwrap().iter().map(|s| (s["name"].clone(), s["steps"].clone())).collect::>(); + assert_eq!(names_and_steps(&s.execute("automate.allScripts", json!({})).unwrap()), names_and_steps(&scripts)); assert_eq!(s.execute("macro.list", json!({})).unwrap().as_array().unwrap().len(), 2); } diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 1ef7c423e2..71d75f6592 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -4,7 +4,7 @@ Each entry is an Excel ribbon or menu command (feature names only). It counts as implemented when GridCraft registers a command with the same id. This measures breadth only; the overall assessment (depth, fidelity, effort) is in [target-app-parity.md](target-app-parity.md). -**Overall: 275 / 291 commands implemented (94.5%).** +**Overall: 276 / 292 commands implemented (94.5%).** | Tab | Implemented | Total | Parity | |---|---|---|---| @@ -16,7 +16,7 @@ Each entry is an Excel ribbon or menu command (feature names only). It counts as | Formulas | 23 | 23 | 100% | | Data | 23 | 28 | 82% | | Review | 15 | 18 | 83% | -| View | 20 | 20 | 100% | +| View | 21 | 21 | 100% | | Automate | 4 | 4 | 100% | | Table Design | 13 | 13 | 100% | | Chart Design | 8 | 8 | 100% | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 0b020ef062..c577e04395 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,10 +1,10 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-24 · **Change:** minor (ribbon catalog count 275 / 291) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-24 · **Change:** minor (ribbon catalog count 276 / 292) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured -separately in [parity-checklist.md](parity-checklist.md) (275 / 291). +separately in [parity-checklist.md](parity-checklist.md) (276 / 292). Sources: `crates/ui-egui/src` (ribbon, grid, editor, formula bar, dialogs, panes), `crates/engine/src/cmd` (shortcuts in command specs), the owner's black-box notes @@ -175,6 +175,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-24 | minor | Ribbon catalog count 275 / 291 → 276 / 292 (View › Macros › Record Macro) | | 2026-10-23 | minor | Frozen panes: Page Up, Ctrl+Up and Shift variants that move from the scrolled rows into the frozen ones bring the scrolled rows back to the top, so the rows in between are seen again (Page Up from far down used to end on row 1 with the view still a page down); Ctrl+Left and Alt+Page Up do the same for frozen columns. Other ways of selecting a frozen cell (click, Find, Go To, Undo, formula pointing) leave the view where it is | | 2026-10-23 | minor | After a left-drag of the fill handle an Auto Fill Options button sits below the filled cells; its menu redoes the fill as Copy Cells, Fill Series, Fill Formatting Only or Fill Without Formatting (undoes the drag's fill, then `edit.autoFill` in that mode). It goes away when the workbook changes or the selection moves (#1403) | | 2026-10-22 | minor | Show Outline Symbols (`view.showOutlineSymbols`, Ctrl+8): a sheet can hide the outline's level buttons and brackets while keeping its outline, as Excel saves with `showOutlineSymbols="0"` (read and written); the grid then gives the bands no room | @@ -183,7 +184,6 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | 2026-10-21 | minor | Paste Special laid out as Excel's: Paste and Operation options each in two columns inside a frame, Skip blanks and Transpose side by side, Paste Link as a button that pastes references at once; labels translated (the generic form didn't translate them) and the window no longer runs past the screen | | 2026-10-21 | minor | Form dialogs (conditional-format quick rules, New Name, Remove Duplicates, Text to Columns and every other `Dialog::form`) fit their content: OK and Cancel sit in a row under the last field instead of a right-to-left layout that took the screen's whole height and left the buttons floating halfway down | | 2026-10-20 | minor | Esc and Enter close a message over the cell editor, such as a data validation alert, and the entry stays open to fix, with the keyboard back in it. Before, the editor took them (the first Esc cancelled the edit behind the alert and only the second closed it), and after the message typing went nowhere until the cell was clicked | -| 2026-10-21 | minor | Ribbon catalog count 274 / 290 → 275 / 291 (View › Macros › Record Macro) | | 2026-10-19 | minor | Ctrl+Enter (`range.fill`) gives each cell what typing the input there gives: a General cell gets the automatic number format (`5%`, `$5`, `3/4/2026`, `=TODAY()`) even when it already held a value, and a cell with its own format, a formatted row or column, or Text keeps it. Cells that held a value used to keep General (`5%` showed 0.05), and empty cells took the active cell's format | | 2026-10-19 | trivial | While a mouse button is held (a click or a drag selecting cells) the selection outline is drawn on its cells at once instead of gliding after the pointer, which read as lag; keyboard moves still glide | | 2026-10-18 | minor | Keytip letters typed with left Alt held continue the sequence (Alt+H, O, A / Alt+H, O, I ran nothing before: Alt+letter cancelled keytips); other Alt combinations, AltGr and macOS Option are unchanged (#1846) | From 2b33fcf881e40ef4ad1effaea270e2762f7b008d Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:49:43 -0400 Subject: [PATCH 168/687] Engine: a failed command rolls back the undo groups it opened A command that opened an undo group and then returned an error (a `?` between `group_begin` and `group_end`) left the group open for good: Undo and Redo refused with "finish or stop the running action first", later edits made no undo steps, and nothing could stop the group. `execute` now rolls back every group a failed command opened, as `group_rollback` does, restoring the workbook, selection, undo and redo history to where each group began. Only a command that succeeds keeps a group it opened, which is what a debugger's start needs. `Session::abort_groups(doc)` and the menu-less command `automate.stopAction` roll back the groups left open on a document, for a debugger the person abandons or a caller that dropped its token. The command is not undoable and works while a group is open, and the Undo refusal now names it. --- crates/engine/src/cmd/extra.rs | 16 ++++++ crates/engine/src/cmd/mod.rs | 2 +- crates/engine/src/lib.rs | 29 +++++++++++ crates/engine/src/tests/seams.rs | 89 ++++++++++++++++++++++++++++++++ docs/architecture.md | 12 +++-- 5 files changed, 143 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index a566f7f124..6197dd5e29 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -212,6 +212,15 @@ pub fn specs() -> Vec { trust_scripts ), cmd!("automate.deleteScript", "Delete Script", [], None, "{name}", has_doc, delete_script), + cmd!( + noundo "automate.stopAction", + "Stop Action", + [], + None, + "{} → {stopped: bool}: stops an action left running on the active workbook (an open undo group, such as a debugger's) and puts back the workbook and its undo history as they were when it began", + has_doc, + stop_action + ), // Cell controls, themes, borders, styles cmd!( "insert.checkbox", @@ -1088,6 +1097,13 @@ fn run_script(s: &mut Session, p: &Json) -> Result { Ok(json!({"ran": n})) } +/// Automate › Stop Action: rolls back the undo groups open on the active workbook, so Undo and +/// Redo work there again. +fn stop_action(s: &mut Session, _: &Json) -> Result { + let uid = s.doc()?.uid; + Ok(json!({"stopped": s.abort_groups(uid)})) +} + fn delete_script(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("automate.deleteScript", "missing `name`"))?.to_string(); edit(s, |cx| { diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 766a410057..8a0bb1052c 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -108,7 +108,7 @@ pub fn can_redo(s: &Session) -> std::result::Result<(), String> { /// Undo and Redo wait while an undo group is open on the active document (an action that spans /// several calls is still running). fn not_grouping(s: &Session) -> std::result::Result<(), String> { - if s.active().is_some_and(|d| s.in_group(d.uid)) { Err("finish or stop the running action first".into()) } else { Ok(()) } + if s.active().is_some_and(|d| s.in_group(d.uid)) { Err("finish or stop the running action first (automate.stopAction)".into()) } else { Ok(()) } } pub fn has_clipboard(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 8a8d8bcfa6..3b40d74018 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -594,6 +594,12 @@ impl Session { return Err(EngineError::Internal(id.to_string(), msg)); } }; + // A command that fails leaves none of its own undo groups open (a `?` between + // `group_begin` and `group_end`): each is rolled back as `group_rollback` does. Only a + // command that succeeds keeps a group it opened (a debugger's start). + if result.is_err() { + self.rollback_groups_from(first_group); + } if spec.journal && result.is_ok() { // A command that started recording is not part of the recording; one that stopped it // finds no recorder. @@ -751,6 +757,29 @@ impl Session { Ok(changed) } + /// Stops the running action on the document with uid `doc`: every undo group open on it is + /// rolled back as [`Session::group_rollback`] does, putting back the workbook, selection, undo + /// and redo history as they were when the outermost one began. Their tokens are then spent + /// (ending or rolling one back reports that it has already ended). Returns whether a group + /// was open. `automate.stopAction` runs it on the active document. + pub fn abort_groups(&mut self, doc: u64) -> bool { + let Some(id) = self.groups.iter().filter(|g| g.doc == doc).map(|g| g.id).min() else { return false }; + // Rolling back the outermost group ends the ones opened inside it. + self.group_rollback(GroupToken { id, doc }).is_ok() + } + + /// Rolls back every open undo group numbered `first` or later (the groups a failed command + /// opened), outermost first on each document. + fn rollback_groups_from(&mut self, first: u64) { + let opened: Vec<(u64, u64)> = self.groups.iter().filter(|g| g.id >= first).map(|g| (g.id, g.doc)).collect(); + for (id, doc) in opened { + // A group inside one rolled back before it is already gone. + if self.groups.iter().any(|g| g.id == id) { + let _ = self.group_rollback(GroupToken { id, doc }); + } + } + } + /// Bounds the undo steps pushed while a command runs (a script of many steps, a subtotal per /// group): at most [`UNDO_LIMIT`] of them within [`UNDO_BYTES_LIMIT`], dropping the oldest. /// Only steps after the latest mark go: a running command's start, an open group's start and diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 0f1bc30722..9d32c66d16 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -643,6 +643,95 @@ fn a_group_ended_by_a_command_that_then_panics_keeps_its_step() { } } +#[test] +fn a_command_that_fails_rolls_back_the_groups_it_opened() { + // A `?` between `group_begin` and `group_end` used to leave the group open for good: Undo + // and Redo refused, and later edits made no steps. + crate::cmd::register_undoable_test_command("test.groupThenFail", |s, _| { + let _token = s.group_begin("Half")?; + put(s, "A1", "half done")?; + Err(EngineError::Other("stopped halfway".into())) + }); + crate::cmd::register_undoable_test_command("test.editGroupThenFail", |s, _| { + put(s, "B1", "outside")?; + let _token = s.group_begin("Half")?; + put(s, "B2", "inside")?; + Err(EngineError::Other("stopped halfway".into())) + }); + let mut s = s(); + set(&mut s, "A1", "before"); + set(&mut s, "A2", "undone"); + s.execute("edit.undo", json!({})).unwrap(); + s.execute("selection.set", json!({"range": "C3"})).unwrap(); + let uid = s.doc().unwrap().uid; + let err = s.execute("test.groupThenFail", json!({})).unwrap_err().to_string(); + assert!(err.contains("stopped halfway"), "{err}"); + assert!(!s.in_group(uid), "no group is left open"); + assert_eq!(v(&s, "A1").display(), "before"); + assert_eq!(s.doc().unwrap().selection.a1(), "C3"); + assert_eq!(undo_labels(&s), ["Enter Cell"]); + assert_eq!(s.doc().unwrap().redo.len(), 1, "the redo history comes back"); + assert_eq!(s.execute("history.list", json!({})).unwrap()["grouping"], json!(false)); + set(&mut s, "A3", "next"); + assert_eq!(undo_labels(&s), ["Enter Cell", "Enter Cell"], "later edits make steps again"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "A3").is_empty()); + + // What the command changed before opening the group stays, as for any failed command, and is + // a step of its own; what it changed inside the group goes. + assert!(s.execute("test.editGroupThenFail", json!({})).is_err()); + assert!(!s.in_group(uid)); + assert_eq!((v(&s, "B1").display(), v(&s, "B2").display()), ("outside".into(), String::new())); + assert_eq!(undo_labels(&s), ["Enter Cell", "test.editGroupThenFail"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "B1").is_empty()); + + // A group open before the command stays open: only the command's own group goes. + let token = s.group_begin("Outer").unwrap(); + set(&mut s, "D1", "outer"); + assert!(s.execute("test.groupThenFail", json!({})).is_err()); + assert!(s.in_group(uid), "the outer group is still running"); + assert_eq!((v(&s, "A1").display(), v(&s, "D1").display()), ("before".into(), "outer".into())); + assert!(s.group_end(token).unwrap()); + assert_eq!(undo_labels(&s), ["Enter Cell", "Outer"]); + s.execute("edit.undo", json!({})).unwrap(); + assert!(v(&s, "D1").is_empty()); +} + +#[test] +fn stop_action_rolls_back_a_group_left_open() { + // A host that dropped its token, or a debugger the person wants to abandon: the message on + // Undo names the command that stops it, and the command puts the start back. + let mut s = s(); + set(&mut s, "A1", "start"); + set(&mut s, "A2", "undone"); + s.execute("edit.undo", json!({})).unwrap(); + let uid = s.doc().unwrap().uid; + let token = s.group_begin("Debug").unwrap(); + set(&mut s, "A1", "step 1"); + drop(s.group_begin("Inner").unwrap()); + set(&mut s, "A3", "step 2"); + let err = s.execute("edit.undo", json!({})).unwrap_err().to_string(); + assert!(err.contains("automate.stopAction"), "{err}"); + assert_eq!(s.execute("automate.stopAction", json!({})).unwrap(), json!({"stopped": true})); + assert!(!s.in_group(uid)); + assert_eq!(v(&s, "A1").display(), "start"); + assert!(v(&s, "A3").is_empty()); + assert_eq!(undo_labels(&s), ["Enter Cell"], "stopping is not a step of its own"); + assert_eq!(s.doc().unwrap().redo.len(), 1, "the redo history comes back"); + assert!(s.group_end(token).is_err(), "the token is spent"); + assert_eq!(s.execute("automate.stopAction", json!({})).unwrap(), json!({"stopped": false})); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(v(&s, "A2").display(), "undone"); + // Other documents keep their groups. + let other = s.new_workbook(); + let other_uid = s.documents()[other].uid; + let _kept = s.group_begin("Theirs").unwrap(); + s.set_active(0); + assert_eq!(s.execute("automate.stopAction", json!({})).unwrap(), json!({"stopped": false})); + assert!(s.in_group(other_uid)); +} + // ---------------------------------------------------------------- recording buffer fn recorded_ids(s: &Session) -> Vec { diff --git a/docs/architecture.md b/docs/architecture.md index f51f3a61bf..63119fe168 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # Architecture -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (engine building blocks for multi-command actions) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-24 · **Change:** minor (engine building blocks for multi-command actions) · **Target:** Microsoft Excel (Microsoft 365) How GridCraft is built today. Rules for contributors are in [`AGENTS.md`](../AGENTS.md). @@ -59,8 +59,12 @@ Actions made of many commands (macros, a debugger that runs one step per call) u closes its own groups still makes one undo step for itself, covering its edits inside and outside the groups. A command that ends a group open before it leaves the group its step and makes one of its own for what it changes after the end; a group a command opens and leaves open starts - where the command did. The history limit is applied when the outermost command finishes; until - then the steps made inside it are bounded on their own. + where the command did, but only when the command succeeds: a command that fails rolls back + every group it opened, as `group_rollback` does. `Session::abort_groups(doc)`, and the command + `automate.stopAction` on the active workbook, roll back the groups left open on a document (a + debugger the person abandons, a caller that dropped its token), so Undo works there again. The + history limit is applied when the outermost command finishes; until then the steps made inside + it are bounded on their own. - **Recording buffer** (`record.rs`). `Session::start_recording` collects each successful top-level command with the document, sheet and selection it ran in, without secret parameters. Commands run by other commands are not recorded, and the buffer is independent of the journal @@ -103,6 +107,6 @@ rpm, tarball), FreeBSD, and web (WASM; WebGPU or WebGL2). Release workflows in | Date | Change | Summary | |---|---|---| -| 2026-10-23 | minor | Engine building blocks for multi-command actions: undo groups, recording buffer, document trust, chunked hidden-name storage | +| 2026-10-24 | minor | Engine building blocks for multi-command actions: undo groups, recording buffer, document trust, chunked hidden-name storage | | 2026-10-14 | minor | Document Undo's retained-cell-storage budget and latest-step guarantee | | 2026-10-10 | major | Created from the code on main: crates, layers, data flow, agent control, known gaps | From 2e72407fc68eb12ba2072e70d342279086c93960 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:51:37 -0400 Subject: [PATCH 169/687] Engine: the recorder skips a call that only opened a dialog A menu-style call with no parameters, such as `file.open`, `data.fromTextCsv` or `home.formatCells` with `{}`, only queues its dialog and succeeds. The recorder kept it as a step with empty parameters, and then kept the dialog's confirming call as well, so playing the recording back ran the command twice, or failed on the empty step when played back headless. Only `execute_headless` took the step back, and only for the open and save-as dialogs. `execute` now compares the queued UI requests before and after a top-level command and does not record one that queued a dialog. The special case in `execute_headless` goes (it keeps turning a missing file into an error), and with it `Recorder::truncate`, which had no other caller. --- crates/engine/src/lib.rs | 17 ++++++++--------- crates/engine/src/record.rs | 9 --------- crates/engine/src/tests/seams.rs | 22 ++++++++++++++++++++++ docs/architecture.md | 3 ++- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 3b40d74018..90de443854 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -549,6 +549,7 @@ impl Session { None => (0, 0, String::new(), Selection::default()), }); let doc_uid = before.as_ref().map(|(uid, ..)| *uid); + let requests_before = self.ui_requests.len(); let marked = doc_uid.zip(start).map(|(uid, (_, len))| (uid, len)); self.marks.extend(marked); self.depth = self.depth.saturating_add(1); @@ -602,8 +603,11 @@ impl Session { } if spec.journal && result.is_ok() { // A command that started recording is not part of the recording; one that stopped it - // finds no recorder. - if let Some((doc, sheet, sheet_name, selection)) = place + // finds no recorder. A menu-style call that only opened a dialog (`file.open` with no + // file, `home.formatCells` with no style) is not recorded either: the dialog's own + // call is, so playing the recording back runs the command once, with its parameters. + let opened_dialog = self.ui_requests.get(requests_before..).is_some_and(|new| new.iter().any(|q| matches!(q, UiRequest::Dialog(..)))); + if let Some((doc, sheet, sheet_name, selection)) = place.filter(|_| !opened_dialog) && let Some(rec) = self.recorder.as_mut() { rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params: cmd::journal_params(¶ms) }); @@ -878,17 +882,12 @@ impl Session { /// queued a file dialog because it got no file (`file.open`, `file.saveAs`, `file.save` on a workbook with no /// file yet, `data.getData`) is an error instead of a silent success. pub fn execute_headless(&mut self, id: &str, params: Json) -> Result { - let recorded = self.recorder.as_ref().map(|r| (r.steps.len(), r.dropped)); let r = self.execute(id, params); let requests = self.take_ui_requests(); let asked_for_file = requests.iter().any(|q| matches!(q, UiRequest::Dialog(name, _) if name == "open" || name == "saveAs")); + // A call that only opened a dialog is not recorded (see `execute`), so the recording + // has no step to take back here. if r.is_ok() && asked_for_file { - // The caller gets an error, so the recording keeps no step for the command. - if let Some((steps, dropped)) = recorded - && let Some(rec) = self.recorder.as_mut() - { - rec.truncate(steps, dropped); - } let what = match id { "file.open" => "`path` or `base64`", "data.getData" | "data.fromTextCsv" => "`path` or `text`", diff --git a/crates/engine/src/record.rs b/crates/engine/src/record.rs index 7206c21a33..7804be6c9e 100644 --- a/crates/engine/src/record.rs +++ b/crates/engine/src/record.rs @@ -63,13 +63,4 @@ impl Recorder { self.dropped = self.dropped.saturating_add(1); } } - - /// Forgets the steps after the first `steps` and puts the dropped count back to `dropped`, - /// for a command the caller received as failed after it was recorded. - pub fn truncate(&mut self, steps: usize, dropped: usize) { - for step in self.steps.drain(steps.min(self.steps.len())..) { - self.bytes = self.bytes.saturating_sub(serde_json::to_vec(&step).map_or(0, |v| v.len())); - } - self.dropped = dropped; - } } diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 9d32c66d16..86db0622fa 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -751,6 +751,28 @@ fn a_headless_call_that_fails_for_want_of_a_file_is_not_recorded() { assert_eq!(rec.dropped, 0); } +#[test] +fn a_call_that_only_opens_a_dialog_is_not_recorded() { + // Menu-style calls with no parameters only queue their dialog; the dialog's confirming call + // is the step, so playing the recording back runs each command once, with its parameters. + let mut s = s(); + s.start_recording(); + for id in ["file.open", "data.fromTextCsv", "home.formatCells"] { + s.execute(id, json!({})).unwrap_or_else(|e| panic!("{id}: {e}")); + let dialogs = s.take_ui_requests(); + assert!(dialogs.iter().any(|q| matches!(q, crate::UiRequest::Dialog(..))), "{id} opens a dialog: {dialogs:?}"); + } + assert!(recorded_ids(&s).is_empty(), "{:?}", recorded_ids(&s)); + set(&mut s, "A1", "1"); + s.execute("home.formatCells", json!({"range": "A1", "style": {"bold": true}})).unwrap(); + assert_eq!(recorded_ids(&s), ["cell.set", "home.formatCells"]); + // A dialog queued before the call, and not taken yet, does not hide the call. + s.execute("file.open", json!({})).unwrap(); + set(&mut s, "A2", "2"); + assert_eq!(recorded_ids(&s), ["cell.set", "home.formatCells", "cell.set"]); + assert_eq!(s.stop_recording().unwrap().dropped, 0); +} + #[test] fn the_recorder_keeps_top_level_calls_with_where_they_ran() { let mut s = s(); diff --git a/docs/architecture.md b/docs/architecture.md index 63119fe168..f820998f13 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -67,7 +67,8 @@ Actions made of many commands (macros, a debugger that runs one step per call) u it are bounded on their own. - **Recording buffer** (`record.rs`). `Session::start_recording` collects each successful top-level command with the document, sheet and selection it ran in, without secret parameters. - Commands run by other commands are not recorded, and the buffer is independent of the journal + Commands run by other commands are not recorded, nor is a menu-style call that only opened a + dialog (the dialog's confirming call is the step), and the buffer is independent of the journal (Automate › Record Actions still records through the journal). - **Document trust.** `DocState::trust` is `Local` for workbooks made in the session and `FromFile` for workbooks opened from a file, and for workbooks that received a paste from one From 0fa6e78863595069425af606f541fdeabd3ac898 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:53:38 -0400 Subject: [PATCH 170/687] Engine: stash items keep characters XML can't carry The xlsx writer drops characters XML 1.0 can't carry: controls other than tab, line feed and carriage return, and U+FFFE and U+FFFF. A stash item holding one lost it on save without a warning, and so did a JSON item, since serde_json leaves U+FFFE and U+FFFF unescaped: a stored macro parameter changed silently. `chunk_formulas` now writes each such character, and the marker itself, as the noncharacter U+FDD0 followed by four hex digits, and `get` turns them back for `_gc_` items. A marker that isn't followed by four hex digits naming a character is read as it is. An escape counts as five units against the 255-unit literal limit and is never split across literals, and the guard on the stored size allows the longest item even when every character is escaped. Legacy `_sc_` items are read unchanged. --- crates/engine/src/cmd/stash.rs | 110 ++++++++++++++++++++++++++++++--- docs/architecture.md | 6 +- 2 files changed, 107 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index bbef7f3ec3..38a6a39fc9 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -7,8 +7,16 @@ //! one that Excel accepts. Older items (custom views, scripts, the watch list and scenarios) keep //! one name each, `_sc_`; reading accepts that form as well, so those items can move to this //! format later, and a write of the same key replaces it. +//! +//! XML can't carry every character (controls other than tab, line feed and carriage return, and +//! U+FFFE and U+FFFF), and the xlsx writer drops those. In `_gc_` items each of them, and the +//! marker itself, is written as [`ESCAPE`] followed by its code as four upper-case hex digits +//! (`U+0001` becomes `\u{FDD0}0001`), and reading turns them back. The marker is a Unicode +//! noncharacter, which XML carries but real text does not use, so the escape costs nothing on +//! ordinary text. Legacy `_sc_` items are read as they are. use std::collections::BTreeMap; +use std::fmt::Write as _; use gridcraft_model::{DefinedName, Workbook}; use serde_json::Value as Json; @@ -25,6 +33,11 @@ const FORMULA_UNITS: usize = 8_192; const MAX_KEY: usize = 200; /// The longest item: 4,194,304 characters. pub const MAX_ITEM_CHARS: usize = 4 * 1024 * 1024; +/// The most bytes the stored (escaped) form of an item can take: an escaped one-byte character +/// takes seven (the three-byte marker and four digits). +const MAX_STORED_BYTES: usize = MAX_ITEM_CHARS * 7; +/// Starts an escaped character in a `_gc_` item: U+FDD0, a noncharacter. +pub const ESCAPE: char = '\u{FDD0}'; /// The item text stored under `key`, or `None` when there is none (or it is over the size limit). pub fn get(wb: &Workbook, key: &str) -> Option { @@ -49,14 +62,47 @@ pub fn get(wb: &Workbook, key: &str) -> Option { break; } out.push_str(&string_literals(f)); - if out.len() > MAX_ITEM_CHARS.saturating_mul(4) { + if out.len() > MAX_STORED_BYTES { return None; } } + out = unescape(&out); } (out.chars().count() <= MAX_ITEM_CHARS).then_some(out) } +/// Whether `c` is stored escaped: XML can't carry it, or it is the marker. +fn needs_escape(c: char) -> bool { + c == ESCAPE || !matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}') +} + +/// Turns [`ESCAPE`] and four hex digits back into the character. A marker not followed by four +/// hex digits naming a character (a hand-made name) stays as it is. +fn unescape(s: &str) -> String { + if !s.contains(ESCAPE) { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(at) = rest.find(ESCAPE) { + out.push_str(rest.get(..at).unwrap_or("")); + let after = rest.get(at + ESCAPE.len_utf8()..).unwrap_or(""); + let code = after.get(..4).filter(|h| h.bytes().all(|b| b.is_ascii_hexdigit())).and_then(|h| u32::from_str_radix(h, 16).ok()); + match code.and_then(char::from_u32) { + Some(c) => { + out.push(c); + rest = after.get(4..).unwrap_or(""); + } + None => { + out.push(ESCAPE); + rest = after; + } + } + } + out.push_str(rest); + out +} + /// Stores `text` under `key`, replacing what was there (in either format). Empty text removes it. pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { check_key(key)?; @@ -153,7 +199,8 @@ fn check_key(key: &str) -> Result<()> { } /// `text` as name formulas: string literals of at most 255 characters joined with `&`, each formula -/// at most 8,192 characters. Concatenating [`string_literals`] of each gives `text` back. +/// at most 8,192 characters, with the characters XML can't carry escaped (see [`ESCAPE`]). +/// Concatenating [`string_literals`] of each and turning the escapes back gives `text` back. pub fn chunk_formulas(text: &str) -> Vec { let mut formulas = Vec::new(); let (mut formula, mut units) = (String::new(), 0usize); @@ -175,14 +222,22 @@ pub fn chunk_formulas(text: &str) -> Vec { *lit_units = 0; }; for ch in text.chars() { - if lit_units + ch.len_utf16() > LITERAL_UNITS { + // An escaped character is the marker and four digits, kept in one literal. + let escaped = needs_escape(ch); + let cost = if escaped { ESCAPE.len_utf16() + 4 } else { ch.len_utf16() }; + if lit_units + cost > LITERAL_UNITS { flush_literal(&mut literal, &mut lit_units, &mut formula, &mut units); } - lit_units += ch.len_utf16(); - if ch == '"' { - literal.push('"'); + lit_units += cost; + if escaped { + literal.push(ESCAPE); + let _ = write!(literal, "{:04X}", u32::from(ch)); + } else { + if ch == '"' { + literal.push('"'); + } + literal.push(ch); } - literal.push(ch); } if lit_units > 0 { flush_literal(&mut literal, &mut lit_units, &mut formula, &mut units); @@ -403,5 +458,46 @@ mod tests { assert!(warnings.is_empty(), "{warnings:?}"); assert_eq!(get(&back, "blob").as_deref(), Some(text.as_str())); assert_eq!(get_json(&back, "watch"), Some(serde_json::json!(["A1"]))); + + // Characters XML can't carry, and the escape marker itself, come back too, in text and + // in JSON (which leaves U+FFFE and U+FFFF unescaped). + let mut wb = Workbook::new(); + let odd: String = (0u32..0x20).filter_map(char::from_u32).chain(['\u{FFFE}', '\u{FFFF}', ESCAPE, 'a', '\u{7F}', '\u{FDD1}']).collect(); + let text: String = odd.chars().cycle().take(odd.chars().count() * 300).collect(); + set(&mut wb, "odd", &text).unwrap(); + excel_limits_hold(&wb); + let json = serde_json::json!({"s": "x\u{FFFF}y\u{1}z\u{FDD0}0041"}); + set_json(&mut wb, "json", &json).unwrap(); + // All escapes: each literal holds whole escapes, within the limits. + let controls = "\u{1}".repeat(9_000); + set(&mut wb, "controls", &controls).unwrap(); + excel_limits_hold(&wb); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let (back, warnings) = crate::io::open_bytes("data.xlsx", &bytes).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(get(&back, "odd").as_deref(), Some(text.as_str())); + assert_eq!(get_json(&back, "json"), Some(json)); + assert_eq!(get(&back, "controls").as_deref(), Some(controls.as_str())); + } + + #[test] + fn escapes_count_against_the_limits_and_odd_markers_read_as_they_are() { + // Excel's limits hold however many characters are escaped, and the longest item of + // characters that all need escaping still reads. + for len in [1, 50, 51, 52, 1_638, 1_639, 5_000] { + let text = "\u{2}".repeat(len); + let fs = chunk_formulas(&text); + assert_eq!(unescape(&fs.iter().map(|f| string_literals(f)).collect::()), text, "{len}"); + assert!(fs.iter().all(|f| f.encode_utf16().count() <= FORMULA_UNITS), "{len}"); + } + let mut wb = Workbook::new(); + let longest = "\u{0}".repeat(MAX_ITEM_CHARS); + set(&mut wb, "longest", &longest).unwrap(); + excel_limits_hold(&wb); + assert_eq!(get(&wb, "longest").map(|t| t.len()), Some(MAX_ITEM_CHARS)); + // A marker in a hand-made name that is not an escape stays as it is. + let mut wb = Workbook::new(); + wb.names.push(name("_gc_hand_0", "\"a\u{FDD0}zz\u{FDD0}D800\u{FDD0}004\u{FDD0}0041\u{FDD0}\"")); + assert_eq!(get(&wb, "hand").as_deref(), Some("a\u{FDD0}zz\u{FDD0}D800\u{FDD0}004A\u{FDD0}")); } } diff --git a/docs/architecture.md b/docs/architecture.md index f820998f13..d900be62d1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -80,8 +80,10 @@ Actions made of many commands (macros, a debugger that runs one step per call) u per-session consent for stored `automate.*` scripts (`automate.trustScripts`). - **Workbook data in hidden names** (`cmd/stash.rs`). An item is split over hidden workbook-scope names `_gc__0`, `_gc__1`, … of string literals within Excel's 255- and - 8,192-character limits, so it survives an xlsx round trip. The older single-name `_sc_` - items are read as well. + 8,192-character limits, so it survives an xlsx round trip. Characters XML can't carry (controls + other than tab, line feed and carriage return, U+FFFE, U+FFFF) are stored as the noncharacter + U+FDD0 and four hex digits, as is U+FDD0 itself, and read back as they were. The older + single-name `_sc_` items are read as well. ## Agent control From b90710794a622fc25b4d848f02311d10deefbc48 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:55:07 -0400 Subject: [PATCH 171/687] Engine: list stash items in one pass over the names Each `stash::get` looks at every defined name, so listing the items with `keys` and then reading each with `get` took time quadratic in the number of names. A file with 20,000 hidden `_gc_macro__0` names took about ten seconds in a debug build, and the macro list reads its items this way from data that comes from the file. `stash::items(wb, prefix)` now groups the names by item in one pass and returns each item's text by key, and `keys` lists from the same pass. Both return at most `MAX_ITEMS` (10,000) items, the first keys in sorted order, so a file with any number of hidden names can't make a listing unbounded. A test counts the names each listing looks at. --- crates/engine/src/cmd/stash.rs | 136 ++++++++++++++++++++++++++++----- docs/architecture.md | 5 +- 2 files changed, 119 insertions(+), 22 deletions(-) diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index 38a6a39fc9..630e84ab64 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -38,23 +38,87 @@ pub const MAX_ITEM_CHARS: usize = 4 * 1024 * 1024; const MAX_STORED_BYTES: usize = MAX_ITEM_CHARS * 7; /// Starts an escaped character in a `_gc_` item: U+FDD0, a noncharacter. pub const ESCAPE: char = '\u{FDD0}'; +/// The most items [`items`] and [`keys`] return: the first this many keys in sorted order. A +/// file can carry any number of hidden names; the rest are not listed. +pub const MAX_ITEMS: usize = 10_000; + +/// The names that hold one item: its `_gc_` chunks by number (the first of each number) and its +/// legacy `_sc_` name. +#[derive(Default)] +struct Parts<'a> { + chunks: BTreeMap, + legacy: Option<&'a str>, +} + +impl<'a> Parts<'a> { + fn add(&mut self, chunk: Option, formula: &'a str) { + match chunk { + Some(i) => { + self.chunks.entry(i).or_insert(formula); + } + None => self.legacy = self.legacy.or(Some(formula)), + } + } + + /// Whether the names make an item: a later chunk on its own does not. + fn is_item(&self) -> bool { + self.chunks.contains_key(&0) || self.legacy.is_some() + } +} + +#[cfg(test)] +thread_local! { + /// Names [`group`] has looked at on this thread, so a test can bound the work of a listing. + static NAME_VISITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The workbook's items whose lower-cased key starts with `prefix` (lower-cased), with the names +/// that hold them, in one pass over the names. +fn group<'a>(wb: &'a Workbook, prefix: &str) -> BTreeMap> { + let prefix = prefix.to_ascii_lowercase(); + let mut items: BTreeMap> = BTreeMap::new(); + for n in wb.names.iter() { + #[cfg(test)] + NAME_VISITS.with(|v| v.set(v.get().saturating_add(1))); + let Some((k, chunk)) = item_of(n) else { continue }; + let k = k.to_ascii_lowercase(); + if k.starts_with(&prefix) { + items.entry(k).or_default().add(chunk, &n.formula); + } + } + items +} /// The item text stored under `key`, or `None` when there is none (or it is over the size limit). pub fn get(wb: &Workbook, key: &str) -> Option { - let mut chunks: BTreeMap = BTreeMap::new(); - let mut legacy = None; + let mut parts = Parts::default(); for n in wb.names.iter() { - match item_of(n) { - Some((k, Some(i))) if k.eq_ignore_ascii_case(key) => { - chunks.entry(i).or_insert(&n.formula); - } - Some((k, None)) if k.eq_ignore_ascii_case(key) => legacy = legacy.or(Some(&n.formula)), - _ => {} + if let Some((k, chunk)) = item_of(n) + && k.eq_ignore_ascii_case(key) + { + parts.add(chunk, &n.formula); } } + assemble(&parts) +} + +/// The items whose key starts with `prefix` (`"macro_"`), by lower-cased key, read in one pass +/// over the names: the first [`MAX_ITEMS`] of [`keys`], without those over the size limit. +pub fn items(wb: &Workbook, prefix: &str) -> BTreeMap { + group(wb, prefix) + .into_iter() + .filter(|(_, parts)| parts.is_item()) + .take(MAX_ITEMS) + .filter_map(|(k, parts)| assemble(&parts).map(|text| (k, text))) + .collect() +} + +/// The text of the item held by `parts`, as [`get`] reads it. +fn assemble(parts: &Parts) -> Option { + let chunks = &parts.chunks; let mut out = String::new(); if !chunks.contains_key(&0) { - out = string_literals(legacy?); + out = string_literals(parts.legacy?); } else { // Chunks run 0, 1, 2, …; anything after a gap isn't part of the item. for (want, (i, f)) in chunks.iter().enumerate() { @@ -144,18 +208,11 @@ pub fn set_json(wb: &mut Workbook, key: &str, v: &Json) -> Result<()> { set(wb, key, &v.to_string()) } -/// The keys of the items stored in the workbook that start with `prefix` (`"macro_"`), sorted. +/// The keys of the items stored in the workbook that start with `prefix` (`"macro_"`), sorted +/// and lower-cased: the first [`MAX_ITEMS`] of them. To read them all, [`items`] takes one pass +/// where a [`get`] per key would take one each. pub fn keys(wb: &Workbook, prefix: &str) -> Vec { - let mut keys: Vec = wb - .names - .iter() - // A later chunk on its own is no item, as `get` reads it. - .filter_map(|n| item_of(n).filter(|(_, chunk)| chunk.is_none_or(|i| i == 0)).map(|(k, _)| k.to_ascii_lowercase())) - .filter(|k| k.starts_with(&prefix.to_ascii_lowercase())) - .collect(); - keys.sort(); - keys.dedup(); - keys + group(wb, prefix).into_iter().filter(|(_, parts)| parts.is_item()).map(|(k, _)| k).take(MAX_ITEMS).collect() } /// The item a hidden name belongs to, lower-cased: `_gc__` and `_sc_` give ``. @@ -447,6 +504,45 @@ mod tests { assert_eq!(item_key("Total"), None); } + #[test] + fn listing_many_items_takes_one_pass_and_is_capped() { + // 20,000 hidden items, as a file can carry: a `get` per key looked at every name for each + // key (400 million visits); `items` looks at each name once, and lists at most MAX_ITEMS. + let mut wb = Workbook::new(); + let total = 20_000; + for i in 0..total { + wb.names.push(name(&format!("_gc_macro_{i:05}_0"), &format!("\"m{i}\""))); + } + set(&mut wb, "macro_big", &"b".repeat(9_000)).unwrap(); + wb.names.push(name("_sc_macro_legacy", "\"old\"")); + wb.names.push(name("_gc_macro_orphan_1", "\"no chunk 0\"")); + wb.names.push(name("_gc_other_0", "\"not a macro\"")); + NAME_VISITS.with(|v| v.set(0)); + let all = items(&wb, "MACRO_"); + assert_eq!(NAME_VISITS.with(|v| v.get()), wb.names.len(), "one pass over the names"); + assert_eq!(all.len(), MAX_ITEMS); + let first: Vec<&String> = all.keys().take(2).collect(); + assert_eq!(first, ["macro_00000", "macro_00001"]); + assert_eq!(all.get("macro_09999").map(String::as_str), Some("m9999")); + assert!(!all.contains_key("macro_10000"), "past the cap"); + NAME_VISITS.with(|v| v.set(0)); + let listed = keys(&wb, "macro_"); + assert_eq!(NAME_VISITS.with(|v| v.get()), wb.names.len()); + assert_eq!(listed, all.keys().cloned().collect::>(), "keys and items agree"); + + // Under the cap every item is there, read as `get` reads it. + wb.names.retain(|n| !n.name.starts_with("_gc_macro_0") && !n.name.starts_with("_gc_macro_1")); + let few = items(&wb, "macro_"); + let want: BTreeMap = + [("macro_big", "b".repeat(9_000)), ("macro_legacy", "old".into())].into_iter().map(|(k, t)| (k.to_string(), t)).collect(); + assert_eq!(few, want); + for (k, text) in &few { + assert_eq!(get(&wb, k).as_ref(), Some(text)); + } + assert_eq!(keys(&wb, "macro_"), ["macro_big", "macro_legacy"]); + assert_eq!(items(&wb, "").len(), 3, "the other item too"); + } + #[test] fn items_round_trip_through_xlsx() { let mut wb = Workbook::new(); diff --git a/docs/architecture.md b/docs/architecture.md index d900be62d1..2e80f56069 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -82,8 +82,9 @@ Actions made of many commands (macros, a debugger that runs one step per call) u workbook-scope names `_gc__0`, `_gc__1`, … of string literals within Excel's 255- and 8,192-character limits, so it survives an xlsx round trip. Characters XML can't carry (controls other than tab, line feed and carriage return, U+FFFE, U+FFFF) are stored as the noncharacter - U+FDD0 and four hex digits, as is U+FDD0 itself, and read back as they were. The older - single-name `_sc_` items are read as well. + U+FDD0 and four hex digits, as is U+FDD0 itself, and read back as they were. `stash::items` + and `stash::keys` list the items under a key prefix in one pass over the names, at most + 10,000 of them (`stash::MAX_ITEMS`). The older single-name `_sc_` items are read as well. ## Agent control From 4668fc30ae985bdc028371a84f496ea863276498 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:57:14 -0400 Subject: [PATCH 172/687] Engine: the name commands leave GridCraft's data names alone `formulas.defineName {"name": "_gc_macro_1_0"}` replaced the hidden chunk of a stored item with a visible name: the item vanished from `stash::get` and `stash::keys`, and its other chunks stayed behind as orphan hidden names. `formulas.deleteName` could remove chunks the same way, and `formulas.createFromSelection` with `replace` could replace one from a cell label. `stash::set` refused a clash with a visible name, but nothing guarded the other direction. Defining, renaming or deleting a name that `stash::item_key` recognises (`_gc__` and legacy `_sc_`) is now refused with an error that names `force: true`, which lets the call go ahead. Create from Selection lists such labels in `skipped` unless `force` is true. The params docs of the three commands say so. --- crates/engine/src/cmd/formulas.rs | 75 +++++++++++++++++++-- crates/engine/tests/it/stored_list_shape.rs | 3 +- docs/architecture.md | 2 + 3 files changed, 75 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index 5334784fae..c5f8a8bf80 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -32,18 +32,26 @@ pub fn specs() -> Vec { "Define Name…", ["Formulas", "Defined Names"], None, - "{name, refersTo?: \"=Sheet1!$A$1:$A$10\" (default: the selection), scope?: \"Workbook\"|sheet name, comment?, oldName?, oldScope?} (oldName: edit that name (Name Manager › Edit); a new name also renames it in formulas)", + "{name, refersTo?: \"=Sheet1!$A$1:$A$10\" (default: the selection), scope?: \"Workbook\"|sheet name, comment?, oldName?, oldScope?, force?: bool} (oldName: edit that name (Name Manager › Edit); a new name also renames it in formulas; names GridCraft keeps its own data in, `_gc__` and `_sc_`, are refused unless force is true)", has_doc, define_name ), - cmd!("formulas.deleteName", "Delete Name", [], None, "{name, scope?: \"Workbook\"|sheet name (default: every scope)}", has_doc, delete_name), + cmd!( + "formulas.deleteName", + "Delete Name", + [], + None, + "{name, scope?: \"Workbook\"|sheet name (default: every scope), force?: bool} (names GridCraft keeps its own data in, `_gc__` and `_sc_`, are refused unless force is true)", + has_doc, + delete_name + ), cmd!(query "formulas.nameManager", "Name Manager", ["Formulas", "Defined Names"], Some("Cmd+F3"), "{} → names with values", has_doc, name_manager), cmd!( "formulas.createFromSelection", "Create from Selection", ["Formulas", "Defined Names"], Some("Cmd+Shift+F3"), - "{range?, top?: true, left?: false, bottom?: false, right?: false, replace?: false (existing names are kept and listed in `skipped`)}", + "{range?, top?: true, left?: false, bottom?: false, right?: false, replace?: false (existing names are kept and listed in `skipped`), force?: false (labels of the shape GridCraft keeps its own data in, `_gc__` and `_sc_`, are listed in `skipped` unless force is true)}", has_doc, create_from_selection ), @@ -268,6 +276,9 @@ fn define_name(s: &mut Session, p: &Json) -> Result { if !valid_name(&name) { return Err(EngineError::Other("The name that you entered is not valid.".into())); } + for n in [Some(name.as_str()), str_param(p, "oldName")].into_iter().flatten() { + refuse_stash_name(p, n)?; + } let d = s.doc()?; let scope = match str_param(p, "scope") { None | Some("Workbook") => None, @@ -374,8 +385,21 @@ fn rename_name_in_formulas(wb: &mut gridcraft_model::Workbook, old: &str, old_sc } } +/// Refuses a name of the shape GridCraft keeps its own workbook data in ([`super::stash`]): +/// defining, renaming or deleting one would replace or remove part of an item (a stored macro, +/// script or view) and leave the rest behind. `force: true` lets the call go ahead. +fn refuse_stash_name(p: &Json, name: &str) -> Result<()> { + if stash::item_key(name).is_some() && !bool_param(p, "force").unwrap_or(false) { + return Err(EngineError::Other(format!( + "'{name}' is a name GridCraft keeps its own data in (stored macros, scripts or views). Pass `force: true` to change it anyway." + ))); + } + Ok(()) +} + fn delete_name(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("formulas.deleteName", "missing `name`"))?.to_string(); + refuse_stash_name(p, &name)?; let scope = match str_param(p, "scope") { None => None, Some("Workbook") => Some(None), @@ -461,7 +485,11 @@ fn create_from_selection(s: &mut Session, p: &Json) -> Result { // Excel asks before replacing an existing name; here it's kept unless `replace` is set, and // reported. let exists = |n: &str| d.wb.names.iter().any(|x| x.name.eq_ignore_ascii_case(n) && x.scope.is_none()); - let (made, skipped): (Vec<_>, Vec<_>) = made.into_iter().partition(|(n, _)| replace || !exists(n)); + // A label of the shape GridCraft keeps its own data in would replace part of an item, or + // stand in the way of one (see `refuse_stash_name`). + let force = bool_param(p, "force").unwrap_or(false); + let ours = |n: &str| !force && stash::item_key(n).is_some(); + let (made, skipped): (Vec<_>, Vec<_>) = made.into_iter().partition(|(n, _)| !ours(n) && (replace || !exists(n))); let skipped: Vec = skipped.into_iter().map(|(n, _)| n).collect(); let n = made.len(); edit(s, |cx| { @@ -742,6 +770,45 @@ mod tests { assert_eq!(r["refersTo"], "=Sheet1!$A$1"); } + #[test] + fn the_name_commands_leave_gridcraft_data_alone() { + // Defining, renaming or deleting a name of a stored item's shape would replace or remove + // one of its chunks and leave the rest behind: refused unless `force` is true. + let mut s = Session::new(); + s.new_workbook(); + let wb = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb); + let text = "m".repeat(9_000); + stash::set(wb, "macro_1", &text).unwrap(); + wb.names.push(DefinedName { name: "_sc_watch".into(), scope: None, formula: "\"[]\"".into(), comment: String::new(), hidden: true }); + let names = |s: &Session| s.doc().unwrap().wb.names.clone(); + let before = names(&s); + assert!(before.len() >= 3, "the item takes two chunks"); + for params in [ + json!({"name": "_gc_macro_1_0", "refersTo": "=1"}), + json!({"name": "_SC_Watch", "refersTo": "=1"}), + json!({"name": "Mine", "refersTo": "=1", "oldName": "_gc_macro_1_1"}), + json!({"name": "_gc_macro_1_0", "refersTo": "=1", "force": false}), + ] { + let err = s.execute("formulas.defineName", params.clone()).unwrap_err().to_string(); + assert!(err.contains("force"), "{params}: {err}"); + } + for name in ["_gc_macro_1_1", "_sc_watch"] { + let err = s.execute("formulas.deleteName", json!({"name": name})).unwrap_err().to_string(); + assert!(err.contains("force"), "{name}: {err}"); + } + s.execute("range.setValues", json!({"range": "A1:B2", "values": [["_gc_macro_1_0", "Rate"], [1, 2]]})).unwrap(); + let r = s.execute("formulas.createFromSelection", json!({"range": "A1:B2", "top": true, "replace": true})).unwrap(); + assert_eq!((r["created"].clone(), r["skipped"].clone()), (json!(1), json!(["_gc_macro_1_0"]))); + let after = names(&s); + assert_eq!(after.iter().filter(|n| n.name != "Rate").cloned().collect::>(), before); + assert_eq!(stash::get(&s.doc().unwrap().wb, "macro_1").as_deref(), Some(text.as_str())); + // Ordinary names and forced calls go ahead. + s.execute("formulas.deleteName", json!({"name": "Rate"})).unwrap(); + s.execute("formulas.defineName", json!({"name": "_gc_macro_1_1", "refersTo": "=1", "force": true})).unwrap(); + s.execute("formulas.deleteName", json!({"name": "_gc_macro_1_1", "force": true})).unwrap(); + assert!(!names(&s).iter().any(|n| n.name == "_gc_macro_1_1")); + } + #[test] fn arabic_defined_names_count_characters_not_bytes() { let mut s = Session::new(); diff --git a/crates/engine/tests/it/stored_list_shape.rs b/crates/engine/tests/it/stored_list_shape.rs index 0d38cd5f96..fd17a4806a 100644 --- a/crates/engine/tests/it/stored_list_shape.rs +++ b/crates/engine/tests/it/stored_list_shape.rs @@ -6,7 +6,8 @@ const NOT_OBJECTS: [&str; 4] = ["\"[1,2]\"", "\"7\"", "\"true\"", "\"\"\"hi\"\"\ fn with_name(name: &str, refers_to: &str) -> Session { let mut s = Session::new(); s.new_workbook(); - s.execute("formulas.defineName", json!({"name": name, "refersTo": refers_to})).unwrap(); + // `force`: the name commands refuse GridCraft's own data names otherwise. + s.execute("formulas.defineName", json!({"name": name, "refersTo": refers_to, "force": true})).unwrap(); s } diff --git a/docs/architecture.md b/docs/architecture.md index 2e80f56069..3fc6cb0b10 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -85,6 +85,8 @@ Actions made of many commands (macros, a debugger that runs one step per call) u U+FDD0 and four hex digits, as is U+FDD0 itself, and read back as they were. `stash::items` and `stash::keys` list the items under a key prefix in one pass over the names, at most 10,000 of them (`stash::MAX_ITEMS`). The older single-name `_sc_` items are read as well. + `formulas.defineName` and `formulas.deleteName` refuse names of either shape, and + `formulas.createFromSelection` skips labels of either shape, unless `force: true` is passed. ## Agent control From 37bf09e94a1c45042958c0d0b121519f6cf13156 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 00:59:45 -0400 Subject: [PATCH 173/687] Engine: queries take no copy of the undo history `execute` copied the document's undo and redo history on every call, so a panic could put it back: up to 100 entries per stack, each with a label and a selection. Queries paid for it too, although no query runs a command or changes the history, and front ends and agents run queries all the time. The copy is now skipped for queries (commands that neither journal nor make undo steps). A test runs every query, with empty and with typical parameters, and checks the history is unchanged, so a query that ever changes it fails there. Commands that can change the history, nested ones included, still take the copy: a nested command that panics is restored at its own level, and the outer command can carry on, so relying on the outermost copy would weaken that restore. --- crates/engine/src/lib.rs | 11 ++++++-- crates/engine/src/tests.rs | 5 ++++ crates/engine/src/tests/seams.rs | 47 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 90de443854..5c6e9f565c 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -534,8 +534,15 @@ impl Session { let start = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (self.in_group(*uid), d.undo.len()))); // A command that panics leaves the history as it found it: the steps commands nested in it // made or undid, and the redo history they cleared, come back. A length is not enough, - // because a nested `edit.undo` removes entries. - let history_before = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (d.undo.clone(), d.redo.clone()))); + // because a nested `edit.undo` removes entries. A query runs no commands and leaves the + // history alone (`every_query_leaves_the_history_alone` runs each one), so it needs no + // copy; front ends run queries all the time. + let query = !spec.journal && !spec.undoable; + let history_before = before.as_ref().filter(|_| !query).and_then(|(uid, ..)| { + #[cfg(test)] + tests::HISTORY_COPIES.with(|n| n.set(n.get().saturating_add(1))); + self.docs.iter().find(|d| d.uid == *uid).map(|d| (d.undo.clone(), d.redo.clone())) + }); // The groups open on the document before the command (usually none), for the same reason. let groups_before: Vec<(u64, String, Arc, Selection)> = match &before { Some((uid, ..)) => { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 4f2fc91bb1..1be90d56c9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3,6 +3,11 @@ use serde_json::json; use crate::Session; +thread_local! { + /// Copies of a document's undo and redo history `execute` has taken on this thread. + pub(crate) static HISTORY_COPIES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + fn s() -> Session { let mut s = Session::new(); s.new_workbook(); diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index 86db0622fa..cab24426ec 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -732,6 +732,53 @@ fn stop_action_rolls_back_a_group_left_open() { assert!(s.in_group(other_uid)); } +fn history_of(s: &Session) -> Vec<(String, *const gridcraft_model::Workbook)> { + let d = s.doc().unwrap(); + d.undo.iter().chain(d.redo.iter()).map(|e| (e.label.clone(), std::sync::Arc::as_ptr(&e.wb))).collect() +} + +#[test] +fn queries_take_no_copy_of_the_history() { + // `execute` copies the history so a panic can put it back; a query leaves it alone, so the + // copy is skipped for the queries front ends run all the time. + let mut s = s(); + for i in 0..50 { + set(&mut s, "A1", &i.to_string()); + } + s.execute("edit.undo", json!({})).unwrap(); + super::HISTORY_COPIES.with(|n| n.set(0)); + for _ in 0..100 { + s.execute("history.list", json!({})).unwrap(); + s.execute("cell.get", json!({"cell": "A1"})).unwrap(); + s.execute("document.inspect", json!({})).unwrap(); + } + assert_eq!(super::HISTORY_COPIES.with(|n| n.get()), 0); + set(&mut s, "A2", "x"); + assert_eq!(super::HISTORY_COPIES.with(|n| n.get()), 1, "a command that can change it still copies it"); +} + +#[test] +fn every_query_leaves_the_history_alone() { + // What lets a query skip the copy: none of them changes the undo or redo history. + let mut s = Session::new(); + s.execute("file.new", json!({"sample": "sales"})).unwrap(); + set(&mut s, "A1", "1"); + set(&mut s, "A1", "2"); + s.execute("edit.undo", json!({})).unwrap(); + let before = history_of(&s); + assert_eq!(before.len(), 2); + let mut queries = 0; + for spec in crate::command_specs().iter().filter(|c| !c.journal && !c.undoable) { + queries += 1; + for params in [json!({}), json!({"range": "A1:B3", "cell": "A1", "sheet": 0}), json!([1, 2])] { + let _ = s.execute(spec.id, params.clone()); + s.set_active(0); + assert_eq!(history_of(&s), before, "{} {params}", spec.id); + } + } + assert!(queries > 40, "{queries}"); +} + // ---------------------------------------------------------------- recording buffer fn recorded_ids(s: &Session) -> Vec { From 9bbba198c6263adc1e448909bf8fa07e445c5ed5 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:01:45 -0400 Subject: [PATCH 174/687] Engine: a document's maker says where its content came from `Trust` defaulted to `Local`, and `DocState::new` set it, so a document was trusted unless its maker remembered to mark it. Every current path that reads a file goes through `file.open`, which marks it `FromFile`, but a later way of building a document from bytes (recovery, templates, a new import) would have come out trusted by omission. `DocState::new` now takes the `Trust`, and `Trust` has no default. New workbooks and samples pass `Local`, `file.open` passes `FromFile`, a new window passes the trust of its workbook, and the tests that build documents from bytes pass `FromFile`. --- crates/engine/src/cmd/extra.rs | 3 +-- crates/engine/src/cmd/file.rs | 7 +++---- crates/engine/src/lib.rs | 16 ++++++++++------ crates/engine/src/sample.rs | 2 +- crates/engine/src/tests/seams.rs | 16 ++++++++++++++++ crates/engine/tests/it/dynamicmetadata.rs | 2 +- crates/engine/tests/it/long_operator_chains.rs | 2 +- crates/ui-egui/tests/it/interaction.rs | 2 +- crates/ui-egui/tests/it/scroll_position.rs | 2 +- docs/architecture.md | 15 ++++++++------- 10 files changed, 43 insertions(+), 24 deletions(-) diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index 6197dd5e29..009d77f2a5 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -809,10 +809,9 @@ fn split(s: &mut Session, _: &Json) -> Result { fn new_window(s: &mut Session, _: &Json) -> Result { let d = s.doc()?.clone(); - let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title())); + let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title()), d.trust); copy.selection = d.selection.clone(); copy.scripts_trusted = d.scripts_trusted; - copy.trust = d.trust; let i = s.add_document(copy); Ok(json!({"index": i})) } diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index 7aee509210..b3e7309e2d 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -34,7 +34,7 @@ fn new_workbook(s: &mut Session, p: &Json) -> Result { Some(name) => { let wb = crate::sample::build(name).ok_or_else(|| bad("file.new", format!("unknown sample `{name}`")))?; let title = crate::sample::title(name); - s.add_document(DocState::new(wb, None, title)) + s.add_document(DocState::new(wb, None, title, crate::Trust::Local)) } None => s.new_workbook(), }; @@ -83,12 +83,11 @@ fn open(s: &mut Session, p: &Json) -> Result { { s.close_document(0); } - let mut doc = DocState::new(wb, path, title); + // Someone else may have made the file: its active content asks before it runs. + let mut doc = DocState::new(wb, path, title, crate::Trust::FromFile); if crate::io::FileKind::from_path(&name).is_none() && gridcraft_xlsx::sniff(&bytes) == gridcraft_xlsx::Format::Xlsx { doc.content_kind = Some(crate::io::FileKind::Xlsx); } - // Someone else may have made the file: its active content asks before it runs. - doc.trust = crate::Trust::FromFile; let i = s.add_document(doc); // The decrypted-workbook notice is for the info bar, not a message box. let shown: Vec<&str> = warnings.iter().map(String::as_str).filter(|w| *w != crate::io::DECRYPTED_NOTICE).collect(); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 5c6e9f565c..1fd2f267b1 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -134,12 +134,13 @@ pub struct Clipboard { pub trust: Trust, } -/// Whether a document's active content (macros) may run without asking the person first. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)] +/// Whether a document's active content (macros) may run without asking the person first. There +/// is no default: whoever makes a document says where it came from ([`DocState::new`]), so a new +/// way of building one from bytes can't come out trusted by omission. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] #[serde(rename_all = "camelCase")] pub enum Trust { /// Made in this session: a new workbook or a sample. - #[default] Local, /// Opened from a file, or holding content pasted from one: active content asks before it /// runs. @@ -259,7 +260,10 @@ fn trim_to_budget(history: &mut Vec, byte_budget: usize) { } impl DocState { - pub fn new(mut wb: Workbook, path: Option, title: String) -> DocState { + /// A document for `wb`, which came from where `trust` says: [`Trust::FromFile`] for anything + /// read from bytes (a file, a download, a recovery copy), [`Trust::Local`] only for a workbook + /// made in this session. + pub fn new(mut wb: Workbook, path: Option, title: String, trust: Trust) -> DocState { let mut calc = Calc::new(); calc.recalc_all(&mut wb); let scripts_trusted = !cmd::extra::has_scripts(&wb); @@ -280,7 +284,7 @@ impl DocState { unlocked_ranges: vec![], content_kind: None, scripts_trusted, - trust: Trust::Local, + trust, } } pub fn is_dirty(&self) -> bool { @@ -480,7 +484,7 @@ impl Session { let name = wb.next_sheet_name(); wb.sheets.push(Arc::new(gridcraft_model::Sheet::new(name))); } - self.add_document(DocState::new(wb, None, format!("Book{n}"))) + self.add_document(DocState::new(wb, None, format!("Book{n}"), Trust::Local)) } /// Journals a command, then trims the oldest entries past either cap. A recording in progress diff --git a/crates/engine/src/sample.rs b/crates/engine/src/sample.rs index 129fe2171b..90029bac36 100644 --- a/crates/engine/src/sample.rs +++ b/crates/engine/src/sample.rs @@ -26,7 +26,7 @@ pub fn build(name: &str) -> Option { _ => return None, }; } - s.add_document(DocState::new(wb, None, title(name))); + s.add_document(DocState::new(wb, None, title(name), crate::Trust::Local)); let ok = match name { "budget" => budget(&mut s), "sales" => sales(&mut s), diff --git a/crates/engine/src/tests/seams.rs b/crates/engine/src/tests/seams.rs index cab24426ec..b445377b77 100644 --- a/crates/engine/src/tests/seams.rs +++ b/crates/engine/src/tests/seams.rs @@ -961,6 +961,22 @@ fn documents_know_where_they_came_from() { assert_eq!(trust(&s), Trust::FromFile); } +#[test] +fn whoever_makes_a_document_says_where_it_came_from() { + // No default trust: a document built from bytes outside `file.open` (a recovery copy, a + // template) is untrusted because its maker says so, not trusted because it forgot to. + let mut s = Session::new(); + let bytes = gridcraft_xlsx::write_xlsx(&gridcraft_model::Workbook::new()).unwrap(); + let (wb, _) = crate::io::open_bytes("recovered.xlsx", &bytes).unwrap(); + s.add_document(crate::DocState::new(wb, None, "Recovered".into(), Trust::FromFile)); + assert_eq!(trust(&s), Trust::FromFile); + assert_eq!(s.execute("document.inspect", json!({})).unwrap()["trust"], "fromFile"); + s.add_document(crate::DocState::new(gridcraft_model::Workbook::new(), None, "Made here".into(), Trust::Local)); + assert_eq!(trust(&s), Trust::Local); + s.new_workbook(); + assert_eq!(trust(&s), Trust::Local); +} + #[test] fn pasting_from_a_file_carries_its_trust() { let mut s = s(); // document 0: Local diff --git a/crates/engine/tests/it/dynamicmetadata.rs b/crates/engine/tests/it/dynamicmetadata.rs index e1edfc9581..f88805476f 100644 --- a/crates/engine/tests/it/dynamicmetadata.rs +++ b/crates/engine/tests/it/dynamicmetadata.rs @@ -26,7 +26,7 @@ fn opened() -> Session { let bytes = io::save_bytes(&wb, "dynamic.xlsx").unwrap(); let (back, _) = io::open_bytes("dynamic.xlsx", &bytes).unwrap(); let mut s = Session::new(); - s.add_document(DocState::new(back, None, "dynamic".into())); + s.add_document(DocState::new(back, None, "dynamic".into(), gridcraft_engine::Trust::FromFile)); s } diff --git a/crates/engine/tests/it/long_operator_chains.rs b/crates/engine/tests/it/long_operator_chains.rs index 97c95ce389..e5e5af4b7d 100644 --- a/crates/engine/tests/it/long_operator_chains.rs +++ b/crates/engine/tests/it/long_operator_chains.rs @@ -78,7 +78,7 @@ fn longest_flat_chains_survive_editing_saving_and_reopening() { let bytes = io::save_bytes(wb, "chains.xlsx").expect("save"); let (reopened, _) = io::open_bytes("chains.xlsx", &bytes).expect("open"); let mut session = Session::new(); - session.add_document(DocState::new(reopened, None, "chains".into())); + session.add_document(DocState::new(reopened, None, "chains".into(), gridcraft_engine::Trust::FromFile)); session.execute("formulas.calculateNow", json!({})).expect("recalc"); assert_eq!(value(&session, "C2"), Value::Number(4096.0)); assert_eq!(value(&session, "C3"), Value::Number(-4094.0)); diff --git a/crates/ui-egui/tests/it/interaction.rs b/crates/ui-egui/tests/it/interaction.rs index 46e981d0aa..08317aad3b 100644 --- a/crates/ui-egui/tests/it/interaction.rs +++ b/crates/ui-egui/tests/it/interaction.rs @@ -84,7 +84,7 @@ fn horizontal_scrollbar_pages_and_drags_across_a_wide_sheet() { // Column A frozen: a page is the scrolling part only. sh.freeze = Some((0, 1)); let mut s = Session::new(); - s.add_document(gridcraft_engine::DocState::new(wb, None, "Book1".into())); + s.add_document(gridcraft_engine::DocState::new(wb, None, "Book1".into(), gridcraft_engine::Trust::Local)); let mut h = harness(s); let track = h.state().grid.hscroll.expect("the tab bar has a horizontal scrollbar"); assert!(track.width() >= 200.0, "the scrollbar gets a usable share of the tab bar: {track:?}"); diff --git a/crates/ui-egui/tests/it/scroll_position.rs b/crates/ui-egui/tests/it/scroll_position.rs index 7945eb3444..bcf62b1ac3 100644 --- a/crates/ui-egui/tests/it/scroll_position.rs +++ b/crates/ui-egui/tests/it/scroll_position.rs @@ -18,7 +18,7 @@ fn harness(freeze: Option<(u32, u32)>) -> Harness { sh.view_active = CellRef::parse("A31").unwrap(); sh.freeze = freeze; let mut session = Session::new(); - session.add_document(DocState::new(wb, None, "Book1".into())); + session.add_document(DocState::new(wb, None, "Book1".into(), gridcraft_engine::Trust::Local)); let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state( |ui, app: &mut SheetApp| { app.logic(ui.ctx()); diff --git a/docs/architecture.md b/docs/architecture.md index 3fc6cb0b10..2a5bed44a7 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -71,13 +71,14 @@ Actions made of many commands (macros, a debugger that runs one step per call) u dialog (the dialog's confirming call is the step), and the buffer is independent of the journal (Automate › Record Actions still records through the journal). - **Document trust.** `DocState::trust` is `Local` for workbooks made in the session and - `FromFile` for workbooks opened from a file, and for workbooks that received a paste from one - that writes formulas or validation: Paste, Paste Special › All, All Except Borders, Formulas, - Formulas and Number Formats or Validation, and any linked paste other than column widths or - notes. Values, formats, column widths and notes carry nothing active. Active content in a - `FromFile` workbook asks before it runs. This - is provenance, which no decision changes; `DocState::scripts_trusted` is the separate - per-session consent for stored `automate.*` scripts (`automate.trustScripts`). + `FromFile` for workbooks opened from a file. `Trust` has no default: `DocState::new` takes it, + so every way of making a document says where its content came from. It is also `FromFile` for + workbooks that received a paste from one that writes formulas or validation: Paste, Paste + Special › All, All Except Borders, Formulas, Formulas and Number Formats or Validation, and any + linked paste other than column widths or notes. Values, formats, column widths and notes carry + nothing active. Active content in a `FromFile` workbook asks before it runs. This is provenance, + which no decision changes; `DocState::scripts_trusted` is the separate per-session consent for + stored `automate.*` scripts (`automate.trustScripts`). - **Workbook data in hidden names** (`cmd/stash.rs`). An item is split over hidden workbook-scope names `_gc__0`, `_gc__1`, … of string literals within Excel's 255- and 8,192-character limits, so it survives an xlsx round trip. Characters XML can't carry (controls From b44a5139ab66bb8b92d0d790c629b29e456035a1 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:19:07 -0400 Subject: [PATCH 175/687] Engine: the name commands refuse only names that hold GridCraft's data The guard refused any name of the stash's shape, so a visible name a person had defined, such as _gc_rate_0, could not be edited or deleted from Name Manager, which cannot pass force. It now refuses a name only when the workbook holds a hidden workbook-scope item under it; a visible name of the shape is the person's own, as the stash itself treats it. The test covers such a name. The query comment says what the copy skip relies on: a query runs nothing that changes the history. --- crates/engine/src/cmd/formulas.rs | 36 ++++++++++++++++++++++--------- crates/engine/src/lib.rs | 4 ++-- docs/architecture.md | 5 +++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index c5f8a8bf80..b3f79e81a3 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -1,7 +1,7 @@ //! Formulas tab: AutoSum, Insert Function, names, auditing, calculation. use gridcraft_core::{CellError, CellRef, RangeRef, Value}; -use gridcraft_model::{CalcMode, DefinedName, Formula}; +use gridcraft_model::{CalcMode, DefinedName, Formula, Workbook}; use serde_json::{Value as Json, json}; use super::*; @@ -32,7 +32,7 @@ pub fn specs() -> Vec { "Define Name…", ["Formulas", "Defined Names"], None, - "{name, refersTo?: \"=Sheet1!$A$1:$A$10\" (default: the selection), scope?: \"Workbook\"|sheet name, comment?, oldName?, oldScope?, force?: bool} (oldName: edit that name (Name Manager › Edit); a new name also renames it in formulas; names GridCraft keeps its own data in, `_gc__` and `_sc_`, are refused unless force is true)", + "{name, refersTo?: \"=Sheet1!$A$1:$A$10\" (default: the selection), scope?: \"Workbook\"|sheet name, comment?, oldName?, oldScope?, force?: bool} (oldName: edit that name (Name Manager › Edit); a new name also renames it in formulas; a hidden name GridCraft keeps its own data in, `_gc__` or `_sc_`, is refused unless force is true)", has_doc, define_name ), @@ -41,7 +41,7 @@ pub fn specs() -> Vec { "Delete Name", [], None, - "{name, scope?: \"Workbook\"|sheet name (default: every scope), force?: bool} (names GridCraft keeps its own data in, `_gc__` and `_sc_`, are refused unless force is true)", + "{name, scope?: \"Workbook\"|sheet name (default: every scope), force?: bool} (a hidden name GridCraft keeps its own data in, `_gc__` or `_sc_`, is refused unless force is true)", has_doc, delete_name ), @@ -277,7 +277,7 @@ fn define_name(s: &mut Session, p: &Json) -> Result { return Err(EngineError::Other("The name that you entered is not valid.".into())); } for n in [Some(name.as_str()), str_param(p, "oldName")].into_iter().flatten() { - refuse_stash_name(p, n)?; + refuse_stash_name(&s.doc()?.wb, p, n)?; } let d = s.doc()?; let scope = match str_param(p, "scope") { @@ -388,8 +388,15 @@ fn rename_name_in_formulas(wb: &mut gridcraft_model::Workbook, old: &str, old_sc /// Refuses a name of the shape GridCraft keeps its own workbook data in ([`super::stash`]): /// defining, renaming or deleting one would replace or remove part of an item (a stored macro, /// script or view) and leave the rest behind. `force: true` lets the call go ahead. -fn refuse_stash_name(p: &Json, name: &str) -> Result<()> { - if stash::item_key(name).is_some() && !bool_param(p, "force").unwrap_or(false) { +/// Whether `name` is one the workbook holds GridCraft's own data in: a hidden workbook-scope +/// name of an item's shape. A visible name of that shape is the person's own (see `stash`), and +/// the name commands may change it. +fn holds_gridcraft_data(wb: &Workbook, name: &str) -> bool { + wb.names.iter().any(|n| n.hidden && n.scope.is_none() && n.name.eq_ignore_ascii_case(name) && stash::item_key(&n.name).is_some()) +} + +fn refuse_stash_name(wb: &Workbook, p: &Json, name: &str) -> Result<()> { + if holds_gridcraft_data(wb, name) && !bool_param(p, "force").unwrap_or(false) { return Err(EngineError::Other(format!( "'{name}' is a name GridCraft keeps its own data in (stored macros, scripts or views). Pass `force: true` to change it anyway." ))); @@ -399,7 +406,7 @@ fn refuse_stash_name(p: &Json, name: &str) -> Result<()> { fn delete_name(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("formulas.deleteName", "missing `name`"))?.to_string(); - refuse_stash_name(p, &name)?; + refuse_stash_name(&s.doc()?.wb, p, &name)?; let scope = match str_param(p, "scope") { None => None, Some("Workbook") => Some(None), @@ -485,10 +492,10 @@ fn create_from_selection(s: &mut Session, p: &Json) -> Result { // Excel asks before replacing an existing name; here it's kept unless `replace` is set, and // reported. let exists = |n: &str| d.wb.names.iter().any(|x| x.name.eq_ignore_ascii_case(n) && x.scope.is_none()); - // A label of the shape GridCraft keeps its own data in would replace part of an item, or - // stand in the way of one (see `refuse_stash_name`). + // A label that names one of GridCraft's own hidden data names would replace part of an item + // (see `refuse_stash_name`). let force = bool_param(p, "force").unwrap_or(false); - let ours = |n: &str| !force && stash::item_key(n).is_some(); + let ours = |n: &str| !force && holds_gridcraft_data(&d.wb, n); let (made, skipped): (Vec<_>, Vec<_>) = made.into_iter().partition(|(n, _)| !ours(n) && (replace || !exists(n))); let skipped: Vec = skipped.into_iter().map(|(n, _)| n).collect(); let n = made.len(); @@ -802,6 +809,15 @@ mod tests { let after = names(&s); assert_eq!(after.iter().filter(|n| n.name != "Rate").cloned().collect::>(), before); assert_eq!(stash::get(&s.doc().unwrap().wb, "macro_1").as_deref(), Some(text.as_str())); + // A visible name of the shape is the person's own (the stash never reads it): the name + // commands change and remove it without `force`, as Name Manager cannot pass one. + let own = DefinedName { name: "_gc_rate_0".into(), scope: None, formula: "=1".into(), comment: String::new(), hidden: false }; + std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).names.push(own.clone()); + s.execute("formulas.defineName", json!({"name": "Rate0", "refersTo": "=2", "oldName": "_gc_rate_0"})).unwrap(); + s.execute("formulas.deleteName", json!({"name": "Rate0"})).unwrap(); + std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).names.push(own); + s.execute("formulas.deleteName", json!({"name": "_gc_rate_0"})).unwrap(); + assert_eq!(stash::get(&s.doc().unwrap().wb, "macro_1").as_deref(), Some(text.as_str()), "the hidden item is untouched"); // Ordinary names and forced calls go ahead. s.execute("formulas.deleteName", json!({"name": "Rate"})).unwrap(); s.execute("formulas.defineName", json!({"name": "_gc_macro_1_1", "refersTo": "=1", "force": true})).unwrap(); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 1fd2f267b1..b4ab60f3a9 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -538,8 +538,8 @@ impl Session { let start = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (self.in_group(*uid), d.undo.len()))); // A command that panics leaves the history as it found it: the steps commands nested in it // made or undid, and the redo history they cleared, come back. A length is not enough, - // because a nested `edit.undo` removes entries. A query runs no commands and leaves the - // history alone (`every_query_leaves_the_history_alone` runs each one), so it needs no + // because a nested `edit.undo` removes entries. A query runs nothing that changes the + // history (`every_query_leaves_the_history_alone` runs each one), so it needs no // copy; front ends run queries all the time. let query = !spec.journal && !spec.undoable; let history_before = before.as_ref().filter(|_| !query).and_then(|(uid, ..)| { diff --git a/docs/architecture.md b/docs/architecture.md index 2a5bed44a7..bfbf2b8c1d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -86,8 +86,9 @@ Actions made of many commands (macros, a debugger that runs one step per call) u U+FDD0 and four hex digits, as is U+FDD0 itself, and read back as they were. `stash::items` and `stash::keys` list the items under a key prefix in one pass over the names, at most 10,000 of them (`stash::MAX_ITEMS`). The older single-name `_sc_` items are read as well. - `formulas.defineName` and `formulas.deleteName` refuse names of either shape, and - `formulas.createFromSelection` skips labels of either shape, unless `force: true` is passed. + `formulas.defineName` and `formulas.deleteName` refuse a name the workbook holds such an item + under (a hidden workbook-scope name of either shape; a visible one is the person's own), and + `formulas.createFromSelection` skips such labels, unless `force: true` is passed. ## Agent control From b09a5502e3a7a092561d13aaa262881b7f3d05c2 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:30:15 -0400 Subject: [PATCH 176/687] UI: the toolbar's Undo and Redo follow the commands' own availability The ribbon enabled them from the history lengths alone, so while an undo group was open on the document (an action spanning several calls) a click ran edit.undo and got its refusal. The buttons now ask cmd::can_undo and cmd::can_redo, the undo list is empty while a group is open, and the two buttons carry a name and their enabled state for assistive technology. A UI test opens a group, clicks both and checks the history and their state. --- crates/ui-egui/src/ribbon.rs | 22 +++++-- crates/ui-egui/tests/it/main.rs | 1 + .../ui-egui/tests/it/undo_while_grouping.rs | 57 +++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/tests/it/undo_while_grouping.rs diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 45856565f4..ab9cc311dd 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -74,15 +74,23 @@ pub fn title_bar(app: &mut SheetApp, ui: &mut Ui) { { app.run_or_alert("file.save", json!({})); } - let can_undo = app.session.active().is_some_and(|d| !d.undo.is_empty()); - let can_redo = app.session.active().is_some_and(|d| !d.redo.is_empty()); - if icon_button(ui, Icon::Undo, if can_undo { t.text_dim } else { t.text_disabled }, "Undo", vec2(26.0, ch)).clicked() && can_undo { + // The commands' own availability: there must be a step, and no undo group may be open on + // the document (an action that spans several calls is still running). + let can_undo = gridcraft_engine::cmd::can_undo(&app.session).is_ok(); + let can_redo = gridcraft_engine::cmd::can_redo(&app.session).is_ok(); + // Named and marked enabled or not for assistive technology (and the UI tests). + let undo = icon_button(ui, Icon::Undo, if can_undo { t.text_dim } else { t.text_disabled }, "Undo", vec2(26.0, ch)); + undo.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, can_undo, "Undo")); + if undo.clicked() && can_undo { app.run_or_alert("edit.undo", json!({})); } let undo_list = icon_button(ui, Icon::Chevron, t.text_dim, "Undo list", vec2(14.0, ch)); egui::Popup::menu(&undo_list).show(|ui| { - let labels: Vec = - app.session.active().map(|d| d.undo.iter().rev().take(20).map(|e| e.label.clone()).collect()).unwrap_or_default(); + let labels: Vec = if can_undo { + app.session.active().map(|d| d.undo.iter().rev().take(20).map(|e| e.label.clone()).collect()).unwrap_or_default() + } else { + Vec::new() + }; if labels.is_empty() { ui.label("Can't Undo"); } @@ -92,7 +100,9 @@ pub fn title_bar(app: &mut SheetApp, ui: &mut Ui) { } } }); - if icon_button(ui, Icon::Redo, if can_redo { t.text_dim } else { t.text_disabled }, "Redo", vec2(26.0, ch)).clicked() && can_redo { + let redo = icon_button(ui, Icon::Redo, if can_redo { t.text_dim } else { t.text_disabled }, "Redo", vec2(26.0, ch)); + redo.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, can_redo, "Redo")); + if redo.clicked() && can_redo { app.run_or_alert("edit.redo", json!({})); } let more = icon_button(ui, Icon::More, t.text_dim, "More commands", vec2(26.0, ch)); diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index fc00fb5201..b9725aed07 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -85,6 +85,7 @@ mod touch_pan; mod trace_navigation; mod translated_form_fields; mod ui_clock; +mod undo_while_grouping; mod validation_alert; mod validation_input_message; mod wheel_zoom; diff --git a/crates/ui-egui/tests/it/undo_while_grouping.rs b/crates/ui-egui/tests/it/undo_while_grouping.rs new file mode 100644 index 0000000000..fdc98ebf9c --- /dev/null +++ b/crates/ui-egui/tests/it/undo_while_grouping.rs @@ -0,0 +1,57 @@ +//! Undo and Redo on the Quick Access Toolbar follow the commands' own availability: while an undo +//! group is open on the document (an action that spans several calls is still running), both +//! buttons are off, and a click neither runs the command nor raises its refusal. + +use egui_kittest::kittest::{NodeT as _, Queryable as _}; +use gridcraft_engine::Session; +use gridcraft_ui_egui::SheetApp; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +fn harness() -> Harness { + let mut s = Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "A1", "input": "1"})).unwrap(); + s.execute("cell.set", json!({"cell": "A1", "input": "2"})).unwrap(); + s.execute("edit.undo", json!({})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + SheetApp::new(s, Default::default()), + ); + h.run_steps(4); + h +} + +fn history(h: &Harness) -> (usize, usize) { + let d = h.state().session.active().unwrap(); + (d.undo.len(), d.redo.len()) +} + +#[test] +fn undo_and_redo_wait_while_an_undo_group_is_open() { + let mut h = harness(); + assert_eq!(history(&h), (1, 1)); + let token = h.state_mut().session.group_begin("Long action").unwrap(); + h.run_steps(2); + for button in ["Undo", "Redo"] { + assert!(h.get_by_label(button).accesskit_node().is_disabled(), "{button} is off while the group is open"); + h.get_by_label(button).click(); + h.run_steps(2); + assert!(h.state().message.is_none(), "{button}: no refusal is raised while the group is open"); + assert_eq!(history(&h), (1, 1), "{button}: the history is as it was"); + } + h.state_mut().session.group_end(token).unwrap(); + h.run_steps(2); + assert!(!h.get_by_label("Undo").accesskit_node().is_disabled() && !h.get_by_label("Redo").accesskit_node().is_disabled()); + // With the group ended, the buttons work again. + h.get_by_label("Undo").click(); + h.run_steps(2); + assert_eq!(history(&h), (0, 2)); + h.get_by_label("Redo").click(); + h.run_steps(2); + assert_eq!(history(&h), (1, 1)); +} From 4a294aefa9fb496ccd55ee6f93bd51f915044e88 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:41:03 -0400 Subject: [PATCH 177/687] Engine: the name commands use the stash's own rule for what holds its data The guard asked for a hidden name, but the stash reads a legacy _sc_ name whether it is hidden or not, so a visible one could be removed or replaced without force. stash::holds_item is the one rule (item_of), shared by the guard; the test covers a visible legacy name. --- crates/engine/src/cmd/formulas.rs | 8 ++++++-- crates/engine/src/cmd/stash.rs | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index b3f79e81a3..7718279b28 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -392,7 +392,8 @@ fn rename_name_in_formulas(wb: &mut gridcraft_model::Workbook, old: &str, old_sc /// name of an item's shape. A visible name of that shape is the person's own (see `stash`), and /// the name commands may change it. fn holds_gridcraft_data(wb: &Workbook, name: &str) -> bool { - wb.names.iter().any(|n| n.hidden && n.scope.is_none() && n.name.eq_ignore_ascii_case(name) && stash::item_key(&n.name).is_some()) + // The stash's own rule, so the two never disagree: a legacy `_sc_` item is read visible or not. + stash::holds_item(wb, name) } fn refuse_stash_name(wb: &Workbook, p: &Json, name: &str) -> Result<()> { @@ -787,6 +788,8 @@ mod tests { let text = "m".repeat(9_000); stash::set(wb, "macro_1", &text).unwrap(); wb.names.push(DefinedName { name: "_sc_watch".into(), scope: None, formula: "\"[]\"".into(), comment: String::new(), hidden: true }); + // A legacy item is read whether or not it is hidden, so a visible one is data too. + wb.names.push(DefinedName { name: "_sc_old".into(), scope: None, formula: "\"[]\"".into(), comment: String::new(), hidden: false }); let names = |s: &Session| s.doc().unwrap().wb.names.clone(); let before = names(&s); assert!(before.len() >= 3, "the item takes two chunks"); @@ -795,11 +798,12 @@ mod tests { json!({"name": "_SC_Watch", "refersTo": "=1"}), json!({"name": "Mine", "refersTo": "=1", "oldName": "_gc_macro_1_1"}), json!({"name": "_gc_macro_1_0", "refersTo": "=1", "force": false}), + json!({"name": "Mine2", "refersTo": "=1", "oldName": "_SC_old"}), ] { let err = s.execute("formulas.defineName", params.clone()).unwrap_err().to_string(); assert!(err.contains("force"), "{params}: {err}"); } - for name in ["_gc_macro_1_1", "_sc_watch"] { + for name in ["_gc_macro_1_1", "_sc_watch", "_sc_old"] { let err = s.execute("formulas.deleteName", json!({"name": name})).unwrap_err().to_string(); assert!(err.contains("force"), "{name}: {err}"); } diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs index 630e84ab64..c0e8302ca5 100644 --- a/crates/engine/src/cmd/stash.rs +++ b/crates/engine/src/cmd/stash.rs @@ -215,6 +215,13 @@ pub fn keys(wb: &Workbook, prefix: &str) -> Vec { group(wb, prefix).into_iter().filter(|(_, parts)| parts.is_item()).map(|(k, _)| k).take(MAX_ITEMS).collect() } +/// Whether the workbook holds an item under `name` (compared without case): a hidden workbook-scope +/// `_gc__` name, or a workbook-scope legacy `_sc_` name, which is read whether or not +/// it is hidden. A visible `_gc_` name is the person's own. +pub fn holds_item(wb: &Workbook, name: &str) -> bool { + wb.names.iter().any(|n| n.name.eq_ignore_ascii_case(name) && item_of(n).is_some()) +} + /// The item a hidden name belongs to, lower-cased: `_gc__` and `_sc_` give ``. pub fn item_key(name: &str) -> Option { parse_name(name).map(|(k, _)| k.to_ascii_lowercase()) From 3b25aa0d16071b460cc7c006d96b57441b958f22 Mon Sep 17 00:00:00 2001 From: prxnvt <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 01:41:03 -0400 Subject: [PATCH 178/687] Docs: the panic guard's note says what happens to a group the command ended It keeps a step from the group's start to the restored workbook, so Undo still returns to the state before the group; the note said the history came back exactly. --- docs/architecture.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/architecture.md b/docs/architecture.md index bfbf2b8c1d..4794a6ec9f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -34,7 +34,9 @@ including tests, 2026-10-10. enabled, run, journal, undoable }`; ids follow Excel's ribbon (`home.bold`, `insert.chart`, `data.sortAscending`) plus primitives (`cell.set`, `selection.set`, `range.setValues`). `Session::execute` catches escaped panics and restores the workbook, its selection and its - undo and redo history as they were before the command (never-crash guard). + undo and redo history as they were before the command (never-crash guard); a group that was + open before the command and that the command ended or rolled back stays ended and keeps a + step from its start to the restored workbook, so Undo still returns to the state before it. 2. **Edits** change the model through copy-on-write bands; undo keeps the previous snapshot. Undo retains at most 100 steps and trims older steps when its estimated retained cell storage exceeds 128 MiB. The newest step remains undoable even if that step alone exceeds the budget. From 6ead1fb51936f7d368c09e8ce68806f51589b6c3 Mon Sep 17 00:00:00 2001 From: Pranav <53838599+prxnvt@users.noreply.github.com> Date: Sat, 24 Oct 2026 02:06:44 -0400 Subject: [PATCH 179/687] Engine: building blocks for multi-command actions (undo groups, recording buffer, document trust, chunked hidden names) (#462) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Engine: undo groups make several commands one undo step Session::grouped(label, f) runs f as one undo step on the active document. On success the group leaves exactly one undo entry holding the workbook from before the group; on error it restores the workbook, selection, undo and redo stacks as they were and returns the error. group_begin, group_end and group_rollback do the same across separate calls, for an action that runs one step per call. While a group is open on a document, undoable commands on it add no undo steps of their own, so a long group neither grows the history nor pushes older entries past the undo limit, and Undo and Redo are disabled there. Groups nest: the outermost one makes the step, and a failing inner group rolls back only its own changes. Closing a document drops its groups, and a command that panics drops the groups it opened. history.list reports whether a group is open. Insert Copied Cells folds its nested steps into one undo step by counting revisions; inside a group the nested steps push no entries, so it now leaves the history alone there instead of removing entries from before the group. * Engine: a recording buffer of top-level commands, apart from the journal A macro recorder needs more than a journal index: the journal is trimmed past 10,000 entries, it also holds the commands that other commands run (so playback would run them twice), and it keeps no selection, which relative references need. Session now tracks how deeply execute calls are nested. While recording, a buffer (record.rs) keeps each successful, journaled top-level call with the document uid, the active sheet (index and name) and the selection it ran in, then its id and parameters without secrets, as the journal stores them. Queries, failed commands and the command that starts recording are not kept. The buffer holds up to 100,000 steps and counts any it drops. Session::start_recording, stop_recording, is_recording and depth are the interface. Automate › Record Actions keeps recording through the journal as before. * Engine: document trust for workbooks opened from files Active content such as macros must not run only because a workbook was opened. DocState gets a trust state: Local for workbooks made in the session (new workbooks and samples) and FromFile for anything opened with file.open. Active content in a FromFile workbook asks before it runs. A second window on a workbook shares its trust. The clipboard carries the trust of its source document. Pasting formulas, formats (conditional formats hold formulas) or validation from a FromFile workbook into another workbook makes that workbook FromFile too; pasting values, column widths or notes carries nothing active and changes nothing. Sheet moves and copies stay inside one workbook, so its trust is unchanged. document.inspect reports the trust. * Engine: workbook data in chunked hidden names (cmd/stash.rs) GridCraft keeps workbook data in hidden defined names so it travels inside the xlsx file. The existing items write one name holding one string literal, which past Excel's 255-character literal and 8,192-character formula limits gives a file Excel does not accept. cmd/stash.rs stores an item `key` as hidden workbook-scope names _gc__0, _gc__1, ..., each a formula of string literals of at most 255 characters (UTF-16 units, as Excel counts them) joined with &, at most 8,192 characters per name. Reading concatenates the chunks in order and stops at a gap; it also accepts the older single _sc_ name. Names are matched case-insensitively and only at workbook scope. Keys are limited to letters, digits, _ and ., items to 4,194,304 characters, and junk around or inside the literals, duplicate or out-of-order chunks, non-JSON and over-deep JSON all read safely. keys(prefix) lists the stored items that start with a prefix. Custom views, scripts, the watch list and scenarios keep their current storage; nothing reads or writes the new names yet. docs/architecture.md describes the engine building blocks added in this series: undo groups, the recording buffer, document trust and this storage. * Engine: a linked paste of values from a file carries the file's trust A linked paste writes formulas even when it pastes values, but the values exception skipped the trust update, so a Local workbook stayed Local after linking to a FromFile one. The exception now applies only to unlinked pastes. * Engine: a stash item with only later chunks reads as missing and falls back to the legacy name With chunk _gc_k_2 but no _gc_k_0, get returned Some("") and never looked at _sc_k. It now takes the legacy path unless chunk 0 exists. * Engine: a command that runs its own undo groups makes one undo step for itself execute treated any change in the group epoch as 'the group owns the step'. A command that edited outside its own group, or whose group rolled back before it edited, left that edit with no undo step or only part of it. Now only a group that was open before the command (or is still open after) owns the step; a command that opened and closed its own groups replaces their steps with one entry for the command. * Engine: a full undo history keeps one step per command that runs its own undo groups When the history held 100 steps, group_end pushed its step and dropped the oldest one at once, so execute's collapse compared against a length that no longer matched. A command that was one group kept the group's step and added its own, losing two old steps. After test.editThenGroup, the second Undo brought back the intermediate state after the outside edit. The history limit now applies after the outermost command has collapsed its steps, and when a group ends outside any command. A document with an open group is trimmed when the group ends. * Engine: pasting only formats from a file leaves the destination's trust alone Paste Special › Formats copies cell styles and merged areas. It copies no conditional formats, formulas or validation, so it carries nothing that runs. It still marked the destination FromFile: copying a formula cell from an opened file and pasting its formats into a new workbook made that workbook ask before running its own active content. Formats pastes now count as passive, like values, column widths and notes. The comment and docs/architecture.md no longer say that pasted formats carry trust. * Engine: a command keeps its group's name only for a group it ended itself The record of the last group step (document, history length, epoch) outlived the Undo of that step. A later command that opened and closed its own groups at the same history length, after rolling one back, passed the epoch check (the record's epoch merely differed from the one the command started with) and took the name of the nested edit it had made instead of its own. The step must now come from a group ended during the command (an epoch past the command's starting one), and a rollback drops a record of a step it truncated away. * Engine: a group rolled back inside a command keeps the redo history A command that changed the workbook cleared the redo history even when an open group owned its step. When the group was then rolled back inside the same command (the debugger's Stop with Undo Changes), the rollback put the redo history back and the command's bookkeeping cleared it again. Now only a command that makes a step of its own clears the redo history; a group clears it when it ends as a step, and a rollback restores it. * Engine: a visible name of the item shape is not workbook data The item store read, listed and removed every workbook-scope name of the `_gc__` shape, visible or not, so saving an item removed a person's own visible name that happened to look like one. Items are the hidden names the store writes; a legacy `_sc_` name still reads. A visible `_gc_` name is left alone by every operation, and writing an item that would need that name is refused before anything changes: a hidden name beside it with the same name is the one an xlsx file drops, so the item would be lost on save. * Engine: a command keeps its group's name only for a step that is exactly the group's The record of the last group step held the document, the history length and a group epoch, and a command reused the group's label when the lengths matched and the epoch was newer. A direct edit after the group (no nested command, so the length still matches) then left the step named after the group alone. The record now holds the workbook the group's step led to (a weak reference, so it keeps nothing alive). A command keeps the group's name only when the one entry it added starts from the command's own starting workbook and ends at its result, which is provenance by identity rather than by position: an entry a nested command made, a step of an earlier command undone since, and a group step the command edited past all name the step after the command. A later group that rolled back changes nothing, and the rollback no longer needs to clear the record. * Engine: the recording buffer is bounded in bytes and keeps the start of a recording The buffer kept at most 100,000 steps but each step keeps its full parameters, and a file.open step holds the whole file as base64, so a recording of a few opens of a large workbook kept tens of MB alive. Like the journal, the buffer now also stops at 64 MB of steps serialized as JSON. A step that does not fit is counted as dropped, and once a step is dropped every later one is too, so what the buffer holds is always how the recording began, never a sequence with gaps. * Docs: the architecture revision row carries the date the change lands; document trust beside script trust The header and the revision row said 2026-10-11, older than the row above them. The document trust paragraph now says how DocState::trust (provenance) differs from DocState::scripts_trusted (per-session consent for stored scripts, #2178). * Engine: writing a stash item checks for a clashing visible name in one pass over the names The check ran once per chunk, each a scan of every defined name: an item of many chunks in a workbook with thousands of names compared every pair. One pass now parses each visible workbook-scope name and refuses when it is a chunk of this key below the chunk count. The behaviour and its test are unchanged. * Engine: a command's changes after ending a group, or before opening one it leaves open, are not lost or a step of their own Two boundaries of the group rules were wrong. A command that ended a group open before it was treated as grouped whatever it did next: with an unchanged group (no step) its later edit had no undo entry at all, and with nested edits after the end the entry was the nested command's. The group's step now stays the group's, and the command makes one step of its own, under its name, for what changed after the end, from the workbook the group ended at. A command that edited and then opened a group it leaves open (a debugger's start) left its nested entries in the history, so one Undo after the group ended brought back a state from inside the command. Such a group now takes the command's start as its own, and its step covers the command's earlier edits. The record of where the last group ended now holds the workbook and selection it ended at, the history length then and whether it made a step; it lives until the outermost command finishes. A rollback records where it ended too, unless the document is back exactly where the last recorded step left it. * Engine: the undo steps made inside a command are bounded while it runs The history limit is applied once the outermost command has finished, so the lengths its bookkeeping compares stay true (round 3). That left the steps nested commands make unbounded until then: a script of up to 100,000 steps, or a subtotal with a step per group, kept every one of them alive, each with the cells it retains, and before this branch every push trimmed the history at once. Each nested command now bounds the steps made since the latest mark on its document to the undo limit and byte budget, dropping the oldest of them. The marks are the starts of the running commands, the starts of the open groups and the end of the last group: the positions a command or group collapses its steps by when it finishes, so nothing before them moves. What the bound drops is only what the final trim would drop. * Engine: a grouped action that closes its document succeeds Session::grouped ended its group after the action, and when the action had closed the group's document (file.close, or file.open taking the place of an untouched new workbook) the group had gone with it, so the action was reported as failed although it had done what it was asked. A group that is gone because its document is gone now counts as ended; a group ended by someone else while the document is open is still an error. * Docs: undo groups at a command's boundaries, and the bound on steps made inside a command * Engine: a linked paste carries the file's trust whatever it pastes Paste Link writes a formula into every cell whatever `what` says, so a linked paste of formats from a file now makes the destination FromFile as a paste of formulas does. Column widths and notes write no cells, linked or not, and still carry nothing. Regression test for the three kinds. * Engine: a stash chunk number with leading zeros names someone else's name `_gc_watch_00` parsed as chunk 0, so a hidden one was read as the item's first chunk and a visible one stopped `_gc_watch_0` from being written. Only the chunk number as `set` writes it counts now. Regression test. * Engine: a command that panics leaves the undo history as it found it The panic guard restored the workbook and selection but not the history, so a nested edit made before the panic left a no-op undo step and the redo history it had cleared stayed lost. The guard now truncates the steps made during the command and puts the redo history back. Regression test, and the architecture note says what the guard restores. * Engine: a headless call that fails for want of a file is not recorded file.open with no file succeeds by queuing a dialog, which execute_headless turns into an error for callers with no UI; the recording kept the step. Recorder::truncate forgets it and keeps the byte count right. Regression test. * Engine: a later stash chunk on its own is not a listed item stash::keys reported a key that get read as missing, because only a later chunk of it existed. keys now counts chunk 0 and legacy names only, as get does. Regression assertions in the hostile-names test. * Tests: a linked paste of column widths or notes copies its metadata and no cell The source now carries a note and a column width, and the test checks that the destination received them while its trust stays Local and no cell is written. * Engine: the panic guard restores entries a nested Undo removed Truncating the undo history to its length at the command's start cannot bring back an entry that a nested edit.undo popped. The guard now keeps the undo and redo history as they were and puts both back. The regression test gains the undo-then-panic case. * Engine: a group a panicking command ended keeps its step A group open before a command and ended or rolled back inside it lost its undo step when the command then panicked: the restored history held no entry for the group's changes, so Undo skipped the state before the group. The panic path now pushes the outermost such group's step from its start to the restored workbook. The group stays ended, because its token went with the command. Regression test for a group ended and one rolled back. * Docs: list every paste that carries a file's trust The architecture note named formulas and validation only; it now lists the Paste Special kinds and linked pastes that write formulas, and the kinds that carry nothing. * Engine: a grouped action that panics is rolled back before the panic goes on Session::grouped ran its action without a panic guard, so a caller outside a command that caught the panic found the group still open: Undo and Redo disabled, the partial edits kept, and the next grouped action nested in the abandoned group. The group is now rolled back and the panic resumed. Regression test. * Engine: the recorder counts a dropped step without serializing it Once a step has been dropped, or the step count is full, push serialized each later step only to count it; a file.open step holds a whole file. The checks that need no size come first. * Engine: a failed command rolls back the undo groups it opened A command that opened an undo group and then returned an error (a `?` between `group_begin` and `group_end`) left the group open for good: Undo and Redo refused with "finish or stop the running action first", later edits made no undo steps, and nothing could stop the group. `execute` now rolls back every group a failed command opened, as `group_rollback` does, restoring the workbook, selection, undo and redo history to where each group began. Only a command that succeeds keeps a group it opened, which is what a debugger's start needs. `Session::abort_groups(doc)` and the menu-less command `automate.stopAction` roll back the groups left open on a document, for a debugger the person abandons or a caller that dropped its token. The command is not undoable and works while a group is open, and the Undo refusal now names it. * Engine: the recorder skips a call that only opened a dialog A menu-style call with no parameters, such as `file.open`, `data.fromTextCsv` or `home.formatCells` with `{}`, only queues its dialog and succeeds. The recorder kept it as a step with empty parameters, and then kept the dialog's confirming call as well, so playing the recording back ran the command twice, or failed on the empty step when played back headless. Only `execute_headless` took the step back, and only for the open and save-as dialogs. `execute` now compares the queued UI requests before and after a top-level command and does not record one that queued a dialog. The special case in `execute_headless` goes (it keeps turning a missing file into an error), and with it `Recorder::truncate`, which had no other caller. * Engine: stash items keep characters XML can't carry The xlsx writer drops characters XML 1.0 can't carry: controls other than tab, line feed and carriage return, and U+FFFE and U+FFFF. A stash item holding one lost it on save without a warning, and so did a JSON item, since serde_json leaves U+FFFE and U+FFFF unescaped: a stored macro parameter changed silently. `chunk_formulas` now writes each such character, and the marker itself, as the noncharacter U+FDD0 followed by four hex digits, and `get` turns them back for `_gc_` items. A marker that isn't followed by four hex digits naming a character is read as it is. An escape counts as five units against the 255-unit literal limit and is never split across literals, and the guard on the stored size allows the longest item even when every character is escaped. Legacy `_sc_` items are read unchanged. * Engine: list stash items in one pass over the names Each `stash::get` looks at every defined name, so listing the items with `keys` and then reading each with `get` took time quadratic in the number of names. A file with 20,000 hidden `_gc_macro__0` names took about ten seconds in a debug build, and the macro list reads its items this way from data that comes from the file. `stash::items(wb, prefix)` now groups the names by item in one pass and returns each item's text by key, and `keys` lists from the same pass. Both return at most `MAX_ITEMS` (10,000) items, the first keys in sorted order, so a file with any number of hidden names can't make a listing unbounded. A test counts the names each listing looks at. * Engine: the name commands leave GridCraft's data names alone `formulas.defineName {"name": "_gc_macro_1_0"}` replaced the hidden chunk of a stored item with a visible name: the item vanished from `stash::get` and `stash::keys`, and its other chunks stayed behind as orphan hidden names. `formulas.deleteName` could remove chunks the same way, and `formulas.createFromSelection` with `replace` could replace one from a cell label. `stash::set` refused a clash with a visible name, but nothing guarded the other direction. Defining, renaming or deleting a name that `stash::item_key` recognises (`_gc__` and legacy `_sc_`) is now refused with an error that names `force: true`, which lets the call go ahead. Create from Selection lists such labels in `skipped` unless `force` is true. The params docs of the three commands say so. * Engine: queries take no copy of the undo history `execute` copied the document's undo and redo history on every call, so a panic could put it back: up to 100 entries per stack, each with a label and a selection. Queries paid for it too, although no query runs a command or changes the history, and front ends and agents run queries all the time. The copy is now skipped for queries (commands that neither journal nor make undo steps). A test runs every query, with empty and with typical parameters, and checks the history is unchanged, so a query that ever changes it fails there. Commands that can change the history, nested ones included, still take the copy: a nested command that panics is restored at its own level, and the outer command can carry on, so relying on the outermost copy would weaken that restore. * Engine: a document's maker says where its content came from `Trust` defaulted to `Local`, and `DocState::new` set it, so a document was trusted unless its maker remembered to mark it. Every current path that reads a file goes through `file.open`, which marks it `FromFile`, but a later way of building a document from bytes (recovery, templates, a new import) would have come out trusted by omission. `DocState::new` now takes the `Trust`, and `Trust` has no default. New workbooks and samples pass `Local`, `file.open` passes `FromFile`, a new window passes the trust of its workbook, and the tests that build documents from bytes pass `FromFile`. * Engine: the name commands refuse only names that hold GridCraft's data The guard refused any name of the stash's shape, so a visible name a person had defined, such as _gc_rate_0, could not be edited or deleted from Name Manager, which cannot pass force. It now refuses a name only when the workbook holds a hidden workbook-scope item under it; a visible name of the shape is the person's own, as the stash itself treats it. The test covers such a name. The query comment says what the copy skip relies on: a query runs nothing that changes the history. * UI: the toolbar's Undo and Redo follow the commands' own availability The ribbon enabled them from the history lengths alone, so while an undo group was open on the document (an action spanning several calls) a click ran edit.undo and got its refusal. The buttons now ask cmd::can_undo and cmd::can_redo, the undo list is empty while a group is open, and the two buttons carry a name and their enabled state for assistive technology. A UI test opens a group, clicks both and checks the history and their state. * Engine: the name commands use the stash's own rule for what holds its data The guard asked for a hidden name, but the stash reads a legacy _sc_ name whether it is hidden or not, so a visible one could be removed or replaced without force. stash::holds_item is the one rule (item_of), shared by the guard; the test covers a visible legacy name. * Docs: the panic guard's note says what happens to a group the command ended It keeps a step from the group's start to the restored workbook, so Undo still returns to the state before the group; the note said the history came back exactly. --- crates/engine/src/cmd/edit.rs | 12 +- crates/engine/src/cmd/extra.rs | 18 +- crates/engine/src/cmd/file.rs | 5 +- crates/engine/src/cmd/formulas.rs | 97 +- crates/engine/src/cmd/inspect.rs | 13 +- crates/engine/src/cmd/mod.rs | 25 + crates/engine/src/cmd/sheet.rs | 8 +- crates/engine/src/cmd/stash.rs | 606 ++++++++++ crates/engine/src/lib.rs | 448 ++++++- crates/engine/src/record.rs | 66 ++ crates/engine/src/sample.rs | 2 +- crates/engine/src/tests.rs | 6 + crates/engine/src/tests/seams.rs | 1052 +++++++++++++++++ crates/engine/tests/it/dynamicmetadata.rs | 2 +- .../engine/tests/it/long_operator_chains.rs | 2 +- crates/engine/tests/it/stored_list_shape.rs | 3 +- crates/ui-egui/src/ribbon.rs | 22 +- crates/ui-egui/tests/it/interaction.rs | 2 +- crates/ui-egui/tests/it/main.rs | 1 + crates/ui-egui/tests/it/scroll_position.rs | 2 +- .../ui-egui/tests/it/undo_while_grouping.rs | 57 + docs/architecture.md | 51 +- 22 files changed, 2461 insertions(+), 39 deletions(-) create mode 100644 crates/engine/src/cmd/stash.rs create mode 100644 crates/engine/src/record.rs create mode 100644 crates/engine/src/tests/seams.rs create mode 100644 crates/ui-egui/tests/it/undo_while_grouping.rs diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 352797eab2..edb0e2d7cd 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -741,7 +741,7 @@ fn copy_impl(s: &mut Session, p: &Json, cut: bool) -> Result { // The HTML rendering is for the host clipboard (UI); it can reach 4 MB, so agents and // scripts get it only when they ask for it. let html = if p.get("html").and_then(Json::as_bool) == Some(true) { crate::io::range_html(&wb, sheet, range, &text) } else { None }; - let clip = Clipboard { wb, sheet, range, cut, doc_uid: d.uid, text: text.clone(), areas, selected }; + let clip = Clipboard { wb, sheet, range, cut, doc_uid: d.uid, text: text.clone(), areas, selected, trust: d.trust }; s.clipboard = Some(clip); let mut result = json!({"range": range.a1(), "text": text}); if let Some(html) = html { @@ -1264,6 +1264,16 @@ fn paste_special(s: &mut Session, p: &Json) -> Result { *cx.sel = crate::Selection { active: at, anchor: at, ranges: vec![RangeRef::new(at, end)] }; Ok(()) })?; + // Formulas and validation from an untrusted file leave the destination untrusted; plain values, + // formats (cell styles and merged areas, no conditional formats), column widths and notes + // carry nothing active. A linked paste writes a formula into every cell whatever `what` says, + // except for column widths and notes, which write no cells at all. + let passive = + matches!(what, What::ColumnWidths | What::Comments) || (!link && matches!(what, What::Formats | What::Values | What::ValuesAndNumberFormats)); + if !same_doc && !passive { + let d = s.doc_mut()?; + d.trust = d.trust.receive(clip.trust); + } if cut_move { s.clipboard = None; } diff --git a/crates/engine/src/cmd/extra.rs b/crates/engine/src/cmd/extra.rs index f248c59f2d..009d77f2a5 100644 --- a/crates/engine/src/cmd/extra.rs +++ b/crates/engine/src/cmd/extra.rs @@ -212,6 +212,15 @@ pub fn specs() -> Vec { trust_scripts ), cmd!("automate.deleteScript", "Delete Script", [], None, "{name}", has_doc, delete_script), + cmd!( + noundo "automate.stopAction", + "Stop Action", + [], + None, + "{} → {stopped: bool}: stops an action left running on the active workbook (an open undo group, such as a debugger's) and puts back the workbook and its undo history as they were when it began", + has_doc, + stop_action + ), // Cell controls, themes, borders, styles cmd!( "insert.checkbox", @@ -800,7 +809,7 @@ fn split(s: &mut Session, _: &Json) -> Result { fn new_window(s: &mut Session, _: &Json) -> Result { let d = s.doc()?.clone(); - let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title())); + let mut copy = crate::DocState::new((*d.wb).clone(), d.path.clone(), format!("{}:2", d.display_title()), d.trust); copy.selection = d.selection.clone(); copy.scripts_trusted = d.scripts_trusted; let i = s.add_document(copy); @@ -1087,6 +1096,13 @@ fn run_script(s: &mut Session, p: &Json) -> Result { Ok(json!({"ran": n})) } +/// Automate › Stop Action: rolls back the undo groups open on the active workbook, so Undo and +/// Redo work there again. +fn stop_action(s: &mut Session, _: &Json) -> Result { + let uid = s.doc()?.uid; + Ok(json!({"stopped": s.abort_groups(uid)})) +} + fn delete_script(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("automate.deleteScript", "missing `name`"))?.to_string(); edit(s, |cx| { diff --git a/crates/engine/src/cmd/file.rs b/crates/engine/src/cmd/file.rs index a61c6298f8..b3e7309e2d 100644 --- a/crates/engine/src/cmd/file.rs +++ b/crates/engine/src/cmd/file.rs @@ -34,7 +34,7 @@ fn new_workbook(s: &mut Session, p: &Json) -> Result { Some(name) => { let wb = crate::sample::build(name).ok_or_else(|| bad("file.new", format!("unknown sample `{name}`")))?; let title = crate::sample::title(name); - s.add_document(DocState::new(wb, None, title)) + s.add_document(DocState::new(wb, None, title, crate::Trust::Local)) } None => s.new_workbook(), }; @@ -83,7 +83,8 @@ fn open(s: &mut Session, p: &Json) -> Result { { s.close_document(0); } - let mut doc = DocState::new(wb, path, title); + // Someone else may have made the file: its active content asks before it runs. + let mut doc = DocState::new(wb, path, title, crate::Trust::FromFile); if crate::io::FileKind::from_path(&name).is_none() && gridcraft_xlsx::sniff(&bytes) == gridcraft_xlsx::Format::Xlsx { doc.content_kind = Some(crate::io::FileKind::Xlsx); } diff --git a/crates/engine/src/cmd/formulas.rs b/crates/engine/src/cmd/formulas.rs index 5334784fae..7718279b28 100644 --- a/crates/engine/src/cmd/formulas.rs +++ b/crates/engine/src/cmd/formulas.rs @@ -1,7 +1,7 @@ //! Formulas tab: AutoSum, Insert Function, names, auditing, calculation. use gridcraft_core::{CellError, CellRef, RangeRef, Value}; -use gridcraft_model::{CalcMode, DefinedName, Formula}; +use gridcraft_model::{CalcMode, DefinedName, Formula, Workbook}; use serde_json::{Value as Json, json}; use super::*; @@ -32,18 +32,26 @@ pub fn specs() -> Vec { "Define Name…", ["Formulas", "Defined Names"], None, - "{name, refersTo?: \"=Sheet1!$A$1:$A$10\" (default: the selection), scope?: \"Workbook\"|sheet name, comment?, oldName?, oldScope?} (oldName: edit that name (Name Manager › Edit); a new name also renames it in formulas)", + "{name, refersTo?: \"=Sheet1!$A$1:$A$10\" (default: the selection), scope?: \"Workbook\"|sheet name, comment?, oldName?, oldScope?, force?: bool} (oldName: edit that name (Name Manager › Edit); a new name also renames it in formulas; a hidden name GridCraft keeps its own data in, `_gc__` or `_sc_`, is refused unless force is true)", has_doc, define_name ), - cmd!("formulas.deleteName", "Delete Name", [], None, "{name, scope?: \"Workbook\"|sheet name (default: every scope)}", has_doc, delete_name), + cmd!( + "formulas.deleteName", + "Delete Name", + [], + None, + "{name, scope?: \"Workbook\"|sheet name (default: every scope), force?: bool} (a hidden name GridCraft keeps its own data in, `_gc__` or `_sc_`, is refused unless force is true)", + has_doc, + delete_name + ), cmd!(query "formulas.nameManager", "Name Manager", ["Formulas", "Defined Names"], Some("Cmd+F3"), "{} → names with values", has_doc, name_manager), cmd!( "formulas.createFromSelection", "Create from Selection", ["Formulas", "Defined Names"], Some("Cmd+Shift+F3"), - "{range?, top?: true, left?: false, bottom?: false, right?: false, replace?: false (existing names are kept and listed in `skipped`)}", + "{range?, top?: true, left?: false, bottom?: false, right?: false, replace?: false (existing names are kept and listed in `skipped`), force?: false (labels of the shape GridCraft keeps its own data in, `_gc__` and `_sc_`, are listed in `skipped` unless force is true)}", has_doc, create_from_selection ), @@ -268,6 +276,9 @@ fn define_name(s: &mut Session, p: &Json) -> Result { if !valid_name(&name) { return Err(EngineError::Other("The name that you entered is not valid.".into())); } + for n in [Some(name.as_str()), str_param(p, "oldName")].into_iter().flatten() { + refuse_stash_name(&s.doc()?.wb, p, n)?; + } let d = s.doc()?; let scope = match str_param(p, "scope") { None | Some("Workbook") => None, @@ -374,8 +385,29 @@ fn rename_name_in_formulas(wb: &mut gridcraft_model::Workbook, old: &str, old_sc } } +/// Refuses a name of the shape GridCraft keeps its own workbook data in ([`super::stash`]): +/// defining, renaming or deleting one would replace or remove part of an item (a stored macro, +/// script or view) and leave the rest behind. `force: true` lets the call go ahead. +/// Whether `name` is one the workbook holds GridCraft's own data in: a hidden workbook-scope +/// name of an item's shape. A visible name of that shape is the person's own (see `stash`), and +/// the name commands may change it. +fn holds_gridcraft_data(wb: &Workbook, name: &str) -> bool { + // The stash's own rule, so the two never disagree: a legacy `_sc_` item is read visible or not. + stash::holds_item(wb, name) +} + +fn refuse_stash_name(wb: &Workbook, p: &Json, name: &str) -> Result<()> { + if holds_gridcraft_data(wb, name) && !bool_param(p, "force").unwrap_or(false) { + return Err(EngineError::Other(format!( + "'{name}' is a name GridCraft keeps its own data in (stored macros, scripts or views). Pass `force: true` to change it anyway." + ))); + } + Ok(()) +} + fn delete_name(s: &mut Session, p: &Json) -> Result { let name = str_param(p, "name").ok_or_else(|| bad("formulas.deleteName", "missing `name`"))?.to_string(); + refuse_stash_name(&s.doc()?.wb, p, &name)?; let scope = match str_param(p, "scope") { None => None, Some("Workbook") => Some(None), @@ -461,7 +493,11 @@ fn create_from_selection(s: &mut Session, p: &Json) -> Result { // Excel asks before replacing an existing name; here it's kept unless `replace` is set, and // reported. let exists = |n: &str| d.wb.names.iter().any(|x| x.name.eq_ignore_ascii_case(n) && x.scope.is_none()); - let (made, skipped): (Vec<_>, Vec<_>) = made.into_iter().partition(|(n, _)| replace || !exists(n)); + // A label that names one of GridCraft's own hidden data names would replace part of an item + // (see `refuse_stash_name`). + let force = bool_param(p, "force").unwrap_or(false); + let ours = |n: &str| !force && holds_gridcraft_data(&d.wb, n); + let (made, skipped): (Vec<_>, Vec<_>) = made.into_iter().partition(|(n, _)| !ours(n) && (replace || !exists(n))); let skipped: Vec = skipped.into_iter().map(|(n, _)| n).collect(); let n = made.len(); edit(s, |cx| { @@ -742,6 +778,57 @@ mod tests { assert_eq!(r["refersTo"], "=Sheet1!$A$1"); } + #[test] + fn the_name_commands_leave_gridcraft_data_alone() { + // Defining, renaming or deleting a name of a stored item's shape would replace or remove + // one of its chunks and leave the rest behind: refused unless `force` is true. + let mut s = Session::new(); + s.new_workbook(); + let wb = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb); + let text = "m".repeat(9_000); + stash::set(wb, "macro_1", &text).unwrap(); + wb.names.push(DefinedName { name: "_sc_watch".into(), scope: None, formula: "\"[]\"".into(), comment: String::new(), hidden: true }); + // A legacy item is read whether or not it is hidden, so a visible one is data too. + wb.names.push(DefinedName { name: "_sc_old".into(), scope: None, formula: "\"[]\"".into(), comment: String::new(), hidden: false }); + let names = |s: &Session| s.doc().unwrap().wb.names.clone(); + let before = names(&s); + assert!(before.len() >= 3, "the item takes two chunks"); + for params in [ + json!({"name": "_gc_macro_1_0", "refersTo": "=1"}), + json!({"name": "_SC_Watch", "refersTo": "=1"}), + json!({"name": "Mine", "refersTo": "=1", "oldName": "_gc_macro_1_1"}), + json!({"name": "_gc_macro_1_0", "refersTo": "=1", "force": false}), + json!({"name": "Mine2", "refersTo": "=1", "oldName": "_SC_old"}), + ] { + let err = s.execute("formulas.defineName", params.clone()).unwrap_err().to_string(); + assert!(err.contains("force"), "{params}: {err}"); + } + for name in ["_gc_macro_1_1", "_sc_watch", "_sc_old"] { + let err = s.execute("formulas.deleteName", json!({"name": name})).unwrap_err().to_string(); + assert!(err.contains("force"), "{name}: {err}"); + } + s.execute("range.setValues", json!({"range": "A1:B2", "values": [["_gc_macro_1_0", "Rate"], [1, 2]]})).unwrap(); + let r = s.execute("formulas.createFromSelection", json!({"range": "A1:B2", "top": true, "replace": true})).unwrap(); + assert_eq!((r["created"].clone(), r["skipped"].clone()), (json!(1), json!(["_gc_macro_1_0"]))); + let after = names(&s); + assert_eq!(after.iter().filter(|n| n.name != "Rate").cloned().collect::>(), before); + assert_eq!(stash::get(&s.doc().unwrap().wb, "macro_1").as_deref(), Some(text.as_str())); + // A visible name of the shape is the person's own (the stash never reads it): the name + // commands change and remove it without `force`, as Name Manager cannot pass one. + let own = DefinedName { name: "_gc_rate_0".into(), scope: None, formula: "=1".into(), comment: String::new(), hidden: false }; + std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).names.push(own.clone()); + s.execute("formulas.defineName", json!({"name": "Rate0", "refersTo": "=2", "oldName": "_gc_rate_0"})).unwrap(); + s.execute("formulas.deleteName", json!({"name": "Rate0"})).unwrap(); + std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).names.push(own); + s.execute("formulas.deleteName", json!({"name": "_gc_rate_0"})).unwrap(); + assert_eq!(stash::get(&s.doc().unwrap().wb, "macro_1").as_deref(), Some(text.as_str()), "the hidden item is untouched"); + // Ordinary names and forced calls go ahead. + s.execute("formulas.deleteName", json!({"name": "Rate"})).unwrap(); + s.execute("formulas.defineName", json!({"name": "_gc_macro_1_1", "refersTo": "=1", "force": true})).unwrap(); + s.execute("formulas.deleteName", json!({"name": "_gc_macro_1_1", "force": true})).unwrap(); + assert!(!names(&s).iter().any(|n| n.name == "_gc_macro_1_1")); + } + #[test] fn arabic_defined_names_count_characters_not_bytes() { let mut s = Session::new(); diff --git a/crates/engine/src/cmd/inspect.rs b/crates/engine/src/cmd/inspect.rs index f373de4897..f5b478ccf7 100644 --- a/crates/engine/src/cmd/inspect.rs +++ b/crates/engine/src/cmd/inspect.rs @@ -7,11 +7,11 @@ use super::*; pub fn specs() -> Vec { vec![ - cmd!(query "document.inspect", "Inspect Workbook", [], None, "{} → sheets, selection, names, tables, charts, dirty, history", has_doc, inspect), + cmd!(query "document.inspect", "Inspect Workbook", [], None, "{} → sheets, selection, names, tables, charts, dirty, trust (local|fromFile), history", has_doc, inspect), cmd!(query "sheet.read", "Read Range", [], None, "{range?: \"A1:D10\" (default: used range), sheet?, formulas?: bool, formatted?: bool} → rows of values", has_doc, read), cmd!(query "cell.get", "Get Cell", [], None, "{cell?, sheet?} → value, formula, display text, style, comment, link", has_doc, cell_get), cmd!(query "selection.stats", "Selection Statistics", [], None, "{} → count, sum, average, min, max", has_doc, sel_stats), - cmd!(query "history.list", "Undo History", [], None, "{} → undo/redo labels", has_doc, history), + cmd!(query "history.list", "Undo History", [], None, "{} → undo/redo labels, grouping (an undo group is open)", has_doc, history), cmd!(query "app.commands", "List Commands", [], None, "{search?} → ids, labels, params", always, list_commands), ] } @@ -63,6 +63,7 @@ fn inspect(s: &mut Session, _: &Json) -> Result { "title": d.display_title(), "path": d.path, "dirty": d.is_dirty(), + "trust": d.trust, "activeSheet": wb.active_sheet, "sheets": sheets, "selection": d.selection.a1(), @@ -148,9 +149,11 @@ fn sel_stats(s: &mut Session, _: &Json) -> Result { fn history(s: &mut Session, _: &Json) -> Result { let d = s.doc()?; - Ok( - json!({"undo": d.undo.iter().rev().map(|e| e.label.clone()).collect::>(), "redo": d.redo.iter().rev().map(|e| e.label.clone()).collect::>()}), - ) + Ok(json!({ + "undo": d.undo.iter().rev().map(|e| e.label.clone()).collect::>(), + "redo": d.redo.iter().rev().map(|e| e.label.clone()).collect::>(), + "grouping": s.in_group(d.uid), + })) } fn list_commands(s: &mut Session, p: &Json) -> Result { diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 1f15ddfb8b..8a0bb1052c 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -19,6 +19,7 @@ pub mod sheet; pub mod slicer; pub mod solver; pub mod spelling; +pub mod stash; pub mod view; pub mod whatif; @@ -96,12 +97,19 @@ pub fn has_doc(s: &Session) -> std::result::Result<(), String> { } pub fn can_undo(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; + not_grouping(s)?; if s.active().is_some_and(|d| !d.undo.is_empty()) { Ok(()) } else { Err("nothing to undo".into()) } } pub fn can_redo(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; + not_grouping(s)?; if s.active().is_some_and(|d| !d.redo.is_empty()) { Ok(()) } else { Err("nothing to redo".into()) } } +/// Undo and Redo wait while an undo group is open on the active document (an action that spans +/// several calls is still running). +fn not_grouping(s: &Session) -> std::result::Result<(), String> { + if s.active().is_some_and(|d| s.in_group(d.uid)) { Err("finish or stop the running action first (automate.stopAction)".into()) } else { Ok(()) } +} pub fn has_clipboard(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; if s.clipboard.is_some() { Ok(()) } else { Err("the clipboard is empty".into()) } @@ -179,6 +187,23 @@ pub fn register_test_command(id: &'static str, run: Run) { test_commands().push(spec); } +/// Test-only: an undoable command, to exercise how `execute` records its step. +#[cfg(test)] +pub(crate) fn register_undoable_test_command(id: &'static str, run: Run) { + let spec: &'static CommandSpec = Box::leak(Box::new(CommandSpec { + id, + label: id, + menu: &[], + shortcut: None, + params: "", + enabled: |_| Ok(()), + run, + journal: false, + undoable: true, + })); + test_commands().push(spec); +} + fn test_commands() -> std::sync::MutexGuard<'static, Vec<&'static CommandSpec>> { #[allow(clippy::unwrap_used)] // a poisoned test registry has no meaningful recovery match TEST_COMMANDS.lock() { diff --git a/crates/engine/src/cmd/sheet.rs b/crates/engine/src/cmd/sheet.rs index 555c2fb796..a453f250b9 100644 --- a/crates/engine/src/cmd/sheet.rs +++ b/crates/engine/src/cmd/sheet.rs @@ -586,11 +586,15 @@ fn insert_copied_cells(s: &mut Session, p: &Json) -> Result { } let revision = d.revision; let before = (d.wb.clone(), d.selection.clone()); + // Inside an undo group the nested steps add no undo entries of their own. + let nested_entries = !s.in_group(d.uid); let result = insert_copied_steps(s, &clip, sheet, at, shift, cut); let d = s.doc_mut()?; // The nested steps collapse into this command's single undo step. - for _ in 0..d.revision.saturating_sub(revision) { - d.undo.pop(); + if nested_entries { + for _ in 0..d.revision.saturating_sub(revision) { + d.undo.pop(); + } } match result { Ok((final_range, out)) => { diff --git a/crates/engine/src/cmd/stash.rs b/crates/engine/src/cmd/stash.rs new file mode 100644 index 0000000000..c0e8302ca5 --- /dev/null +++ b/crates/engine/src/cmd/stash.rs @@ -0,0 +1,606 @@ +//! GridCraft data kept in hidden workbook-scope defined names, so that it travels inside the xlsx +//! file. +//! +//! An item `key` is stored as names `_gc__0`, `_gc__1`, …, each a formula of string +//! literals joined with `&`: `"…"&"…"`. Excel limits a string literal to 255 characters and a +//! formula to 8,192, so a long value is split over as many names as it needs and the file stays +//! one that Excel accepts. Older items (custom views, scripts, the watch list and scenarios) keep +//! one name each, `_sc_`; reading accepts that form as well, so those items can move to this +//! format later, and a write of the same key replaces it. +//! +//! XML can't carry every character (controls other than tab, line feed and carriage return, and +//! U+FFFE and U+FFFF), and the xlsx writer drops those. In `_gc_` items each of them, and the +//! marker itself, is written as [`ESCAPE`] followed by its code as four upper-case hex digits +//! (`U+0001` becomes `\u{FDD0}0001`), and reading turns them back. The marker is a Unicode +//! noncharacter, which XML carries but real text does not use, so the escape costs nothing on +//! ordinary text. Legacy `_sc_` items are read as they are. + +use std::collections::BTreeMap; +use std::fmt::Write as _; + +use gridcraft_model::{DefinedName, Workbook}; +use serde_json::Value as Json; + +use crate::{EngineError, Result}; + +const PREFIX: &str = "_gc_"; +const LEGACY_PREFIX: &str = "_sc_"; +/// Characters (UTF-16 units, as Excel counts them) in one string literal. +const LITERAL_UNITS: usize = 255; +/// Characters in one name's formula. +const FORMULA_UNITS: usize = 8_192; +/// The longest key: `_gc__` stays well inside Excel's 255-character names. +const MAX_KEY: usize = 200; +/// The longest item: 4,194,304 characters. +pub const MAX_ITEM_CHARS: usize = 4 * 1024 * 1024; +/// The most bytes the stored (escaped) form of an item can take: an escaped one-byte character +/// takes seven (the three-byte marker and four digits). +const MAX_STORED_BYTES: usize = MAX_ITEM_CHARS * 7; +/// Starts an escaped character in a `_gc_` item: U+FDD0, a noncharacter. +pub const ESCAPE: char = '\u{FDD0}'; +/// The most items [`items`] and [`keys`] return: the first this many keys in sorted order. A +/// file can carry any number of hidden names; the rest are not listed. +pub const MAX_ITEMS: usize = 10_000; + +/// The names that hold one item: its `_gc_` chunks by number (the first of each number) and its +/// legacy `_sc_` name. +#[derive(Default)] +struct Parts<'a> { + chunks: BTreeMap, + legacy: Option<&'a str>, +} + +impl<'a> Parts<'a> { + fn add(&mut self, chunk: Option, formula: &'a str) { + match chunk { + Some(i) => { + self.chunks.entry(i).or_insert(formula); + } + None => self.legacy = self.legacy.or(Some(formula)), + } + } + + /// Whether the names make an item: a later chunk on its own does not. + fn is_item(&self) -> bool { + self.chunks.contains_key(&0) || self.legacy.is_some() + } +} + +#[cfg(test)] +thread_local! { + /// Names [`group`] has looked at on this thread, so a test can bound the work of a listing. + static NAME_VISITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The workbook's items whose lower-cased key starts with `prefix` (lower-cased), with the names +/// that hold them, in one pass over the names. +fn group<'a>(wb: &'a Workbook, prefix: &str) -> BTreeMap> { + let prefix = prefix.to_ascii_lowercase(); + let mut items: BTreeMap> = BTreeMap::new(); + for n in wb.names.iter() { + #[cfg(test)] + NAME_VISITS.with(|v| v.set(v.get().saturating_add(1))); + let Some((k, chunk)) = item_of(n) else { continue }; + let k = k.to_ascii_lowercase(); + if k.starts_with(&prefix) { + items.entry(k).or_default().add(chunk, &n.formula); + } + } + items +} + +/// The item text stored under `key`, or `None` when there is none (or it is over the size limit). +pub fn get(wb: &Workbook, key: &str) -> Option { + let mut parts = Parts::default(); + for n in wb.names.iter() { + if let Some((k, chunk)) = item_of(n) + && k.eq_ignore_ascii_case(key) + { + parts.add(chunk, &n.formula); + } + } + assemble(&parts) +} + +/// The items whose key starts with `prefix` (`"macro_"`), by lower-cased key, read in one pass +/// over the names: the first [`MAX_ITEMS`] of [`keys`], without those over the size limit. +pub fn items(wb: &Workbook, prefix: &str) -> BTreeMap { + group(wb, prefix) + .into_iter() + .filter(|(_, parts)| parts.is_item()) + .take(MAX_ITEMS) + .filter_map(|(k, parts)| assemble(&parts).map(|text| (k, text))) + .collect() +} + +/// The text of the item held by `parts`, as [`get`] reads it. +fn assemble(parts: &Parts) -> Option { + let chunks = &parts.chunks; + let mut out = String::new(); + if !chunks.contains_key(&0) { + out = string_literals(parts.legacy?); + } else { + // Chunks run 0, 1, 2, …; anything after a gap isn't part of the item. + for (want, (i, f)) in chunks.iter().enumerate() { + if *i != want as u64 { + break; + } + out.push_str(&string_literals(f)); + if out.len() > MAX_STORED_BYTES { + return None; + } + } + out = unescape(&out); + } + (out.chars().count() <= MAX_ITEM_CHARS).then_some(out) +} + +/// Whether `c` is stored escaped: XML can't carry it, or it is the marker. +fn needs_escape(c: char) -> bool { + c == ESCAPE || !matches!(c, '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}') +} + +/// Turns [`ESCAPE`] and four hex digits back into the character. A marker not followed by four +/// hex digits naming a character (a hand-made name) stays as it is. +fn unescape(s: &str) -> String { + if !s.contains(ESCAPE) { + return s.to_string(); + } + let mut out = String::with_capacity(s.len()); + let mut rest = s; + while let Some(at) = rest.find(ESCAPE) { + out.push_str(rest.get(..at).unwrap_or("")); + let after = rest.get(at + ESCAPE.len_utf8()..).unwrap_or(""); + let code = after.get(..4).filter(|h| h.bytes().all(|b| b.is_ascii_hexdigit())).and_then(|h| u32::from_str_radix(h, 16).ok()); + match code.and_then(char::from_u32) { + Some(c) => { + out.push(c); + rest = after.get(4..).unwrap_or(""); + } + None => { + out.push(ESCAPE); + rest = after; + } + } + } + out.push_str(rest); + out +} + +/// Stores `text` under `key`, replacing what was there (in either format). Empty text removes it. +pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> { + check_key(key)?; + if text.chars().count() > MAX_ITEM_CHARS { + return Err(EngineError::Other(format!("The data is too large to keep in the workbook ({} MB at most).", MAX_ITEM_CHARS / (1024 * 1024)))); + } + let formulas = chunk_formulas(text); + // A visible name of the item's shape is someone's own (see `item_of`); a hidden name beside + // it with the same name would be the one an xlsx file drops. Refuse before changing anything, + // in one pass over the names (a file can carry many thousands). + let chunks = formulas.len() as u64; + let taken = |n: &&DefinedName| { + n.scope.is_none() && !n.hidden && parse_name(&n.name).is_some_and(|(k, i)| k.eq_ignore_ascii_case(key) && i.is_some_and(|i| i < chunks)) + }; + if let Some(n) = wb.names.iter().find(taken) { + return Err(EngineError::Other(format!( + "The workbook has a defined name '{}', which GridCraft needs for its own data. Rename it first.", + n.name + ))); + } + remove(wb, key); + for (i, formula) in formulas.into_iter().enumerate() { + wb.names.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true }); + } + Ok(()) +} + +/// Removes the item stored under `key`. +pub fn remove(wb: &mut Workbook, key: &str) { + wb.names.retain(|n| !item_of(n).is_some_and(|(k, _)| k.eq_ignore_ascii_case(key))); +} + +/// The item under `key` as JSON (`None` when missing or not JSON). +pub fn get_json(wb: &Workbook, key: &str) -> Option { + serde_json::from_str(&get(wb, key)?).ok() +} + +pub fn set_json(wb: &mut Workbook, key: &str, v: &Json) -> Result<()> { + set(wb, key, &v.to_string()) +} + +/// The keys of the items stored in the workbook that start with `prefix` (`"macro_"`), sorted +/// and lower-cased: the first [`MAX_ITEMS`] of them. To read them all, [`items`] takes one pass +/// where a [`get`] per key would take one each. +pub fn keys(wb: &Workbook, prefix: &str) -> Vec { + group(wb, prefix).into_iter().filter(|(_, parts)| parts.is_item()).map(|(k, _)| k).take(MAX_ITEMS).collect() +} + +/// Whether the workbook holds an item under `name` (compared without case): a hidden workbook-scope +/// `_gc__` name, or a workbook-scope legacy `_sc_` name, which is read whether or not +/// it is hidden. A visible `_gc_` name is the person's own. +pub fn holds_item(wb: &Workbook, name: &str) -> bool { + wb.names.iter().any(|n| n.name.eq_ignore_ascii_case(name) && item_of(n).is_some()) +} + +/// The item a hidden name belongs to, lower-cased: `_gc__` and `_sc_` give ``. +pub fn item_key(name: &str) -> Option { + parse_name(name).map(|(k, _)| k.to_ascii_lowercase()) +} + +/// The item a name holds a part of: a hidden workbook-scope `_gc__` name, or a legacy +/// `_sc_` one. A visible name of the `_gc_` shape is someone's own name, not an item: it is +/// neither read, nor listed, nor removed on write. +fn item_of(n: &DefinedName) -> Option<(&str, Option)> { + if n.scope.is_some() { + return None; + } + parse_name(&n.name).filter(|(_, chunk)| chunk.is_none() || n.hidden) +} + +/// `_gc__` → (`key`, `Some(k)`); `_sc_` → (`key`, `None`). +fn parse_name(name: &str) -> Option<(&str, Option)> { + if let Some(rest) = strip_prefix_ci(name, PREFIX) { + let (key, k) = rest.rsplit_once('_')?; + // Only the chunk number as `set` writes it: `_gc_watch_00` is someone else's name, not + // chunk 0 under another spelling, so it is neither read nor in the way of `_gc_watch_0`. + let canonical = !k.is_empty() && k.len() <= 9 && k.bytes().all(|b| b.is_ascii_digit()) && (k == "0" || !k.starts_with('0')); + if key.is_empty() || !canonical { + return None; + } + return k.parse().ok().map(|i| (key, Some(i))); + } + strip_prefix_ci(name, LEGACY_PREFIX).filter(|k| !k.is_empty()).map(|k| (k, None)) +} + +fn strip_prefix_ci<'a>(s: &'a str, prefix: &str) -> Option<&'a str> { + let head = s.get(..prefix.len())?; + head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..)).flatten() +} + +fn check_key(key: &str) -> Result<()> { + let ok = !key.is_empty() && key.len() <= MAX_KEY && key.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'.'); + if ok { Ok(()) } else { Err(EngineError::Other(format!("`{key}` can't name workbook data: use letters, digits, `_` and `.`"))) } +} + +/// `text` as name formulas: string literals of at most 255 characters joined with `&`, each formula +/// at most 8,192 characters, with the characters XML can't carry escaped (see [`ESCAPE`]). +/// Concatenating [`string_literals`] of each and turning the escapes back gives `text` back. +pub fn chunk_formulas(text: &str) -> Vec { + let mut formulas = Vec::new(); + let (mut formula, mut units) = (String::new(), 0usize); + let (mut literal, mut lit_units) = (String::from("\""), 0usize); + let mut flush_literal = |literal: &mut String, lit_units: &mut usize, formula: &mut String, units: &mut usize| { + literal.push('"'); + let cost = literal.encode_utf16().count() + usize::from(!formula.is_empty()); + if *units + cost > FORMULA_UNITS && !formula.is_empty() { + formulas.push(std::mem::take(formula)); + *units = 0; + } + if !formula.is_empty() { + formula.push('&'); + *units += 1; + } + *units += literal.encode_utf16().count(); + formula.push_str(literal); + *literal = String::from("\""); + *lit_units = 0; + }; + for ch in text.chars() { + // An escaped character is the marker and four digits, kept in one literal. + let escaped = needs_escape(ch); + let cost = if escaped { ESCAPE.len_utf16() + 4 } else { ch.len_utf16() }; + if lit_units + cost > LITERAL_UNITS { + flush_literal(&mut literal, &mut lit_units, &mut formula, &mut units); + } + lit_units += cost; + if escaped { + literal.push(ESCAPE); + let _ = write!(literal, "{:04X}", u32::from(ch)); + } else { + if ch == '"' { + literal.push('"'); + } + literal.push(ch); + } + } + if lit_units > 0 { + flush_literal(&mut literal, &mut lit_units, &mut formula, &mut units); + } + if !formula.is_empty() { + formulas.push(formula); + } + formulas +} + +/// Concatenates the string literals of a `"a"&"b""c"` formula (anything outside them is ignored). +pub fn string_literals(f: &str) -> String { + let mut out = String::new(); + let mut it = f.chars().peekable(); + let mut in_str = false; + while let Some(c) = it.next() { + if !in_str { + if c == '"' { + in_str = true; + } + continue; + } + if c == '"' { + if it.peek() == Some(&'"') { + it.next(); + out.push('"'); + } else { + in_str = false; + } + } else { + out.push(c); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn name(n: &str, formula: &str) -> DefinedName { + DefinedName { name: n.into(), scope: None, formula: formula.into(), comment: String::new(), hidden: true } + } + + fn excel_limits_hold(wb: &Workbook) { + for n in &wb.names { + assert!(n.formula.encode_utf16().count() <= FORMULA_UNITS, "{} is {} long", n.name, n.formula.len()); + // Every literal (quotes undoubled) is at most 255 characters. + for lit in n.formula.split("\"&\"") { + let inner = lit.trim_start_matches('"').trim_end_matches('"').replace("\"\"", "\""); + assert!(inner.encode_utf16().count() <= LITERAL_UNITS, "{}", inner.len()); + } + } + } + + #[test] + fn long_values_split_over_names_and_come_back() { + let mut wb = Workbook::new(); + // Quotes double when written; characters outside the BMP count twice. + let text: String = (0..30_000).map(|i| ['a', '"', 'é', '\u{1D11E}', '&', '\n'][i % 6]).collect(); + set(&mut wb, "big", &text).unwrap(); + let names: Vec<&str> = wb.names.iter().map(|n| n.name.as_str()).collect(); + assert!(names.len() > 5, "{names:?}"); + assert_eq!(names[0], "_gc_big_0"); + assert!(wb.names.iter().all(|n| n.hidden && n.scope.is_none())); + excel_limits_hold(&wb); + assert_eq!(get(&wb, "big").as_deref(), Some(text.as_str())); + assert_eq!(get(&wb, "BIG").as_deref(), Some(text.as_str()), "names are case-insensitive"); + // Shorter again: the extra chunks go. + set(&mut wb, "big", "short").unwrap(); + assert_eq!(wb.names.len(), 1); + assert_eq!(wb.names[0].formula, "\"short\""); + set(&mut wb, "big", "").unwrap(); + assert!(wb.names.is_empty()); + assert_eq!(get(&wb, "big"), None); + } + + #[test] + fn chunks_and_literals_respect_the_boundaries() { + for len in [0, 1, 254, 255, 256, 8_000, 8_192, 8_193, 20_000] { + let text = "q".repeat(len); + let fs = chunk_formulas(&text); + assert_eq!(fs.iter().map(|f| string_literals(f)).collect::(), text, "{len}"); + assert!(fs.iter().all(|f| f.len() <= FORMULA_UNITS), "{len}"); + } + // All quotes: each literal doubles in the formula. + let text = "\"".repeat(9_000); + let fs = chunk_formulas(&text); + assert_eq!(fs.iter().map(|f| string_literals(f)).collect::(), text); + assert!(fs.iter().all(|f| f.len() <= FORMULA_UNITS)); + } + + #[test] + fn the_old_single_name_still_reads_and_is_replaced_on_write() { + let mut wb = Workbook::new(); + wb.names.push(name("_sc_scripts", "\"{\"\"A\"\":[1]}\"")); + wb.names.push(name("_sc_scenarios", "\"[1,\"&\"2]\"")); + assert_eq!(get_json(&wb, "scripts"), Some(serde_json::json!({"A": [1]}))); + assert_eq!(get_json(&wb, "scenarios"), Some(serde_json::json!([1, 2]))); + set_json(&mut wb, "scripts", &serde_json::json!({"B": []})).unwrap(); + let names: Vec<&str> = wb.names.iter().map(|n| n.name.as_str()).collect(); + assert_eq!(names, ["_sc_scenarios", "_gc_scripts_0"]); + assert_eq!(get_json(&wb, "scripts"), Some(serde_json::json!({"B": []}))); + } + + #[test] + fn hostile_names_read_safely() { + let mut wb = Workbook::new(); + // A gap: chunk 2 without chunk 1. + wb.names.push(name("_gc_gap_0", "\"a\"")); + wb.names.push(name("_gc_gap_2", "\"c\"")); + assert_eq!(get(&wb, "gap").as_deref(), Some("a")); + // Only a later chunk: no item, not listed, and the legacy name is still read. + wb.names.push(name("_gc_orphan_2", "\"c\"")); + assert_eq!(get(&wb, "orphan"), None); + assert_eq!(keys(&wb, "orphan"), Vec::::new()); + wb.names.push(name("_sc_orphan", "\"old\"")); + assert_eq!(get(&wb, "orphan").as_deref(), Some("old")); + assert_eq!(keys(&wb, "orphan"), ["orphan"]); + // Chunks out of order, a duplicate, junk around the literals, an unterminated literal. + wb.names.push(name("_gc_mix_1", "\"b\"&A1&")); + wb.names.push(name("_gc_mix_0", "SUM(\"a\")")); + wb.names.push(name("_gc_mix_0", "\"ignored\"")); + wb.names.push(name("_gc_mix_2", "\"c")); + assert_eq!(get(&wb, "mix").as_deref(), Some("abc")); + // Names that only look like chunks. + for odd in ["_gc_", "_gc__0", "_gc_x_", "_gc_x_-1", "_gc_x_99999999999999999999", "_sc_", "_gcx_0"] { + wb.names.push(name(odd, "\"zzz\"")); + } + assert_eq!(get(&wb, "x"), None); + assert_eq!(get(&wb, ""), None); + // Not JSON, or JSON nested deeper than the parser allows. + wb.names.push(name("_gc_notjson_0", "\"{\"")); + assert_eq!(get_json(&wb, "notjson"), None); + let deep = format!("\"{}{}\"", "[".repeat(10_000), "]".repeat(10_000)); + wb.names.push(name("_gc_deep_0", &deep)); + assert_eq!(get_json(&wb, "deep"), None); + // Sheet-scoped names are someone else's. + wb.names.push(DefinedName { scope: Some(0), ..name("_gc_scoped_0", "\"s\"") }); + assert_eq!(get(&wb, "scoped"), None); + // Bad keys are refused. + for bad in ["", "has space", "quote\"", "a/b", &"k".repeat(201)] { + assert!(set(&mut wb, bad, "v").is_err(), "{bad}"); + } + assert!(set(&mut wb, "big", &"x".repeat(MAX_ITEM_CHARS + 1)).is_err()); + } + + #[test] + fn oversized_items_are_not_read() { + let mut wb = Workbook::new(); + let chunk = format!("\"{}\"", "x".repeat(250)); + let big = vec![chunk.as_str(); 32].join("&"); + for i in 0..(MAX_ITEM_CHARS / 8_000 + 2) { + wb.names.push(name(&format!("_gc_huge_{i}"), &big)); + } + assert_eq!(get(&wb, "huge"), None); + } + + #[test] + fn a_visible_name_of_the_item_shape_is_not_an_item() { + // Someone's own visible `_gc_watch_0` is not storage: it is not read or listed, and + // writing or removing the item leaves it alone. + let mut wb = Workbook::new(); + let own = DefinedName { hidden: false, ..name("_gc_watch_0", "\"mine\"") }; + wb.names.push(own.clone()); + assert_eq!(get(&wb, "watch"), None); + assert!(keys(&wb, "").is_empty()); + // Writing the item would put a hidden name of the same name beside it, and an xlsx file + // keeps only one of them: refused, with the names as they were. + let err = set(&mut wb, "watch", "theirs").unwrap_err().to_string(); + assert!(err.contains("_gc_watch_0"), "{err}"); + assert_eq!(wb.names, vec![own.clone()]); + assert_eq!(get(&wb, "watch"), None); + set(&mut wb, "other", "theirs").unwrap(); + assert_eq!(get(&wb, "other").as_deref(), Some("theirs")); + remove(&mut wb, "watch"); + remove(&mut wb, "other"); + assert_eq!(wb.names, vec![own]); + } + + #[test] + fn a_chunk_number_with_leading_zeros_is_another_name() { + // `_gc_watch_00` is not chunk 0 under another spelling: a hidden one is not read, and a + // visible one is not in the way of writing `_gc_watch_0`. + let mut wb = Workbook::new(); + wb.names.push(name("_gc_watch_00", "\"theirs\"")); + assert_eq!(get(&wb, "watch"), None); + assert!(keys(&wb, "").is_empty()); + let own = DefinedName { hidden: false, ..name("_gc_watch_00", "\"mine\"") }; + wb.names.push(own.clone()); + set(&mut wb, "watch", "ours").unwrap(); + assert_eq!(get(&wb, "watch").as_deref(), Some("ours")); + assert_eq!(wb.names.len(), 3, "both other names stay"); + remove(&mut wb, "watch"); + assert_eq!(wb.names, vec![name("_gc_watch_00", "\"theirs\""), own]); + } + + #[test] + fn keys_list_items_by_prefix() { + let mut wb = Workbook::new(); + set(&mut wb, "macro_2", "b").unwrap(); + set(&mut wb, "macro_1", &"a".repeat(9_000)).unwrap(); + set(&mut wb, "scripts", "{}").unwrap(); + assert_eq!(keys(&wb, "macro_"), ["macro_1", "macro_2"]); + assert_eq!(item_key("_gc_macro_1_3").as_deref(), Some("macro_1")); + assert_eq!(item_key("_SC_Scripts").as_deref(), Some("scripts")); + assert_eq!(item_key("Total"), None); + } + + #[test] + fn listing_many_items_takes_one_pass_and_is_capped() { + // 20,000 hidden items, as a file can carry: a `get` per key looked at every name for each + // key (400 million visits); `items` looks at each name once, and lists at most MAX_ITEMS. + let mut wb = Workbook::new(); + let total = 20_000; + for i in 0..total { + wb.names.push(name(&format!("_gc_macro_{i:05}_0"), &format!("\"m{i}\""))); + } + set(&mut wb, "macro_big", &"b".repeat(9_000)).unwrap(); + wb.names.push(name("_sc_macro_legacy", "\"old\"")); + wb.names.push(name("_gc_macro_orphan_1", "\"no chunk 0\"")); + wb.names.push(name("_gc_other_0", "\"not a macro\"")); + NAME_VISITS.with(|v| v.set(0)); + let all = items(&wb, "MACRO_"); + assert_eq!(NAME_VISITS.with(|v| v.get()), wb.names.len(), "one pass over the names"); + assert_eq!(all.len(), MAX_ITEMS); + let first: Vec<&String> = all.keys().take(2).collect(); + assert_eq!(first, ["macro_00000", "macro_00001"]); + assert_eq!(all.get("macro_09999").map(String::as_str), Some("m9999")); + assert!(!all.contains_key("macro_10000"), "past the cap"); + NAME_VISITS.with(|v| v.set(0)); + let listed = keys(&wb, "macro_"); + assert_eq!(NAME_VISITS.with(|v| v.get()), wb.names.len()); + assert_eq!(listed, all.keys().cloned().collect::>(), "keys and items agree"); + + // Under the cap every item is there, read as `get` reads it. + wb.names.retain(|n| !n.name.starts_with("_gc_macro_0") && !n.name.starts_with("_gc_macro_1")); + let few = items(&wb, "macro_"); + let want: BTreeMap = + [("macro_big", "b".repeat(9_000)), ("macro_legacy", "old".into())].into_iter().map(|(k, t)| (k.to_string(), t)).collect(); + assert_eq!(few, want); + for (k, text) in &few { + assert_eq!(get(&wb, k).as_ref(), Some(text)); + } + assert_eq!(keys(&wb, "macro_"), ["macro_big", "macro_legacy"]); + assert_eq!(items(&wb, "").len(), 3, "the other item too"); + } + + #[test] + fn items_round_trip_through_xlsx() { + let mut wb = Workbook::new(); + let text: String = (0..20_000).map(|i| ['x', '"', 'ü', '\u{1D11E}', ' '][i % 5]).collect(); + set(&mut wb, "blob", &text).unwrap(); + wb.names.push(name("_sc_watch", "\"[\"\"A1\"\"]\"")); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let (back, warnings) = crate::io::open_bytes("data.xlsx", &bytes).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(get(&back, "blob").as_deref(), Some(text.as_str())); + assert_eq!(get_json(&back, "watch"), Some(serde_json::json!(["A1"]))); + + // Characters XML can't carry, and the escape marker itself, come back too, in text and + // in JSON (which leaves U+FFFE and U+FFFF unescaped). + let mut wb = Workbook::new(); + let odd: String = (0u32..0x20).filter_map(char::from_u32).chain(['\u{FFFE}', '\u{FFFF}', ESCAPE, 'a', '\u{7F}', '\u{FDD1}']).collect(); + let text: String = odd.chars().cycle().take(odd.chars().count() * 300).collect(); + set(&mut wb, "odd", &text).unwrap(); + excel_limits_hold(&wb); + let json = serde_json::json!({"s": "x\u{FFFF}y\u{1}z\u{FDD0}0041"}); + set_json(&mut wb, "json", &json).unwrap(); + // All escapes: each literal holds whole escapes, within the limits. + let controls = "\u{1}".repeat(9_000); + set(&mut wb, "controls", &controls).unwrap(); + excel_limits_hold(&wb); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let (back, warnings) = crate::io::open_bytes("data.xlsx", &bytes).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(get(&back, "odd").as_deref(), Some(text.as_str())); + assert_eq!(get_json(&back, "json"), Some(json)); + assert_eq!(get(&back, "controls").as_deref(), Some(controls.as_str())); + } + + #[test] + fn escapes_count_against_the_limits_and_odd_markers_read_as_they_are() { + // Excel's limits hold however many characters are escaped, and the longest item of + // characters that all need escaping still reads. + for len in [1, 50, 51, 52, 1_638, 1_639, 5_000] { + let text = "\u{2}".repeat(len); + let fs = chunk_formulas(&text); + assert_eq!(unescape(&fs.iter().map(|f| string_literals(f)).collect::()), text, "{len}"); + assert!(fs.iter().all(|f| f.encode_utf16().count() <= FORMULA_UNITS), "{len}"); + } + let mut wb = Workbook::new(); + let longest = "\u{0}".repeat(MAX_ITEM_CHARS); + set(&mut wb, "longest", &longest).unwrap(); + excel_limits_hold(&wb); + assert_eq!(get(&wb, "longest").map(|t| t.len()), Some(MAX_ITEM_CHARS)); + // A marker in a hand-made name that is not an escape stays as it is. + let mut wb = Workbook::new(); + wb.names.push(name("_gc_hand_0", "\"a\u{FDD0}zz\u{FDD0}D800\u{FDD0}004\u{FDD0}0041\u{FDD0}\"")); + assert_eq!(get(&wb, "hand").as_deref(), Some("a\u{FDD0}zz\u{FDD0}D800\u{FDD0}004A\u{FDD0}")); + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 066a9e5590..b4ab60f3a9 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -19,6 +19,7 @@ pub mod outline; pub mod paper; pub mod pivot; pub mod pivot_calc; +pub mod record; pub mod sample; pub mod selection; pub mod tables; @@ -66,6 +67,51 @@ pub struct HistoryEntry { pub retained_bytes: usize, } +/// An undo group opened with [`Session::group_begin`]. Hand it back to [`Session::group_end`] +/// (keep the changes as one undo step) or [`Session::group_rollback`] (undo them all). +#[derive(Debug, PartialEq, Eq)] +#[must_use = "an undo group stays open until it is ended or rolled back"] +pub struct GroupToken { + id: u64, + doc: u64, +} + +impl GroupToken { + /// The uid of the document the group belongs to. + pub fn doc(&self) -> u64 { + self.doc + } +} + +/// An open undo group: what runs on `doc` until it ends becomes one undo step. +#[derive(Clone, Debug)] +struct OpenGroup { + id: u64, + doc: u64, + label: String, + /// Undo depth when the group began; the group's own steps are cut back to it. + start: usize, + wb: Arc, + selection: Selection, + /// The redo stack when the group began (a rollback puts it back). + redo: Vec, +} + +/// Where the last [`Session::group_end`] or [`Session::group_rollback`] left its document: the +/// workbook and selection it ended at, the undo length then, and whether it made a step. A +/// command that ended the group tells its own later changes from the group's step by it, and +/// keeps the group's name only when the group's step is exactly its own (the entry holds the +/// workbook the step started from). Dropped when the outermost command finishes, so it keeps no +/// workbook alive between commands. +#[derive(Debug)] +struct GroupStep { + doc: u64, + after: Arc, + selection: Selection, + len: usize, + stepped: bool, +} + /// The clipboard: a block of cells (with formulas and styles) or marching-ants reference. #[derive(Clone, Debug)] pub struct Clipboard { @@ -84,6 +130,34 @@ pub struct Clipboard { /// A multi-area copy: the areas as selected (for the marquee). `wb` then holds them slid /// together into one block at `range`. Empty for a single-range copy. pub areas: Vec, + /// Trust of the source document: pasting content from a file someone else made carries it. + pub trust: Trust, +} + +/// Whether a document's active content (macros) may run without asking the person first. There +/// is no default: whoever makes a document says where it came from ([`DocState::new`]), so a new +/// way of building one from bytes can't come out trusted by omission. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum Trust { + /// Made in this session: a new workbook or a sample. + Local, + /// Opened from a file, or holding content pasted from one: active content asks before it + /// runs. + FromFile, +} + +impl Trust { + /// Whether active content may run without asking. + pub fn allows_code(self) -> bool { + self == Trust::Local + } + + /// The trust of a document after it receives content from a document with trust `source`: + /// content from an untrusted file leaves the receiver untrusted. + pub fn receive(self, source: Trust) -> Trust { + if source == Trust::FromFile { Trust::FromFile } else { self } + } } /// In-cell edit mode as shown in the status bar. @@ -120,6 +194,11 @@ pub struct DocState { /// user trusts it in this session (`automate.trustScripts`); one with no scripts starts /// trusted, since any script it gains later was written here. pub scripts_trusted: bool, + /// Where the workbook's content came from: whether its active content may run without + /// asking. Unlike `scripts_trusted`, this is provenance, not consent: it is set when the + /// workbook is opened from a file, carried by content pasted from such a workbook, and never + /// changed by a trust decision. + pub trust: Trust, } static NEXT_UID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); @@ -160,17 +239,31 @@ fn push_history_with_budget( selection: Selection, byte_budget: usize, ) { + push_entry(history, label, wb, next, selection); + trim_to_budget(history, byte_budget); +} + +/// Adds a step without applying the limits: inside a command they wait for the outermost +/// command to collapse its steps (see [`Session::trim_history`]). +fn push_entry(history: &mut Vec, label: String, wb: Arc, next: &Workbook, selection: Selection) { let retained_bytes = history_retained_bytes(&wb, next); history.push(HistoryEntry { label, wb, selection, retained_bytes }); +} + +/// Drops the oldest steps past [`UNDO_LIMIT`] or `byte_budget`, always keeping the latest one +/// (even if that one edit exceeds the budget). +fn trim_to_budget(history: &mut Vec, byte_budget: usize) { let mut total = history.iter().fold(0usize, |sum, entry| sum.saturating_add(entry.retained_bytes)); - // Always preserve the latest action, even if that one edit exceeds the budget. while history.len() > 1 && (history.len() > UNDO_LIMIT || total > byte_budget) { total = total.saturating_sub(history.remove(0).retained_bytes); } } impl DocState { - pub fn new(mut wb: Workbook, path: Option, title: String) -> DocState { + /// A document for `wb`, which came from where `trust` says: [`Trust::FromFile`] for anything + /// read from bytes (a file, a download, a recovery copy), [`Trust::Local`] only for a workbook + /// made in this session. + pub fn new(mut wb: Workbook, path: Option, title: String, trust: Trust) -> DocState { let mut calc = Calc::new(); calc.recalc_all(&mut wb); let scripts_trusted = !cmd::extra::has_scripts(&wb); @@ -191,6 +284,7 @@ impl DocState { unlocked_ranges: vec![], content_kind: None, scripts_trusted, + trust, } } pub fn is_dirty(&self) -> bool { @@ -307,8 +401,13 @@ pub struct Session { pub last_find: Option, /// View › Show › Ruler is off (rulers in Page Layout view; an application option). pub hide_ruler: bool, - /// Journal position where action recording started. + /// Journal position where Automate › Record Actions started. pub recording: Option, + /// The recording buffer while a recorder runs ([`Session::start_recording`]): top-level + /// commands with the place they ran in, kept apart from the journal. + pub recorder: Option, + /// How many `execute` calls are running (0 between top-level commands). + depth: u32, /// Windows hidden with View › Hide. pub hidden_windows: Vec, /// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width. @@ -320,6 +419,17 @@ pub struct Session { /// Review › Notes › Show All Notes: every note is displayed. An application display setting /// (like Excel's comment indicator option), so it's not saved in the file. pub show_all_notes: bool, + /// Open undo groups, innermost last. + groups: Vec, + next_group: u64, + /// Counts group begins and ends, so `execute` can tell that a command opened or closed one. + group_epoch: u64, + /// Where the last group ended, so `execute` can tell a command's own changes from its group's. + group_step: Option, + /// For each running command, innermost last: its document and that document's undo length + /// when it began. Steps before that length are not touched while the command runs (see + /// [`Session::bound_tail`]). + marks: Vec<(u64, usize)>, /// Measures cell text for AutoFit Column Width. Without it (CLI, MCP) AutoFit uses /// [`cmd::format::approx_text_width`]. pub text_width: Option, @@ -359,7 +469,8 @@ impl Session { } pub fn close_document(&mut self, i: usize) { if i < self.docs.len() { - self.docs.remove(i); + let d = self.docs.remove(i); + self.groups.retain(|g| g.doc != d.uid); if self.active >= self.docs.len() { self.active = self.docs.len().saturating_sub(1); } @@ -373,7 +484,7 @@ impl Session { let name = wb.next_sheet_name(); wb.sheets.push(Arc::new(gridcraft_model::Sheet::new(name))); } - self.add_document(DocState::new(wb, None, format!("Book{n}"))) + self.add_document(DocState::new(wb, None, format!("Book{n}"), Trust::Local)) } /// Journals a command, then trims the oldest entries past either cap. A recording in progress @@ -412,7 +523,8 @@ impl Session { command_specs().iter().map(|c| c.info(self)).collect() } - /// Runs a command by id. Undoable commands record a history step when the workbook changed. + /// Runs a command by id. Undoable commands record a history step when the workbook changed, + /// unless an undo group is open on the document (the group then makes the step). /// Panics inside commands are caught and reported; the workbook is restored. pub fn execute(&mut self, id: &str, params: Json) -> Result { let spec = find_command(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; @@ -421,36 +533,352 @@ impl Session { } let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone())); let params = if params.is_null() { Json::Object(Default::default()) } else { params }; + let (first_group, group_epoch) = (self.next_group, self.group_epoch); + // Whether a group was open on the document before the command, and how long its history was. + let start = before.as_ref().and_then(|(uid, ..)| self.docs.iter().find(|d| d.uid == *uid).map(|d| (self.in_group(*uid), d.undo.len()))); + // A command that panics leaves the history as it found it: the steps commands nested in it + // made or undid, and the redo history they cleared, come back. A length is not enough, + // because a nested `edit.undo` removes entries. A query runs nothing that changes the + // history (`every_query_leaves_the_history_alone` runs each one), so it needs no + // copy; front ends run queries all the time. + let query = !spec.journal && !spec.undoable; + let history_before = before.as_ref().filter(|_| !query).and_then(|(uid, ..)| { + #[cfg(test)] + tests::HISTORY_COPIES.with(|n| n.set(n.get().saturating_add(1))); + self.docs.iter().find(|d| d.uid == *uid).map(|d| (d.undo.clone(), d.redo.clone())) + }); + // The groups open on the document before the command (usually none), for the same reason. + let groups_before: Vec<(u64, String, Arc, Selection)> = match &before { + Some((uid, ..)) => { + self.groups.iter().filter(|g| g.doc == *uid).map(|g| (g.id, g.label.clone(), g.wb.clone(), g.selection.clone())).collect() + } + None => Vec::new(), + }; + // The recorder keeps top-level calls with the place they ran in. + let place = (self.depth == 0 && self.recorder.is_some()).then(|| match self.active() { + Some(d) => (d.uid, d.wb.active_sheet, d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(), d.selection.clone()), + None => (0, 0, String::new(), Selection::default()), + }); + let doc_uid = before.as_ref().map(|(uid, ..)| *uid); + let requests_before = self.ui_requests.len(); + let marked = doc_uid.zip(start).map(|(uid, (_, len))| (uid, len)); + self.marks.extend(marked); + self.depth = self.depth.saturating_add(1); let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, ¶ms))); + self.depth = self.depth.saturating_sub(1); + if marked.is_some() { + self.marks.pop(); + } let result = match result { Ok(r) => r, Err(p) => { let msg = p.downcast_ref::<&str>().map(|s| s.to_string()).or_else(|| p.downcast_ref::().cloned()).unwrap_or_else(|| "panic".into()); - // Restore the workbook as it was. + // Restore the workbook and its history as they were, and drop the undo groups the + // command opened. + self.groups.retain(|g| g.id < first_group); + let group_open = before.as_ref().is_some_and(|(uid, ..)| self.groups.iter().any(|g| g.doc == *uid)); if let Some((uid, wb, sel)) = &before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == *uid) { d.wb = wb.clone(); d.selection = sel.clone(); + if let Some((undo, redo)) = history_before { + d.undo = undo; + d.redo = redo; + } + // A group open before the command and ended or rolled back inside it stays + // ended (its token went with the command), and keeps a step from its start to + // the restored workbook, so Undo still returns to the state before the group. + if !group_open + && let Some((_, label, wb, selection)) = groups_before.into_iter().min_by_key(|g| g.0) + && !Arc::ptr_eq(&wb, &d.wb) + { + push_entry(&mut d.undo, label, wb, &d.wb, selection); + d.redo.clear(); + } d.calc.rebuild(&d.wb); } + if self.depth == 0 { + self.trim_history(); + self.group_step = None; + } return Err(EngineError::Internal(id.to_string(), msg)); } }; + // A command that fails leaves none of its own undo groups open (a `?` between + // `group_begin` and `group_end`): each is rolled back as `group_rollback` does. Only a + // command that succeeds keeps a group it opened (a debugger's start). + if result.is_err() { + self.rollback_groups_from(first_group); + } if spec.journal && result.is_ok() { + // A command that started recording is not part of the recording; one that stopped it + // finds no recorder. A menu-style call that only opened a dialog (`file.open` with no + // file, `home.formatCells` with no style) is not recorded either: the dialog's own + // call is, so playing the recording back runs the command once, with its parameters. + let opened_dialog = self.ui_requests.get(requests_before..).is_some_and(|new| new.iter().any(|q| matches!(q, UiRequest::Dialog(..)))); + if let Some((doc, sheet, sheet_name, selection)) = place.filter(|_| !opened_dialog) + && let Some(rec) = self.recorder.as_mut() + { + rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params: cmd::journal_params(¶ms) }); + } self.push_journal(id, cmd::journal_params(¶ms)); } + // Inside an undo group the group owns the step. A command that ended a group open before + // it leaves the group its step and makes one of its own for what it changed after the end. + // A command that opened and closed its own groups makes one step of its own, which + // replaces the steps those groups made. + let epoch_changed = self.group_epoch != group_epoch; + let in_group_now = before.as_ref().is_some_and(|(uid, ..)| self.in_group(*uid)); + let was_open = start.is_some_and(|(was_open, _)| was_open); if spec.undoable && let Some((uid, wb, sel)) = before && let Some(d) = self.docs.iter_mut().find(|d| d.uid == uid) && !Arc::ptr_eq(&d.wb, &wb) { - push_history(&mut d.undo, spec.label.trim_end_matches('…').to_string(), wb, &d.wb, sel); + let label = spec.label.trim_end_matches('…').to_string(); + if in_group_now { + // A group this command opened and left open (a debugger's start) takes the + // command's start as its own, so what the command changed before opening it is + // part of the group's step and not a step of its own. + if !was_open + && let Some((_, len)) = start + && let Some(g) = self.groups.iter_mut().filter(|g| g.doc == uid && g.id >= first_group).min_by_key(|g| g.id) + { + g.start = len; + g.wb = wb; + g.selection = sel; + } + } else if epoch_changed && was_open { + // The group it ended made its step; changes after the end, whether direct or by + // nested commands, are the command's own step from where the group ended. + if let Some(step) = self.group_step.as_ref().filter(|g| g.doc == uid) + && (d.undo.len() > step.len || !Arc::ptr_eq(&step.after, &d.wb)) + { + d.undo.truncate(step.len); + push_entry(&mut d.undo, label, step.after.clone(), &d.wb, step.selection.clone()); + d.redo.clear(); + } + } else { + let mut label = label; + if epoch_changed && let Some((_, len)) = start { + // A command whose only step is its group's keeps that name ("Run Macro: Totals"). + // That is so only when the one entry the command added is a group's step from + // the command's own start to its own result: an entry a nested command made, + // a group step of an earlier command (undone since), or a group's step that + // the command edited past all name the step after the command instead. + if d.undo.len() == len + 1 + && let Some(last) = d.undo.last() + && Arc::ptr_eq(&last.wb, &wb) + && self.group_step.as_ref().is_some_and(|g| g.doc == uid && g.stepped && Arc::ptr_eq(&g.after, &d.wb)) + { + label = last.label.clone(); + } + d.undo.truncate(len); + } + push_entry(&mut d.undo, label, wb, &d.wb, sel); + d.redo.clear(); + } + // Inside a group the redo history waits for the group: `group_end` clears it when the + // group becomes a step, and `group_rollback` puts it back as it was. + d.revision += 1; + } + // The history limit applies once the outermost command has collapsed its steps, so the + // lengths it compared stay true. Meanwhile the steps made inside it are bounded on their own. + if self.depth == 0 { + self.trim_history(); + self.group_step = None; + } else if let Some(uid) = doc_uid { + self.bound_tail(uid); + } + result + } + + /// Runs `f` as one undo step labelled `label` on the active document: whatever commands `f` + /// runs, one Undo brings back the workbook as it was before. If `f` fails, every change it made + /// is rolled back and its error is returned. Groups nest; the outermost one makes the step. + pub fn grouped(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result) -> Result { + let token = self.group_begin(label)?; + let doc = token.doc(); + // A panic in `f` rolls the group back before it goes on, so a caller that catches it + // (outside `execute`, which drops a command's groups itself) finds no group left open. + match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self))) { + Ok(Ok(r)) => match self.group_end(token) { + Ok(_) => Ok(r), + // `f` closed the group's document (`file.close`, or `file.open` taking the place of + // an untouched new workbook), and the group went with it: nothing is left to undo. + Err(_) if !self.docs.iter().any(|d| d.uid == doc) => Ok(r), + Err(e) => Err(e), + }, + Ok(Err(e)) => { + // The error from `f` is the one to report. A rollback fails only when the group + // has already ended, and then there is nothing left to undo. + let _ = self.group_rollback(token); + Err(e) + } + Err(panic) => { + let _ = self.group_rollback(token); + std::panic::resume_unwind(panic) + } + } + } + + /// Opens an undo group on the active document that can span several calls (for example a + /// debugger that runs one step per call). Until [`Session::group_end`] or + /// [`Session::group_rollback`], undoable commands on that document add no undo steps of their + /// own, and Undo and Redo are unavailable there. + pub fn group_begin(&mut self, label: &str) -> Result { + let d = self.doc()?; + let g = OpenGroup { + id: self.next_group, + doc: d.uid, + label: label.trim_end_matches('…').to_string(), + start: d.undo.len(), + wb: d.wb.clone(), + selection: d.selection.clone(), + redo: d.redo.clone(), + }; + let token = GroupToken { id: g.id, doc: g.doc }; + self.next_group = self.next_group.saturating_add(1); + self.group_epoch = self.group_epoch.wrapping_add(1); + self.groups.push(g); + Ok(token) + } + + /// Ends an undo group. If the workbook changed, the group becomes one undo step, unless it is + /// nested in another open group on the same document, which then owns the step. Groups opened + /// inside it and still open end with it. Returns whether the workbook changed. + pub fn group_end(&mut self, token: GroupToken) -> Result { + let g = self.take_group(&token)?; + let outer = self.in_group(g.doc); + let Some(d) = self.docs.iter_mut().find(|d| d.uid == g.doc) else { return Ok(false) }; + // Steps pushed directly while the group was open fold into the group's step. + d.undo.truncate(g.start); + let changed = !Arc::ptr_eq(&d.wb, &g.wb); + if changed && !outer { + push_entry(&mut d.undo, g.label, g.wb, &d.wb, g.selection); d.redo.clear(); d.revision += 1; } - result + if !outer { + self.group_step = + Some(GroupStep { doc: g.doc, after: d.wb.clone(), selection: d.selection.clone(), len: d.undo.len(), stepped: changed }); + } + // Inside a command, the outermost command applies the history limit when it finishes. + if self.depth == 0 { + self.trim_history(); + } + Ok(changed) + } + + /// Stops the running action on the document with uid `doc`: every undo group open on it is + /// rolled back as [`Session::group_rollback`] does, putting back the workbook, selection, undo + /// and redo history as they were when the outermost one began. Their tokens are then spent + /// (ending or rolling one back reports that it has already ended). Returns whether a group + /// was open. `automate.stopAction` runs it on the active document. + pub fn abort_groups(&mut self, doc: u64) -> bool { + let Some(id) = self.groups.iter().filter(|g| g.doc == doc).map(|g| g.id).min() else { return false }; + // Rolling back the outermost group ends the ones opened inside it. + self.group_rollback(GroupToken { id, doc }).is_ok() + } + + /// Rolls back every open undo group numbered `first` or later (the groups a failed command + /// opened), outermost first on each document. + fn rollback_groups_from(&mut self, first: u64) { + let opened: Vec<(u64, u64)> = self.groups.iter().filter(|g| g.id >= first).map(|g| (g.id, g.doc)).collect(); + for (id, doc) in opened { + // A group inside one rolled back before it is already gone. + if self.groups.iter().any(|g| g.id == id) { + let _ = self.group_rollback(GroupToken { id, doc }); + } + } + } + + /// Bounds the undo steps pushed while a command runs (a script of many steps, a subtotal per + /// group): at most [`UNDO_LIMIT`] of them within [`UNDO_BYTES_LIMIT`], dropping the oldest. + /// Only steps after the latest mark go: a running command's start, an open group's start and + /// the end of the last group are positions the command or group collapses its steps by when + /// it finishes, so what is before them stays as it is until [`Session::trim_history`]. + fn bound_tail(&mut self, uid: u64) { + let Some(d) = self.docs.iter_mut().find(|d| d.uid == uid) else { return }; + let marks = self.marks.iter().filter(|(doc, _)| *doc == uid).map(|(_, len)| *len); + let groups = self.groups.iter().filter(|g| g.doc == uid).map(|g| g.start); + let step = self.group_step.as_ref().filter(|g| g.doc == uid).map(|g| g.len); + let mark = marks.chain(groups).chain(step).max().unwrap_or(0).min(d.undo.len()); + let mut bytes = d.undo.iter().skip(mark).fold(0usize, |sum, e| sum.saturating_add(e.retained_bytes)); + while d.undo.len() > mark + 1 && (d.undo.len() - mark > UNDO_LIMIT || bytes > UNDO_BYTES_LIMIT) { + bytes = bytes.saturating_sub(d.undo.remove(mark).retained_bytes); + } + } + + /// Drops the oldest undo steps past [`UNDO_LIMIT`] or [`UNDO_BYTES_LIMIT`]. A document with + /// an open group waits for the group to end, because the group remembers where its steps start. + fn trim_history(&mut self) { + let groups = &self.groups; + for d in self.docs.iter_mut().filter(|d| !groups.iter().any(|g| g.doc == d.uid)) { + trim_to_budget(&mut d.undo, UNDO_BYTES_LIMIT); + } + } + + /// Ends an undo group by putting back the workbook, selection, undo and redo history as they + /// were when it began. + pub fn group_rollback(&mut self, token: GroupToken) -> Result<()> { + let g = self.take_group(&token)?; + let Some(d) = self.docs.iter_mut().find(|d| d.uid == g.doc) else { return Ok(()) }; + if !Arc::ptr_eq(&d.wb, &g.wb) { + d.wb = g.wb; + d.calc.rebuild(&d.wb); + } + d.selection = g.selection; + d.undo.truncate(g.start); + d.redo = g.redo; + d.revision += 1; + // The record of the last group step stays when the document is back exactly where that + // step left it (this group began there); otherwise the rollback is where the group ended. + let kept = self.group_step.as_ref().is_some_and(|st| st.doc == g.doc && st.len == g.start && Arc::ptr_eq(&st.after, &d.wb)); + if !kept && !self.groups.iter().any(|o| o.doc == g.doc) { + self.group_step = Some(GroupStep { doc: g.doc, after: d.wb.clone(), selection: d.selection.clone(), len: g.start, stepped: false }); + } + Ok(()) + } + + /// Starts recording top-level commands into an empty buffer, replacing any recording in + /// progress. Queries, failed commands and commands run by other commands are not recorded. + pub fn start_recording(&mut self) { + self.recorder = Some(record::Recorder::default()); + } + + /// Stops recording and returns what was recorded (`None` when nothing was recording). + pub fn stop_recording(&mut self) -> Option { + self.recorder.take() + } + + /// Whether the recording buffer is collecting commands. + pub fn is_recording(&self) -> bool { + self.recorder.is_some() + } + + /// How many commands are running: 0 between top-level commands, 1 inside one, 2 inside a + /// command run by a command, and so on. + pub fn depth(&self) -> u32 { + self.depth + } + + /// Whether an undo group is open on the document with this uid. + pub fn in_group(&self, doc: u64) -> bool { + self.groups.iter().any(|g| g.doc == doc) + } + + /// Removes the token's group and any group on the same document opened after it. + fn take_group(&mut self, token: &GroupToken) -> Result { + let ended = || EngineError::Other("That undo group has already ended.".into()); + let at = self.groups.iter().position(|g| g.id == token.id).ok_or_else(ended)?; + let mut later = self.groups.split_off(at).into_iter(); + let g = later.next().ok_or_else(ended)?; + self.groups.extend(later.filter(|o| o.doc != g.doc)); + self.group_epoch = self.group_epoch.wrapping_add(1); + Ok(g) } pub fn run(&mut self, id: &str, params: Json) -> std::result::Result { @@ -468,6 +896,8 @@ impl Session { let r = self.execute(id, params); let requests = self.take_ui_requests(); let asked_for_file = requests.iter().any(|q| matches!(q, UiRequest::Dialog(name, _) if name == "open" || name == "saveAs")); + // A call that only opened a dialog is not recorded (see `execute`), so the recording + // has no step to take back here. if r.is_ok() && asked_for_file { let what = match id { "file.open" => "`path` or `base64`", diff --git a/crates/engine/src/record.rs b/crates/engine/src/record.rs new file mode 100644 index 0000000000..7804be6c9e --- /dev/null +++ b/crates/engine/src/record.rs @@ -0,0 +1,66 @@ +//! The recording buffer: the commands a person (or an agent) ran while recording, each with the +//! document, sheet and selection it ran in. +//! +//! Only top-level calls are kept. A command that runs other commands (Custom Views › Show, a +//! script) is one step, so playing the recording back does not run the inner commands twice. The +//! buffer is separate from the journal, so trimming the journal never loses recorded steps, and +//! secret parameters are left out as they are in the journal ([`crate::cmd::journal_params`]). + +use serde::Serialize; +use serde_json::Value as Json; + +use crate::Selection; + +/// The most steps one recording keeps; later ones are counted in [`Recorder::dropped`]. +pub const RECORD_LIMIT: usize = 100_000; +/// The most bytes (steps serialized as JSON) one recording keeps. A step keeps its full +/// parameters so it can be played back, and a `file.open` holds the whole file as base64, so +/// without a size cap a recording of a few opens of a large workbook keeps tens of MB alive. +pub const RECORD_MAX_BYTES: usize = 64 * 1024 * 1024; + +/// One recorded command and where it ran. +#[derive(Clone, Debug, PartialEq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct RecordedStep { + /// Uid of the document that was active when the command ran (0 when none was open). + pub doc: u64, + /// That document's active sheet (index and name) just before the command ran. + pub sheet: usize, + pub sheet_name: String, + /// That document's selection just before the command ran. + pub selection: Selection, + /// The command id and its parameters, without secrets. + pub id: String, + pub params: Json, +} + +/// The steps recorded since recording started. +#[derive(Clone, Debug, Default)] +pub struct Recorder { + pub steps: Vec, + /// Steps not kept because the recording reached [`RECORD_LIMIT`] or [`RECORD_MAX_BYTES`]. + /// Once a step is dropped, every later one is too, so `steps` is always the beginning of + /// what ran, never a sequence with gaps. + pub dropped: usize, + /// The size of `steps` serialized as JSON. + bytes: usize, +} + +impl Recorder { + /// Adds a step, or counts it as dropped once the buffer is full. + pub fn push(&mut self, step: RecordedStep) { + // Once a step has been dropped, or the step count is full, the rest are counted without + // being serialized (a `file.open` step holds a whole file). + if self.dropped > 0 || self.steps.len() >= RECORD_LIMIT { + self.dropped = self.dropped.saturating_add(1); + return; + } + let size = serde_json::to_vec(&step).map_or(0, |v| v.len()); + if self.bytes.saturating_add(size) <= RECORD_MAX_BYTES { + self.bytes = self.bytes.saturating_add(size); + self.steps.push(step); + } else { + self.dropped = self.dropped.saturating_add(1); + } + } +} diff --git a/crates/engine/src/sample.rs b/crates/engine/src/sample.rs index 129fe2171b..90029bac36 100644 --- a/crates/engine/src/sample.rs +++ b/crates/engine/src/sample.rs @@ -26,7 +26,7 @@ pub fn build(name: &str) -> Option { _ => return None, }; } - s.add_document(DocState::new(wb, None, title(name))); + s.add_document(DocState::new(wb, None, title(name), crate::Trust::Local)); let ok = match name { "budget" => budget(&mut s), "sales" => sales(&mut s), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 756a4d0907..1be90d56c9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3,6 +3,11 @@ use serde_json::json; use crate::Session; +thread_local! { + /// Copies of a document's undo and redo history `execute` has taken on this thread. + pub(crate) static HISTORY_COPIES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + fn s() -> Session { let mut s = Session::new(); s.new_workbook(); @@ -4599,3 +4604,4 @@ fn html_export_without_pictures_or_charts_has_no_fixed_geometry() { let html = crate::io::to_html(&s.doc().unwrap().wb, 0).unwrap(); assert!(!html.contains("") && !html.contains("table-layout") && !html.contains("

0.0) { continue; } let mut t = 0.0; - let mut n = 0; - while t < len && n < 10_000 { + while t < len && budget > 0 { let e = (t + on).min(len); out.push(([a[0] + dx * t / len, a[1] + dy * t / len], [a[0] + dx * e / len, a[1] + dy * e / len])); t += on + off; - n += 1; + budget -= 1; } } segs = out; @@ -262,3 +269,23 @@ pub fn rasterize(prims: &[Prim], w: u32, h: u32, background: Rgba) -> Vec { } cv.buf } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dashed_geometry_stays_within_the_dash_budget() { + // Forty very long dashed lines, each allowed 10_000 dashes per segment: before the + // shared cap they eagerly allocated ~1.6 MB of segment pairs × points and a like + // number of quads before any of it reached a 4-pixel canvas (finding: dash expansion). + let prims: Vec = + (0..40).map(|i| Prim::Line { pts: vec![[0.0, i as f32], [900.0, i as f32]], color: [0, 0, 0, 255], width: 1.0, dash: true }).collect(); + let img = rasterize(&prims, 2, 2, [255, 255, 255, 255]); + assert_eq!(img.len(), 2 * 2 * 4); + // And the geometry itself is bounded even for one huge dashed polyline. + assert!(stroke_paths(&[[0.0, 0.0], [1e6, 0.0]], 1.0, true, false).len() <= 65_536); + let prim = [Prim::Line { pts: vec![[0.0, 0.0], [10.0, 0.0]], color: [0, 0, 0, 255], width: 1.0, dash: true }]; + assert_eq!(rasterize(&prim, 1, 1, [0, 0, 0, 255]).len(), 4); + } +} diff --git a/crates/ui-egui/src/floating_pictures.rs b/crates/ui-egui/src/floating_pictures.rs index 68de6e203d..5caa0869c5 100644 --- a/crates/ui-egui/src/floating_pictures.rs +++ b/crates/ui-egui/src/floating_pictures.rs @@ -95,6 +95,13 @@ impl Cache { } None => (None, u32::MAX), }; + // Eviction only frees entries used before this frame, so a workbook covered in same-frame + // pictures can leave the cache unable to make room. Then draw the texture without + // retaining it: the handle is dropped at the end of the frame and the GPU memory goes + // with it, instead of the budget being outflanked (finding: cache admission). + if self.entries.len() >= MAX_CACHE_ITEMS || self.bytes.saturating_add(bytes) > MAX_CACHE_BYTES { + return texture; + } self.bytes = self.bytes.saturating_add(bytes); self.entries.insert(key, Entry { texture: texture.clone(), bytes, side, used: frame }); texture @@ -212,13 +219,24 @@ mod tests { } assert!(cache.bytes <= MAX_CACHE_BYTES, "{} bytes kept", cache.bytes); assert!(cache.entries.len() < data.len()); - // Many on screen at once, over the budget: every one keeps its texture, frame after frame. + // Many on screen at once, over the budget: each still gets a texture this frame, but the + // cache cannot grow past its budget whatever the workbook shows. Eight of the 16 MB + // textures fit the 128 MB budget, and those stay pinned; the rest re-upload each frame + // instead of being retained over it (finding: cache admission). + // Two frames pass so the rehearsal's entries are old enough to be evictable. + frame(&ctx); + frame(&ctx); let shown: Vec<_> = data.iter().enumerate().take(12).map(|(i, d)| cache.texture(&ctx, key(1, i as u32, d), d, 2048).unwrap().id()).collect(); for _ in 0..3 { frame(&ctx); for (i, d) in data.iter().enumerate().take(12) { - assert_eq!(cache.texture(&ctx, key(1, i as u32, d), d, 2048).map(|t| t.id()), Some(shown[i])); + let id = cache.texture(&ctx, key(1, i as u32, d), d, 2048).unwrap().id(); + if i < 8 { + assert_eq!(id, shown[i], "a picture that fits keeps its texture"); + } } + assert!(cache.bytes <= MAX_CACHE_BYTES, "{} bytes kept", cache.bytes); + assert!(cache.entries.len() <= 8, "{} entries kept", cache.entries.len()); } } diff --git a/packaging/macos/import-cert.sh b/packaging/macos/import-cert.sh index 4611ffe055..ce9432e4e2 100755 --- a/packaging/macos/import-cert.sh +++ b/packaging/macos/import-cert.sh @@ -17,12 +17,16 @@ if [[ -z "${APPLE_CERTIFICATE:-}" ]]; then exit 0 fi -TMP="${RUNNER_TEMP:-${TMPDIR:-/tmp}}" -KEYCHAIN="$TMP/gridcraft-signing.keychain-db" -CERT="$TMP/gridcraft-signing.p12" +BASE="${RUNNER_TEMP:-${TMPDIR:-/tmp}}" +# A private directory created exclusively, not predictable names in a possibly shared temp +# root: another local user's symlink at `gridcraft-signing.p12` would otherwise select the +# write target and capture the decoded certificate. +WORK="$(mktemp -d "$BASE/gridcraft-signing.XXXXXXXX")" +KEYCHAIN="$WORK/gridcraft-signing.keychain-db" +CERT="$WORK/gridcraft-signing.p12" KC_PASS="${KEYCHAIN_PASSWORD:-$(openssl rand -hex 24)}" -trap 'rm -f "$CERT"' EXIT +trap 'rm -rf "$WORK"' EXIT printf '%s' "$APPLE_CERTIFICATE" | base64 --decode >"$CERT" security create-keychain -p "$KC_PASS" "$KEYCHAIN" security set-keychain-settings -lut 21600 "$KEYCHAIN" diff --git a/xtask/src/stats.rs b/xtask/src/stats.rs index f40b1300ac..462f1c7de9 100644 --- a/xtask/src/stats.rs +++ b/xtask/src/stats.rs @@ -2,6 +2,9 @@ use std::path::Path; +#[cfg(unix)] +use std::os::unix::fs::MetadataExt as _; + #[derive(Default, Clone, Copy)] struct Counts { files: usize, @@ -10,7 +13,24 @@ struct Counts { proptests: usize, } -fn walk(dir: &Path, c: &mut Counts) { +fn walk(dir: &Path, c: &mut Counts, seen: &mut std::collections::HashSet, budget: &mut usize) { + if *budget == 0 { + // Hard stop for pathological trees: two directory symlinks pointing at each other + // would otherwise be walked exponentially. + return; + } + // Directory identities (dev+inode on unix) make a symlink to a visited directory a + // no-op, not another exponential branch of the scan. + #[cfg(unix)] + let id = std::fs::metadata(dir).ok().and_then(|m| m.dev().checked_mul(1 << 60).and_then(|d| d.checked_add(m.ino()))); + #[cfg(not(unix))] + let id: Option = None; + if let Some(id) = id + && !seen.insert(id) + { + return; + } + *budget -= 1; let Ok(rd) = std::fs::read_dir(dir) else { return; }; @@ -18,10 +38,11 @@ fn walk(dir: &Path, c: &mut Counts) { let p = e.path(); let name = e.file_name(); let name = name.to_string_lossy(); + // A symlinked directory is descended only if its identity is new (checked above). if p.is_dir() { // Nested fuzz workspaces and build output are not part of the crate. if name != "target" && name != "fuzz" && !name.starts_with('.') { - walk(&p, c); + walk(&p, c, seen, budget); } } else if name.ends_with(".rs") && let Ok(s) = std::fs::read_to_string(&p) @@ -71,7 +92,7 @@ pub fn run(_root: &Path, exact: bool) -> Result<(), String> { continue; }; let mut c = Counts::default(); - walk(&dir, &mut c); + walk(&dir, &mut c, &mut std::collections::HashSet::new(), &mut 10_000); let ex = if exact { exact_tests(&name) } else { None }; rows.push((name, c, ex)); } From 07890b47f8545ea71385a6f4b996a4a0a1ae2fe7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 24 Oct 2026 03:41:44 -0700 Subject: [PATCH 194/687] Keyboard: Ctrl+Shift+U expands and collapses the formula bar instead of underlining Ctrl+Shift+U ran the Ctrl+U shortcut on Windows and Linux and underlined the selection. In Excel it expands or collapses the formula bar, on Windows and as Control+Shift+U on a Mac. It now does that on the grid and in the cell editor or formula bar while editing, and the entry stays open. The new UI command view.expandFormulaBar {on?} sets the state, or toggles it without a parameter, and returns {on}. The chevron at the end of the formula bar is unchanged. --- ROADMAP.md | 1 + crates/ui-egui/src/control.rs | 1 + crates/ui-egui/src/formula_bar.rs | 4 + crates/ui-egui/src/keymap.rs | 2 + crates/ui-egui/src/lib.rs | 4 + crates/ui-egui/tests/it/formula_bar_expand.rs | 95 +++++++++++++++++++ crates/ui-egui/tests/it/main.rs | 1 + docs/ui-parity.md | 8 +- 8 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 crates/ui-egui/tests/it/formula_bar_expand.rs diff --git a/ROADMAP.md b/ROADMAP.md index da06359b56..1d9cf5f484 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -135,6 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-24 | Ctrl+Shift+U (Control+Shift+U on a Mac) expands and collapses the formula bar, on the grid and while editing (`view.expandFormulaBar`); on Windows and Linux it used to underline | | 2026-10-23 | Accounting negatives in text: `$(1,234)`, `$ (5)` and `(5)%` read as negative numbers when typed, coerced by arithmetic or passed to VALUE, like Excel 365; `(-5)`, `((5))`, `$5%` and dates or times in parentheses stay text | | 2026-10-23 | Calc: `INDEX(A1:C3,2)` (one index on a reference of several rows and columns) is `#REF!` as in Excel, instead of the whole row; `INDEX(A1:C3,2,)` still gives the row | | 2026-10-22 | Formula hardening: digits too many for a finite number are text, so `"1 <400 digits>/<400 digits>"` no longer coerces to NaN; a NaN column index in VLOOKUP, HLOOKUP and the D-functions is `#VALUE!` instead of an index underflow; REGEXREPLACE stops building a result as soon as it can only be too long (a worst case went from 1 GB to 7 MB) (#2163, #2162) | diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 11d535ac41..8e29bccb4c 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -230,6 +230,7 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) - for (id, label, params) in [ ("ui.ribbonTab", "Ribbon Tab", ""), ("view.collapseRibbon", "Collapse Ribbon", ""), + ("view.expandFormulaBar", "Expand Formula Bar", "{on?: bool} (toggles without it) → {on}"), ("view.darkMode", "Dark Mode", ""), ("view.theme", "Display Theme", ""), ("view.animations", "Animations", "{on?: bool} (toggles without it) → {on}"), diff --git a/crates/ui-egui/src/formula_bar.rs b/crates/ui-egui/src/formula_bar.rs index b88699f571..3a33f38670 100644 --- a/crates/ui-egui/src/formula_bar.rs +++ b/crates/ui-egui/src/formula_bar.rs @@ -290,6 +290,10 @@ pub fn editor_widget(app: &mut SheetApp, ui: &mut egui::Ui, id: egui::Id, font: state.store(ui.ctx(), id); } } + // Ctrl+Shift+U (Control+Shift+U on a Mac) expands or collapses the formula bar while editing too. + if ui.input_mut(|i| i.consume_key(egui::Modifiers::CTRL | egui::Modifiers::SHIFT, Key::U)) { + app.run_or_alert("view.expandFormulaBar", json!({})); + } } // The text widget. let refs = formula_refs(&ed.text); diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs index 5840036a16..03bd2d6557 100644 --- a/crates/ui-egui/src/keymap.rs +++ b/crates/ui-egui/src/keymap.rs @@ -103,6 +103,8 @@ pub fn lookup(key: Key, physical: Option, m: Modifiers, mac: bool) -> Optio L if (cmd || ctrl) && s => run("data.filter"), L if cmd && mac => run("home.alignLeft"), L if ctrl && mac => Shortcut::Dialog("defineName", json!({})), + // Ctrl+Shift+U (Control+Shift+U in Excel for Mac) expands or collapses the formula bar. + U if ctrl && s => run("view.expandFormulaBar"), U if ctrl && mac && !s => Shortcut::Edit, L if cmd => run("insert.table"), E if cmd && mac && !s => run("home.alignCenter"), diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 14c4423310..5b421a4dbe 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -632,6 +632,10 @@ impl SheetApp { self.ui.ribbon_collapsed = !self.ui.ribbon_collapsed; Ok(Json::Null) } + "view.expandFormulaBar" => { + self.ui.formula_bar_expanded = p.get("on").and_then(Json::as_bool).unwrap_or(!self.ui.formula_bar_expanded); + Ok(json!({"on": self.ui.formula_bar_expanded})) + } "view.darkMode" => { let current = if self.ui.system_theme { self.effective_dark } else { self.ui.dark }; self.ui.dark = p.get("on").and_then(Json::as_bool).unwrap_or(!current); diff --git a/crates/ui-egui/tests/it/formula_bar_expand.rs b/crates/ui-egui/tests/it/formula_bar_expand.rs new file mode 100644 index 0000000000..95b4368e5f --- /dev/null +++ b/crates/ui-egui/tests/it/formula_bar_expand.rs @@ -0,0 +1,95 @@ +//! Ctrl+Shift+U (Control+Shift+U in Excel for Mac) expands and collapses the formula bar. + +use egui::{Event, Key, Modifiers}; +use gridcraft_engine::Session; +use gridcraft_ui_egui::SheetApp; +use gridcraft_ui_egui::keymap::{Shortcut, lookup}; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +/// Ctrl+Shift on Windows and Linux sets `ctrl`, `command` and `shift`. +const CTRL_SHIFT: Modifiers = Modifiers { alt: false, ctrl: true, shift: true, mac_cmd: false, command: true }; +/// Control+Shift on a Mac: `ctrl` without `command`. +const MAC_CONTROL_SHIFT: Modifiers = Modifiers { alt: false, ctrl: true, shift: true, mac_cmd: false, command: false }; + +fn harness() -> Harness { + let mut session = Session::new(); + session.new_workbook(); + session.execute("cell.set", json!({"cell": "A1", "input": "text"})).unwrap(); + session.execute("selection.set", json!({"range": "A1"})).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state( + |ui, app: &mut SheetApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + app.ui(ui); + }, + SheetApp::new(session, Default::default()), + ); + h.run_steps(4); + h +} + +fn ctrl_shift_u(h: &mut Harness, m: Modifiers) { + h.input_mut().events.push(Event::Key { key: Key::U, physical_key: Some(Key::U), pressed: true, repeat: false, modifiers: m }); + h.input_mut().events.push(Event::Key { key: Key::U, physical_key: Some(Key::U), pressed: false, repeat: false, modifiers: m }); + h.run_steps(2); +} + +fn expanded(h: &Harness) -> bool { + h.state().ui.formula_bar_expanded +} + +fn underlined(h: &Harness) -> bool { + h.state().session.journal.iter().any(|(id, _)| id == "home.underline") +} + +#[test] +fn ctrl_shift_u_on_the_grid_expands_and_collapses_the_formula_bar() { + let mut h = harness(); + assert!(!expanded(&h)); + ctrl_shift_u(&mut h, CTRL_SHIFT); + assert!(expanded(&h), "the first Ctrl+Shift+U expands the formula bar"); + ctrl_shift_u(&mut h, CTRL_SHIFT); + assert!(!expanded(&h), "the second Ctrl+Shift+U collapses it"); + assert!(!underlined(&h), "Ctrl+Shift+U is not Underline"); +} + +#[test] +fn control_shift_u_expands_the_formula_bar_in_excel_for_mac() { + let mut h = harness(); + h.ctx.set_os(egui::os::OperatingSystem::Mac); + h.run_steps(2); + ctrl_shift_u(&mut h, MAC_CONTROL_SHIFT); + assert!(expanded(&h)); + assert!(h.state().editor.is_none(), "Control+Shift+U does not start editing as Control+U does"); + let expand = Some(Shortcut::Run("view.expandFormulaBar", json!({}))); + assert_eq!(lookup(Key::U, Some(Key::U), MAC_CONTROL_SHIFT, true), expand); + assert_eq!(lookup(Key::U, Some(Key::U), CTRL_SHIFT, false), expand); + // Control+U on a Mac still edits the active cell. + assert_eq!(lookup(Key::U, Some(Key::U), Modifiers::CTRL, true), Some(Shortcut::Edit)); +} + +#[test] +fn ctrl_shift_u_while_editing_keeps_the_entry() { + let mut h = harness(); + h.state_mut().begin_edit(Some("draft".into()), false); + h.run_steps(2); + ctrl_shift_u(&mut h, CTRL_SHIFT); + assert!(expanded(&h), "Ctrl+Shift+U works in the cell editor too"); + let ed = h.state().editor.as_ref().expect("the edit stays open"); + assert_eq!(ed.text, "draft"); + ctrl_shift_u(&mut h, CTRL_SHIFT); + assert!(!expanded(&h)); + assert!(h.state().editor.is_some()); +} + +#[test] +fn expand_formula_bar_command_sets_or_toggles_the_state() { + let mut h = harness(); + assert_eq!(h.state_mut().run("view.expandFormulaBar", json!({"on": true})), Ok(json!({"on": true}))); + assert!(expanded(&h)); + assert_eq!(h.state_mut().run("view.expandFormulaBar", json!({"on": true})), Ok(json!({"on": true}))); + assert_eq!(h.state_mut().run("view.expandFormulaBar", json!({})), Ok(json!({"on": false}))); + assert!(!expanded(&h)); +} diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 3a9769c1c5..06ffaac023 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -34,6 +34,7 @@ mod find_dialog_focus; mod floating_pictures; mod font_size_box; mod format_painter; +mod formula_bar_expand; mod formula_locale; mod formula_point_headers; mod formula_popup; diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 5d5f1efeee..17538e580c 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (New Formatting Rule's formula box takes references clicked or dragged on the grid, #2348; Page Up and Ctrl+Up into frozen rows, Ctrl+Left and Alt+Page Up into frozen columns bring the scrolled area back; Auto Fill Options button after a left-drag of the fill handle (#1403); Show Outline Symbols, Ctrl+8; a too-wide number in a repeat-fill format such as Accounting shows #### instead of losing its leading digits; Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-24 · **Change:** minor (Ctrl+Shift+U expands and collapses the formula bar; New Formatting Rule's formula box takes references clicked or dragged on the grid, #2348; Page Up and Ctrl+Up into frozen rows, Ctrl+Left and Alt+Page Up into frozen columns bring the scrolled area back; Auto Fill Options button after a left-drag of the fill handle (#1403); Show Outline Symbols, Ctrl+8; a too-wide number in a repeat-fill format such as Accounting shows #### instead of losing its leading digits; Esc and Enter close a data validation alert over the cell editor; Ctrl+Enter fill gives each cell the automatic number format typing gives it; keytip letters typed with Alt held, #1846; Number Stored as Text indicator and Convert to Number) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -134,9 +134,10 @@ otherwise. Number-format and border keys match the physical key (Ctrl+Shift+4, w | F5, Shift+F5 (Mac), F7, Shift+F9, Ctrl+Alt+F9, F12, Ctrl+F4 | same | Go To, Find, Spelling, calculate sheet / all, Save As, close | **added** | | Shift+F10, Alt+Shift+F10, Ctrl+F1 | Shift+F10, Cmd+Option+R | context menu, Error Checking, ribbon | **added** | | F1, F3, F4 (repeat), F6 / Shift+F6, F8 / Shift+F8, F11, End mode | same | help, Paste Name, Repeat, pane cycling, extend/add mode, chart sheet | missing (no such feature yet) | -| Ctrl+Q, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | Quick Analysis, Function Arguments, formula bar height, tooltips | missing | +| Ctrl+Q, Ctrl+A after a function name, Ctrl+Alt+P | same | Quick Analysis, Function Arguments, tooltips | missing | | Ctrl+Shift+A after a function name | Control+Shift+A | argument names: `=SUMIF(` becomes `=SUMIF(range,criteria,sum_range)`, the `(` added when missing; one Undo takes them back | **added** | -| Ctrl+6, Ctrl+Q, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | objects, Quick Analysis, Function Arguments, formula bar height, tooltips | missing | +| Ctrl+Shift+U | Control+Shift+U | expand or collapse the formula bar (`view.expandFormulaBar`), on the grid and while editing; on Windows and Linux it no longer underlines | **added** | +| Ctrl+6, Ctrl+Q, Ctrl+A after a function name, Ctrl+Alt+P | same | objects, Quick Analysis, Function Arguments, tooltips | missing | | Ctrl+Shift+Page Up / Down, Ctrl+Alt+5, Ctrl+Shift+F1, Ctrl+F5–F10, Alt+F8 / F11 / F12 | same | group sheets, shape cycling, full screen, window keys, macros, VBA, Power Query | missing | | — | Cmd+= (calculate), Cmd+T (references while editing; F4 works), Shift+F7, Cmd+Shift+L | Mac-only extras | missing (Cmd+= is egui's zoom key; no thesaurus or Modify Cell Style) | @@ -178,6 +179,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. | Date | Change | Summary | |---|---|---| +| 2026-10-24 | minor | Ctrl+Shift+U (Control+Shift+U on a Mac) expands or collapses the formula bar, as in Excel ("Expand or collapse the formula bar. Ctrl+Shift+U"), on the grid and in the cell editor or formula bar while editing. On Windows and Linux it used to underline the selection, as Ctrl+U does. The command is `view.expandFormulaBar` (`{on?}`, toggles without it) | | 2026-10-23 | minor | Conditional Formatting ▸ New Rule, formula: clicking a cell on the grid writes its absolute reference at the formula box's caret (`=$B$2`, an empty box starts with `=`), a drag writes a range (`$B$2:$C$4`) and a second click replaces the reference just written, as in Excel ("Selecting cells on the worksheet inserts absolute cell references"). The selection the rule applies to doesn't move, and the box keeps the keyboard. After an operand (`=A1>5`) the click does nothing (#2348) | | 2026-10-23 | minor | Frozen panes: Page Up, Ctrl+Up and Shift variants that move from the scrolled rows into the frozen ones bring the scrolled rows back to the top, so the rows in between are seen again (Page Up from far down used to end on row 1 with the view still a page down); Ctrl+Left and Alt+Page Up do the same for frozen columns. Other ways of selecting a frozen cell (click, Find, Go To, Undo, formula pointing) leave the view where it is | | 2026-10-23 | minor | After a left-drag of the fill handle an Auto Fill Options button sits below the filled cells; its menu redoes the fill as Copy Cells, Fill Series, Fill Formatting Only or Fill Without Formatting (undoes the drag's fill, then `edit.autoFill` in that mode). It goes away when the workbook changes or the selection moves (#1403) | From 6cb32201641eb8016ad57290e87e20b7ddf0b1c1 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sat, 24 Oct 2026 11:09:26 +0000 Subject: [PATCH 195/687] Scale MINVERSE rounding cutoff to the input matrix Found while testing crates/functions/src/math.rs:923 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/functions/src/math.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/functions/src/math.rs b/crates/functions/src/math.rs index 53a92f00f5..9824da7bf2 100644 --- a/crates/functions/src/math.rs +++ b/crates/functions/src/math.rs @@ -918,7 +918,9 @@ fn minverse(a: &[Arg], _c: &mut dyn Ctx) -> R { } } } - let inv = inv.into_iter().map(|x| if x.abs() < 1e-15 { 0.0 } else { x }).collect(); + // Rounding noise in the inverse scales with 1/scale of the input matrix. + let noise = 1e-15 / scale; + let inv = inv.into_iter().map(|x| if x.abs() < noise { 0.0 } else { x }).collect(); to_value_array(n, n, inv) } @@ -1622,6 +1624,17 @@ mod tests { is_text(ev2("ROMAN", 99.0, 4.0), "IC"); } + #[test] + fn minverse_scale_relative() { + let big = ev("MINVERSE", vec![av(arr(vec![vec![nv(1e20), nv(0.0)], vec![nv(0.0), nv(1e20)]]))]); + let r = rows_of(&big); + close_tol(r[0][0].clone(), 1e-20, 1e-25); + close_tol(r[1][1].clone(), 1e-20, 1e-25); + let small = ev("MINVERSE", vec![av(arr(vec![vec![nv(1e-20), nv(0.0)], vec![nv(0.0), nv(1e-20)]]))]); + let r = rows_of(&small); + close_tol(r[0][0].clone(), 1e20, 1e10); + } + #[test] fn matrices() { let a = arr(vec![vec![nv(1.0), nv(2.0)], vec![nv(3.0), nv(4.0)]]); From d0fe8b6423694b9124e35cd8f283b7a58220dbe5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 24 Oct 2026 04:50:46 -0700 Subject: [PATCH 196/687] Tests: the engine's file-system tests each use their own temporary folder save_fails_upfront_when_the_temp_name_is_taken, save_keeps_the_destination_access_restrictions and open_refuses_directories_and_unknowable_lengths in crates/engine/src/io.rs all worked in temp_dir()/gridcraft-fs-safety and removed it at the start and the end. cargo test runs them on parallel threads, so one test could delete the folder while another was writing to it, and the other test failed. Each test now uses its own folder, named with a short label and the process id, as the gridcraft-write and gridcraft-sandbox tests in the same file do. --- crates/engine/src/io.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/io.rs b/crates/engine/src/io.rs index be47e0a670..92ab690388 100644 --- a/crates/engine/src/io.rs +++ b/crates/engine/src/io.rs @@ -770,7 +770,7 @@ mod tests { fn save_fails_upfront_when_the_temp_name_is_taken() { // Another local user who can write the directory must not be able to point the // save's predictable temporary name at their own file: creation is exclusive. - let dir = std::env::temp_dir().join("gridcraft-fs-safety"); + let dir = std::env::temp_dir().join(format!("gridcraft-fs-safety-temp-name-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); let victim = dir.join("victim.txt"); @@ -788,7 +788,7 @@ mod tests { #[test] fn save_keeps_the_destination_access_restrictions() { use std::os::unix::fs::PermissionsExt; - let dir = std::env::temp_dir().join("gridcraft-fs-safety"); + let dir = std::env::temp_dir().join(format!("gridcraft-fs-safety-permissions-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); let dest = dir.join("private.xlsx"); @@ -808,7 +808,7 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] #[test] fn open_refuses_directories_and_unknowable_lengths() { - let dir = std::env::temp_dir().join("gridcraft-fs-safety"); + let dir = std::env::temp_dir().join(format!("gridcraft-fs-safety-open-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); assert!(read_file(dir.to_str().unwrap()).is_err(), "a directory is not a regular file"); From 3099df934eda27cd457a9b8d1525d3fc973a1b7b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 24 Oct 2026 07:58:13 -0400 Subject: [PATCH 197/687] M7: verify Agile encryption's dataIntegrity before returning plaintext (#2425) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agile decryption checked only the password verifier, which authenticates the key — not the package. An attacker who could replace EncryptedPackage ciphertext could change workbook content undetected ([MS-OFFCRYPTO] 2.3.4.14, the descriptor's dataIntegrity element). The reader now requires the dataIntegrity pair, decrypts it with the recovered package key, and checks HMAC(hash, hmacKey) over the whole EncryptedPackage stream (StreamSize field included) against the stored value before any plaintext is returned. Excel always writes the element ("SHOULD" in the spec); descriptors without it are refused as damaged rather than accepted unauthenticated. - crypto: RFC 2104/4231 HMAC test vectors, RFC-test cases for a key longer than the block size - fixtures write the pair like Excel; new tests: missing/empty/ragged integrity fields, tampered ciphertext and descriptor, a changed StreamSize field --- crates/xlsx/src/crypto.rs | 58 ++++++++++++++++- crates/xlsx/src/crypto/tests.rs | 82 +++++++++++++++++++++++- crates/xlsx/src/testing.rs | 25 +++++--- crates/xlsx/src/tests/encrypted_tests.rs | 3 +- 4 files changed, 153 insertions(+), 15 deletions(-) diff --git a/crates/xlsx/src/crypto.rs b/crates/xlsx/src/crypto.rs index 46a19d046d..0a63044a0b 100644 --- a/crates/xlsx/src/crypto.rs +++ b/crates/xlsx/src/crypto.rs @@ -26,6 +26,9 @@ const BLOCK: usize = 16; pub(crate) const BLOCK_VERIFIER_INPUT: [u8; 8] = [0xFE, 0xA7, 0xD2, 0x76, 0x3B, 0x4B, 0x9E, 0x79]; pub(crate) const BLOCK_VERIFIER_HASH: [u8; 8] = [0xD7, 0xAA, 0x0F, 0x6D, 0x30, 0x61, 0x34, 0x4E]; pub(crate) const BLOCK_KEY: [u8; 8] = [0x14, 0x6E, 0x0B, 0xE7, 0xAB, 0xAC, 0xD0, 0xD6]; +/// The two dataIntegrity block keys ([MS-OFFCRYPTO] 2.3.4.14), one per HMAC field. +pub(crate) const BLOCK_HMAC_KEY: [u8; 8] = [0x5F, 0xB2, 0xAD, 0x01, 0x0C, 0xB9, 0xE1, 0xF6]; +pub(crate) const BLOCK_HMAC_VALUE: [u8; 8] = [0xA0, 0x67, 0x7F, 0x02, 0xB2, 0x2C, 0x84, 0x33]; /// The password key encryptor's URI (the other kind is certificate-based). pub(crate) const PASSWORD_ENCRYPTOR: &str = "http://schemas.microsoft.com/office/2006/keyEncryptor/password"; @@ -165,6 +168,14 @@ impl Hash { Hash::Sha512 => run::(parts), } } + + /// The bytes the algorithm's compression function takes: 128 for SHA-384/512, else 64. + fn block_size(self) -> usize { + match self { + Hash::Sha384 | Hash::Sha512 => 128, + Hash::Sha1 | Hash::Sha256 => 64, + } + } } /// H0 = H(salt ‖ UTF-16LE(password)), then Hn = H(LE32(n - 1) ‖ Hn-1) for `spin` rounds. @@ -184,6 +195,19 @@ pub(crate) fn derive(hash: Hash, a: &[u8], b: &[u8], len: usize) -> Vec { k } +/// HMAC-SHA-`hash` ([RFC2104]), as Agile's dataIntegrity uses. +pub(crate) fn hmac(hash: Hash, key: &[u8], message: &[u8]) -> Vec { + let bs = hash.block_size(); + let mut k = key.to_vec(); + if k.len() > bs { + k = hash.digest(&[&k]); + } + k.resize(bs, 0); + let pad = |x: u8| k.iter().map(|b| b ^ x).collect::>(); + let inner = hash.digest(&[&pad(0x36), message]); + hash.digest(&[&pad(0x5C), &inner]) +} + /// The Standard encryption key ([MS-OFFCRYPTO] 2.3.4.7): SHA-1 iterated 50,000 times, the /// block-0 hash, then the 0x36 / 0x5C expansion, truncated to `key_len`. pub(crate) fn standard_key(password: &str, salt: &[u8], key_len: usize) -> Vec { @@ -368,8 +392,9 @@ impl Params { } } -/// What an Agile descriptor holds: the package's cipher, and the password key encryptor with its -/// three encrypted values (verifier input, verifier hash, package key). +/// What an Agile descriptor holds: the package's cipher, the password key encryptor with its +/// three encrypted values (verifier input, verifier hash, package key), and the package's +/// integrity pair, which Excel always writes and the reader requires. struct AgileDescriptor { key_data: Params, key: Params, @@ -377,6 +402,8 @@ struct AgileDescriptor { verifier_input: Vec, verifier_hash: Vec, key_value: Vec, + hmac_key: Vec, + hmac_value: Vec, } /// `xml` is the XML encryption descriptor after the 8-byte version and reserved field. @@ -395,6 +422,16 @@ fn agile_descriptor(xml: &[u8]) -> Result { return Err(malformed(format!("a spin count of {spin}, more than the {MAX_SPIN_COUNT} allowed"))); } let field = |name: &str| base64(encryptor.attr(name).ok_or_else(|| malformed(format!("no {name}")))?); + // A descriptor without dataIntegrity can't prove its ciphertext is the one the password + // protects: the verifier only authenticates the key, not the package ([MS-OFFCRYPTO] 2.3.4.14). + let integrity = root.child("dataIntegrity").ok_or_else(|| malformed("no dataIntegrity"))?; + let hmac_field = |attr: &str| { + let v = base64(integrity.attr(attr).ok_or_else(|| malformed(format!("no {attr}")))?)?; + if v.is_empty() || v.len() % BLOCK != 0 { + return Err(malformed(format!("a whole-block {attr}"))); + } + Ok(v) + }; Ok(AgileDescriptor { key_data, key, @@ -402,11 +439,14 @@ fn agile_descriptor(xml: &[u8]) -> Result { verifier_input: field("encryptedVerifierHashInput")?, verifier_hash: field("encryptedVerifierHashValue")?, key_value: field("encryptedKeyValue")?, + hmac_key: hmac_field("encryptedHmacKey")?, + hmac_value: hmac_field("encryptedHmacValue")?, }) } fn agile(xml: &[u8], package: &[u8], password: &str) -> Result, DecryptError> { - let AgileDescriptor { key_data, key, spin, verifier_input, verifier_hash, key_value } = agile_descriptor(xml)?; + let descriptor = agile_descriptor(xml)?; + let AgileDescriptor { key_data, key, spin, verifier_input, verifier_hash, key_value, hmac_key, hmac_value } = descriptor; let h = iterated_hash(key.hash, password, &key.salt, spin); let iv = key.iv(None); let field = |data: &[u8], block_key: &[u8]| -> Result, DecryptError> { @@ -423,6 +463,18 @@ fn agile(xml: &[u8], package: &[u8], password: &str) -> Result, DecryptE } let secret = field(&key_value, &BLOCK_KEY)?; let aes = Aes::new(secret.get(..key_data.key_len).ok_or_else(|| malformed("a short encryptedKeyValue"))?)?; + // Package integrity, [MS-OFFCRYPTO] 2.3.4.14: both HMAC fields are encrypted with the + // package key (the verifier above only authenticates the password), keyed by a random + // salt-size array, computed over the whole EncryptedPackage stream, StreamSize field included. + let mut mac_field = hmac_key; + aes.decrypt_cbc(&key_data.iv(Some(&BLOCK_HMAC_KEY)), &mut mac_field)?; + mac_field.truncate(key_data.salt.len()); + let mut declared = hmac_value; + aes.decrypt_cbc(&key_data.iv(Some(&BLOCK_HMAC_VALUE)), &mut declared)?; + let mac = hmac(key_data.hash, &mac_field, package); + if declared.get(..mac.len()) != Some(mac.as_slice()) { + return Err(malformed("the decrypted data failed its integrity check")); + } let (size, data) = package_parts(package)?; let mut out = Vec::with_capacity(size); for (i, segment) in data.chunks(SEGMENT).enumerate() { diff --git a/crates/xlsx/src/crypto/tests.rs b/crates/xlsx/src/crypto/tests.rs index 0fb0b921be..5f14686e94 100644 --- a/crates/xlsx/src/crypto/tests.rs +++ b/crates/xlsx/src/crypto/tests.rs @@ -6,6 +6,9 @@ use super::*; use crate::testing::{agile_info, encrypt_agile, encrypt_standard, with_agile_header}; const PASSWORD: &str = "Pass1234"; +/// 16 zero bytes (whole AES blocks), a stand-in for a real integrity field where the values +/// can't be reached anyway (scheme errors, descriptor-parse damage). +const HMAC_STUB: &str = "AAAAAAAAAAAAAAAAAAAAAA=="; fn hex(s: &str) -> Vec { (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect() @@ -107,9 +110,9 @@ fn unsupported_schemes_are_named() { assert_eq!(unsupported(&rc4), "RC4"); assert_eq!(unsupported(&[3, 0, 3, 0, 0, 0, 0, 0]), "Extensible"); assert_eq!(unsupported(&[1, 0, 1, 0, 0, 0, 0, 0]), "version 1.1"); - let des = agile_info(PASSWORD, 1000).replace("cipherAlgorithm=\"AES\"", "cipherAlgorithm=\"3DES\""); + let des = agile_info(PASSWORD, 1000, HMAC_STUB, HMAC_STUB).replace("cipherAlgorithm=\"AES\"", "cipherAlgorithm=\"3DES\""); assert_eq!(unsupported(&with_agile_header(&des)), "3DES"); - let cert = agile_info(PASSWORD, 1000).replace("keyEncryptor/password", "keyEncryptor/certificate"); + let cert = agile_info(PASSWORD, 1000, HMAC_STUB, HMAC_STUB).replace("keyEncryptor/password", "keyEncryptor/certificate"); assert_eq!(unsupported(&with_agile_header(&cert)), "certificate"); } @@ -136,7 +139,7 @@ fn damaged_files_are_malformed_not_panics() { ragged[..8].copy_from_slice(&len.to_le_bytes()); assert!(is_malformed(decrypt_streams(&info, &ragged, PASSWORD))); } - let xml = agile_info(PASSWORD, 1000); + let xml = agile_info(PASSWORD, 1000, HMAC_STUB, HMAC_STUB); let bad = |s: String| is_malformed(decrypt_streams(&with_agile_header(&s), &[0; 8], PASSWORD)); assert!(bad(xml.replace("spinCount=\"1000\"", "spinCount=\"4000000000\""))); assert!(bad(xml.replace("saltValue=\"", "saltValue=\"*"))); @@ -155,3 +158,76 @@ fn non_ascii_passwords_hash_as_utf16le_like_msoffcrypto() { hex("2bca96ca0a7e8d88d9314a2eb1819a1ece94ff9ce34266ff82f891a88c4240c4c9f39c729b7c5c410c27fc8e2b7ad90d8c005eb68a603377b8a4ab858041c76a") ); } + +#[test] +fn hmac_matches_the_rfc2104_test_vectors() { + use crate::crypto::hmac as mac; + let h = |v: Vec| v.iter().map(|b| format!("{b:02x}")).collect::(); + // RFC 2202 test case 1 (HMAC-SHA-1) and HMAC-SHA-256 vectors. + assert_eq!(h(mac(Hash::Sha1, &[0x0b; 20], b"Hi There")), "b617318655057264e28bc0b6fb378c8ef146be00"); + assert_eq!(h(mac(Hash::Sha256, &[0x0b; 20], b"Hi There")), "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"); + // RFC 4231 test case 5 (HMAC-SHA-256): a key longer than the block size is hashed first. + assert_eq!( + h(mac(Hash::Sha256, &[0xaa; 131], b"Test Using Larger Than Block-Size Key - Hash Key First")), + "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54" + ); +} + +/// Strips the `` element from an Agile descriptor. +fn without_integrity(info: &[u8]) -> Vec { + let xml = std::str::from_utf8(&info[8..]).unwrap().to_string(); + let start = xml.find("").unwrap() + 2; + let stripped = format!("{}{}", &xml[..start], &xml[end..]); + with_agile_header(&stripped) +} + +#[test] +fn integrity_is_required_and_enforced() { + let plain = payload(300); + let (info, package) = encrypt_agile(&plain, PASSWORD, 1000); + // An attacker can't just delete the integrity element to get the package verified. + assert!(matches!(decrypt_streams(&without_integrity(&info), &package, PASSWORD), + Err(DecryptError::Malformed(m)) if m.contains("dataIntegrity"))); + // An empty or ragged field is damage, not a pass. + for bad in [("encryptedHmacKey", ""), ("encryptedHmacKey", "AA=="), ("encryptedHmacValue", "")] { + let xml = + agile_info(PASSWORD, 1000, HMAC_STUB, HMAC_STUB).replace(&format!("{}=\"{HMAC_STUB}\"", bad.0), &format!("{}=\"{}\"", bad.0, bad.1)); + let r = decrypt_streams(&with_agile_header(&xml), &package, PASSWORD); + assert!(matches!(r, Err(DecryptError::Malformed(_))), "{}=\"{}\"", bad.0, bad.1); + } +} + +#[test] +fn modified_ciphertext_fails_the_integrity_check() { + let plain = payload(300); + let (info, package) = encrypt_agile(&plain, PASSWORD, 1000); + // The correct password proves the verifier can't vouch for a modified package. + for at in [8usize, 100, package.len() - 5] { + let mut tampered = package.clone(); + tampered[at] ^= 0x80; + assert!( + matches!(decrypt_streams(&info, &tampered, PASSWORD), + Err(DecryptError::Malformed(m)) if m.contains("integrity")), + "byte {at}" + ); + } + // And neither can the descriptor: a swapped integrity value is refused. + let xml = String::from_utf8_lossy(&info[8..]).to_string(); + let at = xml.find("encryptedHmacValue=\"").unwrap() + "encryptedHmacValue=\"".len(); + let mut xml: Vec = xml.into_bytes(); + xml[at] ^= b'A' ^ b'B'; + assert!(matches!(decrypt_streams(&with_agile_header(std::str::from_utf8(&xml).unwrap()), &package, PASSWORD), Err(DecryptError::Malformed(_)))); +} + +#[test] +fn integrity_matches_the_stream_not_the_plaintext() { + // The MAC covers the whole EncryptedPackage stream, StreamSize field included: changing the + // stated size alone must fail, while the actual bytes round-trip. + let plain = payload(4200); + let (info, package) = encrypt_agile(&plain, PASSWORD, 1000); + let mut ours = package.clone(); + ours[..8].copy_from_slice(&((plain.len() + 1) as u64).to_le_bytes()); + assert!(matches!(decrypt_streams(&info, &ours, PASSWORD), Err(DecryptError::Malformed(m)) if m.contains("integrity"))); + assert_eq!(decrypt_streams(&info, &package, PASSWORD).unwrap(), plain); +} diff --git a/crates/xlsx/src/testing.rs b/crates/xlsx/src/testing.rs index 348c08af56..5729250bb6 100644 --- a/crates/xlsx/src/testing.rs +++ b/crates/xlsx/src/testing.rs @@ -9,8 +9,8 @@ use aes::cipher::{BlockCipherEncrypt, KeyInit}; use crate::crypto::{ - BLOCK_KEY, BLOCK_VERIFIER_HASH, BLOCK_VERIFIER_INPUT, F_AES, F_CRYPTOAPI, Hash, PASSWORD_ENCRYPTOR, Params, SEGMENT, derive, iterated_hash, - standard_key, + BLOCK_HMAC_KEY, BLOCK_HMAC_VALUE, BLOCK_KEY, BLOCK_VERIFIER_HASH, BLOCK_VERIFIER_INPUT, F_AES, F_CRYPTOAPI, Hash, PASSWORD_ENCRYPTOR, Params, + SEGMENT, derive, hmac, iterated_hash, standard_key, }; /// The spin count `encrypt_workbook` uses: Excel's default. @@ -21,6 +21,7 @@ const KEY_DATA_SALT: [u8; 16] = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, const SECRET_KEY: [u8; 32] = [64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95]; const VERIFIER: [u8; 16] = [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]; +const HMAC_SALT: [u8; 16] = [112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127]; /// `package` (normally an `.xlsx` zip) as Excel stores it with a password: Agile encryption /// (AES-256, SHA-512) inside a compound file. @@ -202,8 +203,9 @@ fn package(size: usize, body: Vec) -> Vec { p } -/// The Agile XML descriptor: AES-256, SHA-512, a password encryptor salted with 0..16. -pub(crate) fn agile_info(password: &str, spin: u32) -> String { +/// The Agile XML descriptor: AES-256, SHA-512, a password encryptor salted with 0..16, and the +/// dataIntegrity pair (`hmac_key_b64`/`hmac_value_b64`) a compliant file carries. +pub(crate) fn agile_info(password: &str, spin: u32, hmac_key_b64: &str, hmac_value_b64: &str) -> String { let key = Params { hash: Hash::Sha512, salt: PASSWORD_SALT.to_vec(), key_len: 32 }; let h = iterated_hash(Hash::Sha512, password, &key.salt, spin); let iv = key.iv(None); @@ -217,9 +219,10 @@ pub(crate) fn agile_info(password: &str, spin: u32) -> String { base64_encode(salt) ) }; + // The dataIntegrity pair ([MS-OFFCRYPTO] 2.3.4.14), encrypted with the package key. format!( - "\r\n", - params(&KEY_DATA_SALT), + "\r\n", + params(KEY_DATA_SALT.as_slice()), params(&PASSWORD_SALT), ) } @@ -231,14 +234,20 @@ pub(crate) fn with_agile_header(xml: &str) -> Vec { info } -/// `EncryptionInfo` and `EncryptedPackage` for `plain`, Agile (AES-256, SHA-512). +/// `EncryptionInfo` and `EncryptedPackage` for `plain`, Agile (AES-256, SHA-512), with the +/// dataIntegrity HMAC over the whole EncryptedPackage stream as Excel writes it. pub(crate) fn encrypt_agile(plain: &[u8], password: &str, spin: u32) -> (Vec, Vec) { let key_data = Params { hash: Hash::Sha512, salt: KEY_DATA_SALT.to_vec(), key_len: 32 }; let mut body = vec![]; for (i, segment) in plain.chunks(SEGMENT).enumerate() { body.extend(encrypt_cbc(&SECRET_KEY, &key_data.iv(Some(&(i as u32).to_le_bytes())), segment)); } - (with_agile_header(&agile_info(password, spin)), package(plain.len(), body)) + let stream = package(plain.len(), body); + let (hmac_key_b64, hmac_value_b64) = ( + base64_encode(&encrypt_cbc(&SECRET_KEY, &key_data.iv(Some(&BLOCK_HMAC_KEY)), &HMAC_SALT)), + base64_encode(&encrypt_cbc(&SECRET_KEY, &key_data.iv(Some(&BLOCK_HMAC_VALUE)), &hmac(Hash::Sha512, &HMAC_SALT, &stream))), + ); + (with_agile_header(&agile_info(password, spin, &hmac_key_b64, &hmac_value_b64)), stream) } /// `EncryptionInfo` and `EncryptedPackage` for `plain`, Standard (AES-128, SHA-1). diff --git a/crates/xlsx/src/tests/encrypted_tests.rs b/crates/xlsx/src/tests/encrypted_tests.rs index 7a1a9bb828..525ed91939 100644 --- a/crates/xlsx/src/tests/encrypted_tests.rs +++ b/crates/xlsx/src/tests/encrypted_tests.rs @@ -56,7 +56,8 @@ fn schemes_no_password_opens_are_named_before_asking_for_one() { let file = |info: &[u8]| compound_file(&[("EncryptionInfo", info), ("EncryptedPackage", &[0; 8])]); assert_eq!(check_encryption(&file(&[3, 0, 2, 0, 4, 0, 0, 0])), unsupported("RC4")); assert_eq!(check_encryption(&file(&[4, 0, 3, 0, 0, 0, 0, 0])), unsupported("Extensible")); - let cert = agile_info("pw", 1000).replace("keyEncryptor/password", "keyEncryptor/certificate"); + let cert = + agile_info("pw", 1000, "AAAAAAAAAAAAAAAAAAAAAA==", "AAAAAAAAAAAAAAAAAAAAAA==").replace("keyEncryptor/password", "keyEncryptor/certificate"); assert_eq!(check_encryption(&file(&with_agile_header(&cert))), unsupported("certificate")); assert!(matches!(check_encryption(&file(&[4, 0])), Err(DecryptError::Malformed(_)))); // Rights management (IRM) keeps an EncryptedPackage but no EncryptionInfo. From 276cd0fe78f543f6f5e5f82a4906970237a47aaf Mon Sep 17 00:00:00 2001 From: Arie Nugraha Date: Sat, 24 Oct 2026 13:00:37 +0000 Subject: [PATCH 198/687] web: parse ?chrome= so an embedding host can leave out the title bar and File tab #1877 added Chrome to SheetApp for hosts that embed the sheet natively, but the web build never set it: an iframe host that owns the files still draws the title bar it duplicates, and Backstage's Open/Save As can replace the document behind the host's back. Parse the flag where the other URL flags are parsed; the UI already honours Chrome, so nothing else changes. Co-Authored-By: Claude Code --- apps/gridcraft-web/src/main.rs | 5 +++-- apps/gridcraft-web/src/web.rs | 11 +++++++++++ packaging/web/README.md | 4 ++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/gridcraft-web/src/main.rs b/apps/gridcraft-web/src/main.rs index 36f3f3eca3..68bee92e6f 100644 --- a/apps/gridcraft-web/src/main.rs +++ b/apps/gridcraft-web/src/main.rs @@ -11,8 +11,9 @@ //! URL query flags: `?webgl` forces WebGL2; `?sample=sales|budget|grades` opens a sample; //! `?host=parent` (inside an `