From 2badcfde5b601c06d7a4df7f7f42c97842aaaeb8 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Fri, 27 Sep 2024 22:49:47 +0800 Subject: [PATCH 1/4] init commentidy --- chore/cmp/cmp.go | 92 +++++++++++++ chore/commentidy/commentidy.go | 36 +++++ chore/commentidy/tidy/tidy.go | 126 ++++++++++++++++++ chore/commentidy/tidy/tidy_test.go | 38 ++++++ .../tidy/tidy_test/_input_file/foo.h | 3 + chore/commentidy/tidy/tidy_test/tidy_test.go | 70 ++++++++++ go.mod | 1 + go.sum | 2 + 8 files changed, 368 insertions(+) create mode 100644 chore/cmp/cmp.go create mode 100644 chore/commentidy/commentidy.go create mode 100644 chore/commentidy/tidy/tidy.go create mode 100644 chore/commentidy/tidy/tidy_test.go create mode 100644 chore/commentidy/tidy/tidy_test/_input_file/foo.h create mode 100644 chore/commentidy/tidy/tidy_test/tidy_test.go diff --git a/chore/cmp/cmp.go b/chore/cmp/cmp.go new file mode 100644 index 000000000..d6cf79ba0 --- /dev/null +++ b/chore/cmp/cmp.go @@ -0,0 +1,92 @@ +package cmp + +import ( + "bufio" + "bytes" + "strings" + "unicode" + + "github.com/google/go-cmp/cmp" +) + +func skipSpace(data []byte, from int) int { + dataBuf := bytes.NewBuffer(data[from:]) + index := from + for { + rn, sz, err := dataBuf.ReadRune() + if err != nil { + break + } + if !unicode.IsSpace(rn) { + break + } + index = index + sz + } + return index +} + +func SplitLineIgnoreSpaceBytes(b []byte) []string { + buf := bytes.NewBuffer(b) + scan := bufio.NewScanner(buf) + results := make([]string, 0) + for scan.Scan() { + lineText := scan.Text() + lineTextBuf := bytes.NewBufferString(lineText) + lineTextScan := bufio.NewScanner(lineTextBuf) + lineTextScan.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { + if atEOF && len(data) == 0 { + return 0, nil, nil + } + if i := bytes.IndexAny(data, " \t"); i >= 0 { + ii := skipSpace(data, i+1) + return ii, data[0:i], nil + } + if atEOF { + return len(data), data, nil + } + return 0, nil, nil + }) + + strBuilder := strings.Builder{} + writeSpace := false + total := 0 + for lineTextScan.Scan() { + word := lineTextScan.Text() + if writeSpace { + strBuilder.WriteRune(' ') + } + n, err := strBuilder.WriteString(word) + if err != nil { + break + } + total += n + writeSpace = total > 0 + } + if total > 0 { + results = append(results, strBuilder.String()) + } + } + return results +} + +func SplitLineIgnoreSpaceString(s string) []string { + return SplitLineIgnoreSpaceBytes([]byte(s)) +} + +func EqualBytesIgnoreSpace(b1 []byte, b2 []byte) (bool, string) { + arr1 := SplitLineIgnoreSpaceBytes(b1) + arr2 := SplitLineIgnoreSpaceBytes(b2) + if !cmp.Equal(arr1, arr2) { + return false, cmp.Diff(arr1, arr2) + } + return true, "" +} + +func EqualStringIgnoreSpace(s1 string, s2 string) (bool, string) { + arr1 := SplitLineIgnoreSpaceString(s1) + arr2 := SplitLineIgnoreSpaceString(s2) + if !cmp.Equal(arr1, arr2) { + return false, cmp.Diff(arr1, arr2) + } + return true, "" +} diff --git a/chore/commentidy/commentidy.go b/chore/commentidy/commentidy.go new file mode 100644 index 000000000..180fcd352 --- /dev/null +++ b/chore/commentidy/commentidy.go @@ -0,0 +1,36 @@ +package main + +import ( + "bytes" + "flag" + "fmt" + "os" + + "github.com/goplus/llgo/chore/commentidy/tidy" +) + +func mainUsage() { + fmt.Fprintln(os.Stderr, "commentidy [filepath]") + os.Exit(2) +} + +func init() { + flag.Usage = mainUsage +} + +func main() { + flag.Parse() + args := flag.Args() + if len(args) < 1 { + flag.Usage() + } + if args[0] == "help" { + flag.Usage() + return + } + file := args[0] + commentidy := tidy.NewCommentidy() + outBytes, _ := commentidy.TidyFile(file) + buf := bytes.NewBuffer(outBytes) + buf.WriteTo(os.Stdout) +} diff --git a/chore/commentidy/tidy/tidy.go b/chore/commentidy/tidy/tidy.go new file mode 100644 index 000000000..a696ae170 --- /dev/null +++ b/chore/commentidy/tidy/tidy.go @@ -0,0 +1,126 @@ +package tidy + +import ( + "bufio" + "bytes" + "fmt" + "os" +) + +type Commentidy struct { +} + +func NewCommentidy() *Commentidy { + return &Commentidy{} +} + +func (p *Commentidy) TidyFile(filename string) ([]byte, error) { + b, err := os.ReadFile(filename) + if err != nil { + return nil, err + } + return p.TidyBytes(b) +} + +func (p *Commentidy) skipString(data []byte, from int, q rune) int { + dataBuf := bytes.NewBuffer(data[from:]) + index := from + for { + rn, sz, err := dataBuf.ReadRune() + if err != nil { + break + } + if q == rn { + break + } + index = index + sz + } + return index +} + +func (p *Commentidy) readRuneAt(data []byte, from int) (rune, int, error) { + dataBuf := bytes.NewBuffer(data[from:]) + return dataBuf.ReadRune() +} + +func (p *Commentidy) readRuneAtAny(data []byte, from int, runes []rune) (rune, int, error) { + dataBuf := bytes.NewBuffer(data[from:]) + rn, sz, err := dataBuf.ReadRune() + if err != nil { + return rn, sz, err + } + for _, r := range runes { + if rn == r { + return rn, sz, nil + } + } + return rn, sz, fmt.Errorf("%s", "not comment char") +} + +func (p *Commentidy) TidyBytes(b []byte) ([]byte, error) { + buf := bytes.NewBuffer(b) + scann := bufio.NewScanner(buf) + scann.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { + if atEOF && len(data) == 0 { + return 0, nil, nil + } + // find slash + if i := bytes.IndexAny(data, "/\"'`"); i >= 0 { + iRune, iSize, _ := p.readRuneAt(data, i) + if iRune == '/' { // handle start with '/' + iSlashNext := i + iSize + nextRune, nextSize, nextError := p.readRuneAtAny(data, iSlashNext, []rune{'/', '*'}) + if nextError != nil { + return iSlashNext, data[0:iSlashNext], nil + } + if nextRune == '/' { // + iSlashSlashNext := iSlashNext + nextSize + // 1. translate // to /// + _, sz, err := p.readRuneAtAny(data, iSlashSlashNext, []rune{'/'}) + if err == nil { /// + return iSlashSlashNext + sz, data[0 : iSlashSlashNext+sz], nil + } else { // + buf := bytes.Buffer{} + buf.Write(data[0:iSlashSlashNext]) + buf.WriteRune('/') + return iSlashSlashNext, buf.Bytes(), nil + } + } else if nextRune == '*' { // /* + // 2. translate /* to /** + iSlashStarNext := iSlashNext + nextSize + _, sz, err := p.readRuneAtAny(data, iSlashStarNext, []rune{'*'}) + if err == nil { // /** + return iSlashStarNext + sz, data[0 : iSlashStarNext+sz], nil + } else { // /* + buf := bytes.Buffer{} + buf.Write(data[0:iSlashStarNext]) + buf.WriteRune('*') + return iSlashStarNext, buf.Bytes(), nil + } + } else { + return iSlashNext + nextSize, data[0 : iSlashNext+nextSize], nil + } + } else { + // skip string + iEndQuite := p.skipString(data, i+1, iRune) + return iEndQuite + 1, data[0 : iEndQuite+1], nil + } + } + // If we're at EOF, we have a final, non-terminated line. Return it. + if atEOF { + return len(data), data, nil + } + // Request more data. + return 0, nil, nil + + }) + outBuf := bytes.Buffer{} + for scann.Scan() { + text := scann.Text() + _, err := outBuf.WriteString(text) + if err != nil { + return outBuf.Bytes(), err + } + } + return outBuf.Bytes(), nil +} diff --git a/chore/commentidy/tidy/tidy_test.go b/chore/commentidy/tidy/tidy_test.go new file mode 100644 index 000000000..43c2ec96a --- /dev/null +++ b/chore/commentidy/tidy/tidy_test.go @@ -0,0 +1,38 @@ +package tidy_test + +import ( + "bytes" + "os" + "testing" + + "github.com/goplus/llgo/chore/commentidy/tidy" +) + +func TestTidySlashSlash(t *testing.T) { + commentidy := tidy.NewCommentidy() + code := ` + //good + ///good + int foo(int a, int b) string { + return "///abcdef" + } + ` + outBytes, _ := commentidy.TidyBytes([]byte(code)) + buf := bytes.NewBuffer(outBytes) + buf.WriteTo(os.Stdout) +} + +func TestTidySlashStar(t *testing.T) { + commentidy := tidy.NewCommentidy() + code := ` + /* */ + /*good + ///good + int foo(int a, int b) string { + return "/*abcdef*/" + } + ` + outBytes, _ := commentidy.TidyBytes([]byte(code)) + buf := bytes.NewBuffer(outBytes) + buf.WriteTo(os.Stdout) +} diff --git a/chore/commentidy/tidy/tidy_test/_input_file/foo.h b/chore/commentidy/tidy/tidy_test/_input_file/foo.h new file mode 100644 index 000000000..8562089a7 --- /dev/null +++ b/chore/commentidy/tidy/tidy_test/_input_file/foo.h @@ -0,0 +1,3 @@ +//good +///good +int foo(int a, int b); \ No newline at end of file diff --git a/chore/commentidy/tidy/tidy_test/tidy_test.go b/chore/commentidy/tidy/tidy_test/tidy_test.go new file mode 100644 index 000000000..4a21c76a5 --- /dev/null +++ b/chore/commentidy/tidy/tidy_test/tidy_test.go @@ -0,0 +1,70 @@ +package tidy_test + +import ( + "testing" + + "github.com/goplus/llgo/chore/cmp" + "github.com/goplus/llgo/chore/commentidy/tidy" +) + +func TestTidySlashSlash(t *testing.T) { + code := ` + //good + ///good + int foo(int a, int b) string { + return "///abcdef" + } + ` + expected := ` + ///good + ///good + int foo(int a, int b) string { + return "///abcdef" + } + ` + commentidy := tidy.NewCommentidy() + outBytes, _ := commentidy.TidyBytes([]byte(code)) + compare(t, outBytes, expected) +} + +func TestTidySlashStar(t *testing.T) { + code := ` + /* */ + /*good + ///good + int foo(int a, int b) string { + return "/*abcdef*/" + } + ` + expected := ` + /** */ + /**good + ///good + int foo(int a, int b) string { + return "/*abcdef*/" + } + ` + commentidy := tidy.NewCommentidy() + outBytes, _ := commentidy.TidyBytes([]byte(code)) + compare(t, outBytes, expected) +} + +func TestTidyFile(t *testing.T) { + expected := ` + ///good + ///good + int foo(int a, int b); + ` + filename := "./_input_file/foo.h" + commentidy := tidy.NewCommentidy() + outBytes, _ := commentidy.TidyFile(filename) + compare(t, outBytes, expected) +} + +func compare(t *testing.T, outBytes []byte, expectedString string) { + eq, diff := cmp.EqualBytesIgnoreSpace(outBytes, []byte(expectedString)) + t.Helper() + if !eq { + t.Error(diff) + } +} diff --git a/go.mod b/go.mod index a8277daf5..70dd9cfea 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/goplus/llgo go 1.20 require ( + github.com/google/go-cmp v0.6.0 github.com/goplus/gogen v1.16.0 github.com/goplus/llvm v0.8.0 github.com/goplus/mod v0.13.12 diff --git a/go.sum b/go.sum index 649c5d8c9..21deadeb4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/goplus/gogen v1.16.0 h1:hAK2ZX8vCjH+Y2QoJl9viSZ8Gw9pzE0vCz5voYBYnv4= github.com/goplus/gogen v1.16.0/go.mod h1:92qEzVgv7y8JEFICWG9GvYI5IzfEkxYdsA1DbmnTkqk= github.com/goplus/llvm v0.8.0 h1:9eFutGm3d0G7bAd8/e+Tuyva0tu1IPNPm0kVG4AgHwY= From e6c16eda5d4cc2d9db303c4a7eeff88130b8dd02 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Fri, 27 Sep 2024 22:57:17 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Change=20to=20only=20skip=20"=20style=20str?= =?UTF-8?q?ings=E2=80=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chore/commentidy/tidy/tidy.go | 2 +- chore/commentidy/tidy/tidy_test.go | 38 ------------------------------ 2 files changed, 1 insertion(+), 39 deletions(-) delete mode 100644 chore/commentidy/tidy/tidy_test.go diff --git a/chore/commentidy/tidy/tidy.go b/chore/commentidy/tidy/tidy.go index a696ae170..94656bc68 100644 --- a/chore/commentidy/tidy/tidy.go +++ b/chore/commentidy/tidy/tidy.go @@ -65,7 +65,7 @@ func (p *Commentidy) TidyBytes(b []byte) ([]byte, error) { return 0, nil, nil } // find slash - if i := bytes.IndexAny(data, "/\"'`"); i >= 0 { + if i := bytes.IndexAny(data, "/\""); i >= 0 { iRune, iSize, _ := p.readRuneAt(data, i) if iRune == '/' { // handle start with '/' iSlashNext := i + iSize diff --git a/chore/commentidy/tidy/tidy_test.go b/chore/commentidy/tidy/tidy_test.go deleted file mode 100644 index 43c2ec96a..000000000 --- a/chore/commentidy/tidy/tidy_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package tidy_test - -import ( - "bytes" - "os" - "testing" - - "github.com/goplus/llgo/chore/commentidy/tidy" -) - -func TestTidySlashSlash(t *testing.T) { - commentidy := tidy.NewCommentidy() - code := ` - //good - ///good - int foo(int a, int b) string { - return "///abcdef" - } - ` - outBytes, _ := commentidy.TidyBytes([]byte(code)) - buf := bytes.NewBuffer(outBytes) - buf.WriteTo(os.Stdout) -} - -func TestTidySlashStar(t *testing.T) { - commentidy := tidy.NewCommentidy() - code := ` - /* */ - /*good - ///good - int foo(int a, int b) string { - return "/*abcdef*/" - } - ` - outBytes, _ := commentidy.TidyBytes([]byte(code)) - buf := bytes.NewBuffer(outBytes) - buf.WriteTo(os.Stdout) -} From 63fca8c9cd0e978a9af65aa3dd7d0f709decf4d2 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Fri, 27 Sep 2024 23:12:45 +0800 Subject: [PATCH 3/4] add TidyDir & TidyDirWithFilter and test it --- chore/commentidy/commentidy.go | 14 +- chore/commentidy/tidy/tidy.go | 128 ++++++++++++++++-- .../tidy/tidy_test/_input_file/foo.h | 19 +++ chore/commentidy/tidy/tidy_test/tidy_test.go | 65 ++++++++- 4 files changed, 206 insertions(+), 20 deletions(-) diff --git a/chore/commentidy/commentidy.go b/chore/commentidy/commentidy.go index 180fcd352..94567cdd8 100644 --- a/chore/commentidy/commentidy.go +++ b/chore/commentidy/commentidy.go @@ -30,7 +30,15 @@ func main() { } file := args[0] commentidy := tidy.NewCommentidy() - outBytes, _ := commentidy.TidyFile(file) - buf := bytes.NewBuffer(outBytes) - buf.WriteTo(os.Stdout) + fileInfo, err := os.Stat(file) + if os.IsNotExist(err) { + panic(err) + } + if fileInfo.IsDir() { + commentidy.TidyDir(file, ".h", ".hpp") + } else { + outBytes, _ := commentidy.TidyFile(file) + buf := bytes.NewBuffer(outBytes) + buf.WriteTo(os.Stdout) + } } diff --git a/chore/commentidy/tidy/tidy.go b/chore/commentidy/tidy/tidy.go index 94656bc68..b32b12d06 100644 --- a/chore/commentidy/tidy/tidy.go +++ b/chore/commentidy/tidy/tidy.go @@ -4,7 +4,10 @@ import ( "bufio" "bytes" "fmt" + "io/fs" "os" + "path/filepath" + "strings" ) type Commentidy struct { @@ -22,28 +25,42 @@ func (p *Commentidy) TidyFile(filename string) ([]byte, error) { return p.TidyBytes(b) } -func (p *Commentidy) skipString(data []byte, from int, q rune) int { +func (p *Commentidy) skipToAny(data []byte, from int, runes ...rune) (_pos int, _rn rune, _sz int, _err error) { + if from >= len(data) { + return from, rune(0), from, fmt.Errorf("%s", "index out of bound") + } dataBuf := bytes.NewBuffer(data[from:]) index := from + var rn rune = rune(0) + var sz int = 0 + var err error = nil for { - rn, sz, err := dataBuf.ReadRune() + rn, sz, err = dataBuf.ReadRune() if err != nil { break } - if q == rn { - break + for _, q := range runes { + if q == rn { + return index, rn, sz, nil + } } index = index + sz } - return index + return index, rn, sz, fmt.Errorf("%s", "not found") } func (p *Commentidy) readRuneAt(data []byte, from int) (rune, int, error) { + if from >= len(data) { + return rune(0), 0, fmt.Errorf("%s", "index out of bound") + } dataBuf := bytes.NewBuffer(data[from:]) return dataBuf.ReadRune() } -func (p *Commentidy) readRuneAtAny(data []byte, from int, runes []rune) (rune, int, error) { +func (p *Commentidy) readRuneAtAny(data []byte, from int, runes ...rune) (rune, int, error) { + if from >= len(data) { + return rune(0), 0, fmt.Errorf("%s", "index out of bound") + } dataBuf := bytes.NewBuffer(data[from:]) rn, sz, err := dataBuf.ReadRune() if err != nil { @@ -61,7 +78,8 @@ func (p *Commentidy) TidyBytes(b []byte) ([]byte, error) { buf := bytes.NewBuffer(b) scann := bufio.NewScanner(buf) scann.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { - if atEOF && len(data) == 0 { + lenData := len(data) + if atEOF && lenData == 0 { return 0, nil, nil } // find slash @@ -69,32 +87,67 @@ func (p *Commentidy) TidyBytes(b []byte) ([]byte, error) { iRune, iSize, _ := p.readRuneAt(data, i) if iRune == '/' { // handle start with '/' iSlashNext := i + iSize - nextRune, nextSize, nextError := p.readRuneAtAny(data, iSlashNext, []rune{'/', '*'}) + nextRune, nextSize, nextError := p.readRuneAtAny(data, iSlashNext, '/', '*') if nextError != nil { return iSlashNext, data[0:iSlashNext], nil } if nextRune == '/' { // iSlashSlashNext := iSlashNext + nextSize - // 1. translate // to /// - _, sz, err := p.readRuneAtAny(data, iSlashSlashNext, []rune{'/'}) + // translate // to /// + _, sz, err := p.readRuneAtAny(data, iSlashSlashNext, '/') + newLinePos, _, _, _ := p.skipToAny(data, iSlashSlashNext, '\n') if err == nil { /// - return iSlashSlashNext + sz, data[0 : iSlashSlashNext+sz], nil + // skip line + iAdvance := iSlashSlashNext + sz + if newLinePos >= 0 { + // find new line + iAdvance = newLinePos + } + return iAdvance, data[0:iAdvance], nil } else { // + // insert '/' then skip line buf := bytes.Buffer{} buf.Write(data[0:iSlashSlashNext]) buf.WriteRune('/') + if newLinePos >= 0 { + //find new line + newLinePosNext := newLinePos + 1 + if newLinePosNext > lenData { + newLinePosNext = lenData + } + buf.Write(data[iSlashSlashNext:newLinePosNext]) + return newLinePosNext, buf.Bytes(), nil + } return iSlashSlashNext, buf.Bytes(), nil } } else if nextRune == '*' { // /* - // 2. translate /* to /** + // translate /* to /** iSlashStarNext := iSlashNext + nextSize - _, sz, err := p.readRuneAtAny(data, iSlashStarNext, []rune{'*'}) + _, sz, err := p.readRuneAtAny(data, iSlashStarNext, '*') if err == nil { // /** return iSlashStarNext + sz, data[0 : iSlashStarNext+sz], nil } else { // /* + // insert '*' buf := bytes.Buffer{} buf.Write(data[0:iSlashStarNext]) buf.WriteRune('*') + //skip to '/' + skipLoop: + posSlashRune, _, _, err := p.skipToAny(data, iSlashStarNext, '/') + if err == nil { + // if find '/' + runePrev, _, errRunePrev := p.readRuneAt(data, posSlashRune-1) + if errRunePrev == nil && runePrev == nextRune { + iEnd := posSlashRune + 1 + // write to iEnd + buf.Write(data[iSlashStarNext:iEnd]) + return posSlashRune + 1, buf.Bytes(), nil + } else { + buf.Write(data[iSlashStarNext : posSlashRune+1]) + iSlashStarNext = posSlashRune + 1 + goto skipLoop + } + } return iSlashStarNext, buf.Bytes(), nil } } else { @@ -102,8 +155,12 @@ func (p *Commentidy) TidyBytes(b []byte) ([]byte, error) { } } else { // skip string - iEndQuite := p.skipString(data, i+1, iRune) - return iEndQuite + 1, data[0 : iEndQuite+1], nil + iEndQuite, _, _, _ := p.skipToAny(data, i+1, iRune) + iAdvance := iEndQuite + 1 + if iAdvance >= lenData { + iAdvance = lenData + } + return iAdvance, data[0:iAdvance], nil } } // If we're at EOF, we have a final, non-terminated line. Return it. @@ -124,3 +181,44 @@ func (p *Commentidy) TidyBytes(b []byte) ([]byte, error) { } return outBuf.Bytes(), nil } + +func (p *Commentidy) needDoTidy(path string, exts ...string) bool { + doTidy := false + if !strings.HasSuffix(path, ".out.h") { + fileExt := filepath.Ext(path) + for _, ext := range exts { + if ext == fileExt { + doTidy = true + break + } + } + } + return doTidy +} + +func (p *Commentidy) TidyDir(dir string, exts ...string) error { + return p.TidyDirWithFilter(dir, func(path string, d fs.DirEntry, err error) bool { + return !p.needDoTidy(path, exts...) + }) +} + +func (p *Commentidy) TidyDirWithFilter(dir string, fnFilter func(path string, d fs.DirEntry, err error) bool) error { + _, err := os.Stat(dir) + if os.IsNotExist(err) { + return err + } + filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if !d.IsDir() { + if !fnFilter(path, d, err) { + outBytes, _ := p.TidyFile(path) + outFile := path + ".out.h" + os.WriteFile(outFile, outBytes, 0644) + } + } + return nil + }) + return nil +} diff --git a/chore/commentidy/tidy/tidy_test/_input_file/foo.h b/chore/commentidy/tidy/tidy_test/_input_file/foo.h index 8562089a7..b483cb7d4 100644 --- a/chore/commentidy/tidy/tidy_test/_input_file/foo.h +++ b/chore/commentidy/tidy/tidy_test/_input_file/foo.h @@ -1,3 +1,22 @@ +/// +/// //good ///good +int foo(int a, int b); + +//good +///good +int foo(int a, int b) string { + return "///abcdef" +} + +/* */ +/*good +///good +int foo(int a, int b) string { + return "/*abcdef*/ +} + +///good +///good int foo(int a, int b); \ No newline at end of file diff --git a/chore/commentidy/tidy/tidy_test/tidy_test.go b/chore/commentidy/tidy/tidy_test/tidy_test.go index 4a21c76a5..bb8b44dab 100644 --- a/chore/commentidy/tidy/tidy_test/tidy_test.go +++ b/chore/commentidy/tidy/tidy_test/tidy_test.go @@ -1,6 +1,10 @@ package tidy_test import ( + "io/fs" + "os" + "path/filepath" + "strings" "testing" "github.com/goplus/llgo/chore/cmp" @@ -51,9 +55,28 @@ func TestTidySlashStar(t *testing.T) { func TestTidyFile(t *testing.T) { expected := ` + /// +/// ///good - ///good - int foo(int a, int b); +///good +int foo(int a, int b); + +///good +///good +int foo(int a, int b) string { + return "///abcdef" +} + +/** */ +/**good +///good +int foo(int a, int b) string { + return "/*abcdef*/ +} + +///good +///good +int foo(int a, int b); ` filename := "./_input_file/foo.h" commentidy := tidy.NewCommentidy() @@ -61,6 +84,44 @@ func TestTidyFile(t *testing.T) { compare(t, outBytes, expected) } +func TestTidyDir(t *testing.T) { + commentidy := tidy.NewCommentidy() + err := commentidy.TidyDir("./_input_file", ".h") + if err != nil { + t.Error(err) + } +} + +func TestTidyDirErr(t *testing.T) { + commentidy := tidy.NewCommentidy() + err := commentidy.TidyDir("./_input_file/files", ".h") + if !os.IsNotExist(err) { + t.Error(err) + } +} + +func TestTidyDirWithFilter(t *testing.T) { + commentidy := tidy.NewCommentidy() + err := commentidy.TidyDirWithFilter("./_input_file", func(path string, d fs.DirEntry, err error) bool { + if strings.HasSuffix(d.Name(), ".out.h") { + return true + } + if !d.IsDir() && strings.Contains(d.Name(), "vki") { + return true + } + if filepath.Ext(path) != ".h" { + return true + } + if d.Name() == "foo.h" { + return true + } + return false + }) + if err != nil { + t.Error(err) + } +} + func compare(t *testing.T, outBytes []byte, expectedString string) { eq, diff := cmp.EqualBytesIgnoreSpace(outBytes, []byte(expectedString)) t.Helper() From c18990f1474a62da0b9a647d1d715cdfe6377c98 Mon Sep 17 00:00:00 2001 From: tsingbx Date: Sun, 29 Sep 2024 15:19:36 +0800 Subject: [PATCH 4/4] add clean to remove all .out.h file --- chore/commentidy/commentidy.go | 11 ++++++++++- chore/commentidy/tidy/tidy.go | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/chore/commentidy/commentidy.go b/chore/commentidy/commentidy.go index 94567cdd8..9a39c0480 100644 --- a/chore/commentidy/commentidy.go +++ b/chore/commentidy/commentidy.go @@ -10,7 +10,7 @@ import ( ) func mainUsage() { - fmt.Fprintln(os.Stderr, "commentidy [filepath]") + fmt.Fprintln(os.Stderr, "commentidy [clean] filepath") os.Exit(2) } @@ -28,6 +28,15 @@ func main() { flag.Usage() return } + if args[0] == "clean" { + commentidy := tidy.NewCommentidy() + dir := "." + if len(args) > 1 { + dir = args[1] + } + commentidy.CleanDir(dir) + return + } file := args[0] commentidy := tidy.NewCommentidy() fileInfo, err := os.Stat(file) diff --git a/chore/commentidy/tidy/tidy.go b/chore/commentidy/tidy/tidy.go index b32b12d06..72a63cfc3 100644 --- a/chore/commentidy/tidy/tidy.go +++ b/chore/commentidy/tidy/tidy.go @@ -202,6 +202,19 @@ func (p *Commentidy) TidyDir(dir string, exts ...string) error { }) } +func (p *Commentidy) CleanDir(dir string) error { + filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if strings.HasSuffix(path, ".out.h") { + os.Remove(path) + } + return nil + }) + return nil +} + func (p *Commentidy) TidyDirWithFilter(dir string, fnFilter func(path string, d fs.DirEntry, err error) bool) error { _, err := os.Stat(dir) if os.IsNotExist(err) {