mirror of
https://codeberg.org/gitnex/GitNex.git
synced 2023-12-13 20:50:18 +01:00
Refactor language resolution
This commit is contained in:
parent
607ba5bb8e
commit
e7e4d15d34
|
@ -12,6 +12,8 @@ import org.mian.gitnex.R;
|
|||
import org.mian.gitnex.core.MainGrammarLocator;
|
||||
import org.mian.gitnex.databinding.ActivityCodeEditorBinding;
|
||||
import org.mian.gitnex.helpers.codeeditor.*;
|
||||
import org.mian.gitnex.helpers.codeeditor.languages.Language;
|
||||
import org.mian.gitnex.helpers.codeeditor.languages.UnknownLanguage;
|
||||
import org.mian.gitnex.helpers.codeeditor.theme.Theme;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -26,7 +28,7 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
|
||||
private Theme currentTheme;
|
||||
private ActivityCodeEditorBinding binding;
|
||||
private LanguageName currentLanguage = LanguageName.UNKNOWN;
|
||||
private Language currentLanguage = new UnknownLanguage();
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -44,14 +46,9 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
currentTheme = Theme.getDefaultTheme(this);
|
||||
|
||||
if(getIntent().getStringExtra("fileExtension") != null) {
|
||||
fileExtension = MainGrammarLocator.fromExtension(getIntent().getStringExtra("fileExtension")).toUpperCase();
|
||||
fileExtension = MainGrammarLocator.fromExtension(getIntent().getStringExtra("fileExtension"));
|
||||
|
||||
if(EnumUtils.isValidEnum(LanguageName.class, fileExtension)) {
|
||||
currentLanguage = LanguageName.valueOf(fileExtension);
|
||||
}
|
||||
else {
|
||||
currentLanguage = LanguageName.UNKNOWN;
|
||||
}
|
||||
currentLanguage = Language.fromName(fileExtension);
|
||||
}
|
||||
|
||||
configCodeView(currentLanguage, fileContent);
|
||||
|
@ -64,7 +61,7 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
setResult(Activity.RESULT_OK, intent);
|
||||
}
|
||||
|
||||
private void configCodeView(LanguageName currentLanguage, String fileContent) {
|
||||
private void configCodeView(Language currentLanguage, String fileContent) {
|
||||
|
||||
binding.codeView.setTypeface(Typeface.createFromAsset(ctx.getAssets(), "fonts/sourcecodeproregular.ttf"));
|
||||
|
||||
|
@ -78,7 +75,7 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
binding.codeView.setEnableAutoIndentation(true);
|
||||
|
||||
// Set up the language and theme with SyntaxManager helper class
|
||||
currentLanguage.getLanguage().applyTheme(this, binding.codeView, currentTheme);
|
||||
currentLanguage.applyTheme(this, binding.codeView, currentTheme);
|
||||
|
||||
// Setup auto pair complete
|
||||
final Map<Character, Character> pairCompleteMap = new HashMap<>();
|
||||
|
@ -103,14 +100,14 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
|
||||
boolean useModernAutoCompleteAdapter = true;
|
||||
if(useModernAutoCompleteAdapter) {
|
||||
List<Code> codeList = currentLanguage.getLanguage().getCodeList();
|
||||
List<Code> codeList = currentLanguage.getCodeList();
|
||||
|
||||
CustomCodeViewAdapter adapter = new CustomCodeViewAdapter(this, codeList);
|
||||
|
||||
binding.codeView.setAdapter(adapter);
|
||||
}
|
||||
else {
|
||||
String[] languageKeywords = currentLanguage.getLanguage().getKeywords();
|
||||
String[] languageKeywords = currentLanguage.getKeywords();
|
||||
|
||||
final int layoutId = R.layout.list_item_suggestion;
|
||||
|
||||
|
@ -122,8 +119,8 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
}
|
||||
|
||||
private void configLanguageAutoIndentation() {
|
||||
binding.codeView.setIndentationStarts(currentLanguage.getLanguage().getIndentationStarts());
|
||||
binding.codeView.setIndentationEnds(currentLanguage.getLanguage().getIndentationEnds());
|
||||
binding.codeView.setIndentationStarts(currentLanguage.getIndentationStarts());
|
||||
binding.codeView.setIndentationEnds(currentLanguage.getIndentationEnds());
|
||||
}
|
||||
|
||||
private void configCodeViewPlugins() {
|
||||
|
@ -134,7 +131,7 @@ public class CodeEditorActivity extends BaseActivity {
|
|||
}
|
||||
|
||||
private void configLanguageName() {
|
||||
binding.languageName.setText(currentLanguage.name().toLowerCase());
|
||||
binding.languageName.setText(currentLanguage.getName().toLowerCase());
|
||||
}
|
||||
|
||||
private void configSourcePositionListener() {
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
package org.mian.gitnex.helpers.codeeditor;
|
||||
|
||||
import org.mian.gitnex.helpers.codeeditor.languages.*;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author AmrDeveloper
|
||||
* @author M M Arif
|
||||
*/
|
||||
|
||||
public enum LanguageName {
|
||||
UNKNOWN, // no language is specified or app currently does not support the mentioned language
|
||||
JAVA, // java
|
||||
PYTHON,
|
||||
GO, // go lang
|
||||
PHP, // php
|
||||
XML, // xml
|
||||
HTML; // html
|
||||
|
||||
private static final HashMap<LanguageName, Language> languages = new HashMap<>();
|
||||
static {
|
||||
languages.put(JAVA, new JavaLanguage());
|
||||
languages.put(PYTHON, new PythonLanguage());
|
||||
languages.put(GO, new GoLanguage());
|
||||
languages.put(PHP, new PhpLanguage());
|
||||
languages.put(XML, new XmlLanguage());
|
||||
languages.put(HTML, new HtmlLanguage());
|
||||
}
|
||||
|
||||
public Language getLanguage() {
|
||||
return languages.get(this);
|
||||
}
|
||||
}
|
|
@ -12,13 +12,6 @@ import android.widget.EditText;
|
|||
|
||||
public class SourcePositionListener {
|
||||
|
||||
@FunctionalInterface
|
||||
public interface OnPositionChanged {
|
||||
|
||||
void onPositionChange(int line, int column);
|
||||
|
||||
}
|
||||
|
||||
private OnPositionChanged onPositionChanged;
|
||||
|
||||
public SourcePositionListener(EditText editText) {
|
||||
|
@ -46,4 +39,11 @@ public class SourcePositionListener {
|
|||
onPositionChanged = listener;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface OnPositionChanged {
|
||||
|
||||
void onPositionChange(int line, int column);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,6 +47,11 @@ public class GoLanguage extends Language {
|
|||
return codeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Go";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
Set<Character> characterSet = new HashSet<>();
|
||||
|
|
|
@ -82,6 +82,11 @@ public class HtmlLanguage extends Language {
|
|||
return codeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "HTML";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
Set<Character> characterSet = new HashSet<>();
|
||||
|
|
|
@ -87,6 +87,11 @@ public class JavaLanguage extends Language {
|
|||
return codeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Java";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
Set<Character> characterSet = new HashSet<>();
|
||||
|
|
|
@ -5,6 +5,7 @@ import android.content.res.Resources;
|
|||
import com.amrdeveloper.codeview.Code;
|
||||
import com.amrdeveloper.codeview.CodeView;
|
||||
import org.mian.gitnex.helpers.codeeditor.theme.Theme;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
@ -15,22 +16,55 @@ import java.util.regex.Pattern;
|
|||
*/
|
||||
|
||||
public abstract class Language {
|
||||
|
||||
private static HashMap<String, Language> languages = null;
|
||||
|
||||
private static void initializeMap() {
|
||||
if(languages == null) {
|
||||
languages = new HashMap<>();
|
||||
languages.put("JAVA", new JavaLanguage());
|
||||
languages.put("PYTHON", new PythonLanguage());
|
||||
languages.put("GO", new GoLanguage());
|
||||
languages.put("PHP", new PhpLanguage());
|
||||
languages.put("XML", new XmlLanguage());
|
||||
languages.put("HTML", new HtmlLanguage());
|
||||
}
|
||||
}
|
||||
|
||||
public static Language fromName(String name) {
|
||||
initializeMap();
|
||||
|
||||
return isValid(name) ? languages.get(name.toUpperCase()) : new UnknownLanguage();
|
||||
}
|
||||
|
||||
public static boolean isValid(String name) {
|
||||
initializeMap();
|
||||
|
||||
return languages.containsKey(name.toUpperCase());
|
||||
}
|
||||
|
||||
public abstract Pattern getPattern(LanguageElement element);
|
||||
|
||||
public abstract Set<Character> getIndentationStarts();
|
||||
|
||||
public abstract Set<Character> getIndentationEnds();
|
||||
|
||||
public abstract String[] getKeywords();
|
||||
|
||||
public abstract List<Code> getCodeList();
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public void applyTheme(Context context, CodeView codeView, Theme theme) {
|
||||
codeView.resetSyntaxPatternList();
|
||||
codeView.resetHighlighter();
|
||||
|
||||
Resources resources = context.getResources();
|
||||
|
||||
//View Background
|
||||
// View Background
|
||||
codeView.setBackgroundColor(resources.getColor(theme.getBackgroundColor(), null));
|
||||
|
||||
//Syntax Colors
|
||||
// Syntax Colors
|
||||
for(LanguageElement e : Objects.requireNonNull(LanguageElement.class.getEnumConstants())) {
|
||||
Pattern p = getPattern(e);
|
||||
if(p != null) {
|
||||
|
@ -38,9 +72,10 @@ public abstract class Language {
|
|||
}
|
||||
}
|
||||
|
||||
//Default Color
|
||||
// Default Color
|
||||
codeView.setTextColor(resources.getColor(theme.getDefaultColor(), null));
|
||||
|
||||
codeView.reHighlightSyntax();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,18 +5,6 @@ package org.mian.gitnex.helpers.codeeditor.languages;
|
|||
*/
|
||||
|
||||
public enum LanguageElement {
|
||||
KEYWORD,
|
||||
ATTRIBUTE,
|
||||
BUILTIN,
|
||||
NUMBER,
|
||||
HEX,
|
||||
OPERATION,
|
||||
GENERIC,
|
||||
ANNOTATION,
|
||||
CHAR,
|
||||
STRING,
|
||||
SINGLE_LINE_COMMENT,
|
||||
TODO_COMMENT,
|
||||
MULTI_LINE_COMMENT
|
||||
KEYWORD, ATTRIBUTE, BUILTIN, NUMBER, HEX, OPERATION, GENERIC, ANNOTATION, CHAR, STRING, SINGLE_LINE_COMMENT, TODO_COMMENT, MULTI_LINE_COMMENT
|
||||
|
||||
}
|
||||
|
|
|
@ -87,6 +87,11 @@ public class PhpLanguage extends Language {
|
|||
return codeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "PHP";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
Set<Character> characterSet = new HashSet<>();
|
||||
|
|
|
@ -85,6 +85,11 @@ public class PythonLanguage extends Language {
|
|||
return codeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Python";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
Set<Character> characterSet = new HashSet<>();
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package org.mian.gitnex.helpers.codeeditor.languages;
|
||||
|
||||
import com.amrdeveloper.codeview.Code;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class UnknownLanguage extends Language {
|
||||
|
||||
@Override
|
||||
public Pattern getPattern(LanguageElement element) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationEnds() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getKeywords() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Code> getCodeList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
}
|
|
@ -26,6 +26,14 @@ public class XmlLanguage extends Language {
|
|||
private static final Pattern PATTERN_ATTRIBUTE = Pattern.compile("\\.[a-zA-Z0-9_]+");
|
||||
private static final Pattern PATTERN_OPERATION = Pattern.compile(":|==|>|<|!=|>=|<=|->|=|>|<|%|-|-=|%=|\\+|\\-|\\-=|\\+=|\\^|\\&|\\|::|\\?|\\*");
|
||||
|
||||
public static String getCommentStart() {
|
||||
return "<!--";
|
||||
}
|
||||
|
||||
public static String getCommentEnd() {
|
||||
return "-->";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pattern getPattern(LanguageElement element) {
|
||||
switch(element) {
|
||||
|
@ -58,11 +66,7 @@ public class XmlLanguage extends Language {
|
|||
|
||||
@Override
|
||||
public String[] getKeywords() {
|
||||
return new String[]{
|
||||
"<xml",
|
||||
"encoding",
|
||||
"version",
|
||||
};
|
||||
return new String[]{"<xml", "encoding", "version",};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -75,6 +79,11 @@ public class XmlLanguage extends Language {
|
|||
return codeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "XML";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Character> getIndentationStarts() {
|
||||
Set<Character> characterSet = new HashSet<>();
|
||||
|
@ -89,12 +98,4 @@ public class XmlLanguage extends Language {
|
|||
return characterSet;
|
||||
}
|
||||
|
||||
public static String getCommentStart() {
|
||||
return "<!--";
|
||||
}
|
||||
|
||||
public static String getCommentEnd() {
|
||||
return "-->";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,43 +12,34 @@ import io.noties.markwon.core.MarkwonTheme;
|
|||
* @author qwerty287
|
||||
*/
|
||||
public class MarkwonHighlighter extends AbstractMarkwonPlugin {
|
||||
@NonNull
|
||||
public static MarkwonHighlighter create(
|
||||
Context context,
|
||||
@NonNull Theme theme) {
|
||||
return create(context, theme, null);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static MarkwonHighlighter create(
|
||||
Context context,
|
||||
@NonNull Theme theme,
|
||||
@Nullable String fallbackLanguage) {
|
||||
return new MarkwonHighlighter(context, theme, fallbackLanguage);
|
||||
}
|
||||
|
||||
private final Theme theme;
|
||||
private final Context context;
|
||||
private final String fallbackLanguage;
|
||||
|
||||
public MarkwonHighlighter(
|
||||
Context context,
|
||||
@NonNull Theme theme,
|
||||
@Nullable String fallbackLanguage) {
|
||||
public MarkwonHighlighter(Context context, @NonNull Theme theme, @Nullable String fallbackLanguage) {
|
||||
this.theme = theme;
|
||||
this.context = context;
|
||||
this.fallbackLanguage = fallbackLanguage;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static MarkwonHighlighter create(Context context, @NonNull Theme theme) {
|
||||
return create(context, theme, null);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static MarkwonHighlighter create(Context context, @NonNull Theme theme, @Nullable String fallbackLanguage) {
|
||||
return new MarkwonHighlighter(context, theme, fallbackLanguage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureTheme(@NonNull MarkwonTheme.Builder builder) {
|
||||
builder
|
||||
.codeTextColor(context.getResources().getColor(theme.getDefaultColor(), null))
|
||||
.codeBackgroundColor(context.getResources().getColor(theme.getBackgroundColor(), null));
|
||||
builder.codeTextColor(context.getResources().getColor(theme.getDefaultColor(), null)).codeBackgroundColor(context.getResources().getColor(theme.getBackgroundColor(), null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
|
||||
builder.syntaxHighlight(SyntaxHighlighter.create(context, theme, fallbackLanguage));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,9 +7,7 @@ import android.text.SpannableStringBuilder;
|
|||
import android.text.style.ForegroundColorSpan;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import org.apache.commons.lang3.EnumUtils;
|
||||
import org.mian.gitnex.core.MainGrammarLocator;
|
||||
import org.mian.gitnex.helpers.codeeditor.LanguageName;
|
||||
import org.mian.gitnex.helpers.codeeditor.languages.Language;
|
||||
import org.mian.gitnex.helpers.codeeditor.languages.LanguageElement;
|
||||
import org.mian.gitnex.helpers.codeeditor.theme.Theme;
|
||||
|
@ -24,34 +22,25 @@ import io.noties.markwon.syntax.SyntaxHighlight;
|
|||
|
||||
public class SyntaxHighlighter implements SyntaxHighlight {
|
||||
|
||||
private final Theme theme;
|
||||
private final Context context;
|
||||
private final String fallback;
|
||||
protected SyntaxHighlighter(Context context, @NonNull Theme theme, @Nullable String fallback) {
|
||||
this.context = context;
|
||||
this.theme = theme;
|
||||
this.fallback = fallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static SyntaxHighlighter create(
|
||||
Context context,
|
||||
@NonNull Theme theme) {
|
||||
public static SyntaxHighlighter create(Context context, @NonNull Theme theme) {
|
||||
return new SyntaxHighlighter(context, theme, null);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static SyntaxHighlighter create(
|
||||
Context context,
|
||||
@NonNull Theme theme,
|
||||
@Nullable String fallback) {
|
||||
public static SyntaxHighlighter create(Context context, @NonNull Theme theme, @Nullable String fallback) {
|
||||
return new SyntaxHighlighter(context, theme, fallback);
|
||||
}
|
||||
|
||||
private final Theme theme;
|
||||
private final Context context;
|
||||
private final String fallback;
|
||||
|
||||
protected SyntaxHighlighter(
|
||||
Context context,
|
||||
@NonNull Theme theme,
|
||||
@Nullable String fallback) {
|
||||
this.context = context;
|
||||
this.theme = theme;
|
||||
this.fallback = fallback;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public CharSequence highlight(@Nullable String info, @NonNull String code) {
|
||||
|
@ -59,32 +48,24 @@ public class SyntaxHighlighter implements SyntaxHighlight {
|
|||
return code;
|
||||
}
|
||||
|
||||
if (info == null) {
|
||||
if(info == null) {
|
||||
info = fallback;
|
||||
}
|
||||
|
||||
if(info != null) {
|
||||
info = MainGrammarLocator.fromExtension(info).toUpperCase();
|
||||
info = MainGrammarLocator.fromExtension(info);
|
||||
}
|
||||
|
||||
Editable highlightedCode = new SpannableStringBuilder(code);
|
||||
|
||||
Language l;
|
||||
if(EnumUtils.isValidEnum(LanguageName.class, info)) {
|
||||
l = LanguageName.valueOf(info).getLanguage();
|
||||
}
|
||||
else {
|
||||
return code;
|
||||
}
|
||||
Language l = Language.fromName(info);
|
||||
|
||||
for(LanguageElement e : Objects.requireNonNull(LanguageElement.class.getEnumConstants())) {
|
||||
Pattern p = l.getPattern(e);
|
||||
if(p != null) {
|
||||
Matcher matcher = p.matcher(highlightedCode);
|
||||
while (matcher.find()) {
|
||||
highlightedCode.setSpan(new ForegroundColorSpan(context.getResources().getColor(theme.getColor(e), null)),
|
||||
matcher.start(), matcher.end(),
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
while(matcher.find()) {
|
||||
highlightedCode.setSpan(new ForegroundColorSpan(context.getResources().getColor(theme.getColor(e), null)), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,13 @@ import org.mian.gitnex.helpers.codeeditor.languages.LanguageElement;
|
|||
public interface Theme {
|
||||
|
||||
FiveColorsTheme FIVE_COLORS = new FiveColorsTheme();
|
||||
|
||||
FiveColorsDarkTheme FIVE_COLORS_DARK = new FiveColorsDarkTheme();
|
||||
|
||||
static Theme getDefaultTheme(Context context) {
|
||||
return AppUtil.getColorFromAttribute(context, R.attr.isDark) == 1 ? FIVE_COLORS_DARK : FIVE_COLORS;
|
||||
}
|
||||
|
||||
@ColorRes
|
||||
int getColor(LanguageElement element);
|
||||
|
||||
|
@ -23,7 +28,4 @@ public interface Theme {
|
|||
@ColorRes
|
||||
int getBackgroundColor();
|
||||
|
||||
static Theme getDefaultTheme(Context context) {
|
||||
return AppUtil.getColorFromAttribute(context, R.attr.isDark) == 1 ? FIVE_COLORS_DARK : FIVE_COLORS;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue