aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/support/Markup.h
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clangd/support/Markup.h')
-rw-r--r--clang-tools-extra/clangd/support/Markup.h42
1 files changed, 36 insertions, 6 deletions
diff --git a/clang-tools-extra/clangd/support/Markup.h b/clang-tools-extra/clangd/support/Markup.h
index 3a869c4..eea6328 100644
--- a/clang-tools-extra/clangd/support/Markup.h
+++ b/clang-tools-extra/clangd/support/Markup.h
@@ -27,9 +27,11 @@ namespace markup {
/// should trim them if need be.
class Block {
public:
+ virtual void renderEscapedMarkdown(llvm::raw_ostream &OS) const = 0;
virtual void renderMarkdown(llvm::raw_ostream &OS) const = 0;
virtual void renderPlainText(llvm::raw_ostream &OS) const = 0;
virtual std::unique_ptr<Block> clone() const = 0;
+ std::string asEscapedMarkdown() const;
std::string asMarkdown() const;
std::string asPlainText() const;
@@ -42,6 +44,7 @@ public:
/// One must introduce different paragraphs to create separate blocks.
class Paragraph : public Block {
public:
+ void renderEscapedMarkdown(llvm::raw_ostream &OS) const override;
void renderMarkdown(llvm::raw_ostream &OS) const override;
void renderPlainText(llvm::raw_ostream &OS) const override;
std::unique_ptr<Block> clone() const override;
@@ -49,6 +52,12 @@ public:
/// Append plain text to the end of the string.
Paragraph &appendText(llvm::StringRef Text);
+ /// Append emphasized text, this translates to the * block in markdown.
+ Paragraph &appendEmphasizedText(llvm::StringRef Text);
+
+ /// Append bold text, this translates to the ** block in markdown.
+ Paragraph &appendBoldText(llvm::StringRef Text);
+
/// Append inline code, this translates to the ` block in markdown.
/// \p Preserve indicates the code span must be apparent even in plaintext.
Paragraph &appendCode(llvm::StringRef Code, bool Preserve = false);
@@ -58,11 +67,9 @@ public:
Paragraph &appendSpace();
private:
+ enum ChunkKind { PlainText, InlineCode, Bold, Emphasized };
struct Chunk {
- enum {
- PlainText,
- InlineCode,
- } Kind = PlainText;
+ ChunkKind Kind = PlainText;
// Preserve chunk markers in plaintext.
bool Preserve = false;
std::string Contents;
@@ -73,6 +80,21 @@ private:
bool SpaceAfter = false;
};
std::vector<Chunk> Chunks;
+
+ /// Estimated size of the string representation of this paragraph.
+ /// Used to reserve space in the output string.
+ /// Each time paragraph content is added, this value is updated.
+ /// This is an estimate, so it may not be accurate but can help
+ /// reducing dynamically reallocating string memory.
+ unsigned EstimatedStringSize = 0;
+
+ Paragraph &appendChunk(llvm::StringRef Contents, ChunkKind K);
+
+ llvm::StringRef chooseMarker(llvm::ArrayRef<llvm::StringRef> Options,
+ llvm::StringRef Text) const;
+ bool punctuationIndicatesLineBreak(llvm::StringRef Line) const;
+ bool isHardLineBreakIndicator(llvm::StringRef Rest) const;
+ bool isHardLineBreakAfter(llvm::StringRef Line, llvm::StringRef Rest) const;
};
/// Represents a sequence of one or more documents. Knows how to print them in a
@@ -82,6 +104,10 @@ public:
BulletList();
~BulletList();
+ // A BulletList rendered in markdown is a tight list if it is not a nested
+ // list and no item contains multiple paragraphs. Otherwise, it is a loose
+ // list.
+ void renderEscapedMarkdown(llvm::raw_ostream &OS) const override;
void renderMarkdown(llvm::raw_ostream &OS) const override;
void renderPlainText(llvm::raw_ostream &OS) const override;
std::unique_ptr<Block> clone() const override;
@@ -117,9 +143,13 @@ public:
BulletList &addBulletList();
+ /// Doesn't contain any trailing newlines and escaped markdown syntax.
+ /// It is expected that the result of this function
+ /// is rendered as markdown.
+ std::string asEscapedMarkdown() const;
/// Doesn't contain any trailing newlines.
- /// We try to make the markdown human-readable, e.g. avoid extra escaping.
- /// At least one client (coc.nvim) displays the markdown verbatim!
+ /// It is expected that the result of this function
+ /// is rendered as markdown.
std::string asMarkdown() const;
/// Doesn't contain any trailing newlines.
std::string asPlainText() const;