Skip to content

Commit

Permalink
Moved DifferenceElement classes to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
ThLeu committed May 19, 2018
1 parent 29a9b8b commit a25f53f
Show file tree
Hide file tree
Showing 13 changed files with 426 additions and 380 deletions.

Large diffs are not rendered by default.

Expand Up @@ -17,6 +17,7 @@
import com.github.javaparser.printer.ConcreteSyntaxModel; import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.concretesyntaxmodel.CsmElement; import com.github.javaparser.printer.concretesyntaxmodel.CsmElement;
import com.github.javaparser.printer.concretesyntaxmodel.CsmToken; import com.github.javaparser.printer.concretesyntaxmodel.CsmToken;
import com.github.javaparser.printer.lexicalpreservation.difference.DifferenceElement;
import org.junit.Test; import org.junit.Test;


import java.io.IOException; import java.io.IOException;
Expand Down Expand Up @@ -270,23 +271,23 @@ public void differenceAfterddingStatementToEmptyBlock() throws IOException {
new NameExpr("aField"), new NameExpr("aField"),
AssignExpr.Operator.ASSIGN AssignExpr.Operator.ASSIGN
)); ));
List<DifferenceElementCalculator.DifferenceElement> differenceElements = ldc.calculateListAdditionDifference( List<DifferenceElement> differenceElements = ldc.calculateListAdditionDifference(
ObservableProperty.STATEMENTS, ObservableProperty.STATEMENTS,
setter.getBody().get().getStatements(), setter.getBody().get().getStatements(),
0, 0,
assignStatement); assignStatement);
int index = 0; int index = 0;
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), differenceElements.get(index++)); assertEquals(DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.newline()), differenceElements.get(index++)); assertEquals(DifferenceElement.kept(CsmElement.newline()), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.indent()), differenceElements.get(index++)); assertEquals(DifferenceElement.added(CsmElement.indent()), differenceElements.get(index++));
assertTrue(isAddedChild(differenceElements.get(index++), ExpressionStmt.class)); assertTrue(isAddedChild(differenceElements.get(index++), ExpressionStmt.class));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.newline()), differenceElements.get(index++)); assertEquals(DifferenceElement.added(CsmElement.newline()), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.unindent()), differenceElements.get(index++)); assertEquals(DifferenceElement.added(CsmElement.unindent()), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), differenceElements.get(index++)); assertEquals(DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), differenceElements.get(index++));
assertEquals(index, differenceElements.size()); assertEquals(index, differenceElements.size());
} }


private boolean isAddedChild(DifferenceElementCalculator.DifferenceElement element, Class<? extends Node> childClass) { private boolean isAddedChild(DifferenceElement element, Class<? extends Node> childClass) {
return element.isAdded() && isChild(element.getElement(), childClass); return element.isAdded() && isChild(element.getElement(), childClass);
} }


