github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/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 /* 417 * line 418 * of 419 * stars 420 */ 421 422 /* another line 423 * of 424 * stars */ 425 426 /* and another line 427 * of 428 * stars */ 429 430 /* a line of 431 * stars */ 432 433 /* and another line of 434 * stars */ 435 436 /* a line of stars 437 */ 438 439 /* and another line of 440 */ 441 442 /* a line of stars 443 */ 444 445 /* and another line of 446 */ 447 448 /* 449 aligned in middle 450 here 451 not here 452 */ 453 454 /* 455 blank line in middle: 456 457 with no leading spaces on blank line. 458 */ 459 460 /* 461 aligned in middle 462 here 463 not here 464 */ 465 466 /* 467 blank line in middle: 468 469 with no leading spaces on blank line. 470 */ 471 472 func _() { 473 /* 474 * line 475 * of 476 * stars 477 */ 478 479 /* 480 aligned in middle 481 here 482 not here 483 */ 484 485 /* 486 blank line in middle: 487 488 with no leading spaces on blank line. 489 */ 490 } 491 492 // Some interesting interspersed comments. 493 // See below for more common cases. 494 func _( /* this */ x /* is */ /* an */ int) { 495 } 496 497 func _( /* no params */) {} 498 499 func _() { 500 f( /* no args */) 501 } 502 503 func ( /* comment1 */ T /* comment2 */) _() {} 504 505 func _() { /* one-line functions with comments are formatted as multi-line functions */ 506 } 507 508 func _() { 509 _ = 0 510 /* closing curly brace should be on new line */ 511 } 512 513 func _() { 514 _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */} 515 } 516 517 // Test cases from issue 1542: 518 // Comments must not be placed before commas and cause invalid programs. 519 func _() { 520 var a = []int{1, 2 /*jasldf*/} 521 _ = a 522 } 523 524 func _() { 525 var a = []int{1, 2}/*jasldf 526 */ 527 528 _ = a 529 } 530 531 func _() { 532 var a = []int{1, 2}// jasldf 533 534 _ = a 535 } 536 537 // Comments immediately adjacent to punctuation followed by a newline 538 // remain after the punctuation (looks better and permits alignment of 539 // comments). 540 func _() { 541 _ = T{ 542 1, // comment after comma 543 2, /* comment after comma */ 544 3, // comment after comma 545 } 546 _ = T{ 547 1, // comment after comma 548 2, /* comment after comma */ 549 3, // comment after comma 550 } 551 _ = T{ 552 /* comment before literal */ 1, 553 2, /* comment before comma - ok to move after comma */ 554 3, /* comment before comma - ok to move after comma */ 555 } 556 557 for i = 0; // comment after semicolon 558 i < 9; /* comment after semicolon */ 559 i++ { // comment after opening curly brace 560 } 561 562 // TODO(gri) the last comment in this example should be aligned */ 563 for i = 0; // comment after semicolon 564 i < 9; /* comment before semicolon - ok to move after semicolon */ 565 i++ /* comment before opening curly brace */ { 566 } 567 } 568 569 // If there is no newline following punctuation, commas move before the punctuation. 570 // This way, commas interspersed in lists stay with the respective expression. 571 func f(x /* comment */, y int, z int /* comment */, u, v, w int /* comment */) { 572 f(x /* comment */, y) 573 f(x, /* comment */ 574 y) 575 f( 576 x, /* comment */ 577 ) 578 } 579 580 func g( 581 x int, /* comment */ 582 ) { 583 } 584 585 type _ struct { 586 a, b /* comment */, c int 587 } 588 589 type _ struct { 590 a, b /* comment */, c int 591 } 592 593 func _() { 594 for a /* comment */, b := range x { 595 } 596 } 597 598 // Print line directives correctly. 599 600 // The following is a legal line directive. 601 //line foo:1 602 func _() { 603 _ = 0 604 // The following is a legal line directive. It must not be indented: 605 //line foo:2 606 _ = 1 607 608 // The following is not a legal line directive (it doesn't start in column 1): 609 //line foo:2 610 _ = 2 611 612 // The following is not a legal line directive (negative line number): 613 //line foo:-3 614 _ = 3 615 } 616 617 // Line comments with tabs 618 func _() { 619 var finput *bufio.Reader // input file 620 var stderr *bufio.Writer 621 var ftable *bufio.Writer // y.go file 622 var foutput *bufio.Writer // y.output file 623 624 var oflag string // -o [y.go] - y.go file 625 var vflag string // -v [y.output] - y.output file 626 var lflag bool // -l - disable line directives 627 } 628 629 // Trailing white space in comments should be trimmed 630 func _() { 631 // This comment has 4 blanks following that should be trimmed: 632 /* Each line of this comment has blanks or tabs following that should be trimmed: 633 line 2: 634 line 3: 635 */ 636 } 637 638 /* This comment is the last entry in this file. It must be printed and should be followed by a newline */