cpptoolsplugin.cpp 11.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

33
#include "cpptoolsplugin.h"
34
#include "completionsettingspage.h"
35
#include "cppfilesettingspage.h"
36
#include "cppcodestylesettingspage.h"
37
#include "cppclassesfilter.h"
38
#include "cppfunctionsfilter.h"
39
#include "cppcurrentdocumentfilter.h"
con's avatar
con committed
40
41
#include "cppmodelmanager.h"
#include "cpptoolsconstants.h"
con's avatar
con committed
42
#include "cpplocatorfilter.h"
43
#include "symbolsfindfilter.h"
Leandro Melo's avatar
Leandro Melo committed
44
#include "cppcompletionassist.h"
45
46
#include "cpptoolssettings.h"
#include "cppcodestylesettingsfactory.h"
con's avatar
con committed
47

48
49
#include <extensionsystem/pluginmanager.h>

con's avatar
con committed
50
51
52
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/coreconstants.h>
53
#include <coreplugin/actionmanager/actionmanager.h>
54
55
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
56
#include <coreplugin/uniqueidmanager.h>
con's avatar
con committed
57
#include <coreplugin/editormanager/editormanager.h>
58
#include <coreplugin/progressmanager/progressmanager.h>
59
#include <coreplugin/vcsmanager.h>
60
#include <coreplugin/filemanager.h>
61
#include <texteditor/texteditorsettings.h>
62
63
#include <texteditor/tabsettings.h>
#include <texteditor/codestylepreferencesmanager.h>
con's avatar
con committed
64
65
#include <cppeditor/cppeditorconstants.h>

66
67
68
69
70
71
72
73
#include <QtCore/QtConcurrentRun>
#include <QtCore/QFutureSynchronizer>
#include <qtconcurrent/runextensions.h>

#include <find/ifindfilter.h>
#include <find/searchresultwindow.h>
#include <utils/filesearch.h>

74
#include <QtCore/QtPlugin>
con's avatar
con committed
75
76
77
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QDebug>
78
#include <QtCore/QSettings>
con's avatar
con committed
79
80
81
#include <QtGui/QMenu>
#include <QtGui/QAction>

82
83
#include <sstream>

con's avatar
con committed
84
using namespace CppTools::Internal;
85
using namespace CPlusPlus;
con's avatar
con committed
86
87
88
89
90

enum { debug = 0 };

CppToolsPlugin *CppToolsPlugin::m_instance = 0;

91
92
93
CppToolsPlugin::CppToolsPlugin() :
    m_modelManager(0),
    m_fileSettings(new CppFileSettings)
con's avatar
con committed
94
95
96
97
98
99
100
101
102
103
{
    m_instance = this;
}

CppToolsPlugin::~CppToolsPlugin()
{
    m_instance = 0;
    m_modelManager = 0; // deleted automatically
}

104
bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
con's avatar
con committed
105
{
106
107
    Q_UNUSED(arguments)
    Q_UNUSED(error)
108
109
    Core::ICore *core = Core::ICore::instance();
    Core::ActionManager *am = core->actionManager();
con's avatar
con committed
110

111
112
    m_settings = new CppToolsSettings(this); // force registration of cpp tools settings

con's avatar
con committed
113
114
    // Objects
    m_modelManager = new CppModelManager(this);
hjk's avatar
hjk committed
115
    Core::VcsManager *vcsManager = core->vcsManager();
116
    Core::FileManager *fileManager = core->fileManager();
117
118
    connect(vcsManager, SIGNAL(repositoryChanged(QString)),
            m_modelManager, SLOT(updateModifiedSourceFiles()));
119
120
    connect(fileManager, SIGNAL(filesChangedInternally(QStringList)),
            m_modelManager, SLOT(updateSourceFiles(QStringList)));
con's avatar
con committed
121
    addAutoReleasedObject(m_modelManager);
Roberto Raggi's avatar
Roberto Raggi committed
122

Leandro Melo's avatar
Leandro Melo committed
123
    addAutoReleasedObject(new CppCompletionAssistProvider);
124
125
126
    addAutoReleasedObject(new CppLocatorFilter(m_modelManager));
    addAutoReleasedObject(new CppClassesFilter(m_modelManager));
    addAutoReleasedObject(new CppFunctionsFilter(m_modelManager));
127
    addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, core->editorManager()));
