@@ -188,9 +188,19 @@ impl Syntax {
191− pub fn update(&mut self, source: &Rope, changeset: &ChangeSet) -> Result<(), Error> {
195+ changeset: &ChangeSet,
196+ ) -> Result<(), Error> {
192− self.root_layer
197+ self.root_layer.update(
193− .update(&mut self.parser, &self.config, source, changeset)
198+ &mut self.parser,
195 // TODO: deal with injections and update them too
205 // TODO: deal with injections and update them too
@@ -385,7 +395,7 @@ impl LanguageLayer {
387 pub(crate) fn generate_edits(
397 pub(crate) fn generate_edits(
388− text: &RopeSlice,
398+ old_text: &RopeSlice,
389 changeset: &ChangeSet,
399 changeset: &ChangeSet,
390 ) -> Vec<tree_sitter::InputEdit> {
400 ) -> Vec<tree_sitter::InputEdit> {
391 use Operation::*;
401 use Operation::*;
@@ -399,7 +409,7 @@ impl LanguageLayer {
399 // TODO; this is a lot easier with Change instead of Operation.
409 // TODO; this is a lot easier with Change instead of Operation.
401 fn point_at_pos(text: &RopeSlice, pos: usize) -> (usize, Point) {
411 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
403 let line = text.char_to_line(pos);
413 let line = text.char_to_line(pos);
404 let line_start_byte = text.line_to_byte(line);
414 let line_start_byte = text.line_to_byte(line);
405 let col = byte - line_start_byte;
415 let col = byte - line_start_byte;
@@ -437,8 +447,8 @@ impl LanguageLayer {
437 new_pos += len;
447 new_pos += len;
439 Delete(_) => {
449 Delete(_) => {
440− let (start_byte, start_position) = point_at_pos(&text, old_pos);
450+ let (start_byte, start_position) = point_at_pos(&old_text, old_pos);
441− let (old_end_byte, old_end_position) = point_at_pos(&text, old_end);
451+ let (old_end_byte, old_end_position) = point_at_pos(&old_text, old_end);
443 // TODO: Position also needs to be byte based...
453 // TODO: Position also needs to be byte based...
444 // let byte = char_to_byte(old_pos)
454 // let byte = char_to_byte(old_pos)
@@ -475,7 +485,7 @@ impl LanguageLayer {
477 Insert(s) => {
487 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);
480 let ins = s.chars().count();
490 let ins = s.chars().count();
@@ -501,6 +511,7 @@ impl LanguageLayer {
501 &mut self,
511 &mut self,
502 parser: &mut Parser,
512 parser: &mut Parser,
503 config: &HighlightConfiguration,
513 config: &HighlightConfiguration,
504 source: &Rope,
515 source: &Rope,
505 changeset: &ChangeSet,
516 changeset: &ChangeSet,
506 ) -> Result<(), Error> {
517 ) -> Result<(), Error> {
@@ -508,7 +519,7 @@ impl LanguageLayer {
508 return Ok(());
519 return Ok(());
511− let edits = Self::generate_edits(&source.slice(..), changeset);
522+ let edits = Self::generate_edits(&old_source.slice(..), changeset);
513 // Notify the tree about all the changes
524 // Notify the tree about all the changes
514 for edit in edits {
525 for edit in edits {
@@ -1530,4 +1541,23 @@ fn test_input_edits() {
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);
1551+ assert_eq!(state.doc(), "fn test(a: u32) {}");
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 }