1
0
mirror of https://github.com/chylex/IntelliJ-Keyboard-Master.git synced 2024-11-25 01:42:47 +01:00

Compare commits

..

2 Commits

8 changed files with 354 additions and 167 deletions

View File

@ -23,9 +23,9 @@ internal interface KeyStrokeNode<T> {
} }
fun getChild(keyEvent: KeyEvent): KeyStrokeNode<T> { fun getChild(keyEvent: KeyEvent): KeyStrokeNode<T> {
val keyStroke = when (keyEvent.id) { val keyStroke = when {
KeyEvent.KEY_TYPED -> KeyStroke.getKeyStroke(keyEvent.keyChar, keyEvent.modifiersEx and KeyEvent.SHIFT_DOWN_MASK.inv()) keyEvent.keyChar != KeyEvent.CHAR_UNDEFINED -> KeyStroke.getKeyStroke(keyEvent.keyChar, keyEvent.modifiersEx and KeyEvent.SHIFT_DOWN_MASK.inv())
KeyEvent.KEY_PRESSED -> KeyStroke.getKeyStroke(keyEvent.keyCode, keyEvent.modifiersEx, false) keyEvent.id == KeyEvent.KEY_PRESSED -> KeyStroke.getKeyStroke(keyEvent.keyCode, keyEvent.modifiersEx, false)
else -> return this else -> return this
} }
@ -63,7 +63,7 @@ internal interface KeyStrokeNode<T> {
} }
companion object { companion object {
fun <T> getAllShortcuts(root: Parent<T>, extra: Set<KeyStroke>? = null): CustomShortcutSet { fun <T> getAllKeyStrokes(root: Parent<T>, extra: Set<KeyStroke>? = null): Set<KeyStroke> {
val allKeyStrokes = HashSet(root.allKeyStrokes) val allKeyStrokes = HashSet(root.allKeyStrokes)
if (extra != null) { if (extra != null) {
@ -74,7 +74,11 @@ internal interface KeyStrokeNode<T> {
allKeyStrokes.add(KeyStroke.getKeyStroke(c)) allKeyStrokes.add(KeyStroke.getKeyStroke(c))
} }
return CustomShortcutSet(*allKeyStrokes.map2Array { KeyboardShortcut(it, null) }) return allKeyStrokes
}
fun getAllShortcuts(keyStrokes: Set<KeyStroke>): CustomShortcutSet {
return CustomShortcutSet(*keyStrokes.map2Array { KeyboardShortcut(it, null) })
} }
} }
} }

View File

@ -0,0 +1,19 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimListNavigation
import com.intellij.ui.UiInterceptors.PersistentUiInterceptor
import com.intellij.ui.awt.RelativePoint
import com.intellij.ui.popup.AbstractPopup
import com.intellij.ui.popup.list.ListPopupImpl
internal object PopupInterceptor : PersistentUiInterceptor<AbstractPopup>(AbstractPopup::class.java) {
override fun shouldIntercept(component: AbstractPopup): Boolean {
if (component is ListPopupImpl) {
VimListNavigation.install(component.list, component)
}
return false
}
override fun doIntercept(component: AbstractPopup, owner: RelativePoint?) {}
}

View File

