diff --git a/gen.treesitter-ng/src/main/java/com/github/gumtreediff/gen/treesitterng/AbstractTreeSitterNgGenerator.java b/gen.treesitter-ng/src/main/java/com/github/gumtreediff/gen/treesitterng/AbstractTreeSitterNgGenerator.java index 3ee59c25..50b45a53 100644 --- a/gen.treesitter-ng/src/main/java/com/github/gumtreediff/gen/treesitterng/AbstractTreeSitterNgGenerator.java +++ b/gen.treesitter-ng/src/main/java/com/github/gumtreediff/gen/treesitterng/AbstractTreeSitterNgGenerator.java @@ -161,10 +161,11 @@ protected static Pair tsNode2GumTree( return null; } } + boolean ignoreLabel = false; if (currentRule.containsKey(YAML_LABEL_IGNORED)) { List ignores = (List) currentRule.get(YAML_LABEL_IGNORED); if (matchNodeOrAncestorTypes(ignores, node) != null) { - return null; + ignoreLabel = true; } } if (currentRule.containsKey(YAML_ALIASED)) { @@ -182,9 +183,11 @@ protected static Pair 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())); @@ -209,7 +212,7 @@ private static TreeContext generateFromTreeSitterTree( while (!tsNodeStack.isEmpty()) { TSNode tsNodeNow = tsNodeStack.pop(); Pair treeNow = treeStack.pop(); - // flatten here + // if node was flattened, ignore children if (treeNow.second) { continue; } diff --git a/gen.treesitter-ng/src/test/java/com/github/gumtreediff/gen/treesitterng/PythonTreeSitterNgTreeGeneratorTest.java b/gen.treesitter-ng/src/test/java/com/github/gumtreediff/gen/treesitterng/PythonTreeSitterNgTreeGeneratorTest.java new file mode 100644 index 00000000..e4329fa3 --- /dev/null +++ b/gen.treesitter-ng/src/test/java/com/github/gumtreediff/gen/treesitterng/PythonTreeSitterNgTreeGeneratorTest.java @@ -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 . + * + * Copyright 2022 Jean-Rémy Falleri + */ +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); + } + +} \ No newline at end of file diff --git a/gen.treesitter-ng/testData/python/foo.py b/gen.treesitter-ng/testData/python/foo.py new file mode 100644 index 00000000..acbfd4da --- /dev/null +++ b/gen.treesitter-ng/testData/python/foo.py @@ -0,0 +1,4 @@ +print("foo") + +def bar(i): + return \ No newline at end of file