[PATCH] D93978: [clangd] DefineOutline doesn't require implementation file being saved

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 2 10:14:31 PST 2021


njames93 created this revision.
njames93 added reviewers: sammccall, kadircet.
Herald added subscribers: usaxena95, arphaman.
njames93 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang.

The DefineOutline tweak can now apply even if the target file is unsaved.
Depends on D93977 <https://reviews.llvm.org/D93977>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93978

Files:
  clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp


Index: clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "AST.h"
+#include "DraftStore.h"
 #include "FindTarget.h"
 #include "HeaderSourceSwitch.h"
 #include "ParsedAST.h"
@@ -36,6 +37,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include <cstddef>
 #include <string>
 
@@ -397,7 +399,8 @@
     return true;
   }
 
-  Expected<Effect> apply(const Selection &Sel, const DraftStore &) override {
+  Expected<Effect> apply(const Selection &Sel,
+                         const DraftStore &DraftMgr) override {
     const SourceManager &SM = Sel.AST->getSourceManager();
     auto MainFileName =
         getCanonicalPath(SM.getFileEntryForID(SM.getMainFileID()), SM);
@@ -408,14 +411,24 @@
     if (!CCFile)
       return error("Couldn't find a suitable implementation file.");
 
-    auto &FS =
-        Sel.AST->getSourceManager().getFileManager().getVirtualFileSystem();
-    auto Buffer = FS.getBufferForFile(*CCFile);
-    // FIXME: Maybe we should consider creating the implementation file if it
-    // doesn't exist?
-    if (!Buffer)
-      return llvm::errorCodeToError(Buffer.getError());
-    auto Contents = Buffer->get()->getBuffer();
+    StringRef Contents;
+    // We need to keep both these alive as Contents can reference the storage
+    // contained in either of them.
+    auto Draft = DraftMgr.getDraft(*CCFile);
+    std::unique_ptr<llvm::MemoryBuffer> Buffer;
+    if (Draft) {
+      Contents = Draft->Contents;
+    } else {
+      auto &FS =
+          Sel.AST->getSourceManager().getFileManager().getVirtualFileSystem();
+      auto BufferOr = FS.getBufferForFile(*CCFile);
+      // FIXME: Maybe we should consider creating the implementation file if it
+      // doesn't exist?
+      if (!BufferOr)
+        return llvm::errorCodeToError(BufferOr.getError());
+      Buffer = std::move(*BufferOr);
+      Contents = Buffer->getBuffer();
+    }
     auto InsertionPoint = getInsertionPoint(
         Contents, Source->getQualifiedNameAsString(), Sel.AST->getLangOpts());
     if (!InsertionPoint)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93978.314249.patch
Type: text/x-patch
Size: 2440 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210102/d7477027/attachment.bin>


More information about the cfe-commits mailing list