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

small changes #1

Merged
merged 15 commits into from
Mar 11, 2023
Merged
92 changes: 57 additions & 35 deletions app/src/main/java/org/cosmicide/rewrite/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cosmicide.rewrite

import android.app.Application
import android.util.Log
import com.itsaky.androidide.config.JavacConfigProvider
import io.github.rosemoe.sora.langs.textmate.registry.FileProviderRegistry
import io.github.rosemoe.sora.langs.textmate.registry.GrammarRegistry
Expand All @@ -9,56 +10,77 @@ import io.github.rosemoe.sora.langs.textmate.registry.model.ThemeModel
import io.github.rosemoe.sora.langs.textmate.registry.provider.AssetsFileResolver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.cosmicide.rewrite.util.FileUtil
import org.eclipse.tm4e.core.registry.IThemeSource
import java.io.File
import java.io.FileNotFoundException

class App : Application() {

private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

private lateinit var indexFile: File

override fun onCreate() {
super.onCreate()
FileUtil.init(this)
CoroutineScope(Dispatchers.IO).launch {
indexFile = File(FileUtil.dataDir, INDEX_FILE_NAME)
scope.launch {
try {
disableModules()
loadTextmateTheme()
extractFiles()
} catch (e: Throwable) {
Log.e("App", "Initialization failed: $e")
}
}
}

private suspend fun extractFiles() {
withContext(Dispatchers.IO) {
assets.open(INDEX_FILE_NAME).use { input ->
indexFile.outputStream().use { output ->
input.copyTo(output)
}
}
}
}

private suspend fun disableModules() {
withContext(Dispatchers.IO) {
JavacConfigProvider.disableModules()
loadTextmateTheme()
extractFiles()
}
}

private fun extractFiles() {
File(FileUtil.dataDir, "index.json").writeBytes(assets.open("index.json").readBytes())
private suspend fun loadTextmateTheme() {
withContext(Dispatchers.IO) {
val fileProvider = AssetsFileResolver(assets)
FileProviderRegistry.getInstance().addFileProvider(fileProvider)
GrammarRegistry.getInstance().loadGrammars(LANGUAGES_FILE_PATH)
val themeRegistry = ThemeRegistry.getInstance()
themeRegistry.loadTheme(loadTheme(DARCULA_THEME_FILE_NAME, DARCULA_THEME_NAME))
themeRegistry.loadTheme(loadTheme(QUIET_LIGHT_THEME_FILE_NAME, QUIET_LIGHT_THEME_NAME))
themeRegistry.setTheme(QUIET_LIGHT_THEME_NAME)
}
}

private fun loadTheme(fileName: String, themeName: String): ThemeModel {
val inputStream = FileProviderRegistry.getInstance().tryGetInputStream("$TEXTMATE_DIR/$fileName")
?: throw FileNotFoundException("Theme file not found: $fileName")
val source = IThemeSource.fromInputStream(inputStream, fileName, null)
return ThemeModel(source, themeName)
}

private fun loadTextmateTheme() {
FileProviderRegistry.getInstance().addFileProvider(
AssetsFileResolver(
assets
)
)
GrammarRegistry.getInstance().loadGrammars("textmate/languages.json")
val themeRegistry = ThemeRegistry.getInstance()
themeRegistry.loadTheme(
ThemeModel(
IThemeSource.fromInputStream(
FileProviderRegistry.getInstance().tryGetInputStream("textmate/darcula.json"),
"darcula.json",
null
),
"darcula"
)
)
themeRegistry.loadTheme(
ThemeModel(
IThemeSource.fromInputStream(
FileProviderRegistry.getInstance()
.tryGetInputStream("textmate/QuietLight.tmTheme"),
"QuietLight.tmTheme",
null
),
"QuietLight"
)
)
themeRegistry.setTheme("QuietLight")
companion object {
private const val INDEX_FILE_NAME = "index.json"
private const val LANGUAGES_FILE_PATH = "textmate/languages.json"
private const val TEXTMATE_DIR = "textmate"
private const val DARCULA_THEME_FILE_NAME = "darcula.json"
private const val DARCULA_THEME_NAME = "darcula"
private const val QUIET_LIGHT_THEME_FILE_NAME = "QuietLight.tmTheme"
private const val QUIET_LIGHT_THEME_NAME = "QuietLight"
}
}
72 changes: 20 additions & 52 deletions app/src/main/java/org/cosmicide/rewrite/adapter/ProjectAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,40 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import org.cosmicide.project.Project
import org.cosmicide.rewrite.databinding.ProjectItemBinding

class ProjectAdapter : RecyclerView.Adapter<ProjectAdapter.ViewHolder>() {
private val mProjects = mutableListOf<Project>()
import kotlin.properties.Delegates

class ProjectAdapter(private val listener: OnProjectEventListener) : RecyclerView.Adapter<ProjectAdapter.ViewHolder>() {
private var projects: List<Project> by Delegates.observable(emptyList()) { _, oldList, newList ->
DiffUtil.calculateDiff(object : DiffUtil.Callback() {
override fun getOldListSize() = oldList.size
override fun getNewListSize() = newList.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int) =
oldList[oldItemPosition] == newList[newItemPosition]
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int) =
oldList[oldItemPosition] == newList[newItemPosition]
}).dispatchUpdatesTo(this)
}

interface OnProjectEventListener {
fun onProjectClicked(project: Project)
fun onProjectLongClicked(project: Project): Boolean
}

lateinit var onProjectEventListener: OnProjectEventListener

fun submitList(projects: List<Project>) {
val diffResult = DiffUtil.calculateDiff(
object : DiffUtil.Callback() {
override fun getOldListSize(): Int {
return mProjects.size
}

override fun getNewListSize(): Int {
return projects.size
}

override fun areItemsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
return (
mProjects[oldItemPosition]
== projects[newItemPosition]
)
}

override fun areContentsTheSame(
oldItemPosition: Int,
newItemPosition: Int
): Boolean {
return (
mProjects[oldItemPosition]
== projects[newItemPosition]
)
}
})
mProjects.clear()
mProjects.addAll(projects)
diffResult.dispatchUpdatesTo(this)
this.projects = projects
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(
ProjectItemBinding.inflate(LayoutInflater.from(parent.context), parent, false),
onProjectEventListener
listener
)

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(mProjects[position])
holder.bind(projects[position])
}

override fun getItemCount() = mProjects.size
override fun getItemCount() = projects.size

class ViewHolder(
private val binding: ProjectItemBinding,
Expand All @@ -73,16 +49,8 @@ class ProjectAdapter : RecyclerView.Adapter<ProjectAdapter.ViewHolder>() {
fun bind(project: Project) {
binding.projectTitle.text = project.name
binding.projectPath.text = project.root.absolutePath
binding.root.setOnClickListener { _ ->
listener.onProjectClicked(
project
)
}
binding.root.setOnLongClickListener { _ ->
listener.onProjectLongClicked(
project
)
}
binding.root.setOnClickListener { listener.onProjectClicked(project) }
binding.root.setOnLongClickListener { listener.onProjectLongClicked(project) }
}
}
}
}
45 changes: 24 additions & 21 deletions app/src/main/java/org/cosmicide/rewrite/editor/JavaLanguage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,28 @@ import java.nio.file.Path
import java.util.logging.Level

class JavaLanguage(
private val mEditor: CodeEditor,
project: Project,
file: File
private val editor: CodeEditor,
private val project: Project,
private val file: File
) : TextMateLanguage(
GrammarRegistry.getInstance().findGrammar("source.java"),
GrammarRegistry.getInstance().findLanguageConfiguration("source.java"),
GrammarRegistry.getInstance(),
ThemeRegistry.getInstance(),
grammarRegistry.findGrammar("source.java"),
grammarRegistry.findLanguageConfiguration("source.java"),
grammarRegistry,
themeRegistry,
false
) {

private val completions: JavaCompletions by lazy { JavaCompletions() }
private val path: Path
private val TAG = "JavaLanguage"
private val path: Path = file.toPath()

init {
val options = JavaCompletionOptionsImpl(
project.binDir.absolutePath + File.pathSeparator + "autocomplete.log",
"${project.binDir.absolutePath}${File.pathSeparator}autocomplete.log",
Level.ALL,
emptyList(),
emptyList()
)
path = file.toPath()
completions.initialize(URI("file://" + project.root.absolutePath), options)
completions.initialize(URI("file://${project.root.absolutePath}"), options)
}

@WorkerThread
Expand All @@ -55,17 +53,16 @@ class JavaLanguage(
extraArguments: Bundle
) {
try {
val text = mEditor.text.toString()
val text = editor.text.toString()
Files.write(path, text.toByteArray())
val result =
completions.project.getCompletionResult(path, position.line, position.column)
result.completionCandidates.forEach {
if (it.name != "<error>") {
val result = completions.project.getCompletionResult(path, position.line, position.column)
result.completionCandidates.forEach { candidate ->
if (candidate.name != "<error>") {
val item = SimpleCompletionItem(
it.name,
it.detail.orElse("Unknown"),
candidate.name,
candidate.detail.orElse("Unknown"),
result.prefix.length,
it.name
candidate.name
)
publisher.addItem(item)
}
Expand All @@ -77,4 +74,10 @@ class JavaLanguage(
}
super.requireAutoComplete(content, position, publisher, extraArguments)
}
}

companion object {
private const val TAG = "JavaLanguage"
private val grammarRegistry = GrammarRegistry.getInstance()
private val themeRegistry = ThemeRegistry.getInstance()
}
}
39 changes: 24 additions & 15 deletions app/src/main/java/org/cosmicide/rewrite/editor/KotlinLanguage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ import org.cosmicide.project.Project
import java.io.File

class KotlinLanguage(
private val mEditor: CodeEditor,
project: Project,
file: File
private val editor: CodeEditor,
private val project: Project,
private val file: File
) : TextMateLanguage(
GrammarRegistry.getInstance().findGrammar("source.kotlin"),
GrammarRegistry.getInstance().findLanguageConfiguration("source.kotlin"),
GrammarRegistry.getInstance(),
ThemeRegistry.getInstance(),
grammarRegistry.findGrammar("source.kotlin"),
grammarRegistry.findLanguageConfiguration("source.kotlin"),
grammarRegistry,
themeRegistry,
false
) {

private val kotlinEnvironment: KotlinEnvironment by lazy { KotlinEnvironment.get(project) }
private val fileName: String
private val TAG = "KotlinLanguage"
private var fileName = file.name

init {
val ktFile = kotlinEnvironment.updateKotlinFile(
file.absolutePath, mEditor.text.toString()
)
fileName = ktFile.name
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)
}
}

@WorkerThread
Expand All @@ -45,7 +48,7 @@ class KotlinLanguage(
extraArguments: Bundle
) {
try {
val text = mEditor.text.toString()
val text = editor.text.toString()
val ktFile = kotlinEnvironment.updateKotlinFile(fileName, text)
val itemList = kotlinEnvironment.complete(
ktFile, position.line, position.column
Expand All @@ -58,4 +61,10 @@ class KotlinLanguage(
}
super.requireAutoComplete(content, position, publisher, extraArguments)
}
}

companion object {
private const val TAG = "KotlinLanguage"
private val grammarRegistry = GrammarRegistry.getInstance()
private val themeRegistry = ThemeRegistry.getInstance()
}
}
Loading