contextkey.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 { isLinux, isMacintosh, isWeb, isWindows, userAgent } from '../../../base/common/platform.js';
  6. import { isFalsyOrWhitespace } from '../../../base/common/strings.js';
  7. import { createDecorator } from '../../instantiation/common/instantiation.js';
  8. let _userAgent = userAgent || '';
  9. const CONSTANT_VALUES = new Map();
  10. CONSTANT_VALUES.set('false', false);
  11. CONSTANT_VALUES.set('true', true);
  12. CONSTANT_VALUES.set('isMac', isMacintosh);
  13. CONSTANT_VALUES.set('isLinux', isLinux);
  14. CONSTANT_VALUES.set('isWindows', isWindows);
  15. CONSTANT_VALUES.set('isWeb', isWeb);
  16. CONSTANT_VALUES.set('isMacNative', isMacintosh && !isWeb);
  17. CONSTANT_VALUES.set('isEdge', _userAgent.indexOf('Edg/') >= 0);
  18. CONSTANT_VALUES.set('isFirefox', _userAgent.indexOf('Firefox') >= 0);
  19. CONSTANT_VALUES.set('isChrome', _userAgent.indexOf('Chrome') >= 0);
  20. CONSTANT_VALUES.set('isSafari', _userAgent.indexOf('Safari') >= 0);
  21. const hasOwnProperty = Object.prototype.hasOwnProperty;
  22. export class ContextKeyExpr {
  23. static has(key) {
  24. return ContextKeyDefinedExpr.create(key);
  25. }
  26. static equals(key, value) {
  27. return ContextKeyEqualsExpr.create(key, value);
  28. }
  29. static regex(key, value) {
  30. return ContextKeyRegexExpr.create(key, value);
  31. }
  32. static not(key) {
  33. return ContextKeyNotExpr.create(key);
  34. }
  35. static and(...expr) {
  36. return ContextKeyAndExpr.create(expr, null);
  37. }
  38. static or(...expr) {
  39. return ContextKeyOrExpr.create(expr, null, true);
  40. }
  41. static deserialize(serialized, strict = false) {
  42. if (!serialized) {
  43. return undefined;
  44. }
  45. return this._deserializeOrExpression(serialized, strict);
  46. }
  47. static _deserializeOrExpression(serialized, strict) {
  48. let pieces = serialized.split('||');
  49. return ContextKeyOrExpr.create(pieces.map(p => this._deserializeAndExpression(p, strict)), null, true);
  50. }
  51. static _deserializeAndExpression(serialized, strict) {
  52. let pieces = serialized.split('&&');
  53. return ContextKeyAndExpr.create(pieces.map(p => this._deserializeOne(p, strict)), null);
  54. }
  55. static _deserializeOne(serializedOne, strict) {
  56. serializedOne = serializedOne.trim();
  57. if (serializedOne.indexOf('!=') >= 0) {
  58. let pieces = serializedOne.split('!=');
  59. return ContextKeyNotEqualsExpr.create(pieces[0].trim(), this._deserializeValue(pieces[1], strict));
  60. }
  61. if (serializedOne.indexOf('==') >= 0) {
  62. let pieces = serializedOne.split('==');
  63. return ContextKeyEqualsExpr.create(pieces[0].trim(), this._deserializeValue(pieces[1], strict));
  64. }
  65. if (serializedOne.indexOf('=~') >= 0) {
  66. let pieces = serializedOne.split('=~');
  67. return ContextKeyRegexExpr.create(pieces[0].trim(), this._deserializeRegexValue(pieces[1], strict));
  68. }
  69. if (serializedOne.indexOf(' in ') >= 0) {
  70. let pieces = serializedOne.split(' in ');
  71. return ContextKeyInExpr.create(pieces[0].trim(), pieces[1].trim());
  72. }
  73. if (/^[^<=>]+>=[^<=>]+$/.test(serializedOne)) {
  74. const pieces = serializedOne.split('>=');
  75. return ContextKeyGreaterEqualsExpr.create(pieces[0].trim(), pieces[1].trim());
  76. }
  77. if (/^[^<=>]+>[^<=>]+$/.test(serializedOne)) {
  78. const pieces = serializedOne.split('>');
  79. return ContextKeyGreaterExpr.create(pieces[0].trim(), pieces[1].trim());
  80. }
  81. if (/^[^<=>]+<=[^<=>]+$/.test(serializedOne)) {
  82. const pieces = serializedOne.split('<=');
  83. return ContextKeySmallerEqualsExpr.create(pieces[0].trim(), pieces[1].trim());
  84. }
  85. if (/^[^<=>]+<[^<=>]+$/.test(serializedOne)) {
  86. const pieces = serializedOne.split('<');
  87. return ContextKeySmallerExpr.create(pieces[0].trim(), pieces[1].trim());
  88. }
  89. if (/^\!\s*/.test(serializedOne)) {
  90. return ContextKeyNotExpr.create(serializedOne.substr(1).trim());
  91. }
  92. return ContextKeyDefinedExpr.create(serializedOne);
  93. }
  94. static _deserializeValue(serializedValue, strict) {
  95. serializedValue = serializedValue.trim();
  96. if (serializedValue === 'true') {
  97. return true;
  98. }
  99. if (serializedValue === 'false') {
  100. return false;
  101. }
  102. let m = /^'([^']*)'$/.exec(serializedValue);
  103. if (m) {
  104. return m[1].trim();
  105. }
  106. return serializedValue;
  107. }
  108. static _deserializeRegexValue(serializedValue, strict) {
  109. if (isFalsyOrWhitespace(serializedValue)) {
  110. if (strict) {
  111. throw new Error('missing regexp-value for =~-expression');
  112. }
  113. else {
  114. console.warn('missing regexp-value for =~-expression');
  115. }
  116. return null;
  117. }
  118. let start = serializedValue.indexOf('/');
  119. let end = serializedValue.lastIndexOf('/');
  120. if (start === end || start < 0 /* || to < 0 */) {
  121. if (strict) {
  122. throw new Error(`bad regexp-value '${serializedValue}', missing /-enclosure`);
  123. }
  124. else {
  125. console.warn(`bad regexp-value '${serializedValue}', missing /-enclosure`);
  126. }
  127. return null;
  128. }
  129. let value = serializedValue.slice(start + 1, end);
  130. let caseIgnoreFlag = serializedValue[end + 1] === 'i' ? 'i' : '';
  131. try {
  132. return new RegExp(value, caseIgnoreFlag);
  133. }
  134. catch (e) {
  135. if (strict) {
  136. throw new Error(`bad regexp-value '${serializedValue}', parse error: ${e}`);
  137. }
  138. else {
  139. console.warn(`bad regexp-value '${serializedValue}', parse error: ${e}`);
  140. }
  141. return null;
  142. }
  143. }
  144. }
  145. export function expressionsAreEqualWithConstantSubstitution(a, b) {
  146. const aExpr = a ? a.substituteConstants() : undefined;
  147. const bExpr = b ? b.substituteConstants() : undefined;
  148. if (!aExpr && !bExpr) {
  149. return true;
  150. }
  151. if (!aExpr || !bExpr) {
  152. return false;
  153. }
  154. return aExpr.equals(bExpr);
  155. }
  156. function cmp(a, b) {
  157. return a.cmp(b);
  158. }
  159. export class ContextKeyFalseExpr {
  160. constructor() {
  161. this.type = 0 /* False */;
  162. }
  163. cmp(other) {
  164. return this.type - other.type;
  165. }
  166. equals(other) {
  167. return (other.type === this.type);
  168. }
  169. substituteConstants() {
  170. return this;
  171. }
  172. evaluate(context) {
  173. return false;
  174. }
  175. serialize() {
  176. return 'false';
  177. }
  178. keys() {
  179. return [];
  180. }
  181. negate() {
  182. return ContextKeyTrueExpr.INSTANCE;
  183. }
  184. }
  185. ContextKeyFalseExpr.INSTANCE = new ContextKeyFalseExpr();
  186. export class ContextKeyTrueExpr {
  187. constructor() {
  188. this.type = 1 /* True */;
  189. }
  190. cmp(other) {
  191. return this.type - other.type;
  192. }
  193. equals(other) {
  194. return (other.type === this.type);
  195. }
  196. substituteConstants() {
  197. return this;
  198. }
  199. evaluate(context) {
  200. return true;
  201. }
  202. serialize() {
  203. return 'true';
  204. }
  205. keys() {
  206. return [];
  207. }
  208. negate() {
  209. return ContextKeyFalseExpr.INSTANCE;
  210. }
  211. }
  212. ContextKeyTrueExpr.INSTANCE = new ContextKeyTrueExpr();
  213. export class ContextKeyDefinedExpr {
  214. constructor(key, negated) {
  215. this.key = key;
  216. this.negated = negated;
  217. this.type = 2 /* Defined */;
  218. }
  219. static create(key, negated = null) {
  220. const constantValue = CONSTANT_VALUES.get(key);
  221. if (typeof constantValue === 'boolean') {
  222. return constantValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
  223. }
  224. return new ContextKeyDefinedExpr(key, negated);
  225. }
  226. cmp(other) {
  227. if (other.type !== this.type) {
  228. return this.type - other.type;
  229. }
  230. return cmp1(this.key, other.key);
  231. }
  232. equals(other) {
  233. if (other.type === this.type) {
  234. return (this.key === other.key);
  235. }
  236. return false;
  237. }
  238. substituteConstants() {
  239. const constantValue = CONSTANT_VALUES.get(this.key);
  240. if (typeof constantValue === 'boolean') {
  241. return constantValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE;
  242. }
  243. return this;
  244. }
  245. evaluate(context) {
  246. return (!!context.getValue(this.key));
  247. }
  248. serialize() {
  249. return this.key;
  250. }
  251. keys() {
  252. return [this.key];
  253. }
  254. negate() {
  255. if (!this.negated) {
  256. this.negated = ContextKeyNotExpr.create(this.key, this);
  257. }
  258. return this.negated;
  259. }
  260. }
  261. export class ContextKeyEqualsExpr {
  262. constructor(key, value, negated) {
  263. this.key = key;
  264. this.value = value;
  265. this.negated = negated;
  266. this.type = 4 /* Equals */;
  267. }
  268. static create(key, value, negated = null) {
  269. if (typeof value === 'boolean') {
  270. return (value ? ContextKeyDefinedExpr.create(key, negated) : ContextKeyNotExpr.create(key, negated));
  271. }
  272. const constantValue = CONSTANT_VALUES.get(key);
  273. if (typeof constantValue === 'boolean') {
  274. const trueValue = constantValue ? 'true' : 'false';
  275. return (value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
  276. }
  277. return new ContextKeyEqualsExpr(key, value, negated);
  278. }
  279. cmp(other) {
  280. if (other.type !== this.type) {
  281. return this.type - other.type;
  282. }
  283. return cmp2(this.key, this.value, other.key, other.value);
  284. }
  285. equals(other) {
  286. if (other.type === this.type) {
  287. return (this.key === other.key && this.value === other.value);
  288. }
  289. return false;
  290. }
  291. substituteConstants() {
  292. const constantValue = CONSTANT_VALUES.get(this.key);
  293. if (typeof constantValue === 'boolean') {
  294. const trueValue = constantValue ? 'true' : 'false';
  295. return (this.value === trueValue ? ContextKeyTrueExpr.INSTANCE : ContextKeyFalseExpr.INSTANCE);
  296. }
  297. return this;
  298. }
  299. evaluate(context) {
  300. // Intentional ==
  301. // eslint-disable-next-line eqeqeq
  302. return (context.getValue(this.key) == this.value);
  303. }
  304. serialize() {
  305. return `${this.key} == '${this.value}'`;
  306. }
  307. keys() {
  308. return [this.key];
  309. }
  310. negate() {
  311. if (!this.negated) {
  312. this.negated = ContextKeyNotEqualsExpr.create(this.key, this.value, this);
  313. }
  314. return this.negated;
  315. }
  316. }
  317. export class ContextKeyInExpr {
  318. constructor(key, valueKey) {
  319. this.key = key;
  320. this.valueKey = valueKey;
  321. this.type = 10 /* In */;
  322. this.negated = null;
  323. }
  324. static create(key, valueKey) {
  325. return new ContextKeyInExpr(key, valueKey);
  326. }
  327. cmp(other) {
  328. if (other.type !== this.type) {
  329. return this.type - other.type;
  330. }
  331. return cmp2(this.key, this.valueKey, other.key, other.valueKey);
  332. }
  333. equals(other) {
  334. if (other.type === this.type) {
  335. return (this.key === other.key && this.valueKey === other.valueKey);
  336. }
  337. return false;
  338. }
  339. substituteConstants() {
  340. return this;
  341. }
  342. evaluate(context) {
  343. const source = context.getValue(this.valueKey);
  344. const item = context.getValue(this.key);
  345. if (Array.isArray(source)) {
  346. return (source.indexOf(item) >= 0);
  347. }
  348. if (typeof item === 'string' && typeof source === 'object' && source !== null) {
  349. return hasOwnProperty.call(source, item);
  350. }
  351. return false;
  352. }
  353. serialize() {
  354. return `${this.key} in '${this.valueKey}'`;
  355. }
  356. keys() {
  357. return [this.key, this.valueKey];
  358. }
  359. negate() {
  360. if (!this.negated) {
  361. this.negated = ContextKeyNotInExpr.create(this);
  362. }
  363. return this.negated;
  364. }
  365. }
  366. export class ContextKeyNotInExpr {
  367. constructor(_actual) {
  368. this._actual = _actual;
  369. this.type = 11 /* NotIn */;
  370. //
  371. }
  372. static create(actual) {
  373. return new ContextKeyNotInExpr(actual);
  374. }
  375. cmp(other) {
  376. if (other.type !== this.type) {
  377. return this.type - other.type;
  378. }
  379. return this._actual.cmp(other._actual);
  380. }
  381. equals(other) {
  382. if (other.type === this.type) {
  383. return this._actual.equals(other._actual);
  384. }
  385. return false;
  386. }
  387. substituteConstants() {
  388. return this;
  389. }
  390. evaluate(context) {
  391. return !this._actual.evaluate(context);
  392. }
  393. serialize() {
  394. throw new Error('Method not implemented.');
  395. }
  396. keys() {
  397. return this._actual.keys();
  398. }
  399. negate() {
  400. return this._actual;
  401. }
  402. }
  403. export class ContextKeyNotEqualsExpr {
  404. constructor(key, value, negated) {
  405. this.key = key;
  406. this.value = value;
  407. this.negated = negated;
  408. this.type = 5 /* NotEquals */;
  409. }
  410. static create(key, value, negated = null) {
  411. if (typeof value === 'boolean') {
  412. if (value) {
  413. return ContextKeyNotExpr.create(key, negated);
  414. }
  415. return ContextKeyDefinedExpr.create(key, negated);
  416. }
  417. const constantValue = CONSTANT_VALUES.get(key);
  418. if (typeof constantValue === 'boolean') {
  419. const falseValue = constantValue ? 'true' : 'false';
  420. return (value === falseValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
  421. }
  422. return new ContextKeyNotEqualsExpr(key, value, negated);
  423. }
  424. cmp(other) {
  425. if (other.type !== this.type) {
  426. return this.type - other.type;
  427. }
  428. return cmp2(this.key, this.value, other.key, other.value);
  429. }
  430. equals(other) {
  431. if (other.type === this.type) {
  432. return (this.key === other.key && this.value === other.value);
  433. }
  434. return false;
  435. }
  436. substituteConstants() {
  437. const constantValue = CONSTANT_VALUES.get(this.key);
  438. if (typeof constantValue === 'boolean') {
  439. const falseValue = constantValue ? 'true' : 'false';
  440. return (this.value === falseValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
  441. }
  442. return this;
  443. }
  444. evaluate(context) {
  445. // Intentional !=
  446. // eslint-disable-next-line eqeqeq
  447. return (context.getValue(this.key) != this.value);
  448. }
  449. serialize() {
  450. return `${this.key} != '${this.value}'`;
  451. }
  452. keys() {
  453. return [this.key];
  454. }
  455. negate() {
  456. if (!this.negated) {
  457. this.negated = ContextKeyEqualsExpr.create(this.key, this.value, this);
  458. }
  459. return this.negated;
  460. }
  461. }
  462. export class ContextKeyNotExpr {
  463. constructor(key, negated) {
  464. this.key = key;
  465. this.negated = negated;
  466. this.type = 3 /* Not */;
  467. }
  468. static create(key, negated = null) {
  469. const constantValue = CONSTANT_VALUES.get(key);
  470. if (typeof constantValue === 'boolean') {
  471. return (constantValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
  472. }
  473. return new ContextKeyNotExpr(key, negated);
  474. }
  475. cmp(other) {
  476. if (other.type !== this.type) {
  477. return this.type - other.type;
  478. }
  479. return cmp1(this.key, other.key);
  480. }
  481. equals(other) {
  482. if (other.type === this.type) {
  483. return (this.key === other.key);
  484. }
  485. return false;
  486. }
  487. substituteConstants() {
  488. const constantValue = CONSTANT_VALUES.get(this.key);
  489. if (typeof constantValue === 'boolean') {
  490. return (constantValue ? ContextKeyFalseExpr.INSTANCE : ContextKeyTrueExpr.INSTANCE);
  491. }
  492. return this;
  493. }
  494. evaluate(context) {
  495. return (!context.getValue(this.key));
  496. }
  497. serialize() {
  498. return `!${this.key}`;
  499. }
  500. keys() {
  501. return [this.key];
  502. }
  503. negate() {
  504. if (!this.negated) {
  505. this.negated = ContextKeyDefinedExpr.create(this.key, this);
  506. }
  507. return this.negated;
  508. }
  509. }
  510. function withFloatOrStr(value, callback) {
  511. if (typeof value === 'string') {
  512. const n = parseFloat(value);
  513. if (!isNaN(n)) {
  514. value = n;
  515. }
  516. }
  517. if (typeof value === 'string' || typeof value === 'number') {
  518. return callback(value);
  519. }
  520. return ContextKeyFalseExpr.INSTANCE;
  521. }
  522. export class ContextKeyGreaterExpr {
  523. constructor(key, value, negated) {
  524. this.key = key;
  525. this.value = value;
  526. this.negated = negated;
  527. this.type = 12 /* Greater */;
  528. }
  529. static create(key, _value, negated = null) {
  530. return withFloatOrStr(_value, (value) => new ContextKeyGreaterExpr(key, value, negated));
  531. }
  532. cmp(other) {
  533. if (other.type !== this.type) {
  534. return this.type - other.type;
  535. }
  536. return cmp2(this.key, this.value, other.key, other.value);
  537. }
  538. equals(other) {
  539. if (other.type === this.type) {
  540. return (this.key === other.key && this.value === other.value);
  541. }
  542. return false;
  543. }
  544. substituteConstants() {
  545. return this;
  546. }
  547. evaluate(context) {
  548. if (typeof this.value === 'string') {
  549. return false;
  550. }
  551. return (parseFloat(context.getValue(this.key)) > this.value);
  552. }
  553. serialize() {
  554. return `${this.key} > ${this.value}`;
  555. }
  556. keys() {
  557. return [this.key];
  558. }
  559. negate() {
  560. if (!this.negated) {
  561. this.negated = ContextKeySmallerEqualsExpr.create(this.key, this.value, this);
  562. }
  563. return this.negated;
  564. }
  565. }
  566. export class ContextKeyGreaterEqualsExpr {
  567. constructor(key, value, negated) {
  568. this.key = key;
  569. this.value = value;
  570. this.negated = negated;
  571. this.type = 13 /* GreaterEquals */;
  572. }
  573. static create(key, _value, negated = null) {
  574. return withFloatOrStr(_value, (value) => new ContextKeyGreaterEqualsExpr(key, value, negated));
  575. }
  576. cmp(other) {
  577. if (other.type !== this.type) {
  578. return this.type - other.type;
  579. }
  580. return cmp2(this.key, this.value, other.key, other.value);
  581. }
  582. equals(other) {
  583. if (other.type === this.type) {
  584. return (this.key === other.key && this.value === other.value);
  585. }
  586. return false;
  587. }
  588. substituteConstants() {
  589. return this;
  590. }
  591. evaluate(context) {
  592. if (typeof this.value === 'string') {
  593. return false;
  594. }
  595. return (parseFloat(context.getValue(this.key)) >= this.value);
  596. }
  597. serialize() {
  598. return `${this.key} >= ${this.value}`;
  599. }
  600. keys() {
  601. return [this.key];
  602. }
  603. negate() {
  604. if (!this.negated) {
  605. this.negated = ContextKeySmallerExpr.create(this.key, this.value, this);
  606. }
  607. return this.negated;
  608. }
  609. }
  610. export class ContextKeySmallerExpr {
  611. constructor(key, value, negated) {
  612. this.key = key;
  613. this.value = value;
  614. this.negated = negated;
  615. this.type = 14 /* Smaller */;
  616. }
  617. static create(key, _value, negated = null) {
  618. return withFloatOrStr(_value, (value) => new ContextKeySmallerExpr(key, value, negated));
  619. }
  620. cmp(other) {
  621. if (other.type !== this.type) {
  622. return this.type - other.type;
  623. }
  624. return cmp2(this.key, this.value, other.key, other.value);
  625. }
  626. equals(other) {
  627. if (other.type === this.type) {
  628. return (this.key === other.key && this.value === other.value);
  629. }
  630. return false;
  631. }
  632. substituteConstants() {
  633. return this;
  634. }
  635. evaluate(context) {
  636. if (typeof this.value === 'string') {
  637. return false;
  638. }
  639. return (parseFloat(context.getValue(this.key)) < this.value);
  640. }
  641. serialize() {
  642. return `${this.key} < ${this.value}`;
  643. }
  644. keys() {
  645. return [this.key];
  646. }
  647. negate() {
  648. if (!this.negated) {
  649. this.negated = ContextKeyGreaterEqualsExpr.create(this.key, this.value, this);
  650. }
  651. return this.negated;
  652. }
  653. }
  654. export class ContextKeySmallerEqualsExpr {
  655. constructor(key, value, negated) {
  656. this.key = key;
  657. this.value = value;
  658. this.negated = negated;
  659. this.type = 15 /* SmallerEquals */;
  660. }
  661. static create(key, _value, negated = null) {
  662. return withFloatOrStr(_value, (value) => new ContextKeySmallerEqualsExpr(key, value, negated));
  663. }
  664. cmp(other) {
  665. if (other.type !== this.type) {
  666. return this.type - other.type;
  667. }
  668. return cmp2(this.key, this.value, other.key, other.value);
  669. }
  670. equals(other) {
  671. if (other.type === this.type) {
  672. return (this.key === other.key && this.value === other.value);
  673. }
  674. return false;
  675. }
  676. substituteConstants() {
  677. return this;
  678. }
  679. evaluate(context) {
  680. if (typeof this.value === 'string') {
  681. return false;
  682. }
  683. return (parseFloat(context.getValue(this.key)) <= this.value);
  684. }
  685. serialize() {
  686. return `${this.key} <= ${this.value}`;
  687. }
  688. keys() {
  689. return [this.key];
  690. }
  691. negate() {
  692. if (!this.negated) {
  693. this.negated = ContextKeyGreaterExpr.create(this.key, this.value, this);
  694. }
  695. return this.negated;
  696. }
  697. }
  698. export class ContextKeyRegexExpr {
  699. constructor(key, regexp) {
  700. this.key = key;
  701. this.regexp = regexp;
  702. this.type = 7 /* Regex */;
  703. this.negated = null;
  704. //
  705. }
  706. static create(key, regexp) {
  707. return new ContextKeyRegexExpr(key, regexp);
  708. }
  709. cmp(other) {
  710. if (other.type !== this.type) {
  711. return this.type - other.type;
  712. }
  713. if (this.key < other.key) {
  714. return -1;
  715. }
  716. if (this.key > other.key) {
  717. return 1;
  718. }
  719. const thisSource = this.regexp ? this.regexp.source : '';
  720. const otherSource = other.regexp ? other.regexp.source : '';
  721. if (thisSource < otherSource) {
  722. return -1;
  723. }
  724. if (thisSource > otherSource) {
  725. return 1;
  726. }
  727. return 0;
  728. }
  729. equals(other) {
  730. if (other.type === this.type) {
  731. const thisSource = this.regexp ? this.regexp.source : '';
  732. const otherSource = other.regexp ? other.regexp.source : '';
  733. return (this.key === other.key && thisSource === otherSource);
  734. }
  735. return false;
  736. }
  737. substituteConstants() {
  738. return this;
  739. }
  740. evaluate(context) {
  741. let value = context.getValue(this.key);
  742. return this.regexp ? this.regexp.test(value) : false;
  743. }
  744. serialize() {
  745. const value = this.regexp
  746. ? `/${this.regexp.source}/${this.regexp.ignoreCase ? 'i' : ''}`
  747. : '/invalid/';
  748. return `${this.key} =~ ${value}`;
  749. }
  750. keys() {
  751. return [this.key];
  752. }
  753. negate() {
  754. if (!this.negated) {
  755. this.negated = ContextKeyNotRegexExpr.create(this);
  756. }
  757. return this.negated;
  758. }
  759. }
  760. export class ContextKeyNotRegexExpr {
  761. constructor(_actual) {
  762. this._actual = _actual;
  763. this.type = 8 /* NotRegex */;
  764. //
  765. }
  766. static create(actual) {
  767. return new ContextKeyNotRegexExpr(actual);
  768. }
  769. cmp(other) {
  770. if (other.type !== this.type) {
  771. return this.type - other.type;
  772. }
  773. return this._actual.cmp(other._actual);
  774. }
  775. equals(other) {
  776. if (other.type === this.type) {
  777. return this._actual.equals(other._actual);
  778. }
  779. return false;
  780. }
  781. substituteConstants() {
  782. return this;
  783. }
  784. evaluate(context) {
  785. return !this._actual.evaluate(context);
  786. }
  787. serialize() {
  788. throw new Error('Method not implemented.');
  789. }
  790. keys() {
  791. return this._actual.keys();
  792. }
  793. negate() {
  794. return this._actual;
  795. }
  796. }
  797. /**
  798. * @returns the same instance if nothing changed.
  799. */
  800. function eliminateConstantsInArray(arr) {
  801. // Allocate array only if there is a difference
  802. let newArr = null;
  803. for (let i = 0, len = arr.length; i < len; i++) {
  804. const newExpr = arr[i].substituteConstants();
  805. if (arr[i] !== newExpr) {
  806. // something has changed!
  807. // allocate array on first difference
  808. if (newArr === null) {
  809. newArr = [];
  810. for (let j = 0; j < i; j++) {
  811. newArr[j] = arr[j];
  812. }
  813. }
  814. }
  815. if (newArr !== null) {
  816. newArr[i] = newExpr;
  817. }
  818. }
  819. if (newArr === null) {
  820. return arr;
  821. }
  822. return newArr;
  823. }
  824. class ContextKeyAndExpr {
  825. constructor(expr, negated) {
  826. this.expr = expr;
  827. this.negated = negated;
  828. this.type = 6 /* And */;
  829. }
  830. static create(_expr, negated) {
  831. return ContextKeyAndExpr._normalizeArr(_expr, negated);
  832. }
  833. cmp(other) {
  834. if (other.type !== this.type) {
  835. return this.type - other.type;
  836. }
  837. if (this.expr.length < other.expr.length) {
  838. return -1;
  839. }
  840. if (this.expr.length > other.expr.length) {
  841. return 1;
  842. }
  843. for (let i = 0, len = this.expr.length; i < len; i++) {
  844. const r = cmp(this.expr[i], other.expr[i]);
  845. if (r !== 0) {
  846. return r;
  847. }
  848. }
  849. return 0;
  850. }
  851. equals(other) {
  852. if (other.type === this.type) {
  853. if (this.expr.length !== other.expr.length) {
  854. return false;
  855. }
  856. for (let i = 0, len = this.expr.length; i < len; i++) {
  857. if (!this.expr[i].equals(other.expr[i])) {
  858. return false;
  859. }
  860. }
  861. return true;
  862. }
  863. return false;
  864. }
  865. substituteConstants() {
  866. const exprArr = eliminateConstantsInArray(this.expr);
  867. if (exprArr === this.expr) {
  868. // no change
  869. return this;
  870. }
  871. return ContextKeyAndExpr.create(exprArr, this.negated);
  872. }
  873. evaluate(context) {
  874. for (let i = 0, len = this.expr.length; i < len; i++) {
  875. if (!this.expr[i].evaluate(context)) {
  876. return false;
  877. }
  878. }
  879. return true;
  880. }
  881. static _normalizeArr(arr, negated) {
  882. const expr = [];
  883. let hasTrue = false;
  884. for (const e of arr) {
  885. if (!e) {
  886. continue;
  887. }
  888. if (e.type === 1 /* True */) {
  889. // anything && true ==> anything
  890. hasTrue = true;
  891. continue;
  892. }
  893. if (e.type === 0 /* False */) {
  894. // anything && false ==> false
  895. return ContextKeyFalseExpr.INSTANCE;
  896. }
  897. if (e.type === 6 /* And */) {
  898. expr.push(...e.expr);
  899. continue;
  900. }
  901. expr.push(e);
  902. }
  903. if (expr.length === 0 && hasTrue) {
  904. return ContextKeyTrueExpr.INSTANCE;
  905. }
  906. if (expr.length === 0) {
  907. return undefined;
  908. }
  909. if (expr.length === 1) {
  910. return expr[0];
  911. }
  912. expr.sort(cmp);
  913. // eliminate duplicate terms
  914. for (let i = 1; i < expr.length; i++) {
  915. if (expr[i - 1].equals(expr[i])) {
  916. expr.splice(i, 1);
  917. i--;
  918. }
  919. }
  920. if (expr.length === 1) {
  921. return expr[0];
  922. }
  923. // We must distribute any OR expression because we don't support parens
  924. // OR extensions will be at the end (due to sorting rules)
  925. while (expr.length > 1) {
  926. const lastElement = expr[expr.length - 1];
  927. if (lastElement.type !== 9 /* Or */) {
  928. break;
  929. }
  930. // pop the last element
  931. expr.pop();
  932. // pop the second to last element
  933. const secondToLastElement = expr.pop();
  934. const isFinished = (expr.length === 0);
  935. // distribute `lastElement` over `secondToLastElement`
  936. const resultElement = ContextKeyOrExpr.create(lastElement.expr.map(el => ContextKeyAndExpr.create([el, secondToLastElement], null)), null, isFinished);
  937. if (resultElement) {
  938. expr.push(resultElement);
  939. expr.sort(cmp);
  940. }
  941. }
  942. if (expr.length === 1) {
  943. return expr[0];
  944. }
  945. return new ContextKeyAndExpr(expr, negated);
  946. }
  947. serialize() {
  948. return this.expr.map(e => e.serialize()).join(' && ');
  949. }
  950. keys() {
  951. const result = [];
  952. for (let expr of this.expr) {
  953. result.push(...expr.keys());
  954. }
  955. return result;
  956. }
  957. negate() {
  958. if (!this.negated) {
  959. const result = [];
  960. for (let expr of this.expr) {
  961. result.push(expr.negate());
  962. }
  963. this.negated = ContextKeyOrExpr.create(result, this, true);
  964. }
  965. return this.negated;
  966. }
  967. }
  968. class ContextKeyOrExpr {
  969. constructor(expr, negated) {
  970. this.expr = expr;
  971. this.negated = negated;
  972. this.type = 9 /* Or */;
  973. }
  974. static create(_expr, negated, extraRedundantCheck) {
  975. return ContextKeyOrExpr._normalizeArr(_expr, negated, extraRedundantCheck);
  976. }
  977. cmp(other) {
  978. if (other.type !== this.type) {
  979. return this.type - other.type;
  980. }
  981. if (this.expr.length < other.expr.length) {
  982. return -1;
  983. }
  984. if (this.expr.length > other.expr.length) {
  985. return 1;
  986. }
  987. for (let i = 0, len = this.expr.length; i < len; i++) {
  988. const r = cmp(this.expr[i], other.expr[i]);
  989. if (r !== 0) {
  990. return r;
  991. }
  992. }
  993. return 0;
  994. }
  995. equals(other) {
  996. if (other.type === this.type) {
  997. if (this.expr.length !== other.expr.length) {
  998. return false;
  999. }
  1000. for (let i = 0, len = this.expr.length; i < len; i++) {
  1001. if (!this.expr[i].equals(other.expr[i])) {
  1002. return false;
  1003. }
  1004. }
  1005. return true;
  1006. }
  1007. return false;
  1008. }
  1009. substituteConstants() {
  1010. const exprArr = eliminateConstantsInArray(this.expr);
  1011. if (exprArr === this.expr) {
  1012. // no change
  1013. return this;
  1014. }
  1015. return ContextKeyOrExpr.create(exprArr, this.negated, false);
  1016. }
  1017. evaluate(context) {
  1018. for (let i = 0, len = this.expr.length; i < len; i++) {
  1019. if (this.expr[i].evaluate(context)) {
  1020. return true;
  1021. }
  1022. }
  1023. return false;
  1024. }
  1025. static _normalizeArr(arr, negated, extraRedundantCheck) {
  1026. let expr = [];
  1027. let hasFalse = false;
  1028. if (arr) {
  1029. for (let i = 0, len = arr.length; i < len; i++) {
  1030. const e = arr[i];
  1031. if (!e) {
  1032. continue;
  1033. }
  1034. if (e.type === 0 /* False */) {
  1035. // anything || false ==> anything
  1036. hasFalse = true;
  1037. continue;
  1038. }
  1039. if (e.type === 1 /* True */) {
  1040. // anything || true ==> true
  1041. return ContextKeyTrueExpr.INSTANCE;
  1042. }
  1043. if (e.type === 9 /* Or */) {
  1044. expr = expr.concat(e.expr);
  1045. continue;
  1046. }
  1047. expr.push(e);
  1048. }
  1049. if (expr.length === 0 && hasFalse) {
  1050. return ContextKeyFalseExpr.INSTANCE;
  1051. }
  1052. expr.sort(cmp);
  1053. }
  1054. if (expr.length === 0) {
  1055. return undefined;
  1056. }
  1057. if (expr.length === 1) {
  1058. return expr[0];
  1059. }
  1060. // eliminate duplicate terms
  1061. for (let i = 1; i < expr.length; i++) {
  1062. if (expr[i - 1].equals(expr[i])) {
  1063. expr.splice(i, 1);
  1064. i--;
  1065. }
  1066. }
  1067. if (expr.length === 1) {
  1068. return expr[0];
  1069. }
  1070. // eliminate redundant terms
  1071. if (extraRedundantCheck) {
  1072. for (let i = 0; i < expr.length; i++) {
  1073. for (let j = i + 1; j < expr.length; j++) {
  1074. if (implies(expr[i], expr[j])) {
  1075. expr.splice(j, 1);
  1076. j--;
  1077. }
  1078. }
  1079. }
  1080. if (expr.length === 1) {
  1081. return expr[0];
  1082. }
  1083. }
  1084. return new ContextKeyOrExpr(expr, negated);
  1085. }
  1086. serialize() {
  1087. return this.expr.map(e => e.serialize()).join(' || ');
  1088. }
  1089. keys() {
  1090. const result = [];
  1091. for (let expr of this.expr) {
  1092. result.push(...expr.keys());
  1093. }
  1094. return result;
  1095. }
  1096. negate() {
  1097. if (!this.negated) {
  1098. let result = [];
  1099. for (let expr of this.expr) {
  1100. result.push(expr.negate());
  1101. }
  1102. // We don't support parens, so here we distribute the AND over the OR terminals
  1103. // We always take the first 2 AND pairs and distribute them
  1104. while (result.length > 1) {
  1105. const LEFT = result.shift();
  1106. const RIGHT = result.shift();
  1107. const all = [];
  1108. for (const left of getTerminals(LEFT)) {
  1109. for (const right of getTerminals(RIGHT)) {
  1110. all.push(ContextKeyAndExpr.create([left, right], null));
  1111. }
  1112. }
  1113. const isFinished = (result.length === 0);
  1114. result.unshift(ContextKeyOrExpr.create(all, null, isFinished));
  1115. }
  1116. this.negated = result[0];
  1117. }
  1118. return this.negated;
  1119. }
  1120. }
  1121. export class RawContextKey extends ContextKeyDefinedExpr {
  1122. constructor(key, defaultValue, metaOrHide) {
  1123. super(key, null);
  1124. this._defaultValue = defaultValue;
  1125. // collect all context keys into a central place
  1126. if (typeof metaOrHide === 'object') {
  1127. RawContextKey._info.push(Object.assign(Object.assign({}, metaOrHide), { key }));
  1128. }
  1129. else if (metaOrHide !== true) {
  1130. RawContextKey._info.push({ key, description: metaOrHide, type: defaultValue !== null && defaultValue !== undefined ? typeof defaultValue : undefined });
  1131. }
  1132. }
  1133. static all() {
  1134. return RawContextKey._info.values();
  1135. }
  1136. bindTo(target) {
  1137. return target.createKey(this.key, this._defaultValue);
  1138. }
  1139. getValue(target) {
  1140. return target.getContextKeyValue(this.key);
  1141. }
  1142. toNegated() {
  1143. return this.negate();
  1144. }
  1145. isEqualTo(value) {
  1146. return ContextKeyEqualsExpr.create(this.key, value);
  1147. }
  1148. }
  1149. RawContextKey._info = [];
  1150. export const IContextKeyService = createDecorator('contextKeyService');
  1151. export const SET_CONTEXT_COMMAND_ID = 'setContext';
  1152. function cmp1(key1, key2) {
  1153. if (key1 < key2) {
  1154. return -1;
  1155. }
  1156. if (key1 > key2) {
  1157. return 1;
  1158. }
  1159. return 0;
  1160. }
  1161. function cmp2(key1, value1, key2, value2) {
  1162. if (key1 < key2) {
  1163. return -1;
  1164. }
  1165. if (key1 > key2) {
  1166. return 1;
  1167. }
  1168. if (value1 < value2) {
  1169. return -1;
  1170. }
  1171. if (value1 > value2) {
  1172. return 1;
  1173. }
  1174. return 0;
  1175. }
  1176. /**
  1177. * Returns true if it is provable `p` implies `q`.
  1178. */
  1179. export function implies(p, q) {
  1180. if (q.type === 6 /* And */ && (p.type !== 9 /* Or */ && p.type !== 6 /* And */)) {
  1181. // covers the case: A implies A && B
  1182. for (const qTerm of q.expr) {
  1183. if (p.equals(qTerm)) {
  1184. return true;
  1185. }
  1186. }
  1187. }
  1188. const notP = p.negate();
  1189. const expr = getTerminals(notP).concat(getTerminals(q));
  1190. expr.sort(cmp);
  1191. for (let i = 0; i < expr.length; i++) {
  1192. const a = expr[i];
  1193. const notA = a.negate();
  1194. for (let j = i + 1; j < expr.length; j++) {
  1195. const b = expr[j];
  1196. if (notA.equals(b)) {
  1197. return true;
  1198. }
  1199. }
  1200. }
  1201. return false;
  1202. }
  1203. function getTerminals(node) {
  1204. if (node.type === 9 /* Or */) {
  1205. return node.expr;
  1206. }
  1207. return [node];
  1208. }