Commit 5a40a682 authored by Robert Griesemer's avatar Robert Griesemer

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
parent 149e3d33
...@@ -119,12 +119,11 @@ func baseTypeName(typ ast.Expr) string { ...@@ -119,12 +119,11 @@ func baseTypeName(typ ast.Expr) string {
func (doc *docReader) addValue(decl *ast.GenDecl) { func (doc *docReader) addValue(decl *ast.GenDecl) {
// determine if decl should be associated with a type // determine if decl should be associated with a type
// Heuristic: Collect all types and determine the most frequent type. // Heuristic: For each typed entry, determine the type name, if any.
// If it is "dominant enough" the decl is associated with // If there is exactly one type name that is sufficiently
// that type. // frequent, associate the decl with the respective type.
domName := "";
// determine type frequencies domFreq := 0;
freq := make(map[string]int);
prev := ""; prev := "";
for _, s := range decl.Specs { for _, s := range decl.Specs {
if v, ok := s.(*ast.ValueSpec); ok { if v, ok := s.(*ast.ValueSpec); ok {
...@@ -141,30 +140,25 @@ func (doc *docReader) addValue(decl *ast.GenDecl) { ...@@ -141,30 +140,25 @@ func (doc *docReader) addValue(decl *ast.GenDecl) {
name = prev; name = prev;
} }
if name != "" { if name != "" {
// increase freq count for name // entry has a named type
f := 0; if domName != "" && domName != name {
if f0, found := freq[name]; found { // more than one type name - do not associate
f = f0; // with any type
domName = "";
break;
} }
freq[name] = f+1; domName = name;
domFreq++;
} }
prev = name; prev = name;
} }
} }
// determine most common type
domName, domFreq := "", 0;
for name, f := range freq {
if f > domFreq {
domName, domFreq = name, f;
}
}
// determine values list // determine values list
const threshold = 0.75; const threshold = 0.75;
values := doc.values; values := doc.values;
if domFreq >= int(float(len(decl.Specs)) * threshold) { if domName != "" && domFreq >= int(float(len(decl.Specs)) * threshold) {
// most common type is "dominant enough" // typed entries are sufficiently frequent
typ := doc.lookupTypeDoc(domName); typ := doc.lookupTypeDoc(domName);
if typ != nil { if typ != nil {
values = typ.values; // associate with that type values = typ.values; // associate with that type
......
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