diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index d0d01750..5aff34fe 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -67,7 +67,7 @@ class Parser { parse(callback, oldTree, options) { if (typeof callback === 'string') { - currentParseCallback = index => callback.slice(index); + currentParseCallback = (index, _, endIndex) => callback.slice(index, endIndex); } else if (typeof callback === 'function') { currentParseCallback = callback; } else { @@ -243,13 +243,23 @@ class Node { } get text() { - const startIndex = this.startIndex; - const length = this.endIndex - startIndex; - let result = this.tree.textCallback(startIndex); - while (result.length < length) { - result += this.tree.textCallback(startIndex + result.length); + 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; + } } - return result.slice(0, length); + if (startIndex > endIndex) { + result = result.slice(0, length); + } + return result; } isNamed() { diff --git a/lib/binding_web/tree-sitter-web.d.ts b/lib/binding_web/tree-sitter-web.d.ts index fe627fce..dc2d629f 100644 --- a/lib/binding_web/tree-sitter-web.d.ts +++ b/lib/binding_web/tree-sitter-web.d.ts @@ -35,10 +35,11 @@ declare module 'web-tree-sitter' { type: "parse" | "lex" ) => void; - export interface Input { - seek(index: number): void; - read(): any; - } + export type Input = ( + startIndex: number, + startPoint?: Point, + endIndex?: number, + ) => string | null; export interface SyntaxNode { tree: Tree;