]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: fix strange indentation artifacts with unexported fields
authorJoe Tsai <joetsai@digital-static.net>
Sat, 24 Oct 2015 00:09:39 +0000 (17:09 -0700)
committerRob Pike <r@golang.org>
Tue, 17 Nov 2015 20:51:19 +0000 (20:51 +0000)
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>
src/cmd/doc/doc_test.go
src/cmd/doc/pkg.go
src/cmd/doc/testdata/pkg.go

index 7c72b878b113c28d59962f292b2816d6a0ba10f8..ed1d0e7c79044e62a847816f461382f1790f4712 100644 (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",
index f99df59ef08f9250bccb3032d3784f24dc0b723a..0b07f7cc7c6429e2260223735e6bc51242347352 100644 (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)}},
                },
index 0f06651d6bc16508dd9bd35a581c6317d4ecd84a..3e7acee50b8719cae5b1d292899b1bbb841235e9 100644 (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