From ccaa96c81e5bd90387294828ceb579fc261dcc0e Mon Sep 17 00:00:00 2001 From: Cory Rivera Date: Mon, 6 Aug 2018 18:24:18 -0700 Subject: [PATCH] Add >= and <= operators for context key expressions. (#2160) --- .../platform/contextkey/common/contextkey.ts | 134 +++++++++++++++++- .../contextkey/test/common/contextkey.test.ts | 17 ++- 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/src/vs/platform/contextkey/common/contextkey.ts b/src/vs/platform/contextkey/common/contextkey.ts index 11c1b8a622..a5a1195388 100644 --- a/src/vs/platform/contextkey/common/contextkey.ts +++ b/src/vs/platform/contextkey/common/contextkey.ts @@ -14,7 +14,11 @@ export enum ContextKeyExprType { Equals = 3, NotEquals = 4, And = 5, - Regex = 6 + Regex = 6, + // {{SQL CARBON EDIT}} + GreaterThanEquals = 7, + LessThanEquals = 8 + // } export abstract class ContextKeyExpr { @@ -43,6 +47,15 @@ export abstract class ContextKeyExpr { return new ContextKeyAndExpr(expr); } + // {{SQL CARBON EDIT}} + public static greaterThanEquals(key: string, value: any): ContextKeyExpr { + return new ContextKeyGreaterThanEqualsExpr(key, value); + } + public static lessThanEquals(key: string, value: any): ContextKeyExpr { + return new ContextKeyLessThanEqualsExpr(key, value); + } + // + public static deserialize(serialized: string): ContextKeyExpr { if (!serialized) { return null; @@ -71,6 +84,17 @@ export abstract class ContextKeyExpr { return new ContextKeyRegexExpr(pieces[0].trim(), this._deserializeRegexValue(pieces[1])); } + // {{SQL CARBON EDIT}} + if (serializedOne.indexOf('>=') >= 0) { + let pieces = serializedOne.split('>='); + return new ContextKeyGreaterThanEqualsExpr(pieces[0].trim(), this._deserializeValue(pieces[1])); + } + if (serializedOne.indexOf('<=') >= 0) { + let pieces = serializedOne.split('<='); + return new ContextKeyLessThanEqualsExpr(pieces[0].trim(), this._deserializeValue(pieces[1])); + } + // + if (/^\!\s*/.test(serializedOne)) { return new ContextKeyNotExpr(serializedOne.substr(1).trim()); } @@ -146,6 +170,12 @@ function cmp(a: ContextKeyExpr, b: ContextKeyExpr): number { return (a).cmp(b); case ContextKeyExprType.Regex: return (a).cmp(b); + // {{SQL CARBON EDIT}} + case ContextKeyExprType.GreaterThanEquals: + return (a).cmp(b); + case ContextKeyExprType.LessThanEquals: + return (a).cmp(b); + // default: throw new Error('Unknown ContextKeyExpr!'); } @@ -504,6 +534,108 @@ export class ContextKeyAndExpr implements ContextKeyExpr { } } +// {{SQL CARBON EDIT}} +export class ContextKeyGreaterThanEqualsExpr implements ContextKeyExpr { + constructor(private key: string, private value: any) { + } + + public getType(): ContextKeyExprType { + return ContextKeyExprType.GreaterThanEquals; + } + + public cmp(other: ContextKeyGreaterThanEqualsExpr): number { + if (this.key < other.key) { + return -1; + } + if (this.key > other.key) { + return 1; + } + if (this.value < other.value) { + return -1; + } + if (this.value > other.value) { + return 1; + } + return 0; + } + + public equals(other: ContextKeyExpr): boolean { + if (other instanceof ContextKeyGreaterThanEqualsExpr) { + return (this.key === other.key && this.value === other.value); + } + return false; + } + + public evaluate(context: IContext): boolean { + let keyInt = parseFloat(context.getValue(this.key)); + let valueInt = parseFloat(this.value); + return (keyInt >= valueInt); + } + + public normalize(): ContextKeyExpr { + return this; + } + + public serialize(): string { + return this.key + ' >= \'' + this.value + '\''; + } + + public keys(): string[] { + return [this.key]; + } +} + +// {{SQL CARBON EDIT}} +export class ContextKeyLessThanEqualsExpr implements ContextKeyExpr { + constructor(private key: string, private value: any) { + } + + public getType(): ContextKeyExprType { + return ContextKeyExprType.LessThanEquals; + } + + public cmp(other: ContextKeyLessThanEqualsExpr): number { + if (this.key < other.key) { + return -1; + } + if (this.key > other.key) { + return 1; + } + if (this.value < other.value) { + return -1; + } + if (this.value > other.value) { + return 1; + } + return 0; + } + + public equals(other: ContextKeyExpr): boolean { + if (other instanceof ContextKeyLessThanEqualsExpr) { + return (this.key === other.key && this.value === other.value); + } + return false; + } + + public evaluate(context: IContext): boolean { + let keyInt = parseFloat(context.getValue(this.key)); + let valueInt = parseFloat(this.value); + return (keyInt <= valueInt); + } + + public normalize(): ContextKeyExpr { + return this; + } + + public serialize(): string { + return this.key + ' <= \'' + this.value + '\''; + } + + public keys(): string[] { + return [this.key]; + } +} + export class RawContextKey extends ContextKeyDefinedExpr { private _defaultValue: T; diff --git a/src/vs/platform/contextkey/test/common/contextkey.test.ts b/src/vs/platform/contextkey/test/common/contextkey.test.ts index 0f2950d630..bc56e3aa92 100644 --- a/src/vs/platform/contextkey/test/common/contextkey.test.ts +++ b/src/vs/platform/contextkey/test/common/contextkey.test.ts @@ -28,18 +28,29 @@ suite('ContextKeyExpr', () => { ContextKeyExpr.notEquals('c1', 'cc1'), ContextKeyExpr.notEquals('c2', 'cc2'), ContextKeyExpr.not('d1'), - ContextKeyExpr.not('d2') + ContextKeyExpr.not('d2'), + // {{SQL CARBON EDIT}} + ContextKeyExpr.greaterThanEquals('e1', 'ee1'), + ContextKeyExpr.greaterThanEquals('e2', 'ee2'), + ContextKeyExpr.lessThanEquals('f1', 'ff1'), + ContextKeyExpr.lessThanEquals('f2', 'ff2') + // ); let b = ContextKeyExpr.and( + // {{SQL CARBON EDIT}} + ContextKeyExpr.greaterThanEquals('e2', 'ee2'), ContextKeyExpr.equals('b2', 'bb2'), ContextKeyExpr.notEquals('c1', 'cc1'), ContextKeyExpr.not('d1'), + ContextKeyExpr.lessThanEquals('f1', 'ff1'), ContextKeyExpr.regex('d4', /\*\*3*/), + ContextKeyExpr.greaterThanEquals('e1', 'ee1'), ContextKeyExpr.notEquals('c2', 'cc2'), ContextKeyExpr.has('a2'), ContextKeyExpr.equals('b1', 'bb1'), ContextKeyExpr.regex('d3', /d.*/), ContextKeyExpr.has('a1'), + ContextKeyExpr.lessThanEquals('f2', 'ff2'), ContextKeyExpr.and(ContextKeyExpr.equals('and.a', true)), ContextKeyExpr.not('d2') ); @@ -82,6 +93,10 @@ suite('ContextKeyExpr', () => { testExpression('!' + expr, !value); testExpression(expr + ' =~ /d.*/', /d.*/.test(value)); testExpression(expr + ' =~ /D/i', /D/i.test(value)); + // {{SQL CARBON EDIT}} + testExpression(expr + ' >= 10', parseFloat(value) >= 10); + testExpression(expr + ' <= 10', parseFloat(value) <= 10); + // } testBatch('a', true);