Need help with DomFileDescription and Registering Extension Point
Let me start with saying I am completely new to IntelliJ plugin development and I have scoured the internet trying to get this to work.
What I am trying to do at the moment is just create a very basic plugin, for practice at the moment, that will get the current xml file from the editor and then use the DOM to access its elements and values. I have been using the XML DOM API dev guide located here https://www.jetbrains.org/intellij/sdk/docs/reference_guide/frameworks_and_external_apis/xml_dom_api.html
I think my issue has to do with registering the DomFileDescription as an extension point but I could be completely wrong there. The guide doesn't cover that bit very well, I suppose its assumed at that point you know how to do that part. From the testing I have done it seems the small amount of code I have is working up until I try and getRootElement from the DomManager. I have pasted my code and plugin.xml information below. Note that when I added the extension point to the plugin.xml file as <dom.fileDescription implementation="com.intellij.dom.fileDescription" /> the dom.fileDescription part is in red and not sure what to do to resolve that.
Any help getting this rolling or pointing out glaring newb mistakes would be much appreciated.
CODE----------------------
package com.jetrees.testPlugin;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlFile;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomFileDescription;
import com.intellij.util.xml.DomManager;
import java.util.List;
interface Root extends DomElement {
Foo getFoo();
}
interface Foo extends DomElement {
List<Bar> getBars();
}
interface Bar extends DomElement {
String getValue();
}
public class GenerateAction extends AnAction {
@Override
public void actionPerformed(final AnActionEvent e) {
// TODO: insert action logic here
//Get all the required data from data keys
Project project = e.getData(PlatformDataKeys.PROJECT);
Editor editor = e.getData(PlatformDataKeys.EDITOR);
//Create DomFileDescription object
DomFileDescription<Root> description = new DomFileDescription<>(Root.class, "Module", "Module");
if (project != null && editor != null) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
XmlFile xmlFile = (XmlFile) psiFile;
DomManager manager = DomManager.getDomManager(project);
Root root = manager.getFileElement(xmlFile, Root.class).getRootElement();
}
}
@Override
public void update(AnActionEvent e) {
final Project project = e.getData(CommonDataKeys.PROJECT);
final Editor editor = e.getData(CommonDataKeys.EDITOR);
//Set visibility only in case of existing project and editor (for now)
e.getPresentation().setVisible((project != null && editor != null));
}
}
<idea-plugin>
<id>com.jetrees.testPlugin</id>
<name>test plugin</name>
<version>1.0</version>
<vendor email="support@yourcompany.com" url="http://www.yourcompany.com">YourCompany</vendor>
<description><![CDATA[
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
]]></description>
<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
]]>
</change-notes>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="145.0"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<!-- uncomment to enable plugin in all products
<depends>com.intellij.modules.lang</depends>
-->
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<dom.fileDescription implementation="com.intellij.dom.fileDescription" />
</extensions>
<actions>
<!-- Add your actions here -->
<action id="testPlugin" class="com.jetrees.testPlugin.GenerateAction"
text="TestPlugin.." description="first plugin practice">
<add-to-group group-id="MainToolBar" anchor="after" relative-to-action="HelpTopics"/>
</action>
</actions>
</idea-plugin>
Please sign in to leave a comment.
You'll have to register your DomFileDescription in plugin.xml, not programmatically as shown above (in Action code).
So you need in plugin.xml:
Code:
package com.your.plugin;
public class YourDomFileDescription extends DomFileDescription {
...
}
Please look at code in IntelliJ Community Edition, you'll find plenty of sample code there.
Ok I looked at some of the plugins in the CE and was able to figure out my issue. I was able to continue my development using the deprecated method until getting this sorted out. Thanks for the help.