Skip to content

Commit 84f8421

Browse files
committed
refactor: replace StringBuilder with simple substrings
1 parent db8c000 commit 84f8421

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@ internal class DefaultTokensFileExportPanel(private val project: Project) :
5555
}
5656

5757
private fun writeFile(fileWrapper: VirtualFileWrapper) {
58-
val virtualFile = fileWrapper.getVirtualFile(/* createIfNotExist = */ true)
58+
val file = fileWrapper.getVirtualFile(/* createIfNotExist = */ true)
5959

60-
if (virtualFile == null || !virtualFile.isWritable) {
60+
if (file == null || !file.isWritable) {
6161
exportInfo.foreground = JBColor.RED
6262
exportInfo.text = CCBundle["cc.config.defaults.exportToPath.error"]
6363
return
6464
}
6565

6666
// When exporting to a file, we also need to add the JSON schema reference.
6767
// Better normalize line endings to \n.
68-
val jsonStr = StringUtil.convertLineSeparators(
68+
val content = StringUtil.convertLineSeparators(
6969
getResourceAsStream("/defaults/${CC.File.Defaults}").bufferedReader().use(Reader::readText)
7070
)
7171

7272
val pluginVersion = getPluginVersion()
7373
val schemaPath = "src/main/resources/defaults/conventionalcommit.schema.json"
7474
val schemaUrl = "https://github.com/lppedd/idea-conventional-commit/raw/$pluginVersion/$schemaPath"
75-
val sb = StringBuilder(jsonStr)
76-
sb.insert(4, $$"\"$schema\": \"$$schemaUrl\",\n ")
75+
val bytes = (content.substring(0, 4) + $$"\"$schema\": \"$$schemaUrl\",\n" + content.substring(2))
76+
.toByteArray(file.charset)
7777

7878
WriteAction.runAndWait<Throwable> {
79-
virtualFile.setBinaryContent("$sb".toByteArray())
79+
file.setBinaryContent(bytes)
8080
}
8181

8282
exportInfo.foreground = UIUtil.getLabelDisabledForeground()

0 commit comments

Comments
 (0)