OLD | NEW |
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 "encoding/json" | 9 "encoding/json" |
10 "flag" | 10 "flag" |
11 "fmt" | 11 "fmt" |
12 "go/ast" | 12 "go/ast" |
13 "go/build" | 13 "go/build" |
14 "go/doc" | 14 "go/doc" |
| 15 "go/format" |
15 "go/printer" | 16 "go/printer" |
16 "go/token" | 17 "go/token" |
17 "io" | 18 "io" |
18 "io/ioutil" | 19 "io/ioutil" |
19 "log" | 20 "log" |
20 "net/http" | 21 "net/http" |
21 "net/url" | 22 "net/url" |
22 "os" | 23 "os" |
23 pathpkg "path" | 24 pathpkg "path" |
24 "path/filepath" | 25 "path/filepath" |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 // remove output comment | 350 // remove output comment |
350 if loc := exampleOutputRx.FindStringIndex(code); loc !=
nil { | 351 if loc := exampleOutputRx.FindStringIndex(code); loc !=
nil { |
351 code = strings.TrimSpace(code[:loc[0]]) | 352 code = strings.TrimSpace(code[:loc[0]]) |
352 } | 353 } |
353 } | 354 } |
354 | 355 |
355 // Write out the playground code in standard Go style | 356 // Write out the playground code in standard Go style |
356 // (use tabs, no comment highlight, etc). | 357 // (use tabs, no comment highlight, etc). |
357 play := "" | 358 play := "" |
358 if eg.Play != nil && *showPlayground { | 359 if eg.Play != nil && *showPlayground { |
359 ast.SortImports(fset, eg.Play) | |
360 var buf bytes.Buffer | 360 var buf bytes.Buffer |
361 » » » err := (&printer.Config{Mode: printer.TabIndent, Tabwidt
h: 8}).Fprint(&buf, fset, eg.Play) | 361 » » » if err := format.Node(&buf, fset, eg.Play); err != nil { |
362 » » » if err != nil { | |
363 log.Print(err) | 362 log.Print(err) |
364 } else { | 363 } else { |
365 play = buf.String() | 364 play = buf.String() |
366 } | 365 } |
367 } | 366 } |
368 | 367 |
369 // Drop output, as the output comment will appear in the code. | 368 // Drop output, as the output comment will appear in the code. |
370 if wholeFile && play == "" { | 369 if wholeFile && play == "" { |
371 out = "" | 370 out = "" |
372 } | 371 } |
(...skipping 1049 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1422 updateIndex() | 1421 updateIndex() |
1423 } | 1422 } |
1424 delay := 60 * time.Second // by default, try every 60s | 1423 delay := 60 * time.Second // by default, try every 60s |
1425 if *testDir != "" { | 1424 if *testDir != "" { |
1426 // in test mode, try once a second for fast startup | 1425 // in test mode, try once a second for fast startup |
1427 delay = 1 * time.Second | 1426 delay = 1 * time.Second |
1428 } | 1427 } |
1429 time.Sleep(delay) | 1428 time.Sleep(delay) |
1430 } | 1429 } |
1431 } | 1430 } |
OLD | NEW |