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

@@ -230,6 +230,7 @@ export abstract class MainThreadLanguageFeaturesShape {
$registerSuggestSupport(handle: number, selector: vscode.DocumentSelector, triggerCharacters: string[]): TPromise<any> { throw ni(); }
$registerSignatureHelpProvider(handle: number, selector: vscode.DocumentSelector, triggerCharacter: string[]): TPromise<any> { throw ni(); }
$registerDocumentLinkProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> { throw ni(); }
$registerColorFormats(formats: IColorFormatMap): TPromise<any> { throw ni(); }
$registerDocumentColorProvider(handle: number, selector: vscode.DocumentSelector): TPromise<any> { throw ni(); }
$setLanguageConfiguration(handle: number, languageId: string, configuration: vscode.LanguageConfiguration): TPromise<any> { throw ni(); }
}
@@ -464,13 +465,16 @@ export namespace ObjectIdentifier {
export abstract class ExtHostHeapServiceShape {
$onGarbageCollection(ids: number[]): void { throw ni(); }
}
export interface IColorInfo {
export interface IRawColorInfo {
color: [number, number, number, number | undefined];
format: string | [string, string];
availableFormats: (string | [string, string])[];
format: number;
availableFormats: number[];
range: IRange;
}
export type IRawColorFormat = string | [string, string];
export type IColorFormatMap = [number, IRawColorFormat][];
export abstract class ExtHostLanguageFeaturesShape {
$provideDocumentSymbols(handle: number, resource: URI): TPromise<modes.SymbolInformation[]> { throw ni(); }
$provideCodeLenses(handle: number, resource: URI): TPromise<modes.ICodeLensSymbol[]> { throw ni(); }
@@ -492,7 +496,7 @@ export abstract class ExtHostLanguageFeaturesShape {
$resolveCompletionItem(handle: number, resource: URI, position: IPosition, suggestion: modes.ISuggestion): TPromise<modes.ISuggestion> { throw ni(); }
$provideSignatureHelp(handle: number, resource: URI, position: IPosition): TPromise<modes.SignatureHelp> { throw ni(); }
$provideDocumentLinks(handle: number, resource: URI): TPromise<modes.ILink[]> { throw ni(); }
$provideDocumentColors(handle: number, resource: URI): TPromise<IColorInfo[]> { throw ni(); }
$provideDocumentColors(handle: number, resource: URI): TPromise<IRawColorInfo[]> { throw ni(); }
$resolveDocumentLink(handle: number, link: modes.ILink): TPromise<modes.ILink> { throw ni(); }
}

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));
}

View File

@@ -16,7 +16,7 @@ import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
import { IColorInfo } from 'vs/workbench/api/node/extHost.protocol';
import { IRawColorFormat } from "vs/workbench/api/node/extHost.protocol";
export interface PositionLike {
line: number;
@@ -370,30 +370,16 @@ export namespace DocumentLink {
}
}
export namespace DocumentColor {
export function from(colorInfo: vscode.ColorInfo): IColorInfo {
export namespace DocumentColorFormat {
export function from(colorFormat: vscode.IColorFormat): IRawColorFormat {
let format: string | [string, string];
if (typeof colorInfo.format === 'string') {
format = colorInfo.format;
if (typeof colorFormat === 'string') {
format = colorFormat;
} else {
format = [colorInfo.format.opaque, colorInfo.format.transparent];
format = [colorFormat.opaque, colorFormat.transparent];
}
let availableFormats: (string | [string, string])[] = [];
colorInfo.availableFormats.forEach(format => {
if (typeof format === 'string') {
availableFormats.push(format);
} else {
availableFormats.push([format.opaque, format.transparent]);
}
});
return {
color: [colorInfo.color.red, colorInfo.color.green, colorInfo.color.blue, colorInfo.color.alpha],
format: format,
availableFormats: availableFormats,
range: fromRange(colorInfo.range)
};
return format;
}
}