Skip to content

Commit

Permalink
Refactor some code
Browse files Browse the repository at this point in the history
  • Loading branch information
liying2008 committed May 31, 2020
1 parent 0a93d02 commit b82b995
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 50 deletions.
14 changes: 10 additions & 4 deletions src/main/java/cc/duduhuo/git/conflict/Global.kt
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -14,13 +15,18 @@ import com.intellij.openapi.editor.Editor
*/
object Global {
// is it highlighted
var sIsHighlightMap: MutableMap<Editor, Boolean> = mutableMapOf()
val sIsHighlightMap: MutableMap<Editor, Boolean> = mutableMapOf()

// conflict item list
var sConflictItemMap: MutableMap<Document, MutableList<ConflictItem>> = mutableMapOf()
val sConflictItemMap: MutableMap<Document, MutableList<ConflictItem>> = mutableMapOf()

var sDocumentListenerMap: MutableMap<Document, InDocumentListener> = mutableMapOf()
// document listener map
val sDocumentListenerMap: MutableMap<Document, InDocumentListener> = 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]!!
}
}
7 changes: 4 additions & 3 deletions src/main/java/cc/duduhuo/git/conflict/InDocumentListener.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/cc/duduhuo/git/conflict/TextAttr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
* =======================================================
Expand All @@ -23,9 +22,8 @@ class HighlightConflictAction : AnAction() {

companion object {
fun refreshHighlight() {
val keySet: Set<Editor> = sIsHighlightMap.keys
for (editor in keySet) {
if (sIsHighlightMap[editor]!!) {
sIsHighlightMap.forEach { (editor, isHighlight) ->
if (isHighlight) {
showConflict(editor)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, MarkColor>
mMarkColors = markColors.clone() as LinkedHashMap<String, MarkColor>

// 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<String> = 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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/cc/duduhuo/git/conflict/tool/BundleTools.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cc.duduhuo.git.conflict.tool

import com.intellij.CommonBundle
import com.intellij.AbstractBundle
import java.util.*

/**
Expand All @@ -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")
}
}
}
17 changes: 8 additions & 9 deletions src/main/java/cc/duduhuo/git/conflict/tool/DocumentTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConflictItem> = 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)
Expand Down Expand Up @@ -104,4 +103,4 @@ object DocumentTools {
sConflictItemMap[document] = conflictItems
return hasConflict
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/cc/duduhuo/git/conflict/tool/Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}

0 comments on commit b82b995

Please sign in to comment.