Skip to content

Commit

Permalink
Merge pull request #208 from petegleeson/support-for-copy-and-p-command
Browse files Browse the repository at this point in the history
Support for copy and p command. Thanks @petegleeson
  • Loading branch information
jpoon committed May 3, 2016
2 parents 602a4df + 5f54784 commit 7655907
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 3 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ notifications:

sudo: false

addons:
apt:
packages:
- xclip

language: node_js

node_js:
- "4.2"

env:
- GITHUB_TOKEN=1b17d62d38a4846efa7ea4de4b773b581787b0f1

before_install:
- if [ $TRAVIS_OS_NAME == "linux" ]; then
export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
Expand Down
5 changes: 5 additions & 0 deletions src/mode/modeNormal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Motion, MotionMode} from './../motion/motion';
import {ModeHandler} from './modeHandler';
import {DeleteOperator} from './../operator/delete';
import {ChangeOperator} from './../operator/change';
import {PutOperator} from './../operator/put';
import {TextEditor} from './../textEditor';

export class NormalMode extends Mode {
Expand Down Expand Up @@ -136,6 +137,10 @@ export class NormalMode extends Mode {
return {};
},
"X" : async (m) => { return vscode.commands.executeCommand("deleteLeft"); },
"p" : async (m) => {
await new PutOperator(this._modeHandler).run(m.position, null);
return {};
},
"esc": async () => { return vscode.commands.executeCommand("workbench.action.closeMessages"); }
};

Expand Down
2 changes: 1 addition & 1 deletion src/mode/modeVisual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class VisualMode extends Mode {
// simply allow the operators to say what mode they transition into.
'd': new DeleteOperator(modeHandler),
'x': new DeleteOperator(modeHandler),
'c': new ChangeOperator(modeHandler)
'c': new ChangeOperator(modeHandler),
};
}

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

import { paste } from 'copy-paste';
import { Position } from './../motion/position';
import { Operator } from './operator';
import { ModeHandler } from './../mode/modeHandler.ts';
import { TextEditor } from './../textEditor';

export class PutOperator extends Operator {

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

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

/**
* Run this operator on a range.
*/
public async run(start: Position, end: Position): Promise<void> {
return new Promise<void>((resolve, reject) => {
paste(async (err, data) => {
if (err) {
reject();
} else {
await TextEditor.insertAt(data, start.getRight());
this.modeHandler.currentMode.motion.moveTo(start.line, start.getRight().character);
resolve();
}
});
});
}
}
2 changes: 2 additions & 0 deletions src/textEditor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

import * as vscode from "vscode";
import {copy} from "copy-paste";

export class TextEditor {
static async insert(text: string): Promise<boolean> {
Expand All @@ -16,6 +17,7 @@ export class TextEditor {
}

static async delete(range: vscode.Range): Promise<boolean> {
copy(vscode.window.activeTextEditor.document.getText(range));
return vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.delete(range);
});
Expand Down
61 changes: 61 additions & 0 deletions test/operator/put.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";

import * as assert from 'assert';
import * as vscode from 'vscode';
import { copy } from "copy-paste";
import { ModeHandler } from "../../src/mode/modeHandler";
import { PutOperator } from "../../src/operator/put";
import { TextEditor } from '../../src/textEditor';
import { Position, PositionOptions } from "../../src/motion/position";
import { setupWorkspace, cleanUpWorkspace } from '../testUtils';

suite("put operator", () => {
suiteSetup(setupWorkspace);

suiteTeardown(cleanUpWorkspace);

test("put 'the dog' into empty file", async () => {
const expectedText = "the dog";
const position = new Position(0, 0, PositionOptions.CharacterWiseExclusive);
const mode = new ModeHandler();
const put = new PutOperator(mode);
copy(expectedText);

await put.run(position, position);

const actualText = TextEditor.readLineAt(0);
const cursorPosition = vscode.window.activeTextEditor.selection.active;
assert.equal(actualText, expectedText,
"did not paste expected content");
assert.equal(cursorPosition.line, position.getRight().line,
"cursor should be on the same line");
assert.equal(cursorPosition.character, position.getRight().character,
"cursor should be on start of put content");
});

test("put ' brown' into 'the dog'", async () => {
const phrase = "brown ";
const expectedText = `the ${phrase}dog`;
const position = new Position(0, 3, PositionOptions.CharacterWiseExclusive);
const mode = new ModeHandler();
const put = new PutOperator(mode);
copy(phrase);

// using ^ to show the cusor position
// before : the dog
// ^
// after : the brown dog
// ^

await put.run(position, position);

const actualText = TextEditor.readLineAt(0);
const cursorPosition = vscode.window.activeTextEditor.selection.active;
assert.equal(actualText, expectedText,
"did not paste expected content");
assert.equal(cursorPosition.line, position.getRight().line,
"cursor should be on the same line");
assert.equal(cursorPosition.character, position.getRight().character,
"cursor should be on start of put content");
});
});
12 changes: 12 additions & 0 deletions test/textEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as assert from 'assert';
import * as vscode from 'vscode';
import {paste} from "copy-paste";
import {TextEditor} from './../src/textEditor';
import {setupWorkspace, cleanUpWorkspace} from './testUtils';

Expand Down Expand Up @@ -59,4 +60,15 @@ suite("text editor", () => {
assert.throws(() => TextEditor.readLineAt(1), RangeError);
assert.throws(() => TextEditor.readLineAt(2), RangeError);
});

test("delete should copy to clipboard", async () => {
const expectedText = "Hello World";
await TextEditor.insert(expectedText);

const range = vscode.window.activeTextEditor.document.lineAt(0).range;

await TextEditor.delete(range);
const actualText = paste();
assert.equal(actualText, expectedText);
});
});
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "vim",
"ambientDependencies": {
"copy-paste": "registry:dt/copy-paste#1.1.3+20160117130525",
"lodash": "github:DefinitelyTyped/DefinitelyTyped/lodash/lodash.d.ts#7b7aa2027a8fb6219a8bcf1b6bb12bcd0ff9539d"
}
}

0 comments on commit 7655907

Please sign in to comment.