我如何“注册” IntelliJ插件中的新模块类型? [英] How do I "register" a new Module Type in an IntelliJ Plugin?

查看:630
本文介绍了我如何“注册” IntelliJ插件中的新模块类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是IntelliJ插件开发的初学者,但我希望我的插件在新建项目/新模块窗口中注册新的模块类型。



我已经在文档中搜索了插件开发人员但是找不到任何有用的东西。我还查看了现有的插件,如Kotlin和Scala,它们也添加了新的Module类型,但我无法弄清楚如何在上面提到的对话框中显示完成的ModuleType。



我需要在plugin.xml文件中更改什么?我已经添加了扩展并为ModuleType,ModuleBuilder和ModuleConfigurationExtensionProvider创建了java类,但这并没有改变任何东西。



我希望你能帮助我并提前感谢。

解决方案

这可以通过IntelliJ IDEA的New Project Wizard功能实现,方法是提供ModuleBuilder的Module / Project类型实现类,即扩展了为智能IDEA提供的扩展点(com.intellij)。



您需要在plugin.xml中进行以下更改才能显示您的新项目向导项目/模块类型列表中的新模块/项目类型。

 < extensions defaultExtensionNs =com .intellij> 
< moduleBuilder builderClass =com.yourcompany.wizards.YourModuleBuilder/>
< / extensions>

为ModuleBuilder类提供 buildlerClass 属性的包,这就够了。 / p>

以下是ModuleBuilder实现示例:

 公共类AsposeJavaModuleBuilder扩展了ModuleBuilder实现SourcePathsBuilder {

private Project myProject;
ResourceBundle bundle = ResourceBundle.getBundle(Bundle);

@Override
public String getBuilderId(){
return getClass()。getName();
}

@Override
public String getPresentableName(){
returnAspose Application;
}

@Override
public String getDescription(){
return bundle.getString(AsposeWizardPanel.myMainPanel.description);


}

@Override
public Icon getBigIcon(){
返回AsposeIcons.AsposeMedium;
}

@Override
public Icon getNodeIcon(){
返回AsposeIcons.AsposeLogo;
}


@Override
public ModuleWizardStep [] createWizardSteps(@NotNull WizardContext wizardContext,@ NotNull ModulesProvider modulesProvider){
return new ModuleWizardStep [] {
new AsposeAPIsWizardStep(this,wizardContext),
};
}


@Override
public void setupRootModel(ModifiableRootModel rootModel)抛出com.intellij.openapi.options.ConfigurationException {



setMyProject(rootModel.getProject());
final CompilerModuleExtension compilerModuleExtension = rootModel.getModuleExtension(CompilerModuleExtension.class);
compilerModuleExtension.setExcludeOutput(true);

if(myJdk!= null){
rootModel.setSdk(myJdk);
} else {
rootModel.inheritSdk();
}

ContentEntry contentEntry = doAddContentEntry(rootModel);
if(contentEntry!= null){
final List< Pair< String,String>> sourcePaths = getSourcePaths();

if(sourcePaths!= null){
for(final Pair< String,String> sourcePath:sourcePaths){
String first = sourcePath.first;
new File(first).mkdirs();
final VirtualFile sourceRoot = LocalFileSystem.getInstance()
.refreshAndFindFileByPath(FileUtil.toSystemIndependentName(first));
if(sourceRoot!= null){
contentEntry.addSourceFolder(sourceRoot,false,sourcePath.second);
}
}
}
}

if(myCompilerOutputPath!= null){
//应该只设置绝对路径
String canonicalPath;
try {
canonicalPath = FileUtil.resolveShortWindowsName(myCompilerOutputPath);
} catch(IOException e){
canonicalPath = myCompilerOutputPath;
}
compilerModuleExtension
.setCompilerOutputPath(VfsUtil.pathToUrl(FileUtil.toSystemIndependentName(canonicalPath)));
} else {
compilerModuleExtension.inheritCompilerOutputPath(true);
}

LibraryTable libraryTable = rootModel.getModuleLibraryTable();
for(Pair< String,String> libInfo:myModuleLibraries){
final String moduleLibraryPath = libInfo.first;
final String sourceLibraryPath = libInfo.second;
Library library = libraryTable.createLibrary();
Library.ModifiableModel modifiableModel = library.getModifiableModel();
modifiableModel.addRoot(getUrlByPath(moduleLibraryPath),OrderRootType.CLASSES);
if(sourceLibraryPath!= null){
modifiableModel.addRoot(getUrlByPath(sourceLibraryPath),OrderRootType.SOURCES);
}
modifiableModel.commit();
}
RunnableHelper.runWhenInitialized(getMyProject(),new Runnable(){
public void run(){
System.out.println(你好我来到这里);
final LibraryTablesRegistrar libTablesRegistrar = LibraryTablesRegistrar.getInstance();

final LibraryTable libraryTable = libTablesRegistrar.getLibraryTable(getMyProject());

final LibraryTable.ModifiableModel libTableModel = libraryTable .getModifiableModel();


库library = libTableModel.createLibrary(AsposeConstants.LIBRARY_NAME);
libTableModel.commit();

@NonNls final String path = getContentEntryPath()+ File.separator + AsposeConstants.LIB_FOLDER;
new File(path).mkdirs();


for(AsposeJavaAPI api:AsposeProject.getApiList( ).values()){
System.out.println(你好我来到这里2);
if(api.is_se lected()){
try {
System.out.println(你好我来到这里3);
AsposeAPIsManager.copyDirectory(AsposeAPIsManager.getLibaryDownloadPath()+ api.get_name()。toLowerCase(),path + File.separator + api.get_name());
} catch(IOException ex){
ex.printStackTrace();
}
String [] children = new File(path + File.separator + api.get_name()。toLowerCase()+ File.separator).list();
for(String _child:children){
String jarPath =jar://+ path + File.separator + api.get_name()+ File.separator + _child +!/;

Library.ModifiableModel model = library.getModifiableModel();

model.addRoot(jarPath,OrderRootType.CLASSES);

model.commit();

}
}
}


集合<模块> modules = ModuleUtil.getModulesOfType(getMyProject(),StdModuleTypes.JAVA);
Iterator itr = modules.iterator();
模块模块= null;
while(itr.hasNext()){
module =(Module)itr.next();
休息;
}
final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);

final ModifiableRootModel moduleRootModel = moduleRootManager.getModifiableModel();

final Library lib = libraryTable.getLibraryByName(AsposeConstants.LIBRARY_NAME);

if(moduleRootModel.findLibraryOrderEntry(lib)== null){

moduleRootModel.addLibraryEntry(lib);

}
moduleRootModel.commit();


}
});
}

