languageFeatureRegistry.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 { Emitter } from '../../../base/common/event.js';
  6. import { doHash } from '../../../base/common/hash.js';
  7. import { toDisposable } from '../../../base/common/lifecycle.js';
  8. import { LRUCache } from '../../../base/common/map.js';
  9. import { MovingAverage } from '../../../base/common/numbers.js';
  10. import { score } from './languageSelector.js';
  11. import { shouldSynchronizeModel } from '../services/modelService.js';
  12. function isExclusive(selector) {
  13. if (typeof selector === 'string') {
  14. return false;
  15. }
  16. else if (Array.isArray(selector)) {
  17. return selector.every(isExclusive);
  18. }
  19. else {
  20. return !!selector.exclusive; // TODO: microsoft/TypeScript#42768
  21. }
  22. }
  23. export class LanguageFeatureRegistry {
  24. constructor() {
  25. this._clock = 0;
  26. this._entries = [];
  27. this._onDidChange = new Emitter();
  28. }
  29. get onDidChange() {
  30. return this._onDidChange.event;
  31. }
  32. register(selector, provider) {
  33. let entry = {
  34. selector,
  35. provider,
  36. _score: -1,
  37. _time: this._clock++
  38. };
  39. this._entries.push(entry);
  40. this._lastCandidate = undefined;
  41. this._onDidChange.fire(this._entries.length);
  42. return toDisposable(() => {
  43. if (entry) {
  44. let idx = this._entries.indexOf(entry);
  45. if (idx >= 0) {
  46. this._entries.splice(idx, 1);
  47. this._lastCandidate = undefined;
  48. this._onDidChange.fire(this._entries.length);
  49. entry = undefined;
  50. }
  51. }
  52. });
  53. }
  54. has(model) {
  55. return this.all(model).length > 0;
  56. }
  57. all(model) {
  58. if (!model) {
  59. return [];
  60. }
  61. this._updateScores(model);
  62. const result = [];
  63. // from registry
  64. for (let entry of this._entries) {
  65. if (entry._score > 0) {
  66. result.push(entry.provider);
  67. }
  68. }
  69. return result;
  70. }
  71. ordered(model) {
  72. const result = [];
  73. this._orderedForEach(model, entry => result.push(entry.provider));
  74. return result;
  75. }
  76. orderedGroups(model) {
  77. const result = [];
  78. let lastBucket;
  79. let lastBucketScore;
  80. this._orderedForEach(model, entry => {
  81. if (lastBucket && lastBucketScore === entry._score) {
  82. lastBucket.push(entry.provider);
  83. }
  84. else {
  85. lastBucketScore = entry._score;
  86. lastBucket = [entry.provider];
  87. result.push(lastBucket);
  88. }
  89. });
  90. return result;
  91. }
  92. _orderedForEach(model, callback) {
  93. if (!model) {
  94. return;
  95. }
  96. this._updateScores(model);
  97. for (const entry of this._entries) {
  98. if (entry._score > 0) {
  99. callback(entry);
  100. }
  101. }
  102. }
  103. _updateScores(model) {
  104. let candidate = {
  105. uri: model.uri.toString(),
  106. language: model.getLanguageId()
  107. };
  108. if (this._lastCandidate
  109. && this._lastCandidate.language === candidate.language
  110. && this._lastCandidate.uri === candidate.uri) {
  111. // nothing has changed
  112. return;
  113. }
  114. this._lastCandidate = candidate;
  115. for (let entry of this._entries) {
  116. entry._score = score(entry.selector, model.uri, model.getLanguageId(), shouldSynchronizeModel(model));
  117. if (isExclusive(entry.selector) && entry._score > 0) {
  118. // support for one exclusive selector that overwrites
  119. // any other selector
  120. for (let entry of this._entries) {
  121. entry._score = 0;
  122. }
  123. entry._score = 1000;
  124. break;
  125. }
  126. }
  127. // needs sorting
  128. this._entries.sort(LanguageFeatureRegistry._compareByScoreAndTime);
  129. }
  130. static _compareByScoreAndTime(a, b) {
  131. if (a._score < b._score) {
  132. return 1;
  133. }
  134. else if (a._score > b._score) {
  135. return -1;
  136. }
  137. else if (a._time < b._time) {
  138. return 1;
  139. }
  140. else if (a._time > b._time) {
  141. return -1;
  142. }
  143. else {
  144. return 0;
  145. }
  146. }
  147. }
  148. const _hashes = new WeakMap();
  149. let pool = 0;
  150. function weakHash(obj) {
  151. let value = _hashes.get(obj);
  152. if (value === undefined) {
  153. value = ++pool;
  154. _hashes.set(obj, value);
  155. }
  156. return value;
  157. }
  158. /**
  159. * Keeps moving average per model and set of providers so that requests
  160. * can be debounce according to the provider performance
  161. */
  162. export class LanguageFeatureRequestDelays {
  163. constructor(_registry, min, max = Number.MAX_SAFE_INTEGER) {
  164. this._registry = _registry;
  165. this.min = min;
  166. this.max = max;
  167. this._cache = new LRUCache(50, 0.7);
  168. }
  169. _key(model) {
  170. return model.id + this._registry.all(model).reduce((hashVal, obj) => doHash(weakHash(obj), hashVal), 0);
  171. }
  172. _clamp(value) {
  173. if (value === undefined) {
  174. return this.min;
  175. }
  176. else {
  177. return Math.min(this.max, Math.max(this.min, Math.floor(value * 1.3)));
  178. }
  179. }
  180. get(model) {
  181. const key = this._key(model);
  182. const avg = this._cache.get(key);
  183. return this._clamp(avg === null || avg === void 0 ? void 0 : avg.value);
  184. }
  185. update(model, value) {
  186. const key = this._key(model);
  187. let avg = this._cache.get(key);
  188. if (!avg) {
  189. avg = new MovingAverage();
  190. this._cache.set(key, avg);
  191. }
  192. avg.update(value);
  193. return this.get(model);
  194. }
  195. }