Add xml and html langs to code editor for auto complete and highlight

This commit is contained in:
M M Arif 2022-08-14 20:38:31 +05:00
parent 327d906383
commit e6a627b6ad
13 changed files with 328 additions and 67 deletions

View file

@ -73,7 +73,7 @@ public class CodeEditorActivity extends BaseActivity {
// Setup Line number feature
binding.codeView.setEnableLineNumber(true);
binding.codeView.setLineNumberTextColor(Color.GRAY);
binding.codeView.setLineNumberTextSize(44f);
binding.codeView.setLineNumberTextSize(32f);
// Setup Auto indenting feature
binding.codeView.setTabLength(4);

View file

@ -4,12 +4,10 @@ import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeViewAdapter;
import com.amrdeveloper.codeview.Snippet;
import org.mian.gitnex.R;
import java.util.List;
@ -33,16 +31,16 @@ public class CustomCodeViewAdapter extends CodeViewAdapter {
convertView = layoutInflater.inflate(R.layout.list_items_autocomplete, parent, false);
}
ImageView codeType = convertView.findViewById(R.id.code_type);
//ImageView codeType = convertView.findViewById(R.id.code_type);
TextView codeTitle = convertView.findViewById(R.id.code_title);
Code currentCode = (Code) getItem(position);
if (currentCode != null) {
codeTitle.setText(currentCode.getCodeTitle());
if (currentCode instanceof Snippet) {
codeType.setImageResource(R.drawable.ic_snippet);
/*if (currentCode instanceof Snippet) {
//codeType.setImageResource(R.drawable.ic_snippet);
} else {
codeType.setImageResource(R.drawable.ic_keyword);
}
//codeType.setImageResource(R.drawable.ic_keyword);
}*/
}
return convertView;

View file

@ -4,9 +4,11 @@ import android.content.Context;
import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;
import org.mian.gitnex.helpers.codeeditor.languages.GoLanguage;
import org.mian.gitnex.helpers.codeeditor.languages.HtmlLanguage;
import org.mian.gitnex.helpers.codeeditor.languages.JavaLanguage;
import org.mian.gitnex.helpers.codeeditor.languages.PhpLanguage;
import org.mian.gitnex.helpers.codeeditor.languages.PythonLanguage;
import org.mian.gitnex.helpers.codeeditor.languages.XmlLanguage;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@ -40,6 +42,8 @@ public class LanguageManager {
case PY: return PythonLanguage.getKeywords(context);
case GO: return GoLanguage.getKeywords(context);
case PHP: return PhpLanguage.getKeywords(context);
case XML: return XmlLanguage.getKeywords(context);
case HTML: return HtmlLanguage.getKeywords(context);
default: return new String[]{};
}
}
@ -50,6 +54,8 @@ public class LanguageManager {
case PY: return PythonLanguage.getCodeList(context);
case GO: return GoLanguage.getCodeList(context);
case PHP: return PhpLanguage.getCodeList(context);
case XML: return XmlLanguage.getCodeList(context);
case HTML: return HtmlLanguage.getCodeList(context);
default: return new ArrayList<>();
}
}
@ -60,6 +66,8 @@ public class LanguageManager {
case PY: return PythonLanguage.getIndentationStarts();
case GO: return GoLanguage.getIndentationStarts();
case PHP: return PhpLanguage.getIndentationStarts();
case XML: return XmlLanguage.getIndentationStarts();
case HTML: return HtmlLanguage.getIndentationStarts();
default: return new HashSet<>();
}
}
@ -70,6 +78,8 @@ public class LanguageManager {
case PY: return PythonLanguage.getIndentationEnds();
case GO: return GoLanguage.getIndentationEnds();
case PHP: return PhpLanguage.getIndentationEnds();
case XML: return XmlLanguage.getIndentationEnds();
case HTML: return HtmlLanguage.getIndentationEnds();
default: return new HashSet<>();
}
}
@ -88,6 +98,12 @@ public class LanguageManager {
case PHP:
PhpLanguage.applyFiveColorsDarkTheme(context, codeView);
break;
case XML:
XmlLanguage.applyFiveColorsDarkTheme(context, codeView);
break;
case HTML:
HtmlLanguage.applyFiveColorsDarkTheme(context, codeView);
break;
}
}
}

View file

@ -10,5 +10,7 @@ public enum LanguageName {
JAVA, // java
PY, // python with py extension
GO, // go lang
PHP // php
PHP, // php
XML, // xml
HTML // html
}

View file

