Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure rename disposes it EditorStateCancellationTokenSource so that its down-level context key is correctly updated #180545

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/vs/base/test/common/cancellation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ suite('CancellationToken', function () {
assert.strictEqual(count, 1);
});

test('dispose does not cancel', function () {
const source = new CancellationTokenSource();
source.dispose();
assert.strictEqual(source.token.isCancellationRequested, false);
});

test('parent cancels child', function () {

const parent = new CancellationTokenSource();
Expand Down
30 changes: 20 additions & 10 deletions src/vs/editor/contrib/rename/browser/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ class RenameController implements IEditorContribution {

async run(): Promise<void> {

// set up cancellation token to prevent reentrant rename, this
// is the parent to the resolve- and rename-tokens
this._cts.dispose(true);
this._cts = new CancellationTokenSource();

if (!this.editor.hasModel()) {
return undefined;
Expand All @@ -168,17 +171,21 @@ class RenameController implements IEditorContribution {
return undefined;
}

this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value);
// part 1 - resolve rename location
const cts1 = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value, undefined, this._cts.token);

// resolve rename location
let loc: RenameLocation & Rejection | undefined;
try {
const resolveLocationOperation = skeleton.resolveRenameLocation(this._cts.token);
const resolveLocationOperation = skeleton.resolveRenameLocation(cts1.token);
this._progressService.showWhile(resolveLocationOperation, 250);
loc = await resolveLocationOperation;

} catch (e) {
MessageController.get(this.editor)?.showMessage(e || nls.localize('resolveRenameLocationFailed', "An unknown error occurred while resolving rename location"), position);
return undefined;

} finally {
cts1.dispose();
}

if (!loc) {
Expand All @@ -190,14 +197,13 @@ class RenameController implements IEditorContribution {
return undefined;
}

if (this._cts.token.isCancellationRequested) {
this._cts.dispose();
if (cts1.token.isCancellationRequested) {
return undefined;
}
this._cts.dispose();
this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value, loc.range);

// do rename at location
// part 2 - do rename at location
const cts2 = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value, loc.range, this._cts.token);

const selection = this.editor.getSelection();
let selectionStart = 0;
let selectionEnd = loc.text.length;
Expand All @@ -208,19 +214,20 @@ class RenameController implements IEditorContribution {
}

const supportPreview = this._bulkEditService.hasPreviewHandler() && this._configService.getValue<boolean>(this.editor.getModel().uri, 'editor.rename.enablePreview');
const inputFieldResult = await this._renameInputField.getInput(loc.range, loc.text, selectionStart, selectionEnd, supportPreview, this._cts.token);
const inputFieldResult = await this._renameInputField.getInput(loc.range, loc.text, selectionStart, selectionEnd, supportPreview, cts2.token);

// no result, only hint to focus the editor or not
if (typeof inputFieldResult === 'boolean') {
if (inputFieldResult) {
this.editor.focus();
}
cts2.dispose();
return undefined;
}

this.editor.focus();

const renameOperation = raceCancellation(skeleton.provideRenameEdits(inputFieldResult.newName, this._cts.token), this._cts.token).then(async renameResult => {
const renameOperation = raceCancellation(skeleton.provideRenameEdits(inputFieldResult.newName, cts2.token), cts2.token).then(async renameResult => {

if (!renameResult || !this.editor.hasModel()) {
return;
Expand Down Expand Up @@ -253,6 +260,9 @@ class RenameController implements IEditorContribution {
}, err => {
this._notificationService.error(nls.localize('rename.failed', "Rename failed to compute edits"));
this._logService.error(err);

}).finally(() => {
cts2.dispose();
});

this._progressService.showWhile(renameOperation, 250);
Expand Down