Jetbrains插件开发教程

在我们日常开发中,使用编辑器时,如IDEA、Android Studio,经常会用到一些插件以便于方便我们的开发,那么一个插件究竟是怎么从0到1开发出来的呢,下面就让我们一起来开发一款简单的插件,探索其中的奥妙吧!

1.安装Plugin DevKit插件

1.1 插件市场搜索Plugin DevKit

2.创建项目

2.1 建项目

2.2 修改build.gradle.kts文件

repositories {
//    mavenCentral()
    maven {
        url = uri("https://maven.aliyun.com/repository/public")
    }
}

2.3 配置Intellij Platform Plugin SDK

3.编写插件action

插件功能是在IDEA中使用浏览器功能

3.1 编写Action

public class OpenWebViewAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();
        if (project == null) {
            Messages.showErrorDialog("No project found.", "Error");
            return;
        }
        if (!JBCefApp.isSupported()) {
            Messages.showErrorDialog("JCEF is not supported in this environment.", "Error");
            return;
        }
        // 打开右侧边栏
        ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("WebView");
        if (toolWindow != null) {
            toolWindow.show(null);
        }
    }
}

3.2 编写WebViewToolWindow

使用com.intellij.ui.jcef.JBCefBrowser内嵌的Chromium浏览器

public class WebViewToolWindow implements ToolWindowFactory {
    private JBCefBrowser browser;
    private JTextField urlField;

    @Override
    public void createToolWindowContent(Project project, ToolWindow toolWindow) {
        // 创建主面板
        JPanel panel = new JPanel(new BorderLayout());

        // 创建地址栏和访问按钮
        JPanel topPanel = new JPanel(new BorderLayout());
        urlField = new JTextField("http://47.120.64.211:8090/"); // 默认网址
        JButton visitButton = new JButton("你点我噻");
        topPanel.add(urlField, BorderLayout.CENTER);
        topPanel.add(visitButton, BorderLayout.EAST);

        // 创建浏览器组件
        browser = new JBCefBrowser();
        panel.add(topPanel, BorderLayout.NORTH);
        panel.add(browser.getComponent(), BorderLayout.CENTER);

        // 处理访问按钮点击事件
        visitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String url = urlField.getText().trim();
                if (!url.isEmpty()) {
                    if (!url.startsWith("http://") && !url.startsWith("https://")) {
                        url = "https://" + url; // 默认使用 HTTPS
                    }
                    browser.loadURL(url);
                }
            }
        });

        // 将面板添加到 ToolWindow
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        Content content = contentFactory.createContent(panel, "", false);
        toolWindow.getContentManager().addContent(content);
    }
}

4.配置plugin.xml

<idea-plugin>
    <id>com.example.webviewplugin</id>
    <name>WebViewPlugin</name>
    <version>1.0</version>
    <vendor email="1158672373@qq.com" url="http://www.example.com">xiaodong</vendor>
    <description>这个插件可以让我们在IDEA中方便使用浏览器,让我们程序员开发更便捷,更快速,同时也能让我们方便浏览网页</description>
    <depends>com.intellij.modules.platform</depends>

    <actions>
        <action id="com.example.webviewplugin.OpenWebViewAction" class="com.example.webviewplugin.OpenWebViewAction" text="Open xiaodong's WebView" description="Open a WebView in a ToolWindow">
            <add-to-group group-id="ToolsMenu" anchor="last"/>
        </action>
    </actions>

    <extensions defaultExtensionNs="com.intellij">
        <toolWindow id="WebView" anchor="right" factoryClass="com.example.webviewplugin.WebViewToolWindow"/>
    </extensions>
</idea-plugin>

5.运行插件

注意:在第一次运行时,会下载Gradle 需要下载一个 IntelliJ IDEA Community Edition 的发行版(即 ideaIC-2022.1.zip),因为这是运行插件的最小化 IDE 环境,还有ideaIC-2022.1-sources.jar等文件,下载完毕即可启动,后续不需要再下载。

启动之后会打开一个新的IDEA窗口,找到我们的右边侧栏种WebView按钮点击

6.打包

6.1 使用Gradle

6.2 jar包位置

在build下的libs中

7.引入jar包并使用插件

打完jar包之后就可以分享给其他小伙伴使用了,使用时通过Plugins中的Install Plugin from Disk,选择刚刚打好的jar包即可

8.发布到jetbrains官方插件应用市场

8.1 访问官网:https://plugins.jetbrains.com/

8.2 点击build plugins

8.3 点击upload plugin

8.4 填写信息后提交,等待审核通过即可

8.5 审核通过后会有邮件提醒,然后在插件应用市场搜索安装即可