Fix acquiring format options for getEditsForRefactor (#18848)
* Fix acquiring format options for getEditsForRefactor * Add test * Fix test description * Use `executeCommandSeq`
This commit is contained in:
parent
637ed57451
commit
eefe5c9706
5 changed files with 67 additions and 12 deletions
|
@ -4245,4 +4245,60 @@ namespace ts.projectSystem {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("refactors", () => {
|
||||||
|
it("use formatting options", () => {
|
||||||
|
const file = {
|
||||||
|
path: "/a.ts",
|
||||||
|
content: "function f() {\n 1;\n}",
|
||||||
|
};
|
||||||
|
const host = createServerHost([file]);
|
||||||
|
const session = createSession(host);
|
||||||
|
openFilesForSession([file], session);
|
||||||
|
|
||||||
|
const response0 = session.executeCommandSeq<server.protocol.ConfigureRequest>({
|
||||||
|
command: server.protocol.CommandTypes.Configure,
|
||||||
|
arguments: {
|
||||||
|
formatOptions: {
|
||||||
|
indentSize: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}).response;
|
||||||
|
assert.deepEqual(response0, /*expected*/ undefined);
|
||||||
|
|
||||||
|
const response1 = session.executeCommandSeq<server.protocol.GetEditsForRefactorRequest>({
|
||||||
|
command: server.protocol.CommandTypes.GetEditsForRefactor,
|
||||||
|
arguments: {
|
||||||
|
refactor: "Extract Symbol",
|
||||||
|
action: "function_scope_1",
|
||||||
|
file: "/a.ts",
|
||||||
|
startLine: 2,
|
||||||
|
startOffset: 3,
|
||||||
|
endLine: 2,
|
||||||
|
endOffset: 4,
|
||||||
|
},
|
||||||
|
}).response;
|
||||||
|
assert.deepEqual(response1, {
|
||||||
|
edits: [
|
||||||
|
{
|
||||||
|
fileName: "/a.ts",
|
||||||
|
textChanges: [
|
||||||
|
{
|
||||||
|
start: { line: 2, offset: 1 },
|
||||||
|
end: { line: 3, offset: 1 },
|
||||||
|
newText: " newFunction();\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
start: { line: 3, offset: 2 },
|
||||||
|
end: { line: 3, offset: 2 },
|
||||||
|
newText: "\nfunction newFunction() {\n 1;\n}\n",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
renameFilename: "/a.ts",
|
||||||
|
renameLocation: { line: 2, offset: 3 },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -573,7 +573,7 @@ namespace ts.server {
|
||||||
|
|
||||||
getEditsForRefactor(
|
getEditsForRefactor(
|
||||||
fileName: string,
|
fileName: string,
|
||||||
formatOptions: FormatCodeSettings,
|
_formatOptions: FormatCodeSettings,
|
||||||
positionOrRange: number | TextRange,
|
positionOrRange: number | TextRange,
|
||||||
refactorName: string,
|
refactorName: string,
|
||||||
actionName: string): RefactorEditInfo {
|
actionName: string): RefactorEditInfo {
|
||||||
|
@ -581,7 +581,6 @@ namespace ts.server {
|
||||||
const args = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName) as protocol.GetEditsForRefactorRequestArgs;
|
const args = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName) as protocol.GetEditsForRefactorRequestArgs;
|
||||||
args.refactor = refactorName;
|
args.refactor = refactorName;
|
||||||
args.action = actionName;
|
args.action = actionName;
|
||||||
args.formatOptions = formatOptions;
|
|
||||||
|
|
||||||
const request = this.processRequest<protocol.GetEditsForRefactorRequest>(CommandNames.GetEditsForRefactor, args);
|
const request = this.processRequest<protocol.GetEditsForRefactorRequest>(CommandNames.GetEditsForRefactor, args);
|
||||||
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);
|
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);
|
||||||
|
|
|
@ -494,7 +494,6 @@ namespace ts.server.protocol {
|
||||||
refactor: string;
|
refactor: string;
|
||||||
/* The 'name' property from the refactoring action */
|
/* The 'name' property from the refactoring action */
|
||||||
action: string;
|
action: string;
|
||||||
formatOptions?: FormatCodeSettings,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1488,7 +1488,7 @@ namespace ts.server {
|
||||||
|
|
||||||
const result = project.getLanguageService().getEditsForRefactor(
|
const result = project.getLanguageService().getEditsForRefactor(
|
||||||
file,
|
file,
|
||||||
args.formatOptions ? convertFormatOptions(args.formatOptions) : this.projectService.getFormatCodeOptions(),
|
this.projectService.getFormatCodeOptions(file),
|
||||||
position || textRange,
|
position || textRange,
|
||||||
args.refactor,
|
args.refactor,
|
||||||
args.action
|
args.action
|
||||||
|
|
|
@ -14,13 +14,14 @@
|
||||||
verify.applicableRefactorAvailableAtMarker('1');
|
verify.applicableRefactorAvailableAtMarker('1');
|
||||||
// NOTE: '// Comment' should be included, but due to incorrect handling of trivia,
|
// NOTE: '// Comment' should be included, but due to incorrect handling of trivia,
|
||||||
// it's omitted right now.
|
// it's omitted right now.
|
||||||
|
// TODO: GH#18445
|
||||||
verify.fileAfterApplyingRefactorAtMarker('1',
|
verify.fileAfterApplyingRefactorAtMarker('1',
|
||||||
`class fn {
|
`class fn {\r
|
||||||
constructor() {
|
constructor() {\r
|
||||||
this.baz = 10;
|
this.baz = 10;\r
|
||||||
}
|
}\r
|
||||||
bar() {
|
bar() {\r
|
||||||
console.log('hello world');
|
console.log('hello world');\r
|
||||||
}
|
}\r
|
||||||
}
|
}\r
|
||||||
`, 'Convert to ES2015 class', 'convert');
|
`, 'Convert to ES2015 class', 'convert');
|
||||||
|
|
Loading…
Reference in a new issue