Skip to content

extractMethod: Support renameLocation #18050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
11 commits merged into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2758,11 +2758,11 @@ namespace FourSlash {
}
}

private getSelection() {
return ({
private getSelection(): ts.TextRange {
return {
pos: this.currentCaretPosition,
end: this.selectionEnd === -1 ? this.currentCaretPosition : this.selectionEnd
});
};
}

public verifyRefactorAvailable(negative: boolean, name: string, actionName?: string) {
Expand Down Expand Up @@ -2803,7 +2803,7 @@ namespace FourSlash {
}
}

public applyRefactor({ refactorName, actionName, actionDescription }: FourSlashInterface.ApplyRefactorOptions) {
public applyRefactor({ refactorName, actionName, actionDescription, newContent: newContentWithRenameMarker }: FourSlashInterface.ApplyRefactorOptions) {
const range = this.getSelection();
const refactors = this.languageService.getApplicableRefactors(this.activeFile.fileName, range);
const refactor = refactors.find(r => r.name === refactorName);
Expand All @@ -2823,6 +2823,35 @@ namespace FourSlash {
for (const edit of editInfo.edits) {
this.applyEdits(edit.fileName, edit.textChanges, /*isFormattingEdit*/ false);
}

const { renamePosition, newContent } = parseNewContent();

this.verifyCurrentFileContent(newContent);

if (renamePosition === undefined) {
if (editInfo.renameLocation !== undefined) {
this.raiseError(`Did not expect a rename location, got ${editInfo.renameLocation}`);
}
}
else {
// TODO: test editInfo.renameFilename value
assert.isDefined(editInfo.renameFilename);
if (renamePosition !== editInfo.renameLocation) {
this.raiseError(`Expected rename position of ${renamePosition}, but got ${editInfo.renameLocation}`);
}
}

function parseNewContent(): { renamePosition: number | undefined, newContent: string } {
const renamePosition = newContentWithRenameMarker.indexOf("/*RENAME*/");
if (renamePosition === -1) {
return { renamePosition: undefined, newContent: newContentWithRenameMarker };
}
else {
const newContent = newContentWithRenameMarker.slice(0, renamePosition) + newContentWithRenameMarker.slice(renamePosition + "/*RENAME*/".length);
return { renamePosition, newContent };
}
}

}

public verifyFileAfterApplyingRefactorAtMarker(
Expand Down Expand Up @@ -4319,6 +4348,7 @@ namespace FourSlashInterface {
refactorName: string;
actionName: string;
actionDescription: string;
newContent: string;
}

export interface CompletionsAtOptions {
Expand Down
7 changes: 5 additions & 2 deletions src/harness/unittests/extractMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,12 @@ function M3() { }`);
data.push(`// ==ORIGINAL==`);
data.push(sourceFile.text);
for (const r of results) {
const changes = refactor.extractMethod.getPossibleExtractions(result.targetRange, context, results.indexOf(r))[0].changes;
const { renameLocation, edits } = refactor.extractMethod.getExtractionAtIndex(result.targetRange, context, results.indexOf(r));
assert.lengthOf(edits, 1);
data.push(`// ==SCOPE::${r.scopeDescription}==`);
data.push(textChanges.applyChanges(sourceFile.text, changes[0].textChanges));
const newText = textChanges.applyChanges(sourceFile.text, edits[0].textChanges);
const newTextWithRename = newText.slice(0, renameLocation) + "/*RENAME*/" + newText.slice(renameLocation);
data.push(newTextWithRename);
}
return data.join(newLineCharacter);
});
Expand Down
4 changes: 1 addition & 3 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,7 @@ namespace ts.server {
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);

if (!response.body) {
return {
edits: []
};
return { edits: [], renameFilename: undefined, renameLocation: undefined };
}

const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);
Expand Down
4 changes: 3 additions & 1 deletion src/services/refactors/convertFunctionToEs6Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ namespace ts.refactor.convertFunctionToES6Class {
}

return {
edits: changeTracker.getChanges()
edits: changeTracker.getChanges(),
renameFilename: undefined,
renameLocation: undefined,
};

function deleteNode(node: Node, inList = false) {
Expand Down
Loading