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
5package imports
6
7import "io"
8
9import (
10	_ "io"
11)
12
13import _ "io"
14
15import (
16	"io"
17	"io"
18	"io"
19)
20
21import (
22	"io"
23	aLongRename "io"
24
25	b "io"
26)
27
28import (
29       "unrenamed"
30       renamed "renameMe"
31       . "io"
32       _ "io"
33       "io"
34       . "os"
35)
36
37// no newlines between consecutive single imports, but
38// respect extra line breaks in the source (at most one empty line)
39import _ "io"
40import _ "io"
41import _ "io"
42
43import _ "os"
44import _ "os"
45import _ "os"
46
47
48import _ "fmt"
49import _ "fmt"
50import _ "fmt"
51
52import "foo"  // a comment
53import "bar"  // a comment
54
55import (
56	_ "foo"
57	// a comment
58	"bar"
59	"foo"  // a comment
60	"bar"  // a comment
61)
62
63// comments + renames
64import (
65       "unrenamed" // a comment
66       renamed "renameMe"
67       . "io" /* a comment */
68       _ "io/ioutil" // a comment
69       "io" // testing alignment
70       . "os"
71       // a comment
72)
73
74// a case that caused problems in the past (comment placement)
75import (
76	. "fmt"
77	"io"
78	"malloc"	// for the malloc count test only
79	"math"
80	"strings"
81	"testing"
82)
83
84// more import examples
85import (
86	"xxx"
87	"much_longer_name" // comment
88	"short_name" // comment
89)
90
91import (
92	_ "xxx"
93	"much_longer_name" // comment
94)
95
96import (
97	mymath "math"
98	"/foo/bar/long_package_path" // a comment
99)
100
101import (
102	"package_a" // comment
103	"package_b"
104	my_better_c "package_c" // comment
105	"package_d" // comment
106	my_e "package_e" // comment
107
108	"package_a"    // comment
109	"package_bb"
110	"package_ccc"  // comment
111	"package_dddd" // comment
112)
113
114// print import paths as double-quoted strings
115// (we would like more test cases but the go/parser
116// already excludes most incorrect paths, and we don't
117// bother setting up test-ASTs manually)
118import (
119	`fmt`
120	"math"
121)
122
123// at least one empty line between declarations of different kind
124import _ "io"
125var _ int
126
127// at least one empty line between declarations of the same kind
128// if there is associated documentation (was issue 2570)
129type T1 struct{}
130// T2 comment
131type T2 struct {
132} // should be a two-line struct
133
134
135// T3 comment
136type T2 struct {
137
138
139} // should be a two-line struct
140
141
142// printing of constant literals
143const (
144	_ = "foobar"
145	_ = "a۰۱۸"
146	_ = "foo६४"
147	_ = "bar9876"
148	_ = 0
149	_ = 1
150	_ = 123456789012345678890
151	_ = 01234567
152	_ = 0xcafebabe
153	_ = 0.
154	_ = .0
155	_ = 3.14159265
156	_ = 1e0
157	_ = 1e+100
158	_ = 1e-100
159	_ = 2.71828e-1000
160	_ = 0i
161	_ = 1i
162	_ = 012345678901234567889i
163	_ = 123456789012345678890i
164	_ = 0.i
165	_ = .0i
166	_ = 3.14159265i
167	_ = 1e0i
168	_ = 1e+100i
169	_ = 1e-100i
170	_ = 2.71828e-1000i
171	_ = 'a'
172	_ = '\000'
173	_ = '\xFF'
174	_ = '\uff16'
175	_ = '\U0000ff16'
176	_ = `foobar`
177	_ = `foo
178---
179---
180bar`
181)
182
183
184func _() {
185	type _ int
186	type _ *int
187	type _ []int
188	type _ map[string]int
189	type _ chan int
190	type _ func() int
191
192	var _ int
193	var _ *int
194	var _ []int
195	var _ map[string]int
196	var _ chan int
197	var _ func() int
198
199	type _ struct{}
200	type _ *struct{}
201	type _ []struct{}
202	type _ map[string]struct{}
203	type _ chan struct{}
204	type _ func() struct{}
205
206	type _ interface{}
207	type _ *interface{}
208	type _ []interface{}
209	type _ map[string]interface{}
210	type _ chan interface{}
211	type _ func() interface{}
212
213	var _ struct{}
214	var _ *struct{}
215	var _ []struct{}
216	var _ map[string]struct{}
217	var _ chan struct{}
218	var _ func() struct{}
219
220	var _ interface{}
221	var _ *interface{}
222	var _ []interface{}
223	var _ map[string]interface{}
224	var _ chan interface{}
225	var _ func() interface{}
226}
227
228
229// don't lose blank lines in grouped declarations
230const (
231	_ int = 0
232	_ float = 1
233
234	_ string = "foo"
235
236	_ = iota
237	_
238
239	// a comment
240	_
241
242	_
243)
244
245
246type (
247	_ int
248	_ struct {}
249
250	_ interface{}
251
252	// a comment
253	_ map[string]int
254)
255
256
257var (
258	_ int = 0
259	_ float = 1
260
261	_ string = "foo"
262
263	_ bool
264
265	// a comment
266	_ bool
267)
268
269
270// don't lose blank lines in this struct
271type _ struct {
272	String struct {
273		Str, Len int
274	}
275	Slice struct {
276		Array, Len, Cap int
277	}
278	Eface struct {
279		Typ, Ptr int
280	}
281
282	UncommonType struct {
283		Name, PkgPath int
284	}
285	CommonType struct {
286		Size, Hash, Alg, Align, FieldAlign, String, UncommonType int
287	}
288	Type struct {
289		Typ, Ptr int
290	}
291	StructField struct {
292		Name, PkgPath, Typ, Tag, Offset int
293	}
294	StructType struct {
295		Fields int
296	}
297	PtrType struct {
298		Elem int
299	}
300	SliceType struct {
301		Elem int
302	}
303	ArrayType struct {
304		Elem, Len int
305	}
306
307	Stktop struct {
308		Stackguard, Stackbase, Gobuf int
309	}
310	Gobuf struct {
311		Sp, Pc, G int
312	}
313	G struct {
314		Stackbase, Sched, Status, Alllink int
315	}
316}
317
318
319// no blank lines in empty structs and interfaces, but leave 1- or 2-line layout alone
320type _ struct{            }
321type _ struct {
322
323}
324
325type _ interface{            }
326type _ interface {
327
328}
329
330
331// no tabs for single or ungrouped decls
332func _() {
333	const xxxxxx = 0
334	type x int
335	var xxx int
336	var yyyy float = 3.14
337	var zzzzz = "bar"
338
339	const (
340		xxxxxx = 0
341	)
342	type (
343		x int
344	)
345	var (
346		xxx int
347	)
348	var (
349		yyyy float = 3.14
350	)
351	var (
352		zzzzz = "bar"
353	)
354}
355
356// tabs for multiple or grouped decls
357func _() {
358	// no entry has a type
359	const (
360		zzzzzz = 1
361		z = 2
362		zzz = 3
363	)
364	// some entries have a type
365	const (
366		xxxxxx = 1
367		x = 2
368		xxx = 3
369		yyyyyyyy float = iota
370		yyyy = "bar"
371		yyy
372		yy = 2
373	)
374}
375
376func _() {
377	// no entry has a type
378	var (
379		zzzzzz = 1
380		z = 2
381		zzz = 3
382	)
383	// no entry has a value
384	var (
385		_ int
386		_ float
387		_ string
388
389		_ int  // comment
390		_ float  // comment
391		_ string  // comment
392	)
393	// some entries have a type
394	var (
395		xxxxxx int
396		x float
397		xxx string
398		yyyyyyyy int = 1234
399		y float = 3.14
400		yyyy = "bar"
401		yyy string = "foo"
402	)
403	// mixed entries - all comments should be aligned
404	var (
405		a, b, c int
406		x = 10
407		d int  // comment
408		y = 20  // comment
409		f, ff, fff, ffff int = 0, 1, 2, 3  // comment
410	)
411	// respect original line breaks
412	var _ = []T {
413		T{0x20,	"Telugu"},
414	}
415	var _ = []T {
416		// respect original line breaks
417		T{0x20,	"Telugu"},
418	}
419}
420
421// use the formatted output rather than the input to decide when to align
422// (was issue 4505)
423const (
424	short = 2 * (
425	1 + 2)
426	aMuchLongerName = 3
427)
428
429var (
430	short = X{
431	}
432	aMuchLongerName = X{}
433
434	x1 = X{} // foo
435	x2 = X{
436	} // foo
437)
438
439func _() {
440	type (
441		xxxxxx int
442		x float
443		xxx string
444		xxxxx []x
445		xx struct{}
446		xxxxxxx struct {
447			_, _ int
448			_ float
449		}
450		xxxx chan<- string
451	)
452}
453
454// alignment of "=" in consecutive lines (extended example from issue 1414)
455const (
456	umax uint                  = ^uint(0) // maximum value for a uint
457	bpu  = 1 << (5 + umax>>63)            // bits per uint
458	foo
459	bar  = -1
460)
461
462// typical enum
463const (
464	a MyType = iota
465	abcd
466	b
467	c
468	def
469)
470
471// excerpt from godoc.go
472var (
473	goroot = flag.String("goroot", runtime.GOROOT(), "Go root directory")
474	testDir = flag.String("testdir", "", "Go root subdirectory - for testing only (faster startups)")
475	pkgPath = flag.String("path", "", "additional package directories (colon-separated)")
476	filter = flag.String("filter", "", "filter file containing permitted package directory paths")
477	filterMin = flag.Int("filter_minutes", 0, "filter file update interval in minutes; disabled if <= 0")
478	filterDelay delayTime // actual filter update interval in minutes; usually filterDelay == filterMin, but filterDelay may back off exponentially
479)
480
481
482// formatting of structs
483type _ struct{}
484
485type _ struct{ /* this comment should be visible */ }
486
487type _ struct{
488	// this comment should be visible and properly indented
489}
490
491type _ struct {  // this comment must not change indentation
492	f int
493	f, ff, fff, ffff int
494}
495
496type _ struct {
497	string
498}
499
500type _ struct {
501	string  // comment
502}
503
504type _ struct {
505	string "tag"
506}
507
508type _ struct {
509	string "tag"  // comment
510}
511
512type _ struct {
513	f int
514}
515
516type _ struct {
517	f int  // comment
518}
519
520type _ struct {
521	f int "tag"
522}
523
524type _ struct {
525	f int "tag"  // comment
526}
527
528type _ struct {
529	bool
530	a, b, c int
531	int "tag"
532	ES // comment
533	float "tag"  // comment
534	f int  // comment
535	f, ff, fff, ffff int  // comment
536	g float "tag"
537	h float "tag"  // comment
538}
539
540type _ struct { a, b,
541c, d int  // this line should be indented
542u, v, w, x float // this line should be indented
543p, q,
544r, s float // this line should be indented
545}
546
547
548// difficult cases
549type _ struct {
550	bool  // comment
551	text []byte  // comment
552}
553
554
555// formatting of interfaces
556type EI interface{}
557
558type _ interface {
559	EI
560}
561
562type _ interface {
563	f()
564	fffff()
565}
566
567type _ interface {
568	EI
569	f()
570	fffffg()
571}
572
573type _ interface {  // this comment must not change indentation
574	EI  // here's a comment
575	f()  // no blank between identifier and ()
576	fffff()  // no blank between identifier and ()
577	gggggggggggg(x, y, z int) ()  // hurray
578}
579
580
581// formatting of variable declarations
582func _() {
583	type day struct { n int; short, long string }
584	var (
585		Sunday = day{ 0, "SUN", "Sunday" }
586		Monday = day{ 1, "MON", "Monday" }
587		Tuesday = day{ 2, "TUE", "Tuesday" }
588		Wednesday = day{ 3, "WED", "Wednesday" }
589		Thursday = day{ 4, "THU", "Thursday" }
590		Friday = day{ 5, "FRI", "Friday" }
591		Saturday = day{ 6, "SAT", "Saturday" }
592	)
593}
594
595
596// formatting of multi-line variable declarations
597var a1, b1, c1 int  // all on one line
598
599var a2, b2,
600c2 int  // this line should be indented
601
602var (a3, b3,
603c3, d3 int  // this line should be indented
604a4, b4, c4 int  // this line should be indented
605)
606
607// Test case from issue 3304: multi-line declarations must end
608// a formatting section and not influence indentation of the
609// next line.
610var (
611	minRefreshTimeSec = flag.Int64("min_refresh_time_sec", 604800,
612		"minimum time window between two refreshes for a given user.")
613	x = flag.Int64("refresh_user_rollout_percent", 100,
614		"temporary flag to ramp up the refresh user rpc")
615	aVeryLongVariableName = stats.GetVarInt("refresh-user-count")
616)
617
618func _() {
619	var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
620					Headers: map[string]string{},
621					Bytes: []uint8{0x30, 0x82, 0x1, 0x3a, 0x2, 0x1, 0x0, 0x2,
622			0x41, 0x0, 0xb2, 0x99, 0xf, 0x49, 0xc4, 0x7d, 0xfa, 0x8c,
623			0xd4, 0x0, 0xae, 0x6a, 0x4d, 0x1b, 0x8a, 0x3b, 0x6a, 0x13,
624			0x64, 0x2b, 0x23, 0xf2, 0x8b, 0x0, 0x3b, 0xfb, 0x97, 0x79,
625		},
626	}
627}
628
629
630func _() {
631	var Universe = Scope {
632		Names: map[string]*Ident {
633			// basic types
634			"bool": nil,
635			"byte": nil,
636			"int8": nil,
637			"int16": nil,
638			"int32": nil,
639			"int64": nil,
640			"uint8": nil,
641			"uint16": nil,
642			"uint32": nil,
643			"uint64": nil,
644			"float32": nil,
645			"float64": nil,
646			"string": nil,
647
648			// convenience types
649			"int": nil,
650			"uint": nil,
651			"uintptr": nil,
652			"float": nil,
653
654			// constants
655			"false": nil,
656			"true": nil,
657			"iota": nil,
658			"nil": nil,
659
660			// functions
661			"cap": nil,
662			"len": nil,
663			"new": nil,
664			"make": nil,
665			"panic": nil,
666			"panicln": nil,
667			"print": nil,
668			"println": nil,
669		},
670	}
671}
672
673
674// alignment of map composite entries
675var _ = map[int]int{
676	// small key sizes: always align even if size ratios are large
677	a: a,
678	abcdefghabcdefgh: a,
679	ab: a,
680	abc: a,
681	abcdefgabcdefg: a,
682	abcd: a,
683	abcde: a,
684	abcdef: a,
685
686	// mixed key sizes: align when key sizes change within accepted ratio
687	abcdefgh: a,
688	abcdefghabcdefg: a,
689	abcdefghij: a,
690	abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij: a, // outlier - do not align with previous line
691	abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij: a, // align with previous line
692
693	ab: a, // do not align with previous line
694	abcde: a, // align with previous line
695}
696
697// alignment of map composite entries: test cases from issue 3965
698// aligned
699var _ = T1{
700	a:                    x,
701	b:                    y,
702	cccccccccccccccccccc: z,
703}
704
705// not aligned
706var _ = T2{
707	a: x,
708	b: y,
709	ccccccccccccccccccccc: z,
710}
711
712// aligned
713var _ = T3{
714	aaaaaaaaaaaaaaaaaaaa: x,
715	b:                    y,
716	c:                    z,
717}
718
719// not aligned
720var _ = T4{
721	aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: x,
722	b:                                       y,
723	c:                                       z,
724}
725
726
727// no alignment of map composite entries if they are not the first entry on a line
728var _ = T{0: 0} // not aligned
729var _ = T{0: 0, // not aligned
730	1: 1, // aligned
731	22: 22, // aligned
732	333: 333, 1234: 12, 12345: 0, // first on line aligned
733}
734
735
736// test cases form issue 8685
737// not aligned
738var _ = map[int]string{1: "spring", 2: "summer",
739					3:             "autumn", 4: "winter"}
740
741// not aligned
742var _ = map[string]string{"a": "spring", "b": "summer",
743	"c": "autumn", "d": "winter"}
744
745// aligned
746var _ = map[string]string{"a": "spring",
747"b": "summer",
748	"c": "autumn",
749"d": "winter"}
750
751
752func _() {
753	var _ = T{
754		a,	// must introduce trailing comma
755	}
756}
757
758
759// formatting of function results
760func _() func() {}
761func _() func(int) { return nil }
762func _() func(int) int { return nil }
763func _() func(int) func(int) func() { return nil }
764
765
766// formatting of consecutive single-line functions
767func _() {}
768func _() {}
769func _() {}
770
771func _() {}  // an empty line before this function
772func _() {}
773func _() {}
774
775func _() { f(1, 2, 3) }
776func _(x int) int { y := x; return y+1 }
777func _() int { type T struct{}; var x T; return x }
778
779// these must remain multi-line since they are multi-line in the source
780func _() {
781	f(1, 2, 3)
782}
783func _(x int) int {
784	y := x; return y+1
785}
786func _() int {
787	type T struct{}; var x T; return x
788}
789
790
791// making function declarations safe for new semicolon rules
792func _() { /* single-line function because of "short-ish" comment */ }
793func _() { /* multi-line function because of "long-ish" comment - much more comment text is following here */ /* and more */ }
794
795func _() {
796/* multi-line func because block is on multiple lines */ }
797
798// test case for issue #19544
799func _() {}
800func _longer_name_() { // this comment must not force the {} from above to alignment
801	// multiple lines
802}
803
804// ellipsis parameters
805func _(...int)
806func _(...*int)
807func _(...[]int)
808func _(...struct{})
809func _(bool, ...interface{})
810func _(bool, ...func())
811func _(bool, ...func(...int))
812func _(bool, ...map[string]int)
813func _(bool, ...chan int)
814
815func _(b bool, x ...int)
816func _(b bool, x ...*int)
817func _(b bool, x ...[]int)
818func _(b bool, x ...struct{})
819func _(x ...interface{})
820func _(x ...func())
821func _(x ...func(...int))
822func _(x ...map[string]int)
823func _(x ...chan int)
824
825
826// these parameter lists must remain multi-line since they are multi-line in the source
827func _(bool,
828int) {
829}
830func _(x bool,
831y int) {
832}
833func _(x,
834y bool) {
835}
836func _(bool, // comment
837int) {
838}
839func _(x bool, // comment
840y int) {
841}
842func _(x, // comment
843y bool) {
844}
845func _(bool, // comment
846// comment
847int) {
848}
849func _(x bool, // comment
850// comment
851y int) {
852}
853func _(x, // comment
854// comment
855y bool) {
856}
857func _(bool,
858// comment
859int) {
860}
861func _(x bool,
862// comment
863y int) {
864}
865func _(x,
866// comment
867y bool) {
868}
869func _(x, // comment
870y,// comment
871z bool) {
872}
873func _(x, // comment
874	y,// comment
875	z bool) {
876}
877func _(x int,	// comment
878	y float,	// comment
879	z bool) {
880}
881
882
883// properly indent multi-line signatures
884func ManageStatus(in <-chan *Status, req <-chan Request,
885stat chan<- *TargetInfo,
886TargetHistorySize int) {
887}
888
889func MultiLineSignature0(
890a, b, c int,
891) {}
892
893func MultiLineSignature1(
894a, b, c int,
895u, v, w float,
896) {}
897
898func MultiLineSignature2(
899a, b,
900c int,
901) {}
902
903func MultiLineSignature3(
904a, b,
905c int, u, v,
906w float,
907		x ...int) {}
908
909func MultiLineSignature4(
910a, b, c int,
911u, v,
912w float,
913		x ...int) {}
914
915func MultiLineSignature5(
916a, b, c int,
917u, v, w float,
918p, q,
919r string,
920		x ...int) {}
921
922// make sure it also works for methods in interfaces
923type _ interface {
924MultiLineSignature0(
925a, b, c int,
926)
927
928MultiLineSignature1(
929a, b, c int,
930u, v, w float,
931)
932
933MultiLineSignature2(
934a, b,
935c int,
936)
937
938MultiLineSignature3(
939a, b,
940c int, u, v,
941w float,
942		x ...int)
943
944MultiLineSignature4(
945a, b, c int,
946u, v,
947w float,
948		x ...int)
949
950MultiLineSignature5(
951a, b, c int,
952u, v, w float,
953p, q,
954r string,
955		x ...int)
956}
957
958// omit superfluous parentheses in parameter lists
959func _((int))
960func _((((((int))))))
961func _(x (int))
962func _(x (((((int))))))
963func _(x, y (int))
964func _(x, y (((((int))))))
965
966func _() (int)
967func _() ((int))
968func _() ((((((int))))))
969
970func _() (x int)
971func _() (x (int))
972func _() (x (((((int))))))
973
974// special cases: some channel types require parentheses
975func _(x chan(<-chan int))
976func _(x (chan(<-chan int)))
977func _(x ((((chan(<-chan int))))))
978
979func _(x chan<-(chan int))
980func _(x (chan<-(chan int)))
981func _(x ((((chan<-(chan int))))))
982
983// don't introduce comma after last parameter if the closing ) is on the same line
984// even if the parameter type itself is multi-line (test cases from issue 4533)
985func _(...interface{})
986func _(...interface {
987	m()
988	n()
989}) // no extra comma between } and )
990
991func (t *T) _(...interface{})
992func (t *T) _(...interface {
993	m()
994	n()
995}) // no extra comma between } and )
996
997func _(interface{})
998func _(interface {
999	m()
1000}) // no extra comma between } and )
1001
1002func _(struct{})
1003func _(struct {
1004	x int
1005	y int
1006}) // no extra comma between } and )
1007
1008// alias declarations
1009
1010type c0 struct{}
1011type c1 = C
1012type c2 = struct{ x int}
1013type c3 = p.C
1014type (
1015	s struct{}
1016	a = A
1017	b = A
1018	c = foo
1019	d = interface{}
1020	ddd = p.Foo
1021)
1022