Skip to content

Commit

Permalink
Merge pull request #685 from judemille/main
Browse files Browse the repository at this point in the history
Find configuration based on CWD when formatting from stdin
  • Loading branch information
allevato authored Feb 4, 2024
2 parents ec73053 + cc15dc6 commit daf7e7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Sources/swift-format/Frontend/ConfigurationLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ struct ConfigurationLoader {
/// The cache of previously loaded configurations.
private var cache = [String: Configuration]()

/// Returns the configuration found by searching in the directory (and ancestor directories)
/// containing the given `.swift` source file.
/// Returns the configuration found by walking up the file tree from `url`.
/// This function works for both files and directories.
///
/// If no configuration file was found during the search, this method returns nil.
///
/// - Throws: If a configuration file was found but an error occurred loading it.
mutating func configuration(forSwiftFileAt url: URL) throws -> Configuration? {
mutating func configuration(forPath url: URL) throws -> Configuration? {
guard let configurationFileURL = Configuration.url(forConfigurationFileApplyingTo: url)
else {
return nil
Expand Down
23 changes: 19 additions & 4 deletions Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class Frontend {
// then try to load the configuration by inferring it based on the source file path.
if let swiftFileURL = swiftFileURL {
do {
if let configuration = try configurationLoader.configuration(forSwiftFileAt: swiftFileURL) {
if let configuration = try configurationLoader.configuration(forPath: swiftFileURL) {
self.checkForUnrecognizedRules(in: configuration)
return configuration
}
Expand All @@ -223,11 +223,26 @@ class Frontend {
"Unable to read configuration for \(swiftFileURL.path): \(error.localizedDescription)")
return nil
}
} else {
// If reading from stdin and no explicit configuration file was given,
// walk up the file tree from the cwd to find a config.

let cwd = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
// Definitely a Swift file. Definitely not a directory. Shhhhhh.
do {
if let configuration = try configurationLoader.configuration(forPath: cwd) {
self.checkForUnrecognizedRules(in: configuration)
return configuration
}
} catch {
diagnosticsEngine.emitError(
"Unable to read configuration for \(cwd): \(error.localizedDescription)")
return nil
}
}

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

Expand Down

0 comments on commit daf7e7d

Please sign in to comment.