[PATCH] D66086: [clangd] Separate chunks with a space when rendering markdown

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 06:09:51 PDT 2019


ilya-biryukov created this revision.
ilya-biryukov added a reviewer: sammccall.
Herald added subscribers: kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66086

Files:
  clang-tools-extra/clangd/FormattedString.cpp
  clang-tools-extra/clangd/unittests/FormattedStringTests.cpp


Index: clang-tools-extra/clangd/unittests/FormattedStringTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FormattedStringTests.cpp
+++ clang-tools-extra/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/clangd/FormattedString.cpp
===================================================================
--- clang-tools-extra/clangd/FormattedString.cpp
+++ clang-tools-extra/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.214618.patch
Type: text/x-patch
Size: 2620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190812/1cee64ed/attachment.bin>


More information about the cfe-commits mailing list