Skip to content

Commit aa7ffdb

Browse files
committed
address CR feedback
1 parent c21b4ab commit aa7ffdb

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/compiler/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ module ts {
221221
FirstTypeNode = TypeReference,
222222
LastTypeNode = ArrayType,
223223
FirstPunctuation= OpenBraceToken,
224-
LastPunctuation = CaretEqualsToken
224+
LastPunctuation = CaretEqualsToken,
225+
FirstToken = EndOfFileToken,
226+
LastToken = StringKeyword
225227
}
226228

227229
export enum NodeFlags {

src/services/formatting/smartIndenter.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module ts.formatting {
4343
var indentation: number;
4444

4545
while (current) {
46-
if (isPositionBelongToNode(current, position, sourceFile) && isNodeContentIndented(current, previous)) {
46+
if (positionBelongsToNode(current, position, sourceFile) && nodeContentIsIndented(current, previous)) {
4747
currentStart = getStartLineAndCharacterForNode(current, sourceFile);
4848

4949
if (discardInitialIndentationIfNextTokenIsOpenOrCloseBrace(precedingToken, current, lineAtPosition, sourceFile)) {
@@ -88,7 +88,7 @@ module ts.formatting {
8888
parentStart = sourceFile.getLineAndCharacterFromPosition(parent.getStart(sourceFile));
8989
var parentAndChildShareLine =
9090
parentStart.line === currentStart.line ||
91-
isChildStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStart.line, sourceFile);
91+
childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStart.line, sourceFile);
9292

9393
// try to fetch actual indentation for current node from source text
9494
var actualIndentation = getActualIndentationForNode(current, parent, currentStart, parentAndChildShareLine, sourceFile, options);
@@ -97,7 +97,7 @@ module ts.formatting {
9797
}
9898

9999
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
100-
var increaseIndentation = isNodeContentIndented(parent, current) && !parentAndChildShareLine;
100+
var increaseIndentation = nodeContentIsIndented(parent, current) && !parentAndChildShareLine;
101101

102102
if (increaseIndentation) {
103103
indentation += options.indentSpaces;
@@ -153,6 +153,7 @@ module ts.formatting {
153153
}
154154
}
155155

156+
// function returns -1 if indentation cannot be determined
156157
function getActualIndentationForListItemBeforeComma(commaToken: Node, sourceFile: SourceFile, options: TypeScript.FormattingOptions): number {
157158
// previous token is comma that separates items in list - find the previous item and try to derive indentation from it
158159
var precedingListItem = findPrecedingListItem(commaToken);
@@ -166,6 +167,7 @@ module ts.formatting {
166167
return -1;
167168
}
168169

170+
// function returns -1 if actual indentation for node should not be used (i.e because node is nested expression)
169171
function getActualIndentationForNode(current: Node, parent: Node, currentLineAndChar: LineAndCharacter, parentAndChildShareLine: boolean, sourceFile: SourceFile, options: TypeScript.FormattingOptions): number {
170172
var useActualIndentation =
171173
(isDeclaration(current) || isStatement(current)) &&
@@ -227,11 +229,11 @@ module ts.formatting {
227229
return children[commaIndex - 1];
228230
}
229231

230-
function isPositionBelongToNode(candidate: Node, position: number, sourceFile: SourceFile): boolean {
232+
function positionBelongsToNode(candidate: Node, position: number, sourceFile: SourceFile): boolean {
231233
return candidate.end > position || !isCompletedNode(candidate, sourceFile);
232234
}
233235

234-
function isChildStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: Node, childStartLine: number, sourceFile: SourceFile): boolean {
236+
function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: Node, childStartLine: number, sourceFile: SourceFile): boolean {
235237
if (parent.kind === SyntaxKind.IfStatement && (<IfStatement>parent).elseStatement === child) {
236238
var elseKeyword = forEach(parent.getChildren(), c => c.kind === SyntaxKind.ElseKeyword && c);
237239
Debug.assert(elseKeyword);
@@ -404,10 +406,10 @@ module ts.formatting {
404406
}
405407

406408
function isToken(n: Node): boolean {
407-
return n.kind < SyntaxKind.Missing;
409+
return n.kind >= SyntaxKind.FirstToken && n.kind <= SyntaxKind.LastToken;
408410
}
409411

410-
function isNodeContentIndented(parent: Node, child: Node): boolean {
412+
function nodeContentIsIndented(parent: Node, child: Node): boolean {
411413
switch (parent.kind) {
412414
case SyntaxKind.ClassDeclaration:
413415
case SyntaxKind.InterfaceDeclaration:
@@ -456,7 +458,7 @@ module ts.formatting {
456458

457459
/// checks if node ends with 'expectedLastToken'.
458460
/// If child at position 'length - 1' is 'SemicolonToken' it is skipped and 'expectedLastToken' is compared with child at position 'length - 2'.
459-
function isNodeEndWith(n: Node, expectedLastToken: SyntaxKind, sourceFile: SourceFile): boolean {
461+
function nodeEndsWith(n: Node, expectedLastToken: SyntaxKind, sourceFile: SourceFile): boolean {
460462
var children = n.getChildren(sourceFile);
461463
if (children.length) {
462464
var last = children[children.length - 1];
@@ -470,7 +472,7 @@ module ts.formatting {
470472
return false;
471473
}
472474

473-
// this function is alwasy called when position of the cursor is located after the node
475+
// this function is always called when position of the cursor is located after the node
474476
function isCompletedNode(n: Node, sourceFile: SourceFile): boolean {
475477
switch (n.kind) {
476478
case SyntaxKind.ClassDeclaration:
@@ -483,11 +485,11 @@ module ts.formatting {
483485
case SyntaxKind.FunctionBlock:
484486
case SyntaxKind.ModuleBlock:
485487
case SyntaxKind.SwitchStatement:
486-
return isNodeEndWith(n, SyntaxKind.CloseBraceToken, sourceFile);
488+
return nodeEndsWith(n, SyntaxKind.CloseBraceToken, sourceFile);
487489
case SyntaxKind.ParenExpression:
488490
case SyntaxKind.CallSignature:
489491
case SyntaxKind.CallExpression:
490-
return isNodeEndWith(n, SyntaxKind.CloseParenToken, sourceFile);
492+
return nodeEndsWith(n, SyntaxKind.CloseParenToken, sourceFile);
491493
case SyntaxKind.FunctionDeclaration:
492494
case SyntaxKind.FunctionExpression:
493495
case SyntaxKind.Method:
@@ -503,7 +505,7 @@ module ts.formatting {
503505
case SyntaxKind.ExpressionStatement:
504506
return isCompletedNode((<ExpressionStatement>n).expression, sourceFile);
505507
case SyntaxKind.ArrayLiteral:
506-
return isNodeEndWith(n, SyntaxKind.CloseBracketToken, sourceFile);
508+
return nodeEndsWith(n, SyntaxKind.CloseBracketToken, sourceFile);
507509
case SyntaxKind.Missing:
508510
return false;
509511
case SyntaxKind.CaseClause:
@@ -516,8 +518,8 @@ module ts.formatting {
516518
// rough approximation: if DoStatement has While keyword - then if node is completed is checking the presence of ')';
517519
var hasWhileKeyword = forEach(n.getChildren(), c => c.kind === SyntaxKind.WhileKeyword && c);
518520
if(hasWhileKeyword) {
519-
return isNodeEndWith(n, SyntaxKind.CloseParenToken, sourceFile);
520-
}
521+
return nodeEndsWith(n, SyntaxKind.CloseParenToken, sourceFile);
522+
}
521523
return isCompletedNode((<DoStatement>n).statement, sourceFile);
522524
default:
523525
return true;

0 commit comments

Comments
 (0)