Skip to content

Commit

Permalink
Add :u[ndo] ex command (#4980)
Browse files Browse the repository at this point in the history
Fixes #3197
  • Loading branch information
adamjhawley committed Jun 28, 2020
1 parent 0c89ce0 commit c77c466
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/actions/commands/actions.ts
Expand Up @@ -2173,7 +2173,7 @@ class CommandGoToOtherEndOfHighlightedText extends BaseCommand {
}

@RegisterAction
class CommandUndo extends BaseCommand {
export class CommandUndo extends BaseCommand {
modes = [Mode.Normal];
keys = ['u'];
runsOnceForEveryCursor() {
Expand Down
28 changes: 28 additions & 0 deletions src/cmd_line/commands/undo.ts
@@ -0,0 +1,28 @@
import * as node from '../node';
import { Logger } from '../../util/logger';
import { VimState } from '../../state/vimState';
import { CommandUndo } from '../../actions/commands/actions';
import { Position } from '../../common/motion/position';

//
// Implements :u[ndo]
// http://vimdoc.sourceforge.net/htmldoc/undo.html
//
export class UndoCommand extends node.CommandBase {
protected _arguments: {};
private readonly _logger = Logger.get('Undo');

constructor(args: {}) {
super();
this._arguments = args;
}

get arguments(): {} {
return this._arguments;
}

async execute(vimState: VimState): Promise<void> {
await new CommandUndo().exec(new Position(0, 0), vimState);
return;
}
}
6 changes: 6 additions & 0 deletions src/cmd_line/subparser.ts
Expand Up @@ -21,6 +21,7 @@ import { parseHistoryCommandArgs } from './subparsers/history';
import { NohlCommand } from './commands/nohl';
import { OnlyCommand } from './commands/only';
import { SmileCommand } from './commands/smile';
import { UndoCommand } from './commands/undo';

// Associates a name and an abbreviation with a command parser
export type CommandParserMapping = {
Expand Down Expand Up @@ -194,6 +195,11 @@ export const commandParsers = {
parser: tabCmd.parseTabPCommandArgs,
},

undo: {
abbrev: 'u',
parser: () => new UndoCommand({}),
},

vnew: {
abbrev: 'vne',
parser: fileCmd.parseEditNewFileInNewVerticalWindowCommandArgs,
Expand Down
23 changes: 23 additions & 0 deletions test/cmd_line/undo.test.ts
@@ -0,0 +1,23 @@
import { getAndUpdateModeHandler } from '../../extension';
import { commandLine } from '../../src/cmd_line/commandLine';
import { ModeHandler } from '../../src/mode/modeHandler';
import { assertEqualLines, cleanUpWorkspace, setupWorkspace } from '../testUtils';
import { VimState } from '../../src/state/vimState';

suite('Undo command', () => {
let modeHandler: ModeHandler;

setup(async () => {
await setupWorkspace();
modeHandler = await getAndUpdateModeHandler();
});

teardown(cleanUpWorkspace);

test('undoes last action after insert mode', async () => {
await modeHandler.handleMultipleKeyEvents(['i', 'a', '<Esc>']);
await modeHandler.handleMultipleKeyEvents(['i', 'b', '<Esc>']);
await commandLine.Run('undo', modeHandler.vimState);
assertEqualLines(['a']);
});
});

0 comments on commit c77c466

Please sign in to comment.