Skip to content

Commit

Permalink
issue124: fix removeTextBetween
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Feb 23, 2017
1 parent 8f20ebc commit a87e932
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
Expand Up @@ -42,4 +42,28 @@ String expand() {
Node getChild() {
return child;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ChildTextElement that = (ChildTextElement) o;

if (!lexicalPreservingPrinter.equals(that.lexicalPreservingPrinter)) return false;
return child.equals(that.child);

}

@Override
public int hashCode() {
int result = lexicalPreservingPrinter.hashCode();
result = 31 * result + child.hashCode();
return result;
}

@Override
public String toString() {
return "ChildTextElement{" + child + '}';
}
}
Expand Up @@ -111,27 +111,27 @@ void addToken(int index, Separator separator) {
* @param child
*/
void removeTextBetween(int tokenKind, Node child) {
int lastTokenFound = -1;
int endDeletion = -1;
int startDeletion = -1;
for (int i=0; i<elements.size(); i++) {
TextElement element = elements.get(i);
if (element instanceof ChildTextElement) {
ChildTextElement childNodeTextElement = (ChildTextElement)element;
if (childNodeTextElement.getChild() == child) {
if (lastTokenFound == -1) {
throw new IllegalArgumentException();
}
while (i > lastTokenFound) {
elements.remove(--i);
}
return;
startDeletion = i;
}
} else if (element instanceof TokenTextElement){
TokenTextElement tokenTextElement = (TokenTextElement)element;
if (tokenTextElement.getTokenKind() == tokenKind) {
lastTokenFound = i;
if (startDeletion != -1 && tokenTextElement.getTokenKind() == tokenKind) {
endDeletion = i;
break;
}
}
}
int i = endDeletion;
while (i >= startDeletion) {
elements.remove(i--);
}
}

void removeTextBetween(Node child, int tokenKind, boolean removeSpaceImmediatelyAfter) {
Expand Down
Expand Up @@ -51,4 +51,28 @@ public int getTokenKind() {
return tokenKind;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

TokenTextElement that = (TokenTextElement) o;

if (tokenKind != that.tokenKind) return false;
return text.equals(that.text);

}

@Override
public int hashCode() {
int result = tokenKind;
result = 31 * result + text.hashCode();
return result;
}

@Override
public String toString() {
return "TokenTextElement(" + tokenKind +
") {" + text + '}';
}
}
Expand Up @@ -18,6 +18,10 @@

public class LexicalPreservingPrinterTest extends AbstractLexicalPreservingTest {

//
// Tests on TextNode definition
//

@Test
public void checkNodeTextCreatedForSimplestClass() {
considerCode("class A {}");
Expand Down Expand Up @@ -68,6 +72,10 @@ public void checkNodeTextCreatedForMethod() {
nodeText.getElements().stream().map(TextElement::expand).collect(Collectors.toList()));
}

//
// Tests on printing
//

@Test
public void printASuperSimpleCUWithoutChanges() {
String code = "class A {}";
Expand Down

0 comments on commit a87e932

Please sign in to comment.