Skip to content

Commit

Permalink
Support multi-cursor in test harness
Browse files Browse the repository at this point in the history
Now you can just put multiple cursors into the 'start' or 'end' of a test, a few examples are in multicursor.test.ts.
I've also done some general refactor of testSimplifier.ts to make it more straightforward.
ALSO, a slight refactor in VimState (making isMultiCursor a function of cursors.length)
Fixes #4582
  • Loading branch information
J-Fields committed Apr 27, 2020
1 parent c915239 commit 1200e5d
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 180 deletions.
13 changes: 1 addition & 12 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class CommandEsc extends BaseCommand {
}

if (vimState.currentMode === Mode.Normal && vimState.isMultiCursor) {
vimState.isMultiCursor = false;
vimState.collapseCursors();
}

if (vimState.currentMode === Mode.EasyMotionMode) {
Expand All @@ -533,10 +533,6 @@ class CommandEsc extends BaseCommand {

await vimState.setCurrentMode(Mode.Normal);

if (!vimState.isMultiCursor) {
vimState.cursors = [vimState.cursors[0]];
}

return vimState;
}
}
Expand Down Expand Up @@ -2649,7 +2645,6 @@ class CommandInsertNewLineAbove extends BaseCommand {
}
vimState.cursors = vimState.cursors.reverse();
vimState.isFakeMultiCursor = true;
vimState.isMultiCursor = true;
return vimState;
}
}
Expand Down Expand Up @@ -2689,7 +2684,6 @@ class CommandInsertNewLineBefore extends BaseCommand {
}
vimState.cursors = vimState.cursors.reverse();
vimState.isFakeMultiCursor = true;
vimState.isMultiCursor = true;
return vimState;
}
}
Expand Down Expand Up @@ -3594,7 +3588,6 @@ class ActionGoToInsertVisualBlockMode extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vimState.setCurrentMode(Mode.Insert);
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { line, start } of TextEditor.iterateLinesInBlock(vimState)) {
Expand Down Expand Up @@ -3626,7 +3619,6 @@ class ActionChangeInVisualBlockMode extends BaseCommand {
}

await vimState.setCurrentMode(Mode.Insert);
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { start } of TextEditor.iterateLinesInBlock(vimState)) {
Expand Down Expand Up @@ -3656,7 +3648,6 @@ class ActionChangeToEOLInVisualBlockMode extends BaseCommand {
}

await vimState.setCurrentMode(Mode.Insert);
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { end } of TextEditor.iterateLinesInBlock(vimState)) {
Expand All @@ -3681,7 +3672,6 @@ abstract class ActionGoToInsertVisualLineModeCommand extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vimState.setCurrentMode(Mode.Insert);
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

vimState.cursors = [];
Expand Down Expand Up @@ -3778,7 +3768,6 @@ class ActionGoToInsertVisualBlockModeAppend extends BaseCommand {

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vimState.setCurrentMode(Mode.Insert);
vimState.isMultiCursor = true;
vimState.isFakeMultiCursor = true;

for (const { line, end } of TextEditor.iterateLinesInBlock(vimState)) {
Expand Down
3 changes: 1 addition & 2 deletions src/actions/commands/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class CommandEscInsertMode extends BaseCommand {
}

if (vimState.isFakeMultiCursor) {
vimState.cursors = [vimState.cursors[0]];
vimState.isMultiCursor = false;
vimState.collapseCursors();
vimState.isFakeMultiCursor = false;
}
return vimState;
Expand Down
4 changes: 0 additions & 4 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ export class ModeHandler implements vscode.Disposable {
return;
}

if (e.selections.length === 1) {
this.vimState.isMultiCursor = false;
}

if (isStatusBarMode(this.vimState.currentMode)) {
return;
}
Expand Down
17 changes: 10 additions & 7 deletions src/state/vimState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ export class VimState implements vscode.Disposable {
*/
public lastKeyPressedTimestamp = 0;

/**
* Are multiple cursors currently present?
*/
// TODO: why isn't this a function?
public isMultiCursor = false;

/**
* Is the multicursor something like visual block "multicursor", where
* natively in vim there would only be one cursor whose changes were applied
Expand Down Expand Up @@ -174,7 +168,6 @@ export class VimState implements vscode.Disposable {
}

this._cursors = Array.from(map.values());
this.isMultiCursor = this._cursors.length > 1;
}

/**
Expand All @@ -188,6 +181,16 @@ export class VimState implements vscode.Disposable {
this._cursorsInitialState = Object.assign([], value);
}

/**
* Are multiple cursors currently present?
*/
public get isMultiCursor(): boolean {
return this.cursors.length > 1;
}
public collapseCursors(): void {
this.cursors = [this.cursors[0]];
}

public isRecordingMacro: boolean = false;
public isReplayingMacro: boolean = false;

Expand Down
4 changes: 4 additions & 0 deletions src/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export class TextEditor {
return vscode.window.activeTextEditor!.selection;
}

static getSelections(): vscode.Range[] {
return vscode.window.activeTextEditor!.selections;
}

static getText(selection?: vscode.Range): string {
return vscode.window.activeTextEditor!.document.getText(selection);
}
Expand Down
8 changes: 4 additions & 4 deletions test/mode/modeNormal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ suite('Mode Normal', () => {
});

newTest({
title: 'can ctrl-a on an octal ',
title: 'can ctrl-a on an octal',
start: ['07|'],
keysPressed: '<C-a>',
end: ['01|0'],
Expand Down Expand Up @@ -2309,10 +2309,10 @@ suite('Mode Normal', () => {
title: "Can 'D'elete the characters under multiple cursors until the end of the line",
start: [
'test aaa test aaa test aaa test |aaa test',
'test aaa test aaa test aaa test aaa test',
'test aaa test aaa test aaa test |aaa test',
],
keysPressed: '<C-alt+down>D<Esc>',
end: ['test aaa test aaa test aaa test| ', 'test aaa test aaa test aaa test '],
keysPressed: 'D',
end: ['test aaa test aaa test aaa test| ', 'test aaa test aaa test aaa test| '],
});

newTest({
Expand Down
18 changes: 12 additions & 6 deletions test/mode/modeVisual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,21 +1338,24 @@ suite('Mode Visual', () => {
title: 'multiline insert from bottom up selection',
start: ['111', '222', '333', '4|44', '555'],
keysPressed: 'vkkI_',
end: ['111', '2_|22', '_333', '_444', '555'],
end: ['111', '2_|22', '_|333', '_|444', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'multiline insert from top down selection',
start: ['111', '2|22', '333', '444', '555'],
keysPressed: 'vjjI_',
end: ['111', '2_|22', '_333', '_444', '555'],
end: ['111', '2_|22', '_|333', '_|444', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'skips blank lines',
start: ['111', '2|22', ' ', '444', '555'],
keysPressed: 'vjjI_',
end: ['111', '2_|22', ' ', '_444', '555'],
end: ['111', '2_|22', ' ', '_|444', '555'],
endMode: Mode.Insert,
});
});

Expand All @@ -1361,21 +1364,24 @@ suite('Mode Visual', () => {
title: 'multiline append from bottom up selection',
start: ['111', '222', '333', '4|44', '555'],
keysPressed: 'vkkA_',
end: ['111', '222_|', '333_', '44_4', '555'],
end: ['111', '222_|', '333_|', '44_|4', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'multiline append from top down selection',
start: ['111', '2|22', '333', '444', '555'],
keysPressed: 'vjjA_',
end: ['111', '222_|', '333_', '44_4', '555'],
end: ['111', '222_|', '333_|', '44_|4', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'skips blank lines',
start: ['111', '2|22', ' ', '444', '555'],
keysPressed: 'vjjA_',
end: ['111', '222_|', ' ', '44_4', '555'],
end: ['111', '222_|', ' ', '44_|4', '555'],
endMode: Mode.Insert,
});
});

Expand Down
21 changes: 11 additions & 10 deletions test/mode/modeVisualBlock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,63 +33,64 @@ suite('Mode Visual Block', () => {
title: 'Can handle A forward select',
start: ['|test', 'test'],
keysPressed: 'l<C-v>ljA123',
end: ['tes123|t', 'tes123t'],
end: ['tes123|t', 'tes123|t'],
});

newTest({
title: 'Can handle A backwards select',
start: ['tes|t', 'test'],
keysPressed: 'h<C-v>hjA123',
end: ['tes123|t', 'tes123t'],
end: ['tes123|t', 'tes123|t'],
});

newTest({
title: 'Can handle I forward select',
start: ['|test', 'test'],
keysPressed: 'l<C-v>ljI123',
end: ['t123|est', 't123est'],
end: ['t123|est', 't123|est'],
});

newTest({
title: 'Can handle I backwards select',
start: ['tes|t', 'test'],
keysPressed: 'h<C-v>hjI123',
end: ['t123|est', 't123est'],
end: ['t123|est', 't123|est'],
});

newTest({
title: 'Can handle I with empty lines on first character (inserts on empty line)',
start: ['|test', '', 'test'],
keysPressed: '<C-v>lljjI123',
end: ['123|test', '123', '123test'],
end: ['123|test', '123|', '123|test'],
});

newTest({
title: 'Can handle I with empty lines on non-first character (does not insert on empty line)',
start: ['t|est', '', 'test'],
keysPressed: '<C-v>lljjI123',
end: ['t123|est', '', 't123est'],
end: ['t123|est', '', 't123|est'],
});

newTest({
title: 'Can handle c forward select',
start: ['|test', 'test'],
keysPressed: 'l<C-v>ljc123',
end: ['t123|t', 't123t'],
end: ['t123|t', 't123|t'],
});

newTest({
title: 'Can handle c backwards select',
start: ['tes|t', 'test'],
keysPressed: 'h<C-v>hjc123',
end: ['t123|t', 't123t'],
end: ['t123|t', 't123|t'],
});

newTest({
title: 'Can handle C',
start: ['tes|t', 'test'],
keysPressed: 'h<C-v>hjC123',
end: ['t123|', 't123'],
end: ['t123|', 't123|'],
endMode: Mode.Insert,
});

newTest({
Expand Down Expand Up @@ -144,7 +145,7 @@ suite('Mode Visual Block', () => {
title: 'Properly add to end of lines j then $',
start: ['|Dog', 'Angry', 'Dog', 'Angry', 'Dog'],
keysPressed: '<C-v>4j$Aaa',
end: ['Dogaa|', 'Angryaa', 'Dogaa', 'Angryaa', 'Dogaa'],
end: ['Dogaa|', 'Angryaa|', 'Dogaa|', 'Angryaa|', 'Dogaa|'],
});

newTest({
Expand Down
18 changes: 12 additions & 6 deletions test/mode/modeVisualLine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,21 +441,24 @@ suite('Mode Visual Line', () => {
title: 'multiline insert from bottom up selection',
start: ['111', '222', '333', '4|44', '555'],
keysPressed: 'VkkI_',
end: ['111', '_|222', '_333', '_444', '555'],
end: ['111', '_|222', '_|333', '_|444', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'multiline insert from top down selection',
start: ['111', '2|22', '333', '444', '555'],
keysPressed: 'VjjI_',
end: ['111', '_|222', '_333', '_444', '555'],
end: ['111', '_|222', '_|333', '_|444', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'skips blank lines',
start: ['111', '2|22', ' ', '444', '555'],
keysPressed: 'VjjI_',
end: ['111', '_|222', ' ', '_444', '555'],
end: ['111', '_|222', ' ', '_|444', '555'],
endMode: Mode.Insert,
});
});

Expand All @@ -464,21 +467,24 @@ suite('Mode Visual Line', () => {
title: 'multiline append from bottom up selection',
start: ['111', '222', '333', '4|44', '555'],
keysPressed: 'VkkA_',
end: ['111', '222_|', '333_', '444_', '555'],
end: ['111', '222_|', '333_|', '444_|', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'multiline append from top down selection',
start: ['111', '2|22', '333', '444', '555'],
keysPressed: 'VjjA_',
end: ['111', '222_|', '333_', '444_', '555'],
end: ['111', '222_|', '333_|', '444_|', '555'],
endMode: Mode.Insert,
});

newTest({
title: 'skips blank lines',
start: ['111', '2|22', ' ', '444', '555'],
keysPressed: 'VjjA_',
end: ['111', '222_|', ' ', '444_', '555'],
end: ['111', '222_|', ' ', '444_|', '555'],
endMode: Mode.Insert,
});

newTest({
Expand Down
Loading

0 comments on commit 1200e5d

Please sign in to comment.