Skip to content

Commit

Permalink
Adjust printing of origin spaces when comment has been missed
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse committed Dec 15, 2020
1 parent d33eb5f commit 4738d12
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class AbstractSourceFragmentPrinter implements SourceFragmentPrinter {
//If next element is new, then run collected separator actions to print DJPP separators
protected final List<Runnable> separatorActions = new ArrayList<>();

private List<Integer> commentFragmentsToBePrinted = new ArrayList<>();

protected AbstractSourceFragmentPrinter(MutableTokenWriter mutableTokenWriter, ChangeResolver changeResolver, List<SourceFragment> childFragments) {
this.mutableTokenWriter = mutableTokenWriter;
this.changeResolver = changeResolver;
Expand Down Expand Up @@ -101,6 +103,10 @@ public int update(PrinterEvent event) {
//note: DJPP sends comments in wrong order/wrong place.
//so skip printing of this comment
//comment will be printed at place where it belongs to - together with spaces

// sometimes when the element directly succeeding the comment is deleted, the comment is lost,
// and so we must keep track of all comments to be printed
commentFragmentsToBePrinted.add(fragmentIndex);
return -1;
} else if (event.getRole() == CtRole.DECLARED_IMPORT && fragmentIndex == 0) {
// this is the first pre-existing import statement, and so we must print all newline
Expand Down Expand Up @@ -176,10 +182,13 @@ protected ModificationStatus isFragmentModified(SourceFragment fragment) {
* @param toIndex index of first not processed fragment.
*/
protected void printOriginSpacesUntilFragmentIndex(int fromIndex, int toIndex) {
int adjustedFromIndex = Math.min(commentFragmentsToBePrinted.isEmpty() ?
fromIndex : commentFragmentsToBePrinted.get(0), fromIndex);

//print all not yet printed comments which still exist in parent
boolean canPrintSpace = true;
boolean skipSpaceAfterDeletedElement = false;
for (int i = fromIndex; i < toIndex; i++) {
for (int i = adjustedFromIndex; i < toIndex; i++) {
SourceFragment fragment = childFragments.get(i);
if (fragment instanceof ElementSourceFragment) {
ElementSourceFragment sourceFragment = (ElementSourceFragment) fragment;
Expand Down Expand Up @@ -220,6 +229,7 @@ protected void printOriginSpacesUntilFragmentIndex(int fromIndex, int toIndex) {
}
}
separatorActions.clear();
commentFragmentsToBePrinted.clear();
}

private boolean childAtIdxIsModifiedCollectionSourceFragment(int idx) {
Expand Down Expand Up @@ -327,6 +337,16 @@ private int getLastNonSpaceNonCommentBefore(int index) {
return 0;
}

private boolean elementExists(SourceFragment fragment) {
if (fragment instanceof ElementSourceFragment) {
return changeResolver.isElementExists(((ElementSourceFragment) fragment).getElement());
} else if (fragment instanceof CollectionSourceFragment) {
return ((CollectionSourceFragment) fragment).getItems().stream().anyMatch(this::elementExists);
} else {
return true;
}
}

@Override
public void onPush() {
}
Expand Down

0 comments on commit 4738d12

Please sign in to comment.