github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/go/printer/testdata/statements.input (about) 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 5 package statements 6 7 var expr bool 8 9 func use(x interface{}) {} 10 11 // Formatting of multi-line return statements. 12 func _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. 100 func F() (*T, os.Error) { 101 return &T{ 102 X: 1, 103 Y: 2, 104 }, 105 nil 106 } 107 108 func 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 120 func _() 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. 131 func _() { 132 if true {} 133 if; true {} // no semicolon printed 134 if expr{} 135 if;expr{} // no semicolon printed 136 if (expr){} // no parens printed 137 if;((expr)){} // no semicolon and parens printed 138 if x:=expr;true{ 139 use(x)} 140 if x:=expr; expr {use(x)} 141 } 142 143 144 // Formatting of switch-statement headers. 145 func _() { 146 switch {} 147 switch;{} // no semicolon printed 148 switch expr {} 149 switch;expr{} // no semicolon printed 150 switch (expr) {} // no parens printed 151 switch;((expr)){} // no semicolon and parens printed 152 switch x := expr; { default:use( 153 x) 154 } 155 switch x := expr; expr {default:use(x)} 156 } 157 158 159 // Formatting of switch statement bodies. 160 func _() { 161 switch { 162 } 163 164 switch x := 0; x { 165 case 1: 166 use(x) 167 use(x) // followed by an empty line 168 169 case 2: // followed by an empty line 170 171 use(x) // followed by an empty line 172 173 case 3: // no empty lines 174 use(x) 175 use(x) 176 } 177 178 switch x { 179 case 0: 180 use(x) 181 case 1: // this comment should have no effect on the previous or next line 182 use(x) 183 } 184 185 switch x := 0; x { 186 case 1: 187 x = 0 188 // this comment should be indented 189 case 2: 190 x = 0 191 // this comment should not be indented, it is aligned with the next case 192 case 3: 193 x = 0 194 /* indented comment 195 aligned 196 aligned 197 */ 198 // bla 199 /* and more */ 200 case 4: 201 x = 0 202 /* not indented comment 203 aligned 204 aligned 205 */ 206 // bla 207 /* and more */ 208 case 5: 209 } 210 } 211 212 213 // Formatting of selected select statements. 214 func _() { 215 select { 216 } 217 select { /* this comment should not be tab-aligned because the closing } is on the same line */ } 218 select { /* this comment should be tab-aligned */ 219 } 220 select { // this comment should be tab-aligned 221 } 222 select { case <-c: } 223 } 224 225 226 // Formatting of for-statement headers for single-line for-loops. 227 func _() { 228 for{} 229 for expr {} 230 for (expr) {} // no parens printed 231 for;;{} // no semicolons printed 232 for x :=expr;; {use( x)} 233 for; expr;{} // no semicolons printed 234 for; ((expr));{} // no semicolons and parens printed 235 for; ; expr = false {} 236 for x :=expr; expr; {use(x)} 237 for x := expr;; expr=false {use(x)} 238 for;expr;expr =false {} 239 for x := expr;expr;expr = false { use(x) } 240 for x := range []int{} { use(x) } 241 for x := range (([]int{})) { use(x) } // no parens printed 242 } 243 244 245 // Formatting of for-statement headers for multi-line for-loops. 246 func _() { 247 for{ 248 } 249 for expr { 250 } 251 for (expr) { 252 } // no parens printed 253 for;;{ 254 } // no semicolons printed 255 for x :=expr;; {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; {use(x) 264 } 265 for x := expr;; expr=false {use(x) 266 } 267 for;expr;expr =false { 268 } 269 for x := expr;expr;expr = false { 270 use(x) 271 } 272 for x := range []int{} { 273 use(x) } 274 for x := range (([]int{})) { 275 use(x) } // no parens printed 276 } 277 278 279 // Formatting of selected short single- and multi-line statements. 280 func _() { 281 if cond {} 282 if cond { 283 } // multiple lines 284 if cond {} else {} // else clause always requires multiple lines 285 286 for {} 287 for i := 0; i < len(a); 1++ {} 288 for i := 0; i < len(a); 1++ { a[i] = i } 289 for i := 0; i < len(a); 1++ { a[i] = i 290 } // multiple lines 291 292 for i := range a {} 293 for i := range a { a[i] = i } 294 for i := range a { a[i] = i 295 } // multiple lines 296 297 go func() { for { a <- <-b } }() 298 defer func() { if x := recover(); x != nil { err = fmt.Sprintf("error: %s", x.msg) } }() 299 } 300 301 302 // Don't remove mandatory parentheses around composite literals in control clauses. 303 func _() { 304 // strip parentheses - no composite literals or composite literals don't start with a type name 305 if (x) {} 306 if (((x))) {} 307 if ([]T{}) {} 308 if (([]T{})) {} 309 if ; (((([]T{})))) {} 310 311 for (x) {} 312 for (((x))) {} 313 for ([]T{}) {} 314 for (([]T{})) {} 315 for ; (((([]T{})))) ; {} 316 317 switch (x) {} 318 switch (((x))) {} 319 switch ([]T{}) {} 320 switch ; (((([]T{})))) {} 321 322 for _ = range ((([]T{T{42}}))) {} 323 324 // leave parentheses - composite literals start with a type name 325 if (T{}) {} 326 if ((T{})) {} 327 if ; ((((T{})))) {} 328 329 for (T{}) {} 330 for ((T{})) {} 331 for ; ((((T{})))) ; {} 332 333 switch (T{}) {} 334 switch ; ((((T{})))) {} 335 336 for _ = range (((T1{T{42}}))) {} 337 338 if x == (T{42}[0]) {} 339 if (x == T{42}[0]) {} 340 if (x == (T{42}[0])) {} 341 if (x == (((T{42}[0])))) {} 342 if (((x == (T{42}[0])))) {} 343 if x == a + b*(T{42}[0]) {} 344 if (x == a + b*T{42}[0]) {} 345 if (x == a + b*(T{42}[0])) {} 346 if (x == a + ((b * (T{42}[0])))) {} 347 if (((x == a + b * (T{42}[0])))) {} 348 if (((a + b * (T{42}[0])) == x)) {} 349 if (((a + b * (T{42}[0])))) == x {} 350 351 if (struct{x bool}{false}.x) {} 352 if (struct{x bool}{false}.x) == false {} 353 if (struct{x bool}{false}.x == false) {} 354 } 355 356 357 // Extra empty lines inside functions. Do respect source code line 358 // breaks between statement boundaries but print at most one empty 359 // line at a time. 360 func _() { 361 362 const _ = 0 363 364 const _ = 1 365 type _ int 366 type _ float 367 368 var _ = 0 369 var x = 1 370 371 // Each use(x) call below should have at most one empty line before and after. 372 // Known bug: The first use call may have more than one empty line before 373 // (see go/printer/nodes.go, func linebreak). 374 375 376 377 use(x) 378 379 if x < x { 380 381 use(x) 382 383 } else { 384 385 use(x) 386 387 } 388 } 389 390 391 // Formatting around labels. 392 func _() { 393 L: 394 } 395 396 397 func _() { 398 // this comment should be indented 399 L: ; // no semicolon needed 400 } 401 402 403 func _() { 404 switch 0 { 405 case 0: 406 L0: ; // semicolon required 407 case 1: 408 L1: ; // semicolon required 409 default: 410 L2: ; // no semicolon needed 411 } 412 } 413 414 415 func _() { 416 f() 417 L1: 418 f() 419 L2: 420 ; 421 L3: 422 } 423 424 425 func _() { 426 // this comment should be indented 427 L: 428 } 429 430 431 func _() { 432 L: _ = 0 433 } 434 435 436 func _() { 437 // this comment should be indented 438 L: _ = 0 439 } 440 441 442 func _() { 443 for { 444 L1: _ = 0 445 L2: 446 _ = 0 447 } 448 } 449 450 451 func _() { 452 // this comment should be indented 453 for { 454 L1: _ = 0 455 L2: 456 _ = 0 457 } 458 } 459 460 461 func _() { 462 if true { 463 _ = 0 464 } 465 _ = 0 // the indentation here should not be affected by the long label name 466 AnOverlongLabel: 467 _ = 0 468 469 if true { 470 _ = 0 471 } 472 _ = 0 473 474 L: _ = 0 475 } 476 477 478 func _() { 479 for { 480 goto L 481 } 482 L: 483 484 MoreCode() 485 } 486 487 488 func _() { 489 for { 490 goto L 491 } 492 L: // A comment on the same line as the label, followed by a single empty line. 493 // Known bug: There may be more than one empty line before MoreCode() 494 // (see go/printer/nodes.go, func linebreak). 495 496 497 498 499 MoreCode() 500 } 501 502 503 func _() { 504 for { 505 goto L 506 } 507 L: 508 509 510 511 512 // There should be a single empty line before this comment. 513 MoreCode() 514 } 515 516 517 func _() { 518 for { 519 goto AVeryLongLabelThatShouldNotAffectFormatting 520 } 521 AVeryLongLabelThatShouldNotAffectFormatting: 522 // There should be a single empty line after this comment. 523 524 // There should be a single empty line before this comment. 525 MoreCode() 526 } 527 528 529 // Formatting of empty statements. 530 func _() { 531 ;;;;;;;;;;;;;;;;;;;;;;;;; 532 } 533 534 func _() {;;;;;;;;;;;;;;;;;;;;;;;;; 535 } 536 537 func _() {;;;;;;;;;;;;;;;;;;;;;;;;;} 538 539 func _() { 540 f();;;;;;;;;;;;;;;;;;;;;;;;; 541 } 542 543 func _() { 544 L:;;;;;;;;;;;; 545 } 546 547 func _() { 548 L:;;;;;;;;;;;; 549 f() 550 }