summaryrefslogtreecommitdiff
path: root/src/pkg/go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-11-03 13:40:11 -0800
committerRobert Griesemer <gri@golang.org>2009-11-03 13:40:11 -0800
commitb591b9b5fd617b7027293bd16af74d54ec8268c4 (patch)
treea3b0fea36ba621dc05b889f41daab71cb33ae3fc /src/pkg/go
parent53f43c11589d80c2a50a4cad053f271078965c61 (diff)
downloadgolang-b591b9b5fd617b7027293bd16af74d54ec8268c4.tar.gz
- don't loose extra line breaks in struct/interface declarations
- start new sections if a field/method declaration spans multiple lines; this avoids tabs from the previous line affecting the next field/method R=rsc http://go/go-review/1017015
Diffstat (limited to 'src/pkg/go')
-rw-r--r--src/pkg/go/ast/ast.go8
-rw-r--r--src/pkg/go/printer/nodes.go28
-rw-r--r--src/pkg/go/printer/testdata/declarations.go47
-rw-r--r--src/pkg/go/printer/testdata/declarations.golden49
4 files changed, 121 insertions, 11 deletions
diff --git a/src/pkg/go/ast/ast.go b/src/pkg/go/ast/ast.go
index c346c48b3..4b90f3b74 100644
--- a/src/pkg/go/ast/ast.go
+++ b/src/pkg/go/ast/ast.go
@@ -95,6 +95,14 @@ type Field struct {
}
+func (f *Field) Pos() token.Position {
+ if len(f.Names) > 0 {
+ return f.Names[0].Pos();
+ }
+ return f.Type.Pos();
+}
+
+
// An expression is represented by a tree consisting of one
// or more of the following concrete expression nodes.
//
diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go
index 10f554e40..20bd18b7e 100644
--- a/src/pkg/go/printer/nodes.go
+++ b/src/pkg/go/printer/nodes.go
@@ -256,15 +256,16 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
sep = blank;
}
for i, f := range list {
+ var ml bool;
extraTabs := 0;
p.leadComment(f.Doc);
if len(f.Names) > 0 {
- p.identList(f.Names, ignoreMultiLine);
+ p.identList(f.Names, &ml);
p.print(sep);
- p.expr(f.Type, ignoreMultiLine);
+ p.expr(f.Type, &ml);
extraTabs = 1;
} else {
- p.expr(f.Type, ignoreMultiLine);
+ p.expr(f.Type, &ml);
extraTabs = 2;
}
if f.Tag != nil {
@@ -272,7 +273,7 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
p.print(sep);
}
p.print(sep);
- p.expr(&ast.StringList{f.Tag}, ignoreMultiLine);
+ p.expr(&ast.StringList{f.Tag}, &ml);
extraTabs = 0;
}
p.print(token.SEMICOLON);
@@ -282,8 +283,10 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
}
p.lineComment(f.Comment);
}
- if i+1 < len(list) || isIncomplete {
- p.print(newline);
+ if i+1 < len(list) {
+ p.linebreak(list[i+1].Pos().Line, 1, 2, ignore, ml);
+ } else if isIncomplete {
+ p.print(formfeed);
}
}
if isIncomplete {
@@ -294,19 +297,22 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
} else { // interface
for i, f := range list {
+ var ml bool;
p.leadComment(f.Doc);
if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
// method
- p.expr(f.Names[0], ignoreMultiLine); // exactly one name
- p.signature(ftyp.Params, ftyp.Results, ignoreMultiLine);
+ p.expr(f.Names[0], &ml);
+ p.signature(ftyp.Params, ftyp.Results, &ml);
} else {
// embedded interface
- p.expr(f.Type, ignoreMultiLine);
+ p.expr(f.Type, &ml);
}
p.print(token.SEMICOLON);
p.lineComment(f.Comment);
- if i+1 < len(list) || isIncomplete {
- p.print(newline);
+ if i+1 < len(list) {
+ p.linebreak(list[i+1].Pos().Line, 1, 2, ignore, ml);
+ } else if isIncomplete {
+ p.print(formfeed);
}
}
if isIncomplete {
diff --git a/src/pkg/go/printer/testdata/declarations.go b/src/pkg/go/printer/testdata/declarations.go
index 3e926664f..a697ef736 100644
--- a/src/pkg/go/printer/testdata/declarations.go
+++ b/src/pkg/go/printer/testdata/declarations.go
@@ -93,6 +93,53 @@ func _() {
}
+// don't lose blank lines in this struct
+type _ struct {
+ String struct {
+ Str, Len int;
+ };
+ Slice struct {
+ Array, Len, Cap int;
+ };
+ Eface struct {
+ Typ, Ptr int;
+ };
+
+ UncommonType struct {
+ Name, PkgPath int;
+ };
+ CommonType struct {
+ Size, Hash, Alg, Align, FieldAlign, String, UncommonType int;
+ };
+ Type struct {
+ Typ, Ptr int;
+ };
+ StructField struct {
+ Name, PkgPath, Typ, Tag, Offset int;
+ };
+ StructType struct {
+ Fields int;
+ };
+ PtrType struct {
+ Elem int;
+ };
+ SliceType struct {
+ Elem int;
+ };
+ ArrayType struct {
+ Elem, Len int;
+ };
+
+ Stktop struct {
+ Stackguard, Stackbase, Gobuf int;
+ };
+ Gobuf struct {
+ Sp, Pc, G int;
+ };
+ G struct {
+ Stackbase, Sched, Status, Alllink int;
+ };
+}
// no tabs for single or ungrouped decls
diff --git a/src/pkg/go/printer/testdata/declarations.golden b/src/pkg/go/printer/testdata/declarations.golden
index 3a8fa8546..e238b4581 100644
--- a/src/pkg/go/printer/testdata/declarations.golden
+++ b/src/pkg/go/printer/testdata/declarations.golden
@@ -94,6 +94,55 @@ func _() {
}
+// don't lose blank lines in this struct
+type _ struct {
+ String struct {
+ Str, Len int;
+ };
+ Slice struct {
+ Array, Len, Cap int;
+ };
+ Eface struct {
+ Typ, Ptr int;
+ };
+
+ UncommonType struct {
+ Name, PkgPath int;
+ };
+ CommonType struct {
+ Size, Hash, Alg, Align, FieldAlign, String, UncommonType int;
+ };
+ Type struct {
+ Typ, Ptr int;
+ };
+ StructField struct {
+ Name, PkgPath, Typ, Tag, Offset int;
+ };
+ StructType struct {
+ Fields int;
+ };
+ PtrType struct {
+ Elem int;
+ };
+ SliceType struct {
+ Elem int;
+ };
+ ArrayType struct {
+ Elem, Len int;
+ };
+
+ Stktop struct {
+ Stackguard, Stackbase, Gobuf int;
+ };
+ Gobuf struct {
+ Sp, Pc, G int;
+ };
+ G struct {
+ Stackbase, Sched, Status, Alllink int;
+ };
+}
+
+
// no tabs for single or ungrouped decls
func _() {
const xxxxxx = 0;