Skip to content

Commit 6434b82

Browse files
committed
Fixes microsoft#85058: Handle isMac, isLinux, isWindows directly
1 parent 970951b commit 6434b82

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

src/vs/platform/contextkey/common/contextkey.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
import { Event } from 'vs/base/common/event';
77
import { isFalsyOrWhitespace } from 'vs/base/common/strings';
88
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
9+
import { isMacintosh, isLinux, isWindows } from 'vs/base/common/platform';
10+
11+
const STATIC_VALUES = new Map<string, boolean>();
12+
STATIC_VALUES.set('false', false);
13+
STATIC_VALUES.set('true', true);
14+
STATIC_VALUES.set('isMac', isMacintosh);
15+
STATIC_VALUES.set('isLinux', isLinux);
16+
STATIC_VALUES.set('isWindows', isWindows);
917

1018
export const enum ContextKeyExprType {
1119
False = 0,
@@ -262,11 +270,9 @@ export class ContextKeyTrueExpr implements IContextKeyExpression {
262270

263271
export class ContextKeyDefinedExpr implements IContextKeyExpression {
264272
public static create(key: string): ContextKeyExpression {
265-
if (key === 'false') {
266-
return ContextKeyFalseExpr.INSTANCE;
267-
}
268-
if (key === 'true') {
269-
return ContextKeyTrueExpr.INSTANCE;
273+
const staticValue = STATIC_VALUES.get(key);
274+
if (typeof staticValue === 'boolean') {
275+
return staticValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
270276
}
271277
return new ContextKeyDefinedExpr(key);
272278
}
@@ -321,18 +327,12 @@ export class ContextKeyEqualsExpr implements IContextKeyExpression {
321327

322328
public static create(key: string, value: any): ContextKeyExpression {
323329
if (typeof value === 'boolean') {
324-
if (value) {
325-
return ContextKeyDefinedExpr.create(key);
326-
}
327-
return ContextKeyNotExpr.create(key);
330+
return (value ? ContextKeyDefinedExpr.create(key) : ContextKeyNotExpr.create(key));
328331
}
329-
if (key === 'false') {
330-
// false only equals false
331-
return (value === 'false' ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
332-
}
333-
if (key === 'true') {
334-
// true only equals true
335-
return (value === 'true' ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
332+
const staticValue = STATIC_VALUES.get(key);
333+
if (typeof staticValue === 'boolean') {
334+
const trueValue = staticValue ? 'true' : 'false';
335+
return (value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
336336
}
337337
return new ContextKeyEqualsExpr(key, value);
338338
}
@@ -400,13 +400,10 @@ export class ContextKeyNotEqualsExpr implements IContextKeyExpression {
400400
}
401401
return ContextKeyDefinedExpr.create(key);
402402
}
403-
if (key === 'false') {
404-
// false only equals false
405-
return (value === 'false' ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
406-
}
407-
if (key === 'true') {
408-
// true only equals true
409-
return (value === 'true' ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
403+
const staticValue = STATIC_VALUES.get(key);
404+
if (typeof staticValue === 'boolean') {
405+
const falseValue = staticValue ? 'true' : 'false';
406+
return (value === falseValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
410407
}
411408
return new ContextKeyNotEqualsExpr(key, value);
412409
}
@@ -468,13 +465,9 @@ export class ContextKeyNotEqualsExpr implements IContextKeyExpression {
468465
export class ContextKeyNotExpr implements IContextKeyExpression {
469466

470467
public static create(key: string): ContextKeyExpression {
471-
if (key === 'false') {
472-
// !false
473-
return ContextKeyTrueExpr.INSTANCE;
474-
}
475-
if (key === 'true') {
476-
// !true
477-
return ContextKeyFalseExpr.INSTANCE;
468+
const staticValue = STATIC_VALUES.get(key);
469+
if (typeof staticValue === 'boolean') {
470+
return (staticValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
478471
}
479472
return new ContextKeyNotExpr(key);
480473
}

src/vs/platform/contextkey/test/common/contextkey.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55
import * as assert from 'assert';
66
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
7+
import { isMacintosh, isLinux, isWindows } from 'vs/base/common/platform';
78

89
function createContext(ctx: any) {
910
return {
@@ -124,10 +125,15 @@ suite('ContextKeyExpr', () => {
124125
assert.strictEqual(actual, expected);
125126
}
126127
testNormalize('true', 'true');
128+
testNormalize('!true', 'false');
127129
testNormalize('false', 'false');
130+
testNormalize('!false', 'true');
128131
testNormalize('a && true', 'a');
129132
testNormalize('a && false', 'false');
130133
testNormalize('a || true', 'true');
131134
testNormalize('a || false', 'a');
135+
testNormalize('isMac', isMacintosh ? 'true' : 'false');
136+
testNormalize('isLinux', isLinux ? 'true' : 'false');
137+
testNormalize('isWindows', isWindows ? 'true' : 'false');
132138
});
133139
});

src/vs/platform/keybinding/common/keybindingResolver.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export class KeybindingResolver {
4949
continue;
5050
}
5151

52+
if (k.when && k.when.type === ContextKeyExprType.False) {
53+
// when condition is false
54+
continue;
55+
}
56+
5257
// TODO@chords
5358
this._addKeyPress(k.keypressParts[0], k);
5459
}

0 commit comments

Comments
 (0)