Skip to content

Commit

Permalink
combined logics to clean the line and added new unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThLeu committed Jun 7, 2018
1 parent 2d3f5e2 commit 4f9e0c6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
Expand Up @@ -307,6 +307,21 @@ public void printASimpleClassRemovingAField() {
" void foo(int p ) { return 'z' \t; }}", LexicalPreservingPrinter.print(c));
}

@Test
public void printASimpleClassRemovingAMethod() {
String code = "class /*a comment*/ A {\t\t" + EOL +
" int f;" + EOL + EOL + EOL +
" void foo(int p ) { return 'z' \t; }" + EOL +
" int g;}";
considerCode(code);

ClassOrInterfaceDeclaration c = cu.getClassByName("A").get();
c.getMembers().remove(1);
assertEquals("class /*a comment*/ A {\t\t" + EOL +
" int f;" + EOL + EOL + EOL +
" int g;}", LexicalPreservingPrinter.print(c));
}

@Test
public void printASimpleMethodAddingAParameterToAMethodWithZeroParameters() {
String code = "class A { void foo() {} }";
Expand Down
Expand Up @@ -73,14 +73,6 @@ private List<TextElement> indentationBlock() {
return res;
}

private int considerCleaningTheLine(NodeText nodeText, int nodeTextIndex) {
while (nodeTextIndex >=1 && nodeText.getElements().get(nodeTextIndex - 1).isSpaceOrTab()) {
nodeText.removeElement(nodeTextIndex - 1);
nodeTextIndex--;
}
return nodeTextIndex;
}

private boolean isAfterLBrace(NodeText nodeText, int nodeTextIndex) {
if (nodeTextIndex > 0 && nodeText.getElements().get(nodeTextIndex - 1).isToken(LBRACE)) {
return true;
Expand Down Expand Up @@ -384,17 +376,29 @@ private void applyRemovedDiffElement(RemovedGroup removedGroup, Removed removed,
throw new UnsupportedOperationException("removed " + removed.getElement() + " vs " + originalElement);
}

if (removedGroup.isACompleteLine() && removedGroup.getLastElement() == removed) {
cleanTheLineOfLeftOverSpace(removedGroup, removed);
}

private void cleanTheLineOfLeftOverSpace(RemovedGroup removedGroup, Removed removed) {
if (removedGroup.isACompleteLine() && removedGroup.getLastElement() == removed && !removedGroup.isProcessed()) {
Integer lastElementIndex = removedGroup.getLastElementIndex();
Optional<Integer> indentation = removedGroup.getIndentation();

if (originalIndex < originalElements.size() && originalElements.get(originalIndex).isNewline()) {
originalIndex = considerCleaningTheLine(nodeText, originalIndex);
} else if (!isReplaced(lastElementIndex) && indentation.isPresent()) {
for (int i = 0; i < indentation.get(); i++) {
nodeText.removeElement(originalIndex);
if (indentation.isPresent()) {
if (originalIndex < originalElements.size() && !originalElements.get(originalIndex).isSpaceOrTab()) {
for (int i = 0; i < indentation.get() && originalIndex >= 1; i++) {
nodeText.removeElement(originalIndex - 1);
originalIndex--;
}
} else if (!isReplaced(lastElementIndex)) {
for (int i = 0; i < indentation.get(); i++) {
nodeText.removeElement(originalIndex);
}
}
}

// Mark RemovedGroup as processed
removedGroup.processed();
}
}

Expand Down
Expand Up @@ -15,6 +15,8 @@ class RemovedGroup implements Iterable<Removed> {
private final Integer firstElementIndex;
private final List<Removed> removedList;

private boolean isProcessed = false;

private RemovedGroup(Integer firstElementIndex, List<Removed> removedList) {
if (firstElementIndex == null) {
throw new IllegalArgumentException("firstElementIndex should not be null");
Expand All @@ -39,6 +41,22 @@ public static RemovedGroup of(Integer firstElementIndex, List<Removed> removedLi
return new RemovedGroup(firstElementIndex, removedList);
}

/**
* Marks the RemovedGroup as processed which indicates that it should not be processed again
*/
void processed() {
isProcessed = true;
}

/**
* Returns whether the RemovedGroup was already processed and should not be processed again
*
* @return wheter the RemovedGroup was already processed
*/
boolean isProcessed() {
return isProcessed;
}

private List<Integer> getIndicesBeingRemoved() {
return IntStream.range(firstElementIndex, firstElementIndex + removedList.size())
.boxed()
Expand Down

0 comments on commit 4f9e0c6

Please sign in to comment.