Skip to content

Commit

Permalink
Merge pull request AllenDang#2 from neclepsio/idiomatic
Browse files Browse the repository at this point in the history
Add idiomatic bindings
  • Loading branch information
neclepsio authored Jan 9, 2023
2 parents 5edd1fc + dbab3a7 commit ecf9498
Show file tree
Hide file tree
Showing 407 changed files with 189,567 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ compile_cimgui_macos:
cd ./cimgui/build; make
cp -f ./cimgui/build/cimgui.a ./lib/macos/arm64/

## generate idiomatic bindings
.PHONY: idiomatic
idiomatic:
@echo "Generating idiomatic bindings"
rm -rf ./idiomatic
mkdir ./idiomatic
mkdir ./idiomatic/cimgui
mkdir ./idiomatic/cimplot
mkdir ./idiomatic/thirdparty
mkdir ./idiomatic/thirdparty/glfw

cp ./*.go ./*.cpp ./*.h ./idiomatic
cp ./cimgui/*.h ./cimgui/*.cpp ./idiomatic/cimgui
cp ./cimplot/*.h ./cimplot/*.cpp ./idiomatic/cimplot
cp -r ./thirdparty/glfw/* ./idiomatic/thirdparty/glfw

go run github.com/AllenDang/cimgui-go/cmd/rename ./idiomatic/*.go

## generate: generates both bindings (equal to `all`)
.PHONY: generate
generate: cimgui cimplot
109 changes: 109 additions & 0 deletions cmd/rename/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"strings"
"unicode"
"unicode/utf8"
)

type Visitor struct {
fset *token.FileSet
file *ast.File
}

var replace = map[string]string{
"GetDrawData": "CurrentDrawData",
"GetDrawListSharedData": "CurrentDrawListSharedData",
"GetFont": "CurrentFont",
"GetIO": "CurrentIO",
"GetPlatformIO": "CurrentPlatformIO",
"GetStyle": "CurrentStyle",
"GetMouseCursor": "CurrentMouseCursor",
"ImAxis": "AxisName",
"GetDrawCursor": "Cursor",
"SetDrawCursor": "SetCursor",
}

func removeImGuiIm(n string) string {
old := n
if strings.HasPrefix(n, "ImGui") {
n = strings.TrimPrefix(n, "ImGui")
} else if strings.HasPrefix(n, "Im") {
n = strings.TrimPrefix(n, "Im")
}
c, _ := utf8.DecodeRuneInString(n)
if !unicode.IsUpper(c) {
n = old
}
return n
}

func renameIdent(n string) string {
if r, ok := replace[n]; ok {
n = r
}
n = removeImGuiIm(n)
if strings.HasPrefix(n, "New") {
n = "New" + removeImGuiIm(n[3:])
} else if strings.HasPrefix(n, "new") {
n = "new" + removeImGuiIm(n[3:])
}
n = strings.TrimPrefix(n, "Get")
if n != "_" {
n = strings.ReplaceAll(n, "_", "")
}
return n
}

func (v *Visitor) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.SelectorExpr:
x, ok := n.X.(*ast.Ident)
if ok && x.Name == "C" {
return nil
}
case *ast.Ident:
if !n.IsExported() {
break
}
n.Name = renameIdent(n.Name)
}
return v
}

func main() {
if len(os.Args) == 1 {
fmt.Println("usage: rename files")
return
}

for _, fname := range os.Args[1:] {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, fname, nil, parser.ParseComments)
if err != nil {
fmt.Println(err)
return
}

//ast.Print(fset, file)

v := &Visitor{fset: fset, file: file}
for _, decl := range file.Decls {
ast.Walk(v, decl)
}

out, err := os.Create(fname)
if err != nil {
fmt.Println(err)
return
}
printer.Fprint(out, fset, file)
out.Close()
}
}
Loading

0 comments on commit ecf9498

Please sign in to comment.