128
    addAutoReleasedObject(new CompletionSettingsPage);
129
    addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
130
    addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
131
132
133
134
    addAutoReleasedObject(new CppCodeStyleSettingsPage);

    TextEditor::CodeStylePreferencesManager::instance()->registerFactory(
                new CppTools::CppCodeStylePreferencesFactory());
con's avatar
con committed
135
136

    // Menus
137
138
    Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
    Core::ActionContainer *mcpptools = am->createMenu(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
139
140
141
142
143
144
    QMenu *menu = mcpptools->menu();
    menu->setTitle(tr("&C++"));
    menu->setEnabled(true);
    mtools->addMenu(mcpptools);

    // Actions
145
    Core::Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
146
147

    QAction *switchAction = new QAction(tr("Switch Header/Source"), this);
148
    Core::Command *command = am->registerAction(switchAction, Constants::SWITCH_HEADER_SOURCE, context, true);
con's avatar
con committed
149
150
151
152
153
154
155
156
157
    command->setDefaultKeySequence(QKeySequence(Qt::Key_F4));
    mcpptools->addAction(command);
    connect(switchAction, SIGNAL(triggered()), this, SLOT(switchHeaderSource()));

    return true;
}

void CppToolsPlugin::extensionsInitialized()
{
158
159
160
161
162
    // The Cpp editor plugin, which is loaded later on, registers the Cpp mime types,
    // so, apply settings here
    m_fileSettings->fromSettings(Core::ICore::instance()->settings());
    if (!m_fileSettings->applySuffixesToMimeDB())
        qWarning("Unable to apply cpp suffixes to mime database (cpp mime types not found).\n");
con's avatar
con committed
163
164
}

165
ExtensionSystem::IPlugin::ShutdownFlag CppToolsPlugin::aboutToShutdown()
166
{
167
    return SynchronousShutdown;
168
169
}

con's avatar
con committed
170
171
void CppToolsPlugin::switchHeaderSource()
{
172
    Core::EditorManager *editorManager = Core::EditorManager::instance();
173
    Core::IEditor *editor = editorManager->currentEditor();
con's avatar
con committed
174
    QString otherFile = correspondingHeaderOrSource(editor->file()->fileName());
175
    if (!otherFile.isEmpty())
176
        editorManager->openEditor(otherFile);
con's avatar
con committed
177
178
179
180
181
182
183
184
}

QFileInfo CppToolsPlugin::findFile(const QDir &dir, const QString &name,
                                   const ProjectExplorer::Project *project) const
{
    if (debug)
        qDebug() << Q_FUNC_INFO << dir << name;

185
186
    QFileInfo fileInSameDir(dir, name);
    if (project && !fileInSameDir.isFile()) {
con's avatar
con committed
187
188
189
190
191
192
193
194
195
        QString pattern = QString(1, QLatin1Char('/'));
        pattern += name;
        const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
        const QStringList::const_iterator pcend = projectFiles.constEnd();
        for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it)
            if (it->endsWith(pattern))
                return QFileInfo(*it);
        return QFileInfo();
    }
196
    return fileInSameDir;
con's avatar
con committed
197
198
199
}

// Figure out file type
200
201
202
203
enum FileType {
    HeaderFile,
    C_SourceFile,
    CPP_SourceFile,
204
    ObjectiveCPP_SourceFile,
205
206
    UnknownType
};
con's avatar
con committed
207
208
209
210
211
212
213
214
215
216
217

static inline FileType fileType(const Core::MimeDatabase *mimeDatase, const  QFileInfo & fi)
{
    const Core::MimeType mimeType = mimeDatase->findByFile(fi);
    if (!mimeType)
        return UnknownType;
    const QString typeName = mimeType.type();
    if (typeName == QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE))
        return C_SourceFile;
    if (typeName == QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE))
        return CPP_SourceFile;
