File size: 12,044 Bytes
83fa459 | 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 | // Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/file_system/file_system.dart' as afs;
import 'package:analyzer/file_system/physical_file_system.dart' as afs;
import 'package:analyzer/source/line_info.dart';
import 'package:file/file.dart';
import 'data_types.dart';
import 'util.dart';
/// Gets an iterable over all of the blocks of documentation comments in a file
/// using the analyzer.
///
/// Each entry in the list is a list of source lines corresponding to the
/// documentation comment block.
Iterable<List<SourceLine>> getFileDocumentationComments(File file) {
return getDocumentationComments(getFileElements(file));
}
/// Gets an iterable over all of the blocks of documentation comments from an
/// iterable over the [SourceElement]s involved.
Iterable<List<SourceLine>> getDocumentationComments(
Iterable<SourceElement> elements) {
return elements
.where((SourceElement element) => element.comment.isNotEmpty)
.map<List<SourceLine>>((SourceElement element) => element.comment);
}
/// Gets an iterable over the comment [SourceElement]s in a file.
Iterable<SourceElement> getFileCommentElements(File file) {
return getCommentElements(getFileElements(file));
}
/// Filters the source `elements` to only return the comment elements.
Iterable<SourceElement> getCommentElements(Iterable<SourceElement> elements) {
return elements.where((SourceElement element) => element.comment.isNotEmpty);
}
/// Reads the file content from a string, to avoid having to read the file more
/// than once if the caller already has the content in memory.
///
/// The `file` argument is used to tag the lines with a filename that they came from.
Iterable<SourceElement> getElementsFromString(String content, File file) {
final ParseStringResult parseResult = parseString(
featureSet: FeatureSet.fromEnableFlags2(
sdkLanguageVersion: FlutterInformation.instance.getDartSdkVersion(),
flags: <String>[],
),
content: content);
final _SourceVisitor<CompilationUnit> visitor =
_SourceVisitor<CompilationUnit>(file);
visitor.visitCompilationUnit(parseResult.unit);
visitor.assignLineNumbers();
return visitor.elements;
}
/// Gets an iterable over the [SourceElement]s in the given `file`.
///
/// Takes an optional [ResourceProvider] to allow reading from a memory
/// filesystem.
Iterable<SourceElement> getFileElements(File file,
{afs.ResourceProvider? resourceProvider}) {
resourceProvider ??= afs.PhysicalResourceProvider.INSTANCE;
final ParseStringResult parseResult = parseFile(
featureSet: FeatureSet.fromEnableFlags2(
sdkLanguageVersion: FlutterInformation.instance.getDartSdkVersion(),
flags: <String>[],
),
path: file.absolute.path,
resourceProvider: resourceProvider);
final _SourceVisitor<CompilationUnit> visitor =
_SourceVisitor<CompilationUnit>(file);
visitor.visitCompilationUnit(parseResult.unit);
visitor.assignLineNumbers();
return visitor.elements;
}
class _SourceVisitor<T> extends RecursiveAstVisitor<T> {
_SourceVisitor(this.file) : elements = <SourceElement>{};
final Set<SourceElement> elements;
String enclosingClass = '';
File file;
void assignLineNumbers() {
final String contents = file.readAsStringSync();
final LineInfo lineInfo = LineInfo.fromContent(contents);
final Set<SourceElement> removedElements = <SourceElement>{};
final Set<SourceElement> replacedElements = <SourceElement>{};
for (final SourceElement element in elements) {
final List<SourceLine> newLines = <SourceLine>[];
for (final SourceLine line in element.comment) {
final CharacterLocation intervalLine =
lineInfo.getLocation(line.startChar);
newLines.add(line.copyWith(line: intervalLine.lineNumber));
}
final int elementLine = lineInfo.getLocation(element.startPos).lineNumber;
replacedElements
.add(element.copyWith(comment: newLines, startLine: elementLine));
removedElements.add(element);
}
elements.removeAll(removedElements);
elements.addAll(replacedElements);
}
List<SourceLine> _processComment(String element, Comment comment) {
final List<SourceLine> result = <SourceLine>[];
if (comment.tokens.isNotEmpty) {
for (final Token token in comment.tokens) {
result.add(SourceLine(
token.toString(),
element: element,
file: file,
startChar: token.charOffset,
endChar: token.charEnd,
));
}
}
return result;
}
@override
T? visitCompilationUnit(CompilationUnit node) {
elements.clear();
return super.visitCompilationUnit(node);
}
static bool isPublic(String name) {
return !name.startsWith('_');
}
static bool isInsideMethod(AstNode startNode) {
AstNode? node = startNode.parent;
while (node != null) {
if (node is MethodDeclaration) {
return true;
}
node = node.parent;
}
return false;
}
@override
T? visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
for (final VariableDeclaration declaration in node.variables.variables) {
if (!isPublic(declaration.name.lexeme)) {
continue;
}
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
comment = _processComment(
declaration.name.lexeme, node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.topLevelVariableType,
declaration.name.lexeme,
node.beginToken.charOffset,
file: file,
className: enclosingClass,
comment: comment,
),
);
}
return super.visitTopLevelVariableDeclaration(node);
}
@override
T? visitGenericTypeAlias(GenericTypeAlias node) {
if (isPublic(node.name.lexeme)) {
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
comment = _processComment(node.name.lexeme, node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.typedefType,
node.name.lexeme,
node.beginToken.charOffset,
file: file,
comment: comment,
),
);
}
return super.visitGenericTypeAlias(node);
}
@override
T? visitFieldDeclaration(FieldDeclaration node) {
for (final VariableDeclaration declaration in node.fields.variables) {
if (!isPublic(declaration.name.lexeme) || !isPublic(enclosingClass)) {
continue;
}
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
assert(enclosingClass.isNotEmpty);
comment = _processComment('$enclosingClass.${declaration.name.lexeme}',
node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.fieldType,
declaration.name.lexeme,
node.beginToken.charOffset,
file: file,
className: enclosingClass,
comment: comment,
override: _isOverridden(node),
),
);
return super.visitFieldDeclaration(node);
}
return null;
}
@override
T? visitConstructorDeclaration(ConstructorDeclaration node) {
final String fullName =
'$enclosingClass${node.name == null ? '' : '.${node.name}'}';
if (isPublic(enclosingClass) &&
(node.name == null || isPublic(node.name!.lexeme))) {
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
comment = _processComment(
'$enclosingClass.$fullName', node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.constructorType,
fullName,
node.beginToken.charOffset,
file: file,
className: enclosingClass,
comment: comment,
),
);
}
return super.visitConstructorDeclaration(node);
}
@override
T? visitFunctionDeclaration(FunctionDeclaration node) {
if (isPublic(node.name.lexeme)) {
List<SourceLine> comment = <SourceLine>[];
// Skip functions that are defined inside of methods.
if (!isInsideMethod(node)) {
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
comment =
_processComment(node.name.lexeme, node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.functionType,
node.name.lexeme,
node.beginToken.charOffset,
file: file,
comment: comment,
override: _isOverridden(node),
),
);
}
}
return super.visitFunctionDeclaration(node);
}
@override
T? visitMethodDeclaration(MethodDeclaration node) {
if (isPublic(node.name.lexeme) && isPublic(enclosingClass)) {
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
assert(enclosingClass.isNotEmpty);
comment = _processComment(
'$enclosingClass.${node.name.lexeme}', node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.methodType,
node.name.lexeme,
node.beginToken.charOffset,
file: file,
className: enclosingClass,
comment: comment,
override: _isOverridden(node),
),
);
}
return super.visitMethodDeclaration(node);
}
bool _isOverridden(AnnotatedNode node) {
return node.metadata.where((Annotation annotation) {
return annotation.name.name == 'override';
}).isNotEmpty;
}
@override
T? visitMixinDeclaration(MixinDeclaration node) {
enclosingClass = node.name.lexeme;
if (!node.name.lexeme.startsWith('_')) {
enclosingClass = node.name.lexeme;
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
comment = _processComment(node.name.lexeme, node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.classType,
node.name.lexeme,
node.beginToken.charOffset,
file: file,
comment: comment,
),
);
}
final T? result = super.visitMixinDeclaration(node);
enclosingClass = '';
return result;
}
@override
T? visitClassDeclaration(ClassDeclaration node) {
enclosingClass = node.name.lexeme;
if (!node.name.lexeme.startsWith('_')) {
enclosingClass = node.name.lexeme;
List<SourceLine> comment = <SourceLine>[];
if (node.documentationComment != null &&
node.documentationComment!.tokens.isNotEmpty) {
comment = _processComment(node.name.lexeme, node.documentationComment!);
}
elements.add(
SourceElement(
SourceElementType.classType,
node.name.lexeme,
node.beginToken.charOffset,
file: file,
comment: comment,
),
);
}
final T? result = super.visitClassDeclaration(node);
enclosingClass = '';
return result;
}
}
|