| 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 _uriToIndexMap = <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 | 24 /// Clear any contents stored in the file repository. The association between |
| 25 /// URI and arbitrary path is preserved. | 25 /// URI and arbitrary path is preserved. |
| 26 /// | 26 /// |
| 27 /// Subsequent calls to [contentsForPath] will have undefined results until | 27 /// Subsequent calls to [contentsForPath] will have undefined results until |
| 28 /// new contents are stored using [store]. | 28 /// new contents are stored using [store]. |
| 29 void clearContents() { | 29 void clearContents() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 47 Map<String, String> getContentsForTesting() { | 47 Map<String, String> getContentsForTesting() { |
| 48 var result = <String, String>{}; | 48 var result = <String, String>{}; |
| 49 for (var i = 0; i < _contents.length; i++) { | 49 for (var i = 0; i < _contents.length; i++) { |
| 50 if (_contents[i] != null) result[_pathForIndex(i)] = _contents[i]; | 50 if (_contents[i] != null) result[_pathForIndex(i)] = _contents[i]; |
| 51 } | 51 } |
| 52 return result; | 52 return result; |
| 53 } | 53 } |
| 54 | 54 |
| 55 /// Return the arbitrary path associated with [uri]. | 55 /// Return the arbitrary path associated with [uri]. |
| 56 /// | 56 /// |
| 57 /// The uri must have previously been passed to [store]. | 57 /// If [allocate] is `false` (the default), the uri must have previously been |
| 58 String pathForUri(Uri uri) { | 58 /// allocated a corresponding path, e.g. via a call to [store]. If [allocate] |
| 59 int index = _indexForUri[uri]; | 59 /// is `true`, then a new path will be allocated if necessary. |
| 60 assert(index != null); | 60 String pathForUri(Uri uri, {bool allocate: false}) { |
| 61 return _pathForIndex(index); | 61 return _pathForIndex(_indexForUri(uri, allocate)); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /// Associate the given [uri] with file [contents]. | 64 /// Associate the given [uri] with file [contents]. |
| 65 /// | 65 /// |
| 66 /// The arbitrary path associated with the file is returned. | 66 /// The arbitrary path associated with the file is returned. |
| 67 String store(Uri uri, String contents) { | 67 String store(Uri uri, String contents) { |
| 68 int index = _indexForUri[uri]; | 68 int index = _indexForUri(uri, true); |
| 69 if (index == null) { | 69 _contents[index] = contents; |
| 70 index = _uris.length; | |
| 71 _uris.add(uri); | |
| 72 _indexForUri[uri] = index; | |
| 73 _contents.add(contents); | |
| 74 } else { | |
| 75 _contents[index] = contents; | |
| 76 } | |
| 77 return _pathForIndex(index); | 70 return _pathForIndex(index); |
| 78 } | 71 } |
| 79 | 72 |
| 80 /// Return the URI for the file whose arbitrary path is [path]. | 73 /// Return the URI for the file whose arbitrary path is [path]. |
| 81 /// | 74 /// |
| 82 /// The path must have been returned by a previous call to [store] or | 75 /// The path must have been returned by a previous call to [store] or |
| 83 /// [pathForUri]. | 76 /// [pathForUri]. |
| 84 Uri uriForPath(String path) => _uris[_indexForPath(path)]; | 77 Uri uriForPath(String path) => _uris[_indexForPath(path)]; |
| 85 | 78 |
| 86 /// Return the index into [_uris] and [_contents] matching the arbitrary path | 79 /// Return the index into [_uris] and [_contents] matching the arbitrary path |
| 87 /// [path]. | 80 /// [path]. |
| 88 int _indexForPath(String path) { | 81 int _indexForPath(String path) { |
| 89 assert(_pathRegexp.hasMatch(path)); | 82 assert(_pathRegexp.hasMatch(path)); |
| 90 return int.parse(path.substring(1, path.length - 5)); | 83 return int.parse(path.substring(1, path.length - 5)); |
| 91 } | 84 } |
| 92 | 85 |
| 86 int _indexForUri(Uri uri, bool allocate) { |
| 87 int index = _uriToIndexMap[uri]; |
| 88 assert(allocate || index != null); |
| 89 if (index == null) { |
| 90 index = _uris.length; |
| 91 _uris.add(uri); |
| 92 _uriToIndexMap[uri] = index; |
| 93 _contents.add(null); |
| 94 } |
| 95 return index; |
| 96 } |
| 97 |
| 93 /// Return the arbitrary path associated with the given index. | 98 /// Return the arbitrary path associated with the given index. |
| 94 String _pathForIndex(int index) => '/$index.dart'; | 99 String _pathForIndex(int index) => '/$index.dart'; |
| 95 } | 100 } |
| OLD | NEW |