Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "+ system clipboard register (#780) #782

Merged
merged 6 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/register/register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { VimState } from './../mode/modeHandler';
import * as clipboard from 'copy-paste';

/**
* There are two different modes of copy/paste in Vim - copy by character
* and copy by line. Copy by line typically happens in Visual Line mode, but
Expand All @@ -21,7 +22,7 @@ export interface IRegisterContent {
export class Register {
/**
* The '"' is the unnamed register.
* The '*' is the special register for stroing into system clipboard.
* The '*' and '+' are special registers for accessing the system clipboard.
* TODO: Read-Only registers
* '.' register has the last inserted text.
* '%' register has the current file path.
Expand All @@ -30,9 +31,16 @@ export class Register {
*/
private static registers: { [key: string]: IRegisterContent } = {
'"': { text: "", registerMode: RegisterMode.CharacterWise },
'*': { text: "", registerMode: RegisterMode.CharacterWise }
'*': { text: "", registerMode: RegisterMode.CharacterWise },
'+': { text: "", registerMode: RegisterMode.CharacterWise }
};

private static systemClipboardRegisters: string[] = ["*", "+"];

public static isSystemClipboardRegister(register: string): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, make IRegisterContent have another optional field called isClipboardRegister and put the value on there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! I've switched the check to look up the isClipboardRegister property on the register.

(For consistency's sake, I also renamed the check from Register.isSystemClipboardRegister to just Register.isClipboardRegister. Saves a few keystrokes too!)

return Register.systemClipboardRegisters.indexOf(register) !== -1;
}

public static isValidRegister(register: string): boolean {
return register in Register.registers || /^[a-z0-9]+$/i.test(register);
}
Expand All @@ -48,7 +56,7 @@ export class Register {
throw new Error(`Invalid register ${register}`);
}

if (register === '*') {
if (Register.isSystemClipboardRegister(register)) {
clipboard.copy(content);
}

Expand All @@ -65,7 +73,7 @@ export class Register {
throw new Error(`Invalid register ${register}`);
}

if (register === '*') {
if (Register.isSystemClipboardRegister(register)) {
clipboard.copy(content);
}

Expand Down Expand Up @@ -94,7 +102,7 @@ export class Register {
}

/* Read from system clipboard */
if (register === '*') {
if (Register.isSystemClipboardRegister(register)) {
const text = await new Promise<string>((resolve, reject) =>
clipboard.paste((err, text) => {
if (err) {
Expand Down
8 changes: 8 additions & 0 deletions test/register/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ suite("register", () => {
});

clipboard.copy("12345");

newTest({
title: "Can access '*' (clipboard) register",
start: ['|one'],
keysPressed: '"*P',
end: ["1234|5one"],
});

newTest({
title: "Can access '+' (clipboard) register",
start: ['|one'],
keysPressed: '"+P',
end: ["1234|5one"],
});

newTest({
title: "Can use two registers together",
start: ['|one', "two"],
Expand Down