Skip to content

Improve PlaintextSyntaxHighlighter #2560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ dependencies {
exclude group: 'commons-beanutils', module: 'commons-beanutils'
}



// UI libs
implementation 'com.github.Pixplicity:gene-rate:v1.1.8'
implementation 'com.github.AppIntro:AppIntro:6.2.0'
Expand All @@ -150,6 +148,9 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${version_plugin_kotlin}"
}

// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation 'com.google.code.gson:gson:2.10.1'

// Processors
def anpros = []
for (anpro in anpros) {
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/assets/highlight/languages/cpp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"language": "C/CPP",
"rules": [
{
"type": "KEYWORDS",
"regex": "\\b(typedef|inline|auto|class|explicit|extern|friend|inline|mutable|operator|register|template|private|protected|public|typeid|virtual|volatile|constexpr|nullptr|decltype)\\b"
},
{
"type": "KEYWORDS_CONTROL",
"regex": "\\b(goto|break|return|continue|asm|case|default|if|else|switch|while|for|do|sizeof|typeof|stdcall|cdecl|const_cast|delete|dynamic_cast|goto|namespace|new|pascal|reinterpret_cast|static_cast|this|throw|try|catch|using|true|false|bitand|and|bitor|or|xor|compl|and_eq|or_eq|xor_eq|not|not_eq|assert|static_assert)\\b"
},
{
"type": "TYPES",
"regex": "\\b(int|long|short|char|void|signed|unsigned|float|double|size_t|wchar_t|ptrdiff_t|sig_atomic_t|fpos_t|clock_t|time_t|va_list|jmp_buf|FILE|DIR|div_t|ldiv_t|static|mbstate_t|wctrans_t|wint_t|wctype_t|const|bool|complex|struct|union|enum|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|int_least8_t|int_least16_t|int_least32_t|int_least64_t|uint_least8_t|uint_least16_t|uint_least32_t|uint_least64_t|int_fast8_t|int_fast16_t|int_fast32_t|int_fast64_t|uint_fast8_t|uint_fast16_t|uint_fast32_t|uint_fast64_t|intptr_t|uintptr_t|intmax_t|uintmax_t)\\b"
},
{
"type": "NUMBER",
"regex": "\\b\\d+(\\.\\d*)?(e(\\+|\\-)?\\d+)?\\b"
},
{
"type": "STRING",
"regex": "(\".*\")|('.*')"
},
{
"type": "ESCAPE_CHAR",
"regex": "\\\\([0bfnrtv'\"]|u[0-9a-fA-F]{4}|[0-7]{3})"
},
{
"type": "LINE_COMMENT",
"regex": "//.*"
},
{
"type": "BLOCK_COMMENT",
"regex": "/\\*[\\s\\S]*?\\*/"
},
{
"type": "FUNCTION",
"regex": "([a-zA-Z0-9_$]+)(?=\\()"
}
]
}
49 changes: 49 additions & 0 deletions app/src/main/assets/highlight/languages/java.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"language": "Java",
"rules": [
{
"type": "KEYWORDS",
"regex": "\\b(abstract|assert|class|do|enum|extends|final|implements|import|interface|instanceof|native|new|package|private|protected|public|static|strictfp|super|synchronized|this|throw|throws|transient|void|volatile)\\b"
},
{
"type": "KEYWORDS_CONTROL",
"regex": "\\b(if|else|while|for|switch|case|default|continue|break|return|try|catch|finally|goto|const)\\b"
},
{
"type": "TYPES",
"regex": "\\b(byte|short|char|int|long|float|double|boolean)\\b"
},
{
"type": "NUMBER",
"regex": "\\b\\d+(\\.\\d*)?(e(\\+|\\-)?\\d+)?\\b"
},
{
"type": "STRING",
"regex": "(\".*\")|('.*')"
},
{
"type": "ESCAPE_CHAR",
"regex": "\\\\([0bfnrtv'\"]|u[0-9a-fA-F]{4}|[0-7]{3})"
},
{
"type": "CONSTANT",
"regex": "\\b(false|null|true|[A-Z_]+)\\b"
},
{
"type": "LINE_COMMENT",
"regex": "//.*"
},
{
"type": "BLOCK_COMMENT",
"regex": "/\\*[\\s\\S]*?\\*/"
},
{
"type": "CLASS",
"regex": "\\b([A-Z]\\w*)(?=[\\.\\[<>]|(\\s+\\w+\\s*=))\\b"
},
{
"type": "FUNCTION",
"regex": "([a-zA-Z0-9_$]+)(?=\\()"
}
]
}
4 changes: 4 additions & 0 deletions app/src/main/assets/highlight/languages/map.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
c=cpp
cpp=cpp
java=java
py=python
45 changes: 45 additions & 0 deletions app/src/main/assets/highlight/languages/python.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"language": "Python",
"rules": [
{
"type": "KEYWORDS",
"regex": "\\b(bool|enumerate|set|frozenset|help|reversed|sorted|sum|Ellipsis|NotImplemented|__import__|abs|apply|buffer|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|eval|execfile|file|filter|float|getattr|globals|hasattr|hash|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|min|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|round|setattr|slice|staticmethod|str|super|tuple|type|unichr|unicode|vars|xrange|zip)\\b"
},
{
"type": "KEYWORDS_CONTROL",
"regex": "\\b(break|continue|del|except|exec|finally|pass|print|raise|return|try|global|assert|lambda|yield|def|class|for|while|if|elif|else|and|in|is|not|or|import|from|as)\\b"
},
{
"type": "TYPES",
"regex": "\\b(byte|short|char|int|long|float|double|boolean)\\b"
},
{
"type": "NUMBER",
"regex": "\\b\\d+(\\.\\d*)?(e(\\+|\\-)?\\d+)?\\b"
},
{
"type": "STRING",
"regex": "(\".+\")|('.+')"
},
{
"type": "ESCAPE_CHAR",
"regex": "\\\\([0bfnrtv'\"]|u[0-9a-fA-F]{4}|[0-7]{3})"
},
{
"type": "CONSTANT",
"regex": "\\b(None|True|False)\\b"
},
{
"type": "LINE_COMMENT",
"regex": "#.*"
},
{
"type": "BLOCK_COMMENT",
"regex": "('''(.*?)''')|(\"{3}(.*?)\"{3})"
},
{
"type": "FUNCTION",
"regex": "([a-zA-Z0-9_$]+)(?=\\()"
}
]
}
44 changes: 44 additions & 0 deletions app/src/main/assets/highlight/themes/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "default",
"styles": {
"FOREGROUND": {
"color": "#000000"
},
"BACKGROUND": {
"color": "#FFFFFF"
},
"KEYWORDS": {
"color": "#008AD8"
},
"KEYWORDS_CONTROL": {
"color": "#C100DB"
},
"TYPES": {
"color": "#1339FF"
},
"NUMBER": {
"color": "#00A600"
},
"STRING": {
"color": "#C36A2B"
},
"ESCAPE_CHAR": {
"color": "#1339FF"
},
"CONSTANT": {
"color": "#FA4616"
},
"LINE_COMMENT": {
"color": "#8F8F88"
},
"BLOCK_COMMENT": {
"color": "#8F8F88"
},
"CLASS": {
"color": "#F2827F"
},
"FUNCTION": {
"color": "#BE8126"
}
}
}
17 changes: 9 additions & 8 deletions app/src/main/java/net/gsantner/markor/format/FormatRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,20 @@ public static FormatRegistry getFormat(int formatId, @NonNull final Context cont
final AppSettings appSettings = ApplicationObject.settings();

switch (formatId) {
case FORMAT_CSV: {
format._converter = CONVERTER_CSV;
format._highlighter = new CsvSyntaxHighlighter(appSettings);

// TODO k3b ????
case FORMAT_PLAIN: {
format._converter = CONVERTER_PLAINTEXT;
format._highlighter = new PlaintextSyntaxHighlighter(appSettings, document.extension);
// Should implement code action buttons for PlaintextActionButtons
format._textActions = new PlaintextActionButtons(context, document);
format._autoFormatInputFilter = new AutoTextFormatter(MarkdownReplacePatternGenerator.formatPatterns);
format._autoFormatTextWatcher = new ListHandler(MarkdownReplacePatternGenerator.formatPatterns);
break;
}
case FORMAT_PLAIN: {
format._converter = CONVERTER_PLAINTEXT;
format._highlighter = new PlaintextSyntaxHighlighter(appSettings);
case FORMAT_CSV: {
format._converter = CONVERTER_CSV;
format._highlighter = new CsvSyntaxHighlighter(appSettings);

// TODO k3b ????
format._textActions = new PlaintextActionButtons(context, document);
format._autoFormatInputFilter = new AutoTextFormatter(MarkdownReplacePatternGenerator.formatPatterns);
format._autoFormatTextWatcher = new ListHandler(MarkdownReplacePatternGenerator.formatPatterns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,59 @@
*
#########################################################*/

import android.graphics.Paint;

import net.gsantner.markor.format.plaintext.highlight.HighlightConfigLoader;
import net.gsantner.markor.format.plaintext.highlight.Syntax;
import net.gsantner.markor.format.plaintext.highlight.Theme;
import net.gsantner.markor.frontend.textview.SyntaxHighlighterBase;
import net.gsantner.markor.model.AppSettings;

import java.util.ArrayList;
import java.util.HashMap;

public class PlaintextSyntaxHighlighter extends SyntaxHighlighterBase {
public final static HighlightConfigLoader configLoader = new HighlightConfigLoader();
private ArrayList<Syntax.Rule> rules;
private HashMap<String, Theme.Style> styles;

public PlaintextSyntaxHighlighter(AppSettings appSettings) {
super(appSettings);
}

public PlaintextSyntaxHighlighter(AppSettings appSettings, String extension) {
super(appSettings);

Syntax syntax = configLoader.getSyntax(extension, appSettings.getContext());
if (syntax != null) {
rules = syntax.getRules();
Theme theme = configLoader.getTheme(appSettings.getContext(), "default");
if (theme != null) {
styles = theme.getStyles();
}
}
}

public PlaintextSyntaxHighlighter(AppSettings as) {
super(as);
@Override
public SyntaxHighlighterBase configure(Paint paint) {
return super.configure(paint);
}

@Override
protected void generateSpans() {
createTabSpans(_tabSize);
createUnderlineHexColorsSpans();
createSmallBlueLinkSpans();
}

}
if (rules == null || styles == null) {
return;
}

for (Syntax.Rule rule : rules) {
Theme.Style style = styles.get(rule.getType());
if (style != null) {
createColorSpanForMatches(rule.getPattern(), style.getColor());
}
}
}
}
Loading