web: Implement TreeCursor.nodeText

Refs #466
This commit is contained in:
Max Brunsfeld 2019-10-22 12:58:27 -07:00
parent 80008b0bcc
commit f736cb3896
2 changed files with 30 additions and 17 deletions

View file

@ -229,23 +229,7 @@ class Node {
}
get text() {
let {startIndex, endIndex} = this;
const length = endIndex - startIndex;
let result = this.tree.textCallback(startIndex, null, endIndex);
startIndex += result.length;
while (startIndex < endIndex) {
const string = this.tree.textCallback(startIndex, null, endIndex);
if (string && string.length > 0) {
startIndex += string.length;
result += string;
} else {
break;
}
}
if (startIndex > endIndex) {
result = result.slice(0, length);
}
return result;
return getText(this.tree, this.startIndex, this.endIndex);
}
isNamed() {
@ -553,6 +537,13 @@ class TreeCursor {
return C._ts_tree_cursor_current_node_is_missing_wasm(this.tree[0]) === 1;
}
get nodeText() {
marshalTreeCursor(this);
const startIndex = C._ts_tree_cursor_start_index_wasm(this.tree[0]);
const endIndex = C._ts_tree_cursor_end_index_wasm(this.tree[0]);
return getText(this.tree, startIndex, endIndex);
}
get startPosition() {
marshalTreeCursor(this);
C._ts_tree_cursor_start_position_wasm(this.tree[0]);
@ -994,6 +985,25 @@ class Query {
}
}
function getText(tree, startIndex, endIndex) {
const length = endIndex - startIndex;
let result = tree.textCallback(startIndex, null, endIndex);
startIndex += result.length;
while (startIndex < endIndex) {
const string = tree.textCallback(startIndex, null, endIndex);
if (string && string.length > 0) {
startIndex += string.length;
result += string;
} else {
break;
}
}
if (startIndex > endIndex) {
result = result.slice(0, length);
}
return result;
}
function unmarshalCaptures(query, tree, address, result) {
for (let i = 0, n = result.length; i < n; i++) {
const captureIndex = getValue(address, 'i32');