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

Create commandState helper function

This commit is contained in:
Alex Plate 2019-11-28 14:42:57 +03:00
parent a4843e57cb
commit 3e78f50a76
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
17 changed files with 50 additions and 49 deletions

View File

@ -22,14 +22,14 @@ import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.handler.VimActionHandler
import com.maddyhome.idea.vim.helper.commandState
class RepeatChangeAction : VimActionHandler.SingleExecution() {
override val type: Command.Type = Command.Type.OTHER_WRITABLE
override fun execute(editor: Editor, context: DataContext, cmd: Command): Boolean {
val state = CommandState.getInstance(editor)
val state = editor.commandState
val lastCommand = VimRepeater.lastChangeCommand ?: return false
if (cmd.rawCount > 0) {

View File

@ -23,7 +23,6 @@ import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.CommandState.SubMode
import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.common.TextRange
@ -31,6 +30,7 @@ import com.maddyhome.idea.vim.group.visual.VimSelection
import com.maddyhome.idea.vim.handler.VisualOperatorActionHandler
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.helper.subMode
import java.util.*
/**
@ -46,7 +46,7 @@ class DeleteVisualLinesAction : VisualOperatorActionHandler.ForEachCaret() {
context: DataContext,
cmd: Command,
range: VimSelection): Boolean {
val mode = CommandState.getInstance(editor).subMode
val mode = editor.subMode
val textRange = range.toVimTextRange(false)
return if (mode == SubMode.VISUAL_BLOCK) {
VimPlugin.getChange()

View File

@ -22,8 +22,8 @@ import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.handler.VimActionHandler
import com.maddyhome.idea.vim.helper.commandState
class ToggleRecordingAction : VimActionHandler.SingleExecution() {
override val type: Command.Type = Command.Type.OTHER_READONLY
@ -31,7 +31,7 @@ class ToggleRecordingAction : VimActionHandler.SingleExecution() {
override val argumentType: Argument.Type = Argument.Type.CHARACTER
override fun execute(editor: Editor, context: DataContext, cmd: Command): Boolean {
return if (!CommandState.getInstance(editor).isRecording) {
return if (!editor.commandState.isRecording) {
val argument = cmd.argument ?: return false
val reg = argument.character
VimPlugin.getRegister().startRecording(editor, reg)

View File

@ -25,12 +25,12 @@ import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.helper.inInsertMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.vimLastColumn
import com.maddyhome.idea.vim.option.OptionsManager
import java.util.*
@ -47,7 +47,7 @@ class MotionLastColumnAction : MotionActionHandler.ForEachCaret() {
var allow = false
if (editor.inInsertMode) {
allow = true
} else if (CommandState.getInstance(editor).mode == CommandState.Mode.VISUAL) {
} else if (editor.inVisualMode) {
val opt = OptionsManager.selection
if (opt.value != "old") {
allow = true
@ -86,7 +86,7 @@ class MotionLastColumnInsertAction : MotionActionHandler.ForEachCaret() {
var allow = false
if (editor.inInsertMode) {
allow = true
} else if (CommandState.getInstance(editor).mode == CommandState.Mode.VISUAL) {
} else if (editor.inVisualMode) {
val opt = OptionsManager.selection
if (opt.value != "old") {
allow = true

View File

@ -23,11 +23,11 @@ import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.inInsertMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.vimLastColumn
import com.maddyhome.idea.vim.option.OptionsManager.selection
@ -41,7 +41,7 @@ class MotionLastScreenColumnAction : MotionActionHandler.ForEachCaret() {
var allow = false
if (editor.inInsertMode) {
allow = true
} else if (CommandState.getInstance(editor).mode == CommandState.Mode.VISUAL) {
} else if (editor.inVisualMode) {
val opt = selection
if (opt.value != "old") {
allow = true

View File

@ -26,6 +26,7 @@ import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MappingMode
import com.maddyhome.idea.vim.group.visual.updateCaretState
import com.maddyhome.idea.vim.handler.VimActionHandler
import com.maddyhome.idea.vim.helper.commandState
/**
* @author Alex Plate
@ -36,7 +37,7 @@ class SelectToggleVisualMode : VimActionHandler.SingleExecution() {
override val type: Command.Type = Command.Type.OTHER_READONLY
override fun execute(editor: Editor, context: DataContext, cmd: Command): Boolean {
val commandState = CommandState.getInstance(editor)
val commandState = editor.commandState
val subMode = commandState.subMode
val mode = commandState.mode
commandState.popState()

View File

@ -24,11 +24,11 @@ import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.handler.MotionActionHandler
import com.maddyhome.idea.vim.helper.enumSetOf
import com.maddyhome.idea.vim.helper.inInsertMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.option.OptionsManager
import java.util.*
@ -46,7 +46,7 @@ class MotionGotoLineLastEndAction : MotionActionHandler.ForEachCaret() {
var allow = false
if (editor.inInsertMode) {
allow = true
} else if (CommandState.getInstance(editor).mode == CommandState.Mode.VISUAL) {
} else if (editor.inVisualMode) {
val opt = OptionsManager.selection
if (opt.value != "old") {
allow = true
@ -71,7 +71,7 @@ class MotionGotoLineLastEndInsertAction : MotionActionHandler.ForEachCaret() {
var allow = false
if (editor.inInsertMode) {
allow = true
} else if (CommandState.getInstance(editor).mode == CommandState.Mode.VISUAL) {
} else if (editor.inVisualMode) {
val opt = OptionsManager.selection
if (opt.value != "old") {
allow = true

View File

@ -24,9 +24,9 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.Ref
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.helper.MessageHelper
import com.maddyhome.idea.vim.helper.Msg
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.noneOfEnum
@ -147,7 +147,7 @@ sealed class CommandHandler {
VimPlugin.showMessage(MessageHelper.message(Msg.e_argforb))
throw NoArgumentAllowedException()
}
CommandState.getInstance(editor).flags = optFlags
editor.commandState.flags = optFlags
if (editor.inVisualMode && Flag.SAVE_VISUAL !in argFlags.flags) {
editor.exitVisualMode()
}

View File

@ -25,7 +25,6 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.VisualPosition
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MappingMode
import com.maddyhome.idea.vim.common.TextRange
import com.maddyhome.idea.vim.extension.VimExtensionFacade.putExtensionHandlerMapping
@ -87,10 +86,9 @@ class VimMultipleCursorsExtension : VimNonDisposableExtension() {
inner class NextOccurrenceHandler(val whole: Boolean = true) : WriteActionHandler() {
override fun executeInWriteAction(editor: Editor, context: DataContext) {
val caretModel = editor.caretModel
val commandState = CommandState.getInstance(editor)
val patternComparator = if (OptionsManager.ignorecase.isSet) String.CASE_INSENSITIVE_ORDER else Comparator(String::compareTo);
if (commandState.mode != CommandState.Mode.VISUAL) {
if (!editor.inVisualMode) {
if (caretModel.caretCount > 1) return
val caret = caretModel.primaryCaret

View File

@ -6,6 +6,7 @@ import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.helper.EditorDataContext
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.exitSelectMode
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.helper.hasVisualSelection
@ -47,13 +48,13 @@ object IdeaSelectionControl {
return@singleTask
}
logger.info("Some carets have selection. State before adjustment: ${CommandState.getInstance(editor).toSimpleString()}")
logger.info("Some carets have selection. State before adjustment: ${editor.commandState.toSimpleString()}")
editor.popAllModes()
activateMode(editor, chooseSelectionMode(editor, selectionSource, true))
} else {
logger.info("None of carets have selection. State before adjustment: ${CommandState.getInstance(editor).toSimpleString()}")
logger.info("None of carets have selection. State before adjustment: ${editor.commandState.toSimpleString()}")
if (editor.inVisualMode) editor.exitVisualMode()
if (editor.inSelectMode) editor.exitSelectMode(false)

View File

@ -18,7 +18,6 @@
package com.maddyhome.idea.vim.group.visual
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
@ -31,6 +30,7 @@ import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.common.TextRange
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.helper.EditorHelper
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.helper.inVisualMode
import com.maddyhome.idea.vim.helper.subMode
@ -51,8 +51,7 @@ class VisualMotionGroup {
editor.caretModel.removeSecondaryCarets()
CommandState.getInstance(editor)
.pushState(CommandState.Mode.VISUAL, lastSelectionType.toSubMode(), MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, lastSelectionType.toSubMode(), MappingMode.VISUAL)
val primaryCaret = editor.caretModel.primaryCaret
primaryCaret.vimSetSelection(visualMarks.startOffset, visualMarks.endOffset, true)
@ -121,7 +120,7 @@ class VisualMotionGroup {
if (rawCount > 0) {
val primarySubMode = editor.caretModel.primaryCaret.vimLastVisualOperatorRange?.type?.toSubMode()
?: subMode
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, primarySubMode, MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, primarySubMode, MappingMode.VISUAL)
editor.vimForEachCaret {
val range = it.vimLastVisualOperatorRange ?: VisualChange.default(subMode)
@ -131,7 +130,7 @@ class VisualMotionGroup {
it.vimSetSelection(it.offset, end, true)
}
} else {
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, subMode, MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, subMode, MappingMode.VISUAL)
editor.vimForEachCaret { it.vimSetSelection(it.offset) }
}
return true
@ -157,9 +156,9 @@ class VisualMotionGroup {
val autodetectedMode = autodetectVisualSubmode(editor)
if (editor.inVisualMode) {
CommandState.getInstance(editor).popState()
editor.commandState.popState()
}
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, autodetectedMode, MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, autodetectedMode, MappingMode.VISUAL)
if (autodetectedMode == CommandState.SubMode.VISUAL_BLOCK) {
val (start, end) = blockModeStartAndEnd(editor)
editor.caretModel.removeSecondaryCarets()
@ -200,7 +199,7 @@ class VisualMotionGroup {
*/
fun enterVisualMode(editor: Editor, subMode: CommandState.SubMode? = null): Boolean {
val autodetectedSubMode = subMode ?: autodetectVisualSubmode(editor)
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, autodetectedSubMode, MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, autodetectedSubMode, MappingMode.VISUAL)
if (autodetectedSubMode == CommandState.SubMode.VISUAL_BLOCK) {
editor.caretModel.primaryCaret.run { vimSelectionStart = vimLeadSelectionOffset }
} else {
@ -211,7 +210,7 @@ class VisualMotionGroup {
}
fun enterSelectMode(editor: Editor, subMode: CommandState.SubMode): Boolean {
CommandState.getInstance(editor).pushState(CommandState.Mode.SELECT, subMode, MappingMode.SELECT)
editor.commandState.pushState(CommandState.Mode.SELECT, subMode, MappingMode.SELECT)
editor.vimForEachCaret { it.vimSelectionStart = it.vimLeadSelectionOffset }
updateCaretState(editor)
return true

View File

@ -28,8 +28,8 @@ import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.helper.StringHelper
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.getTopLevelEditor
import com.maddyhome.idea.vim.helper.noneOfEnum
import java.util.*
@ -133,8 +133,7 @@ sealed class EditorActionHandlerBase(private val myRunForEachCaret: Boolean) {
val editor = _editor.getTopLevelEditor()
logger.debug("Execute command with handler: " + this.javaClass.name)
val state = CommandState.getInstance(editor)
val cmd = state.command ?: run {
val cmd = editor.commandState.command ?: run {
VimPlugin.indicateError()
return
}

View File

@ -28,7 +28,6 @@ import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.action.change.VimRepeater
import com.maddyhome.idea.vim.command.Command
import com.maddyhome.idea.vim.command.CommandFlags
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.SelectionType
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.group.visual.VimBlockSelection
@ -36,6 +35,7 @@ import com.maddyhome.idea.vim.group.visual.VimSelection
import com.maddyhome.idea.vim.group.visual.VimSimpleSelection
import com.maddyhome.idea.vim.group.visual.VisualChange
import com.maddyhome.idea.vim.group.visual.VisualOperation
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.exitVisualMode
import com.maddyhome.idea.vim.helper.inBlockSubMode
import com.maddyhome.idea.vim.helper.inRepeatMode
@ -183,7 +183,7 @@ sealed class VisualOperatorActionHandler : VimActionHandler.SingleExecution() {
}
else -> this.caretModel.allCarets.associateWith { caret ->
val subMode = CommandState.getInstance(this).subMode
val subMode = this.commandState.subMode
VimSimpleSelection.createWithNative(
caret.vimSelectionStart,
caret.offset,

View File

@ -24,12 +24,12 @@ val CommandState.Mode.hasVisualSelection
}
val Editor.mode
get() = CommandState.getInstance(this).mode
get() = this.commandState.mode
var Editor.subMode
get() = CommandState.getInstance(this).subMode
get() = this.commandState.subMode
set(value) {
CommandState.getInstance(this).subMode = value
this.commandState.subMode = value
}
@get:JvmName("inNormalMode")
@ -42,7 +42,7 @@ val Editor.inInsertMode
@get:JvmName("inRepeatMode")
val Editor.inRepeatMode
get() = CommandState.getInstance(this).isDotRepeatInProgress
get() = this.commandState.isDotRepeatInProgress
@get:JvmName("inVisualMode")
val Editor.inVisualMode
@ -59,3 +59,6 @@ val Editor.inBlockSubMode
@get:JvmName("inSingleCommandMode")
val Editor.inSingleCommandMode
get() = this.subMode == CommandState.SubMode.SINGLE_COMMAND && this.inNormalMode
val Editor?.commandState
get() = CommandState.getInstance(this)

View File

@ -16,14 +16,14 @@ import com.maddyhome.idea.vim.listener.SelectionVimListenerSuppressor
* Pop all modes, but leave editor state. E.g. editor selection is not removed.
*/
fun Editor.popAllModes() {
val commandState = CommandState.getInstance(this)
val commandState = this.commandState
while (commandState.mode != CommandState.Mode.COMMAND) {
commandState.popState()
}
}
fun Editor.hardResetAllModes() {
val commandState = CommandState.getInstance(this)
val commandState = this.commandState
while (!this.inNormalMode) {
val statesBefore = commandState.toSimpleString()
when (mode) {
@ -60,7 +60,7 @@ fun Editor.exitVisualMode() {
this.subMode = CommandState.SubMode.NONE
CommandState.getInstance(this).popState()
this.commandState.popState()
}
}
@ -68,7 +68,7 @@ fun Editor.exitVisualMode() {
fun Editor.exitSelectMode(adjustCaretPosition: Boolean) {
if (!this.inSelectMode) return
CommandState.getInstance(this).popState()
this.commandState.popState()
SelectionVimListenerSuppressor.lock().use {
this.caretModel.allCarets.forEach {
it.removeSelection()

View File

@ -43,6 +43,7 @@ import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.group.visual.IdeaSelectionControl
import com.maddyhome.idea.vim.group.visual.moveCaretOneCharLeftFromSelectionEnd
import com.maddyhome.idea.vim.helper.EditorDataContext
import com.maddyhome.idea.vim.helper.commandState
import com.maddyhome.idea.vim.helper.inNormalMode
import com.maddyhome.idea.vim.option.IdeaRefactorMode
import java.beans.PropertyChangeEvent
@ -90,7 +91,7 @@ object IdeaSpecifics {
//region Enter insert mode after surround with if
if (surrounderAction == action.javaClass.name && surrounderItems.any { action.templatePresentation.text.endsWith(it) }) {
editor?.let {
val commandState = CommandState.getInstance(editor)
val commandState = editor.commandState
while (commandState.mode != CommandState.Mode.COMMAND) {
commandState.popState()
}

View File

@ -21,6 +21,7 @@ package org.jetbrains.plugins.ideavim.extension.multiplecursors
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.command.MappingMode
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.helper.commandState
import org.jetbrains.plugins.ideavim.VimTestCase
class VimMultipleCursorsExtensionTest : VimTestCase() {
@ -254,8 +255,7 @@ class VimMultipleCursorsExtensionTest : VimTestCase() {
|dfkjsg
""".trimMargin()
val editor = configureByText(before)
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER,
MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER, MappingMode.VISUAL)
typeText(parseKeys("<A-p>"))
myFixture.checkResult(before)
@ -441,8 +441,7 @@ class VimMultipleCursorsExtensionTest : VimTestCase() {
|dfkjsg
""".trimMargin()
val editor = configureByText(before)
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER,
MappingMode.VISUAL)
editor.commandState.pushState(CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER, MappingMode.VISUAL)
typeText(parseKeys("<A-x>"))
assertMode(CommandState.Mode.VISUAL)