Skip to content

Commit b974cb0

Browse files
committed
refactor: merge CCUI and CCIcons into CC to reduce the amount of declarations
1 parent 7f45c92 commit b974cb0

64 files changed

Lines changed: 233 additions & 241 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.github.lppedd.cc
22

3+
import com.intellij.openapi.util.IconLoader
4+
import com.intellij.ui.ColorUtil.isDark
5+
import com.intellij.ui.JBColor
6+
import com.intellij.util.ui.UIUtil
7+
import java.awt.Color
8+
import javax.swing.Icon as SwingIcon
9+
310
/**
411
* @author Edoardo Luppi
512
*/
@@ -8,24 +15,69 @@ public object CC {
815
public const val PluginId: String = "com.github.lppedd.idea-conventional-commit"
916
public const val AppName: String = "ConventionalCommit"
1017

11-
public object Settings {
12-
public const val File: String = "conventionalCommit.xml"
18+
public object File {
19+
public const val Defaults: String = "conventionalcommit.json"
20+
public const val Schema: String = "conventionalcommit.schema.json"
21+
public const val CoAuthors: String = "conventionalcommit.coauthors"
22+
public const val Settings: String = "conventionalCommit.xml"
1323
}
1424

15-
public object Provider {
16-
public const val MaxItems: Int = 200
25+
public object Registry {
26+
public const val VcsEnabled: String = "com.github.lppedd.cc.providers.vcs"
1727
}
1828

19-
public object CoAuthors {
20-
public const val File: String = "conventionalcommit.coauthors"
21-
}
29+
public object Icon {
30+
@JvmField public val Logo: SwingIcon = icon("logo.svg")
31+
32+
public object General {
33+
@JvmField public val ArrowRight: SwingIcon = icon("general/arrowRight.svg")
34+
@JvmField public val ClearMessageHistory: SwingIcon = icon("general/clearMessageHistory.svg")
35+
}
36+
37+
public object Provider {
38+
@JvmField public val Unknown: SwingIcon = icon("provider/unknown.svg")
39+
@JvmField public val Disabled: SwingIcon = icon("provider/disabled.svg")
40+
@JvmField public val Vcs: SwingIcon = icon("provider/vcs.svg")
41+
}
2242

23-
public object Tokens {
24-
public const val File: String = "conventionalcommit.json"
25-
public const val SchemaFile: String = "conventionalcommit.schema.json"
43+
public object Token {
44+
@JvmField public val Type: SwingIcon = icon("token/commitType.svg")
45+
@JvmField public val Scope: SwingIcon = icon("token/commitScope.svg")
46+
@JvmField public val Subject: SwingIcon = icon("token/commitDescription.svg")
47+
@JvmField public val Body: SwingIcon = icon("token/commitBody.svg")
48+
@JvmField public val Footer: SwingIcon = icon("token/commitFooter.svg")
49+
}
50+
51+
public object FileType {
52+
@JvmField public val Generic: SwingIcon = icon("fileType/generic.svg")
53+
@JvmField public val Json: SwingIcon = icon("fileType/json.svg")
54+
@JvmField public val CoAuthors: SwingIcon = icon("fileType/coauthors.svg")
55+
}
56+
57+
private fun icon(path: String) = IconLoader.getIcon("/icons/$path", CC::class.java)
2658
}
2759

28-
public object Registry {
29-
public const val VcsEnabled: String = "com.github.lppedd.cc.providers.vcs"
60+
public object UI {
61+
@JvmField public val BorderColor: Color = JBColor.lazy {
62+
if (JBColor.isBright()) {
63+
JBColor.border()
64+
} else {
65+
val borderColor = JBColor.border()
66+
67+
if (isDark(borderColor)) {
68+
UIUtil.getListBackground().brighter(0.75)
69+
} else {
70+
borderColor
71+
}
72+
}
73+
}
74+
75+
@JvmField public val ListBackgroundColor: Color = JBColor.lazy {
76+
if (JBColor.isBright()) {
77+
UIUtil.getListBackground()
78+
} else {
79+
UIUtil.getListBackground().brighter(0.96)
80+
}
81+
}
3082
}
3183
}

src/main/kotlin/com/github/lppedd/cc/CCIcons.kt

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/kotlin/com/github/lppedd/cc/CCUI.kt

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main/kotlin/com/github/lppedd/cc/api/ProviderPresentation.kt

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

3-
import com.github.lppedd.cc.CCIcons
3+
import com.github.lppedd.cc.CC
44
import javax.swing.Icon
55

66
/**
@@ -30,5 +30,5 @@ public interface ProviderPresentation {
3030
* @see getIcon
3131
*/
3232
public fun getDisabledIcon(): Icon =
33-
CCIcons.Provider.Disabled
33+
CC.Icon.Provider.Disabled
3434
}

src/main/kotlin/com/github/lppedd/cc/api/impl/InternalCommitTokenProvider.kt

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

3+
import com.github.lppedd.cc.CC
34
import com.github.lppedd.cc.CCBundle
4-
import com.github.lppedd.cc.CCIcons
55
import com.github.lppedd.cc.CCNotificationService
66
import com.github.lppedd.cc.api.*
77
import com.github.lppedd.cc.configuration.CCConfigService
@@ -90,7 +90,7 @@ internal class InternalCommitTokenProvider(private val project: Project) :
9090
CCBundle["cc.config.providers.default"]
9191

9292
override fun getIcon(): Icon =
93-
CCIcons.Logo
93+
CC.Icon.Logo
9494
}
9595

9696
private object DefaultTokenPresentation : TokenPresentation

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.github.lppedd.cc.completion
22

33
import com.github.lppedd.cc.api.*
4-
import com.github.lppedd.cc.completion.providers.ELEMENT_INDEX
5-
import com.github.lppedd.cc.completion.providers.ELEMENT_IS_RECENT
6-
import com.github.lppedd.cc.completion.providers.ELEMENT_PROVIDER
74
import com.github.lppedd.cc.configuration.CCConfigService
85
import com.github.lppedd.cc.lookupElement.*
96
import com.intellij.codeInsight.lookup.LookupElement
@@ -37,23 +34,23 @@ internal class ConventionalCommitLookupElementWeigher(
3734
}
3835

3936
private fun getTokenWeight(element: CommitTokenLookupElement): CommitTokenWeight {
40-
// The token type's priority.
41-
// This is used to prioritize a certain token's type when multiple types
42-
// might be shown in the same completion invocation
43-
// (e.g. a footer type token should be shown before a body token)
37+
// The token type priority.
38+
// This is used to prioritize a certain token type when multiple types
39+
// might be shown in the same completion invocation (e.g., a footer type
40+
// token should be shown before a body token).
4441
val tokenPriority = getTokenPriority(element)
4542

46-
// The token's provider priority.
47-
// Providers can be ordered via settings by the user, so each
48-
val provider = element.getUserData(ELEMENT_PROVIDER)
43+
// The token provider priority.
44+
// Providers can be ordered via settings by the user.
45+
val provider = element.getUserData(LookupElementKey.Provider)
4946
val providerPriority = getProviderPriority(provider)
5047

51-
// The token's index, in ascending order, in the context of extraction from its provider
52-
val index = element.getUserData(ELEMENT_INDEX)!!
48+
// The token index, in ascending order, in the context of extraction from its provider
49+
val index = element.getUserData(LookupElementKey.Index) ?: error("missing element index")
5350

54-
// Indicate if the token has been used recently by the user.
55-
// This flag is set by each CompletionProvider
56-
val isRecentToken = element.getUserData(ELEMENT_IS_RECENT) ?: false
51+
// Indicate if the user has used the token recently.
52+
// This flag is set by each CompletionProvider.
53+
val isRecentToken = element.getUserData(LookupElementKey.IsRecent) ?: false
5754
val isRecentlyUsed = configService.prioritizeRecentlyUsed && isRecentToken
5855

5956
return CommitTokenWeight(tokenPriority, isRecentlyUsed, providerPriority, index)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.lppedd.cc.completion
2+
3+
import com.github.lppedd.cc.api.CommitTokenProvider
4+
import com.intellij.openapi.util.Key
5+
6+
/**
7+
* @author Edoardo Luppi
8+
*/
9+
internal object LookupElementKey {
10+
val Index: Key<Int> = Key.create("com.github.lppedd.cc.lookupElement.index")
11+
val Provider: Key<CommitTokenProvider> = Key.create("com.github.lppedd.cc.lookupElement.provider")
12+
val IsRecent: Key<Boolean> = Key.create("com.github.lppedd.cc.lookupElement.isRecent")
13+
}

src/main/kotlin/com/github/lppedd/cc/completion/menu/FilterAction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.github.lppedd.cc.completion.menu
22

33
import com.github.lppedd.cc.api.CommitTokenProvider
44
import com.github.lppedd.cc.completion.FilterPrefixMatcher
5+
import com.github.lppedd.cc.completion.LookupElementKey
56
import com.github.lppedd.cc.completion.LookupEnhancer
6-
import com.github.lppedd.cc.completion.providers.ELEMENT_PROVIDER
77
import com.github.lppedd.cc.lookupElement.CommitTokenLookupElement
88
import com.github.lppedd.cc.updateIcons
99
import com.intellij.codeInsight.completion.PlainPrefixMatcher
@@ -43,7 +43,7 @@ internal class FilterAction(
4343
.asSequence()
4444
.filterIsInstance<CommitTokenLookupElement>()
4545
.filter {
46-
val elementProvider = it.getUserData(ELEMENT_PROVIDER)!!
46+
val elementProvider = it.getUserData(LookupElementKey.Provider) ?: error("missing element provider")
4747
provider.getId() == elementProvider.getId()
4848
}.onEach {
4949
val delegatePrefixMatcher = arranger.itemMatcher(it)

src/main/kotlin/com/github/lppedd/cc/completion/providers/BodyCompletionProvider.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.lppedd.cc.completion.providers
22

3-
import com.github.lppedd.cc.CC
43
import com.github.lppedd.cc.api.CommitBody
54
import com.github.lppedd.cc.api.CommitBodyProvider
65
import com.github.lppedd.cc.api.CommitTokenProviderService
6+
import com.github.lppedd.cc.completion.LookupElementKey
77
import com.github.lppedd.cc.completion.resultset.ResultSet
88
import com.github.lppedd.cc.lookupElement.CommitBodyLookupElement
99
import com.github.lppedd.cc.parser.CommitTokens
@@ -41,16 +41,16 @@ internal class BodyCompletionProvider(
4141
)
4242

4343
commitBodies.asSequence()
44-
.take(CC.Provider.MaxItems)
44+
.take(CompletionProvider.MaxItems)
4545
.forEach { bodies.add(ProviderCommitToken(provider, it)) }
4646
}
4747
}
4848

4949
bodies.forEachIndexed { index, (provider, commitBody) ->
5050
val psiElement = CommitBodyPsiElement(project, commitBody.getText())
5151
val element = CommitBodyLookupElement(psiElement, commitBody)
52-
element.putUserData(ELEMENT_INDEX, index)
53-
element.putUserData(ELEMENT_PROVIDER, provider)
52+
element.putUserData(LookupElementKey.Index, index)
53+
element.putUserData(LookupElementKey.Provider, provider)
5454
prefixedResultSet.addElement(element)
5555
}
5656
}

src/main/kotlin/com/github/lppedd/cc/completion/providers/CompletionProvider.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ package com.github.lppedd.cc.completion.providers
22

33
import com.github.lppedd.cc.api.CommitTokenProvider
44
import com.github.lppedd.cc.completion.resultset.ResultSet
5-
import com.intellij.openapi.util.Key
6-
7-
// TODO: move them inside an object/namespace
8-
internal val ELEMENT_INDEX: Key<Int> = Key.create("com.github.lppedd.cc.lookupElement.index")
9-
internal val ELEMENT_PROVIDER: Key<CommitTokenProvider> = Key.create("com.github.lppedd.cc.lookupElement.provider")
10-
internal val ELEMENT_IS_RECENT: Key<Boolean> = Key.create("com.github.lppedd.cc.lookupElement.isRecent")
115

126
/**
137
* @author Edoardo Luppi
148
*/
159
internal interface CompletionProvider<out T : CommitTokenProvider> {
16-
fun getProviders(): Collection<T>
10+
@Suppress("ConstPropertyName")
11+
companion object {
12+
const val MaxItems: Int = 200
13+
}
1714

15+
fun getProviders(): Collection<T>
1816
fun stopHere(): Boolean
19-
2017
fun complete(resultSet: ResultSet)
2118
}

0 commit comments

Comments
 (0)