[PATCH] D66086: [clangd] Separate chunks with a space when rendering markdown
Phabricator via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 12 07:35:24 PDT 2019
This revision was automatically updated to reflect the committed changes.
Closed by commit rL368581: [clangd] Separate chunks with a space when rendering markdown (authored by ibiryukov, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Changed prior to commit:
https://reviews.llvm.org/D66086?vs=214618&id=214634#toc
Repository:
rL LLVM
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D66086/new/
https://reviews.llvm.org/D66086
Files:
clang-tools-extra/trunk/clangd/FormattedString.cpp
clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
Index: clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
@@ -72,7 +72,7 @@
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 @@
"`````\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
Index: clang-tools-extra/trunk/clangd/FormattedString.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/FormattedString.cpp
+++ clang-tools-extra/trunk/clangd/FormattedString.cpp
@@ -112,15 +112,20 @@
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:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66086.214634.patch
Type: text/x-patch
Size: 2656 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190812/eab939bf/attachment-0001.bin>
More information about the cfe-commits
mailing list