Skip to content

Commit 7b9586c

Browse files
committed
adjust order of searching config directories
1 parent 46b1d47 commit 7b9586c

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

Documentation/Configuration.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,20 @@ You can also run this command to see the list of rules in the default
121121
## Global Configuration
122122

123123
If no `.swift-format` can be found for the current project/file, the configuration directories
124-
are searched for a `swift-format/config.json` file. While the filename is different,the
124+
are searched for a `swift-format/config.json` file. While the filename is different, the
125125
configuration format stays the same.
126126

127127
Locations that are searched, in this order:
128128

129-
- `~/Library/Application Support/swift-format/config.json`
130129
- `$XDG_CONFIG_HOME/swift-format/config.json`
131-
- `~/.config/swift-format/config.json`
130+
- `$HOME/Library/Application Support/swift-format/config.json`
131+
- each path in `$XDG_CONFIG_DIRS` (system wide configuration)
132+
- `/Library/Application Support/swift-format/config.json` (system wide configuration)
133+
134+
or on windows:
135+
136+
- `%LOCALAPPDATA%/swift-format/config.json`
137+
- `%PROGRAMDATA%/swift-format/config.json` (system wide configuration)
132138

133139
## API Configuration
134140

Sources/swift-format/Frontend/Frontend.swift

+34-29
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import Foundation
1414
@_spi(Internal) import SwiftFormat
15-
import SwiftSyntax
1615
import SwiftParser
16+
import SwiftSyntax
1717

1818
class Frontend {
1919
/// Represents a file to be processed by the frontend and any file-specific options associated
@@ -245,36 +245,40 @@ class Frontend {
245245

246246
// Load global configuration file
247247
// 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))
248+
var configLocations: [URL?] = []
249+
250+
#if os(Windows)
251+
if let localAppData = ProcessInfo.processInfo.environment["LOCALAPPDATA"] {
252+
configLocations.append(URL(fileURLWithPath: localAppData))
256253
}
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))
254+
if let programData = ProcessInfo.processInfo.environment["PROGRAMDATA"] {
255+
configLocations.append(URL(fileURLWithPath: programData))
270256
}
271-
// From "~/.config/" directory
272-
var dotconfig: URL = FileManager.default.homeDirectoryForCurrentUser
273-
dotconfig.appendPathComponent(".config")
274-
configLocations.append(dotconfig)
275-
}
257+
#else
258+
if let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {
259+
configLocations.append(URL(fileURLWithPath: xdgConfigHome))
260+
}
261+
262+
if let libraryUrl = FileManager.default.urls(
263+
for: .applicationSupportDirectory, in: .userDomainMask
264+
).first {
265+
configLocations.append(libraryUrl)
266+
}
267+
268+
if let xdgConfigDirs = ProcessInfo.processInfo.environment["XDG_CONFIG_DIRS"] {
269+
configLocations += xdgConfigDirs.split(separator: ":").map { xdgConfigDir in
270+
URL(fileURLWithPath: String(xdgConfigDir))
271+
}
272+
}
273+
274+
if let libraryUrl = FileManager.default.urls(
275+
for: .applicationSupportDirectory, in: .systemDomainMask
276+
).first {
277+
configLocations.append(libraryUrl)
278+
}
279+
#endif
276280

277-
for var location: URL in configLocations {
281+
for case var location? in configLocations {
278282
if #available(macOS 13.0, iOS 16.0, *) {
279283
location.append(components: "swift-format", "config.json")
280284
} else {
@@ -307,7 +311,8 @@ class Frontend {
307311
// That way they will be printed out, but we'll continue execution on the valid rules.
308312
let invalidRules = configuration.rules.filter { !RuleRegistry.rules.keys.contains($0.key) }
309313
for rule in invalidRules {
310-
diagnosticsEngine.emitWarning("Configuration contains an unrecognized rule: \(rule.key)", location: nil)
314+
diagnosticsEngine.emitWarning(
315+
"Configuration contains an unrecognized rule: \(rule.key)", location: nil)
311316
}
312317
}
313318
}

0 commit comments

Comments
 (0)