TextMate and phpDoc Comment Blocks

I’ve been checking out TextMate for the last few days, and have been enjoying it. I’ve been a BBEdit user for a decade, and despite my switch to Zend Studio last fall for the bulk of my PHP development, I still find myself going to plain text editors for little tweaks, or less formal “hack it out” efforts. Up until this past weekend, BBEdit has been my go-to editor for these smaller projects.

The verdict is still out on TextMate, but I’m really liking certain aspects of it.

To you loyal BBEdit users who are no doubt thinking “Traitor!” — as I myself thought several times as I’ve followed the TextMate hype over the last six months or so — I did spend a good chunk of the wee hours on Sunday going through BBEdit features such as Glossaries and File Groups in an effort to mimic some TextMate capabilities. After all, BBEdit is a very powerful editor — surely all this TextMate hocus-pocus can be achieved with BBEdit as well?

I gave up on that after a couple of hours. I don’t know that I’m giving up on BBEdit at this point, but TextMate has earned “Keep In Dock” status. I’m sure I’ll continue upgrading BBEdit as new versions come out, at least for awhile. However, TextMate’s understanding of what I want as a developer is just too keen to ignore.

There are a few things that I’ve grown very accustomed to as a Zend Studio user over the past several months. One of those is typing

/**

in a PHP document and having a full phpDocumentor docblock appear magically, with the cursor insertion point set on the first line of the comment area.

That’s cool. A similar effect can be achieved in TextMate by typing

doc_cp

… followed by a TAB stroke.

However, where Zend Studio trumps TextMate related to phpDoc docblocks is in the handling of multi-line comments. If you’re already in a comment block and you hit enter for a new line of the comment, Zend Studio will automatically add proper docblock formatting to the new line. For example:

/**
 * Here's a comment
 **/

In Zend Studio, a return/enter keystroke after the “t” in “comment” will result in:

/**
 * Here's a comment
 *
 **/

The cursor is inserted directly under the “H”, with one space after the asterisk on the new line. This is a great feature, as it allows you to worry about what you’re writing in the comment, rather than worrying about the formatting of the comment block.

By default, TextMate doesn’t follow suit. Yes, you can insert some auto-formatted comment blocks. As I mentioned earlier,

doc_cp

followed by a TAB results in this block:

/**
 * undocumented class
 *
 * @package default
 * @author Clay Loveless
 **/

As a bonus, the words “undocumented class” are selected in the resulting block, so that I can begin typing immediately — those words are removed and replaced by what I’m typing. Again, that’s cool. However, a new line kills the joy of this feature. If my comment is “Killersoft_Foo — A class for all that and a bag of chips” followed by a new line, I’ll wind up with this:

/**
 * Killersoft_Foo -- A class for all that and a bag of chips
 
 * @package default
 * @author Clay Loveless
 **/

That’s not cool. Once you’re used to Zend Studio’s behavior in this situation, it’s hard to go back to life without it.

Fortunately, TextMate has some very powerful scripting capabilities. Execute the following steps (assuming TextMate 1.5.2 or later), and you can get the same auto-comment-formatting magic in TextMate.

  • Open the Bundle Editor. From the Bundles menu, select Bundle Editor => Show Bundle Editor.
  • Create a new Snippet. At the bottom of the list of bundles, shortcuts, etc. on the left side of the Bundle Editor dialog, there’s an interface element with a plus sign and a “down arrow.” Choose New Snippet from there.
  • Enter Regular Expression. Clear out the default snippet hint text (which starts with “Syntax Summary”), and enter the following code:
${TM_CURRENT_LINE/(.**/$)|.*?(/*(?!.**/)).*|.*/(?1:
:
(?2: )* )/}

(line breaks are intentional, and necessary. See txt-only version.)

  • Assign Activation and Scope. Choose “Key Equivalent” as the Activation method. Click in the input box and hit your “Return” key. In the “Scope Selector” field, type
    source.php comment.block
  • Clean up. The snippet should be in a bundle referenced by your name. For example, mine is under “Clay Loveless’s Bundle.” Change the name of the bundle to something more meaningful than “untitled”, such as “Comment Newline”.

