Debian Codegraph @

LspCpp/include/LibLsp/lsp/Markup/Markup.h

raw
1 #pragma once
2 #include <cctype>
3 #include <clocale>
4 #include <cstddef>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 #include <sstream>
9
10 #include "string_ref.h"
11
12 namespace lsp
13 {
14
15 /// Holds text and knows how to lay it out. Multiple blocks can be grouped to
16 /// form a document. Blocks include their own trailing newlines, std::string_ref
17 /// should trim them if need be.
18 class Block
19 {
20 public:
21 virtual void renderMarkdown(std::ostringstream& OS) const = 0;
22 virtual void renderPlainText(std::ostringstream& OS) const = 0;
23 virtual std::unique_ptr<Block> clone() const = 0;
24 std::string_ref asMarkdown() const;
25 std::string_ref asPlainText() const;
26
27 virtual bool isRuler() const
28 {
29 return false;
30 }
31 virtual ~Block() = default;
32 };
33
34 /// Represents parts of the markup that can contain strings, like inline code,
35 /// code block or plain text.
36 /// One must introduce different paragraphs to create separate blocks.
37 class Paragraph : public Block
38 {
39 public:
40 void renderMarkdown(std::ostringstream& OS) const override;
41 void renderPlainText(std::ostringstream& OS) const override;
42 std::unique_ptr<Block> clone() const override;
43
44 /// Append plain text to the end of the string.
45 Paragraph& appendText(std::string_ref const& Text);
46
47 /// Append inline code, this translates to the ` block in markdown.
48 /// \p Preserve indicates the code span must be apparent even in plaintext.
49 Paragraph& appendCode(std::string_ref const& Code, bool Preserve = false);
50
51 /// Ensure there is space between the surrounding chunks.
52 /// Has no effect at the beginning or end of a paragraph.
53 Paragraph& appendSpace();
54
55 private:
56 struct Chunk
57 {
58 enum
59 {
60 PlainText,
61 InlineCode,
62 } Kind = PlainText;
63 // Preserve chunk markers in plaintext.
64 bool Preserve = false;
65 std::string_ref Contents;
66 // Whether this chunk should be surrounded by whitespace.
67 // Consecutive SpaceAfter and SpaceBefore will be collapsed into one space.
68 // Code spans don't usually set this: their spaces belong "inside" the span.
69 bool SpaceBefore = false;
70 bool SpaceAfter = false;
71 };
72 std::vector<Chunk> Chunks;
73 };
74
75 /// Represents a sequence of one or more documents. Knows how to print them in a
76 /// list like format, e.g. by prepending with "- " and indentation.
77 class BulletList : public Block
78 {
79 public:
80 void renderMarkdown(std::ostringstream& OS) const override;
81 void renderPlainText(std::ostringstream& OS) const override;
82 std::unique_ptr<Block> clone() const override;
83
84 class Document& addItem();
85
86 private:
87 std::vector<class Document> Items;
88 };
89
90 /// A format-agnostic representation for structured text. Allows rendering into
91 /// markdown and plaintext.
92 class Document
93 {
94 public:
95 Document() = default;
96 Document(Document const& Other)
97 {
98 *this = Other;
99 }
100 Document& operator=(Document const&);
101 Document(Document&&) = default;
102 Document& operator=(Document&&) = default;
103
104 void append(Document Other);
105
106 /// Adds a semantical block that will be separate from others.
107 Paragraph& addParagraph();
108 /// Inserts a horizontal separator to the document.
109 void addRuler();
110 /// Adds a block of code. This translates to a ``` block in markdown. In plain
111 /// text representation, the code block will be surrounded by newlines.
112 void addCodeBlock(std::string_ref Code, std::string_ref Language = "cpp");
113 /// Heading is a special type of paragraph that will be prepended with \p
114 /// Level many '#'s in markdown.
115 Paragraph& addHeading(size_t Level);
116
117 BulletList& addBulletList();
118
119 /// Doesn't contain any trailing newlines.
120 /// We try to make the markdown human-readable, e.g. avoid extra escaping.
121 /// At least one client (coc.nvim) displays the markdown verbatim!
122 std::string_ref asMarkdown() const;
123 /// Doesn't contain any trailing newlines.
124 std::string_ref asPlainText() const;
125
126 private:
127 std::vector<std::unique_ptr<Block>> Children;
128 };
129
130 } // namespace lsp