Skip to content

Commit

Permalink
fix: speedup syntax highlighting for java files
Browse files Browse the repository at this point in the history
Tested-by: Pranav Purwar <[email protected]>
Signed-off-by: PranavPurwar <[email protected]>
  • Loading branch information
PranavPurwar committed Jan 21, 2024
1 parent 2ae0c98 commit a15b537
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

7 changes: 1 addition & 6 deletions app/src/main/kotlin/org/cosmicide/adapter/EditorAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DiffUtil
import androidx.viewpager2.adapter.FragmentStateAdapter
import io.github.rosemoe.sora.event.ContentChangeEvent
Expand All @@ -23,8 +22,6 @@ import io.github.rosemoe.sora.lang.EmptyLanguage
import io.github.rosemoe.sora.langs.textmate.TextMateColorScheme
import io.github.rosemoe.sora.langs.textmate.registry.ThemeRegistry
import io.github.rosemoe.sora.widget.subscribeEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.cosmicide.build.Javap
import org.cosmicide.common.Prefs
import org.cosmicide.databinding.EditorFragmentBinding
Expand Down Expand Up @@ -141,9 +138,7 @@ class EditorAdapter(val fragment: Fragment, val fileViewModel: FileViewModel) :
editor.setFont()
setColorScheme()
editor.isDisableSoftKbdIfHardKbdAvailable = true
lifecycleScope.launch(Dispatchers.IO) {
setEditorLanguage()
}
setEditorLanguage()
}

private fun setupSymbols() {
Expand Down
45 changes: 30 additions & 15 deletions app/src/main/kotlin/org/cosmicide/editor/language/TsLanguageJava.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import io.github.rosemoe.sora.widget.CodeEditor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.cosmicide.completion.java.parser.CompletionProvider
import org.cosmicide.project.Project
import org.cosmicide.common.Prefs
import org.cosmicide.completion.java.parser.CompletionProvider
import org.cosmicide.editor.EditorCompletionItem
import org.cosmicide.project.Project
import java.io.File
import java.net.URI
import java.util.logging.Level
Expand All @@ -45,7 +45,7 @@ class TsLanguageJava(

private lateinit var completionProvider: CompletionProvider

private val completions by lazy { JavaCompletions() }
private lateinit var completions: JavaCompletions
private val path = file.toPath()

init {
Expand All @@ -54,16 +54,18 @@ class TsLanguageJava(
completionProvider = CompletionProvider()
}
} else {
val options = JavaCompletionOptionsImpl(
"${project.binDir.absolutePath}/autocomplete.log",
Level.ALL,
emptyList(),
emptyList()
)
completions.initialize(URI("file://" + project.root.absolutePath), options)
completions.openFile(path, editor.text.toString())
completions = JavaCompletions()
CoroutineScope(Dispatchers.IO).launch {
val options = JavaCompletionOptionsImpl(
"${project.binDir.absolutePath}/autocomplete.log",
Level.ALL,
emptyList(),
emptyList()
)
completions.initialize(URI("file://" + project.root.absolutePath), options)
completions.openFile(path, editor.text.toString())
}
}

}


Expand All @@ -76,9 +78,14 @@ class TsLanguageJava(
super.requireAutoComplete(content, position, publisher, extraArguments)

try {
if (::completions.isInitialized && completions.mInitialized.not()) {
return
}

val text = editor.text.toString()
if (::completionProvider.isInitialized) {
val items = completionProvider.complete(text, "Main.java", position.index)

if (Prefs.experimentalJavaCompletion) {
val items = completionProvider.complete(text, file.name, position.index)
publisher.setComparator(Comparator<CompletionItem> { o1, o2 ->
// if the first letter of the label is lowercase, then its most likely a module/package
if (o1.label[0].isLowerCase() && o2.label[0].isUpperCase()) {
Expand Down Expand Up @@ -127,13 +134,21 @@ class TsLanguageJava(
}
}

override fun destroy() {
super.destroy()

if (Prefs.experimentalJavaCompletion.not()) {
completions.shutdown()
}
}


override val analyzer: TsAnalyzeManager
get() = super.analyzer

companion object {
private val TS_LANGUAGE_JAVA = TSLanguageJava.getInstance()
private val parser: TSParser = TSParser()
private val parser: TSParser = TSParser.create()

init {
parser.language = TS_LANGUAGE_JAVA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class JavaCompletions {

private static final String notInit = "Not yet initialized.";

private boolean mInitialized;
public boolean mInitialized;
private FileManager mFileManager;
private Project mProject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ public TypeReference getTypeReference(Tree node) {
scan(node, null);
if (names.isEmpty()) {
// Malformed input, no type can be referenced
logger.warning(new Throwable(), "Empty type name with %s", node);
if (node == null) {
logger.warning("Empty type name with null node");
} else {
logger.warning("Empty type name with %s", node);
}
return TypeReference.EMPTY_TYPE;
}
return TypeReference.builder()
Expand Down

0 comments on commit a15b537

Please sign in to comment.