views:

81

answers:

1

The Problem

I want to press a key when I have a line highlighted and convert from a single line:

JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1 to:date2 intoMOC:mockRawMOC];

to a multiline statement:

JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
                                                         to:date2
                                                    intoMOC:mockRawMOC];

What I've Tried

I've got a simple ruby script that almost gets me there.

#!/usr/bin/env ruby
s = STDIN.read
s.gsub!(/(:.+?\w) (\w.+?)/,'\1' + "\n\t" +'\2')
print s

When I set the output to "Replace Selection", I get this:

JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
     to:date2
     intoMOC:mockRawMOC];

When I set the output to "Place on Clipboard", then paste it in, I get the desired result:

JGLogEntry *logEntry = [JGLogEntry applicationNoWindowsFrom:date1
                                                         to:date2
                                                    intoMOC:mockRawMOC];

However, this is two keypresses which is clumsy.

Any ideas how I can get the replaced text to obey Xcode's auto indent rules?

+1  A: 

Check the pre-installed script for "Convert tabs to spaces", and how it executes an in-line applescript. Use that to tell XCode to perform the menu item

Edit > Format > Re-Indent

I'm not sure how you do that with ruby, nor the details about the applescript content, but I would wager it's fairly straight-forward..

ohhorob
Thanks for the suggestion. I can get Xcode to reindent via Applescript, but because it gets executed *before* the selection is replaced, it has no effect. I guess I'll have to live with rubbish indenting for now.
John Gallagher