blob: 6d396236dffc14a41f6210895b27b2099372552b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
//===--- 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"
#include <mutex>
#include <string>
#include <vector>
namespace clang {
namespace clangd {
class DocumentStore;
struct DocumentStoreListener {
virtual ~DocumentStoreListener() = default;
virtual void onDocumentAdd(StringRef Uri) {}
virtual void onDocumentRemove(StringRef Uri) {}
};
/// 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.
void addDocument(StringRef Uri, StringRef Text) {
{
std::lock_guard<std::mutex> Guard(DocsMutex);
Docs[Uri] = Text;
}
for (const auto &Listener : Listeners)
Listener->onDocumentAdd(Uri);
}
/// Delete a document from the store.
void removeDocument(StringRef Uri) {
{
std::lock_guard<std::mutex> Guard(DocsMutex);
Docs.erase(Uri);
}
for (const auto &Listener : Listeners)
Listener->onDocumentRemove(Uri);
}
/// Retrieve a document from the store. Empty string if it's unknown.
///
/// This function is thread-safe. It returns a copy to avoid handing out
/// references to unguarded data.
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.
///
/// This function is thread-safe. It returns a copies to avoid handing out
/// references to unguarded data.
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;
}
private:
llvm::StringMap<std::string> Docs;
std::vector<DocumentStoreListener *> Listeners;
mutable std::mutex DocsMutex;
};
} // namespace clangd
} // namespace clang
#endif
|