Skip to content

Commit

Permalink
refactor: Improve JavaLanguage class
Browse files Browse the repository at this point in the history
Use Kotlin null safety: The `it.detail` property in the `forEach` loop may be null, so it would be better to use Kotlin's null safety features (e.g. the `?.` operator) to avoid potential NullPointerExceptions.

Use Kotlin string templates: Instead of concatenating strings using the `+` operator, it would be cleaner to use Kotlin's string templates feature.
  • Loading branch information
aikrq committed Mar 11, 2023
1 parent 56255d3 commit 6d34fd5
Showing 1 changed file with 24 additions and 21 deletions.
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()
}
}

0 comments on commit 6d34fd5

Please sign in to comment.