@@ -203,6 +203,19 @@ public struct DiagnosticsFormatter {
203
203
return resultSourceString
204
204
}
205
205
206
+ /// Strip trailing newlines
207
+ ///
208
+ /// - Parameters:
209
+ /// - s: string from which to strip trailing newlines.
210
+ func stripNewlines(
211
+ _ s: String
212
+ ) -> String {
213
+ if let lastNewline = s. lastIndex ( where: { $0. isNewline } ) {
214
+ return String ( s. prefix ( upTo: lastNewline) )
215
+ }
216
+ return s
217
+ }
218
+
206
219
/// Print given diagnostics for a given syntax tree on the command line
207
220
///
208
221
/// - Parameters:
@@ -232,8 +245,12 @@ public struct DiagnosticsFormatter {
232
245
return nil
233
246
} . joined ( )
234
247
248
+ // Strip trailing newlines
249
+ let strippedLine = stripNewlines ( sourceLine)
250
+ let strippedSuffix = stripNewlines ( suffixText)
251
+
235
252
annotatedSourceLines. append (
236
- AnnotatedSourceLine ( diagnostics: diagsForLine, sourceString: sourceLine , suffixText: suffixText )
253
+ AnnotatedSourceLine ( diagnostics: diagsForLine, sourceString: strippedLine , suffixText: strippedSuffix )
237
254
)
238
255
}
239
256
@@ -293,10 +310,7 @@ public struct DiagnosticsFormatter {
293
310
)
294
311
)
295
312
296
- // If the line did not end with \n (e.g. the last line), append it manually
297
- if annotatedSource. last != " \n " {
298
- annotatedSource. append ( " \n " )
299
- }
313
+ annotatedSource. append ( " \n " )
300
314
301
315
let columnsWithDiagnostics = Set (
302
316
annotatedLine. diagnostics. map {
@@ -331,8 +345,8 @@ public struct DiagnosticsFormatter {
331
345
}
332
346
333
347
// Add suffix text.
334
- annotatedSource . append ( annotatedLine. suffixText)
335
- if annotatedSource. last != " \n " {
348
+ if ! annotatedLine. suffixText. isEmpty {
349
+ annotatedSource. append ( annotatedLine . suffixText )
336
350
annotatedSource. append ( " \n " )
337
351
}
338
352
}
0 commit comments