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

Maven Central
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
2
3
4
5
6
7
8
9
10
dependencies {
... // Other dependencies
// Add editor, which contains the basic widget
implementation 'io.github.Rosemoe.sora-editor:editor:<version>'
// Add languages, if you need them as not everyone wants to implement one by himself
implementation 'io.github.Rosemoe.sora-editor:language-java:<version>'
implementation 'io.github.Rosemoe.sora-editor:language-html:<version>'
implementation 'io.github.Rosemoe.sora-editor:language-python:<version>'
implementation 'io.github.Rosemoe.sora-editor:language-universal:<version>'
}

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
2
3
4
<io.github.rosemoe.sora.widget.CodeEditor
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/code_editor"/>

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
2
3
4
5
import io.github.rosemoe.sora.widget.CodeEditor;
import io.github.rosemoe.sora.lang.java.JavaLanguage;
//...
CodeEditor editor = (CodeEditor) findViewById(R.id.code_editor);
editor.setEditorLanguage(new JavaLanguage());

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
2
3
4
5
editor.setText("public class Main {\n    \n}");
//...
Content text = editor.getText();
// The result content should not be modified if you do not know what it means!
// Any change to the result can cause the composing text to be corrupted and probably a future crash of the editor

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.

Discuss

Telegram
QQ