Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,19 @@ settings in the default configuration can be viewed by running
`swift-format dump-configuration`, which will dump it to standard
output.

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

See [Documentation/Configuration.md](Documentation/Configuration.md) for a
description of the configuration file format and the settings that are
available.
description of the configuration format and the settings that are available.

#### Viewing the Effective Configuration

The `dump-configuration` subcommand accepts a `--effective` flag. If set, it
dumps the configuration that would be used if `swift-format` was executed from
the current working directory, and accounts for `.swift-format` files or
`--configuration` options as outlined above.

### Miscellaneous

Expand Down
35 changes: 35 additions & 0 deletions Sources/SwiftFormat/API/Configuration+Dump.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation

extension Configuration {
/// Return the configuration as a JSON string.
public func asJsonString() throws -> String {
let data: Data

do {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
data = try encoder.encode(self)
} catch {
throw SwiftFormatError.configurationDumpFailed("\(error)")
}

guard let jsonString = String(data: data, encoding: .utf8) else {
// This should never happen, but let's make sure we fail more gracefully than crashing, just in case.
throw SwiftFormatError.configurationDumpFailed("The JSON was not valid UTF-8")
}

return jsonString
}
}
5 changes: 5 additions & 0 deletions Sources/SwiftFormat/API/SwiftFormatError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public enum SwiftFormatError: LocalizedError {
/// The requested experimental feature name was not recognized by the parser.
case unrecognizedExperimentalFeature(String)

/// An error happened while dumping the tool's configuration.
case configurationDumpFailed(String)

public var errorDescription: String? {
switch self {
case .fileNotReadable:
Expand All @@ -38,6 +41,8 @@ public enum SwiftFormatError: LocalizedError {
return "file contains invalid Swift syntax"
case .unrecognizedExperimentalFeature(let name):
return "experimental feature '\(name)' was not recognized by the Swift parser"
case .configurationDumpFailed(let message):
return "dumping configuration failed: \(message)"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftFormat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Copyright (c) 2024 - 2025 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

add_library(SwiftFormat
API/Configuration+Default.swift
API/Configuration+Dump.swift
API/Configuration.swift
API/DebugOptions.swift
API/Finding.swift
Expand Down
4 changes: 2 additions & 2 deletions Sources/swift-format/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Copyright (c) 2024 - 2025 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
Expand All @@ -15,6 +15,7 @@ add_executable(swift-format
Frontend/FormatFrontend.swift
Frontend/Frontend.swift
Frontend/LintFrontend.swift
Subcommands/ConfigurationOptions.swift
Subcommands/DumpConfiguration.swift
Subcommands/Format.swift
Subcommands/Lint.swift
Expand All @@ -23,7 +24,6 @@ add_executable(swift-format
Utilities/Diagnostic.swift
Utilities/DiagnosticsEngine.swift
Utilities/FileHandleTextOutputStream.swift
Utilities/FormatError.swift
Utilities/StderrDiagnosticPrinter.swift
Utilities/TTY.swift)
target_link_libraries(swift-format PRIVATE
Expand Down
6 changes: 3 additions & 3 deletions Sources/swift-format/Frontend/FormatFrontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -20,9 +20,9 @@ class FormatFrontend: Frontend {
/// Whether or not to format the Swift file in-place.
private let inPlace: Bool

init(lintFormatOptions: LintFormatOptions, inPlace: Bool) {
init(configurationOptions: ConfigurationOptions, lintFormatOptions: LintFormatOptions, inPlace: Bool) {
self.inPlace = inPlace
super.init(lintFormatOptions: lintFormatOptions)
super.init(configurationOptions: configurationOptions, lintFormatOptions: lintFormatOptions)
}

override func processFile(_ fileToProcess: FileToProcess) {
Expand Down
Loading