From 32174af29b2f7b74a88a500cd42c5249f0b2903a Mon Sep 17 00:00:00 2001 From: rebornix Date: Wed, 22 Jun 2016 14:04:36 +0800 Subject: [PATCH 1/2] UpperCase support --- src/actions/actions.ts | 36 +++++++++++++++++++++++--------- test/mode/modeVisualLine.test.ts | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/actions/actions.ts b/src/actions/actions.ts index 4eb076df1a8..e5dbc9687d2 100644 --- a/src/actions/actions.ts +++ b/src/actions/actions.ts @@ -229,6 +229,17 @@ export class BaseOperator extends BaseAction { * Run this operator on a range, returning the new location of the cursor. */ run(vimState: VimState, start: Position, stop: Position): Promise { return; } + + public transformRange(start: Position, end: Position): [Position, Position] { + if (start.compareTo(end) <= 0) { + end = new Position(end.line, end.character + 1); + } else { + [start, end] = [end, start]; + end = new Position(end.line, end.character + 1); + } + + return [start, end]; + } } export enum KeypressState { @@ -656,16 +667,7 @@ export class DeleteOperator extends BaseOperator { * Deletes from the position of start to 1 past the position of end. */ public async run(vimState: VimState, start: Position, end: Position): Promise { - if (start.compareTo(end) <= 0) { - end = new Position(end.line, end.character + 1); - } else { - const tmp = start; - start = end; - end = tmp; - - end = new Position(end.line, end.character + 1); - } - + [start, end] = this.transformRange(start, end); const isOnLastLine = end.line === TextEditor.getLineCount() - 1; // Vim does this weird thing where it allows you to select and delete @@ -770,6 +772,20 @@ export class DeleteOperatorXVisual extends BaseOperator { } } +@RegisterAction +export class UpperCaseOperator extends BaseOperator { + public keys = ["U"]; + public modes = [ModeName.Visual, ModeName.VisualLine]; + + public async run(vimState: VimState, start: Position, end: Position): Promise { + [start, end] = this.transformRange(start, end); + let text = vscode.window.activeTextEditor.document.getText(new vscode.Range(start, end)); + await TextEditor.replace(new vscode.Range(start, end), text.toUpperCase()); + vimState.currentMode = ModeName.Normal; + return vimState; + } +} + @RegisterAction export class ChangeOperator extends BaseOperator { public keys = ["c"]; diff --git a/test/mode/modeVisualLine.test.ts b/test/mode/modeVisualLine.test.ts index b6ab94483eb..2d1677bfb1d 100644 --- a/test/mode/modeVisualLine.test.ts +++ b/test/mode/modeVisualLine.test.ts @@ -64,6 +64,18 @@ suite("Mode Visual", () => { assertEqual(modeHandler.currentMode.name, ModeName.Normal); }); + test("Can handle U", async () => { + await modeHandler.handleMultipleKeyEvents("ione two three".split("")); + await modeHandler.handleMultipleKeyEvents([ + '', '^', + 'v', 'U' + ]); + + assertEqualLines(["One two three"]); + + assertEqual(modeHandler.currentMode.name, ModeName.Normal); + }) + test("Can handle x across a selection", async () => { await modeHandler.handleMultipleKeyEvents("ione two three".split("")); await modeHandler.handleMultipleKeyEvents([ @@ -110,6 +122,30 @@ suite("Mode Visual", () => { assertEqualLines(["our"]); }); + test("Can handle U across a selection", async () => { + await modeHandler.handleMultipleKeyEvents("ione two three".split("")); + await modeHandler.handleMultipleKeyEvents([ + '', '^', + 'v', 'l', 'l', 'l', 'l', 'U' + ]); + + assertEqualLines(["ONE Two three"]); + + assertEqual(modeHandler.currentMode.name, ModeName.Normal); + }); + + test("Can handle U across a selection in reverse order", async () => { + await modeHandler.handleMultipleKeyEvents("ione two three".split("")); + await modeHandler.handleMultipleKeyEvents([ + '', '^', + 'w', 'v', 'h', 'h', 'U' + ]); + + assertEqualLines(["onE Two three"]); + + assertEqual(modeHandler.currentMode.name, ModeName.Normal); + }); + test("handles case where we go from selecting on right side to selecting on left side", async () => { await modeHandler.handleMultipleKeyEvents("ione two three".split("")); await modeHandler.handleMultipleKeyEvents([ From 3c729ee90cb494161cd23150315d3cbe513b94c6 Mon Sep 17 00:00:00 2001 From: johnfn Date: Thu, 23 Jun 2016 23:14:24 -0700 Subject: [PATCH 2/2] Clean up. --- src/actions/actions.ts | 23 ++++++++--------------- src/mode/modeHandler.ts | 8 ++++---- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/actions/actions.ts b/src/actions/actions.ts index e5dbc9687d2..099fef50214 100644 --- a/src/actions/actions.ts +++ b/src/actions/actions.ts @@ -229,17 +229,6 @@ export class BaseOperator extends BaseAction { * Run this operator on a range, returning the new location of the cursor. */ run(vimState: VimState, start: Position, stop: Position): Promise { return; } - - public transformRange(start: Position, end: Position): [Position, Position] { - if (start.compareTo(end) <= 0) { - end = new Position(end.line, end.character + 1); - } else { - [start, end] = [end, start]; - end = new Position(end.line, end.character + 1); - } - - return [start, end]; - } } export enum KeypressState { @@ -667,7 +656,8 @@ export class DeleteOperator extends BaseOperator { * Deletes from the position of start to 1 past the position of end. */ public async run(vimState: VimState, start: Position, end: Position): Promise { - [start, end] = this.transformRange(start, end); + end = new Position(end.line, end.character + 1); + const isOnLastLine = end.line === TextEditor.getLineCount() - 1; // Vim does this weird thing where it allows you to select and delete @@ -776,12 +766,15 @@ export class DeleteOperatorXVisual extends BaseOperator { export class UpperCaseOperator extends BaseOperator { public keys = ["U"]; public modes = [ModeName.Visual, ModeName.VisualLine]; - + public async run(vimState: VimState, start: Position, end: Position): Promise { - [start, end] = this.transformRange(start, end); + end = new Position(end.line, end.character + 1); + let text = vscode.window.activeTextEditor.document.getText(new vscode.Range(start, end)); - await TextEditor.replace(new vscode.Range(start, end), text.toUpperCase()); + + await TextEditor.replace(new vscode.Range(start, end), text.toUpperCase()); vimState.currentMode = ModeName.Normal; + return vimState; } } diff --git a/src/mode/modeHandler.ts b/src/mode/modeHandler.ts index 22ef155a3cd..efe8dbf104c 100644 --- a/src/mode/modeHandler.ts +++ b/src/mode/modeHandler.ts @@ -559,11 +559,11 @@ export class ModeHandler implements vscode.Disposable { } } - if (this.currentModeName === ModeName.VisualLine) { - if (Position.EarlierOf(start, stop) === stop) { - [start, stop] = [stop, start]; - } + if (start.compareTo(stop) > 0) { + [start, stop] = [stop, start]; + } + if (this.currentModeName === ModeName.VisualLine) { start = start.getLineBegin(); stop = stop.getLineEnd();