Commit 07d48993 authored by Joe Tsai's avatar Joe Tsai Committed by Rob Pike

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: 's avatarRob Pike <r@golang.org>
parent 90e26f52
......@@ -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",
......
......@@ -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)}},
},
......
......@@ -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
......
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