7e9328992b03fabf1e5ed5ae1c0b0daf320476d4597ab2e96d901c28ceca74de6f52d8e30cf646facf23069109e9fdc492affc6940baf65b7d3a1539c25582 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. import { RunOnceScheduler } from '../../../../base/common/async.js';
  15. import { DisposableStore } from '../../../../base/common/lifecycle.js';
  16. import { LRUCache, TernarySearchTree } from '../../../../base/common/map.js';
  17. import { CompletionItemKinds } from '../../../common/languages.js';
  18. import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
  19. import { registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
  20. import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
  21. import { IStorageService, WillSaveStateReason } from '../../../../platform/storage/common/storage.js';
  22. export class Memory {
  23. constructor(name) {
  24. this.name = name;
  25. }
  26. select(model, pos, items) {
  27. if (items.length === 0) {
  28. return 0;
  29. }
  30. const topScore = items[0].score[0];
  31. for (let i = 0; i < items.length; i++) {
  32. const { score, completion: suggestion } = items[i];
  33. if (score[0] !== topScore) {
  34. // stop when leaving the group of top matches
  35. break;
  36. }
  37. if (suggestion.preselect) {
  38. // stop when seeing an auto-select-item
  39. return i;
  40. }
  41. }
  42. return 0;
  43. }
  44. }
  45. export class NoMemory extends Memory {
  46. constructor() {
  47. super('first');
  48. }
  49. memorize(model, pos, item) {
  50. // no-op
  51. }
  52. toJSON() {
  53. return undefined;
  54. }
  55. fromJSON() {
  56. //
  57. }
  58. }
  59. export class LRUMemory extends Memory {
  60. constructor() {
  61. super('recentlyUsed');
  62. this._cache = new LRUCache(300, 0.66);
  63. this._seq = 0;
  64. }
  65. memorize(model, pos, item) {
  66. const key = `${model.getLanguageId()}/${item.textLabel}`;
  67. this._cache.set(key, {
  68. touch: this._seq++,
  69. type: item.completion.kind,
  70. insertText: item.completion.insertText
  71. });
  72. }
  73. select(model, pos, items) {
  74. if (items.length === 0) {
  75. return 0;
  76. }
  77. const lineSuffix = model.getLineContent(pos.lineNumber).substr(pos.column - 10, pos.column - 1);
  78. if (/\s$/.test(lineSuffix)) {
  79. return super.select(model, pos, items);
  80. }
  81. const topScore = items[0].score[0];
  82. let indexPreselect = -1;
  83. let indexRecency = -1;
  84. let seq = -1;
  85. for (let i = 0; i < items.length; i++) {
  86. if (items[i].score[0] !== topScore) {
  87. // consider only top items
  88. break;
  89. }
  90. const key = `${model.getLanguageId()}/${items[i].textLabel}`;
  91. const item = this._cache.peek(key);
  92. if (item && item.touch > seq && item.type === items[i].completion.kind && item.insertText === items[i].completion.insertText) {
  93. seq = item.touch;
  94. indexRecency = i;
  95. }
  96. if (items[i].completion.preselect && indexPreselect === -1) {
  97. // stop when seeing an auto-select-item
  98. return indexPreselect = i;
  99. }
  100. }
  101. if (indexRecency !== -1) {
  102. return indexRecency;
  103. }
  104. else if (indexPreselect !== -1) {
  105. return indexPreselect;
  106. }
  107. else {
  108. return 0;
  109. }
  110. }
  111. toJSON() {
  112. return this._cache.toJSON();
  113. }
  114. fromJSON(data) {
  115. this._cache.clear();
  116. const seq = 0;
  117. for (const [key, value] of data) {
  118. value.touch = seq;
  119. value.type = typeof value.type === 'number' ? value.type : CompletionItemKinds.fromString(value.type);
  120. this._cache.set(key, value);
  121. }
  122. this._seq = this._cache.size;
  123. }
  124. }
  125. export class PrefixMemory extends Memory {
  126. constructor() {
  127. super('recentlyUsedByPrefix');
  128. this._trie = TernarySearchTree.forStrings();
  129. this._seq = 0;
  130. }
  131. memorize(model, pos, item) {
  132. const { word } = model.getWordUntilPosition(pos);
  133. const key = `${model.getLanguageId()}/${word}`;
  134. this._trie.set(key, {
  135. type: item.completion.kind,
  136. insertText: item.completion.insertText,
  137. touch: this._seq++
  138. });
  139. }
  140. select(model, pos, items) {
  141. const { word } = model.getWordUntilPosition(pos);
  142. if (!word) {
  143. return super.select(model, pos, items);
  144. }
  145. const key = `${model.getLanguageId()}/${word}`;
  146. let item = this._trie.get(key);
  147. if (!item) {
  148. item = this._trie.findSubstr(key);
  149. }
  150. if (item) {
  151. for (let i = 0; i < items.length; i++) {
  152. const { kind, insertText } = items[i].completion;
  153. if (kind === item.type && insertText === item.insertText) {
  154. return i;
  155. }
  156. }
  157. }
  158. return super.select(model, pos, items);
  159. }
  160. toJSON() {
  161. const entries = [];
  162. this._trie.forEach((value, key) => entries.push([key, value]));
  163. // sort by last recently used (touch), then
  164. // take the top 200 item and normalize their
  165. // touch
  166. entries
  167. .sort((a, b) => -(a[1].touch - b[1].touch))
  168. .forEach((value, i) => value[1].touch = i);
  169. return entries.slice(0, 200);
  170. }
  171. fromJSON(data) {
  172. this._trie.clear();
  173. if (data.length > 0) {
  174. this._seq = data[0][1].touch + 1;
  175. for (const [key, value] of data) {
  176. value.type = typeof value.type === 'number' ? value.type : CompletionItemKinds.fromString(value.type);
  177. this._trie.set(key, value);
  178. }
  179. }
  180. }
  181. }
  182. let SuggestMemoryService = class SuggestMemoryService {
  183. constructor(_storageService, _configService) {
  184. this._storageService = _storageService;
  185. this._configService = _configService;
  186. this._disposables = new DisposableStore();
  187. this._persistSoon = new RunOnceScheduler(() => this._saveState(), 500);
  188. this._disposables.add(_storageService.onWillSaveState(e => {
  189. if (e.reason === WillSaveStateReason.SHUTDOWN) {
  190. this._saveState();
  191. }
  192. }));
  193. }
  194. dispose() {
  195. this._disposables.dispose();
  196. this._persistSoon.dispose();
  197. }
  198. memorize(model, pos, item) {
  199. this._withStrategy(model, pos).memorize(model, pos, item);
  200. this._persistSoon.schedule();
  201. }
  202. select(model, pos, items) {
  203. return this._withStrategy(model, pos).select(model, pos, items);
  204. }
  205. _withStrategy(model, pos) {
  206. var _a;
  207. const mode = this._configService.getValue('editor.suggestSelection', {
  208. overrideIdentifier: model.getLanguageIdAtPosition(pos.lineNumber, pos.column),
  209. resource: model.uri
  210. });
  211. if (((_a = this._strategy) === null || _a === void 0 ? void 0 : _a.name) !== mode) {
  212. this._saveState();
  213. const ctor = SuggestMemoryService._strategyCtors.get(mode) || NoMemory;
  214. this._strategy = new ctor();
  215. try {
  216. const share = this._configService.getValue('editor.suggest.shareSuggestSelections');
  217. const scope = share ? 0 /* StorageScope.PROFILE */ : 1 /* StorageScope.WORKSPACE */;
  218. const raw = this._storageService.get(`${SuggestMemoryService._storagePrefix}/${mode}`, scope);
  219. if (raw) {
  220. this._strategy.fromJSON(JSON.parse(raw));
  221. }
  222. }
  223. catch (e) {
  224. // things can go wrong with JSON...
  225. }
  226. }
  227. return this._strategy;
  228. }
  229. _saveState() {
  230. if (this._strategy) {
  231. const share = this._configService.getValue('editor.suggest.shareSuggestSelections');
  232. const scope = share ? 0 /* StorageScope.PROFILE */ : 1 /* StorageScope.WORKSPACE */;
  233. const raw = JSON.stringify(this._strategy);
  234. this._storageService.store(`${SuggestMemoryService._storagePrefix}/${this._strategy.name}`, raw, scope, 1 /* StorageTarget.MACHINE */);
  235. }
  236. }
  237. };
  238. SuggestMemoryService._strategyCtors = new Map([
  239. ['recentlyUsedByPrefix', PrefixMemory],
  240. ['recentlyUsed', LRUMemory],
  241. ['first', NoMemory]
  242. ]);
  243. SuggestMemoryService._storagePrefix = 'suggest/memories';
  244. SuggestMemoryService = __decorate([
  245. __param(0, IStorageService),
  246. __param(1, IConfigurationService)
  247. ], SuggestMemoryService);
  248. export { SuggestMemoryService };
  249. export const ISuggestMemoryService = createDecorator('ISuggestMemories');
  250. registerSingleton(ISuggestMemoryService, SuggestMemoryService, true);