1
0
mirror of https://github.com/golang/go synced 2024-11-24 00:50:10 -07:00

cmd/doc: fix strange indentation artifacts with unexported fields

The NamePos value was not being set, and would default to a value
of zero. This would cause the printing logic to get confused as
to where exactly to place the "Has unexported fields" string.

A trivial package changes from

<
type A struct {
	A int // A
	B int
			// B
	// Has unexported fields.
}
>

to

<
type A struct {
	A int // A
	B int // B
	// Has unexported fields.
}
>

Fixes #12971

Change-Id: I53b7799a1f1c0ad7dcaddff83d9aaeb1d6b7823e
Reviewed-on: https://go-review.googlesource.com/16286
Run-TryBot: Joe Tsai <joetsai@digital-static.net>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Joe Tsai 2015-10-23 17:09:39 -07:00 committed by Rob Pike
parent 90e26f52c6
commit 07d48993f2
3 changed files with 52 additions and 3 deletions

View File

@ -219,7 +219,8 @@ var tests = []test{
[]string{
`Comment about exported type`, // Include comment.
`type ExportedType struct`, // Type definition.
`Comment before exported field.*\n.*ExportedField +int`,
`Comment before exported field.*\n.*ExportedField +int` +
`.*Comment on line with exported field.`,
`Has unexported fields`,
`func \(ExportedType\) ExportedMethod\(a int\) bool`,
`const ExportedTypedConstant ExportedType = iota`, // Must include associated constant.
@ -263,6 +264,40 @@ var tests = []test{
nil,
},
// Interface.
{
"type",
[]string{p, `ExportedInterface`},
[]string{
`Comment about exported interface`, // Include comment.
`type ExportedInterface interface`, // Interface definition.
`Comment before exported method.*\n.*ExportedMethod\(\)` +
`.*Comment on line with exported method`,
`Has unexported methods`,
},
[]string{
`unexportedField`, // No unexported field.
`Comment about exported method`, // No comment about exported method.
`unexportedMethod`, // No unexported method.
`unexportedTypedConstant`, // No unexported constant.
},
},
// Interface -u with unexported methods.
{
"type with unexported methods and -u",
[]string{"-u", p, `ExportedInterface`},
[]string{
`Comment about exported interface`, // Include comment.
`type ExportedInterface interface`, // Interface definition.
`Comment before exported method.*\n.*ExportedMethod\(\)` +
`.*Comment on line with exported method`,
`unexportedMethod\(\).*Comment on line with unexported method.`,
},
[]string{
`Has unexported methods`,
},
},
// Method.
{
"method",

View File

@ -504,7 +504,14 @@ func trimUnexportedFields(fields *ast.FieldList, what string) *ast.FieldList {
return fields
}
unexportedField := &ast.Field{
Type: ast.NewIdent(""), // Hack: printer will treat this as a field with a named type.
Type: &ast.Ident{
// Hack: printer will treat this as a field with a named type.
// Setting Name and NamePos to ("", fields.Closing-1) ensures that
// when Pos and End are called on this field, they return the
// position right before closing '}' character.
Name: "",
NamePos: fields.Closing - 1,
},
Comment: &ast.CommentGroup{
List: []*ast.Comment{{Text: fmt.Sprintf("// Has unexported %s.\n", what)}},
},

View File

@ -60,7 +60,7 @@ func internalFunc(a int) bool
// Comment about exported type.
type ExportedType struct {
// Comment before exported field.
ExportedField int
ExportedField int // Comment on line with exported field.
unexportedField int // Comment on line with unexported field.
}
@ -87,6 +87,13 @@ func ExportedTypeConstructor() *ExportedType {
const unexportedTypedConstant ExportedType = 1 // In a separate section to test -u.
// Comment about exported interface.
type ExportedInterface interface {
// Comment before exported method.
ExportedMethod() // Comment on line with exported method.
unexportedMethod() // Comment on line with unexported method.
}
// Comment about unexported type.
type unexportedType int