Skip to content

Commit

Permalink
fix: don't compute label unless necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfaller committed Oct 14, 2024
1 parent b19c007 commit fa74c65
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ private static String getLabel(List<String> 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();
Expand All @@ -92,14 +94,14 @@ private static String getLabel(List<String> 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);
}

Expand All @@ -118,7 +120,7 @@ private static int calculateOffset(List<String> 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<String> ruleSet, TSNode node) {
Expand Down Expand Up @@ -154,7 +156,6 @@ protected static String matchNodeOrAncestorTypes(Collection<String> ruleSet, TSN
protected static Pair<Tree, Boolean> tsNode2GumTree(
List<String> contentLines, Map<String, Object> currentRule, TreeContext context, TSNode node) {
String type = node.getType();
String label = getLabel(contentLines, node);
if (currentRule.containsKey(YAML_IGNORED)) {
List<String> ignores = (List<String>) currentRule.get(YAML_IGNORED);
if (matchNodeOrAncestorTypes(ignores, node) != null) {
Expand Down Expand Up @@ -185,6 +186,7 @@ protected static Pair<Tree, Boolean> 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 {
Expand Down

0 comments on commit fa74c65

Please sign in to comment.