1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-07-30 00:59:08 +02:00

Refactor expected argument type

We no longer need to track a previous fallback argument type, since we don't support nested commands inside a command builder. We can just return the current argument type, or its fallback
This commit is contained in:
Matt Ellis 2024-07-23 17:46:39 +01:00 committed by lippfi
parent 7217fdf734
commit a8677d3dd7

View File

@ -86,13 +86,15 @@ class CommandBuilder(
val register: Char? val register: Char?
get() = commandParts.lastOrNull { it.register != null }?.register get() = commandParts.lastOrNull { it.register != null }?.register
// The argument type for the current command part's action. Kept separate to handle digraphs and characters. We first /**
// try to accept a digraph. If we get it, set expected argument type to character and handle the converted key. If we * The argument type for the current in-progress command part's action
// fail to convert to a digraph, set expected argument type to character and try to handle the key again. *
var expectedArgumentType: Argument.Type? = null * For digraph arguments, this can fall back to [Argument.Type.CHARACTER] if there isn't a digraph match.
private set */
val expectedArgumentType: Argument.Type?
get() = fallbackArgumentType ?: commandParts.lastOrNull()?.action?.argumentType
private var prevExpectedArgumentType: Argument.Type? = null private var fallbackArgumentType: Argument.Type? = null
val isReady: Boolean get() = commandState == CurrentCommandState.READY val isReady: Boolean get() = commandState == CurrentCommandState.READY
val isEmpty: Boolean get() = commandParts.isEmpty() val isEmpty: Boolean get() = commandParts.isEmpty()
@ -108,8 +110,7 @@ class CommandBuilder(
fun pushCommandPart(action: EditorActionHandlerBase) { fun pushCommandPart(action: EditorActionHandlerBase) {
logger.trace { "pushCommandPart is executed. action = $action" } logger.trace { "pushCommandPart is executed. action = $action" }
commandParts.add(Command(count, action, action.type, action.flags)) commandParts.add(Command(count, action, action.type, action.flags))
prevExpectedArgumentType = expectedArgumentType fallbackArgumentType = null
expectedArgumentType = action.argumentType
count = 0 count = 0
} }
@ -118,7 +119,7 @@ class CommandBuilder(
// We will never execute this command, but we need to push something to correctly handle counts on either side of a // We will never execute this command, but we need to push something to correctly handle counts on either side of a
// select register command part. e.g. 2"a2d2w or even crazier 2"a2"a2"a2"a2"a2d2w // select register command part. e.g. 2"a2d2w or even crazier 2"a2"a2"a2"a2"a2d2w
commandParts.add(Command(count, register)) commandParts.add(Command(count, register))
expectedArgumentType = null fallbackArgumentType = null
count = 0 count = 0
} }
@ -127,7 +128,7 @@ class CommandBuilder(
// Finished handling DIGRAPH. We either succeeded, in which case handle the converted character, or failed to parse, // Finished handling DIGRAPH. We either succeeded, in which case handle the converted character, or failed to parse,
// in which case try to handle input as a character argument. // in which case try to handle input as a character argument.
assert(expectedArgumentType == Argument.Type.DIGRAPH) { "Cannot move state from $expectedArgumentType to CHARACTER" } assert(expectedArgumentType == Argument.Type.DIGRAPH) { "Cannot move state from $expectedArgumentType to CHARACTER" }
expectedArgumentType = Argument.Type.CHARACTER fallbackArgumentType = Argument.Type.CHARACTER
} }
fun addKey(key: KeyStroke) { fun addKey(key: KeyStroke) {
@ -161,10 +162,8 @@ class CommandBuilder(
} }
fun isAwaitingCharOrDigraphArgument(): Boolean { fun isAwaitingCharOrDigraphArgument(): Boolean {
if (commandParts.size == 0) return false val awaiting = expectedArgumentType == Argument.Type.CHARACTER || expectedArgumentType == Argument.Type.DIGRAPH
val argumentType = commandParts.last().action.argumentType logger.debug { "Awaiting char or digraph: $awaiting" }
val awaiting = argumentType == Argument.Type.CHARACTER || argumentType == Argument.Type.DIGRAPH
logger.debug { "Awaiting char of digraph: $awaiting" }
return awaiting return awaiting
} }
@ -215,7 +214,7 @@ class CommandBuilder(
assert(commandParts.size == 0) assert(commandParts.size == 0)
} }
} }
expectedArgumentType = null fallbackArgumentType = null
return command return command
} }
@ -225,8 +224,7 @@ class CommandBuilder(
commandState = CurrentCommandState.NEW_COMMAND commandState = CurrentCommandState.NEW_COMMAND
commandParts.clear() commandParts.clear()
keyList.clear() keyList.clear()
expectedArgumentType = null fallbackArgumentType = null
prevExpectedArgumentType = null
} }
fun resetCount() { fun resetCount() {
@ -253,7 +251,7 @@ class CommandBuilder(
if (commandState != other.commandState) return false if (commandState != other.commandState) return false
if (count != other.count) return false if (count != other.count) return false
if (expectedArgumentType != other.expectedArgumentType) return false if (expectedArgumentType != other.expectedArgumentType) return false
if (prevExpectedArgumentType != other.prevExpectedArgumentType) return false if (fallbackArgumentType != other.fallbackArgumentType) return false
return true return true
} }
@ -265,16 +263,14 @@ class CommandBuilder(
result = 31 * result + commandState.hashCode() result = 31 * result + commandState.hashCode()
result = 31 * result + count result = 31 * result + count
result = 31 * result + (expectedArgumentType?.hashCode() ?: 0) result = 31 * result + (expectedArgumentType?.hashCode() ?: 0)
result = 31 * result + (prevExpectedArgumentType?.hashCode() ?: 0) result = 31 * result + (fallbackArgumentType?.hashCode() ?: 0)
return result return result
} }
public override fun clone(): CommandBuilder { public override fun clone(): CommandBuilder {
val result = CommandBuilder(currentCommandPartNode, ArrayDeque(commandParts), keyList.toMutableList(), count) val result = CommandBuilder(currentCommandPartNode, ArrayDeque(commandParts), keyList.toMutableList(), count)
result.commandState = commandState result.commandState = commandState
result.expectedArgumentType = expectedArgumentType result.fallbackArgumentType = fallbackArgumentType
result.prevExpectedArgumentType = prevExpectedArgumentType
return result return result
} }