写一个亲爱的idea工具的插件 1

初步图片

使用github上的idea plugin模板开始制作

package com.github.moocstudent.plugin1.listeners

import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManagerListener
import com.github.moocstudent.plugin1.services.MyProjectService

internal class MyProjectManagerListener : ProjectManagerListener {

    override fun projectOpened(project: Project) {
        project.service<MyProjectService>()
    }
}

默认语言是kotlin
可以试着转为java,可以参考我的kotlin笔记 https://www.yuque.com/docs/share/c637c257-8ad8-46cb-914c-ff538a116479?#实际我参与过仅仅一个kotlin的项目,但是这个语言确认精炼.
上面的代码转为java:

package com.github.moocstudent.plugin1.listeners;

import com.github.moocstudent.plugin1.services.MyProjectServiceJ;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;

/**
 * @Author: zhangQi
 * @Date: 2021-12-29 9:34
 */
public class MyProjectManagerListenerJ implements ProjectManagerListener {

    @Override
    public void projectOpened(Project project){
        project.getService(MyProjectServiceJ.class);
    }
}

其中里面的.class可以直接使用kotlin的类

package com.github.moocstudent.plugin1.listeners;

import com.github.moocstudent.plugin1.services.MyProjectService;
import com.github.moocstudent.plugin1.services.MyProjectServiceJ;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;

/**
 * @Author: zhangQi
 * @Date: 2021-12-29 9:34
 */
public class MyProjectManagerListenerJ implements ProjectManagerListener {

    @Override
    public void projectOpened(Project project){
        project.getService(MyProjectService.class);
    }
}

因为两者都是运行在JVM上的语言,所以这个kotlin类也可以编译为class

package com.github.moocstudent.plugin1.services

import com.intellij.openapi.project.Project
import com.github.moocstudent.plugin1.MyBundle

class MyProjectService(project: Project) {

    init {
        println(MyBundle.message("projectService", project.name))
    }
}

如果不是从plugin template配置,需要配置一下idea平台的SDK,具体不说了,这里感觉还是直接template比较合适
因为我配置非templateSDK时,没有引入相对应的类,因为不仅对IDEA版本有编排也是对JDK的版本的一个编排问题
使用plugin template创建了一个AnAction继承类

点击后正常弹出提示:

参考:
https://github.com/moocstudent/plugin1
https://plugins.jetbrains.com/docs/intellij/github-template.html
https://plugins.jetbrains.com/docs/intellij/working-with-custom-actions.html#extending-the-update-method
https://plugins.jetbrains.com/docs/marketplace/about-marketplace.html#what-are-the-benefits-of-using-jetbrains-marketplace-extensions
https://www.zhihu.com/question/378975906/answer/1079100012
https://www.cnblogs.com/mushan/p/12275581.html

原文地址:https://www.cnblogs.com/ukzq/p/15741104.html