parent
80008b0bcc
commit
f736cb3896
2 changed files with 30 additions and 17 deletions
|
|
@ -229,23 +229,7 @@ class Node {
|
||||||
}
|
}
|
||||||
|
|
||||||
get text() {
|
get text() {
|
||||||
let {startIndex, endIndex} = this;
|
return getText(this.tree, this.startIndex, this.endIndex);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isNamed() {
|
isNamed() {
|
||||||
|
|
@ -553,6 +537,13 @@ class TreeCursor {
|
||||||
return C._ts_tree_cursor_current_node_is_missing_wasm(this.tree[0]) === 1;
|
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() {
|
get startPosition() {
|
||||||
marshalTreeCursor(this);
|
marshalTreeCursor(this);
|
||||||
C._ts_tree_cursor_start_position_wasm(this.tree[0]);
|
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) {
|
function unmarshalCaptures(query, tree, address, result) {
|
||||||
for (let i = 0, n = result.length; i < n; i++) {
|
for (let i = 0, n = result.length; i < n; i++) {
|
||||||
const captureIndex = getValue(address, 'i32');
|
const captureIndex = getValue(address, 'i32');
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,7 @@ describe("Tree", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
assert(cursor.gotoFirstChild());
|
assert(cursor.gotoFirstChild());
|
||||||
|
assert.equal(cursor.nodeText, 'a');
|
||||||
assertCursorState(cursor, {
|
assertCursorState(cursor, {
|
||||||
nodeType: 'identifier',
|
nodeType: 'identifier',
|
||||||
nodeIsNamed: true,
|
nodeIsNamed: true,
|
||||||
|
|
@ -191,6 +192,7 @@ describe("Tree", () => {
|
||||||
|
|
||||||
assert(!cursor.gotoFirstChild())
|
assert(!cursor.gotoFirstChild())
|
||||||
assert(cursor.gotoNextSibling());
|
assert(cursor.gotoNextSibling());
|
||||||
|
assert.equal(cursor.nodeText, '*');
|
||||||
assertCursorState(cursor, {
|
assertCursorState(cursor, {
|
||||||
nodeType: '*',
|
nodeType: '*',
|
||||||
nodeIsNamed: false,
|
nodeIsNamed: false,
|
||||||
|
|
@ -201,6 +203,7 @@ describe("Tree", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
assert(cursor.gotoNextSibling());
|
assert(cursor.gotoNextSibling());
|
||||||
|
assert.equal(cursor.nodeText, 'b');
|
||||||
assertCursorState(cursor, {
|
assertCursorState(cursor, {
|
||||||
nodeType: 'identifier',
|
nodeType: 'identifier',
|
||||||
nodeIsNamed: true,
|
nodeIsNamed: true,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue