@@ -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