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

Side by Side Diff: pkg/analyzer/lib/src/dart/analysis/file_state.dart

Issue 2473473002: Use FileContentOverlay instead of ContentCache in the new driver. (Closed)
Patch Set: Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:convert'; 5 import 'dart:convert';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart'; 8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/error/listener.dart'; 9 import 'package:analyzer/error/listener.dart';
10 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/src/dart/analysis/byte_store.dart'; 11 import 'package:analyzer/src/dart/analysis/byte_store.dart';
12 import 'package:analyzer/src/dart/analysis/driver.dart'; 12 import 'package:analyzer/src/dart/analysis/driver.dart';
13 import 'package:analyzer/src/dart/scanner/reader.dart'; 13 import 'package:analyzer/src/dart/scanner/reader.dart';
14 import 'package:analyzer/src/dart/scanner/scanner.dart'; 14 import 'package:analyzer/src/dart/scanner/scanner.dart';
15 import 'package:analyzer/src/generated/engine.dart'; 15 import 'package:analyzer/src/generated/engine.dart';
16 import 'package:analyzer/src/generated/parser.dart'; 16 import 'package:analyzer/src/generated/parser.dart';
17 import 'package:analyzer/src/generated/source.dart'; 17 import 'package:analyzer/src/generated/source.dart';
18 import 'package:analyzer/src/summary/format.dart'; 18 import 'package:analyzer/src/summary/format.dart';
19 import 'package:analyzer/src/summary/idl.dart'; 19 import 'package:analyzer/src/summary/idl.dart';
20 import 'package:analyzer/src/summary/summarize_ast.dart'; 20 import 'package:analyzer/src/summary/summarize_ast.dart';
21 import 'package:convert/convert.dart'; 21 import 'package:convert/convert.dart';
22 import 'package:crypto/crypto.dart'; 22 import 'package:crypto/crypto.dart';
23 23
24 /** 24 /**
25 * [FileContentOverlay] is used to temporary override content of files.
26 */
27 class FileContentOverlay {
28 final _map = <String, String>{};
29
30 /**
31 * Return the content of the file with the given [path], or `null` the
32 * overlay does not override the content of the file.
33 *
34 * The [path] must be absolute and normalized.
35 */
36 String operator [](String path) => _map[path];
37
38 /**
39 * Return the new [content] of the file with the given [path].
40 *
41 * The [path] must be absolute and normalized.
42 */
43 void operator []=(String path, String content) {
44 _map[path] = content;
Paul Berry 2016/11/01 19:37:31 If content is `null`, we should do `_map.remove(pa
45 }
46 }
47
48 /**
25 * Information about a file being analyzed, explicitly or implicitly. 49 * Information about a file being analyzed, explicitly or implicitly.
26 * 50 *
27 * It provides a consistent view on its properties. 51 * It provides a consistent view on its properties.
28 * 52 *
29 * The properties are not guaranteed to represent the most recent state 53 * The properties are not guaranteed to represent the most recent state
30 * of the file system. To update the file to the most recent state, [refresh] 54 * of the file system. To update the file to the most recent state, [refresh]
31 * should be called. 55 * should be called.
32 */ 56 */
33 class FileState { 57 class FileState {
34 final FileSystemState _fsState; 58 final FileSystemState _fsState;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 120
97 /** 121 /**
98 * Read the file content and ensure that all of the file properties are 122 * Read the file content and ensure that all of the file properties are
99 * consistent with the read content, including API signature. 123 * consistent with the read content, including API signature.
100 * 124 *
101 * Return `true` if the API signature changed since the last refresh. 125 * Return `true` if the API signature changed since the last refresh.
102 */ 126 */
103 bool refresh() { 127 bool refresh() {
104 // Read the content. 128 // Read the content.
105 try { 129 try {
106 _content = _fsState._contentCache.getContents(source); 130 _content = _fsState._contentOverlay[path];
107 _content ??= source.contents.data; 131 _content ??= _fsState._resourceProvider.getFile(path).readAsStringSync();
108 } catch (_) { 132 } catch (_) {
109 _content = ''; 133 _content = '';
110 // TODO(scheglov) We fail to report URI_DOES_NOT_EXIST. 134 // TODO(scheglov) We fail to report URI_DOES_NOT_EXIST.
111 // On one hand we need to provide an unlinked bundle to prevent 135 // On one hand we need to provide an unlinked bundle to prevent
112 // analysis context from reading the file (we want it to work 136 // analysis context from reading the file (we want it to work
113 // hermetically and handle one one file at a time). OTOH, 137 // hermetically and handle one one file at a time). OTOH,
114 // ResynthesizerResultProvider happily reports that any source in the 138 // ResynthesizerResultProvider happily reports that any source in the
115 // SummaryDataStore has MODIFICATION_TIME `0`. We need to return `-1` 139 // SummaryDataStore has MODIFICATION_TIME `0`. We need to return `-1`
116 // for missing files. Maybe add this feature to SummaryDataStore? 140 // for missing files. Maybe add this feature to SummaryDataStore?
117 } 141 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 _exportedFiles.add(file); 180 _exportedFiles.add(file);
157 } 181 }
158 for (String uriStr in _unlinked.publicNamespace.parts) { 182 for (String uriStr in _unlinked.publicNamespace.parts) {
159 FileState file = _fileForRelativeUri(uriStr); 183 FileState file = _fileForRelativeUri(uriStr);
160 _partedFiles.add(file); 184 _partedFiles.add(file);
161 } 185 }
162 // Return whether the API signature changed. 186 // Return whether the API signature changed.
163 return apiSignatureChanged; 187 return apiSignatureChanged;
164 } 188 }
165 189
190 @override
191 String toString() => path;
192
166 /** 193 /**
167 * Return the [FileState] for the given [relativeUri]. 194 * Return the [FileState] for the given [relativeUri].
168 */ 195 */
169 FileState _fileForRelativeUri(String relativeUri) { 196 FileState _fileForRelativeUri(String relativeUri) {
170 Source uriSource = _fsState._sourceFactory.resolveUri(source, relativeUri); 197 Source uriSource = _fsState._sourceFactory.resolveUri(source, relativeUri);
171 return _fsState.getFile(uriSource.fullName); 198 return _fsState.getFile(uriSource.fullName);
172 } 199 }
173 200
174 /** 201 /**
175 * Return `true` if the given byte lists are equal. 202 * Return `true` if the given byte lists are equal.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 } 239 }
213 } 240 }
214 241
215 /** 242 /**
216 * Information about known file system state. 243 * Information about known file system state.
217 */ 244 */
218 class FileSystemState { 245 class FileSystemState {
219 final PerformanceLog _logger; 246 final PerformanceLog _logger;
220 final ResourceProvider _resourceProvider; 247 final ResourceProvider _resourceProvider;
221 final ByteStore _byteStore; 248 final ByteStore _byteStore;
222 final ContentCache _contentCache; 249 final FileContentOverlay _contentOverlay;
223 final SourceFactory _sourceFactory; 250 final SourceFactory _sourceFactory;
224 final AnalysisOptions _analysisOptions; 251 final AnalysisOptions _analysisOptions;
225 252
226 final Map<String, FileState> _pathToFile = <String, FileState>{}; 253 final Map<String, FileState> _pathToFile = <String, FileState>{};
227 254
228 FileSystemState(this._logger, this._byteStore, this._contentCache, 255 FileSystemState(this._logger, this._byteStore, this._contentOverlay,
229 this._resourceProvider, this._sourceFactory, this._analysisOptions); 256 this._resourceProvider, this._sourceFactory, this._analysisOptions);
230 257
231 /** 258 /**
232 * Return the [FileState] for the give [path]. The returned file has the 259 * Return the [FileState] for the give [path]. The returned file has the
233 * last known state since if was last refreshed. 260 * last known state since if was last refreshed.
234 */ 261 */
235 FileState getFile(String path) { 262 FileState getFile(String path) {
236 FileState file = _pathToFile[path]; 263 FileState file = _pathToFile[path];
237 if (file == null) { 264 if (file == null) {
238 Source source = _sourceForPath(path); 265 Source source = _sourceForPath(path);
239 file = new FileState(this, path, source); 266 file = new FileState(this, path, source);
240 _pathToFile[path] = file; 267 _pathToFile[path] = file;
241 file.refresh(); 268 file.refresh();
242 } 269 }
243 return file; 270 return file;
244 } 271 }
245 272
246 /** 273 /**
247 * Return the [Source] for the given [path] in [_sourceFactory]. 274 * Return the [Source] for the given [path] in [_sourceFactory].
248 */ 275 */
249 Source _sourceForPath(String path) { 276 Source _sourceForPath(String path) {
250 Source fileSource = _resourceProvider.getFile(path).createSource(); 277 Source fileSource = _resourceProvider.getFile(path).createSource();
251 Uri uri = _sourceFactory.restoreUri(fileSource); 278 Uri uri = _sourceFactory.restoreUri(fileSource);
252 return _resourceProvider.getFile(path).createSource(uri); 279 return _resourceProvider.getFile(path).createSource(uri);
253 } 280 }
254 } 281 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/driver.dart ('k') | pkg/analyzer/test/src/dart/analysis/driver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698