| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Data structure storing an association between URI and file contents. | 5 /// Data structure storing an association between URI and file contents. |
| 6 /// | 6 /// |
| 7 /// Each URI is also associated with a unique arbitrary path ending in ".dart". | 7 /// Each URI is also associated with a unique arbitrary path ending in ".dart". |
| 8 /// This allows interfacing with analyzer code that expects to manipulate paths | 8 /// This allows interfacing with analyzer code that expects to manipulate paths |
| 9 /// rather than URIs. | 9 /// rather than URIs. |
| 10 class FileRepository { | 10 class FileRepository { |
| 11 /// Regular expression matching the arbitrary file paths generated by | 11 /// Regular expression matching the arbitrary file paths generated by |
| 12 /// [_pathForIndex]. | 12 /// [_pathForIndex]. |
| 13 static final _pathRegexp = new RegExp(r'^/[0-9]+\.dart$'); | 13 static final _pathRegexp = new RegExp(r'^/[0-9]+\.dart$'); |
| 14 | 14 |
| 15 /// The URIs currently stored in the repository. | 15 /// The URIs currently stored in the repository. |
| 16 final _uris = <Uri>[]; | 16 final _uris = <Uri>[]; |
| 17 | 17 |
| 18 /// Map from a URI to its index in [_uris]. | 18 /// Map from a URI to its index in [_uris]. |
| 19 final _indexForUri = <Uri, int>{}; | 19 final _indexForUri = <Uri, int>{}; |
| 20 | 20 |
| 21 /// The file contents associated with the URIs in [_uris]. | 21 /// The file contents associated with the URIs in [_uris]. |
| 22 final _contents = <String>[]; | 22 final _contents = <String>[]; |
| 23 | 23 |
| 24 /// Clear any contents stored in the file repository. The association between |
| 25 /// URI and arbitrary path is preserved. |
| 26 /// |
| 27 /// Subsequent calls to [contentsForPath] will have undefined results until |
| 28 /// new contents are stored using [store]. |
| 29 void clearContents() { |
| 30 for (var i = 0; i < _contents.length; i++) { |
| 31 _contents[i] = null; |
| 32 } |
| 33 } |
| 34 |
| 24 /// Return the contents of the file whose arbitary path is [path]. | 35 /// Return the contents of the file whose arbitary path is [path]. |
| 25 /// | 36 /// |
| 26 /// The path must have been returned by a previous call to [store] or | 37 /// The path must have been returned by a previous call to [store] or |
| 27 /// [pathForUri]. | 38 /// [pathForUri]. |
| 28 String contentsForPath(String path) => _contents[_indexForPath(path)]; | 39 String contentsForPath(String path) { |
| 40 var contents = _contents[_indexForPath(path)]; |
| 41 assert(contents != null); |
| 42 return contents; |
| 43 } |
| 44 |
| 45 /// For testing purposes, return the contents of all files stored in the file |
| 46 /// repository, as a map from path to contents string. |
| 47 Map<String, String> getContentsForTesting() { |
| 48 var result = <String, String>{}; |
| 49 for (var i = 0; i < _contents.length; i++) { |
| 50 if (_contents[i] != null) result[_pathForIndex(i)] = _contents[i]; |
| 51 } |
| 52 return result; |
| 53 } |
| 29 | 54 |
| 30 /// Return the arbitrary path associated with [uri]. | 55 /// Return the arbitrary path associated with [uri]. |
| 31 /// | 56 /// |
| 32 /// The uri must have previously been passed to [store]. | 57 /// The uri must have previously been passed to [store]. |
| 33 String pathForUri(Uri uri) { | 58 String pathForUri(Uri uri) { |
| 34 int index = _indexForUri[uri]; | 59 int index = _indexForUri[uri]; |
| 35 assert(index != null); | 60 assert(index != null); |
| 36 return _pathForIndex(index); | 61 return _pathForIndex(index); |
| 37 } | 62 } |
| 38 | 63 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 61 /// Return the index into [_uris] and [_contents] matching the arbitrary path | 86 /// Return the index into [_uris] and [_contents] matching the arbitrary path |
| 62 /// [path]. | 87 /// [path]. |
| 63 int _indexForPath(String path) { | 88 int _indexForPath(String path) { |
| 64 assert(_pathRegexp.hasMatch(path)); | 89 assert(_pathRegexp.hasMatch(path)); |
| 65 return int.parse(path.substring(1, path.length - 5)); | 90 return int.parse(path.substring(1, path.length - 5)); |
| 66 } | 91 } |
| 67 | 92 |
| 68 /// Return the arbitrary path associated with the given index. | 93 /// Return the arbitrary path associated with the given index. |
| 69 String _pathForIndex(int index) => '/$index.dart'; | 94 String _pathForIndex(int index) => '/$index.dart'; |
| 70 } | 95 } |
| OLD | NEW |