views:

864

answers:

4

Is there a way to force XCode to trim trailing whitespaces when I save file?

I'm using version 3.1.3 if that matters.

+1  A: 

This is not possible in Xcode 3.2

Edit:

I answered this question so briefly because there's no way to do this properly.

Of course, since it's software, you can do anything: Starting with Input Manager hacks or other ways of code injection to system wide keyboard interception, you can alter your local system to do anything anytime. You could set up an Applescript folder action (arrgh) or use a launch demon and the FSEvents facility to watch your source code files.

You can also add a couple of scripts to Xcode (user scripts in the menu, script phases in targets, custom Actions in the organizer, there's even the very unknown possibility a startup script), but all these solutions are flawed, since it involves the user or custom setup on the user's machine.

I'm not aware of a solution which simply works after checking out a project from SCM. I believe that there's need for this and similar customization scripts, so I filed a bug (radar 7203835, "Feature: more user script triggers in Xcode workflow"). I did not receive any feedback yet.

Here's the full text of the radar entry:

It would be useful to have more places to run scripts in Xcode.

Examples:

  1. Pre build scripts
    Pre build scripts could be used to build prerequisites like *.xcconfig files or config.h headers. This is not possible with a "Run Script Build phases", since dependency tracking takes place before any build phase is triggered.

  2. Post build scripts
    Similar to above, but running after the build finished (including code signing etc). Useful for additional packaging, validity checking etc.

  3. Pre/Post SCM Commit scripts.
    To check project integrity.

  4. Pre/Post File Save Script.
    To check/alter a file before saving. E.g. run cody beautifiers

  5. Custom project actions.
    I'm aware of the organizer's ability to define arbitrary actions. But this is a per user feature (not part of the project). I'd like to define actions like build or clean that show up in the build menu and that are part of a project.

Nikolai Ruhe
And no workarounds? System-wide on-save hooks?..
Alexander Gladysh
Edited to answer why workarounds are unsatisfying in this case
Nikolai Ruhe
Thanks. While I'm in favor of doing it in the proper way, I'd settle for about any not-too-intrusive hack. Those extra whitespaces are really annoying.
Alexander Gladysh
I hate them, too!
Nikolai Ruhe
+1  A: 

You can create a script and bind it to a keyboard shortcut:

  • Select Scripts Menu > Edit User Scripts...
  • Press the + button and select New Shell Script
  • Give it a name like "Strip Trailing Spaces", and give it a shortcut like ⌃⇧R.
  • Set Input to "Selection" and Output to "Replace Selection"

Then enter the following script:

#!/usr/bin/perl

while (<>) {
    s/\s+$//;
    print "$_\n";
}
Darren
Ah, yes, so simple... Thanks. Is there any way to associate this with file save?
Alexander Gladysh
+1  A: 

start working in xcode 3.2.2

thanks a lot

alex.karavan
+4  A: 

I'm using the Google Toolbox For Mac Xcode Plugin, it adds a "Correct whitespace on save" parameter that trim trailing whitespace on save. I missed that a lot from emacs.

Maxime
I'm using this plugin as well. It does what it says.
Nathan Reed