@@ -15,16 +15,7 @@ import SwiftSyntaxBuilder
1515import SyntaxSupport
1616import Utils
1717
18- let triviaFile = SourceFileSyntax ( leadingTrivia: copyrightHeader) {
19- DeclSyntax (
20- """
21- public enum TriviaPosition {
22- case leading
23- case trailing
24- }
25- """
26- )
27-
18+ let triviaPiecesFile = SourceFileSyntax ( leadingTrivia: copyrightHeader) {
2819 try ! EnumDeclSyntax (
2920 """
3021 /// A contiguous stretch of a single kind of trivia. The constituent part of
@@ -115,88 +106,11 @@ let triviaFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
115106 }
116107 }
117108
118- DeclSyntax (
119- """
120- extension TriviaPiece {
121- /// Returns true if the trivia is `.newlines`, `.carriageReturns` or `.carriageReturnLineFeeds`
122- public var isNewline: Bool {
123- switch self {
124- case .newlines,
125- .carriageReturns,
126- .carriageReturnLineFeeds:
127- return true
128- default:
129- return false
130- }
131- }
132- }
133- """
134- )
135-
136- try ! StructDeclSyntax (
109+ try ! ExtensionDeclSyntax (
137110 """
138- /// A collection of leading or trailing trivia. This is the main data structure
139- /// for thinking about trivia.
140- public struct Trivia
111+ extension Trivia
141112 """
142113 ) {
143- DeclSyntax ( " public let pieces: [TriviaPiece] " )
144-
145- DeclSyntax (
146- """
147- /// Creates Trivia with the provided underlying pieces.
148- public init<S: Sequence>(pieces: S) where S.Element == TriviaPiece {
149- self.pieces = Array(pieces)
150- }
151- """
152- )
153-
154- DeclSyntax (
155- """
156- /// Creates Trivia with no pieces.
157- public static var zero: Trivia {
158- return Trivia(pieces: [])
159- }
160- """
161- )
162-
163- DeclSyntax (
164- """
165- /// Whether the Trivia contains no pieces.
166- public var isEmpty: Bool {
167- pieces.isEmpty
168- }
169- """
170- )
171-
172- DeclSyntax (
173- """
174- /// Creates a new `Trivia` by appending the provided `TriviaPiece` to the end.
175- public func appending(_ piece: TriviaPiece) -> Trivia {
176- var copy = pieces
177- copy.append(piece)
178- return Trivia(pieces: copy)
179- }
180- """
181- )
182-
183- DeclSyntax (
184- """
185- public var sourceLength: SourceLength {
186- return pieces.map({ $0.sourceLength }).reduce(.zero, +)
187- }
188- """
189- )
190-
191- DeclSyntax (
192- """
193- /// Get the byteSize of this trivia
194- public var byteSize: Int {
195- return sourceLength.utf8Length
196- }
197- """
198- )
199-
200114 for trivia in TRIVIAS {
201115 if trivia. isCollection {
202116 let joined = trivia. characters. map { " \( $0) " } . joined ( )
@@ -231,99 +145,6 @@ let triviaFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
231145 }
232146 }
233147
234- DeclSyntax (
235- #"""
236- extension Trivia: CustomDebugStringConvertible {
237- public var debugDescription: String {
238- if count == 1, let first = first {
239- return first.debugDescription
240- }
241- return "[" + map(\.debugDescription).joined(separator: ", ") + "]"
242- }
243- }
244- """#
245- )
246-
247- DeclSyntax ( " extension Trivia: Equatable {} " )
248-
249- DeclSyntax (
250- """
251- /// Conformance for Trivia to the Collection protocol.
252- extension Trivia: Collection {
253- public var startIndex: Int {
254- return pieces.startIndex
255- }
256-
257- public var endIndex: Int {
258- return pieces.endIndex
259- }
260-
261- public func index(after i: Int) -> Int {
262- return pieces.index(after: i)
263- }
264-
265- public subscript(_ index: Int) -> TriviaPiece {
266- return pieces[index]
267- }
268- }
269- """
270- )
271-
272- DeclSyntax (
273- """
274- extension Trivia: ExpressibleByArrayLiteral {
275- /// Creates Trivia from the provided pieces.
276- public init(arrayLiteral elements: TriviaPiece...) {
277- self.pieces = elements
278- }
279- }
280- """
281- )
282-
283- DeclSyntax (
284- """
285- extension Trivia: TextOutputStreamable {
286- /// Prints the provided trivia as they would be written in a source file.
287- ///
288- /// - Parameter stream: The stream to which to print the trivia.
289- public func write<Target>(to target: inout Target)
290- where Target: TextOutputStream {
291- for piece in pieces {
292- piece.write(to: &target)
293- }
294- }
295- }
296- """
297- )
298-
299- DeclSyntax (
300- """
301- extension Trivia: CustomStringConvertible {
302- public var description: String {
303- var description = " "
304- self.write(to: &description)
305- return description
306- }
307- }
308- """
309- )
310-
311- DeclSyntax (
312- """
313- extension Trivia {
314- /// Concatenates two collections of `Trivia` into one collection.
315- public static func +(lhs: Trivia, rhs: Trivia) -> Trivia {
316- return Trivia(pieces: lhs.pieces + rhs.pieces)
317- }
318-
319- /// Concatenates two collections of `Trivia` into the left-hand side.
320- public static func +=(lhs: inout Trivia, rhs: Trivia) {
321- lhs = lhs + rhs
322- }
323- }
324- """
325- )
326-
327148 DeclSyntax ( " extension TriviaPiece: Equatable {} " )
328149
329150 try ! ExtensionDeclSyntax ( " extension TriviaPiece " ) {
@@ -388,26 +209,6 @@ let triviaFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
388209 }
389210 }
390211
391- DeclSyntax (
392- """
393- extension RawTriviaPiece: TextOutputStreamable {
394- public func write<Target: TextOutputStream>(to target: inout Target) {
395- TriviaPiece(raw: self).write(to: &target)
396- }
397- }
398- """
399- )
400-
401- DeclSyntax (
402- """
403- extension RawTriviaPiece: CustomDebugStringConvertible {
404- public var debugDescription: String {
405- TriviaPiece(raw: self).debugDescription
406- }
407- }
408- """
409- )
410-
411212 try ! ExtensionDeclSyntax ( " extension TriviaPiece " ) {
412213 try InitializerDeclSyntax ( " @_spi(RawSyntax) public init(raw: RawTriviaPiece) " ) {
413214 try SwitchExprSyntax ( " switch raw " ) {
@@ -463,22 +264,4 @@ let triviaFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
463264 }
464265 }
465266 }
466-
467- DeclSyntax (
468- """
469- extension RawTriviaPiece {
470- /// Returns true if the trivia is `.newlines`, `.carriageReturns` or `.carriageReturnLineFeeds`
471- public var isNewline: Bool {
472- switch self {
473- case .newlines,
474- .carriageReturns,
475- .carriageReturnLineFeeds:
476- return true
477- default:
478- return false
479- }
480- }
481- }
482- """
483- )
484267}
0 commit comments