VS Code 自定义代码片段-midway

# 分词改帕斯卡命名
# 例如:nav-left=》NavLeft
${TM_FILENAME_BASE/(.*)/${1:/pascalcase}/g}
# 截取.之前 小驼峰命名
# 例如:nav-left.service=》navLeft
${TM_FILENAME_BASE/^([^.]+).*/${1:/downcase}/}

重点解决文件名问题

{
  // Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
  // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
  // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
  // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
  // Placeholders with the same ids are connected.
  // Example:
  "Print to console": {
    "scope": "javascript,typescript",
    "prefix": "midway service",
    "body": [
      "import { inject, provide } from 'midway';",
      "import { I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Model } from '../models/${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}.model';",
      "import { BaseService } from '../../base/base.service';",
      "",
      "export interface I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service extends ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service {}",
      "",
      "@provide()",
      "export class ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service extends BaseService {",
      "  @inject()",
      "  private ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Model: I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Model;",
      "}",
    ],
    "description": "midway service"
  }
}
{
  // Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
  // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
  // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
  // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
  // Placeholders with the same ids are connected.
  // Example:
  // "Print to console": {
  //    "scope": "javascript,typescript",
  //    "prefix": "log",
  //    "body": [
  //        "console.log('$1');",
  //        "$2"
  //    ],
  //    "description": "Log output to console"
  // }
  "Print to console": {
    "scope": "javascript,typescript",
    "prefix": "midway controller",
    "body": [
      "import { provide, Context, inject } from 'midway';",
      "import { I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service } from '../../lib/services/${TM_FILENAME_BASE/([-0-9a-z])/${1:/downcase}/}';",
      "import { BaseController } from '../../base/base.controller';",
      "import { SwaggerJoiController as sjc } from 'midway-joi-swagger2';",
      "",
      "@provide()",
      "@sjc({ path: '/${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}', api: '${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}' })",
      "export class ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Controller extends BaseController {",
      "  @inject()",
      "  private ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service: I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service;",
      "",
      // "  @get('/')",
      // "  async index(ctx: Context) {",
      // "    ctx.body = await this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service.find();",
      // "  }",
      "}",
      "",
    ],
    "description": "Log output to console"
  }
}
${TM_FILENAME/(.*)\\..+$/$1/}
  |           |         |  |
  |           |         |  |-> no options
  |           |         |
  |           |         |-> references the contents of the first
  |           |             capture group
  |           |
  |           |-> regex to capture everything before
  |               the final `.suffix`
  |
  |-> resolves to the filename

变量变换
转换允许您在插入变量之前修改变量的值。转型的定义包括三个部分:

与变量值匹配的正则表达式,或无法解析变量时的空字符串。
“格式字符串”,允许从正则表达式引用匹配组。格式字符串允许条件插入和简单修改。
传递给正则表达式的选项。
占位符,变换
与变量转换一样,占位符的转换允许在移动到下一个制表位时更改占位符的插入文本。插入的文本与正则表达式匹配,匹配或匹配 - 取决于选项 - 将替换为指定的替换格式文本。每次出现占位符都可以使用第一个占位符的值独立定义自己的转换。Placeholder-Transforms的格式与Variable-Transforms的格式相同。

转换例子
这些示例显示在双引号内,因为它们会出现在代码段内,以说明需要双重转义某些字符。样本转换以及文件名的结果输出example-123.456-TEST.js。

例 说明

"${TM_FILENAME/[\\.]/_/}"   example-123_456-TEST.js 替换第一个.用_
"${TM_FILENAME/[\\.-]/_/g}" example_123_456_TEST_js 替换每个.或-与_
"${TM_FILENAME/(.*)/${1:/upcase}/}" EXAMPLE-123.456-TEST.JS 改为全部大写
"${TM_FILENAME/[^0-9^a-z]//gi}" example123456TESTjs 删除非字母数字字符

变量

TM_SELECTED_TEXT:当前选定的文本或空字符串
TM_CURRENT_LINE:当前行的内容
TM_CURRENT_WORD:光标下的单词的内容或空字符串
TM_LINE_INDEX:基于零索引的行号
TM_LINE_NUMBER:基于一索引的行号
TM_FILENAME:当前文档的文件名
TM_FILENAME_BASE 当前文档的文件名(不含后缀名)
TM_DIRECTORY:当前文档的目录
TM_FILEPATH:当前文档的完整文件路径
CURRENT_DAY_NAME :当天的名称(’星期一’)。
CURRENT_DAY_NAME_SHORT :当天的短名称(’Mon’)。
CURRENT_MONTH_NAME :本月的全名(’七月’)。
CURRENT_MONTH_NAME_SHORT} :月份的简称(’Jul’)。
CURRENT_YEAR:当前年(四位数)
CURRENT_YEAR_SHORT:当前年(两位数)
CURRENT_MONTH:当前月
CURRENT_DATE:当前日
CURRENT_HOUR:当前小时
CURRENT_MINUTE:当前分钟
CURRENT_SECOND:当前秒
CLIPBOARD:当前剪切板中的内容
${sn:/upcase} 或 ${sn:/downcase} 或 ${sn:/capitalize}:表示将匹配项变更为「所有字母均大写/所有字母均小写/首字母大写其余小写」后,插入

