Skip to content

Commit 1d9c00a

Browse files
committed
[Diag-Experimental-Formatting] Better and more consistent textual description of fix its
1 parent d2db08f commit 1d9c00a

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,32 @@ namespace {
128128
static void describeFixIt(SourceManager &SM, DiagnosticInfo::FixIt fixIt,
129129
raw_ostream &Out) {
130130
if (fixIt.getRange().getByteLength() == 0) {
131-
Out << "[insert '" << fixIt.getText() << "']";
131+
Out << "insert '" << fixIt.getText() << "'";
132132
} else if (fixIt.getText().empty()) {
133-
Out << "[remove '" << SM.extractText(fixIt.getRange()) << "']";
133+
Out << "remove '" << SM.extractText(fixIt.getRange()) << "'";
134134
} else {
135-
Out << "[replace '" << SM.extractText(fixIt.getRange()) << "' with '"
136-
<< fixIt.getText() << "']";
135+
Out << "replace '" << SM.extractText(fixIt.getRange()) << "' with '"
136+
<< fixIt.getText() << "'";
137137
}
138138
}
139139

140+
static void describeFixIts(SourceManager &SM,
141+
ArrayRef<DiagnosticInfo::FixIt> fixIts,
142+
raw_ostream &Out) {
143+
Out << "[";
144+
for (unsigned i = 0; i < fixIts.size(); ++i) {
145+
if (fixIts.size() > 2 && i + 1 == fixIts.size()) {
146+
Out << ", and ";
147+
} else if (fixIts.size() > 2 && i > 0) {
148+
Out << ", ";
149+
} else if (fixIts.size() == 2 && i == 1) {
150+
Out << " and ";
151+
}
152+
describeFixIt(SM, fixIts[i], Out);
153+
}
154+
Out << "]";
155+
}
156+
140157
/// Represents a single line of source code annotated with optional messages,
141158
/// highlights, and fix-its.
142159
class AnnotatedLine {
@@ -543,13 +560,13 @@ static void annotateSnippetWithInfo(SourceManager &SM,
543560
llvm::raw_svector_ostream Out(Text);
544561
DiagnosticEngine::formatDiagnosticText(Out, Info.FormatString,
545562
Info.FormatArgs);
546-
// For notes, show associated fix-its as part of the message. This is a
547-
// better experience when notes offer a choice of fix-its.
548-
if (Info.Kind == DiagnosticKind::Note) {
549-
for (auto fixIt : Info.FixIts) {
550-
Out << " ";
551-
describeFixIt(SM, fixIt, Out);
552-
}
563+
// Show associated fix-its as part of the message. This is a
564+
// better experience when notes offer a choice of fix-its. It's redundant
565+
// for fix-its which are also displayed inline, but helps improve
566+
// readability in some situations.
567+
if (!Info.FixIts.empty()) {
568+
Out << " ";
569+
describeFixIts(SM, Info.FixIts, Out);
553570
}
554571
}
555572

test/diagnostics/pretty-printed-diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct B: Decodable {
6363
// CHECK: 8 |
6464
// CHECK: 9 | foo(a: 2, b: 1, a: 2)
6565
// CHECK: | ~~~~ ~~~~
66-
// CHECK: | ^ error: argument 'a' must precede argument 'b'
66+
// CHECK: | ^ error: argument 'a' must precede argument 'b' [remove ', a: 2' and insert 'a: 2, ']
6767
// CHECK: 10 |
6868

6969

@@ -99,7 +99,7 @@ struct B: Decodable {
9999
// CHECK: 32 | let x: Int = { 42 }()
100100
// CHECK: | ~~~~~~
101101
// CHECK: | ^ error: function produces expected type 'Int'; did you mean to call it with '()'?
102-
// CHECK: | ^ note: Remove '=' to make 'x' a computed property [remove '= '] [replace 'let' with 'var']
102+
// CHECK: | ^ note: Remove '=' to make 'x' a computed property [remove '= ' and replace 'let' with 'var']
103103
// CHECK: 33 | }
104104

105105
// CHECK: SOURCE_DIR/test/diagnostics/pretty-printed-diagnostics.swift:37:9

0 commit comments

Comments
 (0)