github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/go/printer/testdata/comments.golden (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 x int 123 } 124 125 type T struct { 126 x int 127 } 128 129 type T struct { 130 x int 131 } 132 133 type T struct{ x int } 134 135 type T struct { 136 x int 137 } 138 139 type T struct { 140 x int 141 X int 142 x int 143 } 144 145 type T struct { 146 // Comment 147 x int 148 // X. 149 X int 150 // Comment 151 x int 152 } 153 154 const ( // foo 155 156 // bar 157 x int = 0 158 X int = 0 159 ) 160 161 const ( /* foo */ 162 163 // bar 164 x int = 0 165 ) 166 167 const ( // foo 168 169 x int = 0 170 171 X int = 0 172 ) 173 174 const ( /* foo */ 175 176 x int = 0 177 ) 178 179 const ( 180 x int = 0 181 ) 182 183 const ( 184 x int = 0 185 X int = 0 186 ) 187 188 const ( 189 x int = 0 190 X int = 0 191 x int = 0 192 ) 193 194 const ( 195 // Comment 196 x int = 0 197 // X. 198 X int = 0 199 // Comment 200 x int = 0 201 ) 202 203 // This comment group should be separated 204 // with a newline from the next comment 205 // group. 206 207 // This comment should NOT be associated with the next declaration. 208 209 var x int // x 210 var () 211 212 // This comment SHOULD be associated with f0. 213 func f0() { 214 const pi = 3.14 // pi 215 var s1 struct{} /* an empty struct */ /* foo */ 216 // a struct constructor 217 // -------------------- 218 var s2 struct{} = struct{}{} 219 x := pi 220 } 221 222 // 223 // This comment should be associated with f1, with one blank line before the comment. 224 // 225 func f1() { 226 f0() 227 /* 1 */ 228 // 2 229 /* 3 */ 230 /* 4 */ 231 f0() 232 } 233 234 func _() { 235 // this comment should be properly indented 236 } 237 238 func _(x int) int { 239 if x < 0 { // the tab printed before this comment's // must not affect the remaining lines 240 return -x // this statement should be properly indented 241 } 242 if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */ 243 return -x // this statement should be properly indented 244 } 245 return x 246 } 247 248 func typeswitch(x interface{}) { 249 switch v := x.(type) { 250 case bool, int, float: 251 case string: 252 default: 253 } 254 255 switch x.(type) { 256 } 257 258 switch v0, ok := x.(int); v := x.(type) { 259 } 260 261 switch v0, ok := x.(int); x.(type) { 262 case byte: // this comment should be on the same line as the keyword 263 // this comment should be normally indented 264 _ = 0 265 case bool, int, float: 266 // this comment should be indented 267 case string: 268 default: 269 // this comment should be indented 270 } 271 // this comment should not be indented 272 } 273 274 // 275 // Indentation of comments after possibly indented multi-line constructs 276 // (test cases for issue 3147). 277 // 278 279 func _() { 280 s := 1 + 281 2 282 // should be indented like s 283 } 284 285 func _() { 286 s := 1 + 287 2 // comment 288 // should be indented like s 289 } 290 291 func _() { 292 s := 1 + 293 2 // comment 294 // should be indented like s 295 _ = 0 296 } 297 298 func _() { 299 s := 1 + 300 2 301 // should be indented like s 302 _ = 0 303 } 304 305 func _() { 306 s := 1 + 307 2 308 309 // should be indented like s 310 } 311 312 func _() { 313 s := 1 + 314 2 // comment 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 _ = 0 325 } 326 327 func _() { 328 s := 1 + 329 2 330 331 // should be indented like s 332 _ = 0 333 } 334 335 // Test case from issue 3147. 336 func f() { 337 templateText := "a" + // A 338 "b" + // B 339 "c" // C 340 341 // should be aligned with f() 342 f() 343 } 344 345 // Modified test case from issue 3147. 346 func f() { 347 templateText := "a" + // A 348 "b" + // B 349 "c" // C 350 351 // may not be aligned with f() (source is not aligned) 352 f() 353 } 354 355 // 356 // Test cases for alignment of lines in general comments. 357 // 358 359 func _() { 360 /* freestanding comment 361 aligned line 362 aligned line 363 */ 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 func _() { 380 /* freestanding comment 381 aligned line 382 aligned line 383 */ 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 func _() { 400 /* 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 func _() { 423 /* 424 freestanding comment 425 aligned line 426 aligned line 427 */ 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 func _() { 446 /* freestanding comment 447 aligned line 448 */ 449 } 450 451 func _() { 452 /* freestanding comment 453 aligned line 454 */ 455 } 456 457 func _() { 458 /* freestanding comment 459 aligned line */ 460 } 461 462 func _() { 463 /* freestanding comment 464 aligned line 465 */ 466 } 467 468 func _() { 469 /* freestanding comment 470 aligned line 471 */ 472 } 473 474 func _() { 475 /* freestanding comment 476 aligned line */ 477 } 478 479 func _() { 480 /* 481 freestanding comment 482 aligned line 483 */ 484 } 485 486 func _() { 487 /* 488 freestanding comment 489 aligned line 490 */ 491 } 492 493 func _() { 494 /* 495 freestanding comment 496 aligned line */ 497 } 498 499 func _() { 500 /* 501 freestanding comment 502 aligned line 503 */ 504 } 505 506 func _() { 507 /* 508 freestanding comment 509 aligned line 510 */ 511 } 512 513 func _() { 514 /* 515 freestanding comment 516 aligned line */ 517 } 518 519 // Issue 9751. 520 func _() { 521 /*a string 522 523 b string*/ 524 525 /*A string 526 527 528 529 Z string*/ 530 531 /*a string 532 533 b string 534 535 c string*/ 536 537 { 538 /*a string 539 b string*/ 540 541 /*a string 542 543 b string*/ 544 545 /*a string 546 547 b string 548 549 c string*/ 550 } 551 552 { 553 /*a string 554 b string*/ 555 556 /*a string 557 558 b string*/ 559 560 /*a string 561 562 b string 563 564 c string*/ 565 } 566 567 /* 568 */ 569 570 /* 571 572 */ 573 574 /* 575 576 * line 577 578 */ 579 } 580 581 /* 582 * line 583 * of 584 * stars 585 */ 586 587 /* another line 588 * of 589 * stars */ 590 591 /* and another line 592 * of 593 * stars */ 594 595 /* a line of 596 * stars */ 597 598 /* and another line of 599 * stars */ 600 601 /* a line of stars 602 */ 603 604 /* and another line of 605 */ 606 607 /* a line of stars 608 */ 609 610 /* and another line of 611 */ 612 613 /* 614 aligned in middle 615 here 616 not here 617 */ 618 619 /* 620 blank line in middle: 621 622 with no leading spaces on blank line. 623 */ 624 625 /* 626 aligned in middle 627 here 628 not here 629 */ 630 631 /* 632 blank line in middle: 633 634 with no leading spaces on blank line. 635 */ 636 637 func _() { 638 /* 639 * line 640 * of 641 * stars 642 */ 643 644 /* 645 aligned in middle 646 here 647 not here 648 */ 649 650 /* 651 blank line in middle: 652 653 with no leading spaces on blank line. 654 */ 655 } 656 657 // Some interesting interspersed comments. 658 // See below for more common cases. 659 func _( /* this */ x /* is */ /* an */ int) { 660 } 661 662 func _( /* no params - extra blank before and after comment */ ) {} 663 func _(a, b int /* params - no extra blank after comment */) {} 664 665 func _() { f( /* no args - extra blank before and after comment */ ) } 666 func _() { f(a, b /* args - no extra blank after comment */) } 667 668 func _() { 669 f( /* no args - extra blank before and after comment */ ) 670 f(a, b /* args - no extra blank after comment */) 671 } 672 673 func ( /* comment1 */ T /* comment2 */) _() {} 674 675 func _() { /* "short-ish one-line functions with comments are formatted as multi-line functions */ } 676 func _() { x := 0; /* comment */ y = x /* comment */ } 677 678 func _() { 679 _ = 0 680 /* closing curly brace should be on new line */ 681 } 682 683 func _() { 684 _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */} 685 } 686 687 // Test cases from issue 1542: 688 // Comments must not be placed before commas and cause invalid programs. 689 func _() { 690 var a = []int{1, 2 /*jasldf*/} 691 _ = a 692 } 693 694 func _() { 695 var a = []int{1, 2}/*jasldf 696 */ 697 698 _ = a 699 } 700 701 func _() { 702 var a = []int{1, 2}// jasldf 703 704 _ = a 705 } 706 707 // Test cases from issues 11274, 15137: 708 // Semicolon must not be lost when multiple statements are on the same line with a comment. 709 func _() { 710 x := 0 /**/ 711 y := 1 712 } 713 714 func _() { 715 f() 716 f() 717 f() /* comment */ 718 f() 719 f() /* comment */ 720 f() 721 f() /* a */ /* b */ 722 f() 723 f() /* a */ /* b */ 724 f() 725 f() /* a */ /* b */ 726 f() 727 } 728 729 func _() { 730 f() /* a */ /* b */ 731 } 732 733 // Comments immediately adjacent to punctuation followed by a newline 734 // remain after the punctuation (looks better and permits alignment of 735 // comments). 736 func _() { 737 _ = T{ 738 1, // comment after comma 739 2, /* comment after comma */ 740 3, // comment after comma 741 } 742 _ = T{ 743 1, // comment after comma 744 2, /* comment after comma */ 745 3, // comment after comma 746 } 747 _ = T{ 748 /* comment before literal */ 1, 749 2, /* comment before comma - ok to move after comma */ 750 3, /* comment before comma - ok to move after comma */ 751 } 752 753 for i = 0; // comment after semicolon 754 i < 9; /* comment after semicolon */ 755 i++ { // comment after opening curly brace 756 } 757 758 // TODO(gri) the last comment in this example should be aligned */ 759 for i = 0; // comment after semicolon 760 i < 9; /* comment before semicolon - ok to move after semicolon */ 761 i++ /* comment before opening curly brace */ { 762 } 763 } 764 765 // If there is no newline following punctuation, commas move before the punctuation. 766 // This way, commas interspersed in lists stay with the respective expression. 767 func f(x /* comment */, y int, z int /* comment */, u, v, w int /* comment */) { 768 f(x /* comment */, y) 769 f(x, /* comment */ 770 y) 771 f( 772 x, /* comment */ 773 ) 774 } 775 776 func g( 777 x int, /* comment */ 778 ) { 779 } 780 781 type _ struct { 782 a, b /* comment */, c int 783 } 784 785 type _ struct { 786 a, b /* comment */, c int 787 } 788 789 func _() { 790 for a /* comment */, b := range x { 791 } 792 } 793 794 // Print line directives correctly. 795 796 // The following is a legal line directive. 797 //line foo:1 798 func _() { 799 _ = 0 800 // The following is a legal line directive. It must not be indented: 801 //line foo:2 802 _ = 1 803 804 // The following is not a legal line directive (it doesn't start in column 1): 805 //line foo:2 806 _ = 2 807 808 // The following is not a legal line directive (negative line number): 809 //line foo:-3 810 _ = 3 811 } 812 813 // Line comments with tabs 814 func _() { 815 var finput *bufio.Reader // input file 816 var stderr *bufio.Writer 817 var ftable *bufio.Writer // y.go file 818 var foutput *bufio.Writer // y.output file 819 820 var oflag string // -o [y.go] - y.go file 821 var vflag string // -v [y.output] - y.output file 822 var lflag bool // -l - disable line directives 823 } 824 825 // Trailing white space in comments should be trimmed 826 func _() { 827 // This comment has 4 blanks following that should be trimmed: 828 /* Each line of this comment has blanks or tabs following that should be trimmed: 829 line 2: 830 line 3: 831 */ 832 } 833 834 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */