idea 插件开发记录

2 篇文章 0 订阅

前言:
不要盲目的复制粘贴,我的版本比较高,很多语法不兼容!!!

代码:github上可查看,欢迎start

消息通知类,用于提示一些错误,或者警告
Notification notification = new Notification(StaticString.GROUP_ID, StaticString.CONTENT, NotificationType.INFORMATION);
Notifications.Bus.notify(notification);

效果图:

在这里插入图片描述

弹窗通知,用于提示错误或警告(会阻碍idea的运行)
Messages.showMessageDialog("内容", "标题", Messages.getInformationIcon());

效果图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JBnZ9jKS-1659692757184)(/Users/mr.ahai/Library/Application Support/typora-user-images/image-20220804174825402.png)]

创建一个自己的toolWindow,类似idea中的通知小铃铛之类的

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6bib4IrU-1659692757184)(/Users/mr.ahai/Library/Application Support/typora-user-images/image-20220805095301190.png)]

创建窗体之后就可根据自己的需求进行组件拖拽

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tVK4leLi-1659692757184)(/Users/mr.ahai/Library/Application Support/typora-user-images/image-20220805095402343.png)]

//创建一个自己的windowsFactory,之后实现createToolWindowContent方法,将自己创建的窗体添加到idea中
public class TempPluginWindowsFactory implements ToolWindowFactory {
    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        TempPluginUI tempPluginUI = new TempPluginUI(project, toolWindow);
        ContentFactory contentFactory = ContentFactory.getInstance();
        Content content = contentFactory.createContent(tempPluginUI.getPanel1(), "", false);
        toolWindow.getContentManager().addContent(content);
    }
}
//注意配置!!!,在这个extensions标签下添加toolWindows
    <extensions defaultExtensionNs="com.intellij">
        <toolWindow factoryClass="com.example.tempplugin.factory.TempPluginWindowsFactory" id="tempPluginWindowsFactory" anchor="right" icon="AllIcons.Actions.Colors" />
    </extensions>

效果图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Oc3aroIn-1659692757185)(/Users/mr.ahai/Library/Application Support/typora-user-images/image-20220805095850890.png)]

添加一个表格在toolwindows里面
请在JScrollPane下面添加table,否则会不显示table的header的

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qsr03vob-1659692757185)(/Users/mr.ahai/Library/Application Support/typora-user-images/image-20220805154929061.png)]

添加完成之后,在创建好的UI类中添加数据
public class TempData {
    /**
     * 创建一个表头
     */
    public static String[] HEADER = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
    /**
     * 创建数据便于演示
     */
    public static Object[][] TABLE_DATA = {
            {"Kathy", "Smith", "Snowboarding", 5, false},
            {"John", "Doe", "Rowing", 3, true},
            {"Sue", "Black", "Knitting", 2, false},
            {"Jane", "White", "Speed reading", 20, true},
            {"Joe", "Brown", "Pool", 10, false}
    };
    public static DefaultTableModel DEFAULT_TABLE_MODEL = new DefaultTableModel(TABLE_DATA, HEADER);
}
 // 将数据放入表格中
table1.setModel(TempData.DEFAULT_TABLE_MODEL);
table1.setVisible(true);

效果图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nwQkSpTO-1659692757185)(/Users/mr.ahai/Library/Application Support/typora-user-images/image-20220805154817427.png)]


使用idea插件操作编辑器中的内容,实现内容的修改
操作编辑器的方法如下:
package com.example.tempplugin.util;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.richcopy.settings.RichCopySettings;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

/**
 * 用于编辑选中的文本
 *
 * @author mr.ahai
 */
public class EditTextUtil {

    private EditTextUtil() {
    }

    /**
     * 将选中的内容放到剪切板中
     *
     * @return 返回选中的内容
     */
    public static String getSysClipboardText() {
        String ret = "";
        Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
        //获取剪切板中的内容
        Transferable contents = sysClip.getContents(null);
        if (contents != null) {
            //检查是否是文本类型
            if (!contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return ret;
            }
            try {
                ret = (String) contents.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ret;
    }


    /**
     * 将剪切板的内容设为空
     *
     * @param writeText 之前剪切板存在的内容
     */
    public static void setSysClipboardText(String writeText) {
        Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        StringSelection stringSelection = new StringSelection(writeText);
        systemClipboard.setContents(stringSelection, null);
    }

    /**
     * 替换选中的文字
     *
     * @param e AnActionEvent
     */
    public static void replaceMsg(AnActionEvent e) {
        RichCopySettings settings = RichCopySettings.getInstance();
        settings.setEnabled(true);
        ActionManager.getInstance().getAction("EditorCopy").actionPerformed(e);
        settings.setEnabled(settings.isEnabled());
        String sysClipboardText = getSysClipboardText();
        //弹出一个弹框
        Messages.showMessageDialog(sysClipboardText, "您确定要替换吗", Messages.getInformationIcon());
        String tempText = "我是要替换的文字!!!!!";
        Project project = e.getData(CommonDataKeys.PROJECT);
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (null == editor) {
            return;
        }
        e.getPresentation().setVisible(project != null && editor.getSelectionModel().hasSelection());
        Document document = editor.getDocument();
        SelectionModel selectionModel = editor.getSelectionModel();
        int start = selectionModel.getSelectionStart();
        int end = selectionModel.getSelectionEnd();
        Runnable runnable = () -> document.replaceString(start, end, tempText);
        WriteCommandAction.runWriteCommandAction(project, runnable);
        selectionModel.removeSelection();
        setSysClipboardText(sysClipboardText);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值