Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Unified Diff: pkg/front_end/lib/src/base/file_repository.dart

Issue 2645923002: Create a "file repository" data structure to help interface analyzer and front_end code. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/front_end/lib/src/incremental_resolved_ast_generator_impl.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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';
+}
« no previous file with comments | « no previous file | pkg/front_end/lib/src/incremental_resolved_ast_generator_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698