Skip to content

Merge main into release/6.1 #898

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

Merged
merged 7 commits into from
Dec 12, 2024
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
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
Loading