Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exposing Automerge::get_marks through to Swift #186

Merged
merged 13 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/Automerge/Automerge.docc/Curation/Document.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
- ``textAt(obj:heads:)``
- ``lengthAt(obj:heads:)``
- ``marksAt(obj:heads:)``
- ``marksAt(obj:cursor:heads:)``
- ``marksAt(obj:position:heads:)``

### Saving, forking, and merging documents

Expand Down
55 changes: 55 additions & 0 deletions Sources/Automerge/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,61 @@ public final class Document: @unchecked Sendable {
}
}

/// Get the list of marks for a text object at ``Cursor``.
miguelangel-dev marked this conversation as resolved.
Show resolved Hide resolved
///
/// Use:
miguelangel-dev marked this conversation as resolved.
Show resolved Hide resolved
/// ```
/// let doc = Document()
/// let textId = try doc.putObject(obj: ObjId.ROOT, key: "text", ty: .Text)
///
/// let cursor = try doc.cursor(obj: textId, position: 0)
/// let marks = try doc.marksAt(obj: textId, cursor: cursor)
/// ```
///
/// - Parameters:
/// - obj: The identifier of the text object.
/// - cursor: The cursor that indicates a position within the text.
/// - heads: The set of ``ChangeHash`` that represents a point of time in the history the document.
miguelangel-dev marked this conversation as resolved.
Show resolved Hide resolved
/// - Returns: A list of ``Mark`` for the text object at the specified ``Cursor``.
public func marksAt(obj: ObjId, cursor: Cursor, heads: Set<ChangeHash>? = nil) throws -> [Mark] {
try sync {
try self.doc.wrapErrors {
try $0.marksCursor(
obj: obj.bytes,
cursor: cursor.bytes,
heads: heads?.map(\.bytes) ?? $0.heads()
).map(Mark.fromFfi)
}
}
}

/// Get the list of marks for a text object at specified position.
///
/// Use:
/// ```
/// let doc = Document()
/// let textId = try doc.putObject(obj: ObjId.ROOT, key: "text", ty: .Text)
///
/// let marks = try doc.marksAt(obj: textId, position: 0)
/// ```
///
/// - Parameters:
/// - obj: The identifier of the text object.
/// - position: The index that indicates the position of the text offset.
/// - heads: The set of ``ChangeHash`` that represents a point of time in the history the document.
miguelangel-dev marked this conversation as resolved.
Show resolved Hide resolved
/// - Returns: A list of ``Mark`` for the text object at the specified ``position``.
public func marksAt(obj: ObjId, position: UInt64, heads: Set<ChangeHash>? = nil) throws -> [Mark] {
try sync {
try self.doc.wrapErrors {
try $0.marksPosition(
obj: obj.bytes,
position: position,
heads: heads?.map(\.bytes) ?? $0.heads()
).map(Mark.fromFfi)
}
}
}

/// Commit the auto-generated transaction with options.
///
/// - Parameters:
Expand Down
25 changes: 25 additions & 0 deletions Tests/AutomergeTests/TestMarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,29 @@ class MarksTestCase: XCTestCase {
path: [PathElement(obj: ObjId.ROOT, prop: .Key("text"))]
)])
}

func testMarksPosition() throws {
let doc = Document()
let textId = try doc.putObject(obj: ObjId.ROOT, key: "text", ty: .Text)
try doc.spliceText(obj: textId, start: 0, delete: 0, value: "Hello World!")
try doc.mark(obj: textId, start: 2, end: 5, expand: .both, name: "italic", value: .Boolean(true))
try doc.mark(obj: textId, start: 1, end: 5, expand: .both, name: "bold", value: .Boolean(true))

let marks = try doc.marksAt(obj: textId, position: 2)

XCTAssertEqual(marks.count, 2)
}

