From b82b99520fb7cec381b4251851809822eb78e087 Mon Sep 17 00:00:00 2001 From: liying2008 Date: Sun, 31 May 2020 22:36:11 +0800 Subject: [PATCH] Refactor some code --- .../java/cc/duduhuo/git/conflict/Global.kt | 14 +++++++--- .../git/conflict/InDocumentListener.kt | 7 ++--- .../java/cc/duduhuo/git/conflict/TextAttr.kt | 16 ++++++------ .../action/HighlightConflictAction.kt | 6 ++--- .../setting/ColorSettingsPanelImpl.kt | 26 +++++++++---------- .../duduhuo/git/conflict/tool/BundleTools.kt | 6 ++--- .../git/conflict/tool/DocumentTools.kt | 17 ++++++------ .../cc/duduhuo/git/conflict/tool/Tools.kt | 12 ++++----- 8 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/main/java/cc/duduhuo/git/conflict/Global.kt b/src/main/java/cc/duduhuo/git/conflict/Global.kt index 8b2899a..9674d71 100644 --- a/src/main/java/cc/duduhuo/git/conflict/Global.kt +++ b/src/main/java/cc/duduhuo/git/conflict/Global.kt @@ -1,6 +1,7 @@ package cc.duduhuo.git.conflict import cc.duduhuo.git.conflict.model.ConflictItem +import cc.duduhuo.git.conflict.model.MarkColor import com.intellij.openapi.editor.Document import com.intellij.openapi.editor.Editor @@ -14,13 +15,18 @@ import com.intellij.openapi.editor.Editor */ object Global { // is it highlighted - var sIsHighlightMap: MutableMap = mutableMapOf() + val sIsHighlightMap: MutableMap = mutableMapOf() // conflict item list - var sConflictItemMap: MutableMap> = mutableMapOf() + val sConflictItemMap: MutableMap> = mutableMapOf() - var sDocumentListenerMap: MutableMap = mutableMapOf() + // document listener map + val sDocumentListenerMap: MutableMap = mutableMapOf() // current color scheme - var sCurrentColor = GlobalSettings.getPersistentState().markColors[GlobalSettings.getPersistentState().schemeName] + var sCurrentColor: MarkColor = MarkColor() + get() { + val state = GlobalSettings.getPersistentState() + return state.markColors[state.schemeName]!! + } } diff --git a/src/main/java/cc/duduhuo/git/conflict/InDocumentListener.kt b/src/main/java/cc/duduhuo/git/conflict/InDocumentListener.kt index a63ad30..ebd08e1 100644 --- a/src/main/java/cc/duduhuo/git/conflict/InDocumentListener.kt +++ b/src/main/java/cc/duduhuo/git/conflict/InDocumentListener.kt @@ -1,6 +1,7 @@ package cc.duduhuo.git.conflict import cc.duduhuo.git.conflict.Global.sConflictItemMap +import cc.duduhuo.git.conflict.Global.sDocumentListenerMap import cc.duduhuo.git.conflict.Global.sIsHighlightMap import cc.duduhuo.git.conflict.tool.DocumentTools.showConflict import com.intellij.openapi.editor.Editor @@ -23,11 +24,11 @@ class InDocumentListener(private val mEditor: Editor) : DocumentListener { val document = mEditor.document sIsHighlightMap[mEditor] = false sConflictItemMap.remove(document) - val listener = Global.sDocumentListenerMap[document] + val listener = sDocumentListenerMap[document] if (listener != null) { document.removeDocumentListener(listener) - Global.sDocumentListenerMap.remove(document) + sDocumentListenerMap.remove(document) } } } -} \ No newline at end of file +} diff --git a/src/main/java/cc/duduhuo/git/conflict/TextAttr.kt b/src/main/java/cc/duduhuo/git/conflict/TextAttr.kt index 6e3463c..17be595 100644 --- a/src/main/java/cc/duduhuo/git/conflict/TextAttr.kt +++ b/src/main/java/cc/duduhuo/git/conflict/TextAttr.kt @@ -27,32 +27,32 @@ object TextAttr { sCurrentTitleAttributes = TextAttributes( null, JBColor( - Global.sCurrentColor!!.currentTitleColor, - Global.sCurrentColor!!.darkCurrentTitleColor + Global.sCurrentColor.currentTitleColor, + Global.sCurrentColor.darkCurrentTitleColor ), null, EffectType.ROUNDED_BOX, Font.PLAIN ) sCurrentAttributes = TextAttributes( null, JBColor( - Global.sCurrentColor!!.currentColor, - Global.sCurrentColor!!.darkCurrentColor + Global.sCurrentColor.currentColor, + Global.sCurrentColor.darkCurrentColor ), null, EffectType.ROUNDED_BOX, Font.PLAIN ) sIncomingTitleAttributes = TextAttributes( null, JBColor( - Global.sCurrentColor!!.incomingTitleColor, - Global.sCurrentColor!!.darkIncomingTitleColor + Global.sCurrentColor.incomingTitleColor, + Global.sCurrentColor.darkIncomingTitleColor ), null, EffectType.ROUNDED_BOX, Font.PLAIN ) sIncomingAttributes = TextAttributes( null, JBColor( - Global.sCurrentColor!!.incomingColor, - Global.sCurrentColor!!.darkIncomingColor + Global.sCurrentColor.incomingColor, + Global.sCurrentColor.darkIncomingColor ), null, EffectType.ROUNDED_BOX, Font.PLAIN ) diff --git a/src/main/java/cc/duduhuo/git/conflict/action/HighlightConflictAction.kt b/src/main/java/cc/duduhuo/git/conflict/action/HighlightConflictAction.kt index f9ea68c..af0fef5 100644 --- a/src/main/java/cc/duduhuo/git/conflict/action/HighlightConflictAction.kt +++ b/src/main/java/cc/duduhuo/git/conflict/action/HighlightConflictAction.kt @@ -9,7 +9,6 @@ import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys -import com.intellij.openapi.editor.Editor /** * ======================================================= @@ -23,9 +22,8 @@ class HighlightConflictAction : AnAction() { companion object { fun refreshHighlight() { - val keySet: Set = sIsHighlightMap.keys - for (editor in keySet) { - if (sIsHighlightMap[editor]!!) { + sIsHighlightMap.forEach { (editor, isHighlight) -> + if (isHighlight) { showConflict(editor) } } diff --git a/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.kt b/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.kt index 13c07e1..8c6c751 100644 --- a/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.kt +++ b/src/main/java/cc/duduhuo/git/conflict/setting/ColorSettingsPanelImpl.kt @@ -44,30 +44,30 @@ class ColorSettingsPanelImpl : ColorSettingsPanel() { private fun initUI() { val persistentState = GlobalSettings.getPersistentState() - val map = persistentState.markColors + val markColors = persistentState.markColors mSchemeName = persistentState.schemeName // clone markColors map @SuppressWarnings("unchecked") - mMarkColors = map.clone() as LinkedHashMap + mMarkColors = markColors.clone() as LinkedHashMap // make sure the built-in color is displayed at the top. cbColorScheme.addItem(BuiltInColor.AUTO_SCHEME_NAME) cbColorScheme.addItem(BuiltInColor.INTELLIJ_SCHEME_NAME) cbColorScheme.addItem(BuiltInColor.DARCULA_SCHEME_NAME) - val keySet: Set = map.keys - for (key in keySet) { - if (key != BuiltInColor.AUTO_SCHEME_NAME && - key != BuiltInColor.INTELLIJ_SCHEME_NAME && - key != BuiltInColor.DARCULA_SCHEME_NAME + + markColors.forEach { (schemeName, markColor) -> + if (schemeName != BuiltInColor.AUTO_SCHEME_NAME && + schemeName != BuiltInColor.INTELLIJ_SCHEME_NAME && + schemeName != BuiltInColor.DARCULA_SCHEME_NAME ) { - cbColorScheme.addItem(key) + cbColorScheme.addItem(schemeName) } - mAllSchemeNames.add(key) + mAllSchemeNames.add(schemeName) } // Double check. - if (keySet.contains(mSchemeName)) { + if (mSchemeName in markColors) { cbColorScheme.selectedItem = mSchemeName - mOldMarkColor.copy(map[mSchemeName]!!) + mOldMarkColor.copy(markColors[mSchemeName]!!) } else { cbColorScheme.selectedItem = BuiltInColor.DEFAULT_SCHEME_NAME mOldMarkColor.copy(BuiltInColor.DEFAULT_MARK_COLOR) @@ -158,9 +158,9 @@ class ColorSettingsPanelImpl : ColorSettingsPanel() { JOptionPane.OK_CANCEL_OPTION ) if (confirm == JOptionPane.OK_OPTION) { - val markColor = mMarkColors[selectedItem] + val markColor = mMarkColors[selectedItem]!! // Double check. Prevent the built-in color scheme from being deleted. - if (!markColor!!.isBuiltIn) { + if (!markColor.isBuiltIn) { mMarkColors.remove(selectedItem) cbColorScheme.removeItem(selectedItem) mAllSchemeNames.remove(selectedItem) diff --git a/src/main/java/cc/duduhuo/git/conflict/tool/BundleTools.kt b/src/main/java/cc/duduhuo/git/conflict/tool/BundleTools.kt index 534d806..1b540a9 100644 --- a/src/main/java/cc/duduhuo/git/conflict/tool/BundleTools.kt +++ b/src/main/java/cc/duduhuo/git/conflict/tool/BundleTools.kt @@ -1,6 +1,6 @@ package cc.duduhuo.git.conflict.tool -import com.intellij.CommonBundle +import com.intellij.AbstractBundle import java.util.* /** @@ -16,6 +16,6 @@ object BundleTools { private var sBundle: ResourceBundle = ResourceBundle.getBundle(BUNDLE_FILE) fun getValue(key: String): String { - return CommonBundle.message(sBundle, key, "%s") + return AbstractBundle.message(sBundle, key, "%s") } -} \ No newline at end of file +} diff --git a/src/main/java/cc/duduhuo/git/conflict/tool/DocumentTools.kt b/src/main/java/cc/duduhuo/git/conflict/tool/DocumentTools.kt index 384e340..97cf24e 100644 --- a/src/main/java/cc/duduhuo/git/conflict/tool/DocumentTools.kt +++ b/src/main/java/cc/duduhuo/git/conflict/tool/DocumentTools.kt @@ -28,29 +28,28 @@ object DocumentTools { val markupModel = editor.markupModel val text = document.text val textArr = text.split("\n") - val lineCount = textArr.size markupModel.removeAllHighlighters() sConflictItemMap.remove(document) var currentChangeLineNum = -1 var separatorLineNum = -1 var incomingLineNum = -1 val conflictItems: MutableList = mutableListOf() - for (i in 0 until lineCount) { - if (textArr[i].startsWith(Constants.CURRENT_CHANGE)) { - currentChangeLineNum = i + textArr.forEachIndexed { index, line -> + if (line.startsWith(Constants.CURRENT_CHANGE)) { + currentChangeLineNum = index separatorLineNum = -1 incomingLineNum = -1 - } else if (textArr[i].startsWith(Constants.SEPARATOR)) { + } else if (line.startsWith(Constants.SEPARATOR)) { if (currentChangeLineNum > -1 && separatorLineNum == -1) { - separatorLineNum = i + separatorLineNum = index } else { currentChangeLineNum = -1 separatorLineNum = -1 } - } else if (textArr[i].startsWith(Constants.INCOMING_CHANGE)) { + } else if (line.startsWith(Constants.INCOMING_CHANGE)) { if (separatorLineNum > -1) { hasConflict = true - incomingLineNum = i + incomingLineNum = index // get conflict content val currentStartOffset = document.getLineStartOffset(currentChangeLineNum + 1) val currentEndOffset = document.getLineEndOffset(separatorLineNum - 1) @@ -104,4 +103,4 @@ object DocumentTools { sConflictItemMap[document] = conflictItems return hasConflict } -} \ No newline at end of file +} diff --git a/src/main/java/cc/duduhuo/git/conflict/tool/Tools.kt b/src/main/java/cc/duduhuo/git/conflict/tool/Tools.kt index 4187f14..335cde5 100644 --- a/src/main/java/cc/duduhuo/git/conflict/tool/Tools.kt +++ b/src/main/java/cc/duduhuo/git/conflict/tool/Tools.kt @@ -28,12 +28,12 @@ object Tools { } fun int2HexString(color: Int): String { - val s = StringBuilder(Integer.toHexString(color)) - val count = 6 - s.length + val sb = StringBuilder(Integer.toHexString(color)) + val count = 6 - sb.length for (i in 0 until count) { - s.insert(0, "0") + sb.insert(0, "0") } - s.insert(0, "0x") - return s.toString() + sb.insert(0, "0x") + return sb.toString() } -} \ No newline at end of file +}