Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library analyzer.src.source.source_resource; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 | |
| 9 import 'package:analyzer/file_system/file_system.dart'; | |
| 10 import 'package:analyzer/src/generated/engine.dart'; | |
| 11 import 'package:analyzer/src/generated/source.dart'; | |
| 12 | |
| 13 /** | |
| 14 * A source that represents a file. | |
| 15 */ | |
| 16 class FileSource extends Source { | |
| 17 /** | |
| 18 * A function that changes the way that files are read off of disk. | |
| 19 */ | |
| 20 static Function fileReadMode = (String s) => s; | |
| 21 | |
| 22 /** | |
| 23 * Map from encoded URI/filepath pair to a unique integer identifier. This | |
| 24 * identifier is used for equality tests and hash codes. | |
| 25 * | |
| 26 * The URI and filepath are joined into a pair by separating them with an '@' | |
| 27 * character. | |
| 28 */ | |
| 29 static final Map<String, int> _idTable = new HashMap<String, int>(); | |
| 30 | |
| 31 /** | |
| 32 * The URI from which this source was originally derived. | |
| 33 */ | |
| 34 @override | |
| 35 final Uri uri; | |
| 36 | |
| 37 /** | |
| 38 * The unique ID associated with this [FileBasedSource]. | |
| 39 */ | |
| 40 final int id; | |
| 41 | |
| 42 /** | |
| 43 * The file represented by this source. | |
| 44 */ | |
| 45 final File file; | |
| 46 | |
| 47 /** | |
| 48 * The cached absolute path of this source. | |
| 49 */ | |
| 50 String _absolutePath; | |
| 51 | |
| 52 /** | |
| 53 * The cached encoding for this source. | |
| 54 */ | |
| 55 String _encoding; | |
| 56 | |
| 57 /** | |
| 58 * Initialize a newly created source object to represent the given [file]. If | |
| 59 * a [uri] is given, then it will be used as the URI from which the source was | |
| 60 * derived, otherwise a `file:` URI will be created based on the [file]. | |
| 61 */ | |
| 62 FileSource(File file, [Uri uri]) | |
| 63 : this.uri = uri ?? file.toUri(), | |
| 64 this.file = file, | |
| 65 id = _idTable.putIfAbsent( | |
| 66 '${uri ?? file.toUri()}@${file.path}', () => _idTable.length); | |
| 67 | |
| 68 @override | |
| 69 TimestampedData<String> get contents { | |
| 70 return PerformanceStatistics.io.makeCurrentWhile(() { | |
| 71 return contentsFromFile; | |
| 72 }); | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Get the contents and timestamp of the underlying file. | |
| 77 * | |
| 78 * Clients should consider using the method [AnalysisContext.getContents] | |
| 79 * because contexts can have local overrides of the content of a source that t he source is not | |
| 80 * aware of. | |
| 81 * | |
| 82 * @return the contents of the source paired with the modification stamp of th e source | |
| 83 * @throws Exception if the contents of this source could not be accessed | |
| 84 * See [contents]. | |
| 85 */ | |
| 86 TimestampedData<String> get contentsFromFile { | |
| 87 return new TimestampedData<String>( | |
| 88 file.modificationStamp, fileReadMode(file.readAsStringSync())); | |
| 89 } | |
| 90 | |
| 91 @override | |
| 92 String get encoding { | |
| 93 if (_encoding == null) { | |
| 94 _encoding = uri.toString(); | |
|
scheglov
2016/08/16 00:54:26
This can be simplified with ??=.
Brian Wilkerson
2016/08/16 13:21:28
done
| |
| 95 } | |
| 96 return _encoding; | |
| 97 } | |
| 98 | |
| 99 @override | |
| 100 String get fullName { | |
| 101 if (_absolutePath == null) { | |
| 102 _absolutePath = file.path; | |
|
scheglov
2016/08/16 00:54:26
This can be simplified with ??=.
Brian Wilkerson
2016/08/16 13:21:28
done
| |
| 103 } | |
| 104 return _absolutePath; | |
| 105 } | |
| 106 | |
| 107 @override | |
| 108 int get hashCode => id; | |
| 109 | |
| 110 @override | |
| 111 bool get isInSystemLibrary => uri.scheme == DartUriResolver.DART_SCHEME; | |
| 112 | |
| 113 @override | |
| 114 int get modificationStamp => file.modificationStamp; | |
| 115 | |
| 116 @override | |
| 117 String get shortName => file.shortName; | |
| 118 | |
| 119 @override | |
| 120 UriKind get uriKind => UriKind.fromScheme(uri.scheme); | |
| 121 | |
| 122 @override | |
| 123 bool operator ==(Object object) { | |
| 124 if (object is FileSource) { | |
| 125 return id == object.id; | |
| 126 } else if (object is Source) { | |
| 127 return uri == object.uri; | |
| 128 } | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 @override | |
| 133 bool exists() => file.exists; | |
| 134 | |
| 135 @override | |
| 136 String toString() { | |
| 137 if (file == null) { | |
| 138 return "<unknown source>"; | |
| 139 } | |
| 140 return file.path; | |
| 141 } | |
| 142 } | |
| OLD | NEW |