Skip to content

Commit 44abbf6

Browse files
committed
[Windows] Remove extra newlines from diagnostic output.
We are outputting lots of extra newlines on Windows, which makes it hard to read the diagnostics. rdar://148520063
1 parent c54a9d4 commit 44abbf6

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

Diff for: Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

+21-7
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,19 @@ public struct DiagnosticsFormatter {
203203
return resultSourceString
204204
}
205205

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+
206219
/// Print given diagnostics for a given syntax tree on the command line
207220
///
208221
/// - Parameters:
@@ -232,8 +245,12 @@ public struct DiagnosticsFormatter {
232245
return nil
233246
}.joined()
234247

248+
// Strip trailing newlines
249+
let strippedLine = stripNewlines(sourceLine)
250+
let strippedSuffix = stripNewlines(suffixText)
251+
235252
annotatedSourceLines.append(
236-
AnnotatedSourceLine(diagnostics: diagsForLine, sourceString: sourceLine, suffixText: suffixText)
253+
AnnotatedSourceLine(diagnostics: diagsForLine, sourceString: strippedLine, suffixText: strippedSuffix)
237254
)
238255
}
239256

@@ -293,10 +310,7 @@ public struct DiagnosticsFormatter {
293310
)
294311
)
295312

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")
300314

301315
let columnsWithDiagnostics = Set(
302316
annotatedLine.diagnostics.map {
@@ -331,8 +345,8 @@ public struct DiagnosticsFormatter {
331345
}
332346

333347
// Add suffix text.
334-
annotatedSource.append(annotatedLine.suffixText)
335-
if annotatedSource.last != "\n" {
348+
if !annotatedLine.suffixText.isEmpty {
349+
annotatedSource.append(annotatedLine.suffixText)
336350
annotatedSource.append("\n")
337351
}
338352
}

0 commit comments

Comments
 (0)