OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 6 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 7 import 'package:analyzer_plugin/protocol/protocol_common.dart' |
| 8 show ElementKind, Location; |
| 9 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
| 10 import 'package:analyzer_plugin/src/utilities/navigation.dart'; |
| 11 |
| 12 /** |
| 13 * An object that [NavigationContributor]s use to record navigation regions. |
| 14 * |
| 15 * Clients may not extend, implement or mix-in this class. |
| 16 */ |
| 17 abstract class NavigationCollector { |
| 18 /** |
| 19 * Record a new navigation region with the given [offset] and [length] that |
| 20 * should navigate to the given [targetLocation]. |
| 21 */ |
| 22 void addRegion( |
| 23 int offset, int length, ElementKind targetKind, Location targetLocation); |
| 24 } |
| 25 |
| 26 /** |
| 27 * An object used to produce navigation regions. |
| 28 * |
| 29 * Clients may implement this class when implementing plugins. |
| 30 */ |
| 31 abstract class NavigationContributor { |
| 32 /** |
| 33 * Contribute navigation regions for a subset of the content of the given |
| 34 * [source] into the given [collector]. The subset is specified by the |
| 35 * [offset] and [length]. The [driver] can be used to access analysis results. |
| 36 */ |
| 37 void computeNavigation(NavigationCollector collector, |
| 38 AnalysisDriverGeneric driver, String filePath, int offset, int length); |
| 39 } |
| 40 |
| 41 /** |
| 42 * A generator that will generate an 'analysis.navigation' notification. |
| 43 * |
| 44 * Clients may not extend, implement or mix-in this class. |
| 45 */ |
| 46 class NavigationGenerator { |
| 47 /** |
| 48 * The driver to be passed in to the contributors. |
| 49 */ |
| 50 final AnalysisDriverGeneric driver; |
| 51 |
| 52 /** |
| 53 * The contributors to be used to generate the navigation data. |
| 54 */ |
| 55 final List<NavigationContributor> contributors; |
| 56 |
| 57 /** |
| 58 * Initialize a newly created navigation generator. |
| 59 */ |
| 60 NavigationGenerator(this.driver, this.contributors); |
| 61 |
| 62 /** |
| 63 * Create an 'analysis.navigation' notification for the file with the given |
| 64 * [filePath]. If any of the contributors throws an exception, also create a |
| 65 * non-fatal 'plugin.error' notification. |
| 66 */ |
| 67 List<Notification> generateNavigationNotification(String filePath) { |
| 68 List<Notification> notifications = <Notification>[]; |
| 69 NavigationCollectorImpl collector = new NavigationCollectorImpl(); |
| 70 for (NavigationContributor contributor in contributors) { |
| 71 try { |
| 72 contributor.computeNavigation(collector, driver, filePath, null, null); |
| 73 } catch (exception, stackTrace) { |
| 74 notifications.add(new PluginErrorParams( |
| 75 false, exception.toString(), stackTrace.toString()) |
| 76 .toNotification()); |
| 77 } |
| 78 } |
| 79 collector.createRegions(); |
| 80 notifications.add(new AnalysisNavigationParams( |
| 81 filePath, collector.regions, collector.targets, collector.files) |
| 82 .toNotification()); |
| 83 return notifications; |
| 84 } |
| 85 } |
OLD | NEW |