@ -12,8 +12,6 @@ import android.widget.EditText;
public class SourcePositionListener {
private final EditText editText;
@FunctionalInterface
public interface OnPositionChanged {
void onPositionChange(int line, int column);
@ -22,7 +20,22 @@ public class SourcePositionListener {
private OnPositionChanged onPositionChanged;
public SourcePositionListener(EditText editText) {
this.editText = editText;
View.AccessibilityDelegate viewAccessibility = new View.AccessibilityDelegate() {
@Override
public void sendAccessibilityEvent(View host, int eventType) {
super.sendAccessibilityEvent(host, eventType);
if(eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED && onPositionChanged != null) {
int selectionStart = editText.getSelectionStart();
Layout layout = editText.getLayout();
if(layout == null)
return;
int line = editText.getLayout().getLineForOffset(selectionStart);
int column = selectionStart - editText.getLayout().getLineStart(line);
onPositionChanged.onPositionChange(line + 1, column + 1);
}
}
};
editText.setAccessibilityDelegate(viewAccessibility);
}
@ -30,19 +43,4 @@ public class SourcePositionListener {
onPositionChanged = listener;
}
private final View.AccessibilityDelegate viewAccessibility = new View.AccessibilityDelegate() {
@Override
public void sendAccessibilityEvent(View host, int eventType) {
super.sendAccessibilityEvent(host, eventType);
if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED && onPositionChanged != null) {
int selectionStart = editText.getSelectionStart();
Layout layout = editText.getLayout();
if (layout == null) return;
int line = editText.getLayout().getLineForOffset(selectionStart);
int column = selectionStart - editText.getLayout().getLineStart(line);
onPositionChanged.onPositionChange(line + 1, column + 1);
}
}
};
}

View file

@ -0,0 +1,99 @@
package org.mian.gitnex.helpers.codeeditor.languages;
import android.content.Context;
import android.content.res.Resources;
import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;
import com.amrdeveloper.codeview.Keyword;
import org.mian.gitnex.R;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
* @author M M Arif
*/
public class HtmlLanguage {
//Language Keywords
private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(<html|<DOCTYPE|<head|<title|<body|<style|<script|src|href" +
"<h1|<h2|<h3|<h4|<h5|<h6|<br|<hr|<section|<header|<footer|<select|<img|<embed|<iframe|<div|<p|code|strong|small|template|" +
"input|form|textarea|button|option|label|fieldset|legend|datalist|frame|map|area|canvas|picture|svg|audio|source|track|video|" +
"link|nav|ul|ol|li|table|caption|th|tr|td|thead|tbody|tfooter|col|span|main|article|aside|meta|base|noscript|object|param|)\\b");
//Brackets and Colons
private static final Pattern PATTERN_BUILTINS = Pattern.compile("[,:;[->]{}()]");
//Data
private static final Pattern PATTERN_NUMBERS = Pattern.compile("\\b(\\d*[.]?\\d+)\\b");
private static final Pattern PATTERN_CHAR = Pattern.compile("['](.*?)[']");
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_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( ":|==|>|<|!=|>=|<=|->|=|>|<|%|-|-=|%=|\\+|\\-|\\-=|\\+=|\\^|\\&|\\|::|\\?|\\*");
public static void applyFiveColorsDarkTheme(Context context, CodeView codeView) {
codeView.resetSyntaxPatternList();
codeView.resetHighlighter();
Resources resources = context.getResources();
//View Background
codeView.setBackgroundColor(resources.getColor(R.color.five_dark_black, null));
//Syntax Colors
codeView.addSyntaxPattern(PATTERN_HEX, resources.getColor(R.color.five_dark_purple, null));
codeView.addSyntaxPattern(PATTERN_CHAR, resources.getColor(R.color.five_dark_yellow, null));
codeView.addSyntaxPattern(PATTERN_STRING, resources.getColor(R.color.five_dark_yellow, null));
codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.five_dark_purple, null));
codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.five_dark_purple, null));
codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.five_dark_white, null));
codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.five_dark_grey, null));
codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.five_dark_grey, null));
codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.five_dark_blue, null));
codeView.addSyntaxPattern(PATTERN_OPERATION, resources.getColor(R.color.five_dark_purple, null));
//Default Color
codeView.setTextColor(resources.getColor(R.color.five_dark_white, null));
codeView.reHighlightSyntax();
}
public static String[] getKeywords(Context context) {
return context.getResources().getStringArray(R.array.html_keywords);
}
public static List<Code> getCodeList(Context context) {
List<Code> codeList = new ArrayList<>();
String[] keywords = getKeywords(context);
for (String keyword : keywords) {
codeList.add(new Keyword(keyword));
}
return codeList;
}
public static Set<Character> getIndentationStarts() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('{');
return characterSet;
}
public static Set<Character> getIndentationEnds() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('}');
return characterSet;
}
public static String getCommentStart() {
return "//";
}
public static String getCommentEnd() {
return "";
}
}

