如何在IntelliJ中创建支持type.ilist的Instancio Live Template?
解决IntelliJ Instancio Live Template类型捕获问题
- 打开IntelliJ设置(快捷键
Ctrl+Alt+S),导航到Editor > Live Templates - 点击右侧
+号,先新建一个模板组(比如命名为Instancio),再在该组下新建Live Template - 在模板设置中完成以下配置:
- Abbreviation填写
ilist - Template text输入:
List<$TYPE$> $VAR_NAME$ = Instancio.createList($TYPE$.class) - 点击
Edit variables按钮,配置变量规则:TYPE的Expression填写groovyScript("_1.text", complete()),勾选Skip if definedVAR_NAME的Expression填写suggestVariableName(),勾选Skip if defined
- 设置模板的Applicable in范围为
Java(勾选Java相关上下文,比如Java表达式、声明等)
- Abbreviation填写
- 保存设置后,输入
Animal.ilist再按Tab,即可自动生成List<Animal> animalList = Instancio.createList(Animal.class)
核心逻辑是通过groovyScript("_1.text", complete())捕获输入时前缀的类型(如Animal),将其赋值给TYPE变量,实现模板中对应位置的自动替换。
内容的提问来源于stack exchange,提问作者Vml11
相关产品推荐
相关产品推荐

