-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathgetter_setter_combo.dart
237 lines (195 loc) · 7.67 KB
/
getter_setter_combo.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
// Copyright (c) 2019, 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.
import 'dart:convert';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/utils.dart';
import 'package:dartdoc/src/warnings.dart';
/// Mixin for top-level variables and fields (aka properties)
mixin GetterSetterCombo on ModelElement {
Accessor get getter;
Accessor get setter;
Iterable<Accessor> get allAccessors sync* {
for (var a in [getter, setter]) {
if (a != null) yield a;
}
}
Set<String> get comboFeatures {
var allFeatures = <String>{};
if (hasExplicitGetter && hasPublicGetter) {
allFeatures.addAll(getter.features);
}
if (hasExplicitSetter && hasPublicSetter) {
allFeatures.addAll(setter.features);
}
if (readOnly && !isFinal && !isConst) allFeatures.add('read-only');
if (writeOnly) allFeatures.add('write-only');
if (readWrite) allFeatures.add('read / write');
return allFeatures;
}
@override
ModelElement enclosingElement;
bool get isInherited;
Expression get constantInitializer =>
(element as ConstVariableElement).constantInitializer;
String linkifyConstantValue(String original) {
if (constantInitializer is! InstanceCreationExpression) return original;
var creationExpression = constantInitializer as InstanceCreationExpression;
var constructorName = creationExpression.constructorName.toString();
Element staticElement = creationExpression.constructorName.staticElement;
if (staticElement == null) {
warn(PackageWarning.missingConstantConstructor, message: constructorName);
return original;
}
Constructor target = ModelElement.fromElement(staticElement, packageGraph);
Class targetClass = target.enclosingElement;
// TODO(jcollins-g): this logic really should be integrated into Constructor,
// but that's not trivial because of linkedName's usage.
if (targetClass.name == target.name) {
return original.replaceAll(constructorName, '${target.linkedName}');
}
return original.replaceAll('${targetClass.name}.${target.name}',
'${targetClass.linkedName}.${target.linkedName}');
}
String _buildConstantValueBase() {
var result = constantInitializer?.toString() ?? '';
return const HtmlEscape(HtmlEscapeMode.unknown).convert(result);
}
@override
CharacterLocation get characterLocation {
// Handle all synthetic possibilities. Ordinarily, warnings for
// explicit setters/getters will be handled by those objects, but
// if a warning comes up for an enclosing synthetic field we have to
// put it somewhere. So pick an accessor.
if (element.isSynthetic) {
if (hasExplicitGetter) return getter.characterLocation;
if (hasExplicitSetter) return setter.characterLocation;
assert(false, 'Field and accessors can not all be synthetic');
}
return super.characterLocation;
}
String get constantValue => linkifyConstantValue(constantValueBase);
String get constantValueTruncated =>
linkifyConstantValue(truncateString(constantValueBase, 200));
String _constantValueBase;
String get constantValueBase =>
_constantValueBase ??= _buildConstantValueBase();
bool get hasPublicGetter => hasGetter && getter.isPublic;
bool get hasPublicSetter => hasSetter && setter.isPublic;
@override
bool get isPublic => hasPublicGetter || hasPublicSetter;
List<ModelElement> _documentationFrom;
@override
List<ModelElement> get documentationFrom {
if (_documentationFrom == null) {
_documentationFrom = [];
if (hasPublicGetter) {
_documentationFrom.addAll(getter.documentationFrom);
} else if (hasPublicSetter) {
_documentationFrom.addAll(setter.documentationFrom);
}
if (_documentationFrom.isEmpty ||
_documentationFrom.every((e) => e.documentationComment == '')) {
_documentationFrom = computeDocumentationFrom;
}
}
return _documentationFrom;
}
bool get hasAccessorsWithDocs => (hasPublicGetter &&
!getter.isSynthetic &&
getter.documentation.isNotEmpty ||
hasPublicSetter &&
!setter.isSynthetic &&
setter.documentation.isNotEmpty);
bool get getterSetterBothAvailable => (hasPublicGetter &&
getter.documentation.isNotEmpty &&
hasPublicSetter &&
setter.documentation.isNotEmpty);
String _oneLineDoc;
@override
String get oneLineDoc {
if (_oneLineDoc == null) {
if (!hasAccessorsWithDocs) {
_oneLineDoc = super.oneLineDoc;
} else {
var buffer = StringBuffer();
if (hasPublicGetter && getter.oneLineDoc.isNotEmpty) {
buffer.write('${getter.oneLineDoc}');
}
if (hasPublicSetter && setter.oneLineDoc.isNotEmpty) {
buffer.write('${getterSetterBothAvailable ? "" : setter.oneLineDoc}');
}
_oneLineDoc = buffer.toString();
}
}
return _oneLineDoc;
}
String get getterSetterDocumentationComment {
var buffer = StringBuffer();
// Check for synthetic before public, always, or stack overflow.
if (hasGetter && !getter.isSynthetic && getter.isPublic) {
assert(getter.documentationFrom.length == 1);
// We have to check against dropTextFrom here since documentationFrom
// doesn't yield the real elements for GetterSetterCombos.
if (!config.dropTextFrom
.contains(getter.documentationFrom.first.element.library.name)) {
var docs = getter.documentationFrom.first.documentationComment;
if (docs != null) buffer.write(docs);
}
}
if (hasSetter && !setter.isSynthetic && setter.isPublic) {
assert(setter.documentationFrom.length == 1);
if (!config.dropTextFrom
.contains(setter.documentationFrom.first.element.library.name)) {
var docs = setter.documentationFrom.first.documentationComment;
if (docs != null) {
if (buffer.isNotEmpty) buffer.write('\n\n');
buffer.write(docs);
}
}
}
return buffer.toString();
}
String get linkedReturnType {
if (hasGetter) {
return getter.linkedReturnType;
} else {
// TODO(jcollins-g): this results in the wrong span class for the return
// type.
return setter.linkedParamsNoMetadataOrNames;
}
}
@override
bool get canHaveParameters => hasSetter;
@override
List<Parameter> get parameters => setter.parameters;
@override
String get linkedParamsNoMetadata {
if (hasSetter) return setter.linkedParamsNoMetadata;
return null;
}
bool get hasExplicitGetter => hasPublicGetter && !getter.isSynthetic;
bool get hasExplicitSetter => hasPublicSetter && !setter.isSynthetic;
bool get hasGetter => getter != null;
bool get hasNoGetterSetter => !hasGetterOrSetter;
bool get hasGetterOrSetter => hasExplicitGetter || hasExplicitSetter;
bool get hasSetter => setter != null;
bool get hasPublicGetterNoSetter => (hasPublicGetter && !hasPublicSetter);
String get arrow {
// →
if (readOnly) return r'→';
// ←
if (writeOnly) return r'←';
// ↔
if (readWrite) return r'↔';
throw UnsupportedError(
'GetterSetterCombo must be one of readOnly, writeOnly, or readWrite');
}
bool get readOnly => hasPublicGetter && !hasPublicSetter;
bool get readWrite => hasPublicGetter && hasPublicSetter;
bool get writeOnly => hasPublicSetter && !hasPublicGetter;
}