cpptoolsplugin.cpp 17.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
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
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

31
#include "cpptoolsconstants.h"
32
#include "cpptoolsplugin.h"
33
#include "cppfilesettingspage.h"
34
#include "cppcodemodelsettingspage.h"
35
#include "cppcodestylesettingspage.h"
36
#include "cppclassesfilter.h"
37
#include "cppfunctionsfilter.h"
38
#include "cppcurrentdocumentfilter.h"
con's avatar
con committed
39
#include "cppmodelmanager.h"
40
#include "cpplocatorfilter.h"
41
#include "symbolsfindfilter.h"
42
#include "cpptoolsjsextension.h"
43
#include "cpptoolssettings.h"
44
#include "cpptoolsreuse.h"
45
#include "cppprojectfile.h"
46
#include "cpplocatordata.h"
47
#include "cppincludesfilter.h"
con's avatar
con committed
48

49
#include <coreplugin/actionmanager/actioncontainer.h>
50
#include <coreplugin/actionmanager/actionmanager.h>
51
#include <coreplugin/editormanager/editormanager.h>
52 53 54
#include <coreplugin/coreconstants.h>
#include <coreplugin/documentmanager.h>
#include <coreplugin/icore.h>
55
#include <coreplugin/idocument.h>
56
#include <coreplugin/jsexpander.h>
57
#include <coreplugin/vcsmanager.h>
con's avatar
con committed
58
#include <cppeditor/cppeditorconstants.h>
59
#include <projectexplorer/project.h>
60
#include <projectexplorer/projecttree.h>
61

Orgad Shaneh's avatar
Orgad Shaneh committed
62
#include <utils/fileutils.h>
63
#include <utils/hostosinfo.h>
hjk's avatar
hjk committed
64
#include <utils/macroexpander.h>
Eike Ziller's avatar
Eike Ziller committed
65
#include <utils/mimetypes/mimedatabase.h>
66
#include <utils/qtcassert.h>
67

68 69 70 71 72 73
#include <QtPlugin>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QMenu>
#include <QAction>
74

hjk's avatar
hjk committed
75
using namespace Core;
76
using namespace CPlusPlus;
con's avatar
con committed
77

