Skip to content

Commit

Permalink
use progressService for code mapper (#216910)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli authored Jun 23, 2024
1 parent 111a6f7 commit 524d1ec
Showing 1 changed file with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import { ILanguageService } from 'vs/editor/common/languages/language';
import { ITextModel } from 'vs/editor/common/model';
import { ILanguageFeaturesService } from 'vs/editor/common/services/languageFeatures';
import { CopyAction } from 'vs/editor/contrib/clipboard/browser/clipboard';
import { localize2 } from 'vs/nls';
import { localize, localize2 } from 'vs/nls';
import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/progress';
import { TerminalLocation } from 'vs/platform/terminal/common/terminal';
import { IUntitledTextResourceEditorInput } from 'vs/workbench/common/editor';
import { accessibleViewInCodeBlock } from 'vs/workbench/contrib/accessibility/browser/accessibilityConfiguration';
Expand Down Expand Up @@ -267,6 +269,8 @@ export function registerChatCodeBlockActions() {

const bulkEditService = accessor.get(IBulkEditService);
const codeEditorService = accessor.get(ICodeEditorService);
const progressService = accessor.get(IProgressService);
const notificationService = accessor.get(INotificationService);

const mappedEditsProviders = accessor.get(ILanguageFeaturesService).mappedEditsProvider.ordered(activeModel);

Expand Down Expand Up @@ -301,20 +305,39 @@ export function registerChatCodeBlockActions() {
docRefs.push(usedDocuments);
}

let i = 0;
do {
const cancellationTokenSource = new CancellationTokenSource();

mappedEdits = await mappedEditsProviders[i].provideMappedEdits(
activeModel,
[codeBlockActionContext.code],
{ documents: docRefs },
cancellationTokenSource.token);
const cancellationTokenSource = new CancellationTokenSource();

try {
mappedEdits = await progressService.withProgress(
{ location: ProgressLocation.Notification, delay: 500, sticky: true, cancellable: true },
async progress => {
progress.report({ message: localize('applyCodeBlock.progress', "Applying code block...") });

for (const provider of mappedEditsProviders) {
const mappedEdits = await provider.provideMappedEdits(
activeModel,
[codeBlockActionContext.code],
{ documents: docRefs },
cancellationTokenSource.token
);
if (mappedEdits) {
return mappedEdits;
}
}
return null;
},
() => cancellationTokenSource.cancel()
);
} catch (e) {
notificationService.notify({ severity: Severity.Error, message: localize('applyCodeBlock.error', "Failed to apply code block: {0}", e.message) });
} finally {
cancellationTokenSource.dispose();
}

} while (!mappedEdits && ++i < mappedEditsProviders.length);
}

if (mappedEdits) {
console.log('Mapped edits:', mappedEdits);
await bulkEditService.apply(mappedEdits);
} else {
const activeSelection = codeEditor.getSelection() ?? new Range(activeModel.getLineCount(), 1, activeModel.getLineCount(), 1);
Expand Down

0 comments on commit 524d1ec

Please sign in to comment.