Skip to content

Commit

Permalink
fix(completion): improve performance by using ReadAction and runBlock…
Browse files Browse the repository at this point in the history
…ingCancellable #101

Previously, the completion provider was using a background task to load git commits, which could slow down the completion process. This commit optimizes the performance by using ReadAction to perform the commit loading operation in a non-blocking way, and runBlockingCancellable to ensure that the operation can be cancelled if necessary. This change should result in faster and more responsive completion suggestions.
  • Loading branch information
phodal committed Mar 14, 2024
1 parent 33fea81 commit 42449f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Expand Up @@ -5,9 +5,9 @@ import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.icons.AllIcons
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.Task
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.progress.runBlockingCancellable
import com.intellij.openapi.project.Project
import com.intellij.util.ProcessingContext
import git4idea.GitCommit
Expand All @@ -16,6 +16,8 @@ import git4idea.repo.GitRepositoryManager


class RevisionReferenceLanguageProvider : CompletionProvider<CompletionParameters>() {


override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
Expand All @@ -25,21 +27,17 @@ class RevisionReferenceLanguageProvider : CompletionProvider<CompletionParameter
val repository = GitRepositoryManager.getInstance(project).repositories.firstOrNull() ?: return
val branchName = repository.currentBranchName

try {
object : Task.Backgroundable(project, "loading git message", false) {
override fun run(indicator: ProgressIndicator) {
val commits: List<GitCommit> = GitHistoryUtils.history(project, repository.root, branchName)
commits.forEach {
val element = LookupElementBuilder.create(it.id.toShortString())
.withIcon(AllIcons.Vcs.Branch)
.withTypeText(it.fullMessage, true)

result.addElement(element)
}
}
}.queue()
} catch (e: Exception) {
e.printStackTrace()
val commits: List<GitCommit> = ReadAction.compute<List<GitCommit>, Throwable> {
return@compute GitHistoryUtils.history(project, repository.root, branchName)
}

commits.forEach {
val element = LookupElementBuilder.create(it.id.toShortString())
.withIcon(AllIcons.Vcs.Branch)
.withPresentableText(it.fullMessage)
.withTypeText(it.id.toShortString(), true)

result.addElement(element)
}
}
}
@@ -0,0 +1,5 @@
/explain @symbol:cc.unitmesh.devti#RevProvider.constructor

/refactor @symbol:cc.unitmesh.devti#RevProvider.completions

/edit @file:Users/phodal/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/ideaIC/2023.3/eeff824a71d4b240b75e77296f74869ab1690c48/ideaIC-2023.3-sources.jar!/com/intellij/ide/presentation/VirtualFilePresentation.java #line:L1-L12

0 comments on commit 42449f1

Please sign in to comment.