LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "flag" | 9 "flag" |
10 "fmt" | 10 "fmt" |
11 "go/ast" | 11 "go/ast" |
| 12 "go/format" |
12 "go/parser" | 13 "go/parser" |
13 "go/printer" | |
14 "go/scanner" | 14 "go/scanner" |
15 "go/token" | 15 "go/token" |
16 "io/ioutil" | 16 "io/ioutil" |
17 "os" | 17 "os" |
18 "os/exec" | 18 "os/exec" |
19 "path/filepath" | 19 "path/filepath" |
20 "sort" | 20 "sort" |
21 "strings" | 21 "strings" |
22 ) | 22 ) |
23 | 23 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 default: | 90 default: |
91 if err := processFile(path, false); err != nil { | 91 if err := processFile(path, false); err != nil { |
92 report(err) | 92 report(err) |
93 } | 93 } |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 os.Exit(exitCode) | 97 os.Exit(exitCode) |
98 } | 98 } |
99 | 99 |
100 const ( | 100 const parserMode = parser.ParseComments |
101 » tabWidth = 8 | |
102 » parserMode = parser.ParseComments | |
103 » printerMode = printer.TabIndent | printer.UseSpaces | |
104 ) | |
105 | |
106 var printConfig = &printer.Config{ | |
107 » Mode: printerMode, | |
108 » Tabwidth: tabWidth, | |
109 } | |
110 | 101 |
111 func gofmtFile(f *ast.File) ([]byte, error) { | 102 func gofmtFile(f *ast.File) ([]byte, error) { |
112 var buf bytes.Buffer | 103 var buf bytes.Buffer |
113 | 104 » if err := format.Node(&buf, fset, f); err != nil { |
114 » ast.SortImports(fset, f) | |
115 » err := printConfig.Fprint(&buf, fset, f) | |
116 » if err != nil { | |
117 return nil, err | 105 return nil, err |
118 } | 106 } |
119 return buf.Bytes(), nil | 107 return buf.Bytes(), nil |
120 } | 108 } |
121 | 109 |
122 func processFile(filename string, useStdin bool) error { | 110 func processFile(filename string, useStdin bool) error { |
123 var f *os.File | 111 var f *os.File |
124 var err error | 112 var err error |
125 var fixlog bytes.Buffer | 113 var fixlog bytes.Buffer |
126 | 114 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 return nil | 192 return nil |
205 } | 193 } |
206 | 194 |
207 return ioutil.WriteFile(f.Name(), newSrc, 0) | 195 return ioutil.WriteFile(f.Name(), newSrc, 0) |
208 } | 196 } |
209 | 197 |
210 var gofmtBuf bytes.Buffer | 198 var gofmtBuf bytes.Buffer |
211 | 199 |
212 func gofmt(n interface{}) string { | 200 func gofmt(n interface{}) string { |
213 gofmtBuf.Reset() | 201 gofmtBuf.Reset() |
214 » err := printConfig.Fprint(&gofmtBuf, fset, n) | 202 » if err := format.Node(&gofmtBuf, fset, n); err != nil { |
215 » if err != nil { | |
216 return "<" + err.Error() + ">" | 203 return "<" + err.Error() + ">" |
217 } | 204 } |
218 return gofmtBuf.String() | 205 return gofmtBuf.String() |
219 } | 206 } |
220 | 207 |
221 func report(err error) { | 208 func report(err error) { |
222 scanner.PrintError(os.Stderr, err) | 209 scanner.PrintError(os.Stderr, err) |
223 exitCode = 2 | 210 exitCode = 2 |
224 } | 211 } |
225 | 212 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 f2.Write(b2) | 249 f2.Write(b2) |
263 | 250 |
264 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOut
put() | 251 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOut
put() |
265 if len(data) > 0 { | 252 if len(data) > 0 { |
266 // diff exits with a non-zero status when the files don't match. | 253 // diff exits with a non-zero status when the files don't match. |
267 // Ignore that failure as long as we get output. | 254 // Ignore that failure as long as we get output. |
268 err = nil | 255 err = nil |
269 } | 256 } |
270 return | 257 return |
271 } | 258 } |
LEFT | RIGHT |