Skip to content

Commit e3ce37b

Browse files
committed
refactor: support Core 0.21.0 and cleanup
1 parent 66c17e4 commit e3ce37b

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
@file:JvmName("CommitlintConstants")
2-
31
package com.github.lppedd.cc.commitlint
42

5-
import com.github.lppedd.cc.api.ProviderPresentation
6-
7-
internal const val CONFIG_FILE_NAME = ".commitlintrc.json"
8-
internal const val PROVIDER_ID = "37415b03-9388-4c55-b949-8c4526f6934d"
9-
internal val PROVIDER_PRESENTATION = ProviderPresentation("Commitlint", ICON_COMMITLINT)
3+
/**
4+
* @author Edoardo Luppi
5+
*/
6+
object CommitlintConstants {
7+
const val ConfigFileName: String = ".commitlintrc.json"
8+
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
@file:JvmName("CommitlintIcons")
2-
31
package com.github.lppedd.cc.commitlint
42

53
import com.intellij.openapi.util.IconLoader
4+
import javax.swing.Icon
65

7-
internal val ICON_COMMITLINT = IconLoader.getIcon("/icons/commitlint.svg")
6+
/**
7+
* @author Edoardo Luppi
8+
*/
9+
object CommitlintIcons {
10+
@JvmField val Logo: Icon = IconLoader.getIcon("/icons/commitlint.svg")
11+
}

src/main/kotlin/com/github/lppedd/cc/commitlint/CommitlintTokensProvider.kt

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.lppedd.cc.commitlint
22

33
import com.github.lppedd.cc.api.*
4-
import com.github.lppedd.cc.api.CommitTokenElement.CommitTokenRendering
54
import com.intellij.openapi.project.Project
65
import com.intellij.openapi.project.guessProjectDir
76
import org.json.JSONArray
@@ -10,27 +9,33 @@ import org.json.JSONTokener
109
import java.nio.charset.StandardCharsets.UTF_8
1110
import java.nio.file.Files
1211
import java.nio.file.Paths
12+
import javax.swing.Icon
1313

1414
/**
1515
* @author Edoardo Luppi
1616
*/
17-
private class CommitlintTokensProvider(private val project: Project) :
17+
@Suppress("UnstableApiUsage")
18+
internal class CommitlintTokensProvider(private val project: Project) :
1819
CommitTypeProvider,
1920
CommitScopeProvider {
21+
companion object {
22+
const val ID: String = "37415b03-9388-4c55-b949-8c4526f6934d"
23+
}
24+
2025
override fun getId(): String =
21-
PROVIDER_ID
26+
ID
2227

2328
override fun getPresentation(): ProviderPresentation =
24-
PROVIDER_PRESENTATION
29+
CommitlintProviderPresentation
2530

26-
override fun getCommitTypes(prefix: String?): Collection<CommitType> =
31+
override fun getCommitTypes(prefix: String): Collection<CommitType> =
2732
readRuleValuesFromConfigFile("type-enum")
28-
.map(::CommitlintCommitType)
33+
.map(::CommitlintCommitToken)
2934
.toList()
3035

31-
override fun getCommitScopes(commitType: String?): Collection<CommitScope> =
36+
override fun getCommitScopes(commitType: String): Collection<CommitScope> =
3237
readRuleValuesFromConfigFile("scope-enum")
33-
.map(::CommitlintCommitScope)
38+
.map(::CommitlintCommitToken)
3439
.toList()
3540

3641
private fun readRuleValuesFromConfigFile(ruleName: String): Sequence<String> {
@@ -58,7 +63,7 @@ private class CommitlintTokensProvider(private val project: Project) :
5863

5964
private fun getConfigFilePath(): String? =
6065
project.guessProjectDir()
61-
?.findChild(CONFIG_FILE_NAME)
66+
?.findChild(CommitlintConstants.ConfigFileName)
6267
?.path
6368

6469
private inline fun <T> noException(block: () -> T): T? =
@@ -67,14 +72,31 @@ private class CommitlintTokensProvider(private val project: Project) :
6772
} catch (ignored: Exception) {
6873
null
6974
}
70-
}
7175

72-
private val TOKEN_RENDERING = CommitTokenRendering(icon = ICON_COMMITLINT)
76+
private object CommitlintProviderPresentation : ProviderPresentation {
77+
override fun getName(): String =
78+
"Commitlint"
7379

74-
private class CommitlintCommitType(text: String) : CommitType(text, "") {
75-
override fun getRendering() = TOKEN_RENDERING
76-
}
80+
override fun getIcon(): Icon =
81+
CommitlintIcons.Logo
82+
}
7783

78-
private class CommitlintCommitScope(text: String) : CommitScope(text, "") {
79-
override fun getRendering() = TOKEN_RENDERING
84+
private object CommitlintTokenPresentation : TokenPresentation {
85+
override fun getIcon(): Icon =
86+
CommitlintIcons.Logo
87+
}
88+
89+
private class CommitlintCommitToken(private val text: String) : CommitType, CommitScope {
90+
override fun getText(): String =
91+
text
92+
93+
override fun getValue(): String =
94+
getText()
95+
96+
override fun getDescription(): String =
97+
""
98+
99+
override fun getPresentation(): TokenPresentation =
100+
CommitlintTokenPresentation
101+
}
80102
}

0 commit comments

Comments
 (0)