Sora-editor - Get started
Pre
The project is fast-moving. This page may not be updated in time. The content of this page may be invalid for newest editor versions.
Introduction
sora-editor is a optimized editor library with a lot of features on Android.
It aims at making more people able to develop their own IDEs with less efforts. Also developing the editor is part of the interest of mine.
However its license is LGPL v2.1. Be careful about that.
As it is still developing and being tested, there may be some bugs and APIs can be easily changed at the stage.
Get Started
We use Android Gradle project for example.
Check your app environment
Platforms
There are lambdas(Java 8) and var
usages(Java 11) in CodeEditor’s source code, which requires you to use build tools with higher versions. Otherwise, you may meet some problems on old Android platforms telling that some static methods are not found, typically metafactory()
in java.lang.invoke.LambdaMetafactory
Here are the editor’s compiling tool versions:
- Gradle 7.0.2
- AGP(Android Gradle Plugin) 7.0.2
- build-tools version 31.0.0
SDK Version
CodeEditor’s minSdkVersion is 21. We have removed code to adapt older Android versions to avoid much more work.
And targetSdkVersion of editor is 31.
License
Make sure your app is consistent with our license LGPL v2.1
Add dependency
You just need to add dependencies to your Android app’s build.gradle
without adding other repos.
Adding the following lines to your app’s build.gradle
, and CodeEditor
widget can be used.
1 | dependencies { |
Pay attention to the version of editor. Watch our project on GitHub to know about our latest releases.
Don’t forget to click File
->Sync Project with Gradle files
after adding.
Add widget to your layout
There is no custom xml attributes for editor now. You must control it by Java code.
Typically, you should add the editor like this:
1 | <io.github.rosemoe.sora.widget.CodeEditor |
Note: The editor does not support measuring its content’s size, so it will just fill the available space when you use wrap_content
. So use either match_parent
or an exact size for the editor in your layout files.
Adjust editor language by code
Though the view is added, you will find that all texts are in black color without highlighting. That’s because you must specify a language for it in code. Otherwise, CodeEditor just uses the io.github.rosemoe.editor.lang.EmptyLanguage
.
Take language-java for example:
1 | import io.github.rosemoe.sora.widget.CodeEditor; |
And editor will update its text colors soon.
Note: You should always create a new language instance when using CodeEditor#setEditorLanguage(EditorLanguage)
, because the Language
is intended to work for exactly one editor. Otherwise, the result of the Language instance can be messed.
Tip: A language’s constructor is not necessary to have no parameters.
And set/get text…
1 | editor.setText("public class Main {\n \n}"); |
Some related problems
Here are some useful problems you may be interested in.
How to change text colors
Editor’s colors are maintained by IDs.
There are some built-in IDs in EditorColorScheme
. You may add new colors for your language to use by calling EditorColorScheme#setColor(int,int)
Get EditorColorScheme
object used by CodeEditor through CodeEditor#getColorScheme()
to update any color. However it is not recommed.
For a full update of editor’s theme, use CodeEditor#setColorScheme(EditorColorScheme)
.
You can extend EditorColorScheme
to define your own theme.
But note that you should always pass a new instance of EditorColorScheme
to this method. As EditorColorScheme
is designed to serve for only one editor. Otherwise, an IllegalStateException
may be thrown.
How to insert text in editor
Well. There is an API class for that. Though it is originally intended for symbol input view, you can insert any text by that. Turn to SymbolInputView in sample app for instance.
Turn to io.github.rosemoe.sora.widget.SymbolChannel
for more information
How to be informed when text changes
Use CodeEditor#setEventListener(EditorEventListener)
Ligatures
In some fonts, characters are display differently when they are together, such as ‘/‘ and ‘//‘. The space between them can be shrinked and causes the editor to display them wrongly.
This is caused by font ligatures. Ligatures of all types are disabled by the editor by default now.