-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from petegleeson/support-for-copy-and-p-command
Support for copy and p command. Thanks @petegleeson
- Loading branch information
Showing
8 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |