Skip to content

Commit eefe5c9

Browse files
author
Andy
authored
Fix acquiring format options for getEditsForRefactor (#18848)
* Fix acquiring format options for getEditsForRefactor * Add test * Fix test description * Use `executeCommandSeq`
1 parent 637ed57 commit eefe5c9

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,4 +4245,60 @@ namespace ts.projectSystem {
42454245
}
42464246
});
42474247
});
4248+
4249+
describe("refactors", () => {
4250+
it("use formatting options", () => {
4251+
const file = {
4252+
path: "/a.ts",
4253+
content: "function f() {\n 1;\n}",
4254+
};
4255+
const host = createServerHost([file]);
4256+
const session = createSession(host);
4257+
openFilesForSession([file], session);
4258+
4259+
const response0 = session.executeCommandSeq<server.protocol.ConfigureRequest>({
4260+
command: server.protocol.CommandTypes.Configure,
4261+
arguments: {
4262+
formatOptions: {
4263+
indentSize: 2,
4264+
},
4265+
},
4266+
}).response;
4267+
assert.deepEqual(response0, /*expected*/ undefined);
4268+
4269+
const response1 = session.executeCommandSeq<server.protocol.GetEditsForRefactorRequest>({
4270+
command: server.protocol.CommandTypes.GetEditsForRefactor,
4271+
arguments: {
4272+
refactor: "Extract Symbol",
4273+
action: "function_scope_1",
4274+
file: "/a.ts",
4275+
startLine: 2,
4276+
startOffset: 3,
4277+
endLine: 2,
4278+
endOffset: 4,
4279+
},
4280+
}).response;
4281+
assert.deepEqual(response1, {
4282+
edits: [
4283+
{
4284+
fileName: "/a.ts",
4285+
textChanges: [
4286+
{
4287+
start: { line: 2, offset: 1 },
4288+
end: { line: 3, offset: 1 },
4289+
newText: " newFunction();\n",
4290+
},
4291+
{
4292+
start: { line: 3, offset: 2 },
4293+
end: { line: 3, offset: 2 },
4294+
newText: "\nfunction newFunction() {\n 1;\n}\n",
4295+
},
4296+
]
4297+
}
4298+
],
4299+
renameFilename: "/a.ts",
4300+
renameLocation: { line: 2, offset: 3 },
4301+
});
4302+
});
4303+
});
42484304
}

src/server/client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,15 +573,14 @@ namespace ts.server {
573573

574574
getEditsForRefactor(
575575
fileName: string,
576-
formatOptions: FormatCodeSettings,
576+
_formatOptions: FormatCodeSettings,
577577
positionOrRange: number | TextRange,
578578
refactorName: string,
579579
actionName: string): RefactorEditInfo {
580580

581581
const args = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName) as protocol.GetEditsForRefactorRequestArgs;
582582
args.refactor = refactorName;
583583
args.action = actionName;
584-
args.formatOptions = formatOptions;
585584

586585
const request = this.processRequest<protocol.GetEditsForRefactorRequest>(CommandNames.GetEditsForRefactor, args);
587586
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);

src/server/protocol.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ namespace ts.server.protocol {
494494
refactor: string;
495495
/* The 'name' property from the refactoring action */
496496
action: string;
497-
formatOptions?: FormatCodeSettings,
498497
};
499498

500499

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ namespace ts.server {
14881488

14891489
const result = project.getLanguageService().getEditsForRefactor(
14901490
file,
1491-
args.formatOptions ? convertFormatOptions(args.formatOptions) : this.projectService.getFormatCodeOptions(),
1491+
this.projectService.getFormatCodeOptions(file),
14921492
position || textRange,
14931493
args.refactor,
14941494
args.action

tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
verify.applicableRefactorAvailableAtMarker('1');
1515
// NOTE: '// Comment' should be included, but due to incorrect handling of trivia,
1616
// it's omitted right now.
17+
// TODO: GH#18445
1718
verify.fileAfterApplyingRefactorAtMarker('1',
18-
`class fn {
19-
constructor() {
20-
this.baz = 10;
21-
}
22-
bar() {
23-
console.log('hello world');
24-
}
25-
}
19+
`class fn {\r
20+
constructor() {\r
21+
this.baz = 10;\r
22+
}\r
23+
bar() {\r
24+
console.log('hello world');\r
25+
}\r
26+
}\r
2627
`, 'Convert to ES2015 class', 'convert');

0 commit comments

Comments
 (0)