Skip to content

Commit 26c6460

Browse files
committed
Enhance comment trimming and update test cases
1 parent 085f207 commit 26c6460

File tree

3 files changed

+219
-311
lines changed

3 files changed

+219
-311
lines changed

Sources/SwiftSyntax/Trivia.swift

+6-46
Original file line numberDiff line numberDiff line change
@@ -43,65 +43,25 @@ public struct Trivia: Sendable {
4343
}
4444

4545
/// The string contents of all the comment pieces with any comments tokens trimmed.
46-
///
47-
/// Each element in the array is the trimmed contents of a line comment, or, in the case of a multi-line comment a trimmed, concatenated single string.
48-
public var commentValues: [String] {
46+
public var commentValue: String {
4947
var comments = [String]()
50-
var partialComments = [String]()
51-
52-
var foundStartOfCodeBlock = false
53-
var foundEndOfCodeBlock = false
54-
var isInCodeBlock: Bool { foundStartOfCodeBlock && !foundEndOfCodeBlock }
5548

5649
for piece in pieces {
5750
switch piece {
5851
case .blockComment(let text), .docBlockComment(let text):
59-
let text = text.trimmingCharacters(in: "\n")
60-
61-
foundStartOfCodeBlock = text.hasPrefix("/*")
62-
foundEndOfCodeBlock = text.hasSuffix("*/")
63-
64-
let sanitized =
65-
text
66-
.split(separator: "\n")
67-
.map { $0.trimmingAnyCharacters(in: "/*").trimmingAnyCharacters(in: " ") }
68-
.filter { !$0.isEmpty }
69-
.joined(separator: " ")
70-
71-
appendPartialCommentIfPossible(sanitized)
52+
let text = text.trimmingCharacters(in: "/* ").trimmingAnyCharacters(in: ["\n"])
53+
comments.append(String(text))
7254

7355
case .lineComment(let text), .docLineComment(let text):
74-
if isInCodeBlock {
75-
appendPartialCommentIfPossible(text)
76-
} else {
77-
comments.append(String(text.trimmingPrefix("/ ")))
78-
}
56+
let text = text.trimmingPrefix("/ ")
57+
comments.append(String(text))
7958

8059
default:
8160
break
8261
}
83-
84-
if foundEndOfCodeBlock, !partialComments.isEmpty {
85-
appendSubstringsToLines()
86-
partialComments.removeAll()
87-
}
88-
}
89-
90-
if !partialComments.isEmpty {
91-
appendSubstringsToLines()
92-
}
93-
94-
func appendPartialCommentIfPossible(_ text: String) {
95-
guard partialComments.isEmpty || !text.isEmpty else { return }
96-
97-
partialComments.append(text)
98-
}
99-
100-
func appendSubstringsToLines() {
101-
comments.append(partialComments.joined(separator: " "))
10262
}
10363

104-
return comments
64+
return comments.joined(separator: "\n")
10565
}
10666

10767
/// The length of all the pieces in this ``Trivia``.

Sources/SwiftSyntax/Utils.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,4 @@ private func countOfSequentialCharacters(
147147
}
148148

149149
return count
150-
}
150+
}

0 commit comments

Comments
 (0)