Skip to content

Commit eb29cb8

Browse files
committed
Initial stab at option to disable all formatting.
1 parent ed01028 commit eb29cb8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ internal let highestSupportedConfigurationVersion = 1
2424
/// Holds the complete set of configured values and defaults.
2525
public struct Configuration: Codable, Equatable {
2626

27+
/// Name of the configuration file to look for.
28+
/// The presence of this file in a directory will cause the formatter
29+
/// to use the configuration specified in that file.
30+
private static let configurationFileName = ".swift-format"
31+
32+
/// Name of the suppression file to look for.
33+
/// The presence of this file in a directory will cause the formatter
34+
/// to skip formatting files in that directory and its subdirectories.
35+
private static let suppressionFileName = ".no-swift-format"
36+
2737
private enum CodingKeys: CodingKey {
2838
case version
39+
case allDisabled
2940
case maximumBlankLines
3041
case lineLength
3142
case spacesBeforeEndOfLineComments
@@ -60,6 +71,9 @@ public struct Configuration: Codable, Equatable {
6071

6172
/// MARK: Common configuration
6273

74+
/// Is all formatting disbled?
75+
public var allDisabled: Bool
76+
6377
/// The dictionary containing the rule names that we wish to run on. A rule is not used if it is
6478
/// marked as `false`, or if it is missing from the dictionary.
6579
public var rules: [String: Bool]
@@ -270,6 +284,11 @@ public struct Configuration: Codable, Equatable {
270284

271285
/// Creates a new `Configuration` by loading it from a configuration file.
272286
public init(contentsOf url: URL) throws {
287+
if url.lastPathComponent == Self.suppressionFileName {
288+
self = Configuration(allDisabled: true)
289+
return
290+
}
291+
273292
let data = try Data(contentsOf: url)
274293
try self.init(data: data)
275294
}
@@ -303,6 +322,9 @@ public struct Configuration: Codable, Equatable {
303322
// default-initialized instance.
304323
let defaults = Configuration()
305324

325+
self.allDisabled =
326+
try container.decideIfPresent(Bool.self, forKey: .allDisabled)
327+
?? false
306328
self.maximumBlankLines =
307329
try container.decodeIfPresent(Int.self, forKey: .maximumBlankLines)
308330
?? defaults.maximumBlankLines
@@ -396,6 +418,7 @@ public struct Configuration: Codable, Equatable {
396418
var container = encoder.container(keyedBy: CodingKeys.self)
397419

398420
try container.encode(version, forKey: .version)
421+
try container.encode(allDisabled, forKey: .allDisabled)
399422
try container.encode(maximumBlankLines, forKey: .maximumBlankLines)
400423
try container.encode(lineLength, forKey: .lineLength)
401424
try container.encode(spacesBeforeEndOfLineComments, forKey: .spacesBeforeEndOfLineComments)
@@ -440,10 +463,14 @@ public struct Configuration: Codable, Equatable {
440463
}
441464
repeat {
442465
candidateDirectory.deleteLastPathComponent()
443-
let candidateFile = candidateDirectory.appendingPathComponent(".swift-format")
466+
let candidateFile = candidateDirectory.appendingPathComponent(Self.configurationFileName)
444467
if FileManager.default.isReadableFile(atPath: candidateFile.path) {
445468
return candidateFile
446469
}
470+
let suppressingFile = candidateDirectory.appendingPathComponent(Self.suppressionFileName)
471+
if FileManager.default.isReadableFile(atPath: suppressingFile.path) {
472+
return suppressingFile
473+
}
447474
} while !candidateDirectory.isRoot
448475

449476
return nil

Sources/swift-format/Frontend/Frontend.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ class Frontend {
182182
return nil
183183
}
184184

185+
guard !configuration.allDisabled else {
186+
// Formatting is suppressed for this file.
187+
return nil
188+
}
189+
185190
return FileToProcess(
186191
fileHandle: sourceFile,
187192
url: url,

0 commit comments

Comments
 (0)