Saturday, April 11, 2009

Quick Look for Groovy with Syntax Highlighting

In my previous post OS X: Quick Look for Groovy I showed how to enable Quick Look for Groovy source files.
The result was very cool, but syntax highlighting was missing.

After playing around a bit I found out how to enable syntax highlighting for Groovy in Quick Look.

Here is how it works:
  • Install the qlcolorcode-plugin.
    Unfortunately the plugin does not support Groovy out of the box, but we will change that. After installing the plugin you should have the package QLColorCode.qlgenerator in your ~/Library/QuickLook.

  • Go into the QLColorCode.qlgenerator package. Either by right-clicking in Finder and choosing "Show Package Contents" or by navigating into the directory in a shell.

  • Inside the QLColorCode.qlgenerator package edit the script Resources/colorize.sh.

    Extend the case-statement in the middle of the script the following way:
    ...
    case $target in
        *.graffle )
            # some omnigraffle files are XML and get passed to us.  Ignore them.
            exit 1
            ;;
        *.plist )
            lang=xml
            reader=(/usr/bin/plutil -convert xml1 -o - $target)
            ;;
        *.h )
            if grep -q "@interface" $target &> /dev/null; then
                lang=objc
            else
                lang=h
            fi
            ;;
        *.m )
            # look for a matlab-style comment in the first 10 lines, otherwise
            # assume objective-c.  If you never use matlab or never use objc,
            # you might want to hardwire this one way or the other
            if head -n 10 $target | grep -q "^ *%" &> /dev/null; then
                lang=m
            else
                lang=objc
            fi
            ;;
        *.groovy )
        	lang=java
            ;;
        * ) 
            lang=${target##*.}
        ;;
    esac
    ...
    

    This tells Highlight to treat Groovy files as Java source code.

  • Next edit Info.plist inside the QLColorCode.qlgenerator package.
    Add the follwing snippet at the end of Info.plist (just before the ending </array> </dict> </plist>)
    	
    		UTTypeConformsTo
    		
    			public.source-code
    		
    		UTTypeDescription
    		Groovy Source Code
    		UTTypeIdentifier
    		org.codehaus.groovy-source
    		UTTypeTagSpecification
    		
    			public.filename-extension
    			
    				groovy
    			
    		
    		
    

  • Now you need to nudge the system to tell it something has changed. Moving the whole plugin (QLColorCode.qlgenerator) to the desktop then back to its installed location should do the trick.

  • This should be it! The result is Quick Look for Groovy with syntax highlighting:

    Picture 2.png


    The syntax highlighting is still not perfect, since it is Java highlighting... but it is much better than no highlighting.

    2 comments:

    1. Thanks for sharing very useful information.

      ReplyDelete
    2. Update for Snow Leopard:

      In order to get QLColorCode running on Snow Leopard, follow this adivce:
      Important information on using QLColorCode with Xcode v3.2 and later

      ... it worked for me :-)

      ReplyDelete

    Related Posts Plugin for WordPress, Blogger...