Skip to content

Commit

Permalink
Partial implementation of virtualedit config
Browse files Browse the repository at this point in the history
This does not support all config settings,
but does add the 'onemore' option.

This partly addresses https://youtrack.jetbrains.com/issue/VIM-844
  • Loading branch information
i-e-b committed Jul 14, 2020
1 parent 5885096 commit 180bbe3
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import com.maddyhome.idea.vim.command.Argument
import com.maddyhome.idea.vim.command.MotionType
import com.maddyhome.idea.vim.handler.NonShiftedSpecialKeyHandler
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.helper.isEndAllowed
import com.maddyhome.idea.vim.helper.mode
import java.awt.event.KeyEvent
import javax.swing.KeyStroke

Expand All @@ -39,7 +41,8 @@ class MotionArrowRightAction : NonShiftedSpecialKeyHandler(), ComplicatedKeysAct
)

override fun offset(editor: Editor, caret: Caret, context: DataContext, count: Int, rawCount: Int, argument: Argument?): Int {
return VimPlugin.getMotion().moveCaretHorizontal(editor, caret, count, false)
val allowPastEnd = editor.mode.isEndAllowed
return VimPlugin.getMotion().moveCaretHorizontal(editor, caret, count, allowPastEnd)
}
}

3 changes: 2 additions & 1 deletion src/com/maddyhome/idea/vim/helper/CommandStateExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ package com.maddyhome.idea.vim.helper

import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.option.OptionsManager

val CommandState.Mode.isEndAllowed
get() = when (this) {
CommandState.Mode.INSERT, CommandState.Mode.VISUAL, CommandState.Mode.SELECT -> true
CommandState.Mode.COMMAND, CommandState.Mode.CMD_LINE, CommandState.Mode.REPLACE, CommandState.Mode.OP_PENDING -> false
CommandState.Mode.COMMAND, CommandState.Mode.CMD_LINE, CommandState.Mode.REPLACE, CommandState.Mode.OP_PENDING -> OptionsManager.virtualedit.value == "onemore"
}

val CommandState.Mode.isBlockCaret
Expand Down
87 changes: 87 additions & 0 deletions src/com/maddyhome/idea/vim/helper/CommandStateExtensions.kt.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2020 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

@file:JvmName("CommandStateHelper")

package com.maddyhome.idea.vim.helper

import com.intellij.openapi.editor.Editor
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.option.OptionsManager

val CommandState.Mode.isEndAllowed
get() = when (this) {
CommandState.Mode.INSERT, CommandState.Mode.VISUAL, CommandState.Mode.SELECT -> true
<<<<<<< HEAD
CommandState.Mode.COMMAND, CommandState.Mode.CMD_LINE, CommandState.Mode.REPLACE, CommandState.Mode.OP_PENDING -> false
=======
CommandState.Mode.COMMAND, CommandState.Mode.CMD_LINE, CommandState.Mode.REPLACE -> OptionsManager.virtualedit.value == "onemore"
>>>>>>> f7ff3089... Partial implementation of virtualedit config
}

val CommandState.Mode.isBlockCaret
get() = when (this) {
CommandState.Mode.VISUAL, CommandState.Mode.COMMAND, CommandState.Mode.OP_PENDING -> true
CommandState.Mode.INSERT, CommandState.Mode.CMD_LINE, CommandState.Mode.REPLACE, CommandState.Mode.SELECT -> false
}

val CommandState.Mode.hasVisualSelection
get() = when (this) {
CommandState.Mode.VISUAL, CommandState.Mode.SELECT -> true
CommandState.Mode.REPLACE, CommandState.Mode.CMD_LINE, CommandState.Mode.COMMAND, CommandState.Mode.INSERT, CommandState.Mode.OP_PENDING -> false
}

val Editor.mode
get() = this.commandState.mode

var Editor.subMode
get() = this.commandState.subMode
set(value) {
this.commandState.subMode = value
}

@get:JvmName("inNormalMode")
val Editor.inNormalMode
get() = this.mode == CommandState.Mode.COMMAND

@get:JvmName("inInsertMode")
val Editor.inInsertMode
get() = this.mode == CommandState.Mode.INSERT || this.mode == CommandState.Mode.REPLACE

@get:JvmName("inRepeatMode")
val Editor.inRepeatMode
get() = this.commandState.isDotRepeatInProgress

@get:JvmName("inVisualMode")
val Editor.inVisualMode
get() = this.mode == CommandState.Mode.VISUAL

@get:JvmName("inSelectMode")
val Editor.inSelectMode
get() = this.mode == CommandState.Mode.SELECT

@get:JvmName("inBlockSubMode")
val Editor.inBlockSubMode
get() = this.subMode == CommandState.SubMode.VISUAL_BLOCK

