Skip to content

Commit 4eefa53

Browse files
committed
[config] Search in OS config directories
Searches for: - ~/Library/Application Support/swift-format/config.json - $XDG_CONFIG_HOME/swift-format/config.json - ~/.config/swift-format/config.json
1 parent 4f19acc commit 4eefa53

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

Sources/swift-format/Frontend/Frontend.swift

+54-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ class Frontend {
181181
/// it was provided, or by searching in paths inferred by `swiftFilePath` if one exists, or the
182182
/// default configuration otherwise. If an error occurred when reading the configuration, a
183183
/// diagnostic is emitted and `nil` is returned. If neither `pathOrString` nor `swiftFilePath`
184-
/// were provided, a default `Configuration()` will be returned.
184+
/// were provided, a configuration is searched at the current working directory or upwards the
185+
/// path. Next the configuration is searched for at the OS default config locations as
186+
/// swift-format/config.json. Finally the default `Configuration()` will be returned.
185187
private func configuration(
186188
fromPathOrString pathOrString: String?,
187189
orInferredFromSwiftFileAt swiftFileURL: URL?
@@ -241,6 +243,57 @@ class Frontend {
241243
}
242244
}
243245

246+
// Load global configuration file
247+
// First URLs are created, then they are queried. First match is loaded
248+
var configLocations: [URL] = []
249+
250+
if #available(macOS 13.0, iOS 16.0, *) {
251+
// From "~/Library/Application Support/" directory
252+
configLocations.append(URL.applicationSupportDirectory)
253+
// From $XDG_CONFIG_HOME directory
254+
if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {
255+
configLocations.append(URL(filePath: xdgConfig, directoryHint: .isDirectory))
256+
}
257+
// From "~/.config/" directory
258+
var dotconfig: URL = URL.homeDirectory
259+
dotconfig.append(component: ".config", directoryHint: .isDirectory)
260+
configLocations.append(dotconfig)
261+
} else {
262+
// From "~/Library/Application Support/" directory
263+
var appSupport: URL = FileManager.default.homeDirectoryForCurrentUser
264+
appSupport.appendPathComponent("Library", isDirectory: true)
265+
appSupport.appendPathComponent("Application Support", isDirectory: true)
266+
configLocations.append(appSupport)
267+
// From $XDG_CONFIG_HOME directory
268+
if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {
269+
configLocations.append(URL(fileURLWithPath: xdgConfig))
270+
}
271+
// From "~/.config/" directory
272+
var dotconfig: URL = FileManager.default.homeDirectoryForCurrentUser
273+
dotconfig.appendPathComponent(".config")
274+
configLocations.append(dotconfig)
275+
}
276+
277+
for var location: URL in configLocations {
278+
if #available(macOS 13.0, iOS 16.0, *) {
279+
location.append(components: "swift-format", "config.json")
280+
} else {
281+
location.appendPathComponent("swift-format", isDirectory: true)
282+
location.appendPathComponent("config.json", isDirectory: false)
283+
}
284+
if FileManager.default.fileExists(atPath: location.path) {
285+
do {
286+
let configuration = try configurationLoader.configuration(at: location)
287+
self.checkForUnrecognizedRules(in: configuration)
288+
return configuration
289+
} catch {
290+
diagnosticsEngine.emitError(
291+
"Unable to read configuration for \(location.path): \(error.localizedDescription)")
292+
return nil
293+
}
294+
}
295+
}
296+
244297
// An explicit configuration has not been given, and one cannot be found.
245298
// Return the default configuration.
246299
return Configuration()

0 commit comments

Comments
 (0)