From fa74c650a8989c6de4c55318c8b86eb0a8abacba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-R=C3=A9my=20Falleri?= Date: Mon, 14 Oct 2024 16:30:41 +0200 Subject: [PATCH] fix: don't compute label unless necessary. --- .../AbstractTreeSitterNgGenerator.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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 50b45a53..1e6f76c3 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 @@ -72,11 +72,13 @@ private static String getLabel(List contentLines, TSNode node) { // endColumn == startRowBytes.length + 1 when the label in tree-sitter contains line separator if (endColumn == startRowBytes.length + 1) { substringLines = Collections.singletonList(startRowStr); - } else { + } + else { substringLines = Collections.singletonList(new String( startRowBytes, startColumn, endColumn - startColumn)); } - } else { + } + else { substringLines = new ArrayList<>(); String endRowStr = contentLines.get(endRow); byte[] endRowBytes = endRowStr.getBytes(); @@ -92,14 +94,14 @@ private static String getLabel(List contentLines, TSNode node) { String endLineSubstring; if (endColumn > endRowStr.length()) { endLineSubstring = endRowStr; - } else { + } + else { endLineSubstring = new String(endRowBytes, 0, endColumn); } substringLines.add(startLineSubstring); substringLines.addAll(middleLines); substringLines.add(endLineSubstring); } - return String.join(System.lineSeparator(), substringLines); } @@ -118,7 +120,7 @@ private static int calculateOffset(List contentLines, TSPoint point) { * try match node's type or node and its ancestors' types in given ruleSet. * * @param ruleSet a rule's list or rule's ketSet if the rule is a map. - * @param node the node being to match. + * @param node the node to match. * @return matched types. null if not matched. */ protected static String matchNodeOrAncestorTypes(Collection ruleSet, TSNode node) { @@ -154,7 +156,6 @@ protected static String matchNodeOrAncestorTypes(Collection ruleSet, TSN protected static Pair tsNode2GumTree( List contentLines, Map currentRule, TreeContext context, TSNode node) { String type = node.getType(); - String label = getLabel(contentLines, node); if (currentRule.containsKey(YAML_IGNORED)) { List ignores = (List) currentRule.get(YAML_IGNORED); if (matchNodeOrAncestorTypes(ignores, node) != null) { @@ -185,6 +186,7 @@ protected static Pair tsNode2GumTree( Tree tree; // attach label for non ignore-label leafs or flattened nodes if ((node.getChildCount() == 0 && !ignoreLabel) || flatten) { + String label = getLabel(contentLines, node); tree = context.createTree(TypeSet.type(type), label); } else {