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 {",
      " 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('${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;",
      " ",
      " async findAll(query: any): Promise {",
      " return this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc.find(query).exec();",
      " }",
      " ",
      " async findOne(param: any): Promise {",
      " return this.${TM_FILENAME_BASE/([0-9a-z])/${1:/capitalize}/}Doc.findOne({ _id: param._id }).exec();",
      " }",
      " ",
      " async save(param: any): Promise {",
      " 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": [
      "",
      "",
      "",
      "",
      "",
      "",
    ],
    "description": "vue base"
  },
  "vuetify v-col text": {
    "scope": "vue-html",
    "prefix": "vtext",
    "body": [
      "",
      "  ",
      "",
    ],
    "description": "vuetify vol text field"
  },
  "vuetify v-col date": {
    "scope": "vue-html",
    "prefix": "vdate",
    "body": [
      "",
      "  ",
      "",
    ],
    "description": "vuetify vol date picker"
  },
  "vuetify v-col select": {
    "scope": "vue-html",
    "prefix": "vselect",
    "body": [
      "",
      "  ",
      "",
    ],
    "description": "vuetify vol date picker"
  },
  "vuetify v-container form": {
    "scope": "vue-html",
    "prefix": "vCardForm",
    "body": [
      "",
      "  {{$$t('group-person')}}",
      "  ",
      "    ",
      "      ",
      "",
      "      ",
      "    ",
      "  ",
      "  ",
      "    
", " {{$$t('back')}}", " {{$$t('submit')}}", "
", "
", ], "description": "vuetify v-container v-form" } }

你可能感兴趣的:(VS Code 自定义代码片段-midway)