@ -1,25 +1,35 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation package com.chylex.intellij.keyboardmaster.feature.vimNavigation
import com.chylex.intellij.keyboardmaster.PluginDisposableService import com.chylex.intellij.keyboardmaster.PluginDisposableService
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimListNavigation
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimTableNavigation
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimTreeNavigation
import com.intellij.ide.ApplicationInitializedListener import com.intellij.ide.ApplicationInitializedListener
import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ApplicationManager
import com.intellij.ui.UiInterceptors
import com.intellij.util.ui.StartupUiUtil import com.intellij.util.ui.StartupUiUtil
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import java.awt.AWTEvent import java.awt.AWTEvent
import java.awt.event.FocusEvent import java.awt.event.FocusEvent
import javax.swing.JList
import javax.swing.JTable
import javax.swing.JTree import javax.swing.JTree
@Suppress("UnstableApiUsage") @Suppress("UnstableApiUsage")
internal class VimNavigationApplicationListener : ApplicationInitializedListener { internal class VimNavigationApplicationListener : ApplicationInitializedListener {
override suspend fun execute(asyncScope: CoroutineScope) { override suspend fun execute(asyncScope: CoroutineScope) {
StartupUiUtil.addAwtListener(::handleEvent, AWTEvent.FOCUS_EVENT_MASK, ApplicationManager.getApplication().getService(PluginDisposableService::class.java)) val disposable = ApplicationManager.getApplication().getService(PluginDisposableService::class.java)
StartupUiUtil.addAwtListener(::handleEvent, AWTEvent.FOCUS_EVENT_MASK, disposable)
UiInterceptors.registerPersistent(disposable, PopupInterceptor)
} }
private fun handleEvent(event: AWTEvent) { private fun handleEvent(event: AWTEvent) {
if (event is FocusEvent && event.id == FocusEvent.FOCUS_GAINED) { if (event is FocusEvent && event.id == FocusEvent.FOCUS_GAINED) {
val source = event.source when (val source = event.source) {
if (source is JTree) { is JList<*> -> VimListNavigation.install(source)
VimTreeNavigation.install(source) is JTree -> VimTreeNavigation.install(source)
is JTable -> VimTableNavigation.install(source)
} }
} }
} }

View File

@ -0,0 +1,97 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation
import com.chylex.intellij.keyboardmaster.PluginDisposableService
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.ui.SpeedSearchBase
import com.intellij.ui.speedSearch.SpeedSearch
import com.intellij.ui.speedSearch.SpeedSearchActivator
import com.intellij.ui.speedSearch.SpeedSearchSupply
import java.awt.event.KeyEvent
import java.util.concurrent.atomic.AtomicBoolean
import javax.swing.JComponent
import javax.swing.KeyStroke
internal open class VimNavigationDispatcher<T : JComponent>(final override val component: T, private val rootNode: KeyStrokeNode.Parent<VimNavigationDispatcher<T>>) : DumbAwareAction(), ComponentHolder {
private companion object {
private val DISPOSABLE = ApplicationManager.getApplication().getService(PluginDisposableService::class.java)
private val EXTRA_SHORTCUTS = setOf(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
)
}
private var currentNode: KeyStrokeNode.Parent<VimNavigationDispatcher<T>> = rootNode
var isSearching = AtomicBoolean(false)
init {
registerCustomShortcutSet(KeyStrokeNode.getAllShortcuts(getAllKeyStrokes()), component, DISPOSABLE)
val speedSearch = SpeedSearchSupply.getSupply(component, true)
speedSearch?.addChangeListener {
if (it.propertyName == SpeedSearchSupply.ENTERED_PREFIX_PROPERTY_NAME && !speedSearch.isPopupActive) {
isSearching.set(false)
}
}
}
protected fun getAllKeyStrokes(): Set<KeyStroke> {
return KeyStrokeNode.getAllKeyStrokes(rootNode, EXTRA_SHORTCUTS)
}
final override fun actionPerformed(e: AnActionEvent) {
val keyEvent = e.inputEvent as? KeyEvent ?: return
if (keyEvent.id == KeyEvent.KEY_PRESSED && handleSpecialKeyPress(keyEvent)) {
currentNode = rootNode
return
}
when (val nextNode = currentNode.getChild(keyEvent)) {
is KeyStrokeNode.Parent<VimNavigationDispatcher<T>> -> currentNode = nextNode
is KeyStrokeNode.ActionNode<VimNavigationDispatcher<T>> -> {
nextNode.performAction(this, e, keyEvent)
currentNode = rootNode
}
}
}
private fun handleSpecialKeyPress(keyEvent: KeyEvent): Boolean {
if (keyEvent.keyCode == KeyEvent.VK_ESCAPE) {
return true
}
if (keyEvent.keyCode == KeyEvent.VK_ENTER) {
if (isSearching.compareAndSet(true, false)) {
when (val supply = SpeedSearchSupply.getSupply(component)) {
is SpeedSearchBase<*> -> supply.hidePopup()
is SpeedSearch -> supply.reset()
}
}
return true
}
return false
}
final override fun update(e: AnActionEvent) {
e.presentation.isEnabled = !isSearching.get() || e.inputEvent.let { it is KeyEvent && it.id == KeyEvent.KEY_PRESSED && it.keyCode == KeyEvent.VK_ENTER }
}
final override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
class StartSearch<T : JComponent> : KeyStrokeNode.ActionNode<VimNavigationDispatcher<T>> {
@Suppress("UnstableApiUsage")
override fun performAction(holder: VimNavigationDispatcher<T>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val speedSearch = SpeedSearchSupply.getSupply(holder.component, true) as? SpeedSearchActivator ?: return
if (speedSearch.isAvailable) {
holder.isSearching.set(true)
}
}
}
}

View File

@ -1,157 +0,0 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation
import com.chylex.intellij.keyboardmaster.PluginDisposableService
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key
import com.intellij.ui.SpeedSearchBase
import com.intellij.ui.speedSearch.SpeedSearch
import com.intellij.ui.speedSearch.SpeedSearchSupply
import java.awt.event.KeyEvent
import java.util.concurrent.atomic.AtomicBoolean
import javax.swing.JTree
import javax.swing.KeyStroke
internal object VimTreeNavigation {
private val KEY = Key.create<Dispatcher>("KeyboardMaster-VimTreeNavigation")
private val DISPOSABLE = ApplicationManager.getApplication().getService(PluginDisposableService::class.java)
fun install(component: JTree) {
if (component.getUserData(KEY) == null) {
component.putUserData(KEY, Dispatcher(component))
}
}
private val ROOT_NODE = KeyStrokeNode.Parent(
mapOf(
KeyStroke.getKeyStroke('A') to KeyStrokeNode.IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch,
KeyStroke.getKeyStroke('g') to KeyStrokeNode.Parent(
mapOf(
KeyStroke.getKeyStroke('g') to KeyStrokeNode.IdeaAction("Tree-selectFirst"),
)
),
KeyStroke.getKeyStroke('G') to KeyStrokeNode.IdeaAction("Tree-selectLast"),
KeyStroke.getKeyStroke('j') to KeyStrokeNode.IdeaAction("Tree-selectNext"),
KeyStroke.getKeyStroke('j', KeyEvent.ALT_DOWN_MASK) to KeyStrokeNode.IdeaAction("Tree-selectNextSibling"),
KeyStroke.getKeyStroke('k') to KeyStrokeNode.IdeaAction("Tree-selectPrevious"),
KeyStroke.getKeyStroke('k', KeyEvent.ALT_DOWN_MASK) to KeyStrokeNode.IdeaAction("Tree-selectPreviousSibling"),
KeyStroke.getKeyStroke('m') to KeyStrokeNode.IdeaAction("ShowPopupMenu"),
KeyStroke.getKeyStroke('o') to ExpandOrCollapseTreeNode,
KeyStroke.getKeyStroke('O') to KeyStrokeNode.IdeaAction("FullyExpandTreeNode"),
KeyStroke.getKeyStroke('p') to KeyStrokeNode.IdeaAction("Tree-selectParentNoCollapse"),
KeyStroke.getKeyStroke('q') to KeyStrokeNode.IdeaAction("HideActiveWindow"),
KeyStroke.getKeyStroke('x') to CollapseSelfOrParentNode,
KeyStroke.getKeyStroke('X') to KeyStrokeNode.IdeaAction("CollapseTreeNode"),
KeyStroke.getKeyStroke('/') to StartSearch,
)
)
private val ALL_SHORTCUTS = KeyStrokeNode.getAllShortcuts(
ROOT_NODE,
setOf(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
)
)
internal class Dispatcher(override val component: JTree) : DumbAwareAction(), ComponentHolder {
private var currentNode: KeyStrokeNode.Parent<Dispatcher> = ROOT_NODE
var isSearching = AtomicBoolean(false)
init {
registerCustomShortcutSet(ALL_SHORTCUTS, component, DISPOSABLE)
SpeedSearchSupply.getSupply(component, true)?.addChangeListener {
if (it.propertyName == SpeedSearchSupply.ENTERED_PREFIX_PROPERTY_NAME && it.oldValue != null && it.newValue == null) {
isSearching.set(false)
}
}
}
override fun actionPerformed(e: AnActionEvent) {
val keyEvent = e.inputEvent as? KeyEvent ?: return
if (keyEvent.id == KeyEvent.KEY_PRESSED && handleSpecialKeyPress(keyEvent)) {
currentNode = ROOT_NODE
return
}
when (val nextNode = currentNode.getChild(keyEvent)) {
is KeyStrokeNode.Parent<Dispatcher> -> currentNode = nextNode
is KeyStrokeNode.ActionNode<Dispatcher> -> {
nextNode.performAction(this, e, keyEvent)
currentNode = ROOT_NODE
}
}
}
private fun handleSpecialKeyPress(keyEvent: KeyEvent): Boolean {
if (keyEvent.keyCode == KeyEvent.VK_ESCAPE) {
return true
}
if (keyEvent.keyCode == KeyEvent.VK_ENTER) {
if (isSearching.compareAndSet(true, false)) {
when (val supply = SpeedSearchSupply.getSupply(component)) {
is SpeedSearchBase<*> -> supply.hidePopup()
is SpeedSearch -> supply.reset()
}
}
return true
}
return false
}
override fun update(e: AnActionEvent) {
e.presentation.isEnabled = !isSearching.get() || e.inputEvent.let { it is KeyEvent && it.id == KeyEvent.KEY_PRESSED && it.keyCode == KeyEvent.VK_ENTER }
}
override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT
}
}
data object ExpandOrCollapseTreeNode : KeyStrokeNode.ActionNode<Dispatcher> {
override fun performAction(holder: Dispatcher, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath ?: return
if (tree.isExpanded(path)) {
tree.collapsePath(path)
}
else {
tree.expandPath(path)
}
}
}
data object CollapseSelfOrParentNode : KeyStrokeNode.ActionNode<Dispatcher> {
override fun performAction(holder: Dispatcher, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath ?: return
if (tree.isExpanded(path)) {
tree.collapsePath(path)
}
else {
val parentPath = path.parentPath
if (parentPath.parentPath != null || tree.isRootVisible) {
tree.collapsePath(parentPath)
}
}
}
}
data object StartSearch : KeyStrokeNode.ActionNode<Dispatcher> {
override fun performAction(holder: Dispatcher, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
holder.isSearching.set(true)
}
}
}

View File

@ -0,0 +1,93 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation.components
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.IdeaAction
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.Parent
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.StartSearch
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key
import com.intellij.ui.popup.WizardPopup
import com.intellij.ui.speedSearch.SpeedSearch
import com.intellij.ui.speedSearch.SpeedSearchSupply
import java.awt.event.ActionEvent
import java.awt.event.KeyEvent
import javax.swing.AbstractAction
import javax.swing.JList
import javax.swing.KeyStroke
internal object VimListNavigation {
private val KEY = Key.create<VimNavigationDispatcher<JList<*>>>("KeyboardMaster-VimListNavigation")
private val ROOT_NODE = Parent<VimNavigationDispatcher<JList<*>>>(
mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('g') to Parent(
mapOf(
KeyStroke.getKeyStroke('g') to IdeaAction("List-selectFirstRow"),
)
),
KeyStroke.getKeyStroke('G') to IdeaAction("List-selectLastRow"),
KeyStroke.getKeyStroke('j') to IdeaAction("List-selectNextRow"),
KeyStroke.getKeyStroke('k') to IdeaAction("List-selectPreviousRow"),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"),
KeyStroke.getKeyStroke('o') to IdeaAction("List-selectNextColumn"),
KeyStroke.getKeyStroke('q') to IdeaAction("HideActiveWindow"),
KeyStroke.getKeyStroke('x') to IdeaAction("List-selectPreviousColumn"),
KeyStroke.getKeyStroke('/') to StartSearch(),
)
)
fun install(component: JList<*>) {
if (component.getUserData(KEY) == null && component.javaClass.enclosingClass.let { it == null || WizardPopup::class.java.isAssignableFrom(it) }) {
component.putUserData(KEY, VimNavigationDispatcher(component, ROOT_NODE))
}
}
fun install(component: JList<*>, popup: WizardPopup) {
if (component.getUserData(KEY) == null) {
component.putUserData(KEY, VimPopupListNavigationDispatcher(component, popup))
}
}
@Suppress("serial")
private class VimPopupListNavigationDispatcher(component: JList<*>, popup: WizardPopup) : VimNavigationDispatcher<JList<*>>(component, ROOT_NODE) {
init {
val speedSearch = SpeedSearchSupply.getSupply(component, true) as? SpeedSearch
if (speedSearch != null) {
installSpeedSearch(speedSearch, popup)
}
}
private fun installSpeedSearch(speedSearch: SpeedSearch, popup: WizardPopup) {
val pauseAction = PauseSpeedSearchAction(this, speedSearch)
for (keyStroke in getAllKeyStrokes()) {
if (keyStroke.keyEventType != KeyEvent.KEY_TYPED) {
continue
}
val keyCode = KeyEvent.getExtendedKeyCodeForChar(keyStroke.keyChar.code)
if (keyCode != KeyEvent.VK_UNDEFINED) {
popup.registerAction("KeyboardMaster-VimListNavigation-PauseSpeedSearch", KeyStroke.getKeyStroke(keyCode, 0), pauseAction)
popup.registerAction("KeyboardMaster-VimListNavigation-PauseSpeedSearch", KeyStroke.getKeyStroke(keyCode, KeyEvent.SHIFT_DOWN_MASK), pauseAction)
}
}
// WizardPopup only checks key codes against its input map, but key codes may be undefined for some characters.
popup.registerAction("KeyboardMaster-VimListNavigation-PauseSpeedSearch", KeyStroke.getKeyStroke(KeyEvent.CHAR_UNDEFINED, 0), pauseAction)
popup.registerAction("KeyboardMaster-VimListNavigation-PauseSpeedSearch", KeyStroke.getKeyStroke(KeyEvent.CHAR_UNDEFINED, KeyEvent.SHIFT_DOWN_MASK), pauseAction)
}
private class PauseSpeedSearchAction(private val dispatcher: VimNavigationDispatcher<JList<*>>, private val speedSearch: SpeedSearch) : AbstractAction() {
override fun actionPerformed(e: ActionEvent) {
if (!dispatcher.isSearching.get()) {
speedSearch.setEnabled(false)
ApplicationManager.getApplication().invokeLater { speedSearch.setEnabled(true) }
}
}
}
}
}

