Skip to content

Commit b949405

Browse files
author
Andy
authored
Allow applyCodeActionCommand to take an array (microsoft#19870)
* Allow applyCodeActionCommand to take an array * Use this.host.newLine
1 parent 65a191f commit b949405

File tree

7 files changed

+22
-4
lines changed

7 files changed

+22
-4
lines changed

src/server/protocol.ts

+1
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ namespace ts.server.protocol {
586586
}
587587

588588
export interface ApplyCodeActionCommandRequestArgs extends FileRequestArgs {
589+
/** May also be an array of commands. */
589590
command: {};
590591
}
591592

src/server/session.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1571,9 +1571,12 @@ namespace ts.server {
15711571
private applyCodeActionCommand(commandName: string, requestSeq: number, args: protocol.ApplyCodeActionCommandRequestArgs): void {
15721572
const { file, project } = this.getFileAndProject(args);
15731573
const output = (success: boolean, message: string) => this.doOutput({}, commandName, requestSeq, success, message);
1574-
const command = args.command as CodeActionCommand; // They should be sending back the command we sent them.
1574+
const command = args.command as CodeActionCommand | CodeActionCommand[]; // They should be sending back the command we sent them.
1575+
15751576
project.getLanguageService().applyCodeActionCommand(file, command).then(
1576-
({ successMessage }) => { output(/*success*/ true, successMessage); },
1577+
result => {
1578+
output(/*success*/ true, isArray(result) ? result.map(res => res.successMessage).join(`${this.host.newLine}${this.host.newLine}`) : result.successMessage);
1579+
},
15771580
error => { output(/*success*/ false, error); });
15781581
}
15791582

src/services/services.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1868,8 +1868,14 @@ namespace ts {
18681868
});
18691869
}
18701870

1871-
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult> {
1872-
fileName = toPath(fileName, currentDirectory, getCanonicalFileName);
1871+
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
1872+
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
1873+
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]> {
1874+
const path = toPath(fileName, currentDirectory, getCanonicalFileName);
1875+
return isArray(action) ? Promise.all(action.map(a => applySingleCodeActionCommand(path, a))) : applySingleCodeActionCommand(path, action);
1876+
}
1877+
1878+
function applySingleCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult> {
18731879
switch (action.type) {
18741880
case "install package":
18751881
return host.installPackage

src/services/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ namespace ts {
294294

295295
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
296296
applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
297+
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
298+
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
297299
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
298300
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
299301

src/services/utilities.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
interface PromiseConstructor {
44
new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
55
reject(reason: any): Promise<never>;
6+
all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
67
}
78
/* @internal */
89
declare var Promise: PromiseConstructor;

tests/baselines/reference/api/tsserverlibrary.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3965,6 +3965,8 @@ declare namespace ts {
39653965
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
39663966
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
39673967
applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
3968+
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
3969+
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
39683970
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
39693971
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
39703972
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
@@ -5263,6 +5265,7 @@ declare namespace ts.server.protocol {
52635265
errorCodes?: number[];
52645266
}
52655267
interface ApplyCodeActionCommandRequestArgs extends FileRequestArgs {
5268+
/** May also be an array of commands. */
52665269
command: {};
52675270
}
52685271
/**

tests/baselines/reference/api/typescript.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3965,6 +3965,8 @@ declare namespace ts {
39653965
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
39663966
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
39673967
applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
3968+
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
3969+
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
39683970
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
39693971
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
39703972
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;

0 commit comments

Comments
 (0)