From c06e24c2f9ce326d5fe658ea42262876e4e6c5e9 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Tue, 16 Jun 2020 17:20:15 +0200 Subject: [PATCH] refactor: further simplify sniper mode (#3419) --- .../internal/ElementSourceFragment.java | 48 ++----------------- .../test/position/TestSourceFragment.java | 1 - 2 files changed, 5 insertions(+), 44 deletions(-) diff --git a/src/main/java/spoon/support/sniper/internal/ElementSourceFragment.java b/src/main/java/spoon/support/sniper/internal/ElementSourceFragment.java index af089787aa6..2629119a157 100644 --- a/src/main/java/spoon/support/sniper/internal/ElementSourceFragment.java +++ b/src/main/java/spoon/support/sniper/internal/ElementSourceFragment.java @@ -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; @@ -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 diff --git a/src/test/java/spoon/test/position/TestSourceFragment.java b/src/test/java/spoon/test/position/TestSourceFragment.java index a1f54a2d924..d2550967484 100644 --- a/src/test/java/spoon/test/position/TestSourceFragment.java +++ b/src/test/java/spoon/test/position/TestSourceFragment.java @@ -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()); }