base

{
  // Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
  // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
  // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
  // used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
  // Placeholders with the same ids are connected.
  // Example:
  // "Print to console": {
  // "scope": "javascript,typescript",
  // "prefix": "log",
  // "body": [
  // "console.log('$1');",
  // "$2"
  // ],
  // "description": "Log output to console"
  // }
  "Print to console": {
    "scope": "javascript,typescript",
    "prefix": "midway controller",
    "body": [
      "import { provide, Context, inject } from 'midway';",
      "import { I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service } from '../../lib/services/${TM_FILENAME_BASE/([-0-9a-z])/${1:/ downcase}/}';",
      "import { BaseController } from '../../base/base.controller';",
      "import { SwaggerJoiController as sjc } from 'midway-joi-swagger2';",
      "",
      "@provide()",
      "@sjc({ path: '/${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}', api: '${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}' })",
      "export class ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Controller extends BaseController {",
      " @inject()",
      " private ${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}Service: I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service;",
      "",
      // " @get('/')",
      // " async index(ctx: Context) {",
      // " ctx.body = await this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service.find();",
      //" }",
      "}",
      "",
    ],
    "description": "Log output to console"
  },
  "Midway controller functions get": {
    "scope": "typescript",
    "prefix": "midway get",
    "body": [
      "@SwaggerJoiGet({",
      " path: '/$4',",
      " api: '${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}',",
      " description: '$0',",
      " summary: '$1$2',",
      " query: schemas.S${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}/g}$2In,",
      " responses: schemas.S${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}/g}$2Out",
      "})",
      "async $1$2(ctx: Context) {",
      " ctx.body = await this.${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}Service.$1$2(ctx.query);",
      "}"
    ],
    "description": "midway controller function get"
  },
  "Midway controller functions post": {
    "scope": "typescript",
    "prefix": "midway post",
    "body": [
      "@SwaggerJoiPost({",
      " path: '/$4',",
      " api: '${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}',",
      " description: '$0',",
      " summary: '$1$2',",
      " body: schemas.S${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}/g}$2In,",
      " responses: schemas.S${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}/g}$2Out",
      "})",
      "async $1$2(ctx: Context) {",
      " ctx.body = await this.${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}Service.$1$2(ctx.request.body);",
      "}"
    ],
    "description": "midway controller function get"
  },
  "Midway services functions": {
    "scope": "typescript",
    "prefix": "midway services action",
    "body": [
      "async $1$2(param: I${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}/g}$2In):Promise<I${1/([a-z]*)(([A-Z])+([a-z]+))?/${1:/upcase}/g}$2Out> {",
      " return undefined;",
      "}"
    ],
    "description": "midway controller function get"
  },
  "Mongoose schema": {
    "scope": "javascript,typescript",
    "prefix": "mongoose schema",
    "body": [
      "import { Document, Schema, model} from 'mongoose';",
      "import { providerWrapper} from 'midway';",
      "",
      "export const ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Schema: Schema = new Schema(",
      " {",
      " $1",
      "},",
      " {",
      " /**",
      " * 静态模型",
      " */",
      " strict: false,",
      " /**",
      " * __v 版本",
      " */",
      " versionKey: false",
      " timestamps: { createdAt: 'created', updatedAt: 'updated'}",
      " }",
      ");",
      "",
      "export interface I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc extends Document {",
      " [k: string",
      " ]: any;",
      "}",
      "",
      "export const factory = () => model<I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc>('${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}', ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Schema);",
      "providerWrapper([",
      " {",
      " id: '${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc',",
      " provider: factory",
      " }",
      "]);",
    ],
    "description": "Log output to console"
  },
  "midway service": {
    "scope": "javascript,typescript",
    "prefix": "midway service",
    "body": [
      "import { inject, provide } from 'midway';",
      "import { I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Model } from '../models/${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}.model';",
      "import { BaseService } from '../../base/base.service';",
      "",
      "export interface I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service extends ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service {}",
      "",
      "@provide()",
      "export class ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service extends BaseService {",
      " @inject()",
      " private ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Model: I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Model;",
      "}",
    ],
    "description": "midway service"
  },
  "midway service mongoose": {
    "scope": "javascript,typescript",
    "prefix": "midway service mongoose",
    "body": [
      "import { inject, provide } from 'midway';",
      "import { I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc } from '../mongo/${TM_FILENAME_BASE/([0-9a-z])/${1:/downcase}/}';",
      "import { BaseService } from '../../base/base.service.mongo';",
      "import { Model } from 'mongoose';",
      "",
      "export interface I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service extends ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service {}",
      "",
      "@provide()",
      "export class ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Service extends BaseService {",
      " @inject()",
      " private ${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc: Model<I${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc>;",
      " ",
      " async findAll(query: any): Promise<any> {",
      " return this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc.find(query).exec();",
      " }",
      " ",
      " async findOne(param: any): Promise<any> {",
      " return this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc.findOne({ _id: param._id }).exec();",
      " }",
      " ",
      " async save(param: any): Promise<any> {",
      " return this.upset(this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc, param).then(result => {",
      " return { _id: this._.get(result, '_id', param._id)",
      " };",
      " });",
      " }",
      "}",
    ],
    "description": "midway service mongoose"
  }
}

