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

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

merged 6 commits into from
Sep 21, 2016

Conversation

bdchauvette
Copy link
Contributor

This adds support for accessing the system clipboard using the "+ register, addressing #780.

The "+ register is basically an alias for the "* register, so I didn't make any changes to how the useClipboard setting is handled.

@rebornix
Copy link
Member

just a single tiny comment and we are good to go!

@bdchauvette
Copy link
Contributor Author

@rebornix Awesome! Just let me know what part you'd like commented, and I'll add it when I get a chance 👍

Copy link
Member

@johnfn johnfn left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Really nice work, just one small suggestion.

};

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!)

public static isClipboardRegister(registerName: string): boolean {
const register = Register.registers[registerName];

if (register && register.isClipboardRegister) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you just do return register && register.isClipboardRegister?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually tried that initially, but tsc doesn't like it very much:

> vim@0.1.10 vscode:prepublish c:\Users\Ben\Code\vscode-vim
> node ./node_modules/vscode/bin/compile -p ./
src/register/register.ts(42,12): error TS2322: Type 'boolean | undefined' is not assignable to type 'boolean'.
  Type 'undefined' is not assignable to type 'boolean'.

It works fine if I change the signature to isClipboardRegister(string): boolean | undefined, but I wasn't sure if that was preferable to having a messier function body.

Copy link
Member

Choose a reason for hiding this comment

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

Can you show me the exact line(s) of code that you're using to get that error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  public static isClipboardRegister(registerName: string): boolean {
    const register = Register.registers[registerName];
    return register && register.isClipboardRegister;
  }

capture

I think it's being caused by the isClipboardRegister property being optional: the property has a type signature of boolean | undefined, but the Register.isClipboardRegister method expects to only return booleans.

If we try to return the property without first converting it to a boolean, it's possible for the value to undefined (b/c it's optional), which violates the signature for the checking method.

(I think) this would also explain why it works if I change the allowed return type to boolean | undefined.

I'm pretty new to typescript, so I'm not sure what the idiomatic way to handle this kind of thing is 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each of these allow the code to compile without a hitch:

// Cast to boolean at runtime
return register && Boolean(register.isClipboardRegister);

// or:
return register && !!register.isClipboardRegister;
// Type assertion
return register && (register.isClipboardRegister as boolean);
// No return type annotation
public static isClipboardRegister(registerName: string) {
  const register = Register.registers[registerName];
  return register && register.isClipboardRegister;
}

Copy link
Member

Choose a reason for hiding this comment

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

Ohhhhh. It's because it's an optional property, which has type boolean | undefined, which we are returning directly. That makes sense... in a weird way. :)

I guess TypeScript is pointing out that we did a dumb thing by saying the type was isClipboardRegister?: boolean since now we can't distinguish undefined from false in an easy way.

Best is probably to change the type to isClipboardRegister: boolean, and add it to all registers. Then this code will work and also our undefined vs false case will go away.

TypeScript caught a (minor) bug! Yay, TypeScript!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yay, TypeScript! 🙌

Looks like I just broke Travis, though. Any ideas what's going on there? 😕

Copy link
Member

@jpoon jpoon Sep 21, 2016

Choose a reason for hiding this comment

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

¯_(ツ)_/¯ restarted the Travis build.

@johnfn
Copy link
Member

johnfn commented Sep 21, 2016

Annnd it's good!

Thanks @bdchauvette for the PR! :)

@johnfn johnfn merged commit 9a6b359 into VSCodeVim:master Sep 21, 2016
@bdchauvette bdchauvette deleted the register-quoteplus branch September 21, 2016 13:19
@bdchauvette
Copy link
Contributor Author

Awesome! Thanks for such a great project :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants