| Index: pkg/front_end/lib/src/base/file_repository.dart
|
| diff --git a/pkg/front_end/lib/src/base/file_repository.dart b/pkg/front_end/lib/src/base/file_repository.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5c55d86171c6c80e736b98ee63c24db8e42aa173
|
| --- /dev/null
|
| +++ b/pkg/front_end/lib/src/base/file_repository.dart
|
| @@ -0,0 +1,70 @@
|
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +/// Data structure storing an association between URI and file contents.
|
| +///
|
| +/// Each URI is also associated with a unique arbitrary path ending in ".dart".
|
| +/// This allows interfacing with analyzer code that expects to manipulate paths
|
| +/// rather than URIs.
|
| +class FileRepository {
|
| + /// Regular expression matching the arbitrary file paths generated by
|
| + /// [_pathForIndex].
|
| + static final _pathRegexp = new RegExp(r'^/[0-9]+\.dart$');
|
| +
|
| + /// The URIs currently stored in the repository.
|
| + final _uris = <Uri>[];
|
| +
|
| + /// Map from a URI to its index in [_uris].
|
| + final _indexForUri = <Uri, int>{};
|
| +
|
| + /// The file contents associated with the URIs in [_uris].
|
| + final _contents = <String>[];
|
| +
|
| + /// Return the contents of the file whose arbitary path is [path].
|
| + ///
|
| + /// The path must have been returned by a previous call to [store] or
|
| + /// [pathForUri].
|
| + String contentsForPath(String path) => _contents[_indexForPath(path)];
|
| +
|
| + /// Return the arbitrary path associated with [uri].
|
| + ///
|
| + /// The uri must have previously been passed to [store].
|
| + String pathForUri(Uri uri) {
|
| + int index = _indexForUri[uri];
|
| + assert(index != null);
|
| + return _pathForIndex(index);
|
| + }
|
| +
|
| + /// Associate the given [uri] with file [contents].
|
| + ///
|
| + /// The arbitrary path associated with the file is returned.
|
| + String store(Uri uri, String contents) {
|
| + int index = _indexForUri[uri];
|
| + if (index == null) {
|
| + index = _uris.length;
|
| + _uris.add(uri);
|
| + _indexForUri[uri] = index;
|
| + _contents.add(contents);
|
| + } else {
|
| + _contents[index] = contents;
|
| + }
|
| + return _pathForIndex(index);
|
| + }
|
| +
|
| + /// Return the URI for the file whose arbitrary path is [path].
|
| + ///
|
| + /// The path must have been returned by a previous call to [store] or
|
| + /// [pathForUri].
|
| + Uri uriForPath(String path) => _uris[_indexForPath(path)];
|
| +
|
| + /// Return the index into [_uris] and [_contents] matching the arbitrary path
|
| + /// [path].
|
| + int _indexForPath(String path) {
|
| + assert(_pathRegexp.hasMatch(path));
|
| + return int.parse(path.substring(1, path.length - 5));
|
| + }
|
| +
|
| + /// Return the arbitrary path associated with the given index.
|
| + String _pathForIndex(int index) => '/$index.dart';
|
| +}
|
|
|