Skip to content

[Diagnostics] Add a basic terminal markdown printer for educational notes #28612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/Markup/Markup.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MarkupContext final {
LineList getLineList(swift::RawComment RC);
};

Document *parseDocument(MarkupContext &MC, StringRef String);
Document *parseDocument(MarkupContext &MC, LineList &LL);

} // namespace markup
Expand Down
192 changes: 190 additions & 2 deletions lib/Frontend/PrintingDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "swift/AST/DiagnosticEngine.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Markup/Markup.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
Expand All @@ -28,6 +29,7 @@
#include <algorithm>

using namespace swift;
using namespace swift::markup;

namespace {
class ColoredStream : public raw_ostream {
Expand Down Expand Up @@ -85,6 +87,190 @@ namespace {
size_t preferred_buffer_size() const override { return 0; }
};

// MARK: Markdown Printing
class TerminalMarkupPrinter : public MarkupASTVisitor<TerminalMarkupPrinter> {
llvm::raw_ostream &OS;
unsigned Indent;
unsigned ShouldBold;

void indent(unsigned Amount = 2) { Indent += Amount; }

void dedent(unsigned Amount = 2) {
assert(Indent >= Amount && "dedent without matching indent");
Indent -= Amount;
}

void bold() {
++ShouldBold;
updateFormatting();
}

void unbold() {
assert(ShouldBold > 0 && "unbolded without matching bold");
--ShouldBold;
updateFormatting();
}

void updateFormatting() {
OS.resetColor();
if (ShouldBold > 0)
OS.changeColor(raw_ostream::Colors::SAVEDCOLOR, true);
}

void print(StringRef Str) {
for (auto c : Str) {
OS << c;
if (c == '\n')
for (unsigned i = 0; i < Indent; ++i)
OS << ' ';
}
}

public:
TerminalMarkupPrinter(llvm::raw_ostream &OS)
: OS(OS), Indent(0), ShouldBold(0) {}

void printNewline() { print("\n"); }

void visitDocument(const Document *D) {
for (const auto *Child : D->getChildren()) {
if (Child->getKind() == ASTNodeKind::Paragraph) {
// Add a newline before top-level paragraphs
printNewline();
}
visit(Child);
}
}

void visitBlockQuote(const BlockQuote *BQ) {
indent();
printNewline();
for (const auto *Child : BQ->getChildren())
visit(Child);
dedent();
}

void visitList(const List *BL) {
indent();
printNewline();
for (const auto *Child : BL->getChildren())
visit(Child);
dedent();
}

void visitItem(const Item *I) {
print("- ");
for (const auto *N : I->getChildren())
visit(N);
}

void visitCodeBlock(const CodeBlock *CB) {
indent();
printNewline();
print(CB->getLiteralContent());
dedent();
}

void visitCode(const Code *C) {
print("'");
print(C->getLiteralContent());
print("'");
}

void visitHTML(const HTML *H) { print(H->getLiteralContent()); }

void visitInlineHTML(const InlineHTML *IH) {
print(IH->getLiteralContent());
}

void visitSoftBreak(const SoftBreak *SB) { printNewline(); }

void visitLineBreak(const LineBreak *LB) {
printNewline();
printNewline();
}

void visitLink(const Link *L) {
print("[");
for (const auto *Child : L->getChildren())
visit(Child);
print("](");
print(L->getDestination());
print(")");
}

void visitImage(const Image *I) { llvm_unreachable("unsupported"); }

void visitParagraph(const Paragraph *P) {
for (const auto *Child : P->getChildren())
visit(Child);
printNewline();
}

// TODO: add raw_ostream support for italics ANSI codes in LLVM.
void visitEmphasis(const Emphasis *E) {
for (const auto *Child : E->getChildren())
visit(Child);
}

void visitStrong(const Strong *E) {
bold();
for (const auto *Child : E->getChildren())
visit(Child);
unbold();
}

void visitHRule(const HRule *HR) {
print("--------------");
printNewline();
}

void visitHeader(const Header *H) {
bold();
for (const auto *Child : H->getChildren())
visit(Child);
unbold();
printNewline();
}

void visitText(const Text *T) { print(T->getLiteralContent()); }

void visitPrivateExtension(const PrivateExtension *PE) {
llvm_unreachable("unsupported");
}

void visitParamField(const ParamField *PF) {
llvm_unreachable("unsupported");
}

void visitReturnField(const ReturnsField *RF) {
llvm_unreachable("unsupported");
}

void visitThrowField(const ThrowsField *TF) {
llvm_unreachable("unsupported");
}

#define MARKUP_SIMPLE_FIELD(Id, Keyword, XMLKind) \
void visit##Id(const Id *Field) { llvm_unreachable("unsupported"); }
#include "swift/Markup/SimpleFields.def"
};

static void printMarkdown(StringRef Content, raw_ostream &Out,
bool UseColor) {
markup::MarkupContext ctx;
auto document = markup::parseDocument(ctx, Content);
if (UseColor) {
ColoredStream stream{Out};
TerminalMarkupPrinter printer(stream);
printer.visit(document);
} else {
NoColorStream stream{Out};
TerminalMarkupPrinter printer(stream);
printer.visit(document);
}
}

// MARK: Experimental diagnostic printing.

static void printDiagnosticKind(DiagnosticKind kind, raw_ostream &out) {
Expand Down Expand Up @@ -716,8 +902,10 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM,
printDiagnostic(SM, Info);

for (auto path : Info.EducationalNotePaths) {
if (auto buffer = SM.getFileSystem()->getBufferForFile(path))
Stream << buffer->get()->getBuffer() << "\n";
if (auto buffer = SM.getFileSystem()->getBufferForFile(path)) {
printMarkdown(buffer->get()->getBuffer(), Stream, ForceColors);
Stream << "\n";
}
}

for (auto ChildInfo : Info.ChildDiagnosticInfo) {
Expand Down
Loading