nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

--disable ifdef does not disable ifdef indenting #783

Open kevinbhayes opened 3 years ago

kevinbhayes commented 3 years ago

In my .swiftformat file I have two rules

--indent tab --disable ifdef

When I run swiftformat it continues to indent

#if os(ios)
    [code here]
#endif

and also turns all of my tabs to spaces. This is on the 5.2 branch. Expected behaviour is to leave the tabs alone and also leave any #if indenting alone.

nicklockwood commented 3 years ago

@kevinbhayes there isn't a rule called ifdef, so --disable ifdef results in the error:

error: 'ifdef' is not a formatting rule. Did you mean 'indent'?

(I'm not sure why you aren't seeing that when putting the rule in your .swiftformat file - it may be a bug.)

There is an --ifdef option used to configure the indent rule. That requires an argument, so what you probably want to do is remove --disable ifdef and replace it with --ifdef no-indent.

kevinbhayes commented 3 years ago

@nicklockwood Ah sorry, I see now. I am getting the warning, but also getting

warning: No Swift version was specified, so some formatting features were disabled. Specify the version of Swift you are using with the --swiftversion command line option, or by adding a .swift-version file to your project.

When I remove that statement I do not get the above version warning. But I guess that could be due to the invalid rule causing it to believe it's in another version?

I can use --ifdef no-indent, but is there a way to have it just disable the ifdef argument, but enable the rest of the indent arguments? Basically, whether it's indented or not, I want to leave it alone. --ifdef no-indent will modify any indented instances that are already there.

nicklockwood commented 3 years ago

@kevinbhayes no, there's no --ifdef ignore option I'm afraid. The only way to disable indenting of #if clauses would be to disable the indent rule altogether.

AgapovOne commented 10 months ago

Would be great to have an option to ignore separate rules for --indent, because it forces a large codebase to migrate to swiftformat in a harder way.

We could go with one rule per pull request that already leads to 100+ files, but whole indent rule with any option enabled leads to 2000+ files changed. Just harder to start.

nicklockwood commented 10 months ago

@AgapovOne I'm not sure what you mean by "ignore separate rules for indent"? Do you mean disable the indent rule? Or only apply it to certain files?

AgapovOne commented 10 months ago

My use case is that I want to fix ONLY switch cases in my codebase as a start.

So I go and use swift-format indent rule.

But it doesn't have one rule for switch cases, it has 1 super-rule that consists of 7 sub-rules.

--indent | Number of spaces to indent, or "tab" to use tabs
-- | --
--tabwidth | The width of a tab character. Defaults to "unspecified"
--smarttabs | Align code independently of tab width. defaults to "enabled"
--indentcase | Indent cases inside a switch: "true" or "false" (default)
--ifdef | #if indenting: "indent" (default), "no-indent" or "outdent"
--xcodeindentation | Match Xcode indenting: "enabled" or "disabled" (default)
--indentstrings | Indent multiline strings: "false" (default) or "true"

So to fix switch cases I have to decide what configuration I want for those 7 sub-rules and then format all my files, where it will fix switch case and 6 other things.

That would be a lot of files (actually around 2500 in my exact use case from today).

So I'm here to see if I can use only 1 sub-rule to make migration easier :)