From 2dc0f7314158c9fc4e2dbcd7a44327ecced81902 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 17 Nov 2023 16:49:08 +0100 Subject: [PATCH] Fix ScriptColorizer for color codes --- .../lexer/ScriptColorizer.java | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java b/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java index dc78d4d..528d89f 100644 --- a/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java +++ b/src/main/java/de/steamwar/advancedscripts/lexer/ScriptColorizer.java @@ -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)) {