Search code examples
xcodeautomationindentationuserscripts

How do I get the output of an Xcode user script to auto indent?


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?


Solution

  • 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..