views:

92

answers:

3

I think over the last year and a half, while experimenting with Cocoa and Cocoa Touch development, I've been struggling with getting Xcode to support a kind of key macro to handle this specific coding scenario...

I frequently find myself not knowing ahead of time how many message dispatches to chain together while typing a new line of code. For example, I might type this... (Pipe character represents cursor location)

someVariable = [someObject someMessage]|

...I then realize that I need to chain a message from the returned object (since messaging nil is safe in ObjC, this is a common idiom.) So I would need to change the line of code to look like this...

someVariable = [[someObject someMessage] |

The big problem is having to prepend a left bracket before the bracket pair I'm at the end of, which would entail using the mouse or lots of repeated key tapping each time I needed to do this. After wasting about 40 or so hours off and on trying to find any kind of pre-existing keyboard shortcut or script (I investigated Perl, shell scripting, and even a bit of emacs Lisp scripting in my Googling.), I hacked together this monstrosity in AppleScript, of all things...

tell application "System Events"
    delay 0.2
    set editMenu to menu "Edit" of menu bar item "Edit" in menu bar 1 of
         process "Xcode"
    set kLeftArrowKey to 123
    key code kLeftArrowKey
    set theMenu to menu item "Balance" in menu "Format" of menu item
         "Format" in editMenu
    click theMenu
    set theMenu to menu item "Bracket Expression" in menu "Objective C"
         of menu item "Objective C" in menu "Insert Text Macro" of menu
         item "Insert Text Macro" in editMenu
    click theMenu
    key code kLeftArrowKey
end tell

There has got to be a better way.

I count at least five problems with the above... (which I'll comment on if asked.) I see frequent online complaints from new ObjC developers all the time about this syntax, and I suspect this one typing issue is the major sore point. While I like how the syntax reads (so much so that I avoid the dot notation for properties out of coding consistency), how it's first written kind of sucks. (Just like AppleScript, ironically enough, given my current hacked implementation.)

Help me end my suffering, and you'll probably end the suffering of a lot of other ObjC developers as well...

+8  A: 

What version of Xcode are you using? In my version, left brackets are prepended automatically if I type a right-bracket after a valid selector name.

In Xcode 3.2, go to Preferences, then "Text Editing", then choose "Insert opening bracket".

dreamlax
Well, heck. Figures Apple would finally solve this problem after SIX major revs (and who know how many NeXTStep iterations). Pity I'm stuck with 10.5 for now... /-:
Des Courtney
+4  A: 

If you're stuck on pre-Snow Leopard, you might try using TextMate as your editor. It will automatically insert matching braces and supports versions of OS X back to 10.4. It's a tad bit pricey (less so than BBEdit, but more than Xcode, which is free), but it's a really nice editor just the same.

Chuck
Good point, that can help. Personally, TextMate's editing paradigms are different enough from most editors that it throws me off quite a bit and hurts my productivity. In my experience, using it exclusively is the best/only way to ensure uniform productivity; you really have to get into it to fully reap the benefits.
Quinn Taylor
+1 for TextMate
e.James
TextMate's "different editing paradigms" is partly why I can't consider that right now. My workflows in the last two years alone involve Xcode, Textwrangler (nee BBEdit), Visual Studio, and UltraEdit. Looks like I'm stuck with my script until I finally install my 10.6 DVD...
Des Courtney
A: 

Prior to Xcode 3.2, I had to deal with this same problem frequently. Something that eased the pain for me was using the Option key with the left/right arrows to skip by word. Much better than waiting for it to skip by letter.

Quinn Taylor