Skip to content

Commit

Permalink
Fix issue 3358 LexicalPreservingPrinter error on ArrayType
Browse files Browse the repository at this point in the history
  • Loading branch information
jlerbsc committed Aug 30, 2021
1 parent 9616c13 commit 94920f4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

package com.github.javaparser.printer.lexicalpreservation;

import java.util.Optional;

import com.github.javaparser.Range;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.comments.Comment;

import java.util.Optional;

/**
* Represent the position of a child node in the NodeText of its parent.
*/
Expand Down Expand Up @@ -110,6 +110,11 @@ public boolean isIdentifier() {
return false;
}

@Override
public boolean isKeyword() {
return false;
}

@Override
public boolean isPrimitive() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,10 @@ private void applyKeptDiffElement(Kept kept, TextElement originalElement, boolea
int step = getIndexToNextTokenElement((TokenTextElement) originalElement, 0);
originalIndex += step;
originalIndex++;
} else if (originalElement.isIdentifier() && isArrayType(kept)) {
int step = getArrayLevel(kept);
} else if ((originalElement.isIdentifier() || originalElement.isKeyword()) && isArrayType(kept)) {
int tokenToSkip = getIndexToNextTokenElementInArrayType((TokenTextElement)originalElement, getArrayLevel(kept));
diffIndex++;
originalIndex += step*2; // there is a couple of brackets per level
originalIndex += tokenToSkip;
originalIndex++;
} else if (originalElement.isIdentifier()) {
originalIndex++;
Expand Down Expand Up @@ -624,8 +624,8 @@ private int getIndexToNextTokenElement(TokenTextElement element, int nestedDiamo
// because there is a token, first we need to increment the number of token to skip
step++;
// manage nested diamond operators by incrementing the level on LT token and decrementing on GT
JavaToken token = next.get();
Kind kind = Kind.valueOf(token.getKind());
JavaToken nextToken = next.get();
Kind kind = Kind.valueOf(nextToken.getKind());
if (isDiamondOperator(kind)) {
if (kind.GT.equals(kind))
nestedDiamondOperator--;
Expand All @@ -635,10 +635,35 @@ private int getIndexToNextTokenElement(TokenTextElement element, int nestedDiamo
// manage the fact where the first token is not a diamond operator but a whitespace
// and the end of the token sequence to skip
// for example in this declaration List <String> a;
if (nestedDiamondOperator == 0 && !next.get().getCategory().isWhitespace())
if (nestedDiamondOperator == 0 && !nextToken.getCategory().isWhitespace())
return step;
// recursively analyze token to skip
return step += getIndexToNextTokenElement(new TokenTextElement(token), nestedDiamondOperator);
return step += getIndexToNextTokenElement(new TokenTextElement(nextToken), nestedDiamondOperator);
}

/*
* Returns the number of tokens to skip in originalElements list to synchronize it with the DiffElements list
*/
private int getIndexToNextTokenElementInArrayType(TokenTextElement element, int arrayLevel) {
int step = 0; // number of token to skip
Optional<JavaToken> next = element.getToken().getNextToken();
if (!next.isPresent()) return step;
// because there is a token, first we need to increment the number of token to skip
step++;
// manage array Level by decrementing the level on right bracket token
JavaToken nextToken = next.get();
Kind kind = Kind.valueOf(nextToken.getKind());
if (isBracket(kind)) {
if (kind.RBRACKET.equals(kind))
arrayLevel--;
}
// manage the fact where the first token is not a diamond operator but a whitespace
// and the end of the token sequence to skip
// for example in this declaration int [] a;
if (arrayLevel == 0 && !nextToken.getCategory().isWhitespace())
return step;
// recursively analyze token to skip
return step += getIndexToNextTokenElementInArrayType(new TokenTextElement(nextToken), arrayLevel);
}

/*
Expand All @@ -647,6 +672,13 @@ private int getIndexToNextTokenElement(TokenTextElement element, int nestedDiamo
private boolean isDiamondOperator(Kind kind) {
return kind.GT.equals(kind) || kind.LT.equals(kind);
}

/*
* Returns true if the token is a bracket
*/
private boolean isBracket(Kind kind) {
return kind.LBRACKET.equals(kind) || kind.RBRACKET.equals(kind);
}

private boolean openBraceWasOnSameLine() {
int index = originalIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

package com.github.javaparser.printer.lexicalpreservation;

import java.util.Optional;

import com.github.javaparser.GeneratedJavaParserConstants;
import com.github.javaparser.Range;
import com.github.javaparser.ast.Node;

import java.util.Optional;

public abstract class TextElement implements TextElementMatcher {

abstract String expand();
Expand Down Expand Up @@ -59,6 +59,8 @@ public boolean match(TextElement textElement) {
public abstract boolean isSeparator();

public abstract boolean isIdentifier();

public abstract boolean isKeyword();

public abstract boolean isPrimitive();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

package com.github.javaparser.printer.lexicalpreservation;

import java.util.Optional;

import com.github.javaparser.JavaToken;
import com.github.javaparser.Range;
import com.github.javaparser.JavaToken.Kind;
import com.github.javaparser.Range;
import com.github.javaparser.ast.Node;

import java.util.Optional;

class TokenTextElement extends TextElement {
private final JavaToken token;

Expand Down Expand Up @@ -125,6 +125,11 @@ public boolean isChildOfClass(Class<? extends Node> nodeClass) {
public boolean isIdentifier() {
return getToken().getCategory().isIdentifier();
}

@Override
public boolean isKeyword() {
return getToken().getCategory().isKeyword();
}

@Override
public boolean isLiteral() {
Expand Down

0 comments on commit 94920f4

Please sign in to comment.