cpptoolsplugin.cpp 12.2 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
**
7
** Contact: Nokia Corporation (qt-info@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
29
** Nokia at qt-info@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
#include "cpptoolssettings.h"
con's avatar
con committed
46

47
48
#include <extensionsystem/pluginmanager.h>

con's avatar
con committed
49
50
51
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/coreconstants.h>
52
#include <coreplugin/actionmanager/actionmanager.h>
53
54
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
55
#include <coreplugin/id.h>
con's avatar
con committed
56
#include <coreplugin/editormanager/editormanager.h>
57
#include <coreplugin/progressmanager/progressmanager.h>
58
#include <coreplugin/vcsmanager.h>
59
#include <coreplugin/filemanager.h>
con's avatar
con committed
60
61
#include <cppeditor/cppeditorconstants.h>

62
63
64
65
66
67
68
#include <QtCore/QtConcurrentRun>
#include <QtCore/QFutureSynchronizer>
#include <qtconcurrent/runextensions.h>

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

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

79
80
#include <sstream>

con's avatar
con committed
81
using namespace CppTools::Internal;
82
using namespace CPlusPlus;
con's avatar
con committed
83
84
85

enum { debug = 0 };

86
static CppToolsPlugin *m_instance = 0;
con's avatar
con committed
87

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

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

101
bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
con's avatar
con committed
102
{
103
104
    Q_UNUSED(arguments)
    Q_UNUSED(error)
105

hjk's avatar
hjk committed
106
    Core::ActionManager *am = Core::ICore::actionManager();
con's avatar
con committed
107

108
109
    m_settings = new CppToolsSettings(this); // force registration of cpp tools settings

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

Leandro Melo's avatar
Leandro Melo committed
120
    addAutoReleasedObject(new CppCompletionAssistProvider);
121
122
123
    addAutoReleasedObject(new CppLocatorFilter(m_modelManager));
    addAutoReleasedObject(new CppClassesFilter(m_modelManager));
    addAutoReleasedObject(new CppFunctionsFilter(m_modelManager));
hjk's avatar
hjk committed
124
    addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, Core::ICore::editorManager()));
125
    addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
126
    addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
127
128
    addAutoReleasedObject(new CppCodeStyleSettingsPage);

con's avatar
con committed
129
    // Menus
130
131
    Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
    Core::ActionContainer *mcpptools = am->createMenu(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
132
133
134
135
136
137
    QMenu *menu = mcpptools->menu();
    menu->setTitle(tr("&C++"));
    menu->setEnabled(true);
    mtools->addMenu(mcpptools);

    // Actions
138
    Core::Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
139
140

    QAction *switchAction = new QAction(tr("Switch Header/Source"), this);
141
    Core::Command *command = am->registerAction(switchAction, Constants::SWITCH_HEADER_SOURCE, context, true);
con's avatar
con committed
142
143
144
145
146
147
148
149
150
    command->setDefaultKeySequence(QKeySequence(Qt::Key_F4));
    mcpptools->addAction(command);
    connect(switchAction, SIGNAL(triggered()), this, SLOT(switchHeaderSource()));

    return true;
}

void CppToolsPlugin::extensionsInitialized()
{
151
152
    // The Cpp editor plugin, which is loaded later on, registers the Cpp mime types,
    // so, apply settings here
hjk's avatar
hjk committed
153
    m_fileSettings->fromSettings(Core::ICore::settings());
154
155
    if (!m_fileSettings->applySuffixesToMimeDB())
        qWarning("Unable to apply cpp suffixes to mime database (cpp mime types not found).\n");
con's avatar
con committed
156
157
}

158
ExtensionSystem::IPlugin::ShutdownFlag CppToolsPlugin::aboutToShutdown()
159
{
160
    return SynchronousShutdown;
161
162
}

con's avatar
con committed
163
164
void CppToolsPlugin::switchHeaderSource()
{
165
    Core::EditorManager *editorManager = Core::EditorManager::instance();
166
    Core::IEditor *editor = editorManager->currentEditor();
con's avatar
con committed
167
    QString otherFile = correspondingHeaderOrSource(editor->file()->fileName());
168
    if (!otherFile.isEmpty())
169
        editorManager->openEditor(otherFile);
con's avatar
con committed
170
171
}

172
static QStringList findFilesInProject(const QString &name,
173
                                   const ProjectExplorer::Project *project)
con's avatar
con committed
174
175
{
    if (debug)
176
177
178
        qDebug() << Q_FUNC_INFO << name << project;

    if (!project)
179
        return QStringList();
180
181
182
183
184

    QString pattern = QString(1, QLatin1Char('/'));
    pattern += name;
    const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
    const QStringList::const_iterator pcend = projectFiles.constEnd();
185
    QStringList candidateList;
186
187
    for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
        if (it->endsWith(pattern))
188
            candidateList.append(*it);
con's avatar
con committed
189
    }
