From f3439ece090208ab878d80e06ccf669e2dad888f Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 13 Nov 2020 10:27:14 +0100 Subject: [PATCH] modernize vscode.executeCodeLensProvider-command --- src/vs/editor/contrib/codelens/codelens.ts | 17 ++++++----- .../api/common/extHostApiCommands.ts | 28 +++++++------------ 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/vs/editor/contrib/codelens/codelens.ts b/src/vs/editor/contrib/codelens/codelens.ts index fbcfb19913bf4..f8a4f09b77e37 100644 --- a/src/vs/editor/contrib/codelens/codelens.ts +++ b/src/vs/editor/contrib/codelens/codelens.ts @@ -7,11 +7,12 @@ import { mergeSort } from 'vs/base/common/arrays'; import { CancellationToken } from 'vs/base/common/cancellation'; import { illegalArgument, onUnexpectedExternalError } from 'vs/base/common/errors'; import { URI } from 'vs/base/common/uri'; -import { registerLanguageCommand } from 'vs/editor/browser/editorExtensions'; import { ITextModel } from 'vs/editor/common/model'; import { CodeLensProvider, CodeLensProviderRegistry, CodeLens, CodeLensList } from 'vs/editor/common/modes'; import { IModelService } from 'vs/editor/common/services/modelService'; import { DisposableStore } from 'vs/base/common/lifecycle'; +import { CommandsRegistry } from 'vs/platform/commands/common/commands'; +import { assertType } from 'vs/base/common/types'; export interface CodeLensItem { symbol: CodeLens; @@ -79,14 +80,12 @@ export async function getCodeLensModel(model: ITextModel, token: CancellationTok return result; } -registerLanguageCommand('_executeCodeLensProvider', function (accessor, args) { +CommandsRegistry.registerCommand('_executeCodeLensProvider', function (accessor, ...args: [URI, number | undefined | null]) { + let [uri, itemResolveCount] = args; + assertType(URI.isUri(uri)); + assertType(typeof itemResolveCount === 'number' || !itemResolveCount); - let { resource, itemResolveCount } = args; - if (!(resource instanceof URI)) { - throw illegalArgument(); - } - - const model = accessor.get(IModelService).getModel(resource); + const model = accessor.get(IModelService).getModel(uri); if (!model) { throw illegalArgument(); } @@ -99,7 +98,7 @@ registerLanguageCommand('_executeCodeLensProvider', function (accessor, args) { let resolve: Promise[] = []; for (const item of value.lenses) { - if (typeof itemResolveCount === 'undefined' || Boolean(item.symbol.command)) { + if (itemResolveCount === undefined || itemResolveCount === null || Boolean(item.symbol.command)) { result.push(item.symbol); } else if (itemResolveCount-- > 0 && item.provider.resolveCodeLens) { resolve.push(Promise.resolve(item.provider.resolveCodeLens(model, item.symbol, CancellationToken.None)).then(symbol => result.push(symbol || item.symbol))); diff --git a/src/vs/workbench/api/common/extHostApiCommands.ts b/src/vs/workbench/api/common/extHostApiCommands.ts index 637ef3af85c33..e99740c703d84 100644 --- a/src/vs/workbench/api/common/extHostApiCommands.ts +++ b/src/vs/workbench/api/common/extHostApiCommands.ts @@ -264,6 +264,16 @@ const newCommands: ApiCommand[] = [ return undefined; }) ), + // --- code lens + new ApiCommand( + 'vscode.executeCodeLensProvider', '_executeCodeLensProvider', 'Execute code lens provider.', + [ApiCommandArgument.Uri, new ApiCommandArgument('itemResolveCount', '(optional) Number of lenses that should be resolved and returned. Will only return resolved lenses, will impact performance)', v => typeof v === 'number' || typeof v === 'undefined', v => v)], + new ApiCommandResult('A promise that resolves to an array of CodeLens-instances.', (value, _args, converter) => { + return tryMapWith(item => { + return new types.CodeLens(typeConverters.Range.to(item.range), item.command && converter.fromInternal(item.command)); + })(value); + }) + ), ]; //#endregion @@ -298,14 +308,6 @@ export class ExtHostApiCommands { ], returns: 'A promise that resolves to an array of Command-instances.' }); - this._register('vscode.executeCodeLensProvider', this._executeCodeLensProvider, { - description: 'Execute CodeLens provider.', - args: [ - { name: 'uri', description: 'Uri of a text document', constraint: URI }, - { name: 'itemResolveCount', description: '(optional) Number of lenses that should be resolved and returned. Will only return resolved lenses, will impact performance)', constraint: (value: any) => value === undefined || typeof value === 'number' } - ], - returns: 'A promise that resolves to an array of CodeLens-instances.' - }); this._register('vscode.executeDocumentColorProvider', this._executeDocumentColorProvider, { description: 'Execute document color provider.', @@ -461,16 +463,6 @@ export class ExtHostApiCommands { })); } - private _executeCodeLensProvider(resource: URI, itemResolveCount: number): Promise { - const args = { resource, itemResolveCount }; - return this._commands.executeCommand('_executeCodeLensProvider', args) - .then(tryMapWith(item => { - return new types.CodeLens( - typeConverters.Range.to(item.range), - item.command ? this._commands.converter.fromInternal(item.command) : undefined); - })); - } - private _resolveNotebookContentProviders(): Promise<{ viewType: string; displayName: string;