From 07d48993f257a6536d83555bb8cc9daffa07dd56 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Fri, 23 Oct 2015 17:09:39 -0700 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/cmd/doc/doc_test.go | 37 ++++++++++++++++++++++++++++++++++++- src/cmd/doc/pkg.go | 9 ++++++++- src/cmd/doc/testdata/pkg.go | 9 ++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/cmd/doc/doc_test.go b/src/cmd/doc/doc_test.go index 7c72b878b1..ed1d0e7c79 100644 --- a/src/cmd/doc/doc_test.go +++ b/src/cmd/doc/doc_test.go @@ -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", diff --git a/src/cmd/doc/pkg.go b/src/cmd/doc/pkg.go index f99df59ef0..0b07f7cc7c 100644 --- a/src/cmd/doc/pkg.go +++ b/src/cmd/doc/pkg.go @@ -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)}}, }, diff --git a/src/cmd/doc/testdata/pkg.go b/src/cmd/doc/testdata/pkg.go index 0f06651d6b..3e7acee50b 100644 --- a/src/cmd/doc/testdata/pkg.go +++ b/src/cmd/doc/testdata/pkg.go @@ -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 -- 2.48.1