plugin_extension_points.md: more complete sample, ExtensionPointName usage

This commit is contained in:
Yann Cébron 2019-12-13 16:38:25 +01:00
parent 49bb8e1731
commit 78a8ac73a5

View File

@ -19,13 +19,20 @@ You can declare extensions and extension points in the plugin configuration file
To declare extension points in your plugin, add an `<extensionPoints>` section to your `plugin.xml`. Then insert a child element `<extensionPoint>` that defines the extension point name and the name of a bean class or an interface that is allowed to extend the plugin functionality in the `name`, `beanClass` and `interface` attributes, respectively.
```xml
<extensionPoints>
<extensionPoint name="myExtensionPoint1" beanClass="com.myplugin.MyBeanClass">
<extensionPoint name="myExtensionPoint2" interface="com.myplugin.MyInterface">
</extensionPoints>
_myPlugin/META-INF/plugin.xml_
```xml
<idea-plugin>
<id>my.plugin</id>
<extensionPoints>
<extensionPoint name="myExtensionPoint1" beanClass="com.myplugin.MyBeanClass">
<extensionPoint name="myExtensionPoint2" interface="com.myplugin.MyInterface">
</extensionPoints>
</idea-plugin>
```
* The `name` attribute assigns a unique name for this extension point, it will be prefixed with the plugin's `<id>` automatically.
* The `beanClass` attribute sets a bean class that specifies one or several properties annotated with the [`@Attribute`](upsource:///platform/util/src/com/intellij/util/xmlb/annotations/Attribute.java) annotation.
* The `interface` attribute sets an interface the plugin that contributes to the extension point must implement.
@ -33,6 +40,7 @@ The plugin that contributes to the extension point will read those properties fr
To clarify this, consider the following sample `MyBeanClass` bean class used in the above `plugin.xml` file:
_myPlugin/src/com/myplugin/MyBeanClass.java_
```java
public class MyBeanClass extends AbstractExtensionPointBean {
@Attribute("key")
@ -51,6 +59,44 @@ public class MyBeanClass extends AbstractExtensionPointBean {
}
```
To declare an extension designed to access the `myExtensionPoint1` extension point, your `plugin.xml` file must contain the `<myExtensionPoint1>` tag with the `key` and `implementationClass` attributes set to appropriate values.
> **TIP** See [Extension properties code insight](plugin_extensions.md#extension-properties-code-insight) on how to provide smart completion/validation.
For above extension points usage in _anotherPlugin_ would look like this (see also [Declaring Extensions](plugin_extensions.md#declaring-extensions)):
_anotherPlugin/META-INF/plugin.xml_
```xml
<idea-plugin>
<id>another.plugin</id>
<!-- declare dependency on plugin defining extension point -->
<depends>my.plugin</id>
<!-- use "my.plugin" namespace -->
<extensions defaultExtensionNs="my.plugin">
<myExtensionPoint1 key="someKey" implementationClass="another.some.implementation.class"/>
<myExtensionPoint2 implementation="another.MyInterfaceImpl"/>
</extension>
</idea-plugin>
```
## Using extension points
To refer to all registered extension instances at runtime, declare an [`ExtensionPointName`](upsource:///platform/extensions/src/com/intellij/openapi/extensions/ExtensionPointName.java) passing in the fully-qualified name matching its [declaration in `plugin.xml`](#how-to-declare-extension-points).
_myPlugin/src/com/myplugin/MyExtensionUsingService.java_
```java
public class MyExtensionUsingService {
private static final ExtensionPointName<MyBeanClass> EP_NAME = ExtensionPointName.create("my.plugin.myExtensionPoint1");
public void useExtensions() {
for (MyBeanClass extension : EP_NAME.getExtensionList()) {
String key = extension.getKey();
String clazz = extension.getClass();
// ...
}
}
}
```
A gutter icon for the `ExtensionPointName` declaration allows navigating to the corresponding `<extensionPoint>` declaration in `plugin.xml`.