Skip to content

Commit

Permalink
Merge pull request #580 from dingyi222666/main
Browse files Browse the repository at this point in the history
Add monarch support for sora-editor
  • Loading branch information
Rosemoe authored Dec 9, 2024
2 parents aff083d + cc7ba2a commit ff34828
Show file tree
Hide file tree
Showing 69 changed files with 7,773 additions and 41 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,3 @@ lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/

# Idea props
.idea/
/.idea/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/copyright/tm4e_EPL.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/scopes/tm4e_epl_licensed.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ dependencies {
implementation(projects.editor)
implementation(projects.languageJava)
implementation(projects.languageTextmate)
implementation(projects.languageMonarch)
implementation(projects.editorLsp)
implementation(projects.languageTreesitter)

// Tree-sitter languages
implementation(libs.tree.sitter.java)

// Monarch Languages
implementation(libs.monarch.language.pack)

// Kotlin coroutines
implementation(libs.kotlinx.coroutines)

Expand Down
202 changes: 166 additions & 36 deletions app/src/main/java/io/github/rosemoe/sora/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts.GetContent
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import io.github.dingyi222666.monarch.languages.JavaLanguage
import io.github.dingyi222666.monarch.languages.KotlinLanguage
import io.github.dingyi222666.monarch.languages.PythonLanguage
import io.github.dingyi222666.monarch.languages.TypescriptLanguage
import io.github.rosemoe.sora.app.databinding.ActivityMainBinding
import io.github.rosemoe.sora.app.tests.TestActivity
import io.github.rosemoe.sora.event.ContentChangeEvent
Expand All @@ -57,6 +61,11 @@ import io.github.rosemoe.sora.lang.TsLanguageJava
import io.github.rosemoe.sora.lang.diagnostic.DiagnosticRegion
import io.github.rosemoe.sora.lang.diagnostic.DiagnosticsContainer
import io.github.rosemoe.sora.langs.java.JavaLanguage
import io.github.rosemoe.sora.langs.monarch.MonarchColorScheme
import io.github.rosemoe.sora.langs.monarch.MonarchLanguage
import io.github.rosemoe.sora.langs.monarch.registry.MonarchGrammarRegistry
import io.github.rosemoe.sora.langs.monarch.registry.dsl.monarchLanguages
import io.github.rosemoe.sora.langs.monarch.registry.model.ThemeSource
import io.github.rosemoe.sora.langs.textmate.TextMateColorScheme
import io.github.rosemoe.sora.langs.textmate.TextMateLanguage
import io.github.rosemoe.sora.langs.textmate.registry.FileProviderRegistry
Expand Down Expand Up @@ -238,6 +247,10 @@ class MainActivity : AppCompatActivity() {

// Load textmate themes and grammars
setupTextmate()

// Load monarch themes and grammars
setupMonarch()

// Before using Textmate Language, TextmateColorScheme should be applied
ensureTextmateTheme()

Expand Down Expand Up @@ -303,15 +316,15 @@ class MainActivity : AppCompatActivity() {
applicationContext.assets // use application context
)
)
loadDefaultThemes()
loadDefaultLanguages()
loadDefaultTextMateThemes()
loadDefaultTextMateLanguages()
}