You should now get properly formatted new lines in your PHP comment blocks, just like Zend Studio. Enjoy!

Thanks to Paul M. Jones for suggesting that I take another look at TextMate. And, thanks to Brad Choate for the cool blogging bundle that I used to publish this post directly from TextMate.

blogged from TextMate

Technorati , , ,


7 Responses to “TextMate and phpDoc Comment Blocks”  

Leds (www.dsource.org/projects/leds) with PHP autocompetion / parsing is getting closer … I’ve pretty much dumped my old editor for it.

The code for the editor is pretty simple, and easy to understand, so adding stuff like you mention here should be trivial. - you also get to understand editors, without having to read 1000’s of files (think of eclipse). And it’s lightweight (eg. fast) and D’s an interesting language to learn - alot like PHP in many ways…

I guess you probably want a windows version - that should be feasible, although probably a bit of a challenge ;)

Hmm… I can’t think of anything that I’d want a Windows version of. As it is, I’m already resentful that I have to run a copy of Windows on my MacBook Pro. ;)

I’m a Mac User and use jEdit for the following reasons:
- Faster than Zend, Eclipse and co.
- [S]FTP Plugin
- PHP Parser Plugin (shows you trivial mistakes)
- CodeBrowser (TreeView of Classes Functions and all variables)
- XHTML completion
- HyperSearch (Searching Replacing with regexp over whole directorys, open buffers or just the current one)
- Filebrowsepane
- Tidy integration
- It runs on every operating system
- Great Syntax Highlighting and Folding
- Nice OS X integration with the OSX Plugin
- Runs on Windows, OSX, Linux and so on
- Great Plugin Manager with a huge number of usefull Plugins (keep it small, or bloat it your self :-) )
- It’s GPL !!!!!!

There is just one important thing if your a Mac User, you have to download the 4.3pre4 version, because the older versions suck on OSX. One of the Core Developer made a DMG and .app just for me:
http://community.jedit.org/misc/jEdit4.3pre4.dmg

Jonas, that’s interesting — I actually looked at jEdit over the weekend before settling down to play with TextMate.

I’m still relatively new to TextMate, so I may be incorrect with this statement, but — I believe TextMate does all of the things you’ve mentioned, with the exception of running on Windows and Linux.

Since I develop on Mac OS X, I don’t care if my editor of choice runs on Windows or Linux. In fact, TextMate (and BBEdit, for that matter) are appealing because they don’t try to be all things to all users of all platforms. Instead, they focus on Mac users and spare us all the overhead of running Java-based applications. As such, both are considerably faster on OS X than any Java-based editor will ever be.

I’d take that perk over multi-platform support any day.

Enormously helpful. I love TextMate (I’ve entirely ditched BBEdit), but pine for many of the php-specific shortcuts that the ZDE provides. You are a good man.

TextMate is capable of doing that by itself, actually. Since I just got my first Mac and have no clue on what the actual keys names (I suppose ‘K on its side’ just doesn’t fly) are, I’ll just give you the link to the cheat-sheet where I found the key combination in:

http://www.g-design.net/textmate.pdf

Look for “Continue comment line”. Other usefull links for working with TextMate can be found on the MacroMates weblog: http://macromates.com/blog/archives/2006/05/12/tutorials-and-cheat-sheets/

Hi Clay - nice redesign btw ;-) Am very close to caving in for a powermac, does Zend run similarly performance-wise to how it runs on Linux?

I would hate to make the big switch then find the app i use all day long runs like a dog.

The above comments don’t mention debuggers, surely that’s the deciding factor in choosing an editor.



What is Killersoft?

Killersoft is a small web development firm located in Fremont, California, founded by web developer and author Clay Loveless.