1
0
mirror of https://github.com/chylex/IntelliJ-IdeaVim.git synced 2025-08-15 19:31:47 +02:00

Multicaret shift

This commit is contained in:
Vasily Alferov
2018-05-22 22:51:49 +03:00
parent 258467467b
commit ebf081e273
10 changed files with 158 additions and 32 deletions

@@ -19,10 +19,12 @@
package com.maddyhome.idea.vim.action.change.shift;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.Argument;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.handler.ChangeEditorActionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -35,8 +37,14 @@ public class ShiftLeftLinesAction extends EditorAction {
}
private static class Handler extends ChangeEditorActionHandler {
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, int count, int rawCount, @Nullable Argument argument) {
VimPlugin.getChange().indentLines(editor, context, count, -1);
public Handler() {
super(true, CaretOrder.DECREASING_OFFSET);
}
@Override
public boolean execute(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count,
int rawCount, @Nullable Argument argument) {
VimPlugin.getChange().indentLines(editor, caret, context, count, -1);
return true;
}

@@ -19,10 +19,12 @@
package com.maddyhome.idea.vim.action.change.shift;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.Argument;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.handler.ChangeEditorActionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -35,9 +37,15 @@ public class ShiftLeftMotionAction extends EditorAction {
}
private static class Handler extends ChangeEditorActionHandler {
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, int count, int rawCount, @Nullable Argument argument) {
public Handler() {
super(true, CaretOrder.DECREASING_OFFSET);
}
@Override
public boolean execute(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count,
int rawCount, @Nullable Argument argument) {
if (argument != null) {
VimPlugin.getChange().indentMotion(editor, context, count, rawCount, argument, -1);
VimPlugin.getChange().indentMotion(editor, caret, context, count, rawCount, argument, -1);
return true;
}
else {

@@ -19,12 +19,14 @@
package com.maddyhome.idea.vim.action.change.shift;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.action.VimCommandAction;
import com.maddyhome.idea.vim.command.Command;
import com.maddyhome.idea.vim.command.MappingMode;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.handler.VisualOperatorActionHandler;
import org.jetbrains.annotations.NotNull;
@@ -37,10 +39,11 @@ import java.util.Set;
*/
public class ShiftLeftVisualAction extends VimCommandAction {
public ShiftLeftVisualAction() {
super(new VisualOperatorActionHandler() {
protected boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull Command cmd,
@NotNull TextRange range) {
VimPlugin.getChange().indentRange(editor, context, range, cmd.getCount(), -1);
super(new VisualOperatorActionHandler(true, CaretOrder.DECREASING_OFFSET) {
@Override
protected boolean execute(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context,
@NotNull Command cmd, @NotNull TextRange range) {
VimPlugin.getChange().indentRange(editor, caret, context, range, cmd.getCount(), -1);
return true;
}
});

@@ -19,10 +19,12 @@
package com.maddyhome.idea.vim.action.change.shift;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.Argument;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.handler.ChangeEditorActionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -35,8 +37,14 @@ public class ShiftRightLinesAction extends EditorAction {
}
private static class Handler extends ChangeEditorActionHandler {
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, int count, int rawCount, @Nullable Argument argument) {
VimPlugin.getChange().indentLines(editor, context, count, 1);
public Handler() {
super(true, CaretOrder.DECREASING_OFFSET);
}
@Override
public boolean execute(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count,
int rawCount, @Nullable Argument argument) {
VimPlugin.getChange().indentLines(editor, caret, context, count, 1);
return true;
}

@@ -19,10 +19,12 @@
package com.maddyhome.idea.vim.action.change.shift;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorAction;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.command.Argument;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.handler.ChangeEditorActionHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -35,9 +37,15 @@ public class ShiftRightMotionAction extends EditorAction {
}
private static class Handler extends ChangeEditorActionHandler {
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, int count, int rawCount, @Nullable Argument argument) {
public Handler() {
super(true, CaretOrder.DECREASING_OFFSET);
}
@Override
public boolean execute(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count,
int rawCount, @Nullable Argument argument) {
if (argument != null) {
VimPlugin.getChange().indentMotion(editor, context, count, rawCount, argument, 1);
VimPlugin.getChange().indentMotion(editor, caret, context, count, rawCount, argument, 1);
return true;
}
else {

@@ -19,12 +19,14 @@
package com.maddyhome.idea.vim.action.change.shift;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.action.VimCommandAction;
import com.maddyhome.idea.vim.command.Command;
import com.maddyhome.idea.vim.command.MappingMode;
import com.maddyhome.idea.vim.common.TextRange;
import com.maddyhome.idea.vim.handler.CaretOrder;
import com.maddyhome.idea.vim.handler.VisualOperatorActionHandler;
import org.jetbrains.annotations.NotNull;
@@ -37,10 +39,10 @@ import java.util.Set;
*/
public class ShiftRightVisualAction extends VimCommandAction {
public ShiftRightVisualAction() {
super(new VisualOperatorActionHandler() {
protected boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull Command cmd,
@NotNull TextRange range) {
VimPlugin.getChange().indentRange(editor, context, range, cmd.getCount(), 1);
super(new VisualOperatorActionHandler(true, CaretOrder.DECREASING_OFFSET) {
protected boolean execute(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context,
@NotNull Command cmd, @NotNull TextRange range) {
VimPlugin.getChange().indentRange(editor, caret, context, range, cmd.getCount(), 1);
return true;
}
});

@@ -37,7 +37,8 @@ public class ShiftLeftHandler extends CommandHandler {
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) {
TextRange range = cmd.getTextRange(editor, context, true);
VimPlugin.getChange().indentRange(editor, context, range, cmd.getCommand().length(), -1);
VimPlugin.getChange()
.indentRange(editor, editor.getCaretModel().getPrimaryCaret(), context, range, cmd.getCommand().length(), -1);
return true;
}

@@ -37,7 +37,8 @@ public class ShiftRightHandler extends CommandHandler {
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) {
TextRange range = cmd.getTextRange(editor, context, true);
VimPlugin.getChange().indentRange(editor, context, range, cmd.getCommand().length(), 1);
VimPlugin.getChange()
.indentRange(editor, editor.getCaretModel().getPrimaryCaret(), context, range, cmd.getCommand().length(), 1);
return true;
}

@@ -220,7 +220,7 @@ public class ChangeGroup {
*/
public void insertNewLineBelow(@NotNull final Editor editor, @NotNull final DataContext context) {
for (Caret caret : editor.getCaretModel().getAllCarets()) {
MotionGroup.moveCaret(editor,caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret));
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret));
}
initInsert(editor, context, CommandState.Mode.INSERT);
runEnterAction(editor, context);
@@ -1516,22 +1516,23 @@ public class ChangeGroup {
KeyHandler.executeAction("ReformatCode", context);
}
public void indentLines(@NotNull Editor editor, @NotNull DataContext context, int lines, int dir) {
int start = editor.getCaretModel().getOffset();
int end = VimPlugin.getMotion().moveCaretToLineEndOffset(editor, lines - 1, false);
indentRange(editor, context, new TextRange(start, end), 1, dir);
public void indentLines(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int lines,
int dir) {
int start = caret.getOffset();
int end = VimPlugin.getMotion().moveCaretToLineEndOffset(editor, caret, lines - 1, false);
indentRange(editor, caret, context, new TextRange(start, end), 1, dir);
}
public void indentMotion(@NotNull Editor editor, @NotNull DataContext context, int count, int rawCount,
@NotNull Argument argument, int dir) {
final TextRange range = MotionGroup.getMotionRange(editor, context, count, rawCount, argument, false);
public void indentMotion(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count,
int rawCount, @NotNull Argument argument, int dir) {
final TextRange range = MotionGroup.getMotionRange(editor, caret, context, count, rawCount, argument, false);
if (range != null) {
indentRange(editor, context, range, 1, dir);
indentRange(editor, caret, context, range, 1, dir);
}
}
public void indentRange(@NotNull Editor editor, @NotNull DataContext context, @NotNull TextRange range, int count,
int dir) {
public void indentRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context,
@NotNull TextRange range, int count, int dir) {
if (logger.isDebugEnabled()) {
logger.debug("count=" + count);
}
@@ -1640,14 +1641,14 @@ public class ChangeGroup {
if (!CommandState.inInsertMode(editor)) {
if (!range.isMultiple()) {
MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, sline));
MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, sline));
}
else {
MotionGroup.moveCaret(editor, range.getStartOffset());
MotionGroup.moveCaret(editor, caret, range.getStartOffset());
}
}
EditorData.setLastColumn(editor, editor.getCaretModel().getVisualPosition().column);
CaretData.setLastColumn(editor, caret, caret.getVisualPosition().column);
}
/**

@@ -1377,4 +1377,90 @@ public class MultipleCaretsTest extends VimTestCase {
"one <caret>two <caret>three <caret>four\n");
myFixture.checkResult("o<caret>ne t<caret>wo thr<caret>ee four\n");
}
// com.maddyhome.idea.vim.action.change.shift
public void testShiftLeftLinesAction() {
typeTextInFile(parseKeys("2<<"),
" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
" <caret>abcde\n" +
" abcde\n");
myFixture.checkResult(" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
"<caret>abcde\n" +
"abcde\n");
}
public void testShiftLeftMotionAction() {
typeTextInFile(parseKeys("<j"),
" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
" <caret>abcde\n" +
" abcde\n");
myFixture.checkResult(" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
"<caret>abcde\n" +
"abcde\n");
}
public void testShiftLeftVisualAction() {
typeTextInFile(parseKeys("Vj<"),
" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
" <caret>abcde\n" +
" abcde\n");
myFixture.checkResult(" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
"<caret>abcde\n" +
"abcde\n");
}
public void testShiftRightLinesAction() {
typeTextInFile(parseKeys("2>>"),
" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
"<caret>abcde\n" +
"abcde\n");
myFixture.checkResult(" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
" <caret>abcde\n" +
" abcde\n");
}
public void testShiftRightMotionAction() {
typeTextInFile(parseKeys(">j"),
" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
"<caret>abcde\n" +
"abcde\n");
myFixture.checkResult(" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
" <caret>abcde\n" +
" abcde\n");
}
public void testShiftRightVisualAction() {
typeTextInFile(parseKeys("Vj>"),
" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
"<caret>abcde\n" +
"abcde\n");
myFixture.checkResult(" <caret>abcde\n" +
" abcde\n" +
" abcde\n" +
" <caret>abcde\n" +
" abcde\n");
}
}