Skip to content

Commit

Permalink
address CR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengbli committed Apr 18, 2017
1 parent ba6e22c commit 683a0a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
28 changes: 20 additions & 8 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ namespace ts.server {
}

private extractPositionAndRange(args: protocol.FileLocationOrRangeRequestArgs, scriptInfo: ScriptInfo): { position: number, textRange: TextRange } {
let position: number;
let position: number = undefined;
let textRange: TextRange;
if (this.isLocation(args)) {
position = getPosition(args);
Expand Down Expand Up @@ -1481,7 +1481,7 @@ namespace ts.server {
position || textRange,
args.refactorName
);
return map(result, action => this.mapCodeAction(action, scriptInfo));
return result ? map(result, action => this.mapCodeAction(action, scriptInfo)) : undefined;
}

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

private getStartAndEndPosition(args: protocol.FileRangeRequestArgs, scriptInfo: ScriptInfo) {
const startPosition = args.startPosition !== undefined
? args.startPosition
: args.startPosition = scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset);
const endPosition = args.endPosition !== undefined
? args.endPosition
: args.startPosition = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset);
let startPosition: number = undefined, endPosition: number = undefined;
if (args.startPosition !== undefined ) {
startPosition = args.startPosition;
}
else {
startPosition = scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset);
// save the result so we don't always recompute
args.startPosition = startPosition;
}

if (args.endPosition !== undefined) {
endPosition = args.endPosition;
}
else {
endPosition = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset);
args.endPosition = endPosition;
}

return { startPosition, endPosition };
}

Expand Down
11 changes: 8 additions & 3 deletions src/services/refactorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace ts {
isApplicableForPositionOrRange(context: LightRefactorContext, positionOrRange: number | TextRange): boolean;
}

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

const result: CodeAction[] = [];
let result: CodeAction[];
const refactor = refactors.get(refactorName);
if (!refactor) {
return undefined;
}

const codeActions = refactor.getCodeActions(context, positionOrRange);
if (codeActions) {
addRange(result, codeActions);
addRange((result || (result = [])), codeActions);
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,7 @@ namespace ts {
fileName: string,
formatOptions: FormatCodeSettings,
positionOrRange: number | TextRange,
refactorName: string): CodeAction[] {
refactorName: string): CodeAction[] | undefined {

const context: RefactorContext = {
boundSourceFile: getValidSourceFile(fileName),
Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ namespace ts {
getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[], formatOptions: FormatCodeSettings): CodeAction[];

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

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

Expand Down

0 comments on commit 683a0a0

Please sign in to comment.