@@ -24,8 +24,19 @@ internal let highestSupportedConfigurationVersion = 1
24
24
/// Holds the complete set of configured values and defaults.
25
25
public struct Configuration : Codable , Equatable {
26
26
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
+
27
37
private enum CodingKeys : CodingKey {
28
38
case version
39
+ case allDisabled
29
40
case maximumBlankLines
30
41
case lineLength
31
42
case spacesBeforeEndOfLineComments
@@ -59,6 +70,9 @@ public struct Configuration: Codable, Equatable {
59
70
60
71
/// MARK: Common configuration
61
72
73
+ /// Is all formatting disbled?
74
+ public var allDisabled : Bool
75
+
62
76
/// The dictionary containing the rule names that we wish to run on. A rule is not used if it is
63
77
/// marked as `false`, or if it is missing from the dictionary.
64
78
public var rules : [ String : Bool ]
@@ -262,6 +276,11 @@ public struct Configuration: Codable, Equatable {
262
276
263
277
/// Creates a new `Configuration` by loading it from a configuration file.
264
278
public init ( contentsOf url: URL ) throws {
279
+ if url. lastPathComponent == Self . suppressionFileName {
280
+ self = Configuration ( allDisabled: true )
281
+ return
282
+ }
283
+
265
284
let data = try Data ( contentsOf: url)
266
285
try self . init ( data: data)
267
286
}
@@ -295,6 +314,9 @@ public struct Configuration: Codable, Equatable {
295
314
// default-initialized instance.
296
315
let defaults = Configuration ( )
297
316
317
+ self . allDisabled =
318
+ try container. decideIfPresent ( Bool . self, forKey: . allDisabled)
319
+ ?? false
298
320
self . maximumBlankLines =
299
321
try container. decodeIfPresent ( Int . self, forKey: . maximumBlankLines)
300
322
?? defaults. maximumBlankLines
@@ -382,6 +404,7 @@ public struct Configuration: Codable, Equatable {
382
404
var container = encoder. container ( keyedBy: CodingKeys . self)
383
405
384
406
try container. encode ( version, forKey: . version)
407
+ try container. encode ( allDisabled, forKey: . allDisabled)
385
408
try container. encode ( maximumBlankLines, forKey: . maximumBlankLines)
386
409
try container. encode ( lineLength, forKey: . lineLength)
387
410
try container. encode ( spacesBeforeEndOfLineComments, forKey: . spacesBeforeEndOfLineComments)
@@ -426,10 +449,14 @@ public struct Configuration: Codable, Equatable {
426
449
}
427
450
repeat {
428
451
candidateDirectory. deleteLastPathComponent ( )
429
- let candidateFile = candidateDirectory. appendingPathComponent ( " .swift-format " )
452
+ let candidateFile = candidateDirectory. appendingPathComponent ( Self . configurationFileName )
430
453
if FileManager . default. isReadableFile ( atPath: candidateFile. path) {
431
454
return candidateFile
432
455
}
456
+ let suppressingFile = candidateDirectory. appendingPathComponent ( Self . suppressionFileName)
457
+ if FileManager . default. isReadableFile ( atPath: suppressingFile. path) {
458
+ return suppressingFile
459
+ }
433
460
} while !candidateDirectory. isRoot
434
461
435
462
return nil
0 commit comments