Fix ScriptColorizer for color codes
SteamWarCI Build successful Details

Dieser Commit ist enthalten 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; package de.steamwar.advancedscripts.lexer;
import net.minecraft.util.Formatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -56,53 +58,61 @@ public class ScriptColorizer {
switch (c) { switch (c) {
case '"' -> { case '"' -> {
if (inString) { if (inString) {
currentToken.append(c);
if (doubleQuoteString) { if (doubleQuoteString) {
tokens.add(new Token(currentToken.toString(), TokenTypeColors.STRING)); tokens.add(toStringToken(currentToken.toString()));
currentToken = new StringBuilder(); currentToken = new StringBuilder();
tokens.add(new Token(c + "", TokenTypeColors.STRING));
inString = false; inString = false;
doubleQuoteString = false; doubleQuoteString = false;
} }
} else { } else {
inString = true; inString = true;
doubleQuoteString = true; doubleQuoteString = true;
if (currentToken.length() > 0) { if (!currentToken.isEmpty()) {
tokens.add(toToken(currentToken.toString())); tokens.add(toToken(currentToken.toString()));
currentToken = new StringBuilder(); currentToken = new StringBuilder();
} }
currentToken.append(c); tokens.add(new Token(c + "", TokenTypeColors.STRING));
} }
} }
case '\'' -> { case '\'' -> {
if (inString) { if (inString) {
currentToken.append(c);
if (!doubleQuoteString) { if (!doubleQuoteString) {
tokens.add(new Token(currentToken.toString(), TokenTypeColors.STRING)); tokens.add(toStringToken(currentToken.toString()));
currentToken = new StringBuilder(); currentToken = new StringBuilder();
tokens.add(new Token(c + "", TokenTypeColors.STRING));
inString = false; inString = false;
} }
} else { } else {
inString = true; inString = true;
doubleQuoteString = false; doubleQuoteString = false;
if (currentToken.length() > 0) { if (!currentToken.isEmpty()) {
tokens.add(toToken(currentToken.toString())); tokens.add(toToken(currentToken.toString()));
currentToken = new StringBuilder(); currentToken = new StringBuilder();
} }
currentToken.append(c); tokens.add(new Token(c + "", TokenTypeColors.STRING));
} }
} }
case '-', ';', '(', ')', ',', ' ', '{', '\t' -> { case '-', ';', '(', ')', ',', ' ', '{', '\t' -> {
if (inString) { if (inString) {
currentToken.append(c); currentToken.append(c);
} else { } else {
if(currentToken.length() > 0) { if (!currentToken.isEmpty()) {
tokens.add(toToken(currentToken.toString())); tokens.add(toToken(currentToken.toString()));
} }
tokens.add(new Token(String.valueOf(c), TokenTypeColors.OTHER)); tokens.add(new Token(String.valueOf(c), TokenTypeColors.OTHER));
currentToken = new StringBuilder(); 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; 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) { private static Token toToken(String text) {
if (text.length() > 0) { if (text.length() > 0) {
if (KEYWORDS.contains(text)) { if (KEYWORDS.contains(text)) {