UNPKG

@codingame/monaco-vscode-extensions-service-override

Version:

VSCode public API plugged on the monaco editor - extensions service-override

103 lines (100 loc) 3.92 kB
import { __decorate, __param } from 'vscode/external/tslib/tslib.es6.js'; import { URI } from 'vscode/vscode/vs/base/common/uri'; import { Emitter } from 'vscode/vscode/vs/base/common/event'; import { dispose } from 'vscode/vscode/vs/base/common/lifecycle'; import { ExtHostContext, MainContext } from 'vscode/vscode/vs/workbench/api/common/extHost.protocol'; import { extHostNamedCustomer } from '../../services/extensions/common/extHostCustomers.js'; import { IDecorationsService } from 'vscode/vscode/vs/workbench/services/decorations/common/decorations.service'; import { CancellationToken } from 'vscode/vscode/vs/base/common/cancellation'; class DecorationRequestsQueue { constructor(_proxy, _handle) { this._proxy = _proxy; this._handle = _handle; this._idPool = 0; this._requests = ( new Map()); this._resolver = ( new Map()); } enqueue(uri, token) { const id = ++this._idPool; const result = ( new Promise(resolve => { this._requests.set(id, { id, uri }); this._resolver.set(id, resolve); this._processQueue(); })); const sub = token.onCancellationRequested(() => { this._requests.delete(id); this._resolver.delete(id); }); return result.finally(() => sub.dispose()); } _processQueue() { if (typeof this._timer === 'number') { return; } this._timer = setTimeout(() => { const requests = this._requests; const resolver = this._resolver; this._proxy.$provideDecorations(this._handle, [...( requests.values())], CancellationToken.None).then(data => { for (const [id, resolve] of resolver) { resolve(data[id]); } }); this._requests = ( new Map()); this._resolver = ( new Map()); this._timer = undefined; }, 0); } } let MainThreadDecorations = class MainThreadDecorations { constructor(context, _decorationsService) { this._decorationsService = _decorationsService; this._provider = ( new Map()); this._proxy = ( context.getProxy(ExtHostContext.ExtHostDecorations)); } dispose() { this._provider.forEach(value => dispose(value)); this._provider.clear(); } $registerDecorationProvider(handle, label) { const emitter = ( new Emitter()); const queue = ( new DecorationRequestsQueue(this._proxy, handle)); const registration = this._decorationsService.registerDecorationsProvider({ label, onDidChange: emitter.event, provideDecorations: async (uri, token) => { const data = await queue.enqueue(uri, token); if (!data) { return undefined; } const [bubble, tooltip, letter, themeColor] = data; return { weight: 10, bubble: bubble ?? false, color: themeColor?.id, tooltip, letter }; } }); this._provider.set(handle, [emitter, registration]); } $onDidChange(handle, resources) { const provider = this._provider.get(handle); if (provider) { const [emitter] = provider; emitter.fire(resources && ( resources.map(r => URI.revive(r)))); } } $unregisterDecorationProvider(handle) { const provider = this._provider.get(handle); if (provider) { dispose(provider); this._provider.delete(handle); } } }; MainThreadDecorations = __decorate([ extHostNamedCustomer(MainContext.MainThreadDecorations), ( __param(1, IDecorationsService)) ], MainThreadDecorations); export { MainThreadDecorations };