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