Skip to content

Commit c29e9b7

Browse files
author
Paul van Brenk
committed
PR feedback
1 parent 1e9b653 commit c29e9b7

6 files changed

Lines changed: 40 additions & 72 deletions

File tree

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ namespace Harness.LanguageService {
487487
return unwrapJSONCallResult(this.shim.isValidBraceCompletionAtPosition(fileName, position, openingBrace));
488488
}
489489
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[]): ts.CodeAction[] {
490-
return unwrapJSONCallResult(this.shim.getCodeFixesAtPosition(fileName, start, end, JSON.stringify(errorCodes)));
490+
throw new Error("Not supported on the shim.");
491491
}
492492
getEmitOutput(fileName: string): ts.EmitOutput {
493493
return unwrapJSONCallResult(this.shim.getEmitOutput(fileName));

src/server/protocol.d.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ declare namespace ts.server.protocol {
204204
}
205205

206206
/**
207-
* Request for the available codefixed at a specific position.
207+
* Request for the available codefixes at a specific position.
208208
*/
209209
export interface CodeFixRequest extends Request {
210210
arguments: CodeFixRequestArgs;
@@ -924,7 +924,12 @@ declare namespace ts.server.protocol {
924924
textChanges: CodeEdit[];
925925
}
926926

927-
export interface CodeActionResponse extends Response {
927+
export interface CodeFixResponse extends Response {
928+
/** The code actions that are available */
929+
codeActions: CodeAction[];
930+
}
931+
932+
export interface CodeAction {
928933
/** Description of the code action to display in the UI of the editor */
929934
description: string;
930935
/** Text changes to apply to each file as part of the code action */
@@ -1586,30 +1591,6 @@ declare namespace ts.server.protocol {
15861591
/**
15871592
* Changes to apply to each file as part of the code action.
15881593
*/
1589-
changes: FileTextChanges[];
1590-
}
1591-
1592-
export interface FileTextChanges {
1593-
/**
1594-
* File to apply the change to.
1595-
*/
1596-
fileName: string;
1597-
1598-
/**
1599-
* Changes to apply to the file.
1600-
*/
1601-
textChanges: TextChange[];
1602-
}
1603-
1604-
export class TextChange {
1605-
/**
1606-
* The span for the text change.
1607-
*/
1608-
span: TextSpan;
1609-
1610-
/**
1611-
* New text for the span, can be an empty string if we want to delete text.
1612-
*/
1613-
newText: string;
1594+
changes: FileCodeEdits[];
16141595
}
16151596
}

src/server/session.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ namespace ts.server {
136136
export const CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects";
137137
export const GetCodeFixes = "getCodeFixes";
138138
export const GetCodeFixesFull = "getCodeFixes-full";
139+
export const GetSupportedCodeFixes = "getSupportedCodeFixes";
139140
}
140141

141142
export function formatMessage<T extends protocol.Message>(msg: T, logger: server.Logger, byteLength: (s: string, encoding: string) => number, newLine: string): string {
@@ -844,13 +845,7 @@ namespace ts.server {
844845
return undefined;
845846
}
846847

847-
return edits.map((edit) => {
848-
return {
849-
start: scriptInfo.positionToLineOffset(edit.span.start),
850-
end: scriptInfo.positionToLineOffset(ts.textSpanEnd(edit.span)),
851-
newText: edit.newText ? edit.newText : ""
852-
};
853-
});
848+
return edits.map(edit => this.convertTextChangeToCodeEdit(edit, scriptInfo));
854849
}
855850

856851
private getFormattingEditsForRangeFull(args: protocol.FormatRequestArgs) {
@@ -1201,6 +1196,10 @@ namespace ts.server {
12011196
}
12021197
}
12031198

1199+
private getSupportedCodeFixes(): string[] {
1200+
return ts.getSupportedCodeFixes();
1201+
}
1202+
12041203
private getCodeFixes(args: protocol.CodeFixRequestArgs, simplifiedResult: boolean): protocol.CodeAction[] | CodeAction[] {
12051204
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
12061205

@@ -1213,28 +1212,12 @@ namespace ts.server {
12131212
return undefined;
12141213
}
12151214
if (simplifiedResult) {
1216-
return codeActions.map(mapCodeAction);
1215+
return codeActions.map(codeAction => this.mapCodeAction(codeAction, scriptInfo));
12171216
}
12181217
else {
12191218
return codeActions;
12201219
}
12211220

1222-
function mapCodeAction(source: CodeAction): protocol.CodeAction {
1223-
return {
1224-
description: source.description,
1225-
changes: source.changes.map(change => ({
1226-
fileName: change.fileName,
1227-
textChanges: change.textChanges.map(textChange => ({
1228-
span: {
1229-
start: scriptInfo.positionToLineOffset(textChange.span.start),
1230-
end: scriptInfo.positionToLineOffset(textChange.span.start + textChange.span.length)
1231-
},
1232-
newText: textChange.newText
1233-
}))
1234-
}))
1235-
};
1236-
}
1237-
12381221
function getStartPosition() {
12391222
return args.startPosition !== undefined ? args.startPosition : scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset);
12401223
}
@@ -1244,6 +1227,24 @@ namespace ts.server {
12441227
}
12451228
}
12461229

1230+
private mapCodeAction(codeAction: CodeAction, scriptInfo: ScriptInfo): protocol.CodeAction {
1231+
return {
1232+
description: codeAction.description,
1233+
changes: codeAction.changes.map(change => ({
1234+
fileName: change.fileName,
1235+
textChanges: change.textChanges.map(textChange => this.convertTextChangeToCodeEdit(textChange, scriptInfo))
1236+
}))
1237+
};
1238+
}
1239+
1240+
private convertTextChangeToCodeEdit(change: ts.TextChange, scriptInfo: ScriptInfo): protocol.CodeEdit {
1241+
return {
1242+
start: scriptInfo.positionToLineOffset(change.span.start),
1243+
end: scriptInfo.positionToLineOffset(change.span.start + change.span.length),
1244+
newText: change.newText
1245+
};
1246+
}
1247+
12471248
private getBraceMatching(args: protocol.FileLocationRequestArgs, simplifiedResult: boolean): protocol.TextSpan[] | TextSpan[] {
12481249
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
12491250

@@ -1572,6 +1573,9 @@ namespace ts.server {
15721573
},
15731574
[CommandNames.GetCodeFixesFull]: (request: protocol.CodeFixRequest) => {
15741575
return this.requiredResponse(this.getCodeFixes(request.arguments, /*simplifiedResult*/ false));
1576+
},
1577+
[CommandNames.GetSupportedCodeFixes]: (request: protocol.Request) => {
1578+
return this.requiredResponse(this.getSupportedCodeFixes());
15751579
}
15761580
});
15771581

src/services/codefixes/codeFixProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace ts {
33
export interface CodeFix {
44
errorCodes: number[];
5-
getCodeActions(context: CodeFixContext): CodeAction[];
5+
getCodeActions(context: CodeFixContext): CodeAction[] | undefined;
66
}
77

88
export interface CodeFixContext {

src/services/services.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,8 @@ namespace ts {
16481648
let allFixes: CodeAction[] = [];
16491649

16501650
forEach(errorCodes, error => {
1651+
cancellationToken.throwIfCancellationRequested();
1652+
16511653
const context = {
16521654
errorCode: error,
16531655
sourceFile: sourceFile,

src/services/shims.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ namespace ts {
251251
*/
252252
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): string;
253253

254-
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: string): string;
255-
256254
getEmitOutput(fileName: string): string;
257255
getEmitOutputObject(fileName: string): EmitOutput;
258256
}
@@ -268,7 +266,6 @@ namespace ts {
268266
getTSConfigFileInfo(fileName: string, sourceText: IScriptSnapshot): string;
269267
getDefaultCompilationSettings(): string;
270268
discoverTypings(discoverTypingsJson: string): string;
271-
getSupportedCodeFixes(): string;
272269
}
273270

274271
function logInternalError(logger: Logger, err: Error) {
@@ -957,16 +954,6 @@ namespace ts {
957954
);
958955
}
959956

960-
public getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: string): string {
961-
return this.forwardJSONCall(
962-
`getCodeFixesAtPosition( '${fileName}', ${start}, ${end}, ${errorCodes}')`,
963-
() => {
964-
const localErrors: number[] = JSON.parse(errorCodes);
965-
return this.languageService.getCodeFixesAtPosition(fileName, start, end, localErrors);
966-
}
967-
);
968-
}
969-
970957
/// NAVIGATE TO
971958

972959
/** Return a list of symbols that are interesting to navigate to */
@@ -1175,12 +1162,6 @@ namespace ts {
11751162
info.compilerOptions);
11761163
});
11771164
}
1178-
1179-
public getSupportedCodeFixes(): string {
1180-
return this.forwardJSONCall("getSupportedCodeFixes()",
1181-
() => getSupportedCodeFixes()
1182-
);
1183-
}
11841165
}
11851166

11861167
export class TypeScriptServicesFactory implements ShimFactory {

0 commit comments

Comments
 (0)