Skip to content

Commit ba7e45b

Browse files
authored
Merge pull request #341 from psha-/master
Generate const documentation.
2 parents 744da80 + 113c72f commit ba7e45b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

bind/gen_varconst.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ func (g *pyGen) genConstValue(c *Const) {
125125
val = "False"
126126
}
127127
g.pywrap.Printf("%s = %s\n", c.GoName(), val)
128+
if c.doc != "" {
129+
lns := strings.Split(c.doc, "\n")
130+
g.pywrap.Printf(`"""`)
131+
g.pywrap.Printf("\n")
132+
for _, l := range lns {
133+
g.pywrap.Printf("%s\n", l)
134+
}
135+
g.pywrap.Printf(`"""`)
136+
g.pywrap.Printf("\n")
137+
}
128138
}
129139

130140
func (g *pyGen) genEnum(e *Enum) {

bind/package.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,34 @@ func (p *Package) getDoc(parent string, o types.Object) string {
112112
n := o.Name()
113113
switch tp := o.(type) {
114114
case *types.Const:
115+
// Check for untyped consts
115116
for _, c := range p.doc.Consts {
116117
for _, cn := range c.Names {
117118
if n == cn {
118119
return c.Doc
119120
}
120121
}
121122
}
123+
// Check for typed consts
124+
scopeName := p.pkg.Scope().Lookup(n)
125+
if scopeName == nil {
126+
return ""
127+
}
128+
constType := scopeName.Type()
129+
if constType == nil {
130+
return ""
131+
}
132+
for _, t := range p.doc.Types {
133+
if p.pkg.Path()+"."+t.Name == constType.String() {
134+
for _, c := range t.Consts {
135+
for _, cn := range c.Names {
136+
if n == cn {
137+
return c.Doc
138+
}
139+
}
140+
}
141+
}
142+
}
122143

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

0 commit comments

Comments
 (0)