Skip to content

Commit fde3fb0

Browse files
committed
refactor: encapsulate template segment indexes into TemplateSegment
1 parent 034c504 commit fde3fb0

5 files changed

Lines changed: 25 additions & 22 deletions

File tree

src/main/kotlin/com/github/lppedd/cc/completion/ConventionalCommitTextCompletionContributor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.github.lppedd.cc.completion.resultset.ContextResultSet
1111
import com.github.lppedd.cc.completion.resultset.TemplateResultSet
1212
import com.github.lppedd.cc.configuration.CCConfigService
1313
import com.github.lppedd.cc.configuration.CCConfigService.CompletionType.TEMPLATE
14-
import com.github.lppedd.cc.lookupElement.INDEX_TYPE
14+
import com.github.lppedd.cc.lookupElement.TemplateSegment
1515
import com.github.lppedd.cc.parser.CCParser
1616
import com.github.lppedd.cc.parser.CommitContext.*
1717
import com.github.lppedd.cc.parser.FooterContext.FooterTypeContext
@@ -147,10 +147,10 @@ internal class ConventionalCommitTextCompletionContributor : CompletionContribut
147147
val caretLineNumber = caretLogicalPosition.line
148148
var caretOffsetInLine = caretLogicalPosition.column
149149
val templateState = editor.getTemplateState()
150-
val lineUntilCaret = if (templateState?.currentVariableNumber == INDEX_TYPE) {
150+
val lineUntilCaret = if (templateState?.currentVariableNumber == TemplateSegment.Type) {
151151
// If we are completing a type with a template, we need to consider only
152152
// the part of the line after the range marker's start
153-
val typeStartOffset = templateState.getSegmentRange(INDEX_TYPE).startOffset
153+
val typeStartOffset = templateState.getSegmentRange(TemplateSegment.Type).startOffset
154154
val start = typeStartOffset - document.getLineRangeByOffset(typeStartOffset).startOffset
155155

156156
caretOffsetInLine -= typeStartOffset

src/main/kotlin/com/github/lppedd/cc/liveTemplate/CCTemplateEditingListener.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package com.github.lppedd.cc.liveTemplate
22

33
import com.github.lppedd.cc.*
44
import com.github.lppedd.cc.annotation.Compatibility
5-
import com.github.lppedd.cc.lookupElement.INDEX_BODY_OR_FOOTER_TYPE
6-
import com.github.lppedd.cc.lookupElement.INDEX_FOOTER_VALUE
7-
import com.github.lppedd.cc.lookupElement.INDEX_SCOPE
8-
import com.github.lppedd.cc.lookupElement.INDEX_SUBJECT
5+
import com.github.lppedd.cc.lookupElement.TemplateSegment
96
import com.intellij.codeInsight.template.Template
107
import com.intellij.codeInsight.template.TemplateEditingAdapter
118
import com.intellij.codeInsight.template.impl.TemplateState
@@ -44,8 +41,8 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
4441
return
4542
}
4643

47-
if (oldIndex == INDEX_BODY_OR_FOOTER_TYPE && newIndex > oldIndex) {
48-
if (templateState.getSegmentRange(INDEX_BODY_OR_FOOTER_TYPE).isEmpty) {
44+
if (oldIndex == TemplateSegment.BodyOrFooterType && newIndex > oldIndex) {
45+
if (templateState.getSegmentRange(TemplateSegment.BodyOrFooterType).isEmpty) {
4946
deleteFooterValue(templateState)
5047
templateState.gotoEnd()
5148
return
@@ -59,7 +56,7 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
5956
}
6057

6158
override fun beforeTemplateFinished(templateState: TemplateState, template: Template) {
62-
val bodyOrFooterTypeRange = templateState.getSegmentRange(INDEX_BODY_OR_FOOTER_TYPE)
59+
val bodyOrFooterTypeRange = templateState.getSegmentRange(TemplateSegment.BodyOrFooterType)
6360

6461
if (bodyOrFooterTypeRange.isEmpty) {
6562
repositionCursorAfterSubjectAndCleanUp(templateState, bodyOrFooterTypeRange)
@@ -74,7 +71,7 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
7471
) {
7572
// If the body is empty, it means the user didn't need to insert it.
7673
// Thus, we can reposition the cursor at the end of the subject
77-
val newOffset = templateState.getSegmentRange(INDEX_SUBJECT).endOffset
74+
val newOffset = templateState.getSegmentRange(TemplateSegment.Subject).endOffset
7875

7976
if (newOffset <= bodyOrFooterTypeRange.endOffset) {
8077
val editor = templateState.editor
@@ -88,7 +85,7 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
8885
}
8986

9087
private fun deleteScopeParenthesesIfEmpty(templateState: TemplateState) {
91-
val (scopeStart, scopeEnd, isScopeEmpty) = templateState.getSegmentRange(INDEX_SCOPE)
88+
val (scopeStart, scopeEnd, isScopeEmpty) = templateState.getSegmentRange(TemplateSegment.Scope)
9289

9390
// If the scope is empty, it means the user didn't need to insert it, thus we can remove it
9491
if (isScopeEmpty) {
@@ -105,7 +102,7 @@ internal class CCTemplateEditingListener : TemplateEditingAdapter() {
105102
}
106103

107104
private fun deleteFooterValue(templateState: TemplateState) {
108-
val (start, end, isEmpty) = templateState.getSegmentRange(INDEX_FOOTER_VALUE)
105+
val (start, end, isEmpty) = templateState.getSegmentRange(TemplateSegment.FooterValue)
109106

110107
if (!isEmpty) {
111108
runWriteAction {

src/main/kotlin/com/github/lppedd/cc/lookupElement/TemplateCommitTypeLookupElement.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ import com.intellij.codeInsight.template.impl.TemplateImpl
1111
import com.intellij.codeInsight.template.impl.TemplateSettings
1212
import com.intellij.openapi.application.runWriteAction
1313

14-
// TODO: move them inside an object/namespace
15-
internal const val INDEX_TYPE = 0
16-
internal const val INDEX_SCOPE = 1
17-
internal const val INDEX_SUBJECT = 2
18-
internal const val INDEX_BODY_OR_FOOTER_TYPE = 3
19-
internal const val INDEX_FOOTER_VALUE = 4
20-
2114
/**
2215
* @author Edoardo Luppi
2316
*

src/main/kotlin/com/github/lppedd/cc/lookupElement/TemplateLookupElementDecorator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ internal class TemplateLookupElementDecorator(private val delegate: CommitTokenL
114114
delegate.hashCode()
115115

116116
private fun deleteScopeAndNext(editor: Editor, templateState: TemplateState) {
117-
val (start, end) = templateState.getSegmentRange(INDEX_SCOPE)
117+
val (start, end) = templateState.getSegmentRange(TemplateSegment.Scope)
118118
editor.document.deleteString(start, end)
119119
templateState.nextTab()
120120
}
121121

122122
private fun appendSeparatorOnFooterType(editor: Editor, templateState: TemplateState) {
123-
val offset = templateState.getSegmentRange(INDEX_BODY_OR_FOOTER_TYPE).endOffset
123+
val offset = templateState.getSegmentRange(TemplateSegment.BodyOrFooterType).endOffset
124124
editor.document.insertString(offset, ": ")
125125

126126
invokeLaterOnEdt {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.lppedd.cc.lookupElement
2+
3+
/**
4+
* @author Edoardo Luppi
5+
*/
6+
@Suppress("ConstPropertyName")
7+
internal object TemplateSegment {
8+
const val Type: Int = 0
9+
const val Scope: Int = 1
10+
const val Subject: Int = 2
11+
const val BodyOrFooterType: Int = 3
12+
const val FooterValue: Int = 4
13+
}

0 commit comments

Comments
 (0)