PM: merge M1.3 engine workspace and events
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
69130d1efb
30 changed files with 2235 additions and 46 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -2500,6 +2500,7 @@ dependencies = [
|
||||||
"proptest",
|
"proptest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"tempfile",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -2519,8 +2520,11 @@ dependencies = [
|
||||||
name = "lutra-engine"
|
name = "lutra-engine"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"lutra-text",
|
||||||
|
"lutra-vfs",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"tempfile",
|
||||||
"thiserror 2.0.21",
|
"thiserror 2.0.21",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
|
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
|
||||||
|
|
||||||
use lutra_control::Backend;
|
use lutra_control::{Backend, EventSink, Subscribers};
|
||||||
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
|
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
|
|
@ -24,6 +24,9 @@ pub struct Host {
|
||||||
engine: Mutex<Engine>,
|
engine: Mutex<Engine>,
|
||||||
/// The Tauri app, set once the app is set up.
|
/// The Tauri app, set once the app is set up.
|
||||||
app: OnceLock<AppHandle>,
|
app: OnceLock<AppHandle>,
|
||||||
|
/// Control channel connections subscribed to engine events. Forwarding
|
||||||
|
/// events to the frontend and pumping changes on disk arrive with M1.4.
|
||||||
|
subscribers: Subscribers,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Host {
|
impl Host {
|
||||||
|
|
@ -35,6 +38,7 @@ impl Host {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
engine: Mutex::new(Engine::new()?),
|
engine: Mutex::new(Engine::new()?),
|
||||||
app: OnceLock::new(),
|
app: OnceLock::new(),
|
||||||
|
subscribers: Subscribers::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,12 +53,13 @@ impl Host {
|
||||||
self.engine.lock().unwrap_or_else(PoisonError::into_inner)
|
self.engine.lock().unwrap_or_else(PoisonError::into_inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs a command and applies its effects.
|
/// Runs a command, applies its effects and publishes its events.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// The engine's error for the command.
|
/// The engine's error for the command.
|
||||||
pub fn run(&self, command: &str, params: Value) -> Result<Value, EngineError> {
|
pub fn run(&self, command: &str, params: Value) -> Result<Value, EngineError> {
|
||||||
let outcome = self.engine().execute(command, params)?;
|
let outcome = self.engine().execute(command, params)?;
|
||||||
|
self.subscribers.publish(&outcome.events);
|
||||||
for effect in outcome.effects {
|
for effect in outcome.effects {
|
||||||
self.apply(effect);
|
self.apply(effect);
|
||||||
}
|
}
|
||||||
|
|
@ -89,4 +94,8 @@ impl Backend for Host {
|
||||||
let app = self.app.get().ok_or("the window is not open yet")?;
|
let app = self.app.get().ok_or("the window is not open yet")?;
|
||||||
screenshot::capture_window(app, MAIN_WINDOW, path)
|
screenshot::capture_window(app, MAIN_WINDOW, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscribe(&self, sink: EventSink) {
|
||||||
|
self.subscribers.add(sink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,3 +22,4 @@ workspace = true
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lutra-testkit.workspace = true
|
lutra-testkit.workspace = true
|
||||||
proptest = "1.12.0"
|
proptest = "1.12.0"
|
||||||
|
tempfile.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,27 @@
|
||||||
//! The interface the control channel needs from its host.
|
//! The interface the control channel needs from its host, and the
|
||||||
|
//! subscriber list hosts use to publish engine events.
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::sync::mpsc::{SyncSender, TrySendError};
|
||||||
|
use std::sync::{Mutex, PoisonError};
|
||||||
|
|
||||||
use lutra_engine::{CommandInfo, EngineError};
|
use lutra_engine::{CommandInfo, EngineError, Event};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
/// Queue of events for one subscriber. Bounded so that a client that stops
|
||||||
|
/// reading cannot make the host buffer without limit.
|
||||||
|
pub type EventSink = SyncSender<Event>;
|
||||||
|
|
||||||
|
/// Capacity of each subscriber's queue.
|
||||||
|
pub const EVENT_QUEUE: usize = 4096;
|
||||||
|
|
||||||
/// What the control channel can ask the host to do.
|
/// What the control channel can ask the host to do.
|
||||||
///
|
///
|
||||||
/// The desktop app implements it on top of its engine and window; tests
|
/// The desktop app implements it on top of its engine and window; tests
|
||||||
/// implement it with a bare engine. Methods are called from connection
|
/// implement it with a bare engine. Methods are called from connection
|
||||||
/// threads, so implementations synchronise internally.
|
/// threads, so implementations synchronise internally.
|
||||||
pub trait Backend: Send + Sync + 'static {
|
pub trait Backend: Send + Sync + 'static {
|
||||||
/// Runs an engine command and applies its effects.
|
/// Runs an engine command, applies its effects and publishes its events.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// The engine's error for the command.
|
/// The engine's error for the command.
|
||||||
|
|
@ -26,4 +36,79 @@ pub trait Backend: Send + Sync + 'static {
|
||||||
/// A message if there is no window to capture or the file cannot be
|
/// A message if there is no window to capture or the file cannot be
|
||||||
/// written.
|
/// written.
|
||||||
fn screenshot(&self, path: &Path) -> Result<(), String>;
|
fn screenshot(&self, path: &Path) -> Result<(), String>;
|
||||||
|
|
||||||
|
/// Adds a subscriber that receives every engine event from now on.
|
||||||
|
fn subscribe(&self, sink: EventSink);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The event subscribers of a host.
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Subscribers {
|
||||||
|
/// Live subscribers.
|
||||||
|
sinks: Mutex<Vec<EventSink>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Subscribers {
|
||||||
|
/// Adds a subscriber.
|
||||||
|
pub fn add(&self, sink: EventSink) {
|
||||||
|
self.sinks
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(PoisonError::into_inner)
|
||||||
|
.push(sink);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends `events` to every subscriber, in order. A subscriber whose
|
||||||
|
/// queue is full or whose receiver is gone is dropped: a full queue
|
||||||
|
/// means the client stopped reading, and dropping it keeps the host from
|
||||||
|
/// blocking or buffering without limit.
|
||||||
|
pub fn publish(&self, events: &[Event]) {
|
||||||
|
if events.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let mut sinks = self.sinks.lock().unwrap_or_else(PoisonError::into_inner);
|
||||||
|
sinks.retain(|sink| {
|
||||||
|
events
|
||||||
|
.iter()
|
||||||
|
.all(|event| match sink.try_send(event.clone()) {
|
||||||
|
Ok(()) => true,
|
||||||
|
Err(TrySendError::Full(_) | TrySendError::Disconnected(_)) => false,
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of live subscribers.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.sinks
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(PoisonError::into_inner)
|
||||||
|
.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether there is no subscriber.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.len() == 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::mpsc::sync_channel;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn publishes_in_order_and_drops_dead_or_slow_subscribers() {
|
||||||
|
let subs = Subscribers::default();
|
||||||
|
let (fast_tx, fast_rx) = sync_channel(10);
|
||||||
|
let (slow_tx, _slow_rx) = sync_channel(1);
|
||||||
|
let (dead_tx, dead_rx) = sync_channel(10);
|
||||||
|
subs.add(fast_tx);
|
||||||
|
subs.add(slow_tx);
|
||||||
|
subs.add(dead_tx);
|
||||||
|
drop(dead_rx);
|
||||||
|
subs.publish(&[Event::ProjectClosed, Event::BufferClosed { id: 1 }]);
|
||||||
|
assert_eq!(subs.len(), 1);
|
||||||
|
assert_eq!(fast_rx.try_recv().unwrap(), Event::ProjectClosed);
|
||||||
|
assert_eq!(fast_rx.try_recv().unwrap(), Event::BufferClosed { id: 1 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,14 @@
|
||||||
//! Serves JSON lines on `127.0.0.1:PORT` so that scripts and agents can run
|
//! Serves JSON lines on `127.0.0.1:PORT` so that scripts and agents can run
|
||||||
//! engine commands and inspect the user interface. The protocol is described
|
//! engine commands and inspect the user interface. The protocol is described
|
||||||
//! in docs/architecture.md › Control channel; [`protocol`] parses and answers
|
//! in docs/architecture.md › Control channel; [`protocol`] parses and answers
|
||||||
//! single lines, [`server`] owns the sockets. Hosts plug in through
|
//! single lines, [`server`] owns the sockets, [`methods`] lists the
|
||||||
//! [`Backend`].
|
//! methods. Hosts plug in through [`Backend`] and publish engine events to
|
||||||
|
//! subscribed connections through [`Subscribers`].
|
||||||
|
|
||||||
mod backend;
|
mod backend;
|
||||||
|
pub mod methods;
|
||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|
||||||
pub use backend::Backend;
|
pub use backend::{Backend, EVENT_QUEUE, EventSink, Subscribers};
|
||||||
pub use server::{ServerHandle, start};
|
pub use server::{ServerHandle, start};
|
||||||
|
|
|
||||||
57
crates/control/src/methods.rs
Normal file
57
crates/control/src/methods.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
//! The methods of the control channel: names and documentation.
|
||||||
|
//!
|
||||||
|
//! Every request is `{"id", "method", "params"}` and gets one response line
|
||||||
|
//! (see [`crate::protocol`]). After `events.subscribe` the connection also
|
||||||
|
//! receives event lines `{"event": {...}}` between responses; each line is
|
||||||
|
//! written whole, so lines never interleave.
|
||||||
|
|
||||||
|
/// `app.ping`: liveness check.
|
||||||
|
pub const APP_PING: &str = "app.ping";
|
||||||
|
/// `engine.execute`: runs an engine command.
|
||||||
|
pub const ENGINE_EXECUTE: &str = "engine.execute";
|
||||||
|
/// `engine.listCommands`: lists the engine's commands.
|
||||||
|
pub const ENGINE_LIST_COMMANDS: &str = "engine.listCommands";
|
||||||
|
/// `ui.screenshot`: writes a PNG of the main window.
|
||||||
|
pub const UI_SCREENSHOT: &str = "ui.screenshot";
|
||||||
|
/// `events.subscribe`: streams engine events on this connection.
|
||||||
|
pub const EVENTS_SUBSCRIBE: &str = "events.subscribe";
|
||||||
|
|
||||||
|
/// Documentation of one method.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct MethodDoc {
|
||||||
|
/// Method name.
|
||||||
|
pub name: &'static str,
|
||||||
|
/// Params.
|
||||||
|
pub params: &'static str,
|
||||||
|
/// Result.
|
||||||
|
pub result: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every method, for help output and agents.
|
||||||
|
pub const METHODS: &[MethodDoc] = &[
|
||||||
|
MethodDoc {
|
||||||
|
name: APP_PING,
|
||||||
|
params: "none",
|
||||||
|
result: "{ pong: true, version }",
|
||||||
|
},
|
||||||
|
MethodDoc {
|
||||||
|
name: ENGINE_EXECUTE,
|
||||||
|
params: "{ command: string, params?: object }",
|
||||||
|
result: "the command's result; errors carry the engine's code (unknown_command, disabled, invalid_params, stale_version, unsaved_changes, failed, internal)",
|
||||||
|
},
|
||||||
|
MethodDoc {
|
||||||
|
name: ENGINE_LIST_COMMANDS,
|
||||||
|
params: "none",
|
||||||
|
result: "[{ id, label, menuPath, defaultShortcut, params, enabled }]",
|
||||||
|
},
|
||||||
|
MethodDoc {
|
||||||
|
name: UI_SCREENSHOT,
|
||||||
|
params: "{ path: string }",
|
||||||
|
result: "{ path }; error screenshot_failed if the window cannot be captured",
|
||||||
|
},
|
||||||
|
MethodDoc {
|
||||||
|
name: EVENTS_SUBSCRIBE,
|
||||||
|
params: "none",
|
||||||
|
result: "{ subscribed: true }; then {\"event\": {kind, ...}} lines for every engine event until the connection closes. A subscriber that falls more than 4096 events behind is dropped and gets an {\"event\": {\"kind\": \"subscriptionLost\"}} line if possible",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
@ -13,6 +13,9 @@ use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{Value, json};
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
use crate::Backend;
|
use crate::Backend;
|
||||||
|
use crate::methods::{
|
||||||
|
APP_PING, ENGINE_EXECUTE, ENGINE_LIST_COMMANDS, EVENTS_SUBSCRIBE, UI_SCREENSHOT,
|
||||||
|
};
|
||||||
|
|
||||||
/// Largest accepted request line in bytes, newline excluded.
|
/// Largest accepted request line in bytes, newline excluded.
|
||||||
pub const MAX_LINE_BYTES: usize = 16 * 1024 * 1024;
|
pub const MAX_LINE_BYTES: usize = 16 * 1024 * 1024;
|
||||||
|
|
@ -158,28 +161,33 @@ pub fn handle_line(line: &str, backend: &dyn Backend) -> Response {
|
||||||
fn dispatch(request: Request, backend: &dyn Backend) -> Response {
|
fn dispatch(request: Request, backend: &dyn Backend) -> Response {
|
||||||
let Request { id, method, params } = request;
|
let Request { id, method, params } = request;
|
||||||
match method.as_str() {
|
match method.as_str() {
|
||||||
"app.ping" => Response::ok(
|
APP_PING => Response::ok(
|
||||||
id,
|
id,
|
||||||
json!({ "pong": true, "version": env!("CARGO_PKG_VERSION") }),
|
json!({ "pong": true, "version": env!("CARGO_PKG_VERSION") }),
|
||||||
),
|
),
|
||||||
"engine.execute" => match serde_json::from_value::<ExecuteParams>(params) {
|
ENGINE_EXECUTE => match serde_json::from_value::<ExecuteParams>(params) {
|
||||||
Ok(p) => match backend.execute(&p.command, p.params) {
|
Ok(p) => match backend.execute(&p.command, p.params) {
|
||||||
Ok(value) => Response::ok(id, value),
|
Ok(value) => Response::ok(id, value),
|
||||||
Err(e) => engine_error(id, &e),
|
Err(e) => engine_error(id, &e),
|
||||||
},
|
},
|
||||||
Err(e) => Response::err(id, codes::INVALID_PARAMS, e.to_string()),
|
Err(e) => Response::err(id, codes::INVALID_PARAMS, e.to_string()),
|
||||||
},
|
},
|
||||||
"engine.listCommands" => match serde_json::to_value(backend.list_commands()) {
|
ENGINE_LIST_COMMANDS => match serde_json::to_value(backend.list_commands()) {
|
||||||
Ok(list) => Response::ok(id, list),
|
Ok(list) => Response::ok(id, list),
|
||||||
Err(e) => Response::err(id, "internal", e.to_string()),
|
Err(e) => Response::err(id, "internal", e.to_string()),
|
||||||
},
|
},
|
||||||
"ui.screenshot" => match serde_json::from_value::<ScreenshotParams>(params) {
|
UI_SCREENSHOT => match serde_json::from_value::<ScreenshotParams>(params) {
|
||||||
Ok(p) => match backend.screenshot(&p.path) {
|
Ok(p) => match backend.screenshot(&p.path) {
|
||||||
Ok(()) => Response::ok(id, json!({ "path": p.path })),
|
Ok(()) => Response::ok(id, json!({ "path": p.path })),
|
||||||
Err(message) => Response::err(id, codes::SCREENSHOT_FAILED, message),
|
Err(message) => Response::err(id, codes::SCREENSHOT_FAILED, message),
|
||||||
},
|
},
|
||||||
Err(e) => Response::err(id, codes::INVALID_PARAMS, e.to_string()),
|
Err(e) => Response::err(id, codes::INVALID_PARAMS, e.to_string()),
|
||||||
},
|
},
|
||||||
|
EVENTS_SUBSCRIBE => Response::err(
|
||||||
|
id,
|
||||||
|
codes::INVALID_REQUEST,
|
||||||
|
"events.subscribe needs a connection to stream on",
|
||||||
|
),
|
||||||
other => Response::err(
|
other => Response::err(
|
||||||
id,
|
id,
|
||||||
codes::METHOD_NOT_FOUND,
|
codes::METHOD_NOT_FOUND,
|
||||||
|
|
@ -234,12 +242,20 @@ mod tests {
|
||||||
}
|
}
|
||||||
Err("no window".into())
|
Err("no window".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscribe(&self, _sink: crate::EventSink) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(line: &str) -> Response {
|
fn call(line: &str) -> Response {
|
||||||
handle_line(line, &TestBackend::new())
|
handle_line(line, &TestBackend::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subscribe_needs_a_connection() {
|
||||||
|
let r = call(r#"{"id":1,"method":"events.subscribe"}"#);
|
||||||
|
assert_eq!(r.error.unwrap().code, "invalid_request");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ping() {
|
fn ping() {
|
||||||
let r = call(r#"{"id":1,"method":"app.ping"}"#);
|
let r = call(r#"{"id":1,"method":"app.ping"}"#);
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,30 @@
|
||||||
//! The TCP side: a listener on the loopback interface, one thread per
|
//! The TCP side: a listener on the loopback interface, one thread per
|
||||||
//! connection, JSON lines in and out.
|
//! connection, JSON lines in and out.
|
||||||
|
//!
|
||||||
|
//! A connection that sent `events.subscribe` also gets a forwarder thread
|
||||||
|
//! that writes event lines. Responses and events share one writer behind a
|
||||||
|
//! lock and each line is written whole, so lines never interleave. The
|
||||||
|
//! forwarder stops when the connection closes or the host drops the
|
||||||
|
//! subscription.
|
||||||
|
|
||||||
use std::io::{self, BufRead, BufReader, Read, Write};
|
use std::io::{self, BufRead, BufReader, Read, Write};
|
||||||
use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream};
|
use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream};
|
||||||
use std::sync::Arc;
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::sync::mpsc::{Receiver, RecvTimeoutError, sync_channel};
|
||||||
|
use std::sync::{Arc, Mutex, PoisonError};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use serde_json::Value;
|
use lutra_engine::Event;
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
use crate::Backend;
|
use crate::Backend;
|
||||||
use crate::protocol::{MAX_LINE_BYTES, Response, codes, handle_line};
|
use crate::backend::EVENT_QUEUE;
|
||||||
|
use crate::methods::EVENTS_SUBSCRIBE;
|
||||||
|
use crate::protocol::{MAX_LINE_BYTES, Response, codes, handle_line, parse_request};
|
||||||
|
|
||||||
|
/// How often an idle forwarder checks whether its connection closed.
|
||||||
|
const FORWARDER_TICK: Duration = Duration::from_millis(250);
|
||||||
|
|
||||||
/// A running control server.
|
/// A running control server.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -54,6 +69,22 @@ fn accept_loop(listener: &TcpListener, backend: &Arc<dyn Backend>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The write side of a connection, shared by the reader and the forwarder.
|
||||||
|
type SharedWriter = Arc<Mutex<TcpStream>>;
|
||||||
|
|
||||||
|
/// Writes one line, holding the lock for the whole line.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// The write error.
|
||||||
|
fn write_line(writer: &SharedWriter, line: &str) -> io::Result<()> {
|
||||||
|
let mut stream = writer.lock().unwrap_or_else(PoisonError::into_inner);
|
||||||
|
let mut bytes = Vec::with_capacity(line.len() + 1);
|
||||||
|
bytes.extend_from_slice(line.as_bytes());
|
||||||
|
bytes.push(b'\n');
|
||||||
|
stream.write_all(&bytes)?;
|
||||||
|
stream.flush()
|
||||||
|
}
|
||||||
|
|
||||||
/// Serves one connection until the peer closes it or sends an oversized
|
/// Serves one connection until the peer closes it or sends an oversized
|
||||||
/// line. I/O errors end the connection; the caller ignores them because the
|
/// line. I/O errors end the connection; the caller ignores them because the
|
||||||
/// peer is gone either way.
|
/// peer is gone either way.
|
||||||
|
|
@ -61,9 +92,26 @@ fn accept_loop(listener: &TcpListener, backend: &Arc<dyn Backend>) {
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// The first read or write error.
|
/// The first read or write error.
|
||||||
fn serve_connection(stream: TcpStream, backend: &dyn Backend) -> io::Result<()> {
|
fn serve_connection(stream: TcpStream, backend: &dyn Backend) -> io::Result<()> {
|
||||||
let mut writer = stream.try_clone()?;
|
let writer: SharedWriter = Arc::new(Mutex::new(stream.try_clone()?));
|
||||||
|
let closed = Arc::new(AtomicBool::new(false));
|
||||||
|
let result = read_loop(stream, backend, &writer, &closed);
|
||||||
|
closed.store(true, Ordering::Relaxed);
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads and answers request lines.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// The first read or write error.
|
||||||
|
fn read_loop(
|
||||||
|
stream: TcpStream,
|
||||||
|
backend: &dyn Backend,
|
||||||
|
writer: &SharedWriter,
|
||||||
|
closed: &Arc<AtomicBool>,
|
||||||
|
) -> io::Result<()> {
|
||||||
let mut reader = BufReader::new(stream);
|
let mut reader = BufReader::new(stream);
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
|
let mut subscribed = false;
|
||||||
loop {
|
loop {
|
||||||
buf.clear();
|
buf.clear();
|
||||||
let limit = u64::try_from(MAX_LINE_BYTES)
|
let limit = u64::try_from(MAX_LINE_BYTES)
|
||||||
|
|
@ -83,17 +131,69 @@ fn serve_connection(stream: TcpStream, backend: &dyn Backend) -> io::Result<()>
|
||||||
codes::LINE_TOO_LONG,
|
codes::LINE_TOO_LONG,
|
||||||
format!("request lines are limited to {MAX_LINE_BYTES} bytes"),
|
format!("request lines are limited to {MAX_LINE_BYTES} bytes"),
|
||||||
);
|
);
|
||||||
writeln!(writer, "{}", response.to_line())?;
|
write_line(writer, &response.to_line())?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if buf.iter().all(u8::is_ascii_whitespace) {
|
if buf.iter().all(u8::is_ascii_whitespace) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let response = match std::str::from_utf8(&buf) {
|
let response = match std::str::from_utf8(&buf) {
|
||||||
Ok(line) => handle_line(line, backend),
|
Ok(line) => match parse_request(line) {
|
||||||
|
Ok(request) if request.method == EVENTS_SUBSCRIBE => {
|
||||||
|
if !subscribed {
|
||||||
|
subscribe(backend, writer, closed)?;
|
||||||
|
subscribed = true;
|
||||||
|
}
|
||||||
|
Response::ok(request.id, json!({ "subscribed": true }))
|
||||||
|
}
|
||||||
|
_ => handle_line(line, backend),
|
||||||
|
},
|
||||||
Err(e) => Response::err(Value::Null, codes::PARSE_ERROR, e.to_string()),
|
Err(e) => Response::err(Value::Null, codes::PARSE_ERROR, e.to_string()),
|
||||||
};
|
};
|
||||||
writeln!(writer, "{}", response.to_line())?;
|
write_line(writer, &response.to_line())?;
|
||||||
writer.flush()?;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers a subscription with the backend and starts its forwarder.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// If the forwarder thread cannot be spawned.
|
||||||
|
fn subscribe(
|
||||||
|
backend: &dyn Backend,
|
||||||
|
writer: &SharedWriter,
|
||||||
|
closed: &Arc<AtomicBool>,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
let (sink, events) = sync_channel(EVENT_QUEUE);
|
||||||
|
let writer = Arc::clone(writer);
|
||||||
|
let closed = Arc::clone(closed);
|
||||||
|
thread::Builder::new()
|
||||||
|
.name("lutra-control-events".into())
|
||||||
|
.spawn(move || forward(&events, &writer, &closed))?;
|
||||||
|
backend.subscribe(sink);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes events to the connection until it closes or the host drops the
|
||||||
|
/// subscription; in the latter case a final `subscriptionLost` line tells
|
||||||
|
/// the client. Write errors end forwarding because the peer is gone.
|
||||||
|
fn forward(events: &Receiver<Event>, writer: &SharedWriter, closed: &AtomicBool) {
|
||||||
|
loop {
|
||||||
|
if closed.load(Ordering::Relaxed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match events.recv_timeout(FORWARDER_TICK) {
|
||||||
|
Ok(event) => {
|
||||||
|
let line = json!({ "event": event }).to_string();
|
||||||
|
if write_line(writer, &line).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Timeout) => {}
|
||||||
|
Err(RecvTimeoutError::Disconnected) => {
|
||||||
|
let line = json!({ "event": { "kind": "subscriptionLost" } }).to_string();
|
||||||
|
let _ = write_line(writer, &line);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
170
crates/control/tests/events.rs
Normal file
170
crates/control/tests/events.rs
Normal file
|
|
@ -0,0 +1,170 @@
|
||||||
|
//! `events.subscribe` over a real TCP socket: engine events from commands and
|
||||||
|
//! from changes on disk arrive as `{"event": ...}` lines.
|
||||||
|
|
||||||
|
#![allow(
|
||||||
|
clippy::unwrap_used,
|
||||||
|
clippy::expect_used,
|
||||||
|
clippy::indexing_slicing,
|
||||||
|
clippy::panic
|
||||||
|
)]
|
||||||
|
|
||||||
|
use std::io::{BufRead, BufReader, Write};
|
||||||
|
use std::net::TcpStream;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use lutra_testkit::EngineBackend;
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
|
||||||
|
/// A client that keeps responses and event lines apart.
|
||||||
|
struct Client {
|
||||||
|
reader: BufReader<TcpStream>,
|
||||||
|
writer: TcpStream,
|
||||||
|
events: Vec<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client {
|
||||||
|
fn connect(addr: std::net::SocketAddr) -> Self {
|
||||||
|
let stream = TcpStream::connect(addr).unwrap();
|
||||||
|
stream
|
||||||
|
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||||
|
.unwrap();
|
||||||
|
Self {
|
||||||
|
writer: stream.try_clone().unwrap(),
|
||||||
|
reader: BufReader::new(stream),
|
||||||
|
events: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn line(&mut self) -> Value {
|
||||||
|
let mut line = String::new();
|
||||||
|
self.reader.read_line(&mut line).expect("line within 5 s");
|
||||||
|
serde_json::from_str(&line).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends a request and returns its response, collecting event lines that
|
||||||
|
/// arrive before it.
|
||||||
|
fn call(&mut self, request: Value) -> Value {
|
||||||
|
self.writer
|
||||||
|
.write_all(format!("{request}\n").as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
loop {
|
||||||
|
let line = self.line();
|
||||||
|
if line.get("event").is_some() {
|
||||||
|
self.events.push(line["event"].clone());
|
||||||
|
} else {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Waits until an event of `kind` has arrived and returns it.
|
||||||
|
fn event(&mut self, kind: &str) -> Value {
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
if let Some(at) = self.events.iter().position(|e| e["kind"] == kind) {
|
||||||
|
return self.events.remove(at);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
Instant::now() < deadline,
|
||||||
|
"no {kind} event; got {:?}",
|
||||||
|
self.events
|
||||||
|
);
|
||||||
|
let line = self.line();
|
||||||
|
if line.get("event").is_some() {
|
||||||
|
self.events.push(line["event"].clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn execute(command: &str, params: Value) -> Value {
|
||||||
|
json!({"id": command, "method": "engine.execute", "params": {"command": command, "params": params}})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn streams_command_and_disk_events() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
std::fs::write(dir.path().join("a.php"), "<?php\n").unwrap();
|
||||||
|
let backend = Arc::new(EngineBackend::new());
|
||||||
|
let server = lutra_control::start(0, backend.clone()).unwrap();
|
||||||
|
let mut client = Client::connect(server.local_addr());
|
||||||
|
|
||||||
|
let r = client.call(json!({"id": 1, "method": "events.subscribe"}));
|
||||||
|
assert_eq!(r["result"]["subscribed"], true);
|
||||||
|
let again = client.call(json!({"id": 2, "method": "events.subscribe"}));
|
||||||
|
assert_eq!(again["result"]["subscribed"], true);
|
||||||
|
assert_eq!(
|
||||||
|
backend.subscribers.len(),
|
||||||
|
1,
|
||||||
|
"subscribing twice is idempotent"
|
||||||
|
);
|
||||||
|
|
||||||
|
let r = client.call(execute("project.open", json!({"path": dir.path()})));
|
||||||
|
assert!(r["result"]["root"].is_string(), "{r}");
|
||||||
|
client.event("projectOpened");
|
||||||
|
|
||||||
|
let r = client.call(execute("file.open", json!({"path": "a.php"})));
|
||||||
|
let id = r["result"]["id"].as_u64().unwrap();
|
||||||
|
assert_eq!(client.event("bufferOpened")["id"], id);
|
||||||
|
|
||||||
|
let r = client.call(execute(
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 6, "to": 6, "insert": "echo 1;\n"}]}),
|
||||||
|
));
|
||||||
|
assert_eq!(r["result"]["version"], 2);
|
||||||
|
assert_eq!(client.event("bufferChanged")["version"], 2);
|
||||||
|
assert_eq!(client.event("dirtyChanged")["dirty"], true);
|
||||||
|
|
||||||
|
let r = client.call(execute(
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": []}),
|
||||||
|
));
|
||||||
|
assert_eq!(r["error"]["code"], "stale_version");
|
||||||
|
|
||||||
|
client.call(execute("file.save", json!({"id": id})));
|
||||||
|
assert_eq!(client.event("dirtyChanged")["dirty"], false);
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read_to_string(dir.path().join("a.php")).unwrap(),
|
||||||
|
"<?php\necho 1;\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
std::fs::write(dir.path().join("b.php"), "b").unwrap();
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
backend.pump();
|
||||||
|
client.call(json!({"id": "ping", "method": "app.ping"}));
|
||||||
|
if client.events.iter().any(|e| e["kind"] == "treeChanged") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
Instant::now() < deadline,
|
||||||
|
"no treeChanged; got {:?}",
|
||||||
|
client.events
|
||||||
|
);
|
||||||
|
std::thread::sleep(Duration::from_millis(50));
|
||||||
|
}
|
||||||
|
assert_eq!(client.event("treeChanged")["dir"], "");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_closed_connection_stops_receiving() {
|
||||||
|
let backend = Arc::new(EngineBackend::new());
|
||||||
|
let server = lutra_control::start(0, backend.clone()).unwrap();
|
||||||
|
let mut client = Client::connect(server.local_addr());
|
||||||
|
client.call(json!({"id": 1, "method": "events.subscribe"}));
|
||||||
|
assert_eq!(backend.subscribers.len(), 1);
|
||||||
|
drop(client);
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(5);
|
||||||
|
while !backend.subscribers.is_empty() {
|
||||||
|
assert!(Instant::now() < deadline, "subscriber was not dropped");
|
||||||
|
lutra_control::Backend::execute(
|
||||||
|
backend.as_ref(),
|
||||||
|
"project.open",
|
||||||
|
json!({"path": dir.path()}),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -59,7 +59,8 @@ fn serves_requests_over_tcp() {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|c| c["id"].as_str().unwrap())
|
.map(|c| c["id"].as_str().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
assert_eq!(ids, ["app.about", "app.quit"]);
|
assert_eq!(ids[..2], ["app.about", "app.quit"]);
|
||||||
|
assert!(ids.contains(&"project.open") && ids.contains(&"buffer.edit"));
|
||||||
|
|
||||||
let r = client.call(json!({
|
let r = client.call(json!({
|
||||||
"id": "about",
|
"id": "about",
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,14 @@ publish.workspace = true
|
||||||
layer = 4
|
layer = 4
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
lutra-text.workspace = true
|
||||||
|
lutra-vfs.workspace = true
|
||||||
serde = { version = "1.0.229", features = ["derive"] }
|
serde = { version = "1.0.229", features = ["derive"] }
|
||||||
serde_json = "1.0.154"
|
serde_json = "1.0.154"
|
||||||
thiserror = "2.0.21"
|
thiserror = "2.0.21"
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile.workspace = true
|
||||||
|
|
|
||||||
125
crates/engine/src/buffers.rs
Normal file
125
crates/engine/src/buffers.rs
Normal file
|
|
@ -0,0 +1,125 @@
|
||||||
|
//! Open buffers: the engine's copy of every file the user is editing.
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use lutra_text::{Buffer, LineEnding};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
/// Identifier of an open buffer, unique for the engine's lifetime.
|
||||||
|
pub type BufferId = u64;
|
||||||
|
|
||||||
|
/// One open file.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct OpenBuffer {
|
||||||
|
/// The text.
|
||||||
|
pub buffer: Buffer,
|
||||||
|
/// Project-relative path of the file.
|
||||||
|
pub path: PathBuf,
|
||||||
|
/// Incremented by every change of the text.
|
||||||
|
pub version: u64,
|
||||||
|
/// The version that matches the file on disk.
|
||||||
|
pub saved_version: u64,
|
||||||
|
/// The file started with a UTF-8 byte order mark; kept on save.
|
||||||
|
pub bom: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OpenBuffer {
|
||||||
|
/// Whether the buffer has unsaved changes.
|
||||||
|
pub fn dirty(&self) -> bool {
|
||||||
|
self.version != self.saved_version
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The open buffers by id.
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct Buffers {
|
||||||
|
/// Buffers by id.
|
||||||
|
by_id: BTreeMap<BufferId, OpenBuffer>,
|
||||||
|
/// The id the next buffer gets.
|
||||||
|
next_id: BufferId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Buffers {
|
||||||
|
/// Adds a buffer and returns its id.
|
||||||
|
pub fn insert(&mut self, buffer: OpenBuffer) -> BufferId {
|
||||||
|
self.next_id = self.next_id.saturating_add(1);
|
||||||
|
let id = self.next_id;
|
||||||
|
self.by_id.insert(id, buffer);
|
||||||
|
id
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The buffer with `id`.
|
||||||
|
pub fn get(&self, id: BufferId) -> Option<&OpenBuffer> {
|
||||||
|
self.by_id.get(&id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The buffer with `id`, mutable.
|
||||||
|
pub fn get_mut(&mut self, id: BufferId) -> Option<&mut OpenBuffer> {
|
||||||
|
self.by_id.get_mut(&id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes the buffer with `id`.
|
||||||
|
pub fn remove(&mut self, id: BufferId) -> Option<OpenBuffer> {
|
||||||
|
self.by_id.remove(&id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The id of the buffer for the project-relative `path`, if open.
|
||||||
|
pub fn find(&self, path: &Path) -> Option<BufferId> {
|
||||||
|
self.by_id
|
||||||
|
.iter()
|
||||||
|
.find(|(_, b)| b.path == path)
|
||||||
|
.map(|(id, _)| *id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ids of all buffers, ascending.
|
||||||
|
pub fn ids(&self) -> Vec<BufferId> {
|
||||||
|
self.by_id.keys().copied().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ids of the buffers with unsaved changes, ascending.
|
||||||
|
pub fn dirty_ids(&self) -> Vec<BufferId> {
|
||||||
|
self.by_id
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, b)| b.dirty())
|
||||||
|
.map(|(id, _)| *id)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Number of open buffers.
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.by_id.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether no buffer is open.
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.by_id.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes every buffer.
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.by_id.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A line ending as sent to clients: `"lf"`, `"crlf"` or `"cr"`.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum LineEndingName {
|
||||||
|
/// `\n`.
|
||||||
|
Lf,
|
||||||
|
/// `\r\n`.
|
||||||
|
Crlf,
|
||||||
|
/// `\r`.
|
||||||
|
Cr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LineEnding> for LineEndingName {
|
||||||
|
fn from(ending: LineEnding) -> Self {
|
||||||
|
match ending {
|
||||||
|
LineEnding::Lf => Self::Lf,
|
||||||
|
LineEnding::CrLf => Self::Crlf,
|
||||||
|
LineEnding::Cr => Self::Cr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
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.
|
//! Built-in commands, one module per area.
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
|
mod buffer;
|
||||||
|
mod file;
|
||||||
|
mod params;
|
||||||
|
mod project;
|
||||||
|
|
||||||
use crate::{EngineError, Registry};
|
use crate::{EngineError, Registry};
|
||||||
|
|
||||||
|
|
@ -11,5 +15,14 @@ use crate::{EngineError, Registry};
|
||||||
pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
|
pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
|
||||||
registry.register(Box::new(app::About))?;
|
registry.register(Box::new(app::About))?;
|
||||||
registry.register(Box::new(app::Quit))?;
|
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(())
|
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,
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::{EngineError, State};
|
use crate::{EngineError, Event, State};
|
||||||
|
|
||||||
/// Static metadata of a command.
|
/// Static metadata of a command.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
|
@ -37,6 +37,9 @@ pub struct Outcome {
|
||||||
pub value: Value,
|
pub value: Value,
|
||||||
/// Effects for the host to apply, in order.
|
/// Effects for the host to apply, in order.
|
||||||
pub effects: Vec<Effect>,
|
pub effects: Vec<Effect>,
|
||||||
|
/// State changes for the host to publish, oldest first. Filled by the
|
||||||
|
/// engine from the events the command queued; commands leave it empty.
|
||||||
|
pub events: Vec<Event>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Outcome {
|
impl Outcome {
|
||||||
|
|
@ -45,6 +48,7 @@ impl Outcome {
|
||||||
Self {
|
Self {
|
||||||
value,
|
value,
|
||||||
effects: Vec::new(),
|
effects: Vec::new(),
|
||||||
|
events: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,6 +57,7 @@ impl Outcome {
|
||||||
Self {
|
Self {
|
||||||
value: Value::Null,
|
value: Value::Null,
|
||||||
effects: vec![effect],
|
effects: vec![effect],
|
||||||
|
events: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,11 @@ impl Engine {
|
||||||
self.registry.register(command)
|
self.registry.register(command)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs a command inside the guard.
|
/// Runs a command inside the guard. On success the outcome carries every
|
||||||
|
/// event queued so far. When the command fails, events it queued before
|
||||||
|
/// failing (real changes, such as files saved before another failed) stay
|
||||||
|
/// queued and arrive with the next successful outcome or
|
||||||
|
/// [`Engine::take_events`].
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// [`EngineError::UnknownCommand`] for an unknown id,
|
/// [`EngineError::UnknownCommand`] for an unknown id,
|
||||||
|
|
@ -62,7 +66,31 @@ impl Engine {
|
||||||
command.run(state, params)
|
command.run(state, params)
|
||||||
});
|
});
|
||||||
state.commands_run = state.commands_run.saturating_add(1);
|
state.commands_run = state.commands_run.saturating_add(1);
|
||||||
result
|
result.map(|mut outcome| {
|
||||||
|
outcome.events = state.take_events();
|
||||||
|
outcome
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies pending changes on disk (reloads clean buffers, reports
|
||||||
|
/// changed directories and conflicts) and returns every queued event.
|
||||||
|
/// Hosts call it regularly, e.g. every 100 ms from a background thread.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`EngineError::Internal`] if handling the changes panicked; the queued
|
||||||
|
/// events stay queued.
|
||||||
|
pub fn pump_vfs(&mut self) -> Result<Vec<crate::Event>, EngineError> {
|
||||||
|
let state = &mut self.state;
|
||||||
|
guard::run(|| {
|
||||||
|
crate::workspace::pump(state);
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
Ok(state.take_events())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Takes the queued events, oldest first.
|
||||||
|
pub fn take_events(&mut self) -> Vec<crate::Event> {
|
||||||
|
self.state.take_events()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Describes every command with its current enabled state. A command whose
|
/// Describes every command with its current enabled state. A command whose
|
||||||
|
|
@ -191,11 +219,30 @@ mod tests {
|
||||||
engine.register(Box::new(Disabled)).unwrap();
|
engine.register(Box::new(Disabled)).unwrap();
|
||||||
let list = engine.list_commands();
|
let list = engine.list_commands();
|
||||||
let ids: Vec<_> = list.iter().map(|c| c.id.as_str()).collect();
|
let ids: Vec<_> = list.iter().map(|c| c.id.as_str()).collect();
|
||||||
assert_eq!(ids, vec!["app.about", "app.quit", "test.disabled"]);
|
assert_eq!(
|
||||||
let quit = &list[1];
|
ids,
|
||||||
|
vec![
|
||||||
|
"app.about",
|
||||||
|
"app.quit",
|
||||||
|
"buffer.close",
|
||||||
|
"buffer.edit",
|
||||||
|
"buffer.get",
|
||||||
|
"file.open",
|
||||||
|
"file.save",
|
||||||
|
"file.saveAll",
|
||||||
|
"project.children",
|
||||||
|
"project.close",
|
||||||
|
"project.open",
|
||||||
|
"test.disabled",
|
||||||
|
]
|
||||||
|
);
|
||||||
|
let by_id = |id: &str| list.iter().find(|c| c.id == id).unwrap();
|
||||||
|
let quit = by_id("app.quit");
|
||||||
assert_eq!(quit.default_shortcut.as_deref(), Some("Mod+Q"));
|
assert_eq!(quit.default_shortcut.as_deref(), Some("Mod+Q"));
|
||||||
assert!(quit.enabled);
|
assert!(quit.enabled);
|
||||||
assert!(!list[2].enabled);
|
assert!(!by_id("test.disabled").enabled);
|
||||||
|
assert!(by_id("project.open").enabled);
|
||||||
|
assert!(!by_id("file.open").enabled, "needs a project");
|
||||||
let json = serde_json::to_value(quit).unwrap();
|
let json = serde_json::to_value(quit).unwrap();
|
||||||
assert_eq!(json["menuPath"], "File/Quit");
|
assert_eq!(json["menuPath"], "File/Quit");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,21 @@ pub enum EngineError {
|
||||||
/// What is wrong with the params.
|
/// What is wrong with the params.
|
||||||
message: String,
|
message: String,
|
||||||
},
|
},
|
||||||
|
/// An edit was based on an older version of the buffer than the engine
|
||||||
|
/// holds. The caller resynchronises (`buffer.get`) and retries.
|
||||||
|
#[error("buffer {id} is at version {current}, the edit was based on version {base}")]
|
||||||
|
StaleVersion {
|
||||||
|
/// The buffer id.
|
||||||
|
id: u64,
|
||||||
|
/// The version the caller based its edit on.
|
||||||
|
base: u64,
|
||||||
|
/// The engine's current version.
|
||||||
|
current: u64,
|
||||||
|
},
|
||||||
|
/// The action would discard unsaved changes; repeat it with `force` to
|
||||||
|
/// discard them.
|
||||||
|
#[error("{0} has unsaved changes")]
|
||||||
|
UnsavedChanges(String),
|
||||||
/// The command ran and failed, e.g. a file could not be read.
|
/// The command ran and failed, e.g. a file could not be read.
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Failed(String),
|
Failed(String),
|
||||||
|
|
@ -38,11 +53,21 @@ impl EngineError {
|
||||||
Self::UnknownCommand(_) => "unknown_command",
|
Self::UnknownCommand(_) => "unknown_command",
|
||||||
Self::Disabled(_) => "disabled",
|
Self::Disabled(_) => "disabled",
|
||||||
Self::InvalidParams { .. } => "invalid_params",
|
Self::InvalidParams { .. } => "invalid_params",
|
||||||
|
Self::StaleVersion { .. } => "stale_version",
|
||||||
|
Self::UnsavedChanges(_) => "unsaved_changes",
|
||||||
Self::Failed(_) => "failed",
|
Self::Failed(_) => "failed",
|
||||||
Self::Internal(_) => "internal",
|
Self::Internal(_) => "internal",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shorthand for [`EngineError::InvalidParams`].
|
||||||
|
pub fn invalid_params(command: &str, message: impl Into<String>) -> Self {
|
||||||
|
Self::InvalidParams {
|
||||||
|
command: command.to_string(),
|
||||||
|
message: message.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The error as a `{ code, message }` payload for IPC and the control
|
/// The error as a `{ code, message }` payload for IPC and the control
|
||||||
/// channel.
|
/// channel.
|
||||||
pub fn payload(&self) -> ErrorPayload {
|
pub fn payload(&self) -> ErrorPayload {
|
||||||
|
|
|
||||||
103
crates/engine/src/event.rs
Normal file
103
crates/engine/src/event.rs
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
//! Events: state changes the engine pushes to its hosts.
|
||||||
|
//!
|
||||||
|
//! Commands and [`crate::Engine::pump_vfs`] queue events in the state; the
|
||||||
|
//! engine hands them to the host with each [`crate::Outcome`] and from each
|
||||||
|
//! pump. Hosts fan them out to the frontend (Tauri events) and to control
|
||||||
|
//! channel subscribers (`events.subscribe`). Events describe what changed;
|
||||||
|
//! consumers fetch data they need with commands (`project.children`,
|
||||||
|
//! `buffer.get`).
|
||||||
|
//!
|
||||||
|
//! Serialised as `{"kind": "<camelCase variant>", ...fields in camelCase}`.
|
||||||
|
//! Paths are project-relative with `/` separators, the root is `""`.
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
/// A state change.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||||
|
#[serde(
|
||||||
|
tag = "kind",
|
||||||
|
rename_all = "camelCase",
|
||||||
|
rename_all_fields = "camelCase"
|
||||||
|
)]
|
||||||
|
pub enum Event {
|
||||||
|
/// A project was opened.
|
||||||
|
ProjectOpened {
|
||||||
|
/// Absolute root path.
|
||||||
|
root: String,
|
||||||
|
/// Folder name, for titles.
|
||||||
|
name: String,
|
||||||
|
},
|
||||||
|
/// The project was closed; all buffers are gone.
|
||||||
|
ProjectClosed,
|
||||||
|
/// The children of a directory changed on disk; refetch them with
|
||||||
|
/// `project.children`.
|
||||||
|
TreeChanged {
|
||||||
|
/// The directory.
|
||||||
|
dir: String,
|
||||||
|
},
|
||||||
|
/// A buffer was opened.
|
||||||
|
BufferOpened {
|
||||||
|
/// Buffer id.
|
||||||
|
id: u64,
|
||||||
|
/// File path.
|
||||||
|
path: String,
|
||||||
|
},
|
||||||
|
/// A buffer's text changed; `version` is the new version.
|
||||||
|
BufferChanged {
|
||||||
|
/// Buffer id.
|
||||||
|
id: u64,
|
||||||
|
/// New version.
|
||||||
|
version: u64,
|
||||||
|
},
|
||||||
|
/// A buffer became dirty or clean.
|
||||||
|
DirtyChanged {
|
||||||
|
/// Buffer id.
|
||||||
|
id: u64,
|
||||||
|
/// Whether it has unsaved changes.
|
||||||
|
dirty: bool,
|
||||||
|
},
|
||||||
|
/// A buffer was closed.
|
||||||
|
BufferClosed {
|
||||||
|
/// Buffer id.
|
||||||
|
id: u64,
|
||||||
|
},
|
||||||
|
/// The file of an open buffer changed or disappeared on disk and the
|
||||||
|
/// buffer kept its text: it is dirty, or the file is gone.
|
||||||
|
FileChangedOnDisk {
|
||||||
|
/// File path.
|
||||||
|
path: String,
|
||||||
|
/// The open buffer.
|
||||||
|
id: u64,
|
||||||
|
/// The file was removed or renamed away.
|
||||||
|
removed: bool,
|
||||||
|
},
|
||||||
|
/// File watching stopped; the project keeps working without it.
|
||||||
|
WatchFailed {
|
||||||
|
/// Why.
|
||||||
|
message: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
use super::Event;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialises_with_kind_and_camel_case_fields() {
|
||||||
|
let e = Event::FileChangedOnDisk {
|
||||||
|
path: "src/a.php".into(),
|
||||||
|
id: 3,
|
||||||
|
removed: false,
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(e).unwrap(),
|
||||||
|
json!({"kind": "fileChangedOnDisk", "path": "src/a.php", "id": 3, "removed": false})
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(Event::ProjectClosed).unwrap(),
|
||||||
|
json!({"kind": "projectClosed"})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,17 +5,22 @@
|
||||||
//! [`Command`]; menus, the palette, shortcuts, the CLI, the control channel and
|
//! [`Command`]; menus, the palette, shortcuts, the CLI, the control channel and
|
||||||
//! MCP all reach it through [`Engine::execute`].
|
//! MCP all reach it through [`Engine::execute`].
|
||||||
|
|
||||||
|
mod buffers;
|
||||||
pub mod cmd;
|
pub mod cmd;
|
||||||
mod command;
|
mod command;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod event;
|
||||||
pub mod guard;
|
pub mod guard;
|
||||||
mod registry;
|
mod registry;
|
||||||
mod state;
|
mod state;
|
||||||
pub mod theme;
|
pub mod theme;
|
||||||
|
pub mod workspace;
|
||||||
|
|
||||||
|
pub use buffers::{BufferId, LineEndingName, OpenBuffer};
|
||||||
pub use command::{Command, CommandInfo, CommandSpec, Effect, Outcome};
|
pub use command::{Command, CommandInfo, CommandSpec, Effect, Outcome};
|
||||||
pub use engine::Engine;
|
pub use engine::Engine;
|
||||||
pub use error::{EngineError, ErrorPayload};
|
pub use error::{EngineError, ErrorPayload};
|
||||||
|
pub use event::Event;
|
||||||
pub use registry::Registry;
|
pub use registry::Registry;
|
||||||
pub use state::State;
|
pub use state::State;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,35 @@
|
||||||
//! Workspace state that commands read and change.
|
//! Workspace state that commands read and change.
|
||||||
|
|
||||||
|
use crate::Event;
|
||||||
|
use crate::buffers::Buffers;
|
||||||
|
use crate::workspace::Workspace;
|
||||||
|
|
||||||
/// Everything the engine knows about the running session.
|
/// Everything the engine knows about the running session.
|
||||||
///
|
|
||||||
/// Empty in milestone M0; M1 adds the open project, buffers and settings.
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
/// Number of commands that ran to completion, successful or not.
|
/// Number of commands that ran to completion, successful or not.
|
||||||
pub commands_run: u64,
|
pub commands_run: u64,
|
||||||
|
/// The open project, if any.
|
||||||
|
pub project: Option<Workspace>,
|
||||||
|
/// Open buffers. Empty whenever no project is open.
|
||||||
|
pub buffers: Buffers,
|
||||||
|
/// Events queued for the host, oldest first.
|
||||||
|
events: Vec<Event>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
/// Queues an event for the host.
|
||||||
|
pub fn emit(&mut self, event: Event) {
|
||||||
|
self.events.push(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Takes the queued events, oldest first.
|
||||||
|
pub fn take_events(&mut self) -> Vec<Event> {
|
||||||
|
std::mem::take(&mut self.events)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether a project is open.
|
||||||
|
pub fn has_project(&self) -> bool {
|
||||||
|
self.project.is_some()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
283
crates/engine/src/workspace.rs
Normal file
283
crates/engine/src/workspace.rs
Normal file
|
|
@ -0,0 +1,283 @@
|
||||||
|
//! The open project: the VFS project, its watcher, and the translation of
|
||||||
|
//! changes on disk into engine events and buffer reloads.
|
||||||
|
//!
|
||||||
|
//! # Watched directories
|
||||||
|
//! The VFS watcher is non-recursive: the root is watched from the start and
|
||||||
|
//! [`Workspace::touch_dir`] adds each directory whose children a client
|
||||||
|
//! lists. To keep the cost bounded on huge trees (a deep `vendor/`), at most
|
||||||
|
//! [`MAX_WATCHED_DIRS_NATIVE`] directories besides the root are watched with
|
||||||
|
//! native notifications, and [`MAX_WATCHED_DIRS_POLLING`] when the watcher
|
||||||
|
//! fell back to polling, where every watched directory is rescanned each
|
||||||
|
//! interval. When the limit is reached the directory listed least recently
|
||||||
|
//! is unwatched; listing it again watches it again. Changes inside an
|
||||||
|
//! unwatched directory are not reported until then.
|
||||||
|
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::mpsc::{Receiver, TryRecvError};
|
||||||
|
|
||||||
|
use lutra_text::Buffer;
|
||||||
|
use lutra_vfs::{Project, VfsError, VfsEvent, Watcher};
|
||||||
|
|
||||||
|
use crate::buffers::BufferId;
|
||||||
|
use crate::{EngineError, Event, State};
|
||||||
|
|
||||||
|
/// Directories watched besides the root with native notifications.
|
||||||
|
pub const MAX_WATCHED_DIRS_NATIVE: usize = 256;
|
||||||
|
|
||||||
|
/// Directories watched besides the root when polling.
|
||||||
|
pub const MAX_WATCHED_DIRS_POLLING: usize = 32;
|
||||||
|
|
||||||
|
/// Most VFS events handled by one pump; the rest wait for the next one.
|
||||||
|
pub const MAX_EVENTS_PER_PUMP: usize = 10_000;
|
||||||
|
|
||||||
|
/// An open project.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Workspace {
|
||||||
|
/// The VFS project.
|
||||||
|
pub project: Project,
|
||||||
|
/// The watcher, `None` if watching is unavailable or failed.
|
||||||
|
watcher: Option<Watcher>,
|
||||||
|
/// Changes on disk from the watcher.
|
||||||
|
events: Option<Receiver<VfsEvent>>,
|
||||||
|
/// Directories watched besides the root, least recently listed first.
|
||||||
|
watched: VecDeque<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Workspace {
|
||||||
|
/// Opens the folder at `path` and starts watching it. A watcher that
|
||||||
|
/// cannot start is not an error: the workspace works without it and the
|
||||||
|
/// returned events contain [`Event::WatchFailed`].
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// [`EngineError::Failed`] if the folder cannot be opened.
|
||||||
|
pub fn open(path: &Path) -> Result<(Self, Vec<Event>), EngineError> {
|
||||||
|
let project = Project::open(path).map_err(vfs_error)?;
|
||||||
|
let root = project.root().to_path_buf();
|
||||||
|
let mut events = vec![Event::ProjectOpened {
|
||||||
|
root: root.to_string_lossy().into_owned(),
|
||||||
|
name: root
|
||||||
|
.file_name()
|
||||||
|
.map(|n| n.to_string_lossy().into_owned())
|
||||||
|
.unwrap_or_else(|| root.to_string_lossy().into_owned()),
|
||||||
|
}];
|
||||||
|
let (watcher, receiver) = match project.watch() {
|
||||||
|
Ok((watcher, receiver)) => (Some(watcher), Some(receiver)),
|
||||||
|
Err(e) => {
|
||||||
|
events.push(Event::WatchFailed {
|
||||||
|
message: e.to_string(),
|
||||||
|
});
|
||||||
|
(None, None)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok((
|
||||||
|
Self {
|
||||||
|
project,
|
||||||
|
watcher,
|
||||||
|
events: receiver,
|
||||||
|
watched: VecDeque::new(),
|
||||||
|
},
|
||||||
|
events,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Records that a client listed `dir` and watches it, unwatching the
|
||||||
|
/// least recently listed directory if the limit is reached. Watching is
|
||||||
|
/// best effort: a directory the watcher refuses or cannot read is simply
|
||||||
|
/// not watched, because listing it worked and that is what the caller
|
||||||
|
/// asked for.
|
||||||
|
pub fn touch_dir(&mut self, dir: &Path) {
|
||||||
|
let Some(watcher) = &self.watcher else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
if dir.as_os_str().is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if let Some(at) = self.watched.iter().position(|d| d == dir) {
|
||||||
|
if let Some(d) = self.watched.remove(at) {
|
||||||
|
self.watched.push_back(d);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if !matches!(watcher.watch_dir(dir), Ok(true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.watched.push_back(dir.to_path_buf());
|
||||||
|
let limit = self.watch_limit();
|
||||||
|
let Some(watcher) = &self.watcher else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
while self.watched.len() > limit {
|
||||||
|
if let Some(old) = self.watched.pop_front() {
|
||||||
|
watcher.unwatch_dir(&old);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How many directories besides the root may be watched with the
|
||||||
|
/// current backend; 0 without a watcher.
|
||||||
|
pub fn watch_limit(&self) -> usize {
|
||||||
|
match &self.watcher {
|
||||||
|
Some(w) if w.is_native() => MAX_WATCHED_DIRS_NATIVE,
|
||||||
|
Some(_) => MAX_WATCHED_DIRS_POLLING,
|
||||||
|
None => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Directories watched besides the root, least recently listed first.
|
||||||
|
pub fn watched_dirs(&self) -> Vec<PathBuf> {
|
||||||
|
self.watched.iter().cloned().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the project is watched.
|
||||||
|
pub fn is_watching(&self) -> bool {
|
||||||
|
self.watcher.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Takes up to [`MAX_EVENTS_PER_PUMP`] pending changes from the watcher.
|
||||||
|
/// A failed or disconnected watcher is dropped and reported once.
|
||||||
|
fn drain(&mut self) -> (Vec<VfsEvent>, Option<String>) {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
let mut failure = None;
|
||||||
|
let Some(receiver) = &self.events else {
|
||||||
|
return (out, None);
|
||||||
|
};
|
||||||
|
while out.len() < MAX_EVENTS_PER_PUMP {
|
||||||
|
match receiver.try_recv() {
|
||||||
|
Ok(VfsEvent::Failed(e)) => {
|
||||||
|
failure = Some(e.to_string());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Ok(event) => out.push(event),
|
||||||
|
Err(TryRecvError::Empty) => break,
|
||||||
|
Err(TryRecvError::Disconnected) => {
|
||||||
|
failure = Some("the file watcher stopped".to_string());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if failure.is_some() {
|
||||||
|
self.watcher = None;
|
||||||
|
self.events = None;
|
||||||
|
self.watched.clear();
|
||||||
|
}
|
||||||
|
(out, failure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies pending changes on disk to the state: queues
|
||||||
|
/// [`Event::TreeChanged`] for affected directories, reloads clean buffers
|
||||||
|
/// whose file changed, and queues [`Event::FileChangedOnDisk`] for dirty
|
||||||
|
/// buffers and for buffers whose file disappeared.
|
||||||
|
pub fn pump(state: &mut State) {
|
||||||
|
let Some(workspace) = state.project.as_mut() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let (changes, failure) = workspace.drain();
|
||||||
|
let root = workspace.project.root().to_path_buf();
|
||||||
|
let mut dirs: Vec<PathBuf> = Vec::new();
|
||||||
|
let mut add_dir = |path: &Path| {
|
||||||
|
let dir = path.parent().map(Path::to_path_buf).unwrap_or_default();
|
||||||
|
if !dirs.contains(&dir) {
|
||||||
|
dirs.push(dir);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut modified = Vec::new();
|
||||||
|
let mut gone = Vec::new();
|
||||||
|
for change in changes {
|
||||||
|
match change {
|
||||||
|
VfsEvent::Created { path, .. } => add_dir(&path),
|
||||||
|
VfsEvent::Removed { path } => {
|
||||||
|
add_dir(&path);
|
||||||
|
gone.push(path);
|
||||||
|
}
|
||||||
|
VfsEvent::Renamed { from, to } => {
|
||||||
|
add_dir(&from);
|
||||||
|
add_dir(&to);
|
||||||
|
gone.push(from);
|
||||||
|
}
|
||||||
|
VfsEvent::Modified { path } => modified.push(path),
|
||||||
|
VfsEvent::Failed(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for dir in dirs {
|
||||||
|
state.emit(Event::TreeChanged {
|
||||||
|
dir: path_string(&dir),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for path in gone {
|
||||||
|
if let Some(id) = state.buffers.find(&path) {
|
||||||
|
state.emit(Event::FileChangedOnDisk {
|
||||||
|
path: path_string(&path),
|
||||||
|
id,
|
||||||
|
removed: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for path in modified {
|
||||||
|
if let Some(id) = state.buffers.find(&path) {
|
||||||
|
reload(state, id, &root.join(&path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(message) = failure {
|
||||||
|
state.emit(Event::WatchFailed { message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Brings buffer `id` in line with its file after the file changed: a clean
|
||||||
|
/// buffer takes the new text (unless it already matches, as after our own
|
||||||
|
/// save), a dirty one keeps its text and the client is told. A file that
|
||||||
|
/// cannot be read is reported like a removed one.
|
||||||
|
fn reload(state: &mut State, id: BufferId, abs: &Path) {
|
||||||
|
let Some(open) = state.buffers.get(id) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let path = path_string(&open.path);
|
||||||
|
if open.dirty() {
|
||||||
|
state.emit(Event::FileChangedOnDisk {
|
||||||
|
path,
|
||||||
|
id,
|
||||||
|
removed: false,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let file = match lutra_vfs::read_text(abs) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(_) => {
|
||||||
|
state.emit(Event::FileChangedOnDisk {
|
||||||
|
path,
|
||||||
|
id,
|
||||||
|
removed: true,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if file.text == open.buffer.save_text() && file.bom == open.bom {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let Some(open) = state.buffers.get_mut(id) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
open.buffer = Buffer::new(&file.text);
|
||||||
|
open.bom = file.bom;
|
||||||
|
open.version = open.version.saturating_add(1);
|
||||||
|
open.saved_version = open.version;
|
||||||
|
let version = open.version;
|
||||||
|
state.emit(Event::BufferChanged { id, version });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A project-relative path as sent to clients: `/` separators, `""` for the
|
||||||
|
/// root.
|
||||||
|
pub fn path_string(path: &Path) -> String {
|
||||||
|
let text = path.to_string_lossy();
|
||||||
|
if std::path::MAIN_SEPARATOR == '/' {
|
||||||
|
text.into_owned()
|
||||||
|
} else {
|
||||||
|
text.replace(std::path::MAIN_SEPARATOR, "/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts a VFS error into an engine error.
|
||||||
|
pub fn vfs_error(error: VfsError) -> EngineError {
|
||||||
|
EngineError::Failed(error.to_string())
|
||||||
|
}
|
||||||
445
crates/engine/tests/workspace.rs
Normal file
445
crates/engine/tests/workspace.rs
Normal file
|
|
@ -0,0 +1,445 @@
|
||||||
|
//! Workspace commands and events against real folders in temp directories.
|
||||||
|
|
||||||
|
#![allow(
|
||||||
|
clippy::unwrap_used,
|
||||||
|
clippy::expect_used,
|
||||||
|
clippy::panic,
|
||||||
|
clippy::indexing_slicing
|
||||||
|
)]
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use lutra_engine::{Command, CommandSpec, Engine, EngineError, Event, Outcome, State};
|
||||||
|
use serde_json::{Value, json};
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
|
/// A temp folder with a few files, opened in a fresh engine.
|
||||||
|
fn open_project(files: &[(&str, &[u8])]) -> (TempDir, Engine, Vec<Event>) {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
for (path, bytes) in files {
|
||||||
|
let abs = dir.path().join(path);
|
||||||
|
fs::create_dir_all(abs.parent().unwrap()).unwrap();
|
||||||
|
fs::write(abs, bytes).unwrap();
|
||||||
|
}
|
||||||
|
let mut engine = Engine::new().unwrap();
|
||||||
|
let out = engine
|
||||||
|
.execute("project.open", json!({"path": dir.path()}))
|
||||||
|
.unwrap();
|
||||||
|
(dir, engine, out.events)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(engine: &mut Engine, command: &str, params: Value) -> Outcome {
|
||||||
|
engine
|
||||||
|
.execute(command, params)
|
||||||
|
.unwrap_or_else(|e| panic!("{command} failed: {e}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_file(engine: &mut Engine, path: &str) -> (u64, Value) {
|
||||||
|
let out = run(engine, "file.open", json!({"path": path}));
|
||||||
|
(out.value["id"].as_u64().unwrap(), out.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pumps VFS events until `pred` matches one, or fails after five seconds.
|
||||||
|
fn wait_for(engine: &mut Engine, pred: impl Fn(&Event) -> bool) -> Vec<Event> {
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(5);
|
||||||
|
let mut seen = Vec::new();
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
let events = engine.pump_vfs().unwrap();
|
||||||
|
let hit = events.iter().any(&pred);
|
||||||
|
seen.extend(events);
|
||||||
|
if hit {
|
||||||
|
return seen;
|
||||||
|
}
|
||||||
|
std::thread::sleep(Duration::from_millis(25));
|
||||||
|
}
|
||||||
|
panic!("event not seen within 5 s; got {seen:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_commands_are_disabled_without_a_project() {
|
||||||
|
let mut engine = Engine::new().unwrap();
|
||||||
|
for (command, params) in [
|
||||||
|
("project.children", json!({})),
|
||||||
|
("project.close", json!({})),
|
||||||
|
("file.open", json!({"path": "a.php"})),
|
||||||
|
("file.save", json!({"id": 1})),
|
||||||
|
("file.saveAll", Value::Null),
|
||||||
|
("buffer.get", json!({"id": 1})),
|
||||||
|
(
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": 1, "baseVersion": 1, "edits": []}),
|
||||||
|
),
|
||||||
|
("buffer.close", json!({"id": 1})),
|
||||||
|
] {
|
||||||
|
let err = engine.execute(command, params).unwrap_err();
|
||||||
|
assert_eq!(err.code(), "disabled", "{command}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn opening_a_missing_folder_fails_and_keeps_state() {
|
||||||
|
let mut engine = Engine::new().unwrap();
|
||||||
|
let err = engine
|
||||||
|
.execute("project.open", json!({"path": "/definitely/not/here"}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "failed");
|
||||||
|
assert!(!engine.state().has_project());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn open_lists_children_and_reports_the_project() {
|
||||||
|
let (dir, mut engine, events) = open_project(&[
|
||||||
|
("src/Kernel.php", b"<?php\n"),
|
||||||
|
("b.txt", b"b"),
|
||||||
|
("A.txt", b"a"),
|
||||||
|
(".gitignore", b"b.txt\n"),
|
||||||
|
("vendor/x/y.php", b"<?php\n"),
|
||||||
|
]);
|
||||||
|
let root = fs::canonicalize(dir.path()).unwrap();
|
||||||
|
assert!(matches!(&events[0], Event::ProjectOpened { name, .. }
|
||||||
|
if *name == root.file_name().unwrap().to_string_lossy()));
|
||||||
|
|
||||||
|
let out = run(&mut engine, "project.children", json!({}));
|
||||||
|
let names: Vec<&str> = out.value["entries"]
|
||||||
|
.as_array()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.map(|e| e["name"].as_str().unwrap())
|
||||||
|
.collect();
|
||||||
|
assert_eq!(names, ["src", "vendor", ".gitignore", "A.txt", "b.txt"]);
|
||||||
|
let entries = out.value["entries"].as_array().unwrap();
|
||||||
|
let by_name = |n: &str| entries.iter().find(|e| e["name"] == n).unwrap();
|
||||||
|
assert_eq!(by_name("b.txt")["ignored"], true);
|
||||||
|
assert_eq!(by_name("vendor")["role"], "library");
|
||||||
|
assert_eq!(by_name("src")["kind"], "directory");
|
||||||
|
|
||||||
|
let out = run(&mut engine, "project.children", json!({"dir": "src"}));
|
||||||
|
assert_eq!(out.value["entries"][0]["path"], "src/Kernel.php");
|
||||||
|
let err = engine
|
||||||
|
.execute("project.children", json!({"dir": "../.."}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn edit_save_and_dirty_state() {
|
||||||
|
let (dir, mut engine, _) = open_project(&[("a.php", "<?php\n// 😀 x\n".as_bytes())]);
|
||||||
|
let (id, opened) = open_file(&mut engine, "a.php");
|
||||||
|
assert_eq!(opened["version"], 1);
|
||||||
|
assert_eq!(opened["dirty"], false);
|
||||||
|
assert_eq!(opened["lineEnding"], "lf");
|
||||||
|
|
||||||
|
let (again, _) = open_file(&mut engine, "a.php");
|
||||||
|
assert_eq!(again, id, "opening twice returns the same buffer");
|
||||||
|
|
||||||
|
let after_emoji = "<?php\n// 😀".encode_utf16().count();
|
||||||
|
let out = run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [
|
||||||
|
{"from": 0, "to": 5, "insert": "<?PHP"},
|
||||||
|
{"from": after_emoji, "to": after_emoji + 2, "insert": "!"}
|
||||||
|
]}),
|
||||||
|
);
|
||||||
|
assert_eq!(out.value, json!({"id": id, "version": 2, "dirty": true}));
|
||||||
|
assert_eq!(
|
||||||
|
out.events,
|
||||||
|
vec![
|
||||||
|
Event::BufferChanged { id, version: 2 },
|
||||||
|
Event::DirtyChanged { id, dirty: true }
|
||||||
|
]
|
||||||
|
);
|
||||||
|
let text = run(&mut engine, "buffer.get", json!({"id": id})).value["text"].clone();
|
||||||
|
assert_eq!(text, "<?PHP\n// 😀!\n");
|
||||||
|
|
||||||
|
let out = run(&mut engine, "file.save", json!({"id": id}));
|
||||||
|
assert_eq!(out.value["version"], 2);
|
||||||
|
assert_eq!(out.events, vec![Event::DirtyChanged { id, dirty: false }]);
|
||||||
|
assert_eq!(
|
||||||
|
fs::read_to_string(dir.path().join("a.php")).unwrap(),
|
||||||
|
"<?PHP\n// 😀!\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stale_and_invalid_edits_are_rejected_without_change() {
|
||||||
|
let (_dir, mut engine, _) = open_project(&[("a.php", "a😀b".as_bytes())]);
|
||||||
|
let (id, _) = open_file(&mut engine, "a.php");
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "x"}]}),
|
||||||
|
);
|
||||||
|
let err = engine
|
||||||
|
.execute(
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "y"}]}),
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "stale_version");
|
||||||
|
assert_eq!(
|
||||||
|
err,
|
||||||
|
EngineError::StaleVersion {
|
||||||
|
id,
|
||||||
|
base: 1,
|
||||||
|
current: 2
|
||||||
|
}
|
||||||
|
);
|
||||||
|
for edits in [
|
||||||
|
json!([{"from": 3, "to": 3, "insert": "z"}]),
|
||||||
|
json!([{"from": 0, "to": 99, "insert": ""}]),
|
||||||
|
json!([{"from": 0, "to": 2}, {"from": 1, "to": 3}]),
|
||||||
|
] {
|
||||||
|
let err = engine
|
||||||
|
.execute(
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 2, "edits": edits}),
|
||||||
|
)
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "invalid_params", "{edits}");
|
||||||
|
}
|
||||||
|
let got = run(&mut engine, "buffer.get", json!({"id": id})).value;
|
||||||
|
assert_eq!(got["text"], "xa😀b");
|
||||||
|
assert_eq!(got["version"], 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn line_endings_and_bom_survive_a_save() {
|
||||||
|
let mut bytes = vec![0xef, 0xbb, 0xbf];
|
||||||
|
bytes.extend_from_slice(b"a\r\nb\r\n");
|
||||||
|
let (dir, mut engine, _) = open_project(&[("w.php", &bytes)]);
|
||||||
|
let (id, opened) = open_file(&mut engine, "w.php");
|
||||||
|
assert_eq!(opened["lineEnding"], "crlf");
|
||||||
|
assert_eq!(opened["text"], "a\r\nb\r\n");
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 6, "to": 6, "insert": "c\n"}]}),
|
||||||
|
);
|
||||||
|
run(&mut engine, "file.save", json!({"id": id}));
|
||||||
|
let mut expected = vec![0xef, 0xbb, 0xbf];
|
||||||
|
expected.extend_from_slice(b"a\r\nb\r\nc\r\n");
|
||||||
|
assert_eq!(fs::read(dir.path().join("w.php")).unwrap(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn closing_dirty_buffers_needs_force() {
|
||||||
|
let (_dir, mut engine, _) = open_project(&[("a.php", b"a"), ("b.php", b"b")]);
|
||||||
|
let (a, _) = open_file(&mut engine, "a.php");
|
||||||
|
let (b, _) = open_file(&mut engine, "b.php");
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": a, "baseVersion": 1, "edits": [{"from": 1, "to": 1, "insert": "!"}]}),
|
||||||
|
);
|
||||||
|
let err = engine
|
||||||
|
.execute("buffer.close", json!({"id": a}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "unsaved_changes");
|
||||||
|
let out = run(&mut engine, "buffer.close", json!({"id": b}));
|
||||||
|
assert_eq!(out.events, vec![Event::BufferClosed { id: b }]);
|
||||||
|
|
||||||
|
let err = engine.execute("project.close", json!({})).unwrap_err();
|
||||||
|
assert_eq!(err.code(), "unsaved_changes");
|
||||||
|
assert!(engine.state().has_project());
|
||||||
|
let out = run(&mut engine, "project.close", json!({"force": true}));
|
||||||
|
assert_eq!(
|
||||||
|
out.events,
|
||||||
|
vec![Event::BufferClosed { id: a }, Event::ProjectClosed]
|
||||||
|
);
|
||||||
|
assert!(!engine.state().has_project());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn save_all_saves_every_dirty_buffer() {
|
||||||
|
let (dir, mut engine, _) = open_project(&[("a.php", b"a"), ("b.php", b"b")]);
|
||||||
|
let (a, _) = open_file(&mut engine, "a.php");
|
||||||
|
let (b, _) = open_file(&mut engine, "b.php");
|
||||||
|
for id in [a, b] {
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": ">"}]}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let out = run(&mut engine, "file.saveAll", Value::Null);
|
||||||
|
assert_eq!(out.value, json!({"saved": [a, b], "failed": []}));
|
||||||
|
assert_eq!(fs::read_to_string(dir.path().join("b.php")).unwrap(), ">b");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn absolute_paths_must_be_inside_the_project() {
|
||||||
|
let (dir, mut engine, _) = open_project(&[("a.php", b"a")]);
|
||||||
|
let inside = fs::canonicalize(dir.path()).unwrap().join("a.php");
|
||||||
|
let (id, value) = open_file(&mut engine, inside.to_str().unwrap());
|
||||||
|
assert_eq!(value["path"], "a.php");
|
||||||
|
assert!(id > 0);
|
||||||
|
let other = tempfile::tempdir().unwrap();
|
||||||
|
fs::write(other.path().join("x.php"), "x").unwrap();
|
||||||
|
let outside = fs::canonicalize(other.path()).unwrap().join("x.php");
|
||||||
|
let err = engine
|
||||||
|
.execute("file.open", json!({"path": outside}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "failed");
|
||||||
|
let err = engine
|
||||||
|
.execute("file.open", json!({"path": "../x.php"}))
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn clean_buffers_reload_and_dirty_ones_report_conflicts() {
|
||||||
|
let (dir, mut engine, _) = open_project(&[("clean.php", b"one"), ("dirty.php", b"one")]);
|
||||||
|
let (clean, _) = open_file(&mut engine, "clean.php");
|
||||||
|
let (dirty, _) = open_file(&mut engine, "dirty.php");
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": dirty, "baseVersion": 1, "edits": [{"from": 0, "to": 0, "insert": "mine "}]}),
|
||||||
|
);
|
||||||
|
fs::write(dir.path().join("clean.php"), "two, longer").unwrap();
|
||||||
|
fs::write(dir.path().join("dirty.php"), "theirs, longer").unwrap();
|
||||||
|
|
||||||
|
let mut events = wait_for(
|
||||||
|
&mut engine,
|
||||||
|
|e| matches!(e, Event::BufferChanged { id, .. } if *id == clean),
|
||||||
|
);
|
||||||
|
if !events
|
||||||
|
.iter()
|
||||||
|
.any(|e| matches!(e, Event::FileChangedOnDisk { id, .. } if *id == dirty))
|
||||||
|
{
|
||||||
|
events = wait_for(
|
||||||
|
&mut engine,
|
||||||
|
|e| matches!(e, Event::FileChangedOnDisk { id, .. } if *id == dirty),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
events
|
||||||
|
.iter()
|
||||||
|
.all(|e| !matches!(e, Event::FileChangedOnDisk { id, .. } if *id == clean))
|
||||||
|
);
|
||||||
|
let clean_now = run(&mut engine, "buffer.get", json!({"id": clean})).value;
|
||||||
|
assert_eq!(clean_now["text"], "two, longer");
|
||||||
|
assert_eq!(clean_now["dirty"], false);
|
||||||
|
let dirty_now = run(&mut engine, "buffer.get", json!({"id": dirty})).value;
|
||||||
|
assert_eq!(dirty_now["text"], "mine one");
|
||||||
|
assert_eq!(dirty_now["dirty"], true);
|
||||||
|
|
||||||
|
fs::remove_file(dir.path().join("clean.php")).unwrap();
|
||||||
|
wait_for(
|
||||||
|
&mut engine,
|
||||||
|
|e| matches!(e, Event::FileChangedOnDisk { id, removed: true, .. } if *id == clean),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn own_saves_do_not_reload() {
|
||||||
|
let (_dir, mut engine, _) = open_project(&[("a.php", b"a")]);
|
||||||
|
let (id, _) = open_file(&mut engine, "a.php");
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 1, "to": 1, "insert": "b"}]}),
|
||||||
|
);
|
||||||
|
run(&mut engine, "file.save", json!({"id": id}));
|
||||||
|
let deadline = Instant::now() + Duration::from_millis(600);
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
let events = engine.pump_vfs().unwrap();
|
||||||
|
assert!(
|
||||||
|
!events.iter().any(|e| matches!(
|
||||||
|
e,
|
||||||
|
Event::BufferChanged { .. } | Event::FileChangedOnDisk { .. }
|
||||||
|
)),
|
||||||
|
"{events:?}"
|
||||||
|
);
|
||||||
|
std::thread::sleep(Duration::from_millis(25));
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
run(&mut engine, "buffer.get", json!({"id": id})).value["version"],
|
||||||
|
2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tree_changes_are_reported_for_listed_directories() {
|
||||||
|
let (dir, mut engine, _) = open_project(&[("src/a.php", b"a")]);
|
||||||
|
run(&mut engine, "project.children", json!({"dir": "src"}));
|
||||||
|
fs::write(dir.path().join("new.php"), "n").unwrap();
|
||||||
|
wait_for(
|
||||||
|
&mut engine,
|
||||||
|
|e| matches!(e, Event::TreeChanged { dir } if dir.is_empty()),
|
||||||
|
);
|
||||||
|
fs::write(dir.path().join("src").join("b.php"), "b").unwrap();
|
||||||
|
wait_for(
|
||||||
|
&mut engine,
|
||||||
|
|e| matches!(e, Event::TreeChanged { dir } if dir == "src"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn watched_directories_are_capped_least_recently_listed_first() {
|
||||||
|
let (dir, mut engine, _) = open_project(&[]);
|
||||||
|
let limit = engine.state().project.as_ref().unwrap().watch_limit();
|
||||||
|
assert!(limit > 0, "the watcher should start on a temp dir");
|
||||||
|
let count = limit + 5;
|
||||||
|
for n in 0..count {
|
||||||
|
fs::create_dir(dir.path().join(format!("d{n:04}"))).unwrap();
|
||||||
|
}
|
||||||
|
for n in 0..count {
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"project.children",
|
||||||
|
json!({"dir": format!("d{n:04}")}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
run(&mut engine, "project.children", json!({"dir": "d0000"}));
|
||||||
|
let watched = engine.state().project.as_ref().unwrap().watched_dirs();
|
||||||
|
assert_eq!(watched.len(), limit);
|
||||||
|
assert_eq!(watched.last().unwrap(), Path::new("d0000"));
|
||||||
|
assert!(!watched.iter().any(|d| d == Path::new("d0001")));
|
||||||
|
assert!(!watched.iter().any(|d| d == Path::new("d0005")));
|
||||||
|
assert!(watched.iter().any(|d| d == Path::new("d0006")));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A command that reads the state and then panics on an index.
|
||||||
|
struct PanicsMidway;
|
||||||
|
|
||||||
|
impl Command for PanicsMidway {
|
||||||
|
fn spec(&self) -> &CommandSpec {
|
||||||
|
&CommandSpec {
|
||||||
|
id: "test.panicsMidway",
|
||||||
|
label: "Panics",
|
||||||
|
menu_path: None,
|
||||||
|
default_shortcut: None,
|
||||||
|
params: "none",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
|
||||||
|
let _ = state.buffers.len();
|
||||||
|
let empty: Vec<u8> = Vec::new();
|
||||||
|
Ok(Outcome::value(json!(empty[3])))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_panicking_command_leaves_open_buffers_intact() {
|
||||||
|
let (_dir, mut engine, _) = open_project(&[("a.php", b"a")]);
|
||||||
|
engine.register(Box::new(PanicsMidway)).unwrap();
|
||||||
|
let (id, _) = open_file(&mut engine, "a.php");
|
||||||
|
run(
|
||||||
|
&mut engine,
|
||||||
|
"buffer.edit",
|
||||||
|
json!({"id": id, "baseVersion": 1, "edits": [{"from": 1, "to": 1, "insert": " unsaved"}]}),
|
||||||
|
);
|
||||||
|
let err = engine
|
||||||
|
.execute("test.panicsMidway", Value::Null)
|
||||||
|
.unwrap_err();
|
||||||
|
assert_eq!(err.code(), "internal");
|
||||||
|
let got = run(&mut engine, "buffer.get", json!({"id": id})).value;
|
||||||
|
assert_eq!(got["text"], "a unsaved");
|
||||||
|
assert_eq!(got["dirty"], true);
|
||||||
|
run(&mut engine, "file.save", json!({"id": id}));
|
||||||
|
}
|
||||||
|
|
@ -13,14 +13,15 @@
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use lutra_control::Backend;
|
use lutra_control::{Backend, EventSink, Subscribers};
|
||||||
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
|
use lutra_engine::{CommandInfo, Effect, Engine, EngineError};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
/// A control [`Backend`] over a bare engine, without a window.
|
/// A control [`Backend`] over a bare engine, without a window.
|
||||||
///
|
///
|
||||||
/// Effects are recorded instead of applied; screenshots write a tiny marker
|
/// Effects are recorded instead of applied; events are published to
|
||||||
/// file so that tests can check the path handling.
|
/// subscribers like the desktop host does; screenshots record the path and
|
||||||
|
/// fail, because there is no window.
|
||||||
pub struct EngineBackend {
|
pub struct EngineBackend {
|
||||||
/// The engine, behind a lock like in the desktop app.
|
/// The engine, behind a lock like in the desktop app.
|
||||||
pub engine: Mutex<Engine>,
|
pub engine: Mutex<Engine>,
|
||||||
|
|
@ -28,6 +29,8 @@ pub struct EngineBackend {
|
||||||
pub effects: Mutex<Vec<Effect>>,
|
pub effects: Mutex<Vec<Effect>>,
|
||||||
/// Paths passed to [`Backend::screenshot`].
|
/// Paths passed to [`Backend::screenshot`].
|
||||||
pub screenshots: Mutex<Vec<PathBuf>>,
|
pub screenshots: Mutex<Vec<PathBuf>>,
|
||||||
|
/// Event subscribers.
|
||||||
|
pub subscribers: Subscribers,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EngineBackend {
|
impl EngineBackend {
|
||||||
|
|
@ -37,8 +40,16 @@ impl EngineBackend {
|
||||||
engine: Mutex::new(Engine::new().unwrap()),
|
engine: Mutex::new(Engine::new().unwrap()),
|
||||||
effects: Mutex::new(Vec::new()),
|
effects: Mutex::new(Vec::new()),
|
||||||
screenshots: Mutex::new(Vec::new()),
|
screenshots: Mutex::new(Vec::new()),
|
||||||
|
subscribers: Subscribers::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Applies pending changes on disk and publishes the resulting events,
|
||||||
|
/// as the desktop host's pump thread does.
|
||||||
|
pub fn pump(&self) {
|
||||||
|
let events = self.engine.lock().unwrap().pump_vfs().unwrap();
|
||||||
|
self.subscribers.publish(&events);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for EngineBackend {
|
impl Default for EngineBackend {
|
||||||
|
|
@ -51,6 +62,7 @@ impl Backend for EngineBackend {
|
||||||
fn execute(&self, command: &str, params: Value) -> Result<Value, EngineError> {
|
fn execute(&self, command: &str, params: Value) -> Result<Value, EngineError> {
|
||||||
let out = self.engine.lock().unwrap().execute(command, params)?;
|
let out = self.engine.lock().unwrap().execute(command, params)?;
|
||||||
self.effects.lock().unwrap().extend(out.effects);
|
self.effects.lock().unwrap().extend(out.effects);
|
||||||
|
self.subscribers.publish(&out.events);
|
||||||
Ok(out.value)
|
Ok(out.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,4 +74,8 @@ impl Backend for EngineBackend {
|
||||||
self.screenshots.lock().unwrap().push(path.to_path_buf());
|
self.screenshots.lock().unwrap().push(path.to_path_buf());
|
||||||
Err("no window in tests".into())
|
Err("no window in tests".into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscribe(&self, sink: EventSink) {
|
||||||
|
self.subscribers.add(sink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,9 @@ Every user-visible action is a command: `id`, `label`, `menu_path`, `default_sho
|
||||||
As built in M0:
|
As built in M0:
|
||||||
|
|
||||||
- `lutra_engine::Command` is a trait with `spec()`, `enabled()` and `run()`. `spec()` returns a `CommandSpec` with the static metadata. Params and results are `serde_json::Value`; each command documents its params in `CommandSpec::params`.
|
- `lutra_engine::Command` is a trait with `spec()`, `enabled()` and `run()`. `spec()` returns a `CommandSpec` with the static metadata. Params and results are `serde_json::Value`; each command documents its params in `CommandSpec::params`.
|
||||||
- `Engine::execute(id, params)` looks the command up, checks `enabled`, runs it inside the guard and returns an `Outcome { value, effects }`.
|
- `Engine::execute(id, params)` looks the command up, checks `enabled`, runs it inside the guard and returns an `Outcome { value, effects, events }`. Commands queue events with `State::emit`; the engine moves them into the outcome. Events queued by a command that then fails stay queued and arrive with the next outcome, `Engine::pump_vfs` or `Engine::take_events`.
|
||||||
- Effects are requests the engine cannot fulfil below L6 (`Effect::Quit` so far). The host (desktop, tests) applies them after the command returns. Commands never call into Tauri.
|
- Effects are requests the engine cannot fulfil below L6 (`Effect::Quit` so far). The host (desktop, tests) applies them after the command returns. Commands never call into Tauri.
|
||||||
- `EngineError` has a stable string `code()` (`unknown_command`, `disabled`, `invalid_params`, `failed`, `internal`) used by the control channel and the frontend.
|
- `EngineError` has a stable string `code()` used by the control channel and the frontend: `unknown_command`, `disabled` (e.g. a file command without a project), `invalid_params`, `stale_version` (a `buffer.edit` based on an old version), `unsaved_changes` (closing a dirty buffer or project without `force`), `failed`, `internal`.
|
||||||
- Shortcut notation: `Mod` is Ctrl on Windows and Linux and Cmd on macOS (`Mod+Q`). Default shortcuts are Lutra's own.
|
- Shortcut notation: `Mod` is Ctrl on Windows and Linux and Cmd on macOS (`Mod+Q`). Default shortcuts are Lutra's own.
|
||||||
|
|
||||||
## Guard
|
## Guard
|
||||||
|
|
@ -58,16 +58,32 @@ As built in M0:
|
||||||
|
|
||||||
- Transport: one JSON object per line, UTF-8, at most 16 MiB per line; longer lines get an error response and the connection is closed.
|
- Transport: one JSON object per line, UTF-8, at most 16 MiB per line; longer lines get an error response and the connection is closed.
|
||||||
- Request: `{"id": <number|string>, "method": "<name>", "params": {...}}`. Response: `{"id": ..., "result": ...}` or `{"id": ..., "error": {"code": "...", "message": "..."}}`. A line that is not valid JSON gets an error response with `"id": null`.
|
- Request: `{"id": <number|string>, "method": "<name>", "params": {...}}`. Response: `{"id": ..., "result": ...}` or `{"id": ..., "error": {"code": "...", "message": "..."}}`. A line that is not valid JSON gets an error response with `"id": null`.
|
||||||
- Methods (M0): `app.ping`, `engine.execute {command, params}`, `engine.listCommands`, `ui.screenshot {path}`.
|
- Methods: `app.ping`, `engine.execute {command, params}`, `engine.listCommands`, `ui.screenshot {path}`, `events.subscribe`. Names and docs live in `crates/control/src/methods.rs`.
|
||||||
- `lutra_control` defines a `Backend` trait; the desktop app implements it (engine access, effects, screenshots), tests implement it with a bare engine. One thread accepts connections and each connection gets its own thread; no async runtime is needed in the crate.
|
- `events.subscribe` answers `{subscribed: true}` and from then on writes `{"event": {...}}` lines for every engine event on that connection, between responses; a shared writer writes each line whole. Subscribing twice is a no-op. Each subscriber has a queue of 4096 events (`lutra_control::EVENT_QUEUE`); a subscriber that falls behind or disconnects is dropped by `Subscribers::publish`, and the connection gets a final `{"event": {"kind": "subscriptionLost"}}` line if it is still open.
|
||||||
|
- `lutra_control` defines a `Backend` trait; the desktop app implements it (engine access, effects, screenshots, event subscribers), tests implement it with a bare engine (`lutra_testkit::EngineBackend`). Hosts publish each outcome's events through `lutra_control::Subscribers`. One thread accepts connections and each connection gets its own thread; no async runtime is needed in the crate.
|
||||||
- Screenshots: `xcap` does not enumerate windows of its own process (querying their titles can deadlock), so the desktop focuses its main window, reads the client area's physical position and size from Tauri and captures that region of the monitor with `xcap::Monitor::capture_region`. Windows covering Lutra at that moment appear in the image. A webview-native capture (WebView2 `CapturePreview`, WKWebView snapshots) can replace it later.
|
- Screenshots: `xcap` does not enumerate windows of its own process (querying their titles can deadlock), so the desktop focuses its main window, reads the client area's physical position and size from Tauri and captures that region of the monitor with `xcap::Monitor::capture_region`. Windows covering Lutra at that moment appear in the image. A webview-native capture (WebView2 `CapturePreview`, WKWebView snapshots) can replace it later.
|
||||||
|
|
||||||
## Frontend ↔ engine
|
## Frontend ↔ engine
|
||||||
|
|
||||||
The frontend calls `run(id, params)` over Tauri IPC (`engine_execute`, `engine_list_commands`) and subscribes to state events (open buffers, diagnostics, tree changes). Buffer contents live in the engine; CodeMirror sends edits as transactions and receives authoritative changes back.
|
The frontend calls `run(id, params)` over Tauri IPC (`engine_execute`, `engine_list_commands`) and subscribes to state events. Buffer contents live in the engine; CodeMirror sends edits as transactions and receives authoritative changes back.
|
||||||
|
|
||||||
Theme tokens are defined in Rust (`lutra_engine::theme`) and rendered as CSS custom properties (`--lt-*`). The desktop injects them with the window's initialization script, so the first frame is already themed and the frontend needs no round trip. Components use only `var(--lt-…)`.
|
Theme tokens are defined in Rust (`lutra_engine::theme`) and rendered as CSS custom properties (`--lt-*`). The desktop injects them with the window's initialization script, so the first frame is already themed and the frontend needs no round trip. Components use only `var(--lt-…)`.
|
||||||
|
|
||||||
|
### Workspace (M1.3)
|
||||||
|
|
||||||
|
- `State` holds `project: Option<Workspace>` (the `lutra_vfs::Project`, its watcher and the watched directories) and `buffers` (id → `OpenBuffer { buffer: lutra_text::Buffer, path, version, saved_version, bom }`). A buffer is dirty when `version != saved_version`. Ids are never reused.
|
||||||
|
- Commands: `project.open {path, force?}`, `project.close {force?}`, `project.children {dir?}`, `file.open {path}` (returns the existing buffer if the file is already open), `buffer.get {id}`, `buffer.edit {id, baseVersion, edits}`, `file.save {id}` (`Mod+S`), `file.saveAll` (`Mod+Alt+S`), `buffer.close {id, force?}`. Everything except `project.open` is disabled without a project. Paths are project-relative with `/`; absolute paths are accepted when they lie inside the project; `..` escapes are rejected by the VFS.
|
||||||
|
- Edit protocol: `edits` are `{from, to, insert}` in UTF-16 code units, all relative to the text at `baseVersion` (CodeMirror change-set semantics), non-overlapping, applied atomically. A `baseVersion` other than the current version fails with `stale_version` and changes nothing; the client refetches with `buffer.get` and reapplies its pending changes. Each successful batch increments the version by one. Limits: 100,000 edits and 64 MiB inserted per call.
|
||||||
|
- Saving writes `Buffer::save_text` (the file's dominant line ending) atomically through the VFS and keeps a UTF-8 byte order mark.
|
||||||
|
|
||||||
|
### Events
|
||||||
|
|
||||||
|
`lutra_engine::Event`, serialised as `{"kind": "<camelCase>", ...}`: `projectOpened {root, name}`, `projectClosed`, `treeChanged {dir}`, `bufferOpened {id, path}`, `bufferChanged {id, version}`, `dirtyChanged {id, dirty}`, `bufferClosed {id}`, `fileChangedOnDisk {path, id, removed}`, `watchFailed {message}`. Events say what changed; clients fetch data with `project.children` and `buffer.get`.
|
||||||
|
|
||||||
|
Changes on disk reach the engine through `Engine::pump_vfs()`, which hosts call regularly (the desktop's pump thread arrives with M1.4; tests call it directly). It maps VFS events to `treeChanged` for the parent directories, reloads a clean buffer whose file changed (unless the file already matches, as after our own save) with `bufferChanged`, and reports `fileChangedOnDisk` for a dirty buffer, which keeps its text, or for a removed file. At most 10,000 VFS events are handled per pump.
|
||||||
|
|
||||||
|
Watched directories: the VFS watcher is non-recursive, so `project.children` watches each directory it lists. At most 256 directories besides the root are watched natively and 32 when polling (each polled directory is rescanned every 100 ms); beyond that the least recently listed directory is unwatched, and listing it again re-watches it. Expanding a deep `vendor/` therefore costs at most 256 native watches; changes in directories that dropped out are not reported until they are listed again. `node_modules`, ignored and hidden directories are never watched.
|
||||||
|
|
||||||
## Decisions (M0)
|
## Decisions (M0)
|
||||||
|
|
||||||
| Question | Decision | Why |
|
| Question | Decision | Why |
|
||||||
|
|
@ -85,6 +101,7 @@ Theme tokens are defined in Rust (`lutra_engine::theme`) and rendered as CSS cus
|
||||||
|
|
||||||
| Date | Change | Summary |
|
| Date | Change | Summary |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
|
| 2026-10-14 | minor | M1.3: workspace commands, edit protocol, events, events.subscribe, watched-directory limits |
|
||||||
| 2026-10-14 | minor | Screenshot method, placeholder app identifier |
|
| 2026-10-14 | minor | Screenshot method, placeholder app identifier |
|
||||||
| 2026-10-13 | major | M0: layers metadata, command and guard design, control protocol, open questions decided |
|
| 2026-10-13 | major | M0: layers metadata, command and guard design, control protocol, open questions decided |
|
||||||
| 2026-10-13 | major | Initial draft |
|
| 2026-10-13 | major | Initial draft |
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
# Gaps
|
# Gaps
|
||||||
|
|
||||||
> **Status:** M0 closed G1; G2 under way (text buffer) · **Last updated:** 2026-10-14
|
> **Status:** M0 closed G1; G2 under way (text buffer, engine buffers) · **Last updated:** 2026-10-14
|
||||||
|
|
||||||
Every known shortfall, ranked. This is the work list: unless the owner gives a task, take the highest open gap. Hours are agent wall-clock hours for one agent, all estimated.
|
Every known shortfall, ranked. This is the work list: unless the owner gives a task, take the highest open gap. Hours are agent wall-clock hours for one agent, all estimated.
|
||||||
|
|
||||||
| # | Gap | Milestone | Estimate (one agent) |
|
| # | Gap | Milestone | Estimate (one agent) |
|
||||||
|---|---|---|---|
|
|---|---|---|---|
|
||||||
| G1 | ~~Foundation: workspace, quality gate, command registry, guard, control channel~~ closed 2026-10-14 | M0 | done (~2 h measured) |
|
| G1 | ~~Foundation: workspace, quality gate, command registry, guard, control channel~~ closed 2026-10-14 | M0 | done (~2 h measured) |
|
||||||
| G2 | Editor core: buffers, undo, multi-caret, save, recovery journal | M1 | ~30 h |
|
| G2 | Editor core: ~~engine buffers, save~~ (M1.3), editor UI, undo, multi-caret, recovery journal | M1 | ~25 h |
|
||||||
| G3 | Project model: ~~VFS~~ (M1.2: listing, ignore rules, native file watching with polling fallback, safe file IO), project tree (UI exists against a mocked engine, M1.5; wiring after M1.3/M1.4), tabs; command palette, settings, theme switching | M1 | ~27 h |
|
| G3 | Project model: ~~VFS~~ (M1.2: listing, ignore rules, native file watching with polling fallback, safe file IO), project tree (UI exists against a mocked engine, M1.5; engine side with `project.children` and tree events landed in M1.3; wiring in M1.4), tabs; command palette, settings, theme switching | M1 | ~27 h |
|
||||||
| G4 | Syntax and structure: tree-sitter PHP, Lezer highlighting, file structure | M2 | ~15 h |
|
| G4 | Syntax and structure: tree-sitter PHP, Lezer highlighting, file structure | M2 | ~15 h |
|
||||||
| G5 | Search and navigation: find in files, go to file/class/symbol, first index | M2 | ~35 h |
|
| G5 | Search and navigation: find in files, go to file/class/symbol, first index | M2 | ~35 h |
|
||||||
| G6 | Language server: completion, diagnostics, definition, hover, rename via Phpactor | M3 | ~45 h |
|
| G6 | Language server: completion, diagnostics, definition, hover, rename via Phpactor | M3 | ~45 h |
|
||||||
|
|
@ -23,7 +23,7 @@ Every known shortfall, ranked. This is the work list: unless the owner gives a t
|
||||||
| G15 | Database tools, HTTP client, Docker and remote | later | ~430 h |
|
| G15 | Database tools, HTTP client, Docker and remote | later | ~430 h |
|
||||||
| G16 | Plug-ins and ecosystem | later | ~120 h |
|
| G16 | Plug-ins and ecosystem | later | ~120 h |
|
||||||
|
|
||||||
G2 progress: the engine text buffer landed (M1.1, `crates/text`: rope, UTF-16 and char positions, atomic edit batches, line ending policy). Engine buffers, undo, multi-caret, save and the recovery journal are still open; the estimate stays ~30 h.
|
G2 progress: the engine text buffer landed (M1.1, `crates/text`: rope, UTF-16 and char positions, atomic edit batches, line ending policy). M1.3 added engine buffers with versions, the edit protocol, save and save all, and reloading on external changes. Still open: the editor UI, undo, multi-caret and the recovery journal; estimate lowered to ~25 h.
|
||||||
|
|
||||||
Known smaller items from M0:
|
Known smaller items from M0:
|
||||||
|
|
||||||
|
|
@ -35,6 +35,7 @@ Known smaller items from M0:
|
||||||
|
|
||||||
| Date | Change | Summary |
|
| Date | Change | Summary |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
|
| 2026-10-14 | minor | G2 shrunk, G3 note: engine workspace (M1.3) |
|
||||||
| 2026-10-14 | minor | G3 note: project tree UI (M1.5) |
|
| 2026-10-14 | minor | G3 note: project tree UI (M1.5) |
|
||||||
| 2026-10-14 | minor | G2 note: text buffer landed (M1.1) |
|
| 2026-10-14 | minor | G2 note: text buffer landed (M1.1) |
|
||||||
| 2026-10-14 | minor | G3: native file watching (M1.2b); notify item resolved (CC0-1.0 approved for notify) |
|
| 2026-10-14 | minor | G3: native file watching (M1.2b); notify item resolved (CC0-1.0 approved for notify) |
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
# Milestones
|
# Milestones
|
||||||
|
|
||||||
> **Status:** M0 done, current focus M1 · **Last updated:** 2026-10-14
|
> **Status:** M0 done, M1 in progress · **Last updated:** 2026-10-14
|
||||||
|
|
||||||
Hours are agent wall-clock hours for one agent; M0 is measured, the rest estimated.
|
Hours are agent wall-clock hours for one agent; M0 is measured, the rest estimated.
|
||||||
|
|
||||||
| Milestone | Goal | State | Hours |
|
| Milestone | Goal | State | Hours |
|
||||||
|---|---|---|---:|
|
|---|---|---|---:|
|
||||||
| M0 | Foundation: workspace, lints, quality gate, command registry, control channel, empty window | done 2026-10-14 (CI workflow written, not yet run: no remote) | ~2 (measured) |
|
| M0 | Foundation: workspace, lints, quality gate, command registry, control channel, empty window | done 2026-10-14 (CI workflow written, not yet run: no remote) | ~2 (measured) |
|
||||||
| M1 | Editor and project: open folder, tree, tabs, editor, save, recovery, palette, settings, themes | not started | ~60 |
|
| M1 | Editor and project: open folder, tree, tabs, editor, save, recovery, palette, settings, themes | in progress (M1.1 text, M1.2 VFS, M1.5 tree UI, M1.3 engine workspace) | ~60 |
|
||||||
| M2 | Syntax and search: highlighting, structure, find in files, go to file/class/symbol | not started | ~50 |
|
| M2 | Syntax and search: highlighting, structure, find in files, go to file/class/symbol | not started | ~50 |
|
||||||
| M3 | LSP, terminal, CLI: completion, diagnostics, go to definition, hover, rename; terminal; `lutra` CLI and MCP — **MVP** | not started | ~80 |
|
| M3 | LSP, terminal, CLI: completion, diagnostics, go to definition, hover, rename; terminal; `lutra` CLI and MCP — **MVP** | not started | ~80 |
|
||||||
| M4 | Git: status, diff, commit, branches, log, blame, conflicts | not started | ~150 |
|
| M4 | Git: status, diff, commit, branches, log, blame, conflicts | not started | ~150 |
|
||||||
|
|
@ -31,5 +31,6 @@ By the estimates above the alpha gate needs M1–M4 plus cross-platform verifica
|
||||||
|
|
||||||
| Date | Change | Summary |
|
| Date | Change | Summary |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
|
| 2026-10-14 | minor | M1 in progress |
|
||||||
| 2026-10-14 | minor | M0 done; hours per milestone |
|
| 2026-10-14 | minor | M0 done; hours per milestone |
|
||||||
| 2026-10-13 | major | Initial milestones |
|
| 2026-10-13 | major | Initial milestones |
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# Target-app parity
|
# Target-app parity
|
||||||
|
|
||||||
> **Status:** M1 in progress (text buffer landed), no PHP feature ships · **Last updated:** 2026-10-14 · **Target:** PhpStorm 2026.x
|
> **Status:** M1 in progress (text buffer, VFS, tree UI, engine workspace landed), no PHP feature ships · **Last updated:** 2026-10-14 · **Target:** PhpStorm 2026.x
|
||||||
|
|
||||||
## How this is measured
|
## How this is measured
|
||||||
|
|
||||||
|
|
@ -12,8 +12,8 @@ Hours are the estimated agent wall-clock hours for one agent to reach behaviour
|
||||||
|
|
||||||
| Area | Weight | Score | Missing | Hours (one agent) | Evidence |
|
| Area | Weight | Score | Missing | Hours (one agent) | Evidence |
|
||||||
|---|---:|---:|---|---:|---|
|
|---|---:|---:|---|---:|---|
|
||||||
| Editor core | 12 | 0% | everything user-visible; only the engine text buffer exists | ~120 (est.) | `crates/text`: rope buffer, positions, edits (M1.1, unit tests + proptests) |
|
| Editor core | 12 | 0% | everything user-visible: no editor in the UI yet; engine buffers, file commands and events exist | ~120 (est.) | `crates/text`: rope buffer, positions, edits (M1.1, unit tests + proptests). M1.3: engine buffers with versions and dirty state, `file.open`, `buffer.get`, `buffer.edit` (UTF-16 change sets, `stale_version`), `file.save`, `file.saveAll`, `buffer.close` with unsaved-changes guard, line ending and BOM kept on save, clean buffers reload on external change; 14 integration tests on temp dirs (`crates/engine/tests/workspace.rs`) |
|
||||||
| Project view, VFS | 6 | 0% | everything reachable in the app: tree not wired to the engine yet, file commands | ~37 (est.) | `lutra-vfs` (M1.2): lazy sorted listing, ignore rules (.gitignore, .ignore, info/exclude, global excludes, Lutra defaults), native file watching via `notify` with a polling fallback, capped UTF-8 reads, atomic writes; 34 tests on temp dirs, watcher tests run on both backends (`crates/vfs/tests`). M1.5: project tree UI (lazy listing, keyboard navigation, dimmed ignored entries, event refresh) in `ui/src/components/project/`, Vitest against a mocked engine |
|
| Project view, VFS | 6 | 0% | everything reachable in the app: the tree UI is not wired to the engine yet (M1.4) | ~37 (est.) | `lutra-vfs` (M1.2): lazy sorted listing, ignore rules (.gitignore, .ignore, info/exclude, global excludes, Lutra defaults), native file watching via `notify` with a polling fallback, capped UTF-8 reads, atomic writes; 34 tests on temp dirs, watcher tests run on both backends (`crates/vfs/tests`). M1.5: project tree UI (lazy listing, keyboard navigation, dimmed ignored entries, event refresh) in `ui/src/components/project/`, Vitest against a mocked engine. M1.3: `project.open`, `project.close`, `project.children` (watches listed directories, capped at 256 native / 32 polling, least recently listed dropped), `treeChanged` events from disk, `events.subscribe` on the control channel with a TCP test (`crates/control/tests/events.rs`) |
|
||||||
| Navigation | 10 | 0% | everything | ~120 (est.) | — |
|
| Navigation | 10 | 0% | everything | ~120 (est.) | — |
|
||||||
| Search and replace | 6 | 0% | everything | ~80 (est.) | — |
|
| Search and replace | 6 | 0% | everything | ~80 (est.) | — |
|
||||||
| Code completion | 10 | 0% | everything | ~250 (est.) | — |
|
| Code completion | 10 | 0% | everything | ~250 (est.) | — |
|
||||||
|
|
@ -62,6 +62,7 @@ Nothing user-visible yet, so every score stays 0%.
|
||||||
| Date | Change | Summary |
|
| Date | Change | Summary |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| 2026-10-14 | minor | Project view evidence: project tree UI (M1.5), score unchanged |
|
| 2026-10-14 | minor | Project view evidence: project tree UI (M1.5), score unchanged |
|
||||||
|
| 2026-10-14 | minor | M1.3 engine workspace evidence (Editor core, Project view; scores unchanged) |
|
||||||
| 2026-10-14 | minor | M1.1 text buffer evidence (Editor core, score unchanged) |
|
| 2026-10-14 | minor | M1.1 text buffer evidence (Editor core, score unchanged) |
|
||||||
| 2026-10-14 | minor | Project view evidence: native file watching (M1.2b), score unchanged |
|
| 2026-10-14 | minor | Project view evidence: native file watching (M1.2b), score unchanged |
|
||||||
| 2026-10-14 | minor | Project view evidence: `lutra-vfs` (M1.2), score unchanged |
|
| 2026-10-14 | minor | Project view evidence: `lutra-vfs` (M1.2), score unchanged |
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue