1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-03-14 06:15:44 +01:00

Move some logic to CommandCountConsumer

This commit is contained in:
filipp 2024-02-23 13:45:29 +02:00
parent 43175061e0
commit 9826f0a7f0
2 changed files with 67 additions and 25 deletions
vim-engine/src/main/kotlin/com/maddyhome/idea/vim

View File

@ -32,8 +32,10 @@ import com.maddyhome.idea.vim.helper.vimStateMachine
import com.maddyhome.idea.vim.impl.state.toMappingMode
import com.maddyhome.idea.vim.key.CommandNode
import com.maddyhome.idea.vim.key.CommandPartNode
import com.maddyhome.idea.vim.key.KeyConsumer
import com.maddyhome.idea.vim.key.KeyStack
import com.maddyhome.idea.vim.key.Node
import com.maddyhome.idea.vim.key.consumers.CommandCountConsumer
import com.maddyhome.idea.vim.state.KeyHandlerState
import com.maddyhome.idea.vim.state.VimStateMachine
import com.maddyhome.idea.vim.state.mode.Mode
@ -49,6 +51,7 @@ import javax.swing.KeyStroke
* actions. This is a singleton.
*/
public class KeyHandler {
private val keyConsumers: List<KeyConsumer> = listOf(MappingProcessor, CommandCountConsumer())
public var keyHandlerState: KeyHandlerState = KeyHandlerState()
private set
@ -125,16 +128,15 @@ public class KeyHandler {
// We only record unmapped keystrokes. If we've recursed to handle mapping, don't record anything.
val shouldRecord = MutableBoolean(handleKeyRecursionCount == 0 && injector.registerGroup.isRecording)
var isProcessed = false
handleKeyRecursionCount++
try {
LOG.trace("Start key processing...")
if (!MappingProcessor.consumeKey(key, editor, allowKeyMappings, mappingCompleted, processBuilder, shouldRecord)) {
var isProcessed = keyConsumers.any {
it.consumeKey( key, editor, allowKeyMappings, mappingCompleted, processBuilder, shouldRecord )
}
if (!isProcessed) {
LOG.trace("Mappings processed, continue processing key.")
if (isCommandCountKey(chKey, processBuilder.state, editorState)) {
commandBuilder.addCountCharacter(key)
isProcessed = true
} else if (isDeleteCommandCountKey(key, processBuilder.state, editorState.mode)) {
if (isDeleteCommandCountKey(key, processBuilder.state, editorState.mode)) {
commandBuilder.deleteCountCharacter()
isProcessed = true
} else if (isEditorReset(key, editorState)) {
@ -197,8 +199,6 @@ public class KeyHandler {
} else {
isProcessed = true
}
} else {
isProcessed = true
}
if (isProcessed) {
processBuilder.addExecutionStep { lambdaKeyState, lambdaEditor, lambdaContext ->
@ -323,23 +323,6 @@ public class KeyHandler {
reset(keyState, editor.mode)
}
private fun isCommandCountKey(chKey: Char, keyState: KeyHandlerState, editorState: VimStateMachine): Boolean {
// Make sure to avoid handling '0' as the start of a count.
val commandBuilder = keyState.commandBuilder
val notRegisterPendingCommand = editorState.mode is Mode.NORMAL && !editorState.isRegisterPending
val visualMode = editorState.mode is Mode.VISUAL && !editorState.isRegisterPending
val opPendingMode = editorState.mode is Mode.OP_PENDING
if (notRegisterPendingCommand || visualMode || opPendingMode) {
if (commandBuilder.isExpectingCount && Character.isDigit(chKey) && (commandBuilder.count > 0 || chKey != '0')) {
LOG.debug("This is a command key count")
return true
}
}
LOG.debug("This is NOT a command key count")
return false
}
private fun isDeleteCommandCountKey(key: KeyStroke, keyState: KeyHandlerState, mode: Mode): Boolean {
// See `:help N<Del>`
val commandBuilder = keyState.commandBuilder

View File

@ -0,0 +1,59 @@
/*
* Copyright 2003-2024 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/
package com.maddyhome.idea.vim.key.consumers
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.KeyProcessResult
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.diagnostic.vimLogger
import com.maddyhome.idea.vim.helper.vimStateMachine
import com.maddyhome.idea.vim.key.KeyConsumer
import com.maddyhome.idea.vim.state.KeyHandlerState
import com.maddyhome.idea.vim.state.mode.Mode
import java.awt.event.KeyEvent
import javax.swing.KeyStroke
public class CommandCountConsumer : KeyConsumer {
private companion object {
private val logger = vimLogger<CommandCountConsumer>()
}
override fun consumeKey(
key: KeyStroke,
editor: VimEditor,
allowKeyMappings: Boolean,
mappingCompleted: Boolean,
keyProcessResultBuilder: KeyProcessResult.KeyProcessResultBuilder,
shouldRecord: KeyHandler.MutableBoolean,
): Boolean {
val chKey: Char = if (key.keyChar == KeyEvent.CHAR_UNDEFINED) 0.toChar() else key.keyChar
if (!isCommandCountKey(chKey, keyProcessResultBuilder.state, editor)) return false
keyProcessResultBuilder.state.commandBuilder.addCountCharacter(key)
return true
}
private fun isCommandCountKey(chKey: Char, keyState: KeyHandlerState, editor: VimEditor): Boolean {
// Make sure to avoid handling '0' as the start of a count.
val editorState = editor.vimStateMachine
val commandBuilder = keyState.commandBuilder
val notRegisterPendingCommand = editorState.mode is Mode.NORMAL && !editorState.isRegisterPending
val visualMode = editorState.mode is Mode.VISUAL && !editorState.isRegisterPending
val opPendingMode = editorState.mode is Mode.OP_PENDING
if (notRegisterPendingCommand || visualMode || opPendingMode) {
if (commandBuilder.isExpectingCount && Character.isDigit(chKey) && (commandBuilder.count > 0 || chKey != '0')) {
logger.debug("This is a command key count")
return true
}
}
logger.debug("This is NOT a command key count")
return false
}
}