diff --git a/src/cmd_line/main.ts b/src/cmd_line/main.ts index 22db132ea..289e040b3 100644 --- a/src/cmd_line/main.ts +++ b/src/cmd_line/main.ts @@ -23,7 +23,7 @@ export async function showCmdLine(initialText: string, modeHandler : ModeHandler } } -function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> { +export async function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> { if (!command || command.length === 0) { return; } @@ -34,7 +34,7 @@ function runCmdLine(command : string, modeHandler : ModeHandler) : Promise<{}> { return; } - cmd.execute(vscode.window.activeTextEditor, modeHandler); + await cmd.execute(vscode.window.activeTextEditor, modeHandler); } catch (e) { modeHandler.setupStatusBarItem(e.toString()); } diff --git a/src/cmd_line/node.ts b/src/cmd_line/node.ts index f661cf9c0..9fe0db8f7 100644 --- a/src/cmd_line/node.ts +++ b/src/cmd_line/node.ts @@ -87,16 +87,16 @@ export class CommandLine { return ":" + this.range.toString() + " " + this.command.toString(); } - execute(document : vscode.TextEditor, modeHandler : ModeHandler) : void { + async execute(document : vscode.TextEditor, modeHandler : ModeHandler) : Promise { if (!this.command) { this.range.execute(document); return; } if (this.range.isEmpty) { - this.command.execute(modeHandler); + await this.command.execute(modeHandler); } else { - this.command.executeWithRange(modeHandler, this.range); + await this.command.executeWithRange(modeHandler, this.range); } } diff --git a/test/cmd_line/substitute.test.ts b/test/cmd_line/substitute.test.ts new file mode 100644 index 000000000..ee97aeb1a --- /dev/null +++ b/test/cmd_line/substitute.test.ts @@ -0,0 +1,59 @@ +"use strict"; + +import * as assert from 'assert'; +import { ModeHandler } from '../../src/mode/modeHandler'; +import { setupWorkspace, cleanUpWorkspace, assertEqualLines, assertEqual } from './../testUtils'; +import { ModeName } from '../../src/mode/mode'; +import { TextEditor } from '../../src/textEditor'; +import { runCmdLine } from '../../src/cmd_line/main'; + +suite("Basic substitute", () => { + let modeHandler: ModeHandler; + + setup(async () => { + await setupWorkspace(); + modeHandler = new ModeHandler(); + }); + + teardown(cleanUpWorkspace); + + test("Replace single word once", async () => { + await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '']); + await runCmdLine("%s/a/d", modeHandler); + + assertEqualLines([ + "dba" + ]); + }); + + test("Replace with `g` flag", async () => { + await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '']); + await runCmdLine("%s/a/d/g", modeHandler); + + assertEqualLines([ + "dbd" + ]); + }); + + test("Replace multiple lines", async () => { + await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '', 'o', 'a', 'b']); + await runCmdLine("%s/a/d/g", modeHandler); + + assertEqualLines([ + "dbd", + "db" + ]); + }); + + test("Replace across specific lines", async () => { + await modeHandler.handleMultipleKeyEvents(['i', 'a', 'b', 'a', '', 'o', 'a', 'b']); + await runCmdLine("1,1s/a/d/g", modeHandler); + + assertEqualLines([ + "dbd", + "ab" + ]); + }); + + +}); \ No newline at end of file