问题描述
我正在尝试制作一个Sublime文本片段,该片段在以下行中插入一个PHP样板类:
I'm trying to make a Sublime Text-snippet that inserts a PHP boilerplate class, in the lines of:
<?php
namespace Namespace\Subnamespace;
class TestClass
{
public function __construct()
{
//code...
}
}
使用 PHP-FIG 标准(或类似标准)时,名称空间和类名都可以是从文件的路径获得.上面示例中的文件将放置在/Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
中.
When using PHP-FIG standards(or similar), both the namespace and classname can be obtained from the path of the file. The file in the example above would be placed in /Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
.
这是我到目前为止所拥有的:
This is what I have so far:
<snippet>
<content><![CDATA[
<?php
namespace ${1:Namespace};
class ${TM_FILENAME/(.*)[.](.*)/$1/g}
{
public function __construct()
{
${0://code...}
}
}
]]></content>
<tabTrigger>phpclass</tabTrigger>
<scope>text.html</scope>
</snippet>
我已经想出了如何获取类名-但是事实证明,获取名称空间要困难得多.我离正则表达式专家还很远-这需要:
I've already figured out how to get the classname - but getting the namespace has proven to be much more difficult. I'm far from an expert in regexes - and this one requires:
- 在
src/
之后获取所有内容 - ...并且在最后一个
/
之前 - 将所有剩余的斜杠翻转为反斜杠.
- Getting everything after
src/
- ...and before the last
/
- Flipping all the remaining slashes to backslashes.
/Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
变为Namespace\Subnamespace
.
这是我在该主题上发现的最相关的主题,但这是远远超过了我的头,甚至无法正常工作.
This is the most relevant thread I've found on the subject, but it is way over my head, and I couldn't even get it working.
有人可以帮我吗?
推荐答案
此处是名称空间替换,可在ST-3中的两个以上级别上使用:
Here is a namespace substitution that works at more than 2 levels in ST-3:
namespace ${1:${TM_FILEPATH/(?:.*src\/)|(\/)?([^\/]+)(?=\/)|(?:\/[^\/]+\.php$)/(?1:\\$^N:$^N)/g}};
文件:/path/to/project/src/sub1/sub2/sub3/sub4/class.php
输出:namespace sub1\sub2\sub3\sub4;
这篇关于Sublime Text片段,用于插入PSR-0名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!