mapped edits: update code around the command _executeMappedEditsProvider

This commit is contained in:
Ulugbek Abdullaev 2023-09-22 11:54:40 +02:00
parent 384e191d8e
commit 5547de51db
3 changed files with 42 additions and 33 deletions

View file

@ -18,13 +18,12 @@ suite('mapped edits provider', () => {
const r1 = vscode.chat.registerMappedEditsProvider(tsDocFilter, {
provideMappedEdits: (_doc: vscode.TextDocument, codeBlocks: string[], context: vscode.MappedEditsContext, _token: vscode.CancellationToken) => {
assert(context.selections.length === 1);
assert(context.related.length === 1);
assert('uri' in context.related[0] && 'range' in context.related[0]);
assert((context as any).selections.length === 1); // context.selections is for backward compat
assert(context.documents.length === 1);
const edit = new vscode.WorkspaceEdit();
const text = codeBlocks.join('\n//----\n');
edit.replace(uri, context.selections[0], text);
edit.replace(uri, context.documents[0][0].ranges[0], text);
return edit;
}
});
@ -37,12 +36,16 @@ suite('mapped edits provider', () => {
`function foo() {\n\treturn 1;\n}`,
],
{
selections: [new vscode.Selection(0, 0, 1, 0)],
related: [
{
uri,
range: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(1, 0))
}
documents: [
[
{
uri,
version: 1,
ranges: [
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(1, 0))
]
}
]
]
}
);
@ -60,13 +63,9 @@ suite('mapped edits provider', () => {
const r1 = vscode.chat.registerMappedEditsProvider(tsDocFilter, {
provideMappedEdits: (_doc: vscode.TextDocument, codeBlocks: string[], context: vscode.MappedEditsContext, _token: vscode.CancellationToken) => {
assert(context.selections.length === 1);
assert(context.related.length === 1);
assert('uri' in context.related[0] && 'range' in context.related[0]);
const edit = new vscode.WorkspaceEdit();
const text = codeBlocks.join('\n//----\n');
edit.replace(uri, context.selections[0], text);
edit.replace(uri, context.documents[0][0].ranges[0], text);
return edit;
}
});
@ -80,12 +79,16 @@ suite('mapped edits provider', () => {
`function foo() {\n\treturn 1;\n}`,
],
{
selections: [new vscode.Selection(0, 0, 1, 0)],
related: [
{
uri,
range: new vscode.Range(new vscode.Position(0, 0), new vscode.Position(1, 0))
}
documents: [
[
{
uri,
version: 1,
ranges: [
new vscode.Range(new vscode.Position(0, 0), new vscode.Position(1, 0))
]
}
]
]
}
);

View file

@ -123,7 +123,7 @@ export class ExtHostCommands implements ExtHostCommandsShape {
const internalArgs = apiCommand.args.map((arg, i) => {
if (!arg.validate(apiArgs[i])) {
throw new Error(`Invalid argument '${arg.name}' when running '${apiCommand.id}', received: ${apiArgs[i]}`);
throw new Error(`Invalid argument '${arg.name}' when running '${apiCommand.id}', received: ${typeof apiArgs[i] === 'object' ? JSON.stringify(apiArgs[i], null, '\t') : apiArgs[i]} `);
}
return arg.convert(apiArgs[i]);
});
@ -238,7 +238,7 @@ export class ExtHostCommands implements ExtHostCommandsShape {
try {
validateConstraint(args[i], description.args[i].constraint);
} catch (err) {
throw new Error(`Running the contributed command: '${id}' failed. Illegal argument '${description.args[i].name}' - ${description.args[i].description}`);
throw new Error(`Running the contributed command: '${id}' failed.Illegal argument '${description.args[i].name}' - ${description.args[i].description} `);
}
}
}
@ -342,7 +342,7 @@ export const IExtHostCommands = createDecorator<IExtHostCommands>('IExtHostComma
export class CommandsConverter implements extHostTypeConverter.Command.ICommandsConverter {
readonly delegatingCommandId: string = `__vsc${Date.now().toString(36)}`;
readonly delegatingCommandId: string = `__vsc${Date.now().toString(36)} `;
private readonly _cache = new Map<string, vscode.Command>();
private _cachIdPool = 0;
@ -387,7 +387,7 @@ export class CommandsConverter implements extHostTypeConverter.Command.ICommands
// we have a contributed command with arguments. that
// means we don't want to send the arguments around
const id = `${command.command}/${++this._cachIdPool}`;
const id = `${command.command} /${++this._cachIdPool}`;
this._cache.set(id, command);
disposables.add(toDisposable(() => {
this._cache.delete(id);

View file

@ -1567,14 +1567,20 @@ export namespace LanguageSelector {
export namespace MappedEditsContext {
export function is(v: unknown): v is vscode.MappedEditsContext {
return (!!v &&
typeof v === 'object' &&
'selections' in v &&
Array.isArray(v.selections) &&
v.selections.every(s => s instanceof types.Selection) &&
'related' in v &&
Array.isArray(v.related) &&
v.related.every(e => e && typeof e === 'object' && URI.isUri(e.uri) && e.range instanceof types.Range));
return (
!!v && typeof v === 'object' &&
'documents' in v &&
Array.isArray(v.documents) &&
v.documents.every(subArr =>
Array.isArray(subArr) &&
subArr.every(docRef =>
docRef && typeof docRef === 'object' &&
'uri' in docRef && URI.isUri(docRef.uri) &&
'version' in docRef && typeof docRef.version === 'number' &&
'ranges' in docRef && Array.isArray(docRef.ranges) && docRef.ranges.every((r: unknown) => r instanceof types.Range)
)
)
);
}
export function from(extContext: vscode.MappedEditsContext): languages.MappedEditsContext {