detached 562a1aa

Gir + Templates: Generate more Go friendly constructor names

Previously we just added the snake case of the constructor name + the
record/class name. This leads to confusing names (old -> new):

gtk.NewFromStringBuilder -> gtk.NewBuilderFromString
gdkpixbuf.NewFromFilePixbuf -> gdkpixbuf.NewPixbufFromFile

Now we just check if it begins with New and if so add the class/record
name directly after "New"
commit
562a1aaaabb6eb59c197184719b28b807c3bccd0
author
jwijenbergh · · 2024-01-14 14:34 UTC
parent
db0ebd3
signature
GPG Unverified1A2298F6C34F96E5 The key is unknown
3 files · +25 -12
M internal/gir/pass/pass.go +2 -4
@@ -109,9 +109,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
109109 fn := rec.FilenameSafe()
110110 files = append(files, fn)
111111 for i, c := range rec.Constructors {
112- name := util.SnakeToCamel(c.Name)
113112 constructors[i] = types.FuncTemplate{
114- Name: name,
113+ Name: util.ConstructorName(c.Name, rec.Name),
115114 CName: c.CIdentifier,
116115 Doc: c.Doc.StringSafe(),
117116 Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),
@@ -245,10 +244,9 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
245244 files = append(files, fn)
246245
247246 for i, c := range cls.Constructors {
248- name := util.SnakeToCamel(c.Name)
249247 c.ReturnValue.AnyType.Type.Name = cls.Name
250248 constructors[i] = types.FuncTemplate{
251- Name: name,
249+ Name: util.ConstructorName(c.Name, cls.Name),
252250 CName: c.CIdentifier,
253251 Doc: c.Doc.StringSafe(),
254252 Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),
@@ -109,9 +109,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
109 fn := rec.FilenameSafe()109 fn := rec.FilenameSafe()
110 files = append(files, fn)110 files = append(files, fn)
111 for i, c := range rec.Constructors {111 for i, c := range rec.Constructors {
112- name := util.SnakeToCamel(c.Name)
113 constructors[i] = types.FuncTemplate{112 constructors[i] = types.FuncTemplate{
114- Name: name,113+ Name: util.ConstructorName(c.Name, rec.Name),
115 CName: c.CIdentifier,114 CName: c.CIdentifier,
116 Doc: c.Doc.StringSafe(),115 Doc: c.Doc.StringSafe(),
117 Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),116 Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),
@@ -245,10 +244,9 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
245 files = append(files, fn)244 files = append(files, fn)
246 245
247 for i, c := range cls.Constructors {246 for i, c := range cls.Constructors {
248- name := util.SnakeToCamel(c.Name)
249 c.ReturnValue.AnyType.Type.Name = cls.Name247 c.ReturnValue.AnyType.Type.Name = cls.Name
250 constructors[i] = types.FuncTemplate{248 constructors[i] = types.FuncTemplate{
251- Name: name,249+ Name: util.ConstructorName(c.Name, cls.Name),
252 CName: c.CIdentifier,250 CName: c.CIdentifier,
253 Doc: c.Doc.StringSafe(),251 Doc: c.Doc.StringSafe(),
254 Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),252 Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws),
M internal/gir/util/util.go +15 -0
@@ -91,3 +91,18 @@ func ConvertArgsComma(a []string) string {
9191 }
9292 return ", " + strings.Join(a, ", ")
9393 }
94+
95+// ConstructorName returns a Go friendly constructor name given the raw constructor name `name` and the class/record name `outer`
96+func ConstructorName(name string, outer string) string {
97+ cname := SnakeToCamel(name)
98+ // construct the final constructor name
99+ // for example if we have gtk_builder
100+ // gtk_builder_new_from_file
101+ // cname will be NewFromFile
102+ // we convert it to NewBuilderFromFile
103+ if strings.HasPrefix(cname, "New") {
104+ return "New" + outer + cname[3:]
105+ }
106+ // the default is just a concatenation if the constructor doesn't start with New
107+ return outer + cname
108+}
@@ -91,3 +91,18 @@ func ConvertArgsComma(a []string) string {
91 }91 }
92 return ", " + strings.Join(a, ", ")92 return ", " + strings.Join(a, ", ")
93 }93 }
94+
95+// ConstructorName returns a Go friendly constructor name given the raw constructor name `name` and the class/record name `outer`
96+func ConstructorName(name string, outer string) string {
97+ cname := SnakeToCamel(name)
98+ // construct the final constructor name
99+ // for example if we have gtk_builder
100+ // gtk_builder_new_from_file
101+ // cname will be NewFromFile
102+ // we convert it to NewBuilderFromFile
103+ if strings.HasPrefix(cname, "New") {
104+ return "New" + outer + cname[3:]
105+ }
106+ // the default is just a concatenation if the constructor doesn't start with New
107+ return outer + cname
108+}
M templates/go +8 -8
@@ -35,13 +35,13 @@ func (x *{{.Name}}) GoPointer() uintptr {
3535 {{$outer := .}}
3636
3737 {{range .Constructors -}}
38-var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
38+var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
3939
4040
4141 {{.Doc}}
42-func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
42+func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
4343 {{.Ret.Preamble $NotGLib}}
44- {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}})
44+ {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}})
4545 {{.Ret.Fmt $NotGObject}}
4646 }
4747 {{end}}
@@ -150,13 +150,13 @@ func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} {
150150 }
151151
152152 {{range .Constructors -}}
153-var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
153+var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
154154
155155
156156 {{.Doc}}
157-func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
157+func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
158158 {{.Ret.Preamble $NotGLib}}
159- {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}})
159+ {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}})
160160 {{.Ret.Fmt $NotGObject}}
161161 }
162162 {{end}}
@@ -240,7 +240,7 @@ func init() {
240240 {{range .Records -}}
241241 {{$rec := .}}
242242 {{range .Constructors -}}
243- core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}")
243+ core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
244244 {{end}}
245245 {{range .Receivers -}}
246246 core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}")
@@ -250,7 +250,7 @@ func init() {
250250 {{range .Classes -}}
251251 {{$cls := .}}
252252 {{range .Constructors -}}
253- core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}")
253+ core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
254254 {{end}}
255255 {{range .Receivers -}}
256256 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")
@@ -35,13 +35,13 @@ func (x *{{.Name}}) GoPointer() uintptr {
35 {{$outer := .}}35 {{$outer := .}}
36 36
37 {{range .Constructors -}}37 {{range .Constructors -}}
38-var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}38+var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
39 39
40 40
41 {{.Doc}}41 {{.Doc}}
42-func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {42+func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
43 {{.Ret.Preamble $NotGLib}}43 {{.Ret.Preamble $NotGLib}}
44- {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}})44+ {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}})
45 {{.Ret.Fmt $NotGObject}}45 {{.Ret.Fmt $NotGObject}}
46 }46 }
47 {{end}}47 {{end}}
@@ -150,13 +150,13 @@ func {{.Name}}NewFromInternalPtr(ptr uintptr) *{{.Name}} {
150 }150 }
151 151
152 {{range .Constructors -}}152 {{range .Constructors -}}
153-var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}153+var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
154 154
155 155
156 {{.Doc}}156 {{.Doc}}
157-func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {157+func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
158 {{.Ret.Preamble $NotGLib}}158 {{.Ret.Preamble $NotGLib}}
159- {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}})159+ {{if .Ret.Value}}cret :={{end}}x{{.Name}}({{conv .Args.API.Call}})
160 {{.Ret.Fmt $NotGObject}}160 {{.Ret.Fmt $NotGObject}}
161 }161 }
162 {{end}}162 {{end}}
@@ -240,7 +240,7 @@ func init() {
240 {{range .Records -}}240 {{range .Records -}}
241 {{$rec := .}}241 {{$rec := .}}
242 {{range .Constructors -}}242 {{range .Constructors -}}
243- core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}")243+ core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
244 {{end}}244 {{end}}
245 {{range .Receivers -}}245 {{range .Receivers -}}
246 core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}")246 core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}")
@@ -250,7 +250,7 @@ func init() {
250 {{range .Classes -}}250 {{range .Classes -}}
251 {{$cls := .}}251 {{$cls := .}}
252 {{range .Constructors -}}252 {{range .Constructors -}}
253- core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}")253+ core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
254 {{end}}254 {{end}}
255 {{range .Receivers -}}255 {{range .Receivers -}}
256 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")256 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")

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