Skip to content

Commit

Permalink
Renamed given to original
Browse files Browse the repository at this point in the history
  • Loading branch information
ThLeu committed May 18, 2018
1 parent 77386fe commit 2ae6f77
Showing 1 changed file with 69 additions and 69 deletions.
Expand Up @@ -538,13 +538,13 @@ void apply(NodeText nodeText, Node node) {
boolean addedIndentation = false; boolean addedIndentation = false;
List<TokenTextElement> indentation = LexicalPreservingPrinter.findIndentation(node); List<TokenTextElement> indentation = LexicalPreservingPrinter.findIndentation(node);


List<TextElement> givenElements = nodeText.getElements(); List<TextElement> originalElements = nodeText.getElements();
int givenIndex = 0; int originalIndex = 0;


List<DifferenceElement> diffElements = this.elements; List<DifferenceElement> diffElements = this.elements;
int diffIndex = 0; int diffIndex = 0;
do { do {
if (diffIndex < diffElements.size() && givenIndex >= givenElements.size()) { if (diffIndex < diffElements.size() && originalIndex >= originalElements.size()) {
DifferenceElement diffElement = diffElements.get(diffIndex); DifferenceElement diffElement = diffElements.get(diffIndex);
if (diffElement instanceof Kept) { if (diffElement instanceof Kept) {
Kept kept = (Kept) diffElement; Kept kept = (Kept) diffElement;
Expand All @@ -558,19 +558,19 @@ void apply(NodeText nodeText, Node node) {
} else if (diffElement instanceof Added) { } else if (diffElement instanceof Added) {
Added addedElement = (Added) diffElement; Added addedElement = (Added) diffElement;


nodeText.addElement(givenIndex, addedElement.toTextElement()); nodeText.addElement(originalIndex, addedElement.toTextElement());
givenIndex++; originalIndex++;
diffIndex++; diffIndex++;
} else { } else {
throw new UnsupportedOperationException(diffElement.getClass().getSimpleName()); throw new UnsupportedOperationException(diffElement.getClass().getSimpleName());
} }
} else if (diffIndex >= diffElements.size() && givenIndex < givenElements.size()) { } else if (diffIndex >= diffElements.size() && originalIndex < originalElements.size()) {
TextElement givenElement = givenElements.get(givenIndex); TextElement originalElement = originalElements.get(originalIndex);
if (givenElement.isWhiteSpaceOrComment()) { if (originalElement.isWhiteSpaceOrComment()) {
givenIndex++; originalIndex++;
} else { } else {
throw new UnsupportedOperationException("NodeText: " + nodeText + ". Difference: " throw new UnsupportedOperationException("NodeText: " + nodeText + ". Difference: "
+ this + " " + givenElement); + this + " " + originalElement);
} }
} else { } else {
DifferenceElement diffElement = diffElements.get(diffIndex); DifferenceElement diffElement = diffElements.get(diffIndex);
Expand All @@ -596,21 +596,21 @@ void apply(NodeText nodeText, Node node) {
} }
TextElement textElement = addedElement.toTextElement(); TextElement textElement = addedElement.toTextElement();
boolean used = false; boolean used = false;
if (givenIndex > 0 && givenElements.get(givenIndex - 1).isNewline()) { if (originalIndex > 0 && originalElements.get(originalIndex - 1).isNewline()) {
for (TextElement e : processIndentation(indentation, givenElements.subList(0, givenIndex - 1))) { for (TextElement e : processIndentation(indentation, originalElements.subList(0, originalIndex - 1))) {
nodeText.addElement(givenIndex++, e); nodeText.addElement(originalIndex++, e);
} }
} else if (isAfterLBrace(nodeText, givenIndex) && !isAReplacement(diffIndex)) { } else if (isAfterLBrace(nodeText, originalIndex) && !isAReplacement(diffIndex)) {
if (textElement.isNewline()) { if (textElement.isNewline()) {
used = true; used = true;
} }
nodeText.addElement(givenIndex++, new TokenTextElement(TokenTypes.eolTokenKind())); nodeText.addElement(originalIndex++, new TokenTextElement(TokenTypes.eolTokenKind()));
// This remove the space in "{ }" when adding a new line // This remove the space in "{ }" when adding a new line
while (givenElements.get(givenIndex).isSpaceOrTab()) { while (originalElements.get(originalIndex).isSpaceOrTab()) {
givenElements.remove(givenIndex); originalElements.remove(originalIndex);
} }
for (TextElement e : processIndentation(indentation, givenElements.subList(0, givenIndex - 1))) { for (TextElement e : processIndentation(indentation, originalElements.subList(0, originalIndex - 1))) {
nodeText.addElement(givenIndex++, e); nodeText.addElement(originalIndex++, e);
} }
// Indentation is painful... // Indentation is painful...
// Sometimes we want to force indentation: this is the case when indentation was expected but // Sometimes we want to force indentation: this is the case when indentation was expected but
Expand All @@ -619,54 +619,54 @@ void apply(NodeText nodeText, Node node) {
// inserted by us in this transformation we do not want to insert it again // inserted by us in this transformation we do not want to insert it again
if (!addedIndentation) { if (!addedIndentation) {
for (TextElement e : indentationBlock()) { for (TextElement e : indentationBlock()) {
nodeText.addElement(givenIndex++, e); nodeText.addElement(originalIndex++, e);
} }
} }
} }
if (!used) { if (!used) {
nodeText.addElement(givenIndex, textElement); nodeText.addElement(originalIndex, textElement);
givenIndex++; originalIndex++;
} }
if (textElement.isNewline()) { if (textElement.isNewline()) {
boolean followedByUnindent = (diffIndex + 1) < diffElements.size() boolean followedByUnindent = (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;
givenIndex = adjustIndentation(indentation, nodeText, givenIndex, followedByUnindent/* && !addedIndentation*/); originalIndex = adjustIndentation(indentation, nodeText, originalIndex, followedByUnindent/* && !addedIndentation*/);
} }
diffIndex++; diffIndex++;
} else { } else {
TextElement givenElement = givenElements.get(givenIndex); TextElement originalElement = originalElements.get(originalIndex);
boolean givenElementIsChild = givenElement instanceof ChildTextElement; boolean originalElementIsChild = originalElement instanceof ChildTextElement;
boolean givenElementIsToken = givenElement instanceof TokenTextElement; boolean originalElementIsToken = originalElement instanceof TokenTextElement;


if (diffElement instanceof Kept) { if (diffElement instanceof Kept) {
Kept kept = (Kept)diffElement; Kept kept = (Kept)diffElement;
if (givenElement.isComment()) { if (originalElement.isComment()) {
givenIndex++; originalIndex++;
} else if (kept.isChild() && givenElementIsChild) { } else if (kept.isChild() && originalElementIsChild) {
diffIndex++; diffIndex++;
givenIndex++; originalIndex++;
} else if (kept.isChild() && givenElementIsToken) { } else if (kept.isChild() && originalElementIsToken) {
if (givenElement.isWhiteSpaceOrComment()) { if (originalElement.isWhiteSpaceOrComment()) {
givenIndex++; originalIndex++;
} else { } else {
if (kept.isPrimitiveType()) { if (kept.isPrimitiveType()) {
givenIndex++; originalIndex++;
diffIndex++; diffIndex++;
} else { } else {
throw new UnsupportedOperationException("kept " + kept.element + " vs " + givenElement); throw new UnsupportedOperationException("kept " + kept.element + " vs " + originalElement);
} }
} }
} else if (kept.isToken() && givenElementIsToken) { } else if (kept.isToken() && originalElementIsToken) {
CsmToken csmToken = (CsmToken) kept.element; CsmToken csmToken = (CsmToken) kept.element;
TokenTextElement nodeTextToken = (TokenTextElement) givenElement; TokenTextElement nodeTextToken = (TokenTextElement) originalElement;
if (csmToken.getTokenType() == nodeTextToken.getTokenKind()) { if (csmToken.getTokenType() == nodeTextToken.getTokenKind()) {
givenIndex++; originalIndex++;
diffIndex++; diffIndex++;
} else if (kept.isWhiteSpaceOrComment()) { } else if (kept.isWhiteSpaceOrComment()) {
diffIndex++; diffIndex++;
} else if (nodeTextToken.isWhiteSpaceOrComment()) { } else if (nodeTextToken.isWhiteSpaceOrComment()) {
givenIndex++; originalIndex++;
} else { } else {
throw new UnsupportedOperationException("Csm token " + csmToken + " NodeText TOKEN " + nodeTextToken); throw new UnsupportedOperationException("Csm token " + csmToken + " NodeText TOKEN " + nodeTextToken);
} }
Expand All @@ -678,66 +678,66 @@ void apply(NodeText nodeText, Node node) {
} else if (kept.isUnindent()) { } else if (kept.isUnindent()) {
// Nothing to do, beside considering indentation // Nothing to do, beside considering indentation
diffIndex++; diffIndex++;
for (int i = 0; i < STANDARD_INDENTATION_SIZE && givenIndex >= 1 && nodeText.getTextElement(givenIndex - 1).isSpaceOrTab(); i++) { for (int i = 0; i < STANDARD_INDENTATION_SIZE && originalIndex >= 1 && nodeText.getTextElement(originalIndex - 1).isSpaceOrTab(); i++) {
nodeText.removeElement(--givenIndex); nodeText.removeElement(--originalIndex);
} }
} else { } else {
throw new UnsupportedOperationException("kept " + kept.element + " vs " + givenElement); throw new UnsupportedOperationException("kept " + kept.element + " vs " + originalElement);
} }
} else if (diffElement instanceof Removed) { } else if (diffElement instanceof Removed) {
Removed removed = (Removed)diffElement; Removed removed = (Removed)diffElement;
if (removed.isChild() && givenElementIsChild) { if (removed.isChild() && originalElementIsChild) {
ChildTextElement actualChild = (ChildTextElement)givenElement; ChildTextElement actualChild = (ChildTextElement)originalElement;
if (actualChild.isComment()) { if (actualChild.isComment()) {
CsmChild csmChild = (CsmChild)removed.element; CsmChild csmChild = (CsmChild)removed.element;
// We expected to remove a proper node but we found a comment in between. // We expected to remove a proper node but we found a comment in between.
// If the comment is associated to the node we want to remove we remove it as well, otherwise we keep it // If the comment is associated to the node we want to remove we remove it as well, otherwise we keep it
Comment comment = (Comment)actualChild.getChild(); Comment comment = (Comment)actualChild.getChild();
if (!comment.isOrphan() && comment.getCommentedNode().isPresent() && comment.getCommentedNode().get().equals(csmChild.getChild())) { if (!comment.isOrphan() && comment.getCommentedNode().isPresent() && comment.getCommentedNode().get().equals(csmChild.getChild())) {
nodeText.removeElement(givenIndex); nodeText.removeElement(originalIndex);
} else { } else {
givenIndex++; originalIndex++;
} }
} else { } else {
nodeText.removeElement(givenIndex); nodeText.removeElement(originalIndex);
if (givenIndex < givenElements.size() && givenElements.get(givenIndex).isNewline()) { if (originalIndex < originalElements.size() && originalElements.get(originalIndex).isNewline()) {
givenIndex = considerCleaningTheLine(nodeText, givenIndex); originalIndex = considerCleaningTheLine(nodeText, originalIndex);
} else { } else {
if (diffIndex + 1 >= this.getElements().size() || !(this.getElements().get(diffIndex + 1) instanceof Added)) { if (diffIndex + 1 >= this.getElements().size() || !(this.getElements().get(diffIndex + 1) instanceof Added)) {
givenIndex = considerEnforcingIndentation(nodeText, givenIndex); 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 (givenElements.size() > givenIndex && givenIndex > 0) { if (originalElements.size() > originalIndex && originalIndex > 0) {
if (givenElements.get(givenIndex).isWhiteSpace() if (originalElements.get(originalIndex).isWhiteSpace()
&& givenElements.get(givenIndex - 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 Kept)) { if ((diffIndex + 1) == diffElements.size() || (diffElements.get(diffIndex + 1) instanceof Kept)) {
givenElements.remove(givenIndex--); originalElements.remove(originalIndex--);
} }
} }
} }
} }
diffIndex++; diffIndex++;
} }
} else if (removed.isToken() && givenElementIsToken } else if (removed.isToken() && originalElementIsToken
&& ((CsmToken)removed.element).getTokenType() == ((TokenTextElement)givenElement).getTokenKind()) { && ((CsmToken)removed.element).getTokenType() == ((TokenTextElement)originalElement).getTokenKind()) {
nodeText.removeElement(givenIndex); nodeText.removeElement(originalIndex);
diffIndex++; diffIndex++;
} else if (givenElementIsToken && givenElement.isWhiteSpaceOrComment()) { } else if (originalElementIsToken && originalElement.isWhiteSpaceOrComment()) {
givenIndex++; originalIndex++;
} else if (removed.isPrimitiveType()) { } else if (removed.isPrimitiveType()) {
if (isPrimitiveType(givenElement)) { if (isPrimitiveType(originalElement)) {
nodeText.removeElement(givenIndex); nodeText.removeElement(originalIndex);
diffIndex++; diffIndex++;
} else { } else {
throw new UnsupportedOperationException("removed " + removed.element + " vs " + givenElement); throw new UnsupportedOperationException("removed " + removed.element + " vs " + originalElement);
} }
} else if (removed.isWhiteSpace()) { } else if (removed.isWhiteSpace()) {
diffIndex++; diffIndex++;
} else if (givenElement.isWhiteSpace()) { } else if (originalElement.isWhiteSpace()) {
givenIndex++; originalIndex++;
} else { } else {
throw new UnsupportedOperationException("removed " + removed.element + " vs " + givenElement); throw new UnsupportedOperationException("removed " + removed.element + " vs " + originalElement);
} }
} else if (diffElement instanceof Reshuffled) { } else if (diffElement instanceof Reshuffled) {


Expand All @@ -763,7 +763,7 @@ && matching(ne, pe)) {
} }


// We now find out which Node Text elements corresponds to the elements in the original CSM // We now find out which Node Text elements corresponds to the elements in the original CSM
List<Integer> nodeTextIndexOfPreviousElements = findIndexOfCorrespondingNodeTextElement(elementsFromPreviousOrder.getElements(), nodeText, givenIndex, node); List<Integer> nodeTextIndexOfPreviousElements = findIndexOfCorrespondingNodeTextElement(elementsFromPreviousOrder.getElements(), nodeText, originalIndex, node);


Map<Integer, Integer> nodeTextIndexToPreviousCSMIndex = new HashMap<>(); Map<Integer, Integer> nodeTextIndexToPreviousCSMIndex = new HashMap<>();
for (int i=0;i<nodeTextIndexOfPreviousElements.size();i++) { for (int i=0;i<nodeTextIndexOfPreviousElements.size();i++) {
Expand Down Expand Up @@ -810,7 +810,7 @@ && matching(ne, pe)) {
this.getElements().remove(diffIndex); this.getElements().remove(diffIndex);
int diffElIterator = diffIndex; int diffElIterator = diffIndex;
if (lastNodeTextIndex != -1) { if (lastNodeTextIndex != -1) {
for (int ntIndex = givenIndex; ntIndex<=lastNodeTextIndex; ntIndex++) { for (int ntIndex = originalIndex; ntIndex<=lastNodeTextIndex; ntIndex++) {


if (nodeTextIndexToPreviousCSMIndex.containsKey(ntIndex)) { if (nodeTextIndexToPreviousCSMIndex.containsKey(ntIndex)) {
int indexOfOriginalCSMElement = nodeTextIndexToPreviousCSMIndex.get(ntIndex); int indexOfOriginalCSMElement = nodeTextIndexToPreviousCSMIndex.get(ntIndex);
Expand Down Expand Up @@ -838,11 +838,11 @@ && matching(ne, pe)) {
diffElements.add(diffElIterator++, new Added(elementToAdd)); diffElements.add(diffElIterator++, new Added(elementToAdd));
} }
} else { } else {
throw new UnsupportedOperationException("" + diffElement + " vs " + givenElement); throw new UnsupportedOperationException("" + diffElement + " vs " + originalElement);
} }
} }
} }
} while (diffIndex < diffElements.size() || givenIndex < givenElements.size()); } while (diffIndex < diffElements.size() || originalIndex < originalElements.size());
} }


private List<Integer> findIndexOfCorrespondingNodeTextElement(List<CsmElement> elements, NodeText nodeText, int startIndex, Node node) { private List<Integer> findIndexOfCorrespondingNodeTextElement(List<CsmElement> elements, NodeText nodeText, int startIndex, Node node) {
Expand Down

0 comments on commit 2ae6f77

Please sign in to comment.