clang-tools 22.0.0git
Markup.h
Go to the documentation of this file.
1//===--- Markup.h -------------------------------------------*- C++-*------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// A model of formatted text that can be rendered to plaintext or markdown.
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
21namespace clang {
22namespace clangd {
23namespace markup {
24
25/// Holds text and knows how to lay it out. Multiple blocks can be grouped to
26/// form a document. Blocks include their own trailing newlines, container
27/// should trim them if need be.
28class Block {
29public:
30 virtual void renderEscapedMarkdown(llvm::raw_ostream &OS) const = 0;
31 virtual void renderMarkdown(llvm::raw_ostream &OS) const = 0;
32 virtual void renderPlainText(llvm::raw_ostream &OS) const = 0;
33 virtual std::unique_ptr<Block> clone() const = 0;
34 std::string asEscapedMarkdown() const;
35 std::string asMarkdown() const;
36 std::string asPlainText() const;
37
38 virtual bool isRuler() const { return false; }
39 virtual ~Block() = default;
40};
41
42/// Represents parts of the markup that can contain strings, like inline code,
43/// code block or plain text.
44/// One must introduce different paragraphs to create separate blocks.
45class Paragraph : public Block {
46public:
47 void renderEscapedMarkdown(llvm::raw_ostream &OS) const override;
48 void renderMarkdown(llvm::raw_ostream &OS) const override;
49 void renderPlainText(llvm::raw_ostream &OS) const override;
50 std::unique_ptr<Block> clone() const override;
51
52 /// Append plain text to the end of the string.
53 Paragraph &appendText(llvm::StringRef Text);
54
55 /// Append emphasized text, this translates to the * block in markdown.
56 Paragraph &appendEmphasizedText(llvm::StringRef Text);
57
58 /// Append bold text, this translates to the ** block in markdown.
59 Paragraph &appendBoldText(llvm::StringRef Text);
60
61 /// Append inline code, this translates to the ` block in markdown.
62 /// \p Preserve indicates the code span must be apparent even in plaintext.
63 Paragraph &appendCode(llvm::StringRef Code, bool Preserve = false);
64
65 /// Ensure there is space between the surrounding chunks.
66 /// Has no effect at the beginning or end of a paragraph.
67 Paragraph &appendSpace();
68
69private:
70 enum ChunkKind { PlainText, InlineCode, Bold, Emphasized };
71 struct Chunk {
72 ChunkKind Kind = PlainText;
73 // Preserve chunk markers in plaintext.
74 bool Preserve = false;
75 std::string Contents;
76 // Whether this chunk should be surrounded by whitespace.
77 // Consecutive SpaceAfter and SpaceBefore will be collapsed into one space.
78 // Code spans don't usually set this: their spaces belong "inside" the span.
79 bool SpaceBefore = false;
80 bool SpaceAfter = false;
81 };
82 std::vector<Chunk> Chunks;
83
84 /// Estimated size of the string representation of this paragraph.
85 /// Used to reserve space in the output string.
86 /// Each time paragraph content is added, this value is updated.
87 /// This is an estimate, so it may not be accurate but can help
88 /// reducing dynamically reallocating string memory.
89 unsigned EstimatedStringSize = 0;
90
91 Paragraph &appendChunk(llvm::StringRef Contents, ChunkKind K);
92
93 llvm::StringRef chooseMarker(llvm::ArrayRef<llvm::StringRef> Options,
94 llvm::StringRef Text) const;
95 bool punctuationIndicatesLineBreak(llvm::StringRef Line) const;
96 bool isHardLineBreakIndicator(llvm::StringRef Rest) const;
97 bool isHardLineBreakAfter(llvm::StringRef Line, llvm::StringRef Rest) const;
98};
99
100/// Represents a sequence of one or more documents. Knows how to print them in a
101/// list like format, e.g. by prepending with "- " and indentation.
102class BulletList : public Block {
103public:
104 BulletList();
105 ~BulletList();
106
107 // A BulletList rendered in markdown is a tight list if it is not a nested
108 // list and no item contains multiple paragraphs. Otherwise, it is a loose
109 // list.
110 void renderEscapedMarkdown(llvm::raw_ostream &OS) const override;
111 void renderMarkdown(llvm::raw_ostream &OS) const override;
112 void renderPlainText(llvm::raw_ostream &OS) const override;
113 std::unique_ptr<Block> clone() const override;
114
115 class Document &addItem();
116
117private:
118 std::vector<class Document> Items;
119};
120
121/// A format-agnostic representation for structured text. Allows rendering into
122/// markdown and plaintext.
123class Document {
124public:
125 Document() = default;
126 Document(const Document &Other) { *this = Other; }
127 Document &operator=(const Document &);
128 Document(Document &&) = default;
129 Document &operator=(Document &&) = default;
130
131 void append(Document Other);
132
133 /// Adds a semantical block that will be separate from others.
134 Paragraph &addParagraph();
135 /// Inserts a horizontal separator to the document.
136 void addRuler();
137 /// Adds a block of code. This translates to a ``` block in markdown. In plain
138 /// text representation, the code block will be surrounded by newlines.
139 void addCodeBlock(std::string Code, std::string Language = "cpp");
140 /// Heading is a special type of paragraph that will be prepended with \p
141 /// Level many '#'s in markdown.
142 Paragraph &addHeading(size_t Level);
143
144 BulletList &addBulletList();
145
146 /// Doesn't contain any trailing newlines and escaped markdown syntax.
147 /// It is expected that the result of this function
148 /// is rendered as markdown.
149 std::string asEscapedMarkdown() const;
150 /// Doesn't contain any trailing newlines.
151 /// It is expected that the result of this function
152 /// is rendered as markdown.
153 std::string asMarkdown() const;
154 /// Doesn't contain any trailing newlines.
155 std::string asPlainText() const;
156
157private:
158 std::vector<std::unique_ptr<Block>> Children;
159};
160} // namespace markup
161} // namespace clangd
162} // namespace clang
163
164#endif
Holds text and knows how to lay it out.
Definition Markup.h:28
std::string asEscapedMarkdown() const
Definition Markup.cpp:432
virtual bool isRuler() const
Definition Markup.h:38
virtual std::unique_ptr< Block > clone() const =0
virtual void renderPlainText(llvm::raw_ostream &OS) const =0
virtual ~Block()=default
virtual void renderMarkdown(llvm::raw_ostream &OS) const =0
virtual void renderEscapedMarkdown(llvm::raw_ostream &OS) const =0
std::string asMarkdown() const
Definition Markup.cpp:439
std::string asPlainText() const
Definition Markup.cpp:446
Represents parts of the markup that can contain strings, like inline code, code block or plain text.
Definition Markup.h:45
void renderPlainText(llvm::raw_ostream &OS) const override
Definition Markup.cpp:562
Paragraph & appendEmphasizedText(llvm::StringRef Text)
Append emphasized text, this translates to the * block in markdown.
Definition Markup.cpp:705
std::string asMarkdown() const
Doesn't contain any trailing newlines.
Paragraph & appendText(llvm::StringRef Text)
Append plain text to the end of the string.
Definition Markup.cpp:696
std::string asEscapedMarkdown() const
Doesn't contain any trailing newlines and escaped markdown syntax.
void addCodeBlock(std::string Code, std::string Language="cpp")
Append inline code, this translates to the ` block in markdown.
std::string asPlainText() const
Doesn't contain any trailing newlines.
void renderEscapedMarkdown(llvm::raw_ostream &OS) const override
Definition Markup.cpp:453
Paragraph & addHeading(size_t Level)
Heading is a special type of paragraph that will be prepended with Level many '#'s in markdown.
std::unique_ptr< Block > clone() const override
Definition Markup.cpp:511
void renderMarkdown(llvm::raw_ostream &OS) const override
Definition Markup.cpp:482
Paragraph & appendBoldText(llvm::StringRef Text)
Append bold text, this translates to the ** block in markdown.
Definition Markup.cpp:710
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//