blob: 3c2d0c6a4b0fa65e2a7b78760126cbfc69aca073 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- DraftStore.h - File contents container -----------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ilya Biryukov38d79772017-05-16 09:38:59 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H
11
Simon Marchi98082622018-03-26 14:41:40 +000012#include "Protocol.h"
Sam McCallad97ccf2020-04-28 17:49:17 +020013#include "support/Path.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000014#include "clang/Basic/LLVM.h"
15#include "llvm/ADT/StringMap.h"
16#include <mutex>
17#include <string>
18#include <vector>
19
20namespace clang {
21namespace clangd {
22
Ilya Biryukov38d79772017-05-16 09:38:59 +000023/// A thread-safe container for files opened in a workspace, addressed by
Simon Marchi98082622018-03-26 14:41:40 +000024/// filenames. The contents are owned by the DraftStore. This class supports
25/// both whole and incremental updates of the documents.
Sam McCallcaf5a4d2020-03-03 15:57:39 +010026/// Each time a draft is updated, it is assigned a version number. This can be
27/// specified by the caller or incremented from the previous version.
Ilya Biryukov38d79772017-05-16 09:38:59 +000028class DraftStore {
29public:
Sam McCallcaf5a4d2020-03-03 15:57:39 +010030 struct Draft {
31 std::string Contents;
32 int64_t Version = -1;
33 };
34
Simon Marchi9569fd52018-03-16 14:30:42 +000035 /// \return Contents of the stored document.
36 /// For untracked files, a llvm::None is returned.
Sam McCallcaf5a4d2020-03-03 15:57:39 +010037 llvm::Optional<Draft> getDraft(PathRef File) const;
Simon Marchi5178f922018-02-22 14:00:39 +000038
Simon Marchi9569fd52018-03-16 14:30:42 +000039 /// \return List of names of the drafts in this store.
Simon Marchi5178f922018-02-22 14:00:39 +000040 std::vector<Path> getActiveFiles() const;
41
Ilya Biryukov38d79772017-05-16 09:38:59 +000042 /// Replace contents of the draft for \p File with \p Contents.
Sam McCallcaf5a4d2020-03-03 15:57:39 +010043 /// If no version is specified, one will be automatically assigned.
44 /// Returns the version.
45 int64_t addDraft(PathRef File, llvm::Optional<int64_t> Version,
46 StringRef Contents);
Simon Marchi98082622018-03-26 14:41:40 +000047
48 /// Update the contents of the draft for \p File based on \p Changes.
49 /// If a position in \p Changes is invalid (e.g. out-of-range), the
50 /// draft is not modified.
Sam McCallcaf5a4d2020-03-03 15:57:39 +010051 /// If no version is specified, one will be automatically assigned.
Simon Marchi98082622018-03-26 14:41:40 +000052 ///
53 /// \return The new version of the draft for \p File, or an error if the
54 /// changes couldn't be applied.
Sam McCallcaf5a4d2020-03-03 15:57:39 +010055 llvm::Expected<Draft>
56 updateDraft(PathRef File, llvm::Optional<int64_t> Version,
Simon Marchi98082622018-03-26 14:41:40 +000057 llvm::ArrayRef<TextDocumentContentChangeEvent> Changes);
Simon Marchi9569fd52018-03-16 14:30:42 +000058
59 /// Remove the draft from the store.
60 void removeDraft(PathRef File);
Ilya Biryukov38d79772017-05-16 09:38:59 +000061
62private:
63 mutable std::mutex Mutex;
Sam McCallcaf5a4d2020-03-03 15:57:39 +010064 llvm::StringMap<Draft> Drafts;
Ilya Biryukov38d79772017-05-16 09:38:59 +000065};
66
67} // namespace clangd
68} // namespace clang
69
70#endif