@@ -69,18 +69,29 @@ func Gopstyle(file *ast.File) {
6969 formatFile (file )
7070}
7171
72+ type ClassConfig struct {
73+ PkgPath string // Go+ class project pkgpath, empty if normal .gox class. (optional)
74+ ClassName string // project or class name.
75+ Project bool // true means ClassName is project.
76+ Comments bool // true means parse comments.
77+ Gopt map [string ]string // Gopt_ function name mapping. (optional)
78+ Overload map [string ]string // Overload function name mapping. (optional)
79+ }
80+
7281// GopClassSource format Go+ source to Go+ class source.
73- // pkg set class project pkg path, emtpy if normal .gox class.
74- // class set the class name. proj indication project or class.
75- func GopClassSource (src []byte , pkg string , class string , proj bool , filename ... string ) (ret []byte , err error ) {
82+ func GopClassSource (src []byte , cfg * ClassConfig , filename ... string ) (ret []byte , err error ) {
7683 var fname string
7784 if filename != nil {
7885 fname = filename [0 ]
7986 }
8087 fset := token .NewFileSet ()
88+ mode := parser .AllErrors
89+ if cfg .Comments {
90+ mode |= parser .ParseComments
91+ }
8192 var f * ast.File
82- if f , err = parser .ParseFile (fset , fname , src , parser . AllErrors ); err == nil {
83- GopClass (f , pkg , class , proj )
93+ if f , err = parser .ParseFile (fset , fname , src , mode ); err == nil {
94+ GopClass (f , cfg )
8495 var buf bytes.Buffer
8596 if err = format .Node (& buf , fset , f ); err == nil {
8697 ret = buf .Bytes ()
@@ -90,8 +101,8 @@ func GopClassSource(src []byte, pkg string, class string, proj bool, filename ..
90101}
91102
92103// GopClass format ast.File to Go+ class
93- func GopClass (file * ast.File , pkg string , class string , proj bool ) {
94- formatClass (file , pkg , class , proj )
104+ func GopClass (file * ast.File , cfg * ClassConfig ) {
105+ formatClass (file , cfg )
95106}
96107
97108func findFuncDecl (decls []ast.Decl , name string ) (int , * ast.FuncDecl ) {
@@ -167,12 +178,11 @@ type importCtx struct {
167178}
168179
169180type formatCtx struct {
170- imports map [string ]* importCtx
171- scope * types.Scope
172- classMode bool //class mode
173- classPkg string //class pkg name
174- className string //this class
175- funcRecv string //this class func recv
181+ imports map [string ]* importCtx
182+ scope * types.Scope
183+ classCfg * ClassConfig
184+ classPkg string //this class pkg name
185+ funcRecv string //this class func recv
176186}
177187
178188func (ctx * formatCtx ) insert (name string ) {
@@ -238,14 +248,13 @@ func formatFile(file *ast.File) {
238248 }
239249}
240250
241- func formatClass (file * ast.File , pkg string , class string , proj bool ) {
251+ func formatClass (file * ast.File , cfg * ClassConfig ) {
242252 var funcs []* ast.FuncDecl
243253 ctx := & formatCtx {
244- imports : make (map [string ]* importCtx ),
245- scope : types .NewScope (nil , token .NoPos , token .NoPos , "" ),
246- classMode : true ,
247- classPkg : path .Base (pkg ),
248- className : class ,
254+ imports : make (map [string ]* importCtx ),
255+ scope : types .NewScope (nil , token .NoPos , token .NoPos , "" ),
256+ classCfg : cfg ,
257+ classPkg : path .Base (cfg .PkgPath ),
249258 }
250259 if file .Name .Name == "main" {
251260 file .NoPkgDecl = true
@@ -257,17 +266,17 @@ func formatClass(file *ast.File, pkg string, class string, proj bool) {
257266 for _ , decl := range file .Decls {
258267 switch v := decl .(type ) {
259268 case * ast.FuncDecl :
260- if isClassFunc (v , class ) {
269+ if isClassFunc (v , cfg . ClassName ) {
261270 v .IsClass = true
262271 switch v .Name .Name {
263272 case "MainEntry" :
264- if proj {
273+ if cfg . Project {
265274 fnEntry = v
266275 file .ShadowEntry = v
267276 continue
268277 }
269278 case "Main" :
270- if ! proj {
279+ if ! cfg . Project {
271280 fnEntry = v
272281 file .ShadowEntry = v
273282 continue
@@ -276,7 +285,7 @@ func formatClass(file *ast.File, pkg string, class string, proj bool) {
276285 case "Classfname" :
277286 v .Shadow = true
278287 }
279- } else if v .Name .Name == "main" && proj {
288+ } else if v .Name .Name == "main" && cfg . Project {
280289 v .Shadow = true
281290 }
282291 case * ast.GenDecl :
@@ -285,10 +294,10 @@ func formatClass(file *ast.File, pkg string, class string, proj bool) {
285294 imports = append (imports , v )
286295 continue
287296 case token .TYPE :
288- if spec , ok := v .Specs [0 ].(* ast.TypeSpec ); ok && spec .Name .Name == class {
297+ if spec , ok := v .Specs [0 ].(* ast.TypeSpec ); ok && spec .Name .Name == cfg . ClassName {
289298 if st , ok := spec .Type .(* ast.StructType ); ok {
290299 for _ , fs := range st .Fields .List {
291- if len (fs .Names ) == 0 && pkg != "" {
300+ if len (fs .Names ) == 0 && cfg . PkgPath != "" {
292301 continue
293302 }
294303 varSpecs = append (varSpecs , & ast.ValueSpec {Names : fs .Names , Type : fs .Type })
@@ -392,7 +401,7 @@ func funcRecv(v *ast.FuncDecl) *ast.Ident {
392401}
393402
394403func formatFuncDecl (ctx * formatCtx , v * ast.FuncDecl ) {
395- if ctx .classMode && isClassFunc (v , ctx .className ) {
404+ if ctx .classCfg != nil && isClassFunc (v , ctx .classCfg . ClassName ) {
396405 v .IsClass = true
397406 if recv := funcRecv (v ); recv != nil {
398407 ctx .funcRecv = recv .Name
0 commit comments