-
Notifications
You must be signed in to change notification settings - Fork 171
adds support for Custom Token Scanner Symbols #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
40674ed
to
14086a6
Compare
even after running java-prettier on my end travis seems to be failing for the same reason of unformatted code. 🤦♂️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, @gabru-md!
There's definitely some formatting changes here that I can't explain. When I build locally, it doesn't change anything. Could you revert all the changes that are just formatting-related? That will make this easier to review.
Thanks again!
@@ -51,7 +51,10 @@ public OutputNode render(JinjavaInterpreter interpreter) { | |||
if (interpreter.getConfig().isNestedInterpretationEnabled()) { | |||
if ( | |||
!StringUtils.equals(result, master.getImage()) && | |||
(StringUtils.contains(result, "{{") || StringUtils.contains(result, "{%")) | |||
( | |||
StringUtils.contains(result, "{{") || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this should use the scanner symbols as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the changes. Although I'm not quite sure to if the {{
is to be replaced by
TOKEN_PREFIX_CHAR + TOKEN_EXPR_START_CHAR
or is it just
TOKEN_EXPR_START_CHAR + TOKEN_EXPR_START_CHAR
.
Please review it once again.
Cheers!
) return tag((TagToken) token); else if ( | ||
token.getType() == symbols.TOKEN_NOTE() | ||
) { | ||
if (!token.getImage().endsWith("#}")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use TOKEN_NOTE_CHAR
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
char TOKEN_TRIM_CHAR = '-'; | ||
|
||
@Override | ||
public char TOKEN_PREFIX_CHAR() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be getTokenPrefix()
. Same for the others below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes made!
startPosition | ||
); else if ( | ||
tokenKind == symbols.TOKEN_EXPR_START() | ||
) return new ExpressionToken(image, lineNumber, startPosition); else if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if
should be on a new line
int lineNumber, | ||
int startPosition | ||
) { | ||
if (tokenKind == symbols.TOKEN_FIXED()) return new TextToken( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please always wrap bodies of if statements in { }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
int TOKEN_TRIM = TOKEN_TRIM_CHAR; | ||
public abstract class TokenScannerSymbols { | ||
|
||
public abstract char TOKEN_PREFIX_CHAR(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment as before
96ec8ef
to
10bdb16
Compare
while debugging the code after making the changes, I noticed something weird here. this.expr = WhitespaceUtils.unwrap(image, "{{", "}}"); // inside the unwrap function
return s.substring(start + prefix.length(), end - suffix.length() + 1); the unwrap function does not care about the prefix and the suffix being passed into the function. The only purpose it serves is when the function calculates the length of the prefix and the suffix to trim the image and return the expression. Should there not be a validation inside the I was just wondering if it should be added as well? |
@gabru-md, could you run |
@boulter, I executed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good. I have a few suggestions for changes.
@@ -48,10 +49,23 @@ public OutputNode render(JinjavaInterpreter interpreter) { | |||
|
|||
String result = Objects.toString(var, ""); | |||
|
|||
TokenScannerSymbols symbols = interpreter.getConfig().getTokenScannerSymbols(); | |||
String expressionBegins = new StringBuilder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you create these once and make it accessible from the TokenScannerSymbols
class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't quite get what you're trying to say. Can you please explain a little bit so that I can make the changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Add getExpressionStart()
and getExpressionEnd()
methods to TokenScannerSymbols
and ensure they're only built once, rather than every time a node is rendered.
} else if (token.getType() == symbols.getTokenTag()) { | ||
return tag((TagToken) token); | ||
} else if (token.getType() == symbols.getTokenNote()) { | ||
String commentClosed = new StringBuilder() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can also be defined once.
char TOKEN_TRIM_CHAR = '-'; | ||
|
||
@Override | ||
public char getTokenPrefixChar() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all of these start with getToken
so the Token
part is redundant. Can you remove that part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay. I will remove that !
@boulter, I cannot quite seem to figure out why this behavior is happening. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting there!
I ran this locally and I had a lot of null pointer exceptions, so I think there's some fixes needed there.
I'm not sure about eclipse as I use IntelliJ. In any case, I would use the command line build as the source of truth.
Thanks!
String expressionBegins = symbols.getExpressionStart(); | ||
String expressionWithTag = symbols.getExpressionStartWithTag(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary variables as they are only used once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stringBuilder | ||
.append(TokenScannerSymbols.TOKEN_EXPR_START_CHAR) | ||
.append(TokenScannerSymbols.TOKEN_TAG_CHAR); | ||
stringBuilder.append(symbols.getExprStartChar()).append(symbols.getTagChar()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a method to construct this string once as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. I might have missed this out in the last commit.
I will add this to the changes as well.
.append(TokenScannerSymbols.TOKEN_TAG_CHAR) | ||
.append(TokenScannerSymbols.TOKEN_EXPR_END_CHAR); | ||
|
||
stringBuilder.append(symbols.getTagChar()).append(symbols.getExprEndChar()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
import com.hubspot.jinjava.interpret.TemplateSyntaxException; | ||
import com.hubspot.jinjava.util.WhitespaceUtils; | ||
|
||
public class TagToken extends Token { | ||
private static final long serialVersionUID = -4927751270481832992L; | ||
private final int TOKEN_TAG; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private final int TOKEN_TAG; | |
private final int tokenTag; |
only static variables should be in upper case.
|
||
private String tagName; | ||
private String rawTagName; | ||
private String helpers; | ||
|
||
public TagToken(String image, int lineNumber, int startPosition) { | ||
super(image, lineNumber, startPosition); | ||
TOKEN_TAG = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TOKEN_TAG = | |
tokenTag = |
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class TextToken extends Token { | ||
private static final long serialVersionUID = -6168990984496468543L; | ||
private final int TOKEN_FIXED; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private final int TOKEN_FIXED; | |
private final int tokenFixed; |
|
||
public TextToken(String image, int lineNumber, int startPosition) { | ||
super(image, lineNumber, startPosition); | ||
TOKEN_FIXED = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TOKEN_FIXED = | |
tokenFixed = |
return getTrimChar(); | ||
} | ||
|
||
public String getExpressionStart() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would good to fill this out with all the other starting and closing sequences that we'll eventually need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabru-md are all these in there now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, all the required combinations of sequences are in here right now.
|
||
public abstract char getTrimChar(); | ||
|
||
public int getPrefix() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are all these methods that just call abstract methods needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are places when an integer is being required and there are separate places where a character is required during parsing the template. Also, it will be better if the user is allowed to fill in the functions which return char
and not the ASCII value instead. Somebody who wants custom tokens will only have to fill in the tokens in form of characters and do not need to write down their ASCII values.
Also, I could've used those abstract methods instead since they can be cast to integers as well but the problem lies with the fact that when I was writing the if..else
conditions then it was becoming a lot unclear since the casting had to be done during the comparison. Therefore I thought It'd be better to not mess up the code readability a lot and simply have users fill out the functions which return char
and then return their int
value using the getPrefix
functions instead. I believe it was a better way to make changes.
I hope my explanation is fine. If you feel that it is not okay then I'd try and make the changes.
} | ||
|
||
@Test | ||
public void testsThatCustomTokensDoesNotFail() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make these rspec-style? itRendersWithCustomTokens
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure.
I believe that the command line builds should not fail if the same isn't failing for me in Eclipse. But since they are then I will work on it and try to get to the depth of the problem and commit a fix as soon as I am done with it. Since this does not seem to be a high-priority feature then I wish I can take the time I want to get it all fixed. I will perhaps need some more reviews from your end though and will tag you once changes seem fine to me. |
@boulter, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just 2 more loose ends
String expressionBegins = symbols.getExpressionStart(); | ||
String expressionWithTag = symbols.getExpressionStartWithTag(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return getTrimChar(); | ||
} | ||
|
||
public String getExpressionStart() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gabru-md are all these in there now?
@gabru-md |
This pull request aims at implementing the Custom Token Symbols class for Jinjava. The goal is to allow the user to specify the tokens that he/she wants to use. It can be extending using the TokenScannerSymbols abstract class and then filling the abstract methods.
A few lines inside the tree package have been changed from
switch-case
toif-else
sinceswitch-case
allows only constant expressions to be used with them. The changes to the tests which were failing initially due to the change have also been made and it should work fine now. Furthermore a JUnit test file has also been added in support of the changes made.related issue: #402
@KidsDontPlay perhaps this solves your problem.
cheers!