LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 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 "exec" | 9 "exec" |
10 "flag" | 10 "flag" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 ) | 42 ) |
43 | 43 |
44 var ( | 44 var ( |
45 fset = token.NewFileSet() | 45 fset = token.NewFileSet() |
46 exitCode = 0 | 46 exitCode = 0 |
47 rewrite func(*ast.File) *ast.File | 47 rewrite func(*ast.File) *ast.File |
48 parserMode uint | 48 parserMode uint |
49 printerMode uint | 49 printerMode uint |
50 ) | 50 ) |
51 | 51 |
52 func report(err os.Error) { | 52 func report(err error) { |
53 scanner.PrintError(os.Stderr, err) | 53 scanner.PrintError(os.Stderr, err) |
54 exitCode = 2 | 54 exitCode = 2 |
55 } | 55 } |
56 | 56 |
57 func usage() { | 57 func usage() { |
58 fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [path ...]\n") | 58 fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [path ...]\n") |
59 flag.PrintDefaults() | 59 flag.PrintDefaults() |
60 os.Exit(2) | 60 os.Exit(2) |
61 } | 61 } |
62 | 62 |
(...skipping 16 matching lines...) Expand all Loading... |
79 printerMode |= printer.UseSpaces | 79 printerMode |= printer.UseSpaces |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 func isGoFile(f *os.FileInfo) bool { | 83 func isGoFile(f *os.FileInfo) bool { |
84 // ignore non-Go files | 84 // ignore non-Go files |
85 return f.IsRegular() && !strings.HasPrefix(f.Name, ".") && strings.HasSu
ffix(f.Name, ".go") | 85 return f.IsRegular() && !strings.HasPrefix(f.Name, ".") && strings.HasSu
ffix(f.Name, ".go") |
86 } | 86 } |
87 | 87 |
88 // If in == nil, the source is the contents of the file with the given filename. | 88 // If in == nil, the source is the contents of the file with the given filename. |
89 func processFile(filename string, in io.Reader, out io.Writer, stdin bool) os.Er
ror { | 89 func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
{ |
90 if in == nil { | 90 if in == nil { |
91 f, err := os.Open(filename) | 91 f, err := os.Open(filename) |
92 if err != nil { | 92 if err != nil { |
93 return err | 93 return err |
94 } | 94 } |
95 defer f.Close() | 95 defer f.Close() |
96 in = f | 96 in = f |
97 } | 97 } |
98 | 98 |
99 src, err := ioutil.ReadAll(in) | 99 src, err := ioutil.ReadAll(in) |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 if !*list && !*write && !*doDiff { | 152 if !*list && !*write && !*doDiff { |
153 _, err = out.Write(res) | 153 _, err = out.Write(res) |
154 } | 154 } |
155 | 155 |
156 return err | 156 return err |
157 } | 157 } |
158 | 158 |
159 func visitFile(path string, f *os.FileInfo, err os.Error) os.Error { | 159 func visitFile(path string, f *os.FileInfo, err error) error { |
160 if err == nil && isGoFile(f) { | 160 if err == nil && isGoFile(f) { |
161 err = processFile(path, nil, os.Stdout, false) | 161 err = processFile(path, nil, os.Stdout, false) |
162 } | 162 } |
163 if err != nil { | 163 if err != nil { |
164 report(err) | 164 report(err) |
165 } | 165 } |
166 return nil | 166 return nil |
167 } | 167 } |
168 | 168 |
169 func walkDir(path string) { | 169 func walkDir(path string) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 case dir.IsRegular(): | 218 case dir.IsRegular(): |
219 if err := processFile(path, nil, os.Stdout, false); err
!= nil { | 219 if err := processFile(path, nil, os.Stdout, false); err
!= nil { |
220 report(err) | 220 report(err) |
221 } | 221 } |
222 case dir.IsDirectory(): | 222 case dir.IsDirectory(): |
223 walkDir(path) | 223 walkDir(path) |
224 } | 224 } |
225 } | 225 } |
226 } | 226 } |
227 | 227 |
228 func diff(b1, b2 []byte) (data []byte, err os.Error) { | 228 func diff(b1, b2 []byte) (data []byte, err error) { |
229 f1, err := ioutil.TempFile("", "gofmt") | 229 f1, err := ioutil.TempFile("", "gofmt") |
230 if err != nil { | 230 if err != nil { |
231 return | 231 return |
232 } | 232 } |
233 defer os.Remove(f1.Name()) | 233 defer os.Remove(f1.Name()) |
234 defer f1.Close() | 234 defer f1.Close() |
235 | 235 |
236 f2, err := ioutil.TempFile("", "gofmt") | 236 f2, err := ioutil.TempFile("", "gofmt") |
237 if err != nil { | 237 if err != nil { |
238 return | 238 return |
239 } | 239 } |
240 defer os.Remove(f2.Name()) | 240 defer os.Remove(f2.Name()) |
241 defer f2.Close() | 241 defer f2.Close() |
242 | 242 |
243 f1.Write(b1) | 243 f1.Write(b1) |
244 f2.Write(b2) | 244 f2.Write(b2) |
245 | 245 |
246 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOut
put() | 246 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOut
put() |
247 if len(data) > 0 { | 247 if len(data) > 0 { |
248 // diff exits with a non-zero status when the files don't match. | 248 // diff exits with a non-zero status when the files don't match. |
249 // Ignore that failure as long as we get output. | 249 // Ignore that failure as long as we get output. |
250 err = nil | 250 err = nil |
251 } | 251 } |
252 return | 252 return |
253 | 253 |
254 } | 254 } |
255 | 255 |
256 // parse parses src, which was read from filename, | 256 // parse parses src, which was read from filename, |
257 // as a Go source file or statement list. | 257 // as a Go source file or statement list. |
258 func parse(filename string, src []byte, stdin bool) (*ast.File, func(orig, src [
]byte) []byte, os.Error) { | 258 func parse(filename string, src []byte, stdin bool) (*ast.File, func(orig, src [
]byte) []byte, error) { |
259 // Try as whole source file. | 259 // Try as whole source file. |
260 file, err := parser.ParseFile(fset, filename, src, parserMode) | 260 file, err := parser.ParseFile(fset, filename, src, parserMode) |
261 if err == nil { | 261 if err == nil { |
262 return file, nil, nil | 262 return file, nil, nil |
263 } | 263 } |
264 // If the error is that the source file didn't begin with a | 264 // If the error is that the source file didn't begin with a |
265 // package line and this is standard input, fall through to | 265 // package line and this is standard input, fall through to |
266 // try as a source fragment. Stop and return on any other error. | 266 // try as a source fragment. Stop and return on any other error. |
267 » if !stdin || !strings.Contains(err.String(), "expected 'package'") { | 267 » if !stdin || !strings.Contains(err.Error(), "expected 'package'") { |
268 return nil, nil, err | 268 return nil, nil, err |
269 } | 269 } |
270 | 270 |
271 // If this is a declaration list, make it a source file | 271 // If this is a declaration list, make it a source file |
272 // by inserting a package clause. | 272 // by inserting a package clause. |
273 // Insert using a ;, not a newline, so that the line numbers | 273 // Insert using a ;, not a newline, so that the line numbers |
274 // in psrc match the ones in src. | 274 // in psrc match the ones in src. |
275 psrc := append([]byte("package p;"), src...) | 275 psrc := append([]byte("package p;"), src...) |
276 file, err = parser.ParseFile(fset, filename, psrc, parserMode) | 276 file, err = parser.ParseFile(fset, filename, psrc, parserMode) |
277 if err == nil { | 277 if err == nil { |
278 adjust := func(orig, src []byte) []byte { | 278 adjust := func(orig, src []byte) []byte { |
279 // Remove the package clause. | 279 // Remove the package clause. |
280 // Gofmt has turned the ; into a \n. | 280 // Gofmt has turned the ; into a \n. |
281 src = src[len("package p\n"):] | 281 src = src[len("package p\n"):] |
282 return matchSpace(orig, src) | 282 return matchSpace(orig, src) |
283 } | 283 } |
284 return file, adjust, nil | 284 return file, adjust, nil |
285 } | 285 } |
286 // If the error is that the source file didn't begin with a | 286 // If the error is that the source file didn't begin with a |
287 // declaration, fall through to try as a statement list. | 287 // declaration, fall through to try as a statement list. |
288 // Stop and return on any other error. | 288 // Stop and return on any other error. |
289 » if !strings.Contains(err.String(), "expected declaration") { | 289 » if !strings.Contains(err.Error(), "expected declaration") { |
290 return nil, nil, err | 290 return nil, nil, err |
291 } | 291 } |
292 | 292 |
293 // If this is a statement list, make it a source file | 293 // If this is a statement list, make it a source file |
294 // by inserting a package clause and turning the list | 294 // by inserting a package clause and turning the list |
295 // into a function body. This handles expressions too. | 295 // into a function body. This handles expressions too. |
296 // Insert using a ;, not a newline, so that the line numbers | 296 // Insert using a ;, not a newline, so that the line numbers |
297 // in fsrc match the ones in src. | 297 // in fsrc match the ones in src. |
298 fsrc := append(append([]byte("package p; func _() {"), src...), '}') | 298 fsrc := append(append([]byte("package p; func _() {"), src...), '}') |
299 file, err = parser.ParseFile(fset, filename, fsrc, parserMode) | 299 file, err = parser.ParseFile(fset, filename, fsrc, parserMode) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 src = nil | 353 src = nil |
354 } | 354 } |
355 if len(line) > 0 && line[0] != '\n' { // not blank | 355 if len(line) > 0 && line[0] != '\n' { // not blank |
356 b.Write(indent) | 356 b.Write(indent) |
357 } | 357 } |
358 b.Write(line) | 358 b.Write(line) |
359 } | 359 } |
360 b.Write(after) | 360 b.Write(after) |
361 return b.Bytes() | 361 return b.Bytes() |
362 } | 362 } |
LEFT | RIGHT |