55import io .github .treesitter .jtreesitter .internal .TSRange ;
66import io .github .treesitter .jtreesitter .internal .TreeSitter ;
77import java .lang .foreign .*;
8+ import java .nio .charset .Charset ;
89import java .util .ArrayList ;
910import java .util .Collections ;
1011import java .util .List ;
1516@ NullMarked
1617public final class Tree implements AutoCloseable , Cloneable {
1718 private final MemorySegment self ;
18- private @ Nullable String source ;
19+ private byte [] source ;
20+ private @ Nullable Charset charset ;
1921 private final Arena arena ;
2022 private final Language language ;
2123 private @ Nullable List <Range > includedRanges ;
2224
23- Tree (MemorySegment self , Language language , @ Nullable String source ) {
25+ Tree (MemorySegment self , Language language , @ Nullable String source , @ Nullable Charset charset ) {
2426 arena = Arena .ofShared ();
2527 this .self = self .reinterpret (arena , TreeSitter ::ts_tree_delete );
2628 this .language = language ;
27- this .source = source ;
29+ this .source = source != null && charset != null ? source .getBytes (charset ) : new byte [] {};
30+ this .charset = charset ;
2831 }
2932
3033 private Tree (Tree tree ) {
@@ -33,21 +36,28 @@ private Tree(Tree tree) {
3336 self = copy .reinterpret (arena , TreeSitter ::ts_tree_delete );
3437 language = tree .language ;
3538 source = tree .source ;
39+ charset = tree .charset ;
3640 includedRanges = tree .includedRanges ;
3741 }
3842
3943 MemorySegment segment () {
4044 return self ;
4145 }
4246
47+ @ Nullable
48+ String getRegion (@ Unsigned int start , @ Unsigned int end ) {
49+ var length = Math .min (end , source .length ) - start ;
50+ return charset != null ? new String (source , start , length , charset ) : null ;
51+ }
52+
4353 /** Get the language that was used to parse the syntax tree. */
4454 public Language getLanguage () {
4555 return language ;
4656 }
4757
4858 /** Get the source code of the syntax tree, if available. */
4959 public @ Nullable String getText () {
50- return source ;
60+ return charset != null ? new String ( source , charset ) : null ;
5161 }
5262
5363 /** Get the root node of the syntax tree. */
@@ -122,7 +132,8 @@ public void edit(InputEdit edit) {
122132 try (var alloc = Arena .ofConfined ()) {
123133 ts_tree_edit (self , edit .into (alloc ));
124134 } finally {
125- source = null ;
135+ source = new byte [] {};
136+ charset = null ;
126137 }
127138 }
128139
0 commit comments