nicklockwood / SwiftFormat

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

Inheriting rules in a subdirectory #1600

Closed meherkasam closed 9 months ago

meherkasam commented 9 months ago

Apologies if this has already been answered somewhere in the documentation. I read through the README and searched through issues but couldn't quite find what I was looking for.

I have the following directory structure:

example
├── .swiftformat
└── inner
    ├── .swiftformat
    └── test.swift

Contents of example/.swiftformat:

--rules semicolons

Contents of example/inner/.swiftformat:

--rules spaceAroundParens

Contents of example/inner/test.swift:

func hello(){
    print("Hello");
}

Command run from example/:

swiftformat .

Expected contents of example/inner/test.swift:

func hello() {
    print("Hello")
}

Actual contents of example/inner/test.swift:

func hello() {
    print("Hello");
}

It appears that the rules set in the parent directory level are not being applied to a sub-directory if it contains its own .swiftformat file. The goal here is to achieve a structure where the root level would contain a few baseline rules and some subfolders deep down the hierarchy might want to conform to all of the baseline rules, but have their own stricter rules on top of the baseline rules. Is this achievable without having to maintain manual copies of the same rules in multiple places?

Thank you for your help and appreciate any insight you can provide here.

nicklockwood commented 9 months ago

The --rules option is exclusive, meaning only those rules that are listed in the file will be enabled.

What you probably want to do is use --enable in the child directory .swiftformat files instead, so that rules specified higher up are inherited.

meherkasam commented 9 months ago

That's exactly what I needed. Thank you so much!