Expand Down
Expand Up @@ -27,10 +27,10 @@
/** /**
* Represent the position of a child node in the NodeText of its parent. * Represent the position of a child node in the NodeText of its parent.
*/ */
class ChildTextElement extends TextElement { public class ChildTextElement extends TextElement {
private final Node child; private final Node child;


ChildTextElement(Node child) { public ChildTextElement(Node child) {
this.child = child; this.child = child;
} }


Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.printer.concretesyntaxmodel.*; import com.github.javaparser.printer.concretesyntaxmodel.*;
import com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild; import com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild;
import com.github.javaparser.printer.lexicalpreservation.difference.*;


import java.util.*; import java.util.*;


Expand All @@ -21,7 +22,7 @@ public class Difference {


private static final int STANDARD_INDENTATION_SIZE = 4; private static final int STANDARD_INDENTATION_SIZE = 4;


private final List<DifferenceElementCalculator.DifferenceElement> diffElements; private final List<DifferenceElement> diffElements;
private final NodeText nodeText; private final NodeText nodeText;
private final Node node; private final Node node;


Expand All @@ -30,7 +31,7 @@ public class Difference {
private int originalIndex = 0; private int originalIndex = 0;
private int diffIndex = 0; private int diffIndex = 0;


Difference(List<DifferenceElementCalculator.DifferenceElement> diffElements, NodeText nodeText, Node node) { Difference(List<DifferenceElement> diffElements, NodeText nodeText, Node node) {
if (nodeText == null) { if (nodeText == null) {
throw new NullPointerException("nodeText can not be null"); throw new NullPointerException("nodeText can not be null");
} }
Expand Down Expand Up @@ -123,18 +124,18 @@ void apply() {


do { do {
if (diffIndex < diffElements.size() && originalIndex >= originalElements.size()) { if (diffIndex < diffElements.size() && originalIndex >= originalElements.size()) {
DifferenceElementCalculator.DifferenceElement diffElement = diffElements.get(diffIndex); DifferenceElement diffElement = diffElements.get(diffIndex);
if (diffElement instanceof DifferenceElementCalculator.Kept) { if (diffElement instanceof Kept) {
DifferenceElementCalculator.Kept kept = (DifferenceElementCalculator.Kept) diffElement; Kept kept = (Kept) diffElement;


if (kept.isWhiteSpaceOrComment()) { if (kept.isWhiteSpaceOrComment()) {
diffIndex++; diffIndex++;
} else { } else {
throw new IllegalStateException("Cannot keep element because we reached the end of nodetext: " throw new IllegalStateException("Cannot keep element because we reached the end of nodetext: "
+ nodeText + ". Difference: " + this); + nodeText + ". Difference: " + this);
} }
} else if (diffElement instanceof DifferenceElementCalculator.Added) { } else if (diffElement instanceof Added) {
DifferenceElementCalculator.Added addedElement = (DifferenceElementCalculator.Added) diffElement; Added addedElement = (Added) diffElement;


nodeText.addElement(originalIndex, addedElement.toTextElement()); nodeText.addElement(originalIndex, addedElement.toTextElement());
originalIndex++; originalIndex++;
Expand All @@ -152,10 +153,10 @@ void apply() {
+ this + " " + originalElement); + this + " " + originalElement);
} }
} else { } else {
DifferenceElementCalculator.DifferenceElement diffElement = diffElements.get(diffIndex); DifferenceElement diffElement = diffElements.get(diffIndex);


if (diffElement instanceof DifferenceElementCalculator.Added) { if (diffElement instanceof Added) {
DifferenceElementCalculator.Added addedElement = (DifferenceElementCalculator.Added) diffElement; Added addedElement = (Added) diffElement;


if (addedElement.isIndent()) { if (addedElement.isIndent()) {
for (int i=0;i<STANDARD_INDENTATION_SIZE;i++){ for (int i=0;i<STANDARD_INDENTATION_SIZE;i++){
Expand Down Expand Up @@ -220,8 +221,8 @@ void apply() {
boolean originalElementIsChild = originalElement instanceof ChildTextElement; boolean originalElementIsChild = originalElement instanceof ChildTextElement;
boolean originalElementIsToken = originalElement instanceof TokenTextElement; boolean originalElementIsToken = originalElement instanceof TokenTextElement;


if (diffElement instanceof DifferenceElementCalculator.Kept) { if (diffElement instanceof Kept) {
DifferenceElementCalculator.Kept kept = (DifferenceElementCalculator.Kept)diffElement; Kept kept = (Kept)diffElement;


if (originalElement.isComment()) { if (originalElement.isComment()) {
originalIndex++; originalIndex++;
Expand All @@ -236,7 +237,7 @@ void apply() {
originalIndex++; originalIndex++;
diffIndex++; diffIndex++;
} else { } else {
throw new UnsupportedOperationException("kept " + kept.element + " vs " + originalElement); throw new UnsupportedOperationException("kept " + kept.getElement() + " vs " + originalElement);
} }
} }
} else if (kept.isToken() && originalElementIsToken) { } else if (kept.isToken() && originalElementIsToken) {
Expand All @@ -250,7 +251,7 @@ void apply() {
} else if (originalTextToken.isWhiteSpaceOrComment()) { } else if (originalTextToken.isWhiteSpaceOrComment()) {
originalIndex++; originalIndex++;
} else { } else {
throw new UnsupportedOperationException("Csm token " + kept.element + " NodeText TOKEN " + originalTextToken); throw new UnsupportedOperationException("Csm token " + kept.getElement() + " NodeText TOKEN " + originalTextToken);
} }
} else if (kept.isWhiteSpace()) { } else if (kept.isWhiteSpace()) {
diffIndex++; diffIndex++;
Expand All @@ -263,10 +264,10 @@ void apply() {
nodeText.removeElement(--originalIndex); nodeText.removeElement(--originalIndex);
} }
} else { } else {
throw new UnsupportedOperationException("kept " + kept.element + " vs " + originalElement); throw new UnsupportedOperationException("kept " + kept.getElement() + " vs " + originalElement);
} }
} else if (diffElement instanceof DifferenceElementCalculator.Removed) { } else if (diffElement instanceof Removed) {
DifferenceElementCalculator.Removed removed = (DifferenceElementCalculator.Removed)diffElement; Removed removed = (Removed)diffElement;


if (removed.isChild() && originalElementIsChild) { if (removed.isChild() && originalElementIsChild) {
ChildTextElement originalElementChild = (ChildTextElement)originalElement; ChildTextElement originalElementChild = (ChildTextElement)originalElement;
Expand All @@ -284,15 +285,15 @@ void apply() {
if (originalIndex < originalElements.size() && originalElements.get(originalIndex).isNewline()) { if (originalIndex < originalElements.size() && originalElements.get(originalIndex).isNewline()) {
originalIndex = considerCleaningTheLine(nodeText, originalIndex); originalIndex = considerCleaningTheLine(nodeText, originalIndex);
} else { } else {
if (diffIndex + 1 >= diffElements.size() || !(diffElements.get(diffIndex + 1) instanceof DifferenceElementCalculator.Added)) { if (diffIndex + 1 >= diffElements.size() || !(diffElements.get(diffIndex + 1) instanceof Added)) {
originalIndex = considerEnforcingIndentation(nodeText, originalIndex); originalIndex = considerEnforcingIndentation(nodeText, originalIndex);
} }
// If in front we have one space and before also we had space let's drop one space // If in front we have one space and before also we had space let's drop one space
if (originalElements.size() > originalIndex && originalIndex > 0) { if (originalElements.size() > originalIndex && originalIndex > 0) {
if (originalElements.get(originalIndex).isWhiteSpace() if (originalElements.get(originalIndex).isWhiteSpace()
&& originalElements.get(originalIndex - 1).isWhiteSpace()) { && originalElements.get(originalIndex - 1).isWhiteSpace()) {
// However we do not want to do that when we are about to adding or removing elements // However we do not want to do that when we are about to adding or removing elements
if ((diffIndex + 1) == diffElements.size() || (diffElements.get(diffIndex + 1) instanceof DifferenceElementCalculator.Kept)) { if ((diffIndex + 1) == diffElements.size() || (diffElements.get(diffIndex + 1) instanceof Kept)) {
originalElements.remove(originalIndex--); originalElements.remove(originalIndex--);
} }
} }
Expand All @@ -311,20 +312,20 @@ void apply() {
nodeText.removeElement(originalIndex); nodeText.removeElement(originalIndex);
diffIndex++; diffIndex++;
} else { } else {
throw new UnsupportedOperationException("removed " + removed.element + " vs " + originalElement); throw new UnsupportedOperationException("removed " + removed.getElement() + " vs " + originalElement);
} }
} else if (removed.isWhiteSpace()) { } else if (removed.isWhiteSpace()) {
diffIndex++; diffIndex++;
} else if (originalElement.isWhiteSpace()) { } else if (originalElement.isWhiteSpace()) {
originalIndex++; originalIndex++;
} else { } else {
throw new UnsupportedOperationException("removed " + removed.element + " vs " + originalElement); throw new UnsupportedOperationException("removed " + removed.getElement() + " vs " + originalElement);
} }
} else if (diffElement instanceof DifferenceElementCalculator.Reshuffled) { } else if (diffElement instanceof Reshuffled) {
// First, let's see how many tokens we need to attribute to the previous version of the of the CsmMix // First, let's see how many tokens we need to attribute to the previous version of the of the CsmMix
DifferenceElementCalculator.Reshuffled reshuffled = (DifferenceElementCalculator.Reshuffled)diffElement; Reshuffled reshuffled = (Reshuffled)diffElement;
CsmMix elementsFromPreviousOrder = reshuffled.previousOrder; CsmMix elementsFromPreviousOrder = reshuffled.getPreviousOrder();
CsmMix elementsFromNextOrder = reshuffled.element; CsmMix elementsFromNextOrder = reshuffled.getNextOrder();


// This contains indexes from elementsFromNextOrder to indexes from elementsFromPreviousOrder // This contains indexes from elementsFromNextOrder to indexes from elementsFromPreviousOrder
Map<Integer, Integer> correspondanceBetweenNextOrderAndPreviousOrder = getCorrespondanceBetweenNextOrderAndPreviousOrder(elementsFromPreviousOrder, elementsFromNextOrder); Map<Integer, Integer> correspondanceBetweenNextOrderAndPreviousOrder = getCorrespondanceBetweenNextOrderAndPreviousOrder(elementsFromPreviousOrder, elementsFromNextOrder);
Expand Down Expand Up @@ -386,16 +387,16 @@ void apply() {
int indexOfOriginalCSMElement = nodeTextIndexToPreviousCSMIndex.get(ntIndex); int indexOfOriginalCSMElement = nodeTextIndexToPreviousCSMIndex.get(ntIndex);
if (elementsToAddBeforeGivenOriginalCSMElement.containsKey(indexOfOriginalCSMElement)) { if (elementsToAddBeforeGivenOriginalCSMElement.containsKey(indexOfOriginalCSMElement)) {
for (CsmElement elementToAdd : elementsToAddBeforeGivenOriginalCSMElement.get(indexOfOriginalCSMElement)) { for (CsmElement elementToAdd : elementsToAddBeforeGivenOriginalCSMElement.get(indexOfOriginalCSMElement)) {
diffElements.add(diffElIterator++, new DifferenceElementCalculator.Added(elementToAdd)); diffElements.add(diffElIterator++, new Added(elementToAdd));
} }
} }


CsmElement originalCSMElement = elementsFromPreviousOrder.getElements().get(indexOfOriginalCSMElement); CsmElement originalCSMElement = elementsFromPreviousOrder.getElements().get(indexOfOriginalCSMElement);
boolean toBeKept = correspondanceBetweenNextOrderAndPreviousOrder.containsValue(indexOfOriginalCSMElement); boolean toBeKept = correspondanceBetweenNextOrderAndPreviousOrder.containsValue(indexOfOriginalCSMElement);
if (toBeKept) { if (toBeKept) {
diffElements.add(diffElIterator++, new DifferenceElementCalculator.Kept(originalCSMElement)); diffElements.add(diffElIterator++, new Kept(originalCSMElement));
} else { } else {
diffElements.add(diffElIterator++, new DifferenceElementCalculator.Removed(originalCSMElement)); diffElements.add(diffElIterator++, new Removed(originalCSMElement));
} }
} }
// else we have a simple node text element, without associated csm element, just keep ignore it // else we have a simple node text element, without associated csm element, just keep ignore it
Expand All @@ -405,7 +406,7 @@ void apply() {
// Finally we look for the remaining new elements that were not yet added and // Finally we look for the remaining new elements that were not yet added and
// add all of them // add all of them
for (CsmElement elementToAdd : elementsToBeAddedAtTheEnd) { for (CsmElement elementToAdd : elementsToBeAddedAtTheEnd) {
diffElements.add(diffElIterator++, new DifferenceElementCalculator.Added(elementToAdd)); diffElements.add(diffElIterator++, new Added(elementToAdd));
} }
} else { } else {
throw new UnsupportedOperationException("" + diffElement + " vs " + originalElement); throw new UnsupportedOperationException("" + diffElement + " vs " + originalElement);
Expand Down Expand Up @@ -437,7 +438,7 @@ private Map<Integer, Integer> getCorrespondanceBetweenNextOrderAndPreviousOrder(
return correspondanceBetweenNextOrderAndPreviousOrder; return correspondanceBetweenNextOrderAndPreviousOrder;
} }


private boolean isFollowedByUnindent(List<DifferenceElementCalculator.DifferenceElement> diffElements, int diffIndex) { private boolean isFollowedByUnindent(List<DifferenceElement> diffElements, int diffIndex) {
return (diffIndex + 1) < diffElements.size() return (diffIndex + 1) < diffElements.size()
&& diffElements.get(diffIndex + 1).isAdded() && diffElements.get(diffIndex + 1).isAdded()
&& diffElements.get(diffIndex + 1).getElement() instanceof CsmUnindent; && diffElements.get(diffIndex + 1).getElement() instanceof CsmUnindent;
Expand Down Expand Up @@ -551,7 +552,7 @@ private int adjustIndentation(List<TokenTextElement> indentation, NodeText nodeT
} }


private boolean isAReplacement(int diffIndex) { private boolean isAReplacement(int diffIndex) {
return (diffIndex > 0) && diffElements.get(diffIndex) instanceof DifferenceElementCalculator.Added && diffElements.get(diffIndex - 1) instanceof DifferenceElementCalculator.Removed; return (diffIndex > 0) && diffElements.get(diffIndex) instanceof Added && diffElements.get(diffIndex - 1) instanceof Removed;
} }


private boolean isPrimitiveType(TextElement textElement) { private boolean isPrimitiveType(TextElement textElement) {
Expand Down

0 comments on commit a25f53f

Please sign in to comment.