Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan committed Feb 3, 2022
1 parent 950c818 commit 5a04fa6
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions formater.go
Expand Up @@ -7,6 +7,7 @@ import (
"go/ast"
goparser "go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -30,7 +31,7 @@ type Formater struct {
mainFile string
}

// NewFormater create a new formater
// NewFormater create a new formater instance.
func NewFormater() *Formater {
formater := &Formater{
debug: log.New(os.Stdout, "", log.LstdFlags),
Expand Down Expand Up @@ -110,7 +111,7 @@ func (f *Formater) visit(path string, fileInfo os.FileInfo, err error) error {
return nil
}

// FormatMain format the main.go comment
// FormatMain format the main.go comment.
func (f *Formater) FormatMain(mainFilepath string) error {
fileSet := token.NewFileSet()
astFile, err := goparser.ParseFile(fileSet, mainFilepath, nil, goparser.ParseComments)
Expand Down Expand Up @@ -171,17 +172,14 @@ func writeFormatedComments(filepath string, formatedComments bytes.Buffer, oldCo
commentHash, commentContent := commentSplit[0], commentSplit[1]

if !isBlankComment(commentContent) {
oldComment := oldCommentsMap[commentHash]
if strings.Contains(replaceSrc, oldComment) {
replaceSrc = strings.Replace(replaceSrc, oldComment, commentContent, 1)
}
replaceSrc = strings.Replace(replaceSrc, oldCommentsMap[commentHash], commentContent, 1)
}
}
}
return writeBack(filepath, []byte(replaceSrc), srcBytes)
}

func formatFuncDoc(commentList []*ast.Comment, formatedComments *bytes.Buffer, oldCommentsMap map[string]string) {
func formatFuncDoc(commentList []*ast.Comment, formatedComments io.Writer, oldCommentsMap map[string]string) {
tabw := tabwriter.NewWriter(formatedComments, 0, 0, 2, ' ', 0)

for _, comment := range commentList {
Expand Down Expand Up @@ -315,7 +313,6 @@ func backupFile(filename string, data []byte, perm os.FileMode) (string, error)
if err != nil {
return "", err
}
bakname := f.Name()
if chmodSupported {
_ = f.Chmod(perm)
}
Expand All @@ -325,5 +322,5 @@ func backupFile(filename string, data []byte, perm os.FileMode) (string, error)
if err1 := f.Close(); err == nil {
err = err1
}
return bakname, err
return f.Name(), err
}

0 comments on commit 5a04fa6

Please sign in to comment.