}
+func (p *printer) moveCommentsAfter(pos token.Position) {
+ // TODO(gri): Make sure a comment doesn't accidentally introduce
+ // a newline and thus cause a semicolon to be inserted.
+ // Remove this after transitioning to new semicolon
+ // syntax and some reasonable grace period (12/11/09).
+ if p.commentBefore(pos) {
+ p.comment.List[0].Position = pos
+ }
+}
+
+
// block prints an *ast.BlockStmt; it always spans at least two lines.
func (p *printer) block(s *ast.BlockStmt, indent int) {
+ p.moveCommentsAfter(s.Pos());
p.print(s.Pos(), token.LBRACE);
p.stmtList(s.List, indent);
p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true);
p.expr(s.Name, multiLine);
p.print(blank);
}
+ p.moveCommentsAfter(s.Path[0].Pos());
p.expr(&ast.StringList{s.Path}, multiLine);
comment = s.Comment;
case *ast.ValueSpec:
p.leadComment(s.Doc);
p.identList(s.Names, multiLine); // always present
+ if s.Values != nil {
+ p.moveCommentsAfter(s.Values[0].Pos())
+ } else if s.Type != nil {
+ p.moveCommentsAfter(s.Type.Pos())
+ }
if n == 1 {
if s.Type != nil {
p.print(blank);
case *ast.TypeSpec:
p.leadComment(s.Doc);
p.expr(s.Name, multiLine);
+ p.moveCommentsAfter(s.Type.Pos());
if n == 1 {
p.print(blank)
} else {
import (
"bytes";
+ oldParser "exp/parser";
"flag";
"io/ioutil";
"go/ast";
"go/parser";
+ "os";
"path";
"testing";
)
const (
export checkMode = 1 << iota;
rawFormat;
+ oldSyntax;
)
func check(t *testing.T, source, golden string, mode checkMode) {
// parse source
- prog, err := parser.ParseFile(source, nil, parser.ParseComments);
+ var prog *ast.File;
+ var err os.Error;
+ if mode&oldSyntax != 0 {
+ prog, err = oldParser.ParseFile(source, nil, parser.ParseComments)
+ } else {
+ prog, err = parser.ParseFile(source, nil, parser.ParseComments)
+ }
if err != nil {
t.Error(err);
return;
for _, e := range data {
source := path.Join(dataDir, e.source);
golden := path.Join(dataDir, e.golden);
- check(t, source, golden, e.mode);
+ check(t, source, golden, e.mode|oldSyntax);
// TODO(gri) check that golden is idempotent
//check(t, golden, golden, e.mode);
}
aLongRename "io";
b "io";
- c "i" "o";
)
// no newlines between consecutive single imports, but
import _ "fmt"
import _ "fmt"
+// make sure a comment doesn't cause semicolons to be inserted
+import _ "foo" // a comment
+import "bar" // a comment
+
// at least one empty line between declarations of different kind
import _ "io"
}
+func _() {
+ var _ = T{
+ a, // must introduce trailing comma
+ };
+}
+
+
// formatting of consecutive single-line functions
func _() {}
func _() {}
func _() { f(1, 2, 3) }
func _(x int) int { return x + 1 }
func _() int { type T struct{} }
+
+
+// making function declarations safe for new semicolon rules
+func _() { /* one-line func */ }
+
+func _() { // opening "{" must move up /* one-line func */ }
+
+func _() { // opening "{" must move up// multi-line func
+
+ // in the following declarations, a comment must not
+ // introduce a newline and thus cause a semicolon to
+ // be inserted
+ const _ T = x // comment
+ ;
+ const _ = x // comment
+ ;
+
+ type _ T // comment
+ ;
+ type _ struct // comment
+ {
+
+ }
+ type _ interface // comment
+ {
+
+ }
+ type _ * // comment
+ T;
+ type _ [ // comment
+ ]T;
+ type _ [ // comment
+ 10]T;
+ type _ chan // comment
+ T;
+ type _ map // comment
+ [T]T;
+
+ var _ T // comment
+ ;
+ var _ T = x // comment
+ ;
+ var _ struct // comment
+ {
+
+ }
+ var _ interface // comment
+ {
+
+ }
+ var _ * // comment
+ T;
+ var _ [ // comment
+ ]T;
+ var _ [ // comment
+ 10]T;
+ var _ chan // comment
+ T;
+ var _ map // comment
+ [T]T;
+
+ var _ = x // comment
+ ;
+}
aLongRename "io";
b "io";
- c "i" "o";
)
// no newlines between consecutive single imports, but
import _ "fmt"
import _ "fmt"
+// make sure a comment doesn't cause semicolons to be inserted
+import _ // a comment
+ "foo"
+import // a comment
+ "bar"
+
// at least one empty line between declarations of different kind
import _ "io"
)
// respect original line breaks
var _ = []T {
- T{0x20, "Telugu"}
+ T{0x20, "Telugu"},
};
var _ = []T {
// respect original line breaks
- T{0x20, "Telugu"}
+ T{0x20, "Telugu"},
};
}
"panicln": nil,
"print": nil,
"println": nil,
- }
+ },
+ }
+}
+
+
+func _() {
+ var _ = T{
+ a // must introduce trailing comma
}
}
func _() int {
type T struct{}
}
+
+
+// making function declarations safe for new semicolon rules
+func _()
+{ /* one-line func */ }
+
+func _() // opening "{" must move up
+{ /* one-line func */ }
+
+func _() // opening "{" must move up
+// multi-line func
+{
+ // in the following declarations, a comment must not
+ // introduce a newline and thus cause a semicolon to
+ // be inserted
+ const _ // comment
+ T = x;
+ const _ // comment
+ = x;
+
+ type _ // comment
+ T;
+ type _ // comment
+ struct {};
+ type _ // comment
+ interface {};
+ type _ // comment
+ *T;
+ type _ // comment
+ []T;
+ type _ // comment
+ [10]T;
+ type _ // comment
+ chan T;
+ type _ // comment
+ map[T]T;
+
+ var _ // comment
+ T;
+ var _ // comment
+ T = x;
+ var _ // comment
+ struct {};
+ var _ // comment
+ interface {};
+ var _ // comment
+ *T;
+ var _ // comment
+ []T;
+ var _ // comment
+ [10]T;
+ var _ // comment
+ chan T;
+ var _ // comment
+ map[T]T;
+
+ var _ // comment
+ = x;
+}
func _() {
// not not add extra indentation to multi-line string lists
- _ = "foo" "bar";
- _ = "foo"
- "bar"
+ _ = "foo" + "bar";
+ _ = "foo" +
+ "bar" +
"bah";
_ = []string{
- "abc"
+ "abc" +
"def",
- "foo"
+ "foo" +
"bar",
};
}
const _ = F1 +
- `string = "%s";`
- `ptr = *;`
- `datafmt.T2 = s ["-" p "-"];`
+ `string = "%s";` +
+ `ptr = *;` +
+ `datafmt.T2 = s ["-" p "-"];`
-const _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+const _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`
-const _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+const _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`
func _() {
_ = F1 +
- `string = "%s";`
- `ptr = *;`
- `datafmt.T2 = s ["-" p "-"];`;
+ `string = "%s";` +
+ `ptr = *;` +
+ `datafmt.T2 = s ["-" p "-"];`;
_ =
- `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`;
- _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`;
}
c;
_ = a < b ||
b < a;
- _ = "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ _ = "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime
}
const (
_ = "991";
_ = "2432902008176640000"; // 20!
- _ = "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ _ = "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime
)
func _() {
// not not add extra indentation to multi-line string lists
- _ = "foo" "bar";
- _ = "foo"
- "bar"
+ _ = "foo" + "bar";
+ _ = "foo" +
+ "bar" +
"bah";
_ = []string {
- "abc"
+ "abc" +
"def",
- "foo"
- "bar"
+ "foo" +
+ "bar",
}
}
const _ = F1 +
- `string = "%s";`
- `ptr = *;`
+ `string = "%s";` +
+ `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];`
const _ =
- `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`
-const _ = `datafmt "datafmt";`
-`default = "%v";`
-`array = *;`
+const _ = `datafmt "datafmt";` +
+`default = "%v";` +
+`array = *;` +
`datafmt.T3 = s {" " a a / ","};`
func _() {
_ = F1 +
- `string = "%s";`
- `ptr = *;`
+ `string = "%s";` +
+ `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];`;
_ =
- `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`;
- _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`
}
c;
_ = a < b ||
b < a;
- _ = "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ _ = "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime
}
const (
_ = "991";
_ = "2432902008176640000"; // 20!
- _ = "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ _ = "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime
)
func same(t, u *Time) bool {
// respect source lines in multi-line expressions
- return t.Year == u.Year
- && t.Month == u.Month
- && t.Day == u.Day
- && t.Hour == u.Hour
- && t.Minute == u.Minute
- && t.Second == u.Second
- && t.Weekday == u.Weekday
- && t.ZoneOffset == u.ZoneOffset
- && t.Zone == u.Zone
+ return t.Year == u.Year &&
+ t.Month == u.Month &&
+ t.Day == u.Day &&
+ t.Hour == u.Hour &&
+ t.Minute == u.Minute &&
+ t.Second == u.Second &&
+ t.Weekday == u.Weekday &&
+ t.ZoneOffset == u.ZoneOffset &&
+ t.Zone == u.Zone
}
func _() {
// not not add extra indentation to multi-line string lists
- _ = "foo" "bar";
- _ = "foo"
- "bar"
+ _ = "foo" + "bar";
+ _ = "foo" +
+ "bar" +
"bah";
_ = []string{
- "abc"
+ "abc" +
"def",
- "foo"
+ "foo" +
"bar",
};
}
const _ = F1 +
- `string = "%s";`
- `ptr = *;`
- `datafmt.T2 = s ["-" p "-"];`
+ `string = "%s";` +
+ `ptr = *;` +
+ `datafmt.T2 = s ["-" p "-"];`
-const _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+const _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`
-const _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+const _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`
func _() {
_ = F1 +
- `string = "%s";`
- `ptr = *;`
- `datafmt.T2 = s ["-" p "-"];`;
+ `string = "%s";` +
+ `ptr = *;` +
+ `datafmt.T2 = s ["-" p "-"];`;
_ =
- `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`;
- _ = `datafmt "datafmt";`
- `default = "%v";`
- `array = *;`
+ _ = `datafmt "datafmt";` +
+ `default = "%v";` +
+ `array = *;` +
`datafmt.T3 = s {" " a a / ","};`;
}
c;
_ = a < b ||
b < a;
- _ = "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ _ = "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime
}
const (
_ = "991";
_ = "2432902008176640000"; // 20!
- _ = "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ _ = "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime
)
2: "2",
10: "3628800",
20: "2432902008176640000",
- 100: "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ 100: "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000",
}
func usage() {
fmt.Fprintf(os.Stderr,
// TODO(gri): the 2nd string of this string list should not be indented
- "usage: godoc package [name ...]\n"
+ "usage: godoc package [name ...]\n"+
" godoc -http=:6060\n");
flag.PrintDefaults();
os.Exit(2);
},
contents: "Google.com\n",
},
- }
+ },
},
// The truncated test file was produced using these commands:
// dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt
2: "2",
10: "3628800",
20: "2432902008176640000",
- 100: "933262154439441526816992388562667004907159682643816214685929"
- "638952175999932299156089414639761565182862536979208272237582"
+ 100: "933262154439441526816992388562667004907159682643816214685929" +
+ "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000",
}
func usage() {
fmt.Fprintf(os.Stderr,
// TODO(gri): the 2nd string of this string list should not be indented
- "usage: godoc package [name ...]\n"
- " godoc -http=:6060\n"
- );
+ "usage: godoc package [name ...]\n" +
+ " godoc -http=:6060\n");
flag.PrintDefaults();
os.Exit(2);
}