OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * **docgen** is a tool for creating machine readable representations of Dart | 6 * **docgen** is a tool for creating machine readable representations of Dart |
7 * code metadata, including: classes, members, comments and annotations. | 7 * code metadata, including: classes, members, comments and annotations. |
8 * | 8 * |
9 * docgen is run on a `.dart` file or a directory containing `.dart` files. | 9 * docgen is run on a `.dart` file or a directory containing `.dart` files. |
10 * | 10 * |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 CommentInstanceMirror comment = metadata; | 259 CommentInstanceMirror comment = metadata; |
260 if (comment.isDocComment) { | 260 if (comment.isDocComment) { |
261 if (commentText == null) { | 261 if (commentText == null) { |
262 commentText = comment.trimmedText; | 262 commentText = comment.trimmedText; |
263 } else { | 263 } else { |
264 commentText = '$commentText ${comment.trimmedText}'; | 264 commentText = '$commentText ${comment.trimmedText}'; |
265 } | 265 } |
266 } | 266 } |
267 } | 267 } |
268 }); | 268 }); |
269 | |
269 commentText = commentText == null ? '' : | 270 commentText = commentText == null ? '' : |
270 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver) | 271 markdown.markdownToHtml(commentText.trim(), linkResolver: linkResolver); |
271 .replaceAll('\n', ' '); | |
272 return commentText; | 272 return commentText; |
273 } | 273 } |
274 | 274 |
275 /** | 275 /** |
276 * Converts all [foo] references in comments to <a>libraryName.foo</a>. | 276 * Converts all [foo] references in comments to <a>libraryName.foo</a>. |
277 */ | 277 */ |
278 markdown.Node fixReference(String name, LibraryMirror currentLibrary, | 278 markdown.Node fixReference(String name, LibraryMirror currentLibrary, |
279 ClassMirror currentClass, MemberMirror currentMember) { | 279 ClassMirror currentClass, MemberMirror currentMember) { |
280 var reference; | 280 var reference; |
281 var memberScope = currentMember == null ? | 281 var memberScope = currentMember == null ? |
(...skipping 11 matching lines...) Expand all Loading... | |
293 * Returns a map of [Variable] objects constructed from inputted mirrors. | 293 * Returns a map of [Variable] objects constructed from inputted mirrors. |
294 */ | 294 */ |
295 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap, | 295 Map<String, Variable> _getVariables(Map<String, VariableMirror> mirrorMap, |
296 bool includePrivate) { | 296 bool includePrivate) { |
297 var data = {}; | 297 var data = {}; |
298 // TODO(janicejl): When map to map feature is created, replace the below with | 298 // TODO(janicejl): When map to map feature is created, replace the below with |
299 // a filter. Issue(#9590). | 299 // a filter. Issue(#9590). |
300 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { | 300 mirrorMap.forEach((String mirrorName, VariableMirror mirror) { |
301 if (includePrivate || !mirror.isPrivate) { | 301 if (includePrivate || !mirror.isPrivate) { |
302 _currentMember = mirror; | 302 _currentMember = mirror; |
303 data[mirrorName] = new Variable(mirrorName, mirror.qualifiedName, | 303 data[mirrorName] = new Variable(mirrorName, mirror.isFinal, |
304 mirror.isFinal, mirror.isStatic, mirror.type.qualifiedName, | 304 mirror.isStatic, mirror.type.qualifiedName, _getComment(mirror), |
305 _getComment(mirror), _getAnnotations(mirror)); | 305 _getAnnotations(mirror)); |
306 } | 306 } |
307 }); | 307 }); |
308 return data; | 308 return data; |
309 } | 309 } |
310 | 310 |
311 /** | 311 /** |
312 * Returns a map of [Method] objects constructed from inputted mirrors. | 312 * Returns a map of [Method] objects constructed from inputted mirrors. |
313 */ | 313 */ |
314 Map<String, Method> _getMethods(Map<String, MethodMirror> mirrorMap, | 314 Map<String, Map<String, Method>> _getMethods |
315 bool includePrivate) { | 315 (Map<String, MethodMirror> mirrorMap, bool includePrivate) { |
316 var data = {}; | 316 |
317 var setters = {}; | |
318 var getters = {}; | |
319 var constructors = {}; | |
320 var operators = {}; | |
321 var methods = {}; | |
322 | |
323 | |
324 | |
317 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { | 325 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
326 var methodConstructor = new Method(mirrorName, mirror.isStatic, | |
Bob Nystrom
2013/07/15 22:46:43
It's just a method, right? "methodConstructor" ->
janicejl
2013/07/15 23:03:44
Done.
| |
327 mirror.returnType.qualifiedName, _getComment(mirror), | |
328 _getParameters(mirror.parameters), _getAnnotations(mirror)); | |
318 if (includePrivate || !mirror.isPrivate) { | 329 if (includePrivate || !mirror.isPrivate) { |
319 _currentMember = mirror; | 330 _currentMember = mirror; |
320 data[mirrorName] = new Method(mirrorName, mirror.qualifiedName, | 331 if (mirror.isSetter) { |
321 mirror.isSetter, mirror.isGetter, mirror.isConstructor, | 332 setters[mirrorName] = methodConstructor; |
322 mirror.isOperator, mirror.isStatic, mirror.returnType.qualifiedName, | 333 } else if (mirror.isGetter) { |
323 _getComment(mirror), _getParameters(mirror.parameters), | 334 getters[mirrorName] = methodConstructor; |
324 _getAnnotations(mirror)); | 335 } else if (mirror.isConstructor) { |
336 constructors[mirrorName] = methodConstructor; | |
337 } else if (mirror.isOperator) { | |
338 operators[mirrorName] = methodConstructor; | |
339 } else if (mirror.isRegularMethod) { | |
340 methods[mirrorName] = methodConstructor; | |
341 } else { | |
342 throw new StateError('${mirror.qualifiedName} - no method type match'); | |
343 } | |
325 } | 344 } |
326 }); | 345 }); |
327 return data; | 346 return {'setters' : setters, |
347 'getters' : getters, | |
348 'constructors' : constructors, | |
349 'operators' : operators, | |
350 'methods' : methods}; | |
328 } | 351 } |
329 | 352 |
330 /** | 353 /** |
331 * Returns a map of [Class] objects constructed from inputted mirrors. | 354 * Returns a map of [Class] objects constructed from inputted mirrors. |
332 */ | 355 */ |
333 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, | 356 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, |
334 bool includePrivate) { | 357 bool includePrivate) { |
335 var data = {}; | 358 var data = {}; |
336 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { | 359 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
337 if (includePrivate || !mirror.isPrivate) { | 360 if (includePrivate || !mirror.isPrivate) { |
338 _currentClass = mirror; | 361 _currentClass = mirror; |
339 var superclass = (mirror.superclass != null) ? | 362 var superclass = (mirror.superclass != null) ? |
340 mirror.superclass.qualifiedName : ''; | 363 mirror.superclass.qualifiedName : ''; |
341 var interfaces = | 364 var interfaces = |
342 mirror.superinterfaces.map((interface) => interface.qualifiedName); | 365 mirror.superinterfaces.map((interface) => interface.qualifiedName); |
343 data[mirrorName] = new Class(mirrorName, mirror.qualifiedName, | 366 data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, |
344 superclass, mirror.isAbstract, mirror.isTypedef, | 367 mirror.isTypedef, _getComment(mirror), interfaces.toList(), |
345 _getComment(mirror), interfaces.toList(), | |
346 _getVariables(mirror.variables, includePrivate), | 368 _getVariables(mirror.variables, includePrivate), |
347 _getMethods(mirror.methods, includePrivate), | 369 _getMethods(mirror.methods, includePrivate), |
348 _getAnnotations(mirror)); | 370 _getAnnotations(mirror)); |
349 } | 371 } |
350 }); | 372 }); |
351 return data; | 373 return data; |
352 } | 374 } |
353 | 375 |
354 /** | 376 /** |
355 * Returns a map of [Parameter] objects constructed from inputted mirrors. | 377 * Returns a map of [Parameter] objects constructed from inputted mirrors. |
356 */ | 378 */ |
357 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { | 379 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { |
358 var data = {}; | 380 var data = {}; |
359 mirrorList.forEach((ParameterMirror mirror) { | 381 mirrorList.forEach((ParameterMirror mirror) { |
360 _currentMember = mirror; | 382 _currentMember = mirror; |
361 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 383 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
362 mirror.qualifiedName, mirror.isOptional, mirror.isNamed, | 384 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
363 mirror.hasDefaultValue, mirror.type.qualifiedName, | 385 mirror.type.qualifiedName, mirror.defaultValue, |
364 mirror.defaultValue, _getAnnotations(mirror)); | 386 _getAnnotations(mirror)); |
365 }); | 387 }); |
366 return data; | 388 return data; |
367 } | 389 } |
368 | 390 |
369 /** | 391 /** |
370 * Writes text to a file in the 'docs' directory. | 392 * Writes text to a file in the 'docs' directory. |
371 */ | 393 */ |
372 void _writeToFile(String text, String filename) { | 394 void _writeToFile(String text, String filename) { |
373 Directory dir = new Directory('docs'); | 395 Directory dir = new Directory('docs'); |
374 if (!dir.existsSync()) { | 396 if (!dir.existsSync()) { |
375 dir.createSync(); | 397 dir.createSync(); |
376 } | 398 } |
377 File file = new File('docs/$filename'); | 399 File file = new File('docs/$filename'); |
378 if (!file.existsSync()) { | 400 if (!file.existsSync()) { |
379 file.createSync(); | 401 file.createSync(); |
380 } | 402 } |
381 file.openSync(); | 403 file.openSync(); |
382 file.writeAsString(text); | 404 file.writeAsString(text); |
383 } | 405 } |
384 | 406 |
385 /** | 407 /** |
386 * Transforms the map by calling toMap on each value in it. | 408 * Transforms the map by calling toMap on each value in it. |
387 */ | 409 */ |
388 Map recurseMap(Map inputMap) { | 410 Map recurseMap(Map inputMap) { |
389 var outputMap = {}; | 411 var outputMap = {}; |
390 inputMap.forEach((key, value) { | 412 inputMap.forEach((key, value) { |
391 outputMap[key] = value.toMap(); | 413 if (value is Map) { |
414 outputMap[key] = recurseMap(value); | |
415 } else { | |
416 outputMap[key] = value.toMap(); | |
417 } | |
392 }); | 418 }); |
393 return outputMap; | 419 return outputMap; |
394 } | 420 } |
395 | 421 |
396 /** | 422 /** |
397 * A class containing contents of a Dart library. | 423 * A class containing contents of a Dart library. |
398 */ | 424 */ |
399 class Library { | 425 class Library { |
400 | 426 |
401 /// Documentation comment with converted markdown. | 427 /// Documentation comment with converted markdown. |
402 String comment; | 428 String comment; |
403 | 429 |
404 /// Top-level variables in the library. | 430 /// Top-level variables in the library. |
405 Map<String, Variable> variables; | 431 Map<String, Variable> variables; |
406 | 432 |
407 /// Top-level functions in the library. | 433 /// Top-level functions in the library. |
408 Map<String, Method> functions; | 434 Map<String, Map<String, Method>> functions; |
409 | 435 |
410 /// Classes defined within the library | 436 /// Classes defined within the library |
411 Map<String, Class> classes; | 437 Map<String, Class> classes; |
412 | 438 |
413 String name; | 439 String name; |
414 | 440 |
415 Library(this.name, this.comment, this.variables, | 441 Library(this.name, this.comment, this.variables, |
416 this.functions, this.classes); | 442 this.functions, this.classes); |
417 | 443 |
418 /// Generates a map describing the [Library] object. | 444 /// Generates a map describing the [Library] object. |
(...skipping 17 matching lines...) Expand all Loading... | |
436 /// Documentation comment with converted markdown. | 462 /// Documentation comment with converted markdown. |
437 String comment; | 463 String comment; |
438 | 464 |
439 /// List of the names of interfaces that this class implements. | 465 /// List of the names of interfaces that this class implements. |
440 List<String> interfaces; | 466 List<String> interfaces; |
441 | 467 |
442 /// Top-level variables in the class. | 468 /// Top-level variables in the class. |
443 Map<String, Variable> variables; | 469 Map<String, Variable> variables; |
444 | 470 |
445 /// Methods in the class. | 471 /// Methods in the class. |
446 Map<String, Method> methods; | 472 Map<String, Map<String, Method>> methods; |
447 | 473 |
448 String name; | 474 String name; |
449 String qualifiedName; | |
450 String superclass; | 475 String superclass; |
451 bool isAbstract; | 476 bool isAbstract; |
452 bool isTypedef; | 477 bool isTypedef; |
453 | 478 |
454 /// List of the meta annotations on the class. | 479 /// List of the meta annotations on the class. |
455 List<String> annotations; | 480 List<String> annotations; |
456 | 481 |
457 Class(this.name, this.qualifiedName, this.superclass, this.isAbstract, | 482 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, |
458 this.isTypedef, this.comment, this.interfaces, this.variables, | 483 this.comment, this.interfaces, this.variables, this.methods, |
459 this.methods, this.annotations); | 484 this.annotations); |
460 | 485 |
461 /// Generates a map describing the [Class] object. | 486 /// Generates a map describing the [Class] object. |
462 Map toMap() { | 487 Map toMap() { |
463 var classMap = {}; | 488 var classMap = {}; |
464 classMap['name'] = name; | 489 classMap['name'] = name; |
465 classMap['qualifiedname'] = qualifiedName; | |
466 classMap['comment'] = comment; | 490 classMap['comment'] = comment; |
467 classMap['superclass'] = superclass; | 491 classMap['superclass'] = superclass; |
468 classMap['abstract'] = isAbstract.toString(); | 492 classMap['abstract'] = isAbstract.toString(); |
469 classMap['typedef'] = isTypedef.toString(); | 493 classMap['typedef'] = isTypedef.toString(); |
470 classMap['implements'] = new List.from(interfaces); | 494 classMap['implements'] = new List.from(interfaces); |
471 classMap['variables'] = recurseMap(variables); | 495 classMap['variables'] = recurseMap(variables); |
472 classMap['methods'] = recurseMap(methods); | 496 classMap['methods'] = recurseMap(methods); |
473 classMap['annotations'] = new List.from(annotations); | 497 classMap['annotations'] = new List.from(annotations); |
474 return classMap; | 498 return classMap; |
475 } | 499 } |
476 } | 500 } |
477 | 501 |
478 /** | 502 /** |
479 * A class containing properties of a Dart variable. | 503 * A class containing properties of a Dart variable. |
480 */ | 504 */ |
481 class Variable { | 505 class Variable { |
482 | 506 |
483 /// Documentation comment with converted markdown. | 507 /// Documentation comment with converted markdown. |
484 String comment; | 508 String comment; |
485 | 509 |
486 String name; | 510 String name; |
487 String qualifiedName; | |
488 bool isFinal; | 511 bool isFinal; |
489 bool isStatic; | 512 bool isStatic; |
490 String type; | 513 String type; |
491 | 514 |
492 /// List of the meta annotations on the variable. | 515 /// List of the meta annotations on the variable. |
493 List<String> annotations; | 516 List<String> annotations; |
494 | 517 |
495 Variable(this.name, this.qualifiedName, this.isFinal, this.isStatic, | 518 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment, |
496 this.type, this.comment, this.annotations); | 519 this.annotations); |
497 | 520 |
498 /// Generates a map describing the [Variable] object. | 521 /// Generates a map describing the [Variable] object. |
499 Map toMap() { | 522 Map toMap() { |
500 var variableMap = {}; | 523 var variableMap = {}; |
501 variableMap['name'] = name; | 524 variableMap['name'] = name; |
502 variableMap['qualifiedname'] = qualifiedName; | |
503 variableMap['comment'] = comment; | 525 variableMap['comment'] = comment; |
504 variableMap['final'] = isFinal.toString(); | 526 variableMap['final'] = isFinal.toString(); |
505 variableMap['static'] = isStatic.toString(); | 527 variableMap['static'] = isStatic.toString(); |
506 variableMap['type'] = type; | 528 variableMap['type'] = type; |
507 variableMap['annotations'] = new List.from(annotations); | 529 variableMap['annotations'] = new List.from(annotations); |
508 return variableMap; | 530 return variableMap; |
509 } | 531 } |
510 } | 532 } |
511 | 533 |
512 /** | 534 /** |
513 * A class containing properties of a Dart method. | 535 * A class containing properties of a Dart method. |
514 */ | 536 */ |
515 class Method { | 537 class Method { |
516 | 538 |
517 /// Documentation comment with converted markdown. | 539 /// Documentation comment with converted markdown. |
518 String comment; | 540 String comment; |
519 | 541 |
520 /// Parameters for this method. | 542 /// Parameters for this method. |
521 Map<String, Parameter> parameters; | 543 Map<String, Parameter> parameters; |
522 | 544 |
523 String name; | 545 String name; |
524 String qualifiedName; | |
525 bool isSetter; | |
526 bool isGetter; | |
527 bool isConstructor; | |
528 bool isOperator; | |
529 bool isStatic; | 546 bool isStatic; |
530 String returnType; | 547 String returnType; |
531 | 548 |
532 /// List of the meta annotations on the method. | 549 /// List of the meta annotations on the method. |
533 List<String> annotations; | 550 List<String> annotations; |
534 | 551 |
535 Method(this.name, this.qualifiedName, this.isSetter, this.isGetter, | 552 Method(this.name, this.isStatic, this.returnType, this.comment, |
536 this.isConstructor, this.isOperator, this.isStatic, this.returnType, | 553 this.parameters, this.annotations); |
537 this.comment, this.parameters, this.annotations); | |
538 | 554 |
539 /// Generates a map describing the [Method] object. | 555 /// Generates a map describing the [Method] object. |
540 Map toMap() { | 556 Map toMap() { |
541 var methodMap = {}; | 557 var methodMap = {}; |
542 methodMap['name'] = name; | 558 methodMap['name'] = name; |
543 methodMap['qualifiedname'] = qualifiedName; | |
544 methodMap['comment'] = comment; | 559 methodMap['comment'] = comment; |
545 methodMap['type'] = isSetter ? 'setter' : isGetter ? 'getter' : | |
546 isOperator ? 'operator' : isConstructor ? 'constructor' : 'method'; | |
547 methodMap['static'] = isStatic.toString(); | 560 methodMap['static'] = isStatic.toString(); |
548 methodMap['return'] = returnType; | 561 methodMap['return'] = returnType; |
549 methodMap['parameters'] = recurseMap(parameters); | 562 methodMap['parameters'] = recurseMap(parameters); |
550 methodMap['annotations'] = new List.from(annotations); | 563 methodMap['annotations'] = new List.from(annotations); |
551 return methodMap; | 564 return methodMap; |
552 } | 565 } |
553 } | 566 } |
554 | 567 |
555 /** | 568 /** |
556 * A class containing properties of a Dart method/function parameter. | 569 * A class containing properties of a Dart method/function parameter. |
557 */ | 570 */ |
558 class Parameter { | 571 class Parameter { |
559 | 572 |
560 String name; | 573 String name; |
561 String qualifiedName; | |
562 bool isOptional; | 574 bool isOptional; |
563 bool isNamed; | 575 bool isNamed; |
564 bool hasDefaultValue; | 576 bool hasDefaultValue; |
565 String type; | 577 String type; |
566 String defaultValue; | 578 String defaultValue; |
567 | 579 |
568 /// List of the meta annotations on the parameter. | 580 /// List of the meta annotations on the parameter. |
569 List<String> annotations; | 581 List<String> annotations; |
570 | 582 |
571 Parameter(this.name, this.qualifiedName, this.isOptional, this.isNamed, | 583 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
572 this.hasDefaultValue, this.type, this.defaultValue, this.annotations); | 584 this.type, this.defaultValue, this.annotations); |
573 | 585 |
574 /// Generates a map describing the [Parameter] object. | 586 /// Generates a map describing the [Parameter] object. |
575 Map toMap() { | 587 Map toMap() { |
576 var parameterMap = {}; | 588 var parameterMap = {}; |
577 parameterMap['name'] = name; | 589 parameterMap['name'] = name; |
578 parameterMap['qualifiedname'] = qualifiedName; | |
579 parameterMap['optional'] = isOptional.toString(); | 590 parameterMap['optional'] = isOptional.toString(); |
580 parameterMap['named'] = isNamed.toString(); | 591 parameterMap['named'] = isNamed.toString(); |
581 parameterMap['default'] = hasDefaultValue.toString(); | 592 parameterMap['default'] = hasDefaultValue.toString(); |
582 parameterMap['type'] = type; | 593 parameterMap['type'] = type; |
583 parameterMap['value'] = defaultValue; | 594 parameterMap['value'] = defaultValue; |
584 parameterMap['annotations'] = new List.from(annotations); | 595 parameterMap['annotations'] = new List.from(annotations); |
585 return parameterMap; | 596 return parameterMap; |
586 } | 597 } |
587 } | 598 } |
OLD | NEW |