2020-12-03 06:08:30 登录注册 RSS

当前位置: 公理网 >> 曝光信息 >> IDEA下利用LiveTemplate编写注释模版

IDEA下利用LiveTemplate编写注释模版
发布时间:10-19| 来源:公理网 | 点击发表评论
注释模版注释模版是指在编写代码时针对不同的类方法或者其他文件时自动生成的注释信息。在eclipse我们很熟悉可以利用/**,Enter在方法类名等前一行来生成注释模板但是IntelliJIDEA并没有去按照原来的方法去实现。他引进了LiveTemplate来达到类似效果。从eclipse到IDEAeclipse中的注释模板在codeTemplate中,它存储的形式xml文件。修改相关配置如下图eclipsecodetemplate模板样式如下:/***Title:filename?Package{package_name}*Description:TODO*authorsyncwt*datedate{time}*versionV1.0*/配置好之后只需在方法类名等前一行输入/**,Enter即可生成注释。2.IDEA中也有在文件头注释的模板。但仅限于类文件头和所有文件头。配置如下图IDEAFileandCodeTemplate这显然难以满足我们开发的需求,这时候就需要用LiveTemplateLiveTemplate介绍用中文应该叫做热加载模板。它的原理就是配置一些常用代码字母缩写在输入简写时可以出现你预制的模板内容使得开发效率大大提高。最简单的例子就是在Java中输入sout会出现System.out.println();如图所示代码提示同样注解的模板配置也是采用这样的原理和形式来进行配置。配置我当前的版本是2017.1EAP,其他版本应该都差不多。在配置当中找到LiveTemplate,右边加号先添加一个TemplateGroup,选中该分组再点击加号添加一个LiveTemplate.Abbreviation中填写命令,Description填写描述,Templatetext填写你的配置模板。配置模板配置的规则是这样子的变量名以variable_name的形式出现,调用的方法也是先以变量形式出现写完模板信息后去编辑变量(Editvariables)编辑变量找到相应的的方法对应变量名即可。方法名即功能描述表见附件。我这个版本有点坑变量名的default值无法显示所以我直接在Templatetext中写死了一些信息样例Abbreviation:ccDescription:classcommenttemplateTemplatetext:/***class_name:CLASSNAME*package:PACKAGENAME*describe:TODO*creat_user:wanwtsenthinkcom*creat_date:CREATDATE*creat_time:CREATTIME**/Templatevariables:CLASS_NAME-className()PACKAGE_NAME-currentPackage()CREAT_DATE-date()CREAT_TIME-time()Abbreviation:mcDescription:methodcommenttemplateTemplatetext:/***class_name:METHODNAME*param:METHODPARAM*describe:TODO*creat_user:wanwtsenthinkcom*creat_date:CREATDATE*creat_time:CREATTIME**/Templatevariables:METHOD_NAME-methodName()METHOD_PARAM-methodParameters()CREAT_DATE-date()CREAT_TIME-time()效果效果不足1.livetemplate中的函数方法是读取当前函数体的属性所以只有在该方法内使用该命令才能获取,类没有这个问题。2.Templatevariables的Expression不能叠加方法。定制化程度不够好。附件PredefinedfunctionstouseinlivetemplatevariablesItemDescriptionannotated(“annotationqname”)Createsasymboloftypewithanannotationthatresidesatthespecifiedlocation.Foranexample,seeLiveTemplatesintheiterationsgroup.arrayVariable()Suggestsallarrayvariablesapplicableinthecurrentscope.Foranexample,seeLiveTemplatesintheiterationsgroup.anonymousSuper()SuggestsasupertypeforaKotlinobjectexpression.camelCase(String)Returnsthestringpassedasaparameter,convertedtocamelcase.Forexample,my-text-file/mytextfile/my_text_filewillbeconvertedtomyTextFile.capitalize(String)Capitalizesthefirstletterofthenamepassedasaparameter.capitalizeAndUnderscore(sCamelCaseName)CapitalizesthealllettersofaCamelCasenamepassedasaparameter,andinsertsanunderscorebetweentheparts.Forexample,ifthestringpassedasaparameterisFooBar,thenthefunctionreturnsFOO_BAR.castToLeftSideType()Caststheright-sideexpressiontotheleft-sideexpressiontype.Itisusedintheiterationsgrouptohaveasingletemplateforgeneratingbothraw-typeandGenericsCollections.className(sClassName)Returnsthenameofthecurrentclass(theclasswherethetemplateisexpanded).classNameComplete()Thisexpressionsubstitutesfortheclassnamecompletionatthevariableposition.clipboard()Returnsthecontentsofthesystemclipboard.snakeCase(String)ReturnsCamelCasestringoutofsnake_casestring.Forexample,ifthestringpassedasaparameterisfoo_bar,thenthefunctionreturnsfooBar.complete()Thisexpressionsubstitutesforthecodecompletioninvocationatthevariableposition.completeSmart()Thisexpressionsubstitutesforthesmarttypecompletioninvocationatthevariableposition.componentTypeOf(arrayvariableorarraytype)Returnscomponenttypeofanarray.Forexample,seetheLiveTemplatesintheiterationsgroupintheothergroup.currentPackage()Returnsthecurrentpackagename.date(sDate)Returnsthecurrentsystemdateinthespecifiedformat.Bydefault,thecurrentdateisreturnedinthedefaultsystemformat.However,ifyouspecifydateformatindoublequotes,thedatewillbepresentedinthisformat:decapitalize(sName)Replacesthefirstletterofthenamepassedasaparameterwiththecorrespondinglowercaseletter.descendantClassEnum(String)Showsthechildrenoftheclassenteredasastringparameter.enum(sCompletionString1,sCompletionString2,…)Listofcomma-delimitedstringssuggestedforcompletionatthetemplateinvocation.escapeString(sEscapeString)Escapesthespecifiedstring.expectedType()Returnsthetypewhichisexpectedasaresultofthewholetemplate.Makessenseifthetemplateisexpandedintherightpartofanassignment,afterreturn,etc.fileName(sFileName)Returnsfilenamewithextension.fileNameWithoutExtension()Returnsfilenamewithoutextension.firstWord(sFirstWord)Returnsthefirstwordofthestringpassedasaparameter.groovyScript(“groovycode”)ReturnsGroovyscriptwiththespecifiedcode.YoucanusegroovyScriptmacrowithmultiplearguments.Thefirstargumentisascripttextthatisexecutedorapathtothefilethatcontainsascript.Thenextargumentsareboundto_1,_2,_3,…_nvariablesthatareavailableinsideyourscript.Also,_editorvariableisavailableinsidethescript.Thisvariableisboundtothecurrenteditor.guessElementType(container)Makesaguessonthetypeofelementsstoredinajava.util.Collection.Tomakeaguess,IntelliJIDEAtriestofindtheplaceswheretheelementswereaddedtoorextractedfromthecontainer.iterableComponentType(ArrayOrIterable)Returnsthetypeofaniterablecomponent,suchasanarrayoracollection.iterableVariable()Returnsthenameofavariablethatcanbeiterated.lineNumber()Returnsthecurrentlinenumber.lowercaseAndDash(String)Returnslowercaseseparatedbydashes,ofthestringpassedasaparameter.Forexample,thestringMyExampleNameisconvertedtomy-example-name.methodName()Returnsthenameoftheembracingmethod(wherethetemplateisexpanded).methodParameters()Returnsthelistofparametersoftheembracingmethod(wherethetemplateisexpanded).methodReturnType()Returnsthetypeofthevaluereturnedbythecurrentmethod(themethodwithinwhichthetemplateisexpanded).qualifiedClassName()Returnsthefullyqualifiednameofthecurrentclass(theclasswherethetemplateisexpanded).CleartheShortenFQnamescheckbox.rightSideType()Declarestheleft-sidevariablewithatypeoftheright-sideexpression.Itisusedintheiterationsgrouptohaveasingletemplateforgeneratingbothraw-typeandGenericsCollections.snakeCase(sCamelCaseText)Returnssnake_casestringoutofCamelCasestringpassedasaparameter.spaceSeparated(String)ReturnsstringseparatedwithspacesoutofCamelCasestringpassedasaparameter.Forexample,ifthestringpassedasaparameterisfooBar,thenthefunctionreturnsfoobar.subtypes(sType)Returnsthesubtypesofthetypepassedasaparameter.suggestIndexName()Suggeststhenameofanindexvariable.Returnsiifthereisnosuchvariableinscope,otherwisereturnsjifthereisnosuchvariableinscope,etc.suggestVariableName()Suggeststhenameforavariablebasedonthevariabletypeanditsinitializerexpression,accordingtoyourcodestylesettingsthatrefertothevariablenamingrules.Forexample,ifitisavariablethatholdsanelementwithiniteration,IntelliJIDEAmakesaguessonthemostreasonablenames,alsotakingintoaccountthenameofthecontainerbeingiterated.suggestFirstVariableName(sFirstVariableName)Doesn’tsuggesttrue,false,this,super.time(sSystemTime)Returnsthecurrentsystemtime.typeOfVariable(VAR)Returnsthetypeofthevariablepassedasaparameter.underscoresToCamelCase(sCamelCaseText)ReturnsthestringpassedasaparameterwithCamelHumpletterssubstitutingforunderscores.Forexample,ifthestringpassedasaparameterisfoo_bar,thenthefunctionreturnsfooBar.underscoresToSpaces(sParameterWithSpaces)Returnsthestringpassedasaparameterwithspacessubstitutingforunderscores.user()Returnsthenameofthecurrentuser.variableOfType(type)Suggestsallvariablesthatmaybeassignedtothetypepassedasaparameter,forexamplevariableOfType(“java.util.Vector”).Ifyoupassanemptystring(“”)asaparameter,suggestsallvariablesregardlessoftheirtypes.JsArrayVariableReturnsJavaScriptarrayname.jsClassName()ReturnsthenameofthecurrentJavaScriptclass.jsComponentTypeReturnstheJavaScriptcomponenttype.jsMethodName()ReturnsthenameofthecurrentJavaScriptmethod.jsQualifiedClassNameReturnsthecompletenameofthecurrentJavaScriptclass.jsSuggestIndexNameReturnsasuggestednameforanindex.jsSuggestVariableNameReturnsasuggestednameforavariable.参考LiveTemplateVariables原文链接IDEA下利用LiveTemplate编写注释模版如何在IntelliJIDEA中的Gradle项目上使用MybatisGenerator自动生成代码5923

最新新闻

手机浏览

公理网 版权所有

公理网 Total 0.017461(s) query 6, 报料QQ:点击这里

给我发消息