detached 8fc4929

Gen: Add functions for classes

commit
8fc492911b9c142c3bd94663893beae07d214256
author
Peter Fern · · 2023-12-29 04:29 UTC
committer
Peter Fern · · 2024-01-03 01:44 UTC
parent
c57ccf3
signature
unsigned
3 files · +33 -12
M internal/gir/pass/pass.go +17 -12
@@ -212,6 +212,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
212212 for _, cls := range ns.Classes {
213213 implemented := make(map[string]bool)
214214 constructors := make([]types.FuncTemplate, len(cls.Constructors))
215+ functions := make([]types.FuncTemplate, len(cls.Functions))
215216 fn := cls.FilenameSafe()
216217 files = append(files, fn)
217218
@@ -248,6 +249,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
248249 }
249250 }
250251 var interfaces []types.InterfaceTemplate
252+ for i, f := range cls.Functions {
253+ name := fmt.Sprintf("%s%s", util.SnakeToCamel(cls.Name), util.SnakeToCamel(f.Name))
254+ functions[i] = types.FuncTemplate{
255+ Name: name,
256+ CName: f.CIdentifier,
257+ Doc: f.Doc.StringSafe(),
258+ Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws),
259+ Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws),
260+ }
261+ }
251262 for _, impl := range cls.Implements {
252263 interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types))
253264 }
@@ -258,6 +269,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
258269 Constructors: constructors,
259270 Receivers: receivers,
260271 Interfaces: interfaces,
272+ Functions: functions,
261273 Signals: signals,
262274 })
263275 }
@@ -275,22 +287,15 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
275287 //}
276288 methods := 0
277289 for _, i := range interfaces[fn] {
278- for range i.Methods {
279- methods += 1
280- }
290+ methods += len(i.Methods)
281291 }
282292 for _, i := range records[fn] {
283- for range i.Constructors {
284- methods += 1
285- }
293+ methods += len(i.Constructors)
286294 }
287295 for _, i := range classes[fn] {
288- for range i.Constructors {
289- methods += 1
290- }
291- for range i.Receivers {
292- methods += 1
293- }
296+ methods += len(i.Constructors)
297+ methods += len(i.Receivers)
298+ methods += len(i.Functions)
294299 }
295300 // we do not need to add the length of interfaces in here
296301 // as they should only be loaded when there are classes
@@ -212,6 +212,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
212 for _, cls := range ns.Classes {212 for _, cls := range ns.Classes {
213 implemented := make(map[string]bool)213 implemented := make(map[string]bool)
214 constructors := make([]types.FuncTemplate, len(cls.Constructors))214 constructors := make([]types.FuncTemplate, len(cls.Constructors))
215+ functions := make([]types.FuncTemplate, len(cls.Functions))
215 fn := cls.FilenameSafe()216 fn := cls.FilenameSafe()
216 files = append(files, fn)217 files = append(files, fn)
217 218
@@ -248,6 +249,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
248 }249 }
249 }250 }
250 var interfaces []types.InterfaceTemplate251 var interfaces []types.InterfaceTemplate
252+ for i, f := range cls.Functions {
253+ name := fmt.Sprintf("%s%s", util.SnakeToCamel(cls.Name), util.SnakeToCamel(f.Name))
254+ functions[i] = types.FuncTemplate{
255+ Name: name,
256+ CName: f.CIdentifier,
257+ Doc: f.Doc.StringSafe(),
258+ Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws),
259+ Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws),
260+ }
261+ }
251 for _, impl := range cls.Implements {262 for _, impl := range cls.Implements {
252 interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types))263 interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types))
253 }264 }
@@ -258,6 +269,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
258 Constructors: constructors,269 Constructors: constructors,
259 Receivers: receivers,270 Receivers: receivers,
260 Interfaces: interfaces,271 Interfaces: interfaces,
272+ Functions: functions,
261 Signals: signals,273 Signals: signals,
262 })274 })
263 }275 }
@@ -275,22 +287,15 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
275 //}287 //}
276 methods := 0288 methods := 0
277 for _, i := range interfaces[fn] {289 for _, i := range interfaces[fn] {
278- for range i.Methods {290+ methods += len(i.Methods)
279- methods += 1
280- }
281 }291 }
282 for _, i := range records[fn] {292 for _, i := range records[fn] {
283- for range i.Constructors {293+ methods += len(i.Constructors)
284- methods += 1
285- }
286 }294 }
287 for _, i := range classes[fn] {295 for _, i := range classes[fn] {
288- for range i.Constructors {296+ methods += len(i.Constructors)
289- methods += 1297+ methods += len(i.Receivers)
290- }298+ methods += len(i.Functions)
291- for range i.Receivers {
292- methods += 1
293- }
294 }299 }
295 // we do not need to add the length of interfaces in here300 // we do not need to add the length of interfaces in here
296 // as they should only be loaded when there are classes301 // as they should only be loaded when there are classes
M internal/gir/types/template.go +2 -0
@@ -345,6 +345,8 @@ type ClassTemplate struct {
345345 Receivers []FuncTemplate
346346 // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface
347347 Interfaces []InterfaceTemplate
348+ // Functions are the Go function declarations
349+ Functions []FuncTemplate
348350 // Signals are helpers for ConnectX receivers
349351 Signals []SignalsTemplate
350352 }
@@ -345,6 +345,8 @@ type ClassTemplate struct {
345 Receivers []FuncTemplate345 Receivers []FuncTemplate
346 // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface346 // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface
347 Interfaces []InterfaceTemplate347 Interfaces []InterfaceTemplate
348+ // Functions are the Go function declarations
349+ Functions []FuncTemplate
348 // Signals are helpers for ConnectX receivers350 // Signals are helpers for ConnectX receivers
349 Signals []SignalsTemplate351 Signals []SignalsTemplate
350 }352 }
M templates/go +14 -0
@@ -190,6 +190,17 @@ func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
190190 {{end}}
191191 {{end}}
192192
193+{{range .Functions -}}
194+var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
195+
196+{{.Doc}}
197+func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
198+ {{.Ret.Preamble $NotGLib}}
199+ {{if .Ret.Value}}cret := {{end}}x{{.Name}}({{conv .Args.API.Call}})
200+ {{.Ret.Fmt $NotGObject}}
201+}
202+{{end}}
203+
193204 {{end}}
194205
195206
@@ -219,6 +230,9 @@ func init() {
219230 {{range .Receivers -}}
220231 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")
221232 {{end}}
233+ {{range .Functions -}}
234+ core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
235+ {{end}}
222236 {{end}}
223237
224238 {{range .Interfaces -}}
@@ -190,6 +190,17 @@ func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
190 {{end}}190 {{end}}
191 {{end}}191 {{end}}
192 192
193+{{range .Functions -}}
194+var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}}
195+
196+{{.Doc}}
197+func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
198+ {{.Ret.Preamble $NotGLib}}
199+ {{if .Ret.Value}}cret := {{end}}x{{.Name}}({{conv .Args.API.Call}})
200+ {{.Ret.Fmt $NotGObject}}
201+}
202+{{end}}
203+
193 {{end}}204 {{end}}
194 205
195 206
@@ -219,6 +230,9 @@ func init() {
219 {{range .Receivers -}}230 {{range .Receivers -}}
220 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")231 core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}")
221 {{end}}232 {{end}}
233+ {{range .Functions -}}
234+ core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}")
235+ {{end}}
222 {{end}}236 {{end}}
223 237
224 {{range .Interfaces -}}238 {{range .Interfaces -}}

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