1// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package statements
6
7var expr bool
8
9func use(x interface{}) {}
10
11// Formatting of multi-line return statements.
12func _f() {
13 return
14 return x, y, z
15 return T{}
16 return T{1, 2, 3},
17 x, y, z
18 return T{1, 2, 3},
19 x, y,
20 z
21 return T{1,
22 2,
23 3}
24 return T{1,
25 2,
26 3,
27 }
28 return T{
29 1,
30 2,
31 3}
32 return T{
33 1,
34 2,
35 3,
36 }
37 return T{
38 1,
39 T{1, 2, 3},
40 3,
41 }
42 return T{
43 1,
44 T{1,
45 2, 3},
46 3,
47 }
48 return T{
49 1,
50 T{1,
51 2,
52 3},
53 3,
54 }
55 return T{
56 1,
57 2,
58 }, nil
59 return T{
60 1,
61 2,
62 },
63 T{
64 x: 3,
65 y: 4,
66 }, nil
67 return T{
68 1,
69 2,
70 },
71 nil
72 return T{
73 1,
74 2,
75 },
76 T{
77 x: 3,
78 y: 4,
79 },
80 nil
81 return x + y +
82 z
83 return func() {}
84 return func() {
85 _ = 0
86 }, T{
87 1, 2,
88 }
89 return func() {
90 _ = 0
91 }
92 return func() T {
93 return T{
94 1, 2,
95 }
96 }
97}
98
99// Formatting of multi-line returns: test cases from issue 1207.
100func F() (*T, os.Error) {
101 return &T{
102 X: 1,
103 Y: 2,
104 },
105 nil
106}
107
108func G() (*T, *T, os.Error) {
109 return &T{
110 X: 1,
111 Y: 2,
112 },
113 &T{
114 X: 3,
115 Y: 4,
116 },
117 nil
118}
119
120func _() interface{} {
121 return &fileStat{
122 name: basename(file.name),
123 size: mkSize(d.FileSizeHigh, d.FileSizeLow),
124 modTime: mkModTime(d.LastWriteTime),
125 mode: mkMode(d.FileAttributes),
126 sys: mkSysFromFI(&d),
127 }, nil
128}
129
130// Formatting of if-statement headers.
131func _() {
132 if true {
133 }
134 if true {
135 } // no semicolon printed
136 if expr {
137 }
138 if expr {
139 } // no semicolon printed
140 if expr {
141 } // no parens printed
142 if expr {
143 } // no semicolon and parens printed
144 if x := expr; true {
145 use(x)
146 }
147 if x := expr; expr {
148 use(x)
149 }
150}
151
152// Formatting of switch-statement headers.
153func _() {
154 switch {
155 }
156 switch {
157 } // no semicolon printed
158 switch expr {
159 }
160 switch expr {
161 } // no semicolon printed
162 switch expr {
163 } // no parens printed
164 switch expr {
165 } // no semicolon and parens printed
166 switch x := expr; {
167 default:
168 use(
169 x)
170 }
171 switch x := expr; expr {
172 default:
173 use(x)
174 }
175}
176
177// Formatting of switch statement bodies.
178func _() {
179 switch {
180 }
181
182 switch x := 0; x {
183 case 1:
184 use(x)
185 use(x) // followed by an empty line
186
187 case 2: // followed by an empty line
188
189 use(x) // followed by an empty line
190
191 case 3: // no empty lines
192 use(x)
193 use(x)
194 }
195
196 switch x {
197 case 0:
198 use(x)
199 case 1: // this comment should have no effect on the previous or next line
200 use(x)
201 }
202
203 switch x := 0; x {
204 case 1:
205 x = 0
206 // this comment should be indented
207 case 2:
208 x = 0
209 // this comment should not be indented, it is aligned with the next case
210 case 3:
211 x = 0
212 /* indented comment
213 aligned
214 aligned
215 */
216 // bla
217 /* and more */
218 case 4:
219 x = 0
220 /* not indented comment
221 aligned
222 aligned
223 */
224 // bla
225 /* and more */
226 case 5:
227 }
228}
229
230// Formatting of selected select statements.
231func _() {
232 select {}
233 select { /* this comment should not be tab-aligned because the closing } is on the same line */
234 }
235 select { /* this comment should be tab-aligned */
236 }
237 select { // this comment should be tab-aligned
238 }
239 select {
240 case <-c:
241 }
242}
243
244// Formatting of for-statement headers for single-line for-loops.
245func _() {
246 for {
247 }
248 for expr {
249 }
250 for expr {
251 } // no parens printed
252 for {
253 } // no semicolons printed
254 for x := expr; ; {
255 use(x)
256 }
257 for expr {
258 } // no semicolons printed
259 for expr {
260 } // no semicolons and parens printed
261 for ; ; expr = false {
262 }
263 for x := expr; expr; {
264 use(x)
265 }
266 for x := expr; ; expr = false {
267 use(x)
268 }
269 for ; expr; expr = false {
270 }
271 for x := expr; expr; expr = false {
272 use(x)
273 }
274 for x := range []int{} {
275 use(x)
276 }
277 for x := range []int{} {
278 use(x)
279 } // no parens printed
280}
281
282// Formatting of for-statement headers for multi-line for-loops.
283func _() {
284 for {
285 }
286 for expr {
287 }
288 for expr {
289 } // no parens printed
290 for {
291 } // no semicolons printed
292 for x := expr; ; {
293 use(x)
294 }
295 for expr {
296 } // no semicolons printed
297 for expr {
298 } // no semicolons and parens printed
299 for ; ; expr = false {
300 }
301 for x := expr; expr; {
302 use(x)
303 }
304 for x := expr; ; expr = false {
305 use(x)
306 }
307 for ; expr; expr = false {
308 }
309 for x := expr; expr; expr = false {
310 use(x)
311 }
312 for range []int{} {
313 println("foo")
314 }
315 for x := range []int{} {
316 use(x)
317 }
318 for x := range []int{} {
319 use(x)
320 } // no parens printed
321}
322
323// Formatting of selected short single- and multi-line statements.
324func _() {
325 if cond {
326 }
327 if cond {
328 } // multiple lines
329 if cond {
330 } else {
331 } // else clause always requires multiple lines
332
333 for {
334 }
335 for i := 0; i < len(a); 1++ {
336 }
337 for i := 0; i < len(a); 1++ {
338 a[i] = i
339 }
340 for i := 0; i < len(a); 1++ {
341 a[i] = i
342 } // multiple lines
343
344 for range a {
345 }
346 for _ = range a {
347 }
348 for _, _ = range a {
349 }
350 for i := range a {
351 }
352 for i := range a {
353 a[i] = i
354 }
355 for i := range a {
356 a[i] = i
357 } // multiple lines
358
359 go func() {
360 for {
361 a <- <-b
362 }
363 }()
364 defer func() {
365 if x := recover(); x != nil {
366 err = fmt.Sprintf("error: %s", x.msg)
367 }
368 }()
369}
370
371// Don't remove mandatory parentheses around composite literals in control clauses.
372func _() {
373 // strip parentheses - no composite literals or composite literals don't start with a type name
374 if x {
375 }
376 if x {
377 }
378 if []T{} {
379 }
380 if []T{} {
381 }
382 if []T{} {
383 }
384
385 for x {
386 }
387 for x {
388 }
389 for []T{} {
390 }
391 for []T{} {
392 }
393 for []T{} {
394 }
395
396 switch x {
397 }
398 switch x {
399 }
400 switch []T{} {
401 }
402 switch []T{} {
403 }
404
405 for _ = range []T{T{42}} {
406 }
407
408 // leave parentheses - composite literals start with a type name
409 if (T{}) {
410 }
411 if (T{}) {
412 }
413 if (T{}) {
414 }
415
416 for (T{}) {
417 }
418 for (T{}) {
419 }
420 for (T{}) {
421 }
422
423 switch (T{}) {
424 }
425 switch (T{}) {
426 }
427
428 for _ = range (T1{T{42}}) {
429 }
430
431 if x == (T{42}[0]) {
432 }
433 if (x == T{42}[0]) {
434 }
435 if x == (T{42}[0]) {
436 }
437 if x == (T{42}[0]) {
438 }
439 if x == (T{42}[0]) {
440 }
441 if x == a+b*(T{42}[0]) {
442 }
443 if (x == a+b*T{42}[0]) {
444 }
445 if x == a+b*(T{42}[0]) {
446 }
447 if x == a+(b*(T{42}[0])) {
448 }
449 if x == a+b*(T{42}[0]) {
450 }
451 if (a + b*(T{42}[0])) == x {
452 }
453 if (a + b*(T{42}[0])) == x {
454 }
455
456 if struct{ x bool }{false}.x {
457 }
458 if (struct{ x bool }{false}.x) == false {
459 }
460 if struct{ x bool }{false}.x == false {
461 }
462}
463
464// Extra empty lines inside functions. Do respect source code line
465// breaks between statement boundaries but print at most one empty
466// line at a time.
467func _() {
468
469 const _ = 0
470
471 const _ = 1
472 type _ int
473 type _ float
474
475 var _ = 0
476 var x = 1
477
478 // Each use(x) call below should have at most one empty line before and after.
479 // Known bug: The first use call may have more than one empty line before
480 // (see go/printer/nodes.go, func linebreak).
481
482 use(x)
483
484 if x < x {
485
486 use(x)
487
488 } else {
489
490 use(x)
491
492 }
493}
494
495// Formatting around labels.
496func _() {
497L:
498}
499
500func _() {
501 // this comment should be indented
502L: // no semicolon needed
503}
504
505func _() {
506 switch 0 {
507 case 0:
508 L0:
509 ; // semicolon required
510 case 1:
511 L1:
512 ; // semicolon required
513 default:
514 L2: // no semicolon needed
515 }
516}
517
518func _() {
519 f()
520L1:
521 f()
522L2:
523 ;
524L3:
525}
526
527func _() {
528 // this comment should be indented
529L:
530}
531
532func _() {
533L:
534 _ = 0
535}
536
537func _() {
538 // this comment should be indented
539L:
540 _ = 0
541}
542
543func _() {
544 for {
545 L1:
546 _ = 0
547 L2:
548 _ = 0
549 }
550}
551
552func _() {
553 // this comment should be indented
554 for {
555 L1:
556 _ = 0
557 L2:
558 _ = 0
559 }
560}
561
562func _() {
563 if true {
564 _ = 0
565 }
566 _ = 0 // the indentation here should not be affected by the long label name
567AnOverlongLabel:
568 _ = 0
569
570 if true {
571 _ = 0
572 }
573 _ = 0
574
575L:
576 _ = 0
577}
578
579func _() {
580 for {
581 goto L
582 }
583L:
584
585 MoreCode()
586}
587
588func _() {
589 for {
590 goto L
591 }
592L: // A comment on the same line as the label, followed by a single empty line.
593 // Known bug: There may be more than one empty line before MoreCode()
594 // (see go/printer/nodes.go, func linebreak).
595
596 MoreCode()
597}
598
599func _() {
600 for {
601 goto L
602 }
603L:
604
605 // There should be a single empty line before this comment.
606 MoreCode()
607}
608
609func _() {
610 for {
611 goto AVeryLongLabelThatShouldNotAffectFormatting
612 }
613AVeryLongLabelThatShouldNotAffectFormatting:
614 // There should be a single empty line after this comment.
615
616 // There should be a single empty line before this comment.
617 MoreCode()
618}
619
620// Formatting of empty statements.
621func _() {
622
623}
624
625func _() {
626}
627
628func _() {
629}
630
631func _() {
632 f()
633}
634
635func _() {
636L:
637 ;
638}
639
640func _() {
641L:
642 ;
643 f()
644}
View as plain text