/*
Gofmt formats Go programs.
+It uses tabs (width = 8) for indentation and blanks for alignment.
Without an explicit path, it processes the standard input. Given a file,
it operates on that file; given a directory, it operates on all .go files in
If a file's formatting is different from gofmt's, overwrite it
with gofmt's version.
-Formatting control flags:
- -comments=true
- Print comments; if false, all comments are elided from the output.
- -tabs=true
- Indent with tabs; if false, spaces are used instead.
- -tabwidth=8
- Tab width in spaces.
+Debugging support:
+ -cpuprofile filename
+ Write cpu profile to the specified file.
The rewrite rule specified with the -r flag must be a string of the form:
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
- // layout control
- comments = flag.Bool("comments", true, "print comments")
- tabWidth = flag.Int("tabwidth", 8, "tab width")
- tabIndent = flag.Bool("tabs", true, "indent with tabs")
-
// debugging
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
)
+const (
+ tabWidth = 8
+ printerMode = printer.UseSpaces | printer.TabIndent
+)
+
var (
- fileSet = token.NewFileSet() // per process FileSet
- exitCode = 0
- rewrite func(*ast.File) *ast.File
- parserMode parser.Mode
- printerMode printer.Mode
+ fileSet = token.NewFileSet() // per process FileSet
+ exitCode = 0
+ rewrite func(*ast.File) *ast.File
+ parserMode parser.Mode
)
func report(err error) {
}
func initParserMode() {
- parserMode = parser.Mode(0)
- if *comments {
- parserMode |= parser.ParseComments
- }
+ parserMode = parser.ParseComments
if *allErrors {
parserMode |= parser.AllErrors
}
}
-func initPrinterMode() {
- printerMode = printer.UseSpaces
- if *tabIndent {
- printerMode |= printer.TabIndent
- }
-}
-
func isGoFile(f os.FileInfo) bool {
// ignore non-Go files
name := f.Name()
}
var buf bytes.Buffer
- err = (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(&buf, fileSet, file)
+ err = (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(&buf, fileSet, file)
if err != nil {
return err
}
func gofmtMain() {
flag.Usage = usage
flag.Parse()
- if *tabWidth < 0 {
- fmt.Fprintf(os.Stderr, "negative tabwidth %d\n", *tabWidth)
- exitCode = 2
- return
- }
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
}
initParserMode()
- initPrinterMode()
initRewrite()
if flag.NArg() == 0 {
}
ast.SortImports(fset, f)
src.Reset()
- return (&printer.Config{Mode: printerMode, Tabwidth: *tabWidth}).Fprint(src, fset, f)
+ return (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(src, fset, f)
}
func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {