Skip to content

Commit

Permalink
Merge pull request #95739 from ChrisPapp/rename_bug_#92507
Browse files Browse the repository at this point in the history
Prevent unexpected rename cancellation
  • Loading branch information
jrieken authored Apr 27, 2020
2 parents 839ddf9 + 384ec8f commit 5487172
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
17 changes: 13 additions & 4 deletions src/vs/editor/browser/core/editorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as strings from 'vs/base/common/strings';
import { ICodeEditor, IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { Range, IRange } from 'vs/editor/common/core/range';
import { CancellationTokenSource, CancellationToken } from 'vs/base/common/cancellation';
import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { ITextModel } from 'vs/editor/common/model';
Expand Down Expand Up @@ -87,19 +87,28 @@ export class EditorState {
/**
* A cancellation token source that cancels when the editor changes as expressed
* by the provided flags
* @param range If provided, changes in position and selection within this range will not trigger cancellation
*/
export class EditorStateCancellationTokenSource extends EditorKeybindingCancellationTokenSource implements IDisposable {

private readonly _listener = new DisposableStore();

constructor(readonly editor: IActiveCodeEditor, flags: CodeEditorStateFlag, parent?: CancellationToken) {
constructor(readonly editor: IActiveCodeEditor, flags: CodeEditorStateFlag, parent?: CancellationToken, readonly range?: IRange) {
super(editor, parent);

if (flags & CodeEditorStateFlag.Position) {
this._listener.add(editor.onDidChangeCursorPosition(_ => this.cancel()));
this._listener.add(editor.onDidChangeCursorPosition(e => {
if (!this.range || !Range.containsPosition(this.range, e.position)) {
this.cancel();
}
}));
}
if (flags & CodeEditorStateFlag.Selection) {
this._listener.add(editor.onDidChangeCursorSelection(_ => this.cancel()));
this._listener.add(editor.onDidChangeCursorSelection(e => {
if (!this.range || !Range.containsRange(this.range, e.selection)) {
this.cancel();
}
}));
}
if (flags & CodeEditorStateFlag.Scroll) {
this._listener.add(editor.onDidScrollChange(_ => this.cancel()));
Expand Down
4 changes: 3 additions & 1 deletion src/vs/editor/contrib/rename/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class RenameController implements IEditorContribution {
if (this._cts.token.isCancellationRequested) {
return undefined;
}
this._cts.dispose();
this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value, undefined, loc.range);

// do rename at location
let selection = this.editor.getSelection();
Expand All @@ -180,7 +182,7 @@ 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.getValue().getInput(loc.range, loc.text, selectionStart, selectionEnd, supportPreview);
const inputFieldResult = await this._renameInputField.getValue().getInput(loc.range, loc.text, selectionStart, selectionEnd, supportPreview, this._cts.token);

// no result, only hint to focus the editor or not
if (typeof inputFieldResult === 'boolean') {
Expand Down
14 changes: 4 additions & 10 deletions src/vs/editor/contrib/rename/renameInputField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'vs/css!./renameInputField';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
import { Position } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
import { IRange } from 'vs/editor/common/core/range';
import { ScrollType } from 'vs/editor/common/editorCommon';
import { localize } from 'vs/nls';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
Expand All @@ -16,6 +16,7 @@ import { IColorTheme, IThemeService } from 'vs/platform/theme/common/themeServic
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { toggleClass } from 'vs/base/browser/dom';
import { CancellationToken } from 'vs/base/common/cancellation';

export const CONTEXT_RENAME_INPUT_VISIBLE = new RawContextKey<boolean>('renameInputVisible', false);

Expand Down Expand Up @@ -149,7 +150,7 @@ export class RenameInputField implements IContentWidget {
}
}

getInput(where: IRange, value: string, selectionStart: number, selectionEnd: number, supportPreview: boolean): Promise<RenameInputFieldResult | boolean> {
getInput(where: IRange, value: string, selectionStart: number, selectionEnd: number, supportPreview: boolean, token: CancellationToken): Promise<RenameInputFieldResult | boolean> {

toggleClass(this._domNode!, 'preview', supportPreview);

Expand Down Expand Up @@ -185,14 +186,7 @@ export class RenameInputField implements IContentWidget {
});
};

let onCursorChanged = () => {
const editorPosition = this._editor.getPosition();
if (!editorPosition || !Range.containsPosition(where, editorPosition)) {
this.cancelInput(true);
}
};

disposeOnDone.add(this._editor.onDidChangeCursorSelection(onCursorChanged));
token.onCancellationRequested(() => this.cancelInput(true));
disposeOnDone.add(this._editor.onDidBlurEditorWidget(() => this.cancelInput(false)));

this._show();
Expand Down

0 comments on commit 5487172

Please sign in to comment.