Commit 2c69b137 authored by Hans Wennborg's avatar Hans Wennborg
Browse files

Merging r368581:

------------------------------------------------------------------------
r368581 | ibiryukov | 2019-08-12 16:35:30 +0200 (Mon, 12 Aug 2019) | 18 lines

[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: 368670
parent b13c264c
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -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:
+37 −1
Original line number Diff line number Diff line
@@ -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