123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- 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;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- var __param = (this && this.__param) || function (paramIndex, decorator) {
- return function (target, key) { decorator(target, key, paramIndex); }
- };
- import { runWhenIdle } from '../../../base/common/async.js';
- import { once } from '../../../base/common/functional.js';
- import { LRUCache } from '../../../base/common/map.js';
- import { Range } from '../../common/core/range.js';
- import { CodeLensModel } from './codelens.js';
- import { registerSingleton } from '../../../platform/instantiation/common/extensions.js';
- import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
- import { IStorageService, WillSaveStateReason } from '../../../platform/storage/common/storage.js';
- export const ICodeLensCache = createDecorator('ICodeLensCache');
- class CacheItem {
- constructor(lineCount, data) {
- this.lineCount = lineCount;
- this.data = data;
- }
- }
- let CodeLensCache = class CodeLensCache {
- constructor(storageService) {
- this._fakeProvider = new class {
- provideCodeLenses() {
- throw new Error('not supported');
- }
- };
- this._cache = new LRUCache(20, 0.75);
- // remove old data
- const oldkey = 'codelens/cache';
- runWhenIdle(() => storageService.remove(oldkey, 1 /* WORKSPACE */));
- // restore lens data on start
- const key = 'codelens/cache2';
- const raw = storageService.get(key, 1 /* WORKSPACE */, '{}');
- this._deserialize(raw);
- // store lens data on shutdown
- once(storageService.onWillSaveState)(e => {
- if (e.reason === WillSaveStateReason.SHUTDOWN) {
- storageService.store(key, this._serialize(), 1 /* WORKSPACE */, 1 /* MACHINE */);
- }
- });
- }
- put(model, data) {
- // create a copy of the model that is without command-ids
- // but with comand-labels
- const copyItems = data.lenses.map(item => {
- var _a;
- return {
- range: item.symbol.range,
- command: item.symbol.command && { id: '', title: (_a = item.symbol.command) === null || _a === void 0 ? void 0 : _a.title },
- };
- });
- const copyModel = new CodeLensModel();
- copyModel.add({ lenses: copyItems, dispose: () => { } }, this._fakeProvider);
- const item = new CacheItem(model.getLineCount(), copyModel);
- this._cache.set(model.uri.toString(), item);
- }
- get(model) {
- const item = this._cache.get(model.uri.toString());
- return item && item.lineCount === model.getLineCount() ? item.data : undefined;
- }
- delete(model) {
- this._cache.delete(model.uri.toString());
- }
- // --- persistence
- _serialize() {
- const data = Object.create(null);
- for (const [key, value] of this._cache) {
- const lines = new Set();
- for (const d of value.data.lenses) {
- lines.add(d.symbol.range.startLineNumber);
- }
- data[key] = {
- lineCount: value.lineCount,
- lines: [...lines.values()]
- };
- }
- return JSON.stringify(data);
- }
- _deserialize(raw) {
- try {
- const data = JSON.parse(raw);
- for (const key in data) {
- const element = data[key];
- const lenses = [];
- for (const line of element.lines) {
- lenses.push({ range: new Range(line, 1, line, 11) });
- }
- const model = new CodeLensModel();
- model.add({ lenses, dispose() { } }, this._fakeProvider);
- this._cache.set(key, new CacheItem(element.lineCount, model));
- }
- }
- catch (_a) {
- // ignore...
- }
- }
- };
- CodeLensCache = __decorate([
- __param(0, IStorageService)
- ], CodeLensCache);
- export { CodeLensCache };
- registerSingleton(ICodeLensCache, CodeLensCache);
|