|
6 | 6 | import { Event } from 'vs/base/common/event'; |
7 | 7 | import { isFalsyOrWhitespace } from 'vs/base/common/strings'; |
8 | 8 | 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); |
9 | 17 |
|
10 | 18 | export const enum ContextKeyExprType { |
11 | 19 | False = 0, |
@@ -262,11 +270,9 @@ export class ContextKeyTrueExpr implements IContextKeyExpression { |
262 | 270 |
|
263 | 271 | export class ContextKeyDefinedExpr implements IContextKeyExpression { |
264 | 272 | 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; |
270 | 276 | } |
271 | 277 | return new ContextKeyDefinedExpr(key); |
272 | 278 | } |
@@ -321,18 +327,12 @@ export class ContextKeyEqualsExpr implements IContextKeyExpression { |
321 | 327 |
|
322 | 328 | public static create(key: string, value: any): ContextKeyExpression { |
323 | 329 | 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)); |
328 | 331 | } |
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); |
336 | 336 | } |
337 | 337 | return new ContextKeyEqualsExpr(key, value); |
338 | 338 | } |
@@ -400,13 +400,10 @@ export class ContextKeyNotEqualsExpr implements IContextKeyExpression { |
400 | 400 | } |
401 | 401 | return ContextKeyDefinedExpr.create(key); |
402 | 402 | } |
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); |
410 | 407 | } |
411 | 408 | return new ContextKeyNotEqualsExpr(key, value); |
412 | 409 | } |
@@ -468,13 +465,9 @@ export class ContextKeyNotEqualsExpr implements IContextKeyExpression { |
468 | 465 | export class ContextKeyNotExpr implements IContextKeyExpression { |
469 | 466 |
|
470 | 467 | 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); |
478 | 471 | } |
479 | 472 | return new ContextKeyNotExpr(key); |
480 | 473 | } |
|
0 commit comments