diff options
author | Robert Griesemer <gri@golang.org> | 2009-08-31 13:13:04 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2009-08-31 13:13:04 -0700 |
commit | 363ea3df345b3421a1cafcaf3496b5163e82b59d (patch) | |
tree | 11f294d877572d50dac43e5742da5bdf1891fd09 /src/pkg/go/doc/doc.go | |
parent | e44fe9529859922b4c68899da458777534e3806b (diff) | |
download | golang-363ea3df345b3421a1cafcaf3496b5163e82b59d.tar.gz |
simplified heuristic for associating const/var decls with types
(per suggestion from rsc)
R=rsc
DELTA=24 (3 added, 9 deleted, 12 changed)
OCL=34121
CL=34130
Diffstat (limited to 'src/pkg/go/doc/doc.go')
-rw-r--r-- | src/pkg/go/doc/doc.go | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/src/pkg/go/doc/doc.go b/src/pkg/go/doc/doc.go index f40e98d6f..775c4342a 100644 --- a/src/pkg/go/doc/doc.go +++ b/src/pkg/go/doc/doc.go @@ -119,12 +119,11 @@ func baseTypeName(typ ast.Expr) string { func (doc *docReader) addValue(decl *ast.GenDecl) { // determine if decl should be associated with a type - // Heuristic: Collect all types and determine the most frequent type. - // If it is "dominant enough" the decl is associated with - // that type. - - // determine type frequencies - freq := make(map[string]int); + // Heuristic: For each typed entry, determine the type name, if any. + // If there is exactly one type name that is sufficiently + // frequent, associate the decl with the respective type. + domName := ""; + domFreq := 0; prev := ""; for _, s := range decl.Specs { if v, ok := s.(*ast.ValueSpec); ok { @@ -141,30 +140,25 @@ func (doc *docReader) addValue(decl *ast.GenDecl) { name = prev; } if name != "" { - // increase freq count for name - f := 0; - if f0, found := freq[name]; found { - f = f0; + // entry has a named type + if domName != "" && domName != name { + // more than one type name - do not associate + // with any type + domName = ""; + break; } - freq[name] = f+1; + domName = name; + domFreq++; } prev = name; } } - // determine most common type - domName, domFreq := "", 0; - for name, f := range freq { - if f > domFreq { - domName, domFreq = name, f; - } - } - // determine values list const threshold = 0.75; values := doc.values; - if domFreq >= int(float(len(decl.Specs)) * threshold) { - // most common type is "dominant enough" + if domName != "" && domFreq >= int(float(len(decl.Specs)) * threshold) { + // typed entries are sufficiently frequent typ := doc.lookupTypeDoc(domName); if typ != nil { values = typ.values; // associate with that type |