avoid getParent* - call convert explicitly (part 2)

This commit is contained in:
Vladimir Krivosheev
2016-12-09 13:57:29 +01:00
parent 7bd46db65d
commit caaa375835
2 changed files with 21 additions and 34 deletions
@@ -187,8 +187,6 @@ open class KeymapImpl @JvmOverloads constructor(private var dataHolder: SchemeDa
override final fun canModify() = canModify
protected open fun getParentShortcuts(actionId: String) = parent!!.getMutableShortcutList(actionId)
override fun addShortcut(actionId: String, shortcut: Shortcut) {
val list = actionIdToShortcuts.getOrPut(actionId) {
val result = SmartList<Shortcut>()
@@ -206,7 +204,7 @@ open class KeymapImpl @JvmOverloads constructor(private var dataHolder: SchemeDa
list.add(shortcut)
}
if (parent != null && areShortcutsEqual(getParentShortcuts(actionId), list)) {
if (list.areShortcutsEqualToParent(actionId)) {
actionIdToShortcuts.remove(actionId)
}
@@ -228,11 +226,8 @@ open class KeymapImpl @JvmOverloads constructor(private var dataHolder: SchemeDa
override fun removeShortcut(actionId: String, toDelete: Shortcut) {
val list = actionIdToShortcuts.get(actionId)
if (list == null) {
var inherited: List<Shortcut>? = keymapManager.getActionBinding(actionId)?.let { actionIdToShortcuts.get(it) }
if (inherited == null && parent != null) {
inherited = getParentShortcuts(actionId).nullize()
}
val inherited = keymapManager.getActionBinding(actionId)?.let { actionIdToShortcuts.get(it) }
?: parent?.getMutableShortcutList(actionId)?.mapSmart { convertShortcut(it) }.nullize()
if (inherited != null) {
var newShortcuts: MutableList<Shortcut>? = null
for (itemIndex in 0..inherited.lastIndex) {
@@ -255,26 +250,21 @@ open class KeymapImpl @JvmOverloads constructor(private var dataHolder: SchemeDa
}
}
else {
val it = list.iterator()
while (it.hasNext()) {
val each = it.next()
if (toDelete == each) {
it.remove()
val parent = parent
val isRemove = if (parent == null) {
list.isEmpty()
val index = list.indexOf(toDelete)
if (index >= 0) {
if (parent == null) {
if (list.size == 1) {
actionIdToShortcuts.remove(actionId)
}
else {
actionIdToShortcuts.get(actionId)?.let {
areShortcutsEqual(it, parent.getMutableShortcutList(actionId).mapSmart { convertShortcut(it) })
} ?: false
list.removeAt(index)
}
if (isRemove) {
}
else {
list.removeAt(index)
if (list.areShortcutsEqualToParent(actionId)) {
actionIdToShortcuts.remove(actionId)
}
break
}
}
}
@@ -283,6 +273,8 @@ open class KeymapImpl @JvmOverloads constructor(private var dataHolder: SchemeDa
fireShortcutChanged(actionId)
}
private fun MutableList<Shortcut>.areShortcutsEqualToParent(actionId: String) = parent.let { parent -> parent != null && areShortcutsEqual(this, parent.getMutableShortcutList(actionId).mapSmart { convertShortcut(it) }) }
private val gestureToListOfIds: Map<KeyboardModifierGestureShortcut, List<String>> by lazy { fillShortcutToListOfIds(KeyboardModifierGestureShortcut::class.java) }
private fun <T : Shortcut> fillShortcutToListOfIds(shortcutClass: Class<T>): Map<T, MutableList<String>> {
@@ -444,9 +436,9 @@ open class KeymapImpl @JvmOverloads constructor(private var dataHolder: SchemeDa
return emptyList()
}
// it is critical to use getParentShortcuts - otherwise MacOSDefaultKeymap doesn't convert shortcuts
// it is critical to use convertShortcut - otherwise MacOSDefaultKeymap doesn't convert shortcuts
// todo why not convert on add? why we don't need to convert our own shortcuts?
return actionIdToShortcuts.get(actionId) ?: keymapManager.getActionBinding(actionId)?.let { actionIdToShortcuts.get(it) } ?: parent?.let { getParentShortcuts(actionId) } ?: emptyList()
return actionIdToShortcuts.get(actionId) ?: keymapManager.getActionBinding(actionId)?.let { actionIdToShortcuts.get(it) } ?: parent?.getMutableShortcutList(actionId)?.mapSmart { convertShortcut(it) } ?: emptyList()
}
fun getOwnShortcuts(actionId: String): Array<Shortcut> {
@@ -19,7 +19,6 @@ import com.intellij.configurationStore.SchemeDataHolder
import com.intellij.openapi.actionSystem.KeyboardShortcut
import com.intellij.openapi.actionSystem.MouseShortcut
import com.intellij.openapi.actionSystem.Shortcut
import com.intellij.util.containers.mapSmart
import org.intellij.lang.annotations.JdkConstants
import java.awt.event.InputEvent
import javax.swing.KeyStroke
@@ -27,13 +26,11 @@ import javax.swing.KeyStroke
class MacOSDefaultKeymap(dataHolder: SchemeDataHolder<KeymapImpl>) : DefaultKeymapImpl(dataHolder) {
companion object {
@JvmStatic
fun convertShortcutFromParent(parentShortcut: Shortcut): Shortcut {
if (parentShortcut is MouseShortcut) {
return _convertMouseShortcut(parentShortcut)
fun convertShortcutFromParent(shortcut: Shortcut): Shortcut {
if (shortcut is MouseShortcut) {
return _convertMouseShortcut(shortcut)
}
val key = parentShortcut as KeyboardShortcut
return KeyboardShortcut(_convertKeyStroke(key.firstKeyStroke), key.secondKeyStroke?.let(::_convertKeyStroke))
return KeyboardShortcut(_convertKeyStroke((shortcut as KeyboardShortcut).firstKeyStroke), shortcut.secondKeyStroke?.let(::_convertKeyStroke))
}
}
@@ -42,8 +39,6 @@ class MacOSDefaultKeymap(dataHolder: SchemeDataHolder<KeymapImpl>) : DefaultKeym
override fun convertMouseShortcut(shortcut: MouseShortcut) = _convertMouseShortcut(shortcut)
override fun convertShortcut(shortcut: Shortcut) = convertShortcutFromParent(shortcut)
override fun getParentShortcuts(actionId: String) = super.getParentShortcuts(actionId).mapSmart { convertShortcutFromParent(it) }
}
private fun _convertKeyStroke(parentKeyStroke: KeyStroke): KeyStroke = KeyStroke.getKeyStroke(parentKeyStroke.keyCode, mapModifiers(parentKeyStroke.modifiers), parentKeyStroke.isOnKeyRelease)