@@ -44,7 +44,7 @@ public struct Trivia: Sendable {
4444
4545 /// The string contents of all the comment pieces with any comments tokens trimmed.
4646 public var commentValue : String {
47- var comments = [ String ] ( )
47+ var comments = [ Substring ] ( )
4848
4949 // Determine if all line comments have a single space
5050 lazy var allLineCommentsHaveSpace : Bool = {
@@ -60,86 +60,75 @@ public struct Trivia: Sendable {
6060 }
6161 } ( )
6262
63- // Helper function to trim leading and trailing whitespace
64- func trimWhitespace( _ text: String ) -> String {
65- let trimmed = text. drop ( while: { $0 == " " } )
66- . reversed ( )
67- . drop ( while: { $0 == " " } )
68- . reversed ( )
69- return String ( trimmed )
63+ // Returns a substring with leading and trailing spaces removed.
64+ func trimWhitespace( _ text: Substring ) -> Substring {
65+ let trimmed = text. drop ( while: { $0 == " " } )
66+ let reversed = trimmed . reversed ( )
67+ let trimmedEnd = reversed . drop ( while: { $0 == " " } )
68+ let final = trimmedEnd . reversed ( )
69+ return Substring ( final )
7070 }
7171
72- // Helper function to trim leading and trailing newlines
73- func trimNewlines( _ text: String ) -> String {
74- let trimmed = text. drop ( while: { $0 == " \n " } )
75- . reversed ( )
76- . drop ( while: { $0 == " \n " } )
77- . reversed ( )
78- return String ( trimmed)
79- }
80-
81- // Helper function to process block comments
82- func processBlockComment( _ text: String , prefix: String , suffix: String ) -> String {
83- var text = text
84- text. removeFirst ( prefix. count)
85- text. removeLast ( suffix. count)
86- text = trimWhitespace ( text)
87- text = trimNewlines ( text)
88- return text
89- }
90-
91- // Helper function to process multiline block comments
92- func processMultilineBlockComment( _ text: String ) -> String {
93- var lines = text. split ( separator: " \n " , omittingEmptySubsequences: false ) . map ( String . init)
94-
95- lines. removeFirst ( )
72+ // Strips /* */ markers and aligns content by removing common indentation.
73+ func processBlockComment( _ text: Substring ) -> String {
74+ var lines = text. split ( separator: " \n " , omittingEmptySubsequences: false )
9675
9776 let minIndentation =
9877 lines
78+ . dropFirst ( )
9979 . filter { !$0. isEmpty }
10080 . map { $0. prefix { $0 == " " } . count }
10181 . min ( ) ?? 0
10282
83+ var firstLineRemoved = false ;
84+ var firstLine = lines [ 0 ]
85+ if trimWhitespace ( firstLine) == " /* " || trimWhitespace ( firstLine) == " /**" {
86+ lines.removeFirst()
87+ firstLineRemoved = true;
88+ } else {
89+ firstLine = firstLine.hasPrefix("/**") ? firstLine.dropFirst(3) : firstLine.dropFirst(2)
90+ while firstLine.first?.isWhitespace == true {
91+ firstLine = firstLine.dropFirst()
92+ }
93+ lines[0] = firstLine
94+ }
95+
10396 if let lastLine = lines.last {
104- if trimWhitespace ( lastLine) == " */ " {
105- lines. removeLast ( )
106- } else {
107- lines [ lines. count - 1 ] . removeLast ( 2 )
108- lines [ lines. count - 1 ] = trimWhitespace ( lines [ lines. count - 1 ] )
109- }
97+ if trimWhitespace(lastLine) == "*/" {
98+ lines.removeLast()
99+ } else {
100+ var lastLine = lines[lines.count - 1]
101+ lastLine = lastLine.hasSuffix("*/" ) ? lastLine. dropLast ( 2 ) : lastLine
102+ while lastLine. last? . isWhitespace == true {
103+ lastLine = lastLine. dropLast ( )
104+ }
105+ lines [ lines. count - 1 ] = lastLine
106+ }
110107 }
111108
112- let unindentedLines = lines. map { line in
113- guard line. count >= minIndentation else { return line }
114- return String ( line. dropFirst ( minIndentation) )
109+ let unindentedLines = lines. enumerated ( ) . map { index, line -> Substring in
110+ if index == 0 && firstLineRemoved == false {
111+ return line
112+ }
113+ return line. count >= minIndentation ? line. dropFirst ( minIndentation) : line
115114 }
116115
117116 return unindentedLines. joined( separator: " \n" )
118117 }
119118
120119 for piece in pieces {
121120 switch piece {
122- case . blockComment( let text) :
123- let processedText =
124- text. hasPrefix ( " /* \n " )
125- ? processMultilineBlockComment ( text)
126- : processBlockComment ( text, prefix: " /*", suffix: "*/" )
127- comments. append ( processedText)
128-
129- case . docBlockComment( let text) :
130- let processedText =
131- text. hasPrefix ( " /** \n " )
132- ? processMultilineBlockComment ( text)
133- : processBlockComment ( text, prefix: " /**", suffix: "*/" )
134- comments. append ( processedText)
121+ case . blockComment( let text) , . docBlockComment( let text) :
122+ let processedText = processBlockComment ( text [ ... ] )
123+ comments. append ( processedText [ ... ] )
135124
136125 case . lineComment( let text) :
137126 let prefix = allLineCommentsHaveSpace ? " // " : " // "
138- comments. append ( String ( text. dropFirst ( prefix. count) ) )
127+ comments. append ( text. dropFirst ( prefix. count) )
139128
140129 case . docLineComment( let text) :
141130 let prefix = allLineCommentsHaveSpace ? " /// " : " /// "
142- comments. append ( String ( text. dropFirst ( prefix. count) ) )
131+ comments. append ( text. dropFirst ( prefix. count) )
143132
144133 default :
145134 break
0 commit comments