diff --git a/clang-tools-extra/clangd/FormattedString.h b/clang-tools-extra/clangd/FormattedString.h --- a/clang-tools-extra/clangd/FormattedString.h +++ b/clang-tools-extra/clangd/FormattedString.h @@ -49,7 +49,8 @@ Paragraph &appendText(llvm::StringRef Text); /// Append inline code, this translates to the ` block in markdown. - Paragraph &appendCode(llvm::StringRef Code); + /// \p Preserve indicates the code span must be apparent even in plaintext. + Paragraph &appendCode(llvm::StringRef Code, bool Preserve = false); private: struct Chunk { @@ -57,6 +58,8 @@ PlainText, InlineCode, } Kind = PlainText; + // Preserve chunk markers in plaintext. + bool Preserve = false; std::string Contents; /// Language for code block chunks. Ignored for other chunks. std::string Language; diff --git a/clang-tools-extra/clangd/FormattedString.cpp b/clang-tools-extra/clangd/FormattedString.cpp --- a/clang-tools-extra/clangd/FormattedString.cpp +++ b/clang-tools-extra/clangd/FormattedString.cpp @@ -361,7 +361,10 @@ void Paragraph::renderPlainText(llvm::raw_ostream &OS) const { llvm::StringRef Sep = ""; for (auto &C : Chunks) { - OS << Sep << C.Contents; + llvm::StringRef Marker = ""; + if (C.Preserve && C.Kind == Chunk::InlineCode) + Marker = "`"; + OS << Sep << Marker << C.Contents << Marker; Sep = " "; } OS << '\n'; @@ -396,7 +399,7 @@ return *this; } -Paragraph &Paragraph::appendCode(llvm::StringRef Code) { +Paragraph &Paragraph::appendCode(llvm::StringRef Code, bool Preserve) { std::string Norm = canonicalizeSpaces(std::move(Code)); if (Norm.empty()) return *this; @@ -404,6 +407,7 @@ Chunk &C = Chunks.back(); C.Contents = std::move(Norm); C.Kind = Chunk::InlineCode; + C.Preserve = Preserve; return *this; } diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -876,7 +876,7 @@ case '`': if (auto Range = getBacktickQuoteRange(Line, I)) { Out.appendText(Line.substr(0, I)); - Out.appendCode(Range->trim("`")); + Out.appendCode(Range->trim("`"), /*Preserve=*/true); return parseDocumentationLine(Line.substr(I+Range->size()), Out); } break; diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -2022,12 +2022,12 @@ // FIXME: we insert spaces between code and text chunk. "Tests primality of `p`.", "Tests primality of `p` .", - "Tests primality of p .", + "Tests primality of `p` .", }, { "'`' should not occur in `Code`", "'\\`' should not occur in `Code`", - "'`' should not occur in Code", + "'`' should not occur in `Code`", }, { "`not\nparsed`",