@delta-ai/platform 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/action/common/action.d.ts +75 -0
  2. package/dist/action/common/action.d.ts.map +1 -0
  3. package/dist/action/common/action.js +17 -0
  4. package/dist/action/common/action.js.map +1 -0
  5. package/dist/action/common/actionCommonCategories.d.ts +9 -0
  6. package/dist/action/common/actionCommonCategories.d.ts.map +1 -0
  7. package/dist/action/common/actionCommonCategories.js +14 -0
  8. package/dist/action/common/actionCommonCategories.js.map +1 -0
  9. package/dist/commands/common/commands.d.ts +52 -0
  10. package/dist/commands/common/commands.d.ts.map +1 -0
  11. package/dist/commands/common/commands.js +79 -0
  12. package/dist/commands/common/commands.js.map +1 -0
  13. package/dist/contextkey/common/contextkey.d.ts +439 -0
  14. package/dist/contextkey/common/contextkey.d.ts.map +1 -0
  15. package/dist/contextkey/common/contextkey.js +1729 -0
  16. package/dist/contextkey/common/contextkey.js.map +1 -0
  17. package/dist/contextkey/common/scanner.d.ts +142 -0
  18. package/dist/contextkey/common/scanner.d.ts.map +1 -0
  19. package/dist/contextkey/common/scanner.js +311 -0
  20. package/dist/contextkey/common/scanner.js.map +1 -0
  21. package/dist/environment/common/argv.d.ts +173 -0
  22. package/dist/environment/common/argv.d.ts.map +1 -0
  23. package/dist/environment/common/argv.js +6 -0
  24. package/dist/environment/common/argv.js.map +1 -0
  25. package/dist/environment/common/environment.d.ts +113 -0
  26. package/dist/environment/common/environment.d.ts.map +1 -0
  27. package/dist/environment/common/environment.js +9 -0
  28. package/dist/environment/common/environment.js.map +1 -0
  29. package/dist/environment/common/environmentService.d.ts +91 -0
  30. package/dist/environment/common/environmentService.d.ts.map +1 -0
  31. package/dist/environment/common/environmentService.js +278 -0
  32. package/dist/environment/common/environmentService.js.map +1 -0
  33. package/dist/instantiation/common/descriptors.d.ts +10 -0
  34. package/dist/instantiation/common/descriptors.d.ts.map +1 -0
  35. package/dist/instantiation/common/descriptors.js +15 -0
  36. package/dist/instantiation/common/descriptors.js.map +1 -0
  37. package/dist/instantiation/common/graph.d.ts +26 -0
  38. package/dist/instantiation/common/graph.d.ts.map +1 -0
  39. package/dist/instantiation/common/graph.js +96 -0
  40. package/dist/instantiation/common/graph.js.map +1 -0
  41. package/dist/instantiation/common/instantiation.d.ts +76 -0
  42. package/dist/instantiation/common/instantiation.d.ts.map +1 -0
  43. package/dist/instantiation/common/instantiation.js +49 -0
  44. package/dist/instantiation/common/instantiation.js.map +1 -0
  45. package/dist/instantiation/common/instantiationService.d.ts +56 -0
  46. package/dist/instantiation/common/instantiationService.d.ts.map +1 -0
  47. package/dist/instantiation/common/instantiationService.js +415 -0
  48. package/dist/instantiation/common/instantiationService.js.map +1 -0
  49. package/dist/instantiation/common/serviceCollection.d.ts +10 -0
  50. package/dist/instantiation/common/serviceCollection.d.ts.map +1 -0
  51. package/dist/instantiation/common/serviceCollection.js +25 -0
  52. package/dist/instantiation/common/serviceCollection.js.map +1 -0
  53. package/dist/log/common/log.d.ts +297 -0
  54. package/dist/log/common/log.d.ts.map +1 -0
  55. package/dist/log/common/log.js +668 -0
  56. package/dist/log/common/log.js.map +1 -0
  57. package/dist/registry/common/platform.d.ts +21 -0
  58. package/dist/registry/common/platform.d.ts.map +1 -0
  59. package/dist/registry/common/platform.js +31 -0
  60. package/dist/registry/common/platform.js.map +1 -0
  61. package/package.json +42 -0
