Skip to content

LanguageService: add getSupportedCodeFixes to make it proxyable #29010

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,5 +741,9 @@ namespace ts.server {
dispose(): void {
throw new Error("dispose is not available through the server layer.");
}

getSupportedCodeFixes() {
return getSupportedCodeFixes();
}
}
}
3 changes: 3 additions & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ namespace Harness.LanguageService {
return ts.notImplemented();
}
dispose(): void { this.shim.dispose({}); }
getSupportedCodeFixes(): ReadonlyArray<string> {
throw new Error("Not supported on the shim.");
}
}

export class ShimLanguageServiceAdapter implements LanguageServiceAdapter {
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ namespace ts.server.protocol {
*/
export interface GetSupportedCodeFixesRequest extends Request {
command: CommandTypes.GetSupportedCodeFixes;
arguments?: Partial<FileRequestArgs>;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1803,7 +1803,11 @@ namespace ts.server {
}
}

private getSupportedCodeFixes(): string[] {
private getSupportedCodeFixes(args?: Partial<protocol.FileRequestArgs>): ReadonlyArray<string> {
if (args && args.file) {
const {file, project} = this.getFileAndProject(<protocol.FileRequestArgs>args);
return project.getLanguageService().getSupportedCodeFixes(file);
}
return getSupportedCodeFixes();
}

Expand Down Expand Up @@ -2342,8 +2346,8 @@ namespace ts.server {
[CommandNames.ApplyCodeActionCommand]: (request: protocol.ApplyCodeActionCommandRequest) => {
return this.requiredResponse(this.applyCodeActionCommand(request.arguments));
},
[CommandNames.GetSupportedCodeFixes]: () => {
return this.requiredResponse(this.getSupportedCodeFixes());
[CommandNames.GetSupportedCodeFixes]: (request: protocol.GetSupportedCodeFixesRequest) => {
return this.requiredResponse(this.getSupportedCodeFixes(request.arguments));
},
[CommandNames.GetApplicableRefactors]: (request: protocol.GetApplicableRefactorsRequest) => {
return this.requiredResponse(this.getApplicableRefactors(request.arguments));
Expand Down
1 change: 1 addition & 0 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2143,6 +2143,7 @@ namespace ts {
getEditsForRefactor,
toLineColumnOffset: sourceMapper.toLineColumnOffset,
getSourceMapper: () => sourceMapper,
getSupportedCodeFixes,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ namespace ts {
/* @internal */ getNonBoundSourceFile(fileName: string): SourceFile;

dispose(): void;
getSupportedCodeFixes(fileName: string): ReadonlyArray<string>;
}

export interface JsxClosingTagInfo {
Expand Down
72 changes: 72 additions & 0 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10949,6 +10949,78 @@ fn5();`
});
});

describe("tsserverProjectSystem getSupportedCodeFixes", () => {
const tsconfig: File = {
path: "/tsconfig.json",
content: JSON.stringify({
compilerOptions: { plugins: [{ name: "myplugin" }] }
})
};

function createHostWithPlugin(files: ReadonlyArray<File>) {
const host = createServerHost(files);
host.require = (_initialPath, moduleName) => {
assert.equal(moduleName, "myplugin");
return {
module: () => ({
create(info: server.PluginCreateInfo) {
const proxy = Harness.LanguageService.makeDefaultProxy(info);
proxy.getSupportedCodeFixes = (fileName) => {
switch (fileName) {
case "/a.ts":
return ["a"];
case "/b.ts":
return ["b"];
default:
return info.languageService.getSupportedCodeFixes(fileName);
}
};
return proxy;
}
}),
error: undefined
};
};
return host;
}

it("uses default if fileName is not provided", () => {
const aTs: File = { path: "/a.ts", content: `class c { prop = "hello"; foo() { const x = 0; } }` };
const host = createHostWithPlugin([aTs, tsconfig]);
const session = createSession(host);
openFilesForSession([aTs], session);
assert.deepStrictEqual(
executeSessionRequest<protocol.GetSupportedCodeFixesRequest, protocol.GetSupportedCodeFixesResponse>(session, protocol.CommandTypes.GetSupportedCodeFixes, /*args*/ undefined),
getSupportedCodeFixes(),
);
assert.deepStrictEqual(
executeSessionRequest<protocol.GetSupportedCodeFixesRequest, protocol.GetSupportedCodeFixesResponse>(session, protocol.CommandTypes.GetSupportedCodeFixes, {}),
getSupportedCodeFixes(),
);
});

it("forwards the request to the LanguageService", () => {
const aTs: File = { path: "/a.ts", content: `class c { prop = "hello"; foo() { const x = 0; } }` };
const bTs: File = { path: "/b.ts", content: `class c { prop = "hello"; foo() { const x = 0; } }` };
const cTs: File = { path: "/c.ts", content: `class c { prop = "hello"; foo() { const x = 0; } }` };
const host = createHostWithPlugin([aTs, bTs, cTs, tsconfig]);
const session = createSession(host);
openFilesForSession([aTs, bTs, cTs], session);
assert.deepStrictEqual(
executeSessionRequest<protocol.GetSupportedCodeFixesRequest, protocol.GetSupportedCodeFixesResponse>(session, protocol.CommandTypes.GetSupportedCodeFixes, { file: "/a.ts" }),
["a"],
);
assert.deepStrictEqual(
executeSessionRequest<protocol.GetSupportedCodeFixesRequest, protocol.GetSupportedCodeFixesResponse>(session, protocol.CommandTypes.GetSupportedCodeFixes, { file: "/b.ts" }),
["b"],
);
assert.deepStrictEqual(
executeSessionRequest<protocol.GetSupportedCodeFixesRequest, protocol.GetSupportedCodeFixesResponse>(session, protocol.CommandTypes.GetSupportedCodeFixes, { file: "/c.ts" }),
getSupportedCodeFixes(),
);
});
});

function makeReferenceItem(file: File, isDefinition: boolean, text: string, lineText: string, options?: SpanFromSubstringOptions): protocol.ReferencesResponseItem {
return {
...protocolFileSpanFromSubstring(file, text, options),
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4754,6 +4754,7 @@ declare namespace ts {
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
getProgram(): Program | undefined;
dispose(): void;
getSupportedCodeFixes(fileName: string): ReadonlyArray<string>;
}
interface JsxClosingTagInfo {
readonly newText: string;
Expand Down Expand Up @@ -6199,6 +6200,7 @@ declare namespace ts.server.protocol {
*/
interface GetSupportedCodeFixesRequest extends Request {
command: CommandTypes.GetSupportedCodeFixes;
arguments?: Partial<FileRequestArgs>;
}
/**
* A response for GetSupportedCodeFixesRequest request.
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4754,6 +4754,7 @@ declare namespace ts {
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
getProgram(): Program | undefined;
dispose(): void;
getSupportedCodeFixes(fileName: string): ReadonlyArray<string>;
}
interface JsxClosingTagInfo {
readonly newText: string;
Expand Down