From f0770713b310293d60b4c00170eda51fd8e80677 Mon Sep 17 00:00:00 2001 From: Josh Wisenbaker Date: Fri, 6 Dec 2024 15:00:30 -0500 Subject: [PATCH 1/3] PathCchIsRoot PathCchIsRoot PathCchIsRoot Updated isRoot extension. Now that https://github.com/swiftlang/swift-foundation/issues/976 and https://github.com/swiftlang/swift-foundation/issues/980 are fixed we can clean this code up a bit by removing the empty path check on Linux and by using the native `PathCchIsRoot` on Windows. Existing code is retained for toolchains <6.1 --- .../SwiftFormat/Utilities/URL+isRoot.swift | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftFormat/Utilities/URL+isRoot.swift b/Sources/SwiftFormat/Utilities/URL+isRoot.swift index 1ad521c7..6a852288 100644 --- a/Sources/SwiftFormat/Utilities/URL+isRoot.swift +++ b/Sources/SwiftFormat/Utilities/URL+isRoot.swift @@ -12,10 +12,32 @@ 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 == "/" { @@ -23,10 +45,11 @@ extension URL { 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) } } From a652b9dbc5929f9ec10e6bdd73ca84f7243f1804 Mon Sep 17 00:00:00 2001 From: Rauhul Varma Date: Tue, 10 Dec 2024 21:39:51 -0800 Subject: [PATCH 2/3] Fix comment length computation Resolved an issue where "end of line" comments matching the configured line length were incorrectly flagged as exceeding it. --- Sources/SwiftFormat/PrettyPrint/Comment.swift | 2 +- .../PrettyPrint/CommentTests.swift | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftFormat/PrettyPrint/Comment.swift b/Sources/SwiftFormat/PrettyPrint/Comment.swift index 3dff9480..43616a5b 100644 --- a/Sources/SwiftFormat/PrettyPrint/Comment.swift +++ b/Sources/SwiftFormat/PrettyPrint/Comment.swift @@ -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 diff --git a/Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift b/Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift index 99b2259e..b3234274 100644 --- a/Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift +++ b/Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift @@ -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 = From 6566d61233e9065d6407ff17d2e028bab09f8d5e Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Wed, 11 Dec 2024 16:51:24 -0800 Subject: [PATCH 3/3] Fix build warning in tests --- Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift b/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift index 4146ef51..6c15e6f4 100644 --- a/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift +++ b/Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift @@ -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