Skip to content

Commit d7fa663

Browse files
committed
Initial stab at option to disable all formatting.
1 parent c1e7b6e commit d7fa663

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
@@ -59,6 +70,9 @@ public struct Configuration: Codable, Equatable {
5970

6071
/// MARK: Common configuration
6172

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

263277
/// Creates a new `Configuration` by loading it from a configuration file.
264278
public init(contentsOf url: URL) throws {
279+
if url.lastPathComponent == Self.suppressionFileName {
280+
self = Configuration(allDisabled: true)
281+
return
282+
}
283+
265284
let data = try Data(contentsOf: url)
266285
try self.init(data: data)
267286
}
@@ -295,6 +314,9 @@ public struct Configuration: Codable, Equatable {
295314
// default-initialized instance.
296315
let defaults = Configuration()
297316

317+
self.allDisabled =
318+
try container.decideIfPresent(Bool.self, forKey: .allDisabled)
319+
?? false
298320
self.maximumBlankLines =
299321
try container.decodeIfPresent(Int.self, forKey: .maximumBlankLines)
300322
?? defaults.maximumBlankLines
@@ -382,6 +404,7 @@ public struct Configuration: Codable, Equatable {
382404
var container = encoder.container(keyedBy: CodingKeys.self)
383405

384406
try container.encode(version, forKey: .version)
407+
try container.encode(allDisabled, forKey: .allDisabled)
385408
try container.encode(maximumBlankLines, forKey: .maximumBlankLines)
386409
try container.encode(lineLength, forKey: .lineLength)
387410
try container.encode(spacesBeforeEndOfLineComments, forKey: .spacesBeforeEndOfLineComments)
@@ -426,10 +449,14 @@ public struct Configuration: Codable, Equatable {
426449
}
427450
repeat {
428451
candidateDirectory.deleteLastPathComponent()
429-
let candidateFile = candidateDirectory.appendingPathComponent(".swift-format")
452+
let candidateFile = candidateDirectory.appendingPathComponent(Self.configurationFileName)
430453
if FileManager.default.isReadableFile(atPath: candidateFile.path) {
431454
return candidateFile
432455
}
456+
let suppressingFile = candidateDirectory.appendingPathComponent(Self.suppressionFileName)
457+
if FileManager.default.isReadableFile(atPath: suppressingFile.path) {
458+
return suppressingFile
459+
}
433460
} while !candidateDirectory.isRoot
434461

435462
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)