Skip to content

Commit 25b6113

Browse files
committed
refactor: rename CCDefaultTokensService to CCTokensService and clean it up a bit
1 parent 1e77639 commit 25b6113

9 files changed

Lines changed: 75 additions & 75 deletions

File tree

src/main/java/com/github/lppedd/cc/configuration/CCMainConfigurableGui.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import com.github.lppedd.cc.CCBundle;
1616
import com.github.lppedd.cc.configuration.CCConfigService.CompletionType;
17-
import com.github.lppedd.cc.configuration.CCDefaultTokensService.JsonCommitType;
17+
import com.github.lppedd.cc.configuration.CCTokensService.CommitTypeModel;
1818
import com.github.lppedd.cc.configuration.component.CoAuthorsFilePickerPanel;
1919
import com.github.lppedd.cc.configuration.component.DefaultTokensFileExportPanel;
2020
import com.github.lppedd.cc.configuration.component.DefaultTokensFilePickerPanel;
@@ -144,7 +144,7 @@ public void setCustomTokensFilePath(@Nullable final String path) {
144144
defaultTokensFilePickerPanel.setCustomFilePath(path);
145145
}
146146

147-
public void setTokens(@NotNull final Map<String, JsonCommitType> tokens) {
147+
public void setTokens(@NotNull final Map<String, CommitTypeModel> tokens) {
148148
defaultTokensPanel.setTokens(tokens);
149149
}
150150

@@ -158,7 +158,7 @@ public void revalidate() {
158158
defaultTokensFilePickerPanel.revalidateComponent();
159159
}
160160

161-
@SuppressWarnings({"ConstantExpression", "InstanceVariableUsedBeforeInitialized"})
161+
@SuppressWarnings("ConstantExpression")
162162
private void finishUpComponents(
163163
@NotNull final Project project,
164164
@NotNull final Disposable disposable) {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import com.github.lppedd.cc.CC
44
import com.github.lppedd.cc.CCBundle
55
import com.github.lppedd.cc.CCNotification
66
import com.github.lppedd.cc.api.*
7-
import com.github.lppedd.cc.configuration.CCConfigService
8-
import com.github.lppedd.cc.configuration.CCDefaultTokensService
7+
import com.github.lppedd.cc.configuration.CCTokensService
98
import com.github.lppedd.cc.configuration.SchemaValidationException
109
import com.intellij.openapi.components.service
1110
import com.intellij.openapi.diagnostic.logger
@@ -26,17 +25,16 @@ internal class InternalCommitTokenProvider(private val project: Project) :
2625
private val logger = logger<InternalCommitTokenProvider>()
2726
}
2827

29-
private val configService = project.service<CCConfigService>()
30-
private val defaultsService = project.service<CCDefaultTokensService>()
28+
private val tokensService = project.service<CCTokensService>()
3129
private val defaults
3230
get() = try {
33-
defaultsService.getDefaultsFromCustomFile(configService.customFilePath)
31+
tokensService.getTokens()
3432
} catch (e: ProcessCanceledException) {
3533
throw e
3634
} catch (e: Exception) {
3735
logger.debug("Error while reading the custom tokens file", e)
3836
notifyErrorToUser(e)
39-
defaultsService.getBuiltInDefaults()
37+
tokensService.getBundledTokens()
4038
}
4139

4240
override fun getId(): String =
@@ -63,7 +61,7 @@ internal class InternalCommitTokenProvider(private val project: Project) :
6361
subject: String?,
6462
): Collection<CommitFooterValue> {
6563
if ("co-authored-by".equals(footerType, true)) {
66-
return defaultsService.getCoAuthors()
64+
return tokensService.getCoAuthors()
6765
.asSequence()
6866
.take(3)
6967
.map { DefaultCommitToken(it, "", true) }

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import javax.swing.JComponent
1212
* @author Edoardo Luppi
1313
*/
1414
internal class CCMainConfigurable(private val project: Project) : SearchableConfigurable {
15-
private val defaultsService = project.service<CCDefaultTokensService>()
15+
private val tokensService = project.service<CCTokensService>()
1616
private val configService = project.service<CCConfigService>()
1717
private val disposable = Disposer.newDisposable("CCMainConfigurable")
1818
private lateinit var gui: CCMainConfigurableGui
@@ -33,9 +33,9 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
3333
gui.customCoAuthorsFilePath = configService.customCoAuthorsFilePath
3434

3535
val tokens = try {
36-
defaultsService.getDefaultsFromCustomFile(configService.customFilePath)
36+
tokensService.getTokens()
3737
} catch (_: Exception) {
38-
defaultsService.getBuiltInDefaults()
38+
tokensService.getBundledTokens()
3939
}
4040

4141
gui.setTokens(tokens.types)
@@ -60,7 +60,7 @@ internal class CCMainConfigurable(private val project: Project) : SearchableConf
6060
configService.customFilePath = gui.customTokensFilePath
6161

6262
try {
63-
val tokens = defaultsService.getDefaultsFromCustomFile(configService.customFilePath)
63+
val tokens = tokensService.getTokens()
6464
gui.setTokens(tokens.types)
6565
} catch (_: Exception) {
6666
gui.revalidate()

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

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ import java.nio.file.NoSuchFileException
1919
import kotlin.io.path.bufferedReader
2020

2121
/**
22-
* Manages default commit types and scopes.
22+
* Manages bundled and custom commit message tokens.
2323
*
2424
* @author Edoardo Luppi
2525
*/
2626
@Suppress("LightServiceMigrationCode")
27-
internal class CCDefaultTokensService(private val project: Project) {
27+
internal class CCTokensService(private val project: Project) {
2828
private companion object {
29-
private val logger = logger<CCDefaultTokensService>()
29+
private val logger = logger<CCTokensService>()
3030
}
3131

3232
private val configService = project.service<CCConfigService>()
3333

3434
/**
3535
* JSON Schema used to validate the default commit types and scopes JSON file.
3636
*/
37-
private val defaultsSchema: Schema by lazy {
37+
private val tokensSchema: Schema by lazy {
3838
val bufferedReader = getResourceAsStream("/defaults/${CC.File.Schema}").bufferedReader()
3939
val schemaJson = bufferedReader.use {
4040
JsonParser(bufferedReader).parse()
@@ -44,53 +44,56 @@ internal class CCDefaultTokensService(private val project: Project) {
4444
}
4545

4646
/**
47-
* Built-in default commit types and scopes.
48-
*
49-
* @throws SchemaValidationException When the JSON object does not respect the schema
47+
* Bundled commit message tokens.
5048
*/
51-
private val builtInDefaultTokens: JsonDefaults by lazy {
49+
private val bundledTokensModel: TokensModel by lazy {
5250
val jsonStr = getResourceAsStream("/defaults/${CC.File.Defaults}").bufferedReader().use(Reader::readText)
5351
parseJsonStr(jsonStr)
5452
}
5553

56-
fun getDefaultsFromCustomFile(filePath: String? = null): JsonDefaults {
57-
val path = filePath ?: findDefaultFilePathFromProjectRoot()
58-
return path?.let(::readDefaultsFromFile) ?: builtInDefaultTokens
54+
/**
55+
* Returns commit message tokens defined in a custom `conventionalcommit.json` file,
56+
* or falls back to the bundled defaults if no custom file is specified.
57+
*
58+
* @see getBundledTokens
59+
*/
60+
fun getTokens(): TokensModel {
61+
val path = configService.customFilePath ?: findTokensFileInProjectRoot()
62+
return path?.let(::readTokensFromFile) ?: getBundledTokens()
5963
}
6064

6165
/**
62-
* Returns the built-in commit types and scopes.
66+
* Returns commit message tokens defined in the bundled `conventionalcommit.json` file.
6367
*/
64-
fun getBuiltInDefaults(): JsonDefaults =
65-
builtInDefaultTokens
68+
fun getBundledTokens(): TokensModel =
69+
bundledTokensModel
6670

6771
/**
6872
* Validates a file via the inputted absolute path.
6973
*
7074
* @throws SchemaValidationException When the JSON object does not respect the schema
7175
*/
72-
fun validateDefaultsFile(filePath: String) {
76+
fun validateTokensFile(filePath: String) {
7377
val path = FileSystems.getDefault().getPath(filePath)
7478

7579
if (Files.notExists(path)) {
7680
throw NoSuchFileException(filePath)
7781
}
7882

7983
val jsonStr = path.bufferedReader().use(Reader::readText)
80-
defaultsSchema.validateJson(jsonStr)
84+
tokensSchema.validateJson(jsonStr)
8185
}
8286

8387
/**
8488
* Returns the user-defined co-authors.
8589
*/
8690
fun getCoAuthors(): Collection<String> {
8791
val customCoAuthorsFilePath = configService.customCoAuthorsFilePath
88-
val fileSystem = FileSystems.getDefault()
8992
val filePath = if (customCoAuthorsFilePath == null) {
9093
val projectBasePath = project.basePath ?: return emptySet()
91-
fileSystem.getPath(projectBasePath, CC.File.CoAuthors)
94+
FileSystems.getDefault().getPath(projectBasePath, CC.File.CoAuthors)
9295
} else {
93-
fileSystem.getPath(customCoAuthorsFilePath)
96+
FileSystems.getDefault().getPath(customCoAuthorsFilePath)
9497
}
9598

9699
try {
@@ -132,10 +135,9 @@ internal class CCDefaultTokensService(private val project: Project) {
132135
}
133136

134137
/**
135-
* Returns the full path of the default tokens file located
136-
* in the project root directory, or `null`.
138+
* Returns the path of the tokens file in the project root directory, or `null` if there is not.
137139
*/
138-
private fun findDefaultFilePathFromProjectRoot(): String? {
140+
private fun findTokensFileInProjectRoot(): String? {
139141
val projectBasePath = project.basePath ?: return null
140142
return LocalFileSystem.getInstance().refreshAndFindFileByPath(projectBasePath)
141143
?.findChild(CC.File.Defaults)
@@ -147,7 +149,7 @@ internal class CCDefaultTokensService(private val project: Project) {
147149
*
148150
* @throws SchemaValidationException When the JSON object does not respect the schema
149151
*/
150-
private fun readDefaultsFromFile(filePath: String): JsonDefaults {
152+
private fun readTokensFromFile(filePath: String): TokensModel {
151153
val path = FileSystems.getDefault().getPath(filePath)
152154
val jsonStr = path.bufferedReader().use(Reader::readText)
153155
return parseJsonStr(jsonStr)
@@ -156,10 +158,10 @@ internal class CCDefaultTokensService(private val project: Project) {
156158
/**
157159
* @throws SchemaValidationException When the JSON object does not respect the schema
158160
*/
159-
private fun parseJsonStr(jsonStr: String): JsonDefaults {
161+
private fun parseJsonStr(jsonStr: String): TokensModel {
160162
// If the inputted JSON isn't valid, an exception is thrown.
161163
// The exception contains the validation errors which can be used to notify the user
162-
defaultsSchema.validateJson(jsonStr)
164+
tokensSchema.validateJson(jsonStr)
163165

164166
val rootJsonObject = JSONObject(jsonStr)
165167
val commonScopes = buildScopes(rootJsonObject.optJSONObject("commonScopes") ?: JSONObject())
@@ -171,63 +173,63 @@ internal class CCDefaultTokensService(private val project: Project) {
171173
else -> error("Should never get here")
172174
}
173175

174-
return JsonDefaults(types = types, footerTypes = footerTypes)
176+
return TokensModel(types = types, footerTypes = footerTypes)
175177
}
176178

177-
private fun buildTypes(jsonObject: JSONObject, commonScopes: List<JsonCommitScope>): Map<String, JsonCommitType> =
179+
private fun buildTypes(jsonObject: JSONObject, commonScopes: List<CommitScopeModel>): Map<String, CommitTypeModel> =
178180
jsonObject.keySet().associateWith {
179181
val descriptor = jsonObject.getJSONObject(it)
180182
val description = descriptor.optString("description", "")
181183
val scopes = buildScopes(descriptor.optJSONObject("scopes") ?: JSONObject())
182-
JsonCommitType(
184+
CommitTypeModel(
183185
name = it,
184186
description = description,
185187
scopes = scopes + commonScopes,
186188
)
187189
}
188190

189-
private fun buildScopes(jsonObject: JSONObject): List<JsonCommitScope> =
191+
private fun buildScopes(jsonObject: JSONObject): List<CommitScopeModel> =
190192
jsonObject.keySet().map {
191193
val descriptor = jsonObject.getJSONObject(it)
192194
val description = descriptor.optString("description", "")
193-
JsonCommitScope(
195+
CommitScopeModel(
194196
name = it,
195197
description = description,
196198
)
197199
}
198200

199-
private fun buildFooterTypes(jsonObject: JSONObject): Map<String, JsonCommitFooterType> =
201+
private fun buildFooterTypes(jsonObject: JSONObject): Map<String, CommitFooterTypeModel> =
200202
jsonObject.keySet().associateWith {
201203
val descriptor = jsonObject.getJSONObject(it)
202204
val description = descriptor.optString("description", "")
203205
val values = buildFooterValues(descriptor.optJSONObject("values") ?: JSONObject())
204-
JsonCommitFooterType(
206+
CommitFooterTypeModel(
205207
name = it,
206208
description = description,
207209
values = values,
208210
)
209211
}
210212

211-
private fun buildFooterValues(jsonObject: JSONObject): List<JsonCommitFooterValue> =
213+
private fun buildFooterValues(jsonObject: JSONObject): List<CommitFooterValueModel> =
212214
jsonObject.keySet().map {
213215
val descriptor = jsonObject.getJSONObject(it)
214216
val description = descriptor.optString("description", "")
215-
JsonCommitFooterValue(
217+
CommitFooterValueModel(
216218
name = it,
217219
description = description,
218220
)
219221
}
220222

221223
// TODO(Edoardo): will have to remove it as point, as keeping footer values
222224
// as an array does not make sense anymore
223-
private fun buildFooterTypesArray(jsonArray: JSONArray): Map<String, JsonCommitFooterType> {
224-
val map = LinkedHashMap<String, JsonCommitFooterType>()
225+
private fun buildFooterTypesArray(jsonArray: JSONArray): Map<String, CommitFooterTypeModel> {
226+
val map = LinkedHashMap<String, CommitFooterTypeModel>()
225227

226228
for (i in 0..<jsonArray.length()) {
227229
val descriptor = jsonArray.getJSONObject(i)
228230
val name = descriptor.optString("name")
229231
val description = descriptor.optString("description", "")
230-
map[name] = JsonCommitFooterType(
232+
map[name] = CommitFooterTypeModel(
231233
name = name,
232234
description = description,
233235
values = emptyList(),
@@ -237,29 +239,29 @@ internal class CCDefaultTokensService(private val project: Project) {
237239
return map
238240
}
239241

240-
class JsonDefaults(
241-
val types: Map<String, JsonCommitType>,
242-
val footerTypes: Map<String, JsonCommitFooterType>,
242+
class TokensModel(
243+
val types: Map<String, CommitTypeModel>,
244+
val footerTypes: Map<String, CommitFooterTypeModel>,
243245
)
244246

245-
class JsonCommitType(
247+
class CommitTypeModel(
246248
val name: String,
247249
val description: String,
248-
val scopes: List<JsonCommitScope>,
250+
val scopes: List<CommitScopeModel>,
249251
)
250252

251-
class JsonCommitScope(
253+
class CommitScopeModel(
252254
val name: String,
253255
val description: String,
254256
)
255257

256-
class JsonCommitFooterType(
258+
class CommitFooterTypeModel(
257259
val name: String,
258260
val description: String,
259-
val values: List<JsonCommitFooterValue>,
261+
val values: List<CommitFooterValueModel>,
260262
)
261263

262-
class JsonCommitFooterValue(
264+
class CommitFooterValueModel(
263265
val name: String,
264266
val description: String,
265267
)

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

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

33
import com.github.lppedd.cc.CCBundle
4-
import com.github.lppedd.cc.configuration.CCDefaultTokensService
4+
import com.github.lppedd.cc.configuration.CCTokensService
55
import com.github.lppedd.cc.configuration.holders.CoAuthorsTableHolder
66
import com.github.lppedd.cc.readableMessage
77
import com.intellij.openapi.components.service
@@ -19,8 +19,8 @@ import javax.swing.JPanel
1919
* @author Edoardo Luppi
2020
*/
2121
internal class CoAuthorsDialog(project: Project) : DialogWrapper(project) {
22-
private val defaultsService = project.service<CCDefaultTokensService>()
23-
private val coAuthorsTableHolder = CoAuthorsTableHolder(defaultsService)
22+
private val tokensService = project.service<CCTokensService>()
23+
private val coAuthorsTableHolder = CoAuthorsTableHolder(tokensService)
2424

2525
init {
2626
init()
@@ -41,7 +41,7 @@ internal class CoAuthorsDialog(project: Project) : DialogWrapper(project) {
4141

4242
override fun doOKAction() {
4343
try {
44-
defaultsService.setCoAuthors(coAuthorsTableHolder.tableModel.coAuthors)
44+
tokensService.setCoAuthors(coAuthorsTableHolder.tableModel.coAuthors)
4545
super.doOKAction()
4646
} catch (e: Exception) {
4747
setErrorText(CCBundle["cc.config.coAuthorsDialog.saveError", e.readableMessage()])

0 commit comments

Comments
 (0)