Java “我该怎么做?”;登记册;IntelliJ插件中的新模块类型?

Java “我该怎么做?”;登记册;IntelliJ插件中的新模块类型?,java,intellij-idea,module,project,intellij-plugin,Java,Intellij Idea,Module,Project,Intellij Plugin,我是IntelliJ插件开发的初学者,但我希望我的插件在“新项目”/“新模块”窗口中注册一个新的模块类型 我已经搜索了插件开发人员的文档,但是没有找到任何有用的东西。我还研究了Kotlin和Scala等现有插件,它们也添加了新的模块类型,但我不知道如何在上面提到的对话框中显示完成的模块类型 我必须在plugin.xml文件中更改什么?我已经为ModuleType、ModuleBuilder和ModuleConfigurationExtensionProvider添加了扩展并创建了java类,但这

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

我已经搜索了插件开发人员的文档,但是没有找到任何有用的东西。我还研究了Kotlin和Scala等现有插件,它们也添加了新的模块类型,但我不知道如何在上面提到的对话框中显示完成的模块类型

我必须在plugin.xml文件中更改什么?我已经为ModuleType、ModuleBuilder和ModuleConfigurationExtensionProvider添加了扩展并创建了java类,但这并没有改变任何事情


我希望您能提前帮助我并表示感谢。

这可以通过IntelliJ IDEA的新项目向导功能来实现,方法是提供ModuleBuilder的模块/项目类型实现类,即扩展IntelliJ IDEA为其提供的扩展点(com.IntelliJ)

您需要在plugin.xml中进行以下更改,以便在“新建项目向导”“项目/模块类型”列表中显示新模块/项目类型。

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

为ModuleBuilder类提供包tobuildlerClass属性,这就足够了

以下是ModuleBuilder实现示例:

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;
}
}
公共类AsposeJavaModuleBuilder扩展ModuleBuilder实现SourcePathsBuilder{
私人项目myProject;
ResourceBundle=ResourceBundle.getBundle(“bundle”);
@凌驾
公共字符串getBuilderId(){
返回getClass().getName();
}
@凌驾
公共字符串getPresentableName(){
返回“Aspose应用程序”;
}
@凌驾
公共字符串getDescription(){
返回bundle.getString(“AsposeWizardPanel.myMainPanel.description”);
}
@凌驾
公共图标getBigIcon(){
返回AsposeIcons.AsposeMedium;
}
@凌驾
公共图标getNodeIcon(){
返回AsposeIcons.AsposeLogo;
}
@凌驾
public ModuleWizardStep[]createWizardSteps(@NotNull WizardContext WizardContext,@NotNull ModuleProvider ModuleProvider){
返回新的ModuleWizardStep[]{
新的AsposeAPIsWizardStep(此,wizardContext),
};
}
@凌驾
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);
}否则{
rootModel.inheritSdk();
}
ContentEntry=doAddContentEntry(rootModel);
if(contentEntry!=null){
最终列表SourcePath=GetSourcePath();
if(sourcepath!=null){
for(最后一对源路径:源路径){
String first=sourcePath.first;
新文件(第一个).mkdirs();
最终虚拟文件sourceRoot=LocalFileSystem.getInstance()
.refreshAndFindFileByPath(FileUtil.toSystemIndependentName(第一个));
if(sourceRoot!=null){
addSourceFolder(sourceRoot,false,sourcePath.second);
}
}
}
}
if(myCompilerOutputPath!=null){
//应该只设置绝对路径
字符串规范路径;
试一试{
canonicalPath=FileUtil.resolveShortWindowsName(myCompilerOutputPath);
}捕获(IOE异常){
canonicalPath=myCompilerOutputPath;
}
编译器模块扩展
.setCompilerOutputPath(VfsUtil.pathToUrl(FileUtil.toSystemIndependentName(canonicalPath));
}否则{
compilerModuleExtension.InheriteCompilerOutputPath(true);
}
LibraryTable LibraryTable=rootModel.getModuleLibraryTable();
for(对libInfo:myModuleLibraries){
最终字符串moduleLibraryPath=libInfo.first;
最后一个字符串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()){
公开募捐{
System.out.println(“你好,我来过这里”);
final LibraryTablesRegistrator libTablesRegistrar=LibraryTablesRegistrar.getInstance();
final LibraryTable LibraryTable=libtableregistrator.getLibraryTable(getMyProject());
final LibraryTable.ModifiableModel libTableModel=LibraryTable.getModifiableModel();
Library Library=libTableModel.createLibrary(AsposeConstants.Library\u NAME);
提交();
@非NLS最终字符串路径=getContentEntryPath()+File.separator+asposeContents.LIB_文件夹;
新文件(路径).mkdirs();
for(AsposeJavaAPI:AsposeProject.getApiList().values()){
System.out.println(“你好,我来了2”);
如果(api.is_selected()){
试一试{
System.out.println(“你好,我来了3”);
AsposeAPIsManager.copyDirectory(AsposeAPIsManager.getlibary下载路径()+api.get_name().toLowerCase(),path+File.separator+api.get_name());
}捕获(IOEX异常){
例如printStackTrace();
}
String[]children=新文件(path+File.separator+api.get_name().toLowerCase()+File.separator).list();
对于(字符串_子项:ch