State holds the open project (VFS project, watcher, watched directories)
and versioned buffers. New commands: project.open/close/children,
file.open/save/saveAll, buffer.get/edit/close. buffer.edit takes UTF-16
change sets based on a version and fails with the new stale_version code;
closing dirty buffers or projects without force fails with
unsaved_changes. Commands queue events (projectOpened, treeChanged,
bufferChanged, dirtyChanged, fileChangedOnDisk, ...) that the engine
returns in Outcome.events; Engine::pump_vfs maps changes on disk to
events and reloads clean buffers. project.children watches listed
directories, capped at 256 native / 32 polling, least recently listed
dropped first.
Control channel: events.subscribe streams {"event": ...} lines, with
bounded per-subscriber queues (lutra_control::Subscribers) and method docs
in methods.rs. testkit's EngineBackend and the desktop Host implement the
new Backend::subscribe. Verified with 14 engine integration tests, a TCP
events test and the running app over the control channel.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
4.7 KiB
Rust
152 lines
4.7 KiB
Rust
//! File commands: `file.open`, `file.save`, `file.saveAll`.
|
|
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
|
|
use super::buffer::describe;
|
|
use super::params::{absolute, parse, project_path};
|
|
use crate::buffers::{BufferId, OpenBuffer};
|
|
use crate::workspace::{path_string, vfs_error};
|
|
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
|
|
|
|
/// Params of `file.open`.
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct OpenParams {
|
|
/// Project-relative or absolute path of the file.
|
|
path: String,
|
|
}
|
|
|
|
/// `file.open`: opens a file into a buffer, or returns the buffer that
|
|
/// already holds it.
|
|
pub struct Open;
|
|
|
|
impl Command for Open {
|
|
fn spec(&self) -> &CommandSpec {
|
|
&CommandSpec {
|
|
id: "file.open",
|
|
label: "Open File",
|
|
menu_path: None,
|
|
default_shortcut: None,
|
|
params: "{ path: string (project-relative or absolute inside the project) }; returns { id, path, text, version, dirty, lineEnding: lf|crlf|cr }",
|
|
}
|
|
}
|
|
|
|
fn enabled(&self, state: &State) -> bool {
|
|
state.has_project()
|
|
}
|
|
|
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
|
let id = self.spec().id;
|
|
let p: OpenParams = parse(id, params)?;
|
|
let rel = project_path(state, id, &p.path)?;
|
|
if let Some(existing) = state.buffers.find(&rel) {
|
|
return describe(state, id, existing).map(Outcome::value);
|
|
}
|
|
let abs = absolute(state, id, &rel)?;
|
|
let file = lutra_vfs::read_text(&abs).map_err(vfs_error)?;
|
|
let buffer = OpenBuffer {
|
|
buffer: lutra_text::Buffer::new(&file.text),
|
|
path: rel.clone(),
|
|
version: 1,
|
|
saved_version: 1,
|
|
bom: file.bom,
|
|
};
|
|
let new_id = state.buffers.insert(buffer);
|
|
state.emit(Event::BufferOpened {
|
|
id: new_id,
|
|
path: path_string(&rel),
|
|
});
|
|
describe(state, id, new_id).map(Outcome::value)
|
|
}
|
|
}
|
|
|
|
/// Writes buffer `id` to its file, keeping its line ending and byte order
|
|
/// mark, and marks it clean.
|
|
///
|
|
/// # Errors
|
|
/// [`EngineError::Failed`] for an unknown buffer or a failed write.
|
|
fn save(state: &mut State, command: &str, id: BufferId) -> Result<u64, EngineError> {
|
|
let open = state
|
|
.buffers
|
|
.get(id)
|
|
.ok_or_else(|| EngineError::Failed(format!("no buffer {id}")))?;
|
|
let abs = absolute(state, command, &open.path)?;
|
|
lutra_vfs::write_text(&abs, &open.buffer.save_text(), open.bom).map_err(vfs_error)?;
|
|
let open = state
|
|
.buffers
|
|
.get_mut(id)
|
|
.ok_or_else(|| EngineError::Failed(format!("no buffer {id}")))?;
|
|
let was_dirty = open.dirty();
|
|
open.saved_version = open.version;
|
|
let version = open.version;
|
|
if was_dirty {
|
|
state.emit(Event::DirtyChanged { id, dirty: false });
|
|
}
|
|
Ok(version)
|
|
}
|
|
|
|
/// Params of `file.save`.
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct SaveParams {
|
|
/// Buffer id.
|
|
id: BufferId,
|
|
}
|
|
|
|
/// `file.save`: writes one buffer to disk.
|
|
pub struct Save;
|
|
|
|
impl Command for Save {
|
|
fn spec(&self) -> &CommandSpec {
|
|
&CommandSpec {
|
|
id: "file.save",
|
|
label: "Save",
|
|
menu_path: Some("File/Save"),
|
|
default_shortcut: Some("Mod+S"),
|
|
params: "{ id: number }; returns { id, version }",
|
|
}
|
|
}
|
|
|
|
fn enabled(&self, state: &State) -> bool {
|
|
state.has_project()
|
|
}
|
|
|
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
|
let p: SaveParams = parse(self.spec().id, params)?;
|
|
let version = save(state, self.spec().id, p.id)?;
|
|
Ok(Outcome::value(json!({ "id": p.id, "version": version })))
|
|
}
|
|
}
|
|
|
|
/// `file.saveAll`: writes every dirty buffer. A failure doesn't stop the
|
|
/// others; failures are listed in the result.
|
|
pub struct SaveAll;
|
|
|
|
impl Command for SaveAll {
|
|
fn spec(&self) -> &CommandSpec {
|
|
&CommandSpec {
|
|
id: "file.saveAll",
|
|
label: "Save All",
|
|
menu_path: Some("File/Save All"),
|
|
default_shortcut: Some("Mod+Alt+S"),
|
|
params: "none; returns { saved: [id], failed: [{ id, message }] }",
|
|
}
|
|
}
|
|
|
|
fn enabled(&self, state: &State) -> bool {
|
|
state.has_project()
|
|
}
|
|
|
|
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
|
|
let mut saved = Vec::new();
|
|
let mut failed = Vec::new();
|
|
for id in state.buffers.dirty_ids() {
|
|
match save(state, self.spec().id, id) {
|
|
Ok(_) => saved.push(id),
|
|
Err(e) => failed.push(json!({ "id": id, "message": e.to_string() })),
|
|
}
|
|
}
|
|
Ok(Outcome::value(json!({ "saved": saved, "failed": failed })))
|
|
}
|
|
}
|