| 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 analysis_server.src.plugin.plugin_impl; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analysis_server/plugin/plugin.dart'; |
| 10 |
| 11 /** |
| 12 * An object that manages the extension points for a single instance of the |
| 13 * analysis server. |
| 14 */ |
| 15 class ExtensionManager { |
| 16 /** |
| 17 * A table mapping the id's of extension points to the corresponding |
| 18 * extension points. |
| 19 */ |
| 20 Map<String, ExtensionPointImpl> extensionPoints = |
| 21 new HashMap<String, ExtensionPointImpl>(); |
| 22 |
| 23 /** |
| 24 * Process each of the given [plugins] by allowing them to register extension |
| 25 * points and extensions. |
| 26 * |
| 27 * An [ExtensionError] will be thrown if any of the plugins throws such an |
| 28 * exception while registering with this manager. |
| 29 */ |
| 30 void processPlugins(List<Plugin> plugins) { |
| 31 for (Plugin plugin in plugins) { |
| 32 plugin.registerExtensionPoints( |
| 33 (String identifier, [ExtensionValidator validator]) { |
| 34 registerExtensionPoint(plugin, identifier, validator); |
| 35 }); |
| 36 } |
| 37 for (Plugin plugin in plugins) { |
| 38 plugin.registerExtensions(registerExtension); |
| 39 } |
| 40 } |
| 41 |
| 42 /** |
| 43 * Register an [extension] to the extension point with the given unique |
| 44 * [identifier]. |
| 45 */ |
| 46 void registerExtension(String identifier, Object extension) { |
| 47 ExtensionPointImpl extensionPoint = extensionPoints[identifier]; |
| 48 if (extensionPoint == null) { |
| 49 throw new ExtensionError( |
| 50 'There is no extension point with the id "$identifier"'); |
| 51 } |
| 52 extensionPoint.add(extension); |
| 53 } |
| 54 |
| 55 /** |
| 56 * Register an extension point being defined by the given [plugin] with the |
| 57 * given simple [identifier] and [validator]. |
| 58 */ |
| 59 ExtensionPoint registerExtensionPoint(Plugin plugin, String identifier, |
| 60 ExtensionValidator validator) { |
| 61 String uniqueIdentifier = Plugin.buildUniqueIdentifier(plugin, identifier); |
| 62 if (extensionPoints.containsKey(uniqueIdentifier)) { |
| 63 throw new ExtensionError( |
| 64 'There is already an extension point with the id "$identifier"'); |
| 65 } |
| 66 ExtensionPointImpl extensionPoint = |
| 67 new ExtensionPointImpl(plugin, identifier, validator); |
| 68 extensionPoints[uniqueIdentifier] = extensionPoint; |
| 69 return extensionPoint; |
| 70 } |
| 71 } |
| 72 |
| 73 /** |
| 74 * A concrete representation of an extension point. |
| 75 */ |
| 76 class ExtensionPointImpl implements ExtensionPoint { |
| 77 @override |
| 78 final Plugin plugin; |
| 79 |
| 80 @override |
| 81 final String simpleIdentifier; |
| 82 |
| 83 /** |
| 84 * The validator used to validate extensions to this extension point. |
| 85 */ |
| 86 final ExtensionValidator validator; |
| 87 |
| 88 /** |
| 89 * The list of extensions to this extension point. |
| 90 */ |
| 91 final List<Object> _extensions = <Object>[]; |
| 92 |
| 93 /** |
| 94 * Initialize a newly create extension point to belong to the given [plugin] |
| 95 * and have the given [simpleIdentifier]. If the [validator] is non-`null` it |
| 96 * will be used to validate extensions associated with this extension point. |
| 97 */ |
| 98 ExtensionPointImpl(this.plugin, this.simpleIdentifier, this.validator); |
| 99 |
| 100 /** |
| 101 * Return a list containing all of the extensions that have been registered |
| 102 * for this extension point. |
| 103 */ |
| 104 List<Object> get extensions => new UnmodifiableListView(_extensions); |
| 105 |
| 106 /** |
| 107 * Return the identifier used to uniquely identify this extension point. The |
| 108 * unique identifier is the identifier for the plugin, followed by a period |
| 109 * (`.`), followed by the [simpleIdentifier] for the extension point. |
| 110 */ |
| 111 String get uniqueIdentifier => |
| 112 Plugin.buildUniqueIdentifier(plugin, simpleIdentifier); |
| 113 |
| 114 /** |
| 115 * Validate that the given [extension] is appropriate for this extension |
| 116 * point, and if it is then add it to the list of registered exceptions. |
| 117 */ |
| 118 void add(Object extension) { |
| 119 if (validator != null) { |
| 120 validator(extension); |
| 121 } |
| 122 _extensions.add(extension); |
| 123 } |
| 124 } |
| OLD | NEW |