idea插件开发记录

2023-03-31 09:41:18 浏览数 (1)

插件开发示例


功能开发代码示例

java

代码语言:javascript复制
package com.cjl.plugins.code.hints;

import com.cjl.plugins.code.code.NavigatorPanel;
import com.cjl.plugins.code.http.HttpUtils;
import com.cjl.plugins.code.json.Json;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.apache.http.util.TextUtils;

public class CodeHints extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        final Editor mEditor = e.getData(PlatformDataKeys.EDITOR);
        if (null == mEditor) {
            return;
        }
        SelectionModel model = mEditor.getSelectionModel();
        final String selectedText = model.getSelectedText();
        if (TextUtils.isEmpty(selectedText)) {
            return;
        }
        String baseUrl = "http://fanyi.youdao.com/openapi.do?keyfrom=Skykai521&key=977124034&type=data&doctype=json&version=1.1&q=";
        String fanyi = HttpUtils.doGet(baseUrl   selectedText);
        HintManager instance = HintManager.getInstance();
        instance.showInformationHint(mEditor, Json.jsonFormat(fanyi));
//        ToolWindow toolWindow = ToolWindowManager.getInstance(e.getProject()).getToolWindow("CodeHints");
//        toolWindow.activate(()->{
////            ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
//            NavigatorPanel navigatorPanel = new NavigatorPanel(toolWindow, e.getProject());
////            Content content = contentFactory.createContent(navigatorPanel, "", false);
//            navigatorPanel.setList(null);
////            toolWindow.getContentManager().addContent(content);
//        });
    }
}

plugin.xml

代码语言:javascript复制
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
    <id>org.jetbrains.plugins.template</id>
    <name>Template</name>
    <vendor>JetBrains</vendor>

    <depends>com.intellij.modules.platform</depends>

    <extensions defaultExtensionNs="com.intellij">
        <applicationService serviceImplementation="org.jetbrains.plugins.template.services.MyApplicationService"/>
        <projectService serviceImplementation="org.jetbrains.plugins.template.services.MyProjectService"/>
<!--        <toolWindow id="CodeHints" secondary="true" anchor="right" canCloseContents="true"-->
<!--                    factoryClass="com.cjl.plugins.code.code.CodeToolWindow"/>-->
    </extensions>

    <applicationListeners>
        <listener class="org.jetbrains.plugins.template.listeners.MyProjectManagerListener"
                  topic="com.intellij.openapi.project.ProjectManagerListener"/>
    </applicationListeners>
    <actions>
        <group id="MyPlugin.SampleMenu" text="_Sample Menu" description="Sample menu">
            <add-to-group group-id="MainMenu" anchor="last"  />
            <action id="Myplugin.Textboxes" class="com.cjl.plugins.code.hints.Hints" text="Text _Boxes" description="A test menu item" />
        </group>
        <group id="cjlPlugins" text="cjlPlugins" description="cjlPlugins">
            <add-to-group group-id="MainMenu" anchor="last"  />
            <action id="cjlPluginsAction" class="com.cjl.plugins.code.hints.CodeHints" text="翻译" description="A test menu item" >
                <keyboard-shortcut keymap="$default" first-keystroke="C" second-keystroke="C"/>
            </action>
        </group>
<!--        <group id="Test-Group-id" text="Test-Group" >-->

<!--        </group>-->
    </actions>
</idea-plugin>

快捷键CC即可实现选定代码翻译功能


功能展示

翻译选中要翻译的英文点击快捷键 c c 点两次C

打包

查看版本

2020.0=202 版本配置不对,安装插件会导致报错

修改插件版本并打包

安装插件

0 人点赞