nixd
Loading...
Searching...
No Matches
DraftStore.h
Go to the documentation of this file.
1//===--- DraftStore.h - File contents container -----------------*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "Path.h"
12#include "llvm/ADT/StringMap.h"
13#include "llvm/Support/VirtualFileSystem.h"
14#include <mutex>
15#include <optional>
16#include <string>
17#include <vector>
18
19namespace lspserver {
20
21/// A thread-safe container for files opened in a workspace, addressed by
22/// filenames. The contents are owned by the DraftStore.
23/// Each time a draft is updated, it is assigned a version. This can be
24/// specified by the caller or incremented from the previous version.
26public:
27 struct Draft {
28 std::shared_ptr<const std::string> Contents;
29 std::string Version;
30 };
31
32 /// \return Contents of the stored document.
33 /// For untracked files, a std::nullopt is returned.
34 std::optional<Draft> getDraft(PathRef File) const;
35
36 /// \return List of names of the drafts in this store.
37 std::vector<Path> getActiveFiles() const;
38
39 /// Replace contents of the draft for \p File with \p Contents.
40 /// If version is empty, one will be automatically assigned.
41 /// Returns the version.
42 std::string addDraft(PathRef File, llvm::StringRef Version,
43 llvm::StringRef Contents);
44
45 /// Remove the draft from the store.
47
48 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> asVFS() const;
49
50 /// LSP defines file versions as numbers that increase.
51 /// treats them as opaque and therefore uses strings instead.
52 static std::string encodeVersion(std::optional<int64_t> LSPVersion);
53
54 static std::optional<int64_t> decodeVersion(llvm::StringRef Encoded);
55
56private:
57 struct DraftAndTime {
58 Draft D;
59 std::time_t MTime;
60 };
61 mutable std::mutex Mutex;
62 llvm::StringMap<DraftAndTime> Drafts;
63};
64
65} // namespace lspserver
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.
Definition Connection.h:11
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
llvm::StringRef PathRef
Definition Path.h:27
std::shared_ptr< const std::string > Contents
Definition DraftStore.h:28