Skip to content

Commit 7f6afbf

Browse files
committed
chore: GTK offset naming for char-based positions
Stay consistent with how GTK names char-based positioning.
1 parent dc38e55 commit 7f6afbf

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

reflection-app/src/textbuffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ mod imp {
143143
#[upgrade_or]
144144
None,
145145
move |values| {
146-
let pos: i32 = values.get(1).unwrap().get().unwrap();
146+
let offset: i32 = values.get(1).unwrap().get().unwrap();
147147
let text: &str = values.get(2).unwrap().get().unwrap();
148148
if buffer.inhibit_text_change() {
149149
return None;
150150
}
151151

152-
let mut pos_iter = buffer.iter_at_offset(pos);
152+
let mut iter = buffer.iter_at_offset(offset);
153153
buffer.set_inhibit_text_change(true);
154-
buffer.insert(&mut pos_iter, text);
154+
buffer.insert(&mut iter, text);
155155
buffer.set_inhibit_text_change(false);
156156

157157
None
@@ -311,7 +311,7 @@ mod imp {
311311
if let Err(error) = result {
312312
error!("Failed to submit changes to the document: {error}");
313313
} else {
314-
info!("inserting new text {} at pos {}", new_text, offset);
314+
info!("inserting new text {} at offset {}", new_text, offset);
315315
self.parent_insert_text(iter, new_text);
316316
}
317317
}

reflection-doc/src/document.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,21 @@ mod imp {
258258
}
259259
}
260260

261-
pub fn insert_text(&self, index: usize, chunk: &str) -> Result<()> {
261+
pub fn insert_text(&self, offset: usize, chunk: &str) -> Result<()> {
262262
let doc = self.crdt_doc.get().expect("crdt_doc to be set");
263263
let text = doc.get_text(&*TEXT_CONTAINER_ID);
264264

265-
text.insert(index, chunk)?;
265+
text.insert(offset, chunk)?;
266266
doc.commit();
267267

268268
Ok(())
269269
}
270270

271-
pub fn delete_text(&self, index: usize, len: usize) -> Result<()> {
271+
pub fn delete_text(&self, offset: usize, len: usize) -> Result<()> {
272272
let doc = self.crdt_doc.get().expect("crdt_doc to be set");
273273
let text = doc.get_text(&*TEXT_CONTAINER_ID);
274274

275-
text.delete(index, len)?;
275+
text.delete(offset, len)?;
276276
doc.commit();
277277

278278
Ok(())
@@ -468,21 +468,24 @@ mod imp {
468468
// Loro's text deltas are represented as QuillJS "Deltas"
469469
// See: https://quilljs.com/docs/delta/
470470
for commit in text_deltas {
471-
let mut index = 0;
471+
// The `retain` and `delete` integers coming from Loro are the number of
472+
// unicode codepoints (_not_ utf8 bytes or "characters"). In GTK this is
473+
// called an "offset".
474+
let mut offset = 0;
472475
for delta in commit {
473476
match delta {
474477
loro::TextDelta::Retain { retain, .. } => {
475-
index += retain;
478+
offset += retain;
476479
}
477480
loro::TextDelta::Insert { insert, .. } => {
478-
let len = insert.chars().count();
479-
obj.imp().emit_text_inserted(index as i32, insert);
480-
index += len;
481+
let chars_count = insert.chars().count();
482+
obj.imp().emit_text_inserted(offset as i32, insert);
483+
offset += chars_count;
481484
}
482485
loro::TextDelta::Delete { delete } => {
483486
obj.imp().emit_range_deleted(
484-
index as i32,
485-
(index + delete) as i32,
487+
offset as i32,
488+
(offset + delete) as i32,
486489
);
487490
}
488491
}
@@ -739,8 +742,8 @@ impl Document {
739742
.build()
740743
}
741744

742-
pub fn insert_text(&self, pos: i32, text: &str) -> Result<()> {
743-
self.imp().insert_text(pos as usize, text)
745+
pub fn insert_text(&self, offset: i32, text: &str) -> Result<()> {
746+
self.imp().insert_text(offset as usize, text)
744747
}
745748

746749
pub fn delete_range(&self, start_pos: i32, end_pos: i32) -> Result<()> {

0 commit comments

Comments
 (0)