<div dir="ltr"><a class="gmail_plusreply" id="plusReplyChip-3" href="mailto:hwennborg@google.com" tabindex="-1">+Hans Wennborg</a>, could we merge this into the release?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Aug 12, 2019 at 4:34 PM Ilya Biryukov via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: ibiryukov<br>
Date: Mon Aug 12 07:35:30 2019<br>
New Revision: 368581<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=368581&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=368581&view=rev</a><br>
Log:<br>
[clangd] Separate chunks with a space when rendering markdown<br>
<br>
Summary:<br>
This results in better rendering of resulting markdown.<br>
<br>
Especially noticeable in coc.nvim that does not have a visible horizontal<br>
spaces around inline code blocks. More details and a screenshot from<br>
coc.nvim can be found in <a href="https://github.com/clangd/clangd/issues/95" rel="noreferrer" target="_blank">https://github.com/clangd/clangd/issues/95</a>.<br>
<br>
Reviewers: sammccall<br>
<br>
Reviewed By: sammccall<br>
<br>
Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits<br>
<br>
Tags: #clang<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D66086" rel="noreferrer" target="_blank">https://reviews.llvm.org/D66086</a><br>
<br>
Modified:<br>
clang-tools-extra/trunk/clangd/FormattedString.cpp<br>
clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp<br>
<br>
Modified: clang-tools-extra/trunk/clangd/FormattedString.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FormattedString.cpp?rev=368581&r1=368580&r2=368581&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FormattedString.cpp?rev=368581&r1=368580&r2=368581&view=diff</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/clangd/FormattedString.cpp (original)<br>
+++ clang-tools-extra/trunk/clangd/FormattedString.cpp Mon Aug 12 07:35:30 2019<br>
@@ -112,15 +112,20 @@ void FormattedString::appendInlineCode(s<br>
<br>
std::string FormattedString::renderAsMarkdown() const {<br>
std::string R;<br>
+ auto EnsureWhitespace = [&R]() {<br>
+ // Adds a space for nicer rendering.<br>
+ if (!R.empty() && !isWhitespace(R.back()))<br>
+ R += " ";<br>
+ };<br>
for (const auto &C : Chunks) {<br>
switch (C.Kind) {<br>
case ChunkKind::PlainText:<br>
+ if (!C.Contents.empty() && !isWhitespace(C.Contents.front()))<br>
+ EnsureWhitespace();<br>
R += renderText(C.Contents);<br>
continue;<br>
case ChunkKind::InlineCodeBlock:<br>
- // Make sure we don't glue two backticks together.<br>
- if (llvm::StringRef(R).endswith("`"))<br>
- R += " ";<br>
+ EnsureWhitespace();<br>
R += renderInlineBlock(C.Contents);<br>
continue;<br>
case ChunkKind::CodeBlock:<br>
<br>
Modified: clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp?rev=368581&r1=368580&r2=368581&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp?rev=368581&r1=368580&r2=368581&view=diff</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp (original)<br>
+++ clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp Mon Aug 12 07:35:30 2019<br>
@@ -72,7 +72,7 @@ after)md";<br>
S.appendText("baz");<br>
<br>
EXPECT_EQ(S.renderAsPlainText(), "foo bar baz");<br>
- EXPECT_EQ(S.renderAsMarkdown(), "foo`bar`baz");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "foo `bar` baz");<br>
}<br>
<br>
TEST(FormattedString, Escaping) {<br>
@@ -158,6 +158,42 @@ TEST(FormattedString, Escaping) {<br>
"`````\n");<br>
}<br>
<br>
+TEST(FormattedString, MarkdownWhitespace) {<br>
+ // Whitespace should be added as separators between blocks.<br>
+ FormattedString S;<br>
+ S.appendText("foo");<br>
+ S.appendText("bar");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "foo bar");<br>
+<br>
+ S = FormattedString();<br>
+ S.appendInlineCode("foo");<br>
+ S.appendInlineCode("bar");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "`foo` `bar`");<br>
+<br>
+ // However, we don't want to add any extra whitespace.<br>
+ S = FormattedString();<br>
+ S.appendText("foo ");<br>
+ S.appendInlineCode("bar");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "foo `bar`");<br>
+<br>
+ S = FormattedString();<br>
+ S.appendText("foo\n");<br>
+ S.appendInlineCode("bar");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "foo\n`bar`");<br>
+<br>
+ S = FormattedString();<br>
+ S.appendInlineCode("foo");<br>
+ S.appendText(" bar");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "`foo` bar");<br>
+<br>
+ S = FormattedString();<br>
+ S.appendText("foo");<br>
+ S.appendCodeBlock("bar");<br>
+ S.appendText("baz");<br>
+ EXPECT_EQ(S.renderAsMarkdown(), "foo\n```cpp\nbar\n```\nbaz");<br>
+}<br>
+<br>
+<br>
} // namespace<br>
} // namespace clangd<br>
} // namespace clang<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Regards,</div><div>Ilya Biryukov</div></div></div></div></div>