提交 443c0db7 编写于 作者: J Johannes Rieken

modernize vscode.executeColorPresentationProvider-command

上级 e25b8819
......@@ -407,10 +407,6 @@ export abstract class EditorAction2 extends Action2 {
// --- Registration of commands and actions
export function registerLanguageCommand<Args extends { [n: string]: any; }>(id: string, handler: (accessor: ServicesAccessor, args: Args) => any) {
CommandsRegistry.registerCommand(id, (accessor, args) => handler(accessor, args || {}));
}
export function registerModelAndPositionCommand(id: string, handler: (model: ITextModel, position: Position, ...args: any[]) => any) {
CommandsRegistry.registerCommand(id, function (accessor, ...args) {
......
......@@ -6,11 +6,11 @@
import { CancellationToken } from 'vs/base/common/cancellation';
import { illegalArgument } from 'vs/base/common/errors';
import { URI } from 'vs/base/common/uri';
import { registerLanguageCommand } from 'vs/editor/browser/editorExtensions';
import { IRange, Range } from 'vs/editor/common/core/range';
import { ITextModel } from 'vs/editor/common/model';
import { ColorProviderRegistry, DocumentColorProvider, IColorInformation, IColorPresentation } from 'vs/editor/common/modes';
import { IModelService } from 'vs/editor/common/services/modelService';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
export interface IColorData {
......@@ -36,9 +36,9 @@ export function getColorPresentations(model: ITextModel, colorInfo: IColorInform
return Promise.resolve(provider.provideColorPresentations(model, colorInfo, token));
}
registerLanguageCommand('_executeDocumentColorProvider', function (accessor, args) {
CommandsRegistry.registerCommand('_executeDocumentColorProvider', function (accessor, ...args) {
const { resource } = args;
const [resource] = args;
if (!(resource instanceof URI)) {
throw illegalArgument();
}
......@@ -62,15 +62,16 @@ registerLanguageCommand('_executeDocumentColorProvider', function (accessor, arg
});
registerLanguageCommand('_executeColorPresentationProvider', function (accessor, args) {
CommandsRegistry.registerCommand('_executeColorPresentationProvider', function (accessor, ...args) {
const { resource, color, range } = args;
if (!(resource instanceof URI) || !Array.isArray(color) || color.length !== 4 || !Range.isIRange(range)) {
const [color, context] = args;
const { uri, range } = context;
if (!(uri instanceof URI) || !Array.isArray(color) || color.length !== 4 || !Range.isIRange(range)) {
throw illegalArgument();
}
const [red, green, blue, alpha] = color;
const model = accessor.get(IModelService).getModel(resource);
const model = accessor.get(IModelService).getModel(uri);
if (!model) {
throw illegalArgument();
}
......
......@@ -309,6 +309,30 @@ const newCommands: ApiCommand[] = [
})(value);
})
),
// --- colors
new ApiCommand(
'vscode.executeDocumentColorProvider', '_executeDocumentColorProvider', 'Execute document color provider.',
[ApiCommandArgument.Uri],
new ApiCommandResult<IRawColorInfo[], vscode.ColorInformation[]>('A promise that resolves to an array of ColorInformation objects.', result => {
if (result) {
return result.map(ci => new types.ColorInformation(typeConverters.Range.to(ci.range), typeConverters.Color.to(ci.color)));
}
return [];
})
),
new ApiCommand(
'vscode.executeColorPresentationProvider', '_executeColorPresentationProvider', 'Execute color presentation provider.',
[
new ApiCommandArgument<types.Color, [number, number, number, number]>('color', 'The color to show and insert', v => v instanceof types.Color, typeConverters.Color.from),
new ApiCommandArgument<{ uri: URI, range: types.Range; }, { uri: URI, range: IRange; }>('context', 'Context object with uri and range', _v => true, v => ({ uri: v.uri, range: typeConverters.Range.from(v.range) })),
],
new ApiCommandResult<modes.IColorPresentation[], types.ColorPresentation[]>('A promise that resolves to an array of ColorPresentation objects.', result => {
if (result) {
return result.map(typeConverters.ColorPresentation.to);
}
return [];
})
)
];
//#endregion
......@@ -332,21 +356,7 @@ export class ExtHostApiCommands {
registerCommands() {
this._register('vscode.executeDocumentColorProvider', this._executeDocumentColorProvider, {
description: 'Execute document color provider.',
args: [
{ name: 'uri', description: 'Uri of a text document', constraint: URI },
],
returns: 'A promise that resolves to an array of ColorInformation objects.'
});
this._register('vscode.executeColorPresentationProvider', this._executeColorPresentationProvider, {
description: 'Execute color presentation provider.',
args: [
{ name: 'color', description: 'The color to show and insert', constraint: types.Color },
{ name: 'context', description: 'Context object with uri and range' }
],
returns: 'A promise that resolves to an array of ColorPresentation objects.'
});
this._register('vscode.resolveNotebookContentProviders', this._resolveNotebookContentProviders, {
description: 'Resolve Notebook Content Providers',
......@@ -426,32 +436,6 @@ export class ExtHostApiCommands {
this._disposables.add(disposable);
}
private _executeDocumentColorProvider(resource: URI): Promise<types.ColorInformation[]> {
const args = {
resource
};
return this._commands.executeCommand<IRawColorInfo[]>('_executeDocumentColorProvider', args).then(result => {
if (result) {
return result.map(ci => ({ range: typeConverters.Range.to(ci.range), color: typeConverters.Color.to(ci.color) }));
}
return [];
});
}
private _executeColorPresentationProvider(color: types.Color, context: { uri: URI, range: types.Range; }): Promise<types.ColorPresentation[]> {
const args = {
resource: context.uri,
color: typeConverters.Color.from(color),
range: typeConverters.Range.from(context.range),
};
return this._commands.executeCommand<modes.IColorPresentation[]>('_executeColorPresentationProvider', args).then(result => {
if (result) {
return result.map(typeConverters.ColorPresentation.to);
}
return [];
});
}
private _resolveNotebookContentProviders(): Promise<{
viewType: string;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册