Skip to content

Commit

Permalink
Merge pull request #341 from psha-/master
Browse files Browse the repository at this point in the history
Generate const documentation.
  • Loading branch information
rcoreilly authored Dec 12, 2023
2 parents 744da80 + 113c72f commit ba7e45b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bind/gen_varconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ func (g *pyGen) genConstValue(c *Const) {
val = "False"
}
g.pywrap.Printf("%s = %s\n", c.GoName(), val)
if c.doc != "" {
lns := strings.Split(c.doc, "\n")
g.pywrap.Printf(`"""`)
g.pywrap.Printf("\n")
for _, l := range lns {
g.pywrap.Printf("%s\n", l)
}
g.pywrap.Printf(`"""`)
g.pywrap.Printf("\n")
}
}

func (g *pyGen) genEnum(e *Enum) {
Expand Down
21 changes: 21 additions & 0 deletions bind/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,34 @@ func (p *Package) getDoc(parent string, o types.Object) string {
n := o.Name()
switch tp := o.(type) {
case *types.Const:
// Check for untyped consts
for _, c := range p.doc.Consts {
for _, cn := range c.Names {
if n == cn {
return c.Doc
}
}
}
// Check for typed consts
scopeName := p.pkg.Scope().Lookup(n)
if scopeName == nil {
return ""
}
constType := scopeName.Type()
if constType == nil {
return ""
}
for _, t := range p.doc.Types {
if p.pkg.Path()+"."+t.Name == constType.String() {
for _, c := range t.Consts {
for _, cn := range c.Names {
if n == cn {
return c.Doc
}
}
}
}
}

case *types.Var:
if tp.IsField() && parent != "" {
Expand Down

0 comments on commit ba7e45b

Please sign in to comment.