Skip to content

Commit 56ee47f

Browse files
devversionAndrewKushnir
authored andcommitted
feat(language-service): allow code refactorings to compute edits asynchronously (#57214)
VSCode explicitly split code actions into two stages: - what actions are active? - what are the edits, if the user presses the button. The latter stage may take longer to compute complex edits, perform analysis. This stage is currently implemented via our non-LSP standard `applyRefactoring` method. We should make it asynchronous, so that it can easily integrate with migrations that aren't synchronous/or compute in parallel. Long-term we may want to revisit this given integration in 1P with the language service as an actual TS server plugin; but it's not necessary right now and we shouldn't block the effort on this for now. PR Close #57214
1 parent 4900225 commit 56ee47f

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

packages/language-service/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export interface NgLanguageService extends ts.LanguageService {
9191
positionOrRange: number | ts.TextRange,
9292
refactorName: string,
9393
reportProgress: ApplyRefactoringProgressFn,
94-
): ts.RefactorEditInfo | undefined;
94+
): Promise<ts.RefactorEditInfo | undefined>;
9595

9696
hasCodeFixesForErrorCode(errorCode: number): boolean;
9797
}

packages/language-service/src/language_service.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,26 @@ export class LanguageService {
549549
);
550550
}
551551

552-
applyRefactoring(
552+
/**
553+
* Computes edits for applying the specified refactoring.
554+
*
555+
* VSCode explicitly split code actions into two stages:
556+
*
557+
* - 1) what actions are active?
558+
* - 2) what are the edits? <- if the user presses the button
559+
*
560+
* The latter stage may take longer to compute complex edits, perform
561+
* analysis. This stage is currently implemented via our non-LSP standard
562+
* `applyRefactoring` method. We implemented it in a way to support asynchronous
563+
* computation, so that it can easily integrate with migrations that aren't
564+
* synchronous/or compute edits in parallel.
565+
*/
566+
async applyRefactoring(
553567
fileName: string,
554568
positionOrRange: number | ts.TextRange,
555569
refactorName: string,
556570
reportProgress: ApplyRefactoringProgressFn,
557-
): ts.RefactorEditInfo | undefined {
571+
): Promise<ts.RefactorEditInfo | undefined> {
558572
const matchingRefactoring = allRefactorings.find((r) => r.id === refactorName);
559573
if (matchingRefactoring === undefined) {
560574
return undefined;

packages/language-service/src/refactorings/refactoring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface Refactoring {
4040
fileName: string,
4141
positionOrRange: number | ts.TextRange,
4242
reportProgress: ApplyRefactoringProgressFn,
43-
): ts.RefactorEditInfo;
43+
): Promise<ts.RefactorEditInfo>;
4444
}
4545

4646
export const allRefactorings: Refactoring[] = [];

packages/language-service/src/ts_plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export function create(info: ts.server.PluginCreateInfo): NgLanguageService {
281281
positionOrRange: number | ts.TextRange,
282282
refactorName: string,
283283
reportProgress: ApplyRefactoringProgressFn,
284-
): ts.RefactorEditInfo | undefined {
284+
): Promise<ts.RefactorEditInfo | undefined> {
285285
return ngLS.applyRefactoring(fileName, positionOrRange, refactorName, reportProgress);
286286
}
287287

0 commit comments

Comments
 (0)