Skip to content

Commit

Permalink
refactor: further simplify sniper mode (#3419)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored Jun 16, 2020
1 parent a9c4a11 commit c06e24c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,37 +238,8 @@ private ElementSourceFragment addChild(CtRole roleInParent, SourcePositionHolder
SourcePosition otherSourcePosition = otherElement.getPosition();
if (otherSourcePosition instanceof SourcePositionImpl && !(otherSourcePosition.getCompilationUnit() instanceof NoSourcePosition.NullCompilationUnit)) {
ElementSourceFragment otherFragment = new ElementSourceFragment(otherElement, this.getRoleHandler(roleInParent, otherElement));
if (this.getElement() instanceof CtCompilationUnit) {
addChild(otherFragment);
return otherFragment;
}

CMP cmp = this.compare(otherFragment);
if (cmp == CMP.OTHER_IS_CHILD) {
// core contract:
// the position of children fragments must be included inside the positions of the paernt fragment
this.addChild(otherFragment);
return otherFragment;
} else {
// one exception
// comment positions have the right to not be part of the parent fragment in some cases
if (otherElement instanceof CtComment) {
/*
* comments of elements are sometime not included in source position of element.
* because comments are ignored tokens for JDT, which computes start/end of elements
* Example:
*
* //a comment
* aStatement();
*
*/
//add this child into parent's source fragment and extend that parent source fragment
this.addChild(otherFragment);
return otherFragment;
} else {
throw new SpoonException("The SourcePosition of elements are not consistent\nparentFragment: " + this + "\notherFragment: " + otherFragment);
}
}
this.addChild(otherFragment);
return otherFragment;
}
//do not connect that undefined source position
return null;
Expand Down Expand Up @@ -314,23 +285,14 @@ public ElementSourceFragment add(ElementSourceFragment other) {
addChild(other);
return this;
case OTHER_IS_PARENT:
//other is parent of this, merge this and all siblings of `this` as children and siblings of `other`
other.merge(this);
// sometimes the scanning order is not the position order
// so we have to switch the fragments to have the first in first position
other.addChild(this);
return other;
}
throw new SpoonException("Unexpected compare result: " + cmp);
}

private void merge(ElementSourceFragment tobeMerged) {
while (tobeMerged != null) {
ElementSourceFragment nextTobeMerged = tobeMerged.getNextSibling();
//disconnect tobeMerged from nextSiblings before we add it. So it is added individually and not with wrong siblings too
tobeMerged.nextSibling = null;
add(tobeMerged);
tobeMerged = nextTobeMerged;
}
}

/**
* adds `fragment` as child fragment of this fragment. If child is located before or after this fragment,
* then start/end of this fragment is moved
Expand Down
1 change: 0 additions & 1 deletion src/test/java/spoon/test/position/TestSourceFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public void testSourceFragmentWrapChildrenAndSiblings() {
assertSame(childA, childWrapper.getFirstChild());
assertSame(child, childA.getNextSibling());
assertSame(childB, child.getFirstChild());
assertSame(childC, child.getNextSibling());
assertSame(childD, childC.getNextSibling());
}

Expand Down

0 comments on commit c06e24c

Please sign in to comment.