起因
其实接触模板已经很久了。但我一直没怎么重视。但最近在乱学一通后发现。比起传统的笔记来说。用模板当学习笔记真是有很大优势。他不要求你完全记住。复习之前学过的知识也很方便。适合快速学习开发。 下面我就举个例子
工欲善其事,必先利其器。
我们先写模板的模板。 怎么创建模板我就略过了,就是选设置,再选用户代码片段。具体参考vscode自定义模板 因为vscode配置都是json文件,就创建json文件的模板
json.json
"tm-selected-text": {
"prefix": "tm-selected-text",
"body": "\\$TM_SELECTED_TEXT",
"description": "当前选中的文本或空字符串"
},
"tm-current-line": {
"prefix": "tm-current-line",
"body": "\\$TM_CURRENT_LINE",
"description": "当前行的内容"
},
"tm-current-word": {
"prefix": "tm-current-word",
"body": "\\$TM_CURRENT_WORD",
"description": "光标下单词的内容或空字符串"
},
"tm-line-index": {
"prefix": "tm-line-index",
"body": "\\$TM_LINE_INDEX",
"description": "从0开始的行号"
},
"tm-line-number": {
"prefix": "tm-line-number",
"body": "\\$TM_LINE_NUMBER",
"description": "从1开始的行号"
},
"tm-filename-base": {
"prefix": "tm-filename-base",
"body": "\\$TM_FILENAME_BASE",
"description": "当前文档的文件名,不带扩展名"
},
"tm-filename": {
"prefix": "tm-filename",
"body": "\\$TM_FILENAME",
"description": "当前文档的文件名"
},
"tm-directory": {
"prefix": "tm-directory",
"body": "\\$TM-DIRECTORY",
"description": "当前文档的目录"
},
"tm-filepath": {
"prefix": "tm-filepath",
"body": "\\$TM_FILEPATH",
"description": "当前文档的完整文件路径"
},
"relative-filepath": {
"prefix": "relative-filepath",
"body": "\\$RELATIVE-FILEPATH",
"description": "当前文档的相对(相对于打开的工作区或文件夹)文件路径"
},
"clipboard": {
"prefix": "clipboard",
"body": "\\$CLIPBOARD",
"description": "剪贴板的内容"
},
"workspace-name": {
"prefix": "workspace-name",
"body": "\\$WORKSPACE_NAME",
"description": "打开的工作区或文件夹的名称"
},
"workspace-folder": {
"prefix": "workspace-folder",
"body": "\\$WORKSPACE_FOLDER",
"description": "打开的工作区或文件夹的路径"
},
"current-year": {
"prefix": "current-year",
"body": "\\$CURRENT_YEAR",
"description": "今年"
},
"current-year-short": {
"prefix": "current-year-short",
"body": "\\$CURRENT_YEAR_SHORT",
"description": "当前年份的最后两位数字"
},
"current-month": {
"prefix": "current-month",
"body": "\\$CURRENT_MONTH",
"description": "月份为两位数(例如“02”)"
},
"current-month-name": {
"prefix": "current-month-name",
"body": "\\$CURRENT_MONTH_NAME",
"description": "月份的全名(例如“July”)"
},
"current-month-name-short": {
"prefix": "current-month-name-short",
"body": "\\$CURRENT_MONTH_NAME_SHORT",
"description": "月份的简称(例如“Jul”)"
},
"current-date": {
"prefix": "current-date",
"body": "\\$CURRENT_DATE",
"description": "一个月中的哪一天"
},
"current-day-name": {
"prefix": "current-day-name",
"body": "\\$CURRENT_DAY_NAME",
"description": "日期名称(例如“Monday”)"
},
"current-day-name-short": {
"prefix": "current-day-name-short",
"body": "\\$CURRENT_DAY_NAME_SHORT",
"description": "一天的简称(例如“Mon”)"
},
"current-hour24": {
"prefix": "current-hour24",
"body": "\\$CURRENT_HOUR24",
"description": "小时制的当前小时"
},
"current-minute": {
"prefix": "current-minute",
"body": "\\$CURRENT_MINUTE",
"description": "当前分钟"
},
"current-second": {
"prefix": "current-second",
"body": "\\$CURRENT_SECOND",
"description": "当前秒"
},
"current-seconds-unix": {
"prefix": "current-seconds-unix",
"body": "\\$CURRENT_SECONDS_UNIX",
"description": "自Unix纪元以来的秒数"
},
"block-comment-start": {
"prefix": "block-comment-start",
"body": "\\$BLOCK_COMMENT_START",
"description": "块注释的开始,例如java中的/*"
},
"block-comment-end": {
"prefix": "block-comment-end",
"body": "\\$BLOCK_COMMENT_END",
"description": "块注释的结束,例如java中的*/"
},
"line-comment": {
"prefix": "line-comment",
"body": "\\$LINE_COMMENT",
"description": "行注释,例如java中的//"
},
"template": {
"prefix": "template",
"body": [
"\"$1\": {",
"\t\"prefix\": \"$1\",",
"\t\"body\": [$2]",
"},"
]
}
现在假设你在学thymeleaf模板框架。可以边学边把知识点写成模板。这样你学完以后你也拥有了自己的智能提示。当然除了模板智能提示,你也可以用Maven的自定义模板。有兴趣可以看官网。和之前一样建议先写模板的模板。 下面就是我主要参考下面文章写的模板,这里分享出来。
html.json
"strings.": {
"prefix": "strings.",
"body": "#strings.${1|toString,isEmpty,arrayIsEmpty,listIsEmpty,setIsEmpty,defaultString,arrayDefaultString,listDefaultString,setDefaultString,contains,containsIgnoreCase,startsWith,endsWith,indexOf,substring,substringAfter,substringBefore,replace,prepend,append,toUpperCase,toLowerCase,arrayJoin,listJoin,setJoin,arraySplit,listSplit,setSplit,trim,length,abbreviate,capitalize,unCapitalize,capitalizeWords,capitalizeWords,escapeXml,escapeJava,escapeJavaScript,unescapeJava,unescapeJavaScript,equals,equalsIgnoreCase,concat,concatReplaceNulls|}(${2:$TM_SELECTED_TEXT})"
},
"objects.": {
"prefix": "objects.",
"body": "#objects.${1|nullSafe,arrayNullSafe,listNullSafe,setNullSafe|}(${2:$TM_SELECTED_TEXT})"
},
"numbers.": {
"prefix": "numbers.",
"body": "#numbers.${1|listFormatInteger,arrayFormatInteger,setFormatInteger,formatDecimal,arrayFormatDecimal,listFormatDecimal,setFormatDecimal,formatDecimal,sequence,formatInteger|}(${2:$TM_SELECTED_TEXT})"
},
"calendars.": {
"prefix": "calendars.",
"body": "#calendars.${1|format,arrayFormat,listFormat,setFormat,formatISO,arrayFormatISO,listFormatISO,setFormatISO,format,arrayFormat,listFormat,setFormat,day,month,monthName,monthNameShort,year,dayOfWeek,dayOfWeekName,dayOfWeekNameShort,hour,minute,second,millisecond,create,createForTimeZone,createNow,createNowForTimeZone,createToday,createTodayForTimeZone|}(${2:$TM_SELECTED_TEXT})"
},
"dates.": {
"prefix": "dates.",
"body": "#dates.${1|format,arrayFormat,setFormat,formatISO,arrayFormatISO,listFormatISO,setFormatISO,listFormat,setFormat,day,month,monthName,monthNameShort,year,dayOfWeek,dayOfWeekName,dayOfWeekNameShort,hour,minute,second,millisecond,create,createNow,createNowForTimeZone,createToday,createTodayForTimeZone|}(${2:$TM_SELECTED_TEXT})"
},
"th:": {
"prefix": "th:",
"body": "th:${1|id,style,class,include,replace,each,if,unless,switch,case,object,with,attr,attrprepend,attrappend,value,href,src,text,utext,fragment,remove|}='${2:$TM_SELECTED_TEXT}'"
},
"xmlns-th": {
"prefix": "xmlns-th",
"body": "xmlns:th=http://www.thymeleaf.org ",
"description": "thymeleaf"
},
"xmlns-sec": {
"prefix": "xmlns-sec",
"body": "xmlns:sec=http://www.thymeleaf.org/extras/spring-security ",
"description": "sec安全验证标签"
},
"xmlns-shiro": {
"prefix": "xmlns-shiro",
"body": "xmlns:shiro=http://www.pollix.at/thymeleaf/shiro ",
"description": "访问控制标签"
},
"🔗链接": {
"prefix": "link",
"body": [
"@{$TM_SELECTED_TEXT}"
],
"description": "链接🔗"
},
"链接🔗": {
"prefix": "url",
"body": "@{$TM_SELECTED_TEXT}",
"description": "链接🔗"
},
"#": {
"prefix": "#",
"body": [
"#{$TM_SELECTED_TEXT}"
],
"description": "国际化信息"
},
"*": {
"prefix": "*",
"body": [
"*{$TM_SELECTED_TEXT}"
],
"description": "选择"
},
"$": {
"prefix": "$",
"body": [
"${$TM_SELECTED_TEXT}"
],
"description": "变量"
},
|