Skip to content

Commit b97f876

Browse files
committed
Breakpoints in for statement
1 parent c81c0bf commit b97f876

3 files changed

Lines changed: 302 additions & 18 deletions

File tree

src/services/breakpoints.ts

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module ts.BreakpointResolver {
3737
}
3838

3939
function spanInNodeIfStartsOnSameLine(node: Node, otherwiseOnNode?: Node): TypeScript.TextSpan {
40-
if (node && sourceFile.getLineAndCharacterFromPosition(position).line === sourceFile.getLineAndCharacterFromPosition(node.getStart()).line) {
40+
if (node && lineOfPosition === sourceFile.getLineAndCharacterFromPosition(node.getStart()).line) {
4141
return spanInNode(node);
4242
}
4343
return spanInNode(otherwiseOnNode);
@@ -97,6 +97,14 @@ module ts.BreakpointResolver {
9797
case SyntaxKind.ContinueStatement:
9898
return spanInBreakOrContinueStatement(<BreakOrContinueStatement>node);
9999

100+
case SyntaxKind.ForStatement:
101+
return spanInForStatement(<ForStatement>node);
102+
103+
case SyntaxKind.BinaryExpression:
104+
case SyntaxKind.PostfixOperator:
105+
case SyntaxKind.PrefixOperator:
106+
return spanInExpression(node);
107+
100108
// Tokens:
101109
case SyntaxKind.SemicolonToken:
102110
case SyntaxKind.EndOfFileToken:
@@ -127,14 +135,6 @@ module ts.BreakpointResolver {
127135
case SyntaxKind.ElseKeyword:
128136
return spanInNextNode(node);
129137

130-
case SyntaxKind.BinaryExpression:
131-
//TODO (pick this up later) for now lets fix do-while baseline
132-
if (node.parent.kind === SyntaxKind.DoStatement) {
133-
// Set span as if on while keyword
134-
return spanInPreviousNode(node);
135-
}
136-
// Default action for now
137-
138138
default:
139139
// Default go to parent to set the breakpoint
140140
return spanInNode(node.parent);
@@ -143,25 +143,35 @@ module ts.BreakpointResolver {
143143

144144
function spanInVariableDeclaration(variableDeclaration: VariableDeclaration): TypeScript.TextSpan {
145145
var isParentVariableStatement = variableDeclaration.parent.kind === SyntaxKind.VariableStatement;
146-
var isfirstDeclarationOfVariableStatement = isParentVariableStatement &&
147-
(<VariableStatement>variableDeclaration.parent).declarations[0] === variableDeclaration;
146+
var isDeclarationOfForStatement = variableDeclaration.parent.kind === SyntaxKind.ForStatement && contains((<ForStatement>variableDeclaration.parent).declarations, variableDeclaration);
147+
var declarations = isParentVariableStatement
148+
? (<VariableStatement>variableDeclaration.parent).declarations
149+
: isDeclarationOfForStatement
150+
? (<ForStatement>variableDeclaration.parent).declarations
151+
: undefined;
148152

149153
// Breakpoint is possible in variableDeclaration only if there is initialization
150154
if (variableDeclaration.initializer) {
151-
if (isfirstDeclarationOfVariableStatement) {
152-
// First declaration - include var keyword
153-
return textSpan(variableDeclaration.parent, variableDeclaration);
155+
if (declarations && declarations[0] === variableDeclaration) {
156+
if (isParentVariableStatement) {
157+
// First declaration - include var keyword
158+
return textSpan(variableDeclaration.parent, variableDeclaration);
159+
}
160+
else {
161+
Debug.assert(isDeclarationOfForStatement);
162+
// Include var keyword from for statement declarations in the span
163+
return textSpan(findPrecedingToken(variableDeclaration.pos, sourceFile, variableDeclaration.parent), variableDeclaration);
164+
}
154165
}
155166
else {
156167
// Span only on this declaration
157168
return textSpan(variableDeclaration);
158169
}
159170
}
160-
else if (!isfirstDeclarationOfVariableStatement && isParentVariableStatement) {
171+
else if (declarations && declarations[0] !== variableDeclaration) {
161172
// If we cant set breakpoint on this declaration, set it on previous one
162-
var variableStatement = <VariableStatement>variableDeclaration.parent;
163-
var indexOfCurrentDeclaration = indexOf(variableStatement.declarations, variableDeclaration);
164-
return spanInVariableDeclaration(variableStatement.declarations[indexOfCurrentDeclaration - 1]);
173+
var indexOfCurrentDeclaration = indexOf(declarations, variableDeclaration);
174+
return spanInVariableDeclaration(declarations[indexOfCurrentDeclaration - 1]);
165175
}
166176
}
167177

@@ -234,6 +244,10 @@ module ts.BreakpointResolver {
234244
case SyntaxKind.WhileStatement:
235245
case SyntaxKind.IfStatement:
236246
return spanInNodeIfStartsOnSameLine(block.parent, block.statements[0]);
247+
248+
// Set span on previous token if it starts on same line otherwise on the first statement of the block
249+
case SyntaxKind.ForStatement:
250+
return spanInNodeIfStartsOnSameLine(findPrecedingToken(block.pos, sourceFile, block.parent), block.statements[0]);
237251
}
238252

239253
// Default action is to set on first statement
@@ -273,6 +287,37 @@ module ts.BreakpointResolver {
273287
function spanInBreakOrContinueStatement(breakOrContinueStatement: BreakOrContinueStatement): TypeScript.TextSpan {
274288
return textSpan(breakOrContinueStatement, breakOrContinueStatement.label || breakOrContinueStatement.getChildAt(0));
275289
}
290+
291+
function spanInForStatement(forStatement: ForStatement): TypeScript.TextSpan {
292+
if (forStatement.declarations) {
293+
return spanInNode(forStatement.declarations[0]);
294+
}
295+
if (forStatement.initializer) {
296+
return spanInNode(forStatement.initializer);
297+
}
298+
if (forStatement.condition) {
299+
return textSpan(forStatement.condition);
300+
}
301+
302+
if (forStatement.iterator) {
303+
return textSpan(forStatement.iterator);
304+
}
305+
}
306+
307+
function spanInExpression(expression: Expression): TypeScript.TextSpan {
308+
//TODO (pick this up later) for now lets fix do-while baseline if (node.parent.kind === SyntaxKind.DoStatement) {
309+
// Set span as if on while keyword
310+
return spanInPreviousNode(node);
311+
}
312+
313+
if (node.parent.kind === SyntaxKind.ForStatement) {
314+
// For now lets set the span on this expression, fix it later
315+
return textSpan(expression);
316+
}
317+
318+
// Default action for now:
319+
return spanInNode(expression.parent);
320+
}
276321

277322
// Tokens:
278323
function spanInCommaToken(node: Node): TypeScript.TextSpan {
@@ -339,6 +384,7 @@ module ts.BreakpointResolver {
339384
case SyntaxKind.Constructor:
340385
case SyntaxKind.WhileStatement:
341386
case SyntaxKind.DoStatement:
387+
case SyntaxKind.ForStatement:
342388
return spanInPreviousNode(node);
343389

344390
// Default to parent node
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
2+
1 >for (var i = 0; i < 10; i++) {
3+
4+
~~~~~~~~~~~~~~~ => Pos: (0 to 14) SpanInfo: {"start":5,"length":9}
5+
>var i = 0
6+
>:=> (line 1, col 5) to (line 1, col 14)
7+
1 >for (var i = 0; i < 10; i++) {
8+
9+
~~~~~~~~ => Pos: (15 to 22) SpanInfo: {"start":16,"length":6}
10+
>i < 10
11+
>:=> (line 1, col 16) to (line 1, col 22)
12+
1 >for (var i = 0; i < 10; i++) {
13+
14+
~~~~~~~~ => Pos: (23 to 30) SpanInfo: {"start":24,"length":3}
15+
>i++
16+
>:=> (line 1, col 24) to (line 1, col 27)
17+
--------------------------------
18+
2 > WScript.Echo("i: " + i);
19+
20+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ => Pos: (31 to 59) SpanInfo: {"start":35,"length":23}
21+
>WScript.Echo("i: " + i)
22+
>:=> (line 2, col 4) to (line 2, col 27)
23+
--------------------------------
24+
3 >}
25+
26+
~~ => Pos: (60 to 61) SpanInfo: {"start":35,"length":23}
27+
>WScript.Echo("i: " + i)
28+
>:=> (line 2, col 4) to (line 2, col 27)
29+
--------------------------------
30+
4 >for (i = 0; i < 10; i++)
31+
32+
~~~~~~~~~~~ => Pos: (62 to 72) SpanInfo: {"start":67,"length":5}
33+
>i = 0
34+
>:=> (line 4, col 5) to (line 4, col 10)
35+
4 >for (i = 0; i < 10; i++)
36+
37+
~~~~~~~~ => Pos: (73 to 80) SpanInfo: {"start":74,"length":6}
38+
>i < 10
39+
>:=> (line 4, col 12) to (line 4, col 18)
40+
4 >for (i = 0; i < 10; i++)
41+
42+
~~~~~~ => Pos: (81 to 86) SpanInfo: {"start":82,"length":3}
43+
>i++
44+
>:=> (line 4, col 20) to (line 4, col 23)
45+
--------------------------------
46+
5 >{
47+
48+
~~ => Pos: (87 to 88) SpanInfo: {"start":93,"length":23}
49+
>WScript.Echo("i: " + i)
50+
>:=> (line 6, col 4) to (line 6, col 27)
51+
--------------------------------
52+
6 > WScript.Echo("i: " + i);
53+
54+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ => Pos: (89 to 117) SpanInfo: {"start":93,"length":23}
55+
>WScript.Echo("i: " + i)
56+
>:=> (line 6, col 4) to (line 6, col 27)
57+
--------------------------------
58+
7 >}
59+
60+
~~ => Pos: (118 to 119) SpanInfo: {"start":93,"length":23}
61+
>WScript.Echo("i: " + i)
62+
>:=> (line 6, col 4) to (line 6, col 27)
63+
--------------------------------
64+
8 >for (var j = 0; j < 10; ) {
65+
66+
~~~~~~~~~~~~~~~ => Pos: (120 to 134) SpanInfo: {"start":125,"length":9}
67+
>var j = 0
68+
>:=> (line 8, col 5) to (line 8, col 14)
69+
8 >for (var j = 0; j < 10; ) {
70+
71+
~~~~~~~~~~~~~ => Pos: (135 to 147) SpanInfo: {"start":136,"length":6}
72+
>j < 10
73+
>:=> (line 8, col 16) to (line 8, col 22)
74+
--------------------------------
75+
9 > j++;
76+
77+
~~~~~~~~~ => Pos: (148 to 156) SpanInfo: {"start":152,"length":3}
78+
>j++
79+
>:=> (line 9, col 4) to (line 9, col 7)
80+
--------------------------------
81+
10 > if (j == 1) {
82+
83+
~~~~~~~~~~~~~~~~~~ => Pos: (157 to 174) SpanInfo: {"start":161,"length":11}
84+
>if (j == 1)
85+
>:=> (line 10, col 4) to (line 10, col 15)
86+
--------------------------------
87+
11 > continue;
88+
89+
~~~~~~~~~~~~~~~~~~ => Pos: (175 to 192) SpanInfo: {"start":183,"length":8}
90+
>continue
91+
>:=> (line 11, col 8) to (line 11, col 16)
92+
--------------------------------
93+
12 > }
94+
95+
~~~~~~ => Pos: (193 to 198) SpanInfo: {"start":183,"length":8}
96+
>continue
97+
>:=> (line 11, col 8) to (line 11, col 16)
98+
--------------------------------
99+
13 >}
100+
101+
~~ => Pos: (199 to 200) SpanInfo: {"start":161,"length":11}
102+
>if (j == 1)
103+
>:=> (line 10, col 4) to (line 10, col 15)
104+
--------------------------------
105+
14 >for (j = 0; j < 10;)
106+
107+
~~~~~~~~~~~ => Pos: (201 to 211) SpanInfo: {"start":206,"length":5}
108+
>j = 0
109+
>:=> (line 14, col 5) to (line 14, col 10)
110+
14 >for (j = 0; j < 10;)
111+
112+
~~~~~~~~~~ => Pos: (212 to 221) SpanInfo: {"start":213,"length":6}
113+
>j < 10
114+
>:=> (line 14, col 12) to (line 14, col 18)
115+
--------------------------------
116+
15 >{
117+
118+
~~ => Pos: (222 to 223) SpanInfo: {"start":228,"length":3}
119+
>j++
120+
>:=> (line 16, col 4) to (line 16, col 7)
121+
--------------------------------
122+
16 > j++;
123+
124+
~~~~~~~~~ => Pos: (224 to 232) SpanInfo: {"start":228,"length":3}
125+
>j++
126+
>:=> (line 16, col 4) to (line 16, col 7)
127+
--------------------------------
128+
17 >}
129+
130+
~~ => Pos: (233 to 234) SpanInfo: {"start":228,"length":3}
131+
>j++
132+
>:=> (line 16, col 4) to (line 16, col 7)
133+
--------------------------------
134+
18 >for (var k = 0;; k++) {
135+
136+
~~~~~~~~~~~~~~~~ => Pos: (235 to 250) SpanInfo: {"start":240,"length":9}
137+
>var k = 0
138+
>:=> (line 18, col 5) to (line 18, col 14)
139+
18 >for (var k = 0;; k++) {
140+
141+
~~~~~~~~ => Pos: (251 to 258) SpanInfo: {"start":252,"length":3}
142+
>k++
143+
>:=> (line 18, col 17) to (line 18, col 20)
144+
--------------------------------
145+
19 >}
146+
147+
~~ => Pos: (259 to 260) SpanInfo: undefined
148+
--------------------------------
149+
20 >for (k = 0;; k++)
150+
151+
~~~~~~~~~~~~ => Pos: (261 to 272) SpanInfo: {"start":266,"length":5}
152+
>k = 0
153+
>:=> (line 20, col 5) to (line 20, col 10)
154+
20 >for (k = 0;; k++)
155+
156+
~~~~~~ => Pos: (273 to 278) SpanInfo: {"start":274,"length":3}
157+
>k++
158+
>:=> (line 20, col 13) to (line 20, col 16)
159+
--------------------------------
160+
21 >{
161+
162+
~~ => Pos: (279 to 280) SpanInfo: undefined
163+
--------------------------------
164+
22 >}
165+
166+
~~ => Pos: (281 to 282) SpanInfo: undefined
167+
--------------------------------
168+
23 >for (; k < 10; k++) {
169+
170+
~~~~~~~~~~~~~~ => Pos: (283 to 296) SpanInfo: {"start":290,"length":6}
171+
>k < 10
172+
>:=> (line 23, col 7) to (line 23, col 13)
173+
23 >for (; k < 10; k++) {
174+
175+
~~~~~~~~ => Pos: (297 to 304) SpanInfo: {"start":298,"length":3}
176+
>k++
177+
>:=> (line 23, col 15) to (line 23, col 18)
178+
--------------------------------
179+
24 >}
180+
181+
~~ => Pos: (305 to 306) SpanInfo: undefined
182+
--------------------------------
183+
25 >for (;;) {
184+
185+
~~~~~~~~~~~ => Pos: (307 to 317) SpanInfo: undefined
186+
--------------------------------
187+
26 > i++;
188+
189+
~~~~~~~~~ => Pos: (318 to 326) SpanInfo: {"start":322,"length":3}
190+
>i++
191+
>:=> (line 26, col 4) to (line 26, col 7)
192+
--------------------------------
193+
27 >}
194+
195+
~~ => Pos: (327 to 328) SpanInfo: {"start":322,"length":3}
196+
>i++
197+
>:=> (line 26, col 4) to (line 26, col 7)
198+
--------------------------------
199+
28 >for (;;)
200+
201+
~~~~~~~~~ => Pos: (329 to 337) SpanInfo: undefined
202+
--------------------------------
203+
29 >{
204+
205+
~~ => Pos: (338 to 339) SpanInfo: {"start":344,"length":3}
206+
>i++
207+
>:=> (line 30, col 4) to (line 30, col 7)
208+
--------------------------------
209+
30 > i++;
210+
211+
~~~~~~~~~ => Pos: (340 to 348) SpanInfo: {"start":344,"length":3}
212+
>i++
213+
>:=> (line 30, col 4) to (line 30, col 7)
214+
--------------------------------
215+
31 >}
216+
217+
~~ => Pos: (349 to 350) SpanInfo: {"start":344,"length":3}
218+
>i++
219+
>:=> (line 30, col 4) to (line 30, col 7)
220+
--------------------------------
221+
32 >for (i = 0, j = 20; j < 20, i < 20; j++) {
222+
223+
~~~~~~~~~~~~~~~~~~~ => Pos: (351 to 369) SpanInfo: {"start":356,"length":13}
224+
>i = 0, j = 20
225+
>:=> (line 32, col 5) to (line 32, col 18)
226+
32 >for (i = 0, j = 20; j < 20, i < 20; j++) {
227+
228+
~~~~~~~~~~~~~~~~ => Pos: (370 to 385) SpanInfo: {"start":371,"length":14}
229+
>j < 20, i < 20
230+
>:=> (line 32, col 20) to (line 32, col 34)
231+
32 >for (i = 0, j = 20; j < 20, i < 20; j++) {
232+
233+
~~~~~~~~ => Pos: (386 to 393) SpanInfo: {"start":387,"length":3}
234+
>j++
235+
>:=> (line 32, col 36) to (line 32, col 39)
236+
--------------------------------
237+
33 >}
238+
~ => Pos: (394 to 394) SpanInfo: undefined
File renamed without changes.

0 commit comments

Comments
 (0)