Skip to content

Instantly share code, notes, and snippets.

@RobertAudi
Created July 25, 2010 12:32
Show Gist options
  • Save RobertAudi/489532 to your computer and use it in GitHub Desktop.
Save RobertAudi/489532 to your computer and use it in GitHub Desktop.
End of file comment Textmate command

I just created this little textmate command that acts like a snippet and inserts an End of file command a la CodeIgniter. Example:

/* End of file eof.tmsnippet.php */
/* Location: ./Sandbox/php/Textmate_Commands/eof.tmsnippet.php */

If you want to start using it right away, I uploaded the .tmCommand file in CloudApp. I also created a gist with the code of the command so that you can easily modify it if you want to. The links is at the bottom of the post.

Alternatively, you can create the command manually. Here are its attributes:

  • Save: Nothing
  • Command(s): (The code above)
  • Input: None
  • Output: Insert as Snippet
  • Activation: Tab Trigger (Default: eof)
  • Scope Selector: source.php

The command also recognizes three Textmate Shell Variables:

  • $TM_DIRECTORY_SEPERATOR : The default value is /
  • $TM_SERVER_ROOT : This should be the same value as $_SERVER['DOCUMENT_ROOT']. If this is not set, the command will display the full path to the file.
  • $TM_PROJECT_ROOT : This command is the same as $TM_SERVER_ROOT except that it should be set as project-specific, which you can only do if you have the ProjectPlus plugin installed. (hint: click on the little I icon in the bottom right corner of the project drawer)

The command creates one tab stop by directory and each tab stop adds one directory to the selection; I you don't know what I mean, try to use the command and you'll understand.

Enjoy!

#!/usr/bin/env php
<?php
// Directory seperator. Default is "/".
$directory_seperator = ( ! empty( $_ENV['TM_DIRECTORY_SEPERATOR'] ) ) ? $_ENV['TM_DIRECTORY_SEPERATOR'] : '/';
// Server root. Should be the same value as $_SERVER['DOCUMENT_ROOT'] or
// the project root (in which case the TM_ Shell Variable should be set as)
// project specific, not in the Preferences).
// $default_server_root = '/Users/aziz/Sites';
if ( ! empty( $_ENV['TM_PROJECT_ROOT'] ) )
{
$server_root = $_ENV['TM_PROJECT_ROOT'];
$server_root_set = true;
}
elseif ( ! empty( $_ENV['TM_SERVER_ROOT'] ) )
{
$server_root = $_ENV['TM_SERVER_ROOT'];
$server_root_set = true;
}
else
{
$server_root = '';
$server_root_set = false;
}
$filepath = $_ENV['TM_DIRECTORY'];
if ( $server_root_set )
$filepath = ltrim( str_replace( $server_root . $directory_seperator, '', $filepath ), '/');
$s = '';
$f = explode( $directory_seperator, $filepath );
for ( $i = 1, $j = 0, $max = count( $f ); $j < $max; $i++, $j++ )
{
if ( $server_root_set === false && $j == 0 )
continue;
$s = '${' . $i . ':' . $s . $f[$j] . '/}';
}
$snippet = "/* End of file \$TM_FILENAME */\n";
$snippet .= "/* Location: ";
if ( $server_root_set )
$snippet .= '.';
$snippet .= $directory_seperator . $s;
$snippet .= "\$TM_FILENAME */";
echo $snippet;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment