Skip to content

Commit

Permalink
Find config based on cwd when reading from stdin
Browse files Browse the repository at this point in the history
This change is useful for formatting clients that prefer to format via
stdio. The signature of
`ConfigurationLoader.configuration(forSwiftFileAt:URL)` was changed to
`configuration(forPath:URL)` since the function is useful not just for
Swift files, but any arbitrary file path.

Signed-off-by: Julia DeMille <me@jdemille.com>
  • Loading branch information
judemille committed Jan 25, 2024
1 parent 4d634f2 commit f7a2dac
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(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 f7a2dac

Please sign in to comment.