diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/MainActivity.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/MainActivity.java index ebd0737..546ef85 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/MainActivity.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/MainActivity.java @@ -17,26 +17,22 @@ import com.amrdeveloper.codeview.Code; import com.amrdeveloper.codeview.CodeView; -import com.amrdeveloper.codeview.Keyword; -import com.amrdeveloper.codeviewlibrary.syntax.Theme; -import com.amrdeveloper.codeviewlibrary.syntax.Language; -import com.amrdeveloper.codeviewlibrary.syntax.SyntaxManager; +import com.amrdeveloper.codeviewlibrary.syntax.ThemeName; +import com.amrdeveloper.codeviewlibrary.syntax.LanguageName; +import com.amrdeveloper.codeviewlibrary.syntax.LanguageManager; import com.google.android.material.bottomsheet.BottomSheetDialog; -import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; public class MainActivity extends AppCompatActivity { private CodeView codeView; - private SyntaxManager syntaxManager; + private LanguageManager languageManager; - private Language currentLanguage = Language.JAVA; - private Theme currentTheme = Theme.MONOKAI; + private LanguageName currentLanguage = LanguageName.JAVA; + private ThemeName currentTheme = ThemeName.MONOKAI; private final boolean useModernAutoCompleteAdapter = true; @@ -64,17 +60,9 @@ private void configCodeView() { codeView.setTabLength(4); codeView.setEnableAutoIndentation(true); - Set indentationStart = new HashSet<>(); - indentationStart.add('{'); - codeView.setIndentationStarts(indentationStart); - - Set indentationEnds = new HashSet<>(); - indentationEnds.add('}'); - codeView.setIndentationEnds(indentationEnds); - // Setup the language and theme with SyntaxManager helper class - syntaxManager = new SyntaxManager(this, codeView); - syntaxManager.applyTheme(currentLanguage,currentTheme); + languageManager = new LanguageManager(this, codeView); + languageManager.applyTheme(currentLanguage, currentTheme); // Setup auto pair complete final Map pairCompleteMap = new HashMap<>(); @@ -87,33 +75,24 @@ private void configCodeView() { codeView.setPairCompleteMap(pairCompleteMap); codeView.enablePairComplete(true); - // Setup the auto complete for the current language + // Setup the auto complete and auto indenting for the current language configLanguageAutoComplete(); + configLanguageAutoIndentation(); } private void configLanguageAutoComplete() { - final String[] languageKeywords; - switch (currentLanguage) { - case JAVA: - languageKeywords = getResources().getStringArray(R.array.java_keywords); - break; - case PYTHON: - languageKeywords = getResources().getStringArray(R.array.python_keywords); - break; - default: - languageKeywords = getResources().getStringArray(R.array.go_keywords); - break; - } - if (useModernAutoCompleteAdapter) { - final List codeList = new ArrayList<>(); - for (String keyword : languageKeywords) { - codeList.add(new Keyword(keyword, keyword)); - } - // Here you can add snippets to the codeList + // Load the code list (keywords and snippets) for the current language + List codeList = languageManager.getLanguageCodeList(currentLanguage); + + // Use CodeViewAdapter or custom one CustomCodeViewAdapter adapter = new CustomCodeViewAdapter(this, codeList); + + // Add the odeViewAdapter to the CodeView codeView.setAdapter(adapter); } else { + String[] languageKeywords = languageManager.getLanguageKeywords(currentLanguage); + // Custom list item xml layout final int layoutId = R.layout.list_item_suggestion; @@ -121,11 +100,16 @@ private void configLanguageAutoComplete() { final int viewId = R.id.suggestItemTextView; ArrayAdapter adapter = new ArrayAdapter<>(this, layoutId, viewId, languageKeywords); - // Add Custom Adapter to the CodeView + // Add the ArrayAdapter to the CodeView codeView.setAdapter(adapter); } } + private void configLanguageAutoIndentation() { + codeView.setIndentationStarts(languageManager.getLanguageIndentationStarts(currentLanguage)); + codeView.setIndentationEnds(languageManager.getLanguageIndentationEnds(currentLanguage)); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); @@ -146,26 +130,27 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { } private void changeTheEditorLanguage(int languageId) { - final Language oldLanguage = currentLanguage; - if (languageId == R.id.language_java) currentLanguage = Language.JAVA; - else if (languageId == R.id.language_python) currentLanguage = Language.PYTHON; - else if(languageId == R.id.language_go) currentLanguage = Language.GO_LANG; + final LanguageName oldLanguage = currentLanguage; + if (languageId == R.id.language_java) currentLanguage = LanguageName.JAVA; + else if (languageId == R.id.language_python) currentLanguage = LanguageName.PYTHON; + else if(languageId == R.id.language_go) currentLanguage = LanguageName.GO_LANG; if (currentLanguage != oldLanguage) { - syntaxManager.applyTheme(currentLanguage, currentTheme); + languageManager.applyTheme(currentLanguage, currentTheme); configLanguageAutoComplete(); + configLanguageAutoIndentation(); } } private void changeTheEditorTheme(int themeId) { - final Theme oldTheme = currentTheme; - if (themeId == R.id.theme_monokia) currentTheme = Theme.MONOKAI; - else if (themeId == R.id.theme_noctics) currentTheme = Theme.NOCTIS_WHITE; - else if(themeId == R.id.theme_five_color) currentTheme = Theme.FIVE_COLOR; - else if(themeId == R.id.theme_orange_box) currentTheme = Theme.ORANGE_BOX; + final ThemeName oldTheme = currentTheme; + if (themeId == R.id.theme_monokia) currentTheme = ThemeName.MONOKAI; + else if (themeId == R.id.theme_noctics) currentTheme = ThemeName.NOCTIS_WHITE; + else if(themeId == R.id.theme_five_color) currentTheme = ThemeName.FIVE_COLOR; + else if(themeId == R.id.theme_orange_box) currentTheme = ThemeName.ORANGE_BOX; if (currentTheme != oldTheme) { - syntaxManager.applyTheme(currentLanguage, currentTheme); + languageManager.applyTheme(currentLanguage, currentTheme); } } diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoSyntaxUtils.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java similarity index 88% rename from app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoSyntaxUtils.java rename to app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java index 8c3b384..b0246a7 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoSyntaxUtils.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/GoLanguage.java @@ -2,12 +2,18 @@ import android.content.Context; +import com.amrdeveloper.codeview.Code; import com.amrdeveloper.codeview.CodeView; +import com.amrdeveloper.codeview.Keyword; import com.amrdeveloper.codeviewlibrary.R; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.regex.Pattern; -public class GoSyntaxUtils { +public class GoLanguage { //Language Keywords private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(break|default|func|interface|case|defer|" + @@ -129,4 +135,28 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) { codeView.reHighlightSyntax(); } + public static String[] getKeywords(Context context) { + return context.getResources().getStringArray(R.array.go_keywords); + } + + public static List getCodeList(Context context) { + List codeList = new ArrayList<>(); + String[] keywords = getKeywords(context); + for (String keyword : keywords) { + codeList.add(new Keyword(keyword)); + } + return codeList; + } + + public static Set getIndentationStarts() { + Set characterSet = new HashSet<>(); + characterSet.add('{'); + return characterSet; + } + + public static Set getIndentationEnds() { + Set characterSet = new HashSet<>(); + characterSet.add('}'); + return characterSet; + } } diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaSyntaxUtils.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java similarity index 89% rename from app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaSyntaxUtils.java rename to app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java index 2a58efb..0ad0379 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaSyntaxUtils.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/JavaLanguage.java @@ -2,12 +2,18 @@ import android.content.Context; +import com.amrdeveloper.codeview.Code; import com.amrdeveloper.codeview.CodeView; +import com.amrdeveloper.codeview.Keyword; import com.amrdeveloper.codeviewlibrary.R; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.regex.Pattern; -public class JavaSyntaxUtils { +public class JavaLanguage { //Language Keywords private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(abstract|boolean|break|byte|case|catch" + @@ -139,4 +145,28 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) { codeView.reHighlightSyntax(); } + public static String[] getKeywords(Context context) { + return context.getResources().getStringArray(R.array.java_keywords); + } + + public static List getCodeList(Context context) { + List codeList = new ArrayList<>(); + String[] keywords = getKeywords(context); + for (String keyword : keywords) { + codeList.add(new Keyword(keyword)); + } + return codeList; + } + + public static Set getIndentationStarts() { + Set characterSet = new HashSet<>(); + characterSet.add('{'); + return characterSet; + } + + public static Set getIndentationEnds() { + Set characterSet = new HashSet<>(); + characterSet.add('}'); + return characterSet; + } } diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageManager.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageManager.java new file mode 100644 index 0000000..a04630c --- /dev/null +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageManager.java @@ -0,0 +1,132 @@ +package com.amrdeveloper.codeviewlibrary.syntax; + +import android.content.Context; + +import com.amrdeveloper.codeview.Code; +import com.amrdeveloper.codeview.CodeView; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class LanguageManager { + + private final Context context; + private final CodeView codeView; + + public LanguageManager(Context context, CodeView codeView) { + this.context = context; + this.codeView = codeView; + } + + public void applyTheme(LanguageName language, ThemeName theme) { + switch (theme) { + case MONOKAI: + applyMonokaiTheme(language); + break; + case NOCTIS_WHITE: + applyNoctisWhiteTheme(language); + break; + case FIVE_COLOR: + applyFiveColorsDarkTheme(language); + break; + case ORANGE_BOX: + applyOrangeBoxTheme(language); + break; + } + } + + public String[] getLanguageKeywords(LanguageName language) { + switch (language) { + case JAVA: return JavaLanguage.getKeywords(context); + case PYTHON: return PythonLanguage.getKeywords(context); + case GO_LANG: return GoLanguage.getKeywords(context); + default: return new String[]{}; + } + } + + public List getLanguageCodeList(LanguageName language) { + switch (language) { + case JAVA: return JavaLanguage.getCodeList(context); + case PYTHON: return PythonLanguage.getCodeList(context); + case GO_LANG: return GoLanguage.getCodeList(context); + default: return new ArrayList<>(); + } + } + + public Set getLanguageIndentationStarts(LanguageName language) { + switch (language) { + case JAVA: return JavaLanguage.getIndentationStarts(); + case PYTHON: return PythonLanguage.getIndentationStarts(); + case GO_LANG: return GoLanguage.getIndentationStarts(); + default: return new HashSet<>(); + } + } + + public Set getLanguageIndentationEnds(LanguageName language) { + switch (language) { + case JAVA: return JavaLanguage.getIndentationEnds(); + case PYTHON: return PythonLanguage.getIndentationEnds(); + case GO_LANG: return GoLanguage.getIndentationEnds(); + default: return new HashSet<>(); + } + } + + private void applyMonokaiTheme(LanguageName language) { + switch (language) { + case JAVA: + JavaLanguage.applyMonokaiTheme(context, codeView); + break; + case PYTHON: + PythonLanguage.applyMonokaiTheme(context, codeView); + break; + case GO_LANG: + GoLanguage.applyMonokaiTheme(context, codeView); + break; + } + } + + private void applyNoctisWhiteTheme(LanguageName language) { + switch (language) { + case JAVA: + JavaLanguage.applyNoctisWhiteTheme(context, codeView); + break; + case PYTHON: + PythonLanguage.applyNoctisWhiteTheme(context, codeView); + break; + case GO_LANG: + GoLanguage.applyNoctisWhiteTheme(context, codeView); + break; + } + } + + private void applyFiveColorsDarkTheme(LanguageName language) { + switch (language) { + case JAVA: + JavaLanguage.applyFiveColorsDarkTheme(context, codeView); + break; + case PYTHON: + PythonLanguage.applyFiveColorsDarkTheme(context, codeView); + break; + case GO_LANG: + GoLanguage.applyFiveColorsDarkTheme(context, codeView); + break; + } + } + + private void applyOrangeBoxTheme(LanguageName language) { + switch (language) { + case JAVA: + JavaLanguage.applyOrangeBoxTheme(context, codeView); + break; + case PYTHON: + PythonLanguage.applyOrangeBoxTheme(context, codeView); + break; + case GO_LANG: + GoLanguage.applyOrangeBoxTheme(context, codeView); + break; + } + } + +} diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/Language.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageName.java similarity index 83% rename from app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/Language.java rename to app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageName.java index 33e39c5..6478244 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/Language.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/LanguageName.java @@ -3,7 +3,7 @@ /** * List of examples languages for CodeView */ -public enum Language { +public enum LanguageName { JAVA, PYTHON, GO_LANG diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonSyntaxUtils.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonLanguage.java similarity index 89% rename from app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonSyntaxUtils.java rename to app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonLanguage.java index 2bc5062..b84c471 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonSyntaxUtils.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/PythonLanguage.java @@ -2,12 +2,18 @@ import android.content.Context; +import com.amrdeveloper.codeview.Code; import com.amrdeveloper.codeview.CodeView; +import com.amrdeveloper.codeview.Keyword; import com.amrdeveloper.codeviewlibrary.R; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.regex.Pattern; -public class PythonSyntaxUtils { +public class PythonLanguage { //Language Keywords private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(False|await|else|import|pass|None|break|except|in|raise" + @@ -128,4 +134,27 @@ public static void applyOrangeBoxTheme(Context context, CodeView codeView) { codeView.reHighlightSyntax(); } + + public static String[] getKeywords(Context context) { + return context.getResources().getStringArray(R.array.python_keywords); + } + + public static List getCodeList(Context context) { + List codeList = new ArrayList<>(); + String[] keywords = getKeywords(context); + for (String keyword : keywords) { + codeList.add(new Keyword(keyword)); + } + return codeList; + } + + public static Set getIndentationStarts() { + Set characterSet = new HashSet<>(); + characterSet.add(':'); + return characterSet; + } + + public static Set getIndentationEnds() { + return new HashSet<>(); + } } diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/SyntaxManager.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/SyntaxManager.java deleted file mode 100644 index c842b9d..0000000 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/SyntaxManager.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.amrdeveloper.codeviewlibrary.syntax; - -import android.content.Context; - -import com.amrdeveloper.codeview.CodeView; - -public class SyntaxManager { - - private final Context context; - private final CodeView codeView; - - public SyntaxManager(Context context, CodeView codeView) { - this.context = context; - this.codeView = codeView; - } - - public void applyTheme(Language language, Theme theme) { - switch (theme) { - case MONOKAI: - applyMonokaiTheme(language); - break; - case NOCTIS_WHITE: - applyNoctisWhiteTheme(language); - break; - case FIVE_COLOR: - applyFiveColorsDarkTheme(language); - break; - case ORANGE_BOX: - applyOrangeBoxTheme(language); - break; - } - } - - private void applyMonokaiTheme(Language language) { - switch (language) { - case JAVA: - JavaSyntaxUtils.applyMonokaiTheme(context, codeView); - break; - case PYTHON: - PythonSyntaxUtils.applyMonokaiTheme(context, codeView); - break; - case GO_LANG: - GoSyntaxUtils.applyMonokaiTheme(context, codeView); - break; - } - } - - private void applyNoctisWhiteTheme(Language language) { - switch (language) { - case JAVA: - JavaSyntaxUtils.applyNoctisWhiteTheme(context, codeView); - break; - case PYTHON: - PythonSyntaxUtils.applyNoctisWhiteTheme(context, codeView); - break; - case GO_LANG: - GoSyntaxUtils.applyNoctisWhiteTheme(context, codeView); - break; - } - } - - private void applyFiveColorsDarkTheme(Language language) { - switch (language) { - case JAVA: - JavaSyntaxUtils.applyFiveColorsDarkTheme(context, codeView); - break; - case PYTHON: - PythonSyntaxUtils.applyFiveColorsDarkTheme(context, codeView); - break; - case GO_LANG: - GoSyntaxUtils.applyFiveColorsDarkTheme(context, codeView); - break; - } - } - - private void applyOrangeBoxTheme(Language language) { - switch (language) { - case JAVA: - JavaSyntaxUtils.applyOrangeBoxTheme(context, codeView); - break; - case PYTHON: - PythonSyntaxUtils.applyOrangeBoxTheme(context, codeView); - break; - case GO_LANG: - GoSyntaxUtils.applyOrangeBoxTheme(context, codeView); - break; - } - } - -} diff --git a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/Theme.java b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/ThemeName.java similarity index 82% rename from app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/Theme.java rename to app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/ThemeName.java index 4472b4c..73d6a64 100644 --- a/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/Theme.java +++ b/app/src/main/java/com/amrdeveloper/codeviewlibrary/syntax/ThemeName.java @@ -1,6 +1,6 @@ package com.amrdeveloper.codeviewlibrary.syntax; -public enum Theme { +public enum ThemeName { MONOKAI, NOCTIS_WHITE, FIVE_COLOR,