From abb0acc639cc62e181610afa4bfdad892debb169 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 17 Oct 2014 15:42:10 -0700 Subject: [PATCH] Breakpoints for while statement --- src/services/breakpoints.ts | 40 +++++++++-- .../baselines/reference/bpSpan_while.baseline | 70 +++++++++++++++++++ .../breakpointValidationWhile.ts | 3 + 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/bpSpan_while.baseline rename tests/cases/{fourslash_old => fourslash}/breakpointValidationWhile.ts (63%) diff --git a/src/services/breakpoints.ts b/src/services/breakpoints.ts index ea8fd3e0a56..ebb37bb97cc 100644 --- a/src/services/breakpoints.ts +++ b/src/services/breakpoints.ts @@ -36,10 +36,11 @@ module ts.BreakpointResolver { return TypeScript.TextSpan.fromBounds(startNode.getStart(), (endNode || startNode).getEnd()); } - function spanInNodeIfStartsOnSameLine(node: Node): TypeScript.TextSpan { + function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TypeScript.TextSpan { if (node && sourceFile.getLineAndCharacterFromPosition(position).line === sourceFile.getLineAndCharacterFromPosition(node.getStart()).line) { return spanInNode(node); } + return spanInNode(otherwiseOnNode); } function spanInPreviousNode(node: Node): TypeScript.TextSpan { @@ -62,7 +63,7 @@ module ts.BreakpointResolver { return spanInFunctionDeclaration(node); case SyntaxKind.FunctionBlock: - return spanInBlock(node); + return spanInFirstStatementOfBlock(node); case SyntaxKind.ExpressionStatement: return spanInExpressionStatement(node); @@ -70,6 +71,9 @@ module ts.BreakpointResolver { case SyntaxKind.ReturnStatement: return spanInReturnStatement(node); + case SyntaxKind.WhileStatement: + return spanInWhileStatement(node); + // Tokens: case SyntaxKind.SemicolonToken: case SyntaxKind.EndOfFileToken: @@ -173,11 +177,27 @@ module ts.BreakpointResolver { return spanInNode(functionDeclaration.body); } - function spanInBlock(block: Block): TypeScript.TextSpan { + function spanInFirstStatementOfBlock(block: Block): TypeScript.TextSpan { // Set breakpoint in first statement return spanInNode(block.statements[0]); } + function spanInLastStatementOfBlock(block: Block): TypeScript.TextSpan { + // Set breakpoint in first statement + return spanInNode(block.statements[block.statements.length - 1]); + } + + function spanInBlock(block: Block): TypeScript.TextSpan { + switch (block.parent.kind) { + // Set on parent if on same line otherwise on first statement + case SyntaxKind.WhileStatement: + return spanInNodeIfStartsOnSameLine(block.parent, block.statements[0]); + } + + // Default action is to set on first statement + return spanInFirstStatementOfBlock(block); + } + function spanInExpressionStatement(expressionStatement: ExpressionStatement): TypeScript.TextSpan { return textSpan(expressionStatement.expression); } @@ -186,6 +206,10 @@ module ts.BreakpointResolver { return textSpan(returnStatement.getChildAt(0, sourceFile), returnStatement.expression); } + function spanInWhileStatement(whileStatement: WhileStatement): TypeScript.TextSpan { + return textSpan(whileStatement, findNextToken(whileStatement.expression, whileStatement)); + } + // Tokens: function spanInCommaToken(node: Node): TypeScript.TextSpan { switch (node.parent.kind) { @@ -202,6 +226,10 @@ module ts.BreakpointResolver { function spanInOpenBraceToken(node: Node): TypeScript.TextSpan { switch (node.parent.kind) { case SyntaxKind.FunctionBlock: + // Span on first statement + return spanInFirstStatementOfBlock(node.parent); + + case SyntaxKind.Block: return spanInBlock(node.parent); // Default to parent node @@ -216,6 +244,9 @@ module ts.BreakpointResolver { // Span on close brace token return textSpan(node); + case SyntaxKind.Block: + return spanInLastStatementOfBlock(node.parent); + // Default to parent node default: return spanInNode(node.parent); @@ -224,7 +255,8 @@ module ts.BreakpointResolver { function spanInCloseParenToken(node: Node): TypeScript.TextSpan { // Is this close paren token of parameter list, set span in previous token - if (isAnyFunction(node.parent)) { + if (isAnyFunction(node.parent) || + node.parent.kind === SyntaxKind.WhileStatement) { return spanInPreviousNode(node); } diff --git a/tests/baselines/reference/bpSpan_while.baseline b/tests/baselines/reference/bpSpan_while.baseline new file mode 100644 index 00000000000..c48e049fbd7 --- /dev/null +++ b/tests/baselines/reference/bpSpan_while.baseline @@ -0,0 +1,70 @@ + +1 >var a = 10; + + ~~~~~~~~~~~~ => Pos: (0 to 11) SpanInfo: {"start":0,"length":10} + >var a = 10 + >:=> (line 1, col 0) to (line 1, col 10) +-------------------------------- +2 >while (a == 10) { + + ~~~~~~~~~~~~~~~~~~ => Pos: (12 to 29) SpanInfo: {"start":12,"length":15} + >while (a == 10) + >:=> (line 2, col 0) to (line 2, col 15) +-------------------------------- +3 > a++; + + ~~~~~~~~~ => Pos: (30 to 38) SpanInfo: {"start":34,"length":3} + >a++ + >:=> (line 3, col 4) to (line 3, col 7) +-------------------------------- +4 >} + + ~~ => Pos: (39 to 40) SpanInfo: {"start":34,"length":3} + >a++ + >:=> (line 3, col 4) to (line 3, col 7) +-------------------------------- +5 >while (a == 10) + + ~~~~~~~~~~~~~~~~~ => Pos: (41 to 57) SpanInfo: {"start":41,"length":15} + >while (a == 10) + >:=> (line 5, col 0) to (line 5, col 15) +-------------------------------- +6 >{ + + ~~ => Pos: (58 to 59) SpanInfo: {"start":64,"length":3} + >a++ + >:=> (line 7, col 4) to (line 7, col 7) +-------------------------------- +7 > a++; + + ~~~~~~~~~ => Pos: (60 to 68) SpanInfo: {"start":64,"length":3} + >a++ + >:=> (line 7, col 4) to (line 7, col 7) +-------------------------------- +8 >} + + ~~ => Pos: (69 to 70) SpanInfo: {"start":64,"length":3} + >a++ + >:=> (line 7, col 4) to (line 7, col 7) +-------------------------------- +9 >while (a == 10) a++; + + ~~~~~~~~~~~~~~~ => Pos: (71 to 85) SpanInfo: {"start":71,"length":15} + >while (a == 10) + >:=> (line 9, col 0) to (line 9, col 15) +9 >while (a == 10) a++; + + ~~~~~~~ => Pos: (86 to 92) SpanInfo: {"start":88,"length":3} + >a++ + >:=> (line 9, col 17) to (line 9, col 20) +-------------------------------- +10 >while (a == 10) + + ~~~~~~~~~~~~~~~~~ => Pos: (93 to 109) SpanInfo: {"start":93,"length":15} + >while (a == 10) + >:=> (line 10, col 0) to (line 10, col 15) +-------------------------------- +11 > a++; + ~~~~~~~~ => Pos: (110 to 117) SpanInfo: {"start":114,"length":3} + >a++ + >:=> (line 11, col 4) to (line 11, col 7) \ No newline at end of file diff --git a/tests/cases/fourslash_old/breakpointValidationWhile.ts b/tests/cases/fourslash/breakpointValidationWhile.ts similarity index 63% rename from tests/cases/fourslash_old/breakpointValidationWhile.ts rename to tests/cases/fourslash/breakpointValidationWhile.ts index 59d3c4fda3b..9fc913c702c 100644 --- a/tests/cases/fourslash_old/breakpointValidationWhile.ts +++ b/tests/cases/fourslash/breakpointValidationWhile.ts @@ -10,4 +10,7 @@ ////{ //// a++; ////} +////while (a == 10) a++; +////while (a == 10) +//// a++; verify.baselineCurrentFileBreakpointLocations(); \ No newline at end of file