forked from OSchip/llvm-project
[clangd] Separate chunks with a space when rendering markdown
Summary: This results in better rendering of resulting markdown. Especially noticeable in coc.nvim that does not have a visible horizontal spaces around inline code blocks. More details and a screenshot from coc.nvim can be found in https://github.com/clangd/clangd/issues/95. Reviewers: sammccall Reviewed By: sammccall Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D66086 llvm-svn: 368581
This commit is contained in:
parent
3bd2b51340
commit
119d1c278c
|
@ -112,15 +112,20 @@ void FormattedString::appendInlineCode(std::string Code) {
|
|||
|
||||
std::string FormattedString::renderAsMarkdown() const {
|
||||
std::string R;
|
||||
auto EnsureWhitespace = [&R]() {
|
||||
// Adds a space for nicer rendering.
|
||||
if (!R.empty() && !isWhitespace(R.back()))
|
||||
R += " ";
|
||||
};
|
||||
for (const auto &C : Chunks) {
|
||||
switch (C.Kind) {
|
||||
case ChunkKind::PlainText:
|
||||
if (!C.Contents.empty() && !isWhitespace(C.Contents.front()))
|
||||
EnsureWhitespace();
|
||||
R += renderText(C.Contents);
|
||||
continue;
|
||||
case ChunkKind::InlineCodeBlock:
|
||||
// Make sure we don't glue two backticks together.
|
||||
if (llvm::StringRef(R).endswith("`"))
|
||||
R += " ";
|
||||
EnsureWhitespace();
|
||||
R += renderInlineBlock(C.Contents);
|
||||
continue;
|
||||
case ChunkKind::CodeBlock:
|
||||
|
|
|
@ -72,7 +72,7 @@ after)md";
|
|||
S.appendText("baz");
|
||||
|
||||
EXPECT_EQ(S.renderAsPlainText(), "foo bar baz");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "foo`bar`baz");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "foo `bar` baz");
|
||||
}
|
||||
|
||||
TEST(FormattedString, Escaping) {
|
||||
|
@ -158,6 +158,42 @@ TEST(FormattedString, Escaping) {
|
|||
"`````\n");
|
||||
}
|
||||
|
||||
TEST(FormattedString, MarkdownWhitespace) {
|
||||
// Whitespace should be added as separators between blocks.
|
||||
FormattedString S;
|
||||
S.appendText("foo");
|
||||
S.appendText("bar");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "foo bar");
|
||||
|
||||
S = FormattedString();
|
||||
S.appendInlineCode("foo");
|
||||
S.appendInlineCode("bar");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "`foo` `bar`");
|
||||
|
||||
// However, we don't want to add any extra whitespace.
|
||||
S = FormattedString();
|
||||
S.appendText("foo ");
|
||||
S.appendInlineCode("bar");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "foo `bar`");
|
||||
|
||||
S = FormattedString();
|
||||
S.appendText("foo\n");
|
||||
S.appendInlineCode("bar");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "foo\n`bar`");
|
||||
|
||||
S = FormattedString();
|
||||
S.appendInlineCode("foo");
|
||||
S.appendText(" bar");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "`foo` bar");
|
||||
|
||||
S = FormattedString();
|
||||
S.appendText("foo");
|
||||
S.appendCodeBlock("bar");
|
||||
S.appendText("baz");
|
||||
EXPECT_EQ(S.renderAsMarkdown(), "foo\n```cpp\nbar\n```\nbaz");
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
} // namespace clangd
|
||||
} // namespace clang
|
||||
|
|
Loading…
Reference in New Issue