11package com.github.patou.gitmoji
22
3- import com.github.patou.gitmoji.Gitmojis.Companion.insertAt
4- import com.google.gson.Gson
53import com.intellij.ide.TextCopyProvider
64import com.intellij.ide.util.PropertiesComponent
5+ import com.intellij.openapi.actionSystem.ActionUpdateThread
76import com.intellij.openapi.actionSystem.AnAction
87import com.intellij.openapi.actionSystem.AnActionEvent
98import com.intellij.openapi.actionSystem.PlatformDataKeys
@@ -30,19 +29,14 @@ import com.intellij.util.ObjectUtils.sentinel
3029import com.intellij.util.containers.nullize
3130import com.intellij.util.ui.JBUI.scale
3231import com.intellij.vcs.commit.message.CommitMessageInspectionProfile.getSubjectRightMargin
33- import okhttp3.*
34- import okhttp3.Request.Builder
3532import java.awt.Point
36- import java.io.IOException
3733import javax.swing.JList
3834import javax.swing.ListSelectionModel
3935
4036class GitCommitAction : AnAction () {
41- private val gitmojis = ArrayList <GitmojiData >()
4237
4338 init {
4439 isEnabledInModalContext = true
45- loadGitmojiFromHTTP()
4640 }
4741
4842 private val regexPattern = " :[a-z0-9_]+:"
@@ -56,12 +50,13 @@ class GitCommitAction : AnAction() {
5650 val commitMessage = getCommitMessage(actionEvent)
5751 when {
5852 commitMessage != null && project != null -> {
59- createPopup(project, commitMessage, gitmojis)
53+ createPopup(project, commitMessage, Gitmojis . gitmojis)
6054 .showInBestPositionFor(actionEvent.dataContext)
6155 }
6256 }
6357 }
6458
59+ @Suppress(" MissingActionUpdateThread" )
6560 private fun createPopup (
6661 project : Project ,
6762 commitMessage : CommitMessage ,
@@ -73,7 +68,7 @@ class GitCommitAction : AnAction() {
7368 val previewCommandGroup = sentinel(" Preview Commit Message" )
7469 val projectInstance = PropertiesComponent .getInstance(project)
7570 val displayEmoji =
76- projectInstance.getValue(CONFIG_DISPLAY_ICON , Gitmojis . defaultDisplayType()) == " emoji"
71+ projectInstance.getValue(CONFIG_DISPLAY_ICON , defaultDisplayType()) == " emoji"
7772 val currentCommitMessage = commitMessage.editorField.text
7873 val currentOffset = commitMessage.editorField.caretModel.offset
7974
@@ -172,7 +167,7 @@ class GitCommitAction : AnAction() {
172167 var replaced = false
173168 if (! insertInCarretPosition) {
174169 if (useUnicode) {
175- for (moji in gitmojis) {
170+ for (moji in Gitmojis . gitmojis) {
176171 if (message.contains(" ${moji.emoji}$textAfterUnicode " )) {
177172 message = message.replaceFirst(
178173 " ${moji.emoji}$textAfterUnicode " ,
@@ -224,43 +219,8 @@ class GitCommitAction : AnAction() {
224219 private fun getCommitMessage (e : AnActionEvent ) =
225220 e.getData(VcsDataKeys .COMMIT_MESSAGE_CONTROL ) as ? CommitMessage
226221
227- private fun loadGitmojiFromHTTP () {
228- val client = OkHttpClient ().newBuilder().addInterceptor(SafeGuardInterceptor ()).build()
229- val request: Request = Builder ()
230- .url(" https://gitmoji.dev/api/gitmojis" )
231- .build()
232- client.newCall(request).enqueue(object : Callback {
233- override fun onFailure (call : Call , e : IOException ) {
234- loadDefaultGitmoji()
235- }
236-
237- override fun onResponse (call : Call , response : Response ) {
238- response.use {
239- if (! response.isSuccessful) loadDefaultGitmoji()
240- else {
241- loadGitmoji(response.body!! .string())
242- }
243- }
244- }
245- })
246- }
247-
248- private fun loadDefaultGitmoji () {
249- javaClass.getResourceAsStream(" /gitmojis.json" ).use { inputStream ->
250- if (inputStream != null ) {
251- val text = inputStream.bufferedReader().readText()
252- loadGitmoji(text)
253- }
254- }
255- }
256-
257- private fun loadGitmoji (text : String ) {
258- Gson ().fromJson(text, Gitmojis ::class .java).also {
259- it.gitmojis.forEach { gitmoji ->
260- gitmojis.add(GitmojiData (gitmoji.code, gitmoji.emoji, gitmoji.description, gitmoji.name))
261- }
262- }
263- GitmojiLocale .loadTranslations()
222+ override fun getActionUpdateThread (): ActionUpdateThread {
223+ return ActionUpdateThread .BGT
264224 }
265225
266226}
0 commit comments