其他分享
首页 > 其他分享> > idea插件开发笔记——右键菜单添加自定义模板

idea插件开发笔记——右键菜单添加自定义模板

作者:互联网

  1. 再resources文件下创建fileTemplates/internal文件夹(必须是这个)

  2. 添加模板文件 mapper.tsx.ft

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="${NAMESPACE}" >
    </mapper>
    
  3. 创建右键菜单mapper模板创建

          //读取模板
            FileTemplate template = FileTemplateManager.getInstance(project)
                    .getInternalTemplate("mapper");
            Properties properties = new Properties();
            //替换模板里的参数
            properties.setProperty("NAMESPACE", "");
            String renderedText = "";
            try {
                renderedText += template.getText(properties);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
    
            PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText("mapper.xml", XMLLanguage.INSTANCE, renderedText);
            System.out.println(psiFile.isPhysical());
            try {
                //执行写入文件
                Runnable runnable = () -> psiElement.add(psiFile);
                //调度任务
                WriteCommandAction.runWriteCommandAction(project, runnable);
            } catch (Exception exception) {
                //文件存在,会抛错;idea发送通知
                NotificationGroup notificationGroup = new NotificationGroup("testid", NotificationDisplayType.BALLOON, false);
                Notification notification = notificationGroup.createNotification("File already exists", MessageType.INFO);
                Notifications.Bus.notify(notification);
            }
    

标签:mapper,插件,自定义,project,右键,new,properties,模板,psiFile
来源: https://www.cnblogs.com/Crazy-Fengzi/p/16645270.html