From 7b9586c5ab5e890af5af4456ba14e5aa8515efa2 Mon Sep 17 00:00:00 2001 From: WuerfelDev Date: Fri, 8 Nov 2024 11:05:30 +0100 Subject: [PATCH] adjust order of searching config directories --- Documentation/Configuration.md | 12 +++- Sources/swift-format/Frontend/Frontend.swift | 63 +++++++++++--------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/Documentation/Configuration.md b/Documentation/Configuration.md index 6de4c3b5e..198406679 100644 --- a/Documentation/Configuration.md +++ b/Documentation/Configuration.md @@ -121,14 +121,20 @@ You can also run this command to see the list of rules in the default ## Global Configuration If no `.swift-format` can be found for the current project/file, the configuration directories -are searched for a `swift-format/config.json` file. While the filename is different,the +are searched for a `swift-format/config.json` file. While the filename is different, the configuration format stays the same. Locations that are searched, in this order: -- `~/Library/Application Support/swift-format/config.json` - `$XDG_CONFIG_HOME/swift-format/config.json` -- `~/.config/swift-format/config.json` +- `$HOME/Library/Application Support/swift-format/config.json` +- each path in `$XDG_CONFIG_DIRS` (system wide configuration) +- `/Library/Application Support/swift-format/config.json` (system wide configuration) + +or on windows: + +- `%LOCALAPPDATA%/swift-format/config.json` +- `%PROGRAMDATA%/swift-format/config.json` (system wide configuration) ## API Configuration diff --git a/Sources/swift-format/Frontend/Frontend.swift b/Sources/swift-format/Frontend/Frontend.swift index 5ef514c0e..7e1943de5 100644 --- a/Sources/swift-format/Frontend/Frontend.swift +++ b/Sources/swift-format/Frontend/Frontend.swift @@ -12,8 +12,8 @@ import Foundation @_spi(Internal) import SwiftFormat -import SwiftSyntax import SwiftParser +import SwiftSyntax class Frontend { /// Represents a file to be processed by the frontend and any file-specific options associated @@ -245,36 +245,40 @@ class Frontend { // Load global configuration file // First URLs are created, then they are queried. First match is loaded - var configLocations: [URL] = [] - - if #available(macOS 13.0, iOS 16.0, *) { - // From "~/Library/Application Support/" directory - configLocations.append(URL.applicationSupportDirectory) - // From $XDG_CONFIG_HOME directory - if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] { - configLocations.append(URL(filePath: xdgConfig, directoryHint: .isDirectory)) + var configLocations: [URL?] = [] + + #if os(Windows) + if let localAppData = ProcessInfo.processInfo.environment["LOCALAPPDATA"] { + configLocations.append(URL(fileURLWithPath: localAppData)) } - // From "~/.config/" directory - var dotconfig: URL = URL.homeDirectory - dotconfig.append(component: ".config", directoryHint: .isDirectory) - configLocations.append(dotconfig) - } else { - // From "~/Library/Application Support/" directory - var appSupport: URL = FileManager.default.homeDirectoryForCurrentUser - appSupport.appendPathComponent("Library", isDirectory: true) - appSupport.appendPathComponent("Application Support", isDirectory: true) - configLocations.append(appSupport) - // From $XDG_CONFIG_HOME directory - if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] { - configLocations.append(URL(fileURLWithPath: xdgConfig)) + if let programData = ProcessInfo.processInfo.environment["PROGRAMDATA"] { + configLocations.append(URL(fileURLWithPath: programData)) } - // From "~/.config/" directory - var dotconfig: URL = FileManager.default.homeDirectoryForCurrentUser - dotconfig.appendPathComponent(".config") - configLocations.append(dotconfig) - } + #else + if let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] { + configLocations.append(URL(fileURLWithPath: xdgConfigHome)) + } + + if let libraryUrl = FileManager.default.urls( + for: .applicationSupportDirectory, in: .userDomainMask + ).first { + configLocations.append(libraryUrl) + } + + if let xdgConfigDirs = ProcessInfo.processInfo.environment["XDG_CONFIG_DIRS"] { + configLocations += xdgConfigDirs.split(separator: ":").map { xdgConfigDir in + URL(fileURLWithPath: String(xdgConfigDir)) + } + } + + if let libraryUrl = FileManager.default.urls( + for: .applicationSupportDirectory, in: .systemDomainMask + ).first { + configLocations.append(libraryUrl) + } + #endif - for var location: URL in configLocations { + for case var location? in configLocations { if #available(macOS 13.0, iOS 16.0, *) { location.append(components: "swift-format", "config.json") } else { @@ -307,7 +311,8 @@ class Frontend { // That way they will be printed out, but we'll continue execution on the valid rules. let invalidRules = configuration.rules.filter { !RuleRegistry.rules.keys.contains($0.key) } for rule in invalidRules { - diagnosticsEngine.emitWarning("Configuration contains an unrecognized rule: \(rule.key)", location: nil) + diagnosticsEngine.emitWarning( + "Configuration contains an unrecognized rule: \(rule.key)", location: nil) } } }