| 
12 | 12 | 
 
  | 
13 | 13 | import Foundation  | 
14 | 14 | @_spi(Internal) import SwiftFormat  | 
15 |  | -import SwiftSyntax  | 
16 | 15 | import SwiftParser  | 
 | 16 | +import SwiftSyntax  | 
17 | 17 | 
 
  | 
18 | 18 | class Frontend {  | 
19 | 19 |   /// Represents a file to be processed by the frontend and any file-specific options associated  | 
@@ -108,9 +108,10 @@ class Frontend {  | 
108 | 108 | 
 
  | 
109 | 109 |   /// Processes source content from standard input.  | 
110 | 110 |   private func processStandardInput() {  | 
111 |  | -    guard let configuration = configuration(  | 
112 |  | -      fromPathOrString: lintFormatOptions.configuration,  | 
113 |  | -      orInferredFromSwiftFileAt: nil)  | 
 | 111 | +    guard  | 
 | 112 | +      let configuration = configuration(  | 
 | 113 | +        fromPathOrString: lintFormatOptions.configuration,  | 
 | 114 | +        orInferredFromSwiftFileAt: nil)  | 
114 | 115 |     else {  | 
115 | 116 |       // Already diagnosed in the called method.  | 
116 | 117 |       return  | 
@@ -245,36 +246,40 @@ class Frontend {  | 
245 | 246 | 
 
  | 
246 | 247 |     // Load global configuration file  | 
247 | 248 |     // 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))  | 
 | 249 | +    var configLocations: [URL?] = []  | 
 | 250 | + | 
 | 251 | +    #if os(Windows)  | 
 | 252 | +      if let localAppData = ProcessInfo.processInfo.environment["LOCALAPPDATA"] {  | 
 | 253 | +        configLocations.append(URL(fileURLWithPath: localAppData))  | 
256 | 254 |       }  | 
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))  | 
 | 255 | +      if let programData = ProcessInfo.processInfo.environment["PROGRAMDATA"] {  | 
 | 256 | +        configLocations.append(URL(fileURLWithPath: programData))  | 
270 | 257 |       }  | 
271 |  | -      // From "~/.config/" directory  | 
272 |  | -      var dotconfig: URL = FileManager.default.homeDirectoryForCurrentUser  | 
273 |  | -      dotconfig.appendPathComponent(".config")  | 
274 |  | -      configLocations.append(dotconfig)  | 
275 |  | -    }  | 
 | 258 | +    #else  | 
 | 259 | +      if let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {  | 
 | 260 | +        configLocations.append(URL(fileURLWithPath: xdgConfigHome))  | 
 | 261 | +      }  | 
 | 262 | + | 
 | 263 | +      if let libraryUrl = FileManager.default.urls(  | 
 | 264 | +        for: .applicationSupportDirectory, in: .userDomainMask  | 
 | 265 | +      ).first {  | 
 | 266 | +        configLocations.append(libraryUrl)  | 
 | 267 | +      }  | 
 | 268 | + | 
 | 269 | +      if let xdgConfigDirs = ProcessInfo.processInfo.environment["XDG_CONFIG_DIRS"] {  | 
 | 270 | +        configLocations += xdgConfigDirs.split(separator: ":").map { xdgConfigDir in  | 
 | 271 | +          URL(fileURLWithPath: String(xdgConfigDir))  | 
 | 272 | +        }  | 
 | 273 | +      }  | 
 | 274 | + | 
 | 275 | +      if let libraryUrl = FileManager.default.urls(  | 
 | 276 | +        for: .applicationSupportDirectory, in: .systemDomainMask  | 
 | 277 | +      ).first {  | 
 | 278 | +        configLocations.append(libraryUrl)  | 
 | 279 | +      }  | 
 | 280 | +    #endif  | 
276 | 281 | 
 
  | 
277 |  | -    for var location: URL in configLocations {  | 
 | 282 | +    for case var location? in configLocations {  | 
278 | 283 |       if #available(macOS 13.0, iOS 16.0, *) {  | 
279 | 284 |         location.append(components: "swift-format", "config.json")  | 
280 | 285 |       } else {  | 
@@ -307,7 +312,8 @@ class Frontend {  | 
307 | 312 |     // That way they will be printed out, but we'll continue execution on the valid rules.  | 
308 | 313 |     let invalidRules = configuration.rules.filter { !RuleRegistry.rules.keys.contains($0.key) }  | 
309 | 314 |     for rule in invalidRules {  | 
310 |  | -      diagnosticsEngine.emitWarning("Configuration contains an unrecognized rule: \(rule.key)", location: nil)  | 
 | 315 | +      diagnosticsEngine.emitWarning(  | 
 | 316 | +        "Configuration contains an unrecognized rule: \(rule.key)", location: nil)  | 
311 | 317 |     }  | 
312 | 318 |   }  | 
313 | 319 | }  | 
0 commit comments