cppmodelmanager.h 8.37 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32
#ifndef CPPMODELMANAGER_H
#define CPPMODELMANAGER_H

33
#include "cpptools_global.h"
34

35
#include "cppmodelmanagerinterface.h"
36

37
#include <projectexplorer/project.h>
38 39
#include <texteditor/basetexteditor.h>

40 41
#include <QHash>
#include <QMutex>
42
#include <QTimer>
con's avatar
con committed
43

44
namespace Core { class IEditor; }
45
namespace TextEditor { class BaseTextEditorWidget; }
con's avatar
con committed
46 47

namespace CppTools {
48

49
class CppEditorSupport;
50

con's avatar
con committed
51 52
namespace Internal {

Roberto Raggi's avatar
Roberto Raggi committed
53
class CppFindReferences;
con's avatar
con committed
54

55
class CPPTOOLS_EXPORT CppModelManager : public CppTools::CppModelManagerInterface
con's avatar
con committed
56 57 58
{
    Q_OBJECT

59 60 61
public:
    typedef CPlusPlus::Document Document;

con's avatar
con committed
62
public:
63
    CppModelManager(QObject *parent = 0);
con's avatar
con committed
64 65
    virtual ~CppModelManager();

66 67
    static CppModelManager *instance();

68 69
    virtual QFuture<void> updateSourceFiles(const QStringList &sourceFiles,
        ProgressNotificationMode mode = ReservedProgressNotification);
70
    virtual WorkingCopy workingCopy() const;
71
    virtual QByteArray codeModelConfiguration() const;
72 73 74

    virtual QList<ProjectInfo> projectInfos() const;
    virtual ProjectInfo projectInfo(ProjectExplorer::Project *project) const;
75
    virtual QFuture<void> updateProjectInfo(const ProjectInfo &newProjectInfo);
76

77 78
    /// \return The project part with the given project file
    virtual ProjectPart::Ptr projectPartForProjectFile(const QString &projectFile) const;
79 80 81 82 83 84 85
    /// \return All project parts that mention the given file name as one of the sources/headers.
    virtual QList<ProjectPart::Ptr> projectPart(const QString &fileName) const;
    /// This is a fall-back function: find all files that includes the file directly or indirectly,
    /// and return its \c ProjectPart list for use with this file.
    virtual QList<ProjectPart::Ptr> projectPartFromDependencies(const QString &fileName) const;
    /// \return A synthetic \c ProjectPart which consists of all defines/includes/frameworks from
    ///         all loaded projects.
86
    virtual ProjectPart::Ptr fallbackProjectPart() const;
87

88
    virtual CPlusPlus::Snapshot snapshot() const;
89 90
    virtual Document::Ptr document(const QString &fileName) const;
    bool replaceDocument(Document::Ptr newDoc);
con's avatar
con committed
91 92 93

    void emitDocumentUpdated(CPlusPlus::Document::Ptr doc);

94 95 96 97 98 99
    virtual bool isCppEditor(Core::IEditor *editor) const;

    virtual void addExtraEditorSupport(AbstractEditorSupport *editorSupport);
    virtual void removeExtraEditorSupport(AbstractEditorSupport *editorSupport);
    virtual CppEditorSupport *cppEditorSupport(TextEditor::BaseTextEditor *textEditor);
    virtual void deleteCppEditorSupport(TextEditor::BaseTextEditor *textEditor);
100

101
    virtual QList<int> references(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
102

103 104
    virtual void renameUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context,
                              const QString &replacement = QString());
105
    virtual void findUsages(CPlusPlus::Symbol *symbol, const CPlusPlus::LookupContext &context);
Roberto Raggi's avatar
Roberto Raggi committed
106

Christian Kamm's avatar
Christian Kamm committed
107
    virtual void findMacroUsages(const CPlusPlus::Macro &macro);
108
    virtual void renameMacroUsages(const CPlusPlus::Macro &macro, const QString &replacement);
Christian Kamm's avatar
Christian Kamm committed
109

110
    virtual bool setExtraDiagnostics(const QString &fileName, const QString &key,
111
                                     const QList<Document::DiagnosticMessage> &diagnostics);
112 113
    virtual void setIfdefedOutBlocks(const QString &fileName,
                                     const QList<TextEditor::BlockRange> &ifdeffedOutBlocks);
114

115 116
    void finishedRefreshingSourceFiles(const QStringList &files);

117
    virtual void addModelManagerSupport(ModelManagerSupport *modelManagerSupport);
118
    virtual ModelManagerSupport *modelManagerSupportForMimeType(const QString &mimeType) const;
119
    virtual CppCompletionAssistProvider *completionAssistProvider(Core::IEditor *editor) const;
120 121
    virtual CppHighlightingSupport *highlightingSupport(Core::IEditor *editor) const;

122 123
    virtual void setIndexingSupport(CppIndexingSupport *indexingSupport);
    virtual CppIndexingSupport *indexingSupport();
124

125 126 127 128 129 130 131 132 133 134 135 136
    QStringList projectFiles()
    {
        ensureUpdated();
        return m_projectFiles;
    }

    QStringList includePaths()
    {
        ensureUpdated();
        return m_includePaths;
    }

137 138 139 140 141 142
    // Use this *only* for auto tests
    void setIncludePaths(const QStringList &includePaths)
    {
        m_includePaths = includePaths;
    }

143 144 145 146 147 148 149 150 151 152 153 154
    QStringList frameworkPaths()
    {
        ensureUpdated();
        return m_frameworkPaths;
    }

    QByteArray definedMacros()
    {
        ensureUpdated();
        return m_definedMacros;
    }

155 156
    void enableGarbageCollector(bool enable);

157 158
    static QStringList timeStampModifiedFiles(const QList<Document::Ptr> documentsToCheck);

159
signals:
160
    void gcFinished(); // Needed for tests.
161

162
public slots:
163
    virtual void updateModifiedSourceFiles();
164
    virtual void GC();
165

166 167
private slots:
    // This should be executed in the GUI thread.
168
    void onAboutToLoadSession();
169 170
    void onAboutToUnloadSession();
    void onProjectAdded(ProjectExplorer::Project *project);
171 172
    void onAboutToRemoveProject(ProjectExplorer::Project *project);
    void onCoreAboutToClose();
173 174

private:
175
    void delayedGC();
176 177
    void recalculateFileToProjectParts();

178
    void replaceSnapshot(const CPlusPlus::Snapshot &newSnapshot);
179 180 181
    void removeFilesFromSnapshot(const QSet<QString> &removedFiles);
    void removeProjectInfoFilesAndIncludesFromSnapshot(const ProjectInfo &projectInfo);

182 183
    WorkingCopy buildWorkingCopyList();

184
    void ensureUpdated();
185 186 187 188
    QStringList internalProjectFiles() const;
    QStringList internalIncludePaths() const;
    QStringList internalFrameworkPaths() const;
    QByteArray internalDefinedMacros() const;
189

190 191
    void dumpModelManagerConfiguration();

192
private:
193 194
    static QMutex m_instanceMutex;
    static CppModelManager *m_instance;
195

con's avatar
con committed
196
private:
197
    // Snapshot
198
    mutable QMutex m_snapshotMutex;
199
    CPlusPlus::Snapshot m_snapshot;
200

201
    // Project integration
202
    mutable QMutex m_projectMutex;
203 204
    QMap<ProjectExplorer::Project *, ProjectInfo> m_projectToProjectsInfo;
    QMap<QString, QList<CppTools::ProjectPart::Ptr> > m_fileToProjectParts;
205
    QMap<QString, CppTools::ProjectPart::Ptr> m_projectFileToProjectPart;
206
    // The members below are cached/(re)calculated from the projects and/or their project parts
207
    bool m_dirty;
con's avatar
con committed
208
    QStringList m_projectFiles;
209 210 211
    QStringList m_includePaths;
    QStringList m_frameworkPaths;
    QByteArray m_definedMacros;
con's avatar
con committed
212

213 214 215 216
    // Editor integration
    mutable QMutex m_cppEditorSupportsMutex;
    QMap<TextEditor::BaseTextEditor *, CppEditorSupport *> m_cppEditorSupports;
    QSet<AbstractEditorSupport *> m_extraEditorSupports;
217

218
    // Completion & highlighting
219
    QHash<QString, ModelManagerSupport *> m_idTocodeModelSupporter;
220
    QScopedPointer<ModelManagerSupport> m_modelManagerSupportFallback;
221 222

    // Indexing
223
    CppIndexingSupport *m_indexingSupporter;
224
    CppIndexingSupport *m_internalIndexingSupport;
225 226 227 228 229
    bool m_indexerEnabled;

    CppFindReferences *m_findReferences;

    bool m_enableGC;
230
    QTimer *m_delayedGcTimer;
con's avatar
con committed
231
};
232

con's avatar
con committed
233 234 235 236
} // namespace Internal
} // namespace CppTools

#endif // CPPMODELMANAGER_H