1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "clangdocument.h"
29 #include "clangfilesystemwatcher.h"
30 
31 #include <filecontainer.h>
32 
33 #include <QVector>
34 
35 #include <functional>
36 #include <vector>
37 
38 namespace ClangBackEnd {
39 
40 class UnsavedFiles;
41 
42 class Documents
43 {
44 public:
45     Documents(UnsavedFiles &unsavedFiles);
46 
47     std::vector<Document> create(const QVector<FileContainer> &fileContainers);
48     std::vector<Document> update(const QVector<FileContainer> &fileContainers);
49     void remove(const QVector<FileContainer> &fileContainers);
50 
51     void setUsedByCurrentEditor(const Utf8String &filePath);
52     void setVisibleInEditors(const Utf8StringVector &filePaths);
53 
54     const Document &document(const Utf8String &filePath) const;
55     const Document &document(const FileContainer &fileContainer) const;
56     bool hasDocument(const Utf8String &filePath) const;
57 
58     const std::vector<Document> &documents() const;
59     using IsMatchingDocument = std::function<bool(const Document &document)>;
60     const std::vector<Document> filtered(const IsMatchingDocument &isMatchingDocument) const;
61     std::vector<Document> dirtyAndVisibleButNotCurrentDocuments() const;
62 
63     UnsavedFiles unsavedFiles() const;
64 
65     void addWatchedFiles(QSet<Utf8String> &filePaths);
66 
67     void updateDocumentsWithChangedDependency(const Utf8String &filePath);
68     void updateDocumentsWithChangedDependencies(const QVector<FileContainer> &fileContainers);
69 
70     QVector<FileContainer> newerFileContainers(const QVector<FileContainer> &fileContainers) const;
71 
72     const ClangFileSystemWatcher *clangFileSystemWatcher() const;
73 
74 private:
75     Document createDocument(const FileContainer &fileContainer);
76     std::vector<Document> updateDocument(const FileContainer &fileContainer);
77     std::vector<Document>::const_iterator findDocument(const FileContainer &fileContainer) const;
78     std::vector<Document> findAllDocumentsWithFilePath(const Utf8String &filePath);
79     void checkIfDocumentsDoNotExist(const QVector<FileContainer> &fileContainers) const;
80     void checkIfDocumentsForFilePathsExist(const QVector<FileContainer> &fileContainers) const;
81 
82     void removeDocuments(const QVector<FileContainer> &fileContainers);
83 
84 private:
85     ClangFileSystemWatcher fileSystemWatcher;
86     std::vector<Document> documents_;
87     UnsavedFiles &unsavedFiles_;
88 };
89 
90 } // namespace ClangBackEnd
91