What is the correct way to register an extension point in code instead of plugin.xml?
已回答
In my plugin I dynamically register an extension for the paste handler to allow users to disable this feature.
val rootArea = Extensions.getRootArea()
val extensionPoint = rootArea.getExtensionPoint(EditorActionHandlerBean.EP_NAME)
// <editorActionHandler action="EditorPaste" implementationClass="com.vladsch.idea.multimarkdown.editor.actions.handlers.enh.MdPasteHandler" order="first" />
val element = Element("editorActionHandler")
.setAttribute("action", "EditorPaste")
.setAttribute("implementationClass", "com.vladsch.idea.multimarkdown.editor.actions.handlers.enh.MdPasteHandler")
.setAttribute("order", "first")
try {
rootArea.registerExtension(extensionPoint, pluginDescriptor, element)
usingPasteHandler = true
} catch (e: Throwable) {
// ignore
}
I noticed that all methods for registering extensions in ExtensionsAreaare now marked@TestOnlyor @Deprecated.
What is the correct way of dynamically registering an extension in a plugin?
请先登录再写评论。
You can try using `EditorActionManager.getInstance().setActionHandler` instead.
There are no generic approach for any extension, as not every extension point can track its modifications (and your modification code might happen to be called after EP initialisation).
Thanks Aleksey. I will try that. I was wondering about the fact that "Late registry" should not work for all EPs. This extension point seems to have no problems.
The only reason I have this registered dynamically is to give users ability to remove the plugin paste handler. IDE paste handler (or another one in the chain) is causing exceptions that winds up being attributed to my plugin.
Disabling my plugin paste handler at least in theory gives users a chance to forward the exception report to someone other than me. ;)
A quick question about registering a paste handler since it needs a previous handler to which to delegate unhandled pastes.
Are calls to modify any handlers through the EditorActionManager only made on the UI thread? I would think yes but it pays to ask.
You can always follow these exceptions to https://youtrack.jetbrains.com directly.
Yes. There are no assertions at the time, but modifying them from another thread is likely to cause a race somewhere.
Aleksey, I did not want not to modify it from another thread but was wondering if the handler can be modified from another thread between me getting the current handler and setting it to my handler. I suspected that it should not happen but wanted to make sure just in case.
Thank you for your suggestion. Using EditorActionManager is the correct approach for what I am trying to accomplish in my plugin. It also allows me to remove my handler without requiring an IDE restart, if it is still the current handler for the action, which it usually is since it registers in application component initialization.