Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopfmt: support classfile #1378

Merged
merged 3 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions cmd/gopfmt/gopfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@ import (

goformat "go/format"

"github.com/goplus/gop"
"github.com/goplus/gop/format"
)

var (
procCnt = 0
walkSubDir = false
extGops = map[string]struct{}{
".go": {},
".gop": {},
".spx": {},
".gmx": {},
}
rootDir = ""
rootDir = ""
)

var (
Expand All @@ -61,7 +56,7 @@ func report(err error) {
os.Exit(2)
}

func processFile(filename string, in io.Reader, out io.Writer) error {
func processFile(filename string, class bool, in io.Reader, out io.Writer) error {
if in == nil {
var err error
in, err = os.Open(filename)
Expand Down Expand Up @@ -118,6 +113,10 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
return err
}

var (
dirMap = make(map[string]func(ext string) (ok bool, class bool))
)

func walk(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -127,10 +126,41 @@ func walk(path string, d fs.DirEntry, err error) error {
}
} else {
// Directories are walked, ignoring non-Gop files.
dir, _ := filepath.Split(path)
fn, ok := dirMap[dir]
if !ok {
if mod, err := gop.LoadMod(path, nil, &gop.Config{DontUpdateGoMod: true}); err == nil {
fn = func(ext string) (ok bool, class bool) {
switch ext {
case ".go", ".gop":
ok = true
case ".spx", "gmx":
ok, class = true, true
default:
_, class = mod.IsClass(ext)
if class {
ok = true
}
}
return
}
} else {
fn = func(ext string) (ok bool, class bool) {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", "gmx":
ok, class = true, true
}
return
}
}
dirMap[dir] = fn
}
ext := filepath.Ext(path)
if _, ok := extGops[ext]; ok {
if ok, class := fn(ext); ok {
procCnt++
if err = processFile(path, nil, os.Stdout); err != nil {
if err = processFile(path, class, nil, os.Stdout); err != nil {
report(err)
}
}
Expand All @@ -148,7 +178,7 @@ func main() {
report(fmt.Errorf("error: cannot use -w with standard input"))
return
}
if err := processFile("<standard input>", os.Stdin, os.Stdout); err != nil {
if err := processFile("<standard input>", false, os.Stdin, os.Stdout); err != nil {
report(err)
}
return
Expand Down
51 changes: 41 additions & 10 deletions cmd/internal/gopfmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"path/filepath"
"strings"

"github.com/goplus/gop"

"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/format"

Expand Down Expand Up @@ -57,16 +59,10 @@ func init() {
var (
procCnt = 0
walkSubDir = false
extGops = map[string]struct{}{
".go": {},
".gop": {},
".spx": {},
".gmx": {},
}
rootDir = ""
rootDir = ""
)

func gopfmt(path string, smart, mvgo bool) (err error) {
func gopfmt(path string, class, smart, mvgo bool) (err error) {
src, err := ioutil.ReadFile(path)
if err != nil {
return
Expand Down Expand Up @@ -127,6 +123,10 @@ func writeFileWithBackup(path string, target []byte) (err error) {
return os.Rename(tmpfile, path)
}

var (
dirMap = make(map[string]func(ext string) (ok bool, class bool))
visualfc marked this conversation as resolved.
Show resolved Hide resolved
)

func walk(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -135,15 +135,46 @@ func walk(path string, d fs.DirEntry, err error) error {
return filepath.SkipDir
}
} else {
dir, _ := filepath.Split(path)
fn, ok := dirMap[dir]
if !ok {
if mod, err := gop.LoadMod(path, nil, &gop.Config{DontUpdateGoMod: true}); err == nil {
fn = func(ext string) (ok bool, class bool) {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", "gmx":
visualfc marked this conversation as resolved.
Show resolved Hide resolved
ok, class = true, true
default:
_, class = mod.IsClass(ext)
if class {
ok = true
}
}
return
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need use if..else

fn = func(ext string) (ok bool, class bool) {
switch ext {
case ".go", ".gop":
ok = true
case ".gopx", ".spx", "gmx":
visualfc marked this conversation as resolved.
Show resolved Hide resolved
ok, class = true, true
}
return
}
}
dirMap[dir] = fn
}
ext := filepath.Ext(path)
smart := *flagSmart
mvgo := smart && *flagMoveGo
if _, ok := extGops[ext]; ok && (!mvgo || ext == ".go") {
if ok, class := fn(ext); ok && (!mvgo || ext == ".go") {
procCnt++
if *flagNotExec {
fmt.Println("gop fmt", path)
} else {
err = gopfmt(path, smart && (mvgo || ext != ".go"), mvgo)
err = gopfmt(path, class, smart && (mvgo || ext != ".go"), mvgo)
if err != nil {
report(err)
}
Expand Down
6 changes: 3 additions & 3 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NotFound(err error) bool {
return errors.Err(err) == syscall.ENOENT
}

func loadMod(dir string, gop *env.Gop, conf *Config) (mod *gopmod.Module, err error) {
func LoadMod(dir string, gop *env.Gop, conf *Config) (mod *gopmod.Module, err error) {
mod, err = gopmod.Load(dir, 0)
if err != nil && !NotFound(err) {
err = errors.NewWith(err, `gopmod.Load(dir, 0`, -2, "gopmod.Load", dir, 0)
visualfc marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -101,7 +101,7 @@ func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (ou
if gop == nil {
gop = gopenv.Get()
}
mod, err := loadMod(dir, gop, conf)
mod, err := LoadMod(dir, gop, conf)
if err != nil {
return
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func LoadFiles(files []string, conf *Config) (out *gox.Package, err error) {
if gop == nil {
gop = gopenv.Get()
}
mod, err := loadMod("", gop, conf)
mod, err := LoadMod("", gop, conf)
if err != nil {
err = errors.NewWith(err, `loadMod("", gop, conf)`, -2, "gop.loadMod", "", gop, conf)
return
Expand Down