@@ -0,0 +1,1729 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { CharCode } from '@delta-ai/base/common/charCode.js';
6
+ import { Event } from '@delta-ai/base/common/event.js';
7
+ import { isChrome, isEdge, isFirefox, isLinux, isMacintosh, isSafari, isWeb, isWindows } from '@delta-ai/base/common/platform.js';
8
+ import { isFalsyOrWhitespace } from '@delta-ai/base/common/strings.js';
9
+ import { Scanner, TokenType } from './scanner.js';
10
+ import { createDecorator } from '@delta-ai/platform/instantiation/common/instantiation.js';
11
+ import { localize } from '@delta-ai/base/nls.js';
12
+ import { illegalArgument } from '@delta-ai/base/common/errors.js';
13
+ const CONSTANT_VALUES = new Map();
14
+ CONSTANT_VALUES.set('false', false);
15
+ CONSTANT_VALUES.set('true', true);
16
+ CONSTANT_VALUES.set('isMac', isMacintosh);
17
+ CONSTANT_VALUES.set('isLinux', isLinux);
18
+ CONSTANT_VALUES.set('isWindows', isWindows);
19
+ CONSTANT_VALUES.set('isWeb', isWeb);
20
+ CONSTANT_VALUES.set('isMacNative', isMacintosh && !isWeb);
21
+ CONSTANT_VALUES.set('isEdge', isEdge);
22
+ CONSTANT_VALUES.set('isFirefox', isFirefox);
23
+ CONSTANT_VALUES.set('isChrome', isChrome);
24
+ CONSTANT_VALUES.set('isSafari', isSafari);
25
+ /** allow register constant context keys that are known only after startup; requires running `substituteConstants` on the context key - https://github.com/microsoft/vscode/issues/174218#issuecomment-1437972127 */
26
+ export function setConstant(key, value) {
27
+ if (CONSTANT_VALUES.get(key) !== undefined) {
28
+ throw illegalArgument('contextkey.setConstant(k, v) invoked with already set constant `k`');
29
+ }
30
+ CONSTANT_VALUES.set(key, value);
31
+ }
32
+ const hasOwnProperty = Object.prototype.hasOwnProperty;
33
+ export var ContextKeyExprType;
34
+ (function (ContextKeyExprType) {
35
+ ContextKeyExprType[ContextKeyExprType["False"] = 0] = "False";
36
+ ContextKeyExprType[ContextKeyExprType["True"] = 1] = "True";
37
+ ContextKeyExprType[ContextKeyExprType["Defined"] = 2] = "Defined";
38
+ ContextKeyExprType[ContextKeyExprType["Not"] = 3] = "Not";
39
+ ContextKeyExprType[ContextKeyExprType["Equals"] = 4] = "Equals";
40
+ ContextKeyExprType[ContextKeyExprType["NotEquals"] = 5] = "NotEquals";
41
+ ContextKeyExprType[ContextKeyExprType["And"] = 6] = "And";
42
+ ContextKeyExprType[ContextKeyExprType["Regex"] = 7] = "Regex";
43
+ ContextKeyExprType[ContextKeyExprType["NotRegex"] = 8] = "NotRegex";
44
+ ContextKeyExprType[ContextKeyExprType["Or"] = 9] = "Or";
45
+ ContextKeyExprType[ContextKeyExprType["In"] = 10] = "In";
46
+ ContextKeyExprType[ContextKeyExprType["NotIn"] = 11] = "NotIn";
47
+ ContextKeyExprType[ContextKeyExprType["Greater"] = 12] = "Greater";
48
+ ContextKeyExprType[ContextKeyExprType["GreaterEquals"] = 13] = "GreaterEquals";
49
+ ContextKeyExprType[ContextKeyExprType["Smaller"] = 14] = "Smaller";
50
+ ContextKeyExprType[ContextKeyExprType["SmallerEquals"] = 15] = "SmallerEquals";
51
+ })(ContextKeyExprType || (ContextKeyExprType = {}));
52
+ const defaultConfig = {
53
+ regexParsingWithErrorRecovery: true
54
+ };
55
+ const errorEmptyString = localize('contextkey.parser.error.emptyString', "Empty context key expression");
56
+ const hintEmptyString = localize('contextkey.parser.error.emptyString.hint', "Did you forget to write an expression? You can also put 'false' or 'true' to always evaluate to false or true, respectively.");
57
+ const errorNoInAfterNot = localize('contextkey.parser.error.noInAfterNot', "'in' after 'not'.");
58
+ const errorClosingParenthesis = localize('contextkey.parser.error.closingParenthesis', "closing parenthesis ')'");
59
+ const errorUnexpectedToken = localize('contextkey.parser.error.unexpectedToken', "Unexpected token");
60
+ const hintUnexpectedToken = localize('contextkey.parser.error.unexpectedToken.hint', "Did you forget to put && or || before the token?");
61
+ const errorUnexpectedEOF = localize('contextkey.parser.error.unexpectedEOF', "Unexpected end of expression");
62
+ const hintUnexpectedEOF = localize('contextkey.parser.error.unexpectedEOF.hint', "Did you forget to put a context key?");
63
+ /**
64
+ * A parser for context key expressions.
65
+ *
66
+ * Example:
67
+ * ```ts
68
+ * const parser = new Parser();
69
+ * const expr = parser.parse('foo == "bar" && baz == true');
70
+ *
71
+ * if (expr === undefined) {
72
+ * // there were lexing or parsing errors
73
+ * // process lexing errors with `parser.lexingErrors`
74
+ * // process parsing errors with `parser.parsingErrors`
75
+ * } else {
76
+ * // expr is a valid expression
77
+ * }
78
+ * ```
79
+ */
80
+ export class Parser {
81
+ _config;
82
+ // Note: this doesn't produce an exact syntax tree but a normalized one
83
+ // ContextKeyExpression's that we use as AST nodes do not expose constructors that do not normalize
84
+ static _parseError = new Error();
85
+ // lifetime note: `_scanner` lives as long as the parser does, i.e., is not reset between calls to `parse`
86
+ _scanner = new Scanner();
87
+ // lifetime note: `_tokens`, `_current`, and `_parsingErrors` must be reset between calls to `parse`
88
+ _tokens = [];
89
+ _current = 0; // invariant: 0 <= this._current < this._tokens.length ; any incrementation of this value must first call `_isAtEnd`
90
+ _parsingErrors = [];
91
+ get lexingErrors() {
92
+ return this._scanner.errors;
93
+ }
94
+ get parsingErrors() {
95
+ return this._parsingErrors;
96
+ }
97
+ constructor(_config = defaultConfig) {
98
+ this._config = _config;
99
+ }
100
+ /**
101
+ * Parse a context key expression.
102
+ *
103
+ * @param input the expression to parse
104
+ * @returns the parsed expression or `undefined` if there's an error - call `lexingErrors` and `parsingErrors` to see the errors
105
+ */
106
+ parse(input) {
107
+ if (input === '') {
108
+ this._parsingErrors.push({ message: errorEmptyString, offset: 0, lexeme: '', additionalInfo: hintEmptyString });
109
+ return undefined;
110
+ }
111
+ this._tokens = this._scanner.reset(input).scan();
112
+ // @ulugbekna: we do not stop parsing if there are lexing errors to be able to reconstruct regexes with unescaped slashes; TODO@ulugbekna: make this respect config option for recovery
113
+ this._current = 0;
114
+ this._parsingErrors = [];
115
+ try {
116
+ const expr = this._expr();
117
+ if (!this._isAtEnd()) {
118
+ const peek = this._peek();
119
+ const additionalInfo = peek.type === TokenType.Str ? hintUnexpectedToken : undefined;
120
+ this._parsingErrors.push({ message: errorUnexpectedToken, offset: peek.offset, lexeme: Scanner.getLexeme(peek), additionalInfo });
121
+ throw Parser._parseError;
122
+ }
123
+ return expr;
124
+ }
125
+ catch (e) {
126
+ if (!(e === Parser._parseError)) {
127
+ throw e;
128
+ }
129
+ return undefined;
130
+ }
131
+ }
132
+ _expr() {
133
+ return this._or();
134
+ }
135
+ _or() {
136
+ const expr = [this._and()];
137
+ while (this._matchOne(TokenType.Or)) {
138
+ const right = this._and();
139
+ expr.push(right);
140
+ }
141
+ return expr.length === 1 ? expr[0] : ContextKeyExpr.or(...expr);
142
+ }
143
+ _and() {
144
+ const expr = [this._term()];
145
+ while (this._matchOne(TokenType.And)) {
146
+ const right = this._term();
147
+ expr.push(right);
148
+ }
149
+ return expr.length === 1 ? expr[0] : ContextKeyExpr.and(...expr);
150
+ }
151
+ _term() {
152
+ if (this._matchOne(TokenType.Neg)) {
153
+ const peek = this._peek();
154
+ switch (peek.type) {
155
+ case TokenType.True:
156
+ this._advance();
157
+ return ContextKeyFalseExpr.INSTANCE;
158
+ case TokenType.False:
159
+ this._advance();
160
+ return ContextKeyTrueExpr.INSTANCE;
161
+ case TokenType.LParen: {
162
+ this._advance();
163
+ const expr = this._expr();
164
+ this._consume(TokenType.RParen, errorClosingParenthesis);
165
+ return expr?.negate();
166
+ }
167
+ case TokenType.Str:
168
+ this._advance();
169
+ return ContextKeyNotExpr.create(peek.lexeme);
170
+ default:
171
+ throw this._errExpectedButGot(`KEY | true | false | '(' expression ')'`, peek);
172
+ }
173
+ }
174
+ return this._primary();
175
+ }
176
+ _primary() {
177
+ const peek = this._peek();
178
+ switch (peek.type) {
179
+ case TokenType.True:
180
+ this._advance();
181
+ return ContextKeyExpr.true();
182
+ case TokenType.False:
183
+ this._advance();
184
+ return ContextKeyExpr.false();
185
+ case TokenType.LParen: {
186
+ this._advance();
187
+ const expr = this._expr();
188
+ this._consume(TokenType.RParen, errorClosingParenthesis);
189
+ return expr;
190
+ }
191
+ case TokenType.Str: {
192
+ // KEY
193
+ const key = peek.lexeme;
194
+ this._advance();
195
+ // =~ regex
196
+ if (this._matchOne(TokenType.RegexOp)) {
197
+ // @ulugbekna: we need to reconstruct the regex from the tokens because some extensions use unescaped slashes in regexes
198
+ const expr = this._peek();
199
+ if (!this._config.regexParsingWithErrorRecovery) {
200
+ this._advance();
201
+ if (expr.type !== TokenType.RegexStr) {
202
+ throw this._errExpectedButGot(`REGEX`, expr);
203
+ }
204
+ const regexLexeme = expr.lexeme;
205
+ const closingSlashIndex = regexLexeme.lastIndexOf('/');
206
+ const flags = closingSlashIndex === regexLexeme.length - 1 ? undefined : this._removeFlagsGY(regexLexeme.substring(closingSlashIndex + 1));
207
+ let regexp;
208
+ try {
209
+ regexp = new RegExp(regexLexeme.substring(1, closingSlashIndex), flags);
210
+ }
211
+ catch (e) {
212
+ throw this._errExpectedButGot(`REGEX`, expr);
213
+ }
214
+ return ContextKeyRegexExpr.create(key, regexp);
215
+ }
216
+ switch (expr.type) {
217
+ case TokenType.RegexStr:
218
+ case TokenType.Error: { // also handle an ErrorToken in case of smth such as /(/file)/
219
+ const lexemeReconstruction = [expr.lexeme]; // /REGEX/ or /REGEX/FLAGS
220
+ this._advance();
221
+ let followingToken = this._peek();
222
+ let parenBalance = 0;
223
+ for (let i = 0; i < expr.lexeme.length; i++) {
224
+ if (expr.lexeme.charCodeAt(i) === CharCode.OpenParen) {
225
+ parenBalance++;
226
+ }
227
+ else if (expr.lexeme.charCodeAt(i) === CharCode.CloseParen) {
228
+ parenBalance--;
229
+ }
230
+ }
231
+ while (!this._isAtEnd() && followingToken.type !== TokenType.And && followingToken.type !== TokenType.Or) {
232
+ switch (followingToken.type) {
233
+ case TokenType.LParen:
234
+ parenBalance++;
235
+ break;
236
+ case TokenType.RParen:
237
+ parenBalance--;
238
+ break;
239
+ case TokenType.RegexStr:
240
+ case TokenType.QuotedStr:
241
+ for (let i = 0; i < followingToken.lexeme.length; i++) {
242
+ if (followingToken.lexeme.charCodeAt(i) === CharCode.OpenParen) {
243
+ parenBalance++;
244
+ }
245
+ else if (expr.lexeme.charCodeAt(i) === CharCode.CloseParen) {
246
+ parenBalance--;
247
+ }
248
+ }
249
+ }
250
+ if (parenBalance < 0) {
251
+ break;
252
+ }
253
+ lexemeReconstruction.push(Scanner.getLexeme(followingToken));
254
+ this._advance();
255
+ followingToken = this._peek();
256
+ }
257
+ const regexLexeme = lexemeReconstruction.join('');
258
+ const closingSlashIndex = regexLexeme.lastIndexOf('/');
259
+ const flags = closingSlashIndex === regexLexeme.length - 1 ? undefined : this._removeFlagsGY(regexLexeme.substring(closingSlashIndex + 1));
260
+ let regexp;
261
+ try {
262
+ regexp = new RegExp(regexLexeme.substring(1, closingSlashIndex), flags);
263
+ }
264
+ catch (e) {
265
+ throw this._errExpectedButGot(`REGEX`, expr);
266
+ }
267
+ return ContextKeyExpr.regex(key, regexp);
268
+ }
269
+ case TokenType.QuotedStr: {
270
+ const serializedValue = expr.lexeme;
271
+ this._advance();
272
+ // replicate old regex parsing behavior
273
+ let regex = null;
274
+ if (!isFalsyOrWhitespace(serializedValue)) {
275
+ const start = serializedValue.indexOf('/');
276
+ const end = serializedValue.lastIndexOf('/');
277
+ if (start !== end && start >= 0) {
278
+ const value = serializedValue.slice(start + 1, end);
279
+ const caseIgnoreFlag = serializedValue[end + 1] === 'i' ? 'i' : '';
280
+ try {
281
+ regex = new RegExp(value, caseIgnoreFlag);
282
+ }
283
+ catch (_e) {
284
+ throw this._errExpectedButGot(`REGEX`, expr);
285
+ }
286
+ }
287
+ }
288
+ if (regex === null) {
289
+ throw this._errExpectedButGot('REGEX', expr);
290
+ }
291
+ return ContextKeyRegexExpr.create(key, regex);
292
+ }
293
+ default:
294
+ throw this._errExpectedButGot('REGEX', this._peek());
295
+ }
296
+ }
297
+ // [ 'not' 'in' value ]
298
+ if (this._matchOne(TokenType.Not)) {
299
+ this._consume(TokenType.In, errorNoInAfterNot);
300
+ const right = this._value();
301
+ return ContextKeyExpr.notIn(key, right);
302
+ }
303
+ // [ ('==' | '!=' | '<' | '<=' | '>' | '>=' | 'in') value ]
304
+ const maybeOp = this._peek().type;
305
+ switch (maybeOp) {
306
+ case TokenType.Eq: {
307
+ this._advance();
308
+ const right = this._value();
309
+ if (this._previous().type === TokenType.QuotedStr) { // to preserve old parser behavior: "foo == 'true'" is preserved as "foo == 'true'", but "foo == true" is optimized as "foo"
310
+ return ContextKeyExpr.equals(key, right);
311
+ }
312
+ switch (right) {
313
+ case 'true':
314
+ return ContextKeyExpr.has(key);
315
+ case 'false':
316
+ return ContextKeyExpr.not(key);
317
+ default:
318
+ return ContextKeyExpr.equals(key, right);
319
+ }
320
+ }
321
+ case TokenType.NotEq: {
322
+ this._advance();
323
+ const right = this._value();
324
+ if (this._previous().type === TokenType.QuotedStr) { // same as above with "foo != 'true'"
325
+ return ContextKeyExpr.notEquals(key, right);
326
+ }
327
+ switch (right) {
328
+ case 'true':
329
+ return ContextKeyExpr.not(key);
330
+ case 'false':
331
+ return ContextKeyExpr.has(key);
332
+ default:
333
+ return ContextKeyExpr.notEquals(key, right);
334
+ }
335
+ }
336
+ // TODO: ContextKeyExpr.smaller(key, right) accepts only `number` as `right` AND during eval of this node, we just eval to `false` if `right` is not a number
337
+ // consequently, package.json linter should _warn_ the user if they're passing undesired things to ops
338
+ case TokenType.Lt:
339
+ this._advance();
340
+ return ContextKeySmallerExpr.create(key, this._value());
341
+ case TokenType.LtEq:
342
+ this._advance();
343
+ return ContextKeySmallerEqualsExpr.create(key, this._value());
344
+ case TokenType.Gt:
345
+ this._advance();
346
+ return ContextKeyGreaterExpr.create(key, this._value());
347
+ case TokenType.GtEq:
348
+ this._advance();
349
+ return ContextKeyGreaterEqualsExpr.create(key, this._value());
350
+ case TokenType.In:
351
+ this._advance();
352
+ return ContextKeyExpr.in(key, this._value());
353
+ default:
354
+ return ContextKeyExpr.has(key);
355
+ }
356
+ }
357
+ case TokenType.EOF:
358
+ this._parsingErrors.push({ message: errorUnexpectedEOF, offset: peek.offset, lexeme: '', additionalInfo: hintUnexpectedEOF });
359
+ throw Parser._parseError;
360
+ default:
361
+ throw this._errExpectedButGot(`true | false | KEY \n\t| KEY '=~' REGEX \n\t| KEY ('==' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not' 'in') value`, this._peek());
362
+ }
363
+ }
364
+ _value() {
365
+ const token = this._peek();
366
+ switch (token.type) {
367
+ case TokenType.Str:
368
+ case TokenType.QuotedStr:
369
+ this._advance();
370
+ return token.lexeme;
371
+ case TokenType.True:
372
+ this._advance();
373
+ return 'true';
374
+ case TokenType.False:
375
+ this._advance();
376
+ return 'false';
377
+ case TokenType.In: // we support `in` as a value, e.g., "when": "languageId == in" - exists in existing extensions
378
+ this._advance();
379
+ return 'in';
380
+ default:
381
+ // this allows "when": "foo == " which's used by existing extensions
382
+ // we do not call `_advance` on purpose - we don't want to eat unintended tokens
383
+ return '';
384
+ }
385
+ }
386
+ _flagsGYRe = /g|y/g;
387
+ _removeFlagsGY(flags) {
388
+ return flags.replaceAll(this._flagsGYRe, '');
389
+ }
390
+ // careful: this can throw if current token is the initial one (ie index = 0)
391
+ _previous() {
392
+ return this._tokens[this._current - 1];
393
+ }
394
+ _matchOne(token) {
395
+ if (this._check(token)) {
396
+ this._advance();
397
+ return true;
398
+ }
399
+ return false;
400
+ }
401
+ _advance() {
402
+ if (!this._isAtEnd()) {
403
+ this._current++;
404
+ }
405
+ return this._previous();
406
+ }
407
+ _consume(type, message) {
408
+ if (this._check(type)) {
409
+ return this._advance();
410
+ }
411
+ throw this._errExpectedButGot(message, this._peek());
412
+ }
413
+ _errExpectedButGot(expected, got, additionalInfo) {
414
+ const message = localize('contextkey.parser.error.expectedButGot', "Expected: {0}\nReceived: '{1}'.", expected, Scanner.getLexeme(got));
415
+ const offset = got.offset;
416
+ const lexeme = Scanner.getLexeme(got);
417
+ this._parsingErrors.push({ message, offset, lexeme, additionalInfo });
418
+ return Parser._parseError;
419
+ }
420
+ _check(type) {
421
+ return this._peek().type === type;
422
+ }
423
+ _peek() {
424
+ return this._tokens[this._current];
425
+ }
426
+ _isAtEnd() {
427
+ return this._peek().type === TokenType.EOF;
428
+ }
429
+ }
430
+ export class ContextKeyExpr {
431
+ static false() {
432
+ return ContextKeyFalseExpr.INSTANCE;
433
+ }
434
+ static true() {
435
+ return ContextKeyTrueExpr.INSTANCE;
436
+ }
437
+ static has(key) {
438
+ return ContextKeyDefinedExpr.create(key);
439
+ }
440
+ static equals(key, value) {
441
+ return ContextKeyEqualsExpr.create(key, value);
442
+ }
443
+ static notEquals(key, value) {
444
+ return ContextKeyNotEqualsExpr.create(key, value);
445
+ }
446
+ static regex(key, value) {
447
+ return ContextKeyRegexExpr.create(key, value);
448
+ }
449
+ static in(key, value) {
450
+ return ContextKeyInExpr.create(key, value);
451
+ }
452
+ static notIn(key, value) {
453
+ return ContextKeyNotInExpr.create(key, value);
454
+ }
455
+ static not(key) {
456
+ return ContextKeyNotExpr.create(key);
457
+ }
458
+ static and(...expr) {
459
+ return ContextKeyAndExpr.create(expr, null, true);
460
+ }
461
+ static or(...expr) {
462
+ return ContextKeyOrExpr.create(expr, null, true);
463
+ }
464
+ static greater(key, value) {
465
+ return ContextKeyGreaterExpr.create(key, value);
466
+ }
467
+ static greaterEquals(key, value) {
468
+ return ContextKeyGreaterEqualsExpr.create(key, value);
469
+ }
470
+ static smaller(key, value) {
471
+ return ContextKeySmallerExpr.create(key, value);
472
+ }
473
+ static smallerEquals(key, value) {
474
+ return ContextKeySmallerEqualsExpr.create(key, value);
475
+ }
476
+ static _parser = new Parser({ regexParsingWithErrorRecovery: false });
477
+ static deserialize(serialized) {
478
+ if (serialized === undefined || serialized === null) { // an empty string needs to be handled by the parser to get a corresponding parsing error reported
479
+ return undefined;
480
+ }
481
+ const expr = this._parser.parse(serialized);
482
+ return expr;
483
+ }
484
+ }
485
+ export function validateWhenClauses(whenClauses) {
486
+ const parser = new Parser({ regexParsingWithErrorRecovery: false }); // we run with no recovery to guide users to use correct regexes
487
+ return whenClauses.map(whenClause => {
488
+ parser.parse(whenClause);
489
+ if (parser.lexingErrors.length > 0) {
490
+ return parser.lexingErrors.map((se) => ({
491
+ errorMessage: se.additionalInfo ?
492
+ localize('contextkey.scanner.errorForLinterWithHint', "Unexpected token. Hint: {0}", se.additionalInfo) :
493
+ localize('contextkey.scanner.errorForLinter', "Unexpected token."),
494
+ offset: se.offset,
495
+ length: se.lexeme.length,
496
+ }));
497
+ }
498
+ else if (parser.parsingErrors.length > 0) {
499
+ return parser.parsingErrors.map((pe) => ({
500
+ errorMessage: pe.additionalInfo ? `${pe.message}. ${pe.additionalInfo}` : pe.message,
501
+ offset: pe.offset,
502
+ length: pe.lexeme.length,
503
+ }));
504
+ }
505
+ else {
506
+ return [];
507
+ }
508
+ });
509
+ }
510
+ export function expressionsAreEqualWithConstantSubstitution(a, b) {
511
+ const aExpr = a ? a.substituteConstants() : undefined;
512
+ const bExpr = b ? b.substituteConstants() : undefined;
513
+ if (!aExpr && !bExpr) {
514
+ return true;
515
+ }
516
+ if (!aExpr || !bExpr) {
517
+ return false;
518
+ }
519
+ return aExpr.equals(bExpr);
520
+ }
521
+ function cmp(a, b) {
522
+ return a.cmp(b);
523
+ }
524
+ export class ContextKeyFalseExpr {
525
+ static INSTANCE = new ContextKeyFalseExpr();
526
+ type = ContextKeyExprType.False;
527
+ constructor() {
528
+ }
529
+ cmp(other) {
530
+ return this.type - other.type;
531
+ }
532
+ equals(other) {
533
+ return (other.type === this.type);
534
+ }
535
+ substituteConstants() {
536
+ return this;
537
+ }
538
+ evaluate(context) {
539
+ return false;
540
+ }
541
+ serialize() {
542
+ return 'false';
543
+ }
544
+ keys() {
545
+ return [];
546
+ }
547
+ map(mapFnc) {
548
+ return this;
549
+ }
550
+ negate() {
551
+ return ContextKeyTrueExpr.INSTANCE;
552
+ }
553
+ }
554
+ export class ContextKeyTrueExpr {
555
+ static INSTANCE = new ContextKeyTrueExpr();
556
+ type = ContextKeyExprType.True;
557
+ constructor() {
558
+ }
559
+ cmp(other) {
560
+ return this.type - other.type;
561
+ }
562
+ equals(other) {
563
+ return (other.type === this.type);
564
+ }
565
+ substituteConstants() {
566
+ return this;
567
+ }
568
+ evaluate(context) {
569
+ return true;
570
+ }
571
+ serialize() {
572
+ return 'true';
573
+ }
574
+ keys() {
575
+ return [];
576
+ }
577
+ map(mapFnc) {
578
+ return this;
579
+ }
580
+ negate() {
581
+ return ContextKeyFalseExpr.INSTANCE;
582
+ }
583
+ }
584
+ export class ContextKeyDefinedExpr {
585
+ key;
586
+ negated;
587
+ static create(key, negated = null) {
588
+ const constantValue = CONSTANT_VALUES.get(key);
589
+ if (typeof constantValue === 'boolean') {
590
+ return constantValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
591
+ }
592
+ return new ContextKeyDefinedExpr(key, negated);
593
+ }
594
+ type = ContextKeyExprType.Defined;
595
+ constructor(key, negated) {
596
+ this.key = key;
597
+ this.negated = negated;
598
+ }
599
+ cmp(other) {
600
+ if (other.type !== this.type) {
601
+ return this.type - other.type;
602
+ }
603
+ return cmp1(this.key, other.key);
604
+ }
605
+ equals(other) {
606
+ if (other.type === this.type) {
607
+ return (this.key === other.key);
608
+ }
609
+ return false;
610
+ }
611
+ substituteConstants() {
612
+ const constantValue = CONSTANT_VALUES.get(this.key);
613
+ if (typeof constantValue === 'boolean') {
614
+ return constantValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
615
+ }
616
+ return this;
617
+ }
618
+ evaluate(context) {
619
+ return (!!context.getValue(this.key));
620
+ }
621
+ serialize() {
622
+ return this.key;
623
+ }
624
+ keys() {
625
+ return [this.key];
626
+ }
627
+ map(mapFnc) {
628
+ return mapFnc.mapDefined(this.key);
629
+ }
630
+ negate() {
631
+ if (!this.negated) {
632
+ this.negated = ContextKeyNotExpr.create(this.key, this);
633
+ }
634
+ return this.negated;
635
+ }
636
+ }
637
+ export class ContextKeyEqualsExpr {
638
+ key;
639
+ value;
640
+ negated;
641
+ static create(key, value, negated = null) {
642
+ if (typeof value === 'boolean') {
643
+ return (value ? ContextKeyDefinedExpr.create(key, negated) : ContextKeyNotExpr.create(key, negated));
644
+ }
645
+ const constantValue = CONSTANT_VALUES.get(key);
646
+ if (typeof constantValue === 'boolean') {
647
+ const trueValue = constantValue ? 'true' : 'false';
648
+ return (value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
649
+ }
650
+ return new ContextKeyEqualsExpr(key, value, negated);
651
+ }
652
+ type = ContextKeyExprType.Equals;
653
+ constructor(key, value, negated) {
654
+ this.key = key;
655
+ this.value = value;
656
+ this.negated = negated;
657
+ }
658
+ cmp(other) {
659
+ if (other.type !== this.type) {
660
+ return this.type - other.type;
661
+ }
662
+ return cmp2(this.key, this.value, other.key, other.value);
663
+ }
664
+ equals(other) {
665
+ if (other.type === this.type) {
666
+ return (this.key === other.key && this.value === other.value);
667
+ }
668
+ return false;
669
+ }
670
+ substituteConstants() {
671
+ const constantValue = CONSTANT_VALUES.get(this.key);
672
+ if (typeof constantValue === 'boolean') {
673
+ const trueValue = constantValue ? 'true' : 'false';
674
+ return (this.value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
675
+ }
676
+ return this;
677
+ }
678
+ evaluate(context) {
679
+ // Intentional ==
680
+ // eslint-disable-next-line eqeqeq
681
+ return (context.getValue(this.key) == this.value);
682
+ }
683
+ serialize() {
684
+ return `${this.key} == '${this.value}'`;
685
+ }
686
+ keys() {
687
+ return [this.key];
688
+ }
689
+ map(mapFnc) {
690
+ return mapFnc.mapEquals(this.key, this.value);
691
+ }
692
+ negate() {
693
+ if (!this.negated) {
694
+ this.negated = ContextKeyNotEqualsExpr.create(this.key, this.value, this);
695
+ }
696
+ return this.negated;
697
+ }
698
+ }
699
+ export class ContextKeyInExpr {
700
+ key;
701
+ valueKey;
702
+ static create(key, valueKey) {
703
+ return new ContextKeyInExpr(key, valueKey);
704
+ }
705
+ type = ContextKeyExprType.In;
706
+ negated = null;
707
+ constructor(key, valueKey) {
708
+ this.key = key;
709
+ this.valueKey = valueKey;
710
+ }
711
+ cmp(other) {
712
+ if (other.type !== this.type) {
713
+ return this.type - other.type;
714
+ }
715
+ return cmp2(this.key, this.valueKey, other.key, other.valueKey);
716
+ }
717
+ equals(other) {
718
+ if (other.type === this.type) {
719
+ return (this.key === other.key && this.valueKey === other.valueKey);
720
+ }
721
+ return false;
722
+ }
723
+ substituteConstants() {
724
+ return this;
725
+ }
726
+ evaluate(context) {
727
+ const source = context.getValue(this.valueKey);
728
+ const item = context.getValue(this.key);
729
+ if (Array.isArray(source)) {
730
+ // eslint-disable-next-line local/code-no-any-casts
731
+ if (source.includes(item)) {
732
+ return true;
733
+ }
734
+ // On Windows, file paths are case-insensitive so file URI
735
+ // comparisons must be done in a case-insensitive manner.
736
+ if (isWindows && typeof item === 'string' && item.startsWith('file:///')) {
737
+ const itemLower = item.toLowerCase();
738
+ return source.some(s => typeof s === 'string' && s.toLowerCase() === itemLower);
739
+ }
740
+ return false;
741
+ }
742
+ if (typeof item === 'string' && typeof source === 'object' && source !== null) {
743
+ if (hasOwnProperty.call(source, item)) {
744
+ return true;
745
+ }
746
+ // On Windows, file paths are case-insensitive so file URI
747
+ // property lookups must be done in a case-insensitive manner.
748
+ if (isWindows && item.startsWith('file:///')) {
749
+ const itemLower = item.toLowerCase();
750
+ return Object.keys(source).some(key => key.toLowerCase() === itemLower);
751
+ }
752
+ return false;
753
+ }
754
+ return false;
755
+ }
756
+ serialize() {
757
+ return `${this.key} in '${this.valueKey}'`;
758
+ }
759
+ keys() {
760
+ return [this.key, this.valueKey];
761
+ }
762
+ map(mapFnc) {
763
+ return mapFnc.mapIn(this.key, this.valueKey);
764
+ }
765
+ negate() {
766
+ if (!this.negated) {
767
+ this.negated = ContextKeyNotInExpr.create(this.key, this.valueKey);
768
+ }
769
+ return this.negated;
770
+ }
771
+ }
772
+ export class ContextKeyNotInExpr {
773
+ key;
774
+ valueKey;
775
+ static create(key, valueKey) {
776
+ return new ContextKeyNotInExpr(key, valueKey);
777
+ }
778
+ type = ContextKeyExprType.NotIn;
779
+ _negated;
780
+ constructor(key, valueKey) {
781
+ this.key = key;
782
+ this.valueKey = valueKey;
783
+ this._negated = ContextKeyInExpr.create(key, valueKey);
784
+ }
785
+ cmp(other) {
786
+ if (other.type !== this.type) {
787
+ return this.type - other.type;
788
+ }
789
+ return this._negated.cmp(other._negated);
790
+ }
791
+ equals(other) {
792
+ if (other.type === this.type) {
793
+ return this._negated.equals(other._negated);
794
+ }
795
+ return false;
796
+ }
797
+ substituteConstants() {
798
+ return this;
799
+ }
800
+ evaluate(context) {
801
+ return !this._negated.evaluate(context);
802
+ }
803
+ serialize() {
804
+ return `${this.key} not in '${this.valueKey}'`;
805
+ }
806
+ keys() {
807
+ return this._negated.keys();
808
+ }
809
+ map(mapFnc) {
810
+ return mapFnc.mapNotIn(this.key, this.valueKey);
811
+ }
812
+ negate() {
813
+ return this._negated;
814
+ }
815
+ }
816
+ export class ContextKeyNotEqualsExpr {
817
+ key;
818
+ value;
819
+ negated;
820
+ static create(key, value, negated = null) {
821
+ if (typeof value === 'boolean') {
822
+ if (value) {
823
+ return ContextKeyNotExpr.create(key, negated);
824
+ }
825
+ return ContextKeyDefinedExpr.create(key, negated);
826
+ }
827
+ const constantValue = CONSTANT_VALUES.get(key);
828
+ if (typeof constantValue === 'boolean') {
829
+ const falseValue = constantValue ? 'true' : 'false';
830
+ return (value === falseValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
831
+ }
832
+ return new ContextKeyNotEqualsExpr(key, value, negated);
833
+ }
834
+ type = ContextKeyExprType.NotEquals;
835
+ constructor(key, value, negated) {
836
+ this.key = key;
837
+ this.value = value;
838
+ this.negated = negated;
839
+ }
840
+ cmp(other) {
841
+ if (other.type !== this.type) {
842
+ return this.type - other.type;
843
+ }
844
+ return cmp2(this.key, this.value, other.key, other.value);
845
+ }
846
+ equals(other) {
847
+ if (other.type === this.type) {
848
+ return (this.key === other.key && this.value === other.value);
849
+ }
850
+ return false;
851
+ }
852
+ substituteConstants() {
853
+ const constantValue = CONSTANT_VALUES.get(this.key);
854
+ if (typeof constantValue === 'boolean') {
855
+ const falseValue = constantValue ? 'true' : 'false';
856
+ return (this.value === falseValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
857
+ }
858
+ return this;
859
+ }
860
+ evaluate(context) {
861
+ // Intentional !=
862
+ // eslint-disable-next-line eqeqeq
863
+ return (context.getValue(this.key) != this.value);
864
+ }
865
+ serialize() {
866
+ return `${this.key} != '${this.value}'`;
867
+ }
868
+ keys() {
869
+ return [this.key];
870
+ }
871
+ map(mapFnc) {
872
+ return mapFnc.mapNotEquals(this.key, this.value);
873
+ }
874
+ negate() {
875
+ if (!this.negated) {
876
+ this.negated = ContextKeyEqualsExpr.create(this.key, this.value, this);
877
+ }
878
+ return this.negated;
879
+ }
880
+ }
881
+ export class ContextKeyNotExpr {
882
+ key;
883
+ negated;
884
+ static create(key, negated = null) {
885
+ const constantValue = CONSTANT_VALUES.get(key);
886
+ if (typeof constantValue === 'boolean') {
887
+ return (constantValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
888
+ }
889
+ return new ContextKeyNotExpr(key, negated);
890
+ }
891
+ type = ContextKeyExprType.Not;
892
+ constructor(key, negated) {
893
+ this.key = key;
894
+ this.negated = negated;
895
+ }
896
+ cmp(other) {
897
+ if (other.type !== this.type) {
898
+ return this.type - other.type;
899
+ }
900
+ return cmp1(this.key, other.key);
901
+ }
902
+ equals(other) {
903
+ if (other.type === this.type) {
904
+ return (this.key === other.key);
905
+ }
906
+ return false;
907
+ }
908
+ substituteConstants() {
909
+ const constantValue = CONSTANT_VALUES.get(this.key);
910
+ if (typeof constantValue === 'boolean') {
911
+ return (constantValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
912
+ }
913
+ return this;
914
+ }
915
+ evaluate(context) {
916
+ return (!context.getValue(this.key));
917
+ }
918
+ serialize() {
919
+ return `!${this.key}`;
920
+ }
921
+ keys() {
922
+ return [this.key];
923
+ }
924
+ map(mapFnc) {
925
+ return mapFnc.mapNot(this.key);
926
+ }
927
+ negate() {
928
+ if (!this.negated) {
929
+ this.negated = ContextKeyDefinedExpr.create(this.key, this);
930
+ }
931
+ return this.negated;
932
+ }
933
+ }
934
+ function withFloatOrStr(value, callback) {
935
+ if (typeof value === 'string') {
936
+ const n = parseFloat(value);
937
+ if (!isNaN(n)) {
938
+ value = n;
939
+ }
940
+ }
941
+ if (typeof value === 'string' || typeof value === 'number') {
942
+ return callback(value);
943
+ }
944
+ return ContextKeyFalseExpr.INSTANCE;
945
+ }
946
+ export class ContextKeyGreaterExpr {
947
+ key;
948
+ value;
949
+ negated;
950
+ static create(key, _value, negated = null) {
951
+ return withFloatOrStr(_value, (value) => new ContextKeyGreaterExpr(key, value, negated));
952
+ }
953
+ type = ContextKeyExprType.Greater;
954
+ constructor(key, value, negated) {
955
+ this.key = key;
956
+ this.value = value;
957
+ this.negated = negated;
958
+ }
959
+ cmp(other) {
960
+ if (other.type !== this.type) {
961
+ return this.type - other.type;
962
+ }
963
+ return cmp2(this.key, this.value, other.key, other.value);
964
+ }
965
+ equals(other) {
966
+ if (other.type === this.type) {
967
+ return (this.key === other.key && this.value === other.value);
968
+ }
969
+ return false;
970
+ }
971
+ substituteConstants() {
972
+ return this;
973
+ }
974
+ evaluate(context) {
975
+ if (typeof this.value === 'string') {
976
+ return false;
977
+ }
978
+ return (parseFloat(context.getValue(this.key)) > this.value);
979
+ }
980
+ serialize() {
981
+ return `${this.key} > ${this.value}`;
982
+ }
983
+ keys() {
984
+ return [this.key];
985
+ }
986
+ map(mapFnc) {
987
+ return mapFnc.mapGreater(this.key, this.value);
988
+ }
989
+ negate() {
990
+ if (!this.negated) {
991
+ this.negated = ContextKeySmallerEqualsExpr.create(this.key, this.value, this);
992
+ }
993
+ return this.negated;
994
+ }
995
+ }
996
+ export class ContextKeyGreaterEqualsExpr {
997
+ key;
998
+ value;
999
+ negated;
1000
+ static create(key, _value, negated = null) {
1001
+ return withFloatOrStr(_value, (value) => new ContextKeyGreaterEqualsExpr(key, value, negated));
1002
+ }
1003
+ type = ContextKeyExprType.GreaterEquals;
1004
+ constructor(key, value, negated) {
1005
+ this.key = key;
1006
+ this.value = value;
1007
+ this.negated = negated;
1008
+ }
1009
+ cmp(other) {
1010
+ if (other.type !== this.type) {
1011
+ return this.type - other.type;
1012
+ }
1013
+ return cmp2(this.key, this.value, other.key, other.value);
1014
+ }
1015
+ equals(other) {
1016
+ if (other.type === this.type) {
1017
+ return (this.key === other.key && this.value === other.value);
1018
+ }
1019
+ return false;
1020
+ }
1021
+ substituteConstants() {
1022
+ return this;
1023
+ }
1024
+ evaluate(context) {
1025
+ if (typeof this.value === 'string') {
1026
+ return false;
1027
+ }
1028
+ return (parseFloat(context.getValue(this.key)) >= this.value);
1029
+ }
1030
+ serialize() {
1031
+ return `${this.key} >= ${this.value}`;
1032
+ }
1033
+ keys() {
1034
+ return [this.key];
1035
+ }
1036
+ map(mapFnc) {
1037
+ return mapFnc.mapGreaterEquals(this.key, this.value);
1038
+ }
1039
+ negate() {
1040
+ if (!this.negated) {
1041
+ this.negated = ContextKeySmallerExpr.create(this.key, this.value, this);
1042
+ }
1043
+ return this.negated;
1044
+ }
1045
+ }
1046
+ export class ContextKeySmallerExpr {
1047
+ key;
1048
+ value;
1049
+ negated;
1050
+ static create(key, _value, negated = null) {
1051
+ return withFloatOrStr(_value, (value) => new ContextKeySmallerExpr(key, value, negated));
1052
+ }
1053
+ type = ContextKeyExprType.Smaller;
1054
+ constructor(key, value, negated) {
1055
+ this.key = key;
1056
+ this.value = value;
1057
+ this.negated = negated;
1058
+ }
1059
+ cmp(other) {
1060
+ if (other.type !== this.type) {
1061
+ return this.type - other.type;
1062
+ }
1063
+ return cmp2(this.key, this.value, other.key, other.value);
1064
+ }
1065
+ equals(other) {
1066
+ if (other.type === this.type) {
1067
+ return (this.key === other.key && this.value === other.value);
1068
+ }
1069
+ return false;
1070
+ }
1071
+ substituteConstants() {
1072
+ return this;
1073
+ }
1074
+ evaluate(context) {
1075
+ if (typeof this.value === 'string') {
1076
+ return false;
1077
+ }
1078
+ return (parseFloat(context.getValue(this.key)) < this.value);
1079
+ }
1080
+ serialize() {
1081
+ return `${this.key} < ${this.value}`;
1082
+ }
1083
+ keys() {
1084
+ return [this.key];
1085
+ }
1086
+ map(mapFnc) {
1087
+ return mapFnc.mapSmaller(this.key, this.value);
1088
+ }
1089
+ negate() {
1090
+ if (!this.negated) {
1091
+ this.negated = ContextKeyGreaterEqualsExpr.create(this.key, this.value, this);
1092
+ }
1093
+ return this.negated;
1094
+ }
1095
+ }
1096
+ export class ContextKeySmallerEqualsExpr {
1097
+ key;
1098
+ value;
1099
+ negated;
1100
+ static create(key, _value, negated = null) {
1101
+ return withFloatOrStr(_value, (value) => new ContextKeySmallerEqualsExpr(key, value, negated));
1102
+ }
1103
+ type = ContextKeyExprType.SmallerEquals;
1104
+ constructor(key, value, negated) {
1105
+ this.key = key;
1106
+ this.value = value;
1107
+ this.negated = negated;
1108
+ }
1109
+ cmp(other) {
1110
+ if (other.type !== this.type) {
1111
+ return this.type - other.type;
1112
+ }
1113
+ return cmp2(this.key, this.value, other.key, other.value);
1114
+ }
1115
+ equals(other) {
1116
+ if (other.type === this.type) {
1117
+ return (this.key === other.key && this.value === other.value);
1118
+ }
1119
+ return false;
1120
+ }
1121
+ substituteConstants() {
1122
+ return this;
1123
+ }
1124
+ evaluate(context) {
1125
+ if (typeof this.value === 'string') {
1126
+ return false;
1127
+ }
1128
+ return (parseFloat(context.getValue(this.key)) <= this.value);
1129
+ }
1130
+ serialize() {
1131
+ return `${this.key} <= ${this.value}`;
1132
+ }
1133
+ keys() {
1134
+ return [this.key];
1135
+ }
1136
+ map(mapFnc) {
1137
+ return mapFnc.mapSmallerEquals(this.key, this.value);
1138
+ }
1139
+ negate() {
1140
+ if (!this.negated) {
1141
+ this.negated = ContextKeyGreaterExpr.create(this.key, this.value, this);
1142
+ }
1143
+ return this.negated;
1144
+ }
1145
+ }
1146
+ export class ContextKeyRegexExpr {
1147
+ key;
1148
+ regexp;
1149
+ static create(key, regexp) {
1150
+ return new ContextKeyRegexExpr(key, regexp);
1151
+ }
1152
+ type = ContextKeyExprType.Regex;
1153
+ negated = null;
1154
+ constructor(key, regexp) {
1155
+ this.key = key;
1156
+ this.regexp = regexp;
1157
+ //
1158
+ }
1159
+ cmp(other) {
1160
+ if (other.type !== this.type) {
1161
+ return this.type - other.type;
1162
+ }
1163
+ if (this.key < other.key) {
1164
+ return -1;
1165
+ }
1166
+ if (this.key > other.key) {
1167
+ return 1;
1168
+ }
1169
+ const thisSource = this.regexp ? this.regexp.source : '';
1170
+ const otherSource = other.regexp ? other.regexp.source : '';
1171
+ if (thisSource < otherSource) {
1172
+ return -1;
1173
+ }
1174
+ if (thisSource > otherSource) {
1175
+ return 1;
1176
+ }
1177
+ return 0;
1178
+ }
1179
+ equals(other) {
1180
+ if (other.type === this.type) {
1181
+ const thisSource = this.regexp ? this.regexp.source : '';
1182
+ const otherSource = other.regexp ? other.regexp.source : '';
1183
+ return (this.key === other.key && thisSource === otherSource);
1184
+ }
1185
+ return false;
1186
+ }
1187
+ substituteConstants() {
1188
+ return this;
1189
+ }
1190
+ evaluate(context) {
1191
+ const value = context.getValue(this.key);
1192
+ return this.regexp ? this.regexp.test(value) : false;
1193
+ }
1194
+ serialize() {
1195
+ const value = this.regexp
1196
+ ? `/${this.regexp.source}/${this.regexp.flags}`
1197
+ : '/invalid/';
1198
+ return `${this.key} =~ ${value}`;
1199
+ }
1200
+ keys() {
1201
+ return [this.key];
1202
+ }
1203
+ map(mapFnc) {
1204
+ return mapFnc.mapRegex(this.key, this.regexp);
1205
+ }
1206
+ negate() {
1207
+ if (!this.negated) {
1208
+ this.negated = ContextKeyNotRegexExpr.create(this);
1209
+ }
1210
+ return this.negated;
1211
+ }
1212
+ }
1213
+ export class ContextKeyNotRegexExpr {
1214
+ _actual;
1215
+ static create(actual) {
1216
+ return new ContextKeyNotRegexExpr(actual);
1217
+ }
1218
+ type = ContextKeyExprType.NotRegex;
1219
+ constructor(_actual) {
1220
+ this._actual = _actual;
1221
+ //
1222
+ }
1223
+ cmp(other) {
1224
+ if (other.type !== this.type) {
1225
+ return this.type - other.type;
1226
+ }
1227
+ return this._actual.cmp(other._actual);
1228
+ }
1229
+ equals(other) {
1230
+ if (other.type === this.type) {
1231
+ return this._actual.equals(other._actual);
1232
+ }
1233
+ return false;
1234
+ }
1235
+ substituteConstants() {
1236
+ return this;
1237
+ }
1238
+ evaluate(context) {
1239
+ return !this._actual.evaluate(context);
1240
+ }
1241
+ serialize() {
1242
+ return `!(${this._actual.serialize()})`;
1243
+ }
1244
+ keys() {
1245
+ return this._actual.keys();
1246
+ }
1247
+ map(mapFnc) {
1248
+ return new ContextKeyNotRegexExpr(this._actual.map(mapFnc));
1249
+ }
1250
+ negate() {
1251
+ return this._actual;
1252
+ }
1253
+ }
1254
+ /**
1255
+ * @returns the same instance if nothing changed.
1256
+ */
1257
+ function eliminateConstantsInArray(arr) {
1258
+ // Allocate array only if there is a difference
1259
+ let newArr = null;
1260
+ for (let i = 0, len = arr.length; i < len; i++) {
1261
+ const newExpr = arr[i].substituteConstants();
1262
+ if (arr[i] !== newExpr) {
1263
+ // something has changed!
1264
+ // allocate array on first difference
1265
+ if (newArr === null) {
1266
+ newArr = [];
1267
+ for (let j = 0; j < i; j++) {
1268
+ newArr[j] = arr[j];
1269
+ }
1270
+ }
1271
+ }
1272
+ if (newArr !== null) {
1273
+ newArr[i] = newExpr;
1274
+ }
1275
+ }
1276
+ if (newArr === null) {
1277
+ return arr;
1278
+ }
1279
+ return newArr;
1280
+ }
1281
+ export class ContextKeyAndExpr {
1282
+ expr;
1283
+ negated;
1284
+ static create(_expr, negated, extraRedundantCheck) {
1285
+ return ContextKeyAndExpr._normalizeArr(_expr, negated, extraRedundantCheck);
1286
+ }
1287
+ type = ContextKeyExprType.And;
1288
+ constructor(expr, negated) {
1289
+ this.expr = expr;
1290
+ this.negated = negated;
1291
+ }
1292
+ cmp(other) {
1293
+ if (other.type !== this.type) {
1294
+ return this.type - other.type;
1295
+ }
1296
+ if (this.expr.length < other.expr.length) {
1297
+ return -1;
1298
+ }
1299
+ if (this.expr.length > other.expr.length) {
1300
+ return 1;
1301
+ }
1302
+ for (let i = 0, len = this.expr.length; i < len; i++) {
1303
+ const r = cmp(this.expr[i], other.expr[i]);
1304
+ if (r !== 0) {
1305
+ return r;
1306
+ }
1307
+ }
1308
+ return 0;
1309
+ }
1310
+ equals(other) {
1311
+ if (other.type === this.type) {
1312
+ if (this.expr.length !== other.expr.length) {
1313
+ return false;
1314
+ }
1315
+ for (let i = 0, len = this.expr.length; i < len; i++) {
1316
+ if (!this.expr[i].equals(other.expr[i])) {
1317
+ return false;
1318
+ }
1319
+ }
1320
+ return true;
1321
+ }
1322
+ return false;
1323
+ }
1324
+ substituteConstants() {
1325
+ const exprArr = eliminateConstantsInArray(this.expr);
1326
+ if (exprArr === this.expr) {
1327
+ // no change
1328
+ return this;
1329
+ }
1330
+ return ContextKeyAndExpr.create(exprArr, this.negated, false);
1331
+ }
1332
+ evaluate(context) {
1333
+ for (let i = 0, len = this.expr.length; i < len; i++) {
1334
+ if (!this.expr[i].evaluate(context)) {
1335
+ return false;
1336
+ }
1337
+ }
1338
+ return true;
1339
+ }
1340
+ static _normalizeArr(arr, negated, extraRedundantCheck) {
1341
+ const expr = [];
1342
+ let hasTrue = false;
1343
+ for (const e of arr) {
1344
+ if (!e) {
1345
+ continue;
1346
+ }
1347
+ if (e.type === ContextKeyExprType.True) {
1348
+ // anything && true ==> anything
1349
+ hasTrue = true;
1350
+ continue;
1351
+ }
1352
+ if (e.type === ContextKeyExprType.False) {
1353
+ // anything && false ==> false
1354
+ return ContextKeyFalseExpr.INSTANCE;
1355
+ }
1356
+ if (e.type === ContextKeyExprType.And) {
1357
+ expr.push(...e.expr);
1358
+ continue;
1359
+ }
1360
+ expr.push(e);
1361
+ }
1362
+ if (expr.length === 0 && hasTrue) {
1363
+ return ContextKeyTrueExpr.INSTANCE;
1364
+ }
1365
+ if (expr.length === 0) {
1366
+ return undefined;
1367
+ }
1368
+ if (expr.length === 1) {
1369
+ return expr[0];
1370
+ }
1371
+ expr.sort(cmp);
1372
+ // eliminate duplicate terms
1373
+ for (let i = 1; i < expr.length; i++) {
1374
+ if (expr[i - 1].equals(expr[i])) {
1375
+ expr.splice(i, 1);
1376
+ i--;
1377
+ }
1378
+ }
1379
+ if (expr.length === 1) {
1380
+ return expr[0];
1381
+ }
1382
+ // We must distribute any OR expression because we don't support parens
1383
+ // OR extensions will be at the end (due to sorting rules)
1384
+ while (expr.length > 1) {
1385
+ const lastElement = expr[expr.length - 1];
1386
+ if (lastElement.type !== ContextKeyExprType.Or) {
1387
+ break;
1388
+ }
1389
+ // pop the last element
1390
+ expr.pop();
1391
+ // pop the second to last element
1392
+ const secondToLastElement = expr.pop();
1393
+ const isFinished = (expr.length === 0);
1394
+ // distribute `lastElement` over `secondToLastElement`
1395
+ const resultElement = ContextKeyOrExpr.create(lastElement.expr.map(el => ContextKeyAndExpr.create([el, secondToLastElement], null, extraRedundantCheck)), null, isFinished);
1396
+ if (resultElement) {
1397
+ expr.push(resultElement);
1398
+ expr.sort(cmp);
1399
+ }
1400
+ }
1401
+ if (expr.length === 1) {
1402
+ return expr[0];
1403
+ }
1404
+ // resolve false AND expressions
1405
+ if (extraRedundantCheck) {
1406
+ for (let i = 0; i < expr.length; i++) {
1407
+ for (let j = i + 1; j < expr.length; j++) {
1408
+ if (expr[i].negate().equals(expr[j])) {
1409
+ // A && !A case
1410
+ return ContextKeyFalseExpr.INSTANCE;
1411
+ }
1412
+ }
1413
+ }
1414
+ if (expr.length === 1) {
1415
+ return expr[0];
1416
+ }
1417
+ }
1418
+ return new ContextKeyAndExpr(expr, negated);
1419
+ }
1420
+ serialize() {
1421
+ return this.expr.map(e => e.serialize()).join(' && ');
1422
+ }
1423
+ keys() {
1424
+ const result = [];
1425
+ for (const expr of this.expr) {
1426
+ result.push(...expr.keys());
1427
+ }
1428
+ return result;
1429
+ }
1430
+ map(mapFnc) {
1431
+ return new ContextKeyAndExpr(this.expr.map(expr => expr.map(mapFnc)), null);
1432
+ }
1433
+ negate() {
1434
+ if (!this.negated) {
1435
+ const result = [];
1436
+ for (const expr of this.expr) {
1437
+ result.push(expr.negate());
1438
+ }
1439
+ this.negated = ContextKeyOrExpr.create(result, this, true);
1440
+ }
1441
+ return this.negated;
1442
+ }
1443
+ }
1444
+ export class ContextKeyOrExpr {
1445
+ expr;
1446
+ negated;
1447
+ static create(_expr, negated, extraRedundantCheck) {
1448
+ return ContextKeyOrExpr._normalizeArr(_expr, negated, extraRedundantCheck);
1449
+ }
1450
+ type = ContextKeyExprType.Or;
1451
+ constructor(expr, negated) {
1452
+ this.expr = expr;
1453
+ this.negated = negated;
1454
+ }
1455
+ cmp(other) {
1456
+ if (other.type !== this.type) {
1457
+ return this.type - other.type;
1458
+ }
1459
+ if (this.expr.length < other.expr.length) {
1460
+ return -1;
1461
+ }
1462
+ if (this.expr.length > other.expr.length) {
1463
+ return 1;
1464
+ }
1465
+ for (let i = 0, len = this.expr.length; i < len; i++) {
1466
+ const r = cmp(this.expr[i], other.expr[i]);
1467
+ if (r !== 0) {
1468
+ return r;
1469
+ }
1470
+ }
1471
+ return 0;
1472
+ }
1473
+ equals(other) {
1474
+ if (other.type === this.type) {
1475
+ if (this.expr.length !== other.expr.length) {
1476
+ return false;
1477
+ }
1478
+ for (let i = 0, len = this.expr.length; i < len; i++) {
1479
+ if (!this.expr[i].equals(other.expr[i])) {
1480
+ return false;
1481
+ }
1482
+ }
1483
+ return true;
1484
+ }
1485
+ return false;
1486
+ }
1487
+ substituteConstants() {
1488
+ const exprArr = eliminateConstantsInArray(this.expr);
1489
+ if (exprArr === this.expr) {
1490
+ // no change
1491
+ return this;
1492
+ }
1493
+ return ContextKeyOrExpr.create(exprArr, this.negated, false);
1494
+ }
1495
+ evaluate(context) {
1496
+ for (let i = 0, len = this.expr.length; i < len; i++) {
1497
+ if (this.expr[i].evaluate(context)) {
1498
+ return true;
1499
+ }
1500
+ }
1501
+ return false;
1502
+ }
1503
+ static _normalizeArr(arr, negated, extraRedundantCheck) {
1504
+ let expr = [];
1505
+ let hasFalse = false;
1506
+ if (arr) {
1507
+ for (let i = 0, len = arr.length; i < len; i++) {
1508
+ const e = arr[i];
1509
+ if (!e) {
1510
+ continue;
1511
+ }
1512
+ if (e.type === ContextKeyExprType.False) {
1513
+ // anything || false ==> anything
1514
+ hasFalse = true;
1515
+ continue;
1516
+ }
1517
+ if (e.type === ContextKeyExprType.True) {
1518
+ // anything || true ==> true
1519
+ return ContextKeyTrueExpr.INSTANCE;
1520
+ }
1521
+ if (e.type === ContextKeyExprType.Or) {
1522
+ expr = expr.concat(e.expr);
1523
+ continue;
1524
+ }
1525
+ expr.push(e);
1526
+ }
1527
+ if (expr.length === 0 && hasFalse) {
1528
+ return ContextKeyFalseExpr.INSTANCE;
1529
+ }
1530
+ expr.sort(cmp);
1531
+ }
1532
+ if (expr.length === 0) {
1533
+ return undefined;
1534
+ }
1535
+ if (expr.length === 1) {
1536
+ return expr[0];
1537
+ }
1538
+ // eliminate duplicate terms
1539
+ for (let i = 1; i < expr.length; i++) {
1540
+ if (expr[i - 1].equals(expr[i])) {
1541
+ expr.splice(i, 1);
1542
+ i--;
1543
+ }
1544
+ }
1545
+ if (expr.length === 1) {
1546
+ return expr[0];
1547
+ }
1548
+ // resolve true OR expressions
1549
+ if (extraRedundantCheck) {
1550
+ for (let i = 0; i < expr.length; i++) {
1551
+ for (let j = i + 1; j < expr.length; j++) {
1552
+ if (expr[i].negate().equals(expr[j])) {
1553
+ // A || !A case
1554
+ return ContextKeyTrueExpr.INSTANCE;
1555
+ }
1556
+ }
1557
+ }
1558
+ if (expr.length === 1) {
1559
+ return expr[0];
1560
+ }
1561
+ }
1562
+ return new ContextKeyOrExpr(expr, negated);
1563
+ }
1564
+ serialize() {
1565
+ return this.expr.map(e => e.serialize()).join(' || ');
1566
+ }
1567
+ keys() {
1568
+ const result = [];
1569
+ for (const expr of this.expr) {
1570
+ result.push(...expr.keys());
1571
+ }
1572
+ return result;
1573
+ }
1574
+ map(mapFnc) {
1575
+ return new ContextKeyOrExpr(this.expr.map(expr => expr.map(mapFnc)), null);
1576
+ }
1577
+ negate() {
1578
+ if (!this.negated) {
1579
+ const result = [];
1580
+ for (const expr of this.expr) {
1581
+ result.push(expr.negate());
1582
+ }
1583
+ // We don't support parens, so here we distribute the AND over the OR terminals
1584
+ // We always take the first 2 AND pairs and distribute them
1585
+ while (result.length > 1) {
1586
+ const LEFT = result.shift();
1587
+ const RIGHT = result.shift();
1588
+ const all = [];
1589
+ for (const left of getTerminals(LEFT)) {
1590
+ for (const right of getTerminals(RIGHT)) {
1591
+ all.push(ContextKeyAndExpr.create([left, right], null, false));
1592
+ }
1593
+ }
1594
+ result.unshift(ContextKeyOrExpr.create(all, null, false));
1595
+ }
1596
+ this.negated = ContextKeyOrExpr.create(result, this, true);
1597
+ }
1598
+ return this.negated;
1599
+ }
1600
+ }
1601
+ export class RawContextKey extends ContextKeyDefinedExpr {
1602
+ static _info = [];
1603
+ static all() {
1604
+ return RawContextKey._info.values();
1605
+ }
1606
+ _defaultValue;
1607
+ constructor(key, defaultValue, metaOrHide) {
1608
+ super(key, null);
1609
+ this._defaultValue = defaultValue;
1610
+ // collect all context keys into a central place
1611
+ if (typeof metaOrHide === 'object') {
1612
+ RawContextKey._info.push({ ...metaOrHide, key });
1613
+ }
1614
+ else if (metaOrHide !== true) {
1615
+ RawContextKey._info.push({ key, description: metaOrHide, type: defaultValue !== null && defaultValue !== undefined ? typeof defaultValue : undefined });
1616
+ }
1617
+ }
1618
+ bindTo(target) {
1619
+ return target.createKey(this.key, this._defaultValue);
1620
+ }
1621
+ getValue(target) {
1622
+ return target.getContextKeyValue(this.key);
1623
+ }
1624
+ toNegated() {
1625
+ return this.negate();
1626
+ }
1627
+ isEqualTo(value) {
1628
+ return ContextKeyEqualsExpr.create(this.key, value);
1629
+ }
1630
+ notEqualsTo(value) {
1631
+ return ContextKeyNotEqualsExpr.create(this.key, value);
1632
+ }
1633
+ greater(value) {
1634
+ return ContextKeyGreaterExpr.create(this.key, value);
1635
+ }
1636
+ }
1637
+ export const IContextKeyService = createDecorator('contextKeyService');
1638
+ function cmp1(key1, key2) {
1639
+ if (key1 < key2) {
1640
+ return -1;
1641
+ }
1642
+ if (key1 > key2) {
1643
+ return 1;
1644
+ }
1645
+ return 0;
1646
+ }
1647
+ function cmp2(key1, value1, key2, value2) {
1648
+ if (key1 < key2) {
1649
+ return -1;
1650
+ }
1651
+ if (key1 > key2) {
1652
+ return 1;
1653
+ }
1654
+ if (value1 < value2) {
1655
+ return -1;
1656
+ }
1657
+ if (value1 > value2) {
1658
+ return 1;
1659
+ }
1660
+ return 0;
1661
+ }
1662
+ /**
1663
+ * Returns true if it is provable `p` implies `q`.
1664
+ */
1665
+ export function implies(p, q) {
1666
+ if (p.type === ContextKeyExprType.False || q.type === ContextKeyExprType.True) {
1667
+ // false implies anything
1668
+ // anything implies true
1669
+ return true;
1670
+ }
1671
+ if (p.type === ContextKeyExprType.Or) {
1672
+ if (q.type === ContextKeyExprType.Or) {
1673
+ // `a || b || c` can only imply something like `a || b || c || d`
1674
+ return allElementsIncluded(p.expr, q.expr);
1675
+ }
1676
+ return false;
1677
+ }
1678
+ if (q.type === ContextKeyExprType.Or) {
1679
+ for (const element of q.expr) {
1680
+ if (implies(p, element)) {
1681
+ return true;
1682
+ }
1683
+ }
1684
+ return false;
1685
+ }
1686
+ if (p.type === ContextKeyExprType.And) {
1687
+ if (q.type === ContextKeyExprType.And) {
1688
+ // `a && b && c` implies `a && c`
1689
+ return allElementsIncluded(q.expr, p.expr);
1690
+ }
1691
+ for (const element of p.expr) {
1692
+ if (implies(element, q)) {
1693
+ return true;
1694
+ }
1695
+ }
1696
+ return false;
1697
+ }
1698
+ return p.equals(q);
1699
+ }
1700
+ /**
1701
+ * Returns true if all elements in `p` are also present in `q`.
1702
+ * The two arrays are assumed to be sorted
1703
+ */
1704
+ function allElementsIncluded(p, q) {
1705
+ let pIndex = 0;
1706
+ let qIndex = 0;
1707
+ while (pIndex < p.length && qIndex < q.length) {
1708
+ const cmp = p[pIndex].cmp(q[qIndex]);
1709
+ if (cmp < 0) {
1710
+ // an element from `p` is missing from `q`
1711
+ return false;
1712
+ }
1713
+ else if (cmp === 0) {
1714
+ pIndex++;
1715
+ qIndex++;
1716
+ }
1717
+ else {
1718
+ qIndex++;
1719
+ }
1720
+ }
1721
+ return (pIndex === p.length);
1722
+ }
1723
+ function getTerminals(node) {
1724
+ if (node.type === ContextKeyExprType.Or) {
1725
+ return node.expr;
1726
+ }
1727
+ return [node];
1728
+ }
1729
+ //# sourceMappingURL=contextkey.js.map