Skip to content

Commit

Permalink
Merge pull request #1 from patou/caiyucong/master
Browse files Browse the repository at this point in the history
♻️ Some refactor and improvement
  • Loading branch information
yuki-sakura-chan authored Apr 2, 2024
2 parents f27de1a + e133a62 commit 16a49f6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
16 changes: 7 additions & 9 deletions src/main/kotlin/com/github/patou/gitmoji/GitCommitAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class GitCommitAction : AnAction() {
appendTextPadding(5)
append(
first(
convertLineSeparators(value.description, RETURN_SYMBOL),
convertLineSeparators(value.localeDescription, RETURN_SYMBOL),
rightMargin,
false
)
Expand Down Expand Up @@ -134,7 +134,7 @@ class GitCommitAction : AnAction() {
}
}
})
.setNamerForFiltering { "${it.code} ${it.description}" }
.setNamerForFiltering { "${it.code} ${it.localeDescription} ${it.description}" }
.setAutoPackHeightOnFiltering(false)
.createPopup()
.apply {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}

}
3 changes: 2 additions & 1 deletion src/main/kotlin/com/github/patou/gitmoji/GitMojiConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/com/github/patou/gitmoji/GitmojiData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
51 changes: 31 additions & 20 deletions src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,48 @@ import org.yaml.snakeyaml.Yaml
import java.io.IOException
import java.util.*

class GitmojiLocale {
object GitmojiLocale {

private var map: Map<String, Any> = emptyMap()
private var translations: MutableMap<String, String> = 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)) {

Check notice on line 29 in src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt

View workflow job for this annotation

GitHub Actions / Inspect code

Return or assignment can be lifted out

'Assignment' can be lifted out of 'if'
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()
}
}
}
Expand All @@ -58,14 +59,24 @@ 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))
}
}
}

// 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<String, Any>) {
loadedTranslation["gitmojis"]?.let { it ->

Check notice on line 74 in src/main/kotlin/com/github/patou/gitmoji/GitmojiLocale.kt

View workflow job for this annotation

GitHub Actions / Inspect code

Redundant lambda arrow

Redundant lambda arrow
if (it is Map<*, *>) {
it.forEach { (key, value) ->
translations[key.toString()] = value.toString()
}
}
}
}
}

0 comments on commit 16a49f6

Please sign in to comment.