Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
fix æ appearing in diagnostics, add proper messages to kotlin diagn…
Browse files Browse the repository at this point in the history
…ostics and upgrade kotlin-stdlib

Signed-off-by: PranavPurwar <[email protected]>
  • Loading branch information
PranavPurwar committed Jul 31, 2023
1 parent 9909c3d commit f9c1297
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 107 deletions.
Binary file removed app/src/main/assets/kotlin-stdlib-1.8.0.jar
Binary file not shown.
Binary file added app/src/main/assets/kotlin-stdlib-1.9.0.jar
Binary file not shown.
Binary file not shown.
121 changes: 65 additions & 56 deletions app/src/main/kotlin/org/cosmicide/rewrite/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package org.cosmicide.rewrite

import android.app.Application
import android.app.UiModeManager
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.StrictMode
Expand All @@ -33,8 +32,9 @@ import org.cosmicide.rewrite.plugin.api.Hook
import org.cosmicide.rewrite.plugin.api.HookManager
import org.cosmicide.rewrite.plugin.api.PluginLoader
import org.cosmicide.rewrite.util.FileUtil
import org.cosmicide.rewrite.util.MultipleDexClassLoader
import org.cosmicide.rewrite.util.then
import org.eclipse.tm4e.core.registry.IThemeSource
import org.jetbrains.kotlin.utils.addToStdlib.ifTrue
import org.lsposed.hiddenapibypass.HiddenApiBypass
import java.io.File
import java.io.FileNotFoundException
Expand All @@ -50,18 +50,12 @@ class App : Application() {
*/
@JvmStatic
lateinit var instance: WeakReference<App>

/**
* The class loader used to load plugins.
*/
@JvmStatic
val loader = MultipleDexClassLoader.INSTANCE
}

