Use the cancellation token to trigger rename cancellation

上级 a9306b4f
......@@ -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';
......@@ -87,19 +87,30 @@ 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) {
set range(range: IRange | undefined) { this._range = range; }
constructor(readonly editor: IActiveCodeEditor, flags: CodeEditorStateFlag, parent?: CancellationToken, private _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()));
......
......@@ -173,6 +173,7 @@ class RenameController implements IEditorContribution {
let selection = this.editor.getSelection();
let selectionStart = 0;
let selectionEnd = loc.text.length;
(this._cts as EditorStateCancellationTokenSource).range = loc.range;
if (!Range.isEmpty(selection) && !Range.spansMultipleLines(selection) && Range.containsRange(loc.range, selection)) {
selectionStart = Math.max(0, selection.startColumn - loc.range.startColumn);
......@@ -180,10 +181,7 @@ class RenameController implements IEditorContribution {
}
const supportPreview = this._bulkEditService.hasPreviewHandler() && this._configService.getValue<boolean>(this.editor.getModel().uri, 'editor.rename.enablePreview');
// Special cancellation handling here (allows the user to move the cursor but only within loc.range):
this._cts.dispose(false);
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') {
......@@ -193,8 +191,6 @@ class RenameController implements IEditorContribution {
return undefined;
}
this._cts = new EditorStateCancellationTokenSource(this.editor, CodeEditorStateFlag.Position | CodeEditorStateFlag.Value);
this.editor.focus();
const renameOperation = raceCancellation(skeleton.provideRenameEdits(inputFieldResult.newName, 0, [], this._cts.token), this._cts.token).then(async renameResult => {
......
......@@ -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';
......@@ -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);
......@@ -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);
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册