File indexing completed on 2024-04-28 04:37:28

0001 /*
0002     SPDX-FileCopyrightText: 2010 Dmitry Risenberg <dmitry.risenberg@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PLUGIN_WATCHEDDOCUMENTSET_H
0008 #define KDEVPLATFORM_PLUGIN_WATCHEDDOCUMENTSET_H
0009 
0010 #include <QObject>
0011 #include <QSet>
0012 #include <QUrl>
0013 
0014 #include <serialization/indexedstring.h>
0015 #include <shell/shellexport.h>
0016 
0017 #include "problemconstants.h"
0018 
0019 namespace KDevelop {
0020 class IDocument;
0021 class IProject;
0022 class IProjectFileManager;
0023 class ProjectFileItem;
0024 class Path;
0025 class WatchedDocumentSetPrivate;
0026 
0027 /**
0028  * Helper class that tracks set of documents and notifies its owner whenever this set changes. Derived classes implement different tracking strategies.
0029  */
0030 class KDEVPLATFORMSHELL_EXPORT WatchedDocumentSet : public QObject
0031 {
0032     Q_OBJECT
0033 public:
0034     using DocumentSet = QSet<IndexedString>;
0035 
0036     explicit WatchedDocumentSet(QObject* parent);
0037     ~WatchedDocumentSet() override;
0038 
0039     bool showImports() const;
0040     void setShowImports(bool showImports);
0041 
0042     virtual DocumentSet get() const;
0043     virtual DocumentSet imports() const;
0044 
0045     virtual void setCurrentDocument(const IndexedString& url);
0046     virtual ProblemScope scope() const = 0;
0047 
0048 Q_SIGNALS:
0049     void changed();
0050 
0051 protected:
0052     const QScopedPointer<class WatchedDocumentSetPrivate> d_ptr;
0053     Q_DECLARE_PRIVATE(WatchedDocumentSet)
0054 };
0055 
0056 /**
0057  * Tracks a document that is current at any given moment.
0058  * When a new file is activated, it becomes tracked instead of the old one.
0059  */
0060 class CurrentDocumentSet : public WatchedDocumentSet
0061 {
0062     Q_OBJECT
0063 public:
0064     explicit CurrentDocumentSet(const IndexedString& document, QObject* parent);
0065     void setCurrentDocument(const IndexedString& url) override;
0066     ProblemScope scope() const override;
0067 };
0068 
0069 /**
0070  * Tracks all open documents.
0071  */
0072 class OpenDocumentSet : public WatchedDocumentSet
0073 {
0074     Q_OBJECT
0075 public:
0076     explicit OpenDocumentSet(QObject* parent);
0077     ProblemScope scope() const override;
0078 
0079 private Q_SLOTS:
0080     void documentClosed(IDocument* doc);
0081     void documentCreated(IDocument* doc);
0082     void documentUrlChanged(IDocument* doc, const QUrl& previousUrl);
0083 };
0084 
0085 class ProjectSet : public WatchedDocumentSet
0086 {
0087     Q_OBJECT
0088 public:
0089     explicit ProjectSet(QObject* parent);
0090 
0091 protected:
0092     void trackProjectFiles(const IProjectFileManager* projectFileManager);
0093     void stopTrackingProjectFiles(const IProjectFileManager* projectFileManager);
0094 
0095 protected Q_SLOTS:
0096     void fileAdded(KDevelop::ProjectFileItem*);
0097     void fileRemoved(KDevelop::ProjectFileItem* file);
0098     void fileRenamed(const KDevelop::Path& oldFile, KDevelop::ProjectFileItem* newFile);
0099 
0100 protected:
0101     virtual bool include(const IndexedString& /*url*/) const { return true; }
0102 
0103 private:
0104     bool m_pauseAddingFiles = false;
0105 };
0106 
0107 /**
0108  * Tracks documents that are in the same project as the current file.
0109  * If current file is not in any project, none are tracked.
0110  */
0111 class CurrentProjectSet : public ProjectSet
0112 {
0113     Q_OBJECT
0114 public:
0115     explicit CurrentProjectSet(const IndexedString& document, QObject* parent);
0116     void setCurrentDocument(const IndexedString& url) override;
0117     ProblemScope scope() const override;
0118 
0119 private:
0120     void handleCurrentDocumentChange();
0121     bool include(const IndexedString& url) const override;
0122 
0123     QUrl m_currentDocumentUrl;
0124     const IProject* m_currentProject = nullptr;
0125 };
0126 
0127 /**
0128  * Tracks files in all open projects.
0129  */
0130 class AllProjectSet : public ProjectSet
0131 {
0132     Q_OBJECT
0133 public:
0134     explicit AllProjectSet(QObject* parent);
0135     ProblemScope scope() const override;
0136 
0137 protected:
0138     enum class InitFlag {
0139         LoadOnInit,
0140         SkipLoadOnInit,
0141     };
0142     explicit AllProjectSet(InitFlag initFlag, QObject* parent);
0143     void reload();
0144 
0145 private:
0146     void projectOpened(const IProject* project);
0147     void addProjectFiles(const IProject& project);
0148 
0149     QSet<const IProjectFileManager*> m_trackedProjectFileManagers;
0150 };
0151 
0152 class DocumentsInPathSet : public AllProjectSet
0153 {
0154     Q_OBJECT
0155 public:
0156     explicit DocumentsInPathSet(const QString& path, QObject* parent);
0157     ProblemScope scope() const override;
0158     void setPath(const QString& path);
0159 
0160 private:
0161     bool include(const IndexedString& url) const override;
0162 
0163     QString m_path;
0164 };
0165 
0166 class DocumentsInCurrentPathSet : public DocumentsInPathSet
0167 {
0168     Q_OBJECT
0169 public:
0170     explicit DocumentsInCurrentPathSet(const IndexedString& document, QObject* parent);
0171     ProblemScope scope() const override;
0172     void setCurrentDocument(const IndexedString& url) override;
0173 };
0174 
0175 class BypassSet : public WatchedDocumentSet
0176 {
0177     Q_OBJECT
0178 public:
0179     explicit BypassSet(QObject* parent);
0180 
0181     ProblemScope scope() const override;
0182 };
0183 
0184 }
0185 
0186 #endif // KDEVPLATFORM_PLUGIN_WATCHEDDOCUMENTSET_H