OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library analyzer.src.task.options_work_manager; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analyzer/src/context/cache.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart' |
| 11 show |
| 12 AnalysisEngine, |
| 13 AnalysisErrorInfo, |
| 14 AnalysisErrorInfoImpl, |
| 15 AnalysisOptions, |
| 16 CacheState, |
| 17 InternalAnalysisContext; |
| 18 import 'package:analyzer/src/generated/error.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:analyzer/src/task/options.dart'; |
| 21 import 'package:analyzer/task/model.dart'; |
| 22 |
| 23 /// The manager for `.analysis_options` specific analysis. |
| 24 class OptionsWorkManager implements WorkManager { |
| 25 /// The context for which work is being managed. |
| 26 final InternalAnalysisContext context; |
| 27 |
| 28 /// The options file sources. |
| 29 final LinkedHashSet<Source> sourceQueue = new LinkedHashSet<Source>(); |
| 30 |
| 31 /// The [TargetedResult]s that should be computed with priority. |
| 32 final LinkedHashSet<TargetedResult> priorityResultQueue = |
| 33 new LinkedHashSet<TargetedResult>(); |
| 34 |
| 35 /// Initialize a newly created manager. |
| 36 OptionsWorkManager(this.context) { |
| 37 analysisCache.onResultInvalidated.listen(onResultInvalidated); |
| 38 } |
| 39 |
| 40 /// Returns the correctly typed result of `context.analysisCache`. |
| 41 AnalysisCache get analysisCache => context.analysisCache; |
| 42 |
| 43 /// Specifies that the client wants the given [result] of the given [target] |
| 44 /// to be computed with priority. |
| 45 void addPriorityResult(AnalysisTarget target, ResultDescriptor result) { |
| 46 priorityResultQueue.add(new TargetedResult(target, result)); |
| 47 } |
| 48 |
| 49 @override |
| 50 void applyChange(List<Source> addedSources, List<Source> changedSources, |
| 51 List<Source> removedSources) { |
| 52 addedSources = addedSources.where(_isOptionsSource).toList(); |
| 53 changedSources = changedSources.where(_isOptionsSource).toList(); |
| 54 removedSources = removedSources.where(_isOptionsSource).toList(); |
| 55 // source queue |
| 56 sourceQueue.addAll(addedSources); |
| 57 sourceQueue.addAll(changedSources); |
| 58 sourceQueue.removeAll(removedSources); |
| 59 } |
| 60 |
| 61 @override |
| 62 void applyPriorityTargets(List<AnalysisTarget> targets) { |
| 63 // Unschedule the old targets. |
| 64 List<TargetedResult> resultsToUnschedule = <TargetedResult>[]; |
| 65 for (TargetedResult result in priorityResultQueue) { |
| 66 if (result.result == ANALYSIS_OPTIONS_ERRORS) { |
| 67 resultsToUnschedule.add(result); |
| 68 } |
| 69 } |
| 70 priorityResultQueue.removeAll(resultsToUnschedule); |
| 71 // Schedule new targets. |
| 72 for (AnalysisTarget target in targets) { |
| 73 if (_isOptionsSource(target)) { |
| 74 addPriorityResult(target, ANALYSIS_OPTIONS_ERRORS); |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 @override |
| 80 List<AnalysisError> getErrors(Source source) { |
| 81 if (!_isOptionsSource(source)) { |
| 82 return AnalysisError.NO_ERRORS; |
| 83 } |
| 84 // If analysis is finished, use all the errors. |
| 85 if (analysisCache.getState(source, ANALYSIS_OPTIONS_ERRORS) == |
| 86 CacheState.VALID) { |
| 87 return analysisCache.getValue(source, ANALYSIS_OPTIONS_ERRORS); |
| 88 } |
| 89 // No partial results. |
| 90 return AnalysisError.NO_ERRORS; |
| 91 } |
| 92 |
| 93 @override |
| 94 TargetedResult getNextResult() { |
| 95 // Try to find a priority result to compute. |
| 96 while (priorityResultQueue.isNotEmpty) { |
| 97 TargetedResult result = priorityResultQueue.first; |
| 98 if (!_needsComputing(result.target, result.result)) { |
| 99 priorityResultQueue.remove(result); |
| 100 continue; |
| 101 } |
| 102 return result; |
| 103 } |
| 104 // Try to find a new options file to analyze. |
| 105 while (sourceQueue.isNotEmpty) { |
| 106 Source optionsSource = sourceQueue.first; |
| 107 if (!_needsComputing(optionsSource, ANALYSIS_OPTIONS_ERRORS)) { |
| 108 sourceQueue.remove(optionsSource); |
| 109 continue; |
| 110 } |
| 111 return new TargetedResult(optionsSource, ANALYSIS_OPTIONS_ERRORS); |
| 112 } |
| 113 // No results to compute. |
| 114 return null; |
| 115 } |
| 116 |
| 117 @override |
| 118 WorkOrderPriority getNextResultPriority() { |
| 119 if (priorityResultQueue.isNotEmpty) { |
| 120 return WorkOrderPriority.PRIORITY; |
| 121 } |
| 122 if (sourceQueue.isNotEmpty) { |
| 123 return WorkOrderPriority.NORMAL; |
| 124 } |
| 125 return WorkOrderPriority.NONE; |
| 126 } |
| 127 |
| 128 @override |
| 129 void onAnalysisOptionsChanged() { |
| 130 // Do nothing. |
| 131 } |
| 132 |
| 133 /// Notifies the manager that a result has been invalidated. |
| 134 void onResultInvalidated(InvalidatedResult event) { |
| 135 ResultDescriptor descriptor = event.descriptor; |
| 136 if (descriptor == ANALYSIS_OPTIONS_ERRORS) { |
| 137 sourceQueue.add(event.entry.target); |
| 138 } |
| 139 } |
| 140 |
| 141 @override |
| 142 void onSourceFactoryChanged() { |
| 143 // Do nothing. |
| 144 } |
| 145 |
| 146 @override |
| 147 void resultsComputed( |
| 148 AnalysisTarget target, Map<ResultDescriptor, dynamic> outputs) { |
| 149 // Update notice. |
| 150 if (_isOptionsSource(target)) { |
| 151 bool shouldSetErrors = false; |
| 152 outputs.forEach((ResultDescriptor descriptor, value) { |
| 153 if (descriptor == ANALYSIS_OPTIONS_ERRORS) { |
| 154 shouldSetErrors = true; |
| 155 } |
| 156 }); |
| 157 if (shouldSetErrors) { |
| 158 AnalysisErrorInfo info = context.getErrors(target); |
| 159 context.getNotice(target).setErrors(info.errors, info.lineInfo); |
| 160 } |
| 161 } |
| 162 } |
| 163 |
| 164 /// Returns `true` if the given [result] of the given [target] needs |
| 165 /// computing, i.e. it is not in the valid and not in the error state. |
| 166 bool _needsComputing(AnalysisTarget target, ResultDescriptor result) { |
| 167 CacheState state = analysisCache.getState(target, result); |
| 168 return state != CacheState.VALID && state != CacheState.ERROR; |
| 169 } |
| 170 |
| 171 /// Return `true` if the given target is an `.analysis_options` source. |
| 172 static bool _isOptionsSource(AnalysisTarget target) => target is Source && |
| 173 AnalysisEngine.isAnalysisOptionsFileName(target.fullName); |
| 174 } |
OLD | NEW |