SwiftFormat icon indicating copy to clipboard operation
SwiftFormat copied to clipboard

[Bug] Unexpected `--ifdef no-indent` behavior on SwiftUI modifiers

Open davdroman opened this issue 2 years ago • 4 comments

I have this piece of SwiftUI code:

import SwiftUI

struct Example: View {
	var body: some View {
		Text("Example")
			.frame(maxWidth: 500, alignment: .leading)
			#if !os(tvOS)
			.font(.system(size: 14, design: .monospaced))
			#endif
			.padding(10)
	}
}

When running SwiftFormat 0.50.6 with --ifdef no-indent I get:

import SwiftUI

struct Example: View {
	var body: some View {
		Text("Example")
			.frame(maxWidth: 500, alignment: .leading)
		#if !os(tvOS)
			.font(.system(size: 14, design: .monospaced))
		#endif
			.padding(10)
	}
}

... whereas I would expect no changes to be made.

As a side note, I was hoping to just be able to disable ifdef as a rule altogether but I don't believe this is possible given it doesn't have a valid name to pass onto --disable.

davdroman avatar Dec 19 '22 00:12 davdroman

This is as-designed, but I'm not sure if it's desirable behavior or not. no-indent means to not indent the code inside the ifdef, rather than the ifdef statement itself. I guess an additional preserve option to preserve the relative position of the statement as-is might be a good idea?

--ifdef is just a configuration option for the indent rule, so the only way to disable it is with --disable indent. You could do that for just this file or section using // swiftformat:disable indent.

nicklockwood avatar Dec 19 '22 09:12 nicklockwood

I guess an additional preserve option to preserve the relative position of the statement as-is might be a good idea?

I could definitely use something like that.

You could do that for just this file or section using // swiftformat:disable indent.

Neat! Thanks 🙂

davdroman avatar Dec 19 '22 20:12 davdroman

Similar (same?) issue, where, for example, this:

.toolbar {
    ToolbarItem(placement: {
        #if os(iOS)
        .cancellationAction
        #else
        .automatic
        #endif
    }()) { ... }
}

...gets formatted to this:

.toolbar {
    ToolbarItem(placement: {
        #if os(iOS)
            .cancellationAction
        #else
            .automatic
        #endif
    }()) { ... }
}

Would a solution be to allow for specifying the size of the indent in the ifdef configuration option? Presumably if I could pass --ifdef double-indent or --ifdef align-with-code then it would not make any changes to the original code.

Just a thought - thanks for your work on this great tool!

JUSTINMKAUFMAN avatar Feb 17 '23 16:02 JUSTINMKAUFMAN

We're seeing the same issues as @JUSTINMKAUFMAN, primarily for SwiftUI modifiers, and seemingly semi-randomly (sometimes it doesn't add pesky indents, sometimes it does, without any obvious reason).

gillesguillemin avatar Aug 24 '23 10:08 gillesguillemin