M1.3: engine workspace, file and buffer commands, event stream
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>
This commit is contained in:
parent
a63ab4ac45
commit
f1976c7d67
30 changed files with 2235 additions and 46 deletions
219
crates/engine/src/cmd/buffer.rs
Normal file
219
crates/engine/src/cmd/buffer.rs
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
//! Buffer commands: `buffer.get`, `buffer.edit`, `buffer.close`.
|
||||
//!
|
||||
//! Edits use UTF-16 code unit offsets, as CodeMirror and LSP do, and follow
|
||||
//! CodeMirror's change-set semantics: every `{from, to}` of a batch refers
|
||||
//! to the text at `baseVersion`, ranges must not overlap, and the batch is
|
||||
//! applied atomically. An edit based on an older version is rejected with
|
||||
//! `stale_version`; the client fetches the text with `buffer.get` and
|
||||
//! reapplies its pending changes.
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::params::parse;
|
||||
use crate::buffers::{BufferId, LineEndingName};
|
||||
use crate::workspace::path_string;
|
||||
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
|
||||
|
||||
/// Most edits accepted in one `buffer.edit`.
|
||||
pub const MAX_EDITS: usize = 100_000;
|
||||
|
||||
/// Most inserted bytes accepted in one `buffer.edit`.
|
||||
pub const MAX_INSERT_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
/// The full description of buffer `id`, as returned by `file.open` and
|
||||
/// `buffer.get`.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::Failed`] for an unknown buffer.
|
||||
pub fn describe(state: &State, command: &str, id: BufferId) -> Result<Value, EngineError> {
|
||||
let open = state
|
||||
.buffers
|
||||
.get(id)
|
||||
.ok_or_else(|| EngineError::Failed(format!("{command}: no buffer {id}")))?;
|
||||
Ok(json!({
|
||||
"id": id,
|
||||
"path": path_string(&open.path),
|
||||
"text": open.buffer.text(),
|
||||
"version": open.version,
|
||||
"dirty": open.dirty(),
|
||||
"lineEnding": LineEndingName::from(open.buffer.line_ending()),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Params naming one buffer.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct IdParams {
|
||||
/// Buffer id.
|
||||
id: BufferId,
|
||||
}
|
||||
|
||||
/// `buffer.get`: the text and state of a buffer.
|
||||
pub struct Get;
|
||||
|
||||
impl Command for Get {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "buffer.get",
|
||||
label: "Get Buffer",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "{ id: number }; returns { id, path, text, version, dirty, lineEnding }",
|
||||
}
|
||||
}
|
||||
|
||||
fn enabled(&self, state: &State) -> bool {
|
||||
state.has_project()
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||
let p: IdParams = parse(self.spec().id, params)?;
|
||||
describe(state, self.spec().id, p.id).map(Outcome::value)
|
||||
}
|
||||
}
|
||||
|
||||
/// One change of a `buffer.edit` batch.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Change {
|
||||
/// Start, in UTF-16 code units of the text at the base version.
|
||||
from: usize,
|
||||
/// End (exclusive), same units; equal to `from` for an insertion.
|
||||
to: usize,
|
||||
/// Text to insert; empty for a deletion.
|
||||
#[serde(default)]
|
||||
insert: String,
|
||||
}
|
||||
|
||||
/// Params of `buffer.edit`.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields, rename_all = "camelCase")]
|
||||
struct EditParams {
|
||||
/// Buffer id.
|
||||
id: BufferId,
|
||||
/// The version the changes are based on.
|
||||
base_version: u64,
|
||||
/// The changes.
|
||||
edits: Vec<Change>,
|
||||
}
|
||||
|
||||
/// `buffer.edit`: applies a batch of changes.
|
||||
pub struct Edit;
|
||||
|
||||
impl Command for Edit {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "buffer.edit",
|
||||
label: "Edit Buffer",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "{ id: number, baseVersion: number, edits: [{ from, to, insert }] } with UTF-16 offsets into the text at baseVersion; returns { id, version, dirty }; fails with stale_version if baseVersion is not the current version",
|
||||
}
|
||||
}
|
||||
|
||||
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: EditParams = parse(command, params)?;
|
||||
if p.edits.len() > MAX_EDITS {
|
||||
return Err(EngineError::invalid_params(
|
||||
command,
|
||||
format!("at most {MAX_EDITS} edits per call"),
|
||||
));
|
||||
}
|
||||
let inserted: usize = p.edits.iter().map(|c| c.insert.len()).sum();
|
||||
if inserted > MAX_INSERT_BYTES {
|
||||
return Err(EngineError::invalid_params(
|
||||
command,
|
||||
format!("at most {MAX_INSERT_BYTES} inserted bytes per call"),
|
||||
));
|
||||
}
|
||||
let open = state
|
||||
.buffers
|
||||
.get_mut(p.id)
|
||||
.ok_or_else(|| EngineError::Failed(format!("{command}: no buffer {}", p.id)))?;
|
||||
if p.base_version != open.version {
|
||||
return Err(EngineError::StaleVersion {
|
||||
id: p.id,
|
||||
base: p.base_version,
|
||||
current: open.version,
|
||||
});
|
||||
}
|
||||
if p.edits.is_empty() {
|
||||
let (version, dirty) = (open.version, open.dirty());
|
||||
return Ok(Outcome::value(
|
||||
json!({ "id": p.id, "version": version, "dirty": dirty }),
|
||||
));
|
||||
}
|
||||
let mut edits = Vec::with_capacity(p.edits.len());
|
||||
for change in p.edits {
|
||||
let invalid =
|
||||
|e: lutra_text::TextError| EngineError::invalid_params(command, e.to_string());
|
||||
let from = open.buffer.utf16_to_offset(change.from).map_err(invalid)?;
|
||||
let to = open.buffer.utf16_to_offset(change.to).map_err(invalid)?;
|
||||
edits.push(lutra_text::Edit::replace(from..to, change.insert));
|
||||
}
|
||||
let was_dirty = open.dirty();
|
||||
open.buffer
|
||||
.apply(&edits)
|
||||
.map_err(|e| EngineError::invalid_params(command, e.to_string()))?;
|
||||
open.version = open.version.saturating_add(1);
|
||||
let (version, dirty) = (open.version, open.dirty());
|
||||
state.emit(Event::BufferChanged { id: p.id, version });
|
||||
if dirty != was_dirty {
|
||||
state.emit(Event::DirtyChanged { id: p.id, dirty });
|
||||
}
|
||||
Ok(Outcome::value(
|
||||
json!({ "id": p.id, "version": version, "dirty": dirty }),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Params of `buffer.close`.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct CloseParams {
|
||||
/// Buffer id.
|
||||
id: BufferId,
|
||||
/// Discard unsaved changes.
|
||||
#[serde(default)]
|
||||
force: bool,
|
||||
}
|
||||
|
||||
/// `buffer.close`: closes a buffer.
|
||||
pub struct Close;
|
||||
|
||||
impl Command for Close {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "buffer.close",
|
||||
label: "Close Buffer",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "{ id: number, force?: bool (discard unsaved changes) }; returns null; fails with unsaved_changes if the buffer is dirty and force is not set",
|
||||
}
|
||||
}
|
||||
|
||||
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: CloseParams = parse(command, params)?;
|
||||
let open = state
|
||||
.buffers
|
||||
.get(p.id)
|
||||
.ok_or_else(|| EngineError::Failed(format!("{command}: no buffer {}", p.id)))?;
|
||||
if open.dirty() && !p.force {
|
||||
return Err(EngineError::UnsavedChanges(path_string(&open.path)));
|
||||
}
|
||||
state.buffers.remove(p.id);
|
||||
state.emit(Event::BufferClosed { id: p.id });
|
||||
Ok(Outcome::value(Value::Null))
|
||||
}
|
||||
}
|
||||
152
crates/engine/src/cmd/file.rs
Normal file
152
crates/engine/src/cmd/file.rs
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
//! 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 })))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
//! Built-in commands, one module per area.
|
||||
|
||||
mod app;
|
||||
mod buffer;
|
||||
mod file;
|
||||
mod params;
|
||||
mod project;
|
||||
|
||||
use crate::{EngineError, Registry};
|
||||
|
||||
|
|
@ -11,5 +15,14 @@ use crate::{EngineError, Registry};
|
|||
pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
|
||||
registry.register(Box::new(app::About))?;
|
||||
registry.register(Box::new(app::Quit))?;
|
||||
registry.register(Box::new(project::Open))?;
|
||||
registry.register(Box::new(project::Close))?;
|
||||
registry.register(Box::new(project::Children))?;
|
||||
registry.register(Box::new(file::Open))?;
|
||||
registry.register(Box::new(file::Save))?;
|
||||
registry.register(Box::new(file::SaveAll))?;
|
||||
registry.register(Box::new(buffer::Get))?;
|
||||
registry.register(Box::new(buffer::Edit))?;
|
||||
registry.register(Box::new(buffer::Close))?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
59
crates/engine/src/cmd/params.rs
Normal file
59
crates/engine/src/cmd/params.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
//! Shared helpers for command params: typed parsing and project paths.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::workspace::vfs_error;
|
||||
use crate::{EngineError, State};
|
||||
|
||||
/// Parses `params` into `T`. `null` counts as an empty object, so commands
|
||||
/// whose params are all optional accept both.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::InvalidParams`] naming `command` if the params don't match.
|
||||
pub fn parse<T: DeserializeOwned>(command: &str, params: Value) -> Result<T, EngineError> {
|
||||
let params = if params.is_null() {
|
||||
Value::Object(serde_json::Map::new())
|
||||
} else {
|
||||
params
|
||||
};
|
||||
serde_json::from_value(params).map_err(|e| EngineError::invalid_params(command, e.to_string()))
|
||||
}
|
||||
|
||||
/// Resolves a path from a client to a project-relative path: relative paths
|
||||
/// (with `/` or the platform separator) are taken as they are, absolute ones
|
||||
/// must lie inside the project. The VFS rejects `..` and other escapes when
|
||||
/// the path is used.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::Disabled`] without a project, [`EngineError::Failed`] for
|
||||
/// an absolute path outside the project.
|
||||
pub fn project_path(state: &State, command: &str, path: &str) -> Result<PathBuf, EngineError> {
|
||||
let workspace = state
|
||||
.project
|
||||
.as_ref()
|
||||
.ok_or_else(|| EngineError::Disabled(command.to_string()))?;
|
||||
let given = Path::new(path);
|
||||
if given.is_absolute() {
|
||||
return workspace
|
||||
.project
|
||||
.relative(given)
|
||||
.ok_or_else(|| EngineError::Failed(format!("{path} is outside the project")));
|
||||
}
|
||||
Ok(given.components().collect())
|
||||
}
|
||||
|
||||
/// Resolves a project-relative path to an absolute one.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::Disabled`] without a project, [`EngineError::Failed`] if
|
||||
/// the path escapes the project.
|
||||
pub fn absolute(state: &State, command: &str, rel: &Path) -> Result<PathBuf, EngineError> {
|
||||
let workspace = state
|
||||
.project
|
||||
.as_ref()
|
||||
.ok_or_else(|| EngineError::Disabled(command.to_string()))?;
|
||||
workspace.project.resolve(rel).map_err(vfs_error)
|
||||
}
|
||||
197
crates/engine/src/cmd/project.rs
Normal file
197
crates/engine/src/cmd/project.rs
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
//! Project commands: `project.open`, `project.close`, `project.children`.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::params::{parse, project_path};
|
||||
use crate::workspace::{Workspace, path_string, vfs_error};
|
||||
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
|
||||
|
||||
/// Closes the open project and all its buffers.
|
||||
///
|
||||
/// # Errors
|
||||
/// [`EngineError::UnsavedChanges`] if a buffer is dirty and `force` is false.
|
||||
pub fn close_project(state: &mut State, force: bool) -> Result<(), EngineError> {
|
||||
if state.project.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
let dirty = state.buffers.dirty_ids();
|
||||
if !force && let Some(first) = dirty.first() {
|
||||
let name = state
|
||||
.buffers
|
||||
.get(*first)
|
||||
.map(|b| path_string(&b.path))
|
||||
.unwrap_or_default();
|
||||
let what = if dirty.len() == 1 {
|
||||
name
|
||||
} else {
|
||||
format!("{name} and {} more files", dirty.len() - 1)
|
||||
};
|
||||
return Err(EngineError::UnsavedChanges(what));
|
||||
}
|
||||
for id in state.buffers.ids() {
|
||||
state.buffers.remove(id);
|
||||
state.emit(Event::BufferClosed { id });
|
||||
}
|
||||
state.project = None;
|
||||
state.emit(Event::ProjectClosed);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Params of `project.open`.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct OpenParams {
|
||||
/// Folder to open.
|
||||
path: String,
|
||||
/// Discard unsaved changes of a project that is open.
|
||||
#[serde(default)]
|
||||
force: bool,
|
||||
}
|
||||
|
||||
/// `project.open`: opens a folder as the project, closing the current one.
|
||||
pub struct Open;
|
||||
|
||||
impl Command for Open {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "project.open",
|
||||
label: "Open Folder",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "{ path: string (absolute folder), force?: bool (discard unsaved changes of the open project) }; returns { root, name }",
|
||||
}
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||
let p: OpenParams = parse(self.spec().id, params)?;
|
||||
let (workspace, events) = Workspace::open(std::path::Path::new(&p.path))?;
|
||||
close_project(state, p.force)?;
|
||||
let root = workspace.project.root().to_path_buf();
|
||||
state.project = Some(workspace);
|
||||
let mut name = String::new();
|
||||
for event in events {
|
||||
if let Event::ProjectOpened { name: n, .. } = &event {
|
||||
name = n.clone();
|
||||
}
|
||||
state.emit(event);
|
||||
}
|
||||
Ok(Outcome::value(json!({
|
||||
"root": root.to_string_lossy(),
|
||||
"name": name,
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
/// Params of `project.close`.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct CloseParams {
|
||||
/// Discard unsaved changes.
|
||||
#[serde(default)]
|
||||
force: bool,
|
||||
}
|
||||
|
||||
/// `project.close`: closes the project and its buffers.
|
||||
pub struct Close;
|
||||
|
||||
impl Command for Close {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "project.close",
|
||||
label: "Close Project",
|
||||
menu_path: Some("File/Close Project"),
|
||||
default_shortcut: None,
|
||||
params: "{ force?: bool (discard unsaved changes) }; returns null; fails with unsaved_changes if a buffer is dirty and force is not set",
|
||||
}
|
||||
}
|
||||
|
||||
fn enabled(&self, state: &State) -> bool {
|
||||
state.has_project()
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||
let p: CloseParams = parse(self.spec().id, params)?;
|
||||
close_project(state, p.force)?;
|
||||
Ok(Outcome::value(Value::Null))
|
||||
}
|
||||
}
|
||||
|
||||
/// Params of `project.children`.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ChildrenParams {
|
||||
/// Directory, project-relative; `""` or missing for the root.
|
||||
#[serde(default)]
|
||||
dir: String,
|
||||
}
|
||||
|
||||
/// One entry of a `project.children` result.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct EntryOut {
|
||||
/// Display name.
|
||||
name: String,
|
||||
/// Project-relative path.
|
||||
path: String,
|
||||
/// `file`, `directory` or `other`.
|
||||
kind: &'static str,
|
||||
/// Matched by an ignore rule.
|
||||
ignored: bool,
|
||||
/// `normal`, `library` or `excluded`.
|
||||
role: &'static str,
|
||||
}
|
||||
|
||||
/// `project.children`: lists one directory and watches it for changes.
|
||||
pub struct Children;
|
||||
|
||||
impl Command for Children {
|
||||
fn spec(&self) -> &CommandSpec {
|
||||
&CommandSpec {
|
||||
id: "project.children",
|
||||
label: "List Directory",
|
||||
menu_path: None,
|
||||
default_shortcut: None,
|
||||
params: "{ dir?: string (project-relative, \"\" = root) }; returns { dir, entries: [{ name, path, kind: file|directory|other, ignored, role: normal|library|excluded }], truncated }",
|
||||
}
|
||||
}
|
||||
|
||||
fn enabled(&self, state: &State) -> bool {
|
||||
state.has_project()
|
||||
}
|
||||
|
||||
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
||||
let p: ChildrenParams = parse(self.spec().id, params)?;
|
||||
let dir = project_path(state, self.spec().id, &p.dir)?;
|
||||
let workspace = state
|
||||
.project
|
||||
.as_mut()
|
||||
.ok_or_else(|| EngineError::Disabled(self.spec().id.to_string()))?;
|
||||
let listing = workspace.project.children(&dir).map_err(vfs_error)?;
|
||||
workspace.touch_dir(&dir);
|
||||
let entries: Vec<EntryOut> = listing
|
||||
.entries
|
||||
.into_iter()
|
||||
.map(|e| EntryOut {
|
||||
name: e.name,
|
||||
path: path_string(&e.path),
|
||||
kind: match e.kind {
|
||||
lutra_vfs::EntryKind::File => "file",
|
||||
lutra_vfs::EntryKind::Directory => "directory",
|
||||
lutra_vfs::EntryKind::Other => "other",
|
||||
},
|
||||
ignored: e.ignored,
|
||||
role: match e.role {
|
||||
lutra_vfs::EntryRole::Normal => "normal",
|
||||
lutra_vfs::EntryRole::Library => "library",
|
||||
lutra_vfs::EntryRole::Excluded => "excluded",
|
||||
},
|
||||
})
|
||||
.collect();
|
||||
Ok(Outcome::value(json!({
|
||||
"dir": path_string(&dir),
|
||||
"entries": entries,
|
||||
"truncated": listing.truncated,
|
||||
})))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue