mirror of
https://github.com/chylex/IntelliJ-Keyboard-Master.git
synced 2024-11-25 01:42:47 +01:00
Compare commits
1 Commits
95fca176ba
...
70aec7b196
Author | SHA1 | Date | |
---|---|---|---|
70aec7b196 |
@ -0,0 +1,9 @@
|
||||
package com.chylex.intellij.keyboardmaster
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import com.intellij.openapi.components.Service
|
||||
|
||||
@Service
|
||||
internal class PluginDisposableService : Disposable {
|
||||
override fun dispose() {}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.chylex.intellij.keyboardmaster.feature.vimNavigation
|
||||
|
||||
import javax.swing.JComponent
|
||||
|
||||
internal interface ComponentHolder {
|
||||
val component: JComponent
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.chylex.intellij.keyboardmaster.feature.vimNavigation
|
||||
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent
|
||||
import com.intellij.openapi.actionSystem.CustomShortcutSet
|
||||
import com.intellij.openapi.actionSystem.CustomizedDataContext
|
||||
import com.intellij.openapi.actionSystem.KeyboardShortcut
|
||||
import com.intellij.openapi.actionSystem.PlatformDataKeys
|
||||
import com.intellij.openapi.actionSystem.ex.ActionUtil
|
||||
import com.intellij.util.containers.map2Array
|
||||
import java.awt.event.KeyEvent
|
||||
import javax.swing.KeyStroke
|
||||
|
||||
internal interface KeyStrokeNode<T> {
|
||||
class Parent<T>(private val keys: Map<KeyStroke, KeyStrokeNode<T>>) : KeyStrokeNode<T> {
|
||||
val allKeyStrokes: Set<KeyStroke> = mutableSetOf<KeyStroke>().apply {
|
||||
for ((key, node) in keys) {
|
||||
add(key)
|
||||
|
||||
if (node is Parent) {
|
||||
node.collectKeys(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getChild(keyEvent: KeyEvent): KeyStrokeNode<T> {
|
||||
val keyStroke = when (keyEvent.id) {
|
||||
KeyEvent.KEY_TYPED -> KeyStroke.getKeyStroke(keyEvent.keyChar, keyEvent.modifiersEx and KeyEvent.SHIFT_DOWN_MASK.inv())
|
||||
KeyEvent.KEY_PRESSED -> KeyStroke.getKeyStroke(keyEvent.keyCode, keyEvent.modifiersEx, false)
|
||||
else -> return this
|
||||
}
|
||||
|
||||
return keys[keyStroke] ?: this
|
||||
}
|
||||
|
||||
private fun collectKeys(target: MutableSet<KeyStroke>) {
|
||||
for ((key, node) in keys) {
|
||||
target.add(key)
|
||||
|
||||
if (node is Parent) {
|
||||
node.collectKeys(target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ActionNode<T> : KeyStrokeNode<T> {
|
||||
fun performAction(holder: T, actionEvent: AnActionEvent, keyEvent: KeyEvent)
|
||||
}
|
||||
|
||||
class IdeaAction<T : ComponentHolder>(private val name: String) : ActionNode<T> {
|
||||
override fun performAction(holder: T, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
|
||||
val action = actionEvent.actionManager.getAction(name) ?: return
|
||||
|
||||
val dataContext = CustomizedDataContext.create(actionEvent.dataContext) {
|
||||
when {
|
||||
PlatformDataKeys.CONTEXT_COMPONENT.`is`(it) -> holder.component
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
ActionUtil.invokeAction(action, dataContext, actionEvent.place, null, null)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <T> getAllShortcuts(root: Parent<T>, extra: Set<KeyStroke>? = null): CustomShortcutSet {
|
||||
val allKeyStrokes = HashSet(root.allKeyStrokes)
|
||||
|
||||
if (extra != null) {
|
||||
allKeyStrokes.addAll(extra)
|
||||
}
|
||||
|
||||
for (c in ('a'..'z') + ('A'..'Z')) {
|
||||
allKeyStrokes.add(KeyStroke.getKeyStroke(c))
|
||||
}
|
||||
|
||||
return CustomShortcutSet(*allKeyStrokes.map2Array { KeyboardShortcut(it, null) })
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.chylex.intellij.keyboardmaster.feature.vimNavigation
|
||||
|
||||
import com.chylex.intellij.keyboardmaster.PluginDisposableService
|
||||
import com.intellij.ide.ApplicationInitializedListener
|
||||
import com.intellij.openapi.application.ApplicationManager
|
||||
import com.intellij.util.ui.StartupUiUtil
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import java.awt.AWTEvent
|
||||
import java.awt.event.FocusEvent
|
||||
import javax.swing.JTree
|
||||
|
||||
@Suppress("UnstableApiUsage")
|
||||
internal class VimNavigationApplicationListener : ApplicationInitializedListener {
|
||||
override suspend fun execute(asyncScope: CoroutineScope) {
|
||||
StartupUiUtil.addAwtListener(::handleEvent, AWTEvent.FOCUS_EVENT_MASK, ApplicationManager.getApplication().getService(PluginDisposableService::class.java))
|
||||
}
|
||||
|
||||
private fun handleEvent(event: AWTEvent) {
|
||||
if (event is FocusEvent && event.id == FocusEvent.FOCUS_GAINED) {
|
||||
val source = event.source
|
||||
if (source is JTree) {
|
||||
VimTreeNavigation.install(source)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<applicationService serviceImplementation="com.chylex.intellij.keyboardmaster.configuration.PluginConfiguration" />
|
||||
<applicationConfigurable parentId="tools" instance="com.chylex.intellij.keyboardmaster.configuration.PluginConfigurable" id="com.chylex.keyboardmaster" displayName="Keyboard Master" />
|
||||
<applicationInitializedListener implementation="com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationApplicationListener" />
|
||||
<postStartupActivity implementation="com.chylex.intellij.keyboardmaster.PluginStartup" order="last" />
|
||||
</extensions>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user