|
1 | | -# CodeEditor |
2 | | -A professional code editor on Android device with highlight and auto completion. |
3 | | -***Work In Progress*** This project is still developing slowly because of school. It will update more frequently after July. |
4 | | -See Projects/CodeEditor to get more information about what I am working on. |
5 | | -Issues are welcome. |
6 | | -## Features Implemented Currently |
7 | | -* Syntax highlight |
8 | | -* Automatic completion |
9 | | -* Automatic Indent |
10 | | -* Code block lines |
11 | | -* Format code |
12 | | -* Scale text |
13 | | -* Select text |
14 | | -* Scroll freely, Scrollbars |
15 | | -* Undo/redo actions |
16 | | -## Language Supported |
17 | | -* S5droid(context sensitive auto completion,highlight,code block line,navigation) |
18 | | -* Java(Basic Support:highlight,code block line,identifier and keyword auto completion) |
19 | | -## Screenshots |
20 | | - |
21 | | - |
22 | | -## Extra Module Inside |
23 | | -* A man-made lexer |
24 | | -Language:Java,S5droid |
25 | | -## How to use this Editor |
26 | | -This project hasn't deploy to any place such as jcenter. |
27 | | -To include this project into your project: |
28 | | -* Copy files in src/assets/ to your project(If you do not want to use S5droid language,please go on to the next step) |
29 | | -* Copy files in src/res/layout to your project(Except src/res/layout/activity_main.xml) |
30 | | -* Copy files in src/java/ to your project(Except src/com/rose/editor/android/MainActivity.java) |
31 | | -* Change these files "import com.rose.editor.android.R" to "import yourPackageName.R" : |
32 | | -** src/java/com/rose/editor/android/AutoCompletePanel.java |
33 | | -** src/java/com/rose/editor/android/TextComposePanel.java |
34 | | -* Now you have finished all the steps! |
35 | | -## How to customize your language for editor |
36 | | -* Make a lexer for the language (Use JFlex or ANTLR to analyze code and highlight. JFlex is recommended) |
37 | | -[ANTLR Website](https://www.antlr.org/) |
38 | | -[ANTLR4 Repository](https://github.com/antlr/antlr4) |
39 | | -[Grammars for ANTLR4](https://github.com/antlr/grammars-v4) |
40 | | -[JFLex Website](https://jflex.de/) |
41 | | -[JFLex Repository](https://github.com/jflex-de/jflex) |
42 | | -* Implement a CodeAnalyzer: |
43 | | -```Java |
44 | | -public class XxxCodeAnalyzer { |
45 | | - |
46 | | - public void analyze(CharSequence content, TextAnalyzer.TextColors colors, TextAnalyzer.AnalyzeThread.Delegate delegate) { |
47 | | - //Analyze code here(Use your Lexer) |
48 | | - //Use colors.addIfNeeded() to add a new span if required |
49 | | - //Use colors.add() to add a new span forcely |
50 | | - //Use colors.addBlockLine() to add a new code block line |
51 | | - //Use colors.setNavigation() to specify navigation list |
52 | | - //Assign colors.mExtra to pass your information to AutoCompleteProvider module |
53 | | - |
54 | | - //Note that you should stop when delegate.shouldAnalyze() returns false. |
55 | | - //... |
56 | | - } |
57 | | - |
58 | | -} |
59 | | -``` |
60 | | -* Create an AutoCompleteProvider: |
61 | | -You have two choices: |
62 | | -A.Implement a AutoCompleteProvider by your self |
63 | | -```Java |
64 | | -public class XxxAutoComplete { |
65 | | - |
66 | | - public List<ResultItem> getAutoCompleteItems(String prefix, boolean isInCodeBlock, TextColorProvider.TextColors colors, int line) { |
67 | | - //Match items here |
68 | | - //How to match is up to you (contains/startsWith/toLowerCase...) |
69 | | - //You can get information from last analysis by colors.mExtra |
70 | | - //This value can be null |
71 | | - //... |
72 | | - |
73 | | - //You are unexpected to return null(May cause crash) |
74 | | - return ...; |
75 | | - } |
76 | | - |
77 | | -} |
78 | | -``` |
79 | | -B.Use a simple AutoCompleteProvider implementation -- IdentifierAutoComplete |
80 | | -This can only match your given word case insensitively by using startsWith(). |
81 | | -And you should use IdentifierAutoComplete.Identifiers as extra data from CodeAnalyzer or there will not be any effect. |
82 | | -Sample: |
83 | | -```Java |
84 | | -IdentifierAutoComplete autoComplete = new IdentifierAutoComplete(); |
85 | | -String[] keywords = new String[]{"public","private","protected"}; |
86 | | -//True if your all of keywords are in small case |
87 | | -autoComplete.setKeywords(keywords,true); |
88 | | -``` |
89 | | -And in your CodeAnalyzer: |
90 | | -```Java |
91 | | -public class XxxCodeAnalyzer { |
92 | | - |
93 | | - public void analyze(CharSequence content, TextAnalyzer.TextColors colors, TextAnalyzer.AnalyzeThread.Delegate delegate) { |
94 | | - XxxTokenizer tokenizer = ...; |
95 | | - ... |
96 | | - IdentifierAutoComplete.Identifiers identifiers = new IdentifierAutoComplete.Identifiers(); |
97 | | - identifiers.begin(); |
98 | | - while(...) { |
99 | | - switch(...) { |
100 | | - ... |
101 | | - case IDENTIFIER: |
102 | | - identifiers.addIdentifier(tokenizer.getTokenText()); |
103 | | - ... |
104 | | - break; |
105 | | - ... |
106 | | - } |
107 | | - } |
108 | | - identifiers.finish(); |
109 | | - colors.mExtra = identifiers; |
110 | | - ... |
111 | | - } |
112 | | - |
113 | | -} |
114 | | -``` |
115 | | -Note that repeat identifier will not be added. |
116 | | -And you must call begin() before you call addIdentifier(). |
117 | | -Remember to call finish() after all addIdentifier() actions done.(This will free some space) |
118 | | -* Implement your formatter (Optional) |
119 | | -Use lexer/tokenizer to create a formatter for your language. |
120 | | -* Implement EditorLanguage: |
121 | | -```Java |
122 | | -public class MyLanguage implements EditorLanguage { |
123 | | - |
124 | | - public CodeAnalyzer createAnalyzer() { |
125 | | - //NonNull or crash |
126 | | - return new XxxCodeAnalyzer(); |
127 | | - } |
128 | | - |
129 | | - public AutoCompleteProvider createAutoComplete() { |
130 | | - //NonNull or crash |
131 | | - return new XxxAutoComplete(); |
132 | | - //Or |
133 | | - IdentifierAutoComplete autoComplete = new IdentifierAutoComplete(); |
134 | | - String[] keywords = new String[]{"public","private","protected"}; |
135 | | - //True if your all of keywords are in small case |
136 | | - autoComplete.setKeywords(keywords,true); |
137 | | - return autoComplete; |
138 | | - } |
139 | | - |
140 | | - public boolean isAutoCompleteChar(char ch) { |
141 | | - //This is up to your. |
142 | | - //We will search character from the tail of insert position |
143 | | - //The default implementation is Character#isJavaIdentifierPart(char) |
144 | | - } |
145 | | - |
146 | | - public int getIndentAdvance(String content) { |
147 | | - //This is will be called when a newline is at start of input |
148 | | - //And you will receive text on current line before cursor |
149 | | - //The return value is counted as space |
150 | | - } |
151 | | - |
152 | | - public boolean useTab() { |
153 | | - //Whether we use tab to indent |
154 | | - //And how many spaces can tab replace is related to editor's settings |
155 | | - } |
156 | | - |
157 | | - public CharSequence format(CharSequence text) { |
158 | | - //Format code |
159 | | - //You should return formatted code |
160 | | - //If you can not,just return the given text |
161 | | - } |
162 | | -} |
163 | | -``` |
164 | | -* Apply your language |
165 | | -It's very simple: |
166 | | -```Java |
167 | | -editor.setEditorLanguage(yourLanguage); |
168 | | -``` |
169 | | -All things after will be done in a minute by editor |
| 1 | +# CodeEditor |
| 2 | +A professional code editor on Android device with highlight and auto completion. |
| 3 | +***Work In Progress*** This project is still developing slowly because of school. It will update more frequently after July. |
| 4 | +See Projects/CodeEditor to get more information about what I am working on. |
| 5 | +Issues are welcome. |
| 6 | +## Features Implemented Currently |
| 7 | +* Syntax highlight |
| 8 | +* Automatic completion |
| 9 | +* Automatic Indent |
| 10 | +* Code block lines |
| 11 | +* Format code |
| 12 | +* Scale text |
| 13 | +* Select text |
| 14 | +* Scroll freely, Scrollbars (with EdgeEffect) |
| 15 | +* Undo/redo actions |
| 16 | +* Search and replace text |
| 17 | +* Common Shortcuts |
| 18 | +## Language Supported |
| 19 | +* Java,JavaScript,C,C++(Basic Support:highlight,code block line,identifier and keyword auto completion) |
| 20 | +* S5droid(context sensitive auto completion,highlight,code block line,navigation) |
| 21 | +## Screenshots |
| 22 | + |
| 23 | + |
| 24 | +## Extra Module Inside |
| 25 | +* A man-made lexer for Java,S5droid |
| 26 | +* A simple style-able syntax lexer (Including comments, keywords and operators) |
| 27 | +## How to use this Editor |
| 28 | +This project hasn't deploy to any place such as jitpack. |
| 29 | +To include this project into your project: |
| 30 | +* Copy files in src/assets/ to your project(If you do not want to use S5droid language,please go on to the next step) |
| 31 | +* Copy files in src/res/layout to your project(Except src/res/layout/activity_main.xml) |
| 32 | +* Copy files in src/java/ to your project(Except src/com/rose/editor/android/MainActivity.java) |
| 33 | +* Change these files "import com.rose.editor.android.R" to "import yourPackageName.R" : |
| 34 | +** src/java/com/rose/editor/android/AutoCompletePanel.java |
| 35 | +** src/java/com/rose/editor/android/TextComposePanel.java |
| 36 | +* Now you have finished all the steps! |
| 37 | +## How to customize your language for editor |
| 38 | +### A Simple Method |
| 39 | +You just have to implement a `LanguageDescription`! |
| 40 | +See the interface in `/app/src/main/java/com/rose/editor/langs/universal/LanguageDescription` to get more information about it. |
| 41 | +There is also some implementations in `/app/src/main/java/com/rose/editor/langs/desc/`. You can view them and learn how to implement one by yourself. |
| 42 | +This way supports: Highlight(Keywords,Comments,Operators,Literals), Code block lines |
| 43 | +This method is quite simple for everyone, but it will have some limits that are annoying. |
| 44 | +If you want to get advanced features of CodeEditor, you'd better use the followig method. |
| 45 | +### Advanced Method - For experts |
| 46 | +* Make a lexer for the language (Use JFlex or ANTLR to analyze code and highlight. JFlex is recommended) |
| 47 | +[ANTLR Website](https://www.antlr.org/) |
| 48 | +[ANTLR4 Repository](https://github.com/antlr/antlr4) |
| 49 | +[Grammars for ANTLR4](https://github.com/antlr/grammars-v4) |
| 50 | +[JFLex Website](https://jflex.de/) |
| 51 | +[JFLex Repository](https://github.com/jflex-de/jflex) |
| 52 | +You may obtain a example flex for Java in ThisRepo/jflex/ |
| 53 | +* Implement a CodeAnalyzer: |
| 54 | +```Java |
| 55 | +public class XxxCodeAnalyzer { |
| 56 | + |
| 57 | + public void analyze(CharSequence content, TextAnalyzer.TextColors colors, TextAnalyzer.AnalyzeThread.Delegate delegate) { |
| 58 | + //Analyze code here(Use your Lexer) |
| 59 | + //Use colors.addIfNeeded() to add a new span if required |
| 60 | + //Use colors.add() to add a new span forcely |
| 61 | + //Use colors.addBlockLine() to add a new code block line |
| 62 | + //Use colors.setNavigation() to specify navigation list |
| 63 | + //Assign colors.mExtra to pass your information to AutoCompleteProvider module |
| 64 | + |
| 65 | + //Note that you should stop when delegate.shouldAnalyze() returns false. |
| 66 | + //... |
| 67 | + } |
| 68 | + |
| 69 | +} |
| 70 | +``` |
| 71 | +* Create an AutoCompleteProvider: |
| 72 | +You have two choices: |
| 73 | +A.Implement a AutoCompleteProvider by your self |
| 74 | +```Java |
| 75 | +public class XxxAutoComplete { |
| 76 | + |
| 77 | + public List<ResultItem> getAutoCompleteItems(String prefix, boolean isInCodeBlock, TextColorProvider.TextColors colors, int line) { |
| 78 | + //Match items here |
| 79 | + //How to match is up to you (contains/startsWith/toLowerCase...) |
| 80 | + //You can get information from last analysis by colors.mExtra |
| 81 | + //This value can be null |
| 82 | + //... |
| 83 | + |
| 84 | + //You are unexpected to return null(May cause crash) |
| 85 | + return ...; |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | +``` |
| 90 | +B.Use a simple AutoCompleteProvider implementation -- IdentifierAutoComplete |
| 91 | +This can only match your given word case insensitively by using startsWith(). |
| 92 | +And you should use IdentifierAutoComplete.Identifiers as extra data from CodeAnalyzer or there will not be any effect. |
| 93 | +Sample: |
| 94 | +```Java |
| 95 | +IdentifierAutoComplete autoComplete = new IdentifierAutoComplete(); |
| 96 | +String[] keywords = new String[]{"public","private","protected"}; |
| 97 | +//True if your all of keywords are in small case |
| 98 | +autoComplete.setKeywords(keywords,true); |
| 99 | +``` |
| 100 | +And in your CodeAnalyzer: |
| 101 | +```Java |
| 102 | +public class XxxCodeAnalyzer { |
| 103 | + |
| 104 | + public void analyze(CharSequence content, TextAnalyzer.TextColors colors, TextAnalyzer.AnalyzeThread.Delegate delegate) { |
| 105 | + XxxTokenizer tokenizer = ...; |
| 106 | + ... |
| 107 | + IdentifierAutoComplete.Identifiers identifiers = new IdentifierAutoComplete.Identifiers(); |
| 108 | + identifiers.begin(); |
| 109 | + while(...) { |
| 110 | + switch(...) { |
| 111 | + ... |
| 112 | + case IDENTIFIER: |
| 113 | + identifiers.addIdentifier(tokenizer.getTokenText()); |
| 114 | + ... |
| 115 | + break; |
| 116 | + ... |
| 117 | + } |
| 118 | + } |
| 119 | + identifiers.finish(); |
| 120 | + colors.mExtra = identifiers; |
| 121 | + ... |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | +``` |
| 126 | +Note that repeat identifier will not be added. |
| 127 | +And you must call begin() before you call addIdentifier(). |
| 128 | +Remember to call finish() after all addIdentifier() actions done.(This will free some space) |
| 129 | +* Implement your formatter (Optional) |
| 130 | +Use lexer/tokenizer to create a formatter for your language. |
| 131 | +* Implement EditorLanguage: |
| 132 | +```Java |
| 133 | +public class MyLanguage implements EditorLanguage { |
| 134 | + |
| 135 | + public CodeAnalyzer getAnalyzer() { |
| 136 | + //NonNull or crash |
| 137 | + return new XxxCodeAnalyzer(); |
| 138 | + } |
| 139 | + |
| 140 | + public AutoCompleteProvider getAutoCompleteProvider() { |
| 141 | + //NonNull or crash |
| 142 | + return new XxxAutoComplete(); |
| 143 | + //Or |
| 144 | + IdentifierAutoComplete autoComplete = new IdentifierAutoComplete(); |
| 145 | + String[] keywords = new String[]{"public","private","protected"}; |
| 146 | + //True if your all of keywords are in small case |
| 147 | + autoComplete.setKeywords(keywords,true); |
| 148 | + return autoComplete; |
| 149 | + } |
| 150 | + |
| 151 | + public boolean isAutoCompleteChar(char ch) { |
| 152 | + //This is up to your. |
| 153 | + //We will search character from the tail of insert position |
| 154 | + //The default implementation is Character#isJavaIdentifierPart(char) |
| 155 | + } |
| 156 | + |
| 157 | + public int getIndentAdvance(String content) { |
| 158 | + //This is will be called when a newline is at start of input |
| 159 | + //And you will receive text on current line before cursor |
| 160 | + //The return value is counted as space |
| 161 | + } |
| 162 | + |
| 163 | + public boolean useTab() { |
| 164 | + //Whether we use tab to indent |
| 165 | + //And how many spaces can tab replace is related to editor's settings |
| 166 | + } |
| 167 | + |
| 168 | + public CharSequence format(CharSequence text) { |
| 169 | + //Format code |
| 170 | + //You should return formatted code |
| 171 | + //If you can not,just return the given text |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | +* Apply your language |
| 176 | +It's very simple: |
| 177 | +```Java |
| 178 | +editor.setEditorLanguage(yourLanguage); |
| 179 | +``` |
| 180 | +All things after will be done in a minute by editor |
0 commit comments