use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken in project javaparser by javaparser.
the class LexicalPreservingPrinter method interpret.
private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) {
LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node);
List<TokenTextElement> indentation = findIndentation(node);
boolean pendingIndentation = false;
for (CsmElement element : calculatedSyntaxModel.elements) {
if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) {
indentation.forEach(nodeText::addElement);
}
pendingIndentation = false;
if (element instanceof LexicalDifferenceCalculator.CsmChild) {
nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild());
} else if (element instanceof CsmToken) {
CsmToken csmToken = (CsmToken) element;
nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node));
if (csmToken.isNewLine()) {
pendingIndentation = true;
}
} else if (element instanceof CsmMix) {
CsmMix csmMix = (CsmMix) element;
csmMix.getElements().forEach(e -> interpret(node, e, nodeText));
} else {
throw new UnsupportedOperationException(element.getClass().getSimpleName());
}
}
// so they have to be handled in a special way
if (node instanceof VariableDeclarator) {
VariableDeclarator variableDeclarator = (VariableDeclarator) node;
variableDeclarator.getParentNode().ifPresent(parent -> ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> {
int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel();
for (int i = 0; i < extraArrayLevels; i++) {
nodeText.addElement(new TokenTextElement(LBRACKET));
nodeText.addElement(new TokenTextElement(RBRACKET));
}
}));
}
return nodeText;
}
use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken in project javaparser by javaparser.
the class DifferenceTest method addingStatementToEmptyMethodBody.
@Test
public void addingStatementToEmptyMethodBody() {
String code = "class A { void foo(char p1, int p2) {} }";
considerCode(code);
Statement s = new ExpressionStmt(new BinaryExpr(new IntegerLiteralExpr("10"), new IntegerLiteralExpr("2"), BinaryExpr.Operator.PLUS));
MethodDeclaration m = cu.getClassByName("A").get().getMethodsByName("foo").get(0);
LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(m.getBody().get());
LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterListAddition(m.getBody().get(), ObservableProperty.STATEMENTS, 0, s);
Difference diff = Difference.calculate(csmOriginal, csmChanged);
int i = 0;
assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmIndent()), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmChild(s)), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.added(new CsmUnindent()), diff.getElements().get(i++));
assertEquals(Difference.DifferenceElement.kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
assertEquals(i, diff.getElements().size());
}
use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken in project javaparser by javaparser.
the class DifferenceTest method annotationDeclarationExampleWithNameChanged.
@Test
public void annotationDeclarationExampleWithNameChanged() throws IOException {
considerExample("AnnotationDeclaration_Example1_original");
AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
SimpleName newName = new SimpleName("NewName");
LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.NAME, annotationDeclaration.getName(), newName);
Difference diff = Difference.calculate(csmOriginal, csmChanged);
diff.removeIndentationElements();
int i = 0;
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(removed(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
assertEquals(added(new CsmChild(newName)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
assertEquals(i, diff.getElements().size());
}
use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken in project javaparser by javaparser.
the class DifferenceTest method annotationDeclarationExampleWithJavadocRemoved.
@Test
public void annotationDeclarationExampleWithJavadocRemoved() throws IOException {
considerExample("AnnotationDeclaration_Example9_original");
AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.COMMENT, annotationDeclaration.getComment().get(), null);
Difference diff = Difference.calculate(csmOriginal, csmChanged);
diff.removeIndentationElements();
int i = 0;
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
assertEquals(i, diff.getElements().size());
}
use of com.github.javaparser.printer.concretesyntaxmodel.CsmToken in project javaparser by javaparser.
the class DifferenceTest method annotationDeclarationExampleWithModifierAdded.
@Test
public void annotationDeclarationExampleWithModifierAdded() throws IOException {
considerExample("AnnotationDeclaration_Example1_original");
AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
LexicalDifferenceCalculator.CalculatedSyntaxModel csmOriginal = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(element, annotationDeclaration);
LexicalDifferenceCalculator.CalculatedSyntaxModel csmChanged = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.MODIFIERS, EnumSet.noneOf(Modifier.class), EnumSet.of(Modifier.PUBLIC));
Difference diff = Difference.calculate(csmOriginal, csmChanged);
diff.removeIndentationElements();
int i = 0;
assertEquals(added(new CsmToken(GeneratedJavaParserConstants.PUBLIC)), diff.getElements().get(i++));
assertEquals(added(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.AT)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.INTERFACE)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getName())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(spaceTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(0))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(1))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(2))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(3))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(4))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmChild(annotationDeclaration.getMember(5))), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(eolTokenKind())), diff.getElements().get(i++));
assertEquals(kept(new CsmToken(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(i++));
assertEquals(i, diff.getElements().size());
}
Aggregations