Skip to content

Commit

Permalink
refactor: better API for creating ElementSourceFragment from a CtElem…
Browse files Browse the repository at this point in the history
…ent (#3405)
  • Loading branch information
monperrus committed Jun 11, 2020
1 parent 7a783d8 commit df91513
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ public ElementSourceFragment getOriginalSourceFragment() {
if (declaredTypeReferences.isEmpty()) {
throw new SpoonException("Root source fragment of compilation unit of package is not supported");
}
rootFragment = new ElementSourceFragment(this, null);
rootFragment.addTreeOfSourceFragmentsOfElement(this);
rootFragment = ElementSourceFragment.createSourceFragmentsFrom(this);
}
return rootFragment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,13 @@ private boolean isFromSameSource(SourcePosition position) {
}

/**
* Builds tree of {@link SourcePosition}s of `element` and all it's children
* Builds a tree of source fragments for `element` and all its children, based on the source positions of each element
* @param element the root element of the tree
*/
public void addTreeOfSourceFragmentsOfElement(CtElement element) {
public static ElementSourceFragment createSourceFragmentsFrom(CtElement element) {
ElementSourceFragment rootFragment = new ElementSourceFragment(element, null);
Deque<ElementSourceFragment> parents = new ArrayDeque<>();
parents.push(this);
parents.push(rootFragment);
/*
* scan all children of `element` and build tree of SourceFragments
* Note: we cannot skip implicit elements,
Expand Down Expand Up @@ -228,6 +229,7 @@ protected void exit(CtElement e) {
}
.setVisitCompilationUnitContent(true)
.scan(element.getRoleInParent(), element);
return rootFragment;
}
/**
* @param parentFragment the parent {@link ElementSourceFragment}, which will receive {@link ElementSourceFragment} made for `otherElement`
Expand Down

0 comments on commit df91513

Please sign in to comment.