View File

@ -0,0 +1,41 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation.components
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.IdeaAction
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.Parent
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.StartSearch
import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key
import javax.swing.JTable
import javax.swing.KeyStroke
internal object VimTableNavigation {
private val KEY = Key.create<VimNavigationDispatcher<JTable>>("KeyboardMaster-VimTableNavigation")
private val ROOT_NODE = Parent<VimNavigationDispatcher<JTable>>(
mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('g') to Parent(
mapOf(
KeyStroke.getKeyStroke('g') to IdeaAction("Table-selectFirstRow"),
)
),
KeyStroke.getKeyStroke('G') to IdeaAction("Table-selectLastRow"),
KeyStroke.getKeyStroke('h') to IdeaAction("Table-selectPreviousColumn"),
KeyStroke.getKeyStroke('j') to IdeaAction("Table-selectNextRow"),
KeyStroke.getKeyStroke('k') to IdeaAction("Table-selectPreviousRow"),
KeyStroke.getKeyStroke('l') to IdeaAction("Table-selectNextColumn"),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"),
KeyStroke.getKeyStroke('q') to IdeaAction("HideActiveWindow"),
KeyStroke.getKeyStroke('/') to StartSearch(),
)
)
fun install(component: JTable) {
if (component.getUserData(KEY) == null) {
component.putUserData(KEY, VimNavigationDispatcher(component, ROOT_NODE))
}
}
}

