1
0
mirror of https://github.com/golang/go synced 2024-11-20 10:04:45 -07:00

go/printer: allow one-method interfaces to be printed on a single line

Previously, only the empty interface could be formatted to print on a
single line. This behaviour made short one-method interfaces in function
definitions and type assertions more verbose than they had to be.

For example, the following type assertion:

    if c, ok := v.(interface {
        Close() error
    }); ok {
    }

Can now be formatted as:

    if c, ok := v.(interface{ Close() error }); ok {
    }

Fixes #21952

Change-Id: I896f796c5a30b9f4da2be3fe67cb6fea5871b835
Reviewed-on: https://go-review.googlesource.com/66130
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Tim Cooper 2017-09-25 22:34:23 -03:00 committed by Robert Griesemer
parent e04ff3d133
commit 92ba9b5c40
4 changed files with 51 additions and 11 deletions

View File

@ -398,22 +398,33 @@ func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool)
// no blank between keyword and {} in this case // no blank between keyword and {} in this case
p.print(lbrace, token.LBRACE, rbrace, token.RBRACE) p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
return return
} else if isStruct && p.isOneLineFieldList(list) { // for now ignore interfaces } else if p.isOneLineFieldList(list) {
// small enough - print on one line // small enough - print on one line
// (don't use identList and ignore source line breaks) // (don't use identList and ignore source line breaks)
p.print(lbrace, token.LBRACE, blank) p.print(lbrace, token.LBRACE, blank)
f := list[0] f := list[0]
for i, x := range f.Names { if isStruct {
if i > 0 { for i, x := range f.Names {
// no comments so no need for comma position if i > 0 {
p.print(token.COMMA, blank) // no comments so no need for comma position
p.print(token.COMMA, blank)
}
p.expr(x)
}
if len(f.Names) > 0 {
p.print(blank)
}
p.expr(f.Type)
} else { // interface
if ftyp, isFtyp := f.Type.(*ast.FuncType); isFtyp {
// method
p.expr(f.Names[0])
p.signature(ftyp.Params, ftyp.Results)
} else {
// embedded interface
p.expr(f.Type)
} }
p.expr(x)
} }
if len(f.Names) > 0 {
p.print(blank)
}
p.expr(f.Type)
p.print(blank, rbrace, token.RBRACE) p.print(blank, rbrace, token.RBRACE)
return return
} }

View File

@ -290,6 +290,16 @@ func _() {
_ = struct{ x, y, z int }{0, 1, 2} _ = struct{ x, y, z int }{0, 1, 2}
_ = struct{ int }{0} _ = struct{ int }{0}
_ = struct{ s struct{ int } }{struct{ int }{0}} _ = struct{ s struct{ int } }{struct{ int }{0}}
_ = (interface{})(nil)
_ = (interface{ String() string })(nil)
_ = (interface {
String() string
})(nil)
_ = (interface{ fmt.Stringer })(nil)
_ = (interface {
fmt.Stringer
})(nil)
} }
func _() { func _() {

View File

@ -295,8 +295,17 @@ func _() {
_ = struct{ x, y, z int }{0, 1, 2} _ = struct{ x, y, z int }{0, 1, 2}
_ = struct{ int }{0} _ = struct{ int }{0}
_ = struct{ s struct { int } }{struct{ int}{0} } _ = struct{ s struct { int } }{struct{ int}{0} }
}
_ = (interface{})(nil)
_ = (interface{String() string})(nil)
_ = (interface{
String() string
})(nil)
_ = (interface{fmt.Stringer})(nil)
_ = (interface{
fmt.Stringer
})(nil)
}
func _() { func _() {
// do not modify literals // do not modify literals

View File

@ -290,6 +290,16 @@ func _() {
_ = struct{ x, y, z int }{0, 1, 2} _ = struct{ x, y, z int }{0, 1, 2}
_ = struct{ int }{0} _ = struct{ int }{0}
_ = struct{ s struct{ int } }{struct{ int }{0}} _ = struct{ s struct{ int } }{struct{ int }{0}}
_ = (interface{})(nil)
_ = (interface{ String() string })(nil)
_ = (interface {
String() string
})(nil)
_ = (interface{ fmt.Stringer })(nil)
_ = (interface {
fmt.Stringer
})(nil)
} }
func _() { func _() {