Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d1d4020

Browse files
committedMar 29, 2024
Added commentValues on Trivia
This holds an array of comments made from the trivia's pieces, with one line per element of the array
1 parent cfd0487 commit d1d4020

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
 

‎Sources/SwiftSyntax/Trivia.swift

+31
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public struct Trivia: Sendable {
4242
pieces.isEmpty
4343
}
4444

45+
/// 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] {
49+
pieces.compactMap {
50+
switch $0 {
51+
case .lineComment(let text), .docLineComment(let text):
52+
sanitizingLineComment(text)
53+
54+
default:
55+
nil
56+
}
57+
}
58+
}
59+
4560
/// The length of all the pieces in this ``Trivia``.
4661
public var sourceLength: SourceLength {
4762
return pieces.map({ $0.sourceLength }).reduce(.zero, +)
@@ -215,3 +230,19 @@ extension RawTriviaPiece: CustomDebugStringConvertible {
215230
TriviaPiece(raw: self).debugDescription
216231
}
217232
}
233+
234+
private func sanitizingLineComment(_ text: String) -> String {
235+
// TODO: adammcarter - can we import Foundation instead and use trimmingCharacters(in:)
236+
237+
var charactersToDrop = 0
238+
239+
for character in text {
240+
if (character == "/" || character == " ") {
241+
charactersToDrop += 1
242+
} else {
243+
break
244+
}
245+
}
246+
247+
return String(text.dropFirst(charactersToDrop))
248+
}

‎Tests/SwiftSyntaxTest/TriviaTests.swift

+82
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,86 @@ class TriviaTests: XCTestCase {
6969
XCTAssertNotEqual(TriviaPiece.unexpectedText("e"), .unexpectedText("f"))
7070
XCTAssertNotEqual(TriviaPiece.unexpectedText("e"), .lineComment("e"))
7171
}
72+
73+
func testTriviaCommentValues() {
74+
XCTAssertTrue(Trivia(pieces: []).commentValues.isEmpty)
75+
76+
// MARK: line comment
77+
78+
XCTAssertEqual(
79+
Trivia(pieces: [.lineComment("")]).commentValues,
80+
[""]
81+
)
82+
83+
XCTAssertEqual(
84+
Trivia(pieces: [.lineComment("Some line comment")]).commentValues,
85+
["Some line comment"]
86+
)
87+
88+
XCTAssertEqual(
89+
Trivia(pieces: [.lineComment("// Some line comment")]).commentValues,
90+
["Some line comment"]
91+
)
92+
93+
XCTAssertEqual(
94+
Trivia(pieces: [
95+
.lineComment("// Some line comment"),
96+
.lineComment("// Another"),
97+
]).commentValues,
98+
[
99+
"Some line comment",
100+
"Another",
101+
]
102+
)
103+
104+
XCTAssertEqual(
105+
Trivia(pieces: [
106+
.lineComment("// Some line comment"),
107+
.lineComment("Other"),
108+
]).commentValues,
109+
[
110+
"Some line comment",
111+
"Other",
112+
]
113+
)
114+
115+
// MARK: doc line comment
116+
117+
XCTAssertEqual(
118+
Trivia(pieces: [.docLineComment("")]).commentValues,
119+
[""]
120+
)
121+
122+
XCTAssertEqual(
123+
Trivia(pieces: [.docLineComment("Some doc line comment")]).commentValues,
124+
["Some doc line comment"]
125+
)
126+
127+
XCTAssertEqual(
128+
Trivia(pieces: [.docLineComment("/// Some doc line comment")]).commentValues,
129+
["Some doc line comment"]
130+
)
131+
132+
XCTAssertEqual(
133+
Trivia(pieces: [
134+
.docLineComment("/// Some doc line comment"),
135+
.docLineComment("/// Another"),
136+
]).commentValues,
137+
[
138+
"Some doc line comment",
139+
"Another",
140+
]
141+
)
142+
143+
XCTAssertEqual(
144+
Trivia(pieces: [
145+
.docLineComment("/// Some doc line comment"),
146+
.docLineComment("Other"),
147+
]).commentValues,
148+
[
149+
"Some doc line comment",
150+
"Other",
151+
]
152+
)
153+
}
72154
}

0 commit comments

Comments
 (0)
Please sign in to comment.