Simplified extHost <-> mainThread communication by having color formatters map on both sides.

This commit is contained in:
Michel Kaporin
2017-07-27 17:16:16 +02:00
parent 6629ed8634
commit be836877ee
5 changed files with 78 additions and 51 deletions

View File

@@ -19,7 +19,7 @@ import { ExtHostCommands, CommandsConverter } from 'vs/workbench/api/node/extHos
import { ExtHostDiagnostics } from 'vs/workbench/api/node/extHostDiagnostics';
import { IWorkspaceSymbolProvider } from 'vs/workbench/parts/search/common/search';
import { asWinJsPromise } from 'vs/base/common/async';
import { MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, ObjectIdentifier, IColorInfo } from './extHost.protocol';
import { MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesShape, ObjectIdentifier, IRawColorInfo, IRawColorFormat, IColorFormatMap } from './extHost.protocol';
import { regExpLeadsToEndlessLoop } from 'vs/base/common/strings';
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
@@ -668,20 +668,51 @@ class LinkProviderAdapter {
class ColorProviderAdapter {
private _proxy: MainThreadLanguageFeaturesShape;
private _documents: ExtHostDocuments;
private _provider: vscode.DocumentColorProvider;
private _formatStorageMap: Map<vscode.IColorFormat, number>;
private _formatStorageIndex;
constructor(documents: ExtHostDocuments, provider: vscode.DocumentColorProvider) {
constructor(proxy: MainThreadLanguageFeaturesShape, documents: ExtHostDocuments, provider: vscode.DocumentColorProvider) {
this._proxy = proxy;
this._documents = documents;
this._provider = provider;
this._formatStorageMap = new Map<vscode.IColorFormat, number>();
this._formatStorageIndex = 0;
}
provideColors(resource: URI): TPromise<IColorInfo[]> {
provideColors(resource: URI): TPromise<IRawColorInfo[]> {
const doc = this._documents.getDocumentData(resource).document;
return asWinJsPromise(token => this._provider.provideDocumentColors(doc, token)).then(colors => {
if (Array.isArray(colors)) {
return colors.map(TypeConverters.DocumentColor.from);
const colorFormats: IColorFormatMap = [];
const colorInfos: IRawColorInfo[] = [];
colors.forEach(ci => {
let cachedId = this._formatStorageMap.get(ci.format);
if (cachedId === undefined) {
cachedId = this._formatStorageIndex;
this._formatStorageMap.set(ci.format, cachedId);
this._formatStorageIndex += 1;
if (typeof ci.format === 'string') { // Append to format list for registration
colorFormats.push([cachedId, ci.format]);
} else {
colorFormats.push([cachedId, [ci.format.opaque, ci.format.transparent]]);
}
}
colorInfos.push({
color: [ci.color.red, ci.color.green, ci.color.blue, ci.color.alpha],
format: cachedId,
availableFormats: [],
range: TypeConverters.fromRange(ci.range)
});
});
this._proxy.$registerColorFormats(colorFormats);
return colorInfos;
}
return undefined;
});
@@ -978,12 +1009,12 @@ export class ExtHostLanguageFeatures extends ExtHostLanguageFeaturesShape {
registerColorProvider(selector: vscode.DocumentSelector, provider: vscode.DocumentColorProvider): vscode.Disposable {
const handle = this._nextHandle();
this._adapter.set(handle, new ColorProviderAdapter(this._documents, provider));
this._adapter.set(handle, new ColorProviderAdapter(this._proxy, this._documents, provider));
this._proxy.$registerDocumentColorProvider(handle, selector);
return this._createDisposable(handle);
}
$provideDocumentColors(handle: number, resource: URI): TPromise<IColorInfo[]> {
$provideDocumentColors(handle: number, resource: URI): TPromise<IRawColorInfo[]> {
return this._withAdapter(handle, ColorProviderAdapter, adapter => adapter.provideColors(resource));
}