Skip to content

Commit 5487172

Browse files
authored
Merge pull request microsoft#95739 from ChrisPapp/rename_bug_#92507
Prevent unexpected rename cancellation
2 parents 839ddf9 + 384ec8f commit 5487172

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/vs/editor/browser/core/editorState.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as strings from 'vs/base/common/strings';
77
import { ICodeEditor, IActiveCodeEditor } from 'vs/editor/browser/editorBrowser';
88
import { Position } from 'vs/editor/common/core/position';
9-
import { Range } from 'vs/editor/common/core/range';
9+
import { Range, IRange } from 'vs/editor/common/core/range';
1010
import { CancellationTokenSource, CancellationToken } from 'vs/base/common/cancellation';
1111
import { IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
1212
import { ITextModel } from 'vs/editor/common/model';
@@ -87,19 +87,28 @@ export class EditorState {
8787
/**
8888
* A cancellation token source that cancels when the editor changes as expressed
8989
* by the provided flags
90+
* @param range If provided, changes in position and selection within this range will not trigger cancellation
9091
*/
9192
export class EditorStateCancellationTokenSource extends EditorKeybindingCancellationTokenSource implements IDisposable {
9293

9394
private readonly _listener = new DisposableStore();
9495

95-
constructor(readonly editor: IActiveCodeEditor, flags: CodeEditorStateFlag, parent?: CancellationToken) {
96+
constructor(readonly editor: IActiveCodeEditor, flags: CodeEditorStateFlag, parent?: CancellationToken, readonly range?: IRange) {
9697
super(editor, parent);
9798

9899
if (flags & CodeEditorStateFlag.Position) {
99-
this._listener.add(editor.onDidChangeCursorPosition(_ => this.cancel()));
100+
this._listener.add(editor.onDidChangeCursorPosition(e => {
101+
if (!this.range || !Range.containsPosition(this.range, e.position)) {
102+
this.cancel();
103+
}
104+
}));
100105
}
101106
if (flags & CodeEditorStateFlag.Selection) {
102-
this._listener.add(editor.onDidChangeCursorSelection(_ => this.cancel()));
107+
this._listener.add(editor.onDidChangeCursorSelection(e => {
108+
if (!this.range || !Range.containsRange(this.range, e.selection)) {
109+
this.cancel();
110+
}
111+
}));
103112
}
104113
if (flags & CodeEditorStateFlag.Scroll) {
105114
this._listener.add(editor.onDidScrollChange(_ => this.cancel()));

src/vs/editor/contrib/rename/rename.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class RenameController implements IEditorContribution {
168168
if (this._cts.token.isCancellationRequested) {
169169
return undefined;
170170
}
171+
this._cts.dispose();
172+
this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value, undefined, loc.range);
171173

172174
// do rename at location
173175
let selection = this.editor.getSelection();
@@ -180,7 +182,7 @@ class RenameController implements IEditorContribution {
180182
}
181183

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

185187
// no result, only hint to focus the editor or not
186188
if (typeof inputFieldResult === 'boolean') {

src/vs/editor/contrib/rename/renameInputField.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'vs/css!./renameInputField';
77
import { DisposableStore } from 'vs/base/common/lifecycle';
88
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
99
import { Position } from 'vs/editor/common/core/position';
10-
import { IRange, Range } from 'vs/editor/common/core/range';
10+
import { IRange } from 'vs/editor/common/core/range';
1111
import { ScrollType } from 'vs/editor/common/editorCommon';
1212
import { localize } from 'vs/nls';
1313
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
@@ -16,6 +16,7 @@ import { IColorTheme, IThemeService } from 'vs/platform/theme/common/themeServic
1616
import { EditorOption } from 'vs/editor/common/config/editorOptions';
1717
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
1818
import { toggleClass } from 'vs/base/browser/dom';
19+
import { CancellationToken } from 'vs/base/common/cancellation';
1920

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

@@ -149,7 +150,7 @@ export class RenameInputField implements IContentWidget {
149150
}
150151
}
151152

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

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

@@ -185,14 +186,7 @@ export class RenameInputField implements IContentWidget {
185186
});
186187
};
187188

188-
let onCursorChanged = () => {
189-
const editorPosition = this._editor.getPosition();
190-
if (!editorPosition || !Range.containsPosition(where, editorPosition)) {
191-
this.cancelInput(true);
192-
}
193-
};
194-
195-
disposeOnDone.add(this._editor.onDidChangeCursorSelection(onCursorChanged));
189+
token.onCancellationRequested(() => this.cancelInput(true));
196190
disposeOnDone.add(this._editor.onDidBlurEditorWidget(() => this.cancelInput(false)));
197191

198192
this._show();

0 commit comments

Comments
 (0)