| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 'package:analysis_server/plugin/analysis/occurrences/occurrences_core.dar
t'; | 5 import 'package:analysis_server/plugin/analysis/occurrences/occurrences_core.dar
t'; |
| 6 import 'package:analysis_server/src/analysis_server.dart'; | |
| 7 import 'package:analysis_server/src/protocol_server.dart' as protocol; | 6 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 8 import 'package:analyzer/exception/exception.dart'; | |
| 9 import 'package:analyzer/src/generated/engine.dart' | |
| 10 show AnalysisContext, AnalysisEngine; | |
| 11 import 'package:analyzer/src/generated/source.dart' show Source; | |
| 12 | |
| 13 /** | |
| 14 * Compute all known occurrences for the given [source]. | |
| 15 */ | |
| 16 OccurrencesCollectorImpl computeOccurrences( | |
| 17 AnalysisServer server, AnalysisContext context, Source source) { | |
| 18 OccurrencesCollectorImpl collector = new OccurrencesCollectorImpl(); | |
| 19 List<OccurrencesContributor> contributors = | |
| 20 server.serverPlugin.occurrencesContributors; | |
| 21 for (OccurrencesContributor contributor in contributors) { | |
| 22 try { | |
| 23 contributor.computeOccurrences(collector, context, source); | |
| 24 } catch (exception, stackTrace) { | |
| 25 AnalysisEngine.instance.logger.logError( | |
| 26 'Exception from occurrences contributor: ${contributor.runtimeType}', | |
| 27 new CaughtException(exception, stackTrace)); | |
| 28 } | |
| 29 } | |
| 30 return collector; | |
| 31 } | |
| 32 | 7 |
| 33 /** | 8 /** |
| 34 * A concrete implementation of [OccurrencesCollector]. | 9 * A concrete implementation of [OccurrencesCollector]. |
| 35 */ | 10 */ |
| 36 class OccurrencesCollectorImpl implements OccurrencesCollector { | 11 class OccurrencesCollectorImpl implements OccurrencesCollector { |
| 37 Map<protocol.Element, protocol.Occurrences> elementOccurrences = | 12 Map<protocol.Element, protocol.Occurrences> elementOccurrences = |
| 38 <protocol.Element, protocol.Occurrences>{}; | 13 <protocol.Element, protocol.Occurrences>{}; |
| 39 | 14 |
| 40 List<protocol.Occurrences> get allOccurrences { | 15 List<protocol.Occurrences> get allOccurrences { |
| 41 return elementOccurrences.values.toList(); | 16 return elementOccurrences.values.toList(); |
| 42 } | 17 } |
| 43 | 18 |
| 44 @override | 19 @override |
| 45 void addOccurrences(protocol.Occurrences current) { | 20 void addOccurrences(protocol.Occurrences current) { |
| 46 protocol.Element element = current.element; | 21 protocol.Element element = current.element; |
| 47 protocol.Occurrences existing = elementOccurrences[element]; | 22 protocol.Occurrences existing = elementOccurrences[element]; |
| 48 if (existing != null) { | 23 if (existing != null) { |
| 49 List<int> offsets = _merge(existing.offsets, current.offsets); | 24 List<int> offsets = _merge(existing.offsets, current.offsets); |
| 50 current = new protocol.Occurrences(element, offsets, existing.length); | 25 current = new protocol.Occurrences(element, offsets, existing.length); |
| 51 } | 26 } |
| 52 elementOccurrences[element] = current; | 27 elementOccurrences[element] = current; |
| 53 } | 28 } |
| 54 | 29 |
| 55 static List<int> _merge(List<int> a, List<int> b) { | 30 static List<int> _merge(List<int> a, List<int> b) { |
| 56 return <int>[]..addAll(a)..addAll(b); | 31 return <int>[]..addAll(a)..addAll(b); |
| 57 } | 32 } |
| 58 } | 33 } |
| OLD | NEW |