Skip to content

Commit

Permalink
Merge pull request #33 from wingrunr21/tree_cursor_updates
Browse files Browse the repository at this point in the history
Add reset and currentNode support to TreeCursor
  • Loading branch information
Max Brunsfeld authored Nov 15, 2018
2 parents 81d5f45 + 2b16eb2 commit 32df455
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,14 @@ Parser.prototype.parseTextBufferSync = function(buffer, oldTree, {includedRanges
return tree;
};

const {startPosition, endPosition} = TreeCursor.prototype;
const {startPosition, endPosition, currentNode, reset} = TreeCursor.prototype;

Object.defineProperties(TreeCursor.prototype, {
currentNode: {
get() {
return currentNode.call(this) || unmarshalNode(this.tree);
}
},
startPosition: {
get() {
startPosition.call(this);
Expand All @@ -361,6 +366,11 @@ Object.defineProperties(TreeCursor.prototype, {
}
});

TreeCursor.prototype.reset = function(node) {
marshalNode(node);
reset.call(this);
}

function getTextFromString (node) {
return this.input.substring(node.startIndex, node.endIndex);
}
Expand Down
2 changes: 1 addition & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void MarshalNullNode() {
memset(transfer_buffer, 0, FIELD_COUNT_PER_NODE * sizeof(transfer_buffer[0]));
}

static TSNode UnmarshalNode(const Tree *tree) {
TSNode UnmarshalNode(const Tree *tree) {
TSNode result = {{0, 0, 0, 0}, nullptr, nullptr};
result.tree = tree->tree_;
if (!result.tree) {
Expand Down
1 change: 1 addition & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace node_methods {

void Init(v8::Local<v8::Object>);
void MarshalNode(const Nan::FunctionCallbackInfo<v8::Value> &info, const Tree *, TSNode);
TSNode UnmarshalNode(const Tree *tree);

static inline const void *UnmarshalNodeId(const uint32_t *buffer) {
const void *result;
Expand Down
20 changes: 20 additions & 0 deletions src/tree_cursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <v8.h>
#include "./util.h"
#include "./conversions.h"
#include "./node.h"
#include "./tree.h"

namespace node_tree_sitter {

Expand Down Expand Up @@ -31,6 +33,8 @@ void TreeCursor::Init(v8::Local<v8::Object> exports) {
{"gotoFirstChild", GotoFirstChild},
{"gotoFirstChildForIndex", GotoFirstChildForIndex},
{"gotoNextSibling", GotoNextSibling},
{"currentNode", CurrentNode},
{"reset", Reset},
};

for (size_t i = 0; i < length_of_array(getters); i++) {
Expand Down Expand Up @@ -112,6 +116,22 @@ void TreeCursor::EndPosition(const Nan::FunctionCallbackInfo<Value> &info) {
TransferPoint(ts_node_end_point(node));
}

void TreeCursor::CurrentNode(const Nan::FunctionCallbackInfo<Value> &info) {
TreeCursor *cursor = Nan::ObjectWrap::Unwrap<TreeCursor>(info.This());
Local<String> key = Nan::New<String>("tree").ToLocalChecked();
const Tree *tree = Tree::UnwrapTree(info.This()->Get(key));
TSNode node = ts_tree_cursor_current_node(&cursor->cursor_);
node_methods::MarshalNode(info, tree, node);
}

void TreeCursor::Reset(const Nan::FunctionCallbackInfo<Value> &info) {
TreeCursor *cursor = Nan::ObjectWrap::Unwrap<TreeCursor>(info.This());
Local<String> key = Nan::New<String>("tree").ToLocalChecked();
const Tree *tree = Tree::UnwrapTree(info.This()->Get(key));
TSNode node = node_methods::UnmarshalNode(tree);
ts_tree_cursor_reset(&cursor->cursor_, node);
}

void TreeCursor::NodeType(v8::Local<v8::String> prop, const Nan::PropertyCallbackInfo<v8::Value> &info) {
TreeCursor *cursor = Nan::ObjectWrap::Unwrap<TreeCursor>(info.This());
TSNode node = ts_tree_cursor_current_node(&cursor->cursor_);
Expand Down
2 changes: 2 additions & 0 deletions src/tree_cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class TreeCursor : public Nan::ObjectWrap {
static void GotoNextSibling(const Nan::FunctionCallbackInfo<v8::Value> &);
static void StartPosition(const Nan::FunctionCallbackInfo<v8::Value> &);
static void EndPosition(const Nan::FunctionCallbackInfo<v8::Value> &);
static void CurrentNode(const Nan::FunctionCallbackInfo<v8::Value> &);
static void Reset(const Nan::FunctionCallbackInfo<v8::Value> &);

static void NodeType(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
static void NodeIsNamed(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
Expand Down
2 changes: 2 additions & 0 deletions tree-sitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ declare module "tree-sitter" {
endPosition: Point;
startIndex: number;
endIndex: number;
readonly currentNode: SyntaxNode

reset(node: SyntaxNode): void
gotoParent(): boolean;
gotoFirstChild(): boolean;
gotoFirstChildForIndex(index: number): boolean;
Expand Down

0 comments on commit 32df455

Please sign in to comment.