-
Notifications
You must be signed in to change notification settings - Fork 237
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
Question: Multiple styles in code area #62
Comments
this is the css .gold { |
You are probably getting an error because you didn't reset You need a little work with regular expressions: private static final String[] GOLD = new String[] {
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "double", "else",
"enum", "extends", "final", "finally", "float",
"goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile",};
private static final String[] GREEN = new String[] {
"for", "while", "do"
};
private static final String GOLD_PATTERN = "\\b(" + String.join("|", GOLD) + ")\\b";
private static final String GREEN_PATTERN = "\\b(" + String.join("|", GREEN) + ")\\b";
private static final Pattern PATTERN = Pattern.compile(
"(?<GOLD>" + GOLD_PATTERN + ")" +
"|" +
"(?<GREEN>" + GREEN_PATTERN + ")");
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder
= new StyleSpansBuilder<>();
while(matcher.find()) {
String styleClass = matcher.group("GOLD") != null ? "gold" : "green";
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
} |
Thanks! that did it. I will take it from here. |
Tomas, I am really sorry my questions might be really simple, but I can't figure out how to set multiple colors. How do you do that? how do I make this function sort multiple colors?
|
You mean more than two colors? Any number of colors? |
I mean a finite set if colours. In my case it's probably 5 or 6.
|
If you have a fixed set of colors, then extending my sample is really simple. String styleClass =
matcher.group("GOLD") != null ? "gold" :
matcher.group("GREEN") != null ? "green" :
matcher.group("BLUE") != null ? "brown" :
matcher.group("RED") != null ? "red" :
"purple"; |
The last colour, purple, is basically the colour that gets applied when no
|
No, everything inside the String styleClass;
if(matcher.group("GOLD") != null) {
styleClass = "gold";
} else if(matcher.group("GREEN") != null) {
styleClass = "green";
} else if(matcher.group("BLUE") != null) {
styleClass = "blue";
} else if(matcher.group("RED") != null) {
styleClass = "red";
} else if(matcher.group("PURPLE") != null) {
styleClass = "purple";
} else {
throw new AssertionError("Unreachable code");
} |
Thanks! I am still in the verbose code writing skill level. :)
|
From above example i tried modifying my CodeArea code, but it didn't worked for me. Here is the url to my code https://github.com/deepsidhu1313/java-example/ . Am i doing something wrong ?? |
Brackets, semicolons, etc. are not words for the purposes of regular expressions, so you shouldn't delimit them by word boundaries ( private static final Pattern SEMICOL_PATTERN = Pattern.compile("(" + String.join("|", SEMICOL) + ")"); i.e. omitting |
Also, I noticed you use private static final String SEMICOL_PATTERN = "(" + String.join("|", SEMICOL) + ")"; |
I made few changes in code, now it doesn't return any error but color of other elements except keyword is not changing. updated version of my code is here https://github.com/deepsidhu1313/java-example/ |
Oh well, private static final String[] SEMICOL = new String[]{"\\;"};
private static final String[] RBRAC = new String[]{"\\(", "\\)"};
private static final String[] SBRAC = new String[]{"\\[", "\\]"};
private static final String[] OBRAC = new String[]{"\\{", "\\}"}; and report back whether it resolved your issue. |
Thanks a lot that solved the problem . :) Everything is working fine now. :) |
Good idea. I updated the JavaKeywords example to use multiple colors. I also added highlighting of string literals. |
Did you tested your latest example ?? i am getting stackoverflow error by implementing your new code given in JavaKeywords. |
Yes, I can run the JavaKeywords demo without any problem. What is the exact error? Do you get an error when running the JavaKeywords demo, or after incorporating the code into your program? |
Hi Tomas, I was looking at the code highlighting example. I have a question.
How do get multiple colors per keyword.
I did this:
but this of course is giving me an error at the copmuteHighlighting method. My understanding is that you build a collection of styleSpans based on the matches founds. so I just went and made another loop to look for words that match another pattern. What is the right way to get it to work? I must be extremely wrong somewhere :) Thanks!
The text was updated successfully, but these errors were encountered: