Skip to content

Commit 26930a6

Browse files
authored
Merge pull request #923 from andre-richter/dump-effective-conf
Add dump-effective-configuration subcommand
2 parents de99480 + 11d6de8 commit 26930a6

14 files changed

+274
-175
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,19 @@ settings in the default configuration can be viewed by running
210210
`swift-format dump-configuration`, which will dump it to standard
211211
output.
212212

213-
If the `--configuration <file>` option is passed to `swift-format`, then that
214-
configuration will be used unconditionally and the file system will not be
215-
searched.
213+
If the `--configuration <configuration>` option is passed to `swift-format`,
214+
then that configuration will be used unconditionally and the file system will
215+
not be searched.
216216

217217
See [Documentation/Configuration.md](Documentation/Configuration.md) for a
218-
description of the configuration file format and the settings that are
219-
available.
218+
description of the configuration format and the settings that are available.
219+
220+
#### Viewing the Effective Configuration
221+
222+
The `dump-configuration` subcommand accepts a `--effective` flag. If set, it
223+
dumps the configuration that would be used if `swift-format` was executed from
224+
the current working directory, and accounts for `.swift-format` files or
225+
`--configuration` options as outlined above.
220226

221227
### Miscellaneous
222228

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
15+
extension Configuration {
16+
/// Return the configuration as a JSON string.
17+
public func asJsonString() throws -> String {
18+
let data: Data
19+
20+
do {
21+
let encoder = JSONEncoder()
22+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
23+
data = try encoder.encode(self)
24+
} catch {
25+
throw SwiftFormatError.configurationDumpFailed("\(error)")
26+
}
27+
28+
guard let jsonString = String(data: data, encoding: .utf8) else {
29+
// This should never happen, but let's make sure we fail more gracefully than crashing, just in case.
30+
throw SwiftFormatError.configurationDumpFailed("The JSON was not valid UTF-8")
31+
}
32+
33+
return jsonString
34+
}
35+
}

Sources/SwiftFormat/API/SwiftFormatError.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public enum SwiftFormatError: LocalizedError {
2828
/// The requested experimental feature name was not recognized by the parser.
2929
case unrecognizedExperimentalFeature(String)
3030

31+
/// An error happened while dumping the tool's configuration.
32+
case configurationDumpFailed(String)
33+
3134
public var errorDescription: String? {
3235
switch self {
3336
case .fileNotReadable:
@@ -38,6 +41,8 @@ public enum SwiftFormatError: LocalizedError {
3841
return "file contains invalid Swift syntax"
3942
case .unrecognizedExperimentalFeature(let name):
4043
return "experimental feature '\(name)' was not recognized by the Swift parser"
44+
case .configurationDumpFailed(let message):
45+
return "dumping configuration failed: \(message)"
4146
}
4247
}
4348
}

Sources/SwiftFormat/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#[[
22
This source file is part of the swift-format open source project
33
4-
Copyright (c) 2024 Apple Inc. and the swift-format project authors
4+
Copyright (c) 2024 - 2025 Apple Inc. and the swift-format project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66
77
See https://swift.org/LICENSE.txt for license information
88
#]]
99

1010
add_library(SwiftFormat
1111
API/Configuration+Default.swift
12+
API/Configuration+Dump.swift
1213
API/Configuration.swift
1314
API/DebugOptions.swift
1415
API/Finding.swift

Sources/swift-format/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[[
22
This source file is part of the swift-format open source project
33
4-
Copyright (c) 2024 Apple Inc. and the swift-format project authors
4+
Copyright (c) 2024 - 2025 Apple Inc. and the swift-format project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66
77
See https://swift.org/LICENSE.txt for license information
@@ -15,6 +15,7 @@ add_executable(swift-format
1515
Frontend/FormatFrontend.swift
1616
Frontend/Frontend.swift
1717
Frontend/LintFrontend.swift
18+
Subcommands/ConfigurationOptions.swift
1819
Subcommands/DumpConfiguration.swift
1920
Subcommands/Format.swift
2021
Subcommands/Lint.swift
@@ -23,7 +24,6 @@ add_executable(swift-format
2324
Utilities/Diagnostic.swift
2425
Utilities/DiagnosticsEngine.swift
2526
Utilities/FileHandleTextOutputStream.swift
26-
Utilities/FormatError.swift
2727
Utilities/StderrDiagnosticPrinter.swift
2828
Utilities/TTY.swift)
2929
target_link_libraries(swift-format PRIVATE

Sources/swift-format/Frontend/FormatFrontend.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -20,9 +20,9 @@ class FormatFrontend: Frontend {
2020
/// Whether or not to format the Swift file in-place.
2121
private let inPlace: Bool
2222

23-
init(lintFormatOptions: LintFormatOptions, inPlace: Bool) {
23+
init(configurationOptions: ConfigurationOptions, lintFormatOptions: LintFormatOptions, inPlace: Bool) {
2424
self.inPlace = inPlace
25-
super.init(lintFormatOptions: lintFormatOptions)
25+
super.init(configurationOptions: configurationOptions, lintFormatOptions: lintFormatOptions)
2626
}
2727

2828
override func processFile(_ fileToProcess: FileToProcess) {

0 commit comments

Comments
 (0)