Skip to content

Commit 683a0a0

Browse files
committed
address CR feedback
1 parent ba6e22c commit 683a0a0

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

src/server/session.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ namespace ts.server {
14471447
}
14481448

14491449
private extractPositionAndRange(args: protocol.FileLocationOrRangeRequestArgs, scriptInfo: ScriptInfo): { position: number, textRange: TextRange } {
1450-
let position: number;
1450+
let position: number = undefined;
14511451
let textRange: TextRange;
14521452
if (this.isLocation(args)) {
14531453
position = getPosition(args);
@@ -1481,7 +1481,7 @@ namespace ts.server {
14811481
position || textRange,
14821482
args.refactorName
14831483
);
1484-
return map(result, action => this.mapCodeAction(action, scriptInfo));
1484+
return result ? map(result, action => this.mapCodeAction(action, scriptInfo)) : undefined;
14851485
}
14861486

14871487
private getCodeFixes(args: protocol.CodeFixRequestArgs, simplifiedResult: boolean): protocol.CodeAction[] | CodeAction[] {
@@ -1507,12 +1507,24 @@ namespace ts.server {
15071507
}
15081508

15091509
private getStartAndEndPosition(args: protocol.FileRangeRequestArgs, scriptInfo: ScriptInfo) {
1510-
const startPosition = args.startPosition !== undefined
1511-
? args.startPosition
1512-
: args.startPosition = scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset);
1513-
const endPosition = args.endPosition !== undefined
1514-
? args.endPosition
1515-
: args.startPosition = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset);
1510+
let startPosition: number = undefined, endPosition: number = undefined;
1511+
if (args.startPosition !== undefined ) {
1512+
startPosition = args.startPosition;
1513+
}
1514+
else {
1515+
startPosition = scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset);
1516+
// save the result so we don't always recompute
1517+
args.startPosition = startPosition;
1518+
}
1519+
1520+
if (args.endPosition !== undefined) {
1521+
endPosition = args.endPosition;
1522+
}
1523+
else {
1524+
endPosition = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset);
1525+
args.endPosition = endPosition;
1526+
}
1527+
15161528
return { startPosition, endPosition };
15171529
}
15181530

src/services/refactorProvider.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ namespace ts {
1414
isApplicableForPositionOrRange(context: LightRefactorContext, positionOrRange: number | TextRange): boolean;
1515
}
1616

17+
/**
18+
* The `GetApplicableRefactor` API call is supposed to be fast, therefore only syntactic checks should be conducted
19+
* to see if a refactor is applicable. The `LightRefactorContent` limits the context information accesable to the
20+
* refactor to enforce such design. Such context should not provide a bound source file with symbols.
21+
*/
1722
export interface LightRefactorContext {
1823
/**
1924
* The AST that was not bound, so the symbols associated with the nodes are not accessible.
@@ -55,17 +60,17 @@ namespace ts {
5560
export function getRefactorCodeActions(
5661
context: RefactorContext,
5762
positionOrRange: number | TextRange,
58-
refactorName: string) {
63+
refactorName: string): CodeAction[] | undefined {
5964

60-
const result: CodeAction[] = [];
65+
let result: CodeAction[];
6166
const refactor = refactors.get(refactorName);
6267
if (!refactor) {
6368
return undefined;
6469
}
6570

6671
const codeActions = refactor.getCodeActions(context, positionOrRange);
6772
if (codeActions) {
68-
addRange(result, codeActions);
73+
addRange((result || (result = [])), codeActions);
6974
}
7075
return result;
7176
}

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,7 @@ namespace ts {
19991999
fileName: string,
20002000
formatOptions: FormatCodeSettings,
20012001
positionOrRange: number | TextRange,
2002-
refactorName: string): CodeAction[] {
2002+
refactorName: string): CodeAction[] | undefined {
20032003

20042004
const context: RefactorContext = {
20052005
boundSourceFile: getValidSourceFile(fileName),

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ namespace ts {
264264
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];
265265

266266
getApplicableRefactors(fileName: string, positionOrRaneg: number | TextRange): ApplicableRefactorInfo[];
267-
getRefactorCodeActions(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string): CodeAction[];
267+
getRefactorCodeActions(fileName: string, formatOptions: FormatCodeSettings, positionOrRange: number | TextRange, refactorName: string): CodeAction[] | undefined;
268268

269269
getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput;
270270

0 commit comments

Comments
 (0)