Skip to content

Commit 67a3dd0

Browse files
committed
rename suggestions: refactor: move selection computation where it belongs
1 parent 6efb7ca commit 67a3dd0

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,11 @@ class RenameController implements IEditorContribution {
234234
const newSymbolNameProvidersResults = newSymbolNamesProviders.map(p => p.provideNewSymbolNames(model, loc.range, renameCandidatesCts.token));
235235
trace(`requested new symbol names from ${newSymbolNamesProviders.length} providers`);
236236

237-
const selection = this.editor.getSelection();
238-
let selectionStart = 0;
239-
let selectionEnd = loc.text.length;
240-
241-
if (!Range.isEmpty(selection) && !Range.spansMultipleLines(selection) && Range.containsRange(loc.range, selection)) {
242-
selectionStart = Math.max(0, selection.startColumn - loc.range.startColumn);
243-
selectionEnd = Math.min(loc.range.endColumn, selection.endColumn) - loc.range.startColumn;
244-
}
245-
246237
trace('creating rename input field and awaiting its result');
247238
const supportPreview = this._bulkEditService.hasPreviewHandler() && this._configService.getValue<boolean>(this.editor.getModel().uri, 'editor.rename.enablePreview');
248239
const inputFieldResult = await this._renameInputField.getInput(
249240
loc.range,
250241
loc.text,
251-
selectionStart,
252-
selectionEnd,
253242
supportPreview,
254243
newSymbolNameProvidersResults,
255244
renameCandidatesCts

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { EditorOption } from 'vs/editor/common/config/editorOptions';
2323
import { FontInfo } from 'vs/editor/common/config/fontInfo';
2424
import { IDimension } from 'vs/editor/common/core/dimension';
2525
import { Position } from 'vs/editor/common/core/position';
26-
import { IRange } from 'vs/editor/common/core/range';
26+
import { IRange, Range } from 'vs/editor/common/core/range';
2727
import { ScrollType } from 'vs/editor/common/editorCommon';
2828
import { NewSymbolName, NewSymbolNameTag, ProviderResult } from 'vs/editor/common/languages';
2929
import { localize } from 'vs/nls';
@@ -85,7 +85,13 @@ interface IRenameInputField {
8585
/**
8686
* @returns a `boolean` standing for `shouldFocusEditor`, if user didn't pick a new name, or a {@link RenameInputFieldResult}
8787
*/
88-
getInput(where: IRange, value: string, selectionStart: number, selectionEnd: number, supportPreview: boolean, candidates: ProviderResult<NewSymbolName[]>[], cts: CancellationTokenSource): Promise<RenameInputFieldResult | boolean>;
88+
getInput(
89+
where: IRange,
90+
currentName: string,
91+
supportPreview: boolean,
92+
candidates: ProviderResult<NewSymbolName[]>[],
93+
cts: CancellationTokenSource
94+
): Promise<RenameInputFieldResult | boolean>;
8995

9096
acceptInput(wantsPreview: boolean): void;
9197
cancelInput(focusEditor: boolean, caller: string): void;
@@ -349,13 +355,13 @@ export class RenameInputField implements IRenameInputField, IContentWidget, IDis
349355
getInput(
350356
where: IRange,
351357
currentName: string,
352-
selectionStart: number,
353-
selectionEnd: number,
354358
supportPreview: boolean,
355359
candidates: ProviderResult<NewSymbolName[]>[],
356360
cts: CancellationTokenSource
357361
): Promise<RenameInputFieldResult | boolean> {
358362

363+
const { start: selectionStart, end: selectionEnd } = this._getSelection(where, currentName);
364+
359365
this._isEditingRenameCandidate = false;
360366

361367
this._domNode!.classList.toggle('preview', supportPreview);
@@ -441,6 +447,24 @@ export class RenameInputField implements IRenameInputField, IContentWidget, IDis
441447
return inputResult.p;
442448
}
443449

450+
/**
451+
* This allows selecting only part of the symbol name in the input field based on the selection in the editor
452+
*/
453+
private _getSelection(where: IRange, currentName: string): { start: number; end: number } {
454+
assertType(this._editor.hasModel());
455+
456+
const selection = this._editor.getSelection();
457+
let start = 0;
458+
let end = currentName.length;
459+
460+
if (!Range.isEmpty(selection) && !Range.spansMultipleLines(selection) && Range.containsRange(where, selection)) {
461+
start = Math.max(0, selection.startColumn - where.startColumn);
462+
end = Math.min(where.endColumn, selection.endColumn) - where.startColumn;
463+
}
464+
465+
return { start, end };
466+
}
467+
444468
private _show(): void {
445469
this._trace('invoking _show');
446470
this._editor.revealLineInCenterIfOutsideViewport(this._position!.lineNumber, ScrollType.Smooth);

0 commit comments

Comments
 (0)