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

Side by Side Diff: pkg/analysis_server/lib/src/context_manager.dart

Issue 2350143004: More clean-up in server (Closed)
Patch Set: Created 4 years, 3 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 context.directory.manager; 5 library context.directory.manager;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 import 'dart:convert'; 9 import 'dart:convert';
10 import 'dart:core'; 10 import 'dart:core';
11 11
12 import 'package:analysis_server/src/analysis_server.dart'; 12 import 'package:analysis_server/src/analysis_server.dart';
13 import 'package:analyzer/exception/exception.dart';
13 import 'package:analyzer/file_system/file_system.dart'; 14 import 'package:analyzer/file_system/file_system.dart';
14 import 'package:analyzer/instrumentation/instrumentation.dart'; 15 import 'package:analyzer/instrumentation/instrumentation.dart';
15 import 'package:analyzer/plugin/options.dart'; 16 import 'package:analyzer/plugin/options.dart';
16 import 'package:analyzer/plugin/resolver_provider.dart'; 17 import 'package:analyzer/plugin/resolver_provider.dart';
17 import 'package:analyzer/source/analysis_options_provider.dart'; 18 import 'package:analyzer/source/analysis_options_provider.dart';
18 import 'package:analyzer/source/config.dart'; 19 import 'package:analyzer/source/config.dart';
19 import 'package:analyzer/source/package_map_provider.dart'; 20 import 'package:analyzer/source/package_map_provider.dart';
20 import 'package:analyzer/source/package_map_resolver.dart'; 21 import 'package:analyzer/source/package_map_resolver.dart';
21 import 'package:analyzer/source/path_filter.dart'; 22 import 'package:analyzer/source/path_filter.dart';
22 import 'package:analyzer/source/pub_package_map_provider.dart'; 23 import 'package:analyzer/source/pub_package_map_provider.dart';
23 import 'package:analyzer/source/sdk_ext.dart'; 24 import 'package:analyzer/source/sdk_ext.dart';
24 import 'package:analyzer/src/context/builder.dart'; 25 import 'package:analyzer/src/context/builder.dart';
25 import 'package:analyzer/src/context/context.dart' as context; 26 import 'package:analyzer/src/context/context.dart' as context;
26 import 'package:analyzer/src/dart/sdk/sdk.dart'; 27 import 'package:analyzer/src/dart/sdk/sdk.dart';
27 import 'package:analyzer/src/generated/engine.dart'; 28 import 'package:analyzer/src/generated/engine.dart';
28 import 'package:analyzer/exception/exception.dart';
29 import 'package:analyzer/src/generated/java_io.dart'; 29 import 'package:analyzer/src/generated/java_io.dart';
30 import 'package:analyzer/src/generated/sdk.dart'; 30 import 'package:analyzer/src/generated/sdk.dart';
31 import 'package:analyzer/src/generated/source.dart'; 31 import 'package:analyzer/src/generated/source.dart';
32 import 'package:analyzer/src/generated/source_io.dart'; 32 import 'package:analyzer/src/generated/source_io.dart';
33 import 'package:analyzer/src/task/options.dart'; 33 import 'package:analyzer/src/task/options.dart';
34 import 'package:analyzer/src/util/absolute_path.dart'; 34 import 'package:analyzer/src/util/absolute_path.dart';
35 import 'package:analyzer/src/util/glob.dart'; 35 import 'package:analyzer/src/util/glob.dart';
36 import 'package:analyzer/src/util/yaml.dart'; 36 import 'package:analyzer/src/util/yaml.dart';
37 import 'package:package_config/packages.dart'; 37 import 'package:package_config/packages.dart';
38 import 'package:package_config/packages_file.dart' as pkgfile show parse; 38 import 'package:package_config/packages_file.dart' as pkgfile show parse;
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 * operations are in progress, and (c) determine which files should be 315 * operations are in progress, and (c) determine which files should be
316 * analyzed. 316 * analyzed.
317 * 317 *
318 * TODO(paulberry): eliminate this interface, and instead have [ContextManager] 318 * TODO(paulberry): eliminate this interface, and instead have [ContextManager]
319 * operations return data structures describing how context state should be 319 * operations return data structures describing how context state should be
320 * modified. 320 * modified.
321 */ 321 */
322 abstract class ContextManagerCallbacks { 322 abstract class ContextManagerCallbacks {
323 /** 323 /**
324 * Create and return a new analysis context rooted at the given [folder], with 324 * Create and return a new analysis context rooted at the given [folder], with
325 * the given analysis [options], allowing [disposition] to govern details of 325 * the given analysis [options].
326 * how the context is to be created.
327 */ 326 */
328 AnalysisContext addContext( 327 AnalysisContext addContext(Folder folder, AnalysisOptions options);
329 Folder folder, AnalysisOptions options, FolderDisposition disposition);
330 328
331 /** 329 /**
332 * Called when the set of files associated with a context have changed (or 330 * Called when the set of files associated with a context have changed (or
333 * some of those files have been modified). [changeSet] is the set of 331 * some of those files have been modified). [changeSet] is the set of
334 * changes that need to be applied to the context. 332 * changes that need to be applied to the context.
335 */ 333 */
336 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet); 334 void applyChangesToContext(Folder contextFolder, ChangeSet changeSet);
337 335
338 /** 336 /**
339 * Signals that the context manager has started to compute a package map (if 337 * Signals that the context manager has started to compute a package map (if
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 _computeFolderDisposition(folder, dependencies.add, packagespecFile); 1028 _computeFolderDisposition(folder, dependencies.add, packagespecFile);
1031 ContextInfo info = new ContextInfo(this, parent, folder, packagespecFile, 1029 ContextInfo info = new ContextInfo(this, parent, folder, packagespecFile,
1032 normalizedPackageRoots[folder.path], disposition); 1030 normalizedPackageRoots[folder.path], disposition);
1033 1031
1034 Map<String, Object> optionMap = readOptions(info.folder); 1032 Map<String, Object> optionMap = readOptions(info.folder);
1035 AnalysisOptions options = 1033 AnalysisOptions options =
1036 new AnalysisOptionsImpl.from(defaultContextOptions); 1034 new AnalysisOptionsImpl.from(defaultContextOptions);
1037 applyToAnalysisOptions(options, optionMap); 1035 applyToAnalysisOptions(options, optionMap);
1038 1036
1039 info.setDependencies(dependencies); 1037 info.setDependencies(dependencies);
1040 info.context = callbacks.addContext(folder, options, disposition); 1038 info.context = callbacks.addContext(folder, options);
1041 folderMap[folder] = info.context; 1039 folderMap[folder] = info.context;
1042 info.context.name = folder.path; 1040 info.context.name = folder.path;
1043 1041
1044 // Look for pubspec-specified analysis configuration. 1042 // Look for pubspec-specified analysis configuration.
1045 File pubspec; 1043 File pubspec;
1046 if (packagespecFile?.exists == true) { 1044 if (packagespecFile?.exists == true) {
1047 if (packagespecFile.shortName == PUBSPEC_NAME) { 1045 if (packagespecFile.shortName == PUBSPEC_NAME) {
1048 pubspec = packagespecFile; 1046 pubspec = packagespecFile;
1049 } 1047 }
1050 } 1048 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 ChangeSet changeSet = new ChangeSet(); 1119 ChangeSet changeSet = new ChangeSet();
1122 _addSourceFiles(changeSet, folder, parent); 1120 _addSourceFiles(changeSet, folder, parent);
1123 callbacks.applyChangesToContext(folder, changeSet); 1121 callbacks.applyChangesToContext(folder, changeSet);
1124 } 1122 }
1125 } 1123 }
1126 1124
1127 /** 1125 /**
1128 * Set up a [SourceFactory] that resolves packages as appropriate for the 1126 * Set up a [SourceFactory] that resolves packages as appropriate for the
1129 * given [disposition]. 1127 * given [disposition].
1130 */ 1128 */
1131 SourceFactory _createSourceFactory(InternalAnalysisContext context, 1129 SourceFactory _createSourceFactory(
1132 AnalysisOptions options, FolderDisposition disposition, Folder folder) { 1130 InternalAnalysisContext context, AnalysisOptions options, Folder folder) {
1133 List<UriResolver> resolvers = []; 1131 ContextBuilder builder = callbacks.createContextBuilder(folder, options);
1134 List<UriResolver> packageUriResolvers = 1132 return builder.createSourceFactory(folder.path, options);
1135 disposition.createPackageUriResolvers(resourceProvider);
1136
1137 EmbedderYamlLocator locator =
1138 disposition.getEmbedderLocator(resourceProvider);
1139 Map<Folder, YamlMap> embedderYamls = locator.embedderYamls;
1140 EmbedderSdk embedderSdk = new EmbedderSdk(resourceProvider, embedderYamls);
1141 if (embedderSdk.libraryMap.size() == 0) {
1142 // There was no embedder file, or the file was empty, so used the default
1143 // SDK.
1144 resolvers.add(new DartUriResolver(sdkManager.getSdkForOptions(options)));
1145 } else {
1146 // The embedder file defines an alternate SDK, so use it.
1147 List<String> paths = <String>[];
1148 for (Folder folder in embedderYamls.keys) {
1149 paths.add(folder
1150 .getChildAssumingFile(EmbedderYamlLocator.EMBEDDER_FILE_NAME)
1151 .path);
1152 }
1153 DartSdk dartSdk =
1154 sdkManager.getSdk(new SdkDescription(paths, options), () {
1155 embedderSdk.analysisOptions = options;
1156 // TODO(brianwilkerson) Enable summary use after we have decided where
1157 // summary files for embedder files will live.
1158 embedderSdk.useSummary = false;
1159 return embedderSdk;
1160 });
1161 resolvers.add(new DartUriResolver(dartSdk));
1162 }
1163
1164 resolvers.addAll(packageUriResolvers);
1165 resolvers.add(new ResourceUriResolver(resourceProvider));
1166 return new SourceFactory(resolvers, disposition.packages);
1167 } 1133 }
1168 1134
1169 /** 1135 /**
1170 * Clean up and destroy the context associated with the given folder. 1136 * Clean up and destroy the context associated with the given folder.
1171 */ 1137 */
1172 void _destroyContext(ContextInfo info) { 1138 void _destroyContext(ContextInfo info) {
1173 changeSubscriptions.remove(info.folder)?.cancel(); 1139 changeSubscriptions.remove(info.folder)?.cancel();
1174 callbacks.removeContext(info.folder, _computeFlushedFiles(info)); 1140 callbacks.removeContext(info.folder, _computeFlushedFiles(info));
1175 bool wasRemoved = info.parent.children.remove(info); 1141 bool wasRemoved = info.parent.children.remove(info);
1176 assert(wasRemoved); 1142 assert(wasRemoved);
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
1541 /** 1507 /**
1542 * Recompute the [FolderDisposition] for the context described by [info], 1508 * Recompute the [FolderDisposition] for the context described by [info],
1543 * and update the client appropriately. 1509 * and update the client appropriately.
1544 */ 1510 */
1545 void _recomputeFolderDisposition(ContextInfo info) { 1511 void _recomputeFolderDisposition(ContextInfo info) {
1546 // TODO(paulberry): when computePackageMap is changed into an 1512 // TODO(paulberry): when computePackageMap is changed into an
1547 // asynchronous API call, we'll want to suspend analysis for this context 1513 // asynchronous API call, we'll want to suspend analysis for this context
1548 // while we're rerunning "pub list", since any analysis we complete while 1514 // while we're rerunning "pub list", since any analysis we complete while
1549 // "pub list" is in progress is just going to get thrown away anyhow. 1515 // "pub list" is in progress is just going to get thrown away anyhow.
1550 List<String> dependencies = <String>[]; 1516 List<String> dependencies = <String>[];
1551 FolderDisposition disposition = _computeFolderDisposition(
1552 info.folder, dependencies.add, _findPackageSpecFile(info.folder));
1553 info.setDependencies(dependencies); 1517 info.setDependencies(dependencies);
1554 _updateContextPackageUriResolver(info.folder, disposition); 1518 _updateContextPackageUriResolver(info.folder);
1555 } 1519 }
1556 1520
1557 /** 1521 /**
1558 * Return `true` if the given [file] should be analyzed. 1522 * Return `true` if the given [file] should be analyzed.
1559 */ 1523 */
1560 bool _shouldFileBeAnalyzed(File file) { 1524 bool _shouldFileBeAnalyzed(File file) {
1561 for (Glob glob in analyzedFilesGlobs) { 1525 for (Glob glob in analyzedFilesGlobs) {
1562 if (glob.matches(file.path)) { 1526 if (glob.matches(file.path)) {
1563 // Emacs creates dummy links to track the fact that a file is open for 1527 // Emacs creates dummy links to track the fact that a file is open for
1564 // editing and has unsaved changes (e.g. having unsaved changes to 1528 // editing and has unsaved changes (e.g. having unsaved changes to
(...skipping 19 matching lines...) Expand all
1584 stringMap[key] = object[key]; 1548 stringMap[key] = object[key];
1585 } else { 1549 } else {
1586 return null; 1550 return null;
1587 } 1551 }
1588 } 1552 }
1589 return stringMap; 1553 return stringMap;
1590 } 1554 }
1591 return null; 1555 return null;
1592 } 1556 }
1593 1557
1594 void _updateContextPackageUriResolver( 1558 void _updateContextPackageUriResolver(Folder contextFolder) {
1595 Folder contextFolder, FolderDisposition disposition) {
1596 AnalysisContext context = folderMap[contextFolder]; 1559 AnalysisContext context = folderMap[contextFolder];
1597 context.sourceFactory = _createSourceFactory( 1560 context.sourceFactory =
1598 context, context.analysisOptions, disposition, contextFolder); 1561 _createSourceFactory(context, context.analysisOptions, contextFolder);
1599 callbacks.updateContextPackageUriResolver(context); 1562 callbacks.updateContextPackageUriResolver(context);
1600 } 1563 }
1601 1564
1602 /** 1565 /**
1603 * Create and return a source representing the given [file] within the given 1566 * Create and return a source representing the given [file] within the given
1604 * [context]. 1567 * [context].
1605 */ 1568 */
1606 static Source createSourceInContext(AnalysisContext context, File file) { 1569 static Source createSourceInContext(AnalysisContext context, File file) {
1607 // TODO(brianwilkerson) Optimize this, by allowing support for source 1570 // TODO(brianwilkerson) Optimize this, by allowing support for source
1608 // factories to restore URI's from a file path rather than a source. 1571 // factories to restore URI's from a file path rather than a source.
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
1854 } 1817 }
1855 return _embedderLocator; 1818 return _embedderLocator;
1856 } 1819 }
1857 1820
1858 @override 1821 @override
1859 SdkExtensionFinder getSdkExtensionFinder(ResourceProvider resourceProvider) { 1822 SdkExtensionFinder getSdkExtensionFinder(ResourceProvider resourceProvider) {
1860 return _sdkExtensionFinder ??= 1823 return _sdkExtensionFinder ??=
1861 new SdkExtensionFinder(buildPackageMap(resourceProvider)); 1824 new SdkExtensionFinder(buildPackageMap(resourceProvider));
1862 } 1825 }
1863 } 1826 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698