@@ -188,9 +188,19 @@ impl Syntax {
188188 syntax
189189 }
190190
191− pub fn update(&mut self, source: &Rope, changeset: &ChangeSet) -> Result<(), Error> {
192− self.root_layer
193− .update(&mut self.parser, &self.config, source, changeset)
191+ pub fn update(
192+ &mut self,
193+ old_source: &Rope,
194+ source: &Rope,
195+ changeset: &ChangeSet,
196+ ) -> Result<(), Error> {
197+ self.root_layer.update(
198+ &mut self.parser,
199+ &self.config,
200+ old_source,
201+ source,
202+ changeset,
203+ )
194204
195205 // TODO: deal with injections and update them too
196206 }
@@ -385,7 +395,7 @@ impl LanguageLayer {
385395 }
386396
387397 pub(crate) fn generate_edits(
388− text: &RopeSlice,
398+ old_text: &RopeSlice,
389399 changeset: &ChangeSet,
390400 ) -> Vec<tree_sitter::InputEdit> {
391401 use Operation::*;
@@ -399,7 +409,7 @@ impl LanguageLayer {
399409 // TODO; this is a lot easier with Change instead of Operation.
400410
401411 fn point_at_pos(text: &RopeSlice, pos: usize) -> (usize, Point) {
402− let byte = text.char_to_byte(pos);
412+ let byte = text.char_to_byte(pos); // <- attempted to index past end
403413 let line = text.char_to_line(pos);
404414 let line_start_byte = text.line_to_byte(line);
405415 let col = byte - line_start_byte;
@@ -437,8 +447,8 @@ impl LanguageLayer {
437447 new_pos += len;
438448 }
439449 Delete(_) => {
440− let (start_byte, start_position) = point_at_pos(&text, old_pos);
441− let (old_end_byte, old_end_position) = point_at_pos(&text, old_end);
450+ let (start_byte, start_position) = point_at_pos(&old_text, old_pos);
451+ let (old_end_byte, old_end_position) = point_at_pos(&old_text, old_end);
442452
443453 // TODO: Position also needs to be byte based...
444454 // let byte = char_to_byte(old_pos)
@@ -475,7 +485,7 @@ impl LanguageLayer {
475485 };
476486 }
477487 Insert(s) => {
478− let (start_byte, start_position) = point_at_pos(&text, old_pos);
488+ let (start_byte, start_position) = point_at_pos(&old_text, old_pos);
479489
480490 let ins = s.chars().count();
481491
@@ -501,6 +511,7 @@ impl LanguageLayer {
501511 &mut self,
502512 parser: &mut Parser,
503513 config: &HighlightConfiguration,
514+ old_source: &Rope,
504515 source: &Rope,
505516 changeset: &ChangeSet,
506517 ) -> Result<(), Error> {
@@ -508,7 +519,7 @@ impl LanguageLayer {
508519 return Ok(());
509520 }
510521
511− let edits = Self::generate_edits(&source.slice(..), changeset);
522+ let edits = Self::generate_edits(&old_source.slice(..), changeset);
512523
513524 // Notify the tree about all the changes
514525 for edit in edits {
@@ -1530,4 +1541,23 @@ fn test_input_edits() {
15301541 }
15311542 ]
15321543 );
1544+
1545+ // Testing with the official example from tree-sitter
1546+ let mut state = State::new("fn test() {}".into());
1547+ let transaction = Transaction::change(&state, vec![(8, 8, Some("a: u32".into()))].into_iter());
1548+ let edits = LanguageLayer::generate_edits(&state.doc.slice(..), &transaction.changes);
1549+ transaction.apply(&mut state);
1550+
1551+ assert_eq!(state.doc(), "fn test(a: u32) {}");
1552+ assert_eq!(
1553+ edits,
1554+ &[InputEdit {
1555+ start_byte: 8,
1556+ old_end_byte: 8,
1557+ new_end_byte: 14,
1558+ start_position: Point { row: 0, column: 8 },
1559+ old_end_position: Point { row: 0, column: 8 },
1560+ new_end_position: Point { row: 0, column: 14 }
1561+ }]
1562+ );
15331563}