Skip to content

Commit

Permalink
Merge pull request #370 from xushiwei/q
Browse files Browse the repository at this point in the history
markGopPkg for Gopx_XXX, Gopt_XXX, Gopo_XXX
  • Loading branch information
xushiwei authored Feb 8, 2024
2 parents d683e9c + 4ebcefe commit 31705c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
11 changes: 11 additions & 0 deletions gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func newGopMainPackage() *gox.Package {

// ----------------------------------------------------------------------------

func TestGopoConst(t *testing.T) {
pkg := newMainPackage()
pkg.CB().NewConstStart(nil, "Gopo_x").
Val("Hello").EndInit(1)
domTest(t, pkg, `package main
const GopPackage = true
const Gopo_x = "Hello"
`)
}

func TestFmtPrintln(t *testing.T) {
pkg := newGopMainPackage()
pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
Expand Down
11 changes: 8 additions & 3 deletions gow.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package gox

import (
"fmt"
"go/ast"
"go/token"
"go/types"
Expand Down Expand Up @@ -87,6 +86,12 @@ func (p *Package) WriteTo(dst io.Writer, fname ...string) (err error) {
return format.Node(dst, fset, file)
}

// GeneratedHeader is the default string that the source file generated by gox start with.
// Change it if you want to make it different.
var (
GeneratedHeader = "// Code generated by gox; DO NOT EDIT.\n\n"
)

// WriteFile writes a file named fname.
// If fname is not provided, it writes the default (NOT current) file.
func (p *Package) WriteFile(file string, fname ...string) (err error) {
Expand All @@ -108,8 +113,8 @@ func (p *Package) WriteFile(file string, fname ...string) (err error) {
os.Remove(file)
}
}()
if len(GeneratedHeader) > 0 {
fmt.Fprint(f, GeneratedHeader)
if GeneratedHeader != "" {
f.WriteString(GeneratedHeader)
}
fset := token.NewFileSet()
return format.Node(f, fset, ast)
Expand Down
6 changes: 1 addition & 5 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ func isGopoConst(name string) bool {
}

func isGopFunc(name string) bool {
return isOverload(name) || isGoptFunc(name)
}

func isGoptFunc(name string) bool {
return strings.HasPrefix(name, goptPrefix)
return isOverload(name) || strings.HasPrefix(name, goptPrefix) || strings.HasPrefix(name, gopxPrefix)
}

func isOverload(name string) bool {
Expand Down
4 changes: 0 additions & 4 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ func fatal(msg string) {
panic(fatalMsg(msg))
}

// GeneratedHeader is the default string that the source file generated by gox start with.
// Change it if you want to make it different.
const GeneratedHeader = "// Code generated by gox; DO NOT EDIT.\n\n"

// ----------------------------------------------------------------------------

// Recorder represents a gox event recorder.
Expand Down
2 changes: 2 additions & 0 deletions type_var_and_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ func (p *Package) newValueDecl(
stmt := &ast.AssignStmt{Tok: token.DEFINE, Lhs: nameIdents}
at := p.cb.startStmtAt(stmt)
return &ValueDecl{names: names, tok: tok, pos: pos, scope: scope, vals: &stmt.Rhs, at: at}
} else if tok == token.CONST && len(names) == 1 && isGopoConst(names[0]) { // Gopo_XXX
p.isGopPkg = true
}
// var a, b = expr
// const a, b = expr
Expand Down

0 comments on commit 31705c4

Please sign in to comment.