I am trying to make a Sublime Text fragment that inserts a PHP template class in lines:
<?php namespace Namespace\Subnamespace; class TestClass { public function __construct() {
When using PHP-FIG standards (or similar), both the namespace and the class name can be obtained from the file path. The file in the above example will be placed in /Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
.
This is what I have so far:
<snippet> <content></$1/g} { public function __construct() { ${0://code...} } } ]]></content> <tabTrigger>phpclass</tabTrigger> <scope>text.html</scope> </snippet>
I already figured out how to get the class name, but getting the namespace turned out to be a lot harder. I am far from a regular expression expert, and this requires:
- Getting everything after
src/
- ... and to the last
/
- Flip all other slashes to backslashes.
/Users/Projects/Whatever/src/Namespace/Subnamespace/TestClass.php
becomes Namespace\Subnamespace
.
This is the most relevant thread that I found on this issue, but it is a way above my head, and I could not even get it to work.
Can anyone help me with this?
source share