...
Run Format

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

Documentation: go/printer/testdata

     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	// Issue 9751.
   422	func _() {
   423		/*a string
   424	
   425		b string*/
   426	
   427		/*A string
   428	
   429	
   430	
   431		Z string*/
   432	
   433		/*a string
   434	
   435		b string
   436	
   437		c string*/
   438	
   439		{
   440			/*a string
   441	b string*/
   442	
   443			/*a string
   444	
   445	b string*/
   446	
   447			/*a string
   448	
   449	b string
   450	
   451	c string*/
   452		}
   453	
   454		{
   455			/*a string
   456					b string*/
   457	
   458			/*a string
   459	
   460					b string*/
   461	
   462			/*a string
   463	
   464					b string
   465	
   466					c string*/
   467		}
   468	
   469		/*
   470		*/
   471	
   472		/*
   473	
   474		*/
   475	
   476		/*
   477	
   478		 * line
   479	
   480		*/
   481	}
   482	
   483	/*
   484	 * line
   485	 * of
   486	 * stars
   487	 */
   488	
   489	/* another line
   490	 * of
   491	 * stars */
   492	
   493	/*	and another line
   494	 *	of
   495	 *	stars */
   496	
   497	/* a line of
   498	 * stars */
   499	
   500	/*	and another line of
   501	 *	stars */
   502	
   503	/* a line of stars
   504	*/
   505	
   506	/*	and another line of
   507	*/
   508	
   509	/* a line of stars
   510	 */
   511	
   512	/*	and another line of
   513	 */
   514	
   515	/*
   516	aligned in middle
   517	here
   518	        not here
   519	*/
   520	
   521	/*
   522	blank line in middle:
   523	
   524	with no leading spaces on blank line.
   525	*/
   526	
   527	/*
   528	   aligned in middle
   529	   here
   530	           not here
   531	*/
   532	
   533	/*
   534		blank line in middle:
   535	
   536		with no leading spaces on blank line.
   537	*/
   538	
   539	func _() {
   540		/*
   541		 * line
   542		 * of
   543		 * stars
   544		 */
   545	
   546		/*
   547		aligned in middle
   548		here
   549			not here
   550		*/
   551	
   552		/*
   553		blank line in middle:
   554	
   555		with no leading spaces on blank line.
   556	*/
   557	}
   558	
   559	
   560	// Some interesting interspersed comments.
   561	// See below for more common cases.
   562	func _(/* this */x/* is *//* an */ int) {
   563	}
   564	
   565	func _(/* no params - extra blank before and after comment */) {}
   566	func _(a, b int /* params - no extra blank after comment */) {}
   567	
   568	func _() { f(/* no args - extra blank before and after comment */) }
   569	func _() { f(a, b /* args - no extra blank after comment */) }
   570	
   571	func _() {
   572		f(/* no args - extra blank before and after comment */)
   573		f(a, b /* args - no extra blank after comment */)
   574	}
   575	
   576	func (/* comment1 */ T /* comment2 */) _() {}
   577	
   578	func _() { /* "short-ish one-line functions with comments are formatted as multi-line functions */ }
   579	func _() { x := 0; /* comment */ y = x /* comment */ }
   580	
   581	func _() {
   582		_ = 0
   583		/* closing curly brace should be on new line */ }
   584	
   585	func _() {
   586		_ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */}
   587	}
   588	
   589	// Test cases from issue 1542:
   590	// Comments must not be placed before commas and cause invalid programs.
   591	func _() {
   592		var a = []int{1, 2, /*jasldf*/
   593		}
   594		_ = a
   595	}
   596	
   597	func _() {
   598		var a = []int{1, 2, /*jasldf
   599							*/
   600		}
   601		_ = a
   602	}
   603	
   604	func _() {
   605		var a = []int{1, 2, // jasldf
   606		}
   607		_ = a
   608	}
   609	
   610	// Test cases from issues 11274, 15137:
   611	// Semicolon must not be lost when multiple statements are on the same line with a comment.
   612	func _() {
   613	    x := 0 /**/; y := 1
   614	}
   615	
   616	func _() {
   617		f(); f()
   618		f(); /* comment */ f()
   619		f() /* comment */; f()	
   620		f(); /* a */ /* b */ f()
   621		f() /* a */ /* b */; f()
   622		f() /* a */; /* b */ f()
   623	}
   624	
   625	func _() {
   626		f() /* a */ /* b */ }
   627	
   628	// Comments immediately adjacent to punctuation followed by a newline
   629	// remain after the punctuation (looks better and permits alignment of
   630	// comments).
   631	func _() {
   632		_ = T{
   633			1,    // comment after comma
   634			2,    /* comment after comma */
   635			3  ,  // comment after comma
   636		}
   637		_ = T{
   638			1  ,// comment after comma
   639			2  ,/* comment after comma */
   640			3,// comment after comma
   641		}
   642		_ = T{
   643			/* comment before literal */1,
   644			2/* comment before comma - ok to move after comma */,
   645			3  /* comment before comma - ok to move after comma */  ,
   646		}
   647	
   648		for
   649			i=0;// comment after semicolon
   650			i<9;/* comment after semicolon */
   651			i++{// comment after opening curly brace
   652		}
   653	
   654		// TODO(gri) the last comment in this example should be aligned */
   655		for
   656			i=0;// comment after semicolon
   657			i<9/* comment before semicolon - ok to move after semicolon */;
   658			i++ /* comment before opening curly brace */ {
   659		}
   660	}
   661	
   662	// If there is no newline following punctuation, commas move before the punctuation.
   663	// This way, commas interspersed in lists stay with the respective expression.
   664	func f(x/* comment */, y int, z int /* comment */, u, v, w int /* comment */) {
   665		f(x /* comment */, y)
   666		f(x /* comment */, 
   667		y)
   668		f(
   669			x /* comment */,
   670		)
   671	}
   672	
   673	func g(
   674		x int /* comment */,
   675	) {}
   676	
   677	type _ struct {
   678		a, b /* comment */, c int
   679	}
   680	
   681	type _ struct { a, b /* comment */, c int }
   682	
   683	func _() {
   684		for a /* comment */, b := range x {
   685		}
   686	}
   687	
   688	// Print line directives correctly.
   689	
   690	// The following is a legal line directive.
   691	//line foo:1
   692	func _() {
   693		_ = 0
   694	// The following is a legal line directive. It must not be indented:
   695	//line foo:2
   696		_ = 1
   697	
   698	// The following is not a legal line directive (it doesn't start in column 1):
   699		//line foo:2
   700		_ = 2
   701	
   702	// The following is not a legal line directive (negative line number):
   703	//line foo:-3
   704		_ = 3
   705	}
   706	
   707	// Line comments with tabs
   708	func _() {
   709	var	finput		*bufio.Reader			// input file
   710	var	stderr		*bufio.Writer
   711	var	ftable		*bufio.Writer			// y.go file
   712	var	foutput		*bufio.Writer			// y.output file
   713	
   714	var	oflag		string				// -o [y.go]		- y.go file
   715	var	vflag		string				// -v [y.output]	- y.output file
   716	var	lflag		bool				// -l			- disable line directives
   717	}
   718	
   719	// Trailing white space in comments should be trimmed
   720	func _() {
   721	// This comment has 4 blanks following that should be trimmed:    
   722	/* Each line of this comment has blanks or tabs following that should be trimmed:	
   723	   line 2:    
   724	   line 3:    			
   725	*/
   726	}
   727	
   728	/* This comment is the last entry in this file. It must be printed and should be followed by a newline */

View as plain text