1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-03-07 12:32:52 +01:00

Update down motion

This commit is contained in:
Alex Plate 2019-04-08 16:00:35 +03:00
parent 68efd8f3d0
commit 740ef8a2d7
No known key found for this signature in database
GPG Key ID: 0B97153C8FFEC09F
6 changed files with 353 additions and 21 deletions
src/com/maddyhome/idea/vim
test/org/jetbrains/plugins/ideavim

View File

@ -23,13 +23,21 @@ private object SelectToggleVisualModeHandler : EditorActionHandlerBase() {
commandState.popState()
if (mode == CommandState.Mode.VISUAL) {
commandState.pushState(CommandState.Mode.SELECT, subMode, MappingMode.SELECT)
editor.caretModel.runForEachCaret {
it.moveToOffset(it.offset + VimPlugin.getVisualMotion().selectionAdj)
if (subMode != CommandState.SubMode.VISUAL_LINE) {
editor.caretModel.runForEachCaret {
if (it.offset + VimPlugin.getVisualMotion().selectionAdj == it.selectionEnd) {
it.moveToOffset(it.offset + VimPlugin.getVisualMotion().selectionAdj)
}
}
}
} else {
commandState.pushState(CommandState.Mode.VISUAL, subMode, MappingMode.VISUAL)
editor.caretModel.runForEachCaret {
it.moveToOffset(it.selectionEnd - VimPlugin.getVisualMotion().selectionAdj)
if (subMode != CommandState.SubMode.VISUAL_LINE) {
editor.caretModel.runForEachCaret {
if (it.offset == it.selectionEnd && it.visualLineStart <= it.offset - VimPlugin.getVisualMotion().selectionAdj) {
it.moveToOffset(it.offset - VimPlugin.getVisualMotion().selectionAdj)
}
}
}
}
ChangeGroup.resetCursor(editor, mode == CommandState.Mode.VISUAL)

View File

@ -244,7 +244,8 @@ public class MotionGroup {
col = newColumn;
}
newColumn = EditorHelper.normalizeVisualColumn(editor, newline, newColumn, CommandState.inInsertMode(editor));
newColumn = EditorHelper.normalizeVisualColumn(editor, newline, newColumn, CommandState.inInsertMode(editor) ||
CommandState.inSelectMode(editor));
if (newline != caretVisualLine || newColumn != oldColumn) {
int offset = EditorHelper.visualPositionToOffset(editor, new VisualPosition(newline, newColumn));
@ -1270,8 +1271,11 @@ public class MotionGroup {
else {
int col = CaretDataKt.getVimLastColumn(caret);
int line = EditorHelper.normalizeVisualLine(editor, pos.line + count);
VisualPosition newPos = new VisualPosition(line, EditorHelper
.normalizeVisualColumn(editor, line, col, CommandState.inInsertMode(editor)));
VisualPosition newPos = new VisualPosition(line, EditorHelper.normalizeVisualColumn(editor, line, col,
CommandState
.inInsertMode(editor) ||
CommandState
.inSelectMode(editor)));
return EditorHelper.visualPositionToOffset(editor, newPos);
}

View File

@ -135,7 +135,10 @@ val Caret.vimLeadSelectionOffset: Int
return if (CommandState.getInstance(editor).subMode == CommandState.SubMode.VISUAL_LINE) {
val selectionStartLine = editor.offsetToLogicalPosition(selectionStart).line
val caretLine = editor.offsetToLogicalPosition(this.offset).line
if (caretLine == selectionStartLine) selectionEnd else selectionStart
if (caretLine == selectionStartLine) {
val column = editor.offsetToLogicalPosition(selectionEnd).column
if (column == 0) (selectionEnd - 1).coerceAtLeast(0) else selectionEnd
} else selectionStart
} else if (CommandState.getInstance(editor).subMode == CommandState.SubMode.VISUAL_BLOCK) {
val selections = editor.caretModel.allCarets.map { it.selectionStart to it.selectionEnd }.sortedBy { it.first }
val pCaret = editor.caretModel.primaryCaret
@ -207,7 +210,7 @@ private fun setVisualSelection(selectionStart: Int, selectionEnd: Int, caret: Ca
if (lastColumn >= MotionGroup.LAST_COLUMN) {
aCaret.vimSetSelectionSilently(aCaret.selectionStart, lineEndOffset)
}
if (mode != CommandState.Mode.SELECT && !EditorHelper.isLineEmpty(editor, line, false)) {
if (mode != CommandState.Mode.SELECT && !EditorHelper.isLineEmpty(editor, line, false) && aCaret.offset == aCaret.selectionEnd) {
aCaret.moveToOffset(aCaret.selectionEnd - 1)
}
}

View File

@ -788,8 +788,7 @@ public class MultipleCaretsTest extends VimTestCase {
typeTextInFile(parseKeys("<C-V>", "2l", "j", "O"),
"a<caret>abcc\n" +
"ddeff\n");
myFixture.checkResult("a<selection><caret>abc</selection>c\n" +
"d<selection>de<caret>f</selection>f\n");
myFixture.checkResult("a<selection><caret>abc</selection>c\n" + "d<selection><caret>def</selection>f\n");
}
public void testVisualBlockMovementAfterSwapEndsBlockAction() {
@ -799,8 +798,7 @@ public class MultipleCaretsTest extends VimTestCase {
"gghii\n" +
"jjkll\n");
myFixture.checkResult("aabcc\n" +
"<selection><caret>ddef</selection>f\n" +
"<selection>ggh<caret>i</selection>i\n" +
"<selection><caret>ddef</selection>f\n" + "<selection><caret>gghi</selection>i\n" +
"jjkll\n");
typeText(parseKeys("j"));
myFixture.checkResult("aabcc\n" +

View File

@ -1,7 +1,10 @@
@file:Suppress("RemoveCurlyBracesFromTemplate")
package org.jetbrains.plugins.ideavim.action.motion.select
import com.maddyhome.idea.vim.command.CommandState
import com.maddyhome.idea.vim.helper.StringHelper.parseKeys
import com.maddyhome.idea.vim.helper.VimBehaviourDiffers
import org.jetbrains.plugins.ideavim.VimTestCase
class SelectToggleVisualModeHandlerTest : VimTestCase() {
@ -25,6 +28,86 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to select mode characterwise left motion`() {
doTest(parseKeys("vb", "<C-G>"),
"""
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 $s${c}found i${se}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.SELECT,
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to select mode characterwise empty line`() {
doTest(parseKeys("v", "<C-G>"),
"""
A Discovery
$c
I found 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
$s$c$se
I found 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(),
CommandState.Mode.SELECT,
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to select mode characterwise to line end`() {
doTest(parseKeys("vel", "<C-G>"),
"""
A Discovery
I found it in a legendary ${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 legendary ${s}land${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.SELECT,
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to select mode characterwise one letter`() {
doTest(parseKeys("v", "<C-G>"),
"""
A Discovery
I found it in a legendary ${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 legendary ${s}l${c}${se}and
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.SELECT,
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to select mode characterwise multicaret`() {
doTest(parseKeys("ve", "<C-G>"),
"""
@ -65,6 +148,142 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to visual mode characterwise left motion`() {
doTest(parseKeys("gh", "<S-Left>".repeat(5), "<C-G>"),
"""
A Discovery
I foun${c}d 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 ${s}${c}foun${se}d 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(),
CommandState.Mode.VISUAL,
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to visual mode characterwise empty line`() {
doTest(parseKeys("gh", "<C-G>"),
"""
A Discovery
${c}
I found 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
${s}${c}${se}
I found 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(),
CommandState.Mode.VISUAL,
CommandState.SubMode.VISUAL_CHARACTER)
}
fun `test switch to visual mode characterwise line end`() {
doTest(parseKeys("gh", "<S-Right>".repeat(5), "<C-G>"),
"""
A Discovery
I found it in a legendary ${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 legendary ${s}lan${c}d${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 switch to visual mode characterwise one letter`() {
doTest(parseKeys("gh", "<C-G>"),
"""
A Discovery
I found it in a legendary ${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 legendary ${s}${c}l${se}and
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)
}
@VimBehaviourDiffers(originalVimAfter = """
A Discovery
${s}${c}I${se} found 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.
""")
fun `test switch to visual mode characterwise line start`() {
doTest(parseKeys("gh", "<S-Left>", "<C-G>"),
"""
A Discovery
${c}I found 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
${s}${c}${se}I found 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(),
CommandState.Mode.VISUAL,
CommandState.SubMode.VISUAL_CHARACTER)
}
@VimBehaviourDiffers(originalVimAfter = """
A Discovery
${s}I found it in a legendary land
${c}a${se}ll rocks and lavender and tufted grass,
where it was settled on some sodden sand
hard by the torrent of a mountain pass.
""")
fun `test switch to visual mode characterwise end on line start`() {
doTest(parseKeys("gh", "<S-Left>", "<S-Down>", "<C-G>"),
"""
A Discovery
${c}I found 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
${s}I found it in a legendary land
${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 switch to visual mode characterwise multicaret`() {
doTest(parseKeys("gh", "<S-Right>".repeat(4), "<C-G>"),
"""
@ -97,7 +316,47 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
"""
A Discovery
${s}I found$c it in a legendary land$se
${s}I foun${c}d it in a legendary land$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.SELECT,
CommandState.SubMode.VISUAL_LINE)
}
fun `test switch to select mode linewise up motion`() {
doTest(parseKeys("V", "k", "<C-G>"),
"""
A Discovery
I found it in a legendary land
all ${c}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
${s}I fo${c}und it in a legendary land
all rocks and lavender and tufted grass,${se}
where it was settled on some sodden sand
hard by the torrent of a mountain pass.""".trimIndent(),
CommandState.Mode.SELECT,
CommandState.SubMode.VISUAL_LINE)
}
fun `test switch to select mode linewise empty line`() {
doTest(parseKeys("V", "<C-G>"),
"""
A Discovery
${c}
I found 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
${s}${c}${se}
I found 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(),
@ -117,9 +376,9 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
"""
A Discovery
${s}I found$c it in a legendary land$se
${s}I foun${c}d it in a legendary land$se
all rocks and lavender and tufted grass,
${s}where it was settled$c on some sodden sand$se
${s}where it was settle${c}d on some sodden sand$se
hard by the torrent of a mountain pass.""".trimIndent(),
CommandState.Mode.SELECT,
CommandState.SubMode.VISUAL_LINE)
@ -137,7 +396,27 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
"""
A Discovery
${s}I found it in a legendary lan${c}d$se
${s}I ${c}found it in a legendary land$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_LINE)
}
fun `test switch to visual mode linewise empty line`() {
doTest(parseKeys("gH", "<C-G>"),
"""
A Discovery
${c}
I found 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
${s}${c}${se}
I found 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(),
@ -157,9 +436,9 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
"""
A Discovery
${s}I found it in a legendary lan${c}d$se
${s}I ${c}found it in a legendary land$se
all rocks and lavender and tufted grass,
${s}where it was settled on some sodden san${c}d$se
${s}where it was ${c}settled on some sodden sand$se
hard by the torrent of a mountain pass.""".trimIndent(),
CommandState.Mode.VISUAL,
CommandState.SubMode.VISUAL_LINE)
@ -185,6 +464,26 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
CommandState.SubMode.VISUAL_BLOCK)
}
fun `test switch to select mode blockwise left motion`() {
doTest(parseKeys("<C-V>bjj", "<C-G>"),
"""
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 ${s}${c}found ${se}it in a legendary land
al${s}${c}l rock${se}s and lavender and tufted grass,
wh${s}${c}ere it${se} was settled on some sodden sand
hard by the torrent of a mountain pass.""".trimIndent(),
CommandState.Mode.SELECT,
CommandState.SubMode.VISUAL_BLOCK)
}
fun `test switch to visual mode blockwise`() {
doTest(parseKeys("g<C-H>", "<S-Right>".repeat(4), "<S-Down>".repeat(2), "<C-G>"),
"""
@ -204,4 +503,24 @@ class SelectToggleVisualModeHandlerTest : VimTestCase() {
CommandState.Mode.VISUAL,
CommandState.SubMode.VISUAL_BLOCK)
}
fun `test switch to visual mode blockwise to left`() {
doTest(parseKeys("g<C-H>", "<S-Left>".repeat(4), "<S-Down>".repeat(2), "<C-G>"),
"""
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 fou$s${c}nd ${se}it in a legendary land
all r$s${c}ock${se}s and lavender and tufted grass,
where$s${c} it${se} was settled on some sodden sand
hard by the torrent of a mountain pass.""".trimIndent(),
CommandState.Mode.VISUAL,
CommandState.SubMode.VISUAL_BLOCK)
}
}

View File

@ -508,8 +508,8 @@ class VisualMotionGroup_ControlNonVimSelection_Test : VimTestCase() {
A Discovery
I ${s}found it in a legendary land$c$se
al${s}l rocks and lavender and tufted gras$c${se}s,
wh${s}ere it was settled on some sodden sa$c${se}nd
al${s}l rocks and lavender and tufted grass$c${se},
wh${s}ere it was settled on some sodden san$c${se}d
ha${s}rd by the torrent of a mountain pass.$c$se
""".trimIndent())
assertMode(CommandState.Mode.SELECT)