View File

@ -0,0 +1,80 @@
package com.chylex.intellij.keyboardmaster.feature.vimNavigation.components
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.ActionNode
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.IdeaAction
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.Parent
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.StartSearch
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key
import java.awt.event.KeyEvent
import javax.swing.JTree
import javax.swing.KeyStroke
internal object VimTreeNavigation {
private val KEY = Key.create<VimNavigationDispatcher<JTree>>("KeyboardMaster-VimTreeNavigation")
private val ROOT_NODE = Parent(
mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('g') to Parent(
mapOf(
KeyStroke.getKeyStroke('g') to IdeaAction("Tree-selectFirst"),
)
),
KeyStroke.getKeyStroke('G') to IdeaAction("Tree-selectLast"),
KeyStroke.getKeyStroke('j') to IdeaAction("Tree-selectNext"),
KeyStroke.getKeyStroke('j', KeyEvent.ALT_DOWN_MASK) to IdeaAction("Tree-selectNextSibling"),
KeyStroke.getKeyStroke('k') to IdeaAction("Tree-selectPrevious"),
KeyStroke.getKeyStroke('k', KeyEvent.ALT_DOWN_MASK) to IdeaAction("Tree-selectPreviousSibling"),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"),
KeyStroke.getKeyStroke('o') to ExpandOrCollapseTreeNode,
KeyStroke.getKeyStroke('O') to IdeaAction("FullyExpandTreeNode"),
KeyStroke.getKeyStroke('p') to IdeaAction("Tree-selectParentNoCollapse"),
KeyStroke.getKeyStroke('q') to IdeaAction("HideActiveWindow"),
KeyStroke.getKeyStroke('x') to CollapseSelfOrParentNode,
KeyStroke.getKeyStroke('X') to IdeaAction("CollapseTreeNode"),
KeyStroke.getKeyStroke('/') to StartSearch(),
)
)
fun install(component: JTree) {
if (component.getUserData(KEY) == null) {
component.putUserData(KEY, VimNavigationDispatcher(component, ROOT_NODE))
}
}
private data object ExpandOrCollapseTreeNode : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath ?: return
if (tree.isExpanded(path)) {
tree.collapsePath(path)
}
else {
tree.expandPath(path)
}
}
}
private data object CollapseSelfOrParentNode : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath ?: return
if (tree.isExpanded(path)) {
tree.collapsePath(path)
}
else {
val parentPath = path.parentPath
if (parentPath.parentPath != null || tree.isRootVisible) {
tree.collapsePath(parentPath)
}
}
}
}
}