78 79 80
namespace CppTools {
namespace Internal {

con's avatar
con committed
81 82
enum { debug = 0 };

83
static CppToolsPlugin *m_instance = 0;
84
static QHash<QString, QString> m_headerSourceMapping;
con's avatar
con committed
85

86 87 88
CppToolsPlugin::CppToolsPlugin()
    : m_fileSettings(new CppFileSettings)
    , m_codeModelSettings(new CppCodeModelSettings)
con's avatar
con committed
89 90 91 92 93 94 95 96 97
{
    m_instance = this;
}

CppToolsPlugin::~CppToolsPlugin()
{
    m_instance = 0;
}

98 99 100 101 102
CppToolsPlugin *CppToolsPlugin::instance()
{
    return m_instance;
}

103 104 105 106 107
void CppToolsPlugin::clearHeaderSourceCache()
{
    m_headerSourceMapping.clear();
}

108 109 110 111 112 113 114 115 116 117
Utils::FileName CppToolsPlugin::licenseTemplatePath()
{
    return Utils::FileName::fromString(m_instance->m_fileSettings->licenseTemplatePath);
}

QString CppToolsPlugin::licenseTemplate()
{
    return m_instance->m_fileSettings->licenseTemplate();
}

118 119 120 121 122 123 124 125 126 127
const QStringList &CppToolsPlugin::headerSearchPaths()
{
    return m_instance->m_fileSettings->headerSearchPaths;
}

const QStringList &CppToolsPlugin::sourceSearchPaths()
{
    return m_instance->m_fileSettings->sourceSearchPaths;
}

128 129 130 131 132 133 134 135 136 137
const QStringList &CppToolsPlugin::headerPrefixes()
{
    return m_instance->m_fileSettings->headerPrefixes;
}

const QStringList &CppToolsPlugin::sourcePrefixes()
{
    return m_instance->m_fileSettings->sourcePrefixes;
}

138
bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
con's avatar
con committed
139
{
140 141
    Q_UNUSED(arguments)
    Q_UNUSED(error)
142

143 144
    CppModelManager::instance()->setParent(this);

145 146
    m_settings = new CppToolsSettings(this); // force registration of cpp tools settings

con's avatar
con committed
147
    // Objects
148
    CppModelManager *modelManager = CppModelManager::instance();
hjk's avatar
hjk committed
149
    connect(VcsManager::instance(), SIGNAL(repositoryChanged(QString)),
150
            modelManager, SLOT(updateModifiedSourceFiles()));
151 152 153 154
    connect(DocumentManager::instance(), &DocumentManager::filesChangedInternally,
            [=](const QStringList &files) {
        modelManager->updateSourceFiles(files.toSet());
    });
Roberto Raggi's avatar
Roberto Raggi committed
155

156 157
    m_codeModelSettings->fromSettings(ICore::settings());

158
    JsExpander::registerQObjectForJs(QLatin1String("Cpp"), new CppToolsJsExtension);
159

160
    CppLocatorData *locatorData = new CppLocatorData;
Montel Laurent's avatar
Montel Laurent committed
161 162
    connect(modelManager, &CppModelManager::documentUpdated,
            locatorData, &CppLocatorData::onDocumentUpdated);
163

Montel Laurent's avatar
Montel Laurent committed
164 165
    connect(modelManager, &CppModelManager::aboutToRemoveFiles,
            locatorData, &CppLocatorData::onAboutToRemoveFiles);
166

167 168 169
    addAutoReleasedObject(locatorData);
    addAutoReleasedObject(new CppLocatorFilter(locatorData));
    addAutoReleasedObject(new CppClassesFilter(locatorData));
170
    addAutoReleasedObject(new CppIncludesFilter);
171
    addAutoReleasedObject(new CppFunctionsFilter(locatorData));
172
    addAutoReleasedObject(new CppCurrentDocumentFilter(modelManager, m_stringTable));
173
    addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
174
    addAutoReleasedObject(new CppCodeModelSettingsPage(m_codeModelSettings));
175
    addAutoReleasedObject(new SymbolsFindFilter(modelManager));
176 177
    addAutoReleasedObject(new CppCodeStyleSettingsPage);

con's avatar
con committed
178
    // Menus
hjk's avatar
hjk committed
179 180
    ActionContainer *mtools = ActionManager::actionContainer(Core::Constants::M_TOOLS);
    ActionContainer *mcpptools = ActionManager::createMenu(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
181 182 183 184 185 186
    QMenu *menu = mcpptools->menu();
    menu->setTitle(tr("&C++"));
    menu->setEnabled(true);
    mtools->addMenu(mcpptools);

    // Actions
187
    Context context(CppEditor::Constants::CPPEDITOR_ID);
con's avatar
con committed
188 189

    QAction *switchAction = new QAction(tr("Switch Header/Source"), this);
hjk's avatar
hjk committed
190
    Command *command = ActionManager::registerAction(switchAction, Constants::SWITCH_HEADER_SOURCE, context, true);
con's avatar
con committed
191 192
    command->setDefaultKeySequence(QKeySequence(Qt::Key_F4));
    mcpptools->addAction(command);
Montel Laurent's avatar
Montel Laurent committed
193 194
    connect(switchAction, &QAction::triggered,
            this, &CppToolsPlugin::switchHeaderSource);
con's avatar
con committed
195

196
    QAction *openInNextSplitAction = new QAction(tr("Open Corresponding Header/Source in Next Split"), this);
hjk's avatar
hjk committed
197
    command = ActionManager::registerAction(openInNextSplitAction, Constants::OPEN_HEADER_SOURCE_IN_NEXT_SPLIT, context, true);
198 199 200
    command->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                                ? tr("Meta+E, F4")
                                                : tr("Ctrl+E, F4")));
201
    mcpptools->addAction(command);
Montel Laurent's avatar
Montel Laurent committed
202 203
    connect(openInNextSplitAction, &QAction::triggered,
            this, &CppToolsPlugin::switchHeaderSourceInNextSplit);
204

hjk's avatar
hjk committed
205
    Utils::MacroExpander *expander = Utils::globalMacroExpander();
206 207 208 209 210 211
    expander->registerVariable("Cpp:LicenseTemplate",
                               tr("The license template."),
                               [this]() { return CppToolsPlugin::licenseTemplate(); });
    expander->registerFileVariables("Cpp:LicenseTemplatePath",
                                    tr("The configured path to the license template"),
                                    [this]() { return CppToolsPlugin::licenseTemplatePath().toString(); });
212

con's avatar
con committed
213 214 215 216 217
    return true;
}

void CppToolsPlugin::extensionsInitialized()
{
218 219
    // The Cpp editor plugin, which is loaded later on, registers the Cpp mime types,
    // so, apply settings here
hjk's avatar
hjk committed
220
    m_fileSettings->fromSettings(ICore::settings());
221 222
    if (!m_fileSettings->applySuffixesToMimeDB())
        qWarning("Unable to apply cpp suffixes to mime database (cpp mime types not found).\n");
con's avatar
con committed
223 224
}

225
ExtensionSystem::IPlugin::ShutdownFlag CppToolsPlugin::aboutToShutdown()
226
{
227
    return SynchronousShutdown;
228 229
}

230 231 232 233 234
QSharedPointer<CppCodeModelSettings> CppToolsPlugin::codeModelSettings() const
{
    return m_codeModelSettings;
}

235 236 237 238 239
StringTable &CppToolsPlugin::stringTable()
{
    return instance()->m_stringTable;
}

con's avatar
con committed
240 241
void CppToolsPlugin::switchHeaderSource()
{
242
    CppTools::switchHeaderSource();
243 244 245 246 247
}

void CppToolsPlugin::switchHeaderSourceInNextSplit()
{
    QString otherFile = correspondingHeaderOrSource(
248
                EditorManager::currentDocument()->filePath().toString());
249
    if (!otherFile.isEmpty())
hjk's avatar
hjk committed
250
        EditorManager::openEditor(otherFile, Id(), EditorManager::OpenInOtherSplit);
con's avatar
con committed
251 252
}

253
static QStringList findFilesInProject(const QString &name,
254
                                   const ProjectExplorer::Project *project)
con's avatar
con committed
255 256
{
    if (debug)
257 258 259
        qDebug() << Q_FUNC_INFO << name << project;

    if (!project)
260
        return QStringList();
261 262 263 264 265

    QString pattern = QString(1, QLatin1Char('/'));
    pattern += name;
    const QStringList projectFiles = project->files(ProjectExplorer::Project::AllFiles);
    const QStringList::const_iterator pcend = projectFiles.constEnd();
266
    QStringList candidateList;
267
    for (QStringList::const_iterator it = projectFiles.constBegin(); it != pcend; ++it) {
268
        if (it->endsWith(pattern, Utils::HostOsInfo::fileNameCaseSensitivity()))
269
            candidateList.append(*it);
con's avatar
con committed
270
    }
271
    return candidateList;
con's avatar
con committed
272 273 274 275
}

// Return the suffixes that should be checked when trying to find a
// source belonging to a header and vice versa
276
static QStringList matchingCandidateSuffixes(ProjectFile::Kind kind)
con's avatar
con committed
277
{
Eike Ziller's avatar
Eike Ziller committed
278
    Utils::MimeDatabase mdb;
279 280 281 282 283 284
    switch (kind) {
     // Note that C/C++ headers are undistinguishable
    case ProjectFile::CHeader:
    case ProjectFile::CXXHeader:
    case ProjectFile::ObjCHeader:
    case ProjectFile::ObjCXXHeader:
Eike Ziller's avatar
Eike Ziller committed
285 286 287 288
        return mdb.mimeTypeForName(QLatin1String(Constants::C_SOURCE_MIMETYPE)).suffixes()
                + mdb.mimeTypeForName(QLatin1String(Constants::CPP_SOURCE_MIMETYPE)).suffixes()
                + mdb.mimeTypeForName(QLatin1String(Constants::OBJECTIVE_C_SOURCE_MIMETYPE)).suffixes()
                + mdb.mimeTypeForName(QLatin1String(Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)).suffixes();
289 290
    case ProjectFile::CSource:
    case ProjectFile::ObjCSource:
Eike Ziller's avatar
Eike Ziller committed
291
        return mdb.mimeTypeForName(QLatin1String(Constants::C_HEADER_MIMETYPE)).suffixes();
292 293 294 295
    case ProjectFile::CXXSource:
    case ProjectFile::ObjCXXSource:
    case ProjectFile::CudaSource:
    case ProjectFile::OpenCLSource:
Eike Ziller's avatar
Eike Ziller committed
296
        return mdb.mimeTypeForName(QLatin1String(Constants::CPP_HEADER_MIMETYPE)).suffixes();
297 298
    default:
        return QStringList();
con's avatar
con committed
299 300 301
    }
}

302 303 304 305 306 307 308 309 310 311 312 313 314
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;
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
static QStringList baseNamesWithAllPrefixes(const QStringList &baseNames, bool isHeader)
{
    QStringList result;
    const QStringList &sourcePrefixes = m_instance->sourcePrefixes();
    const QStringList &headerPrefixes = m_instance->headerPrefixes();

    foreach (const QString &name, baseNames) {
        foreach (const QString &prefix, isHeader ? headerPrefixes : sourcePrefixes) {
            if (name.startsWith(prefix)) {
                QString nameWithoutPrefix = name.mid(prefix.size());
                result += nameWithoutPrefix;
                foreach (const QString &prefix, isHeader ? sourcePrefixes : headerPrefixes)
                    result += prefix + nameWithoutPrefix;
            }
        }
        foreach (const QString &prefix, isHeader ? sourcePrefixes : headerPrefixes)
            result += prefix + name;

    }
    return result;
}

337 338 339 340 341 342 343 344
static QStringList baseDirWithAllDirectories(const QDir &baseDir, const QStringList &directories)
{
    QStringList result;
    foreach (const QString &dir, directories)
        result << QDir::cleanPath(baseDir.absoluteFilePath(dir));
    return result;
}

345
static int commonFilePathLength(const QString &s1, const QString &s2)
346 347 348
{
    int length = qMin(s1.length(), s2.length());
    for (int i = 0; i < length; ++i)
349 350 351 352 353 354 355
        if (Utils::HostOsInfo::fileNameCaseSensitivity() == Qt::CaseSensitive) {
            if (s1[i] != s2[i])
                return i;
        } else {
            if (s1[i].toLower() != s2[i].toLower())
                return i;
        }
356 357 358
    return length;
}

359 360 361 362 363 364 365 366 367 368 369
static QString correspondingHeaderOrSourceInProject(const QFileInfo &fileInfo,
                                                    const QStringList &candidateFileNames,
                                                    const ProjectExplorer::Project *project)
{
    QString bestFileName;
    int compareValue = 0;
    const QString filePath = fileInfo.filePath();
    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) {
370
            int value = commonFilePathLength(filePath, projectFile);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
            if (value > compareValue) {
                compareValue = value;
                bestFileName = projectFile;
            }
        }
    }
    if (!bestFileName.isEmpty()) {
        const QFileInfo candidateFi(bestFileName);
        QTC_ASSERT(candidateFi.isFile(), return QString());
        m_headerSourceMapping[fileInfo.absoluteFilePath()] = candidateFi.absoluteFilePath();
        m_headerSourceMapping[candidateFi.absoluteFilePath()] = fileInfo.absoluteFilePath();
        return candidateFi.absoluteFilePath();
    }

    return QString();
}

