A1: layout.get/layout.set keep the editor tab layout per project; editor.tabs.preview setting

The tab layout of the open project lives in <data_dir>/layout/<project-id>.json
(written atomically, 256 KiB cap both ways, a broken file reads as null),
never in webview storage. New bool setting editor.tabs.preview (default on).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-08 12:18:28 +01:00
parent 1c6c67194a
commit 02922eb89d
6 changed files with 295 additions and 0 deletions

View file

@ -0,0 +1,139 @@
//! `layout.get` and `layout.set` (C10 A1): the editor tab layout of the
//! open project, kept in Lutra's own state file
//! `<data_dir>/layout/<project-id>.json` (the project id is the hash the
//! crash-recovery journal uses), never in the webview's storage.
//!
//! The engine treats the layout as an opaque JSON object: the frontend
//! writes it and reads it back defensively. Its size is capped by
//! [`MAX_BYTES`] both ways. An engine without persistence keeps nothing.
use std::path::PathBuf;
use serde::Deserialize;
use serde_json::{Map, Value, json};
use super::params::parse;
use crate::journal::hex_hash;
use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State};
/// Directory below the data directory that holds the layout files.
const DIR_NAME: &str = "layout";
/// Largest layout file read or written, in bytes.
pub const MAX_BYTES: usize = 256 * 1024;
/// Registers `layout.get` and `layout.set`.
///
/// # Errors
/// [`EngineError::Internal`] if an id is taken.
pub fn register(registry: &mut Registry) -> Result<(), EngineError> {
registry.register(Box::new(Get))?;
registry.register(Box::new(Set))
}
/// The layout file of the open project, `None` without a project or
/// without persistence.
fn file(state: &State) -> Option<PathBuf> {
let root = state.project.as_ref()?.project.root();
let config = state.config.as_ref()?;
Some(config.data_dir.join(DIR_NAME).join(format!(
"{}.json",
hex_hash(root.to_string_lossy().as_bytes())
)))
}
/// Reads the stored layout. A missing, oversized or unreadable file gives
/// `None`: the layout is a convenience, so a broken file only means the
/// tabs start empty, and the next `layout.set` replaces it.
fn read(state: &State) -> Option<Map<String, Value>> {
let path = file(state)?;
let meta = std::fs::metadata(&path).ok()?;
if usize::try_from(meta.len()).map_or(true, |len| len > MAX_BYTES) {
return None;
}
let bytes = std::fs::read(&path).ok()?;
match serde_json::from_slice::<Value>(&bytes).ok()? {
Value::Object(map) => Some(map),
_ => None,
}
}
/// `layout.get`.
struct Get;
impl Command for Get {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "layout.get",
label: "Editor: Read Tab Layout",
menu_path: None,
default_shortcut: None,
params: "no params; returns { layout: object | null }, the editor tab layout last stored for the open project (null when none was stored, the file is unreadable or the engine keeps no files)",
}
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
let layout = read(state).map_or(Value::Null, Value::Object);
Ok(Outcome::value(json!({ "layout": layout })))
}
}
/// Params of `layout.set`.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct SetParams {
/// The layout, an object the frontend defines.
layout: Map<String, Value>,
}
/// `layout.set`.
struct Set;
impl Command for Set {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "layout.set",
label: "Editor: Store Tab Layout",
menu_path: None,
default_shortcut: None,
params: "{ layout: object } (at most 256 KiB as JSON); stores the editor tab layout of the open project in Lutra's state file; returns { stored: bool }, false when the engine keeps no files",
}
}
fn enabled(&self, state: &State) -> bool {
state.has_project()
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let command = self.spec().id;
let p: SetParams = parse(command, params)?;
let text = serde_json::to_vec(&Value::Object(p.layout))
.map_err(|e| EngineError::Internal(format!("layout: {e}")))?;
if text.len() > MAX_BYTES {
return Err(EngineError::invalid_params(
command,
format!("layout is larger than {MAX_BYTES} bytes"),
));
}
let Some(path) = file(state) else {
return Ok(Outcome::value(json!({ "stored": false })));
};
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).map_err(|e| {
EngineError::Failed(format!("cannot create {}: {e}", dir.display()))
})?;
}
lutra_vfs::write_atomic(&path, &text)
.map_err(|e| EngineError::Failed(format!("cannot store the tab layout: {e}")))?;
Ok(Outcome::value(json!({ "stored": true })))
}
}
/// The ids of the commands this module registers, checked against the
/// registry by the engine's listing test. Add a new command's id here.
#[cfg(test)]
pub(crate) const COMMAND_IDS: &[&str] = &["layout.get", "layout.set"];

View file

@ -15,6 +15,7 @@ pub mod git_merge;
pub mod git_remote;
pub mod git_stash;
pub mod goto;
mod layout;
mod lsp_assist;
mod lsp_diagnostics;
mod lsp_nav;
@ -88,6 +89,7 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
git_chunks::register(registry)?;
git_ignore::register(registry)?;
deploy::register(registry)?;
layout::register(registry)?;
Ok(())
}
@ -111,6 +113,7 @@ pub(crate) const BUILTIN_COMMAND_IDS: &[&[&str]] = &[
git_remote::COMMAND_IDS,
git_stash::COMMAND_IDS,
goto::COMMAND_IDS,
layout::COMMAND_IDS,
lsp_assist::COMMAND_IDS,
lsp_diagnostics::COMMAND_IDS,
lsp_nav::COMMAND_IDS,