🔮一个神奇的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
📝 直接抄作业
cmd
+shift
+p
输入snippet
选择Configure Snippets
- 选择
New Global Snippets File
(或者选建在工作区) - 命名
custom
- 将以下片段粘贴到
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小驼峰作为变量名
class类名(大写开头)
输入 classname
以当前文件名
或目录名
/index 转换成pascalcase大驼峰作为类名输出
导出变量
输入 export const
导出小驼峰命名的变量
导出函数
输入 export function
导出小驼峰命名的函数
导出组件
输入 export function component
(efc
即可)
导出大驼峰命名的组件
导出箭头函数组件
输入 export const component
(ecc
即可)
导出大驼峰命名的组件
导出class类
输入 export class
导出大驼峰命名的class类
snippet语法详解
研究+写文章花了一晚上。。累了。。需要详解的同学请点赞关注评论留言,后续更新。
转载自:https://juejin.cn/post/7411029614627422235