likes
comments
collection
share

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

作者站长头像
站长
· 阅读数 47

🔮 直接看效果

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

gif图很大 请耐心等待

该代码片段通过复杂的正则和transform规则将文件名或目录名/index转成大驼峰或小驼峰的命名,新建文件的时候铺开很爽

输出示例:

文件路径 -> 输出命名

Button/index.jsx -> Button

Button/PrimaryButton.jsx -> PrimaryButton

utils/math/index.js -> math

utils/sum.js -> sum

UserModel.js -> UserModel

user-model.js -> UserModel

user.model.js -> UserModel

📝 直接抄作业

  1. cmd+shift+p 输入 snippet 选择 Configure Snippets
  2. 选择 New Global Snippets File (或者选建在工作区)
  3. 命名 custom
  4. 将以下片段粘贴到 custom.code-snippets.json
{
  "variablename": {
    "prefix": "variablename",
    "body": [
      "${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/camelcase}${2:/camelcase}/}"
    ],
    "description": "variablename"
  },
  "classname": {
    "prefix": "classname",
    "body": [
      "${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/pascalcase}${2:/pascalcase}/}"
    ],
    "description": "classname"
  },
  "export const": {
    "prefix": "export const",
    "body": [
      "export const ${1:${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/camelcase}${2:/camelcase}/}} = $0;",
      ""
    ],
    "description": "export const"
  },
  "export function": {
    "prefix": "export function",
    "body": [
      "export function ${1:${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/camelcase}${2:/camelcase}/}}($2) {",
      "  $0",
      "};",
      ""
    ],
    "description": "export function"
  },
  "export const component": {
    "prefix": "export const component",
    "body": [
      "export const ${1:${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/pascalcase}${2:/pascalcase}/}} = ($2) => $0;",
      ""
    ],
    "description": "export const component"
  },
  "export function component": {
    "prefix": "export function component",
    "body": [
      "export function ${1:${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/pascalcase}${2:/pascalcase}/}}($2) {",
      "  $0",
      "};",
      ""
    ],
    "description": "export function component"
  },
  "export class": {
    "prefix": "export class",
    "body": [
      "export class ${1:${TM_FILEPATH/.+[\\/\\\\](.+)[\\/\\\\]index.+|.+[\\/\\\\](.+)\\..+$/${1:/pascalcase}${2:/pascalcase}/}} {",
      "  $0",
      "};",
      ""
    ],
    "description": "export class"
  }
}

使用

变量名

输入 variablename (不用输全 一般varna就够了)

以当前文件名目录名/index 转换成camelcase小驼峰作为变量名

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

class类名(大写开头)

输入 classname

以当前文件名目录名/index 转换成pascalcase大驼峰作为类名输出

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

导出变量

输入 export const

导出小驼峰命名的变量

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

导出函数

输入 export function

导出小驼峰命名的函数

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

导出组件

输入 export function component (efc即可)

导出大驼峰命名的组件

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

导出箭头函数组件

输入 export const component (ecc即可)

导出大驼峰命名的组件

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

导出class类

输入 export class

导出大驼峰命名的class类

🔮一个神奇的vscode代码片段由文件生成变量名、组件名,无视各种乱七八糟的文件命名,兼容目录名/index

snippet语法详解

研究+写文章花了一晚上。。累了。。需要详解的同学请点赞关注评论留言,后续更新。

转载自:https://juejin.cn/post/7411029614627422235
评论
请登录