Skip to content

About

Formatting technology for Swift source code

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

swift-format, CaptureContext fork

A fork of swift-format that formats and lints Swift to the CaptureContext Swift style guide.

format rewrites what the guide settles mechanically. lint reports the rest. Code that no rule touches comes out exactly as it was written.

Note

This is a pre-release. The rules agree with the guide's examples and the test suite passes, but the formatter hasn't yet been proven on a large project by formatting it and building the result. Review what format changes before you commit it.

How it differs from upstream

  • No line-width printer. Upstream re-prints every file to fit a line length. The guide lays code out by visual weight and keeps the author's blank lines, which a printer can't express. Here each rule edits only the layout it owns, and everything else is left byte for byte. The printer is still in the tree behind usePrettyPrinter, off by default.
  • The guide's rules. About fifty rules named after the guide's pages: guard-two-line, argument-layout, explicit-access-control, trailing-closures and so on. Upstream rules the guide doesn't ask for are off.
  • Kebab-case rule names. Every rule is named the way its findings print it, [ternary-indentation]. Upstream's type names (NeverForceUnwrap) still work in configuration files.
  • Format comments. // format:disable, stash, restore and adjust switch rules or change a rule's parameter for a statement, a few lines, a region or a file.
  • A rules object merges with the defaults. A rule the configuration doesn't list keeps its default. Upstream reads the object as the complete set.
  • Warnings don't fail. lint exits successfully when it only has warnings. Pass --strict to fail on them.

How it relates to the guide

  • The guide's text and examples are the reference. The tool follows them, never the other way round.
  • format never produces a shape the guide calls wrong. A rule that can't promise that only warns.
  • When two shapes are both correct, the one that was written stays.
  • Some rules of judgment are turned into thresholds, such as 80 columns for a line or 120 lines for a span without a blank line. A threshold is how the tool is built, not the rule.
  • What depends on what the code does, such as grouping statements into paragraphs, has no rule and stays with review.

Build

git clone https://github.com/capturecontext/swift-format
cd swift-format
swift build -c release

The binary is .build/release/swift-format. It needs a Swift 6 toolchain. Only the SwiftPM build is maintained; the CMake files are upstream's and don't know about this fork's dependencies.

This is a separate tool from the swift format that ships in the Swift toolchain. Run this binary by its path, or put it on your PATH under another name.

Use

Format files in place:

swift-format format --in-place --recursive Sources Tests

Without --in-place the result goes to standard output, which shows what a file would become.

List findings:

swift-format lint --recursive Sources Tests
Profile.swift:12:2: warning: [explicit-property-type] Give a local set from a literal its type: the literal doesn't say which type it becomes.

The name in brackets is the rule. It's the same name in the configuration, in comments and on the guide's pages. lint reports the fixable rules too, so it shows what format would change.

Configure

A project that follows the guide as written needs no configuration. To change a default, add a .swift-format JSON file at the project root and list only what changes:

{
	"rules": {
		"dense-span": false,
		"reducer-name-suffix": true
	},
	"preferredLineLimit": { "local": 80 },
	"ruleParameters": {
		"argument-layout": { "inline-labeled-arguments": 2 }
	},
	"naming": {
		"acronyms": ["HTML", "HTTP", "ID", "JSON", "URL", "UUID", "CSV"],
		"brands": ["iOS", "macOS", "iCloud"]
	}
}
  • rules turns rules on and off. The rules for the Composable Architecture (action-domains, reduce-switch, reducer-name-suffix, reducer-action-equatable, legacy-view-store, legacy-reduce-into) and inlinable-public-api are off until a project turns them on.
  • preferredLineLimit.local counts from a line's first character, so indentation doesn't use it up. global counts indentation too and is off by default.
  • ruleParameters sets a rule's parameters.
  • naming gives the acronyms and brands the naming rules know. Each list replaces the default one.

Print every option and rule with its current value:

swift-format dump-configuration

Silence a rule

// format:disable:next argument-layout
let header: Font = .system(size: 17, weight: .semibold)

// format:stash trailing-operator ternary-indentation
let value = condition ?
	first :
	second
// format:restore last
Comment Effect
// format:disable <rules> turns the rules off from here on
// format:enable <rules> turns them on, even ones the configuration leaves off
// format:stash <rules> turns off the ones that are on and remembers them
// format:restore <rules>, // format:restore last turns remembered rules back on
// format:adjust <rule> <key>:<value> sets a rule parameter
:next, :this, :previous after any command for one statement
:next:3, :previous:3 for the statements on that many lines

Nothing carries over to other files. Upstream's // swift-format-ignore: <rule> and // swift-format-ignore-file work too.

Documentation

Development

swift test

The fork's rules are in Sources/SwiftFormat/Rules/CaptureContext/ and their tests in Tests/SwiftFormatTests/Rules/CaptureContext/. After adding or renaming a rule, regenerate the pipeline and the rule documentation:

swift run generate-swift-format

main holds the fork's work on top of swiftlang/swift-format's main. The Sync upstream workflow merges upstream weekly: it pushes when the merge is clean and the tests pass, and opens a pull request otherwise.

License

Apache License 2.0 with Runtime Library Exception, as upstream. See LICENSE.txt.

About

Formatting technology for Swift source code

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages