Textmate snippet to add file name and path in comment?
I'm using an existing snippet in Textmate to reduce the repetition of creating controllers and models. The snippet works great, but I'd love to add a comment to the end of each file. For example:
/* End of file filename.php */
/* Location: ./system/application/controllers/filename.php */
The first line is easy:
/* End of file ${TM_FILENAME} */
The second part is almost as easy with the TM_FILEPATH开发者_运维知识库 variable:
/* Location: ./${TM_FILEPATH} */
The problem is, I don't want the entire file path returned, just anything AFTER 'system' if it exists, or 'application' if not. For instance, using TM_FILEPATH returns this:
/* Location: ./path/from/root/system/application/controllers/filename.php */
-or-
/* Location: ./path/from/root/application/controllers/filename.php */
...when I want:
/* Location: ./system/application/controllers/filename.php */
-or-
/* Location: ./application/controllers/filename.php */
I assume it is going to be some regex trickery, but I have no idea how. Any suggestions please?
UPDATE: I just found the TextMate variable TM_PROJECT_DIRECTORY which contains the info that I want REMOVED from TM_FILEPATH if that makes things any easier.
So, whereas TM_FILEPATH produces this:
/path/from/root/system/application/controllers/filename.php
TM_PROJECT_DIRECTORY produces this:
/path/from/root
I don't know about Textmate, but could you use the CI constant FCPATH? This is the full server path to the file.
I use two different snippets for controllers and models (since the syntax is similar, but just a bit different; I.E.: every controller needs an index function, but models don't). I just hard code them as follows...
Controller:
/* Location: ./application/controllers/${TM_FILENAME} */
Model:
/* Location: ./application/models/${TM_FILENAME} */
Since I always pull the application folder out of the system folder, and put the system folder in a different directory, this works out great. I also added a tabstop on application, just in case I renamed the application directory.
@TM_PROJECT_DIRECTORY: I think this only works if you open your files as a project. I use e text editor, which is based on Textmate, so it might be a little bit different.
Hope this helps.
Here is the solution I came up with. I have no idea if it is the best way, but it seams to work:
/* End of file ${TM_FILENAME} */
/* Location: ${TM_FILEPATH/(.*?)(\/system)?(\/application.*)/(?1:).$2$3/} */
I'll break it down as much as I understand it ;)
${TM_FILEPATH - The 'source' string TextMate variable
/ - Indicates next chars are 'pattern'
(.*?) - Group 1: Zero or more of any character. ? Makes it non-greedy
(\/system)? - Group 2: /system but it's optional because of the ?
(\/application.*) - Group 3: /application and any other characters
/ - Indicates next chars are 'replacement'
(?1:).$2$3 - If Group 1 is found, replace with blank, then a dot, Group 2, Group 3.
/ - Indicates regex is finished.
} - Closes off TextMate variable.
精彩评论