[PATCH] D72499: [clangd] (DO NOT COMMIT) hovering on "md" raw strings displays those.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 10 03:46:27 PST 2020


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

This is useful for testing markdown rendering in hover of editors.

It shoudn't be checked in (it'd be a nice easter egg, but the implementation
is too messy).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72499

Files:
  clang-tools-extra/clangd/FormattedString.h
  clang-tools-extra/clangd/Hover.cpp
  clang-tools-extra/clangd/Hover.h


Index: clang-tools-extra/clangd/Hover.h
===================================================================
--- clang-tools-extra/clangd/Hover.h
+++ clang-tools-extra/clangd/Hover.h
@@ -71,6 +71,8 @@
   /// Contains the evaluated value of the symbol if available.
   llvm::Optional<std::string> Value;
 
+  llvm::Optional<std::string> RawResponse;
+
   /// Produce a user-readable information.
   markup::Document present() const;
 };
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -439,6 +439,15 @@
         // Look for a close enclosing expression to show the value of.
         if (!HI->Value)
           HI->Value = printExprValue(N, AST.getASTContext());
+      } else if (auto *SL = N->ASTNode.get<StringLiteral>()) {
+        if (SL->getBeginLoc().isFileID()) {
+          const char *Data = SM.getCharacterData(SL->getBeginLoc());
+          // safe because null-terminated
+          if (Data && !strncmp("R\"md(", Data, 5)) {
+            HI.emplace();
+            HI->RawResponse = SL->getString();
+          }
+        }
       }
       // FIXME: support hovers for other nodes?
       //  - certain expressions (sizeof etc)
@@ -463,6 +472,10 @@
 
 markup::Document HoverInfo::present() const {
   markup::Document Output;
+  if (RawResponse) {
+    Output.addRaw(*RawResponse);
+    return Output;
+  }
   // Header contains a text of the form:
   // variable `var` : `int`
   //
Index: clang-tools-extra/clangd/FormattedString.h
===================================================================
--- clang-tools-extra/clangd/FormattedString.h
+++ clang-tools-extra/clangd/FormattedString.h
@@ -35,6 +35,15 @@
   virtual ~Block() = default;
 };
 
+class RawBlock : public Block {
+ public:
+  RawBlock(std::string Text) : Text(std::move(Text)) {}
+  virtual void renderMarkdown(llvm::raw_ostream &OS) const override { OS << Text; }
+  virtual void renderPlainText(llvm::raw_ostream &OS) const override { OS << Text; }
+ private:
+  std::string Text;
+};
+
 /// Represents parts of the markup that can contain strings, like inline code,
 /// code block or plain text.
 /// One must introduce different paragraphs to create separate blocks.
@@ -88,6 +97,9 @@
   void addCodeBlock(std::string Code, std::string Language = "cpp");
 
   BulletList &addBulletList();
+  void addRaw(std::string Text) {
+    Children.push_back(std::make_unique<RawBlock>(std::move(Text)));
+  }
 
   /// Doesn't contain any trailing newlines.
   std::string asMarkdown() const;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72499.237280.patch
Type: text/x-patch
Size: 2614 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200110/fdee16b0/attachment.bin>


More information about the cfe-commits mailing list