Skip to content

Commit 25bf487

Browse files
authored
improve the readabilty of diagnostics that have a "location prefix" (#4120)
motivation: diagnostics with "location prefix" print that prefix before the diagnostics severity prefix (eg warning, errror) which is less clear changes: * always print severity prefix first * reduce overhead when printing diagnostics by avoiding redundant stream flushes
1 parent c13b885 commit 25bf487

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,24 +1027,29 @@ extension Basics.Diagnostic {
10271027
func print() {
10281028
let writer = InteractiveWriter.stderr
10291029

1030-
if let diagnosticPrefix = self.metadata?.diagnosticPrefix {
1031-
writer.write(diagnosticPrefix)
1032-
writer.write(": ")
1033-
}
1034-
1030+
var message: String
10351031
switch self.severity {
10361032
case .error:
1037-
writer.write("error: ", inColor: .red, bold: true)
1033+
message = writer.format("error: ", inColor: .red, bold: true)
10381034
case .warning:
1039-
writer.write("warning: ", inColor: .yellow, bold: true)
1035+
message = writer.format("warning: ", inColor: .yellow, bold: true)
10401036
case .info:
1041-
writer.write("info: ", inColor: .white, bold: true)
1037+
message = writer.format("info: ", inColor: .white, bold: true)
10421038
case .debug:
1043-
writer.write("debug: ", inColor: .white, bold: true)
1039+
message = writer.format("debug: ", inColor: .white, bold: true)
1040+
}
1041+
1042+
if let diagnosticPrefix = self.metadata?.diagnosticPrefix {
1043+
message += diagnosticPrefix
1044+
message += ": "
10441045
}
10451046

1046-
writer.write(self.message)
1047-
writer.write("\n")
1047+
message += self.message
1048+
if !self.message.hasPrefix("\n") {
1049+
message += "\n"
1050+
}
1051+
1052+
writer.write(message)
10481053
}
10491054
}
10501055

@@ -1074,13 +1079,21 @@ private final class InteractiveWriter {
10741079

10751080
/// Write the string to the contained terminal or stream.
10761081
func write(_ string: String, inColor color: TerminalController.Color = .noColor, bold: Bool = false) {
1077-
if let term = term {
1082+
if let term = self.term {
10781083
term.write(string, inColor: color, bold: bold)
10791084
} else {
10801085
stream <<< string
10811086
stream.flush()
10821087
}
10831088
}
1089+
1090+
func format(_ string: String, inColor color: TerminalController.Color = .noColor, bold: Bool = false) -> String {
1091+
if let term = self.term {
1092+
return term.wrap(string, inColor: color, bold: bold)
1093+
} else {
1094+
return string
1095+
}
1096+
}
10841097
}
10851098

10861099
// FIXME: this is for backwards compatibility with existing diagnostics printing format

0 commit comments

Comments
 (0)