smacker/go-tree-sitter

How does tree.Edit work for multiple changes?

ddbelyaev opened this issue · 1 comments

From this example in README it is clear how to reparse one change:

...
// from input = []byte("let a = 1") to
// change 1 -> true
newText := []byte("let a = true")
tree.Edit(sitter.EditInput{
   ...
})

// generate new tree
newTree := parser.Parse(tree, newText)

But what if I had for example input := []byte("let a = 1; let c = 3;") and my newInput would be newInput := []byte("let a = 1; let b = 2; let c = 3; let d = 4;"), how would I construct sitter.EditInput then?

Also, is there a way to insert let b = 2; without having the whole input let a = 1; let b = 2; let c = 3; apart from manipulating byte slices directly?

Thank you!

Hi @ddbelyaev !

But what if I had for example input := []byte("let a = 1; let c = 3;") and my newInput would be newInput := []byte("let a = 1; let b = 2; let c = 3; let d = 4;"), how would I construct sitter.EditInput then?

Edit is meant to be used if you already have a source of edits that contains necessary information. Common examples are text editors.

If you don't have that information you can try to run any text diff algorithm on the previous & next text to get the ranges. But probably it will be slower than just parsing new input from scratch.

Also, is there a way to insert let b = 2; without having the whole input let a = 1; let b = 2; let c = 3; apart from manipulating byte slices directly?

We expect input as []byte type. How you generate it is out of the context of this library.