func testMarksCursor() throws {
let doc = Document()
let textId = try doc.putObject(obj: ObjId.ROOT, key: "text", ty: .Text)
try doc.spliceText(obj: textId, start: 0, delete: 0, value: "Hello World!")
try doc.mark(obj: textId, start: 2, end: 5, expand: .both, name: "italic", value: .Boolean(true))
try doc.mark(obj: textId, start: 1, end: 5, expand: .both, name: "bold", value: .Boolean(true))

let cursor = try doc.cursor(obj: textId, position: 2)
let marks = try doc.marksAt(obj: textId, cursor: cursor)

XCTAssertEqual(marks.count, 2)
}
}
4 changes: 4 additions & 0 deletions rust/src/automerge.udl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ interface Doc {
sequence<Mark> marks(ObjId obj);
[Throws=DocError]
sequence<Mark> marks_at(ObjId obj, sequence<ChangeHash> heads);
[Throws=DocError]
sequence<Mark> marks_cursor(ObjId obj, Cursor cursor, sequence<ChangeHash> heads);
[Throws=DocError]
sequence<Mark> marks_position(ObjId obj, u64 position, sequence<ChangeHash> heads);

[Throws=DocError]
ObjId split_block(ObjId obj, u32 index);
Expand Down
44 changes: 38 additions & 6 deletions rust/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use automerge as am;
use automerge::{transaction::Transactable, ReadDoc};

use crate::actor_id::ActorId;
use crate::mark::{ExpandMark, Mark};
use crate::mark::{ExpandMark, KeyValue, Mark};
use crate::patches::Patch;
use crate::{
Change, ChangeHash, Cursor, ObjId, ObjType, PathElement, ScalarValue, SyncState, Value,
Expand Down Expand Up @@ -33,11 +33,6 @@ pub enum ReceiveSyncError {
InvalidMessage,
}

pub struct KeyValue {
pub key: String,
pub value: Value,
}

pub struct Doc(RwLock<automerge::AutoCommit>);

// These are okay because on the swift side we wrap all accesses of the
Expand Down Expand Up @@ -481,6 +476,43 @@ impl Doc {
.collect())
}

pub fn marks_cursor(
miguelangel-dev marked this conversation as resolved.
Show resolved Hide resolved
&self,
obj: ObjId,
cursor: Cursor,
heads: Vec<ChangeHash>,
) -> Result<Vec<Mark>, DocError> {
let obj = am::ObjId::from(obj);
let doc = self.0.read().unwrap();
assert_text(&*doc, &obj)?;
let heads = heads
.into_iter()
.map(am::ChangeHash::from)
.collect::<Vec<_>>();
let index = doc
.get_cursor_position(obj.clone(), &cursor.into(), Some(&heads))
.unwrap() as u64;
let markset = doc.get_marks(obj, index as usize, Some(&heads)).unwrap();
Ok(KeyValue::from_marks(markset, index))
}

pub fn marks_position(
&self,
obj: ObjId,
index: u64,
heads: Vec<ChangeHash>,
) -> Result<Vec<Mark>, DocError> {
let obj = am::ObjId::from(obj);
let doc = self.0.write().unwrap();
assert_text(&*doc, &obj)?;
let heads = heads
.into_iter()
.map(am::ChangeHash::from)
.collect::<Vec<_>>();
let markset = doc.get_marks(obj, index as usize, Some(&heads)).unwrap();
Ok(KeyValue::from_marks(markset, index))
}

pub fn split_block(&self, obj: ObjId, index: u32) -> Result<ObjId, DocError> {
let mut doc = self.0.write().unwrap();
let obj = am::ObjId::from(obj);
Expand Down
4 changes: 2 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use change::Change;
mod change_hash;
use change_hash::ChangeHash;
mod doc;
use doc::{Doc, DocError, KeyValue, LoadError, ReceiveSyncError};
use doc::{Doc, DocError, LoadError, ReceiveSyncError};
mod mark;
use mark::{ExpandMark, Mark};
use mark::{ExpandMark, KeyValue, Mark};
mod obj_id;
use obj_id::{root, ObjId};
mod obj_type;
Expand Down
23 changes: 22 additions & 1 deletion rust/src/mark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use automerge as am;

use crate::ScalarValue;
use crate::{ScalarValue, Value};
heckj marked this conversation as resolved.
Show resolved Hide resolved

pub enum ExpandMark {
Before,
Expand Down Expand Up @@ -37,3 +37,24 @@ impl<'a> From<&'a am::marks::Mark<'a>> for Mark {
}
}
}

pub struct KeyValue {
pub key: String,
pub value: Value,
heckj marked this conversation as resolved.
Show resolved Hide resolved
}

impl KeyValue {
pub fn from_marks(mark_set: am::marks::MarkSet, index: u64) -> Vec<Mark> {
let mut result = Vec::new();
for (key, value) in mark_set.iter() {
let mark = Mark {
start: index,
end: index,
name: key.to_string(),
value: value.into(),
};
result.push(mark);
}
result
}
}
1 change: 1 addition & 0 deletions rust/src/obj_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::UniffiCustomTypeConverter;
use automerge as am;

#[derive(Debug, Clone)]
pub struct ObjId(Vec<u8>);

impl From<ObjId> for automerge::ObjId {
Expand Down
Loading