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

[FR] Add 'or' logic for when clause contexts #22778

Open
formulahendry opened this Issue Mar 17, 2017 · 6 comments

Comments

Projects
None yet
6 participants
@formulahendry
Copy link
Member

formulahendry commented Mar 17, 2017

In https://code.visualstudio.com/docs/customization/keybindings#_when-clause-contexts , there are and logic like "editorTextFocus && editorLangId == csharp". When I try some like editorLangId == c || editorLangId == cpp || editorLangId == arduino || editorLangId == ini in context menu. It not works. So I guess it is not supported yet. This logic would be very useful. 😄

@rmunn

This comment has been minimized.

Copy link
Contributor

rmunn commented Mar 23, 2017

This would be nice to have, I agree. Until it's added, it looks like you can get what you want by defining multiple keybindings with a single when clause each. E.g., the example at https://code.visualstudio.com/docs/getstarted/keybindings#_keyboard-rules has:

// Keybindings that are complementary
{ "key": "f5", "command": "workbench.action.debug.continue", "when": "inDebugMode" },
{ "key": "f5", "command": "workbench.action.debug.start",    "when": "!inDebugMode" },

So you could probably write:

// Example
{ "key": "ctrl+e", "command": "do.something", "when": "editorLangId == c" },
{ "key": "ctrl+e", "command": "do.something", "when": "editorLangId == cpp" },
{ "key": "ctrl+e", "command": "do.something", "when": "editorLangId == arduino" },
{ "key": "ctrl+e", "command": "do.something", "when": "editorLangId == ini" },

But that gets a bit unwieldy, and I would also like to see the || operator available. Plus parentheses, so that we can distinguish between condition1 && (condition2 || condition3) and (condition1 && condition2) || condition3.

@Chillee

This comment has been minimized.

Copy link

Chillee commented Jun 12, 2018

@rmunn Unluckily, I don't believe that workaround works for extensions that are trying to contribute keybindings.

@Adam13531

This comment has been minimized.

Copy link

Adam13531 commented Sep 17, 2018

For those looking for another workaround, you can apply De Morgan's laws to represent a boolean OR with just AND operators:

A || B == !(!A && !B)

For example:

image

@formulahendry

This comment has been minimized.

Copy link
Member Author

formulahendry commented Sep 25, 2018

@Adam13531 Thanks for the workaround. I have tired, but seems to not work as expected.
E.g. view == iotHubDevices && !(viewItem != moduleA && viewItem != moduleB) will just be the same as view == iotHubDevices

@Adam13531

This comment has been minimized.

Copy link

Adam13531 commented Oct 1, 2018

@formulahendry sorry for the late response. This is just boolean logic, so you can try this in any online code interpreter for any language just to see if you've properly converted || to && before putting it into Visual Studio Code. Barring that, it can help to write out truth tables when working with boolean logic so that you can see exactly how something should end up, e.g.:

A B && ||
0 0 0  0
0 1 0  1
1 0 0  1
1 1 1  1

If you want, you can post the condition you're trying to achieve here and I can maybe help (if I ever read my GitHub notifications 😳).

@joshtynjala

This comment has been minimized.

Copy link

joshtynjala commented Oct 3, 2018

I also found that !(!A && !B) does not work. I'm guessing that parentheses are not supported either.

In a duplicate issue, I found an actual valid workaround that will help in some cases. Apparently, you can use regular expressions to compare a field against multiple values. So, the original post that wants to allow multiple values for editorLangId can be converted to this:

editorLangId =~ /(c|cpp|arduino|ini)/

That's obviously not going to work for all possible cases where you'd want to use ||, but at least it helps for some of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment