use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement 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.CsmElement 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.CsmElement 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.CsmElement 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());
}
use of com.github.javaparser.printer.concretesyntaxmodel.CsmElement in project javaparser by javaparser.
the class LexicalDifferenceCalculatorTest method annotationDeclarationModifiersExampleModified.
@Test
public void annotationDeclarationModifiersExampleModified() throws IOException {
considerExample("AnnotationDeclaration_Example1_original");
AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.MODIFIERS, EnumSet.noneOf(Modifier.class), EnumSet.of(Modifier.PUBLIC));
csm.removeIndentationElements();
int i = 0;
assertEquals(new CsmToken(GeneratedJavaParserConstants.PUBLIC), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getName()), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
assertEquals(i, csm.elements.size());
}
Aggregations