66
77import { createDecorator } from 'vs/platform/instantiation/common/instantiation' ;
88import Event from 'vs/base/common/event' ;
9+ import { isFalsyOrWhitespace } from 'vs/base/common/strings' ;
910
1011export enum ContextKeyExprType {
1112 Defined = 1 ,
@@ -30,7 +31,7 @@ export abstract class ContextKeyExpr {
3031 return new ContextKeyNotEqualsExpr ( key , value ) ;
3132 }
3233
33- public static regex ( key : string , value : string ) : ContextKeyExpr {
34+ public static regex ( key : string , value : RegExp ) : ContextKeyExpr {
3435 return new ContextKeyRegexExpr ( key , value ) ;
3536 }
3637
@@ -67,7 +68,7 @@ export abstract class ContextKeyExpr {
6768
6869 if ( serializedOne . indexOf ( '=~' ) >= 0 ) {
6970 let pieces = serializedOne . split ( '=~' ) ;
70- return new ContextKeyRegexExpr ( pieces [ 0 ] . trim ( ) , this . _deserializeValue ( pieces [ 1 ] ) ) ;
71+ return new ContextKeyRegexExpr ( pieces [ 0 ] . trim ( ) , this . _deserializeRegexValue ( pieces [ 1 ] ) ) ;
7172 }
7273
7374 if ( / ^ \! \s * / . test ( serializedOne ) ) {
@@ -96,6 +97,29 @@ export abstract class ContextKeyExpr {
9697 return serializedValue ;
9798 }
9899
100+ private static _deserializeRegexValue ( serializedValue : string ) : RegExp {
101+
102+ if ( isFalsyOrWhitespace ( serializedValue ) ) {
103+ console . warn ( 'missing regexp-value for =~-expression' ) ;
104+ return null ;
105+ }
106+
107+ let start = serializedValue . indexOf ( '/' ) ;
108+ let end = serializedValue . lastIndexOf ( '/' ) ;
109+ if ( start === end || start < 0 /* || to < 0 */ ) {
110+ console . warn ( `bad regexp-value '${ serializedValue } ', missing /-enclosure` ) ;
111+ return null ;
112+ }
113+
114+ let value = serializedValue . slice ( start + 1 , end ) ;
115+ try {
116+ return new RegExp ( value ) ;
117+ } catch ( e ) {
118+ console . warn ( `bad regexp-value '${ serializedValue } ', parse error: ${ e } ` ) ;
119+ return null ;
120+ }
121+ }
122+
99123 public abstract getType ( ) : ContextKeyExprType ;
100124 public abstract equals ( other : ContextKeyExpr ) : boolean ;
101125 public abstract evaluate ( context : IContext ) : boolean ;
@@ -334,15 +358,8 @@ export class ContextKeyNotExpr implements ContextKeyExpr {
334358
335359export class ContextKeyRegexExpr implements ContextKeyExpr {
336360
337- private regexp : { source : string , test ( s : string ) : boolean } ;
338-
339- constructor ( private key : string , value : any ) {
340- try {
341- this . regexp = new RegExp ( value ) ;
342- } catch ( e ) {
343- this . regexp = { source : '' , test ( ) { return false ; } } ;
344- console . warn ( `Bad value for glob-context key expression: ${ value } ` ) ;
345- }
361+ constructor ( private key : string , private regexp : RegExp ) {
362+ //
346363 }
347364
348365 public getType ( ) : ContextKeyExprType {
@@ -356,32 +373,34 @@ export class ContextKeyRegexExpr implements ContextKeyExpr {
356373 if ( this . key > other . key ) {
357374 return 1 ;
358375 }
359- if ( this . regexp . source < other . regexp . source ) {
376+ const source = this . regexp ? this . regexp . source : undefined ;
377+ if ( source < other . regexp . source ) {
360378 return - 1 ;
361379 }
362- if ( this . regexp . source > other . regexp . source ) {
380+ if ( source > other . regexp . source ) {
363381 return 1 ;
364382 }
365383 return 0 ;
366384 }
367385
368386 public equals ( other : ContextKeyExpr ) : boolean {
369387 if ( other instanceof ContextKeyRegexExpr ) {
370- return ( this . key === other . key && this . regexp . source === other . regexp . source ) ;
388+ const source = this . regexp ? this . regexp . source : undefined ;
389+ return ( this . key === other . key && source === other . regexp . source ) ;
371390 }
372391 return false ;
373392 }
374393
375394 public evaluate ( context : IContext ) : boolean {
376- return this . regexp . test ( context . getValue ( this . key ) ) ;
395+ return this . regexp ? this . regexp . test ( context . getValue ( this . key ) ) : false ;
377396 }
378397
379398 public normalize ( ) : ContextKeyExpr {
380399 return this ;
381400 }
382401
383402 public serialize ( ) : string {
384- return this . key + ' =~ \'' + this . regexp . source + '\'' ;
403+ return ` ${ this . keys } =~ / ${ this . regexp ? this . regexp . source : '<invalid>' } /` ;
385404 }
386405
387406 public keys ( ) : string [ ] {
0 commit comments