我正在尝试创建一个VSCode 代码片段 https://code.visualstudio.com/api/language-extensions/snippet-guide for PHP https://php.net自动放置命名空间(基于文件夹路径)。
这个想法是对当前文档的目录进行替换,考虑到我的所有类都位于MVC
文件夹,并且它位于src
文件夹,示例:
- /开发/项目/src/MVC/Models/Access/Auth,或
- /home/用户/dev/项目/src/MVC/Models/Access/Auth,或
- /任何其他文件夹/src/MVC/模型/访问/身份验证
我通过 VSCODE 片段变量获得了完整的文件夹名称$TM_DIRECTORY
.
显然 REGEX 上没有错误,如您所见:https://regex101.com/r/P8CpkX/1 https://regex101.com/r/P8CpkX/1
我对片段代码的尝试:
"namep": {
"prefix": [
"namep"
],
"body": [
"",
"// TM_DIRECTORY: $TM_DIRECTORY",
"namespace ${TM_DIRECTORY/.*src\/(([^\/]*)(\/)?)|(\/)([^\/]*)/$2\\$5/g};"
],
"scope": "php",
"description": "Try to put namespace automatically"
}
其结果是:
// TM_DIRECTORY: /home/user/dev/project/src/MVC/Models/Access/Auth
namespace ${TM_DIRECTORY/.*src/(([^/]*)(/)?)|(/)([^/]*)/$5/g};
但预期的是(如上所示REGEX https://regex101.com/r/P8CpkX/1):
// TM_DIRECTORY: /home/user/dev/project/src/MVC/Models/Access/Auth
namespace \MVC\Models\Access\Auth;
有人可以帮忙修复它吗?
多谢!!!