Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support greater than and less than
  • Loading branch information
Wscats authored Nov 18, 2020
commit 72defdc77c3dae16cff9ad97a8cf51389a944c55
154 changes: 153 additions & 1 deletion src/vs/platform/contextkey/common/contextkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const enum ContextKeyExprType {
Or = 9,
In = 10,
NotIn = 11,
Greater = 12,
Less = 13,
}

export interface IContextKeyExprMapper {
Expand All @@ -57,7 +59,7 @@ export interface IContextKeyExpression {
export type ContextKeyExpression = (
ContextKeyFalseExpr | ContextKeyTrueExpr | ContextKeyDefinedExpr | ContextKeyNotExpr
| ContextKeyEqualsExpr | ContextKeyNotEqualsExpr | ContextKeyRegexExpr
| ContextKeyNotRegexExpr | ContextKeyAndExpr | ContextKeyOrExpr | ContextKeyInExpr | ContextKeyNotInExpr
| ContextKeyNotRegexExpr | ContextKeyAndExpr | ContextKeyOrExpr | ContextKeyInExpr | ContextKeyNotInExpr | ContextKeyGreaterExpr | ContextKeyLessExpr
);

export abstract class ContextKeyExpr {
Expand Down Expand Up @@ -102,6 +104,14 @@ export abstract class ContextKeyExpr {
return ContextKeyOrExpr.create(expr);
}

public static greater(key: string, value: any): ContextKeyExpression {
return ContextKeyGreaterExpr.create(key, value);
}

public static less(key: string, value: any): ContextKeyExpression {
return ContextKeyLessExpr.create(key, value);
}

public static deserialize(serialized: string | null | undefined, strict: boolean = false): ContextKeyExpression | undefined {
if (!serialized) {
return undefined;
Expand Down Expand Up @@ -143,6 +153,16 @@ export abstract class ContextKeyExpr {
return ContextKeyInExpr.create(pieces[0].trim(), pieces[1].trim());
}

if (serializedOne.indexOf('>') >= 0) {
let pieces = serializedOne.split('>');
return ContextKeyGreaterExpr.create(pieces[0].trim(), this._deserializeValue(pieces[1], strict));
}

if (serializedOne.indexOf('<') >= 0) {
let pieces = serializedOne.split('<');
return ContextKeyLessExpr.create(pieces[0].trim(), this._deserializeValue(pieces[1], strict));
}

if (/^\!\s*/.test(serializedOne)) {
return ContextKeyNotExpr.create(serializedOne.substr(1).trim());
}
Expand Down Expand Up @@ -650,6 +670,138 @@ export class ContextKeyNotExpr implements IContextKeyExpression {
}
}

export class ContextKeyGreaterExpr implements IContextKeyExpression {

public static create(key: string, value: any): ContextKeyExpression {
if (typeof value === 'boolean') {
return (value ? ContextKeyDefinedExpr.create(key) : ContextKeyNotExpr.create(key));
}
const staticValue = STATIC_VALUES.get(key);
if (typeof staticValue === 'boolean') {
const trueValue = staticValue ? 'true' : 'false';
return (value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
}
return new ContextKeyGreaterExpr(key, value);
}

public readonly type = ContextKeyExprType.Greater;

private constructor(private readonly key: string, private readonly value: any) {
}

public cmp(other: ContextKeyExpression): number {
if (other.type !== this.type) {
return this.type - other.type;
}
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: ContextKeyExpression): boolean {
if (other.type === this.type) {
return (this.key === other.key && this.value === other.value);
}
return false;
}

public evaluate(context: IContext): boolean {
return (Number(context.getValue(this.key)) > this.value);
}

public serialize(): string {
return `${this.key} > '${this.value}'`;
}

public keys(): string[] {
return [this.key];
}

public map(mapFnc: IContextKeyExprMapper): ContextKeyExpression {
return mapFnc.mapEquals(this.key, this.value);
}

public negate(): ContextKeyExpression {
return ContextKeyNotEqualsExpr.create(this.key, this.value);
}
}

export class ContextKeyLessExpr implements IContextKeyExpression {

public static create(key: string, value: any): ContextKeyExpression {
if (typeof value === 'boolean') {
return (value ? ContextKeyDefinedExpr.create(key) : ContextKeyNotExpr.create(key));
}
const staticValue = STATIC_VALUES.get(key);
if (typeof staticValue === 'boolean') {
const trueValue = staticValue ? 'true' : 'false';
return (value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
}
return new ContextKeyLessExpr(key, value);
}

public readonly type = ContextKeyExprType.Less;

private constructor(private readonly key: string, private readonly value: any) {
}

public cmp(other: ContextKeyExpression): number {
if (other.type !== this.type) {
return this.type - other.type;
}
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: ContextKeyExpression): boolean {
if (other.type === this.type) {
return (this.key === other.key && this.value === other.value);
}
return false;
}

public evaluate(context: IContext): boolean {
return (Number(context.getValue(this.key)) < this.value);
}

public serialize(): string {
return `${this.key} < '${this.value}'`;
}

public keys(): string[] {
return [this.key];
}

public map(mapFnc: IContextKeyExprMapper): ContextKeyExpression {
return mapFnc.mapEquals(this.key, this.value);
}

public negate(): ContextKeyExpression {
return ContextKeyNotEqualsExpr.create(this.key, this.value);
}
}

export class ContextKeyRegexExpr implements IContextKeyExpression {

public static create(key: string, regexp: RegExp | null): ContextKeyRegexExpr {
Expand Down