detached fd80be9

Gen: Add constants

No SnakeCase for constants because GTK exports a lot of overlapping
mixed-case const names that would produce conflicts.
commit
fd80be9e41f580421fac9eaaff0b4ce424b48039
author
Peter Fern · · 2023-12-31 05:38 UTC
committer
Peter Fern · · 2024-01-03 08:08 UTC
parent
dfc009c
signature
unsigned
4 files · +48 -0
M internal/gir/pass/pass.go +8 -0
@@ -92,6 +92,13 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
9292 enums[fn] = append(enums[fn], temp)
9393 }
9494
95+ constants := make(map[string][]types.ConstantTemplate)
96+ for _, con := range ns.Constants {
97+ fn := con.FilenameSafe()
98+ files = append(files, fn)
99+ constants[fn] = append(constants[fn], con.Template(ns.Name, p.Types))
100+ }
101+
95102 records := make(map[string][]types.RecordTemplate)
96103 recordLookup := make(map[string]bool)
97104 for _, rec := range ns.Records {
@@ -332,6 +339,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
332339 Callbacks: callbacks[fn],
333340 Records: records[fn],
334341 Enums: enums[fn],
342+ Constants: constants[fn],
335343 Functions: functions[fn],
336344 Interfaces: interfaces[fn],
337345 Classes: classes[fn],
@@ -92,6 +92,13 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
92 enums[fn] = append(enums[fn], temp)92 enums[fn] = append(enums[fn], temp)
93 }93 }
94 94
95+ constants := make(map[string][]types.ConstantTemplate)
96+ for _, con := range ns.Constants {
97+ fn := con.FilenameSafe()
98+ files = append(files, fn)
99+ constants[fn] = append(constants[fn], con.Template(ns.Name, p.Types))
100+ }
101+
95 records := make(map[string][]types.RecordTemplate)102 records := make(map[string][]types.RecordTemplate)
96 recordLookup := make(map[string]bool)103 recordLookup := make(map[string]bool)
97 for _, rec := range ns.Records {104 for _, rec := range ns.Records {
@@ -332,6 +339,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
332 Callbacks: callbacks[fn],339 Callbacks: callbacks[fn],
333 Records: records[fn],340 Records: records[fn],
334 Enums: enums[fn],341 Enums: enums[fn],
342+ Constants: constants[fn],
335 Functions: functions[fn],343 Functions: functions[fn],
336 Interfaces: interfaces[fn],344 Interfaces: interfaces[fn],
337 Classes: classes[fn],345 Classes: classes[fn],
M internal/gir/types/template.go +13 -0
@@ -202,6 +202,17 @@ type EnumTemplate struct {
202202 Values []enumValues
203203 }
204204
205+type ConstantTemplate struct {
206+ // Name is the name of the constant
207+ Name string
208+ // Doc is the documentation for the constant
209+ Doc string
210+ // Type is the Go type for the constant
211+ Type string
212+ // Values are the list of values for the constant
213+ Value string
214+}
215+
205216 type funcRetTemplate struct {
206217 // Raw is the raw value for the underlying purego function
207218 Raw string
@@ -380,6 +391,8 @@ type TemplateArg struct {
380391 Callbacks []CallbackTemplate
381392 // Enums are enumerations declared as const ... .... = ....
382393 Enums []EnumTemplate
394+ // Constants are declared as const ... .... = ....
395+ Constants []ConstantTemplate
383396 // Functions are the Go function declarations
384397 Functions []FuncTemplate
385398 // Interfaces is the list of interfaces that this package implements
@@ -202,6 +202,17 @@ type EnumTemplate struct {
202 Values []enumValues202 Values []enumValues
203 }203 }
204 204
205+type ConstantTemplate struct {
206+ // Name is the name of the constant
207+ Name string
208+ // Doc is the documentation for the constant
209+ Doc string
210+ // Type is the Go type for the constant
211+ Type string
212+ // Values are the list of values for the constant
213+ Value string
214+}
215+
205 type funcRetTemplate struct {216 type funcRetTemplate struct {
206 // Raw is the raw value for the underlying purego function217 // Raw is the raw value for the underlying purego function
207 Raw string218 Raw string
@@ -380,6 +391,8 @@ type TemplateArg struct {
380 Callbacks []CallbackTemplate391 Callbacks []CallbackTemplate
381 // Enums are enumerations declared as const ... .... = ....392 // Enums are enumerations declared as const ... .... = ....
382 Enums []EnumTemplate393 Enums []EnumTemplate
394+ // Constants are declared as const ... .... = ....
395+ Constants []ConstantTemplate
383 // Functions are the Go function declarations396 // Functions are the Go function declarations
384 Functions []FuncTemplate397 Functions []FuncTemplate
385 // Interfaces is the list of interfaces that this package implements398 // Interfaces is the list of interfaces that this package implements
M internal/gir/types/types.go +18 -0
@@ -4,6 +4,7 @@ package types
44
55 import (
66 "encoding/xml"
7+ "fmt"
78 "strconv"
89 "strings"
910
@@ -333,6 +334,23 @@ type Constant struct {
333334 InfoElements
334335 }
335336
337+func (c *Constant) Template(ns string, kinds KindMap) ConstantTemplate {
338+ t := c.Type.Template(ns, kinds)
339+ v := c.Value
340+
341+ switch t {
342+ case "string":
343+ v = fmt.Sprintf(`"%s"`, v)
344+ }
345+
346+ return ConstantTemplate{
347+ Name: c.Name,
348+ Doc: c.Doc.StringSafe(),
349+ Type: t,
350+ Value: v,
351+ }
352+}
353+
336354 type Constructor struct {
337355 XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"`
338356 CallableAttrs
@@ -4,6 +4,7 @@ package types
4 4
5 import (5 import (
6 "encoding/xml"6 "encoding/xml"
7+ "fmt"
7 "strconv"8 "strconv"
8 "strings"9 "strings"
9 10
@@ -333,6 +334,23 @@ type Constant struct {
333 InfoElements334 InfoElements
334 }335 }
335 336
337+func (c *Constant) Template(ns string, kinds KindMap) ConstantTemplate {
338+ t := c.Type.Template(ns, kinds)
339+ v := c.Value
340+
341+ switch t {
342+ case "string":
343+ v = fmt.Sprintf(`"%s"`, v)
344+ }
345+
346+ return ConstantTemplate{
347+ Name: c.Name,
348+ Doc: c.Doc.StringSafe(),
349+ Type: t,
350+ Value: v,
351+ }
352+}
353+
336 type Constructor struct {354 type Constructor struct {
337 XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"`355 XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"`
338 CallableAttrs356 CallableAttrs
M templates/go +9 -0
@@ -104,6 +104,15 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret
104104 type {{.Name}} = {{.Value}}
105105 {{end}}
106106
107+{{if .Constants -}}
108+const (
109+{{range .Constants -}}
110+ {{.Doc}}
111+ {{.Name}} {{.Type}} = {{.Value}}
112+{{end}}
113+)
114+{{end}}
115+
107116 {{range .Enums -}}
108117 {{.Doc}}
109118 type {{.Name}} int
@@ -104,6 +104,15 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret
104 type {{.Name}} = {{.Value}}104 type {{.Name}} = {{.Value}}
105 {{end}}105 {{end}}
106 106
107+{{if .Constants -}}
108+const (
109+{{range .Constants -}}
110+ {{.Doc}}
111+ {{.Name}} {{.Type}} = {{.Value}}
112+{{end}}
113+)
114+{{end}}
115+
107 {{range .Enums -}}116 {{range .Enums -}}
108 {{.Doc}}117 {{.Doc}}
109 type {{.Name}} int118 type {{.Name}} int

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close