Skip to content

Commit

Permalink
Let TokenTextElement hold an actual JavaToken
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Aug 12, 2017
1 parent c4e25c5 commit 1144d3e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
Expand Up @@ -323,4 +323,13 @@ public JavaToken findFirstToken() {
}
return current;
}

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


}
Expand Up @@ -21,57 +21,41 @@

package com.github.javaparser.printer.lexicalpreservation;

import com.github.javaparser.GeneratedJavaParserConstants;
import com.github.javaparser.JavaToken;
import com.github.javaparser.ast.Node;
import com.github.javaparser.TokenTypes;

import static com.github.javaparser.utils.Utils.EOL;
import com.github.javaparser.ast.Node;

class TokenTextElement extends TextElement {

private final int tokenKind;
private final String text;

public static TokenTextElement newLine() {
return new TokenTextElement(TokenTypes.eolTokenKind(), EOL);
}
private final JavaToken token;

TokenTextElement(JavaToken token) {
this(token.getKind(), token.getText());
this.token = token;
}

TokenTextElement(int tokenKind, String text) {
this.tokenKind = tokenKind;
this.text = text;
this(new JavaToken(tokenKind, text));
}

TokenTextElement(int tokenKind) {
String content = GeneratedJavaParserConstants.tokenImage[tokenKind];
if (content.startsWith("\"")) {
content = content.substring(1, content.length() - 1);
}
if (TokenTypes.isEndOfLineToken(tokenKind)) {
content = EOL;
} else if (TokenTypes.isWhitespace(tokenKind)) {
content = " ";
}
this.tokenKind = tokenKind;
this.text = content;
this(new JavaToken(tokenKind));
}

@Override
String expand() {
return text;
return token.getText();
}

// Visible for testing
String getText() {
return text;
return token.getText();
}

public int getTokenKind() {
return tokenKind;
return token.getKind();
}

public JavaToken getToken() {
return token;
}

@Override
Expand All @@ -81,27 +65,25 @@ public boolean equals(Object o) {

TokenTextElement that = (TokenTextElement) o;

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

}

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

@Override
public String toString() {
return "TokenTextElement(" + tokenKind +
") {" + text + '}';
return "TokenTextElement(" + token.getKind() +
") {" + getText() + '}';
}

@Override
boolean isToken(int tokenKind) {
return this.tokenKind == tokenKind;
return token.getKind() == tokenKind;
}

@Override
Expand All @@ -111,22 +93,22 @@ boolean isNode(Node node) {

@Override
public boolean isWhiteSpace() {
return TokenTypes.isWhitespace(tokenKind);
return TokenTypes.isWhitespace(token.getKind());
}

@Override
public boolean isSpaceOrTab() {
return TokenTypes.isSpaceOrTab(tokenKind);
return TokenTypes.isSpaceOrTab(token.getKind());
}

@Override
public boolean isComment() {
return TokenTypes.isComment(tokenKind);
return TokenTypes.isComment(token.getKind());
}

@Override
public boolean isNewline() {
return TokenTypes.isEndOfLineToken(tokenKind);
return TokenTypes.isEndOfLineToken(token.getKind());
}

@Override
Expand Down

0 comments on commit 1144d3e

Please sign in to comment.