@get:JvmName("inSingleCommandMode")
val Editor.inSingleCommandMode
get() = this.subMode == CommandState.SubMode.SINGLE_COMMAND && this.inNormalMode

val Editor?.commandState
get() = CommandState.getInstance(this)
1 change: 1 addition & 0 deletions src/com/maddyhome/idea/vim/option/OptionsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ object OptionsManager {
val idearefactormode = addOption(BoundStringOption(IdeaRefactorMode.name, IdeaRefactorMode.name, IdeaRefactorMode.select, IdeaRefactorMode.availableValues))
val ideastatusicon = addOption(BoundStringOption(IdeaStatusIcon.name, IdeaStatusIcon.name, IdeaStatusIcon.enabled, IdeaStatusIcon.allValues))
val ideastrictmode = addOption(ToggleOption("ideastrictmode", "ideastrictmode", false))
val virtualedit = addOption(BoundStringOption("virtualedit","ve","", arrayOf("block", "insert", "all", "onemore") ))

// Dev only experimental options
val dialogescape = addOption(BoundStringOption("dialogescape", "de", "legacy", arrayOf("legacy", "on", "off")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package org.jetbrains.plugins.ideavim.action.motion.leftright
import com.maddyhome.idea.vim.command.CommandState
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
import com.maddyhome.idea.vim.option.OptionsManager
import org.jetbrains.plugins.ideavim.VimTestCase

class MotionRightActionTest : VimTestCase() {
Expand Down Expand Up @@ -80,6 +81,25 @@ class MotionRightActionTest : VimTestCase() {
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

fun `test virtual edit motion to the end`() {
OptionsManager.virtualedit.set("onemore")
doTest("3l", """
A Discovery
I found it in a legendary lan${c}d
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery
I found it in a legendary land${c}
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

@TestWithoutNeovim(SkipNeovimReason.NON_ASCII)
fun `test simple motion non-ascii`() {
doTest("l", """
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2020 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

@file:Suppress("RemoveCurlyBracesFromTemplate")

package org.jetbrains.plugins.ideavim.action.motion.leftright

import com.maddyhome.idea.vim.command.CommandState
<<<<<<< HEAD
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
=======
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.option.OptionsManager
>>>>>>> f7ff3089... Partial implementation of virtualedit config
import org.jetbrains.plugins.ideavim.VimTestCase

class MotionRightActionTest : VimTestCase() {
fun `test simple motion`() {
doTest("l", """
A Discovery

I found ${c}it in a legendary land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found i${c}t in a legendary land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

fun `test simple motion with repeat`() {
doTest("3l", """
A Discovery

I found ${c}it in a legendary land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it ${c}in a legendary land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

fun `test simple motion to the end`() {
doTest("3l", """
A Discovery

I found it in a legendary lan${c}d
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendary lan${c}d
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

<<<<<<< HEAD
@TestWithoutNeovim(SkipNeovimReason.NON_ASCII)
=======
fun `test virtual edit motion to the end`() {
OptionsManager.virtualedit.set("onemore")
doTest(parseKeys("3l"), """
A Discovery

I found it in a legendary lan${c}d
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendary land${c}
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

>>>>>>> f7ff3089... Partial implementation of virtualedit config
fun `test simple motion non-ascii`() {
doTest("l", """
A Discovery

I found it in a legendar${c}𝛁 land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendar𝛁${c} land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

@TestWithoutNeovim(SkipNeovimReason.NON_ASCII)
fun `test simple motion emoji`() {
doTest("l", """
A Discovery

I found it in a legendar${c}🐔 land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendar🐔${c} land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

@TestWithoutNeovim(SkipNeovimReason.NON_ASCII)
fun `test simple motion czech`() {
doTest("l", """
A Discovery

I found it in a legendar${c}ž land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendarž${c} land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

fun `test simple motion tab`() {
doTest("l", """
A Discovery

I found it in a legendar${c}. land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass
""".trimIndent().dotToTab(), """
A Discovery

I found it in a legendar.${c} land
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass
""".trimIndent().dotToTab(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE)
}

fun `test char visual mode`() {
doTest(listOf("v", "ll"), """
A Discovery

I found it in a legendary lan${c}d
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendary lan${s}d${c}${se}
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER)
}

fun `test block visual mode`() {
doTest(listOf("<C-V>", "ll"), """
A Discovery

I found it in a legendary lan${c}d
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), """
A Discovery

I found it in a legendary lan${s}d${c}${se}
all rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""".trimIndent(), CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_BLOCK)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class VisualToggleBlockModeActionTest : VimTestCase() {
CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_BLOCK)
}

// Ignore the failture - issue in the platform
// Ignore the failure - issue in the platform
fun `test on empty file`() {
doTest("<C-V>", "", "",
CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_BLOCK)
Expand Down

0 comments on commit 180bbe3

Please sign in to comment.