1 //===--- DraftStore.cpp - File contents container ---------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "DraftStore.h" 11 12 using namespace clang; 13 using namespace clang::clangd; 14 15 VersionedDraft DraftStore::getDraft(PathRef File) const { 16 std::lock_guard<std::mutex> Lock(Mutex); 17 18 auto It = Drafts.find(File); 19 if (It == Drafts.end()) 20 return {0, llvm::None}; 21 return It->second; 22 } 23 24 std::vector<Path> DraftStore::getActiveFiles() const { 25 std::lock_guard<std::mutex> Lock(Mutex); 26 std::vector<Path> ResultVector; 27 28 for (auto DraftIt = Drafts.begin(); DraftIt != Drafts.end(); DraftIt++) 29 if (DraftIt->second.Draft) 30 ResultVector.push_back(DraftIt->getKey()); 31 32 return ResultVector; 33 } 34 35 DocVersion DraftStore::getVersion(PathRef File) const { 36 std::lock_guard<std::mutex> Lock(Mutex); 37 38 auto It = Drafts.find(File); 39 if (It == Drafts.end()) 40 return 0; 41 return It->second.Version; 42 } 43 44 DocVersion DraftStore::updateDraft(PathRef File, StringRef Contents) { 45 std::lock_guard<std::mutex> Lock(Mutex); 46 47 auto &Entry = Drafts[File]; 48 DocVersion NewVersion = ++Entry.Version; 49 Entry.Draft = Contents; 50 return NewVersion; 51 } 52 53 DocVersion DraftStore::removeDraft(PathRef File) { 54 std::lock_guard<std::mutex> Lock(Mutex); 55 56 auto &Entry = Drafts[File]; 57 DocVersion NewVersion = ++Entry.Version; 58 Entry.Draft = llvm::None; 59 return NewVersion; 60 } 61