Skip to content

Commit 484ea2e

Browse files
committed
refactor: rework the tokens model to allow keyed lookups
1 parent e9f4a5d commit 484ea2e

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

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
@@ -36,7 +36,7 @@ internal class InternalCommitTokenProvider(private val project: Project) :
3636

3737
override fun getCommitScopes(type: String): Collection<CommitScope> {
3838
val defaultType = getTokens().types[type] ?: return emptyList()
39-
return defaultType.scopes.map { DefaultCommitToken(it.name, it.description) }
39+
return defaultType.scopes.map { (_, value) -> DefaultCommitToken(value.name, value.description) }
4040
}
4141

4242
override fun getCommitFooterTypes(): Collection<CommitFooterType> =
@@ -62,7 +62,7 @@ internal class InternalCommitTokenProvider(private val project: Project) :
6262
}
6363

6464
val defaultFooterType = getTokens().footerTypes[footerType] ?: return emptyList()
65-
return defaultFooterType.values.map { DefaultCommitToken(it.name, it.description) }
65+
return defaultFooterType.values.map { (_, value) -> DefaultCommitToken(value.name, value.description) }
6666
}
6767

6868
private fun getTokens(): TokensModel {

src/main/kotlin/com/github/lppedd/cc/configuration/CCTokensService.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,20 @@ internal class CCTokensService(private val project: Project) {
222222
return null
223223
}
224224

225-
private fun buildTypes(jsonObject: JSONObject, commonScopes: List<CommitScopeModel>): Map<String, CommitTypeModel> =
225+
private fun buildTypes(jsonObject: JSONObject, commonScopes: Map<String, CommitScopeModel>): Map<String, CommitTypeModel> =
226226
jsonObject.keySet().associateWith {
227227
val descriptor = jsonObject.getJSONObject(it)
228228
val description = descriptor.optString("description", "")
229229
val scopes = buildScopes(descriptor.optJSONObject("scopes") ?: JSONObject())
230230
CommitTypeModel(
231231
name = it,
232232
description = description,
233-
scopes = scopes + commonScopes,
233+
scopes = commonScopes + scopes,
234234
)
235235
}
236236

237-
private fun buildScopes(jsonObject: JSONObject): List<CommitScopeModel> =
238-
jsonObject.keySet().map {
237+
private fun buildScopes(jsonObject: JSONObject): Map<String, CommitScopeModel> =
238+
jsonObject.keySet().associateWithTo(LinkedHashMap()) {
239239
val descriptor = jsonObject.getJSONObject(it)
240240
val description = descriptor.optString("description", "")
241241
CommitScopeModel(
@@ -256,8 +256,8 @@ internal class CCTokensService(private val project: Project) {
256256
)
257257
}
258258

259-
private fun buildFooterValues(jsonObject: JSONObject): List<CommitFooterValueModel> =
260-
jsonObject.keySet().map {
259+
private fun buildFooterValues(jsonObject: JSONObject): Map<String, CommitFooterValueModel> =
260+
jsonObject.keySet().associateWithTo(LinkedHashMap()) {
261261
val descriptor = jsonObject.getJSONObject(it)
262262
val description = descriptor.optString("description", "")
263263
CommitFooterValueModel(
@@ -278,7 +278,7 @@ internal class CCTokensService(private val project: Project) {
278278
map[name] = CommitFooterTypeModel(
279279
name = name,
280280
description = description,
281-
values = emptyList(),
281+
values = emptyMap(),
282282
)
283283
}
284284

@@ -293,7 +293,7 @@ internal class CCTokensService(private val project: Project) {
293293
class CommitTypeModel(
294294
val name: String,
295295
val description: String,
296-
val scopes: List<CommitScopeModel>,
296+
val scopes: Map<String, CommitScopeModel>,
297297
)
298298

299299
class CommitScopeModel(
@@ -304,7 +304,7 @@ internal class CCTokensService(private val project: Project) {
304304
class CommitFooterTypeModel(
305305
val name: String,
306306
val description: String,
307-
val values: List<CommitFooterValueModel>,
307+
val values: Map<String, CommitFooterValueModel>,
308308
)
309309

310310
class CommitFooterValueModel(

src/main/kotlin/com/github/lppedd/cc/configuration/component/DefaultTokensPanel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.github.lppedd.cc.configuration.component
22

33
import com.github.lppedd.cc.CC
44
import com.github.lppedd.cc.CCBundle
5-
import com.github.lppedd.cc.configuration.CCTokensService.CommitScopeModel
65
import com.github.lppedd.cc.configuration.CCTokensService.CommitTypeModel
76
import com.github.lppedd.cc.configuration.component.tokens.CommitTokenList
87
import com.github.lppedd.cc.scaled
@@ -45,7 +44,7 @@ internal class DefaultTokensPanel : JPanel(ScaledGridLayout(1, 1, 24, 1)) {
4544

4645
if (selectedValue != null) {
4746
val jsonCommitType = latestTokens[selectedValue] ?: return
48-
val scopes = jsonCommitType.scopes.map(CommitScopeModel::name)
47+
val scopes = jsonCommitType.scopes.map { (_, value) -> value.name }
4948
scopeList.setTokens(scopes)
5049
}
5150
}

0 commit comments

Comments
 (0)