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