11#include "llvm/ADT/StringExtras.h"
12#include "llvm/Support/VirtualFileSystem.h"
19 std::lock_guard<std::mutex> Lock(Mutex);
21 auto It = Drafts.find(
File);
22 if (It == Drafts.end())
29 std::lock_guard<std::mutex> Lock(Mutex);
30 std::vector<Path> ResultVector;
32 for (
auto DraftIt = Drafts.begin(); DraftIt != Drafts.end(); DraftIt++)
33 ResultVector.push_back(std::string(DraftIt->getKey()));
38static void increment(std::string &S) {
40 if (S.empty() || !llvm::isDigit(S.back())) {
45 auto I = S.rbegin(), E = S.rend();
47 if (I == E || !llvm::isDigit(*I)) {
49 S.insert(I.base(),
'1');
63 if (llvm::to_integer(Encoded, Result, 10))
66 elog(
"unexpected non-numeric version {0}", Encoded);
71 llvm::StringRef SpecifiedVersion) {
72 if (!SpecifiedVersion.empty()) {
74 if (SpecifiedVersion.compare_numeric(D.
Version) <= 0)
77 D.
Version = SpecifiedVersion.str();
85 return LSPVersion ? std::to_string(*LSPVersion) :
"";
89 llvm::StringRef Contents) {
90 std::lock_guard<std::mutex> Lock(Mutex);
92 auto &D = Drafts[
File];
93 updateVersion(D.D, Version);
95 D.D.
Contents = std::make_shared<std::string>(Contents);
100 std::lock_guard<std::mutex> Lock(Mutex);
109class SharedStringBuffer :
public llvm::MemoryBuffer {
110 const std::shared_ptr<const std::string> BufferContents;
111 const std::string Name;
114 BufferKind getBufferKind()
const override {
115 return MemoryBuffer::MemoryBuffer_Malloc;
118 llvm::StringRef getBufferIdentifier()
const override {
return Name; }
120 SharedStringBuffer(std::shared_ptr<const std::string> Data,
121 llvm::StringRef Name)
122 : BufferContents(std::move(Data)), Name(Name) {
123 assert(BufferContents &&
"Can't create from empty shared_ptr");
124 MemoryBuffer::init(BufferContents->c_str(),
125 BufferContents->c_str() + BufferContents->size(),
132 auto MemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
133 std::lock_guard<std::mutex> Guard(Mutex);
134 for (
const auto &
Draft : Drafts)
135 MemFS->addFile(
Draft.getKey(),
Draft.getValue().MTime,
136 std::make_unique<SharedStringBuffer>(
static std::string encodeVersion(std::optional< int64_t > LSPVersion)
std::vector< Path > getActiveFiles() const
std::optional< Draft > getDraft(PathRef File) const
void removeDraft(PathRef File)
Remove the draft from the store.
std::string addDraft(PathRef File, llvm::StringRef Version, llvm::StringRef Contents)
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > asVFS() const
static std::optional< int64_t > decodeVersion(llvm::StringRef Encoded)
Whether current platform treats paths case insensitively.
void elog(const char *Fmt, Ts &&...Vals)
void log(const char *Fmt, Ts &&...Vals)
std::shared_ptr< const std::string > Contents