Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
11 changes: 7 additions & 4 deletions src/harness/unittests/extractMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace ts {
testExtractRange(`
function f() {
while (true) {
[#|
[#|
if (x) {
return;
} |]
Expand All @@ -234,7 +234,7 @@ namespace ts {
testExtractRange(`
function f() {
while (true) {
[#|
[#|
[$|if (x) {
}
return;|]
Expand Down Expand Up @@ -691,9 +691,12 @@ function test(x: number) {
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.getPossibleExtractionAtIndex(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