Skip to content

Commit ecf9498

Browse files
authored
Merge pull request AllenDang#2 from neclepsio/idiomatic
Add idiomatic bindings
2 parents 5edd1fc + dbab3a7 commit ecf9498

File tree

407 files changed

+189567
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

407 files changed

+189567
-0
lines changed

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ compile_cimgui_macos:
3535
cd ./cimgui/build; make
3636
cp -f ./cimgui/build/cimgui.a ./lib/macos/arm64/
3737

38+
## generate idiomatic bindings
39+
.PHONY: idiomatic
40+
idiomatic:
41+
@echo "Generating idiomatic bindings"
42+
rm -rf ./idiomatic
43+
mkdir ./idiomatic
44+
mkdir ./idiomatic/cimgui
45+
mkdir ./idiomatic/cimplot
46+
mkdir ./idiomatic/thirdparty
47+
mkdir ./idiomatic/thirdparty/glfw
48+
49+
cp ./*.go ./*.cpp ./*.h ./idiomatic
50+
cp ./cimgui/*.h ./cimgui/*.cpp ./idiomatic/cimgui
51+
cp ./cimplot/*.h ./cimplot/*.cpp ./idiomatic/cimplot
52+
cp -r ./thirdparty/glfw/* ./idiomatic/thirdparty/glfw
53+
54+
go run github.com/AllenDang/cimgui-go/cmd/rename ./idiomatic/*.go
55+
3856
## generate: generates both bindings (equal to `all`)
3957
.PHONY: generate
4058
generate: cimgui cimplot

cmd/rename/main.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"go/ast"
6+
"go/parser"
7+
"go/printer"
8+
"go/token"
9+
"os"
10+
"strings"
11+
"unicode"
12+
"unicode/utf8"
13+
)
14+
15+
type Visitor struct {
16+
fset *token.FileSet
17+
file *ast.File
18+
}
19+
20+
var replace = map[string]string{
21+
"GetDrawData": "CurrentDrawData",
22+
"GetDrawListSharedData": "CurrentDrawListSharedData",
23+
"GetFont": "CurrentFont",
24+
"GetIO": "CurrentIO",
25+
"GetPlatformIO": "CurrentPlatformIO",
26+
"GetStyle": "CurrentStyle",
27+
"GetMouseCursor": "CurrentMouseCursor",
28+
"ImAxis": "AxisName",
29+
"GetDrawCursor": "Cursor",
30+
"SetDrawCursor": "SetCursor",
31+
}
32+
33+
func removeImGuiIm(n string) string {
34+
old := n
35+
if strings.HasPrefix(n, "ImGui") {
36+
n = strings.TrimPrefix(n, "ImGui")
37+
} else if strings.HasPrefix(n, "Im") {
38+
n = strings.TrimPrefix(n, "Im")
39+
}
40+
c, _ := utf8.DecodeRuneInString(n)
41+
if !unicode.IsUpper(c) {
42+
n = old
43+
}
44+
return n
45+
}
46+
47+
func renameIdent(n string) string {
48+
if r, ok := replace[n]; ok {
49+
n = r
50+
}
51+
n = removeImGuiIm(n)
52+
if strings.HasPrefix(n, "New") {
53+
n = "New" + removeImGuiIm(n[3:])
54+
} else if strings.HasPrefix(n, "new") {
55+
n = "new" + removeImGuiIm(n[3:])
56+
}
57+
n = strings.TrimPrefix(n, "Get")
58+
if n != "_" {
59+
n = strings.ReplaceAll(n, "_", "")
60+
}
61+
return n
62+
}
63+
64+
func (v *Visitor) Visit(node ast.Node) ast.Visitor {
65+
switch n := node.(type) {
66+
case *ast.SelectorExpr:
67+
x, ok := n.X.(*ast.Ident)
68+
if ok && x.Name == "C" {
69+
return nil
70+
}
71+
case *ast.Ident:
72+
if !n.IsExported() {
73+
break
74+
}
75+
n.Name = renameIdent(n.Name)
76+
}
77+
return v
78+
}
79+
80+
func main() {
81+
if len(os.Args) == 1 {
82+
fmt.Println("usage: rename files")
83+
return
84+
}
85+
86+
for _, fname := range os.Args[1:] {
87+
fset := token.NewFileSet()
88+
file, err := parser.ParseFile(fset, fname, nil, parser.ParseComments)
89+
if err != nil {
90+
fmt.Println(err)
91+
return
92+
}
93+
94+
//ast.Print(fset, file)
95+
96+
v := &Visitor{fset: fset, file: file}
97+
for _, decl := range file.Decls {
98+
ast.Walk(v, decl)
99+
}
100+
101+
out, err := os.Create(fname)
102+
if err != nil {
103+
fmt.Println(err)
104+
return
105+
}
106+
printer.Fprint(out, fset, file)
107+
out.Close()
108+
}
109+
}

0 commit comments

Comments
 (0)