| 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((String identifier, | |
| 33 [ValidateExtension validateExtension]) => | |
| 34 registerExtensionPoint(plugin, identifier, validateExtension)); | |
| 35 } | |
| 36 for (Plugin plugin in plugins) { | |
| 37 plugin.registerExtensions(registerExtension); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 /** | |
| 42 * Register an [extension] to the extension point with the given unique | |
| 43 * [identifier]. | |
| 44 */ | |
| 45 void registerExtension(String identifier, Object extension) { | |
| 46 ExtensionPointImpl extensionPoint = extensionPoints[identifier]; | |
| 47 if (extensionPoint == null) { | |
| 48 throw new ExtensionError( | |
| 49 'There is no extension point with the id "$identifier"'); | |
| 50 } | |
| 51 extensionPoint.add(extension); | |
| 52 } | |
| 53 | |
| 54 /** | |
| 55 * Register an extension point being defined by the given [plugin] with the | |
| 56 * given simple [identifier] and [validateExtension]. | |
| 57 */ | |
| 58 ExtensionPoint registerExtensionPoint( | |
| 59 Plugin plugin, String identifier, ValidateExtension validateExtension) { | |
| 60 String uniqueIdentifier = Plugin.buildUniqueIdentifier(plugin, identifier); | |
| 61 if (extensionPoints.containsKey(uniqueIdentifier)) { | |
| 62 throw new ExtensionError( | |
| 63 'There is already an extension point with the id "$identifier"'); | |
| 64 } | |
| 65 ExtensionPointImpl extensionPoint = | |
| 66 new ExtensionPointImpl(plugin, identifier, validateExtension); | |
| 67 extensionPoints[uniqueIdentifier] = extensionPoint; | |
| 68 return extensionPoint; | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * A concrete representation of an extension point. | |
| 74 */ | |
| 75 class ExtensionPointImpl implements ExtensionPoint { | |
| 76 @override | |
| 77 final Plugin plugin; | |
| 78 | |
| 79 @override | |
| 80 final String simpleIdentifier; | |
| 81 | |
| 82 /** | |
| 83 * The function used to validate extensions to this extension point. | |
| 84 */ | |
| 85 final ValidateExtension validateExtension; | |
| 86 | |
| 87 /** | |
| 88 * The list of extensions to this extension point. | |
| 89 */ | |
| 90 final List<Object> _extensions = <Object>[]; | |
| 91 | |
| 92 /** | |
| 93 * Initialize a newly create extension point to belong to the given [plugin] | |
| 94 * and have the given [simpleIdentifier]. If [validateExtension] is non-`null` | |
| 95 * it will be used to validate extensions associated with this extension | |
| 96 * point. | |
| 97 */ | |
| 98 ExtensionPointImpl( | |
| 99 this.plugin, this.simpleIdentifier, this.validateExtension); | |
| 100 | |
| 101 /** | |
| 102 * Return a list containing all of the extensions that have been registered | |
| 103 * for this extension point. | |
| 104 */ | |
| 105 List<Object> get extensions => new UnmodifiableListView(_extensions); | |
| 106 | |
| 107 /** | |
| 108 * Return the identifier used to uniquely identify this extension point. The | |
| 109 * unique identifier is the identifier for the plugin, followed by a period | |
| 110 * (`.`), followed by the [simpleIdentifier] for the extension point. | |
| 111 */ | |
| 112 String get uniqueIdentifier => | |
| 113 Plugin.buildUniqueIdentifier(plugin, simpleIdentifier); | |
| 114 | |
| 115 /** | |
| 116 * Validate that the given [extension] is appropriate for this extension | |
| 117 * point, and if it is then add it to the list of registered exceptions. | |
| 118 */ | |
| 119 void add(Object extension) { | |
| 120 if (validateExtension != null) { | |
| 121 validateExtension(extension); | |
| 122 } | |
| 123 _extensions.add(extension); | |
| 124 } | |
| 125 } | |
| OLD | NEW |