Skip to content

Commit

Permalink
continue iterating..
Browse files Browse the repository at this point in the history
- figured out how to get the individual "splice" and "delete" ops for
  the changes coming in from network (ie. Patches), we can now apply
  those to the textView

- still lots of FIXMEs
  • Loading branch information
jonas2515 committed Dec 9, 2024
1 parent 71e6439 commit d018d41
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
74 changes: 51 additions & 23 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use gettextrs::gettext;
use gtk::{gio, glib};
use tokio::sync::{mpsc, oneshot};
use automerge::ScalarValue;
use automerge::PatchAction;

use crate::config::VERSION;
use crate::glib::closure_local;
Expand All @@ -54,20 +55,10 @@ mod imp {
fn update_text(&self, position: i32, del: i32, text: &str) {
let mut doc = self.automerge.borrow_mut();

let current_text = match doc.get(automerge::ROOT, "root").expect("root exists") {
/*let current_text = match doc.get(automerge::ROOT, "root").expect("root exists") {
Some((_, root)) => doc.text(&root).unwrap(),
None => "".to_owned(),
};

println!("CMP '{}' == '{}'", current_text, text);

if text == "" {
return;
}

if text == current_text {
return;
}
};*/

let root = match doc.get(automerge::ROOT, "root").expect("root exists") {
Some(root) => root.1,
Expand All @@ -77,15 +68,30 @@ mod imp {
};
println!("root = {}", root);

// doc.update_text(&root, text).unwrap();
doc.splice(
&root,
position as usize,
del as isize,
text
.chars()
.map(|character| ScalarValue::Str(character.to_string().into()))
);
doc.splice_text(&root,position as usize, del as isize, text).unwrap();

// move the diff pointer forward to current position
//doc.update_diff_cursor();
let patches = doc.diff_incremental();
for patch in patches.iter() {
match &patch.action {
PatchAction::SpliceText { index, value, marks } => {
},
PatchAction::DeleteSeq { index, length } => {
},
PatchAction::PutMap { key: _, value: _, conflict: _ } => {},
PatchAction::PutSeq { index: _, value: _, conflict: _ } => {},
PatchAction::Insert { index: _, values: _ } => {},
PatchAction::Increment { prop: _, value: _ } => {},
PatchAction::Conflict { prop: _ } => {},
PatchAction::DeleteMap { key: _ } => {},
PatchAction::Mark { marks: _ } => {},
}
// there's probably either PatchAction::SpliceText or PatchAction::DeleteSeq
// in here, and those we can now apply using splice_text_view
println!("{}", patch.action);
//w.splice_text_view(pos, del, &text);
}

{
let bytes = doc.save_incremental();
Expand Down Expand Up @@ -164,12 +170,34 @@ mod imp {
.expect("inserting map at root"),
};
println!("root = {}", root);

// get the latest changes
let patches = doc_local.diff_incremental();
for patch in patches.iter() {
println!("PATCH: {}", patch.action);
match &patch.action {
PatchAction::SpliceText { index, value, marks: _ } => {
// FIXME: actually pass the value here instead of an empty string
w.splice_text_view(*index as i32, 0, "");
},
PatchAction::DeleteSeq { index, length } => {
w.splice_text_view(*index as i32, *length as i32, "");
},
PatchAction::PutMap { key: _, value: _, conflict: _ } => {},
PatchAction::PutSeq { index: _, value: _, conflict: _ } => {},
PatchAction::Insert { index: _, values: _ } => {},
PatchAction::Increment { prop: _, value: _ } => {},
PatchAction::Conflict { prop: _ } => {},
PatchAction::DeleteMap { key: _ } => {},
PatchAction::Mark { marks: _ } => {},
}
}

doc_local.text(&root).unwrap()
};
dbg!(&text);

println!("SET_TEXT = '{}'", text);
w.splice_text_view(0, 0, &text);
println!("new final text = '{}'", text);
}
});

Expand Down
9 changes: 6 additions & 3 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ impl AardvarkWindow {
let window = self.imp();
let buffer = window.text_view.buffer();

let s = buffer.text(&buffer.start_iter(), &buffer.end_iter(), false);
if text == s { println!("bailing out on superfluous set_text"); return; }
//buffer.set_text(text);
if del != 0 {
// FIXME: delete stuff from buffer here
return;
}

let mut pos_iter = buffer.iter_at_offset(pos);
// FIXME: need to tell AardvarkTextBuffer to not emit ::text-change signal during this insert op
// to avoid a loop
buffer.insert(&mut pos_iter, text);
}

Expand Down

0 comments on commit d018d41

Please sign in to comment.