rename suggestions: refactor: move selection computation where it belongs

This commit is contained in:
Ulugbek Abdullaev 2024-03-20 16:04:49 +01:00
parent 6efb7ca55b
commit 67a3dd0cc2
2 changed files with 28 additions and 15 deletions

View file

@ -234,22 +234,11 @@ class RenameController implements IEditorContribution {
const newSymbolNameProvidersResults = newSymbolNamesProviders.map(p => p.provideNewSymbolNames(model, loc.range, renameCandidatesCts.token));
trace(`requested new symbol names from ${newSymbolNamesProviders.length} providers`);
const selection = this.editor.getSelection();
let selectionStart = 0;
let selectionEnd = loc.text.length;
if (!Range.isEmpty(selection) && !Range.spansMultipleLines(selection) && Range.containsRange(loc.range, selection)) {
selectionStart = Math.max(0, selection.startColumn - loc.range.startColumn);
selectionEnd = Math.min(loc.range.endColumn, selection.endColumn) - loc.range.startColumn;
}
trace('creating rename input field and awaiting its result');
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,
newSymbolNameProvidersResults,
renameCandidatesCts

View file

@ -23,7 +23,7 @@ import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { FontInfo } from 'vs/editor/common/config/fontInfo';
import { IDimension } from 'vs/editor/common/core/dimension';
import { Position } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { IRange, Range } from 'vs/editor/common/core/range';
import { ScrollType } from 'vs/editor/common/editorCommon';
import { NewSymbolName, NewSymbolNameTag, ProviderResult } from 'vs/editor/common/languages';
import { localize } from 'vs/nls';
@ -85,7 +85,13 @@ interface IRenameInputField {
/**
* @returns a `boolean` standing for `shouldFocusEditor`, if user didn't pick a new name, or a {@link RenameInputFieldResult}
*/
getInput(where: IRange, value: string, selectionStart: number, selectionEnd: number, supportPreview: boolean, candidates: ProviderResult<NewSymbolName[]>[], cts: CancellationTokenSource): Promise<RenameInputFieldResult | boolean>;
getInput(
where: IRange,
currentName: string,
supportPreview: boolean,
candidates: ProviderResult<NewSymbolName[]>[],
cts: CancellationTokenSource
): Promise<RenameInputFieldResult | boolean>;
acceptInput(wantsPreview: boolean): void;
cancelInput(focusEditor: boolean, caller: string): void;
@ -349,13 +355,13 @@ export class RenameInputField implements IRenameInputField, IContentWidget, IDis
getInput(
where: IRange,
currentName: string,
selectionStart: number,
selectionEnd: number,
supportPreview: boolean,
candidates: ProviderResult<NewSymbolName[]>[],
cts: CancellationTokenSource
): Promise<RenameInputFieldResult | boolean> {
const { start: selectionStart, end: selectionEnd } = this._getSelection(where, currentName);
this._isEditingRenameCandidate = false;
this._domNode!.classList.toggle('preview', supportPreview);
@ -441,6 +447,24 @@ export class RenameInputField implements IRenameInputField, IContentWidget, IDis
return inputResult.p;
}
/**
* This allows selecting only part of the symbol name in the input field based on the selection in the editor
*/
private _getSelection(where: IRange, currentName: string): { start: number; end: number } {
assertType(this._editor.hasModel());
const selection = this._editor.getSelection();
let start = 0;
let end = currentName.length;
if (!Range.isEmpty(selection) && !Range.spansMultipleLines(selection) && Range.containsRange(where, selection)) {
start = Math.max(0, selection.startColumn - where.startColumn);
end = Math.min(where.endColumn, selection.endColumn) - where.startColumn;
}
return { start, end };
}
private _show(): void {
this._trace('invoking _show');
this._editor.revealLineInCenterIfOutsideViewport(this._position!.lineNumber, ScrollType.Smooth);