From 73ca00a6b9cbcb818e29595ccf068c43e862564b Mon Sep 17 00:00:00 2001 From: Federico Tomassetti Date: Mon, 24 Dec 2018 11:21:24 +0100 Subject: [PATCH] working on expanding removed child for FieldDeclaration --- .../lexicalpreservation/Difference.java | 2 + .../DifferenceElementCalculator.java | 51 +++++++++++++------ .../printer/lexicalpreservation/NodeText.java | 2 +- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/Difference.java b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/Difference.java index 13f0d7a312..22ee568ffd 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/Difference.java +++ b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/Difference.java @@ -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; diff --git a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/DifferenceElementCalculator.java b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/DifferenceElementCalculator.java index 03ed39c591..efeea746f9 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/DifferenceElementCalculator.java +++ b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/DifferenceElementCalculator.java @@ -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; @@ -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; @@ -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; @@ -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; } } @@ -76,8 +78,8 @@ private static Map findChildrenPositions(LexicalDifferenceCalcula Map positions = new HashMap<>(); for (int i=0;i 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; } @@ -130,7 +132,8 @@ static List calculate(LexicalDifferenceCalculator.CalculatedS return elements; } - private static List calculateImpl(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) { + private static List calculateImpl(LexicalDifferenceCalculator.CalculatedSyntaxModel original, + LexicalDifferenceCalculator.CalculatedSyntaxModel after) { List elements = new LinkedList<>(); int originalIndex = 0; @@ -141,8 +144,24 @@ private static List 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++; diff --git a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/NodeText.java b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/NodeText.java index 65cdf232a9..ba1ccfee6f 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/NodeText.java +++ b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/NodeText.java @@ -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 nodeChildren = node.getChildNodes(); + List nodeChildren = new LinkedList<>(node.getChildNodes()); List textChildren = elements.stream() .filter(TextElement::isChild) .map(el -> ((ChildTextElement)el).getChild())