wzcvbase

{
  // Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
  // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
  // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
  // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
  // Placeholders with the same ids are connected.
  // Example:
  "vue base": {
    "scope": "vue",
    "prefix": "vbase-wzc",
    "body": [
      "<template>",
      "  <div></div>",
      "</template>",
      "",
      "<script>",
      "/**",
      " * $1",
      " */",
      "import viewBase from '../../base/view-base';",
      "export default {",
      "  extends: viewBase,",
      "  data: () => ({}),",
      "  created() {",
      "    this.pageInit();",
      "  },",
      "  methods: {",
      "    pageInit() {}",
      "  }",
      "};",
      "</script>",
      "",
      "<style lang=\"scss\" scoped>",
      "</style>",
      "",
    ],
    "description": "vue base"
  },
  "vuetify v-col text": {
    "scope": "vue-html",
    "prefix": "vtext",
    "body": [
      "<v-col cols=\"12\" md=\"6\" lg=\"4\">",
      "  <v-text-field",
      "    v-model=\"formData.$1\"",
      "    :rules=\"$$rules({dis:'$1'})\" ",
      "    :label=\"$$t('$1')\"",
      "    required",
      "    clearable",
      "  ></v-text-field>",
      "</v-col>",
    ],
    "description": "vuetify vol text field"
  },
  "vuetify v-col date": {
    "scope": "vue-html",
    "prefix": "vdate",
    "body": [
      "<v-col cols=\"12\" md=\"6\" lg=\"4\">",
      "  <w-date-picker",
      "    v-model=\"formData.$1\"",
      "    :label=\"$$t('$1')\"",
      "    :rules=\"$$rules({ dis: '$1' })\"",
      "    prepend-icon=\"fa-calendar-alt\"",
      "  ></w-date-picker>",
      "</v-col>",
    ],
    "description": "vuetify vol date picker"
  },
  "vuetify v-col select": {
    "scope": "vue-html",
    "prefix": "vselect",
    "body": [
      "<v-col cols=\"12\" sm=\"6\" md=\"4\">",
      "  <i18n-select",
      "    v-model=\"formData.$1\"",
      "    :label=\"$$t('currency')\"",
      "    :rules=\"$$rules({ dis: 'currency' })\"",
      "    :item-text=\"`dataDisplay.$${$$i18n.locale}`\"",
      "    api-get-url=\"/dictionary-item?typeKey=$2&propertyKey=$3\"",
      "  ></i18n-select>",
      "</v-col>",
    ],
    "description": "vuetify vol date picker"
  },
  "vuetify v-container form": {
    "scope": "vue-html",
    "prefix": "vCardForm",
    "body": [
      "<v-card class=\"v-body\">",
      "  <v-card-title>{{$$t('group-person')}}</v-card-title>",
      "  <v-container class=\"grey lighten-5\" fluid>",
      "    <v-form ref=\"form\" v-model=\"valid\" lazy-validation>",
      "      <v-row>",
      "",
      "      </v-row>",
      "    </v-form>",
      "  </v-container>",
      "  <v-card-actions>",
      "    <div class=\"flex-grow-1\"></div>",
      "    <v-btn @click=\"$$router.go(-1)\">{{$$t('back')}}</v-btn>",
      "    <v-btn color=\"primary\" @click=\"submitClick\">{{$$t('submit')}}</v-btn>",
      "  </v-card-actions>",
      "</v-card>",
    ],
    "description": "vuetify v-container v-form"
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,072评论 0 3
  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,146评论 0 9
  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,362评论 0 5
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 3,204评论 0 4
  • 一、字符串在C#中,字符串是一系列不可修改的Unicode字符,创建字符串后,就不能修改它。要创建字符串,最常用的...
    CarlDonitz阅读 1,264评论 0 2