Skip to content

Commit dbe49bf

Browse files
committed
feat: implement missing upstream APIs
1 parent f8e3630 commit dbe49bf

24 files changed

Lines changed: 2792 additions & 156 deletions

.github/workflows/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ env:
1616
ELECTRON_PREBUILD_CMD: npx prebuild -r electron -t 3.0.0 -t 4.0.0 -t 5.0.0 -t 6.0.0 -t 7.0.0 -t 8.0.0 -t 9.0.0 -t 10.0.0 -t 11.0.0 -t 12.0.0 -t 13.0.0 -t 14.0.0 -t 15.0.0 -t 16.0.0 -t 17.0.0 -t 18.0.0 -t 19.0.0 -t 20.0.0 -t 21.0.0 -t 22.0.0 -t 23.0.0 -t 24.0.0 -t 25.0.0 --strip -u ${{ secrets.GH_TOKEN }}
1717

1818
jobs:
19-
2019
test:
2120
strategy:
2221
matrix:

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"src/conversions.cc",
99
"src/language.cc",
1010
"src/logger.cc",
11+
"src/lookaheaditerator.cc",
1112
"src/node.cc",
1213
"src/parser.cc",
1314
"src/query.cc",

index.js

Lines changed: 101 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ try {
1010
}
1111

1212
const util = require('util')
13-
const {Query, Parser, NodeMethods, Tree, TreeCursor} = binding;
13+
const {Query, Parser, NodeMethods, Tree, TreeCursor, LookaheadIterator} = binding;
1414

1515
/*
1616
* Tree
1717
*/
1818

19-
const {rootNode, edit} = Tree.prototype;
19+
const {rootNode, rootNodeWithOffset, edit} = Tree.prototype;
2020

2121
Object.defineProperty(Tree.prototype, 'rootNode', {
2222
get() {
2323
return unmarshalNode(rootNode.call(this), this);
2424
}
2525
});
2626

