Skip to content

Commit

Permalink
Moved method parameters from Difference.apply to its constructor as i…
Browse files Browse the repository at this point in the history
…nstance variables
  • Loading branch information
ThLeu committed May 19, 2018
1 parent 40f6fef commit b9976c5
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 266 deletions.

Large diffs are not rendered by default.

Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.util.EnumSet;
import java.util.List;

import static com.github.javaparser.TokenTypes.eolTokenKind;
import static com.github.javaparser.TokenTypes.spaceTokenKind;
Expand Down Expand Up @@ -269,20 +270,20 @@ public void differenceAfterddingStatementToEmptyBlock() throws IOException {
new NameExpr("aField"),
AssignExpr.Operator.ASSIGN
));
Difference diff = ldc.calculateListAdditionDifference(
List<DifferenceElementCalculator.DifferenceElement> differenceElements = ldc.calculateListAdditionDifference(
ObservableProperty.STATEMENTS,
setter.getBody().get().getStatements(),
0,
assignStatement);
int index = 0;
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), diff.getElements().get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.newline()), diff.getElements().get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.indent()), diff.getElements().get(index++));
assertTrue(isAddedChild(diff.getElements().get(index++), ExpressionStmt.class));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.newline()), diff.getElements().get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.unindent()), diff.getElements().get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), diff.getElements().get(index++));
assertEquals(index, diff.getElements().size());
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.LBRACE)), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.newline()), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.indent()), differenceElements.get(index++));
assertTrue(isAddedChild(differenceElements.get(index++), ExpressionStmt.class));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.newline()), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.added(CsmElement.unindent()), differenceElements.get(index++));
assertEquals(DifferenceElementCalculator.DifferenceElement.kept(CsmElement.token(GeneratedJavaParserConstants.RBRACE)), differenceElements.get(index++));
assertEquals(index, differenceElements.size());
}

