cpptoolsplugin.cpp 12.3 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** 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

30
#include "cpptoolsplugin.h"
31
#include "completionsettingspage.h"
32
#include "cppfilesettingspage.h"
33
#include "cppcodestylesettingspage.h"
34
#include "cppclassesfilter.h"
35
#include "cppfunctionsfilter.h"
36
#include "cppcurrentdocumentfilter.h"
con's avatar
con committed
37
38
#include "cppmodelmanager.h"
#include "cpptoolsconstants.h"
con's avatar
con committed
39
#include "cpplocatorfilter.h"
40
#include "symbolsfindfilter.h"
41
#include "cpptoolssettings.h"
42
#include "cpptoolsreuse.h"
con's avatar
con committed
43

44
45
#include <extensionsystem/pluginmanager.h>

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

59
60
#include <QtConcurrentRun>
#include <QFutureSynchronizer>
61
#include <utils/runextensions.h>
62
63
64
65

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

68
69
70
71
72
73
74
#include <QtPlugin>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QSettings>
#include <QMenu>
#include <QAction>
con's avatar
con committed
75

76
77
78
#include <sstream>

using namespace CPlusPlus;
con's avatar
con committed
79

80
81
82
namespace CppTools {
namespace Internal {

con's avatar
con committed
83
84
enum { debug = 0 };

85
static CppToolsPlugin *m_instance = 0;
86
static QHash<QString, QString> m_headerSourceMapping;
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

106
107
    m_settings = new CppToolsSettings(this); // force registration of cpp tools settings

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

117
118
119
    addAutoReleasedObject(new CppLocatorFilter(m_modelManager));
    addAutoReleasedObject(new CppClassesFilter(m_modelManager));
    addAutoReleasedObject(new CppFunctionsFilter(m_modelManager));
hjk's avatar
hjk committed
120
    addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, Core::ICore::editorManager()));
121
    addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
122
    addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
123
124
    addAutoReleasedObject(new CppCodeStyleSettingsPage);

con's avatar
con committed
125
    // Menus
Eike Ziller's avatar
Eike Ziller committed
126
127
    Core::ActionContainer *mtools = Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
    Core::ActionContainer *mcpptools = Core::ActionManager::createMenu(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
128
129
130
131
132
133
    QMenu *menu = mcpptools->menu();
    menu->setTitle(tr("&C++"));
    menu->setEnabled(true);
    mtools->addMenu(mcpptools);

    // Actions
134
    Core::Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
135
136

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

    return true;
}

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

154
ExtensionSystem::IPlugin::ShutdownFlag CppToolsPlugin::aboutToShutdown()
155
{
156
    return SynchronousShutdown;
157
158
}

con's avatar
con committed
159
160
void CppToolsPlugin::switchHeaderSource()
{
hjk's avatar
hjk committed
161
    Core::IEditor *editor = Core::EditorManager::currentEditor();
162
    QString otherFile = correspondingHeaderOrSource(editor->document()->fileName());
163
    if (!otherFile.isEmpty())
hjk's avatar
hjk committed
164
        Core::EditorManager::openEditor(otherFile);
con's avatar
con committed
165
166
}

167
static QStringList findFilesInProject(const QString &name,
168
                                   const ProjectExplorer::Project *project)
con's avatar
con committed
169
170
{
    if (debug)
171
172
173
        qDebug() << Q_FUNC_INFO << name << project;

    if (!project)
174
        return QStringList();
175
176
177
178
179

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

// Figure out file type
189
190
191
192
enum FileType {
    HeaderFile,
    C_SourceFile,
    CPP_SourceFile,
193
    ObjectiveCPP_SourceFile,
194
195
    UnknownType
};
con's avatar
con committed
196
197
198
199
200
201
202
203
204
205
206

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

235
236
237
238
239
240
241
242
243
244
245
246
247
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;
}

248
249
250
251
252
253
254
255
256
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;
}

257
258
259
} // namespace Internal

QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
con's avatar
con committed
260
{
261
    using namespace Internal;
262

hjk's avatar
hjk committed
263
    const Core::MimeDatabase *mimeDatase = Core::ICore::mimeDatabase();
264
265
266
267
268
269
    const QFileInfo fi(fileName);
    if (m_headerSourceMapping.contains(fi.absoluteFilePath())) {
        if (wasHeader)
            *wasHeader = fileType(mimeDatase, fi) == HeaderFile;
        return m_headerSourceMapping.value(fi.absoluteFilePath());
    }
con's avatar
con committed
270

271
272
273
    FileType type = fileType(mimeDatase, fi);
    if (wasHeader)
        *wasHeader = type == HeaderFile;
con's avatar
con committed
274
275
276
277
278
279
280

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

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

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

285
    QStringList candidateFileNames = baseNameWithAllSuffixes(baseName, suffixes);
con's avatar
con committed
286
287
288
289
    if (type == HeaderFile) {
        if (baseName.endsWith(privateHeaderSuffix)) {
            QString sourceBaseName = baseName;
            sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size());
290
            candidateFileNames += baseNameWithAllSuffixes(sourceBaseName, suffixes);
con's avatar
con committed
291
292
        }
    } else {
293
294
295
296
297
298
299
300
        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
301
302
    foreach (const QString &candidateFileName, candidateFileNames) {
        const QFileInfo candidateFi(absoluteDir, candidateFileName);
303
304
        if (candidateFi.isFile()) {
            m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
305
306
            if (type != HeaderFile || !baseName.endsWith(privateHeaderSuffix))
                m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
307
            return candidateFi.absoluteFilePath();
308
        }
309
310
311
    }

    // Find files in the project
312
    ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject();
313
    if (project) {
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
        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);
329
            QTC_ASSERT(candidateFi.isFile(), return QString());
330
331
            m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
            m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
332
            return candidateFi.absoluteFilePath();
con's avatar
con committed
333
334
        }
    }
335

con's avatar
con committed
336
337
338
    return QString();
}

339
} // namespace CppTools
con's avatar
con committed
340

341
Q_EXPORT_PLUGIN(CppTools::Internal::CppToolsPlugin)