feat: custom printer ast

main
sudo pacman -Syu 2022-11-28 11:14:32 +07:00
parent a341a91279
commit 065e5ec321
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
1 changed files with 11 additions and 2 deletions

View File

@ -200,6 +200,7 @@ func (ft *Formatter) formatImports(
// Parse ast
fset := token.NewFileSet()
// Copy from gofumpt
parserMode := parser.Mode(0)
parserMode |= parser.ParseComments
parserMode |= parser.SkipObjectResolution
@ -276,8 +277,16 @@ func (ft *Formatter) formatImports(
// Print formatted bytes from formatted ast
var formattedBytes []byte
formattedBuffer := bytes.NewBuffer(formattedBytes)
if err := printer.Fprint(formattedBuffer, fset, astFile); err != nil {
return nil, err
// Copy from goimports
printerMode := printer.UseSpaces
printerMode |= printer.TabIndent
printerCfg := &printer.Config{
Mode: printerMode,
Tabwidth: 8,
}
if err := printerCfg.Fprint(formattedBuffer, fset, astFile); err != nil {
return nil, fmt.Errorf("printer: failed to fprint: %w", err)
}
return formattedBuffer.Bytes(), nil