@@ -44,7 +44,7 @@ public struct Trivia: Sendable {
44
44
45
45
/// The string contents of all the comment pieces with any comments tokens trimmed.
46
46
public var commentValue : String {
47
- var comments = [ String ] ( )
47
+ var comments = [ Substring ] ( )
48
48
49
49
// Determine if all line comments have a single space
50
50
lazy var allLineCommentsHaveSpace : Bool = {
@@ -60,86 +60,75 @@ public struct Trivia: Sendable {
60
60
}
61
61
} ( )
62
62
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 )
70
70
}
71
71
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 )
96
75
97
76
let minIndentation =
98
77
lines
78
+ . dropFirst ( )
99
79
. filter { !$0. isEmpty }
100
80
. map { $0. prefix { $0 == " " } . count }
101
81
. min ( ) ?? 0
102
82
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
+
103
96
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
+ }
110
107
}
111
108
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
115
114
}
116
115
117
116
return unindentedLines. joined( separator: " \n" )
118
117
}
119
118
120
119
for piece in pieces {
121
120
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 [ ... ] )
135
124
136
125
case . lineComment( let text) :
137
126
let prefix = allLineCommentsHaveSpace ? " // " : " // "
138
- comments. append ( String ( text. dropFirst ( prefix. count) ) )
127
+ comments. append ( text. dropFirst ( prefix. count) )
139
128
140
129
case . docLineComment( let text) :
141
130
let prefix = allLineCommentsHaveSpace ? " /// " : " /// "
142
- comments. append ( String ( text. dropFirst ( prefix. count) ) )
131
+ comments. append ( text. dropFirst ( prefix. count) )
143
132
144
133
default :
145
134
break
0 commit comments