2017-02-07 18:28:20 +08:00
|
|
|
//===--- DocumentStore.h - File contents container --------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DOCUMENTSTORE_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DOCUMENTSTORE_H
|
|
|
|
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2017-02-15 23:04:20 +08:00
|
|
|
#include <mutex>
|
2017-02-07 18:28:20 +08:00
|
|
|
#include <string>
|
2017-02-15 23:19:13 +08:00
|
|
|
#include <vector>
|
2017-02-07 18:28:20 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-02-15 23:04:20 +08:00
|
|
|
class DocumentStore;
|
|
|
|
|
|
|
|
struct DocumentStoreListener {
|
|
|
|
virtual ~DocumentStoreListener() = default;
|
|
|
|
virtual void onDocumentAdd(StringRef Uri) {}
|
|
|
|
virtual void onDocumentRemove(StringRef Uri) {}
|
|
|
|
};
|
2017-02-07 18:28:20 +08:00
|
|
|
|
|
|
|
/// A container for files opened in a workspace, addressed by URI. The contents
|
|
|
|
/// are owned by the DocumentStore.
|
|
|
|
class DocumentStore {
|
|
|
|
public:
|
|
|
|
/// Add a document to the store. Overwrites existing contents.
|
2017-02-15 23:04:20 +08:00
|
|
|
void addDocument(StringRef Uri, StringRef Text) {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Guard(DocsMutex);
|
|
|
|
Docs[Uri] = Text;
|
|
|
|
}
|
|
|
|
for (const auto &Listener : Listeners)
|
|
|
|
Listener->onDocumentAdd(Uri);
|
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
/// Delete a document from the store.
|
2017-02-15 23:04:20 +08:00
|
|
|
void removeDocument(StringRef Uri) {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Guard(DocsMutex);
|
|
|
|
Docs.erase(Uri);
|
|
|
|
}
|
|
|
|
for (const auto &Listener : Listeners)
|
|
|
|
Listener->onDocumentRemove(Uri);
|
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
/// Retrieve a document from the store. Empty string if it's unknown.
|
2017-03-02 18:25:00 +08:00
|
|
|
///
|
|
|
|
/// This function is thread-safe. It returns a copy to avoid handing out
|
|
|
|
/// references to unguarded data.
|
2017-02-15 23:04:20 +08:00
|
|
|
std::string getDocument(StringRef Uri) const {
|
|
|
|
// FIXME: This could be a reader lock.
|
|
|
|
std::lock_guard<std::mutex> Guard(DocsMutex);
|
|
|
|
return Docs.lookup(Uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a listener. Does not take ownership.
|
|
|
|
void addListener(DocumentStoreListener *DSL) { Listeners.push_back(DSL); }
|
|
|
|
|
|
|
|
/// Get name and constents of all documents in this store.
|
2017-03-02 18:25:00 +08:00
|
|
|
///
|
|
|
|
/// This function is thread-safe. It returns a copies to avoid handing out
|
|
|
|
/// references to unguarded data.
|
2017-02-15 23:04:20 +08:00
|
|
|
std::vector<std::pair<std::string, std::string>> getAllDocuments() const {
|
|
|
|
std::vector<std::pair<std::string, std::string>> AllDocs;
|
|
|
|
std::lock_guard<std::mutex> Guard(DocsMutex);
|
|
|
|
for (const auto &P : Docs)
|
|
|
|
AllDocs.emplace_back(P.first(), P.second);
|
|
|
|
return AllDocs;
|
2017-02-07 22:35:09 +08:00
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::StringMap<std::string> Docs;
|
2017-02-15 23:04:20 +08:00
|
|
|
std::vector<DocumentStoreListener *> Listeners;
|
|
|
|
|
|
|
|
mutable std::mutex DocsMutex;
|
2017-02-07 18:28:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|