Skip to content

Add common configuration for swift-format #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 13 additions & 13 deletions Sources/_OpenAPIGeneratorCore/Extensions/SwiftFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftFormat
import SwiftFormatConfiguration

extension String {
private static let configurationURL = {
let configurationFilePath = #file
.components(separatedBy: "/")
.prefix(while: { $0 != "Sources" })
.joined(separator: "/")
.appending("/.swift-format")
Comment on lines +20 to +24
Copy link
Contributor Author

@denil-ct denil-ct Jun 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is quite susceptible to be triggered by other folders that have a Sources in their name. I started with it because it seemed the simplest. What is your opinion, am I being overly cautious here?

Maybe something like this might work better as it narrows down the search criteria?

Suggested change
let configurationFilePath = #file
.components(separatedBy: "/")
.prefix(while: { $0 != "Sources" })
.joined(separator: "/")
.appending("/.swift-format")
let configurationFilePath = #file
.components(separatedBy: "swift-openapi-generator/Sources")[0]
.appending("swift-openapi-generator/.swift-format")

Or is there some other way of getting the file location, that I am missing?

guard #available(macOS 13.0, *) else {
return URL(fileURLWithPath: configurationFilePath)
}
return URL(filePath: configurationFilePath)
}()
/// A copy of the string formatted using swift-format.
var swiftFormatted: Self {
get throws {
var formattedString = ""
// TODO: Should be loaded from a swift-format file that we also use to format our own code.
var configuration = Configuration()
configuration.rules["OrderedImports"] = false
configuration.rules["NoAccessLevelOnExtensionDeclaration"] = false
configuration.rules["UseLetInEveryBoundCaseVariable"] = false
configuration.indentation = .spaces(4)
configuration.respectsExistingLineBreaks = false
configuration.lineBreakBeforeEachArgument = true
configuration.lineBreakBeforeControlFlowKeywords = false
configuration.lineBreakBeforeEachGenericRequirement = true
configuration.lineBreakAroundMultilineExpressionChainComponents = true
configuration.indentConditionalCompilationBlocks = false
configuration.maximumBlankLines = 0
let configuration = try Configuration(contentsOf: Self.configurationURL)
let formatter = SwiftFormatter(configuration: configuration)
try formatter.format(
source: self,
Expand Down