-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmodel.dart
2325 lines (1859 loc) · 65.6 KB
/
model.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// The models used to represent Dart code.
library dartdoc.models;
import 'dart:convert';
import 'dart:io';
import 'package:analyzer/dart/ast/ast.dart'
show AnnotatedNode, Annotation, Declaration;
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/generated/resolver.dart'
show Namespace, NamespaceBuilder, InheritanceManager;
import 'package:analyzer/src/generated/utilities_dart.dart' show ParameterKind;
import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:quiver/core.dart' show hash3;
import 'config.dart';
import 'element_type.dart';
import 'line_number_cache.dart';
import 'markdown_processor.dart' show Documentation;
import 'model_utils.dart';
import 'package_meta.dart' show PackageMeta, FileContents;
import 'utils.dart';
Map<String, Map<String, List<Map<String, dynamic>>>> __crossdartJson;
final Map<Class, List<Class>> _implementors = new Map();
Map<String, Map<String, List<Map<String, dynamic>>>> get _crossdartJson {
if (__crossdartJson == null) {
if (config != null) {
var crossdartFile =
new File(p.join(config.inputDir.path, "crossdart.json"));
if (crossdartFile.existsSync()) {
__crossdartJson = JSON.decode(crossdartFile.readAsStringSync())
as Map<String, Map<String, List<Map<String, dynamic>>>>;
} else {
__crossdartJson = {};
}
} else {
__crossdartJson = {};
}
}
return __crossdartJson;
}
int byName(Nameable a, Nameable b) =>
compareAsciiLowerCaseNatural(a.name, b.name);
void _addToImplementors(Class c) {
_implementors.putIfAbsent(c, () => []);
void _checkAndAddClass(Class key, Class implClass) {
_implementors.putIfAbsent(key, () => []);
List list = _implementors[key];
if (!list.any((l) => l.element == c.element)) {
list.add(implClass);
}
}
if (!c._mixins.isEmpty) {
c._mixins.forEach((t) {
_checkAndAddClass(t.element, c);
});
}
if (c._supertype != null) {
_checkAndAddClass(c._supertype.element, c);
}
if (!c._interfaces.isEmpty) {
c._interfaces.forEach((t) {
_checkAndAddClass(t.element, c);
});
}
}
/// Getters and setters.
class Accessor extends ModelElement
with SourceCodeMixin
implements EnclosedElement {
Accessor(PropertyAccessorElement element, Library library)
: super(element, library);
@override
ModelElement get enclosingElement {
if (_accessor.enclosingElement is CompilationUnitElement) {
return package
._getLibraryFor(_accessor.enclosingElement.enclosingElement);
}
return new ModelElement.from(_accessor.enclosingElement, library);
}
@override
String get href =>
'${library.dirName}/${_accessor.enclosingElement.name}/${name}.html';
bool get isGetter => _accessor.isGetter;
@override
String get kind => 'accessor';
PropertyAccessorElement get _accessor => (element as PropertyAccessorElement);
}
class Class extends ModelElement implements EnclosedElement {
List<ElementType> _mixins;
ElementType _supertype;
List<ElementType> _interfaces;
List<Constructor> _constructors;
List<Method> _allMethods;
List<Operator> _operators;
List<Operator> _inheritedOperators;
List<Operator> _allOperators;
final List<Operator> _genPageOperators = <Operator>[];
List<Method> _inheritedMethods;
List<Method> _staticMethods;
List<Method> _instanceMethods;
List<Method> _allInstanceMethods;
final List<Method> _genPageMethods = <Method>[];
List<Field> _fields;
List<Field> _staticFields;
List<Field> _constants;
List<Field> _instanceFields;
List<Field> _inheritedProperties;
List<Field> _allInstanceProperties;
final List<Field> _genPageProperties = <Field>[];
Class(ClassElement element, Library library) : super(element, library) {
Package p = library.package;
_modelType = new ElementType(_cls.type, this);
_mixins = _cls.mixins
.map((f) {
Library lib = new Library(f.element.library, p);
ElementType t =
new ElementType(f, new ModelElement.from(f.element, lib));
bool exclude = t.element.element.isPrivate;
if (exclude) {
return null;
} else {
return t;
}
})
.where((mixin) => mixin != null)
.toList(growable: false);
if (_cls.supertype != null && _cls.supertype.element.supertype != null) {
Library lib = package._getLibraryFor(_cls.supertype.element);
_supertype = new ElementType(
_cls.supertype, new ModelElement.from(_cls.supertype.element, lib));
/* Private Superclasses should not be shown. */
var exclude = _supertype.element.element.isPrivate;
/* Hide dart2js related stuff */
exclude = exclude ||
(lib.name.startsWith("dart:") &&
_supertype.name == "NativeFieldWrapperClass2");
if (exclude) {
_supertype = null;
}
}
_interfaces = _cls.interfaces
.map((f) {
var lib = new Library(f.element.library, p);
var t = new ElementType(f, new ModelElement.from(f.element, lib));
var exclude = t.element.element.isPrivate;
if (exclude) {
return null;
} else {
return t;
}
})
.where((it) => it != null)
.toList(growable: false);
}
List<Method> get allInstanceMethods {
if (_allInstanceMethods != null) return _allInstanceMethods;
_allInstanceMethods = []
..addAll(instanceMethods)
..addAll(inheritedMethods)
..sort(byName);
return _allInstanceMethods;
}
bool get allInstanceMethodsInherited =>
instanceMethods.every((f) => f.isInherited);
List<Field> get allInstanceProperties {
if (_allInstanceProperties != null) return _allInstanceProperties;
// TODO best way to make this a fixed length list?
_allInstanceProperties = []
..addAll(instanceProperties)
..addAll(inheritedProperties)
..sort(byName);
return _allInstanceProperties;
}
bool get allInstancePropertiesInherited =>
instanceProperties.every((f) => f.isInherited);
List<Operator> get allOperators {
if (_allOperators != null) return _allOperators;
_allOperators = []
..addAll(operators)
..addAll(inheritedOperators)
..sort(byName);
return _allOperators;
}
bool get allOperatorsInherited => operators.every((f) => f.isInherited);
List<Field> get constants {
if (_constants != null) return _constants;
_constants = _allFields.where((f) => f.isConst).toList(growable: false)
..sort(byName);
return _constants;
}
List<Constructor> get constructors {
if (_constructors != null) return _constructors;
_constructors = _cls.constructors.where(isPublic).map((e) {
return new Constructor(e, library);
}).toList(growable: true)..sort(byName);
return _constructors;
}
/// Returns the library that encloses this element.
@override
ModelElement get enclosingElement => library;
String get fileName => "${name}-class.html";
String get fullkind {
if (isAbstract) return 'abstract $kind';
return kind;
}
bool get hasConstants => constants.isNotEmpty;
bool get hasConstructors => constructors.isNotEmpty;
@override
int get hashCode => hash3(
name.hashCode, library.name.hashCode, library.package.name.hashCode);
bool get hasImplementors => implementors.isNotEmpty;
bool get hasInheritedMethods => inheritedMethods.isNotEmpty;
bool get hasInstanceMethods => instanceMethods.isNotEmpty;
bool get hasInstanceProperties => instanceProperties.isNotEmpty;
bool get hasInterfaces => interfaces.isNotEmpty;
bool get hasMethods =>
instanceMethods.isNotEmpty || inheritedMethods.isNotEmpty;
bool get hasMixins => mixins.isNotEmpty;
bool get hasModifiers =>
hasMixins ||
hasAnnotations ||
hasInterfaces ||
hasSupertype ||
hasImplementors;
bool get hasOperators =>
operators.isNotEmpty || inheritedOperators.isNotEmpty;
bool get hasProperties =>
inheritedProperties.isNotEmpty || instanceProperties.isNotEmpty;
bool get hasStaticMethods => staticMethods.isNotEmpty;
bool get hasStaticProperties => staticProperties.isNotEmpty;
bool get hasSupertype => supertype != null;
@override
String get href => '${library.dirName}/$fileName';
/// Returns all the implementors of the class specified.
List<Class> get implementors =>
_implementors[this] != null ? _implementors[this] : [];
List<Method> get inheritedMethods {
if (_inheritedMethods != null) return _inheritedMethods;
InheritanceManager manager = new InheritanceManager(element.library);
Map<String, ExecutableElement> cmap = manager.getMembersInheritedFromClasses(element);
Map<String, ExecutableElement> imap = manager.getMembersInheritedFromInterfaces(element);
// remove methods that exist on this class
_methods.forEach((method) {
cmap.remove(method.name);
imap.remove(method.name);
});
_inheritedMethods = [];
List<ExecutableElement> vs = [];
Set<String> uniqueNames = new Set();
instanceProperties.forEach((f) {
if (f._setter != null) uniqueNames.add(f._setter.name);
if (f._getter != null) uniqueNames.add(f._getter.name);
});
for (String key in cmap.keys) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(key)) continue;
uniqueNames.add(key);
vs.add(cmap[key]);
}
for (String key in imap.keys) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(key)) continue;
uniqueNames.add(key);
vs.add(imap[key]);
}
for (ExecutableElement value in vs) {
if (value != null &&
value is MethodElement &&
isPublic(value) &&
!value.isOperator &&
value.enclosingElement != null) {
if (!package.isDocumented(value.enclosingElement)) {
Method m = new Method.inherited(value, this, library);
_inheritedMethods.add(m);
_genPageMethods.add(m);
} else {
Library lib = package._getLibraryFor(value.enclosingElement);
_inheritedMethods.add(new Method.inherited(
value, new Class(value.enclosingElement, lib), lib));
}
}
}
_inheritedMethods.sort(byName);
return _inheritedMethods;
}
List<Operator> get inheritedOperators {
if (_inheritedOperators != null) return _inheritedOperators;
InheritanceManager manager = new InheritanceManager(element.library);
Map<String, ExecutableElement> cmap = manager.getMembersInheritedFromClasses(element);
Map<String, ExecutableElement> imap = manager.getMembersInheritedFromInterfaces(element);
operators.forEach((operator) {
cmap.remove(operator.element.name);
imap.remove(operator.element.name);
});
_inheritedOperators = [];
Map<String, ExecutableElement> vs = {};
bool _isInheritedOperator(ExecutableElement value) {
if (value != null &&
value is MethodElement &&
!value.isPrivate &&
value.isOperator &&
value.enclosingElement != null) {
return true;
}
return false;
}
for (String key in imap.keys) {
ExecutableElement value = imap[key];
if (_isInheritedOperator(value)) {
vs.putIfAbsent(value.name, () => value);
}
}
for (String key in cmap.keys) {
ExecutableElement value = cmap[key];
if (_isInheritedOperator(value)) {
vs.putIfAbsent(value.name, () => value);
}
}
for (ExecutableElement value in vs.values) {
if (!package.isDocumented(value.enclosingElement)) {
Operator o = new Operator.inherited(value, this, library);
_inheritedOperators.add(o);
_genPageOperators.add(o);
} else {
Library lib = package._getLibraryFor(value.enclosingElement);
_inheritedOperators.add(new Operator.inherited(
value, new Class(value.enclosingElement, lib), lib));
}
}
_inheritedOperators.sort(byName);
return _inheritedOperators;
}
List<Field> get inheritedProperties {
if (_inheritedProperties != null) return _inheritedProperties;
InheritanceManager manager = new InheritanceManager(element.library);
Map<String, ExecutableElement> cmap = manager.getMembersInheritedFromClasses(element);
Map<String, ExecutableElement> imap = manager.getMembersInheritedFromInterfaces(element);
_inheritedProperties = [];
List<ExecutableElement> vs = [];
Set<String> uniqueNames = new Set();
instanceProperties.forEach((f) {
if (f._setter != null) uniqueNames.add(f._setter.name);
if (f._getter != null) uniqueNames.add(f._getter.name);
});
for (String key in cmap.keys) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(key)) continue;
uniqueNames.add(key);
vs.add(cmap[key]);
}
for (String key in imap.keys) {
// XXX: if we care about showing a hierarchy with our inherited methods,
// then don't do this
if (uniqueNames.contains(key)) continue;
uniqueNames.add(key);
vs.add(imap[key]);
}
vs.removeWhere((it) => instanceProperties.any((i) => it.name == i.name));
for (var value in vs) {
if (value != null &&
value is PropertyAccessorElement &&
isPublic(value) &&
value.enclosingElement != null) {
// TODO: why is this here?
var e = value.variable;
if (_inheritedProperties.any((f) => f.element == e)) {
continue;
}
if (!package.isDocumented(value.enclosingElement)) {
Field f = new Field.inherited(e, this, library);
_inheritedProperties.add(f);
_genPageProperties.add(f);
} else {
Library lib = package._getLibraryFor(e.enclosingElement);
_inheritedProperties.add(
new Field.inherited(e, new Class(e.enclosingElement, lib), lib));
}
}
}
_inheritedProperties.sort(byName);
return _inheritedProperties;
}
List<Method> get instanceMethods {
if (_instanceMethods != null) return _instanceMethods;
_instanceMethods = _methods
.where((m) => !m.isStatic && !m.isOperator)
.toList(growable: false)..sort(byName);
_genPageMethods.addAll(_instanceMethods);
return _instanceMethods;
}
List<Field> get instanceProperties {
if (_instanceFields != null) return _instanceFields;
_instanceFields = _allFields
.where((f) => !f.isStatic)
.toList(growable: false)..sort(byName);
_genPageProperties.addAll(_instanceFields);
return _instanceFields;
}
List<ElementType> get interfaces => _interfaces;
bool get isAbstract => _cls.isAbstract;
bool get isErrorOrException {
bool _doCheck(InterfaceType type) {
return (type.element.library.isDartCore &&
(type.name == 'Exception' || type.name == 'Error'));
}
// if this class is itself Error or Exception, return true
if (_doCheck(_cls.type)) return true;
return _cls.allSupertypes.any(_doCheck);
}
@override
String get kind => 'class';
List<Method> get methodsForPages => _genPageMethods;
// TODO: make this method smarter about hierarchies and overrides. Right
// now, we're creating a flat list. We're not paying attention to where
// these methods are actually coming from. This might turn out to be a
// problem if we want to show that info later.
List<ElementType> get mixins => _mixins;
@override
String get nameWithGenerics {
if (!modelType.isParameterizedType) return name;
return '$name<${_typeParameters.map((t) => t.name).join(', ')}>';
}
List<Operator> get operators {
if (_operators != null) return _operators;
_operators = _methods.where((m) => m.isOperator).toList(growable: false)
..sort(byName);
_genPageOperators.addAll(_operators);
return _operators;
}
List<Operator> get operatorsForPages => _genPageOperators;
// TODO: make this method smarter about hierarchies and overrides. Right
// now, we're creating a flat list. We're not paying attention to where
// these methods are actually coming from. This might turn out to be a
// problem if we want to show that info later.
List<Field> get propertiesForPages => _genPageProperties;
List<Method> get staticMethods {
if (_staticMethods != null) return _staticMethods;
_staticMethods = _methods.where((m) => m.isStatic).toList(growable: false)
..sort(byName);
return _staticMethods;
}
List<Field> get staticProperties {
if (_staticFields != null) return _staticFields;
_staticFields = _allFields
.where((f) => f.isStatic)
.where((f) => !f.isConst)
.toList(growable: false)..sort(byName);
return _staticFields;
}
List<ElementType> get superChain {
List<ElementType> typeChain = [];
var parent = _supertype;
while (parent != null) {
typeChain.add(parent);
parent = (parent.element as Class)._supertype;
}
return typeChain;
}
List<ElementType> get superChainReversed => superChain.reversed.toList();
ElementType get supertype => _supertype;
List<Field> get _allFields {
if (_fields != null) return _fields;
_fields = _cls.fields
.where(isPublic)
.map((e) => new Field(e, library))
.toList(growable: false)..sort(byName);
return _fields;
}
ClassElement get _cls => (element as ClassElement);
List<Method> get _methods {
if (_allMethods != null) return _allMethods;
_allMethods = _cls.methods.where(isPublic).map((e) {
if (!e.isOperator) {
return new Method(e, library);
} else {
return new Operator(e, library);
}
}).toList(growable: false)..sort(byName);
return _allMethods;
}
// a stronger hash?
List<TypeParameter> get _typeParameters => _cls.typeParameters.map((f) {
var lib = new Library(f.enclosingElement.library, package);
return new TypeParameter(f, lib);
}).toList();
@override
bool operator ==(o) =>
o is Class &&
name == o.name &&
o.library.name == library.name &&
o.library.package.name == library.package.name;
}
class Constructor extends ModelElement
with SourceCodeMixin
implements EnclosedElement {
Constructor(ConstructorElement element, Library library)
: super(element, library);
@override
ModelElement get enclosingElement =>
new ModelElement.from(_constructor.enclosingElement, library);
String get fullKind {
if (isConst) return 'const $kind';
if (isFactory) return 'factory $kind';
return kind;
}
@override
String get fullyQualifiedName => '${library.name}.$name';
@override
String get href =>
'${library.dirName}/${_constructor.enclosingElement.name}/$name.html';
@override
bool get isConst => _constructor.isConst;
bool get isFactory => _constructor.isFactory;
@override
String get kind => 'constructor';
@override
String get name {
String constructorName = element.name;
Class c = new ModelElement.from(element.enclosingElement, library) as Class;
if (constructorName.isEmpty) {
return c.name;
} else {
return '${c.name}.$constructorName';
}
}
String get shortName {
if (name.contains('.')) {
return name.substring(_constructor.enclosingElement.name.length + 1);
} else {
return name;
}
}
ConstructorElement get _constructor => (element as ConstructorElement);
}
/// Bridges the gap between model elements and packages,
/// both of which have documentation.
abstract class Documentable {
String get documentation;
String get documentationAsHtml;
bool get hasDocumentation;
String get oneLineDoc;
}
class Dynamic extends ModelElement {
Dynamic(Element element, Library library) : super(element, library);
ModelElement get enclosingElement => throw new UnsupportedError('');
@override
String get href => throw new StateError('dynamic should not have an href');
@override
String get kind => 'dynamic';
@override
String get linkedName => 'dynamic';
}
/// An element that is enclosed by some other element.
///
/// Libraries are not enclosed.
abstract class EnclosedElement {
ModelElement get enclosingElement;
}
class Enum extends Class {
List<EnumField> _enumFields;
Enum(ClassElement element, Library library) : super(element, library);
@override
List<EnumField> get constants {
if (_enumFields != null) return _enumFields;
// This is a hack to give 'values' an index of -1 and all other fields
// their expected indicies. https://github.com/dart-lang/dartdoc/issues/1176
var index = -1;
_enumFields = _cls.fields
.where(isPublic)
.where((f) => f.isConst)
.map((field) => new EnumField.forConstant(index++, field, library))
.toList(growable: false)..sort(byName);
return _enumFields;
}
@override
List<EnumField> get instanceProperties {
return super
.instanceProperties
.map((Field p) => new EnumField(p.element, p.library))
.toList(growable: false);
}
@override
String get kind => 'enum';
}
/// Enum's fields are virtual, so we do a little work to create
/// usable values for the docs.
class EnumField extends Field {
int _index;
EnumField(FieldElement element, Library library) : super(element, library);
EnumField.forConstant(this._index, FieldElement element, Library library)
: super(element, library);
@override
String get constantValue {
if (name == 'values') {
return 'const List<${_field.enclosingElement.name}>';
} else {
return 'const ${_field.enclosingElement.name}($_index)';
}
}
@override
String get documentation {
if (name == 'values') {
return 'A constant List of the values in this enum, in order of their declaration.';
} else {
return super.documentation;
}
}
@override
String get href =>
'${library.dirName}/${(enclosingElement as Class).fileName}';
@override
String get linkedName => name;
@override
String get oneLineDoc => documentationAsHtml;
}
class Field extends ModelElement
with GetterSetterCombo
implements EnclosedElement {
String _constantValue;
bool _isInherited = false;
Class _enclosingClass;
Field(FieldElement element, Library library) : super(element, library) {
_setModelType();
}
Field.inherited(FieldElement element, this._enclosingClass, Library library)
: super(element, library) {
_isInherited = true;
_setModelType();
}
String get constantValue {
if (_constantValue != null) return _constantValue;
if (_field.computeNode() == null) return null;
var v = _field.computeNode().toSource();
if (v == null) return null;
var string = v.substring(v.indexOf('=') + 1, v.length).trim();
_constantValue = string.replaceAll(modelType.name, modelType.linkedName);
return _constantValue;
}
String get constantValueTruncated => truncateString(constantValue, 200);
@override
ModelElement get enclosingElement {
if (_enclosingClass == null) {
_enclosingClass = new ModelElement.from(_field.enclosingElement, library);
}
return _enclosingClass;
}
@override
bool get hasGetter => _field.getter != null;
@override
bool get hasSetter => _field.setter != null;
@override
String get href {
if (enclosingElement is Class) {
return '${library.dirName}/${enclosingElement.name}/$_fileName';
} else if (enclosingElement is Library) {
return '${library.dirName}/$_fileName';
} else {
throw new StateError(
'$name is not in a class or library, instead it is a ${enclosingElement.element}');
}
}
@override
bool get isConst => _field.isConst;
@override
bool get isFinal => _field.isFinal;
bool get isInherited => _isInherited;
@override
String get kind => 'property';
String get linkedReturnType => modelType.linkedName;
bool get readOnly => hasGetter && !hasSetter;
bool get readWrite => hasGetter && hasSetter;
String get typeName => "property";
bool get writeOnly => hasSetter && !hasGetter;
@override
String get _computeDocumentationComment {
String docs = getterSetterDocumentationComment;
if (docs.isEmpty) return _field.documentationComment;
return docs;
}
FieldElement get _field => (element as FieldElement);
String get _fileName => isConst ? '$name-constant.html' : '$name.html';
@override
PropertyAccessorElement get _getter => _field.getter;
@override
PropertyAccessorElement get _setter => _field.setter;
void _setModelType() {
if (hasGetter) {
var t = _field.getter.returnType;
_modelType = new ElementType(
t, new ModelElement.from(t.element, _findLibraryFor(t.element)));
} else {
var s = _field.setter.parameters.first.type;
_modelType = new ElementType(
s, new ModelElement.from(s.element, _findLibraryFor(s.element)));
}
}
}
/// Mixin for top-level variables and fields (aka properties)
abstract class GetterSetterCombo {
Accessor get getter {
return _getter == null ? null : new ModelElement.from(_getter, library);
}
String get getterSetterDocumentationComment {
var buffer = new StringBuffer();
if (hasGetter && !_getter.isSynthetic) {
String docs = stripComments(_getter.documentationComment);
if (docs != null) buffer.write(docs);
}
if (hasSetter && !_setter.isSynthetic) {
String docs = stripComments(_setter.documentationComment);
if (docs != null) {
if (buffer.isNotEmpty) buffer.write('\n\n');
buffer.write(docs);
}
}
return buffer.toString();
}
bool get hasExplicitGetter => hasGetter && !_getter.isSynthetic;
bool get hasExplicitSetter => hasSetter && !_setter.isSynthetic;
bool get hasGetter;
bool get hasNoGetterSetter => !hasExplicitGetter && !hasExplicitSetter;
bool get hasSetter;
Library get library;
Accessor get setter {
return _setter == null ? null : new ModelElement.from(_setter, library);
}
PropertyAccessorElement get _getter;
// TODO: now that we have explicit getter and setters, we probably
// want a cleaner way to do this. Only the one-liner is using this
// now. The detail pages should be using getter and setter directly.
PropertyAccessorElement get _setter;
}
class Library extends ModelElement {
static final Map<String, Library> _libraryMap = <String, Library>{};
@override
final Package package;
List<Class> _classes;
List<Class> _enums;
List<ModelFunction> _functions;
List<Typedef> _typeDefs;
List<TopLevelVariable> _variables;
Namespace _exportedNamespace;
String _name;
String _packageName;
factory Library(LibraryElement element, Package package) {
String key = element == null ? 'null' : element.name;
if (key.isEmpty) {
String name = element.definingCompilationUnit.name;
key = name.substring(0, name.length - '.dart'.length);
}
if (_libraryMap.containsKey(key)) {
return _libraryMap[key];
}
Library library = new Library._(element, package);
_libraryMap[key] = library;
return library;
}
Library._(LibraryElement element, this.package) : super(element, null) {
if (element == null) throw new ArgumentError.notNull('element');
_exportedNamespace =
new NamespaceBuilder().createExportNamespaceForLibrary(element);
}
List<Class> get allClasses => _allClasses;
List<Class> get classes {
return _allClasses
.where((c) => !c.isErrorOrException)
.toList(growable: false);
}
List<TopLevelVariable> get constants {
return _getVariables().where((v) => v.isConst).toList(growable: false)
..sort(byName);
}
String get dirName => name.replaceAll(':', '-');
/// Libraries are not enclosed by anything.
ModelElement get enclosingElement => null;
List<Class> get enums {
if (_enums != null) return _enums;
List<ClassElement> enumClasses = [];
enumClasses.addAll(_exportedNamespace.definedNames.values
.where((element) => element is ClassElement && element.isEnum));