Skip to content

Commit b237db7

Browse files
committed
feat(git-completion): add asynchronous loading of git commits #101
This commit introduces asynchronous loading of git commits for RevisionReferenceLanguageProvider, improving performance and responsiveness. The old synchronous approach has been replaced with a coroutine-based solution, utilizing CompletableFuture and ProgressManager to load commits in the background without blocking the UI thread.
1 parent 42449f1 commit b237db7

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

exts/devin-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/RevisionReferenceLanguageProvider.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import com.intellij.codeInsight.completion.CompletionProvider
55
import com.intellij.codeInsight.completion.CompletionResultSet
66
import com.intellij.codeInsight.lookup.LookupElementBuilder
77
import com.intellij.icons.AllIcons
8-
import com.intellij.openapi.application.ReadAction
9-
import com.intellij.openapi.application.runReadAction
8+
import com.intellij.openapi.progress.ProgressIndicator
9+
import com.intellij.openapi.progress.ProgressManager
10+
import com.intellij.openapi.progress.Task
11+
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
1012
import com.intellij.openapi.progress.runBlockingCancellable
1113
import com.intellij.openapi.project.Project
1214
import com.intellij.util.ProcessingContext
1315
import git4idea.GitCommit
1416
import git4idea.history.GitHistoryUtils
1517
import git4idea.repo.GitRepositoryManager
18+
import kotlinx.coroutines.future.await
19+
import java.util.concurrent.CompletableFuture
1620

1721

1822
class RevisionReferenceLanguageProvider : CompletionProvider<CompletionParameters>() {
19-
20-
2123
override fun addCompletions(
2224
parameters: CompletionParameters,
2325
context: ProcessingContext,
@@ -27,17 +29,27 @@ class RevisionReferenceLanguageProvider : CompletionProvider<CompletionParameter
2729
val repository = GitRepositoryManager.getInstance(project).repositories.firstOrNull() ?: return
2830
val branchName = repository.currentBranchName
2931

30-
val commits: List<GitCommit> = ReadAction.compute<List<GitCommit>, Throwable> {
31-
return@compute GitHistoryUtils.history(project, repository.root, branchName)
32+
val future = CompletableFuture<List<GitCommit>>()
33+
val task = object : Task.Backgroundable(project, "loading git message", false) {
34+
override fun run(indicator: ProgressIndicator) {
35+
val commits: List<GitCommit> = GitHistoryUtils.history(project, repository.root, branchName)
36+
future.complete(commits)
37+
}
3238
}
3339

34-
commits.forEach {
35-
val element = LookupElementBuilder.create(it.id.toShortString())
36-
.withIcon(AllIcons.Vcs.Branch)
37-
.withPresentableText(it.fullMessage)
38-
.withTypeText(it.id.toShortString(), true)
40+
ProgressManager.getInstance()
41+
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
42+
43+
runBlockingCancellable {
44+
val commits = future.await()
45+
commits.forEach {
46+
val element = LookupElementBuilder.create(it.id.toShortString())
47+
.withIcon(AllIcons.Vcs.Branch)
48+
.withPresentableText(it.fullMessage)
49+
.withTypeText(it.id.toShortString(), true)
3950

40-
result.addElement(element)
51+
result.addElement(element)
52+
}
4153
}
4254
}
4355
}

0 commit comments

Comments
 (0)