Skip to content

Commit

Permalink
working on expanding removed child for FieldDeclaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Dec 24, 2018
1 parent a37a404 commit 73ca00a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
Expand Up @@ -3,6 +3,8 @@
import com.github.javaparser.GeneratedJavaParserConstants;
import com.github.javaparser.TokenTypes;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.printer.concretesyntaxmodel.*;
import com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild;
Expand Down
@@ -1,16 +1,18 @@
package com.github.javaparser.printer.lexicalpreservation;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.printer.concretesyntaxmodel.*;
import com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild;

import java.util.*;

class DifferenceElementCalculator {
static boolean matching(CsmElement a, CsmElement b) {
if (a instanceof LexicalDifferenceCalculator.CsmChild) {
if (b instanceof LexicalDifferenceCalculator.CsmChild) {
LexicalDifferenceCalculator.CsmChild childA = (LexicalDifferenceCalculator.CsmChild) a;
LexicalDifferenceCalculator.CsmChild childB = (LexicalDifferenceCalculator.CsmChild) b;
if (a instanceof CsmChild) {
if (b instanceof CsmChild) {
CsmChild childA = (CsmChild) a;
CsmChild childB = (CsmChild) b;
return childA.getChild().equals(childB.getChild());
} else if (b instanceof CsmToken) {
return false;
Expand All @@ -26,7 +28,7 @@ static boolean matching(CsmElement a, CsmElement b) {
CsmToken childA = (CsmToken)a;
CsmToken childB = (CsmToken)b;
return childA.getTokenType() == childB.getTokenType();
} else if (b instanceof LexicalDifferenceCalculator.CsmChild) {
} else if (b instanceof CsmChild) {
return false;
} else if (b instanceof CsmIndent) {
return false;
Expand All @@ -47,10 +49,10 @@ private static boolean replacement(CsmElement a, CsmElement b) {
if (a instanceof CsmIndent || b instanceof CsmIndent || a instanceof CsmUnindent || b instanceof CsmUnindent) {
return false;
}
if (a instanceof LexicalDifferenceCalculator.CsmChild) {
if (b instanceof LexicalDifferenceCalculator.CsmChild) {
LexicalDifferenceCalculator.CsmChild childA = (LexicalDifferenceCalculator.CsmChild) a;
LexicalDifferenceCalculator.CsmChild childB = (LexicalDifferenceCalculator.CsmChild) b;
if (a instanceof CsmChild) {
if (b instanceof CsmChild) {
CsmChild childA = (CsmChild) a;
CsmChild childB = (CsmChild) b;
return childA.getChild().getClass().equals(childB.getChild().getClass());
} else if (b instanceof CsmToken) {
return false;
Expand All @@ -62,7 +64,7 @@ private static boolean replacement(CsmElement a, CsmElement b) {
CsmToken childA = (CsmToken)a;
CsmToken childB = (CsmToken)b;
return childA.getTokenType() == childB.getTokenType();
} else if (b instanceof LexicalDifferenceCalculator.CsmChild) {
} else if (b instanceof CsmChild) {
return false;
}
}
Expand All @@ -76,8 +78,8 @@ private static Map<Node, Integer> findChildrenPositions(LexicalDifferenceCalcula
Map<Node, Integer> positions = new HashMap<>();
for (int i=0;i<calculatedSyntaxModel.elements.size();i++) {
CsmElement element = calculatedSyntaxModel.elements.get(i);
if (element instanceof LexicalDifferenceCalculator.CsmChild) {
positions.put(((LexicalDifferenceCalculator.CsmChild)element).getChild(), i);
if (element instanceof CsmChild) {
positions.put(((CsmChild)element).getChild(), i);
}
}
return positions;
Expand Down Expand Up @@ -119,7 +121,7 @@ static List<DifferenceElement> calculate(LexicalDifferenceCalculator.CalculatedS
if (originalIndex < posOfNextChildInOriginal || afterIndex < posOfNextChildInAfter) {
elements.addAll(calculateImpl(original.sub(originalIndex, posOfNextChildInOriginal), after.sub(afterIndex, posOfNextChildInAfter)));
}
elements.add(new Kept(new LexicalDifferenceCalculator.CsmChild(child)));
elements.add(new Kept(new CsmChild(child)));
originalIndex = posOfNextChildInOriginal + 1;
afterIndex = posOfNextChildInAfter + 1;
}
Expand All @@ -130,7 +132,8 @@ static List<DifferenceElement> calculate(LexicalDifferenceCalculator.CalculatedS
return elements;
}

private static List<DifferenceElement> 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 All @@ -141,8 +144,24 @@ private static List<DifferenceElement> calculateImpl(LexicalDifferenceCalculator

do {
if (originalIndex < original.elements.size() && afterIndex >= after.elements.size()) {
elements.add(new Removed(original.elements.get(originalIndex)));
originalIndex++;
CsmElement removedElement = original.elements.get(originalIndex);
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());
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()) {
elements.add(new Added(after.elements.get(afterIndex)));
afterIndex++;
Expand Down
Expand Up @@ -39,7 +39,7 @@ class NodeText {

public void ensureIsAcceptableFor(Node node) {
// We want to check that all children in the node and in the node text are the same
List<Node> nodeChildren = node.getChildNodes();
List<Node> nodeChildren = new LinkedList<>(node.getChildNodes());
List<Node> textChildren = elements.stream()
.filter(TextElement::isChild)
.map(el -> ((ChildTextElement)el).getChild())
Expand Down

0 comments on commit 73ca00a

Please sign in to comment.