[commit] extract recent commit messages contributor
[idea/community.git] / platform / vcs-impl / src / com / intellij / openapi / vcs / RecentMessagesCommitCompletionContributor.kt
1 package com.intellij.openapi.vcs
2
3 import com.intellij.codeInsight.completion.*
4 import com.intellij.codeInsight.lookup.LookupElementBuilder
5 import com.intellij.openapi.vcs.ui.CommitMessage
6 import com.intellij.psi.PsiDocumentManager
7 import com.intellij.ui.TextFieldWithAutoCompletionListProvider
8 import one.util.streamex.StreamEx
9
10 class RecentMessagesCommitCompletionContributor : CompletionContributor() {
11   override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
12     val file = parameters.originalFile
13     val project = file.project
14     val document = PsiDocumentManager.getInstance(project).getDocument(file) ?: return
15     if (document.getUserData(CommitMessage.DATA_KEY) == null) return
16     if (parameters.invocationCount == 0) return
17
18     result.caseInsensitive()
19       .withPrefixMatcher(PlainPrefixMatcher(TextFieldWithAutoCompletionListProvider.getCompletionPrefix(parameters)))
20       .addAllElements(
21         StreamEx.of(VcsConfiguration.getInstance(project).recentMessages)
22           .reverseSorted()
23           .map { lookupString: String ->
24             PrioritizedLookupElement.withPriority(LookupElementBuilder.create(lookupString), Int.MIN_VALUE.toDouble())
25           })
26   }
27 }