Text file src/go/printer/testdata/comments.input

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

View as plain text