mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/doc: print Go syntax when printing struct.field docs
Fixes #20928 Change-Id: I7f7aafb8ff4b5deb50c286a9ae81c34ee85e56a9 Reviewed-on: https://go-review.googlesource.com/47730 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
d1340ee2e9
commit
39ed6b14b4
3 changed files with 38 additions and 18 deletions
|
|
@ -89,7 +89,7 @@ var tests = []test{
|
||||||
`VarFive = 5`, // From block starting with unexported variable.
|
`VarFive = 5`, // From block starting with unexported variable.
|
||||||
`type unexportedType`, // No unexported type.
|
`type unexportedType`, // No unexported type.
|
||||||
`unexportedTypedConstant`, // No unexported typed constant.
|
`unexportedTypedConstant`, // No unexported typed constant.
|
||||||
`Field`, // No fields.
|
`\bField`, // No fields.
|
||||||
`Method`, // No methods.
|
`Method`, // No methods.
|
||||||
`someArgument[5-8]`, // No truncated arguments.
|
`someArgument[5-8]`, // No truncated arguments.
|
||||||
`type T1 T2`, // Type alias does not display as type declaration.
|
`type T1 T2`, // Type alias does not display as type declaration.
|
||||||
|
|
@ -395,14 +395,16 @@ var tests = []test{
|
||||||
"field",
|
"field",
|
||||||
[]string{p, `ExportedType.ExportedField`},
|
[]string{p, `ExportedType.ExportedField`},
|
||||||
[]string{
|
[]string{
|
||||||
|
`type ExportedType struct`,
|
||||||
`ExportedField int`,
|
`ExportedField int`,
|
||||||
`Comment before exported field.`,
|
`Comment before exported field.`,
|
||||||
`Comment on line with exported field.`,
|
`Comment on line with exported field.`,
|
||||||
|
`other fields elided`,
|
||||||
},
|
},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Field with -u.
|
// Field with -u.
|
||||||
{
|
{
|
||||||
"method with -u",
|
"method with -u",
|
||||||
[]string{"-u", p, `ExportedType.unexportedField`},
|
[]string{"-u", p, `ExportedType.unexportedField`},
|
||||||
|
|
@ -413,6 +415,14 @@ var tests = []test{
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Field of struct with only one field.
|
||||||
|
{
|
||||||
|
"single-field struct",
|
||||||
|
[]string{p, `ExportedStructOneField.OnlyField`},
|
||||||
|
[]string{`the only field`},
|
||||||
|
[]string{`other fields elided`},
|
||||||
|
},
|
||||||
|
|
||||||
// Case matching off.
|
// Case matching off.
|
||||||
{
|
{
|
||||||
"case matching off",
|
"case matching off",
|
||||||
|
|
|
||||||
|
|
@ -833,6 +833,7 @@ func (pkg *Package) printFieldDoc(symbol, fieldName string) bool {
|
||||||
pkg.Fatalf("symbol %s is not a type in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath)
|
pkg.Fatalf("symbol %s is not a type in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath)
|
||||||
}
|
}
|
||||||
found := false
|
found := false
|
||||||
|
numUnmatched := 0
|
||||||
for _, typ := range types {
|
for _, typ := range types {
|
||||||
// Type must be a struct.
|
// Type must be a struct.
|
||||||
spec := pkg.findTypeSpec(typ.Decl, typ.Name)
|
spec := pkg.findTypeSpec(typ.Decl, typ.Name)
|
||||||
|
|
@ -844,27 +845,32 @@ func (pkg *Package) printFieldDoc(symbol, fieldName string) bool {
|
||||||
for _, field := range structType.Fields.List {
|
for _, field := range structType.Fields.List {
|
||||||
// TODO: Anonymous fields.
|
// TODO: Anonymous fields.
|
||||||
for _, name := range field.Names {
|
for _, name := range field.Names {
|
||||||
if match(fieldName, name.Name) {
|
if !match(fieldName, name.Name) {
|
||||||
if !found {
|
numUnmatched++
|
||||||
pkg.Printf("struct %s {\n", typ.Name)
|
continue
|
||||||
}
|
|
||||||
if field.Doc != nil {
|
|
||||||
for _, comment := range field.Doc.List {
|
|
||||||
doc.ToText(&pkg.buf, comment.Text, indent, indent, indentedWidth)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s := pkg.oneLineNode(field.Type)
|
|
||||||
lineComment := ""
|
|
||||||
if field.Comment != nil {
|
|
||||||
lineComment = fmt.Sprintf(" %s", field.Comment.List[0].Text)
|
|
||||||
}
|
|
||||||
pkg.Printf("%s%s %s%s\n", indent, name, s, lineComment)
|
|
||||||
found = true
|
|
||||||
}
|
}
|
||||||
|
if !found {
|
||||||
|
pkg.Printf("type %s struct {\n", typ.Name)
|
||||||
|
}
|
||||||
|
if field.Doc != nil {
|
||||||
|
for _, comment := range field.Doc.List {
|
||||||
|
doc.ToText(&pkg.buf, comment.Text, indent, indent, indentedWidth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s := pkg.oneLineNode(field.Type)
|
||||||
|
lineComment := ""
|
||||||
|
if field.Comment != nil {
|
||||||
|
lineComment = fmt.Sprintf(" %s", field.Comment.List[0].Text)
|
||||||
|
}
|
||||||
|
pkg.Printf("%s%s %s%s\n", indent, name, s, lineComment)
|
||||||
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if found {
|
if found {
|
||||||
|
if numUnmatched > 0 {
|
||||||
|
pkg.Printf("\n // ... other fields elided ...\n")
|
||||||
|
}
|
||||||
pkg.Printf("}\n")
|
pkg.Printf("}\n")
|
||||||
}
|
}
|
||||||
return found
|
return found
|
||||||
|
|
|
||||||
4
src/cmd/doc/testdata/pkg.go
vendored
4
src/cmd/doc/testdata/pkg.go
vendored
|
|
@ -80,6 +80,10 @@ func (ExportedType) unexportedMethod(a int) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExportedStructOneField struct {
|
||||||
|
OnlyField int // the only field
|
||||||
|
}
|
||||||
|
|
||||||
// Constants tied to ExportedType. (The type is a struct so this isn't valid Go,
|
// Constants tied to ExportedType. (The type is a struct so this isn't valid Go,
|
||||||
// but it parses and that's all we need.)
|
// but it parses and that's all we need.)
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue