Skip to content

Commit

Permalink
Merge pull request #898 from ahoppen/ahoppen/merge-main-2024-12-12
Browse files Browse the repository at this point in the history
Merge `main` into `release/6.1`
  • Loading branch information
ahoppen authored Dec 12, 2024
2 parents a978bf3 + 84c0b4e commit f410c3c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/PrettyPrint/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ struct Comment {

switch kind {
case .line, .docLine:
self.length = text.count
self.text = [text]
self.text[0].removeFirst(kind.prefixLength)
self.length = self.text.reduce(0, { $0 + $1.count + kind.prefixLength + 1 })

case .block, .docBlock:
var fulltext: String = text
Expand Down
31 changes: 27 additions & 4 deletions Sources/SwiftFormat/Utilities/URL+isRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,44 @@

import Foundation

#if os(Windows)
import WinSDK
#endif

extension URL {
/// Returns a `Bool` to indicate if the given `URL` leads to the root of a filesystem.
/// A non-filesystem type `URL` will always return false.
@_spi(Testing) public var isRoot: Bool {
guard isFileURL else { return false }

#if compiler(>=6.1)
#if os(Windows)
let filePath = self.withUnsafeFileSystemRepresentation { pointer in
guard let pointer else {
return ""
}
return String(cString: pointer)
}
return filePath.withCString(encodedAs: UTF16.self, PathCchIsRoot)
#else // os(Windows)
return self.path == "/"
#endif // os(Windows)
#else // compiler(>=6.1)

#if os(Windows)
// FIXME: We should call into Windows' native check to check if this path is a root once https://github.com/swiftlang/swift-foundation/issues/976 is fixed.
// This is needed as the fixes from #844 aren't in the Swift 6.0 toolchain.
// https://github.com/swiftlang/swift-format/issues/844
var pathComponents = self.pathComponents
if pathComponents.first == "/" {
// Canonicalize `/C:/` to `C:/`.
pathComponents = Array(pathComponents.dropFirst())
}
return pathComponents.count <= 1
#else
#else // os(Windows)
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
// This is needed as the fixes from #980 aren't in the Swift 6.0 toolchain.
return self.path == "/" || self.path == ""
#endif
#endif // os(Windows)
#endif // compiler(>=6.1)
}
}
23 changes: 23 additions & 0 deletions Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,29 @@ final class CommentTests: PrettyPrintTestCase {
)
}

// Tests that "end of line" comments are flagged only when they exceed the configured line length.
func testDiagnoseMoveEndOfLineCommentAroundBoundary() {
assertPrettyPrintEqual(
input: """
x // 789
x // 7890
x 1️⃣// 78901
""",
expected: """
x // 789
x // 7890
x // 78901
""",
linelength: 10,
whitespaceOnly: true,
findings: [
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
]
)
}

func testLineWithDocLineComment() {
// none of these should be merged if/when there is comment formatting
let input =
Expand Down
4 changes: 3 additions & 1 deletion Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ final class FileIteratorTests: XCTestCase {
while !root.isRoot {
root.deleteLastPathComponent()
}
var rootPath = root.path
#if os(Windows) && compiler(<6.1)
var rootPath = root.path
if rootPath.hasPrefix("/") {
// Canonicalize /C: to C:
rootPath = String(rootPath.dropFirst())
}
#else
let rootPath = root.path
#endif
// Make sure that we don't drop the beginning of the path if we are running in root.
// https://github.com/swiftlang/swift-format/issues/862
Expand Down

0 comments on commit f410c3c

Please sign in to comment.