Skip to content

Fix acquiring format options for getEditsForRefactor #18848

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
4 commits merged into from
Oct 2, 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
56 changes: 56 additions & 0 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
});
});
}
3 changes: 1 addition & 2 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,14 @@ namespace ts.server {

getEditsForRefactor(
fileName: string,
formatOptions: FormatCodeSettings,
_formatOptions: FormatCodeSettings,
positionOrRange: number | TextRange,
refactorName: string,
actionName: string): RefactorEditInfo {

const args = this.createFileLocationOrRangeRequestArgs(positionOrRange, fileName) as protocol.GetEditsForRefactorRequestArgs;
args.refactor = refactorName;
args.action = actionName;
args.formatOptions = formatOptions;

const request = this.processRequest<protocol.GetEditsForRefactorRequest>(CommandNames.GetEditsForRefactor, args);
const response = this.processResponse<protocol.GetEditsForRefactorResponse>(request);
Expand Down
1 change: 0 additions & 1 deletion src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ namespace ts.server.protocol {
refactor: string;
/* The 'name' property from the refactoring action */
action: string;
formatOptions?: FormatCodeSettings,
};


Expand Down
2 changes: 1 addition & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ namespace ts.server {

const result = project.getLanguageService().getEditsForRefactor(
file,
args.formatOptions ? convertFormatOptions(args.formatOptions) : this.projectService.getFormatCodeOptions(),
this.projectService.getFormatCodeOptions(file),
position || textRange,
args.refactor,
args.action
Expand Down
17 changes: 9 additions & 8 deletions tests/cases/fourslash/server/convertFunctionToEs6Class-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
verify.applicableRefactorAvailableAtMarker('1');
// NOTE: '// Comment' should be included, but due to incorrect handling of trivia,
// it's omitted right now.
// TODO: GH#18445
verify.fileAfterApplyingRefactorAtMarker('1',
`class fn {
constructor() {
this.baz = 10;
}
bar() {
console.log('hello world');
}
}
`class fn {\r
constructor() {\r
this.baz = 10;\r
}\r
bar() {\r
console.log('hello world');\r
}\r
}\r
`, 'Convert to ES2015 class', 'convert');