@@ -120,8 +120,8 @@ public struct Configuration: Codable, Equatable {
120
120
self . version = highestSupportedConfigurationVersion
121
121
}
122
122
123
- /// Constructs a Configuration using values from a JSON config file
124
- public init ( configFile url: URL ) throws {
123
+ /// Constructs a Configuration by loading it from a configuration file.
124
+ public init ( contentsOf url: URL ) throws {
125
125
let data = try Data ( contentsOf: url)
126
126
self = try JSONDecoder ( ) . decode ( Configuration . self, from: data)
127
127
}
@@ -196,16 +196,18 @@ public struct Configuration: Codable, Equatable {
196
196
try container. encode ( rules, forKey: . rules)
197
197
}
198
198
199
- /// Configuration file associated with a swift file.
200
- ///
201
- /// Looks for a ".swift-format" file in the same directory as the swift file, or its nearest parent.
202
- /// If one is not found, returns "nil".
203
- public static func configurationFile( forSwiftFile url: URL ) -> URL ? {
199
+ /// Returns the URL of the configuration file that applies to the given file or directory.
200
+ public static func url( forConfigurationFileApplyingTo url: URL ) -> URL ? {
204
201
var path = url. absoluteURL
205
202
let configFilename = " .swift-format "
206
-
203
+ var isDirectory : ObjCBool = false
204
+ if FileManager . default. fileExists ( atPath: path. path, isDirectory: & isDirectory) ,
205
+ isDirectory. boolValue {
206
+ // will be deleted in a loop
207
+ path. appendPathComponent ( " placeholder " )
208
+ }
207
209
repeat {
208
- path = path . deletingLastPathComponent ( )
210
+ path. deleteLastPathComponent ( )
209
211
let candidateFile = path. appendingPathComponent ( configFilename)
210
212
if FileManager . default. isReadableFile ( atPath: candidateFile. path) {
211
213
return candidateFile
0 commit comments