How can I force Xcode to retain my indentation when I copy and paste?
Asked Answered
C

1

34

I find myself wasting so much time dealing with Xcodes auto indentation that I have to ask if there is something I just have wrong in my settings. Basically if I spend time indenting code within a method, and then copy that entire method and paste it, the newly pasted method does not retain any of the white space that I applied to the original.

For example, here is a screenshot where the top method I indented all the objects of an array so they are lined up properly. Then I selected the entire method, copied and pasted, and you can see the method below has the indentation all messed up.

enter image description here

I am using Xcode 4.4.1, here are my settings:

my indentation settings

Cobra answered 11/9, 2012 at 5:28 Comment(2)
When you paste use Shift-Option-Command-V instead of Command-V and your formatting will be retained.Carafe
You'll love this trick: Create a line of code within the scope of a function. Make sure it's semi long. After it put a { on the line below it, close it or not, will not matter. Then, put your cursor at the end of the first line, hit space (or tab), then hit enter. I just like reproducing that bug. If you don't get it, add some more characters, but don't make the line long enough to go past the new line wrapping. Your cursor will now appear ahead and below the {, yet the text shows in the right area. Harmless, but funny one I can repro on XCode 4 as a funny party-show-off trickProduction
R
-5

Works as intended.

…Objects: and forKeys: should be aligned as they form part of the same method signature.

It might be easier to format your code if you use the new object literal syntax:

- (int)minBrokenPieces {
   NSDictionary *mapping = [NSDictionary dictionaryWithObjects:@[@"3", @"4", @"4", @"5", @"6", @"7", @"8"]
                                                       forKeys:[Note types]];
  [(NSString *)mapping[self.note.type] integerValue];
}

As for the code itself, it seems a bit dangerous to define these constants in one place and the note types elsewhere. Also, why use strings, when NSNumbers would suffice?

(This code assumes this function is only called from one thread).

- (int)minBrokenPieces {
    static NSDictionary *mappings;
    if (!mappings) {
        mappings = @{
            noteType1  : @3,
            noteType2  : @4,
            noteType3  : @4,
            noteType4  : @5,
            noteType5  : @6,
            noteType6  : @7,
            noteType7  : @8,
        };
    }
    NSAssert(mappings[self.note.type] != nil, @"Used a note type for which there is no minBrokenPieces defined");
    return [mappings[self.note.type] intValue];
}
Raspy answered 9/11, 2012 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.