Skip to content

Commit f410c3c

Browse files
authored
Merge pull request #898 from ahoppen/ahoppen/merge-main-2024-12-12
Merge `main` into `release/6.1`
2 parents a978bf3 + 84c0b4e commit f410c3c

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

Sources/SwiftFormat/PrettyPrint/Comment.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ struct Comment {
6767

6868
switch kind {
6969
case .line, .docLine:
70+
self.length = text.count
7071
self.text = [text]
7172
self.text[0].removeFirst(kind.prefixLength)
72-
self.length = self.text.reduce(0, { $0 + $1.count + kind.prefixLength + 1 })
7373

7474
case .block, .docBlock:
7575
var fulltext: String = text

Sources/SwiftFormat/Utilities/URL+isRoot.swift

+27-4
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,44 @@
1212

1313
import Foundation
1414

15+
#if os(Windows)
16+
import WinSDK
17+
#endif
18+
1519
extension URL {
20+
/// Returns a `Bool` to indicate if the given `URL` leads to the root of a filesystem.
21+
/// A non-filesystem type `URL` will always return false.
1622
@_spi(Testing) public var isRoot: Bool {
23+
guard isFileURL else { return false }
24+
25+
#if compiler(>=6.1)
26+
#if os(Windows)
27+
let filePath = self.withUnsafeFileSystemRepresentation { pointer in
28+
guard let pointer else {
29+
return ""
30+
}
31+
return String(cString: pointer)
32+
}
33+
return filePath.withCString(encodedAs: UTF16.self, PathCchIsRoot)
34+
#else // os(Windows)
35+
return self.path == "/"
36+
#endif // os(Windows)
37+
#else // compiler(>=6.1)
38+
1739
#if os(Windows)
18-
// 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.
40+
// This is needed as the fixes from #844 aren't in the Swift 6.0 toolchain.
1941
// https://github.com/swiftlang/swift-format/issues/844
2042
var pathComponents = self.pathComponents
2143
if pathComponents.first == "/" {
2244
// Canonicalize `/C:/` to `C:/`.
2345
pathComponents = Array(pathComponents.dropFirst())
2446
}
2547
return pathComponents.count <= 1
26-
#else
48+
#else // os(Windows)
2749
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
28-
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
50+
// This is needed as the fixes from #980 aren't in the Swift 6.0 toolchain.
2951
return self.path == "/" || self.path == ""
30-
#endif
52+
#endif // os(Windows)
53+
#endif // compiler(>=6.1)
3154
}
3255
}

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

+23
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,29 @@ final class CommentTests: PrettyPrintTestCase {
10091009
)
10101010
}
10111011

1012+
// Tests that "end of line" comments are flagged only when they exceed the configured line length.
1013+
func testDiagnoseMoveEndOfLineCommentAroundBoundary() {
1014+
assertPrettyPrintEqual(
1015+
input: """
1016+
x // 789
1017+
x // 7890
1018+
x 1️⃣// 78901
1019+
1020+
""",
1021+
expected: """
1022+
x // 789
1023+
x // 7890
1024+
x // 78901
1025+
1026+
""",
1027+
linelength: 10,
1028+
whitespaceOnly: true,
1029+
findings: [
1030+
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length")
1031+
]
1032+
)
1033+
}
1034+
10121035
func testLineWithDocLineComment() {
10131036
// none of these should be merged if/when there is comment formatting
10141037
let input =

Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ final class FileIteratorTests: XCTestCase {
103103
while !root.isRoot {
104104
root.deleteLastPathComponent()
105105
}
106-
var rootPath = root.path
107106
#if os(Windows) && compiler(<6.1)
107+
var rootPath = root.path
108108
if rootPath.hasPrefix("/") {
109109
// Canonicalize /C: to C:
110110
rootPath = String(rootPath.dropFirst())
111111
}
112+
#else
113+
let rootPath = root.path
112114
#endif
113115
// Make sure that we don't drop the beginning of the path if we are running in root.
114116
// https://github.com/swiftlang/swift-format/issues/862

0 commit comments

Comments
 (0)