| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, 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 dev_compiler.src.in_memory; | |
| 6 | |
| 7 import 'package:analyzer/src/generated/ast.dart'; | |
| 8 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | |
| 9 import 'package:analyzer/src/generated/source.dart'; | |
| 10 import 'package:path/path.dart' as path; | |
| 11 import 'package:source_span/source_span.dart'; | |
| 12 | |
| 13 import 'dependency_graph.dart' show runtimeFilesForServerMode; | |
| 14 | |
| 15 /// Uri resolver that can load test files from memory. | |
| 16 class InMemoryUriResolver extends UriResolver { | |
| 17 final Map<Uri, InMemorySource> files = <Uri, InMemorySource>{}; | |
| 18 | |
| 19 /// Whether to represent a non-existing file with a [TestSource] (default | |
| 20 /// behavior from analyzer), or to use null (possible when overriding the | |
| 21 /// package-url-resolvers.) | |
| 22 final bool representNonExistingFiles; | |
| 23 | |
| 24 InMemoryUriResolver(Map<String, String> allFiles, | |
| 25 {this.representNonExistingFiles: true}) { | |
| 26 allFiles.forEach((key, value) { | |
| 27 var uri = key.startsWith('package:') ? Uri.parse(key) : new Uri.file(key); | |
| 28 files[uri] = new InMemorySource(uri, value); | |
| 29 }); | |
| 30 | |
| 31 // TODO(vsm): Separate flag here? | |
| 32 if (representNonExistingFiles) { | |
| 33 runtimeFilesForServerMode.forEach((filepath) { | |
| 34 var uri = Uri.parse('/dev_compiler_runtime/$filepath'); | |
| 35 files[uri] = | |
| 36 new InMemorySource(uri, '/* test contents of $filepath */'); | |
| 37 }); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 Source resolveAbsolute(Uri uri) { | |
| 42 if (uri.scheme != 'file' && uri.scheme != 'package') return null; | |
| 43 if (!representNonExistingFiles) return files[uri]; | |
| 44 return files.putIfAbsent(uri, () => new InMemorySource(uri, null)); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 class InMemoryContents implements TimestampedData<String> { | |
| 49 int modificationTime; | |
| 50 String data; | |
| 51 | |
| 52 InMemoryContents(this.modificationTime, this.data); | |
| 53 } | |
| 54 | |
| 55 /// An in memory source file. | |
| 56 class InMemorySource implements Source { | |
| 57 final Uri uri; | |
| 58 InMemoryContents contents; | |
| 59 final SourceFile _file; | |
| 60 final UriKind uriKind; | |
| 61 | |
| 62 InMemorySource(uri, contents) | |
| 63 : uri = uri, | |
| 64 contents = new InMemoryContents(1, contents), | |
| 65 _file = contents != null ? new SourceFile(contents, url: uri) : null, | |
| 66 uriKind = uri.scheme == 'file' ? UriKind.FILE_URI : UriKind.PACKAGE_URI; | |
| 67 | |
| 68 bool exists() => contents.data != null; | |
| 69 | |
| 70 Source get source => this; | |
| 71 | |
| 72 String _encoding; | |
| 73 String get encoding => _encoding != null ? _encoding : (_encoding = '$uri'); | |
| 74 | |
| 75 String get fullName => uri.path; | |
| 76 | |
| 77 int get modificationStamp => contents.modificationTime; | |
| 78 String get shortName => path.basename(uri.path); | |
| 79 | |
| 80 operator ==(other) => other is InMemorySource && uri == other.uri; | |
| 81 int get hashCode => uri.hashCode; | |
| 82 bool get isInSystemLibrary => false; | |
| 83 | |
| 84 Uri resolveRelativeUri(Uri relativeUri) => uri.resolveUri(relativeUri); | |
| 85 | |
| 86 SourceSpan spanFor(AstNode node) { | |
| 87 final begin = node is AnnotatedNode | |
| 88 ? node.firstTokenAfterCommentAndMetadata.offset | |
| 89 : node.offset; | |
| 90 return _file.span(begin, node.end); | |
| 91 } | |
| 92 | |
| 93 String toString() => '[$runtimeType: $uri]'; | |
| 94 } | |
| OLD | NEW |