Skip to content

Commit

Permalink
Add yank support for Visual mode (#217)
Browse files Browse the repository at this point in the history
* add setNormal to ModeHandler

This allows safe switching to Normal mode

* add copy to TextEditor class

This allows to copy a selection to the clipboard. The clipboard is
handled by the copy-paste package that was already a dependency.

* add yank operator

This allows to yank selected text in visual mode.

* Remove unused import

* Add missing semicolon
  • Loading branch information
pjvds authored and jpoon committed May 27, 2016
1 parent 2c2243a commit 988afcf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class ModeHandler implements vscode.Disposable {
get currentMode() : Mode {
return this._modes.find(mode => mode.isActive);
}

setNormal() {
this.setCurrentModeByName(ModeName.Normal);
}

setCurrentModeByName(modeName : ModeName) {
for (let mode of this._modes) {
Expand Down
2 changes: 2 additions & 0 deletions src/mode/modeVisual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Motion} from './../motion/motion';
import { Position } from './../motion/position';
import { Operator } from './../operator/operator';
import { DeleteOperator } from './../operator/delete';
import { YankOperator } from './../operator/yank';
import { ModeHandler } from './modeHandler.ts';
import { ChangeOperator } from './../operator/change';

Expand Down Expand Up @@ -35,6 +36,7 @@ export class VisualMode extends Mode {
'd': new DeleteOperator(modeHandler),
'x': new DeleteOperator(modeHandler),
'c': new ChangeOperator(modeHandler),
'y': new YankOperator(modeHandler),
};
}

Expand Down
27 changes: 27 additions & 0 deletions src/operator/yank.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

import * as vscode from "vscode";
import { Position } from './../motion/position';
import { Operator } from './operator';
import { ModeHandler } from './../mode/modeHandler.ts';
import { TextEditor } from './../textEditor';

export class YankOperator extends Operator {

constructor(modeHandler: ModeHandler) {
super(modeHandler);
}

public key(): string { return "y"; }

/**
* Run this operator on a range.
*/
public async run(start: Position, end: Position): Promise<void> {
// TODO: use start and end
return TextEditor.copy(new vscode.Range(start, end)).then(() => {
this.modeHandler.currentMode.motion.select(end, end);
this.modeHandler.setNormal();
});
}
}
11 changes: 10 additions & 1 deletion src/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ export class TextEditor {
});
}

static async copy(range: vscode.Range): Promise<void> {
return new Promise<void>((resolve, reject) => {
const text = vscode.window.activeTextEditor.document.getText(range);
copy(text, (err) => {
(err) ? reject() : resolve();
});
});
}

static async delete(range: vscode.Range): Promise<boolean> {
copy(vscode.window.activeTextEditor.document.getText(range));
TextEditor.copy(range);
return vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.delete(range);
});
Expand Down

0 comments on commit 988afcf

Please sign in to comment.