private boolean isAddedChild(DifferenceElementCalculator.DifferenceElement element, Class<? extends Node> childClass) {
Expand Down
Expand Up @@ -22,9 +22,13 @@ public class Difference {
private static final int STANDARD_INDENTATION_SIZE = 4;

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

Difference(List<DifferenceElementCalculator.DifferenceElement> elements) {
Difference(List<DifferenceElementCalculator.DifferenceElement> elements, NodeText nodeText, Node node) {
this.elements = elements;
this.nodeText = nodeText;
this.node = node;
}

private List<TextElement> processIndentation(List<TokenTextElement> indentation, List<TextElement> prevElements) {
Expand Down Expand Up @@ -79,7 +83,7 @@ private boolean isAfterLBrace(NodeText nodeText, int nodeTextIndex) {
*/
private int considerEnforcingIndentation(NodeText nodeText, int nodeTextIndex) {
boolean hasOnlyWsBefore = true;
for (int i=nodeTextIndex; i >= 0 && hasOnlyWsBefore && i < nodeText.getElements().size(); i--) {
for (int i = nodeTextIndex; i >= 0 && hasOnlyWsBefore && i < nodeText.getElements().size(); i--) {
if (nodeText.getElements().get(i).isNewline()) {
break;
}
Expand All @@ -88,7 +92,7 @@ private int considerEnforcingIndentation(NodeText nodeText, int nodeTextIndex) {
}
}
if (hasOnlyWsBefore) {
for (int i=nodeTextIndex; i >= 0 && i < nodeText.getElements().size(); i--) {
for (int i = nodeTextIndex; i >= 0 && i < nodeText.getElements().size(); i--) {
if (nodeText.getElements().get(i).isNewline()) {
break;
}
Expand All @@ -102,12 +106,12 @@ private int considerEnforcingIndentation(NodeText nodeText, int nodeTextIndex) {
* Node that we have calculate the Difference we can apply to a concrete NodeText, modifying it according
* to the difference (adding and removing the elements provided).
*/
void apply(NodeText nodeText, Node node) {
if (nodeText == null) {
void apply() {
if (this.nodeText == null) {
throw new NullPointerException();
}
boolean addedIndentation = false;
List<TokenTextElement> indentation = LexicalPreservingPrinter.findIndentation(node);
List<TokenTextElement> indentation = LexicalPreservingPrinter.findIndentation(this.node);

List<TextElement> originalElements = nodeText.getElements();
int originalIndex = 0;
Expand Down Expand Up @@ -445,7 +449,7 @@ private List<Integer> findIndexOfCorrespondingNodeTextElement(List<CsmElement> e
int nextCsmElementIndex = csmElementListIterator.nextIndex();

Map<MatchClassification, Integer> potentialMatches = new EnumMap<>(MatchClassification.class);
for (int i = startIndex; i<nodeText.getElements().size(); i++){
for (int i = startIndex; i< nodeText.getElements().size(); i++){
if (!correspondingIndices.contains(i)) {
TextElement textElement = nodeText.getTextElement(i);

Expand Down Expand Up @@ -534,7 +538,7 @@ private int adjustIndentation(List<TokenTextElement> indentation, NodeText nodeT
indentationAdj = indentationAdj.subList(0, Math.max(0, indentationAdj.size() - STANDARD_INDENTATION_SIZE));
}
for (TextElement e : indentationAdj) {
if ((nodeTextIndex<nodeText.getElements().size()) && nodeText.getElements().get(nodeTextIndex).isSpaceOrTab()) {
if ((nodeTextIndex< nodeText.getElements().size()) && nodeText.getElements().get(nodeTextIndex).isSpaceOrTab()) {
nodeTextIndex++;
} else {
nodeText.getElements().add(nodeTextIndex++, e);
Expand Down Expand Up @@ -562,11 +566,6 @@ private boolean isPrimitiveType(TextElement textElement) {
return false;
}
}

long cost() {
return elements.stream().filter(e -> !(e instanceof DifferenceElementCalculator.Kept)).count();
}

@Override
public String toString() {
return "Difference{" + elements + '}';
Expand All @@ -575,12 +574,4 @@ public String toString() {
List<DifferenceElementCalculator.DifferenceElement> getElements() {
return elements;
}

/**
* Remove from the difference all the elements related to indentation.
* This is mainly intended for test purposes.
*/
void removeIndentationElements() {
elements.removeIf(el -> el.getElement() instanceof CsmIndent || el.getElement() instanceof CsmUnindent);
}
}
Expand Up @@ -89,7 +89,7 @@ private static Map<Node, Integer> findChildrenPositions(LexicalDifferenceCalcula
* Calculate the Difference between two CalculatedSyntaxModel elements, determining which elements were kept,
* which were added and which were removed.
*/
static Difference calculate(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
static List<DifferenceElement> calculate(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
// For performance reasons we use the positions of matching children
// to guide the calculation of the difference
//
Expand Down Expand Up @@ -119,20 +119,20 @@ static Difference calculate(LexicalDifferenceCalculator.CalculatedSyntaxModel or
int posOfNextChildInOriginal = childrenInOriginal.get(child);
int posOfNextChildInAfter = childrenInAfter.get(child);
if (originalIndex < posOfNextChildInOriginal || afterIndex < posOfNextChildInAfter) {
elements.addAll(calculateImpl(original.sub(originalIndex, posOfNextChildInOriginal), after.sub(afterIndex, posOfNextChildInAfter)).getElements());
elements.addAll(calculateImpl(original.sub(originalIndex, posOfNextChildInOriginal), after.sub(afterIndex, posOfNextChildInAfter)));
}
elements.add(new Kept(new LexicalDifferenceCalculator.CsmChild(child)));
originalIndex = posOfNextChildInOriginal + 1;
afterIndex = posOfNextChildInAfter + 1;
}

if (originalIndex < original.elements.size() || afterIndex < after.elements.size()) {
elements.addAll(calculateImpl(original.sub(originalIndex, original.elements.size()), after.sub(afterIndex, after.elements.size())).getElements());
elements.addAll(calculateImpl(original.sub(originalIndex, original.elements.size()), after.sub(afterIndex, after.elements.size())));
}
return new Difference(elements);
return elements;
}

private static Difference calculateImpl(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
private static List<DifferenceElement> calculateImpl(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
List<DifferenceElement> elements = new LinkedList<>();

int originalIndex = 0;
Expand Down Expand Up @@ -172,13 +172,13 @@ private static Difference calculateImpl(LexicalDifferenceCalculator.CalculatedSy
afterIndex++;
} else {
// We can try to remove the element or add it and look which one leads to the lower difference
Difference adding = calculate(original.from(originalIndex), after.from(afterIndex + 1));
Difference removing = null;
if (adding.cost() > 0) {
removing = calculate(original.from(originalIndex + 1), after.from(afterIndex));
List<DifferenceElement> addingElements = calculate(original.from(originalIndex), after.from(afterIndex + 1));
List<DifferenceElement> removingElements = null;
if (cost(addingElements) > 0) {
removingElements = calculate(original.from(originalIndex + 1), after.from(afterIndex));
}

if (removing == null || removing.cost() > adding.cost()) {
if (removingElements == null || cost(removingElements) > cost(addingElements)) {
elements.add(new Added(nextAfter));
afterIndex++;
} else {
Expand All @@ -189,7 +189,7 @@ private static Difference calculateImpl(LexicalDifferenceCalculator.CalculatedSy
}
} while (originalIndex < original.elements.size() || afterIndex < after.elements.size());

return new Difference(elements);
return elements;
}

interface DifferenceElement {
Expand Down Expand Up @@ -471,4 +471,17 @@ boolean isWhiteSpace() {
return false;
}
}

static long cost(List<DifferenceElement> elements) {
return elements.stream().filter(e -> !(e instanceof DifferenceElementCalculator.Kept)).count();
}


/**
* Remove from the difference all the elements related to indentation.
* This is mainly intended for test purposes.
*/
static void removeIndentationElements(List<DifferenceElement> elements) {
elements.removeIf(el -> el.getElement() instanceof CsmIndent || el.getElement() instanceof CsmUnindent);
}
}
Expand Up @@ -86,23 +86,23 @@ public int hashCode() {
}
}

Difference calculateListRemovalDifference(ObservableProperty observableProperty, NodeList nodeList, int index) {
List<DifferenceElementCalculator.DifferenceElement> calculateListRemovalDifference(ObservableProperty observableProperty, NodeList nodeList, int index) {
Node container = nodeList.getParentNodeForChildren();
CsmElement element = ConcreteSyntaxModel.forClass(container.getClass());
CalculatedSyntaxModel original = calculatedSyntaxModelForNode(element, container);
CalculatedSyntaxModel after = calculatedSyntaxModelAfterListRemoval(element, observableProperty, nodeList, index);
return DifferenceElementCalculator.calculate(original, after);
}

Difference calculateListAdditionDifference(ObservableProperty observableProperty, NodeList nodeList, int index, Node nodeAdded) {
List<DifferenceElementCalculator.DifferenceElement> calculateListAdditionDifference(ObservableProperty observableProperty, NodeList nodeList, int index, Node nodeAdded) {
Node container = nodeList.getParentNodeForChildren();
CsmElement element = ConcreteSyntaxModel.forClass(container.getClass());
CalculatedSyntaxModel original = calculatedSyntaxModelForNode(element, container);
CalculatedSyntaxModel after = calculatedSyntaxModelAfterListAddition(element, observableProperty, nodeList, index, nodeAdded);
return DifferenceElementCalculator.calculate(original, after);
}

Difference calculateListReplacementDifference(ObservableProperty observableProperty, NodeList nodeList, int index, Node newValue) {
List<DifferenceElementCalculator.DifferenceElement> calculateListReplacementDifference(ObservableProperty observableProperty, NodeList nodeList, int index, Node newValue) {
Node container = nodeList.getParentNodeForChildren();
CsmElement element = ConcreteSyntaxModel.forClass(container.getClass());
CalculatedSyntaxModel original = calculatedSyntaxModelForNode(element, container);
Expand All @@ -117,8 +117,9 @@ public void calculatePropertyChange(NodeText nodeText, Node observedNode, Observ
CsmElement element = ConcreteSyntaxModel.forClass(observedNode.getClass());
CalculatedSyntaxModel original = calculatedSyntaxModelForNode(element, observedNode);
CalculatedSyntaxModel after = calculatedSyntaxModelAfterPropertyChange(element, observedNode, property, oldValue, newValue);
Difference difference = DifferenceElementCalculator.calculate(original, after);
difference.apply(nodeText, observedNode);
List<DifferenceElementCalculator.DifferenceElement> differenceElements = DifferenceElementCalculator.calculate(original, after);
Difference difference = new Difference(differenceElements, nodeText, observedNode);
difference.apply();
}

// Visible for testing
Expand Down
Expand Up @@ -197,22 +197,26 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
@Override
public void concreteListChange(NodeList changedList, AstObserver.ListChangeType type, int index, Node nodeAddedOrRemoved) {
NodeText nodeText = getOrCreateNodeText(changedList.getParentNodeForChildren());
final Difference difference;
List<DifferenceElementCalculator.DifferenceElement> differenceElements;
if (type == AstObserver.ListChangeType.REMOVAL) {
difference = LEXICAL_DIFFERENCE_CALCULATOR.calculateListRemovalDifference(findNodeListName(changedList), changedList, index);
differenceElements = LEXICAL_DIFFERENCE_CALCULATOR.calculateListRemovalDifference(findNodeListName(changedList), changedList, index);
} else if (type == AstObserver.ListChangeType.ADDITION) {
difference = LEXICAL_DIFFERENCE_CALCULATOR.calculateListAdditionDifference(findNodeListName(changedList), changedList, index, nodeAddedOrRemoved);
differenceElements = LEXICAL_DIFFERENCE_CALCULATOR.calculateListAdditionDifference(findNodeListName(changedList), changedList, index, nodeAddedOrRemoved);
} else {
throw new UnsupportedOperationException();
}
difference.apply(nodeText, changedList.getParentNodeForChildren());

Difference difference = new Difference(differenceElements, nodeText, changedList.getParentNodeForChildren());
difference.apply();
}

@Override
public void concreteListReplacement(NodeList changedList, int index, Node oldValue, Node newValue) {
NodeText nodeText = getOrCreateNodeText(changedList.getParentNodeForChildren());
Difference difference = LEXICAL_DIFFERENCE_CALCULATOR.calculateListReplacementDifference(findNodeListName(changedList), changedList, index, newValue);
difference.apply(nodeText, changedList.getParentNodeForChildren());
List<DifferenceElementCalculator.DifferenceElement> differenceElements = LEXICAL_DIFFERENCE_CALCULATOR.calculateListReplacementDifference(findNodeListName(changedList), changedList, index, newValue);

Difference difference = new Difference(differenceElements, nodeText, changedList.getParentNodeForChildren());
difference.apply();
}
}

Expand Down

0 comments on commit b9976c5

Please sign in to comment.