1
0
mirror of https://github.com/chylex/IntelliJ-Keyboard-Master.git synced 2025-09-15 17:32:10 +02:00

Compare commits

..

35 Commits

Author SHA1 Message Date
7596b8727a Release version 0.6.2 2024-09-19 05:36:42 +02:00
38c80a7b27 Change vim-style navigation binding for 'X' to close all tree nodes 2024-09-18 20:00:34 +02:00
efe13712ad Add new vim-style bindings for navigating tree hierarchy 2024-09-18 20:00:29 +02:00
5aea7947ec Disable automatic tree auto-expansion in vim-style navigation 2024-09-18 08:52:51 +02:00
f814ec04bd Release version 0.6.1 2024-08-11 11:22:06 +02:00
c7229a6d8e Fix leaking editor instances 2024-08-11 11:21:49 +02:00
d711e60da8 Release version 0.6.0 2024-08-10 15:35:08 +02:00
356826f6ae Update uses of obsolete APIs for IDEA 2024.2 2024-08-10 15:35:08 +02:00
f3a3685084 Release version 0.5.8 2024-08-05 03:55:32 +02:00
5e654762fc Fix vim-style navigation not scrolling after collapsing a tree node 2024-08-05 03:55:14 +02:00
723b8af939 Release version 0.5.7 2024-07-15 13:08:10 +02:00
72158689ff Add vim-style binding to expand tree to the next level 2024-07-15 13:06:44 +02:00
d76e259c25 Release version 0.5.6 2024-06-18 12:40:58 +02:00
6ba2a0863e Add vim-style bindings for Ctrl/Cmd+Enter to immediately execute Enter action during search 2024-06-18 12:17:12 +02:00
48a9159c87 Release version 0.5.5 2024-05-19 15:58:49 +02:00
844738794b Make vim-style navigation Enter handling during search in popups consistent with other components 2024-05-19 15:58:25 +02:00
0e2928a737 Fix vim-style navigation preventing default Enter action in components with key listeners 2024-05-19 15:54:30 +02:00
b131413c8d Fix vim-style navigation being active in popups even when disabled 2024-05-19 15:13:59 +02:00
ca075869eb Fix vim-style bindings stealing keys from inline text fields 2024-05-17 19:12:08 +02:00
cd69968868 Release version 0.5.4 2024-05-16 16:27:36 +02:00
be41677c59 Fix vim-style bindings forgetting that a search is active 2024-05-16 12:33:27 +02:00
b1d5c57e04 Fix not scrolling selected row into view when jumping to first/last sibling 2024-05-16 12:32:28 +02:00
401d5101e7 Release version 0.5.3 2024-05-06 23:11:29 +02:00
23becff81e Fix ClassCastException in some wrapped Enter action handlers 2024-05-06 23:11:16 +02:00
83bb3f6a83 Add vim-style bindings with selection 2024-05-06 23:05:34 +02:00
e6ab49cb16 Add additional vim-style bindings for navigating submenus 2024-05-06 23:05:33 +02:00
6931387370 Fix vim-style navigation breaking Escape actions 2024-05-06 22:43:39 +02:00
6404b5b926 Release version 0.5.2 2024-05-06 21:03:53 +02:00
2b7f1a363f Fix more cases of vim-style navigation breaking Enter actions 2024-05-06 21:03:40 +02:00
215049bf26 Release version 0.5.1 2024-05-06 05:41:24 +02:00
250e8fa8f8 Fix vim-style navigation breaking Enter actions 2024-05-06 05:40:54 +02:00
381db0e664 Release version 0.5.0 2024-05-05 18:32:52 +02:00
5ca46cf8b2 Implement vim-style navigation for menu popups 2024-05-05 18:32:10 +02:00
d3989a0e87 Implement vim-style navigation for list-based popups 2024-05-05 18:32:09 +02:00
6e38809ef2 Implement vim-style navigation for lists, trees, and tables 2024-05-05 18:32:09 +02:00
15 changed files with 447 additions and 145 deletions

View File