@Override
public String getGroupName(){
return JavaModuleType.JAVA_GROUP;
}

public Project getMyProject(){
return myProject;
}

public void setMyProject(Project myProject){
this.myProject = myProject;
}

@Nullable
public ModuleWizardStep getCustomOptionsStep(WizardContext context,Disposable parentDisposable){
AsposeIntroWizardStep step = new AsposeIntroWizardStep();
Disposer.register(parentDisposable,step);
返回步骤;
}


private String myCompilerOutputPath;
//对<源路径,包前缀>
private List< Pair< String,String>> mySourcePaths;
//对<库路径,源路径>
private final List< Pair< String,String>> myModuleLibraries = new ArrayList< Pair< String,String>>();

public final void setCompilerOutputPath(String compilerOutputPath){
myCompilerOutputPath = acceptParameter(compilerOutputPath);
}

public List< Pair< String,String>> getSourcePaths(){
if(mySourcePaths == null){
final List< Pair< String,String>> paths = new ArrayList< Pair< String,String>>();
@NonNls final String path = getContentEntryPath()+ File.separator +src;
new File(path).mkdirs();
paths.add(Pair.create(path,));
返回路径;
}
返回mySourcePaths;
}

public void setSourcePaths(List< Pair< String,String>> sourcePaths){
mySourcePaths = sourcePaths!= null? new ArrayList< Pair< String,String>>(sourcePaths):null;
}

public void addSourcePath(Pair< String,String> sourcePathInfo){
if(mySourcePaths == null){
mySourcePaths = new ArrayList< Pair< String ,String>>();
}
mySourcePaths.add(sourcePathInfo);
}

public ModuleType getModuleType(){
return StdModuleTypes.JAVA;
}

@Override
public boolean isSuitableSdkType(SdkTypeId sdkType){
return sdkType instanceof JavaSdkType;
}

@Nullable
@Override
public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep){
return StdModuleTypes.JAVA.modifySettingsStep(settingsStep,this) ;
}

private static String getUrlByPath(final String path){
return VfsUtil.getUrlForLibraryRoot(new File(path));
}

public void addModuleLibrary(String moduleLibraryPath,String sourcePath){
myModuleLibraries.add(Pair.create(moduleLibraryPath,sourcePath));
}

@Nullable
protected static String getPathForOutputPathStep(){
return null;
}
}

