Skip to content

Commit e8d0bd7

Browse files
committed
Merge pull request #1064 from keertip/caching
add caching for file contents
2 parents fe4eee6 + cb5379d commit e8d0bd7

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

lib/src/model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ abstract class SourceCodeMixin {
13801380

13811381
String get sourceCode {
13821382
if (_sourceCodeCache == null) {
1383-
String contents = element.source.contents.data;
1383+
String contents = getFileContentsFor(element);
13841384
var node = element.computeNode();
13851385
if (node != null) {
13861386
// Find the start of the line, so that we can line up all the indents.

lib/src/model_utils.dart

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,24 @@
44

55
library dartdoc.model_utils;
66

7+
import 'dart:io';
8+
79
import 'package:analyzer/src/generated/element.dart';
810
import 'package:analyzer/src/generated/engine.dart';
911
import 'package:analyzer/src/generated/sdk.dart';
1012
import 'package:analyzer/src/generated/source_io.dart';
1113

12-
bool isPrivate(Element e) => e.name.startsWith('_');
14+
final Map<String, String> _fileContents = <String, String>{};
1315

14-
bool isPublic(Element e) {
15-
if (isPrivate(e)) return false;
16-
// check to see if element is part of the public api, that is it does not
17-
// have a '#nodoc' in the documentation comment
18-
if (e is PropertyAccessorElement && e.isSynthetic) {
19-
var accessor = (e as PropertyAccessorElement);
20-
if (accessor.correspondingSetter != null &&
21-
!accessor.correspondingSetter.isSynthetic) {
22-
e = accessor.correspondingSetter;
23-
} else if (accessor.correspondingGetter != null &&
24-
!accessor.correspondingGetter.isSynthetic) {
25-
e = accessor.correspondingGetter;
26-
} else {
27-
e = accessor.variable;
28-
}
29-
}
16+
List<InterfaceType> getAllSupertypes(ClassElement c) => c.allSupertypes;
3017

31-
var docComment = e.computeDocumentationComment();
32-
if (docComment != null && docComment.contains('<nodoc>')) return false;
33-
return true;
18+
String getFileContentsFor(Element e) {
19+
var location = e.source.fullName;
20+
if (!_fileContents.containsKey(location)) {
21+
var contents = new File(location).readAsStringSync();
22+
_fileContents.putIfAbsent(location, () => contents);
23+
}
24+
return _fileContents[location];
3425
}
3526

3627
Iterable<LibraryElement> getSdkLibrariesToDocument(
@@ -48,22 +39,34 @@ Iterable<LibraryElement> getSdkLibrariesToDocument(
4839
}
4940
}
5041

51-
List<InterfaceType> getAllSupertypes(ClassElement c) => c.allSupertypes;
52-
5342
bool isInExportedLibraries(
5443
List<LibraryElement> libraries, LibraryElement library) {
5544
return libraries
5645
.any((lib) => lib == library || lib.exportedLibraries.contains(library));
5746
}
5847

59-
/// Strip the common indent from the given source fragment.
60-
String stripIndentFromSource(String source) {
61-
String remainer = source.trimLeft();
62-
String indent = source.substring(0, source.length - remainer.length);
63-
return source.split('\n').map((line) {
64-
line = line.trimRight();
65-
return line.startsWith(indent) ? line.substring(indent.length) : line;
66-
}).join('\n');
48+
bool isPrivate(Element e) => e.name.startsWith('_');
49+
50+
bool isPublic(Element e) {
51+
if (isPrivate(e)) return false;
52+
// check to see if element is part of the public api, that is it does not
53+
// have a '#nodoc' in the documentation comment
54+
if (e is PropertyAccessorElement && e.isSynthetic) {
55+
var accessor = (e as PropertyAccessorElement);
56+
if (accessor.correspondingSetter != null &&
57+
!accessor.correspondingSetter.isSynthetic) {
58+
e = accessor.correspondingSetter;
59+
} else if (accessor.correspondingGetter != null &&
60+
!accessor.correspondingGetter.isSynthetic) {
61+
e = accessor.correspondingGetter;
62+
} else {
63+
e = accessor.variable;
64+
}
65+
}
66+
67+
var docComment = e.computeDocumentationComment();
68+
if (docComment != null && docComment.contains('<nodoc>')) return false;
69+
return true;
6770
}
6871

6972
/// Strip leading dartdoc comments from the given source code.
@@ -89,3 +92,13 @@ String stripDartdocCommentsFromSource(String source) {
8992
return true;
9093
}).join('\n');
9194
}
95+
96+
/// Strip the common indent from the given source fragment.
97+
String stripIndentFromSource(String source) {
98+
String remainer = source.trimLeft();
99+
String indent = source.substring(0, source.length - remainer.length);
100+
return source.split('\n').map((line) {
101+
line = line.trimRight();
102+
return line.startsWith(indent) ? line.substring(indent.length) : line;
103+
}).join('\n');
104+
}

0 commit comments

Comments
 (0)