Skip to content

Commit c56a822

Browse files
author
Andy
authored
Allow applyCodeActionCommand to take an array (#19870) (#19884)
* Allow applyCodeActionCommand to take an array * Use this.host.newLine
1 parent 047854d commit c56a822

File tree

7 files changed

+22
-4
lines changed

7 files changed

+22
-4
lines changed

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ namespace ts.server.protocol {
583583
}
584584

585585
export interface ApplyCodeActionCommandRequestArgs extends FileRequestArgs {
586+
/** May also be an array of commands. */
586587
command: {};
587588
}
588589

src/server/session.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,12 @@ namespace ts.server {
15571557
private applyCodeActionCommand(commandName: string, requestSeq: number, args: protocol.ApplyCodeActionCommandRequestArgs): void {
15581558
const { file, project } = this.getFileAndProject(args);
15591559
const output = (success: boolean, message: string) => this.doOutput({}, commandName, requestSeq, success, message);
1560-
const command = args.command as CodeActionCommand; // They should be sending back the command we sent them.
1560+
const command = args.command as CodeActionCommand | CodeActionCommand[]; // They should be sending back the command we sent them.
1561+
15611562
project.getLanguageService().applyCodeActionCommand(file, command).then(
1562-
({ successMessage }) => { output(/*success*/ true, successMessage); },
1563+
result => {
1564+
output(/*success*/ true, isArray(result) ? result.map(res => res.successMessage).join(`${this.host.newLine}${this.host.newLine}`) : result.successMessage);
1565+
},
15631566
error => { output(/*success*/ false, error); });
15641567
}
15651568

src/services/services.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,8 +1786,14 @@ namespace ts {
17861786
});
17871787
}
17881788

1789-
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult> {
1790-
fileName = toPath(fileName, currentDirectory, getCanonicalFileName);
1789+
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
1790+
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
1791+
function applyCodeActionCommand(fileName: Path, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]> {
1792+
const path = toPath(fileName, currentDirectory, getCanonicalFileName);
1793+
return isArray(action) ? Promise.all(action.map(a => applySingleCodeActionCommand(path, a))) : applySingleCodeActionCommand(path, action);
1794+
}
1795+
1796+
function applySingleCodeActionCommand(fileName: Path, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult> {
17911797
switch (action.type) {
17921798
case "install package":
17931799
return host.installPackage

src/services/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ namespace ts {
290290

291291
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
292292
applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
293+
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
294+
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
293295
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
294296
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
295297

src/services/utilities.ts

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,8 @@ declare namespace ts {
39463946
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
39473947
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
39483948
applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
3949+
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
3950+
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
39493951
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
39503952
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
39513953
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
@@ -5239,6 +5241,7 @@ declare namespace ts.server.protocol {
52395241
errorCodes?: number[];
52405242
}
52415243
interface ApplyCodeActionCommandRequestArgs extends FileRequestArgs {
5244+
/** May also be an array of commands. */
52425245
command: {};
52435246
}
52445247
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,8 @@ declare namespace ts {
39463946
getSpanOfEnclosingComment(fileName: string, position: number, onlyMultiLine: boolean): TextSpan;
39473947
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
39483948
applyCodeActionCommand(fileName: string, action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
3949+
applyCodeActionCommand(fileName: string, action: CodeActionCommand[]): Promise<ApplyCodeActionCommandResult[]>;
3950+
applyCodeActionCommand(fileName: string, action: CodeActionCommand | CodeActionCommand[]): Promise<ApplyCodeActionCommandResult | ApplyCodeActionCommandResult[]>;
39493951
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
39503952
getEditsForRefactor(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string, actionName: string): RefactorEditInfo | undefined;
39513953
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;

0 commit comments

Comments
 (0)