Syntax highlighting comment keywords in TextMate (TODO, FIXME, etc)

mst's State of the Velociraptor talk at YAPC::EU inspired me to start a blog here, although this first post has nothing to do with Perl...

After reading Chisel's post on highlighting useful words like TODO in Vim, I decided to see if TextMate could do the same. After a bit of trial-and-error and a brief glance at the documentation, the following worked for me:

  1. Bundles / Bundle Editor / Edit Languages (and choose Perl if not already selected)
  2. Find this section in the language grammar definition:
    
    	repository = {
                   ...
    		line_comment = {
    			patterns = (
    				{	name = 'meta.comment.full-line.perl';
    					match = '^((#).*$\n?)';
    					captures = {
    						1 = { name = 'comment.line.number-sign.perl'; };
    						2 = { name = 'punctuation.definition.comment.perl'; };
    					};
    				},
    				{	name = 'comment.line.number-sign.perl';
    					match = '(#).*$\n?';
    					captures = { 1 = { name = 'punctuation.definition.comment.perl'; }; };
    				},
    			);
    		};
    
  3. Change it to:
    
    	repository = {
                   ...
    		line_comment = {
    			patterns = (
    				{	name = 'meta.comment.full-line.perl';
    					match = '^((#)\s?((TODO|FIXME|BUG|XXX):?)?.*$\n?)';
    					captures = {
    						1 = { name = 'comment.line.number-sign.perl'; };
    						2 = { name = 'punctuation.definition.comment.perl'; };
    						3 = { name = 'comment.keyword.string.perl'; };
    					};
    				},
    				{	name = 'comment.line.number-sign.perl';
    					match = '(#)\s?((TODO|FIXME|BUG|XXX):?)?.*$\n?';
    					captures = {
    						1 = { name = 'punctuation.definition.comment.perl'; };
    						2 = { name = 'comment.keyword.string.perl'; };
    					};
    				},
    			);
    		};
    
    and click 'Test'
  4. Now open TextMate / Preferences... and open the Fonts & Colours tab
  5. Create a new element, set Scope Selector to 'comment.keyword', and choose foreground and background colours as appropriate - I picked yellow background and red text too, but if you don't have a black background then something else may be more appropriate
  6. Done!

The TextMate Perl language grammar defines two comment items - a comment line and a comment string (i.e. a partial line) - to both we've added an optional section to the regex and an extra capture. It could be further improved by matching the keyword anywhere within the comment if required, and unlike the Vim syntax highlighting, this only matches within comments, which I think is probably preferable.

2 Comments

Nothing against what you've written above, but have you looked at the TODO bundle? I'm pretty sure it comes with the base TextMate install, and it makes this sort of thing incredibly slick and easy, including a nice html output window with clickable links.

Cheers,
Paul

Leave a comment

About Michael J

user-pic I blog about Perl.