github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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 // This comment group should be separated 101 // with a newline from the next comment 102 // group. 103 104 // This comment should NOT be associated with the next declaration. 105 106 var x int // x 107 var () 108 109 // This comment SHOULD be associated with f0. 110 func f0() { 111 const pi = 3.14 // pi 112 var s1 struct{} /* an empty struct */ /* foo */ 113 // a struct constructor 114 // -------------------- 115 var s2 struct{} = struct{}{} 116 x := pi 117 } 118 119 // 120 // This comment should be associated with f1, with one blank line before the comment. 121 // 122 func f1() { 123 f0() 124 /* 1 */ 125 // 2 126 /* 3 */ 127 /* 4 */ 128 f0() 129 } 130 131 func _() { 132 // this comment should be properly indented 133 } 134 135 func _(x int) int { 136 if x < 0 { // the tab printed before this comment's // must not affect the remaining lines 137 return -x // this statement should be properly indented 138 } 139 if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */ 140 return -x // this statement should be properly indented 141 } 142 return x 143 } 144 145 func typeswitch(x interface{}) { 146 switch v := x.(type) { 147 case bool, int, float: 148 case string: 149 default: 150 } 151 152 switch x.(type) { 153 } 154 155 switch v0, ok := x.(int); v := x.(type) { 156 } 157 158 switch v0, ok := x.(int); x.(type) { 159 case byte: // this comment should be on the same line as the keyword 160 // this comment should be normally indented 161 _ = 0 162 case bool, int, float: 163 // this comment should be indented 164 case string: 165 default: 166 // this comment should be indented 167 } 168 // this comment should not be indented 169 } 170 171 // 172 // Indentation of comments after possibly indented multi-line constructs 173 // (test cases for issue 3147). 174 // 175 176 func _() { 177 s := 1 + 178 2 179 // should be indented like s 180 } 181 182 func _() { 183 s := 1 + 184 2 // comment 185 // should be indented like s 186 } 187 188 func _() { 189 s := 1 + 190 2 // comment 191 // should be indented like s 192 _ = 0 193 } 194 195 func _() { 196 s := 1 + 197 2 198 // should be indented like s 199 _ = 0 200 } 201 202 func _() { 203 s := 1 + 204 2 205 206 // should be indented like s 207 } 208 209 func _() { 210 s := 1 + 211 2 // comment 212 213 // should be indented like s 214 } 215 216 func _() { 217 s := 1 + 218 2 // comment 219 220 // should be indented like s 221 _ = 0 222 } 223 224 func _() { 225 s := 1 + 226 2 227 228 // should be indented like s 229 _ = 0 230 } 231 232 // Test case from issue 3147. 233 func f() { 234 templateText := "a" + // A 235 "b" + // B 236 "c" // C 237 238 // should be aligned with f() 239 f() 240 } 241 242 // Modified test case from issue 3147. 243 func f() { 244 templateText := "a" + // A 245 "b" + // B 246 "c" // C 247 248 // may not be aligned with f() (source is not aligned) 249 f() 250 } 251 252 // 253 // Test cases for alignment of lines in general comments. 254 // 255 256 func _() { 257 /* freestanding comment 258 aligned line 259 aligned line 260 */ 261 } 262 263 func _() { 264 /* freestanding comment 265 aligned line 266 aligned line 267 */ 268 } 269 270 func _() { 271 /* freestanding comment 272 aligned line 273 aligned line */ 274 } 275 276 func _() { 277 /* freestanding comment 278 aligned line 279 aligned line 280 */ 281 } 282 283 func _() { 284 /* freestanding comment 285 aligned line 286 aligned line 287 */ 288 } 289 290 func _() { 291 /* freestanding comment 292 aligned line 293 aligned line */ 294 } 295 296 func _() { 297 /* 298 freestanding comment 299 aligned line 300 aligned line 301 */ 302 } 303 304 func _() { 305 /* 306 freestanding comment 307 aligned line 308 aligned line 309 */ 310 } 311 312 func _() { 313 /* 314 freestanding comment 315 aligned line 316 aligned line */ 317 } 318 319 func _() { 320 /* 321 freestanding comment 322 aligned line 323 aligned line 324 */ 325 } 326 327 func _() { 328 /* 329 freestanding comment 330 aligned line 331 aligned line 332 */ 333 } 334 335 func _() { 336 /* 337 freestanding comment 338 aligned line 339 aligned line */ 340 } 341 342 func _() { 343 /* freestanding comment 344 aligned line 345 */ 346 } 347 348 func _() { 349 /* freestanding comment 350 aligned line 351 */ 352 } 353 354 func _() { 355 /* freestanding comment 356 aligned line */ 357 } 358 359 func _() { 360 /* freestanding comment 361 aligned line 362 */ 363 } 364 365 func _() { 366 /* freestanding comment 367 aligned line 368 */ 369 } 370 371 func _() { 372 /* freestanding comment 373 aligned line */ 374 } 375 376 func _() { 377 /* 378 freestanding comment 379 aligned line 380 */ 381 } 382 383 func _() { 384 /* 385 freestanding comment 386 aligned line 387 */ 388 } 389 390 func _() { 391 /* 392 freestanding comment 393 aligned line */ 394 } 395 396 func _() { 397 /* 398 freestanding comment 399 aligned line 400 */ 401 } 402 403 func _() { 404 /* 405 freestanding comment 406 aligned line 407 */ 408 } 409 410 func _() { 411 /* 412 freestanding comment 413 aligned line */ 414 } 415 416 // Issue 9751. 417 func _() { 418 /*a string 419 420 b string*/ 421 422 /*A string 423 424 425 426 Z string*/ 427 428 /*a string 429 430 b string 431 432 c string*/ 433 434 { 435 /*a string 436 b string*/ 437 438 /*a string 439 440 b string*/ 441 442 /*a string 443 444 b string 445 446 c string*/ 447 } 448 449 { 450 /*a string 451 b string*/ 452 453 /*a string 454 455 b string*/ 456 457 /*a string 458 459 b string 460 461 c string*/ 462 } 463 464 /* 465 */ 466 467 /* 468 469 */ 470 471 /* 472 473 * line 474 475 */ 476 } 477 478 /* 479 * line 480 * of 481 * stars 482 */ 483 484 /* another line 485 * of 486 * stars */ 487 488 /* and another line 489 * of 490 * stars */ 491 492 /* a line of 493 * stars */ 494 495 /* and another line of 496 * stars */ 497 498 /* a line of stars 499 */ 500 501 /* and another line of 502 */ 503 504 /* a line of stars 505 */ 506 507 /* and another line of 508 */ 509 510 /* 511 aligned in middle 512 here 513 not here 514 */ 515 516 /* 517 blank line in middle: 518 519 with no leading spaces on blank line. 520 */ 521 522 /* 523 aligned in middle 524 here 525 not here 526 */ 527 528 /* 529 blank line in middle: 530 531 with no leading spaces on blank line. 532 */ 533 534 func _() { 535 /* 536 * line 537 * of 538 * stars 539 */ 540 541 /* 542 aligned in middle 543 here 544 not here 545 */ 546 547 /* 548 blank line in middle: 549 550 with no leading spaces on blank line. 551 */ 552 } 553 554 // Some interesting interspersed comments. 555 // See below for more common cases. 556 func _( /* this */ x /* is */ /* an */ int) { 557 } 558 559 func _( /* no params - extra blank before and after comment */ ) {} 560 func _(a, b int /* params - no extra blank after comment */) {} 561 562 func _() { f( /* no args - extra blank before and after comment */ ) } 563 func _() { f(a, b /* args - no extra blank after comment */) } 564 565 func _() { 566 f( /* no args - extra blank before and after comment */ ) 567 f(a, b /* args - no extra blank after comment */) 568 } 569 570 func ( /* comment1 */ T /* comment2 */) _() {} 571 572 func _() { /* "short-ish one-line functions with comments are formatted as multi-line functions */ } 573 func _() { x := 0; /* comment */ y = x /* comment */ } 574 575 func _() { 576 _ = 0 577 /* closing curly brace should be on new line */ 578 } 579 580 func _() { 581 _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */} 582 } 583 584 // Test cases from issue 1542: 585 // Comments must not be placed before commas and cause invalid programs. 586 func _() { 587 var a = []int{1, 2 /*jasldf*/} 588 _ = a 589 } 590 591 func _() { 592 var a = []int{1, 2}/*jasldf 593 */ 594 595 _ = a 596 } 597 598 func _() { 599 var a = []int{1, 2}// jasldf 600 601 _ = a 602 } 603 604 // Comments immediately adjacent to punctuation followed by a newline 605 // remain after the punctuation (looks better and permits alignment of 606 // comments). 607 func _() { 608 _ = T{ 609 1, // comment after comma 610 2, /* comment after comma */ 611 3, // comment after comma 612 } 613 _ = T{ 614 1, // comment after comma 615 2, /* comment after comma */ 616 3, // comment after comma 617 } 618 _ = T{ 619 /* comment before literal */ 1, 620 2, /* comment before comma - ok to move after comma */ 621 3, /* comment before comma - ok to move after comma */ 622 } 623 624 for i = 0; // comment after semicolon 625 i < 9; /* comment after semicolon */ 626 i++ { // comment after opening curly brace 627 } 628 629 // TODO(gri) the last comment in this example should be aligned */ 630 for i = 0; // comment after semicolon 631 i < 9; /* comment before semicolon - ok to move after semicolon */ 632 i++ /* comment before opening curly brace */ { 633 } 634 } 635 636 // If there is no newline following punctuation, commas move before the punctuation. 637 // This way, commas interspersed in lists stay with the respective expression. 638 func f(x /* comment */, y int, z int /* comment */, u, v, w int /* comment */) { 639 f(x /* comment */, y) 640 f(x, /* comment */ 641 y) 642 f( 643 x, /* comment */ 644 ) 645 } 646 647 func g( 648 x int, /* comment */ 649 ) { 650 } 651 652 type _ struct { 653 a, b /* comment */, c int 654 } 655 656 type _ struct { 657 a, b /* comment */, c int 658 } 659 660 func _() { 661 for a /* comment */, b := range x { 662 } 663 } 664 665 // Print line directives correctly. 666 667 // The following is a legal line directive. 668 //line foo:1 669 func _() { 670 _ = 0 671 // The following is a legal line directive. It must not be indented: 672 //line foo:2 673 _ = 1 674 675 // The following is not a legal line directive (it doesn't start in column 1): 676 //line foo:2 677 _ = 2 678 679 // The following is not a legal line directive (negative line number): 680 //line foo:-3 681 _ = 3 682 } 683 684 // Line comments with tabs 685 func _() { 686 var finput *bufio.Reader // input file 687 var stderr *bufio.Writer 688 var ftable *bufio.Writer // y.go file 689 var foutput *bufio.Writer // y.output file 690 691 var oflag string // -o [y.go] - y.go file 692 var vflag string // -v [y.output] - y.output file 693 var lflag bool // -l - disable line directives 694 } 695 696 // Trailing white space in comments should be trimmed 697 func _() { 698 // This comment has 4 blanks following that should be trimmed: 699 /* Each line of this comment has blanks or tabs following that should be trimmed: 700 line 2: 701 line 3: 702 */ 703 } 704 705 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */