Skip to content

Commit c3199c7

Browse files
author
Andy
authored
extractMethod: Support renameLocation (#18050)
* extractMethod: Support renameLocation * Add tslint disable * Properly analyze list of changes to always get a correct rename location * Update test * Ensure name is really unique * Improvements to test code * Respond to PR comments
1 parent f2c81cc commit c3199c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+418
-325
lines changed

src/harness/fourslash.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,11 +2758,11 @@ namespace FourSlash {
27582758
}
27592759
}
27602760

2761-
private getSelection() {
2762-
return ({
2761+
private getSelection(): ts.TextRange {
2762+
return {
27632763
pos: this.currentCaretPosition,
27642764
end: this.selectionEnd === -1 ? this.currentCaretPosition : this.selectionEnd
2765-
});
2765+
};
27662766
}
27672767

27682768
public verifyRefactorAvailable(negative: boolean, name: string, actionName?: string) {
@@ -2803,7 +2803,7 @@ namespace FourSlash {
28032803
}
28042804
}
28052805

2806-
public applyRefactor({ refactorName, actionName, actionDescription }: FourSlashInterface.ApplyRefactorOptions) {
2806+
public applyRefactor({ refactorName, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) {
28072807
const range = this.getSelection();
28082808
const refactors = this.languageService.getApplicableRefactors(this.activeFile.fileName, range);
28092809
const refactor = refactors.find(r => r.name === refactorName);
@@ -2823,6 +2823,35 @@ namespace FourSlash {
28232823
for (const edit of editInfo.edits) {
28242824
this.applyEdits(edit.fileName, edit.textChanges, /*isFormattingEdit*/ false);
28252825
}
2826+
2827+
const { renamePosition, newContent } = parseNewContent();
2828+
2829+
this.verifyCurrentFileContent(newContent);
2830+
2831+
if (renamePosition === undefined) {
2832+
if (editInfo.renameLocation !== undefined) {
2833+
this.raiseError(`Did not expect a rename location, got ${editInfo.renameLocation}`);
2834+
}
2835+
}
2836+
else {
2837+
// TODO: test editInfo.renameFilename value
2838+
assert.isDefined(editInfo.renameFilename);
2839+
if (renamePosition !== editInfo.renameLocation) {
2840+
this.raiseError(`Expected rename position of ${renamePosition}, but got ${editInfo.renameLocation}`);
2841+
}
2842+
}
2843+
2844+
function parseNewContent(): { renamePosition: number | undefined, newContent: string } {
2845+
const renamePosition = newContentWithRenameMarker.indexOf("/*RENAME*/");
2846+
if (renamePosition === -1) {
2847+
return { renamePosition: undefined, newContent: newContentWithRenameMarker };
2848+
}
2849+
else {
2850+
const newContent = newContentWithRenameMarker.slice(0, renamePosition) + newContentWithRenameMarker.slice(renamePosition + "/*RENAME*/".length);
2851+
return { renamePosition, newContent };
2852+
}
2853+
}
2854+
28262855
}
28272856

28282857
public verifyFileAfterApplyingRefactorAtMarker(
@@ -4319,6 +4348,7 @@ namespace FourSlashInterface {
43194348
refactorName: string;
43204349
actionName: string;
43214350
actionDescription: string;
4351+
newContent: string;
43224352
}
43234353

43244354
export interface CompletionsAtOptions {

src/harness/unittests/extractMethods.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,12 @@ function M3() { }`);
745745
data.push(`// ==ORIGINAL==`);
746746
data.push(sourceFile.text);
747747
for (const r of results) {
748-
const changes = refactor.extractMethod.getPossibleExtractions(result.targetRange, context, results.indexOf(r))[0].changes;
748+
const { renameLocation, edits } = refactor.extractMethod.getExtractionAtIndex(result.targetRange, context, results.indexOf(r));
749+
assert.lengthOf(edits, 1);
749750
data.push(`// ==SCOPE::${r.scopeDescription}==`);
750-
data.push(textChanges.applyChanges(sourceFile.text, changes[0].textChanges));
751+
const newText = textChanges.applyChanges(sourceFile.text, edits[0].textChanges);
752+
const newTextWithRename = newText.slice(0, renameLocation) + "/*RENAME*/" + newText.slice(renameLocation);
753+
data.push(newTextWithRename);
751754
}
752755
return data.join(newLineCharacter);
753756
});

src/server/client.ts

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

588588
if (!response.body) {
589-
return {
590-
edits: []
591-
};
589+
return { edits: [], renameFilename: undefined, renameLocation: undefined };
592590
}
593591

594592
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)