Skip to content

Commit 31460eb

Browse files
committed
adjust order of searching config directories
1 parent 46b1d47 commit 31460eb

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
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

+38-32
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
@@ -108,9 +108,10 @@ class Frontend {
108108

109109
/// Processes source content from standard input.
110110
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)
114115
else {
115116
// Already diagnosed in the called method.
116117
return
@@ -245,36 +246,40 @@ class Frontend {
245246

246247
// Load global configuration file
247248
// 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))
256254
}
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))
270257
}
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
276281

277-
for var location: URL in configLocations {
282+
for case var location? in configLocations {
278283
if #available(macOS 13.0, iOS 16.0, *) {
279284
location.append(components: "swift-format", "config.json")
280285
} else {
@@ -307,7 +312,8 @@ class Frontend {
307312
// That way they will be printed out, but we'll continue execution on the valid rules.
308313
let invalidRules = configuration.rules.filter { !RuleRegistry.rules.keys.contains($0.key) }
309314
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)
311317
}
312318
}
313319
}

0 commit comments

Comments
 (0)