Skip to content

Commit 9434a81

Browse files
Andymhegazy
Andy
authored andcommitted
extractMethod: Support renameLocation (#18351)
1 parent 236eb1e commit 9434a81

35 files changed

+319
-230
lines changed

Gulpfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ gulp.task("browserify", "Runs browserify on run.js to produce a file suitable fo
776776
const file = new Vinyl({ contents, path: bundlePath });
777777
console.log(`Fixing sourcemaps for ${file.path}`);
778778
// assumes contents is a Buffer, since that's what browserify yields
779-
const maps = convertMap.fromSource(stringContent, /*largeSource*/ true).toObject();
779+
const maps = convertMap.fromSource(stringContent).toObject();
780780
delete maps.sourceRoot;
781781
maps.sources = maps.sources.map(s => path.resolve(s === "_stream_0.js" ? "built/local/_stream_0.js" : s));
782782
// Strip browserify's inline comments away (could probably just let sorcery do this, but then we couldn't fix the paths)

src/harness/fourslash.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,11 +2736,11 @@ namespace FourSlash {
27362736
}
27372737
}
27382738

2739-
private getSelection() {
2740-
return ({
2739+
private getSelection(): ts.TextRange {
2740+
return {
27412741
pos: this.currentCaretPosition,
27422742
end: this.selectionEnd === -1 ? this.currentCaretPosition : this.selectionEnd
2743-
});
2743+
};
27442744
}
27452745

27462746
public verifyRefactorAvailable(negative: boolean, name: string, actionName?: string) {
@@ -2781,7 +2781,7 @@ namespace FourSlash {
27812781
}
27822782
}
27832783

2784-
public applyRefactor({ refactorName, actionName, actionDescription }: FourSlashInterface.ApplyRefactorOptions) {
2784+
public applyRefactor({ refactorName, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) {
27852785
const range = this.getSelection();
27862786
const refactors = this.languageService.getApplicableRefactors(this.activeFile.fileName, range);
27872787
const refactor = refactors.find(r => r.name === refactorName);
@@ -2801,6 +2801,35 @@ namespace FourSlash {
28012801
for (const edit of editInfo.edits) {
28022802
this.applyEdits(edit.fileName, edit.textChanges, /*isFormattingEdit*/ false);
28032803
}
2804+
2805+
const { renamePosition, newContent } = parseNewContent();
2806+
2807+
this.verifyCurrentFileContent(newContent);
2808+
2809+
if (renamePosition === undefined) {
2810+
if (editInfo.renameLocation !== undefined) {
2811+
this.raiseError(`Did not expect a rename location, got ${editInfo.renameLocation}`);
2812+
}
2813+
}
2814+
else {
2815+
// TODO: test editInfo.renameFilename value
2816+
assert.isDefined(editInfo.renameFilename);
2817+
if (renamePosition !== editInfo.renameLocation) {
2818+
this.raiseError(`Expected rename position of ${renamePosition}, but got ${editInfo.renameLocation}`);
2819+
}
2820+
}
2821+
2822+
function parseNewContent(): { renamePosition: number | undefined, newContent: string } {
2823+
const renamePosition = newContentWithRenameMarker.indexOf("/*RENAME*/");
2824+
if (renamePosition === -1) {
2825+
return { renamePosition: undefined, newContent: newContentWithRenameMarker };
2826+
}
2827+
else {
2828+
const newContent = newContentWithRenameMarker.slice(0, renamePosition) + newContentWithRenameMarker.slice(renamePosition + "/*RENAME*/".length);
2829+
return { renamePosition, newContent };
2830+
}
2831+
}
2832+
28042833
}
28052834

28062835
public verifyFileAfterApplyingRefactorAtMarker(
@@ -4291,5 +4320,6 @@ namespace FourSlashInterface {
42914320
refactorName: string;
42924321
actionName: string;
42934322
actionDescription: string;
4323+
newContent: string;
42944324
}
42954325
}

src/harness/unittests/extractMethods.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ namespace ts {
224224
testExtractRange(`
225225
function f() {
226226
while (true) {
227-
[#|
227+
[#|
228228
if (x) {
229229
return;
230230
} |]
@@ -234,7 +234,7 @@ namespace ts {
234234
testExtractRange(`
235235
function f() {
236236
while (true) {
237-
[#|
237+
[#|
238238
[$|if (x) {
239239
}
240240
return;|]
@@ -580,9 +580,12 @@ namespace A {
580580
data.push(`==ORIGINAL==`);
581581
data.push(sourceFile.text);
582582
for (const r of results) {
583-
const changes = refactor.extractMethod.getPossibleExtractions(result.targetRange, context, results.indexOf(r))[0].changes;
583+
const { renameLocation, edits } = refactor.extractMethod.getExtractionAtIndex(result.targetRange, context, results.indexOf(r));
584+
assert.lengthOf(edits, 1);
584585
data.push(`==SCOPE::${r.scopeDescription}==`);
585-
data.push(textChanges.applyChanges(sourceFile.text, changes[0].textChanges));
586+
const newText = textChanges.applyChanges(sourceFile.text, edits[0].textChanges);
587+
const newTextWithRename = newText.slice(0, renameLocation) + "/*RENAME*/" + newText.slice(renameLocation);
588+
data.push(newTextWithRename);
586589
}
587590
return data.join(newLineCharacter);
588591
});

src/server/client.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,7 @@ namespace ts.server {
582582
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);
583583

584584
if (!response.body) {
585-
return {
586-
edits: []
587-
};
585+
return { edits: [], renameFilename: undefined, renameLocation: undefined };
588586
}
589587

590588
const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);

src/services/refactors/convertFunctionToEs6Class.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ namespace ts.refactor.convertFunctionToES6Class {
9797
}
9898

9999
return {
100-
edits: changeTracker.getChanges()
100+
edits: changeTracker.getChanges(),
101+
renameFilename: undefined,
102+
renameLocation: undefined,
101103
};
102104

103105
function deleteNode(node: Node, inList = false) {

0 commit comments

Comments
 (0)