218
219
    if (typeName == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
        return ObjectiveCPP_SourceFile;
con's avatar
con committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    if (typeName == QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)
        || typeName == QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE))
        return HeaderFile;
    return UnknownType;
}

// Return the suffixes that should be checked when trying to find a
// source belonging to a header and vice versa
static QStringList matchingCandidateSuffixes(const Core::MimeDatabase *mimeDatase, FileType type)
{
    switch (type) {
    case UnknownType:
        break;
    case HeaderFile: // Note that C/C++ headers are undistinguishable
234
235
236
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::C_SOURCE_MIMETYPE)).suffixes()
               + mimeDatase->findByType(QLatin1String(CppTools::Constants::CPP_SOURCE_MIMETYPE)).suffixes()
               + mimeDatase->findByType(QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)).suffixes();
con's avatar
con committed
237
238
239
    case C_SourceFile:
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)).suffixes();
    case CPP_SourceFile:
240
    case ObjectiveCPP_SourceFile:
con's avatar
con committed
241
242
243
244
245
246
247
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)).suffixes();
    }
    return QStringList();
}

QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
{
248
    const Core::ICore *core = Core::ICore::instance();
con's avatar
con committed
249
250
    const Core::MimeDatabase *mimeDatase = core->mimeDatabase();
    ProjectExplorer::ProjectExplorerPlugin *explorer =
251
       ProjectExplorer::ProjectExplorerPlugin::instance();
con's avatar
con committed
252
253
254
255
256
257
258
259
260
261
262
263
    ProjectExplorer::Project *project = (explorer ? explorer->currentProject() : 0);

    const QFileInfo fi(fileName);
    const FileType type = fileType(mimeDatase, fi);

    if (debug)
        qDebug() << Q_FUNC_INFO << fileName <<  type;

    if (type == UnknownType)
        return QString();

    const QDir absoluteDir = fi.absoluteDir();
264
    const QString baseName = fi.completeBaseName();
con's avatar
con committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    const QStringList suffixes = matchingCandidateSuffixes(mimeDatase, type);

    const QString privateHeaderSuffix = QLatin1String("_p");
    const QChar dot = QLatin1Char('.');
    // Check base matches 'source.h'-> 'source.cpp' and vice versa
    const QStringList::const_iterator scend = suffixes.constEnd();
    for (QStringList::const_iterator it = suffixes.constBegin(); it != scend; ++it) {
        QString candidate = baseName;
        candidate += dot;
        candidate += *it;
        const QFileInfo candidateFi = findFile(absoluteDir, candidate, project);
        if (candidateFi.isFile())
            return candidateFi.absoluteFilePath();
    }
    if (type == HeaderFile) {
        // 'source_p.h': try 'source.cpp'
        if (baseName.endsWith(privateHeaderSuffix)) {
            QString sourceBaseName = baseName;
            sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size());
            for (QStringList::const_iterator it = suffixes.constBegin(); it != scend; ++it) {
                QString candidate = sourceBaseName;
                candidate += dot;
                candidate += *it;
                const QFileInfo candidateFi = findFile(absoluteDir, candidate, project);
                if (candidateFi.isFile())
                    return candidateFi.absoluteFilePath();
            }
        }
    } else {
        // 'source.cpp': try 'source_p.h'
        const QStringList::const_iterator scend = suffixes.constEnd();
        for (QStringList::const_iterator it = suffixes.constBegin(); it != scend; ++it) {
            QString candidate = baseName;
            candidate += privateHeaderSuffix;
            candidate += dot;
            candidate += *it;
            const QFileInfo candidateFi = findFile(absoluteDir, candidate, project);
            if (candidateFi.isFile())
                return candidateFi.absoluteFilePath();
        }
    }
    return QString();
}

QString CppToolsPlugin::correspondingHeaderOrSource(const QString &fileName) const
{
    const QString rc = correspondingHeaderOrSourceI(fileName);
    if (debug)
        qDebug() << Q_FUNC_INFO << fileName << rc;
    return rc;
}

Q_EXPORT_PLUGIN(CppToolsPlugin)