Fix ScriptColorizer for color codes
SteamWarCI Build successful Details

This commit is contained in:
yoyosource 2023-11-17 16:49:08 +01:00
Ursprung e8d779914a
Commit 2dc0f73141
1 geänderte Dateien mit 31 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,8 @@
package de.steamwar.advancedscripts.lexer;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -56,53 +58,61 @@ public class ScriptColorizer {
switch (c) {
case '"' -> {
if (inString) {
currentToken.append(c);
if (doubleQuoteString) {
tokens.add(new Token(currentToken.toString(), TokenTypeColors.STRING));
tokens.add(toStringToken(currentToken.toString()));
currentToken = new StringBuilder();
tokens.add(new Token(c + "", TokenTypeColors.STRING));
inString = false;
doubleQuoteString = false;
}
} else {
inString = true;
doubleQuoteString = true;
if (currentToken.length() > 0) {
if (!currentToken.isEmpty()) {
tokens.add(toToken(currentToken.toString()));
currentToken = new StringBuilder();
}
currentToken.append(c);
tokens.add(new Token(c + "", TokenTypeColors.STRING));
}
}
case '\'' -> {
if (inString) {
currentToken.append(c);
if (!doubleQuoteString) {
tokens.add(new Token(currentToken.toString(), TokenTypeColors.STRING));
tokens.add(toStringToken(currentToken.toString()));
currentToken = new StringBuilder();
tokens.add(new Token(c + "", TokenTypeColors.STRING));
inString = false;
}
} else {
inString = true;
doubleQuoteString = false;
if (currentToken.length() > 0) {
if (!currentToken.isEmpty()) {
tokens.add(toToken(currentToken.toString()));
currentToken = new StringBuilder();
}
currentToken.append(c);
tokens.add(new Token(c + "", TokenTypeColors.STRING));
}
}
case '-', ';', '(', ')', ',', ' ', '{', '\t' -> {
if (inString) {
currentToken.append(c);
} else {
if(currentToken.length() > 0) {
if (!currentToken.isEmpty()) {
tokens.add(toToken(currentToken.toString()));
}
tokens.add(new Token(String.valueOf(c), TokenTypeColors.OTHER));
currentToken = new StringBuilder();
}
}
default -> currentToken.append(c);
default -> {
if (inString && c == '&') {
if (!currentToken.isEmpty()) {
tokens.add(toStringToken(currentToken.toString()));
}
currentToken = new StringBuilder();
}
currentToken.append(c);
}
}
}
@ -113,6 +123,17 @@ public class ScriptColorizer {
return tokens;
}
private static Token toStringToken(String text) {
if (text.length() > 1 && text.charAt(0) == '&') {
Formatting formatting = Formatting.byCode(text.charAt(1));
if (formatting == null || !formatting.isColor()) {
return new Token(text, TokenTypeColors.STRING);
}
return new Token(text, formatting.getColorValue());
}
return new Token(text, TokenTypeColors.STRING);
}
private static Token toToken(String text) {
if (text.length() > 0) {
if (KEYWORDS.contains(text)) {