| OLD | NEW |
| 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 library analysis_server.src.single_context_manager; | 5 library analysis_server.src.single_context_manager; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
| 9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
| 10 | 10 |
| 11 import 'package:analysis_server/src/context_manager.dart'; | 11 import 'package:analysis_server/src/context_manager.dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/plugin/resolver_provider.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart'; | 14 import 'package:analyzer/src/generated/engine.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart'; | 15 import 'package:analyzer/src/generated/sdk.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/util/glob.dart'; | 17 import 'package:analyzer/src/util/glob.dart'; |
| 17 import 'package:path/path.dart' as path; | 18 import 'package:path/path.dart' as path; |
| 18 import 'package:watcher/watcher.dart'; | 19 import 'package:watcher/watcher.dart'; |
| 19 | 20 |
| 20 /** | 21 /** |
| 21 * A function that will return a [UriResolver] that can be used to resolve | |
| 22 * `package:` URIs in [SingleContextManager]. | |
| 23 */ | |
| 24 typedef UriResolver PackageResolverProvider(); | |
| 25 | |
| 26 /** | |
| 27 * Implementation of [ContextManager] that supports only one [AnalysisContext]. | 22 * Implementation of [ContextManager] that supports only one [AnalysisContext]. |
| 28 * So, sources from all analysis roots are added to this single context. All | 23 * So, sources from all analysis roots are added to this single context. All |
| 29 * features that could otherwise cause creating additional contexts, such as | 24 * features that could otherwise cause creating additional contexts, such as |
| 30 * presence of `pubspec.yaml` or `.packages` files, or `.analysis_options` files | 25 * presence of `pubspec.yaml` or `.packages` files, or `.analysis_options` files |
| 31 * are ignored. | 26 * are ignored. |
| 32 */ | 27 */ |
| 33 class SingleContextManager implements ContextManager { | 28 class SingleContextManager implements ContextManager { |
| 34 /** | 29 /** |
| 35 * The [ResourceProvider] using which paths are converted into [Resource]s. | 30 * The [ResourceProvider] using which paths are converted into [Resource]s. |
| 36 */ | 31 */ |
| 37 final ResourceProvider resourceProvider; | 32 final ResourceProvider resourceProvider; |
| 38 | 33 |
| 39 /** | 34 /** |
| 40 * The context used to work with file system paths. | 35 * The context used to work with file system paths. |
| 41 */ | 36 */ |
| 42 path.Context pathContext; | 37 path.Context pathContext; |
| 43 | 38 |
| 44 /** | 39 /** |
| 45 * The manager used to access the SDK that should be associated with a | 40 * The manager used to access the SDK that should be associated with a |
| 46 * particular context. | 41 * particular context. |
| 47 */ | 42 */ |
| 48 final DartSdkManager sdkManager; | 43 final DartSdkManager sdkManager; |
| 49 | 44 |
| 50 /** | 45 /** |
| 51 * A function that will return a [UriResolver] that can be used to resolve | 46 * A function that will return a [UriResolver] that can be used to resolve |
| 52 * `package:` URIs. | 47 * `package:` URIs. |
| 53 */ | 48 */ |
| 54 final PackageResolverProvider packageResolverProvider; | 49 final ResolverProvider packageResolverProvider; |
| 55 | 50 |
| 56 /** | 51 /** |
| 57 * A list of the globs used to determine which files should be analyzed. | 52 * A list of the globs used to determine which files should be analyzed. |
| 58 */ | 53 */ |
| 59 final List<Glob> analyzedFilesGlobs; | 54 final List<Glob> analyzedFilesGlobs; |
| 60 | 55 |
| 61 /** | 56 /** |
| 57 * The default options used to create new analysis contexts. |
| 58 */ |
| 59 final AnalysisOptionsImpl defaultContextOptions; |
| 60 |
| 61 /** |
| 62 * The list of included paths (folders and files) most recently passed to | 62 * The list of included paths (folders and files) most recently passed to |
| 63 * [setRoots]. | 63 * [setRoots]. |
| 64 */ | 64 */ |
| 65 List<String> includedPaths = <String>[]; | 65 List<String> includedPaths = <String>[]; |
| 66 | 66 |
| 67 /** | 67 /** |
| 68 * The list of excluded paths (folders and files) most recently passed to | 68 * The list of excluded paths (folders and files) most recently passed to |
| 69 * [setRoots]. | 69 * [setRoots]. |
| 70 */ | 70 */ |
| 71 List<String> excludedPaths = <String>[]; | 71 List<String> excludedPaths = <String>[]; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 96 | 96 |
| 97 /** | 97 /** |
| 98 * The current watch subscriptions. | 98 * The current watch subscriptions. |
| 99 */ | 99 */ |
| 100 Map<String, StreamSubscription<WatchEvent>> watchSubscriptions = | 100 Map<String, StreamSubscription<WatchEvent>> watchSubscriptions = |
| 101 new Map<String, StreamSubscription<WatchEvent>>(); | 101 new Map<String, StreamSubscription<WatchEvent>>(); |
| 102 | 102 |
| 103 /** | 103 /** |
| 104 * The [packageResolverProvider] must not be `null`. | 104 * The [packageResolverProvider] must not be `null`. |
| 105 */ | 105 */ |
| 106 SingleContextManager(this.resourceProvider, this.sdkManager, | 106 SingleContextManager( |
| 107 this.packageResolverProvider, this.analyzedFilesGlobs) { | 107 this.resourceProvider, |
| 108 this.sdkManager, |
| 109 this.packageResolverProvider, |
| 110 this.analyzedFilesGlobs, |
| 111 this.defaultContextOptions) { |
| 108 pathContext = resourceProvider.pathContext; | 112 pathContext = resourceProvider.pathContext; |
| 109 } | 113 } |
| 110 | 114 |
| 111 @override | 115 @override |
| 112 Iterable<AnalysisContext> get analysisContexts => | 116 Iterable<AnalysisContext> get analysisContexts => |
| 113 context == null ? <AnalysisContext>[] : <AnalysisContext>[context]; | 117 context == null ? <AnalysisContext>[] : <AnalysisContext>[context]; |
| 114 | 118 |
| 115 @override | 119 @override |
| 116 Map<Folder, AnalysisContext> get folderMap => {contextFolder: context}; | 120 Map<Folder, AnalysisContext> get folderMap => {contextFolder: context}; |
| 117 | 121 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 193 } |
| 190 // Remember the subscription. | 194 // Remember the subscription. |
| 191 newSubscriptions[includedPath] = subscription; | 195 newSubscriptions[includedPath] = subscription; |
| 192 } | 196 } |
| 193 _cancelCurrentWatchSubscriptions(); | 197 _cancelCurrentWatchSubscriptions(); |
| 194 this.watchSubscriptions = newSubscriptions; | 198 this.watchSubscriptions = newSubscriptions; |
| 195 } | 199 } |
| 196 } | 200 } |
| 197 // Create or update the analysis context. | 201 // Create or update the analysis context. |
| 198 if (context == null) { | 202 if (context == null) { |
| 199 UriResolver packageResolver = packageResolverProvider(); | 203 UriResolver packageResolver = packageResolverProvider(contextFolder); |
| 200 context = callbacks.addContext(contextFolder, new AnalysisOptionsImpl(), | 204 context = callbacks.addContext(contextFolder, defaultContextOptions, |
| 201 new CustomPackageResolverDisposition(packageResolver)); | 205 new CustomPackageResolverDisposition(packageResolver)); |
| 202 ChangeSet changeSet = | 206 ChangeSet changeSet = |
| 203 _buildChangeSet(added: _includedFiles(includedPaths, excludedPaths)); | 207 _buildChangeSet(added: _includedFiles(includedPaths, excludedPaths)); |
| 204 callbacks.applyChangesToContext(contextFolder, changeSet); | 208 callbacks.applyChangesToContext(contextFolder, changeSet); |
| 205 } else { | 209 } else { |
| 206 // TODO(brianwilkerson) Optimize this. | 210 // TODO(brianwilkerson) Optimize this. |
| 207 List<File> oldFiles = | 211 List<File> oldFiles = |
| 208 _includedFiles(this.includedPaths, this.excludedPaths); | 212 _includedFiles(this.includedPaths, this.excludedPaths); |
| 209 List<File> newFiles = _includedFiles(includedPaths, excludedPaths); | 213 List<File> newFiles = _includedFiles(includedPaths, excludedPaths); |
| 210 ChangeSet changeSet = _buildChangeSet( | 214 ChangeSet changeSet = _buildChangeSet( |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 static List<Resource> _getChildrenSafe(Folder folder) { | 499 static List<Resource> _getChildrenSafe(Folder folder) { |
| 496 try { | 500 try { |
| 497 return folder.getChildren(); | 501 return folder.getChildren(); |
| 498 } on FileSystemException { | 502 } on FileSystemException { |
| 499 // The folder either doesn't exist or cannot be read. | 503 // The folder either doesn't exist or cannot be read. |
| 500 // Either way, there are no children. | 504 // Either way, there are no children. |
| 501 return const <Resource>[]; | 505 return const <Resource>[]; |
| 502 } | 506 } |
| 503 } | 507 } |
| 504 } | 508 } |
| OLD | NEW |