github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/go/printer/testdata/comments.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 // This is a package for testing comment placement by go/printer. 6 // 7 package main 8 9 import "fmt" // fmt 10 11 const c0 = 0 // zero 12 const ( 13 c1 = iota // c1 14 c2 // c2 15 ) 16 17 // Alignment of comments in declarations> 18 const ( 19 _ T = iota // comment 20 _ // comment 21 _ // comment 22 _ = iota+10 23 _ // comments 24 25 _ = 10 // comment 26 _ T = 20 // comment 27 ) 28 29 const ( 30 _____ = iota // foo 31 _ // bar 32 _ = 0 // bal 33 _ // bat 34 ) 35 36 const ( 37 _ T = iota // comment 38 _ // comment 39 _ // comment 40 _ = iota + 10 41 _ // comment 42 _ = 10 43 _ = 20 // comment 44 _ T = 0 // comment 45 ) 46 47 // The SZ struct; it is empty. 48 type SZ struct {} 49 50 // The S0 struct; no field is exported. 51 type S0 struct { 52 int 53 x, y, z int // 3 unexported fields 54 } 55 56 // The S1 struct; some fields are not exported. 57 type S1 struct { 58 S0 59 A, B, C float // 3 exported fields 60 D, b, c int // 2 unexported fields 61 } 62 63 // The S2 struct; all fields are exported. 64 type S2 struct { 65 S1 66 A, B, C float // 3 exported fields 67 } 68 69 // The IZ interface; it is empty. 70 type SZ interface {} 71 72 // The I0 interface; no method is exported. 73 type I0 interface { 74 f(x int) int // unexported method 75 } 76 77 // The I1 interface; some methods are not exported. 78 type I1 interface { 79 I0 80 F(x float) float // exported methods 81 g(x int) int // unexported method 82 } 83 84 // The I2 interface; all methods are exported. 85 type I2 interface { 86 I0 87 F(x float) float // exported method 88 G(x float) float // exported method 89 } 90 91 // The S3 struct; all comments except for the last one must appear in the export. 92 type S3 struct { 93 // lead comment for F1 94 F1 int // line comment for F1 95 // lead comment for F2 96 F2 int // line comment for F2 97 f3 int // f3 is not exported 98 } 99 100 type T struct { // foo 101 102 // bar 103 x int 104 X int 105 } 106 107 type T struct { /* foo */ 108 109 // bar 110 x int 111 } 112 113 type T struct { // foo 114 115 x int 116 117 X int 118 } 119 120 type T struct { /* foo */ 121 122 123 124 x int 125 } 126 127 type T struct {x int 128 } 129 130 type T struct { 131 x int} 132 133 type T struct {x int} 134 135 type T struct { 136 137 x int 138 } 139 140 type T struct { 141 x int 142 X int 143 x int 144 } 145 146 type T struct { 147 // Comment 148 x int 149 // X. 150 X int 151 // Comment 152 x int 153 } 154 155 const ( // foo 156 157 // bar 158 x int = 0 159 X int = 0 160 ) 161 162 const ( /* foo */ 163 164 // bar 165 x int = 0 166 ) 167 168 const ( // foo 169 170 x int = 0 171 172 X int = 0 173 ) 174 175 const ( /* foo */ 176 177 x int = 0 178 ) 179 180 const ( 181 182 x int = 0 183 ) 184 185 const ( 186 187 x int = 0 188 X int = 0 189 190 ) 191 192 const ( 193 x int = 0 194 X int = 0 195 x int = 0 196 ) 197 198 const ( 199 // Comment 200 x int = 0 201 // X. 202 X int = 0 203 // Comment 204 x int = 0 205 ) 206 207 // This comment group should be separated 208 // with a newline from the next comment 209 // group. 210 211 // This comment should NOT be associated with the next declaration. 212 213 var x int // x 214 var () 215 216 217 // This comment SHOULD be associated with f0. 218 func f0() { 219 const pi = 3.14 // pi 220 var s1 struct {} /* an empty struct */ /* foo */ 221 // a struct constructor 222 // -------------------- 223 var s2 struct {} = struct {}{} 224 x := pi 225 } 226 // 227 // This comment should be associated with f1, with one blank line before the comment. 228 // 229 func f1() { 230 f0() 231 /* 1 */ 232 // 2 233 /* 3 */ 234 /* 4 */ 235 f0() 236 } 237 238 239 func _() { 240 // this comment should be properly indented 241 } 242 243 244 func _(x int) int { 245 if x < 0 { // the tab printed before this comment's // must not affect the remaining lines 246 return -x // this statement should be properly indented 247 } 248 if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */ 249 return -x // this statement should be properly indented 250 } 251 return x 252 } 253 254 255 func typeswitch(x interface{}) { 256 switch v := x.(type) { 257 case bool, int, float: 258 case string: 259 default: 260 } 261 262 switch x.(type) { 263 } 264 265 switch v0, ok := x.(int); v := x.(type) { 266 } 267 268 switch v0, ok := x.(int); x.(type) { 269 case byte: // this comment should be on the same line as the keyword 270 // this comment should be normally indented 271 _ = 0 272 case bool, int, float: 273 // this comment should be indented 274 case string: 275 default: 276 // this comment should be indented 277 } 278 // this comment should not be indented 279 } 280 281 // 282 // Indentation of comments after possibly indented multi-line constructs 283 // (test cases for issue 3147). 284 // 285 286 func _() { 287 s := 1 + 288 2 289 // should be indented like s 290 } 291 292 func _() { 293 s := 1 + 294 2 // comment 295 // should be indented like s 296 } 297 298 func _() { 299 s := 1 + 300 2 // comment 301 // should be indented like s 302 _ = 0 303 } 304 305 func _() { 306 s := 1 + 307 2 308 // should be indented like s 309 _ = 0 310 } 311 312 func _() { 313 s := 1 + 314 2 315 316 // should be indented like s 317 } 318 319 func _() { 320 s := 1 + 321 2 // comment 322 323 // should be indented like s 324 } 325 326 func _() { 327 s := 1 + 328 2 // comment 329 330 // should be indented like s 331 _ = 0 332 } 333 334 func _() { 335 s := 1 + 336 2 337 338 // should be indented like s 339 _ = 0 340 } 341 342 // Test case from issue 3147. 343 func f() { 344 templateText := "a" + // A 345 "b" + // B 346 "c" // C 347 348 // should be aligned with f() 349 f() 350 } 351 352 // Modified test case from issue 3147. 353 func f() { 354 templateText := "a" + // A 355 "b" + // B 356 "c" // C 357 358 // may not be aligned with f() (source is not aligned) 359 f() 360 } 361 362 // 363 // Test cases for alignment of lines in general comments. 364 // 365 366 func _() { 367 /* freestanding comment 368 aligned line 369 aligned line 370 */ 371 } 372 373 func _() { 374 /* freestanding comment 375 aligned line 376 aligned line 377 */ 378 } 379 380 func _() { 381 /* freestanding comment 382 aligned line 383 aligned line */ 384 } 385 386 func _() { 387 /* freestanding comment 388 aligned line 389 aligned line 390 */ 391 } 392 393 func _() { 394 /* freestanding comment 395 aligned line 396 aligned line 397 */ 398 } 399 400 func _() { 401 /* freestanding comment 402 aligned line 403 aligned line */ 404 } 405 406 407 func _() { 408 /* 409 freestanding comment 410 aligned line 411 aligned line 412 */ 413 } 414 415 func _() { 416 /* 417 freestanding comment 418 aligned line 419 aligned line 420 */ 421 } 422 423 func _() { 424 /* 425 freestanding comment 426 aligned line 427 aligned line */ 428 } 429 430 func _() { 431 /* 432 freestanding comment 433 aligned line 434 aligned line 435 */ 436 } 437 438 func _() { 439 /* 440 freestanding comment 441 aligned line 442 aligned line 443 */ 444 } 445 446 func _() { 447 /* 448 freestanding comment 449 aligned line 450 aligned line */ 451 } 452 453 func _() { 454 /* freestanding comment 455 aligned line 456 */ 457 } 458 459 func _() { 460 /* freestanding comment 461 aligned line 462 */ 463 } 464 465 func _() { 466 /* freestanding comment 467 aligned line */ 468 } 469 470 func _() { 471 /* freestanding comment 472 aligned line 473 */ 474 } 475 476 func _() { 477 /* freestanding comment 478 aligned line 479 */ 480 } 481 482 func _() { 483 /* freestanding comment 484 aligned line */ 485 } 486 487 488 func _() { 489 /* 490 freestanding comment 491 aligned line 492 */ 493 } 494 495 func _() { 496 /* 497 freestanding comment 498 aligned line 499 */ 500 } 501 502 func _() { 503 /* 504 freestanding comment 505 aligned line */ 506 } 507 508 func _() { 509 /* 510 freestanding comment 511 aligned line 512 */ 513 } 514 515 func _() { 516 /* 517 freestanding comment 518 aligned line 519 */ 520 } 521 522 func _() { 523 /* 524 freestanding comment 525 aligned line */ 526 } 527 528 // Issue 9751. 529 func _() { 530 /*a string 531 532 b string*/ 533 534 /*A string 535 536 537 538 Z string*/ 539 540 /*a string 541 542 b string 543 544 c string*/ 545 546 { 547 /*a string 548 b string*/ 549 550 /*a string 551 552 b string*/ 553 554 /*a string 555 556 b string 557 558 c string*/ 559 } 560 561 { 562 /*a string 563 b string*/ 564 565 /*a string 566 567 b string*/ 568 569 /*a string 570 571 b string 572 573 c string*/ 574 } 575 576 /* 577 */ 578 579 /* 580 581 */ 582 583 /* 584 585 * line 586 587 */ 588 } 589 590 /* 591 * line 592 * of 593 * stars 594 */ 595 596 /* another line 597 * of 598 * stars */ 599 600 /* and another line 601 * of 602 * stars */ 603 604 /* a line of 605 * stars */ 606 607 /* and another line of 608 * stars */ 609 610 /* a line of stars 611 */ 612 613 /* and another line of 614 */ 615 616 /* a line of stars 617 */ 618 619 /* and another line of 620 */ 621 622 /* 623 aligned in middle 624 here 625 not here 626 */ 627 628 /* 629 blank line in middle: 630 631 with no leading spaces on blank line. 632 */ 633 634 /* 635 aligned in middle 636 here 637 not here 638 */ 639 640 /* 641 blank line in middle: 642 643 with no leading spaces on blank line. 644 */ 645 646 func _() { 647 /* 648 * line 649 * of 650 * stars 651 */ 652 653 /* 654 aligned in middle 655 here 656 not here 657 */ 658 659 /* 660 blank line in middle: 661 662 with no leading spaces on blank line. 663 */ 664 } 665 666 667 // Some interesting interspersed comments. 668 // See below for more common cases. 669 func _(/* this */x/* is *//* an */ int) { 670 } 671 672 func _(/* no params - extra blank before and after comment */) {} 673 func _(a, b int /* params - no extra blank after comment */) {} 674 675 func _() { f(/* no args - extra blank before and after comment */) } 676 func _() { f(a, b /* args - no extra blank after comment */) } 677 678 func _() { 679 f(/* no args - extra blank before and after comment */) 680 f(a, b /* args - no extra blank after comment */) 681 } 682 683 func (/* comment1 */ T /* comment2 */) _() {} 684 685 func _() { /* "short-ish one-line functions with comments are formatted as multi-line functions */ } 686 func _() { x := 0; /* comment */ y = x /* comment */ } 687 688 func _() { 689 _ = 0 690 /* closing curly brace should be on new line */ } 691 692 func _() { 693 _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */} 694 } 695 696 // Test cases from issue 1542: 697 // Comments must not be placed before commas and cause invalid programs. 698 func _() { 699 var a = []int{1, 2, /*jasldf*/ 700 } 701 _ = a 702 } 703 704 func _() { 705 var a = []int{1, 2, /*jasldf 706 */ 707 } 708 _ = a 709 } 710 711 func _() { 712 var a = []int{1, 2, // jasldf 713 } 714 _ = a 715 } 716 717 // Test cases from issues 11274, 15137: 718 // Semicolon must not be lost when multiple statements are on the same line with a comment. 719 func _() { 720 x := 0 /**/; y := 1 721 } 722 723 func _() { 724 f(); f() 725 f(); /* comment */ f() 726 f() /* comment */; f() 727 f(); /* a */ /* b */ f() 728 f() /* a */ /* b */; f() 729 f() /* a */; /* b */ f() 730 } 731 732 func _() { 733 f() /* a */ /* b */ } 734 735 // Comments immediately adjacent to punctuation followed by a newline 736 // remain after the punctuation (looks better and permits alignment of 737 // comments). 738 func _() { 739 _ = T{ 740 1, // comment after comma 741 2, /* comment after comma */ 742 3 , // comment after comma 743 } 744 _ = T{ 745 1 ,// comment after comma 746 2 ,/* comment after comma */ 747 3,// comment after comma 748 } 749 _ = T{ 750 /* comment before literal */1, 751 2/* comment before comma - ok to move after comma */, 752 3 /* comment before comma - ok to move after comma */ , 753 } 754 755 for 756 i=0;// comment after semicolon 757 i<9;/* comment after semicolon */ 758 i++{// comment after opening curly brace 759 } 760 761 // TODO(gri) the last comment in this example should be aligned */ 762 for 763 i=0;// comment after semicolon 764 i<9/* comment before semicolon - ok to move after semicolon */; 765 i++ /* comment before opening curly brace */ { 766 } 767 } 768 769 // If there is no newline following punctuation, commas move before the punctuation. 770 // This way, commas interspersed in lists stay with the respective expression. 771 func f(x/* comment */, y int, z int /* comment */, u, v, w int /* comment */) { 772 f(x /* comment */, y) 773 f(x /* comment */, 774 y) 775 f( 776 x /* comment */, 777 ) 778 } 779 780 func g( 781 x int /* comment */, 782 ) {} 783 784 type _ struct { 785 a, b /* comment */, c int 786 } 787 788 type _ struct { a, b /* comment */, c int } 789 790 func _() { 791 for a /* comment */, b := range x { 792 } 793 } 794 795 // Print line directives correctly. 796 797 // The following is a legal line directive. 798 //line foo:1 799 func _() { 800 _ = 0 801 // The following is a legal line directive. It must not be indented: 802 //line foo:2 803 _ = 1 804 805 // The following is not a legal line directive (it doesn't start in column 1): 806 //line foo:2 807 _ = 2 808 809 // The following is not a legal line directive (negative line number): 810 //line foo:-3 811 _ = 3 812 } 813 814 // Line comments with tabs 815 func _() { 816 var finput *bufio.Reader // input file 817 var stderr *bufio.Writer 818 var ftable *bufio.Writer // y.go file 819 var foutput *bufio.Writer // y.output file 820 821 var oflag string // -o [y.go] - y.go file 822 var vflag string // -v [y.output] - y.output file 823 var lflag bool // -l - disable line directives 824 } 825 826 // Trailing white space in comments should be trimmed 827 func _() { 828 // This comment has 4 blanks following that should be trimmed: 829 /* Each line of this comment has blanks or tabs following that should be trimmed: 830 line 2: 831 line 3: 832 */ 833 } 834 835 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */