Commit c8c3f1d5 authored by Robert Griesemer's avatar Robert Griesemer

- respect source line breaks in grouped declarations

- made ast.Spec nodes implement Node interface
- added extra test cases

R=rsc
http://go/go-review/1016038
parent 524ade9a
...@@ -596,6 +596,7 @@ func (s *RangeStmt) stmtNode() {} ...@@ -596,6 +596,7 @@ func (s *RangeStmt) stmtNode() {}
type ( type (
// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec. // The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec.
Spec interface { Spec interface {
Node;
specNode(); specNode();
}; };
...@@ -627,6 +628,23 @@ type ( ...@@ -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 // specNode() ensures that only spec nodes can be
// assigned to a Spec. // assigned to a Spec.
// //
......
...@@ -255,8 +255,12 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok ...@@ -255,8 +255,12 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
if len(list) == 1 { if len(list) == 1 {
sep = blank; sep = blank;
} }
var ml bool;
for i, f := range list { for i, f := range list {
var ml bool; if i > 0 {
p.linebreak(f.Pos().Line, 1, 2, ignore, ml);
}
ml = false;
extraTabs := 0; extraTabs := 0;
p.leadComment(f.Doc); p.leadComment(f.Doc);
if len(f.Names) > 0 { if len(f.Names) > 0 {
...@@ -283,21 +287,23 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok ...@@ -283,21 +287,23 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
} }
p.lineComment(f.Comment); 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 isIncomplete {
if len(list) > 0 {
p.print(formfeed);
}
// TODO(gri): this needs to be styled like normal comments // TODO(gri): this needs to be styled like normal comments
p.print("// contains unexported fields"); p.print("// contains unexported fields");
} }
} else { // interface } else { // interface
var ml bool;
for i, f := range list { 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); p.leadComment(f.Doc);
if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp { if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
// method // method
...@@ -309,13 +315,11 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok ...@@ -309,13 +315,11 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
} }
p.print(token.SEMICOLON); p.print(token.SEMICOLON);
p.lineComment(f.Comment); 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 isIncomplete {
if len(list) > 0 {
p.print(formfeed);
}
// TODO(gri): this needs to be styled like normal comments // TODO(gri): this needs to be styled like normal comments
p.print("// contains unexported methods"); p.print("// contains unexported methods");
} }
...@@ -941,11 +945,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool) ...@@ -941,11 +945,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
var ml bool; var ml bool;
for i, s := range d.Specs { for i, s := range d.Specs {
if i > 0 { if i > 0 {
if ml { p.linebreak(s.Pos().Line, 1, 2, ignore, ml);
p.print(formfeed);
} else {
p.print(newline);
}
} }
ml = false; ml = false;
p.spec(s, len(d.Specs), inGroup, &ml); p.spec(s, len(d.Specs), inGroup, &ml);
......
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
import ( import (
"io"; "io";
aLongRename "io"; aLongRename "io";
b "io"; b "io";
c "i" "o"; c "i" "o";
) )
...@@ -93,6 +94,47 @@ func _() { ...@@ -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 // don't lose blank lines in this struct
type _ struct { type _ struct {
String struct { String struct {
......
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
import ( import (
"io"; "io";
aLongRename "io"; aLongRename "io";
b "io"; b "io";
c "i" "o"; c "i" "o";
) )
...@@ -94,6 +95,47 @@ func _() { ...@@ -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 // don't lose blank lines in this struct
type _ struct { type _ struct {
String struct { String struct {
...@@ -200,6 +242,7 @@ func _() { ...@@ -200,6 +242,7 @@ func _() {
_ int; _ int;
_ float; _ float;
_ string; _ string;
_ int; // comment _ int; // comment
_ float; // comment _ float; // comment
_ string; // comment _ string; // comment
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment