]> Cypherpunks repositories - gostls13.git/commitdiff
- respect source line breaks in grouped declarations
authorRobert Griesemer <gri@golang.org>
Wed, 4 Nov 2009 18:58:54 +0000 (10:58 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 4 Nov 2009 18:58:54 +0000 (10:58 -0800)
- made ast.Spec nodes implement Node interface
- added extra test cases

R=rsc
http://go/go-review/1016038

src/pkg/go/ast/ast.go
src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/declarations.go
src/pkg/go/printer/testdata/declarations.golden

index 4b90f3b74e3603af3a8228c1d453086ac6e77bd0..efe2af459e9aa0e9d82a0bbd91c73f19b800ca7a 100644 (file)
@@ -596,6 +596,7 @@ func (s *RangeStmt) stmtNode() {}
 type (
        // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
        Spec    interface {
+               Node;
                specNode();
        };
 
@@ -627,6 +628,23 @@ type (
 )
 
 
+// Pos() implementations for spec nodes.
+//
+func (s *ImportSpec) Pos() token.Position {
+       if s.Name != nil {
+               return s.Name.Pos();
+       }
+       return s.Path[0].Pos();
+}
+
+func (s *ValueSpec) Pos() token.Position {
+       return s.Names[0].Pos();
+}
+func (s *TypeSpec) Pos() token.Position {
+       return s.Name.Pos();
+}
+
+
 // specNode() ensures that only spec nodes can be
 // assigned to a Spec.
 //
index 20bd18b7e3183c370319bb2acad7504d77212971..2aa0a5fbab1b6d3e3f77739db0021521dd51544d 100644 (file)
@@ -255,8 +255,12 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
                if len(list) == 1 {
                        sep = blank;
                }
+               var ml bool;
                for i, f := range list {
-                       var ml bool;
+                       if i > 0 {
+                               p.linebreak(f.Pos().Line, 1, 2, ignore, ml);
+                       }
+                       ml = false;
                        extraTabs := 0;
                        p.leadComment(f.Doc);
                        if len(f.Names) > 0 {
@@ -283,21 +287,23 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
                                }
                                p.lineComment(f.Comment);
                        }
-                       if i+1 < len(list) {
-                               p.linebreak(list[i+1].Pos().Line, 1, 2, ignore, ml);
-                       } else if isIncomplete {
-                               p.print(formfeed);
-                       }
                }
                if isIncomplete {
+                       if len(list) > 0 {
+                               p.print(formfeed);
+                       }
                        // TODO(gri): this needs to be styled like normal comments
                        p.print("// contains unexported fields");
                }
 
        } else { // interface
 
+               var ml bool;
                for i, f := range list {
-                       var ml bool;
+                       if i > 0 {
+                               p.linebreak(f.Pos().Line, 1, 2, ignore, ml);
+                       }
+                       ml = false;
                        p.leadComment(f.Doc);
                        if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
                                // method
@@ -309,13 +315,11 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
                        }
                        p.print(token.SEMICOLON);
                        p.lineComment(f.Comment);
-                       if i+1 < len(list) {
-                               p.linebreak(list[i+1].Pos().Line, 1, 2, ignore, ml);
-                       } else if isIncomplete {
-                               p.print(formfeed);
-                       }
                }
                if isIncomplete {
+                       if len(list) > 0 {
+                               p.print(formfeed);
+                       }
                        // TODO(gri): this needs to be styled like normal comments
                        p.print("// contains unexported methods");
                }
@@ -941,11 +945,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
                        var ml bool;
                        for i, s := range d.Specs {
                                if i > 0 {
-                                       if ml {
-                                               p.print(formfeed);
-                                       } else {
-                                               p.print(newline);
-                                       }
+                                       p.linebreak(s.Pos().Line, 1, 2, ignore, ml);
                                }
                                ml = false;
                                p.spec(s, len(d.Specs), inGroup, &ml);
index a697ef736e3491932c07e774385d58aae80b034e..c54a2ce22251828092ad4c6727b874e06b4708ca 100644 (file)
@@ -21,6 +21,7 @@ import (
 import (
        "io";
        aLongRename "io";
+
        b "io";
        c "i" "o";
 )
@@ -93,6 +94,47 @@ func _() {
 }
 
 
+// don't lose blank lines in grouped declarations
+const (
+       _ int = 0;
+       _ float = 1;
+
+       _ string = "foo";
+
+       _ = iota;
+       _;
+       
+       // a comment
+       _;
+
+       _;
+)
+
+
+type (
+       _ int;
+       _ struct {};
+       
+       _ interface{};
+       
+       // a comment
+       _ map[string]int;
+)
+
+
+var (
+       _ int = 0;
+       _ float = 1;
+
+       _ string = "foo";
+
+       _ bool;
+       
+       // a comment
+       _ bool;
+)
+
+
 // don't lose blank lines in this struct
 type _ struct {
        String struct {
index e238b4581cbf2e7449d21d074d7b036f4cb680e0..2adf7b5fe4ac73c657e49ff848d86eaa65be350f 100644 (file)
@@ -21,6 +21,7 @@ import (
 import (
        "io";
        aLongRename "io";
+
        b "io";
        c "i" "o";
 )
@@ -94,6 +95,47 @@ func _() {
 }
 
 
+// don't lose blank lines in grouped declarations
+const (
+       _       int     = 0;
+       _       float   = 1;
+
+       _       string  = "foo";
+
+       _       = iota;
+       _;
+
+       // a comment
+       _;
+
+       _;
+)
+
+
+type (
+       _       int;
+       _       struct{};
+
+       _       interface{};
+
+       // a comment
+       _       map[string]int;
+)
+
+
+var (
+       _       int     = 0;
+       _       float   = 1;
+
+       _       string  = "foo";
+
+       _       bool;
+
+       // a comment
+       _       bool;
+)
+
+
 // don't lose blank lines in this struct
 type _ struct {
        String  struct {
@@ -200,6 +242,7 @@ func _() {
                _       int;
                _       float;
                _       string;
+
                _       int;    // comment
                _       float;  // comment
                _       string; // comment