190
    return candidateList;
con's avatar
con committed
191
192
193
}

// Figure out file type
194
195
196
197
enum FileType {
    HeaderFile,
    C_SourceFile,
    CPP_SourceFile,
198
    ObjectiveCPP_SourceFile,
199
200
    UnknownType
};
con's avatar
con committed
201
202
203
204
205
206
207
208
209
210
211

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;
212
213
    if (typeName == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
        return ObjectiveCPP_SourceFile;
con's avatar
con committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    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
228
229
230
        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
231
232
233
    case C_SourceFile:
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)).suffixes();
    case CPP_SourceFile:
234
    case ObjectiveCPP_SourceFile:
con's avatar
con committed
235
236
237
238
239
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)).suffixes();
    }
    return QStringList();
}

240
241
242
243
244
245
246
247
248
249
250
251
252
static QStringList baseNameWithAllSuffixes(const QString &baseName, const QStringList &suffixes)
{
    QStringList result;
    const QChar dot = QLatin1Char('.');
    foreach (const QString &suffix, suffixes) {
        QString fileName = baseName;
        fileName += dot;
        fileName += suffix;
        result += fileName;
    }
    return result;
}

253
254
255
256
257
258
259
260
261
static int commonStringLength(const QString &s1, const QString &s2)
{
    int length = qMin(s1.length(), s2.length());
    for (int i = 0; i < length; ++i)
        if (s1[i] != s2[i])
            return i;
    return length;
}

con's avatar
con committed
262
263
QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
{
264
265
266
267
    const QFileInfo fi(fileName);
    if (m_headerSourceMapping.contains(fi.absoluteFilePath()))
        return m_headerSourceMapping.value(fi.absoluteFilePath());

hjk's avatar
hjk committed
268
    const Core::MimeDatabase *mimeDatase = Core::ICore::mimeDatabase();
269
    ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject();
con's avatar
con committed
270
271
272
273
274
275
276
277
278

    const FileType type = fileType(mimeDatase, fi);

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

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

279
    const QString baseName = fi.completeBaseName();
280
    const QString privateHeaderSuffix = QLatin1String("_p");
con's avatar
con committed
281
282
    const QStringList suffixes = matchingCandidateSuffixes(mimeDatase, type);

283
    QStringList candidateFileNames = baseNameWithAllSuffixes(baseName, suffixes);
con's avatar
con committed
284
285
286
287
    if (type == HeaderFile) {
        if (baseName.endsWith(privateHeaderSuffix)) {
            QString sourceBaseName = baseName;
            sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size());
288
            candidateFileNames += baseNameWithAllSuffixes(sourceBaseName, suffixes);
con's avatar
con committed
289
290
        }
    } else {
291
292
293
294
295
296
297
298
        QString privateHeaderBaseName = baseName;
        privateHeaderBaseName.append(privateHeaderSuffix);
        candidateFileNames += baseNameWithAllSuffixes(privateHeaderBaseName, suffixes);
    }

    const QDir absoluteDir = fi.absoluteDir();

    // Try to find a file in the same directory first
299
300
    foreach (const QString &candidateFileName, candidateFileNames) {
        const QFileInfo candidateFi(absoluteDir, candidateFileName);
301
302
303
        if (candidateFi.isFile()) {
            m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
            m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
304
            return candidateFi.absoluteFilePath();
305
        }
306
307
308
309
    }

    // Find files in the project
    if (project) {
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
        QString bestFileName;
        int compareValue = 0;
        foreach (const QString &candidateFileName, candidateFileNames) {
            const QStringList projectFiles = findFilesInProject(candidateFileName, project);
            // Find the file having the most common path with fileName
            foreach (const QString projectFile, projectFiles) {
                int value = commonStringLength(fileName, projectFile);
                if (value > compareValue) {
                    compareValue = value;
                    bestFileName = projectFile;
                }
            }
        }
        if (!bestFileName.isEmpty()) {
            const QFileInfo candidateFi(bestFileName);
325
            QTC_ASSERT(candidateFi.isFile(), return QString());
326
327
            m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
            m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
328
            return candidateFi.absoluteFilePath();
con's avatar
con committed
329
330
        }
    }
331

con's avatar
con committed
332
333
334
    return QString();
}

335
QString CppToolsPlugin::correspondingHeaderOrSource(const QString &fileName)
con's avatar
con committed
336
{
337
    const QString rc = m_instance->correspondingHeaderOrSourceI(fileName);
con's avatar
con committed
338
339
340
341
342
343
    if (debug)
        qDebug() << Q_FUNC_INFO << fileName << rc;
    return rc;
}

Q_EXPORT_PLUGIN(CppToolsPlugin)