| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_MARKUP_H |
| 13 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_MARKUP_H |
| 14 |
|
| 15 |
#include "llvm/Support/raw_ostream.h" |
| 16 |
#include <cstddef> |
| 17 |
#include <memory> |
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
namespace clang { |
| 22 |
namespace clangd { |
| 23 |
namespace markup { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class Block { |
| 29 |
public: |
| 30 |
virtual void renderMarkdown(llvm::raw_ostream &OS) const = 0; |
| 31 |
virtual void renderPlainText(llvm::raw_ostream &OS) const = 0; |
| 32 |
virtual std::unique_ptr<Block> clone() const = 0; |
| 33 |
std::string asMarkdown() const; |
| 34 |
std::string asPlainText() const; |
| 35 |
|
| 36 |
virtual bool isRuler() const { return false; } |
| 37 |
virtual ~Block() = default; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class Paragraph : public Block { |
| 44 |
public: |
| 45 |
void renderMarkdown(llvm::raw_ostream &OS) const override; |
| 46 |
void renderPlainText(llvm::raw_ostream &OS) const override; |
| 47 |
std::unique_ptr<Block> clone() const override; |
| 48 |
|
| 49 |
|
| 50 |
Paragraph &appendText(llvm::StringRef Text); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
Paragraph &appendCode(llvm::StringRef Code, bool Preserve = false); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
Paragraph &appendSpace(); |
| 59 |
|
| 60 |
private: |
| 61 |
struct Chunk { |
| 62 |
enum { |
| 63 |
PlainText, |
| 64 |
InlineCode, |
| 65 |
} Kind = PlainText; |
| 66 |
|
| 67 |
bool Preserve = false; |
| 68 |
std::string Contents; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
bool SpaceBefore = false; |
| 73 |
bool SpaceAfter = false; |
| 74 |
}; |
| 75 |
std::vector<Chunk> Chunks; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
class BulletList : public Block { |
| 81 |
public: |
| 82 |
BulletList(); |
| 83 |
~BulletList(); |
| 84 |
|
| 85 |
void renderMarkdown(llvm::raw_ostream &OS) const override; |
| 86 |
void renderPlainText(llvm::raw_ostream &OS) const override; |
| 87 |
std::unique_ptr<Block> clone() const override; |
| 88 |
|
| 89 |
class Document &addItem(); |
| 90 |
|
| 91 |
private: |
| 92 |
std::vector<class Document> Items; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
class Document { |
| 98 |
public: |
| 99 |
Document() = default; |
| 100 |
Document(const Document &Other) { *this = Other; } |
| 101 |
Document &operator=(const Document &); |
| 102 |
Document(Document &&) = default; |
| 103 |
Document &operator=(Document &&) = default; |
| 104 |
|
| 105 |
void append(Document Other); |
| 106 |
|
| 107 |
|
| 108 |
Paragraph &addParagraph(); |
| 109 |
|
| 110 |
void addRuler(); |
| 111 |
|
| 112 |
|
| 113 |
void addCodeBlock(std::string Code, std::string Language = "cpp"); |
| 114 |
|
| 115 |
|
| 116 |
Paragraph &addHeading(size_t Level); |
| 117 |
|
| 118 |
BulletList &addBulletList(); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
std::string asMarkdown() const; |
| 124 |
|
| 125 |
std::string asPlainText() const; |
| 126 |
|
| 127 |
private: |
| 128 |
std::vector<std::unique_ptr<Block>> Children; |
| 129 |
}; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#endif |