blob: 90a2d2cfc73295db5a30af5a04bfc19394ae940d [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- DraftStore.h - 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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H
12
13#include "Path.h"
Simon Marchi98082622018-03-26 14:41:40 +000014#include "Protocol.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000015#include "clang/Basic/LLVM.h"
16#include "llvm/ADT/StringMap.h"
17#include <mutex>
18#include <string>
19#include <vector>
20
21namespace clang {
22namespace clangd {
23
Ilya Biryukov38d79772017-05-16 09:38:59 +000024/// A thread-safe container for files opened in a workspace, addressed by
Simon Marchi98082622018-03-26 14:41:40 +000025/// filenames. The contents are owned by the DraftStore. This class supports
26/// both whole and incremental updates of the documents.
Ilya Biryukov38d79772017-05-16 09:38:59 +000027class DraftStore {
28public:
Simon Marchi9569fd52018-03-16 14:30:42 +000029 /// \return Contents of the stored document.
30 /// For untracked files, a llvm::None is returned.
31 llvm::Optional<std::string> getDraft(PathRef File) const;
Simon Marchi5178f922018-02-22 14:00:39 +000032
Simon Marchi9569fd52018-03-16 14:30:42 +000033 /// \return List of names of the drafts in this store.
Simon Marchi5178f922018-02-22 14:00:39 +000034 std::vector<Path> getActiveFiles() const;
35
Ilya Biryukov38d79772017-05-16 09:38:59 +000036 /// Replace contents of the draft for \p File with \p Contents.
Simon Marchi98082622018-03-26 14:41:40 +000037 void addDraft(PathRef File, StringRef Contents);
38
39 /// Update the contents of the draft for \p File based on \p Changes.
40 /// If a position in \p Changes is invalid (e.g. out-of-range), the
41 /// draft is not modified.
42 ///
43 /// \return The new version of the draft for \p File, or an error if the
44 /// changes couldn't be applied.
45 llvm::Expected<std::string>
46 updateDraft(PathRef File,
47 llvm::ArrayRef<TextDocumentContentChangeEvent> Changes);
Simon Marchi9569fd52018-03-16 14:30:42 +000048
49 /// Remove the draft from the store.
50 void removeDraft(PathRef File);
Ilya Biryukov38d79772017-05-16 09:38:59 +000051
52private:
53 mutable std::mutex Mutex;
Simon Marchi9569fd52018-03-16 14:30:42 +000054 llvm::StringMap<std::string> Drafts;
Ilya Biryukov38d79772017-05-16 09:38:59 +000055};
56
57} // namespace clangd
58} // namespace clang
59
60#endif