override fun onCreate() {
super.onCreate()
instance = WeakReference(this)
HookManager.context = instance as WeakReference<Context>
HookManager.context = WeakReference(this)

CrashConfig.Builder.create()
.showRestartButton(true)
Expand All @@ -73,43 +67,10 @@ class App : Application() {
Prefs.init(applicationContext)
FileUtil.init(externalStorage)

setupHooks()
loadPlugins()

// Some libraries may call System.exit() to exit the app, which crashes the app.
// Currently, only JGit does this.
HookManager.registerHook(object : Hook(
method = "exit",
argTypes = arrayOf(Int::class.java),
type = System::class.java
) {
override fun before(param: XC_MethodHook.MethodHookParam) {
System.err.println("System.exit() called!")
param.result = null
}
})

// Fix crash in ViewPager2
HookManager.registerHook(object : Hook(
method = "onLayoutChildren",
argTypes = arrayOf(RecyclerView.Recycler::class.java, RecyclerView.State::class.java),
type = LinearLayoutManager::class.java
) {
override fun before(param: XC_MethodHook.MethodHookParam) {
try {
HookManager.invokeOriginal(
param.method,
param.thisObject,
param.args[0],
param.args[1]
)
} catch (e: Exception) {
e.printStackTrace()
}
param.result = null
}
})

if (BuildConfig.DEBUG) {
BuildConfig.DEBUG.then {
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder().apply {
detectLeakedRegistrationObjects()
Expand All @@ -134,9 +95,8 @@ class App : Application() {
)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
HiddenApiBypass.addHiddenApiExemptions("Lsun/misc/Unsafe;")
}
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
.then(HiddenApiBypass.addHiddenApiExemptions("Lsun/misc/Unsafe;"))

DynamicColors.applyToActivitiesIfAvailable(this)

Expand All @@ -160,9 +120,8 @@ class App : Application() {
Analytics.setAnalyticsCollectionEnabled(Prefs.analyticsEnabled)
val theme = getTheme(Prefs.appTheme)
val uiModeManager = getSystemService(UiModeManager::class.java)
if (uiModeManager.nightMode == theme) {
return
}
if (uiModeManager.nightMode == theme) return

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
uiModeManager.setApplicationNightMode(theme)
} else {
Expand All @@ -178,17 +137,25 @@ class App : Application() {
}
}

/**
* Extracts kotlin stdlib and stdlib-common from assets.
*/
fun extractFiles() {
FileUtil.classpathDir.resolve("kotlin-stdlib-1.8.0.jar").apply {
exists().then { delete() }
}
extractAsset(
"kotlin-stdlib-1.8.0.jar",
FileUtil.classpathDir.resolve("kotlin-stdlib-1.8.0.jar")
"kotlin-stdlib-1.9.0.jar",
FileUtil.classpathDir.resolve("kotlin-stdlib-1.9.0.jar")
)
extractAsset(
"kotlin-stdlib-common-1.9.0.jar",
FileUtil.classpathDir.resolve("kotlin-stdlib-common-1.9.0.jar")
)
}

fun extractAsset(assetName: String, targetFile: File) {
if (targetFile.exists()) {
return
}
targetFile.exists().ifTrue { return }
try {
assets.open(assetName).use { inputStream ->
targetFile.outputStream().use { outputStream ->
Expand Down Expand Up @@ -217,10 +184,52 @@ class App : Application() {
applyThemeBasedOnConfiguration()
}

private fun setupHooks() {

// Some libraries may call System.exit() to exit the app, which crashes the app.
// Currently, only JGit does this.
HookManager.registerHook(object : Hook(
method = "exit",
argTypes = arrayOf(Int::class.java),
type = System::class.java
) {
override fun before(param: XC_MethodHook.MethodHookParam) {
System.err.println("System.exit() called!")
// Setting result to null bypasses the original method call.
param.result = null
}
})

// Fix crash in ViewPager2
HookManager.registerHook(object : Hook(
method = "onLayoutChildren",
argTypes = arrayOf(RecyclerView.Recycler::class.java, RecyclerView.State::class.java),
type = LinearLayoutManager::class.java
) {
override fun before(param: XC_MethodHook.MethodHookParam) {
try {
// Call the original method.
HookManager.invokeOriginal(
param.method,
param.thisObject,
param.args[0],
param.args[1]
)
} catch (e: Exception) {
e.printStackTrace()
}
// Bypass method call as we have already called the original method.
param.result = null
}
})


}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
applyThemeBasedOnConfiguration()
setTheme(Prefs.appAccent.toInt())
applyThemeBasedOnConfiguration()
}

fun applyThemeBasedOnConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.cosmicide.project.Project
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import java.io.File

/**
Expand All @@ -45,24 +45,16 @@ class KotlinLanguage(
themeRegistry
) {
val kotlinEnvironment: KotlinEnvironment = KotlinEnvironment.get(project)
private var fileName: String = file.name

init {
try {
val ktFile =
kotlinEnvironment.updateKotlinFile(file.absolutePath, editor.text.toString())
fileName = ktFile.name
} catch (e: Exception) {
Log.e(TAG, "Failed to update Kotlin file", e)
}
editor.post {
editor.diagnostics = DiagnosticsContainer()
}
kotlinEnvironment.addIssueListener {
if (it == null) return@addIssueListener
val severity = when (it.severity) {
Severity.ERROR -> DiagnosticRegion.SEVERITY_ERROR
Severity.WARNING -> DiagnosticRegion.SEVERITY_WARNING
CompilerMessageSeverity.ERROR -> DiagnosticRegion.SEVERITY_ERROR
CompilerMessageSeverity.WARNING, CompilerMessageSeverity.STRONG_WARNING -> DiagnosticRegion.SEVERITY_WARNING
else -> return@addIssueListener
}
editor.post {
Expand All @@ -80,7 +72,7 @@ class KotlinLanguage(
CoroutineScope(Dispatchers.IO).launch {
kotlinEnvironment.analysisOf(kotlinEnvironment.kotlinFiles.map {
it.value.kotlinFile
}, kotlinEnvironment.kotlinFiles[fileName]!!.kotlinFile)
}, kotlinEnvironment.kotlinFiles[file.absolutePath]!!.kotlinFile)
}
}

Expand All @@ -97,7 +89,7 @@ class KotlinLanguage(
editor.diagnostics = DiagnosticsContainer()
}
val text = editor.text.toString()
val ktFile = kotlinEnvironment.updateKotlinFile(fileName, text)
val ktFile = kotlinEnvironment.updateKotlinFile(file.absolutePath, text)
val itemList = ktFile.let {
kotlinEnvironment.complete(
it, position.line, position.column
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/kotlin/org/cosmicide/rewrite/util/ext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* This file is part of Cosmic IDE.
* Cosmic IDE is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Cosmic IDE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*/

package org.cosmicide.rewrite.util

import org.jetbrains.kotlin.utils.addToStdlib.ifTrue

fun Boolean.then(block: () -> Unit) {
this.ifTrue(block)
}

fun Boolean.then(smth: Any) {
this.ifTrue { smth }
}
Loading

0 comments on commit f9c1297

Please sign in to comment.