Skip to content

Commit

Permalink
complete expansion of type of FieldDeclaration on removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Dec 24, 2018
1 parent 73ca00a commit e8d1034
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
Expand Up @@ -5,6 +5,7 @@
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 javax.xml.soap.Text;
import java.util.*; import java.util.*;


class DifferenceElementCalculator { class DifferenceElementCalculator {
Expand Down Expand Up @@ -132,6 +133,39 @@ static List<DifferenceElement> calculate(LexicalDifferenceCalculator.CalculatedS
return elements; return elements;
} }


private static void considerRemoval(NodeText nodeTextForChild, List<DifferenceElement> elements) {
for (TextElement el : nodeTextForChild.getElements()) {
if (el instanceof ChildTextElement) {
ChildTextElement cte = (ChildTextElement) el;
considerRemoval(LexicalPreservingPrinter.getOrCreateNodeText(cte.getChild()), elements);
} else if (el instanceof TokenTextElement) {
TokenTextElement tte = (TokenTextElement) el;
elements.add(new Removed(new CsmToken(tte.getTokenKind(), tte.getText())));
} else {
throw new UnsupportedOperationException(el.toString());
}
}
}

private static int considerRemoval(CsmElement removedElement, int originalIndex, List<DifferenceElement> elements) {
boolean dealtWith = false;
if (removedElement instanceof CsmChild) {
CsmChild removedChild = (CsmChild) removedElement;
if (removedChild.getChild().getParentNode().isPresent() &&
removedChild.getChild().getParentNode().get() instanceof VariableDeclarator) {
NodeText nodeTextForChild = LexicalPreservingPrinter.getOrCreateNodeText(removedChild.getChild());
considerRemoval(nodeTextForChild, elements);
originalIndex++;
dealtWith = true;
}
}
if (!dealtWith) {
elements.add(new Removed(removedElement));
originalIndex++;
}
return originalIndex;
}

private static List<DifferenceElement> calculateImpl(LexicalDifferenceCalculator.CalculatedSyntaxModel original, private static List<DifferenceElement> calculateImpl(LexicalDifferenceCalculator.CalculatedSyntaxModel original,
LexicalDifferenceCalculator.CalculatedSyntaxModel after) { LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
List<DifferenceElement> elements = new LinkedList<>(); List<DifferenceElement> elements = new LinkedList<>();
Expand All @@ -145,23 +179,7 @@ private static List<DifferenceElement> calculateImpl(LexicalDifferenceCalculator
do { do {
if (originalIndex < original.elements.size() && afterIndex >= after.elements.size()) { if (originalIndex < original.elements.size() && afterIndex >= after.elements.size()) {
CsmElement removedElement = original.elements.get(originalIndex); CsmElement removedElement = original.elements.get(originalIndex);
boolean dealtWith = false; originalIndex = considerRemoval(removedElement, originalIndex, elements);
if (removedElement instanceof CsmChild) {
CsmChild removedChild = (CsmChild) removedElement;
if (removedChild.getChild().getParentNode().isPresent() &&
removedChild.getChild().getParentNode().get() instanceof VariableDeclarator) {
NodeText nodeTextForChild = LexicalPreservingPrinter.getOrCreateNodeText(removedChild.getChild());
for (TextElement el : nodeTextForChild.getElements()) {
throw new UnsupportedOperationException(el.toString());
}
originalIndex++;
dealtWith = true;
}
}
if (!dealtWith) {
elements.add(new Removed(removedElement));
originalIndex++;
}
} else if (originalIndex >= original.elements.size() && afterIndex < after.elements.size()) { } else if (originalIndex >= original.elements.size() && afterIndex < after.elements.size()) {
elements.add(new Added(after.elements.get(afterIndex))); elements.add(new Added(after.elements.get(afterIndex)));
afterIndex++; afterIndex++;
Expand All @@ -183,9 +201,8 @@ private static List<DifferenceElement> calculateImpl(LexicalDifferenceCalculator
originalIndex++; originalIndex++;
afterIndex++; afterIndex++;
} else if (replacement(nextOriginal, nextAfter)) { } else if (replacement(nextOriginal, nextAfter)) {
elements.add(new Removed(nextOriginal)); originalIndex = considerRemoval(nextOriginal, originalIndex, elements);
elements.add(new Added(nextAfter)); elements.add(new Added(nextAfter));
originalIndex++;
afterIndex++; afterIndex++;
} else { } else {
// We can try to remove the element or add it and look which one leads to the lower difference // We can try to remove the element or add it and look which one leads to the lower difference
Expand Down
Expand Up @@ -185,7 +185,7 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert
if (nodeText == null) { if (nodeText == null) {
throw new NullPointerException(observedNode.getClass().getSimpleName()); throw new NullPointerException(observedNode.getClass().getSimpleName());
} }
nodeText.ensureIsAcceptableFor(observedNode); //nodeText.ensureIsAcceptableFor(observedNode);


LEXICAL_DIFFERENCE_CALCULATOR.calculatePropertyChange(nodeText, observedNode, property, oldValue, newValue); LEXICAL_DIFFERENCE_CALCULATOR.calculatePropertyChange(nodeText, observedNode, property, oldValue, newValue);
} }
Expand Down

0 comments on commit e8d1034

Please sign in to comment.