Skip to content

Commit

Permalink
refactor: better handling of prefix in template completion
Browse files Browse the repository at this point in the history
  • Loading branch information
lppedd committed Apr 10, 2020
1 parent 24309e1 commit c6a9e60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/com/github/lppedd/cc/CCExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal fun Editor.getCurrentLine(): CharSequence {
return document.immutableCharSequence.subSequence(start, end)
}

@Suppress("unused")
internal fun Editor.getCurrentLineUntilCaret(): CharSequence {
val logicalPosition = caretModel.logicalPosition
val lineStartOffset = document.getLineStartOffset(logicalPosition.line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.github.lppedd.cc.completion.resultset.DelegateResultSet
import com.github.lppedd.cc.completion.resultset.TemplateDelegateResultSet
import com.github.lppedd.cc.configuration.CCConfigService
import com.github.lppedd.cc.configuration.CCConfigService.CompletionType.TEMPLATE
import com.github.lppedd.cc.lookupElement.INDEX_TYPE
import com.github.lppedd.cc.parser.CCParser
import com.github.lppedd.cc.parser.CommitContext.*
import com.github.lppedd.cc.parser.FooterContext.FooterTypeContext
Expand Down Expand Up @@ -76,20 +77,23 @@ private class CommitCompletionContributor : CompletionContributor() {

ProgressManager.checkCanceled()

val project = file.project
val configService = CCConfigService.getInstance(project)
val resultSet = result
.caseInsensitive()
.withPrefixMatcher(FlatPrefixMatcher(parameters.getCompletionPrefix()))
.withRelevanceSorter(sorter(CommitLookupElementWeigher))

val isTemplateActive = TemplateManagerImpl.getTemplateState(parameters.editor) != null
val editor = parameters.editor
val templateState = TemplateManagerImpl.getTemplateState(editor)
val isTemplateActive = templateState != null

val myResultSet = if (isTemplateActive) {
TemplateDelegateResultSet(resultSet)
} else {
DelegateResultSet(resultSet)
}

val project = file.project
val configService = CCConfigService.getInstance(project)
val process = parameters.process

// If the user configured commit messages to be completed via templates,
Expand All @@ -105,20 +109,33 @@ private class CommitCompletionContributor : CompletionContributor() {
return
}

val editor = parameters.editor
val caretModel = editor.caretModel
val (lineNumber, lineCaretOffset) = caretModel.logicalPosition
val lineUntilCaret = editor.getCurrentLineUntilCaret()
val document = editor.document
val caretLogicalPosition = editor.caretModel.logicalPosition
val caretLineNumber = caretLogicalPosition.line
var caretOffsetInLine = caretLogicalPosition.column
val lineStartOffset = document.getLineStartOffset(caretLineNumber)
val lineUntilCaret = if (templateState?.currentVariableNumber == INDEX_TYPE) {
// If we are completing a type with template, we need to consider only
// the part of the line after the range marker's start
val typeStartOffset = templateState.getSegmentRange(INDEX_TYPE).startOffset
val start = typeStartOffset - document.getLineRangeByOffset(typeStartOffset).startOffset

caretOffsetInLine -= typeStartOffset
document.getSegment(start, start + caretOffsetInLine)
} else {
document.getSegment(lineStartOffset, lineStartOffset + caretOffsetInLine)
}

val providers = mutableListOf<CompletionProvider<*>>()

// After the second line we are inside the body/footer context
val isInBodyOrFooterContext = lineNumber > 1
val isInBodyOrFooterContext = caretLineNumber > 1

if (isInBodyOrFooterContext) {
val firstLineTokens = CCParser.parseHeader(editor.document.getLine(0))
val firstLineTokens = CCParser.parseHeader(document.getLine(0))
val footerTokens = CCParser.parseFooter(lineUntilCaret)

when (val context = footerTokens.getContext(lineCaretOffset)) {
when (val context = footerTokens.getContext(caretOffsetInLine)) {
is FooterTypeContext -> {
providers.add(FooterTypeCompletionProvider(project, context))
providers.add(BodyCompletionProvider(project, context, firstLineTokens))
Expand All @@ -132,7 +149,7 @@ private class CommitCompletionContributor : CompletionContributor() {
if (!isTemplateActive || !isInBodyOrFooterContext) {
val commitTokens = CCParser.parseHeader(lineUntilCaret)

when (val context = commitTokens.getContext(lineCaretOffset)) {
when (val context = commitTokens.getContext(caretOffsetInLine)) {
is TypeCommitContext -> providers.add(TypeCompletionProvider(project, context))
is ScopeCommitContext -> {
if (isTemplateActive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.intellij.openapi.util.TextRange
import kotlin.math.max
import kotlin.math.min

internal const val INDEX_TYPE = 0
internal const val INDEX_SCOPE = 1
internal const val INDEX_SUBJECT = 2
internal const val INDEX_BODY_OR_FOOTER_TYPE = 3
Expand Down

0 comments on commit c6a9e60

Please sign in to comment.