use automerge::transaction::Transactable;
use automerge::{ObjId, ObjType, ROOT, ReadDoc, Value};
use serde::{Deserialize, Serialize};
use tilo_actions::{ActionError, ActionParam, ParamType};
use crate::model::{Doc, ModelError};
use crate::resource::{ActionDecl, Resource, SearchEntry};
pub const TEXT_KEY: &str = "text";
pub const AUTHOR_MARK: &str = "author";
pub fn mark_authored(
doc: &mut impl Transactable,
obj: &ObjId,
at: usize,
inserted: &str,
) -> Result<(), automerge::AutomergeError> {
let Some(author) = crate::action::writing_author() else {
return Ok(());
};
tilo_automerge::mark_text_author(doc, obj, at, inserted, &author_mark(&author))
}
#[must_use]
pub fn authored(encoding: automerge::TextEncoding, text: &str) -> automerge::hydrate::Text {
match crate::action::writing_author() {
Some(author) => tilo_automerge::authored_text(encoding, text, &author_mark(&author)),
None => automerge::hydrate::Text::new(encoding, text),
}
}
#[must_use]
pub fn author_mark(author: &tilo_action_policy::MemberId) -> String {
String::from_utf8_lossy(&author.actor_bytes()).into_owned()
}
pub const TEXT_ACTION_TYPE: &str = "tilo.text";
pub struct PlainText;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "kebab-case")]
pub enum TextEdit {
Splice {
at: usize,
delete: usize,
insert: String,
},
ReplaceText { text: String },
}
impl PlainText {
pub fn read_under(doc: &Doc, obj: &ObjId) -> Result<String, ModelError> {
doc.text(obj)
.map_err(|error| ModelError::invalid(format!("not a text: {error}")))
}
pub fn root_text(doc: &Doc) -> Option<ObjId> {
match doc.get(ROOT, TEXT_KEY) {
Ok(Some((Value::Object(ObjType::Text), obj))) => Some(obj),
_ => None,
}
}
}
pub fn copy_whole(
doc: &mut Doc,
from: &ObjId,
into: &ObjId,
) -> Result<(), automerge::AutomergeError> {
let text = doc.text(from)?;
doc.splice_text(into, 0, 0, &text)?;
for mark in doc.marks(from)? {
doc.mark(into, mark, automerge::marks::ExpandMark::None)?;
}
Ok(())
}
fn index(doc: &Doc, text: &str, chars: usize) -> usize {
let prefix: &str = text
.char_indices()
.nth(chars)
.and_then(|(byte, _)| text.get(..byte))
.unwrap_or(text);
match doc.text_encoding() {
automerge::TextEncoding::Utf8CodeUnit => prefix.len(),
automerge::TextEncoding::Utf16CodeUnit => prefix.encode_utf16().count(),
_ => prefix.chars().count(),
}
}
pub use tilo_actions::{common_ends, common_text_ends};
#[must_use]
pub fn splice_between(before: &str, after: &str) -> Option<TextEdit> {
tilo_actions::splice_between(before, after).map(|splice| TextEdit::Splice {
at: splice.at,
delete: splice.delete,
insert: splice.insert,
})
}
impl TextEdit {
pub fn apply_under(&self, doc: &mut Doc, obj: &ObjId) -> Result<(), ActionError> {
let current = doc
.text(obj)
.map_err(|_| ActionError::Refused("the text is gone".into()))?;
self.apply_to(doc, obj, ¤t)
}
pub fn apply_to(&self, doc: &mut Doc, obj: &ObjId, current: &str) -> Result<(), ActionError> {
let (at, delete, insert) = match self {
Self::Splice { at, delete, insert } => (*at, *delete, insert.clone()),
Self::ReplaceText { text } => match splice_between(current, text) {
Some(Self::Splice { at, delete, insert }) => (at, delete, insert),
_ => return Ok(()),
},
};
let length = current.chars().count();
if at > length || delete > length.saturating_sub(at) {
return Err(ActionError::Refused(format!(
"the text has {length} characters; the edit reaches past its end"
)));
}
let start = index(doc, current, at);
let end = index(doc, current, at.saturating_add(delete));
let removed = isize::try_from(end.saturating_sub(start))
.map_err(|_| ActionError::Refused("the edit is too large".into()))?;
doc.splice_text(obj, start, removed, &insert)?;
mark_authored(doc, obj, start, &insert)?;
Ok(())
}
}
impl tilo_actions::Action for TextEdit {
fn apply(&self, doc: &mut Doc) -> Result<(), ActionError> {
let obj = match PlainText::root_text(doc) {
Some(obj) => obj,
None => doc.put_object(ROOT, TEXT_KEY, ObjType::Text)?,
};
self.apply_under(doc, &obj)
}
}
impl Resource for PlainText {
const KIND: &'static str = "tilo.text";
const LABEL: &'static str = "Text";
const ACTION_TYPE: &'static str = TEXT_ACTION_TYPE;
type State = String;
type Action = TextEdit;
fn read(doc: &Doc) -> Result<String, ModelError> {
PlainText::root_text(doc)
.map_or_else(|| Ok(String::new()), |obj| Self::read_under(doc, &obj))
}
fn initialize(doc: &mut Doc) -> Result<(), ModelError> {
if PlainText::root_text(doc).is_none() {
doc.put_object(ROOT, TEXT_KEY, ObjType::Text)
.map_err(|error| ModelError::invalid(error.to_string()))?;
}
Ok(())
}
fn coalesces(previous: &TextEdit, next: &TextEdit) -> bool {
let (
TextEdit::Splice {
at: before_at,
delete: before_delete,
insert: before_insert,
},
TextEdit::Splice { at, delete, insert },
) = (previous, next)
else {
return false;
};
match (
*before_delete,
*delete,
before_insert.is_empty(),
insert.is_empty(),
) {
(0, 0, false, false) => {
*at == before_at.saturating_add(before_insert.chars().count())
&& !before_insert.ends_with(char::is_whitespace)
}
(1.., 1.., true, true) => at.saturating_add(*delete) == *before_at || at == before_at,
_ => false,
}
}
fn declarations() -> Vec<ActionDecl<Self>> {
vec![
ActionDecl::new(
"replace-text",
"Replace text",
vec![ActionParam::required(
"text",
ParamType::Text,
"The whole new text",
)],
|args, _| {
Ok(TextEdit::ReplaceText {
text: args.text("text").unwrap_or_default().to_owned(),
})
},
)
.describe("Replace the whole text; only what differs is written."),
]
}
fn action_schema() -> Option<serde_json::Value> {
Some(serde_json::json!({
"oneOf": [
{
"type": "object",
"description": "Replace `delete` characters at `at` with `insert`",
"properties": {
"action": {"const": "splice"},
"at": {"type": "integer", "minimum": 0},
"delete": {"type": "integer", "minimum": 0},
"insert": {"type": "string"}
},
"required": ["action", "at", "delete", "insert"],
"additionalProperties": false
},
{
"type": "object",
"description": "Replace the whole text",
"properties": {
"action": {"const": "replace-text"},
"text": {"type": "string"}
},
"required": ["action", "text"],
"additionalProperties": false
}
]
}))
}
fn search(state: &String) -> Vec<SearchEntry> {
let title = state.lines().next().unwrap_or_default().trim();
if title.is_empty() {
return Vec::new();
}
vec![SearchEntry::new(title, state.clone())]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Replica;
use tilo_automerge::ApplyIncoming;
fn spliced(text: &str, edit: &TextEdit) -> String {
let mut doc = Doc::new();
let obj = doc.put_object(ROOT, "body", ObjType::Text).unwrap();
doc.splice_text(&obj, 0, 0, text).unwrap();
edit.apply_under(&mut doc, &obj).unwrap();
doc.text(&obj).unwrap()
}
tilo_test_env::scenario! {
fn text_is_spliced_by_character_and_merges_concurrent_typing;
phase each_authors_typing_is_marked_with_its_author;
{
use automerge::marks::Mark;
let typed_by = |doc: &mut Doc, who: &str, edit: TextEdit| {
let encoded = tilo_actions::EncodedAction::encode(TEXT_ACTION_TYPE, &edit).unwrap();
let context = tilo_actions::ActionContext::for_resource("text", "tilo.text", "Text")
.with_invoker(tilo_actions::ActionInvoker::person(who));
tilo_actions::apply_local_with_context(doc, &edit, encoded, None, context).unwrap();
};
let mut doc = Doc::new();
typed_by(
&mut doc,
"alice",
TextEdit::Splice {
at: 0,
delete: 0,
insert: "héllo".into(),
},
);
typed_by(
&mut doc,
"bob",
TextEdit::Splice {
at: 5,
delete: 0,
insert: " wörld".into(),
},
);
let obj = PlainText::root_text(&doc).unwrap();
let text = doc.text(&obj).unwrap();
assert_eq!(text, "héllo wörld");
let runs: Vec<(String, String)> = doc
.marks(&obj)
.unwrap()
.into_iter()
.filter(|mark: &Mark| mark.name() == AUTHOR_MARK)
.map(|mark| {
let run: String = text
.chars()
.skip(mark.start)
.take(mark.end.saturating_sub(mark.start))
.collect();
(mark.value().as_str().unwrap_or_default().to_owned(), run)
})
.collect();
assert_eq!(
runs,
vec![
("alice".to_owned(), "héllo".to_owned()),
("bob".to_owned(), " wörld".to_owned()),
],
"each person's typing carries the actor their changes are named by"
);
let mut plain = Doc::new();
let obj = plain.put_object(ROOT, "body", ObjType::Text).unwrap();
TextEdit::Splice {
at: 0,
delete: 0,
insert: "x".into(),
}
.apply_under(&mut plain, &obj)
.unwrap();
assert!(plain.marks(&obj).unwrap().is_empty());
}
phase a_text_made_whole_is_marked_as_a_store_marks_it;
{
use crate::model::{Fresh, Model};
let _writer = crate::action::ResourceActorScope::enter(
tilo_action_policy::MemberId::agent("helper", "ada"),
);
let actor = automerge::ActorId::from(b"writer".as_slice());
for text in ["", "héllo wörld"] {
let mut made = Doc::new().with_actor(actor.clone());
let Fresh::Value(value) = text.to_owned().fresh(made.text_encoding()) else {
panic!("a text is made with its map");
};
made.batch_create_object(ROOT, "title", &value, false)
.unwrap();
let mut stored = Doc::new().with_actor(actor.clone());
let obj = stored.put_object(ROOT, "title", ObjType::Text).unwrap();
stored.splice_text(&obj, 0, 0, text).unwrap();
mark_authored(&mut stored, &obj, 0, text).unwrap();
assert_eq!(
stored.marks(&obj).unwrap().len(),
usize::from(!text.is_empty()),
"{text:?} is the helper's"
);
assert_eq!(made.save(), stored.save(), "{text:?}");
}
}
phase a_copied_text_keeps_its_words_and_its_marks;
{
use automerge::marks::{ExpandMark, Mark};
let mut doc = Doc::new();
let from = doc.put_object(ROOT, "from", ObjType::Text).unwrap();
doc.splice_text(&from, 0, 0, "hello world").unwrap();
doc.mark(
&from,
Mark::new("strong".to_owned(), true, 0, 5),
ExpandMark::After,
)
.unwrap();
let into = doc.put_object(ROOT, "into", ObjType::Text).unwrap();
copy_whole(&mut doc, &from, &into).unwrap();
assert_eq!(doc.text(&into).unwrap(), "hello world");
let marks: Vec<(String, usize, usize)> = doc
.marks(&into)
.unwrap()
.into_iter()
.map(|mark| (mark.name().to_owned(), mark.start, mark.end))
.collect();
assert_eq!(marks, vec![("strong".to_owned(), 0, 5)]);
}
phase a_splice_counts_characters_not_bytes;
{
let edit = TextEdit::Splice {
at: 2,
delete: 1,
insert: "ö".into(),
};
assert_eq!(spliced("naïve", &edit), "naöve");
}
phase a_splice_past_the_end_is_refused_and_writes_nothing;
{
let mut doc = Doc::new();
let obj = doc.put_object(ROOT, "body", ObjType::Text).unwrap();
doc.splice_text(&obj, 0, 0, "abc").unwrap();
let edit = TextEdit::Splice {
at: 2,
delete: 5,
insert: String::new(),
};
assert!(matches!(
edit.apply_under(&mut doc, &obj),
Err(ActionError::Refused(_))
));
assert_eq!(doc.text(&obj).unwrap(), "abc");
}
phase replacing_the_text_keeps_a_members_concurrent_typing;
{
let mut ours = Doc::new();
let obj = ours.put_object(ROOT, "body", ObjType::Text).unwrap();
ours.splice_text(&obj, 0, 0, "select 1").unwrap();
let mut theirs = ours.fork();
theirs.splice_text(&obj, 8, 0, ";").unwrap();
TextEdit::ReplaceText {
text: "SELECT 1".into(),
}
.apply_under(&mut ours, &obj)
.unwrap();
ours.merge_incoming(&mut theirs).unwrap();
assert_eq!(ours.text(&obj).unwrap(), "SELECT 1;");
}
phase the_smallest_splice_keeps_what_both_texts_share;
{
assert_eq!(
splice_between("hello world", "hello brave world"),
Some(TextEdit::Splice {
at: 6,
delete: 0,
insert: "brave ".into()
})
);
assert_eq!(
splice_between("aaa", "aa"),
Some(TextEdit::Splice {
at: 2,
delete: 1,
insert: String::new()
})
);
assert_eq!(splice_between("same", "same"), None);
}
phase byte_ends_land_where_character_ends_do;
{
let atoms = [
"", "a", "é", "e\u{301}", "😀", "😁", "\u{100}", "\u{101}", "\n",
];
for a in atoms {
for b in atoms {
for c in atoms {
for d in atoms {
let (left, right) = (format!("{a}{b}{c}"), format!("{a}{d}{c}"));
let chars = |text: &str| text.chars().collect::<Vec<_>>();
let (prefix, suffix) = common_ends(&chars(&left), &chars(&right));
let (start, end) = common_text_ends(&left, &right);
let (head, _) = left.split_at(start);
let (_, tail) = left.split_at(left.len().saturating_sub(end));
assert_eq!(
(head.chars().count(), tail.chars().count()),
(prefix, suffix),
"{left:?} -> {right:?}"
);
}
}
}
}
}
phase typing_a_word_coalesces_until_a_space;
{
let typed = |at: usize, insert: &str| TextEdit::Splice {
at,
delete: 0,
insert: insert.into(),
};
assert!(PlainText::coalesces(&typed(0, "h"), &typed(1, "i")));
assert!(!PlainText::coalesces(&typed(0, "h"), &typed(3, "i")));
assert!(!PlainText::coalesces(&typed(0, " "), &typed(1, "i")));
let deleted = |at: usize| TextEdit::Splice {
at,
delete: 1,
insert: String::new(),
};
assert!(PlainText::coalesces(&deleted(4), &deleted(3)));
assert!(!PlainText::coalesces(&typed(4, "x"), &deleted(4)));
}
phase a_record_of_its_own_text_replays_its_edits;
{
let mut replica = Replica::<PlainText>::new().in_app("so.tilo.model", "Model");
replica
.apply(
crate::Invoke::<PlainText>::apply(&TextEdit::Splice {
at: 0,
delete: 0,
insert: "draft".into(),
})
.unwrap(),
)
.unwrap();
assert_eq!(replica.state().unwrap().as_str(), "draft");
}
}
}