Jetbrains 插件开发教程

699 阅读2分钟

文本绑定

在 resource 文件夹下新建文件 ApiGeneratorPlusBundle.properties,输入如下内容

test.message=test message
test.message.title=test message title
test.message.notification.listener=test message notification listener <a href="{0}">Link</>

新建类 ApiGeneratorPlusBundle.class

package site.forgus.plugins.apigeneratorplus;

import com.intellij.AbstractBundle;
import com.intellij.ide.IdeBundle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.PropertyKey;

/**
 * @author lmx 2021/11/14 16:32
 **/

public class ApiGeneratorPlusBundle extends AbstractBundle {

    public static String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, @NotNull Object... params) {
        return INSTANCE.getMessage(key, params);
    }

    public static final String BUNDLE = "messages.ApiGeneratorPlusBundle";
    private static final ApiGeneratorPlusBundle INSTANCE = new ApiGeneratorPlusBundle();
    protected ApiGeneratorPlusBundle() {
        super(BUNDLE);
    }
}

在程序中使用

// message 的值为 test message
String message = ApiGeneratorPlusBundle.message("test.message");

// message 的值为 test message notification listener <a href="{0}">Link</>
String message = ApiGeneratorPlusBundle.message("test.message.notification.listener");


// 替换模板中的变量 {0}, message 的值为 test message notification listener <a href="#test">Link</>
String message = ApiGeneratorPlusBundle.message("test.message.notification.listener", "#test");

通知

创建一个通知

package site.forgus.plugins.apigeneratorplus.action.com.example.action;

import com.intellij.notification.*;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import site.forgus.plugins.apigeneratorplus.icons.SdkIcons;

import javax.swing.event.HyperlinkEvent;
import com.intellij.openapi.diagnostic.Logger;

public class TestNotificationAction extends AnAction {

    private Logger LOG = Logger.getInstance(TestNotificationAction.class);

    @Override
    public void actionPerformed(AnActionEvent actionEvent) {
        // TODO: insert action logic here
        Project project = actionEvent.getProject();
        
        // 创建通知开始
        NotificationGroup notificationGroup = new NotificationGroup("Java2Json.NotificationGroup",
                NotificationDisplayType.BALLOON, true, null, SdkIcons.Logo);

        NotificationListener.Adapter notificationListener = new NotificationListener.Adapter() {
            @Override
            protected void hyperlinkActivated(@NotNull Notification notification, @NotNull HyperlinkEvent e) {
                // e.getDescription() 的值就是标签 a 中的 href 属性值
                System.out.println("你点击了我" + e.getDescription() + "🎉🎉");
            }
        };
        String title = "我来组成标题👶";
        String subtitle = "我来组成副标题👨‍🎓";
        String content = "我来组成身体👨‍🦯。<a href="我的头">来点我的头</a>.\n" +
                "<a href="来点我的左手">我的左手</a> <a href="我的右手">来点我的右手</a> <a href="我的肚腩">来点我的肚腩</a>";
        Notification notification = notificationGroup.createNotification(title, subtitle,
                content, NotificationType.INFORMATION, notificationListener);
        Notifications.Bus.notify(notification, project);
        // 创建通知结束
    }
}

把这个Action 注册到 plugin.xml 文件中

</idea-plugin>
    <actions>
        <action id="com.example.action.TestNotificationAction"
           class="site.forgus.plugins.apigeneratorplus.action.com.example.action.TestNotificationAction"
                text="Test Notification Action" description="Test Notification Action Description">
            <add-to-group group-id="ToolsMenu" anchor="first"/>
        </action>
    </actions>
</idea-plugin>

image.png

image.png

创建 NotificationGroup 是传入的 DisplayId 可以在这里查看

image.png

如果 a 标签语法写错了的话会出现这样的情况

image.png

点击 show balloon 弹出正常情况的显示 🎉✨✨

image.png

日志

import com.intellij.openapi.diagnostic.Logger;
private Logger LOG = Logger.getInstance(TestNotificationAction.class);
LOG.info("你打印了一条日志");

这种方法打印的日志会写到 IDE 的日志文件中,Debug 时不会打印到控制台
查看打印的日志:

ec26cc72fb691cc2917917e59201eca.png

打开 idea.log 文件,你会看到日志如下:

image.png

Action

新建一个 action

package site.forgus.plugins.apigeneratorplus.action.com.example.action;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationListener;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import site.forgus.plugins.apigeneratorplus.ApiGeneratorPlusBundle;
import site.forgus.plugins.apigeneratorplus.util.NotificationUtil;

import javax.swing.event.HyperlinkEvent;
import java.util.logging.Logger;

public class TestNotificationAction extends AnAction {

    private Logger log = Logger.getLogger(TestNotificationAction.class.getName());

    @Override
    public void actionPerformed(AnActionEvent actionEvent) {
        // TODO: insert action logic here
        Project project = actionEvent.getProject();

        // 自定义链接状态改变时的逻辑,如:点击链接后,hyperlinkUpdate 方法会被调用
        // 也可以实现 NotificationListener.Adapter ,这个类默认处理点击事件
        //NotificationListener notificationListener = new NotificationListener() {
        //    @Override
        //    public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
        //        log.info("execute hyperlinkUpdate");
        //    }
        //};

        NotificationListener.Adapter notificationListener = new NotificationListener.Adapter() {
            @Override
            protected void hyperlinkActivated(@NotNull Notification notification, @NotNull HyperlinkEvent e) {
                log.info("execute hyperlinkUpdate");
            }
        };
        String message = "The IDE has been updated by Snap. <a href="{0}">Blog post</a>.";
        String title = ApiGeneratorPlusBundle.message("group.site.forgus.plugins.apigeneratorplus.action.CopyAsActionGroup.text");
        Notification notification = NotificationUtil.notificationGroup.createNotification(title,
                message, NotificationType.INFORMATION, notificationListener);
        Notifications.Bus.notify(notification, project);
    }
}