Skip to content

Commit 245510c

Browse files
committed
refactor!: renaming and aligning with wasm
1 parent 9d8c5fd commit 245510c

File tree

10 files changed

+173
-148
lines changed

10 files changed

+173
-148
lines changed

index.js

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ class SyntaxNode {
9999
return NodeMethods.type(this.tree);
100100
}
101101

102-
get grammarName() {
102+
get grammarType() {
103103
marshalNode(this);
104-
return NodeMethods.grammarName(this.tree);
104+
return NodeMethods.grammarType(this.tree);
105105
}
106106

107107
get isExtra() {
@@ -270,9 +270,9 @@ class SyntaxNode {
270270
return NodeMethods.fieldNameForChild(this.tree, childIndex);
271271
}
272272

273-
childrenForFieldName(fieldName, cursor) {
273+
childrenForFieldName(fieldName) {
274274
marshalNode(this);
275-
return unmarshalNodes(NodeMethods.childrenForFieldName(this.tree, fieldName, cursor), this.tree);
275+
return unmarshalNodes(NodeMethods.childrenForFieldName(this.tree, fieldName), this.tree);
276276
}
277277

278278
childrenForFieldId(fieldId) {
@@ -330,6 +330,7 @@ class SyntaxNode {
330330
marshalNode(this);
331331
const cursor = NodeMethods.walk(this.tree);
332332
cursor.tree = this.tree;
333+
unmarshalNode(cursor.currentNode, this.tree);
333334
return cursor;
334335
}
335336
}
@@ -352,15 +353,15 @@ Parser.prototype.setLanguage = function(language) {
352353
return this;
353354
};
354355

355-
Parser.prototype.getLanguage = function(language) {
356+
Parser.prototype.getLanguage = function(_language) {
356357
return this[languageSymbol] || null;
357358
};
358359

359360
Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={}) {
360361
let getText, treeInput = input
361362
if (typeof input === 'string') {
362363
const inputString = input;
363-
input = (offset, position) => inputString.slice(offset)
364+
input = (offset, _position) => inputString.slice(offset)
364365
getText = getTextFromString
365366
} else {
366367
getText = getTextFromFunction
@@ -387,7 +388,7 @@ Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={
387388
* TreeCursor
388389
*/
389390

390-
const {startPosition, endPosition, currentNode, reset} = TreeCursor.prototype;
391+
const {startPosition, endPosition, currentNode} = TreeCursor.prototype;
391392

392393
Object.defineProperties(TreeCursor.prototype, {
393394
currentNode: {
@@ -424,13 +425,6 @@ Object.defineProperties(TreeCursor.prototype, {
424425
}
425426
});
426427

427-
TreeCursor.prototype.reset = function(node) {
428-
marshalNode(node);
429-
if (this instanceof TreeCursor && reset) {
430-
reset.call(this);
431-
}
432-
}
433-
434428
/*
435429
* Query
436430
*/
@@ -627,7 +621,7 @@ Query.prototype._init = function() {
627621
}
628622

629623
Query.prototype.matches = function(
630-
rootNode,
624+
node,
631625
{
632626
startPosition = ZERO_POINT,
633627
endPosition = ZERO_POINT,
@@ -637,13 +631,13 @@ Query.prototype.matches = function(
637631
maxStartDepth = 0xFFFFFFFF
638632
} = {}
639633
) {
640-
marshalNode(rootNode);
641-
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
634+
marshalNode(node);
635+
const [returnedMatches, returnedNodes] = _matches.call(this, node.tree,
642636
startPosition.row, startPosition.column,
643637
endPosition.row, endPosition.column,
644638
startIndex, endIndex, matchLimit, maxStartDepth
645639
);
646-
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
640+
const nodes = unmarshalNodes(returnedNodes, node.tree);
647641
const results = [];
648642

649643
let i = 0
@@ -675,13 +669,24 @@ Query.prototype.matches = function(
675669
return results;
676670
}
677671

678-
Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
679-
marshalNode(rootNode);
680-
const [returnedMatches, returnedNodes] = _captures.call(this, rootNode.tree,
672+
Query.prototype.captures = function(
673+
node,
674+
{
675+
startPosition = ZERO_POINT,
676+
endPosition = ZERO_POINT,
677+
startIndex = 0,
678+
endIndex = 0,
679+
matchLimit = 0xFFFFFFFF,
680+
maxStartDepth = 0xFFFFFFFF
681+
} = {}
682+
) {
683+
marshalNode(node);
684+
const [returnedMatches, returnedNodes] = _captures.call(this, node.tree,
681685
startPosition.row, startPosition.column,
682-
endPosition.row, endPosition.column
686+
endPosition.row, endPosition.column,
687+
startIndex, endIndex, matchLimit, maxStartDepth
683688
);
684-
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
689+
const nodes = unmarshalNodes(returnedNodes, node.tree);
685690
const results = [];
686691

687692
let i = 0
@@ -714,6 +719,23 @@ Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPos
714719
return results;
715720
}
716721

722+
/*
723+
* LookaheadIterator
724+
*/
725+
726+
LookaheadIterator.prototype[Symbol.iterator] = function() {
727+
const self = this;
728+
return {
729+
next() {
730+
if (self._next()) {
731+
return {done: false, value: self.currentType};
732+
}
733+
734+
return {done: true, value: ''};
735+
},
736+
};
737+
}
738+
717739
/*
718740
* Other functions
719741
*/
@@ -730,7 +752,7 @@ function getTextFromFunction ({startIndex, endIndex}) {
730752
const text = input(startIndex + result.length);
731753
result += text;
732754
}
733-
return result.substr(0, goalLength);
755+
return result.slice(0, goalLength);
734756
}
735757

736758
const {pointTransferArray} = binding;
@@ -869,7 +891,7 @@ function initializeLanguageNodeClasses(language) {
869891
}
870892

871893
function camelCase(name, upperCase) {
872-
name = name.replace(/_(\w)/g, (match, letter) => letter.toUpperCase());
894+
name = name.replace(/_(\w)/g, (_match, letter) => letter.toUpperCase());
873895
if (upperCase) name = name[0].toUpperCase() + name.slice(1);
874896
return name;
875897
}

src/lookaheaditerator.cc

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ void LookaheadIterator::Init(Napi::Env env, Napi::Object exports) {
1313
auto *data = env.GetInstanceData<AddonData>();
1414

1515
Function ctor = DefineClass(env, "LookaheadIterator", {
16-
InstanceAccessor("currentSymbol", &LookaheadIterator::CurrentSymbol, nullptr, napi_default_method),
17-
InstanceAccessor("currentSymbolName", &LookaheadIterator::CurrentSymbolName, nullptr, napi_default_method),
16+
InstanceAccessor("currentTypeId", &LookaheadIterator::CurrentTypeId, nullptr, napi_default_method),
17+
InstanceAccessor("currentType", &LookaheadIterator::CurrentType, nullptr, napi_default_method),
1818

1919
InstanceMethod("reset", &LookaheadIterator::Reset, napi_default_method),
2020
InstanceMethod("resetState", &LookaheadIterator::ResetState, napi_default_method),
21-
InstanceMethod("next", &LookaheadIterator::Next, napi_default_method),
22-
InstanceMethod("iterNames", &LookaheadIterator::IterNames, napi_default_method),
21+
InstanceMethod("_next", &LookaheadIterator::Next, napi_default_method),
2322
});
2423

2524
exports["LookaheadIterator"] = ctor;
@@ -75,12 +74,12 @@ LookaheadIterator *LookaheadIterator::UnwrapLookaheadIterator(const Napi::Value
7574
return LookaheadIterator::Unwrap(js_iterator);
7675
}
7776

78-
Napi::Value LookaheadIterator::CurrentSymbolName(const Napi::CallbackInfo &info) {
77+
Napi::Value LookaheadIterator::CurrentType(const Napi::CallbackInfo &info) {
7978
LookaheadIterator *iterator = UnwrapLookaheadIterator(info.This());
8079
return Napi::String::New(info.Env(), ts_lookahead_iterator_current_symbol_name(iterator->iterator_));
8180
}
8281

83-
Napi::Value LookaheadIterator::CurrentSymbol(const Napi::CallbackInfo &info) {
82+
Napi::Value LookaheadIterator::CurrentTypeId(const Napi::CallbackInfo &info) {
8483
LookaheadIterator *iterator = UnwrapLookaheadIterator(info.This());
8584
return Napi::Number::New(info.Env(), ts_lookahead_iterator_current_symbol(iterator->iterator_));
8685
}
@@ -120,16 +119,4 @@ Napi::Value LookaheadIterator::Next(const Napi::CallbackInfo &info) {
120119
return Napi::Boolean::New(info.Env(), ts_lookahead_iterator_next(iterator->iterator_));
121120
}
122121

123-
Napi::Value LookaheadIterator::IterNames(const Napi::CallbackInfo &info) {
124-
LookaheadIterator *iterator = UnwrapLookaheadIterator(info.This());
125-
auto result = Napi::Array::New(info.Env());
126-
127-
while (ts_lookahead_iterator_next(iterator->iterator_)) {
128-
const char *name = ts_lookahead_iterator_current_symbol_name(iterator->iterator_);
129-
result.Set(result.Length(), Napi::String::New(info.Env(), name));
130-
}
131-
132-
return result;
133-
}
134-
135122
} // namespace node_tree_sitter

src/lookaheaditerator.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ class LookaheadIterator final : public Napi::ObjectWrap<LookaheadIterator> {
2323
Napi::Value Reset(const Napi::CallbackInfo &);
2424
Napi::Value ResetState(const Napi::CallbackInfo &);
2525
Napi::Value Next(const Napi::CallbackInfo &);
26-
Napi::Value IterNames(const Napi::CallbackInfo &);
2726

28-
Napi::Value CurrentSymbol(const Napi::CallbackInfo &);
29-
Napi::Value CurrentSymbolName(const Napi::CallbackInfo &);
27+
Napi::Value CurrentTypeId(const Napi::CallbackInfo &);
28+
Napi::Value CurrentType(const Napi::CallbackInfo &);
3029
};
3130

3231
} // namespace node_tree_sitter

src/node.cc

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ Napi::Value Type(const Napi::CallbackInfo &info) {
323323
return env.Undefined();
324324
}
325325

326-
Napi::Value GrammarName(const Napi::CallbackInfo &info) {
326+
Napi::Value GrammarType(const Napi::CallbackInfo &info) {
327327
Env env = info.Env();
328328
const Tree *tree = Tree::UnwrapTree(info[0]);
329329
TSNode node = UnmarshalNode(env, tree);
@@ -635,33 +635,30 @@ Napi::Value ChildrenForFieldName(const Napi::CallbackInfo &info) {
635635
}
636636
std::string field_name = info[1].As<String>().Utf8Value();
637637

638-
if (!info[2].IsObject()) {
639-
throw TypeError::New(env, "Second argument must be a cursor");
640-
}
641-
TSTreeCursor *cursor = &TreeCursor::Unwrap(info[2].As<Object>())->cursor_;
638+
TSTreeCursor cursor = ts_tree_cursor_new(node);
642639

643640
const TSLanguage *language = ts_tree_language(node.tree);
644641
TSFieldId field_id = ts_language_field_id_for_name(language, field_name.c_str(), field_name.length());
645642

646643
bool done = field_id == 0;
647644
if (!done) {
648-
ts_tree_cursor_reset(cursor, node);
649-
ts_tree_cursor_goto_first_child(cursor);
645+
ts_tree_cursor_reset(&cursor, node);
646+
ts_tree_cursor_goto_first_child(&cursor);
650647
}
651648

652649
vector<TSNode> result;
653650
while (!done) {
654-
while (ts_tree_cursor_current_field_id(cursor) != field_id) {
655-
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
651+
while (ts_tree_cursor_current_field_id(&cursor) != field_id) {
652+
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
656653
done = true;
657654
break;
658655
}
659656
}
660657
if (done) {
661658
break;
662659
}
663-
TSNode result_node = ts_tree_cursor_current_node(cursor);
664-
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
660+
TSNode result_node = ts_tree_cursor_current_node(&cursor);
661+
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
665662
done = true;
666663
}
667664
result.push_back(result_node);
@@ -683,30 +680,28 @@ Napi::Value ChildrenForFieldId(const Napi::CallbackInfo &info) {
683680
}
684681
uint32_t field_id = info[1].As<Number>().Uint32Value();
685682

686-
if (!info[2].IsObject()) {
687-
throw TypeError::New(env, "Second argument must be a cursor");
688-
}
689-
TSTreeCursor *cursor = &TreeCursor::Unwrap(info[2].As<Object>())->cursor_;
683+
684+
TSTreeCursor cursor = ts_tree_cursor_new(node);
690685

691686
bool done = field_id == 0;
692687
if (!done) {
693-
ts_tree_cursor_reset(cursor, node);
694-
ts_tree_cursor_goto_first_child(cursor);
688+
ts_tree_cursor_reset(&cursor, node);
689+
ts_tree_cursor_goto_first_child(&cursor);
695690
}
696691

697692
vector<TSNode> result;
698693
while (!done) {
699-
while (ts_tree_cursor_current_field_id(cursor) != field_id) {
700-
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
694+
while (ts_tree_cursor_current_field_id(&cursor) != field_id) {
695+
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
701696
done = true;
702697
break;
703698
}
704699
}
705700
if (done) {
706701
break;
707702
}
708-
TSNode result_node = ts_tree_cursor_current_node(cursor);
709-
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
703+
TSNode result_node = ts_tree_cursor_current_node(&cursor);
704+
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
710705
done = true;
711706
}
712707
result.push_back(result_node);
@@ -998,7 +993,7 @@ void Init(Napi::Env env, Napi::Object exports) {
998993
{"typeId", TypeId},
999994
{"grammarId", GrammarId},
1000995
{"type", Type},
1001-
{"grammarName", GrammarName},
996+
{"grammarType", GrammarType},
1002997
{"isNamed", IsNamed},
1003998
{"isExtra", IsExtra},
1004999
{"hasChanges", HasChanges},

0 commit comments

Comments
 (0)