Tag Archive for ruby

Xcode for Ruby, pt 2

A second issue with using Xcode for editing Ruby is that it by default does not correctly comment code in Ruby files. This again is easy to fix:
Open the scripts menu, choose Edit User Scripts.
Select Comments: Un/Comment Selection
This will bring up a Perl script that Xcode uses to perform the comment/uncomment function. We need to modify this to recognize Ruby file types:

Replace:

    # determine the type of file we have by looking for the #! line at the top
    # careful--it might already be commented out!
    my $commentString;
    if ($fileString =~ m!^($perlCmt|$cCmt)?#\!\s*.*?/perl|^($perlCmt|$cCmt)?#\!\s*.*?#\!\s*.*?/sh!) {
        $commentString = $perlCmt;
    } else {
        $commentString = $cCmt;
    }

with:

    my $fileName = "%%%{PBXFilePath}%%%";
    # determine the type of file we have by looking for the #! line at the top
    # careful--it might already be commented out!
    # Otherwise look at the file extension
    my $commentString;
    if ($fileString =~ m!^($perlCmt|$cCmt)?#\!\s*.*?/perl|^($perlCmt|$cCmt)?#\!\s*.*?/ruby|^($perlCmt|$cCmt)?#\!\s*.*?/sh!
            || $fileName =~ m!\.(rb|erb|rake|pl|sh)$!) {
        $commentString = $perlCmt;
    } else {
        $commentString = $cCmt;
    }

Xcode for Ruby

Xcode 3’s language support for Ruby is somewhat broken. Fortunately, fixing it only requires a few small modifications.

One major problem, Xcode doesn’t know what .erb and .rake files are.
There should be a file:
/Developer/Library/PrivateFrameworks/DevToolsCore.framework/Versions/Current/Resources/Standard file types.pbfilespec

Copy this to /Library/Application Support/Developer/Shared/Xcode/Specifications/, creating that directory if it doesn’t exist. Editing the original works, but your changes will be overwritten if you update Xcode, better to just override the standard files.
Edit the html-like files entry (line 335 in mine) to add erb:

    // html-like files
    {
          Identifier = text.html.other;
          BasedOn = text.html;
          Extensions = (shtml, jsp, rhtml, erb);
    },

I did the same to add a “rake” file type, around line 543, and also added the line “FilenamePatterns = (“[rR]akefile”);” to detect rakefiles without the .rake extension:

     {
          Identifier = text.script.ruby;
          BasedOn = text.script;
          Extensions = (rb,rbw,rake);
          FilenamePatterns = ("[rR]akefile");
          MagicWord = ("#!/bin/ruby", "#! /bin/ruby", "#!/usr/bin/ruby", "#! /usr/bin/ruby", "#!/usr/local/bin/ruby", "#! /usr/local/bin/ruby");
          ComputerLanguage = shell.ruby;
          Language = "xcode.lang.ruby";
          IsExecutable = YES;
    },

Next, the Ruby language syntax highlighting, selecting, etc need some tweaking. Copy the file
/Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/Current/Resources/Ruby.xclangspec
to /Library/Application Support/Developer/Shared/Xcode/Specifications/

By default, Xcode will select too much when doubleclicking. When trying to select an array identifier, it will select the opening [ bracket around the indices.
Go to the lines:

    Identifier = "xcode.lang.ruby.identifier";
    Syntax = {
        StartChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_[";
        Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_?[]";

And remove the [ and [] characters, which will make selections break at the brackets around array indices.

Now restart Xcode…the changes will not take effect until you do.