Skip to content

Commit

Permalink
refactor: Improve KotlinLanguage class
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq committed Mar 11, 2023
1 parent 6d34fd5 commit 6ba4ac7
Showing 1 changed file with 24 additions and 15 deletions.
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()
}
}

0 comments on commit 6ba4ac7

Please sign in to comment.