From f0a7a2d89ded28e8b12a380fff5732db8c9889ac Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Mon, 28 Mar 2022 11:04:39 +0200 Subject: [PATCH] Replace c-style comments pattern with 2 patterns that can compile without error on Android 5.1 and below --- .../codeviewlibrary/syntax/GoLanguage.java | 24 ++++++++----------- .../codeviewlibrary/syntax/JavaLanguage.java | 16 ++++++++----- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java index 10e6ae3..71c8768 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java @@ -30,8 +30,8 @@ public class GoLanguage { private static final Pattern PATTERN_CHAR = Pattern.compile("'[a-zA-Z]'"); private static final Pattern PATTERN_STRING = Pattern.compile("\".*\""); private static final Pattern PATTERN_HEX = Pattern.compile("0x[0-9a-fA-F]+"); - private static final Pattern PATTERN_TODO_COMMENT = Pattern.compile("//TODO[^\n]*"); - private static final Pattern PATTERN_COMMENT = Pattern.compile("//(?!TODO )[^\\n]*" + "|" + "/\\*(.|\\R)*?\\*/"); + private static final Pattern PATTERN_SINGLE_LINE_COMMENT = Pattern.compile("//[^\\n]*"); + private static final Pattern PATTERN_MULTI_LINE_COMMENT = Pattern.compile("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/"); private static final Pattern PATTERN_ATTRIBUTE = Pattern.compile("\\.[a-zA-Z0-9_]+"); private static final Pattern PATTERN_OPERATION =Pattern.compile( ":|==|>|<|!=|>=|<=|->|=|>|<|%|-|-=|%=|\\+|\\-|\\-=|\\+=|\\^|\\&|\\|::|\\?|\\*"); @@ -51,15 +51,14 @@ public static void applyMonokaiTheme(Context context, CodeView codeView) { codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.monokia_pro_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.monokia_pro_pink)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.monokia_pro_white)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.monokia_pro_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.monokia_pro_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.monokia_pro_grey)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.monokia_pro_sky)); codeView.addSyntaxPattern(PATTERN_OPERATION, resources.getColor(R.color.monokia_pro_pink)); //Default Color codeView.setTextColor(resources.getColor(R.color.monokia_pro_white)); - codeView.addSyntaxPattern(PATTERN_TODO_COMMENT, resources.getColor(R.color.gold)); - codeView.reHighlightSyntax(); } @@ -79,15 +78,14 @@ public static void applyNoctisWhiteTheme(Context context, CodeView codeView) { codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.noctis_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.noctis_pink)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.noctis_dark_blue)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.noctis_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.noctis_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.noctis_grey)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.noctis_blue)); codeView.addSyntaxPattern(PATTERN_OPERATION, resources.getColor(R.color.monokia_pro_pink)); //Default Color codeView.setTextColor(resources.getColor(R.color.noctis_orange)); - codeView.addSyntaxPattern(PATTERN_TODO_COMMENT, resources.getColor(R.color.gold)); - codeView.reHighlightSyntax(); } @@ -107,15 +105,14 @@ public static void applyFiveColorsDarkTheme(Context context, CodeView codeView) codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.five_dark_white)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.five_dark_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.five_dark_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.five_dark_grey)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.five_dark_blue)); codeView.addSyntaxPattern(PATTERN_OPERATION, resources.getColor(R.color.five_dark_purple)); //Default Color codeView.setTextColor(resources.getColor(R.color.five_dark_white)); - codeView.addSyntaxPattern(PATTERN_TODO_COMMENT, resources.getColor(R.color.gold)); - codeView.reHighlightSyntax(); } @@ -135,15 +132,14 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) { codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.orange_box_orange1)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.orange_box_grey)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.orange_box_dark_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.orange_box_dark_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.orange_box_dark_grey)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.orange_box_orange3)); codeView.addSyntaxPattern(PATTERN_OPERATION, resources.getColor(R.color.gold)); //Default Color codeView.setTextColor(resources.getColor(R.color.five_dark_white)); - codeView.addSyntaxPattern(PATTERN_TODO_COMMENT, resources.getColor(R.color.gold)); - codeView.reHighlightSyntax(); } diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java index 37c956e..df59cd8 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java @@ -26,7 +26,8 @@ public class JavaLanguage { "|synchronized|this|throw|transient|try|void|volatile|while)\\b"); private static final Pattern PATTERN_BUILTINS = Pattern.compile("[,:;[->]{}()]"); - private static final Pattern PATTERN_COMMENT = Pattern.compile("//(?!TODO )[^\\n]*" + "|" + "/\\*(.|\\R)*?\\*/"); + private static final Pattern PATTERN_SINGLE_LINE_COMMENT = Pattern.compile("//[^\\n]*"); + private static final Pattern PATTERN_MULTI_LINE_COMMENT = Pattern.compile("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/"); private static final Pattern PATTERN_ATTRIBUTE = Pattern.compile("\\.[a-zA-Z0-9_]+"); private static final Pattern PATTERN_OPERATION =Pattern.compile( ":|==|>|<|!=|>=|<=|->|=|>|<|%|-|-=|%=|\\+|\\-|\\-=|\\+=|\\^|\\&|\\|::|\\?|\\*"); private static final Pattern PATTERN_GENERIC = Pattern.compile("<[a-zA-Z0-9,<>]+>"); @@ -53,7 +54,8 @@ public static void applyMonokaiTheme(Context context, CodeView codeView) { codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.monokia_pro_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.monokia_pro_pink)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.monokia_pro_white)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.monokia_pro_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.monokia_pro_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.monokia_pro_grey)); codeView.addSyntaxPattern(PATTERN_ANNOTATION, resources.getColor(R.color.monokia_pro_pink)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.monokia_pro_sky)); codeView.addSyntaxPattern(PATTERN_GENERIC, resources.getColor(R.color.monokia_pro_pink)); @@ -82,8 +84,8 @@ public static void applyNoctisWhiteTheme(Context context, CodeView codeView) { codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.noctis_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.noctis_pink)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.noctis_dark_blue)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.noctis_grey)); - + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.noctis_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.noctis_grey)); codeView.addSyntaxPattern(PATTERN_ANNOTATION, resources.getColor(R.color.monokia_pro_pink)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.noctis_blue)); codeView.addSyntaxPattern(PATTERN_GENERIC, resources.getColor(R.color.monokia_pro_pink)); @@ -113,7 +115,8 @@ public static void applyFiveColorsDarkTheme(Context context, CodeView codeView) codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.five_dark_white)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.five_dark_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.five_dark_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.five_dark_grey)); codeView.addSyntaxPattern(PATTERN_ANNOTATION, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.five_dark_blue)); codeView.addSyntaxPattern(PATTERN_GENERIC, resources.getColor(R.color.five_dark_purple)); @@ -143,7 +146,8 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) { codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.five_dark_purple)); codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.orange_box_orange1)); codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.orange_box_grey)); - codeView.addSyntaxPattern(PATTERN_COMMENT, resources.getColor(R.color.orange_box_dark_grey)); + codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.orange_box_dark_grey)); + codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.orange_box_dark_grey)); codeView.addSyntaxPattern(PATTERN_ANNOTATION, resources.getColor(R.color.orange_box_orange1)); codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.orange_box_orange3)); codeView.addSyntaxPattern(PATTERN_GENERIC, resources.getColor(R.color.orange_box_orange1));