views:

31

answers:

2

What I would like is a snippet that when executed, grabs the TM_FILEPATH output Explodes it on the slash / Then split out each part as a placeholder containing that part and an underscore (apart from the last part (the filename)) For Example: for a file in directory path /Path/To/Original/file we would get

class ${1:Path_}${2:To_}${3:Original_}${4:File} {
    // code here
}

Then I can step through and remove the parts I don't want ending with a className that fits the standard PHP autoloader

Does this sound possible?

Cheers, Chris

A: 

I've managed to get part of the way there by having textmate execute PHP code as a textmate command, however this is then poured onto my file rather than become a snippet which I can actually step through. Here is what I have as a command.

#!/usr/bin/php
<?php
$path = $_ENV['TM_FILEPATH'];
$parts = explode('/', $path);
$lastPart = end($parts);
foreach ($parts as $id => $part) {
    if ($lastPart == $part) {
        echo '${'.$id.':'.$part.'}';
    } else {
        print '${'.$id.':'.$part.'_}';
    }
}
?>

Which results in:

${0:_}${1:Path_}${2:To_}${3:To_}${4:My_}${5:File.php}

Being presented in my file.

So... I want that to then become a snippet I guess. Is there any way to have a snippet inherit from a command being executed? Any ideas?

Cheers, Chris

catchamonkey
Yes!In the Creation of a command there is an output option which you can set to 'output as snippet'This does what I need so will jsut tidy it up, removing the extension etc and done!I'll post when complete.
catchamonkey
A: 

Have to add this final result as an answer to enable code display.
Just make sure you set 'output as snippet'

#!/usr/bin/php
<?php
$path = $_ENV['TM_FILEPATH'];
$path = trim($path, '/');
$path = trim($path, '.php');
$parts = explode('/', $path);
$lastPart = end($parts);
echo 'class ';
foreach ($parts as $id => $part) {
    // textmate placeholders start at 1
    $id = $id+1;
    if ($lastPart == $part) {
        echo '${'.$id.':'.$part.'}';
    } else {
        echo '${'.$id.':'.$part.'_}';
    }
}
?>
catchamonkey