deploy.browse, readRemote, writeRemote, renameRemote, mkdirRemote, deleteRemote (confirm: true required), compare, sync (confirmDelete required for deletions). deploy.upload saves dirty buffers under its paths first (saveFirst, default true) without queueing uploads on save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
223 lines
7.7 KiB
Rust
223 lines
7.7 KiB
Rust
//! File commands: `file.open`, `file.save`, `file.saveAll`.
|
|
//!
|
|
//! `file.saveAll {skipConflicts: true}` is the engine half of "save every
|
|
//! dirty buffer and wait" (M3.10): autosave and anything that must see the
|
|
//! files on disk first (deployment, run configurations, terminal commands)
|
|
//! call it through the frontend's `flushDirty()`. It extends `file.saveAll`
|
|
//! instead of adding a command, so there is one save-all with one result
|
|
//! shape; the flag only adds the rule that a buffer in conflict with its file
|
|
//! on disk is skipped, because only the user can settle that.
|
|
|
|
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::journal;
|
|
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 and emits
|
|
/// [`Event::BufferOpened`], or returns the buffer that already holds it and
|
|
/// emits [`Event::BufferFocused`] so clients switch to 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 }; emits bufferOpened for a new buffer, bufferFocused for one already open",
|
|
}
|
|
}
|
|
|
|
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) {
|
|
let value = describe(state, id, existing)?;
|
|
state.emit(Event::BufferFocused { id: existing });
|
|
return Ok(Outcome::value(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
|
|
.buffers
|
|
.note_written(new_id, journal::hex_hash(file.text.as_bytes()), file.bom);
|
|
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, marks it clean, clears a disk conflict (saving is the user's answer
|
|
/// to it) and remembers what was written, so the watcher's report of this
|
|
/// write is not taken for an outside change.
|
|
///
|
|
/// # 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)?;
|
|
let text = open.buffer.save_text();
|
|
let bom = open.bom;
|
|
lutra_vfs::write_text(&abs, &text, bom).map_err(vfs_error)?;
|
|
state
|
|
.buffers
|
|
.note_written(id, journal::hex_hash(text.as_bytes()), bom);
|
|
state.buffers.clear_conflict(id);
|
|
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;
|
|
let path = open.path.clone();
|
|
journal::forget(state, id);
|
|
if was_dirty {
|
|
state.emit(Event::DirtyChanged { id, dirty: false });
|
|
}
|
|
super::deploy::saved(state, &path.to_string_lossy().replace('\\', "/"));
|
|
Ok(version)
|
|
}
|
|
|
|
/// Writes buffer `id` to disk like `file.save`, for other commands (such as
|
|
/// `deploy.upload`, which saves before it uploads).
|
|
///
|
|
/// # Errors
|
|
/// Like `file.save`.
|
|
pub(super) fn save_buffer(
|
|
state: &mut State,
|
|
command: &str,
|
|
id: BufferId,
|
|
) -> Result<u64, EngineError> {
|
|
save(state, command, id)
|
|
}
|
|
|
|
/// 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 })))
|
|
}
|
|
}
|
|
|
|
/// Params of `file.saveAll`.
|
|
#[derive(Deserialize, Default)]
|
|
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
|
struct SaveAllParams {
|
|
/// Leave buffers in conflict with their file on disk unsaved and list
|
|
/// them under `skipped`.
|
|
#[serde(default)]
|
|
skip_conflicts: bool,
|
|
}
|
|
|
|
/// `file.saveAll`: writes every dirty buffer. A failure doesn't stop the
|
|
/// others; failures are listed in the result. With `skipConflicts` a dirty
|
|
/// buffer whose file changed or disappeared on disk is not written and is
|
|
/// listed under `skipped`.
|
|
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: "{ skipConflicts?: boolean (leave buffers whose file changed on disk unsaved) }; returns { saved: [id], failed: [{ id, path, message }], skipped: [id] }",
|
|
}
|
|
}
|
|
|
|
fn enabled(&self, state: &State) -> bool {
|
|
state.has_project()
|
|
}
|
|
|
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
|
let p: SaveAllParams = parse(self.spec().id, params)?;
|
|
let mut saved = Vec::new();
|
|
let mut failed = Vec::new();
|
|
let mut skipped = Vec::new();
|
|
for id in state.buffers.dirty_ids() {
|
|
if p.skip_conflicts && state.buffers.has_conflict(id) {
|
|
skipped.push(id);
|
|
continue;
|
|
}
|
|
match save(state, self.spec().id, id) {
|
|
Ok(_) => saved.push(id),
|
|
Err(e) => {
|
|
let path = state
|
|
.buffers
|
|
.get(id)
|
|
.map(|b| path_string(&b.path))
|
|
.unwrap_or_default();
|
|
failed.push(json!({ "id": id, "path": path, "message": e.to_string() }));
|
|
}
|
|
}
|
|
}
|
|
Ok(Outcome::value(
|
|
json!({ "saved": saved, "failed": failed, "skipped": skipped }),
|
|
))
|
|
}
|
|
}
|
|
|
|
/// 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] = &["file.open", "file.save", "file.saveAll"];
|