From e133a62c94ec19f8af38813a64052366c36f1f4e Mon Sep 17 00:00:00 2001 From: Patrice de Saint Steban Date: Mon, 1 Apr 2024 23:48:19 +0200 Subject: [PATCH] :recycle: Some refactor and improvement --- .../github/patou/gitmoji/GitCommitAction.kt | 16 +++--- .../com/github/patou/gitmoji/GitMojiConfig.kt | 3 +- .../com/github/patou/gitmoji/GitmojiData.kt | 5 +- .../com/github/patou/gitmoji/GitmojiLocale.kt | 51 +++++++++++-------- 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/github/patou/gitmoji/GitCommitAction.kt b/src/main/kotlin/com/github/patou/gitmoji/GitCommitAction.kt index 72ca321..e09b4a9 100644 --- a/src/main/kotlin/com/github/patou/gitmoji/GitCommitAction.kt +++ b/src/main/kotlin/com/github/patou/gitmoji/GitCommitAction.kt @@ -103,7 +103,7 @@ class GitCommitAction : AnAction() { appendTextPadding(5) append( first( - convertLineSeparators(value.description, RETURN_SYMBOL), + convertLineSeparators(value.localeDescription, RETURN_SYMBOL), rightMargin, false ) @@ -134,7 +134,7 @@ class GitCommitAction : AnAction() { } } }) - .setNamerForFiltering { "${it.code} ${it.description}" } + .setNamerForFiltering { "${it.code} ${it.localeDescription} ${it.description}" } .setAutoPackHeightOnFiltering(false) .createPopup() .apply { @@ -195,7 +195,7 @@ class GitCommitAction : AnAction() { } val startPosition = insertPosition + selectedGitmoji.length if (includeGitMojiDescription) { - message = message.substring(0, startPosition) + gitmoji.description + message = message.substring(0, startPosition) + gitmoji.localeDescription } commitMessage.setCommitMessage(message) @@ -255,14 +255,12 @@ class GitCommitAction : AnAction() { } private fun loadGitmoji(text: String) { - val gitmojiLocale = GitmojiLocale() - gitmojiLocale.loadMap { - Gson().fromJson(text, Gitmojis::class.java).also { - it.gitmojis.forEach { gitmoji -> - gitmojis.add(GitmojiData(gitmoji.code, gitmoji.emoji, gitmojiLocale.t(gitmoji.name, gitmoji.description))) - } + Gson().fromJson(text, Gitmojis::class.java).also { + it.gitmojis.forEach { gitmoji -> + gitmojis.add(GitmojiData(gitmoji.code, gitmoji.emoji, gitmoji.description, gitmoji.name)) } } + GitmojiLocale.loadTranslations() } } diff --git a/src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt b/src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt index ac71954..8bce93a 100644 --- a/src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt +++ b/src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt @@ -59,7 +59,7 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable { textAfterUnicodePanel.add(textAfterUnicode, null) mainPanel.add(textAfterUnicodePanel) val languageJPanel = JPanel(FlowLayout(FlowLayout.LEADING)) - languageJPanel.add(JLabel("Language (Take effect after restart) ")) + languageJPanel.add(JLabel("Language")) languageJPanel.add(languages, null) mainPanel.add(languageJPanel) } @@ -84,6 +84,7 @@ class GitMojiConfig(private val project: Project) : SearchableConfigurable { projectInstance.setValue(CONFIG_INCLUDE_GITMOJI_DESCRIPTION, includeGitMojiDescriptionConfig) projectInstance.setValue(CONFIG_AFTER_UNICODE, textAfterUnicodeConfig) instance.setValue(CONFIG_LANGUAGE, languagesConfig) + GitmojiLocale.loadTranslations() } override fun reset() { diff --git a/src/main/kotlin/com/github/patou/gitmoji/GitmojiData.kt b/src/main/kotlin/com/github/patou/gitmoji/GitmojiData.kt index 8842489..b60431c 100644 --- a/src/main/kotlin/com/github/patou/gitmoji/GitmojiData.kt +++ b/src/main/kotlin/com/github/patou/gitmoji/GitmojiData.kt @@ -10,7 +10,7 @@ const val CONFIG_LANGUAGE: String = "com.github.patou.gitmoji.language" const val CONFIG_INSERT_IN_CURSOR_POSITION: String = "com.github.patou.gitmoji.insert-in-cursor-position" const val CONFIG_INCLUDE_GITMOJI_DESCRIPTION: String = "com.github.patou.gitmoji.include-gitmoji-description" -data class GitmojiData(val code: String, val emoji: String, val description: String) { +data class GitmojiData(val code: String, val emoji: String, val description: String, val name: String) { private lateinit var _icon: Icon fun getIcon(): Icon { @@ -28,4 +28,7 @@ data class GitmojiData(val code: String, val emoji: String, val description: Str } return _icon } + + val localeDescription: String + get() = GitmojiLocale.t(name, description) } diff --git a/src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt b/src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt index d384459..fdaeb62 100644 --- a/src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt +++ b/src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt @@ -6,47 +6,48 @@ import org.yaml.snakeyaml.Yaml import java.io.IOException import java.util.* -class GitmojiLocale { +object GitmojiLocale { - private var map: Map = emptyMap() + private var translations: MutableMap = HashMap() - companion object { - val LANGUAGE_CONFIG_LIST = arrayOf("auto", "en_US", "zh_CN") - } + val LANGUAGE_CONFIG_LIST = arrayOf("auto", "en_US", "zh_CN") fun t(name: String, description: String): String { - if (map.isEmpty()) { + if (translations.isEmpty()) { return description } // Maybe not very elegant, maybe it can be optimized ? - return (map["gitmojis"] as Map<*, *>)[name]?.toString() ?: return description + return translations[name] ?: return description } - fun loadMap(onMapLoaded:() -> Unit) { + fun loadTranslations() { + translations.clear() val instance = PropertiesComponent.getInstance() - val language = instance.getValue(CONFIG_LANGUAGE) - var defaultLanguage = Locale.getDefault().toString() - if (language != null && language != "auto") { - defaultLanguage = language.toString() + var language = instance.getValue(CONFIG_LANGUAGE) ?: "auto" + if (language == "auto") { + val defaultLanguage = Locale.getDefault().toString() + if (LANGUAGE_CONFIG_LIST.contains(defaultLanguage)) { + language = defaultLanguage + } + else { + language = "en_US" + } } val client = OkHttpClient().newBuilder().addInterceptor(SafeGuardInterceptor()).build() val request: Request = Request.Builder() - .url("https://raw.githubusercontent.com/caiyucong/gitmoji-intellij-plugin-chinese/t/gitmojis-${defaultLanguage}.yaml") + .url("https://raw.githubusercontent.com/patou/gitmoji-intellij-plugin/master/src/main/resources/gitmojis-${language}.yaml") .build() client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { - loadLocalYaml(defaultLanguage) - onMapLoaded() + loadLocalYaml(language) } override fun onResponse(call: Call, response: Response) { response.use { if (!response.isSuccessful) { - loadLocalYaml(defaultLanguage) - onMapLoaded() + loadLocalYaml(language) } else { loadYaml(response.body!!.string()) - onMapLoaded() } } } @@ -58,7 +59,7 @@ class GitmojiLocale { val yaml = Yaml() javaClass.getResourceAsStream("/gitmojis-${language}.yaml").use { inputStream -> if (inputStream != null) { - map = yaml.loadAs(inputStream, HashMap::class.java) + addTranslation(yaml.loadAs(inputStream, HashMap::class.java)) } } } @@ -66,6 +67,16 @@ class GitmojiLocale { // load remote yaml private fun loadYaml(text: String) { val yaml = Yaml() - map = yaml.loadAs(text, HashMap::class.java) + addTranslation(yaml.loadAs(text, HashMap::class.java)) + } + + private fun addTranslation(loadedTranslation : HashMap) { + loadedTranslation["gitmojis"]?.let { it -> + if (it is Map<*, *>) { + it.forEach { (key, value) -> + translations[key.toString()] = value.toString() + } + } + } } }