Skip to content

Commit daf7e7d

Browse files
authored
Merge pull request #685 from judemille/main
Find configuration based on CWD when formatting from stdin
2 parents ec73053 + cc15dc6 commit daf7e7d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

Sources/swift-format/Frontend/ConfigurationLoader.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ struct ConfigurationLoader {
1919
/// The cache of previously loaded configurations.
2020
private var cache = [String: Configuration]()
2121

22-
/// Returns the configuration found by searching in the directory (and ancestor directories)
23-
/// containing the given `.swift` source file.
22+
/// Returns the configuration found by walking up the file tree from `url`.
23+
/// This function works for both files and directories.
2424
///
2525
/// If no configuration file was found during the search, this method returns nil.
2626
///
2727
/// - Throws: If a configuration file was found but an error occurred loading it.
28-
mutating func configuration(forSwiftFileAt url: URL) throws -> Configuration? {
28+
mutating func configuration(forPath url: URL) throws -> Configuration? {
2929
guard let configurationFileURL = Configuration.url(forConfigurationFileApplyingTo: url)
3030
else {
3131
return nil

Sources/swift-format/Frontend/Frontend.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class Frontend {
213213
// then try to load the configuration by inferring it based on the source file path.
214214
if let swiftFileURL = swiftFileURL {
215215
do {
216-
if let configuration = try configurationLoader.configuration(forSwiftFileAt: swiftFileURL) {
216+
if let configuration = try configurationLoader.configuration(forPath: swiftFileURL) {
217217
self.checkForUnrecognizedRules(in: configuration)
218218
return configuration
219219
}
@@ -223,11 +223,26 @@ class Frontend {
223223
"Unable to read configuration for \(swiftFileURL.path): \(error.localizedDescription)")
224224
return nil
225225
}
226+
} else {
227+
// If reading from stdin and no explicit configuration file was given,
228+
// walk up the file tree from the cwd to find a config.
229+
230+
let cwd = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
231+
// Definitely a Swift file. Definitely not a directory. Shhhhhh.
232+
do {
233+
if let configuration = try configurationLoader.configuration(forPath: cwd) {
234+
self.checkForUnrecognizedRules(in: configuration)
235+
return configuration
236+
}
237+
} catch {
238+
diagnosticsEngine.emitError(
239+
"Unable to read configuration for \(cwd): \(error.localizedDescription)")
240+
return nil
241+
}
226242
}
227243

228-
// If neither path was given (for example, formatting standard input with no assumed filename)
229-
// or if there was no configuration found by inferring it from the source file path, return the
230-
// default configuration.
244+
// An explicit configuration has not been given, and one cannot be found.
245+
// Return the default configuration.
231246
return Configuration()
232247
}
233248

0 commit comments

Comments
 (0)