codeLensCache.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 { runWhenIdle } from '../../../base/common/async.js';
  15. import { once } from '../../../base/common/functional.js';
  16. import { LRUCache } from '../../../base/common/map.js';
  17. import { Range } from '../../common/core/range.js';
  18. import { CodeLensModel } from './codelens.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 const ICodeLensCache = createDecorator('ICodeLensCache');
  23. class CacheItem {
  24. constructor(lineCount, data) {
  25. this.lineCount = lineCount;
  26. this.data = data;
  27. }
  28. }
  29. let CodeLensCache = class CodeLensCache {
  30. constructor(storageService) {
  31. this._fakeProvider = new class {
  32. provideCodeLenses() {
  33. throw new Error('not supported');
  34. }
  35. };
  36. this._cache = new LRUCache(20, 0.75);
  37. // remove old data
  38. const oldkey = 'codelens/cache';
  39. runWhenIdle(() => storageService.remove(oldkey, 1 /* WORKSPACE */));
  40. // restore lens data on start
  41. const key = 'codelens/cache2';
  42. const raw = storageService.get(key, 1 /* WORKSPACE */, '{}');
  43. this._deserialize(raw);
  44. // store lens data on shutdown
  45. once(storageService.onWillSaveState)(e => {
  46. if (e.reason === WillSaveStateReason.SHUTDOWN) {
  47. storageService.store(key, this._serialize(), 1 /* WORKSPACE */, 1 /* MACHINE */);
  48. }
  49. });
  50. }
  51. put(model, data) {
  52. // create a copy of the model that is without command-ids
  53. // but with comand-labels
  54. const copyItems = data.lenses.map(item => {
  55. var _a;
  56. return {
  57. range: item.symbol.range,
  58. command: item.symbol.command && { id: '', title: (_a = item.symbol.command) === null || _a === void 0 ? void 0 : _a.title },
  59. };
  60. });
  61. const copyModel = new CodeLensModel();
  62. copyModel.add({ lenses: copyItems, dispose: () => { } }, this._fakeProvider);
  63. const item = new CacheItem(model.getLineCount(), copyModel);
  64. this._cache.set(model.uri.toString(), item);
  65. }
  66. get(model) {
  67. const item = this._cache.get(model.uri.toString());
  68. return item && item.lineCount === model.getLineCount() ? item.data : undefined;
  69. }
  70. delete(model) {
  71. this._cache.delete(model.uri.toString());
  72. }
  73. // --- persistence
  74. _serialize() {
  75. const data = Object.create(null);
  76. for (const [key, value] of this._cache) {
  77. const lines = new Set();
  78. for (const d of value.data.lenses) {
  79. lines.add(d.symbol.range.startLineNumber);
  80. }
  81. data[key] = {
  82. lineCount: value.lineCount,
  83. lines: [...lines.values()]
  84. };
  85. }
  86. return JSON.stringify(data);
  87. }
  88. _deserialize(raw) {
  89. try {
  90. const data = JSON.parse(raw);
  91. for (const key in data) {
  92. const element = data[key];
  93. const lenses = [];
  94. for (const line of element.lines) {
  95. lenses.push({ range: new Range(line, 1, line, 11) });
  96. }
  97. const model = new CodeLensModel();
  98. model.add({ lenses, dispose() { } }, this._fakeProvider);
  99. this._cache.set(key, new CacheItem(element.lineCount, model));
  100. }
  101. }
  102. catch (_a) {
  103. // ignore...
  104. }
  105. }
  106. };
  107. CodeLensCache = __decorate([
  108. __param(0, IStorageService)
  109. ], CodeLensCache);
  110. export { CodeLensCache };
  111. registerSingleton(ICodeLensCache, CodeLensCache);