cpptoolsplugin.cpp 12.1 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 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
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
con's avatar
con committed
31
**
32
**************************************************************************/
hjk's avatar
hjk committed
33

34
#include "cpptoolsplugin.h"
35
#include "completionsettingspage.h"
36
#include "cppfilesettingspage.h"
37
#include "cppclassesfilter.h"
con's avatar
con committed
38
#include "cppcodecompletion.h"
39
#include "cppfunctionsfilter.h"
40
#include "cppcurrentdocumentfilter.h"
con's avatar
con committed
41 42
#include "cppmodelmanager.h"
#include "cpptoolsconstants.h"
con's avatar
con committed
43
#include "cpplocatorfilter.h"
44
#include "symbolsfindfilter.h"
con's avatar
con committed
45

46 47
#include <extensionsystem/pluginmanager.h>

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

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

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

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

78 79
#include <sstream>

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

enum { debug = 0 };

CppToolsPlugin *CppToolsPlugin::m_instance = 0;

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

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

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

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

117 118
    CppCodeCompletion *completion = new CppCodeCompletion(m_modelManager);
    addAutoReleasedObject(completion);
Roberto Raggi's avatar
Roberto Raggi committed
119

120 121 122
    addAutoReleasedObject(new CppLocatorFilter(m_modelManager));
    addAutoReleasedObject(new CppClassesFilter(m_modelManager));
    addAutoReleasedObject(new CppFunctionsFilter(m_modelManager));
123
    addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, core->editorManager()));
124
    addAutoReleasedObject(new CompletionSettingsPage);
125
    addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
126
    addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
con's avatar
con committed
127 128

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

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

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

145 146 147 148 149
    // Set completion settings and keep them up to date
    TextEditor::TextEditorSettings *textEditorSettings = TextEditor::TextEditorSettings::instance();
    completion->setCompletionSettings(textEditorSettings->completionSettings());
    connect(textEditorSettings, SIGNAL(completionSettingsChanged(TextEditor::CompletionSettings)),
            completion, SLOT(setCompletionSettings(TextEditor::CompletionSettings)));
150

con's avatar
con committed
151 152 153 154 155
    return true;
}

void CppToolsPlugin::extensionsInitialized()
{
156 157 158 159 160
    // 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");
161 162 163 164 165

    // Initialize header suffixes
    const Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase();
    const Core::MimeType mimeType = mimeDatabase->findByType(QLatin1String("text/x-c++hdr"));
    m_modelManager->setHeaderSuffixes(mimeType.suffixes());
con's avatar
con committed
166 167
}

168
ExtensionSystem::IPlugin::ShutdownFlag CppToolsPlugin::aboutToShutdown()
169
{
170
    return SynchronousShutdown;
171 172
}

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

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

188 189
    QFileInfo fileInSameDir(dir, name);
    if (project && !fileInSameDir.isFile()) {
con's avatar
con committed
190 191 192 193 194 195 196 197 198
        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();
    }
199
    return fileInSameDir;
con's avatar
con committed
200 201 202
}

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

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;
221 222
    if (typeName == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
        return ObjectiveCPP_SourceFile;
con's avatar
con committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236
    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
237 238 239
        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
240 241 242
    case C_SourceFile:
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)).suffixes();
    case CPP_SourceFile:
243
    case ObjectiveCPP_SourceFile:
con's avatar
con committed
244 245 246 247 248 249 250
        return mimeDatase->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)).suffixes();
    }
    return QStringList();
}

QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
{
251
    const Core::ICore *core = Core::ICore::instance();
con's avatar
con committed
252 253
    const Core::MimeDatabase *mimeDatase = core->mimeDatabase();
    ProjectExplorer::ProjectExplorerPlugin *explorer =
254
       ProjectExplorer::ProjectExplorerPlugin::instance();
con's avatar
con committed
255 256 257 258 259 260 261 262 263 264 265 266
    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();
267
    const QString baseName = fi.completeBaseName();
con's avatar
con committed
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 318 319 320
    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)