Skip to content

Make getSupportedCodeFixes on LS so it can be proxied by plugins #51769

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
merged 1 commit into from
Dec 8, 2022
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
5 changes: 5 additions & 0 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
FormatCodeOptions,
FormatCodeSettings,
getSnapshotText,
getSupportedCodeFixes,
identity,
ImplementationLocation,
InlayHint,
Expand Down Expand Up @@ -950,6 +951,10 @@ export class SessionClient implements LanguageService {
return response.body.map(item => this.convertCallHierarchyOutgoingCall(fileName, item));
}

getSupportedCodeFixes(): readonly string[] {
return getSupportedCodeFixes();
}

getProgram(): Program {
throw new Error("Program objects are not serializable through the server protocol.");
}
Expand Down
3 changes: 3 additions & 0 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ class LanguageServiceShimProxy implements ts.LanguageService {
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): ts.TextSpan {
return unwrapJSONCallResult(this.shim.getSpanOfEnclosingComment(fileName, position, onlyMultiLine));
}
getSupportedCodeFixes(): never {
throw new Error("Not supported on the shim.");
}
getCodeFixesAtPosition(): never {
throw new Error("Not supported on the shim.");
}
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ export interface FileLocationRequest extends FileRequest {
*/
export interface GetSupportedCodeFixesRequest extends Request {
command: CommandTypes.GetSupportedCodeFixes;
arguments?: Partial<FileRequestArgs>;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2640,8 +2640,15 @@ export class Session<TMessage = string> implements EventSender {
}
}

private getSupportedCodeFixes(): string[] {
return getSupportedCodeFixes();
private getSupportedCodeFixes(args: Partial<protocol.FileRequestArgs> | undefined): readonly string[] {
if (!args) return getSupportedCodeFixes(); // Compatibility
if (args.file) {
const { file, project } = this.getFileAndProject(args as protocol.FileRequestArgs);
return project.getLanguageService().getSupportedCodeFixes(file);
}
const project = this.getProject(args.projectFileName);
if (!project) Errors.ThrowNoProject();
return project.getLanguageService().getSupportedCodeFixes();
}

private isLocation(locationOrSpan: protocol.FileLocationOrRangeRequestArgs): locationOrSpan is protocol.FileLocationRequestArgs {
Expand Down Expand Up @@ -3419,8 +3426,8 @@ export class Session<TMessage = string> implements EventSender {
[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
2 changes: 1 addition & 1 deletion src/services/codeFixProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function registerCodeFix(reg: CodeFixRegistration) {
}

/** @internal */
export function getSupportedErrorCodes(): string[] {
export function getSupportedErrorCodes(): readonly string[] {
return arrayFrom(errorCodeToFixes.keys());
}

Expand Down
4 changes: 3 additions & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,8 @@ const invalidOperationsInPartialSemanticMode: readonly (keyof LanguageService)[]
"prepareCallHierarchy",
"provideCallHierarchyIncomingCalls",
"provideCallHierarchyOutgoingCalls",
"provideInlayHints"
"provideInlayHints",
"getSupportedCodeFixes",
];

const invalidOperationsInSyntacticMode: readonly (keyof LanguageService)[] = [
Expand Down Expand Up @@ -3046,6 +3047,7 @@ export function createLanguageService(
commentSelection,
uncommentSelection,
provideInlayHints,
getSupportedCodeFixes,
};

switch (languageServiceMode) {
Expand Down
2 changes: 2 additions & 0 deletions src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ export interface LanguageService {
commentSelection(fileName: string, textRange: TextRange): TextChange[];
uncommentSelection(fileName: string, textRange: TextRange): TextChange[];

getSupportedCodeFixes(fileName?: string): readonly string[];

dispose(): void;
}

Expand Down
69 changes: 69 additions & 0 deletions src/testRunner/unittests/tsserver/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,73 @@ describe("unittests:: tsserver:: plugins loading", () => {

baselineTsserverLogs("plugins", "gets external files with config file reload", session);
});
});

describe("unittests:: tsserver:: plugins overriding getSupportedCodeFixes", () => {
it("getSupportedCodeFixes can be proxied", () => {
const aTs: File = {
path: "/a.ts",
content: `class c { prop = "hello"; foo() { const x = 0; } }`
};
const bTs: File = {
path: "/b.ts",
content: aTs.content
};
const cTs: File = {
path: "/c.ts",
content: aTs.content
};
const config: File = {
path: "/tsconfig.json",
content: JSON.stringify({
compilerOptions: { plugins: [{ name: "myplugin" }] }
})
};
const host = createServerHost([aTs, bTs, cTs, config, libFile]);
host.require = () => {
return {
module: () => ({
create(info: ts.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
};
};
const session = createSession(host, { logger: createLoggerWithInMemoryLogs(host) });
openFilesForSession([aTs, bTs, cTs], session);
// Without arguments
session.executeCommandSeq<ts.server.protocol.GetSupportedCodeFixesRequest>({
command: ts.server.protocol.CommandTypes.GetSupportedCodeFixes,
});
session.executeCommandSeq<ts.server.protocol.GetSupportedCodeFixesRequest>({
command: ts.server.protocol.CommandTypes.GetSupportedCodeFixes,
arguments: { file: aTs.path }
});
session.executeCommandSeq<ts.server.protocol.GetSupportedCodeFixesRequest>({
command: ts.server.protocol.CommandTypes.GetSupportedCodeFixes,
arguments: { file: bTs.path }
});
session.executeCommandSeq<ts.server.protocol.GetSupportedCodeFixesRequest>({
command: ts.server.protocol.CommandTypes.GetSupportedCodeFixes,
arguments: { file: cTs.path }
});
session.executeCommandSeq<ts.server.protocol.GetSupportedCodeFixesRequest>({
command: ts.server.protocol.CommandTypes.GetSupportedCodeFixes,
arguments: { projectFileName: config.path }
});

baselineTsserverLogs("plugins", "getSupportedCodeFixes can be proxied", session);
});
});
4 changes: 3 additions & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ declare namespace ts {
*/
interface GetSupportedCodeFixesRequest extends Request {
command: CommandTypes.GetSupportedCodeFixes;
arguments?: Partial<FileRequestArgs>;
}
/**
* A response for GetSupportedCodeFixesRequest request.
Expand Down Expand Up @@ -10038,6 +10039,7 @@ declare namespace ts {
toggleMultilineComment(fileName: string, textRange: TextRange): TextChange[];
commentSelection(fileName: string, textRange: TextRange): TextChange[];
uncommentSelection(fileName: string, textRange: TextRange): TextChange[];
getSupportedCodeFixes(fileName?: string): readonly string[];
dispose(): void;
}
interface JsxClosingTagInfo {
Expand Down Expand Up @@ -11020,7 +11022,7 @@ declare namespace ts {
function toEditorSettings(options: EditorOptions | EditorSettings): EditorSettings;
function displayPartsToString(displayParts: SymbolDisplayPart[] | undefined): string;
function getDefaultCompilerOptions(): CompilerOptions;
function getSupportedCodeFixes(): string[];
function getSupportedCodeFixes(): readonly string[];
function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTargetOrOptions: ScriptTarget | CreateSourceFileOptions, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange | undefined, aggressiveChecks?: boolean): SourceFile;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry, syntaxOnlyOrLanguageServiceMode?: boolean | LanguageServiceMode): LanguageService;
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6178,6 +6178,7 @@ declare namespace ts {
toggleMultilineComment(fileName: string, textRange: TextRange): TextChange[];
commentSelection(fileName: string, textRange: TextRange): TextChange[];
uncommentSelection(fileName: string, textRange: TextRange): TextChange[];
getSupportedCodeFixes(fileName?: string): readonly string[];
dispose(): void;
}
interface JsxClosingTagInfo {
Expand Down Expand Up @@ -7160,7 +7161,7 @@ declare namespace ts {
function toEditorSettings(options: EditorOptions | EditorSettings): EditorSettings;
function displayPartsToString(displayParts: SymbolDisplayPart[] | undefined): string;
function getDefaultCompilerOptions(): CompilerOptions;
function getSupportedCodeFixes(): string[];
function getSupportedCodeFixes(): readonly string[];
function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTargetOrOptions: ScriptTarget | CreateSourceFileOptions, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile;
function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange | undefined, aggressiveChecks?: boolean): SourceFile;
function createLanguageService(host: LanguageServiceHost, documentRegistry?: DocumentRegistry, syntaxOnlyOrLanguageServiceMode?: boolean | LanguageServiceMode): LanguageService;
Expand Down
Loading