| 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 function that can translate the contents of files on disk as they are read. |
| 15 * This is now obsolete, but supported the ability of server to deal with |
| 16 * clients that convert all text to an internal format. |
| 17 */ |
| 18 typedef String FileReadMode(String s); |
| 19 |
| 20 /** |
| 21 * A source that represents a file. |
| 22 */ |
| 23 class FileSource extends Source { |
| 24 /** |
| 25 * A function that changes the way that files are read off of disk. |
| 26 */ |
| 27 static FileReadMode fileReadMode = (String s) => s; |
| 28 |
| 29 /** |
| 30 * Map from encoded URI/filepath pair to a unique integer identifier. This |
| 31 * identifier is used for equality tests and hash codes. |
| 32 * |
| 33 * The URI and filepath are joined into a pair by separating them with an '@' |
| 34 * character. |
| 35 */ |
| 36 static final Map<String, int> _idTable = new HashMap<String, int>(); |
| 37 |
| 38 /** |
| 39 * The URI from which this source was originally derived. |
| 40 */ |
| 41 @override |
| 42 final Uri uri; |
| 43 |
| 44 /** |
| 45 * The unique ID associated with this source. |
| 46 */ |
| 47 final int id; |
| 48 |
| 49 /** |
| 50 * The file represented by this source. |
| 51 */ |
| 52 final File file; |
| 53 |
| 54 /** |
| 55 * The cached absolute path of this source. |
| 56 */ |
| 57 String _absolutePath; |
| 58 |
| 59 /** |
| 60 * The cached encoding for this source. |
| 61 */ |
| 62 String _encoding; |
| 63 |
| 64 /** |
| 65 * Initialize a newly created source object to represent the given [file]. If |
| 66 * a [uri] is given, then it will be used as the URI from which the source was |
| 67 * derived, otherwise a `file:` URI will be created based on the [file]. |
| 68 */ |
| 69 FileSource(File file, [Uri uri]) |
| 70 : this.uri = uri ?? file.toUri(), |
| 71 this.file = file, |
| 72 id = _idTable.putIfAbsent( |
| 73 '${uri ?? file.toUri()}@${file.path}', () => _idTable.length); |
| 74 |
| 75 @override |
| 76 TimestampedData<String> get contents { |
| 77 return PerformanceStatistics.io.makeCurrentWhile(() { |
| 78 return contentsFromFile; |
| 79 }); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Get and return the contents and timestamp of the underlying file. |
| 84 * |
| 85 * Clients should consider using the method [AnalysisContext.getContents] |
| 86 * because contexts can have local overrides of the content of a source that |
| 87 * the source is not aware of. |
| 88 * |
| 89 * Throws an exception if the contents of this source could not be accessed. |
| 90 * See [contents]. |
| 91 */ |
| 92 TimestampedData<String> get contentsFromFile { |
| 93 return new TimestampedData<String>( |
| 94 modificationStamp, fileReadMode(file.readAsStringSync())); |
| 95 } |
| 96 |
| 97 @override |
| 98 String get encoding => _encoding ??= uri.toString(); |
| 99 |
| 100 @override |
| 101 String get fullName => _absolutePath ??= file.path; |
| 102 |
| 103 @override |
| 104 int get hashCode => uri.hashCode; |
| 105 |
| 106 @override |
| 107 bool get isInSystemLibrary => uri.scheme == DartUriResolver.DART_SCHEME; |
| 108 |
| 109 @override |
| 110 int get modificationStamp { |
| 111 try { |
| 112 return file.modificationStamp; |
| 113 } on FileSystemException { |
| 114 return -1; |
| 115 } |
| 116 } |
| 117 |
| 118 @override |
| 119 String get shortName => file.shortName; |
| 120 |
| 121 @override |
| 122 UriKind get uriKind => UriKind.fromScheme(uri.scheme); |
| 123 |
| 124 @override |
| 125 bool operator ==(Object object) { |
| 126 if (object is FileSource) { |
| 127 return id == object.id; |
| 128 } else if (object is Source) { |
| 129 return uri == object.uri; |
| 130 } |
| 131 return false; |
| 132 } |
| 133 |
| 134 @override |
| 135 bool exists() => file.exists; |
| 136 |
| 137 @override |
| 138 String toString() { |
| 139 if (file == null) { |
| 140 return "<unknown source>"; |
| 141 } |
| 142 return file.path; |
| 143 } |
| 144 } |
| OLD | NEW |