[clang-tools-extra] r368581 - [clangd] Separate chunks with a space when rendering markdown

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 08:12:00 PDT 2019


+Hans Wennborg <hwennborg at google.com>, could we merge this into the release?

On Mon, Aug 12, 2019 at 4:34 PM Ilya Biryukov via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> Author: ibiryukov
> Date: Mon Aug 12 07:35:30 2019
> New Revision: 368581
>
> URL: http://llvm.org/viewvc/llvm-project?rev=368581&view=rev
> Log:
> [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
>
> Modified:
>     clang-tools-extra/trunk/clangd/FormattedString.cpp
>     clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
>
> Modified: clang-tools-extra/trunk/clangd/FormattedString.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/FormattedString.cpp?rev=368581&r1=368580&r2=368581&view=diff
>
> ==============================================================================
> --- clang-tools-extra/trunk/clangd/FormattedString.cpp (original)
> +++ clang-tools-extra/trunk/clangd/FormattedString.cpp Mon Aug 12 07:35:30
> 2019
> @@ -112,15 +112,20 @@ void FormattedString::appendInlineCode(s
>
>  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:
>
> Modified: clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp?rev=368581&r1=368580&r2=368581&view=diff
>
> ==============================================================================
> --- clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp
> (original)
> +++ clang-tools-extra/trunk/clangd/unittests/FormattedStringTests.cpp Mon
> Aug 12 07:35:30 2019
> @@ -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
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>


-- 
Regards,
Ilya Biryukov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190812/954f0a0a/attachment-0001.html>


More information about the cfe-commits mailing list