Skip to content

Commit

Permalink
fix: regression of handling of rule file in treesitter-ng.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfaller committed Sep 20, 2024
1 parent f42e392 commit b19c007
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ protected static Pair<Tree, Boolean> tsNode2GumTree(
return null;
}
}
boolean ignoreLabel = false;
if (currentRule.containsKey(YAML_LABEL_IGNORED)) {
List<String> ignores = (List<String>) currentRule.get(YAML_LABEL_IGNORED);
if (matchNodeOrAncestorTypes(ignores, node) != null) {
return null;
ignoreLabel = true;
}
}
if (currentRule.containsKey(YAML_ALIASED)) {
Expand All @@ -182,9 +183,11 @@ protected static Pair<Tree, Boolean> tsNode2GumTree(
}
}
Tree tree;
if (node.getChildCount() == 0) {
// attach label for non ignore-label leafs or flattened nodes
if ((node.getChildCount() == 0 && !ignoreLabel) || flatten) {
tree = context.createTree(TypeSet.type(type), label);
} else {
}
else {
tree = context.createTree(TypeSet.type(type));
}
tree.setPos(calculateOffset(contentLines, node.getStartPoint()));
Expand All @@ -209,7 +212,7 @@ private static TreeContext generateFromTreeSitterTree(
while (!tsNodeStack.isEmpty()) {
TSNode tsNodeNow = tsNodeStack.pop();
Pair<Tree, Boolean> treeNow = treeStack.pop();
// flatten here
// if node was flattened, ignore children
if (treeNow.second) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of GumTree.
*
* GumTree is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GumTree is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GumTree. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2022 Jean-Rémy Falleri <[email protected]>
*/
package com.github.gumtreediff.gen.treesitterng;

import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.TreeContext;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PythonTreeSitterNgTreeGeneratorTest {
private final PythonTreeSitterNgTreeGenerator generator = new PythonTreeSitterNgTreeGenerator();

@Test
public void testString() throws IOException {
TreeContext src = generator.generateFrom().file("testData/python/foo.py");
assertEquals(12, src.getRoot().getMetrics().size);
}

}
4 changes: 4 additions & 0 deletions gen.treesitter-ng/testData/python/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
print("foo")

def bar(i):
return

0 comments on commit b19c007

Please sign in to comment.