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 // Extract example functions from file ASTs. | 5 // Extract example functions from file ASTs. |
6 | 6 |
7 package doc | 7 package doc |
8 | 8 |
9 import ( | 9 import ( |
10 "go/ast" | 10 "go/ast" |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 // Include comments that are inside the function body. | 251 // Include comments that are inside the function body. |
252 for _, c := range file.Comments { | 252 for _, c := range file.Comments { |
253 if body.Pos() <= c.Pos() && c.End() <= body.End() { | 253 if body.Pos() <= c.Pos() && c.End() <= body.End() { |
254 comments = append(comments, c) | 254 comments = append(comments, c) |
255 } | 255 } |
256 } | 256 } |
257 | 257 |
258 » // Strip "Output:" commment and adjust body end position. | 258 » // Strip "Output:" comment and adjust body end position. |
259 body, comments = stripOutputComment(body, comments) | 259 body, comments = stripOutputComment(body, comments) |
260 | 260 |
261 // Synthesize import declaration. | 261 // Synthesize import declaration. |
262 importDecl := &ast.GenDecl{ | 262 importDecl := &ast.GenDecl{ |
263 Tok: token.IMPORT, | 263 Tok: token.IMPORT, |
264 Lparen: 1, // Need non-zero Lparen and Rparen so that printer | 264 Lparen: 1, // Need non-zero Lparen and Rparen so that printer |
265 Rparen: 1, // treats this as a factored import. | 265 Rparen: 1, // treats this as a factored import. |
266 } | 266 } |
267 for n, p := range namedImports { | 267 for n, p := range namedImports { |
268 s := &ast.ImportSpec{Path: &ast.BasicLit{Value: strconv.Quote(p)
}} | 268 s := &ast.ImportSpec{Path: &ast.BasicLit{Value: strconv.Quote(p)
}} |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 } | 311 } |
312 | 312 |
313 // Copy the File, as it may be used elsewhere. | 313 // Copy the File, as it may be used elsewhere. |
314 f := *file | 314 f := *file |
315 f.Name = ast.NewIdent("main") | 315 f.Name = ast.NewIdent("main") |
316 f.Decls = decls | 316 f.Decls = decls |
317 f.Comments = comments | 317 f.Comments = comments |
318 return &f | 318 return &f |
319 } | 319 } |
320 | 320 |
321 // stripOutputComment finds and removes an "Output:" commment from body | 321 // stripOutputComment finds and removes an "Output:" comment from body |
322 // and comments, and adjusts the body block's end position. | 322 // and comments, and adjusts the body block's end position. |
323 func stripOutputComment(body *ast.BlockStmt, comments []*ast.CommentGroup) (*ast
.BlockStmt, []*ast.CommentGroup) { | 323 func stripOutputComment(body *ast.BlockStmt, comments []*ast.CommentGroup) (*ast
.BlockStmt, []*ast.CommentGroup) { |
324 // Do nothing if no "Output:" comment found. | 324 // Do nothing if no "Output:" comment found. |
325 i, last := lastComment(body, comments) | 325 i, last := lastComment(body, comments) |
326 if last == nil || !outputPrefix.MatchString(last.Text()) { | 326 if last == nil || !outputPrefix.MatchString(last.Text()) { |
327 return body, comments | 327 return body, comments |
328 } | 328 } |
329 | 329 |
330 // Copy body and comments, as the originals may be used elsewhere. | 330 // Copy body and comments, as the originals may be used elsewhere. |
331 newBody := &ast.BlockStmt{ | 331 newBody := &ast.BlockStmt{ |
(...skipping 14 matching lines...) Expand all Loading... |
346 if cg.Pos() < pos { | 346 if cg.Pos() < pos { |
347 continue | 347 continue |
348 } | 348 } |
349 if cg.End() > end { | 349 if cg.End() > end { |
350 break | 350 break |
351 } | 351 } |
352 i, last = j, cg | 352 i, last = j, cg |
353 } | 353 } |
354 return | 354 return |
355 } | 355 } |
LEFT | RIGHT |