27+
Tree.prototype.rootNodeWithOffset = function(offset_bytes, offset_extent) {
28+
return unmarshalNode(rootNodeWithOffset.call(this, offset_bytes, offset_extent.row, offset_extent.column), this);
29+
}
30+
2731
Tree.prototype.edit = function(arg) {
2832
edit.call(
2933
this,
@@ -58,16 +62,31 @@ class SyntaxNode {
5862
'}'
5963
}
6064

61-
get type() {
65+
get id() {
6266
marshalNode(this);
63-
return NodeMethods.type(this.tree);
67+
return NodeMethods.id(this.tree);
6468
}
6569

6670
get typeId() {
6771
marshalNode(this);
6872
return NodeMethods.typeId(this.tree);
6973
}
7074

75+
get grammarId() {
76+
marshalNode(this);
77+
return NodeMethods.grammarId(this.tree);
78+
}
79+
80+
get type() {
81+
marshalNode(this);
82+
return NodeMethods.type(this.tree);
83+
}
84+
85+
get grammarName() {
86+
marshalNode(this);
87+
return NodeMethods.grammarName(this.tree);
88+
}
89+
7190
get isNamed() {
7291
marshalNode(this);
7392
return NodeMethods.isNamed(this.tree);
@@ -164,6 +183,21 @@ class SyntaxNode {
164183
return unmarshalNode(NodeMethods.previousNamedSibling(this.tree), this.tree);
165184
}
166185

186+
get parseState() {
187+
marshalNode(this);
188+
return NodeMethods.parseState(this.tree);
189+
}
190+
191+
get nextParseState() {
192+
marshalNode(this);
193+
return NodeMethods.nextParseState(this.tree);
194+
}
195+
196+
get descendantCount() {
197+
marshalNode(this);
198+
return NodeMethods.descendantCount(this.tree);
199+
}
200+
167201
hasChanges() {
168202
marshalNode(this);
169203
return NodeMethods.hasChanges(this.tree);
@@ -179,6 +213,16 @@ class SyntaxNode {
179213
return NodeMethods.isMissing(this.tree);
180214
}
181215

216+
isExtra() {
217+
marshalNode(this);
218+
return NodeMethods.isExtra(this.tree);
219+
}
220+
221+
isError() {
222+
marshalNode(this);
223+
return NodeMethods.isError(this.tree);
224+
}
225+
182226
toString() {
183227
marshalNode(this);
184228
return NodeMethods.toString(this.tree);
@@ -194,6 +238,31 @@ class SyntaxNode {
194238
return unmarshalNode(NodeMethods.namedChild(this.tree, index), this.tree);
195239
}
196240

241+
childForFieldName(fieldName) {
242+
marshalNode(this);
243+
return unmarshalNode(NodeMethods.childForFieldName(this.tree, fieldName), this.tree);
244+
}
245+
246+
childForFieldId(fieldId) {
247+
marshalNode(this);
248+
return unmarshalNode(NodeMethods.childForFieldId(this.tree, fieldId), this.tree);
249+
}
250+
251+
fieldNameForChild(childIndex) {
252+
marshalNode(this);
253+
return NodeMethods.fieldNameForChild(this.tree, childIndex);
254+
}
255+
256+
childrenForFieldName(fieldName, cursor) {
257+
marshalNode(this);
258+
return unmarshalNodes(NodeMethods.childrenForFieldName(this.tree, fieldName, cursor), this.tree);
259+
}
260+
261+
childrenForFieldId(fieldId) {
262+
marshalNode(this);
263+
return unmarshalNodes(NodeMethods.childrenForFieldId(this.tree, fieldId), this.tree);
264+
}
265+
197266
firstChildForIndex(index) {
198267
marshalNode(this);
199268
return unmarshalNode(NodeMethods.firstChildForIndex(this.tree, index), this.tree);
@@ -282,7 +351,7 @@ Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={
282351
input,
283352
oldTree,
284353
bufferSize,
285-
includedRanges
354+
includedRanges,
286355
);
287356
if (tree) {
288357
tree.input = treeInput
@@ -467,11 +536,22 @@ Query.prototype._init = function() {
467536
this.refutedProperties = Object.freeze(refutedProperties);
468537
}
469538

470-
Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
539+
Query.prototype.matches = function(
540+
rootNode,
541+
{
542+
startPosition = ZERO_POINT,
543+
endPosition = ZERO_POINT,
544+
startIndex = 0,
545+
endIndex = 0,
546+
matchLimit = 0xFFFFFFFF,
547+
maxStartDepth = 0xFFFFFFFF
548+
} = {}
549+
) {
471550
marshalNode(rootNode);
472551
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
473552
startPosition.row, startPosition.column,
474-
endPosition.row, endPosition.column
553+
endPosition.row, endPosition.column,
554+
startIndex, endIndex, matchLimit, maxStartDepth
475555
);
476556
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
477557
const results = [];
@@ -505,11 +585,22 @@ Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosi
505585
return results;
506586
}
507587

508-
Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
588+
Query.prototype.captures = function(
589+
rootNode,
590+
{
591+
startPosition = ZERO_POINT,
592+
endPosition = ZERO_POINT,
593+
startIndex = 0,
594+
endIndex = 0,
595+
matchLimit = 0xFFFFFFFF,
596+
maxStartDepth = 0xFFFFFFFF
597+
} = {}
598+
) {
509599
marshalNode(rootNode);
510600
const [returnedMatches, returnedNodes] = _captures.call(this, rootNode.tree,
511601
startPosition.row, startPosition.column,
512-
endPosition.row, endPosition.column
602+
endPosition.row, endPosition.column,
603+
startIndex, endIndex, matchLimit, maxStartDepth
513604
);
514605
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
515606
const results = [];
@@ -709,3 +800,4 @@ module.exports.Query = Query;
709800
module.exports.Tree = Tree;
710801
module.exports.SyntaxNode = SyntaxNode;
711802
module.exports.TreeCursor = TreeCursor;
803+
module.exports.LookaheadIterator = LookaheadIterator;

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@
1919
"prebuild-install": "^7.1.1"
2020
},
2121
"devDependencies": {
22-
"@types/node": "^20.5.9",
22+
"@types/node": "^20.6.0",
2323
"chai": "^4.3.8",
24-
"mocha": "^8.4.0",
24+
"mocha": "^10.2.0",
2525
"node-gyp": "^9.4.0",
2626
"prebuild": "^12.0.0",
27-
"tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#master"
27+
"tmp": "^0.2.1",
28+
"tree-sitter-c": "https://github.com/tree-sitter/tree-sitter-c.git#master",
29+
"tree-sitter-embedded-template": "https://github.com/tree-sitter/tree-sitter-embedded-template.git#master",
30+
"tree-sitter-html": "https://github.com/tree-sitter/tree-sitter-html.git#master",
31+
"tree-sitter-java": "https://github.com/tree-sitter/tree-sitter-java.git#master",
32+
"tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#master",
33+
"tree-sitter-json": "https://github.com/tree-sitter/tree-sitter-json.git#master",
34+
"tree-sitter-python": "https://github.com/tree-sitter/tree-sitter-python.git#master",
35+
"tree-sitter-ruby": "https://github.com/tree-sitter/tree-sitter-ruby.git#master",
36+
"tree-sitter-rust": "https://github.com/tree-sitter/tree-sitter-rust.git#master"
2837
},
2938
"scripts": {
3039
"install": "prebuild-install || node-gyp rebuild",

src/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "./language.h"
22
#include "./conversions.h"
3+
#include "./lookaheaditerator.h"
34
#include "./node.h"
45
#include "./parser.h"
56
#include "./query.h"
@@ -17,6 +18,7 @@ void InitAll(Local<Object> exports, Local<Value> m_, void* v_) {
1718
InitConversions(exports);
1819
node_methods::Init(exports);
1920
language_methods::Init(exports);
21+
LookaheadIterator::Init(exports);
2022
Parser::Init(exports);
2123
Query::Init(exports);
2224
Tree::Init(exports);

src/conversions.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ Nan::Maybe<TSRange> RangeFromJS(const Local<Value> &arg) {
7777
Nan::ThrowTypeError("Range must be a {startPosition, endPosition, startIndex, endIndex} object"); \
7878
return Nan::Nothing<TSRange>(); \
7979
} \
80-
auto field = Convert(value.ToLocalChecked()); \
81-
if (field.IsJust()) { \
82-
result.field = field.FromJust(); \
80+
auto (field) = Convert(value.ToLocalChecked()); \
81+
if ((field).IsJust()) { \
82+
result.field = (field).FromJust(); \
8383
} else { \
8484
return Nan::Nothing<TSRange>(); \
8585
} \

src/logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace node_tree_sitter {
99

10-
class Logger {
10+
class Logger final {
1111
public:
1212
static TSLogger Make(v8::Local<v8::Function>);
1313
Nan::Persistent<v8::Function> func;

0 commit comments

Comments
 (0)