View file

@ -19,7 +19,7 @@ import java.util.regex.Pattern;
public class PhpLanguage {
//Language Keywords
private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(php|__construct|var_dump|define|echo|var|float|" +
private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(<?php|__construct|var_dump|define|echo|var|float|" +
"int|bool|false|true|function|private|public|protected|interface|return|copy|struct|abstract|extends|" +
"trait|static|namespace|implements|__set|__get|unlink|this|try|catch|Throwable|Exception|pdo|" +
"str_replace|form|date|abs|min|max|strtotime|mktime|" +

View file

@ -0,0 +1,96 @@
package org.mian.gitnex.helpers.codeeditor.languages;
import android.content.Context;
import android.content.res.Resources;
import com.amrdeveloper.codeview.Code;
import com.amrdeveloper.codeview.CodeView;
import com.amrdeveloper.codeview.Keyword;
import org.mian.gitnex.R;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
/**
* @author M M Arif
*/
public class XmlLanguage {
//Language Keywords
private static final Pattern PATTERN_KEYWORDS = Pattern.compile("\\b(<xml|version|encoding)\\b");
//Brackets and Colons
private static final Pattern PATTERN_BUILTINS = Pattern.compile("[,:;[->]{}()]");
//Data
private static final Pattern PATTERN_NUMBERS = Pattern.compile("\\b(\\d*[.]?\\d+)\\b");
private static final Pattern PATTERN_CHAR = Pattern.compile("['](.*?)[']");
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_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( ":|==|>|<|!=|>=|<=|->|=|>|<|%|-|-=|%=|\\+|\\-|\\-=|\\+=|\\^|\\&|\\|::|\\?|\\*");
public static void applyFiveColorsDarkTheme(Context context, CodeView codeView) {
codeView.resetSyntaxPatternList();
codeView.resetHighlighter();
Resources resources = context.getResources();
//View Background
codeView.setBackgroundColor(resources.getColor(R.color.five_dark_black, null));
//Syntax Colors
codeView.addSyntaxPattern(PATTERN_HEX, resources.getColor(R.color.five_dark_purple, null));
codeView.addSyntaxPattern(PATTERN_CHAR, resources.getColor(R.color.five_dark_yellow, null));
codeView.addSyntaxPattern(PATTERN_STRING, resources.getColor(R.color.five_dark_yellow, null));
codeView.addSyntaxPattern(PATTERN_NUMBERS, resources.getColor(R.color.five_dark_purple, null));
codeView.addSyntaxPattern(PATTERN_KEYWORDS, resources.getColor(R.color.five_dark_purple, null));
codeView.addSyntaxPattern(PATTERN_BUILTINS, resources.getColor(R.color.five_dark_white, null));
codeView.addSyntaxPattern(PATTERN_SINGLE_LINE_COMMENT, resources.getColor(R.color.five_dark_grey, null));
codeView.addSyntaxPattern(PATTERN_MULTI_LINE_COMMENT, resources.getColor(R.color.five_dark_grey, null));
codeView.addSyntaxPattern(PATTERN_ATTRIBUTE, resources.getColor(R.color.five_dark_blue, null));
codeView.addSyntaxPattern(PATTERN_OPERATION, resources.getColor(R.color.five_dark_purple, null));
//Default Color
codeView.setTextColor(resources.getColor(R.color.five_dark_white, null));
codeView.reHighlightSyntax();
}
public static String[] getKeywords(Context context) {
return context.getResources().getStringArray(R.array.xml_keywords);
}
public static List<Code> getCodeList(Context context) {
List<Code> codeList = new ArrayList<>();
String[] keywords = getKeywords(context);
for (String keyword : keywords) {
codeList.add(new Keyword(keyword));
}
return codeList;
}
public static Set<Character> getIndentationStarts() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('{');
return characterSet;
}
public static Set<Character> getIndentationEnds() {
Set<Character> characterSet = new HashSet<>();
characterSet.add('}');
return characterSet;
}
public static String getCommentStart() {
return "//";
}
public static String getCommentEnd() {
return "";
}
}

View file

@ -1,12 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="25dp"
android:height="25dp"
android:viewportWidth="25"
android:viewportHeight="25">
<path
android:pathData="M12.5,12.5m-12.5,0a12.5,12.5 0,1 1,25 0a12.5,12.5 0,1 1,-25 0"
android:fillColor="#89D0E9"/>
<path
android:pathData="M11.972,13.052L10.256,14.792V17H9.368V8.6H10.256V13.676L15.188,8.6H16.208L12.572,12.404L16.46,17H15.404L11.972,13.052Z"
android:fillColor="#486069"/>
</vector>

View file

@ -47,6 +47,7 @@
android:layout_gravity="start"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_header"
android:background="?attr/primaryBackgroundColor"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

View file

@ -3,36 +3,17 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/autocomplete_background">
<RelativeLayout
android:id="@+id/code_container"
android:layout_width="@dimen/dimen40dp"
android:layout_height="@dimen/dimen50dp">
<ImageView
android:id="@+id/code_type"
android:layout_width="@dimen/dimen24dp"
android:layout_height="@dimen/dimen24dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_keyword"
android:textColor="@color/colorWhite"
android:textSize="@dimen/dimen20sp"
android:textStyle="bold"
tools:text="K"
android:contentDescription="@string/generalImgContentText" />
</RelativeLayout>
android:background="?attr/primaryBackgroundColor">
<TextView
android:id="@+id/code_title"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen40dp"
android:layout_toEndOf="@id/code_container"
android:layout_height="@dimen/dimen50dp"
android:gravity="start|center_vertical"
android:paddingStart="@dimen/dimen4dp"
android:paddingEnd="@dimen/dimen4dp"
android:textColor="@color/colorWhite"
android:paddingStart="@dimen/dimen8dp"
android:paddingEnd="@dimen/dimen8dp"
android:textColor="?attr/primaryTextColor"
tools:ignore="RtlSymmetry"
tools:text="Keyword" />
</RelativeLayout>

View file

@ -74,5 +74,4 @@
<color name="five_dark_grey">#a9b1ae</color>
<color name="five_dark_white">#ffffff</color>
<color name="gold">#e6b121</color>
<color name="autocomplete_background">#0c0c0c</color>
</resources>

View file

@ -1,9 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--HTML Keywords List-->
<string-array name="html_keywords">
<item><![CDATA[<html]]></item>
<item><![CDATA[<DOCTYPE]]></item>
<item><![CDATA[<head]]></item>
<item><![CDATA[<title]]></item>
<item><![CDATA[<body]]></item>
<item><![CDATA[<h1]]></item>
<item><![CDATA[<h2]]></item>
<item><![CDATA[<h3]]></item>
<item><![CDATA[<h4]]></item>
<item><![CDATA[<h5]]></item>
<item><![CDATA[<h6]]></item>
<item><![CDATA[<br]]></item>
<item><![CDATA[<hr]]></item>
<item><![CDATA[<section]]></item>
<item><![CDATA[<header]]></item>
<item><![CDATA[<footer]]></item>
<item><![CDATA[<select]]></item>
<item><![CDATA[<img]]></item>
<item><![CDATA[<embed]]></item>
<item><![CDATA[<iframe]]></item>
<item><![CDATA[<style]]></item>
<item><![CDATA[<script]]></item>
<item><![CDATA[<div]]></item>
<item><![CDATA[<p]]></item>
<item>code</item>
<item>strong</item>
<item>small</item>
<item>template</item>
<item>form</item>
<item>input</item>
<item>textarea</item>
<item>button</item>
<item>option</item>
<item>label</item>
<item>fieldset</item>
<item>legend</item>
<item>datalist</item>
<item>frame</item>
<item>map</item>
<item>area</item>
<item>canvas</item>
<item>picture</item>
<item>svg</item>
<item>audio</item>
<item>source</item>
<item>track</item>
<item>video</item>
<item>link</item>
<item>nav</item>
<item>ul</item>
<item>ol</item>
<item>li</item>
<item>table</item>
<item>caption</item>
<item>th</item>
<item>tr</item>
<item>td</item>
<item>thead</item>
<item>tbody</item>
<item>tfooter</item>
<item>col</item>
<item>span</item>
<item>main</item>
<item>article</item>
<item>aside</item>
<item>meta</item>
<item>base</item>
<item>noscript</item>
<item>object</item>
<item>param</item>
<item>src</item>
<item>href</item>
</string-array>
<!--XML Keywords List-->
<string-array name="xml_keywords">
<item><![CDATA[<xml]]></item>
<item>encoding</item>
<item>version</item>
</string-array>
<!--PHP Keywords List-->
<string-array name="php_keywords">
<item>php</item>
<item><![CDATA[<?php]]></item>
<item>__construct</item>
<item>var_dump</item>
<item>define</item>