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 | |
317 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { | 323 mirrorMap.forEach((String mirrorName, MethodMirror mirror) { |
318 if (includePrivate || !mirror.isPrivate) { | 324 if (includePrivate || !mirror.isPrivate) { |
319 _currentMember = mirror; | 325 _currentMember = mirror; |
320 data[mirrorName] = new Method(mirrorName, mirror.qualifiedName, | 326 if (mirror.isSetter) { |
321 mirror.isSetter, mirror.isGetter, mirror.isConstructor, | 327 setters[mirrorName] = new Method(mirrorName, mirror.isStatic, |
322 mirror.isOperator, mirror.isStatic, mirror.returnType.qualifiedName, | 328 mirror.returnType.qualifiedName, _getComment(mirror), |
323 _getComment(mirror), _getParameters(mirror.parameters), | 329 _getParameters(mirror.parameters), _getAnnotations(mirror)); |
Bob Nystrom
2013/07/15 22:16:03
This constructor call is the same for every case,
janicejl
2013/07/15 22:38:43
Done.
| |
324 _getAnnotations(mirror)); | 330 } else if (mirror.isGetter) { |
331 getters[mirrorName] = new Method(mirrorName, mirror.isStatic, | |
332 mirror.returnType.qualifiedName, _getComment(mirror), | |
333 _getParameters(mirror.parameters), _getAnnotations(mirror)); | |
334 } else if (mirror.isConstructor) { | |
335 constructors[mirrorName] = new Method(mirrorName, mirror.isStatic, | |
336 mirror.returnType.qualifiedName, _getComment(mirror), | |
337 _getParameters(mirror.parameters), _getAnnotations(mirror)); | |
338 } else if (mirror.isOperator) { | |
339 operators[mirrorName] = new Method(mirrorName, mirror.isStatic, | |
340 mirror.returnType.qualifiedName, _getComment(mirror), | |
341 _getParameters(mirror.parameters), _getAnnotations(mirror)); | |
342 } else if (mirror.isRegularMethod) { | |
343 methods[mirrorName] = new Method(mirrorName, mirror.isStatic, | |
344 mirror.returnType.qualifiedName, _getComment(mirror), | |
345 _getParameters(mirror.parameters), _getAnnotations(mirror)); | |
346 } else { | |
347 throw new StateError('${mirror.qualifiedName} - no method type match'); | |
348 } | |
325 } | 349 } |
326 }); | 350 }); |
351 var data = {'setters' : setters, | |
Bob Nystrom
2013/07/15 22:16:03
The variable isn't doing much for you here. May as
janicejl
2013/07/15 22:38:43
Done.
| |
352 'getters' : getters, | |
353 'constructors' : constructors, | |
354 'operators' : operators, | |
355 'methods' : methods}; | |
356 | |
327 return data; | 357 return data; |
328 } | 358 } |
329 | 359 |
330 /** | 360 /** |
331 * Returns a map of [Class] objects constructed from inputted mirrors. | 361 * Returns a map of [Class] objects constructed from inputted mirrors. |
332 */ | 362 */ |
333 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, | 363 Map<String, Class> _getClasses(Map<String, ClassMirror> mirrorMap, |
334 bool includePrivate) { | 364 bool includePrivate) { |
335 var data = {}; | 365 var data = {}; |
336 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { | 366 mirrorMap.forEach((String mirrorName, ClassMirror mirror) { |
337 if (includePrivate || !mirror.isPrivate) { | 367 if (includePrivate || !mirror.isPrivate) { |
338 _currentClass = mirror; | 368 _currentClass = mirror; |
339 var superclass = (mirror.superclass != null) ? | 369 var superclass = (mirror.superclass != null) ? |
340 mirror.superclass.qualifiedName : ''; | 370 mirror.superclass.qualifiedName : ''; |
341 var interfaces = | 371 var interfaces = |
342 mirror.superinterfaces.map((interface) => interface.qualifiedName); | 372 mirror.superinterfaces.map((interface) => interface.qualifiedName); |
343 data[mirrorName] = new Class(mirrorName, mirror.qualifiedName, | 373 data[mirrorName] = new Class(mirrorName, superclass, mirror.isAbstract, |
344 superclass, mirror.isAbstract, mirror.isTypedef, | 374 mirror.isTypedef, _getComment(mirror), interfaces.toList(), |
345 _getComment(mirror), interfaces.toList(), | |
346 _getVariables(mirror.variables, includePrivate), | 375 _getVariables(mirror.variables, includePrivate), |
347 _getMethods(mirror.methods, includePrivate), | 376 _getMethods(mirror.methods, includePrivate), |
348 _getAnnotations(mirror)); | 377 _getAnnotations(mirror)); |
349 } | 378 } |
350 }); | 379 }); |
351 return data; | 380 return data; |
352 } | 381 } |
353 | 382 |
354 /** | 383 /** |
355 * Returns a map of [Parameter] objects constructed from inputted mirrors. | 384 * Returns a map of [Parameter] objects constructed from inputted mirrors. |
356 */ | 385 */ |
357 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { | 386 Map<String, Parameter> _getParameters(List<ParameterMirror> mirrorList) { |
358 var data = {}; | 387 var data = {}; |
359 mirrorList.forEach((ParameterMirror mirror) { | 388 mirrorList.forEach((ParameterMirror mirror) { |
360 _currentMember = mirror; | 389 _currentMember = mirror; |
361 data[mirror.simpleName] = new Parameter(mirror.simpleName, | 390 data[mirror.simpleName] = new Parameter(mirror.simpleName, |
362 mirror.qualifiedName, mirror.isOptional, mirror.isNamed, | 391 mirror.isOptional, mirror.isNamed, mirror.hasDefaultValue, |
363 mirror.hasDefaultValue, mirror.type.qualifiedName, | 392 mirror.type.qualifiedName, mirror.defaultValue, |
364 mirror.defaultValue, _getAnnotations(mirror)); | 393 _getAnnotations(mirror)); |
365 }); | 394 }); |
366 return data; | 395 return data; |
367 } | 396 } |
368 | 397 |
369 /** | 398 /** |
370 * Writes text to a file in the 'docs' directory. | 399 * Writes text to a file in the 'docs' directory. |
371 */ | 400 */ |
372 void _writeToFile(String text, String filename) { | 401 void _writeToFile(String text, String filename) { |
373 Directory dir = new Directory('docs'); | 402 Directory dir = new Directory('docs'); |
374 if (!dir.existsSync()) { | 403 if (!dir.existsSync()) { |
375 dir.createSync(); | 404 dir.createSync(); |
376 } | 405 } |
377 File file = new File('docs/$filename'); | 406 File file = new File('docs/$filename'); |
378 if (!file.existsSync()) { | 407 if (!file.existsSync()) { |
379 file.createSync(); | 408 file.createSync(); |
380 } | 409 } |
381 file.openSync(); | 410 file.openSync(); |
382 file.writeAsString(text); | 411 file.writeAsString(text); |
383 } | 412 } |
384 | 413 |
385 /** | 414 /** |
386 * Transforms the map by calling toMap on each value in it. | 415 * Transforms the map by calling toMap on each value in it. |
387 */ | 416 */ |
388 Map recurseMap(Map inputMap) { | 417 Map recurseMap(Map inputMap) { |
389 var outputMap = {}; | 418 var outputMap = {}; |
390 inputMap.forEach((key, value) { | 419 inputMap.forEach((key, value) { |
391 outputMap[key] = value.toMap(); | 420 if (value is Map) { |
421 outputMap[key] = recurseMap(value); | |
422 } else { | |
423 outputMap[key] = value.toMap(); | |
424 } | |
392 }); | 425 }); |
393 return outputMap; | 426 return outputMap; |
394 } | 427 } |
395 | 428 |
396 /** | 429 /** |
397 * A class containing contents of a Dart library. | 430 * A class containing contents of a Dart library. |
398 */ | 431 */ |
399 class Library { | 432 class Library { |
400 | 433 |
401 /// Documentation comment with converted markdown. | 434 /// Documentation comment with converted markdown. |
402 String comment; | 435 String comment; |
403 | 436 |
404 /// Top-level variables in the library. | 437 /// Top-level variables in the library. |
405 Map<String, Variable> variables; | 438 Map<String, Variable> variables; |
406 | 439 |
407 /// Top-level functions in the library. | 440 /// Top-level functions in the library. |
408 Map<String, Method> functions; | 441 Map<String, Map<String, Method>> functions; |
409 | 442 |
410 /// Classes defined within the library | 443 /// Classes defined within the library |
411 Map<String, Class> classes; | 444 Map<String, Class> classes; |
412 | 445 |
413 String name; | 446 String name; |
414 | 447 |
415 Library(this.name, this.comment, this.variables, | 448 Library(this.name, this.comment, this.variables, |
416 this.functions, this.classes); | 449 this.functions, this.classes); |
417 | 450 |
418 /// Generates a map describing the [Library] object. | 451 /// Generates a map describing the [Library] object. |
(...skipping 17 matching lines...) Expand all Loading... | |
436 /// Documentation comment with converted markdown. | 469 /// Documentation comment with converted markdown. |
437 String comment; | 470 String comment; |
438 | 471 |
439 /// List of the names of interfaces that this class implements. | 472 /// List of the names of interfaces that this class implements. |
440 List<String> interfaces; | 473 List<String> interfaces; |
441 | 474 |
442 /// Top-level variables in the class. | 475 /// Top-level variables in the class. |
443 Map<String, Variable> variables; | 476 Map<String, Variable> variables; |
444 | 477 |
445 /// Methods in the class. | 478 /// Methods in the class. |
446 Map<String, Method> methods; | 479 Map<String, Map<String, Method>> methods; |
447 | 480 |
448 String name; | 481 String name; |
449 String qualifiedName; | |
450 String superclass; | 482 String superclass; |
451 bool isAbstract; | 483 bool isAbstract; |
452 bool isTypedef; | 484 bool isTypedef; |
453 | 485 |
454 /// List of the meta annotations on the class. | 486 /// List of the meta annotations on the class. |
455 List<String> annotations; | 487 List<String> annotations; |
456 | 488 |
457 Class(this.name, this.qualifiedName, this.superclass, this.isAbstract, | 489 Class(this.name, this.superclass, this.isAbstract, this.isTypedef, |
458 this.isTypedef, this.comment, this.interfaces, this.variables, | 490 this.comment, this.interfaces, this.variables, this.methods, |
459 this.methods, this.annotations); | 491 this.annotations); |
460 | 492 |
461 /// Generates a map describing the [Class] object. | 493 /// Generates a map describing the [Class] object. |
462 Map toMap() { | 494 Map toMap() { |
463 var classMap = {}; | 495 var classMap = {}; |
464 classMap['name'] = name; | 496 classMap['name'] = name; |
465 classMap['qualifiedname'] = qualifiedName; | |
466 classMap['comment'] = comment; | 497 classMap['comment'] = comment; |
467 classMap['superclass'] = superclass; | 498 classMap['superclass'] = superclass; |
468 classMap['abstract'] = isAbstract.toString(); | 499 classMap['abstract'] = isAbstract.toString(); |
469 classMap['typedef'] = isTypedef.toString(); | 500 classMap['typedef'] = isTypedef.toString(); |
470 classMap['implements'] = new List.from(interfaces); | 501 classMap['implements'] = new List.from(interfaces); |
471 classMap['variables'] = recurseMap(variables); | 502 classMap['variables'] = recurseMap(variables); |
472 classMap['methods'] = recurseMap(methods); | 503 classMap['methods'] = recurseMap(methods); |
473 classMap['annotations'] = new List.from(annotations); | 504 classMap['annotations'] = new List.from(annotations); |
474 return classMap; | 505 return classMap; |
475 } | 506 } |
476 } | 507 } |
477 | 508 |
478 /** | 509 /** |
479 * A class containing properties of a Dart variable. | 510 * A class containing properties of a Dart variable. |
480 */ | 511 */ |
481 class Variable { | 512 class Variable { |
482 | 513 |
483 /// Documentation comment with converted markdown. | 514 /// Documentation comment with converted markdown. |
484 String comment; | 515 String comment; |
485 | 516 |
486 String name; | 517 String name; |
487 String qualifiedName; | |
488 bool isFinal; | 518 bool isFinal; |
489 bool isStatic; | 519 bool isStatic; |
490 String type; | 520 String type; |
491 | 521 |
492 /// List of the meta annotations on the variable. | 522 /// List of the meta annotations on the variable. |
493 List<String> annotations; | 523 List<String> annotations; |
494 | 524 |
495 Variable(this.name, this.qualifiedName, this.isFinal, this.isStatic, | 525 Variable(this.name, this.isFinal, this.isStatic, this.type, this.comment, |
496 this.type, this.comment, this.annotations); | 526 this.annotations); |
497 | 527 |
498 /// Generates a map describing the [Variable] object. | 528 /// Generates a map describing the [Variable] object. |
499 Map toMap() { | 529 Map toMap() { |
500 var variableMap = {}; | 530 var variableMap = {}; |
501 variableMap['name'] = name; | 531 variableMap['name'] = name; |
502 variableMap['qualifiedname'] = qualifiedName; | |
503 variableMap['comment'] = comment; | 532 variableMap['comment'] = comment; |
504 variableMap['final'] = isFinal.toString(); | 533 variableMap['final'] = isFinal.toString(); |
505 variableMap['static'] = isStatic.toString(); | 534 variableMap['static'] = isStatic.toString(); |
506 variableMap['type'] = type; | 535 variableMap['type'] = type; |
507 variableMap['annotations'] = new List.from(annotations); | 536 variableMap['annotations'] = new List.from(annotations); |
508 return variableMap; | 537 return variableMap; |
509 } | 538 } |
510 } | 539 } |
511 | 540 |
512 /** | 541 /** |
513 * A class containing properties of a Dart method. | 542 * A class containing properties of a Dart method. |
514 */ | 543 */ |
515 class Method { | 544 class Method { |
516 | 545 |
517 /// Documentation comment with converted markdown. | 546 /// Documentation comment with converted markdown. |
518 String comment; | 547 String comment; |
519 | 548 |
520 /// Parameters for this method. | 549 /// Parameters for this method. |
521 Map<String, Parameter> parameters; | 550 Map<String, Parameter> parameters; |
522 | 551 |
523 String name; | 552 String name; |
524 String qualifiedName; | |
525 bool isSetter; | |
526 bool isGetter; | |
527 bool isConstructor; | |
528 bool isOperator; | |
529 bool isStatic; | 553 bool isStatic; |
530 String returnType; | 554 String returnType; |
531 | 555 |
532 /// List of the meta annotations on the method. | 556 /// List of the meta annotations on the method. |
533 List<String> annotations; | 557 List<String> annotations; |
534 | 558 |
535 Method(this.name, this.qualifiedName, this.isSetter, this.isGetter, | 559 Method(this.name, this.isStatic, this.returnType, this.comment, |
536 this.isConstructor, this.isOperator, this.isStatic, this.returnType, | 560 this.parameters, this.annotations); |
537 this.comment, this.parameters, this.annotations); | |
538 | 561 |
539 /// Generates a map describing the [Method] object. | 562 /// Generates a map describing the [Method] object. |
540 Map toMap() { | 563 Map toMap() { |
541 var methodMap = {}; | 564 var methodMap = {}; |
542 methodMap['name'] = name; | 565 methodMap['name'] = name; |
543 methodMap['qualifiedname'] = qualifiedName; | |
544 methodMap['comment'] = comment; | 566 methodMap['comment'] = comment; |
545 methodMap['type'] = isSetter ? 'setter' : isGetter ? 'getter' : | |
546 isOperator ? 'operator' : isConstructor ? 'constructor' : 'method'; | |
547 methodMap['static'] = isStatic.toString(); | 567 methodMap['static'] = isStatic.toString(); |
548 methodMap['return'] = returnType; | 568 methodMap['return'] = returnType; |
549 methodMap['parameters'] = recurseMap(parameters); | 569 methodMap['parameters'] = recurseMap(parameters); |
550 methodMap['annotations'] = new List.from(annotations); | 570 methodMap['annotations'] = new List.from(annotations); |
551 return methodMap; | 571 return methodMap; |
552 } | 572 } |
553 } | 573 } |
554 | 574 |
555 /** | 575 /** |
556 * A class containing properties of a Dart method/function parameter. | 576 * A class containing properties of a Dart method/function parameter. |
557 */ | 577 */ |
558 class Parameter { | 578 class Parameter { |
559 | 579 |
560 String name; | 580 String name; |
561 String qualifiedName; | |
562 bool isOptional; | 581 bool isOptional; |
563 bool isNamed; | 582 bool isNamed; |
564 bool hasDefaultValue; | 583 bool hasDefaultValue; |
565 String type; | 584 String type; |
566 String defaultValue; | 585 String defaultValue; |
567 | 586 |
568 /// List of the meta annotations on the parameter. | 587 /// List of the meta annotations on the parameter. |
569 List<String> annotations; | 588 List<String> annotations; |
570 | 589 |
571 Parameter(this.name, this.qualifiedName, this.isOptional, this.isNamed, | 590 Parameter(this.name, this.isOptional, this.isNamed, this.hasDefaultValue, |
572 this.hasDefaultValue, this.type, this.defaultValue, this.annotations); | 591 this.type, this.defaultValue, this.annotations); |
573 | 592 |
574 /// Generates a map describing the [Parameter] object. | 593 /// Generates a map describing the [Parameter] object. |
575 Map toMap() { | 594 Map toMap() { |
576 var parameterMap = {}; | 595 var parameterMap = {}; |
577 parameterMap['name'] = name; | 596 parameterMap['name'] = name; |
578 parameterMap['qualifiedname'] = qualifiedName; | |
579 parameterMap['optional'] = isOptional.toString(); | 597 parameterMap['optional'] = isOptional.toString(); |
580 parameterMap['named'] = isNamed.toString(); | 598 parameterMap['named'] = isNamed.toString(); |
581 parameterMap['default'] = hasDefaultValue.toString(); | 599 parameterMap['default'] = hasDefaultValue.toString(); |
582 parameterMap['type'] = type; | 600 parameterMap['type'] = type; |
583 parameterMap['value'] = defaultValue; | 601 parameterMap['value'] = defaultValue; |
584 parameterMap['annotations'] = new List.from(annotations); | 602 parameterMap['annotations'] = new List.from(annotations); |
585 return parameterMap; | 603 return parameterMap; |
586 } | 604 } |
587 } | 605 } |
OLD | NEW |