@@ -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
@@ -60,6 +71,9 @@ public struct Configuration: Codable, Equatable {
60
71
61
72
/// MARK: Common configuration
62
73
74
+ /// Is all formatting disbled?
75
+ public var allDisabled : Bool
76
+
63
77
/// The dictionary containing the rule names that we wish to run on. A rule is not used if it is
64
78
/// marked as `false`, or if it is missing from the dictionary.
65
79
public var rules : [ String : Bool ]
@@ -270,6 +284,11 @@ public struct Configuration: Codable, Equatable {
270
284
271
285
/// Creates a new `Configuration` by loading it from a configuration file.
272
286
public init ( contentsOf url: URL ) throws {
287
+ if url. lastPathComponent == Self . suppressionFileName {
288
+ self = Configuration ( allDisabled: true )
289
+ return
290
+ }
291
+
273
292
let data = try Data ( contentsOf: url)
274
293
try self . init ( data: data)
275
294
}
@@ -303,6 +322,9 @@ public struct Configuration: Codable, Equatable {
303
322
// default-initialized instance.
304
323
let defaults = Configuration ( )
305
324
325
+ self . allDisabled =
326
+ try container. decideIfPresent ( Bool . self, forKey: . allDisabled)
327
+ ?? false
306
328
self . maximumBlankLines =
307
329
try container. decodeIfPresent ( Int . self, forKey: . maximumBlankLines)
308
330
?? defaults. maximumBlankLines
@@ -396,6 +418,7 @@ public struct Configuration: Codable, Equatable {
396
418
var container = encoder. container ( keyedBy: CodingKeys . self)
397
419
398
420
try container. encode ( version, forKey: . version)
421
+ try container. encode ( allDisabled, forKey: . allDisabled)
399
422
try container. encode ( maximumBlankLines, forKey: . maximumBlankLines)
400
423
try container. encode ( lineLength, forKey: . lineLength)
401
424
try container. encode ( spacesBeforeEndOfLineComments, forKey: . spacesBeforeEndOfLineComments)
@@ -440,10 +463,14 @@ public struct Configuration: Codable, Equatable {
440
463
}
441
464
repeat {
442
465
candidateDirectory. deleteLastPathComponent ( )
443
- let candidateFile = candidateDirectory. appendingPathComponent ( " .swift-format " )
466
+ let candidateFile = candidateDirectory. appendingPathComponent ( Self . configurationFileName )
444
467
if FileManager . default. isReadableFile ( atPath: candidateFile. path) {
445
468
return candidateFile
446
469
}
470
+ let suppressingFile = candidateDirectory. appendingPathComponent ( Self . suppressionFileName)
471
+ if FileManager . default. isReadableFile ( atPath: suppressingFile. path) {
472
+ return suppressingFile
473
+ }
447
474
} while !candidateDirectory. isRoot
448
475
449
476
return nil
0 commit comments