idea插件开发笔记——右键菜单添加自定义模板
-
再resources文件下创建fileTemplates/internal文件夹(必须是这个)
-
添加模板文件 mapper.xml.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> -
创建右键菜单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); }
2022年9月3日更新日志;通知更新至最新API
NotificationGroupManager.getInstance().getNotificationGroup("notificationGroup")
.createNotification("filename cannot be null", NotificationType.WARNING)
.notify(project);
详情看官方文档;https://plugins.jetbrains.com/docs/intellij/notifications.html#top-level-notifications-balloons

避坑指南:在读取模板时,需要把模板内容中的\r\n全部替换成\n;详情看官方文档



本文来自博客园,作者:Sadness_sa,转载请注明原文链接:https://www.cnblogs.com/sadness-sa/p/16645270.html

浙公网安备 33010602011771号