-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
just a single tiny comment and we are good to go! |
@rebornix Awesome! Just let me know what part you'd like commented, and I'll add it when I get a chance 👍 |
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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;
}
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 😕
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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? 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
¯_(ツ)_/¯ restarted the Travis build.
Annnd it's good! Thanks @bdchauvette for the PR! :) |
Awesome! Thanks for such a great project :D |
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 theuseClipboard
setting is handled.