388 389 390
} // namespace Internal

QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
con's avatar
con committed
391
{
392
    using namespace Internal;
393

394
    const QFileInfo fi(fileName);
395 396
    ProjectFile::Kind kind = ProjectFile::classify(fileName);
    const bool isHeader = ProjectFile::isHeader(kind);
397
    if (wasHeader)
398 399
        *wasHeader = isHeader;
    if (m_headerSourceMapping.contains(fi.absoluteFilePath()))
400
        return m_headerSourceMapping.value(fi.absoluteFilePath());
con's avatar
con committed
401 402

    if (debug)
403
        qDebug() << Q_FUNC_INFO << fileName <<  kind;
con's avatar
con committed
404

405
    if (kind == ProjectFile::Unclassified)
con's avatar
con committed
406 407
        return QString();

408
    const QString baseName = fi.completeBaseName();
409
    const QString privateHeaderSuffix = QLatin1String("_p");
410
    const QStringList suffixes = matchingCandidateSuffixes(kind);
con's avatar
con committed
411

412
    QStringList candidateFileNames = baseNameWithAllSuffixes(baseName, suffixes);
413
    if (isHeader) {
con's avatar
con committed
414 415 416
        if (baseName.endsWith(privateHeaderSuffix)) {
            QString sourceBaseName = baseName;
            sourceBaseName.truncate(sourceBaseName.size() - privateHeaderSuffix.size());
417
            candidateFileNames += baseNameWithAllSuffixes(sourceBaseName, suffixes);
con's avatar
con committed
418 419
        }
    } else {
420 421 422 423 424 425
        QString privateHeaderBaseName = baseName;
        privateHeaderBaseName.append(privateHeaderSuffix);
        candidateFileNames += baseNameWithAllSuffixes(privateHeaderBaseName, suffixes);
    }

    const QDir absoluteDir = fi.absoluteDir();
426 427 428 429 430 431
    QStringList candidateDirs(absoluteDir.absolutePath());
    // If directory is not root, try matching against its siblings
    const QStringList searchPaths = isHeader ? m_instance->sourceSearchPaths()
                                             : m_instance->headerSearchPaths();
    candidateDirs += baseDirWithAllDirectories(absoluteDir, searchPaths);

432 433
    candidateFileNames += baseNamesWithAllPrefixes(candidateFileNames, isHeader);

434 435 436 437
    // Try to find a file in the same or sibling directories first
    foreach (const QString &candidateDir, candidateDirs) {
        foreach (const QString &candidateFileName, candidateFileNames) {
            const QString candidateFilePath = candidateDir + QLatin1Char('/') + candidateFileName;
Orgad Shaneh's avatar
Orgad Shaneh committed
438
            const QString normalized = Utils::FileUtils::normalizePathName(candidateFilePath);
439 440 441 442 443 444 445
            const QFileInfo candidateFi(normalized);
            if (candidateFi.isFile()) {
                m_headerSourceMapping[fi.absoluteFilePath()] = candidateFi.absoluteFilePath();
                if (!isHeader || !baseName.endsWith(privateHeaderSuffix))
                    m_headerSourceMapping[candidateFi.absoluteFilePath()] = fi.absoluteFilePath();
                return candidateFi.absoluteFilePath();
            }
446
        }
447 448
    }

449
    // Find files in the current project
450
    ProjectExplorer::Project *currentProject = ProjectExplorer::ProjectTree::currentProject();
451 452 453 454 455 456 457
    if (currentProject) {
        const QString path = correspondingHeaderOrSourceInProject(fi, candidateFileNames,
                                                                  currentProject);
        if (!path.isEmpty())
            return path;

    // Find files in other projects
458 459
    } else {
        CppModelManager *modelManager = CppModelManager::instance();
460 461
        QList<ProjectInfo> projectInfos = modelManager->projectInfos();
        foreach (const ProjectInfo &projectInfo, projectInfos) {
462 463 464 465 466 467 468 469
            const ProjectExplorer::Project *project = projectInfo.project().data();
            if (project == currentProject)
                continue; // We have already checked the current project.

            const QString path = correspondingHeaderOrSourceInProject(fi, candidateFileNames, project);
            if (!path.isEmpty())
                return path;
        }
con's avatar
con committed
470
    }
471

con's avatar
con committed
472 473 474
    return QString();
}

475
} // namespace CppTools