/**
* Load default textmate themes
*/
private /*suspend*/ fun loadDefaultThemes() /*= withContext(Dispatchers.IO)*/ {
private /*suspend*/ fun loadDefaultTextMateThemes() /*= withContext(Dispatchers.IO)*/ {
val themes = arrayOf("darcula", "abyss", "quietlight", "solarized_drak")
val themeRegistry = ThemeRegistry.getInstance()
themes.forEach { name ->
Expand All @@ -337,10 +350,78 @@ class MainActivity : AppCompatActivity() {
*
* @see loadDefaultLanguagesWithDSL Load by Kotlin DSL
*/
private /*suspend*/ fun loadDefaultLanguages() /*= withContext(Dispatchers.Main)*/ {
private /*suspend*/ fun loadDefaultTextMateLanguages() /*= withContext(Dispatchers.Main)*/ {
GrammarRegistry.getInstance().loadGrammars("textmate/languages.json")
}

/**
* Setup monarch. Load our grammars and themes from assets
*/
private fun setupMonarch() {
// Add assets file provider so that files in assets can be loaded
io.github.rosemoe.sora.langs.monarch.registry.FileProviderRegistry.addProvider(
io.github.rosemoe.sora.langs.monarch.registry.provider.AssetsFileResolver(
applicationContext.assets // use application context
)
)
loadDefaultMonarchThemes()
loadDefaultMonarchLanguages()
}


/**
* Load default monarch themes
*
*/
private /*suspend*/ fun loadDefaultMonarchThemes() /*= withContext(Dispatchers.IO)*/ {
val themes = arrayOf("darcula", "abyss", "quietlight", "solarized_drak")

themes.forEach { name ->
val path = "textmate/$name.json"
io.github.rosemoe.sora.langs.monarch.registry.ThemeRegistry.loadTheme(
io.github.rosemoe.sora.langs.monarch.registry.model.ThemeModel(
ThemeSource(path, name)
).apply {
if (name != "quietlight") {
isDark = true
}
}, false
)
}

io.github.rosemoe.sora.langs.monarch.registry.ThemeRegistry.setTheme("quietlight")
}

/**
* Load default languages from Monarch
*/
private fun loadDefaultMonarchLanguages() {
MonarchGrammarRegistry.INSTANCE.loadGrammars(
monarchLanguages {
language("java") {
monarchLanguage = JavaLanguage
defaultScopeName()
languageConfiguration = "textmate/java/language-configuration.json"
}
language("kotlin") {
monarchLanguage = KotlinLanguage
defaultScopeName()
languageConfiguration = "textmate/kotlin/language-configuration.json"
}
language("python") {
monarchLanguage = PythonLanguage
defaultScopeName()
languageConfiguration = "textmate/python/language-configuration.json"
}
language("typescript") {
monarchLanguage = TypescriptLanguage
defaultScopeName()
languageConfiguration = "textmate/javascript/language-configuration.json"
}
}
)
}

private fun loadDefaultLanguagesWithDSL() {
GrammarRegistry.getInstance().loadGrammars(
languages {
Expand Down Expand Up @@ -405,6 +486,20 @@ class MainActivity : AppCompatActivity() {
}
}

/**
* Ensure the editor uses a [MonarchColorScheme]
*/
private fun ensureMonarchTheme() {
val editor = binding.editor
var editorColorScheme = editor.colorScheme
if (editorColorScheme !is MonarchColorScheme) {
editorColorScheme =
MonarchColorScheme.create(io.github.rosemoe.sora.langs.monarch.registry.ThemeRegistry.currentTheme)
editor.colorScheme = editorColorScheme
switchThemeIfRequired(this, editor)
}
}

private fun generateKeybindingString(event: KeyBindingEvent): String {
val sb = StringBuilder()
if (event.isCtrlPressed) {
Expand Down Expand Up @@ -755,6 +850,10 @@ class MainActivity : AppCompatActivity() {
"TextMate MarkDown",
"TM Language from file",
"Tree-sitter Java",
"Monarch Java",
"Monarch Kotlin",
"Monarch Python",
"Monarch TypeScript",
"Text"
)
val tmLanguages = mapOf(
Expand All @@ -765,45 +864,76 @@ class MainActivity : AppCompatActivity() {
"TextMate JavaScript" to Pair("source.js", "source.js"),
"TextMate MarkDown" to Pair("text.html.markdown", "text.html.markdown")
)

val monarchLanguages = mapOf(
"Monarch Java" to "source.java",
"Monarch Kotlin" to "source.kotlin",
"Monarch Python" to "source.python",
"Monarch TypeScript" to "source.typescript"
)

AlertDialog.Builder(this)
.setTitle(R.string.switch_language)
.setSingleChoiceItems(languageOptions, -1) { dialog: DialogInterface, which: Int ->
val selected = languageOptions[which]
if (selected in tmLanguages) {
val info = tmLanguages[selected]!!
try {
ensureTextmateTheme()
val editorLanguage = editor.editorLanguage
val language = if (editorLanguage is TextMateLanguage) {
editorLanguage.updateLanguage(info.first)
editorLanguage
} else {
TextMateLanguage.create(info.second, true)
when (val selected = languageOptions[which]) {
in tmLanguages -> {
val info = tmLanguages[selected]!!
try {
ensureTextmateTheme()
val editorLanguage = editor.editorLanguage
val language = if (editorLanguage is TextMateLanguage) {
editorLanguage.updateLanguage(info.first)
editorLanguage
} else {
TextMateLanguage.create(info.second, true)
}
editor.setEditorLanguage(language)
} catch (e: Exception) {
e.printStackTrace()
}
editor.setEditorLanguage(language)
} catch (e: Exception) {
e.printStackTrace()
}
} else {
when (selected) {
"Java" -> editor.setEditorLanguage(JavaLanguage())
"Text" -> editor.setEditorLanguage(EmptyLanguage())
"TM Language from file" -> loadTMLLauncher.launch("*/*")
"Tree-sitter Java" -> {
editor.setEditorLanguage(
TsLanguageJava(
JavaLanguageSpec(
highlightScmSource = assets.open("tree-sitter-queries/java/highlights.scm")
.reader().readText(),
codeBlocksScmSource = assets.open("tree-sitter-queries/java/blocks.scm")
.reader().readText(),
bracketsScmSource = assets.open("tree-sitter-queries/java/brackets.scm")
.reader().readText(),
localsScmSource = assets.open("tree-sitter-queries/java/locals.scm")
.reader().readText()

in monarchLanguages -> {
val info = monarchLanguages[selected]!!

try {
ensureMonarchTheme()

val editorLanguage = editor.editorLanguage

val language = if (editorLanguage is MonarchLanguage) {
editorLanguage.updateLanguage(info)
editorLanguage
} else {
MonarchLanguage.create(info, true)
}
editor.setEditorLanguage(language)
} catch (e: Exception) {
e.printStackTrace()
}
}

else -> {
when (selected) {
"Java" -> editor.setEditorLanguage(JavaLanguage())
"Text" -> editor.setEditorLanguage(EmptyLanguage())
"TM Language from file" -> loadTMLLauncher.launch("*/*")
"Tree-sitter Java" -> {
editor.setEditorLanguage(
TsLanguageJava(
JavaLanguageSpec(
highlightScmSource = assets.open("tree-sitter-queries/java/highlights.scm")
.reader().readText(),
codeBlocksScmSource = assets.open("tree-sitter-queries/java/blocks.scm")
.reader().readText(),
bracketsScmSource = assets.open("tree-sitter-queries/java/brackets.scm")
.reader().readText(),
localsScmSource = assets.open("tree-sitter-queries/java/locals.scm")
.reader().readText()
)
)
)
)
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/io/github/rosemoe/sora/app/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import io.github.rosemoe.sora.langs.monarch.MonarchColorScheme
import io.github.rosemoe.sora.langs.textmate.TextMateColorScheme
import io.github.rosemoe.sora.langs.textmate.registry.ThemeRegistry
import io.github.rosemoe.sora.widget.CodeEditor
Expand All @@ -38,12 +39,16 @@ fun switchThemeIfRequired(context: Context, editor: CodeEditor) {
if ((context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) {
if (editor.colorScheme is TextMateColorScheme) {
ThemeRegistry.getInstance().setTheme("darcula")
} else if (editor.colorScheme is MonarchColorScheme) {
io.github.rosemoe.sora.langs.monarch.registry.ThemeRegistry.setTheme("darcula")
} else {
editor.colorScheme = SchemeDarcula()
}
} else {
if (editor.colorScheme is TextMateColorScheme) {
ThemeRegistry.getInstance().setTheme("quietlight")
} else if (editor.colorScheme is MonarchColorScheme) {
io.github.rosemoe.sora.langs.monarch.registry.ThemeRegistry.setTheme("quietlight")
} else {
editor.colorScheme = EditorColorScheme()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ private void initialize() {
styles.blocks = computeBlocks(shadowed, delegate);
styles.setSuppressSwitch(delegate.suppressSwitch);
styles.finishBuilding();

if (!abort)
sendNewStyles(styles);
}
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ gson = { module = "com.google.code.gson:gson", version = "2.11.0" }
jcodings = { module = "org.jruby.jcodings:jcodings", version = "1.0.58" }
joni = { module = "org.jruby.joni:joni", version = "2.2.1" }
snakeyaml-engine = { module = "org.snakeyaml:snakeyaml-engine", version = "2.7" }
moshi = { module = "com.squareup.moshi:moshi", version = "1.15.0" }
jdt-annotation = { module = "org.eclipse.jdt:org.eclipse.jdt.annotation", version = "2.3.0" }
monarch-code = { module = "io.github.dingyi222666.monarch:monarch", version = "1.0.3" }
monarch-language-pack = { module = "io.github.dingyi222666.monarch:monarch-language-pack", version = "1.0.2" }
monarch-json = { module = "io.github.dingyi222666.monarch:monarch-json-loader", version = "1.0.2" }
regex-onig = { module = "io.github.dingyi222666.regex-lib:regex-lib-oniguruma", version = "1.0.3" }
regex-re2j = { module = "io.github.dingyi222666.regex-lib:regex-lib-re2j", version = "1.0.2" }

tests-google-truth = { module = "com.google.truth:truth", version = "1.4.4" }
tests-robolectric = { module = "org.robolectric:robolectric", version = "4.13" }
Expand Down
1 change: 1 addition & 0 deletions language-monarch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Loading

0 comments on commit ff34828

Please sign in to comment.