Skip to content

Commit

Permalink
make = an operator and align with Code's command API
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Oct 19, 2016
1 parent a7caf7e commit 4bb8ab3
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1504,27 +1504,6 @@ export class CommandSearchBackwards extends BaseCommand {
}
}

@RegisterAction
class CommandFormatCode extends BaseCommand {
modes = [ModeName.Visual, ModeName.VisualLine];
keys = ["="];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand("editor.action.format");
let line = vimState.cursorStartPosition.line;

if (vimState.cursorStartPosition.isAfter(vimState.cursorPosition)) {
line = vimState.cursorPosition.line;
}

let newCursorPosition = new Position(line, 0);
vimState.cursorPosition = newCursorPosition;
vimState.cursorStartPosition = newCursorPosition;
vimState.currentMode = ModeName.Normal;
return vimState;
}
}

@RegisterAction
export class DeleteOperator extends BaseOperator {
public keys = ["d"];
Expand Down Expand Up @@ -1716,6 +1695,27 @@ export class ChangeOperatorSVisual extends BaseOperator {
}
}

@RegisterAction
export class FormatOperator extends BaseOperator {
public keys = ["="];
public modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];

public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
vscode.window.activeTextEditor.selection = new vscode.Selection(start, end);
await vscode.commands.executeCommand("editor.action.formatSelection");
let line = vimState.cursorStartPosition.line;

if (vimState.cursorStartPosition.isAfter(vimState.cursorPosition)) {
line = vimState.cursorPosition.line;
}

let newCursorPosition = new Position(line, 0);
vimState.cursorPosition = newCursorPosition;
vimState.cursorStartPosition = newCursorPosition;
vimState.currentMode = ModeName.Normal;
return vimState;
}
}

@RegisterAction
export class UpperCaseOperator extends BaseOperator {
Expand Down Expand Up @@ -3935,7 +3935,7 @@ class InsertInInsertVisualBlockMode extends BaseCommand {
}

// DOUBLE MOTIONS
// (dd yy cc << >>)
// (dd yy cc << >> ==)
// These work because there is a check in does/couldActionApply where
// you can't run an operator if you already have one going (which is logical).
// However there is the slightly weird behavior where dy actually deletes the whole
Expand Down Expand Up @@ -4008,6 +4008,19 @@ class MoveOutdent extends BaseMovement {
}
}

@RegisterAction
class MoveFormat extends BaseMovement {
modes = [ModeName.Normal];
keys = ["="];

public async execAction(position: Position, vimState: VimState): Promise<IMovement> {
return {
start : position.getLineBegin(),
stop : position.getLineEnd(),
};
}
}

@RegisterAction
class ActionDeleteLineVisualMode extends BaseCommand {
modes = [ModeName.Visual, ModeName.VisualLine];
Expand Down

0 comments on commit 4bb8ab3

Please sign in to comment.