forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_node.dart
74 lines (62 loc) · 2.38 KB
/
model_node.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
// 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 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/model_utils.dart' as model_utils;
/// A stripped down [CommentReference] containing only that information needed
/// for Dartdoc. Drops link to the [CommentReference] after construction.
class ModelCommentReference {
final String name;
final Element staticElement;
ModelCommentReference(CommentReference ref)
: name = ref.identifier.name,
staticElement = ref.identifier.staticElement;
}
/// Stripped down information derived from [AstNode] containing only information
/// needed for Dartdoc. Drops link to the [AstNode] after construction.
class ModelNode {
final List<ModelCommentReference> commentRefs;
final Element element;
final int _sourceOffset;
final int _sourceEnd;
ModelNode(AstNode sourceNode, this.element)
: _sourceOffset = sourceNode?.offset,
_sourceEnd = sourceNode?.end,
commentRefs = _commentRefsFor(sourceNode);
static List<ModelCommentReference> _commentRefsFor(AstNode node) {
if (node is AnnotatedNode &&
node?.documentationComment?.references != null) {
return node.documentationComment.references
.map((c) => ModelCommentReference(c))
.toList(growable: false);
}
return null;
}
String _sourceCode;
String get sourceCode {
if (_sourceCode == null) {
if (_sourceOffset != null) {
var contents = model_utils.getFileContentsFor(element);
// Find the start of the line, so that we can line up all the indents.
var i = _sourceOffset;
while (i > 0) {
i -= 1;
if (contents[i] == '\n' || contents[i] == '\r') {
i += 1;
break;
}
}
// Trim the common indent from the source snippet.
var start = _sourceOffset - (_sourceOffset - i);
var source = contents.substring(start, _sourceEnd);
source = model_utils.stripIndentFromSource(source);
source = model_utils.stripDartdocCommentsFromSource(source);
_sourceCode = source.trim();
} else {
_sourceCode = '';
}
}
return _sourceCode;
}
}