Skip to content

Commit 86d5dfc

Browse files
committed
Unparse comment references via tokens instead of file access
1 parent e6a9b7c commit 86d5dfc

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/src/comment_references/model_comment_reference.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/element/element.dart';
77
import 'package:analyzer/file_system/file_system.dart';
88
import 'package:dartdoc/src/comment_references/parser.dart';
9-
import 'package:dartdoc/src/model_utils.dart';
109

1110
abstract class ModelCommentReference {
1211
/// Does the structure of the reference itself imply a possible default
@@ -70,9 +69,11 @@ class _ModelCommentReferenceImpl implements ModelCommentReference {
7069
/// [CommentReference].
7170
static String _referenceText(
7271
CommentReference ref, ResourceProvider resourceProvider) {
73-
var contents = getFileContentsFor(
74-
(ref.root as CompilationUnit).declaredElement, resourceProvider);
75-
return contents.substring(ref.offset, ref.end);
72+
var token = (ref.parent as Comment).tokens.firstWhere((t) => t.offset <= ref.offset && t.end >= ref.end);
73+
// This is a little sketchy, but works since comments happen to be a token
74+
// that is fully preserved in its string representation.
75+
// TODO(jcollins-g): replace unparsing in general with lower level changes.
76+
return token.toString().substring(ref.offset - token.offset, ref.end - token.offset);
7677
}
7778

7879
List<CommentReferenceNode> _parsed;

lib/src/model_utils.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ Iterable<Class> findCanonicalFor(Iterable<Class> classes) {
9696
c.packageGraph.findCanonicalModelElementFor(c.element) as Class ?? c);
9797
}
9898

99+
/// Uses direct file access to get the contents of a file, out of a
100+
/// cache if possible.
101+
///
102+
/// Direct reading of source code via a [PhysicalResourceProvider] is not
103+
/// allowed in some environments, so avoid this where possible.
104+
// TODO(jcollins-g): consider deprecating this and the `--include-source`
105+
// feature that uses it now that source code linking is possible.
99106
String getFileContentsFor(Element e, ResourceProvider resourceProvider) {
100107
var location = e.source.fullName;
101108
if (!_fileContents.containsKey(location)) {

0 commit comments

Comments
 (0)