有关创建新模块/项目的完整源代码参考IntelliJ IDEA中的类型,请参阅 Aspose项目向导的源代码(IntelliJ IDEA插件通过 Aspose pty Ltd



可以从以下网址下载源代码:



https://asposejetbrains.codeplex.com/


I am a beginner when it comes to IntelliJ plugin development but i want my plugin to register a new Module Type in the "New Project" / "New Module" windows.

I already searched through the Documentation for plugin developers but wasn't able to find anything useful. I also looked at existing plugins like Kotlin and Scala which also add new Module types but I can't figure out how to get a finished ModuleType to show up in the dialogs mentioned above.

What do I have to change in the plugin.xml file? I already added extensions and created java classes for ModuleType, ModuleBuilder and the ModuleConfigurationExtensionProvider but that doesn't change anything.

I hope you can help me and thanks in advance.

解决方案

This can be achieved through New Project Wizard feature of IntelliJ IDEA, by providing your Module / Project type implementation class of ModuleBuilder i.e by extending the intelliJ IDEA provided extension point for the same (com.intellij).

You need the below changes to make in your plugin.xml for making appear your new Module / Project type in New Project Wizard project /modules types list.

<extensions defaultExtensionNs="com.intellij">
    <moduleBuilder builderClass="com.yourcompany.wizards.YourModuleBuilder"/>
</extensions>

Provide your ModuleBuilder class with package to buildlerClass attribute, thats enough.

Here is sample ModuleBuilder implementation:

public class AsposeJavaModuleBuilder extends ModuleBuilder implements SourcePathsBuilder {

private Project myProject;
ResourceBundle bundle = ResourceBundle.getBundle("Bundle");

@Override
public String getBuilderId() {
    return getClass().getName();
}

@Override
public String getPresentableName() {
    return "Aspose Application";
}

@Override
public String getDescription() {
    return bundle.getString("AsposeWizardPanel.myMainPanel.description");


}

@Override
public Icon getBigIcon() {
    return AsposeIcons.AsposeMedium;
}

@Override
public Icon getNodeIcon() {
    return AsposeIcons.AsposeLogo;
}


@Override
public ModuleWizardStep[] createWizardSteps(@NotNull WizardContext wizardContext, @NotNull ModulesProvider modulesProvider) {
    return new ModuleWizardStep[]{
            new AsposeAPIsWizardStep(this, wizardContext),
    };
}


@Override
public void setupRootModel(ModifiableRootModel rootModel) throws com.intellij.openapi.options.ConfigurationException {



    setMyProject(rootModel.getProject());
    final CompilerModuleExtension compilerModuleExtension = rootModel.getModuleExtension(CompilerModuleExtension.class);
    compilerModuleExtension.setExcludeOutput(true);

    if (myJdk != null) {
        rootModel.setSdk(myJdk);
    } else {
        rootModel.inheritSdk();
    }

    ContentEntry contentEntry = doAddContentEntry(rootModel);
    if (contentEntry != null) {
        final List<Pair<String, String>> sourcePaths = getSourcePaths();

        if (sourcePaths != null) {
            for (final Pair<String, String> sourcePath : sourcePaths) {
                String first = sourcePath.first;
                new File(first).mkdirs();
                final VirtualFile sourceRoot = LocalFileSystem.getInstance()
                        .refreshAndFindFileByPath(FileUtil.toSystemIndependentName(first));
                if (sourceRoot != null) {
                    contentEntry.addSourceFolder(sourceRoot, false, sourcePath.second);
                }
            }
        }
    }

    if (myCompilerOutputPath != null) {
        // should set only absolute paths
        String canonicalPath;
        try {
            canonicalPath = FileUtil.resolveShortWindowsName(myCompilerOutputPath);
        } catch (IOException e) {
            canonicalPath = myCompilerOutputPath;
        }
        compilerModuleExtension
                .setCompilerOutputPath(VfsUtil.pathToUrl(FileUtil.toSystemIndependentName(canonicalPath)));
    } else {
        compilerModuleExtension.inheritCompilerOutputPath(true);
    }

    LibraryTable libraryTable = rootModel.getModuleLibraryTable();
    for (Pair<String, String> libInfo : myModuleLibraries) {
        final String moduleLibraryPath = libInfo.first;
        final String sourceLibraryPath = libInfo.second;
        Library library = libraryTable.createLibrary();
        Library.ModifiableModel modifiableModel = library.getModifiableModel();
        modifiableModel.addRoot(getUrlByPath(moduleLibraryPath), OrderRootType.CLASSES);
        if (sourceLibraryPath != null) {
            modifiableModel.addRoot(getUrlByPath(sourceLibraryPath), OrderRootType.SOURCES);
        }
        modifiableModel.commit();
    }
    RunnableHelper.runWhenInitialized(getMyProject(), new Runnable() {
        public void run() {
            System.out.println("Hello I came here");
            final LibraryTablesRegistrar libTablesRegistrar = LibraryTablesRegistrar.getInstance();

            final LibraryTable libraryTable = libTablesRegistrar.getLibraryTable(getMyProject());

            final LibraryTable.ModifiableModel libTableModel = libraryTable.getModifiableModel();


            Library library = libTableModel.createLibrary(AsposeConstants.LIBRARY_NAME);
            libTableModel.commit();

            @NonNls final String path = getContentEntryPath() + File.separator + AsposeConstants.LIB_FOLDER;
            new File(path).mkdirs();


            for (AsposeJavaAPI api : AsposeProject.getApiList().values()) {
                System.out.println("Hello I came here2");
                if (api.is_selected()) {
                    try {
                        System.out.println("Hello I came here3");
                        AsposeAPIsManager.copyDirectory(AsposeAPIsManager.getLibaryDownloadPath() + api.get_name().toLowerCase(), path + File.separator + api.get_name());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    String[] children = new File(path + File.separator + api.get_name().toLowerCase() + File.separator).list();
                    for (String _child : children) {
                        String jarPath = "jar://" + path + File.separator + api.get_name() + File.separator + _child + "!/";

                        Library.ModifiableModel model = library.getModifiableModel();

                        model.addRoot(jarPath, OrderRootType.CLASSES);

                        model.commit();

                    }
                }
            }


            Collection<Module> modules = ModuleUtil.getModulesOfType(getMyProject(), StdModuleTypes.JAVA);
            Iterator itr = modules.iterator();
            Module module = null;
            while (itr.hasNext()) {
                module = (Module) itr.next();
                break;
            }
            final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);

            final ModifiableRootModel moduleRootModel = moduleRootManager.getModifiableModel();

            final Library lib = libraryTable.getLibraryByName(AsposeConstants.LIBRARY_NAME);

            if (moduleRootModel.findLibraryOrderEntry(lib) == null) {

                moduleRootModel.addLibraryEntry(lib);

            }
            moduleRootModel.commit();


        }
    });
}

@Override
public String getGroupName() {
    return JavaModuleType.JAVA_GROUP;
}

public Project getMyProject() {
    return myProject;
}

public void setMyProject(Project myProject) {
    this.myProject = myProject;
}

@Nullable
public ModuleWizardStep getCustomOptionsStep(WizardContext context, Disposable parentDisposable) {
    AsposeIntroWizardStep step = new AsposeIntroWizardStep();
    Disposer.register(parentDisposable, step);
    return step;
}


private String myCompilerOutputPath;
// Pair<Source Path, Package Prefix>
private List<Pair<String, String>> mySourcePaths;
// Pair<Library path, Source path>
private final List<Pair<String, String>> myModuleLibraries = new ArrayList<Pair<String, String>>();

public final void setCompilerOutputPath(String compilerOutputPath) {
    myCompilerOutputPath = acceptParameter(compilerOutputPath);
}

public List<Pair<String, String>> getSourcePaths() {
    if (mySourcePaths == null) {
        final List<Pair<String, String>> paths = new ArrayList<Pair<String, String>>();
        @NonNls final String path = getContentEntryPath() + File.separator + "src";
        new File(path).mkdirs();
        paths.add(Pair.create(path, ""));
        return paths;
    }
    return mySourcePaths;
}

public void setSourcePaths(List<Pair<String, String>> sourcePaths) {
    mySourcePaths = sourcePaths != null ? new ArrayList<Pair<String, String>>(sourcePaths) : null;
}

public void addSourcePath(Pair<String, String> sourcePathInfo) {
    if (mySourcePaths == null) {
        mySourcePaths = new ArrayList<Pair<String, String>>();
    }
    mySourcePaths.add(sourcePathInfo);
}

public ModuleType getModuleType() {
    return StdModuleTypes.JAVA;
}

@Override
public boolean isSuitableSdkType(SdkTypeId sdkType) {
    return sdkType instanceof JavaSdkType;
}

@Nullable
@Override
public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep) {
    return StdModuleTypes.JAVA.modifySettingsStep(settingsStep, this);
}

private static String getUrlByPath(final String path) {
    return VfsUtil.getUrlForLibraryRoot(new File(path));
}

public void addModuleLibrary(String moduleLibraryPath, String sourcePath) {
    myModuleLibraries.add(Pair.create(moduleLibraryPath, sourcePath));
}

@Nullable
protected static String getPathForOutputPathStep() {
    return null;
}
}

For complete source code reference for creating new Module / Project Types in IntelliJ IDEA, please see the source code of Aspose Project Wizard (an IntelliJ IDEA Plugin By Aspose pty Ltd)

Source codes can be downloaded from the following URL:

https://asposejetbrains.codeplex.com/

这篇关于我如何“注册” IntelliJ插件中的新模块类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