From f736cb389659a3227677baf50294b110dfd5436d Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 22 Oct 2019 12:58:27 -0700 Subject: [PATCH] web: Implement TreeCursor.nodeText Refs #466 --- lib/binding_web/binding.js | 44 +++++++++++++++++++------------ lib/binding_web/test/tree-test.js | 3 +++ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 7f631844..7af8a92c 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -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'); diff --git a/lib/binding_web/test/tree-test.js b/lib/binding_web/test/tree-test.js index 2311ce4e..ccb7a830 100644 --- a/lib/binding_web/test/tree-test.js +++ b/lib/binding_web/test/tree-test.js @@ -180,6 +180,7 @@ describe("Tree", () => { }); assert(cursor.gotoFirstChild()); + assert.equal(cursor.nodeText, 'a'); assertCursorState(cursor, { nodeType: 'identifier', nodeIsNamed: true, @@ -191,6 +192,7 @@ describe("Tree", () => { assert(!cursor.gotoFirstChild()) assert(cursor.gotoNextSibling()); + assert.equal(cursor.nodeText, '*'); assertCursorState(cursor, { nodeType: '*', nodeIsNamed: false, @@ -201,6 +203,7 @@ describe("Tree", () => { }); assert(cursor.gotoNextSibling()); + assert.equal(cursor.nodeText, 'b'); assertCursorState(cursor, { nodeType: 'identifier', nodeIsNamed: true,