2844 lines
108 KiB
Rust
2844 lines
108 KiB
Rust
use std::{
|
|
collections::BTreeMap,
|
|
io,
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
use crate::{
|
|
editor::{Cursor, Editor, Effect, Input, Invocation, Mode},
|
|
lsp::Lsp,
|
|
plugin::{
|
|
DocumentEvent, DocumentEventKind, DocumentSnapshot, PluginEvent, PluginUpdate,
|
|
TextPosition, UpdateEffect,
|
|
},
|
|
ruby::{Config, RubyPlugin, load_config},
|
|
};
|
|
|
|
struct TrackedDocument {
|
|
snapshot: DocumentSnapshot,
|
|
save_revision: u64,
|
|
}
|
|
|
|
#[derive(Clone, Copy)]
|
|
struct InvocationSource {
|
|
buffer_id: usize,
|
|
revision: u64,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct AsyncRequests {
|
|
pending: BTreeMap<u64, InvocationSource>,
|
|
latest: Option<(u64, InvocationSource)>,
|
|
}
|
|
|
|
/// Owns command eligibility and execution hosts together. Frontends can inspect
|
|
/// the editor, but cannot independently mutate its plugin registry.
|
|
pub struct EditorSession {
|
|
editor: Editor,
|
|
plugins: BTreeMap<String, RubyPlugin>,
|
|
documents: BTreeMap<usize, TrackedDocument>,
|
|
requests: BTreeMap<String, AsyncRequests>,
|
|
lsp: Lsp,
|
|
lsp_request_id: u64,
|
|
lsp_source_cursor: Option<Cursor>,
|
|
pending_hover: bool,
|
|
hover_owner: Option<String>,
|
|
}
|
|
|
|
impl EditorSession {
|
|
/// File errors fail opening; rejected configuration leaves core editing
|
|
/// available and presents a persistent startup diagnostic.
|
|
pub fn open(
|
|
file: Option<PathBuf>,
|
|
capabilities: Vec<String>,
|
|
config: Option<&Path>,
|
|
) -> io::Result<Self> {
|
|
let mut editor = Editor::open(file, capabilities)?;
|
|
let mut notices = Vec::new();
|
|
let config = match config {
|
|
Some(path) => load_config(path).unwrap_or_else(|error| {
|
|
editor.show_startup_diagnostic(format!(
|
|
"{}\n\nConfiguration was rejected; using default settings and bindings.\n\n{error}",
|
|
path.display()
|
|
));
|
|
Config::default()
|
|
}),
|
|
None => Config::default(),
|
|
};
|
|
if let Err(error) = editor.set_leader(&config.leader) {
|
|
notices.push(format!("leader {:?}: {error}", config.leader));
|
|
}
|
|
if let Err(error) = editor.set_file_explorer_side(&config.file_explorer_side) {
|
|
notices.push(format!(
|
|
"file explorer side {:?}: {error}",
|
|
config.file_explorer_side
|
|
));
|
|
}
|
|
if let Err(error) = editor.set_file_picker_view(&config.file_picker_view) {
|
|
notices.push(format!(
|
|
"file picker view {:?}: {error}",
|
|
config.file_picker_view
|
|
));
|
|
}
|
|
if let Err(error) = editor.set_file_explorer_view(&config.file_explorer_view) {
|
|
notices.push(format!(
|
|
"file explorer view {:?}: {error}",
|
|
config.file_explorer_view
|
|
));
|
|
}
|
|
if let Err(error) = editor.set_clipboard_mode(&config.clipboard_mode) {
|
|
notices.push(format!(
|
|
"clipboard mode {:?}: {error}",
|
|
config.clipboard_mode
|
|
));
|
|
}
|
|
let mut plugins = BTreeMap::new();
|
|
for configured in config.plugins {
|
|
let path = configured.path;
|
|
match RubyPlugin::start(&path, &configured.options) {
|
|
Ok((plugin, feature)) => {
|
|
let name = feature.name.clone();
|
|
match editor.register(feature) {
|
|
Ok(()) => {
|
|
plugins.insert(name, plugin);
|
|
}
|
|
Err(error) => notices.push(format!("{}: {error}", path.display())),
|
|
}
|
|
}
|
|
Err(error) => notices.push(format!("{}: {error}", path.display())),
|
|
}
|
|
}
|
|
for binding in config.bindings {
|
|
if let Err(error) = editor.bind(&binding.mode, &binding.key, binding.command) {
|
|
notices.push(format!(
|
|
"binding {} {:?}: {error}",
|
|
binding.mode, binding.key
|
|
));
|
|
}
|
|
}
|
|
if notices.is_empty() {
|
|
editor.set_message("Ready • --no-config uses default keybindings");
|
|
} else {
|
|
editor.set_message(notices.join(" | "));
|
|
}
|
|
let mut session = Self {
|
|
editor,
|
|
plugins,
|
|
documents: BTreeMap::new(),
|
|
requests: BTreeMap::new(),
|
|
lsp: Lsp::new(config.lsp),
|
|
lsp_request_id: 0,
|
|
lsp_source_cursor: None,
|
|
pending_hover: false,
|
|
hover_owner: None,
|
|
};
|
|
session.synchronize_documents();
|
|
Ok(session)
|
|
}
|
|
|
|
pub fn editor(&self) -> &Editor {
|
|
&self.editor
|
|
}
|
|
|
|
pub fn handle(&mut self, input: Input) {
|
|
self.cancel_pending_hover();
|
|
let invocation = self.editor.handle(input);
|
|
self.synchronize_documents();
|
|
if let Some(invocation) = invocation {
|
|
self.dispatch(invocation);
|
|
self.synchronize_documents();
|
|
}
|
|
}
|
|
|
|
pub fn execute(&mut self, command: &str) {
|
|
self.cancel_pending_hover();
|
|
let invocation = self.editor.execute(command);
|
|
self.synchronize_documents();
|
|
if let Some(invocation) = invocation {
|
|
self.dispatch(invocation);
|
|
self.synchronize_documents();
|
|
}
|
|
}
|
|
|
|
fn dispatch(&mut self, invocation: Invocation) {
|
|
if invocation.plugin == "core" {
|
|
let result = match invocation.command.as_str() {
|
|
"lsp.definition" | "lsp.hover" => {
|
|
if let Some(id) = self.lsp_request_id.checked_add(1) {
|
|
self.lsp_request_id = id;
|
|
let context = self.editor.context();
|
|
let source = InvocationSource {
|
|
buffer_id: context.buffer_id,
|
|
revision: context.revision,
|
|
};
|
|
self.requests.entry("core".into()).or_default().latest = Some((id, source));
|
|
self.lsp_source_cursor = Some(self.editor.cursor());
|
|
self.pending_hover = invocation.command == "lsp.hover";
|
|
if self.pending_hover {
|
|
self.lsp.hover(&context, id)
|
|
} else {
|
|
self.lsp.definition(&context, id)
|
|
}
|
|
} else {
|
|
Err("LSP request IDs exhausted".into())
|
|
}
|
|
}
|
|
"lsp.diagnostics" => self.lsp.diagnostics(self.editor.buffer_id()),
|
|
_ => unreachable!("session-dispatched core command"),
|
|
};
|
|
if let Err(error) = result {
|
|
self.editor.set_message(format!("LSP: {error}"));
|
|
}
|
|
return;
|
|
}
|
|
let context = self.editor.context();
|
|
let result = match self.plugins.get_mut(&invocation.plugin) {
|
|
Some(plugin) if plugin.is_async() => {
|
|
let requests = self.requests.entry(invocation.plugin.clone()).or_default();
|
|
plugin
|
|
.queue_invoke(&invocation.command, &invocation.args, &context)
|
|
.map(|id| {
|
|
let source = InvocationSource {
|
|
buffer_id: context.buffer_id,
|
|
revision: context.revision,
|
|
};
|
|
requests.pending.insert(id, source);
|
|
requests.latest = Some((id, source));
|
|
})
|
|
}
|
|
Some(plugin) => plugin
|
|
.invoke(&invocation.command, &invocation.args, &context)
|
|
.and_then(|effects| self.editor.apply(effects)),
|
|
None => Err("plugin host is unavailable".into()),
|
|
};
|
|
if let Err(error) = result {
|
|
self.retire(&invocation.plugin, &error);
|
|
}
|
|
}
|
|
|
|
fn cancel_pending_hover(&mut self) {
|
|
if self.pending_hover {
|
|
if let Some(requests) = self.requests.get_mut("core") {
|
|
requests.latest = None;
|
|
}
|
|
self.pending_hover = false;
|
|
}
|
|
}
|
|
|
|
fn retire(&mut self, owner: &str, error: &str) {
|
|
// Drop also stops a live host whose protocol-valid effects were rejected.
|
|
drop(self.plugins.remove(owner));
|
|
self.requests.remove(owner);
|
|
self.editor.disable_plugin(owner, error);
|
|
if self.hover_owner.as_deref() == Some(owner) {
|
|
self.editor.clear_hover();
|
|
self.hover_owner = None;
|
|
}
|
|
}
|
|
|
|
fn synchronize_documents(&mut self) -> bool {
|
|
if !self.lsp.is_enabled() && !self.plugins.values().any(RubyPlugin::is_async) {
|
|
self.documents.clear();
|
|
return false;
|
|
}
|
|
let mut events = Vec::new();
|
|
if !self.editor.should_quit() {
|
|
for document in self.editor.documents() {
|
|
let path = document.path.map(Path::to_string_lossy);
|
|
let previous = self.documents.get(&document.id);
|
|
let renamed = previous
|
|
.is_some_and(|previous| previous.snapshot.path.as_deref() != path.as_deref());
|
|
if renamed {
|
|
events.push(DocumentEvent {
|
|
event: DocumentEventKind::Close,
|
|
document: previous.expect("renamed document existed").snapshot.clone(),
|
|
});
|
|
}
|
|
let changed = previous.is_none_or(|previous| {
|
|
previous.snapshot.revision != document.revision || renamed
|
|
});
|
|
if changed {
|
|
let snapshot = DocumentSnapshot {
|
|
id: document.id,
|
|
revision: document.revision,
|
|
path: path.map(|path| path.into_owned()),
|
|
root: self.editor.project_root().to_string_lossy().into_owned(),
|
|
text: document.lines.join("\n"),
|
|
};
|
|
events.push(DocumentEvent {
|
|
event: if previous.is_none() || renamed {
|
|
DocumentEventKind::Open
|
|
} else {
|
|
DocumentEventKind::Change
|
|
},
|
|
document: snapshot.clone(),
|
|
});
|
|
let save_revision = previous.map_or(0, |previous| previous.save_revision);
|
|
self.documents.insert(
|
|
document.id,
|
|
TrackedDocument {
|
|
snapshot,
|
|
save_revision,
|
|
},
|
|
);
|
|
}
|
|
let tracked = self
|
|
.documents
|
|
.get_mut(&document.id)
|
|
.expect("document tracked");
|
|
if tracked.save_revision != document.save_revision {
|
|
events.push(DocumentEvent {
|
|
event: DocumentEventKind::Save,
|
|
document: tracked.snapshot.clone(),
|
|
});
|
|
tracked.save_revision = document.save_revision;
|
|
}
|
|
}
|
|
}
|
|
let closed: Vec<_> = self
|
|
.documents
|
|
.keys()
|
|
.filter(|id| self.editor.should_quit() || !self.editor.has_buffer(**id))
|
|
.copied()
|
|
.collect();
|
|
for id in closed {
|
|
let document = self.documents.remove(&id).expect("closed document existed");
|
|
events.push(DocumentEvent {
|
|
event: DocumentEventKind::Close,
|
|
document: document.snapshot,
|
|
});
|
|
}
|
|
let mut failures = Vec::new();
|
|
let mut changed = false;
|
|
for event in &events {
|
|
if let Err(error) = self.lsp.notify_document(event) {
|
|
self.editor.set_message(format!("LSP: {error}"));
|
|
changed = true;
|
|
}
|
|
}
|
|
for (owner, plugin) in &mut self.plugins {
|
|
if !plugin.is_async() {
|
|
continue;
|
|
}
|
|
for event in &events {
|
|
if let Err(error) = plugin.notify_document(event) {
|
|
failures.push((owner.clone(), error));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
changed |= !failures.is_empty();
|
|
for (owner, error) in failures {
|
|
self.retire(&owner, &error);
|
|
}
|
|
changed
|
|
}
|
|
|
|
fn source_is_current(&self, source: InvocationSource) -> bool {
|
|
self.editor
|
|
.documents()
|
|
.any(|document| document.id == source.buffer_id && document.revision == source.revision)
|
|
}
|
|
|
|
fn request_is_current(
|
|
&self,
|
|
owner: &str,
|
|
request_id: Option<u64>,
|
|
source: InvocationSource,
|
|
) -> bool {
|
|
self.editor.buffer_id() == source.buffer_id
|
|
&& self.source_is_current(source)
|
|
&& (owner != "core"
|
|
|| (self.lsp_source_cursor == Some(self.editor.cursor())
|
|
&& self.editor.mode() == Mode::Normal))
|
|
&& self
|
|
.requests
|
|
.get(owner)
|
|
.and_then(|requests| requests.latest)
|
|
.is_some_and(|(id, latest)| {
|
|
request_id == Some(id)
|
|
&& latest.buffer_id == source.buffer_id
|
|
&& latest.revision == source.revision
|
|
})
|
|
}
|
|
|
|
fn hover_is_current(
|
|
&self,
|
|
owner: &str,
|
|
request_id: Option<u64>,
|
|
source: InvocationSource,
|
|
) -> bool {
|
|
self.request_is_current(owner, request_id, source)
|
|
&& self.editor.mode() == Mode::Normal
|
|
&& self.editor.palette().is_none()
|
|
&& self
|
|
.editor
|
|
.file_picker()
|
|
.is_none_or(|picker| !picker.focused)
|
|
}
|
|
|
|
fn apply_update(&mut self, owner: &str, update: PluginUpdate) -> Result<bool, String> {
|
|
if owner == "core"
|
|
&& let Some(id) = update.request_id
|
|
&& self
|
|
.requests
|
|
.get(owner)
|
|
.and_then(|requests| requests.latest)
|
|
.map(|latest| latest.0)
|
|
!= Some(id)
|
|
{
|
|
return Ok(false);
|
|
}
|
|
// Validate the whole applicable annotation batch before changing any buffer.
|
|
for effect in &update.effects {
|
|
match effect {
|
|
UpdateEffect::Diagnostics {
|
|
buffer_id,
|
|
revision,
|
|
items,
|
|
} => {
|
|
self.editor
|
|
.validate_diagnostics(*buffer_id, *revision, items)?;
|
|
}
|
|
UpdateEffect::Navigate {
|
|
buffer_id,
|
|
revision,
|
|
path,
|
|
..
|
|
} => {
|
|
let source = InvocationSource {
|
|
buffer_id: *buffer_id,
|
|
revision: *revision,
|
|
};
|
|
if self.request_is_current(owner, update.request_id, source)
|
|
&& (path.as_os_str().is_empty()
|
|
|| path.to_string_lossy().chars().any(char::is_control))
|
|
{
|
|
return Err(
|
|
"Navigation path must be nonempty and contain no controls".into()
|
|
);
|
|
}
|
|
}
|
|
UpdateEffect::Hover {
|
|
buffer_id,
|
|
revision,
|
|
text,
|
|
} => {
|
|
let source = InvocationSource {
|
|
buffer_id: *buffer_id,
|
|
revision: *revision,
|
|
};
|
|
if self.hover_is_current(owner, update.request_id, source) {
|
|
crate::editor::validate_hover_text(text)?;
|
|
}
|
|
}
|
|
UpdateEffect::Message { .. } => {}
|
|
}
|
|
}
|
|
let mut changed = false;
|
|
for effect in update.effects {
|
|
match effect {
|
|
UpdateEffect::Diagnostics {
|
|
buffer_id,
|
|
revision,
|
|
items,
|
|
} => {
|
|
changed |= self
|
|
.editor
|
|
.replace_validated_diagnostics(owner, buffer_id, revision, items);
|
|
}
|
|
UpdateEffect::Navigate {
|
|
buffer_id,
|
|
revision,
|
|
path,
|
|
row,
|
|
column,
|
|
} => {
|
|
let source = InvocationSource {
|
|
buffer_id,
|
|
revision,
|
|
};
|
|
if self.request_is_current(owner, update.request_id, source) {
|
|
if let Err(error) =
|
|
self.editor.navigate_to(path, TextPosition { row, column })
|
|
{
|
|
self.editor
|
|
.set_message(format!("Navigation failed: {error}"));
|
|
}
|
|
changed = true;
|
|
}
|
|
}
|
|
UpdateEffect::Hover {
|
|
buffer_id,
|
|
revision,
|
|
text,
|
|
} => {
|
|
let source = InvocationSource {
|
|
buffer_id,
|
|
revision,
|
|
};
|
|
if self.hover_is_current(owner, update.request_id, source) {
|
|
self.editor.show_hover(text)?;
|
|
self.hover_owner = Some(owner.into());
|
|
if owner == "core" {
|
|
self.pending_hover = false;
|
|
}
|
|
changed = true;
|
|
}
|
|
}
|
|
UpdateEffect::Message { text } => {
|
|
changed |= self.editor.message() != text;
|
|
self.editor.set_message(text);
|
|
}
|
|
}
|
|
}
|
|
Ok(changed)
|
|
}
|
|
|
|
fn apply_response(
|
|
&mut self,
|
|
owner: &str,
|
|
id: u64,
|
|
effects: Vec<Effect>,
|
|
) -> Result<bool, String> {
|
|
let source = self
|
|
.requests
|
|
.get_mut(owner)
|
|
.and_then(|requests| requests.pending.remove(&id));
|
|
let Some(source) = source else {
|
|
return Err("plugin response has an unknown invocation ID".into());
|
|
};
|
|
if !self.source_is_current(source) || self.editor.buffer_id() != source.buffer_id {
|
|
return Ok(false);
|
|
}
|
|
if effects
|
|
.iter()
|
|
.all(|effect| matches!(effect, Effect::Message(_)))
|
|
{
|
|
let mut changed = false;
|
|
for effect in effects {
|
|
if let Effect::Message(text) = effect {
|
|
changed |= self.editor.message() != text;
|
|
self.editor.set_message(text);
|
|
}
|
|
}
|
|
return Ok(changed);
|
|
}
|
|
let changed = !effects.is_empty();
|
|
self.editor.apply(effects)?;
|
|
Ok(changed)
|
|
}
|
|
|
|
/// Drains ready plugin work without waiting for command or server responses.
|
|
pub fn poll(&mut self) -> bool {
|
|
let mut changed = self.synchronize_documents();
|
|
for update in self.lsp.poll() {
|
|
match self.apply_update("core", update) {
|
|
Ok(applied) => changed |= applied,
|
|
Err(error) => {
|
|
self.editor.clear_plugin_diagnostics("core");
|
|
self.editor.set_message(format!("LSP: {error}"));
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
let mut ready = Vec::new();
|
|
for (owner, plugin) in &mut self.plugins {
|
|
if plugin.is_async() {
|
|
let result = plugin.poll();
|
|
if match &result {
|
|
Ok(events) => !events.is_empty(),
|
|
Err(_) => true,
|
|
} {
|
|
ready.push((owner.clone(), result));
|
|
}
|
|
}
|
|
}
|
|
for (owner, events) in ready {
|
|
let events = match events {
|
|
Ok(events) => events,
|
|
Err(error) => {
|
|
self.retire(&owner, &error);
|
|
changed = true;
|
|
continue;
|
|
}
|
|
};
|
|
for event in events {
|
|
let result = match event {
|
|
PluginEvent::Response { id, effects } => {
|
|
self.apply_response(&owner, id, effects)
|
|
}
|
|
PluginEvent::Update(update) => self.apply_update(&owner, update),
|
|
};
|
|
match result {
|
|
Ok(applied) => changed |= applied,
|
|
Err(error) => {
|
|
self.retire(&owner, &error);
|
|
changed = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
changed |= self.synchronize_documents();
|
|
changed
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::{
|
|
fs,
|
|
sync::atomic::{AtomicU64, Ordering},
|
|
};
|
|
|
|
static NEXT_FIXTURE: AtomicU64 = AtomicU64::new(0);
|
|
|
|
struct Fixture(PathBuf);
|
|
|
|
impl Fixture {
|
|
fn new() -> Self {
|
|
let path = std::env::temp_dir().join(format!(
|
|
"vamp-session-{}-{}",
|
|
std::process::id(),
|
|
NEXT_FIXTURE.fetch_add(1, Ordering::Relaxed),
|
|
));
|
|
fs::create_dir(&path).unwrap();
|
|
Self(path)
|
|
}
|
|
|
|
fn file(&self, name: &str, text: &str) -> PathBuf {
|
|
let path = self.0.join(name);
|
|
fs::write(&path, text).unwrap();
|
|
path
|
|
}
|
|
|
|
fn open(&self, config: &str) -> EditorSession {
|
|
let config = self.file("config.rb", config);
|
|
let mut session = EditorSession::open(
|
|
Some(self.0.join("buffer.txt")),
|
|
vec!["text".into()],
|
|
Some(&config),
|
|
)
|
|
.unwrap();
|
|
// Legacy editing scenarios exercise the internal register, never the user's desktop.
|
|
session.editor.set_clipboard_mode("separate").unwrap();
|
|
session
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
fn system_session(&self, config: &str) -> EditorSession {
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let config = self.file("config.rb", config);
|
|
let mut session = EditorSession::open(
|
|
Some(self.0.join("buffer.txt")),
|
|
vec!["text".into()],
|
|
Some(&config),
|
|
)
|
|
.unwrap();
|
|
self.file("system.txt", "");
|
|
let reader = self.file("clip-read", "#!/bin/sh\n[ -e \"${0%/*}/read.fail\" ] && exit 13\nexec /bin/cat \"${0%/*}/system.txt\"\n");
|
|
let writer = self.file("clip-write", "#!/bin/sh\n[ -e \"${0%/*}/write.fail\" ] && exit 13\nexec /bin/cat > \"${0%/*}/system.txt\"\n");
|
|
fs::set_permissions(&reader, fs::Permissions::from_mode(0o700)).unwrap();
|
|
fs::set_permissions(&writer, fs::Permissions::from_mode(0o700)).unwrap();
|
|
session
|
|
.editor
|
|
.set_system_clipboard(crate::clipboard::SystemClipboard::with_commands(
|
|
reader, writer,
|
|
));
|
|
session
|
|
}
|
|
|
|
fn healthy(&self) {
|
|
self.file(
|
|
"healthy.rb",
|
|
r#"
|
|
Vamp.plugin "healthy", requires: ["text"]
|
|
Vamp.command("healthy") { |editor, args| editor.replace "healthy #{args}" }
|
|
"#,
|
|
);
|
|
}
|
|
fn asynchronous(&self, file: Option<PathBuf>) -> EditorSession {
|
|
self.file(
|
|
"generic.rb",
|
|
r#"
|
|
Vamp.plugin "generic_fixture", requires: ["text", "diagnostics"], async: true
|
|
log_mutex = Mutex.new
|
|
record = ->(value) { log_mutex.synchronize { File.open(Vamp.options.fetch("log"), "a") { |file| file.puts JSON.generate(value) } } }
|
|
Vamp.on_document { |event| record.call(event) }
|
|
Vamp.command("fixture_barrier") { |editor, args| editor.message args }
|
|
Vamp.command("fixture_replace") { |editor, args| editor.replace args }
|
|
Vamp.command("fixture_crash") { |_editor, _args| Process.exit!(7) }
|
|
["fixture_publish", "fixture_hold"].each do |name|
|
|
Vamp.command(name) do |editor, args|
|
|
payload = JSON.parse(args)
|
|
effects = payload.fetch("effects")
|
|
effects.each do |effect|
|
|
next unless ["diagnostics", "navigate"].include?(effect["type"])
|
|
effect["buffer_id"] ||= editor.buffer_id
|
|
effect["revision"] ||= editor.revision
|
|
end
|
|
record.call({"invoke" => name, "buffer_id" => editor.buffer_id, "revision" => editor.revision,
|
|
"row" => editor.row, "column" => editor.column, "request_id" => editor.request_id})
|
|
if name == "fixture_hold"
|
|
Thread.new do
|
|
gate = payload.fetch("gate")
|
|
sleep 0.002 until File.exist?(gate)
|
|
Vamp.publish(effects + [{"type" => "message", "text" => "delivered #{gate}"}], request_id: editor.request_id)
|
|
end
|
|
else
|
|
Vamp.publish(effects, request_id: editor.request_id)
|
|
end
|
|
end
|
|
end
|
|
"#,
|
|
);
|
|
let log = self.0.join("events.jsonl");
|
|
self.file("events.jsonl", "");
|
|
let config = self.file(
|
|
"async-config.rb",
|
|
&format!(
|
|
"Vamp.configure {{ |config| config.clipboard_mode = \"separate\"; config.plugin \"generic.rb\", options: {{log: {}}} }}",
|
|
serde_json::to_string(&log.to_string_lossy()).unwrap(),
|
|
),
|
|
);
|
|
EditorSession::open(
|
|
file,
|
|
vec!["text".into(), "diagnostics".into()],
|
|
Some(&config),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
fn events(&self) -> Vec<serde_json::Value> {
|
|
fs::read_to_string(self.0.join("events.jsonl"))
|
|
.unwrap()
|
|
.lines()
|
|
.map(|line| serde_json::from_str(line).unwrap())
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
impl Drop for Fixture {
|
|
fn drop(&mut self) {
|
|
let _ = fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn invalid_clipboard_policy_retains_system_default_and_valid_bindings() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "abc");
|
|
let mut session = fixture.system_session(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.clipboard_mode = "combined"
|
|
config.bind "n", config.core.right
|
|
end
|
|
"#,
|
|
);
|
|
assert_eq!(
|
|
session.editor().clipboard_mode(),
|
|
crate::clipboard::ClipboardMode::System
|
|
);
|
|
session.handle(Input::Char('n'));
|
|
session.handle(Input::Char('v'));
|
|
session.handle(Input::Char('y'));
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("system.txt")).unwrap(),
|
|
"b"
|
|
);
|
|
assert_eq!(session.editor().lines(), &["abc"]);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn default_system_clipboard_tracks_external_text_and_preserves_cut_failures() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha beta");
|
|
let mut session = fixture.system_session("");
|
|
assert_eq!(
|
|
session.editor().clipboard_mode(),
|
|
crate::clipboard::ClipboardMode::System
|
|
);
|
|
fixture.file("system.txt", "external");
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["externalalpha beta"]);
|
|
session.handle(Input::Char('u'));
|
|
for ch in "vey".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("system.txt")).unwrap(),
|
|
"alpha"
|
|
);
|
|
fixture.file("system.txt", "outside");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["outsidealpha beta"]);
|
|
fixture.file("system.txt", "fresh");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["outsideoutsidealpha beta"]);
|
|
session.handle(Input::Char('u'));
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["alpha beta"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('v'));
|
|
session.handle(Input::Char('e'));
|
|
let selection = session.editor().selections();
|
|
fixture.file("write.fail", "");
|
|
session.handle(Input::Char('c'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Visual);
|
|
assert_eq!(session.editor().selections(), selection);
|
|
assert_eq!(session.editor().lines(), &["alpha beta"]);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("system.txt")).unwrap(),
|
|
"fresh"
|
|
);
|
|
fs::remove_file(fixture.0.join("write.fail")).unwrap();
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Home);
|
|
fixture.file("system.txt", "\u{1b}");
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["alpha beta"]);
|
|
assert_eq!(session.editor().cursor(), crate::editor::Cursor::default());
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn separate_clipboards_keep_internal_and_explicit_system_routes_independent() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha beta");
|
|
let mut session = fixture.system_session(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.clipboard_mode = "separate"
|
|
config.bind "ctrl+s", config.core.copy_system, mode: config.modes.visual
|
|
config.bind "ctrl+g", config.core.paste_system_before
|
|
end
|
|
"#,
|
|
);
|
|
fixture.file("system.txt", "outside");
|
|
for ch in "vey".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("system.txt")).unwrap(),
|
|
"outside"
|
|
);
|
|
for ch in "wve".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Ctrl('s'));
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("system.txt")).unwrap(),
|
|
"beta"
|
|
);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["alphaalpha beta"]);
|
|
session.handle(Input::Char('u'));
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Ctrl('g'));
|
|
assert_eq!(session.editor().lines(), &["betaalpha beta"]);
|
|
session.handle(Input::Char('u'));
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('v'));
|
|
session.handle(Input::Char('e'));
|
|
fixture.file("write.fail", "");
|
|
session.handle(Input::Char('d'));
|
|
assert_eq!(session.editor().lines(), &[" beta"]);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("system.txt")).unwrap(),
|
|
"beta"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn selection_bindings_remap_palette_navigation_without_changing_editing_bindings() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "one two three\none two three");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.bind "ctrl+j", nil, mode: config.modes.selection
|
|
config.bind "ctrl+f", config.core.down, mode: config.modes.selection
|
|
config.bind "ctrl+b", config.core.up, mode: config.modes.selection
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Down);
|
|
session.handle(Input::Ctrl('p'));
|
|
for ch in "core.word".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Ctrl('j'));
|
|
assert_eq!(session.editor().palette().unwrap().selected, 0);
|
|
assert_eq!(
|
|
session.editor().cursor(),
|
|
crate::editor::Cursor { row: 1, column: 0 }
|
|
);
|
|
session.handle(Input::Ctrl('f'));
|
|
assert_eq!(
|
|
session.editor().palette().unwrap().entries[1].name,
|
|
"core.word_next"
|
|
);
|
|
assert_eq!(session.editor().palette().unwrap().selected, 1);
|
|
session.handle(Input::Ctrl('b'));
|
|
assert_eq!(session.editor().palette().unwrap().selected, 0);
|
|
session.handle(Input::Ctrl('f'));
|
|
session.handle(Input::Enter);
|
|
assert!(session.editor().palette().is_none());
|
|
assert_eq!(
|
|
session.editor().cursor(),
|
|
crate::editor::Cursor { row: 1, column: 4 }
|
|
);
|
|
session.handle(Input::Ctrl('k'));
|
|
assert_eq!(session.editor().selection_count(), 2);
|
|
assert_eq!(
|
|
session.editor().lines(),
|
|
&["one two three", "one two three"]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn configured_picker_presentations_preserve_editing_and_docked_focus() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.file_explorer_side = "left"
|
|
config.bind "ctrl+o", config.core.file_picker
|
|
config.bind "ctrl+e", config.core.file_explorer
|
|
end
|
|
"#,
|
|
);
|
|
assert_eq!(
|
|
session.editor().file_explorer_side(),
|
|
crate::files::DockSide::Left
|
|
);
|
|
session.handle(Input::Ctrl('e'));
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().presentation,
|
|
crate::files::FilePresentation::Docked
|
|
);
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().view,
|
|
crate::files::FileView::Tree
|
|
);
|
|
assert!(!session.editor().file_picker().unwrap().searching);
|
|
session.handle(Input::Char('i'));
|
|
for ch in "dir e\u{301}".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Backspace);
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "dir ");
|
|
session.handle(Input::Ctrl('h'));
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "dir ");
|
|
session.handle(Input::Escape);
|
|
assert!(!session.editor().file_picker().unwrap().searching);
|
|
assert!(session.editor().file_picker().unwrap().focused);
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "dir ");
|
|
session.handle(Input::Ctrl('u'));
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "");
|
|
assert!(!session.editor().file_picker().unwrap().searching);
|
|
session.handle(Input::Char('i'));
|
|
for ch in "dir ".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Ctrl('u'));
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "");
|
|
assert!(session.editor().file_picker().unwrap().searching);
|
|
for ch in "dir ".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "dir ");
|
|
session.handle(Input::Escape);
|
|
assert!(!session.editor().file_picker().unwrap().focused);
|
|
session.handle(Input::Char('i'));
|
|
for ch in " a b ".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &[" a b alpha"]);
|
|
session.handle(Input::Ctrl('o'));
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().presentation,
|
|
crate::files::FilePresentation::Floating
|
|
);
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().view,
|
|
crate::files::FileView::Tree
|
|
);
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "dir ");
|
|
assert!(session.editor().file_picker().unwrap().searching);
|
|
session.handle(Input::Escape);
|
|
assert!(session.editor().file_picker().unwrap().focused);
|
|
session.handle(Input::Escape);
|
|
assert!(session.editor().file_picker().is_none());
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["alpha"]);
|
|
}
|
|
|
|
#[test]
|
|
fn configured_flat_dock_and_tree_picker_keep_control_h_distinct_from_backspace() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.file_explorer_view = "list"
|
|
config.file_picker_view = "tree"
|
|
config.bind "ctrl+f", config.core.right, mode: config.modes.tree
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char(' '));
|
|
session.handle(Input::Char('e'));
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().view,
|
|
crate::files::FileView::List
|
|
);
|
|
for ch in "e\u{301}".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Ctrl('h'));
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "e\u{301}");
|
|
session.handle(Input::Backspace);
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "");
|
|
session.handle(Input::Ctrl('w'));
|
|
session.handle(Input::Char(' '));
|
|
session.handle(Input::Char('o'));
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().view,
|
|
crate::files::FileView::Tree
|
|
);
|
|
assert!(session.editor().file_picker().unwrap().searching);
|
|
session.handle(Input::Char('x'));
|
|
session.handle(Input::Ctrl('h'));
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "x");
|
|
session.handle(Input::Escape);
|
|
assert!(!session.editor().file_picker().unwrap().searching);
|
|
assert!(session.editor().file_picker().unwrap().focused);
|
|
assert_eq!(session.editor().file_picker().unwrap().query, "x");
|
|
session.handle(Input::Ctrl('t'));
|
|
assert_eq!(
|
|
session.editor().file_picker().unwrap().view,
|
|
crate::files::FileView::List
|
|
);
|
|
session.handle(Input::Escape);
|
|
assert!(session.editor().file_picker().is_none());
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('x'));
|
|
session.handle(Input::Ctrl('h'));
|
|
assert_eq!(session.editor().lines(), &["xalpha"]);
|
|
session.handle(Input::Backspace);
|
|
assert_eq!(session.editor().lines(), &["alpha"]);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_explorer_preferences_preserve_defaults_and_valid_bindings() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "abc");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.file_explorer_side = "diagonal"
|
|
config.file_explorer_view = "grid"
|
|
config.file_picker_view = "grid"
|
|
config.bind "n", config.core.right
|
|
end
|
|
"#,
|
|
);
|
|
assert_eq!(
|
|
session.editor().file_explorer_side(),
|
|
crate::files::DockSide::Right
|
|
);
|
|
assert_eq!(
|
|
session.editor().file_explorer_view(),
|
|
crate::files::FileView::Tree
|
|
);
|
|
session.handle(Input::Char('n'));
|
|
assert_eq!(
|
|
session.editor().cursor(),
|
|
crate::editor::Cursor { row: 0, column: 1 }
|
|
);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('!'));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &["a!bc"]);
|
|
}
|
|
|
|
#[test]
|
|
fn no_config_uses_space_leader_for_palette_and_write() {
|
|
let fixture = Fixture::new();
|
|
let buffer = fixture.file("buffer.txt", "base");
|
|
let mut session =
|
|
EditorSession::open(Some(buffer.clone()), vec!["text".into()], None).unwrap();
|
|
session.handle(Input::Char(' '));
|
|
assert_eq!(session.editor().leader_pending(), Some("Space"));
|
|
session.handle(Input::Char(' '));
|
|
assert!(session.editor().palette().is_some());
|
|
assert!(session.editor().leader_pending().is_none());
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('!'));
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char(' '));
|
|
session.handle(Input::Char('w'));
|
|
assert_eq!(fs::read_to_string(buffer).unwrap(), "!base");
|
|
assert!(!session.editor().dirty());
|
|
}
|
|
|
|
#[test]
|
|
fn ruby_leader_sequences_share_one_config_and_plugin_host_across_buffers() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha");
|
|
let second = fixture.file("second.txt", "beta");
|
|
fixture.file(
|
|
"shared.rb",
|
|
r##"
|
|
Vamp.plugin "shared", requires: ["text"]
|
|
calls = 0
|
|
Vamp.command("stamp") do |editor, _|
|
|
calls += 1
|
|
editor.insert "#{calls}:#{File.basename(editor.path)}:"
|
|
end
|
|
Vamp.command("fail") { |_, _| raise "shared host failure" }
|
|
"##,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
File.open("loads.txt", "a") { |file| file.puts "loaded" }
|
|
Vamp.configure do |config|
|
|
config.leader = ","
|
|
config.plugin "shared.rb"
|
|
config.bind "leader z n", "stamp"
|
|
config.bind "leader z f", "fail"
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char(','));
|
|
session.handle(Input::Char('z'));
|
|
assert_eq!(session.editor().leader_pending(), Some(", z"));
|
|
assert_eq!(session.editor().lines(), &["alpha"]);
|
|
session.handle(Input::Char('n'));
|
|
assert_eq!(session.editor().lines(), &["1:buffer.txt:alpha"]);
|
|
assert!(session.editor().leader_pending().is_none());
|
|
session.handle(Input::Char(','));
|
|
session.handle(Input::Char('z'));
|
|
session.execute(&format!("edit {}", second.display()));
|
|
assert!(session.editor().leader_pending().is_none());
|
|
for key in [',', 'z', 'n'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert_eq!(session.editor().lines(), &["2:second.txt:beta"]);
|
|
for key in [',', 'b', 'p'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert_eq!(session.editor().buffer_id(), 1);
|
|
session.handle(Input::Home);
|
|
for key in [',', 'z', 'n'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert_eq!(
|
|
session.editor().lines(),
|
|
&["3:buffer.txt:1:buffer.txt:alpha"]
|
|
);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("loads.txt")).unwrap(),
|
|
"loaded\n"
|
|
);
|
|
for key in [',', 'z', 'f'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains("Plugin shared disabled:")
|
|
);
|
|
for key in [',', 'z', 'n'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert_eq!(
|
|
session.editor().lines(),
|
|
&["3:buffer.txt:1:buffer.txt:alpha"]
|
|
);
|
|
session.handle(Input::Ctrl('p'));
|
|
assert!(
|
|
!session
|
|
.editor()
|
|
.palette()
|
|
.unwrap()
|
|
.entries
|
|
.iter()
|
|
.any(|entry| entry.owner == "shared")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn ruby_disabled_leader_suffix_and_cancellation_keep_counts_and_selections_safe() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "abcdef");
|
|
fixture.healthy();
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.leader = ","
|
|
config.plugin "healthy.rb"
|
|
config.bind "leader z n", "healthy"
|
|
config.bind "leader z x", nil
|
|
config.bind "leader z h", config.core.left, mode: config.modes.visual
|
|
end
|
|
"#,
|
|
);
|
|
for last in ['x', 'd'] {
|
|
for key in ['3', ',', 'z', last] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert!(session.editor().leader_pending().is_none());
|
|
assert_eq!(session.editor().lines(), &["abcdef"]);
|
|
session.handle(Input::Char('l'));
|
|
}
|
|
assert_eq!(session.editor().cursor().column, 2);
|
|
for key in ['3', ',', 'z'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
session.handle(Input::Paste("n:q!\n".into()));
|
|
assert!(session.editor().leader_pending().is_none());
|
|
session.handle(Input::Char('l'));
|
|
assert_eq!(session.editor().cursor().column, 3);
|
|
assert_eq!(session.editor().lines(), &["abcdef"]);
|
|
session.handle(Input::Char('v'));
|
|
let selected = session.editor().selections();
|
|
for key in ['3', ',', 'z'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Visual);
|
|
assert_eq!(session.editor().selections(), selected);
|
|
assert!(session.editor().leader_pending().is_none());
|
|
session.handle(Input::Char('h'));
|
|
assert_eq!(session.editor().cursor().column, 2);
|
|
assert_eq!(session.editor().lines(), &["abcdef"]);
|
|
assert!(!session.editor().should_quit());
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_backend_leader_retains_space_and_installs_valid_config_bindings() {
|
|
for key in ["not-a-key", "esc", "7", "i"] {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "base");
|
|
fixture.healthy();
|
|
let mut session = fixture.open(&format!(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.leader = "{key}"
|
|
config.plugin "healthy.rb"
|
|
config.bind "ctrl+f", config.core.right
|
|
config.bind "leader z n", "healthy"
|
|
end
|
|
"#,
|
|
));
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains(&format!("leader {key:?}:"))
|
|
);
|
|
assert!(!session.editor().message().contains("config disabled"));
|
|
session.handle(Input::Ctrl('f'));
|
|
assert_eq!(session.editor().cursor().column, 1);
|
|
session.handle(Input::Char(' '));
|
|
assert_eq!(session.editor().leader_pending(), Some("Space"));
|
|
session.handle(Input::Char('z'));
|
|
session.handle(Input::Char('n'));
|
|
assert_eq!(session.editor().lines(), &["healthy "]);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn buffers_share_config_and_plugin_host_but_keep_history_and_protect_hidden_edits() {
|
|
let fixture = Fixture::new();
|
|
let first = fixture.file("buffer.txt", "alpha");
|
|
let second = fixture.file("second.txt", "beta");
|
|
fixture.file(
|
|
"shared.rb",
|
|
r##"
|
|
Vamp.plugin "shared", requires: ["text"]
|
|
calls = 0
|
|
Vamp.command("stamp") do |editor, _|
|
|
calls += 1
|
|
editor.insert "#{calls}:#{File.basename(editor.path)}:"
|
|
end
|
|
Vamp.command("fail") { |_, _| raise "shared host failure" }
|
|
"##,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "shared.rb"
|
|
config.bind "g", "stamp"
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &["1:buffer.txt:alpha"]);
|
|
session.execute(&format!("edit {}", second.display()));
|
|
assert_eq!(session.editor().buffer_id(), 2);
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &["2:second.txt:beta"]);
|
|
session.execute("bdelete");
|
|
assert_eq!(session.editor().buffer_id(), 2);
|
|
assert_eq!(session.editor().buffer_count(), 2);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["beta"]);
|
|
session.execute("q");
|
|
assert!(!session.editor().should_quit());
|
|
session.execute("wq");
|
|
assert!(!session.editor().should_quit());
|
|
assert_eq!(fs::read_to_string(&first).unwrap(), "alpha");
|
|
assert_eq!(fs::read_to_string(&second).unwrap(), "beta");
|
|
session.execute("buffer 1");
|
|
assert_eq!(session.editor().lines(), &["1:buffer.txt:alpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["alpha"]);
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &["3:buffer.txt:alpha"]);
|
|
session.execute("w");
|
|
assert_eq!(fs::read_to_string(&first).unwrap(), "3:buffer.txt:alpha");
|
|
session.execute("bnext");
|
|
assert_eq!(session.editor().lines(), &["beta"]);
|
|
session.execute("fail");
|
|
assert!(session.editor().message().contains("disabled"));
|
|
session.execute("buffer 1");
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &["3:buffer.txt:alpha"]);
|
|
session.execute("q");
|
|
assert!(session.editor().should_quit());
|
|
}
|
|
|
|
#[test]
|
|
fn typed_palette_binding_prepares_arguments_and_retires_failed_plugin_commands() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "base");
|
|
fixture.file(
|
|
"palette.rb",
|
|
r#"
|
|
Vamp.plugin "palette_fixture", requires: ["text"]
|
|
Vamp.command("palette_insert") { |editor, args| editor.insert args }
|
|
Vamp.command("palette_fail") { |_, _| raise "palette fixture failure" }
|
|
"#,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "palette.rb"
|
|
config.bind "ctrl+p", nil
|
|
config.bind "ctrl+f", config.core.palette
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Ctrl('p'));
|
|
assert!(session.editor().palette().is_none());
|
|
session.handle(Input::Ctrl('f'));
|
|
for ch in "palette_insert".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
assert_eq!(
|
|
session.editor().palette().unwrap().entries[0].owner,
|
|
"palette_fixture"
|
|
);
|
|
session.handle(Input::Enter);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Command);
|
|
assert_eq!(session.editor().command_line(), "palette_insert ");
|
|
assert_eq!(session.editor().lines(), &["base"]);
|
|
for ch in "界!".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Enter);
|
|
assert_eq!(session.editor().lines(), &["界!base"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["base"]);
|
|
session.handle(Input::Ctrl('f'));
|
|
for ch in "palette_fail".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Enter);
|
|
session.handle(Input::Enter);
|
|
assert!(session.editor().message().contains("disabled"));
|
|
session.handle(Input::Ctrl('f'));
|
|
assert!(
|
|
!session
|
|
.editor()
|
|
.palette()
|
|
.unwrap()
|
|
.entries
|
|
.iter()
|
|
.any(|entry| entry.owner == "palette_fixture")
|
|
);
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &["base"]);
|
|
}
|
|
|
|
#[test]
|
|
fn configured_selection_actions_edit_together_and_invoke_plugins_once() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", " alpha\nb\n gamma");
|
|
fixture.file(
|
|
"once.rb",
|
|
r#"
|
|
Vamp.plugin "once", requires: ["text"]
|
|
Vamp.command("once") { |editor, _| editor.insert "!" }
|
|
"#,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "once.rb"
|
|
config.bind "r", config.core.select_lines, mode: config.modes.visual
|
|
config.bind "z", config.core.clone_below
|
|
config.bind "f", "once"
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('v'));
|
|
session.handle(Input::Char('j'));
|
|
session.handle(Input::Char('j'));
|
|
session.handle(Input::Char('r'));
|
|
assert_eq!(session.editor().selection_count(), 3);
|
|
session.handle(Input::Char('A'));
|
|
session.handle(Input::Char('?'));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &[" alpha?", "b?", " gamma?"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[" alpha", "b", " gamma"]);
|
|
assert_eq!(session.editor().selection_count(), 3);
|
|
session.handle(Input::Char('f'));
|
|
assert_eq!(session.editor().selection_count(), 1);
|
|
assert_eq!(
|
|
session
|
|
.editor()
|
|
.lines()
|
|
.iter()
|
|
.map(|line| line.matches('!').count())
|
|
.sum::<usize>(),
|
|
1
|
|
);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[" alpha", "b", " gamma"]);
|
|
assert_eq!(session.editor().selection_count(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn startup_rejection_installs_no_commands_and_keeps_other_plugins_and_core() {
|
|
let fixture = Fixture::new();
|
|
fixture.healthy();
|
|
fixture.file(
|
|
"graphics.rb",
|
|
r#"
|
|
Vamp.plugin "graphics", requires: ["graphics"]
|
|
Vamp.command("draw") { |editor, args| editor.insert "incompatible" }
|
|
"#,
|
|
);
|
|
fixture.file(
|
|
"collision.rb",
|
|
r#"
|
|
Vamp.plugin "collision", requires: ["text"]
|
|
Vamp.command("leaked") { |editor, args| editor.insert "collision" }
|
|
Vamp.command("q") { |editor, args| editor.insert "override" }
|
|
"#,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "graphics.rb"
|
|
config.plugin "collision.rb"
|
|
config.plugin "missing.rb"
|
|
config.plugin "healthy.rb"
|
|
config.bind "g", "healthy"
|
|
end
|
|
"#,
|
|
);
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains("unavailable capability: graphics")
|
|
);
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains("Command already registered: q")
|
|
);
|
|
for command in ["draw", "leaked"] {
|
|
session.execute(command);
|
|
assert_eq!(
|
|
session.editor().message(),
|
|
format!("Unknown command: {command}")
|
|
);
|
|
assert_eq!(session.editor().lines(), &[String::new()]);
|
|
}
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &["healthy "]);
|
|
session.execute("w");
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("buffer.txt")).unwrap(),
|
|
"healthy "
|
|
);
|
|
session.execute("q");
|
|
assert!(session.editor().should_quit());
|
|
}
|
|
|
|
#[test]
|
|
fn rejected_effects_discard_entire_transaction_and_retire_commands_and_bindings() {
|
|
let fixture = Fixture::new();
|
|
fixture.healthy();
|
|
fixture.file(
|
|
"invalid.rb",
|
|
r#"
|
|
Vamp.plugin "invalid", requires: ["text"]
|
|
Vamp.command "invalid" do |editor, args|
|
|
editor.insert "partial"
|
|
editor.message "\e"
|
|
end
|
|
Vamp.command("sibling") { |editor, args| editor.insert "must be retired too" }
|
|
"#,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "invalid.rb"
|
|
config.plugin "healthy.rb"
|
|
config.bind "g", "invalid"
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &[String::new()]);
|
|
assert!(!session.editor().dirty());
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains("Plugin invalid disabled: Unsupported control character")
|
|
);
|
|
let message = session.editor().message().to_owned();
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().message(), message);
|
|
for command in ["invalid", "sibling"] {
|
|
session.execute(command);
|
|
assert_eq!(
|
|
session.editor().message(),
|
|
format!("Unknown command: {command}")
|
|
);
|
|
}
|
|
session.execute("healthy survivor");
|
|
assert_eq!(session.editor().lines(), &["healthy survivor"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[String::new()]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().message(), "Nothing to undo");
|
|
}
|
|
|
|
#[test]
|
|
fn callback_crash_and_timeout_retire_plugin_without_losing_core_editing() {
|
|
for failure in ["raise 'callback failed'", "Process.exit!(7)", "sleep 30"] {
|
|
let fixture = Fixture::new();
|
|
fixture.healthy();
|
|
fixture.file(
|
|
"failing.rb",
|
|
&format!(
|
|
r#"
|
|
Vamp.plugin "failing", requires: ["text"]
|
|
Vamp.command "fail" do |editor, args|
|
|
editor.insert "never commit"
|
|
{failure}
|
|
end
|
|
"#
|
|
),
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "failing.rb"
|
|
config.plugin "healthy.rb"
|
|
config.bind "g", "fail"
|
|
end
|
|
"#,
|
|
);
|
|
session.execute("fail");
|
|
assert_eq!(session.editor().lines(), &[String::new()]);
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains("Plugin failing disabled:")
|
|
);
|
|
session.execute("fail");
|
|
assert_eq!(session.editor().message(), "Unknown command: fail");
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &[String::new()]);
|
|
session.execute("healthy survivor");
|
|
session.execute("w");
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('x'));
|
|
session.handle(Input::Escape);
|
|
session.execute("q");
|
|
assert!(!session.editor().should_quit());
|
|
session.execute("wq");
|
|
assert!(session.editor().should_quit());
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("buffer.txt")).unwrap(),
|
|
"xhealthy survivor"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn configured_bindings_preserve_modal_editing_history_and_literal_paste() {
|
|
let fixture = Fixture::new();
|
|
fixture.healthy();
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "healthy.rb"
|
|
config.bind "n", config.core.insert_first_nonblank
|
|
config.bind "i", nil
|
|
config.bind "ctrl+r", config.core.redo
|
|
config.bind "ctrl+c", config.core.normal, mode: config.modes.insert
|
|
config.bind "esc", nil, mode: config.modes.insert
|
|
config.bind "!", config.core.newline, mode: config.modes.insert
|
|
config.bind "shift+x", nil, mode: config.modes.insert
|
|
config.bind ":", nil
|
|
config.bind "space", config.core.command
|
|
config.bind "enter", nil, mode: config.modes.command
|
|
config.bind "ctrl+x", config.core.submit, mode: config.modes.command
|
|
config.bind "ctrl+c", config.core.normal, mode: config.modes.command
|
|
config.bind "ctrl+h", config.core.backspace, mode: config.modes.command
|
|
config.bind "g", config.core.undo
|
|
config.bind "g", "healthy"
|
|
config.bind "n", config.core.newline
|
|
config.bind "unknown", config.core.undo
|
|
config.bind "ctrl+f", "healthy", mode: config.modes.insert
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('i'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Char('n'));
|
|
for ch in "界Xab!".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
assert_eq!(session.editor().lines(), &["界ab", ""]);
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Insert);
|
|
session.handle(Input::Paste("X!".into()));
|
|
assert_eq!(session.editor().lines(), &["界ab", "X!"]);
|
|
session.handle(Input::Ctrl('C'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["界ab", ""]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[String::new()]);
|
|
session.handle(Input::Ctrl('r'));
|
|
session.handle(Input::Ctrl('r'));
|
|
assert_eq!(session.editor().lines(), &["界ab", "X!"]);
|
|
session.handle(Input::Char(':'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Char(' '));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Command);
|
|
assert!(session.editor().leader_pending().is_none());
|
|
for ch in "w界".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Backspace);
|
|
assert_eq!(session.editor().command_line(), "w");
|
|
session.handle(Input::Enter);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Command);
|
|
session.handle(Input::Ctrl('x'));
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("buffer.txt")).unwrap(),
|
|
"界ab\nX!"
|
|
);
|
|
session.handle(Input::Char(' '));
|
|
session.handle(Input::Char('q'));
|
|
session.handle(Input::Ctrl('c'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert!(session.editor().command_line().is_empty());
|
|
assert!(!session.editor().should_quit());
|
|
session.handle(Input::Char('g'));
|
|
assert_eq!(session.editor().lines(), &["healthy "]);
|
|
session.handle(Input::Char(' '));
|
|
session.handle(Input::Char('q'));
|
|
session.handle(Input::Ctrl('x'));
|
|
assert!(!session.editor().should_quit());
|
|
session.handle(Input::Char(' '));
|
|
session.handle(Input::Char('q'));
|
|
session.handle(Input::Char('!'));
|
|
session.handle(Input::Ctrl('x'));
|
|
assert!(session.editor().should_quit());
|
|
}
|
|
|
|
#[test]
|
|
fn default_dot_replaces_fresh_words_and_each_repeat_has_one_undo() {
|
|
let fixture = Fixture::new();
|
|
let buffer = fixture.file("buffer.txt", "old elephant zebra");
|
|
let mut session = EditorSession::open(Some(buffer), vec!["text".into()], None).unwrap();
|
|
session.editor.set_clipboard_mode("separate").unwrap();
|
|
for key in ['v', 'e', 'c'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
for ch in "item".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &["item elephant zebra"]);
|
|
for key in ['w', '3', '.'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert_eq!(session.editor().lines(), &["item item zebra"]);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["item elephant zebra"]);
|
|
session.handle(Input::Home);
|
|
for key in ['w', 'w', 'v', 'e', '.'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
assert_eq!(session.editor().lines(), &["item elephant item"]);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["item elephant zebra"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["old elephant zebra"]);
|
|
assert!(!session.editor().dirty());
|
|
}
|
|
|
|
#[test]
|
|
fn typed_repeat_remaps_use_resolved_motions_and_current_visual_ranges() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "old enormous tail end");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.bind ".", nil
|
|
config.bind ".", nil, mode: config.modes.visual
|
|
config.bind "r", config.core.repeat_last_edit
|
|
config.bind "r", config.core.repeat_last_edit, mode: config.modes.visual
|
|
config.bind "e", config.core.left, mode: config.modes.visual
|
|
config.bind "ctrl+f", config.core.word_end, mode: config.modes.visual
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('v'));
|
|
session.handle(Input::Ctrl('f'));
|
|
session.handle(Input::Char('c'));
|
|
for ch in "item".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char('w'));
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["item enormous tail end"]);
|
|
session.handle(Input::Char('r'));
|
|
assert_eq!(session.editor().lines(), &["item item tail end"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["item enormous tail end"]);
|
|
session.handle(Input::Home);
|
|
for key in ['w', 'v', '2'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
session.handle(Input::Ctrl('f'));
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Visual);
|
|
assert_eq!(session.editor().lines(), &["item enormous tail end"]);
|
|
session.handle(Input::Char('r'));
|
|
assert_eq!(session.editor().lines(), &["item item end"]);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["item enormous tail end"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["old enormous tail end"]);
|
|
}
|
|
|
|
#[test]
|
|
fn repeat_recipe_crosses_buffers_but_pending_insert_and_undo_stay_per_buffer() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha");
|
|
let second = fixture.file("second.txt", "beta");
|
|
let mut session = fixture.open("");
|
|
session.handle(Input::Char('i'));
|
|
for ch in "one".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.execute(&format!("edit {}", second.display()));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert_eq!(session.editor().lines(), &["beta"]);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["onebeta"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["beta"]);
|
|
session.execute("buffer 1");
|
|
assert_eq!(session.editor().lines(), &["onealpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["alpha"]);
|
|
session.handle(Input::Char('U'));
|
|
assert_eq!(session.editor().lines(), &["onealpha"]);
|
|
session.execute("buffer 2");
|
|
assert_eq!(session.editor().lines(), &["beta"]);
|
|
session.handle(Input::Char('U'));
|
|
assert_eq!(session.editor().lines(), &["onebeta"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('i'));
|
|
for ch in "two".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.execute("buffer 1");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["twoonealpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["onealpha"]);
|
|
session.execute("buffer 2");
|
|
assert_eq!(session.editor().lines(), &["twoonebeta"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["onebeta"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["beta"]);
|
|
}
|
|
|
|
#[test]
|
|
fn plugin_effects_commit_pending_typing_without_replacing_or_reinvoking_native_recipe() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "base");
|
|
fixture.file(
|
|
"effects.rb",
|
|
r##"
|
|
Vamp.plugin "effects", requires: ["text"]
|
|
calls = 0
|
|
Vamp.command("effect") do |editor, args|
|
|
calls += 1
|
|
File.open("calls.txt", "a") { |file| file.puts "#{calls} #{args}" }
|
|
case args
|
|
when "replace"
|
|
editor.replace "plugin"
|
|
when "insert"
|
|
editor.insert "?"
|
|
end
|
|
editor.message "calls=#{calls}"
|
|
end
|
|
"##,
|
|
);
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.plugin "effects.rb"
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('!'));
|
|
session.execute("effect replace");
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert_eq!(session.editor().lines(), &["plugin"]);
|
|
assert_eq!(session.editor().message(), "calls=1");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["!plugin"]);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("calls.txt")).unwrap(),
|
|
"1 replace\n"
|
|
);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["plugin"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["!base"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["base"]);
|
|
session.handle(Input::Home);
|
|
session.execute("effect insert");
|
|
assert_eq!(session.editor().lines(), &["?base"]);
|
|
assert_eq!(session.editor().message(), "calls=2");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["!?base"]);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("calls.txt")).unwrap(),
|
|
"1 replace\n2 insert\n"
|
|
);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["?base"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["base"]);
|
|
session.execute("effect message");
|
|
assert_eq!(session.editor().message(), "calls=3");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["!base"]);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("calls.txt")).unwrap(),
|
|
"1 replace\n2 insert\n3 message\n"
|
|
);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["base"]);
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_save_commits_pending_change_and_repeat_does_not_save_again() {
|
|
let fixture = Fixture::new();
|
|
let buffer = fixture.file("buffer.txt", "old elephant");
|
|
let mut session = fixture.open("");
|
|
for key in ['v', 'e', 'c'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
for ch in "item".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.execute("w");
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert_eq!(fs::read_to_string(&buffer).unwrap(), "item elephant");
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('w'));
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["item item"]);
|
|
assert!(session.editor().dirty());
|
|
assert_eq!(fs::read_to_string(&buffer).unwrap(), "item elephant");
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["item elephant"]);
|
|
assert!(!session.editor().dirty());
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["old elephant"]);
|
|
}
|
|
|
|
#[test]
|
|
fn dots_in_insert_and_focused_queries_are_literal_and_excluded_from_recipe() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "alpha");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.file_explorer_view = "list"
|
|
config.file_picker_view = "list"
|
|
config.bind "ctrl+f", config.core.file_explorer
|
|
config.bind "ctrl+o", config.core.file_picker
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('X'));
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Ctrl('f'));
|
|
for ch in ".query".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
assert_eq!(session.editor().file_picker().unwrap().query, ".query");
|
|
assert_eq!(session.editor().lines(), &["Xalpha"]);
|
|
session.handle(Input::Escape);
|
|
assert!(!session.editor().file_picker().unwrap().focused);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Ctrl('o'));
|
|
session.handle(Input::Ctrl('u'));
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().file_picker().unwrap().query, ".");
|
|
assert_eq!(session.editor().lines(), &["Xalpha"]);
|
|
session.handle(Input::Escape);
|
|
assert!(session.editor().file_picker().is_none());
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["XXalpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["Xalpha"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('.'));
|
|
session.handle(Input::Ctrl('p'));
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.palette()
|
|
.unwrap()
|
|
.entries
|
|
.iter()
|
|
.any(|entry| entry.name == "core.repeat_last_edit")
|
|
);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().palette().unwrap().query, ".");
|
|
assert_eq!(session.editor().lines(), &[".Xalpha"]);
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char('!'));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &[".!Xalpha"]);
|
|
for prompt in [':', '/'] {
|
|
session.handle(Input::Char(prompt));
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().command_line(), ".");
|
|
assert_eq!(session.editor().lines(), &[".!Xalpha"]);
|
|
session.handle(Input::Escape);
|
|
}
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &[".!.!Xalpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[".!Xalpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["Xalpha"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["alpha"]);
|
|
}
|
|
|
|
#[test]
|
|
fn repeated_clipboard_paste_uses_captured_text_after_a_different_yank() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "one two three");
|
|
let mut session = fixture.open("");
|
|
for key in ['v', 'e', 'y'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["oneone two three"]);
|
|
for key in ['w', 'v', 'e', 'y'] {
|
|
session.handle(Input::Char(key));
|
|
}
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["oneoneone two three"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["oneone two three"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["twooneone two three"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["oneone two three"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["one two three"]);
|
|
}
|
|
|
|
#[test]
|
|
fn literal_paste_repeats_without_dispatch_and_preserves_native_input_groups() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "seed");
|
|
let mut session = fixture.open("");
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('a'));
|
|
session.handle(Input::Paste(".:q!\r\n界".into()));
|
|
session.handle(Input::Char('b'));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &["a.:q!", "界bseed"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["a.:q!", "界seed"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["aseed"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["seed"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &["bseed"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["seed"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Paste(".:q!\r\n界".into()));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &[".:q!", "界seed"]);
|
|
session.handle(Input::Home);
|
|
session.handle(Input::Char('.'));
|
|
assert_eq!(session.editor().lines(), &[".:q!", ".:q!", "界界seed"]);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert!(!session.editor().should_quit());
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[".:q!", "界seed"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["seed"]);
|
|
}
|
|
|
|
#[test]
|
|
fn typed_visual_change_remaps_c_and_command_execution_commits_one_replacement() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "abc DEF tail");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.bind "c", nil, mode: config.modes.visual
|
|
config.bind "ctrl+r", config.core.change, mode: config.modes.visual
|
|
config.bind "ctrl+v", config.core.select
|
|
config.bind "ctrl+f", config.core.word_end, mode: config.modes.visual
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('c'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert!(!session.editor().dirty());
|
|
session.handle(Input::Ctrl('v'));
|
|
session.handle(Input::Ctrl('f'));
|
|
session.handle(Input::Char('c'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Visual);
|
|
assert_eq!(session.editor().lines(), &["abc DEF tail"]);
|
|
session.handle(Input::Ctrl('r'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Insert);
|
|
assert_eq!(session.editor().cursor().column, 0);
|
|
assert_eq!(session.editor().lines(), &[" DEF tail"]);
|
|
for ch in "cZ".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.execute("w");
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("buffer.txt")).unwrap(),
|
|
"cZ DEF tail"
|
|
);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["abc DEF tail"]);
|
|
session.handle(Input::Char('P'));
|
|
assert_eq!(session.editor().lines(), &["ababcc DEF tail"]);
|
|
}
|
|
|
|
#[test]
|
|
fn typed_visual_actions_copy_cut_paste_and_cancel_without_splitting_history() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "one two");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.bind "v", nil
|
|
config.bind "ctrl+v", config.core.select
|
|
config.bind "ctrl+c", config.core.copy, mode: config.modes.visual
|
|
config.bind "ctrl+x", config.core.cut, mode: config.modes.visual
|
|
config.bind "ctrl+g", config.core.normal, mode: config.modes.visual
|
|
config.bind "ctrl+f", config.core.word_end, mode: config.modes.visual
|
|
config.bind "ctrl+b", config.core.paste_before
|
|
config.bind "ctrl+p", config.core.paste_after
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('v'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Ctrl('v'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Visual);
|
|
session.handle(Input::Ctrl('f'));
|
|
assert_eq!(session.editor().selections()[0].end.column, 3);
|
|
session.handle(Input::Ctrl('c'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert!(!session.editor().dirty());
|
|
session.handle(Input::Ctrl('p'));
|
|
assert_eq!(session.editor().lines(), &["oonene two"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["one two"]);
|
|
session.handle(Input::Ctrl('v'));
|
|
session.handle(Input::Ctrl('f'));
|
|
session.handle(Input::Ctrl('x'));
|
|
assert_eq!(session.editor().lines(), &[" two"]);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Ctrl('b'));
|
|
assert_eq!(session.editor().lines(), &["one two"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[" two"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["one two"]);
|
|
assert!(!session.editor().dirty());
|
|
session.handle(Input::Ctrl('v'));
|
|
session.handle(Input::Ctrl('g'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert!(session.editor().selections().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn typed_word_motions_remap_keys_and_keep_insert_typing_in_one_undo_group() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "foo...bar baz");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.bind "w", nil
|
|
config.bind "ctrl+f", config.core.word_next
|
|
config.bind "ctrl+p", config.core.word_previous
|
|
config.bind "ctrl+e", config.core.word_end
|
|
config.bind "ctrl+a", config.core.big_word_next
|
|
config.bind "ctrl+z", config.core.big_word_previous
|
|
config.bind "ctrl+t", config.core.big_word_end
|
|
config.bind "ctrl+b", config.core.word_previous, mode: config.modes.insert
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('w'));
|
|
assert_eq!(session.editor().cursor().column, 0);
|
|
for (key, column) in [
|
|
('f', 3),
|
|
('e', 5),
|
|
('f', 6),
|
|
('p', 3),
|
|
('z', 0),
|
|
('a', 10),
|
|
('t', 12),
|
|
('z', 10),
|
|
('z', 0),
|
|
] {
|
|
session.handle(Input::Ctrl(key));
|
|
assert_eq!(session.editor().cursor().column, column, "{key}");
|
|
}
|
|
assert!(!session.editor().dirty());
|
|
session.handle(Input::Char('i'));
|
|
for ch in "wbeWBE".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Ctrl('b'));
|
|
assert_eq!(session.editor().cursor().column, 0);
|
|
session.handle(Input::Char('X'));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().lines(), &["XwbeWBEfoo...bar baz"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &["foo...bar baz"]);
|
|
assert!(!session.editor().dirty());
|
|
}
|
|
|
|
#[test]
|
|
fn typed_search_bindings_drive_navigation_and_the_search_prompt_only() {
|
|
let fixture = Fixture::new();
|
|
fixture.file("buffer.txt", "cat\ncat\ncat");
|
|
let mut session = fixture.open(
|
|
r#"
|
|
Vamp.configure do |config|
|
|
config.bind "ctrl+f", config.core.search
|
|
config.bind "ctrl+n", config.core.search_next
|
|
config.bind "ctrl+p", config.core.search_previous
|
|
config.bind "ctrl+x", config.core.submit, mode: config.modes.search
|
|
config.bind "ctrl+c", config.core.normal, mode: config.modes.search
|
|
config.bind "ctrl+h", config.core.backspace, mode: config.modes.search
|
|
config.bind "enter", nil, mode: config.modes.search
|
|
config.bind "/", nil
|
|
end
|
|
"#,
|
|
);
|
|
session.handle(Input::Char('/'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
session.handle(Input::Ctrl('f'));
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Search);
|
|
for ch in "cat界".chars() {
|
|
session.handle(Input::Char(ch));
|
|
}
|
|
session.handle(Input::Backspace);
|
|
session.handle(Input::Enter);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Search);
|
|
session.handle(Input::Ctrl('x'));
|
|
assert_eq!(session.editor().cursor().row, 1);
|
|
session.handle(Input::Ctrl('n'));
|
|
assert_eq!(session.editor().cursor().row, 2);
|
|
session.handle(Input::Ctrl('p'));
|
|
assert_eq!(session.editor().cursor().row, 1);
|
|
session.handle(Input::Ctrl('f'));
|
|
session.handle(Input::Char('q'));
|
|
session.handle(Input::Ctrl('c'));
|
|
session.handle(Input::Ctrl('n'));
|
|
assert_eq!(session.editor().cursor().row, 2);
|
|
assert!(!session.editor().dirty());
|
|
assert!(!session.editor().should_quit());
|
|
}
|
|
|
|
#[test]
|
|
fn typed_config_errors_discard_partial_bindings_and_leave_default_editing_available() {
|
|
let fixture = Fixture::new();
|
|
for (expression, error) in [
|
|
("config.core.reod", "NoMethodError"),
|
|
("config.modes.insret", "NoMethodError"),
|
|
("config.leader = 7", "leader key must be a String"),
|
|
("config.leader = nil", "leader key must be a String"),
|
|
(r#"config.leader = """#, "leader key must not be empty"),
|
|
(
|
|
"config.file_explorer_side = 7",
|
|
"file explorer side must be a String",
|
|
),
|
|
(
|
|
"config.file_explorer_side = nil",
|
|
"file explorer side must be a String",
|
|
),
|
|
(
|
|
r#"config.file_explorer_side = """#,
|
|
"file explorer side must not be empty",
|
|
),
|
|
(
|
|
"config.file_picker_view = 7",
|
|
"file picker view must be a String",
|
|
),
|
|
(
|
|
"config.file_picker_view = nil",
|
|
"file picker view must be a String",
|
|
),
|
|
(
|
|
r#"config.file_picker_view = """#,
|
|
"file picker view must not be empty",
|
|
),
|
|
(
|
|
"config.file_explorer_view = 7",
|
|
"file explorer view must be a String",
|
|
),
|
|
(
|
|
"config.file_explorer_view = nil",
|
|
"file explorer view must be a String",
|
|
),
|
|
(
|
|
r#"config.file_explorer_view = """#,
|
|
"file explorer view must not be empty",
|
|
),
|
|
(
|
|
"config.clipboard_mode = 7",
|
|
"clipboard mode must be a String",
|
|
),
|
|
(
|
|
"config.clipboard_mode = nil",
|
|
"clipboard mode must be a String",
|
|
),
|
|
(
|
|
r#"config.clipboard_mode = """#,
|
|
"clipboard mode must not be empty",
|
|
),
|
|
(r#"config.bind "n", "core.insert""#, "TypeError"),
|
|
(r#"config.bind "n", nil, mode: "insert""#, "TypeError"),
|
|
(r#"config.bind "n", config.modes.insert"#, "TypeError"),
|
|
(
|
|
r#"config.bind "n", nil, mode: config.core.insert"#,
|
|
"TypeError",
|
|
),
|
|
(r#"config.core.redo.name = "core.undo""#, "FrozenError"),
|
|
(
|
|
r#"config.core.redo.name.replace("core.undo")"#,
|
|
"FrozenError",
|
|
),
|
|
(r#"config.modes.insert.name = "normal""#, "FrozenError"),
|
|
(
|
|
r#"config.modes.insert.name.replace("normal")"#,
|
|
"FrozenError",
|
|
),
|
|
] {
|
|
let mut session = fixture.open(&format!(
|
|
"Vamp.configure do |config|\n config.leader = \",\"\n config.bind \"i\", nil\n {expression}\nend"
|
|
));
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.startup_diagnostic()
|
|
.unwrap()
|
|
.lines
|
|
.iter()
|
|
.any(|line| line.contains(error)),
|
|
"{expression}"
|
|
);
|
|
session.handle(Input::Enter);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('界'));
|
|
session.handle(Input::Escape);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert_eq!(session.editor().lines(), &["界"], "{expression}");
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), &[String::new()], "{expression}");
|
|
session.handle(Input::Char(' '));
|
|
assert_eq!(
|
|
session.editor().leader_pending(),
|
|
Some("Space"),
|
|
"{expression}"
|
|
);
|
|
session.handle(Input::Char(' '));
|
|
assert!(session.editor().palette().is_some(), "{expression}");
|
|
session.handle(Input::Escape);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn rejected_config_diagnostic_persists_until_dismissed_and_default_editing_survives() {
|
|
for (config, reason, dismiss) in [
|
|
("raise 'broken config'", "broken config", Input::Enter),
|
|
(
|
|
"Vamp.configure { |config| config.lsp['unsupported-language'] = true }",
|
|
"unsupported-language",
|
|
Input::Escape,
|
|
),
|
|
] {
|
|
let fixture = Fixture::new();
|
|
let mut session = fixture.open(config);
|
|
let diagnostic = session
|
|
.editor()
|
|
.startup_diagnostic()
|
|
.expect("config failure must be visible");
|
|
let text = diagnostic.lines.join("\n");
|
|
assert!(text.contains(&fixture.0.join("config.rb").display().to_string()));
|
|
assert!(text.contains(reason));
|
|
session.editor.set_message("unrelated background message");
|
|
session.poll();
|
|
for input in [
|
|
Input::Char('i'),
|
|
Input::Paste("accidental edit".into()),
|
|
Input::Ctrl('p'),
|
|
] {
|
|
session.handle(input);
|
|
}
|
|
assert_eq!(
|
|
session
|
|
.editor()
|
|
.startup_diagnostic()
|
|
.unwrap()
|
|
.lines
|
|
.join("\n"),
|
|
text
|
|
);
|
|
assert_eq!(session.editor().mode(), Mode::Normal);
|
|
assert_eq!(session.editor().lines(), &[""]);
|
|
session.handle(dismiss);
|
|
assert!(session.editor().startup_diagnostic().is_none());
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('x'));
|
|
session.handle(Input::Escape);
|
|
session.execute("wq");
|
|
assert!(session.editor().should_quit());
|
|
assert_eq!(
|
|
fs::read_to_string(fixture.0.join("buffer.txt")).unwrap(),
|
|
"x"
|
|
);
|
|
}
|
|
}
|
|
|
|
fn drain_until(session: &mut EditorSession, ready: impl Fn(&EditorSession) -> bool) {
|
|
// Process-I/O timeout, not a date-dependent assertion.
|
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3);
|
|
loop {
|
|
session.poll();
|
|
if ready(session) {
|
|
return;
|
|
}
|
|
assert!(
|
|
std::time::Instant::now() < deadline,
|
|
"plugin did not settle: {}",
|
|
session.editor().message()
|
|
);
|
|
std::thread::sleep(std::time::Duration::from_millis(2));
|
|
}
|
|
}
|
|
|
|
fn barrier(session: &mut EditorSession, message: &str) {
|
|
session.execute(&format!("fixture_barrier {message}"));
|
|
drain_until(session, |session| session.editor().message() == message);
|
|
}
|
|
|
|
fn diagnostic_effect(id: usize, revision: u64, message: &str) -> serde_json::Value {
|
|
serde_json::json!({
|
|
"type": "diagnostics", "buffer_id": id, "revision": revision,
|
|
"items": [{"start": {"row": 0, "column": 0}, "end": {"row": 0, "column": 1},
|
|
"severity": "warning", "message": message}]
|
|
})
|
|
}
|
|
|
|
fn publish(session: &mut EditorSession, effects: Vec<serde_json::Value>) {
|
|
session.execute(&format!(
|
|
"fixture_publish {}",
|
|
serde_json::json!({"effects": effects})
|
|
));
|
|
}
|
|
|
|
fn hold(session: &mut EditorSession, gate: &Path, effects: Vec<serde_json::Value>) {
|
|
session.execute(&format!(
|
|
"fixture_hold {}",
|
|
serde_json::json!({"gate": gate, "effects": effects})
|
|
));
|
|
}
|
|
|
|
fn release(session: &mut EditorSession, gate: &Path) {
|
|
fs::write(gate, "").unwrap();
|
|
let delivered = format!("delivered {}", gate.display());
|
|
drain_until(session, |session| session.editor().message() == delivered);
|
|
}
|
|
|
|
#[test]
|
|
fn async_documents_include_unnamed_hidden_versions_real_saves_renames_and_closes() {
|
|
let fixture = Fixture::new();
|
|
let mut session = fixture.asynchronous(None);
|
|
barrier(&mut session, "initial");
|
|
let initial = fixture.events();
|
|
assert_eq!(initial[0]["event"], "open");
|
|
assert_eq!(initial[0]["document"]["id"], 1);
|
|
assert_eq!(initial[0]["document"]["revision"], 0);
|
|
assert_eq!(initial[0]["document"]["path"], serde_json::Value::Null);
|
|
assert_eq!(initial[0]["document"]["text"], "");
|
|
assert_eq!(
|
|
initial[0]["document"]["root"],
|
|
session.editor().project_root().to_string_lossy().as_ref()
|
|
);
|
|
session.handle(Input::Char('i'));
|
|
for input in [
|
|
Input::Char('😀'),
|
|
Input::Char('e'),
|
|
Input::Char('\u{301}'),
|
|
Input::Enter,
|
|
] {
|
|
session.handle(input);
|
|
}
|
|
session.handle(Input::Escape);
|
|
let revision = session.editor().text_revision();
|
|
session.handle(Input::Char('h'));
|
|
session.handle(Input::Char('l'));
|
|
let renamed = fixture.0.join("saved unicode 😀.txt");
|
|
session.execute(&format!("w {}", renamed.display()));
|
|
session.execute("w");
|
|
session.execute(&format!("w {}", fixture.0.join("missing/failed").display()));
|
|
barrier(&mut session, "saved");
|
|
assert_eq!(session.editor().text_revision(), revision);
|
|
assert_eq!(
|
|
session.editor().documents().next().unwrap().save_revision,
|
|
2
|
|
);
|
|
assert_eq!(fs::read_to_string(&renamed).unwrap(), "😀e\u{301}\n");
|
|
let events = fixture.events();
|
|
assert_eq!(
|
|
events
|
|
.iter()
|
|
.filter(|event| event["event"] == "change")
|
|
.count(),
|
|
revision as usize
|
|
);
|
|
let kinds: Vec<_> = events
|
|
.iter()
|
|
.filter_map(|event| event["event"].as_str())
|
|
.collect();
|
|
assert_eq!(&kinds[kinds.len() - 4..], ["close", "open", "save", "save"]);
|
|
let reopened = events
|
|
.iter()
|
|
.find(|event| event["event"] == "open" && !event["document"]["path"].is_null())
|
|
.unwrap();
|
|
assert_eq!(reopened["document"]["revision"], revision);
|
|
assert_eq!(reopened["document"]["text"], "😀e\u{301}\n");
|
|
let target = fixture.file("target.txt", "target");
|
|
session.execute(&format!("edit {}", target.display()));
|
|
session.handle(Input::Char('x'));
|
|
session.execute("buffer 1");
|
|
let documents: Vec<_> = session.editor().documents().collect();
|
|
assert_eq!(
|
|
documents
|
|
.iter()
|
|
.map(|document| document.id)
|
|
.collect::<Vec<_>>(),
|
|
[1, 2]
|
|
);
|
|
assert_eq!(documents[1].lines, ["arget"]);
|
|
assert_eq!(documents[1].revision, 1);
|
|
assert_eq!(documents[1].save_revision, 0);
|
|
session.execute("bdelete! 2");
|
|
barrier(&mut session, "closed");
|
|
let events = fixture.events();
|
|
let closed = events
|
|
.iter()
|
|
.find(|event| event["event"] == "close" && event["document"]["id"] == 2)
|
|
.unwrap();
|
|
assert_eq!(closed["document"]["text"], "arget");
|
|
assert_eq!(closed["document"]["revision"], 1);
|
|
session.execute("q!");
|
|
barrier(&mut session, "quit flushed");
|
|
assert!(session.editor().should_quit());
|
|
let events = fixture.events();
|
|
let final_close = events
|
|
.iter()
|
|
.rev()
|
|
.find(|event| event["event"] == "close")
|
|
.unwrap();
|
|
assert_eq!(final_close["document"]["id"], 1);
|
|
assert_eq!(
|
|
final_close["document"]["path"],
|
|
renamed.to_string_lossy().as_ref()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn async_annotations_and_messages_preserve_insert_group_and_ignore_stale_closed_sources() {
|
|
let fixture = Fixture::new();
|
|
let file = fixture.file("buffer.txt", "abc");
|
|
let mut session = fixture.asynchronous(Some(file));
|
|
let gate = fixture.0.join("annotations-ready");
|
|
hold(&mut session, &gate, vec![diagnostic_effect(1, 1, "live")]);
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('x'));
|
|
release(&mut session, &gate);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Insert);
|
|
assert_eq!(
|
|
session
|
|
.editor()
|
|
.diagnostic_at_cursor()
|
|
.map(|diagnostic| diagnostic.message.as_str()),
|
|
None
|
|
);
|
|
assert_eq!(session.editor().diagnostics().count(), 1);
|
|
session.handle(Input::Char('y'));
|
|
assert_eq!(session.editor().diagnostics().count(), 0);
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), ["abc"]);
|
|
let current = session.editor().text_revision();
|
|
let mut stale = diagnostic_effect(1, 0, "stale");
|
|
stale["items"][0]["end"]["row"] = serde_json::json!(999);
|
|
publish(
|
|
&mut session,
|
|
vec![
|
|
stale,
|
|
diagnostic_effect(999, current, "closed"),
|
|
serde_json::json!({"type":"message","text":"stale checked"}),
|
|
],
|
|
);
|
|
drain_until(&mut session, |session| {
|
|
session.editor().message() == "stale checked"
|
|
});
|
|
assert_eq!(session.editor().diagnostics().count(), 0);
|
|
assert!(session.plugins.contains_key("generic_fixture"));
|
|
let target = fixture.file("closed.txt", "closed");
|
|
session.execute(&format!("edit {}", target.display()));
|
|
let gate = fixture.0.join("closed-ready");
|
|
hold(
|
|
&mut session,
|
|
&gate,
|
|
vec![diagnostic_effect(2, 0, "late close")],
|
|
);
|
|
session.execute("bdelete 2");
|
|
release(&mut session, &gate);
|
|
assert_eq!(session.editor().buffer_id(), 1);
|
|
assert_eq!(session.editor().diagnostics().count(), 0);
|
|
assert!(session.plugins.contains_key("generic_fixture"));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_annotation_batches_retire_owner_and_clear_hidden_annotations_only() {
|
|
let fixture = Fixture::new();
|
|
let file = fixture.file("buffer.txt", "source");
|
|
let target = fixture.file("target.txt", "target");
|
|
let mut session = fixture.asynchronous(Some(file));
|
|
session.execute(&format!("edit {}", target.display()));
|
|
session.execute("buffer 1");
|
|
publish(
|
|
&mut session,
|
|
vec![
|
|
diagnostic_effect(1, 0, "source"),
|
|
diagnostic_effect(2, 0, "hidden"),
|
|
],
|
|
);
|
|
drain_until(&mut session, |session| {
|
|
session.editor().diagnostics().count() == 1
|
|
});
|
|
let other = crate::plugin::Diagnostic {
|
|
start: TextPosition { row: 0, column: 0 },
|
|
end: TextPosition { row: 0, column: 1 },
|
|
severity: crate::plugin::DiagnosticSeverity::Hint,
|
|
message: "other owner".into(),
|
|
};
|
|
session
|
|
.editor
|
|
.replace_diagnostics("other", 1, 0, vec![other])
|
|
.unwrap();
|
|
let mut invalid = diagnostic_effect(1, 0, "bad range");
|
|
invalid["items"][0]["end"]["column"] = serde_json::json!(999);
|
|
publish(
|
|
&mut session,
|
|
vec![diagnostic_effect(2, 0, "must not commit"), invalid],
|
|
);
|
|
drain_until(&mut session, |session| {
|
|
!session.plugins.contains_key("generic_fixture")
|
|
});
|
|
assert!(
|
|
session
|
|
.editor()
|
|
.message()
|
|
.contains("Diagnostic position is outside")
|
|
);
|
|
assert_eq!(session.editor().diagnostics().count(), 1);
|
|
assert_eq!(
|
|
session.editor().diagnostic_at_cursor().unwrap().message,
|
|
"other owner"
|
|
);
|
|
session.execute("buffer 2");
|
|
assert_eq!(session.editor().diagnostics().count(), 0);
|
|
session.execute("fixture_publish {}");
|
|
assert!(session.editor().message().contains("Unknown command"));
|
|
session.handle(Input::Char('x'));
|
|
assert_eq!(session.editor().lines(), ["arget"]);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), ["target"]);
|
|
}
|
|
|
|
fn navigation_effect(path: &Path) -> serde_json::Value {
|
|
serde_json::json!({"type":"navigate", "path":path, "row":0, "column":1})
|
|
}
|
|
|
|
#[test]
|
|
fn async_navigation_reuses_dirty_target_and_reports_open_failure_without_retiring() {
|
|
let fixture = Fixture::new();
|
|
let source = fixture.file("buffer.txt", "source");
|
|
let target = fixture.file("dirty 😀 target.txt", "a😀e\u{301}");
|
|
let mut session = fixture.asynchronous(Some(source));
|
|
session.execute(&format!("edit {}", target.display()));
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('!'));
|
|
session.handle(Input::Escape);
|
|
session.execute("buffer 1");
|
|
publish(&mut session, vec![navigation_effect(&target)]);
|
|
drain_until(&mut session, |session| session.editor().buffer_id() == 2);
|
|
assert_eq!(session.editor().lines(), ["!a😀e\u{301}"]);
|
|
assert_eq!(
|
|
session.editor().cursor(),
|
|
crate::editor::Cursor { row: 0, column: 1 }
|
|
);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Normal);
|
|
assert_eq!(session.editor().buffer_count(), 2);
|
|
assert_eq!(fs::read_to_string(&target).unwrap(), "a😀e\u{301}");
|
|
let gate = fixture.0.join("missing-ready");
|
|
hold(
|
|
&mut session,
|
|
&gate,
|
|
vec![navigation_effect(&fixture.0.join("missing"))],
|
|
);
|
|
fs::write(&gate, "").unwrap();
|
|
drain_until(&mut session, |session| {
|
|
session
|
|
.requests
|
|
.get("generic_fixture")
|
|
.is_some_and(|requests| requests.pending.is_empty())
|
|
&& session.editor().message().starts_with("delivered ")
|
|
});
|
|
assert_eq!(session.editor().buffer_id(), 2);
|
|
assert_eq!(session.editor().lines(), ["!a😀e\u{301}"]);
|
|
assert!(session.plugins.contains_key("generic_fixture"));
|
|
// A standalone failure message remains visible when no later plugin message overwrites it.
|
|
publish(
|
|
&mut session,
|
|
vec![navigation_effect(&fixture.0.join("missing"))],
|
|
);
|
|
drain_until(&mut session, |session| {
|
|
session.editor().message().starts_with("Navigation failed:")
|
|
});
|
|
assert_eq!(session.editor().buffer_id(), 2);
|
|
assert_eq!(session.editor().buffer_count(), 2);
|
|
assert!(session.plugins.contains_key("generic_fixture"));
|
|
}
|
|
|
|
#[test]
|
|
fn async_navigation_drops_superseded_changed_or_inactive_source_replies() {
|
|
for guard in ["superseded", "changed", "inactive", "closed"] {
|
|
let fixture = Fixture::new();
|
|
let source = fixture.file("buffer.txt", "source");
|
|
let target = fixture.file("target.txt", "target");
|
|
let mut session = fixture.asynchronous(Some(source));
|
|
let gate = fixture.0.join("navigation-ready");
|
|
hold(&mut session, &gate, vec![navigation_effect(&target)]);
|
|
match guard {
|
|
"superseded" => barrier(&mut session, "new request"),
|
|
"changed" => {
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('x'));
|
|
}
|
|
"inactive" => {
|
|
let other = fixture.file("other.txt", "other");
|
|
session.execute(&format!("edit {}", other.display()));
|
|
}
|
|
"closed" => session.execute("bdelete 1"),
|
|
_ => unreachable!(),
|
|
}
|
|
let active = session.editor().buffer_id();
|
|
let text = session.editor().context().text;
|
|
release(&mut session, &gate);
|
|
assert_eq!(session.editor().buffer_id(), active, "{guard}");
|
|
assert_eq!(session.editor().context().text, text, "{guard}");
|
|
assert!(session.plugins.contains_key("generic_fixture"), "{guard}");
|
|
assert!(
|
|
!session
|
|
.editor()
|
|
.buffers()
|
|
.any(|buffer| buffer.path == Some(target.as_path())),
|
|
"{guard}"
|
|
);
|
|
let invokes: Vec<_> = fixture
|
|
.events()
|
|
.into_iter()
|
|
.filter(|event| event["invoke"] == "fixture_hold")
|
|
.collect();
|
|
assert_eq!(invokes.len(), 1);
|
|
assert_eq!(invokes[0]["buffer_id"], 1);
|
|
assert_eq!(invokes[0]["revision"], 0);
|
|
assert_eq!(invokes[0]["row"], 1);
|
|
assert_eq!(invokes[0]["column"], 1);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn stale_async_text_response_and_dead_host_never_break_native_typing() {
|
|
let fixture = Fixture::new();
|
|
let file = fixture.file("buffer.txt", "source");
|
|
let mut session = fixture.asynchronous(Some(file));
|
|
session.execute("fixture_replace BAD");
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('x'));
|
|
drain_until(&mut session, |session| {
|
|
session
|
|
.requests
|
|
.get("generic_fixture")
|
|
.is_some_and(|requests| requests.pending.is_empty())
|
|
});
|
|
assert_eq!(session.editor().lines(), ["xsource"]);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Insert);
|
|
session.handle(Input::Char('y'));
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), ["source"]);
|
|
let revision = session.editor().text_revision();
|
|
publish(
|
|
&mut session,
|
|
vec![diagnostic_effect(1, revision, "before crash")],
|
|
);
|
|
drain_until(&mut session, |session| {
|
|
session.editor().diagnostics().count() == 1
|
|
});
|
|
session.execute("fixture_crash");
|
|
session.handle(Input::Char('i'));
|
|
session.handle(Input::Char('z'));
|
|
drain_until(&mut session, |session| {
|
|
!session.plugins.contains_key("generic_fixture")
|
|
});
|
|
assert_eq!(session.editor().diagnostics().count(), 0);
|
|
assert_eq!(session.editor().mode(), crate::editor::Mode::Insert);
|
|
session.handle(Input::Char('q'));
|
|
session.handle(Input::Escape);
|
|
session.handle(Input::Char('u'));
|
|
assert_eq!(session.editor().lines(), ["source"]);
|
|
assert!(!session.editor().should_quit());
|
|
session.execute("plugins");
|
|
assert!(session.editor().message().contains("core [core]"));
|
|
}
|
|
|
|
#[test]
|
|
fn async_navigation_invokes_once_at_primary_with_multiple_carets() {
|
|
let fixture = Fixture::new();
|
|
let source = fixture.file("buffer.txt", "abc\nabc");
|
|
let target = fixture.file("target.txt", "target");
|
|
let mut session = fixture.asynchronous(Some(source));
|
|
session.handle(Input::Char('l'));
|
|
session.handle(Input::Ctrl('e'));
|
|
assert_eq!(session.editor().selection_count(), 2);
|
|
assert_eq!(
|
|
session.editor().cursor(),
|
|
crate::editor::Cursor { row: 1, column: 1 }
|
|
);
|
|
let gate = fixture.0.join("primary-ready");
|
|
hold(&mut session, &gate, vec![navigation_effect(&target)]);
|
|
release(&mut session, &gate);
|
|
assert_eq!(session.editor().path(), Some(target.as_path()));
|
|
assert_eq!(session.editor().selection_count(), 1);
|
|
let invokes: Vec<_> = fixture
|
|
.events()
|
|
.into_iter()
|
|
.filter(|event| event["invoke"] == "fixture_hold")
|
|
.collect();
|
|
assert_eq!(invokes.len(), 1);
|
|
assert_eq!(invokes[0]["row"], 2);
|
|
assert_eq!(invokes[0]["column"], 2);
|
|
assert_eq!(invokes[0]["revision"], 0);
|
|
}
|
|
}
|