Skip to content

Commit

Permalink
UpperCase support
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Jun 22, 2016
1 parent 4454e8d commit 32174af
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VimState> { 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 {
Expand Down Expand Up @@ -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<VimState> {
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
Expand Down Expand Up @@ -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<VimState> {
[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"];
Expand Down
36 changes: 36 additions & 0 deletions test/mode/modeVisualLine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
'<esc>', '^',
'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([
Expand Down Expand Up @@ -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([
'<esc>', '^',
'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([
'<esc>', '^',
'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([
Expand Down

0 comments on commit 32174af

Please sign in to comment.