@@ -8,7 +8,7 @@ plugins {
} }
group = "com.chylex.intellij.keyboardmaster" group = "com.chylex.intellij.keyboardmaster"
version = "0.4.1" version = "0.6.2"
repositories { repositories {
mavenCentral() mavenCentral()
@@ -16,7 +16,7 @@ repositories {
intellij { intellij {
type.set("IU") type.set("IU")
version.set("2024.1.1") version.set("2024.2")
updateSinceUntilBuild.set(false) updateSinceUntilBuild.set(false)
plugins.add("com.intellij.java") plugins.add("com.intellij.java")
@@ -35,7 +35,7 @@ dependencies {
} }
tasks.patchPluginXml { tasks.patchPluginXml {
sinceBuild.set("241") sinceBuild.set("242")
} }
tasks.test { tasks.test {

View File

@@ -17,7 +17,7 @@ class PluginConfigurable : Configurable {
private val enableVimNavigation = JBCheckBox("Vim-style navigation in lists / trees / tables").also { checkBox -> private val enableVimNavigation = JBCheckBox("Vim-style navigation in lists / trees / tables").also { checkBox ->
checkBox.addActionListener { checkBox.addActionListener {
if (!checkBox.isSelected) { if (!checkBox.isSelected) {
Messages.showInfoMessage(checkBox, "Vim-style navigation will be disabled after restarting the IDE.", "Keyboard Master") Messages.showInfoMessage(checkBox, "Vim-style navigation will be fully disabled after restarting the IDE.", "Keyboard Master")
} }
} }
} }

View File

@@ -2,9 +2,10 @@ package com.chylex.intellij.keyboardmaster.feature.action.gotoError
import com.intellij.codeInsight.CodeInsightActionHandler import com.intellij.codeInsight.CodeInsightActionHandler
import com.intellij.codeInsight.daemon.impl.actions.GotoNextErrorAction import com.intellij.codeInsight.daemon.impl.actions.GotoNextErrorAction
import com.intellij.openapi.actionSystem.DataContext
class GotoNextErrorInOtherModeAction : GotoNextErrorAction() { class GotoNextErrorInOtherModeAction : GotoNextErrorAction() {
override fun getHandler(): CodeInsightActionHandler { override fun getHandler(dataContext: DataContext): CodeInsightActionHandler {
return GotoErrorInOtherModeHandler(forward = true) return GotoErrorInOtherModeHandler(forward = true)
} }
} }

View File

@@ -2,9 +2,10 @@ package com.chylex.intellij.keyboardmaster.feature.action.gotoError
import com.intellij.codeInsight.CodeInsightActionHandler import com.intellij.codeInsight.CodeInsightActionHandler
import com.intellij.codeInsight.daemon.impl.actions.GotoPreviousErrorAction import com.intellij.codeInsight.daemon.impl.actions.GotoPreviousErrorAction
import com.intellij.openapi.actionSystem.DataContext
class GotoPreviousErrorInOtherModeAction : GotoPreviousErrorAction() { class GotoPreviousErrorInOtherModeAction : GotoPreviousErrorAction() {
override fun getHandler(): CodeInsightActionHandler { override fun getHandler(dataContext: DataContext): CodeInsightActionHandler {
return GotoErrorInOtherModeHandler(forward = false) return GotoErrorInOtherModeHandler(forward = false)
} }
} }

View File

@@ -52,7 +52,7 @@ class GotoTypeInFileHandler(private val forward: Boolean) : CodeInsightActionHan
private companion object { private companion object {
fun getNavigationOffsets(file: PsiFile, searchedOffsetRange: IntRange): IntArray { fun getNavigationOffsets(file: PsiFile, searchedOffsetRange: IntRange): IntArray {
val structureViewBuilder = LanguageStructureViewBuilder.INSTANCE.getStructureViewBuilder(file) val structureViewBuilder = LanguageStructureViewBuilder.getInstance().getStructureViewBuilder(file)
if (structureViewBuilder !is TreeBasedStructureViewBuilder) { if (structureViewBuilder !is TreeBasedStructureViewBuilder) {
return intArrayOf() return intArrayOf()
} }

View File

@@ -1,13 +1,13 @@
package com.chylex.intellij.keyboardmaster.feature.codeCompletion package com.chylex.intellij.keyboardmaster.feature.codeCompletion
import com.intellij.util.containers.IntIntHashMap import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap
object CodeCompletionPopupConfiguration { object CodeCompletionPopupConfiguration {
const val SHORTCUT_NONE = -1 const val SHORTCUT_NONE = -1
const val SHORTCUT_NEXT_PAGE = -2 const val SHORTCUT_NEXT_PAGE = -2
const val SHORTCUT_PREV_PAGE = -3 const val SHORTCUT_PREV_PAGE = -3
private val charToShortcutMap = IntIntHashMap(16, SHORTCUT_NONE) private val charToShortcutMap = Int2IntOpenHashMap(16).also { it.defaultReturnValue(SHORTCUT_NONE) }
private var hintTexts = mutableListOf<String>() private var hintTexts = mutableListOf<String>()
val itemShortcutCount val itemShortcutCount

View File

@@ -17,7 +17,7 @@ internal interface KeyStrokeNode<T> {
add(key) add(key)
if (node is Parent) { if (node is Parent) {
node.collectKeys(this) addAll(node.allKeyStrokes)
} }
} }
} }
@@ -32,14 +32,22 @@ internal interface KeyStrokeNode<T> {
return keys[keyStroke] ?: this return keys[keyStroke] ?: this
} }
private fun collectKeys(target: MutableSet<KeyStroke>) { operator fun plus(other: Parent<T>): Parent<T> {
for ((key, node) in keys) { val mergedKeys = HashMap(keys)
target.add(key)
if (node is Parent) { for ((otherKey, otherNode) in other.keys) {
node.collectKeys(target) if (otherNode is Parent) {
val ourNode = keys[otherKey]
if (ourNode is Parent) {
mergedKeys[otherKey] = ourNode + otherNode
continue
}
} }
mergedKeys[otherKey] = otherNode
} }
return Parent(mergedKeys)
} }
} }
@@ -51,7 +59,7 @@ internal interface KeyStrokeNode<T> {
override fun performAction(holder: T, actionEvent: AnActionEvent, keyEvent: KeyEvent) { override fun performAction(holder: T, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val action = actionEvent.actionManager.getAction(name) ?: return val action = actionEvent.actionManager.getAction(name) ?: return
val dataContext = CustomizedDataContext.create(actionEvent.dataContext) { val dataContext = CustomizedDataContext.withProvider(actionEvent.dataContext) {
when { when {
PlatformDataKeys.CONTEXT_COMPONENT.`is`(it) -> holder.component PlatformDataKeys.CONTEXT_COMPONENT.`is`(it) -> holder.component
else -> null else -> null

View File

@@ -8,7 +8,7 @@ import com.intellij.ui.popup.list.ListPopupImpl
internal object PopupInterceptor : PersistentUiInterceptor<AbstractPopup>(AbstractPopup::class.java) { internal object PopupInterceptor : PersistentUiInterceptor<AbstractPopup>(AbstractPopup::class.java) {
override fun shouldIntercept(component: AbstractPopup): Boolean { override fun shouldIntercept(component: AbstractPopup): Boolean {
if (component is ListPopupImpl) { if (component is ListPopupImpl && VimNavigation.isEnabled) {
VimListNavigation.install(component.list, component) VimListNavigation.install(component.list, component)
} }

View File

@@ -4,6 +4,7 @@ import com.chylex.intellij.keyboardmaster.PluginDisposableService
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimListNavigation 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.VimTableNavigation
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimTreeNavigation import com.chylex.intellij.keyboardmaster.feature.vimNavigation.components.VimTreeNavigation
import com.intellij.ide.ui.UISettings
import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ApplicationManager
import com.intellij.ui.UiInterceptors import com.intellij.ui.UiInterceptors
import com.intellij.util.ui.StartupUiUtil import com.intellij.util.ui.StartupUiUtil
@@ -17,49 +18,53 @@ import javax.swing.KeyStroke
import javax.swing.UIManager import javax.swing.UIManager
object VimNavigation { object VimNavigation {
private val isEnabled = AtomicBoolean(false) private val isEnabledFlag = AtomicBoolean(false)
private var originalPopupBindings: Array<*>? = null private var originalPopupBindings: Array<*>? = null
val isEnabled
get() = isEnabledFlag.get()
fun register() { fun register() {
val disposable = ApplicationManager.getApplication().getService(PluginDisposableService::class.java) val disposable = ApplicationManager.getApplication().getService(PluginDisposableService::class.java)
StartupUiUtil.addAwtListener(::handleEvent, AWTEvent.FOCUS_EVENT_MASK, disposable) StartupUiUtil.addAwtListener(AWTEvent.FOCUS_EVENT_MASK, disposable, ::handleEvent)
UiInterceptors.registerPersistent(disposable, PopupInterceptor) UiInterceptors.registerPersistent(disposable, PopupInterceptor)
} }
fun setEnabled(enabled: Boolean) { fun setEnabled(enabled: Boolean) {
if (!isEnabled.compareAndSet(!enabled, enabled)) { if (!isEnabledFlag.compareAndSet(!enabled, enabled)) {
return return
} }
if (enabled) { ApplicationManager.getApplication().invokeLater {
val originalBindings = (UIManager.get("PopupMenu.selectedWindowInputMapBindings") as Array<*>).also { originalPopupBindings = it } if (enabled) {
val updatedBindings = mutableListOf(*originalBindings) val originalBindings = (UIManager.get("PopupMenu.selectedWindowInputMapBindings") as Array<*>).also { originalPopupBindings = it }
val updatedBindings = mutableListOf(*originalBindings)
updatedBindings.add(KeyStroke.getKeyStroke('h')) addBinding(updatedBindings, "selectParent", setOf('h', 'p', 'x'))
updatedBindings.add("selectParent") addBinding(updatedBindings, "selectNext", setOf('j'))
addBinding(updatedBindings, "selectPrevious", setOf('k'))
addBinding(updatedBindings, "selectChild", setOf('l', 'o'))
addBinding(updatedBindings, "cancel", setOf('q'))
updatedBindings.add(KeyStroke.getKeyStroke('j')) UIManager.put("PopupMenu.selectedWindowInputMapBindings", updatedBindings.toTypedArray())
updatedBindings.add("selectNext") UISettings.getInstance().disableMnemonics = true
}
updatedBindings.add(KeyStroke.getKeyStroke('k')) else {
updatedBindings.add("selectPrevious") UIManager.put("PopupMenu.selectedWindowInputMapBindings", originalPopupBindings)
}
updatedBindings.add(KeyStroke.getKeyStroke('l'))
updatedBindings.add("selectChild")
updatedBindings.add(KeyStroke.getKeyStroke('q'))
updatedBindings.add("cancel")
UIManager.put("PopupMenu.selectedWindowInputMapBindings", updatedBindings.toTypedArray())
} }
else { }
UIManager.put("PopupMenu.selectedWindowInputMapBindings", originalPopupBindings)
private fun addBinding(bindings: MutableList<Any?>, action: String, chars: Set<Char>) {
for (char in chars) {
bindings.add(KeyStroke.getKeyStroke(char))
bindings.add(action)
} }
} }
private fun handleEvent(event: AWTEvent) { private fun handleEvent(event: AWTEvent) {
if (event is FocusEvent && event.id == FocusEvent.FOCUS_GAINED && isEnabled.get()) { if (event is FocusEvent && event.id == FocusEvent.FOCUS_GAINED && isEnabled) {
when (val source = event.source) { when (val source = event.source) {
is JList<*> -> VimListNavigation.install(source) is JList<*> -> VimListNavigation.install(source)
is JTree -> VimTreeNavigation.install(source) is JTree -> VimTreeNavigation.install(source)

View File

@@ -1,56 +1,102 @@
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.feature.vimNavigation.VimNavigationDispatcher.WrappedAction.ForKeyListener
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.actionSystem.KeyboardShortcut
import com.intellij.openapi.actionSystem.ex.ActionUtil
import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.DumbAwareAction import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.wm.ex.ToolWindowManagerEx import com.intellij.openapi.util.SystemInfo
import com.intellij.pom.Navigatable
import com.intellij.toolWindow.InternalDecoratorImpl import com.intellij.toolWindow.InternalDecoratorImpl
import com.intellij.ui.SpeedSearchBase import com.intellij.ui.SpeedSearchBase
import com.intellij.ui.speedSearch.SpeedSearch import com.intellij.ui.speedSearch.SpeedSearch
import com.intellij.ui.speedSearch.SpeedSearchActivator
import com.intellij.ui.speedSearch.SpeedSearchSupply import com.intellij.ui.speedSearch.SpeedSearchSupply
import com.intellij.util.containers.JBIterable
import java.awt.Container
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.KeyEvent import java.awt.event.KeyEvent
import java.beans.PropertyChangeEvent
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import javax.swing.Action
import javax.swing.JComponent import javax.swing.JComponent
import javax.swing.KeyStroke import javax.swing.KeyStroke
internal open class VimNavigationDispatcher<T : JComponent>(final override val component: T, private val rootNode: KeyStrokeNode.Parent<VimNavigationDispatcher<T>>) : DumbAwareAction(), ComponentHolder { internal open class VimNavigationDispatcher<T : JComponent>(final override val component: T, private val rootNode: KeyStrokeNode.Parent<VimNavigationDispatcher<T>>, disposable: Disposable? = null) : DumbAwareAction(), ComponentHolder {
private companion object { companion object {
private val DISPOSABLE = ApplicationManager.getApplication().getService(PluginDisposableService::class.java) @JvmStatic
private val EXTRA_SHORTCUTS = setOf( protected val ENTER_KEY: KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), private val CTRL_ENTER_KEY: KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK)
) private val META_ENTER_KEY: KeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.META_DOWN_MASK)
private fun findOriginalEnterAction(component: JComponent): WrappedAction {
var originalEnterAction: WrappedAction? = null
for (container in JBIterable.generate<Container>(component) { it.parent }) {
if (container !is JComponent) {
continue
}
container.getActionForKeyStroke(ENTER_KEY)?.takeUnless(::isIgnoredEnterAction)?.let {
originalEnterAction = WrappedAction.ForActionListener(container, it)
}
for (action in ActionUtil.getActions(container)) {
if (action.shortcutSet.shortcuts.any { it is KeyboardShortcut && it.firstKeyStroke == ENTER_KEY && it.secondKeyStroke == null }) {
originalEnterAction = WrappedAction.ForAnAction(action)
}
}
}
return originalEnterAction ?: ForKeyListener(component)
}
private fun isIgnoredEnterAction(action: ActionListener): Boolean {
return action is Action && action.getValue(Action.NAME) == "toggle"
}
@Suppress("UnstableApiUsage")
fun JComponent.getParentToolWindowId(): String? {
return InternalDecoratorImpl.findNearestDecorator(this)?.toolWindowId
}
} }
private val originalEnterAction = findOriginalEnterAction(component)
private var currentNode: KeyStrokeNode.Parent<VimNavigationDispatcher<T>> = rootNode private var currentNode: KeyStrokeNode.Parent<VimNavigationDispatcher<T>> = rootNode
var isSearching = AtomicBoolean(false) var isSearching = AtomicBoolean(false)
init { init {
registerCustomShortcutSet(KeyStrokeNode.getAllShortcuts(getAllKeyStrokes()), component, DISPOSABLE) registerCustomShortcutSet(KeyStrokeNode.getAllShortcuts(getAllKeyStrokes()), component, disposable)
SpeedSearchSupply.getSupply(component, true)?.addChangeListener(::handleSpeedSearchChange)
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> { protected fun getAllKeyStrokes(): Set<KeyStroke> {
return KeyStrokeNode.getAllKeyStrokes(rootNode, EXTRA_SHORTCUTS) return KeyStrokeNode.getAllKeyStrokes(rootNode, setOf(ENTER_KEY, CTRL_ENTER_KEY, META_ENTER_KEY))
}
private fun handleSpeedSearchChange(e: PropertyChangeEvent) {
if (e.propertyName == SpeedSearchSupply.ENTERED_PREFIX_PROPERTY_NAME) {
val speedSearch = e.source as? SpeedSearchSupply ?: return
ApplicationManager.getApplication().invokeLater {
if (!speedSearch.isPopupActive) {
isSearching.set(false)
currentNode = rootNode
}
}
}
} }
final override fun actionPerformed(e: AnActionEvent) { final override fun actionPerformed(e: AnActionEvent) {
val keyEvent = e.inputEvent as? KeyEvent ?: return val keyEvent = e.inputEvent as? KeyEvent ?: return
if (keyEvent.id == KeyEvent.KEY_PRESSED && handleSpecialKeyPress(keyEvent, e.dataContext)) { if (keyEvent.id == KeyEvent.KEY_PRESSED && keyEvent.keyCode == KeyEvent.VK_ENTER) {
currentNode = rootNode handleEnterKeyPress(e, keyEvent)
return return
} }
@@ -63,65 +109,68 @@ internal open class VimNavigationDispatcher<T : JComponent>(final override val c
} }
} }
private fun handleSpecialKeyPress(keyEvent: KeyEvent, dataContext: DataContext): Boolean { private fun handleEnterKeyPress(actionEvent: AnActionEvent, keyEvent: KeyEvent) {
if (keyEvent.keyCode == KeyEvent.VK_ESCAPE) { handleEnterKeyPress(keyEvent) { originalEnterAction.perform(actionEvent, it) }
return true
}
if (keyEvent.keyCode == KeyEvent.VK_ENTER) {
handleEnterKeyPress(dataContext)
return true
}
return false
} }
private fun handleEnterKeyPress(dataContext: DataContext) { protected inline fun handleEnterKeyPress(keyEvent: KeyEvent, originalAction: (KeyEvent) -> Unit) {
if (isSearching.compareAndSet(true, false)) { if (isSearching.compareAndSet(true, false) && !runEnterActionImmediately(keyEvent)) {
when (val supply = SpeedSearchSupply.getSupply(component)) { stopSpeedSearch()
is SpeedSearchBase<*> -> supply.hidePopup()
is SpeedSearch -> supply.reset()
}
} }
else { else {
val navigatables = dataContext.getData(CommonDataKeys.NAVIGATABLE_ARRAY)?.filter(Navigatable::canNavigate).orEmpty() currentNode = rootNode
for ((index, navigatable) in navigatables.withIndex()) { originalAction(keyEvent)
navigatable.navigate(index == navigatables.lastIndex) }
} }
private fun runEnterActionImmediately(keyEvent: KeyEvent): Boolean {
return if (SystemInfo.isMac) keyEvent.isMetaDown else keyEvent.isControlDown
}
protected open fun stopSpeedSearch() {
when (val supply = SpeedSearchSupply.getSupply(component)) {
is SpeedSearchBase<*> -> supply.hidePopup()
is SpeedSearch -> supply.reset()
} }
} }
final override fun update(e: AnActionEvent) { 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 } e.presentation.isEnabled = !ignoreEventDueToActiveSearch(e) && !ignoreEventDueToActiveEditing(e)
}
private fun ignoreEventDueToActiveSearch(e: AnActionEvent): Boolean {
return isSearching.get() && !e.inputEvent.let { it is KeyEvent && it.id == KeyEvent.KEY_PRESSED && it.keyCode == KeyEvent.VK_ENTER }
}
private fun ignoreEventDueToActiveEditing(e: AnActionEvent): Boolean {
// Avoid stealing keys from inline text fields.
return e.dataContext.getData(CommonDataKeys.EDITOR) != null
} }
final override fun getActionUpdateThread(): ActionUpdateThread { final override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.BGT return ActionUpdateThread.BGT
} }
class StartSearch<T : JComponent> : KeyStrokeNode.ActionNode<VimNavigationDispatcher<T>> { private sealed interface WrappedAction {
@Suppress("UnstableApiUsage") fun perform(actionEvent: AnActionEvent, keyEvent: KeyEvent)
override fun performAction(holder: VimNavigationDispatcher<T>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val speedSearch = SpeedSearchSupply.getSupply(holder.component, true) as? SpeedSearchActivator ?: return class ForActionListener(private val component: JComponent, private val listener: ActionListener) : WrappedAction {
if (speedSearch.isAvailable) { override fun perform(actionEvent: AnActionEvent, keyEvent: KeyEvent) {
holder.isSearching.set(true) listener.actionPerformed(ActionEvent(component, ActionEvent.ACTION_PERFORMED, "Enter", keyEvent.`when`, keyEvent.modifiersEx))
speedSearch.activate()
} }
} }
}
@Suppress("UnstableApiUsage") class ForAnAction(val action: AnAction) : WrappedAction {
class CloseParentPopupOrToolWindow<T : JComponent> : KeyStrokeNode.ActionNode<VimNavigationDispatcher<T>> { override fun perform(actionEvent: AnActionEvent, keyEvent: KeyEvent) {
override fun performAction(holder: VimNavigationDispatcher<T>, actionEvent: AnActionEvent, keyEvent: KeyEvent) { action.actionPerformed(actionEvent)
val popup = holder.popup
if (popup != null) {
popup.cancel()
return
} }
}
val project = actionEvent.project ?: return class ForKeyListener(private val component: JComponent) : WrappedAction {
val decorator = InternalDecoratorImpl.findNearestDecorator(holder.component) ?: return override fun perform(actionEvent: AnActionEvent, keyEvent: KeyEvent) {
ToolWindowManagerEx.getInstanceEx(project).hideToolWindow(decorator.toolWindowId, true) val unconsumedKeyEvent = KeyEvent(component, keyEvent.id, keyEvent.`when`, keyEvent.modifiersEx, keyEvent.keyCode, keyEvent.keyChar, keyEvent.keyLocation)
component.dispatchEvent(unconsumedKeyEvent)
}
} }
} }
} }

View File

@@ -0,0 +1,66 @@
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.Companion.getParentToolWindowId
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.wm.ToolWindowId
import com.intellij.openapi.wm.ex.ToolWindowManagerEx
import com.intellij.ui.speedSearch.SpeedSearchActivator
import com.intellij.ui.speedSearch.SpeedSearchSupply
import java.awt.event.KeyEvent
import javax.swing.JComponent
import javax.swing.KeyStroke
internal object VimCommonNavigation {
fun <T : JComponent> commonRootNode() = Parent<VimNavigationDispatcher<T>>(
mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('I') to ToggleExcludedFilesInProjectView(),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"),
KeyStroke.getKeyStroke('r') to IdeaAction("SynchronizeCurrentFile"),
KeyStroke.getKeyStroke('R') to IdeaAction("Synchronize"),
KeyStroke.getKeyStroke('q') to CloseParentPopupOrToolWindow(),
KeyStroke.getKeyStroke('/') to StartSearch(),
)
)
private class StartSearch<T : JComponent> : 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)
speedSearch.activate()
}
}
}
private class CloseParentPopupOrToolWindow<T : JComponent> : ActionNode<VimNavigationDispatcher<T>> {
override fun performAction(holder: VimNavigationDispatcher<T>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val popup = holder.popup
if (popup != null) {
popup.cancel()
return
}
val project = actionEvent.project ?: return
val toolWindowId = holder.component.getParentToolWindowId() ?: return
ToolWindowManagerEx.getInstanceEx(project).hideToolWindow(toolWindowId, true)
}
}
private class ToggleExcludedFilesInProjectView<T : JComponent> : ActionNode<VimNavigationDispatcher<T>> {
private val showExcludedFilesAction = IdeaAction<VimNavigationDispatcher<T>>("ProjectView.ShowExcludedFiles")
override fun performAction(holder: VimNavigationDispatcher<T>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
if (holder.component.getParentToolWindowId() == ToolWindowId.PROJECT_VIEW) {
showExcludedFilesAction.performAction(holder, actionEvent, keyEvent)
}
}
}
}

View File

@@ -3,13 +3,12 @@ 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.IdeaAction
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.Parent 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
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.CloseParentPopupOrToolWindow
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.StartSearch
import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.ui.getUserData import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.ui.popup.WizardPopup import com.intellij.ui.popup.WizardPopup
import com.intellij.ui.popup.list.ListPopupImpl
import com.intellij.ui.speedSearch.SpeedSearch import com.intellij.ui.speedSearch.SpeedSearch
import com.intellij.ui.speedSearch.SpeedSearchSupply import com.intellij.ui.speedSearch.SpeedSearchSupply
import java.awt.event.ActionEvent import java.awt.event.ActionEvent
@@ -21,10 +20,8 @@ import javax.swing.KeyStroke
internal object VimListNavigation { internal object VimListNavigation {
private val KEY = Key.create<VimNavigationDispatcher<JList<*>>>("KeyboardMaster-VimListNavigation") private val KEY = Key.create<VimNavigationDispatcher<JList<*>>>("KeyboardMaster-VimListNavigation")
private val ROOT_NODE = Parent<VimNavigationDispatcher<JList<*>>>( private val ROOT_NODE = VimCommonNavigation.commonRootNode<JList<*>>() + Parent(
mapOf( mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('g') to Parent( KeyStroke.getKeyStroke('g') to Parent(
mapOf( mapOf(
KeyStroke.getKeyStroke('g') to IdeaAction("List-selectFirstRow"), KeyStroke.getKeyStroke('g') to IdeaAction("List-selectFirstRow"),
@@ -32,12 +29,21 @@ internal object VimListNavigation {
), ),
KeyStroke.getKeyStroke('G') to IdeaAction("List-selectLastRow"), KeyStroke.getKeyStroke('G') to IdeaAction("List-selectLastRow"),
KeyStroke.getKeyStroke('h') to IdeaAction("List-selectPreviousColumn"), KeyStroke.getKeyStroke('h') to IdeaAction("List-selectPreviousColumn"),
KeyStroke.getKeyStroke('H') to IdeaAction("List-selectPreviousColumnExtendSelection"),
KeyStroke.getKeyStroke('j') to IdeaAction("List-selectNextRow"), KeyStroke.getKeyStroke('j') to IdeaAction("List-selectNextRow"),
KeyStroke.getKeyStroke('J') to IdeaAction("List-selectNextRowExtendSelection"),
KeyStroke.getKeyStroke('k') to IdeaAction("List-selectPreviousRow"), KeyStroke.getKeyStroke('k') to IdeaAction("List-selectPreviousRow"),
KeyStroke.getKeyStroke('K') to IdeaAction("List-selectPreviousRowExtendSelection"),
KeyStroke.getKeyStroke('l') to IdeaAction("List-selectNextColumn"), KeyStroke.getKeyStroke('l') to IdeaAction("List-selectNextColumn"),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"), KeyStroke.getKeyStroke('L') to IdeaAction("List-selectNextColumnExtendSelection"),
KeyStroke.getKeyStroke('q') to CloseParentPopupOrToolWindow(), )
KeyStroke.getKeyStroke('/') to StartSearch(), )
private val POPUP_LIST_ROOT_NODE = ROOT_NODE + Parent(
mapOf(
KeyStroke.getKeyStroke('o') to IdeaAction("List-selectNextColumn"),
KeyStroke.getKeyStroke('p') to IdeaAction("List-selectPreviousColumn"),
KeyStroke.getKeyStroke('x') to IdeaAction("List-selectPreviousColumn"),
) )
) )
@@ -54,7 +60,7 @@ internal object VimListNavigation {
} }
@Suppress("serial") @Suppress("serial")
private class VimPopupListNavigationDispatcher(component: JList<*>, override val popup: WizardPopup) : VimNavigationDispatcher<JList<*>>(component, ROOT_NODE) { private class VimPopupListNavigationDispatcher(component: JList<*>, override val popup: WizardPopup) : VimNavigationDispatcher<JList<*>>(component, POPUP_LIST_ROOT_NODE, popup.parent) {
init { init {
val speedSearch = SpeedSearchSupply.getSupply(component, true) as? SpeedSearch val speedSearch = SpeedSearchSupply.getSupply(component, true) as? SpeedSearch
if (speedSearch != null) { if (speedSearch != null) {
@@ -80,6 +86,24 @@ internal object VimListNavigation {
// WizardPopup only checks key codes against its input map, but key codes may be undefined for some characters. // 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, 0), pauseAction)
popup.registerAction("KeyboardMaster-VimListNavigation-PauseSpeedSearch", KeyStroke.getKeyStroke(KeyEvent.CHAR_UNDEFINED, KeyEvent.SHIFT_DOWN_MASK), pauseAction) popup.registerAction("KeyboardMaster-VimListNavigation-PauseSpeedSearch", KeyStroke.getKeyStroke(KeyEvent.CHAR_UNDEFINED, KeyEvent.SHIFT_DOWN_MASK), pauseAction)
if (popup is ListPopupImpl) {
popup.registerAction("KeyboardMaster-VimListNavigation-Enter", ENTER_KEY, object : AbstractAction() {
override fun actionPerformed(e: ActionEvent) {
handleEnterKeyPress(createEnterEvent(e)) { popup.handleSelect(true, it) }
}
private fun createEnterEvent(e: ActionEvent): KeyEvent {
return KeyEvent(component, KeyEvent.KEY_PRESSED, e.`when`, e.modifiers, KeyEvent.VK_ENTER, KeyEvent.CHAR_UNDEFINED)
}
})
}
}
override fun stopSpeedSearch() {
val selectedValue = component.selectedValue
super.stopSpeedSearch()
component.setSelectedValue(selectedValue, true)
} }
private class PauseSpeedSearchAction(private val dispatcher: VimNavigationDispatcher<JList<*>>, private val speedSearch: SpeedSearch) : AbstractAction() { private class PauseSpeedSearchAction(private val dispatcher: VimNavigationDispatcher<JList<*>>, private val speedSearch: SpeedSearch) : AbstractAction() {

View File

@@ -3,8 +3,6 @@ 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.IdeaAction
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.Parent 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
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.CloseParentPopupOrToolWindow
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.StartSearch
import com.intellij.openapi.ui.getUserData import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
@@ -14,10 +12,8 @@ import javax.swing.KeyStroke
internal object VimTableNavigation { internal object VimTableNavigation {
private val KEY = Key.create<VimNavigationDispatcher<JTable>>("KeyboardMaster-VimTableNavigation") private val KEY = Key.create<VimNavigationDispatcher<JTable>>("KeyboardMaster-VimTableNavigation")
private val ROOT_NODE = Parent<VimNavigationDispatcher<JTable>>( private val ROOT_NODE = VimCommonNavigation.commonRootNode<JTable>() + Parent(
mapOf( mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('g') to Parent( KeyStroke.getKeyStroke('g') to Parent(
mapOf( mapOf(
KeyStroke.getKeyStroke('g') to IdeaAction("Table-selectFirstRow"), KeyStroke.getKeyStroke('g') to IdeaAction("Table-selectFirstRow"),
@@ -25,12 +21,13 @@ internal object VimTableNavigation {
), ),
KeyStroke.getKeyStroke('G') to IdeaAction("Table-selectLastRow"), KeyStroke.getKeyStroke('G') to IdeaAction("Table-selectLastRow"),
KeyStroke.getKeyStroke('h') to IdeaAction("Table-selectPreviousColumn"), KeyStroke.getKeyStroke('h') to IdeaAction("Table-selectPreviousColumn"),
KeyStroke.getKeyStroke('H') to IdeaAction("Table-selectPreviousColumnExtendSelection"),
KeyStroke.getKeyStroke('j') to IdeaAction("Table-selectNextRow"), KeyStroke.getKeyStroke('j') to IdeaAction("Table-selectNextRow"),
KeyStroke.getKeyStroke('J') to IdeaAction("Table-selectNextRowExtendSelection"),
KeyStroke.getKeyStroke('k') to IdeaAction("Table-selectPreviousRow"), KeyStroke.getKeyStroke('k') to IdeaAction("Table-selectPreviousRow"),
KeyStroke.getKeyStroke('K') to IdeaAction("Table-selectPreviousRowExtendSelection"),
KeyStroke.getKeyStroke('l') to IdeaAction("Table-selectNextColumn"), KeyStroke.getKeyStroke('l') to IdeaAction("Table-selectNextColumn"),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"), KeyStroke.getKeyStroke('L') to IdeaAction("Table-selectNextColumnExtendSelection"),
KeyStroke.getKeyStroke('q') to CloseParentPopupOrToolWindow(),
KeyStroke.getKeyStroke('/') to StartSearch(),
) )
) )

View File

@@ -4,43 +4,47 @@ import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.Ac
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.KeyStrokeNode.IdeaAction 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.KeyStrokeNode.Parent
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.CloseParentPopupOrToolWindow
import com.chylex.intellij.keyboardmaster.feature.vimNavigation.VimNavigationDispatcher.StartSearch
import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.ui.getUserData import com.intellij.openapi.ui.getUserData
import com.intellij.openapi.ui.putUserData import com.intellij.openapi.ui.putUserData
import com.intellij.openapi.util.Key import com.intellij.openapi.util.Key
import com.intellij.ui.ClientProperty
import com.intellij.ui.tree.ui.DefaultTreeUI
import java.awt.event.KeyEvent import java.awt.event.KeyEvent
import javax.swing.JTree import javax.swing.JTree
import javax.swing.KeyStroke import javax.swing.KeyStroke
import javax.swing.tree.TreePath
internal object VimTreeNavigation { internal object VimTreeNavigation {
private val KEY = Key.create<VimNavigationDispatcher<JTree>>("KeyboardMaster-VimTreeNavigation") private val KEY = Key.create<VimNavigationDispatcher<JTree>>("KeyboardMaster-VimTreeNavigation")
private val ROOT_NODE = Parent( private val ROOT_NODE = VimCommonNavigation.commonRootNode<JTree>() + Parent(
mapOf( mapOf(
KeyStroke.getKeyStroke('A') to IdeaAction("MaximizeToolWindow"),
KeyStroke.getKeyStroke('f') to StartSearch(),
KeyStroke.getKeyStroke('g') to Parent( KeyStroke.getKeyStroke('g') to Parent(
mapOf( mapOf(
KeyStroke.getKeyStroke('g') to IdeaAction("Tree-selectFirst"), KeyStroke.getKeyStroke('g') to IdeaAction("Tree-selectFirst"),
KeyStroke.getKeyStroke('j') to SelectLastSibling,
KeyStroke.getKeyStroke('k') to SelectFirstSibling,
KeyStroke.getKeyStroke('o') to ExpandChildrenToNextLevel,
) )
), ),
KeyStroke.getKeyStroke('G') to IdeaAction("Tree-selectLast"), KeyStroke.getKeyStroke('G') to IdeaAction("Tree-selectLast"),
KeyStroke.getKeyStroke('h') to CollapseSelfOrMoveToParentNode,
KeyStroke.getKeyStroke('H') to CollapseUntilRootNode,
KeyStroke.getKeyStroke('j') to IdeaAction("Tree-selectNext"), KeyStroke.getKeyStroke('j') to IdeaAction("Tree-selectNext"),
KeyStroke.getKeyStroke('j', KeyEvent.ALT_DOWN_MASK) to IdeaAction("Tree-selectNextSibling"), KeyStroke.getKeyStroke('j', KeyEvent.ALT_DOWN_MASK) to IdeaAction("Tree-selectNextSibling"),
KeyStroke.getKeyStroke('J') to SelectLastSibling, KeyStroke.getKeyStroke('J') to IdeaAction("Tree-selectNextExtendSelection"),
KeyStroke.getKeyStroke('k') to IdeaAction("Tree-selectPrevious"), KeyStroke.getKeyStroke('k') to IdeaAction("Tree-selectPrevious"),
KeyStroke.getKeyStroke('k', KeyEvent.ALT_DOWN_MASK) to IdeaAction("Tree-selectPreviousSibling"), KeyStroke.getKeyStroke('k', KeyEvent.ALT_DOWN_MASK) to IdeaAction("Tree-selectPreviousSibling"),
KeyStroke.getKeyStroke('K') to SelectFirstSibling, KeyStroke.getKeyStroke('K') to IdeaAction("Tree-selectPreviousExtendSelection"),
KeyStroke.getKeyStroke('m') to IdeaAction("ShowPopupMenu"), KeyStroke.getKeyStroke('l') to ExpandSelfOrMoveToFirstChildNode,
KeyStroke.getKeyStroke('L') to ExpandUntilFirstLeafNode,
KeyStroke.getKeyStroke('o') to ExpandOrCollapseTreeNode, KeyStroke.getKeyStroke('o') to ExpandOrCollapseTreeNode,
KeyStroke.getKeyStroke('O') to IdeaAction("FullyExpandTreeNode"), KeyStroke.getKeyStroke('O') to IdeaAction("FullyExpandTreeNode"),
KeyStroke.getKeyStroke('p') to IdeaAction("Tree-selectParentNoCollapse"), KeyStroke.getKeyStroke('p') to IdeaAction("Tree-selectParentNoCollapse"),
KeyStroke.getKeyStroke('q') to CloseParentPopupOrToolWindow(), KeyStroke.getKeyStroke('P') to IdeaAction("Tree-selectFirst"),
KeyStroke.getKeyStroke('x') to CollapseSelfOrParentNode, KeyStroke.getKeyStroke('x') to CollapseSelfOrParentNode,
KeyStroke.getKeyStroke('X') to IdeaAction("CollapseTreeNode"), KeyStroke.getKeyStroke('X') to CollapseAll,
KeyStroke.getKeyStroke('/') to StartSearch(),
) )
) )
@@ -59,7 +63,51 @@ internal object VimTreeNavigation {
tree.collapsePath(path) tree.collapsePath(path)
} }
else { else {
tree.expandPath(path) runWithoutAutoExpand(tree) { tree.expandPath(path) }
}
}
}
private data object ExpandSelfOrMoveToFirstChildNode : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath?.takeUnless { isLeaf(tree, it) } ?: return
if (tree.isExpanded(path)) {
selectRow(tree, getFirstChild(tree, path))
}
else {
runWithoutAutoExpand(tree) { tree.expandPath(path) }
}
}
}
private data object ExpandUntilFirstLeafNode : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath ?: return
var firstChildPath = path
while (!isLeaf(tree, firstChildPath)) {
tree.expandPath(firstChildPath)
firstChildPath = getFirstChild(tree, firstChildPath)
}
selectRow(tree, firstChildPath)
}
}
private data object CollapseSelfOrMoveToParentNode : 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)) {
collapseAndScroll(tree, path)
}
else {
withParentPath(tree, path) { selectRow(tree, it) }
} }
} }
} }
@@ -70,17 +118,76 @@ internal object VimTreeNavigation {
val path = tree.selectionPath ?: return val path = tree.selectionPath ?: return
if (tree.isExpanded(path)) { if (tree.isExpanded(path)) {
tree.collapsePath(path) collapseAndScroll(tree, path)
} }
else { else {
val parentPath = path.parentPath withParentPath(tree, path) { collapseAndScroll(tree, it) }
if (parentPath.parentPath != null || tree.isRootVisible) {
tree.collapsePath(parentPath)
}
} }
} }
} }
private data object CollapseUntilRootNode : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val path = tree.selectionPath ?: return
var parentPath = path
while (true) {
parentPath = parentPath.parentPath.takeUnless { isInvisibleRoot(tree, it) } ?: break
}
collapseAndScroll(tree, parentPath)
}
}
private data object CollapseAll : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
CollapseUntilRootNode.performAction(holder, actionEvent, keyEvent)
var row = 0
while (row < tree.rowCount) {
tree.collapseRow(row)
row++
}
}
}
private data object ExpandChildrenToNextLevel : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component
val model = tree.model
val path = tree.selectionPath?.takeUnless { isLeaf(tree, it) } ?: return
var pathsToExpand = mutableListOf(path)
do {
if (pathsToExpand.any(tree::isCollapsed)) {
runWithoutAutoExpand(tree) { pathsToExpand.forEach(tree::expandPath) }
break
}
val nextPathsToExpand = mutableListOf<TreePath>()
for (parentPath in pathsToExpand) {
val lastPathComponent = parentPath.lastPathComponent
for (i in 0 until model.getChildCount(lastPathComponent)) {
val child = model.getChild(lastPathComponent, i)
if (!model.isLeaf(child)) {
nextPathsToExpand.add(parentPath.pathByAddingChild(child))
}
}
}
pathsToExpand = nextPathsToExpand
} while (pathsToExpand.isNotEmpty())
}
}
private data object SelectFirstSibling : ActionNode<VimNavigationDispatcher<JTree>> { private data object SelectFirstSibling : ActionNode<VimNavigationDispatcher<JTree>> {
override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) { override fun performAction(holder: VimNavigationDispatcher<JTree>, actionEvent: AnActionEvent, keyEvent: KeyEvent) {
val tree = holder.component val tree = holder.component
@@ -89,7 +196,7 @@ internal object VimTreeNavigation {
val parentPath = path.parentPath ?: return val parentPath = path.parentPath ?: return
val parentRow = tree.getRowForPath(parentPath) val parentRow = tree.getRowForPath(parentPath)
tree.setSelectionRow(parentRow + 1) selectRow(tree, parentRow + 1)
} }
} }
@@ -115,7 +222,50 @@ internal object VimTreeNavigation {
} }
} }
tree.setSelectionRow(targetRow) selectRow(tree, targetRow)
} }
} }
private inline fun runWithoutAutoExpand(tree: JTree, action: () -> Unit) {
val previousAutoExpandValue = ClientProperty.get(tree, DefaultTreeUI.AUTO_EXPAND_ALLOWED)
ClientProperty.put(tree, DefaultTreeUI.AUTO_EXPAND_ALLOWED, false)
try {
action()
} finally {
ClientProperty.put(tree, DefaultTreeUI.AUTO_EXPAND_ALLOWED, previousAutoExpandValue)
}
}
private fun selectRow(tree: JTree, row: Int) {
tree.setSelectionRow(row)
tree.scrollRowToVisible(row)
}
private fun selectRow(tree: JTree, path: TreePath) {
selectRow(tree, tree.getRowForPath(path))
}
private fun collapseAndScroll(tree: JTree, path: TreePath) {
tree.collapsePath(path)
tree.scrollRowToVisible(tree.getRowForPath(path))
}
private inline fun withParentPath(tree: JTree, path: TreePath, action: (TreePath) -> Unit) {
val parentPath = path.parentPath
if (!isInvisibleRoot(tree, parentPath)) {
action(parentPath)
}
}
private fun isInvisibleRoot(tree: JTree, parentPath: TreePath): Boolean {
return parentPath.parentPath == null && !tree.isRootVisible
}
private fun getFirstChild(tree: JTree, path: TreePath): TreePath {
return path.pathByAddingChild(tree.model.getChild(path.lastPathComponent, 0))
}
private fun isLeaf(tree: JTree, firstChildPath: TreePath): Boolean {
return tree.model.isLeaf(firstChildPath.lastPathComponent)
}
} }

View File

@@ -7,6 +7,7 @@
Collection of keyboard-centric additions. Collection of keyboard-centric additions.
<ul> <ul>
<li>Code completion items can be jumped to using customizable characters (numbers by default).</li> <li>Code completion items can be jumped to using customizable characters (numbers by default).</li>
<li>Vim-style navigation in lists / trees / tables.</li>
<li>Action to go to next/previous highlighted error in other mode.</li> <li>Action to go to next/previous highlighted error in other mode.</li>
<li>Action to go to next/previous type in file.</li> <li>Action to go to next/previous type in file.</li>
</ul> </ul>