Skip to content

Commit

Permalink
adjust order of searching config directories
Browse files Browse the repository at this point in the history
  • Loading branch information
WuerfelDev committed Nov 8, 2024
1 parent 46b1d47 commit 7b9586c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 32 deletions.
12 changes: 9 additions & 3 deletions Documentation/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
63 changes: 34 additions & 29 deletions Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 7b9586c

Please sign in to comment.