Skip to content

Commit

Permalink
modernize vscode.executeCodeLensProvider-command
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Nov 13, 2020
1 parent 94a5740 commit f3439ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
17 changes: 8 additions & 9 deletions src/vs/editor/contrib/codelens/codelens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -99,7 +98,7 @@ registerLanguageCommand('_executeCodeLensProvider', function (accessor, args) {
let resolve: Promise<any>[] = [];

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)));
Expand Down
28 changes: 10 additions & 18 deletions src/vs/workbench/api/common/extHostApiCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<modes.CodeLens[], vscode.CodeLens[] | undefined>('A promise that resolves to an array of CodeLens-instances.', (value, _args, converter) => {
return tryMapWith<modes.CodeLens, vscode.CodeLens>(item => {
return new types.CodeLens(typeConverters.Range.to(item.range), item.command && converter.fromInternal(item.command));
})(value);
})
),
];

//#endregion
Expand Down Expand Up @@ -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.',
Expand Down Expand Up @@ -461,16 +463,6 @@ export class ExtHostApiCommands {
}));
}

private _executeCodeLensProvider(resource: URI, itemResolveCount: number): Promise<vscode.CodeLens[] | undefined> {
const args = { resource, itemResolveCount };
return this._commands.executeCommand<modes.CodeLens[]>('_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;
Expand Down

0 comments on commit f3439ec

Please sign in to comment.