Create a template for a custom language and available in the File -> New -> custom file

已回答

Hi, I've a custom language plugin and I'm looking for an example implementation. Here is what I've done so far looking into various posts. 

1. Created an action class 

public class CreateFofikFileAction extends CreateFileFromTemplateAction implements DumbAware {
public CreatePlistFileAction() {
super("Fofik File", "Creates new Fofik File", PlistFileType.INSTANCE.getIcon());
}

@Override
protected void buildDialog(Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
builder.setTitle("New Fofik File");
}

@Override
protected String getActionName(PsiDirectory directory, @NotNull String newName,
String templateName) {
return "Plist File";
}

@Override
protected PsiFile createFile(String name, String templateName, PsiDirectory dir) {
FileTemplate template = FileTemplateManager.getInstance(dir.getProject())
.getTemplate("Property.fofik"); // the template file "Property.fofik.ft" is stored under resources/fileTemplates/
return createFileFromTemplate(name, template, dir).getOriginalFile();
}

2. Registered the action class in plugin.xml

<action id="com.fofik.actions.CreateFofikFileAction"
class="com.fofik.actions.CreateFofikFileAction" text="Fofik File"
description="Creating new Fofik File">
<add-to-group group-id="NewGroup" anchor="last"/>
</action>

I can see File -> New -> Fofik. When I clicked, it popped the dialog to specify the file name. I typed the name and pressed <enter> key and got the following error. In the IntelliJ IDEA terminal (where the gradle-> intellij -> runIde was clicked).

2020-09-16 15:05:56,633 [4054167]  ERROR - llij.ide.plugins.PluginManager - Last Action: com.fofik.actions.CreateFofikFileAction 
2020-09-16 15:06:04,515 [4062049]  ERROR - llij.ide.plugins.PluginManager - null 
java.lang.NullPointerException
at com.intellij.ide.actions.newclass.CreateWithTemplatesDialogPanel.getSelectedTemplate(CreateWithTemplatesDialogPanel.java:42)
at com.intellij.ide.actions.CreateFileFromTemplateDialog$NonBlockingPopupBuilderImpl$1.getActionName(CreateFileFromTemplateDialog.java:270)
at com.intellij.ide.actions.ElementCreator.tryCreate(ElementCreator.java:66)
at com.intellij.ide.actions.CreateFileFromTemplateDialog$NonBlockingPopupBuilderImpl.createElement(CreateFileFromTemplateDialog.java:308)
at com.intellij.ide.actions.CreateFileFromTemplateDialog$NonBlockingPopupBuilderImpl.lambda$show$2(CreateFileFromTemplateDialog.java:282)
at com.intellij.ide.ui.newItemPopup.NewItemSimplePopupPanel$1.keyPressed(NewItemSimplePopupPanel.java:103)
at java.desktop/java.awt.Component.processKeyEvent(Component.java:6609)
at java.desktop/javax.swing.JComponent.processKeyEvent(JComponent.java:2849)
at java.desktop/java.awt.Component.processEvent(Component.java:6428)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5026)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)

Could somebody help? 

 

0

Please provide full plugin sources

0

请先登录再写评论。