2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-05-06 15:05:37 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-12 14:39:51 +01:00
|
|
|
#include "cpptoolsplugin.h"
|
2008-12-12 16:36:33 +01:00
|
|
|
#include "completionsettingspage.h"
|
2009-03-18 16:43:01 +01:00
|
|
|
#include "cppfilesettingspage.h"
|
2011-02-03 15:48:14 +01:00
|
|
|
#include "cppcodestylesettingspage.h"
|
2008-11-28 15:36:54 +01:00
|
|
|
#include "cppclassesfilter.h"
|
2008-12-09 17:15:00 +01:00
|
|
|
#include "cppfunctionsfilter.h"
|
2009-05-14 21:08:08 +02:00
|
|
|
#include "cppcurrentdocumentfilter.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cppmodelmanager.h"
|
|
|
|
|
#include "cpptoolsconstants.h"
|
2009-10-16 10:55:58 +02:00
|
|
|
#include "cpplocatorfilter.h"
|
2010-07-19 14:46:53 +02:00
|
|
|
#include "symbolsfindfilter.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "cppcompletionassist.h"
|
2011-02-03 15:48:14 +01:00
|
|
|
#include "cpptoolssettings.h"
|
|
|
|
|
#include "cppcodestylesettingsfactory.h"
|
2011-05-27 09:35:47 +02:00
|
|
|
#include "cppcodestylesettings.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-06-02 14:56:03 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/mimedatabase.h>
|
|
|
|
|
#include <coreplugin/coreconstants.h>
|
2009-01-13 13:39:31 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2010-03-18 10:59:06 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/command.h>
|
2010-11-02 16:53:56 +01:00
|
|
|
#include <coreplugin/uniqueidmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2009-06-02 14:56:03 +02:00
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
2009-11-11 14:32:54 +01:00
|
|
|
#include <coreplugin/vcsmanager.h>
|
2009-12-21 11:08:20 +01:00
|
|
|
#include <coreplugin/filemanager.h>
|
2010-05-10 18:30:09 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
2011-02-03 15:48:14 +01:00
|
|
|
#include <texteditor/tabsettings.h>
|
|
|
|
|
#include <texteditor/codestylepreferencesmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
|
|
|
|
|
2009-06-02 14:56:03 +02:00
|
|
|
#include <QtCore/QtConcurrentRun>
|
|
|
|
|
#include <QtCore/QFutureSynchronizer>
|
|
|
|
|
#include <qtconcurrent/runextensions.h>
|
|
|
|
|
|
|
|
|
|
#include <find/ifindfilter.h>
|
|
|
|
|
#include <find/searchresultwindow.h>
|
|
|
|
|
#include <utils/filesearch.h>
|
|
|
|
|
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <QtCore/QtPlugin>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
#include <QtCore/QDebug>
|
2008-12-12 16:36:33 +01:00
|
|
|
#include <QtCore/QSettings>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QMenu>
|
|
|
|
|
#include <QtGui/QAction>
|
|
|
|
|
|
2009-06-02 14:56:03 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace CppTools::Internal;
|
2009-06-02 14:56:03 +02:00
|
|
|
using namespace CPlusPlus;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
|
|
|
|
CppToolsPlugin *CppToolsPlugin::m_instance = 0;
|
|
|
|
|
|
2009-04-29 09:04:59 +02:00
|
|
|
CppToolsPlugin::CppToolsPlugin() :
|
|
|
|
|
m_modelManager(0),
|
|
|
|
|
m_fileSettings(new CppFileSettings)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
m_instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppToolsPlugin::~CppToolsPlugin()
|
|
|
|
|
{
|
|
|
|
|
m_instance = 0;
|
|
|
|
|
m_modelManager = 0; // deleted automatically
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-07-13 17:35:17 +02:00
|
|
|
Q_UNUSED(arguments)
|
|
|
|
|
Q_UNUSED(error)
|
2011-05-27 09:35:47 +02:00
|
|
|
|
|
|
|
|
qRegisterMetaType<CppTools::CppCodeStyleSettings>("CppTools::CppCodeStyleSettings");
|
|
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
Core::ICore *core = Core::ICore::instance();
|
|
|
|
|
Core::ActionManager *am = core->actionManager();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-02-03 15:48:14 +01:00
|
|
|
m_settings = new CppToolsSettings(this); // force registration of cpp tools settings
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// Objects
|
|
|
|
|
m_modelManager = new CppModelManager(this);
|
2010-12-07 17:34:43 +01:00
|
|
|
Core::VcsManager *vcsManager = core->vcsManager();
|
2009-12-21 11:08:20 +01:00
|
|
|
Core::FileManager *fileManager = core->fileManager();
|
2009-11-11 14:32:54 +01:00
|
|
|
connect(vcsManager, SIGNAL(repositoryChanged(QString)),
|
|
|
|
|
m_modelManager, SLOT(updateModifiedSourceFiles()));
|
2009-12-21 11:08:20 +01:00
|
|
|
connect(fileManager, SIGNAL(filesChangedInternally(QStringList)),
|
|
|
|
|
m_modelManager, SLOT(updateSourceFiles(QStringList)));
|
2008-12-02 12:01:29 +01:00
|
|
|
addAutoReleasedObject(m_modelManager);
|
2009-06-09 13:52:27 +02:00
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
addAutoReleasedObject(new CppCompletionAssistProvider);
|
2010-07-19 14:46:53 +02:00
|
|
|
addAutoReleasedObject(new CppLocatorFilter(m_modelManager));
|
|
|
|
|
addAutoReleasedObject(new CppClassesFilter(m_modelManager));
|
|
|
|
|
addAutoReleasedObject(new CppFunctionsFilter(m_modelManager));
|
2009-05-14 21:08:08 +02:00
|
|
|
addAutoReleasedObject(new CppCurrentDocumentFilter(m_modelManager, core->editorManager()));
|
2010-05-10 18:30:09 +02:00
|
|
|
addAutoReleasedObject(new CompletionSettingsPage);
|
2009-04-29 09:04:59 +02:00
|
|
|
addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
|
2010-07-19 14:46:53 +02:00
|
|
|
addAutoReleasedObject(new SymbolsFindFilter(m_modelManager));
|
2011-02-03 15:48:14 +01:00
|
|
|
addAutoReleasedObject(new CppCodeStyleSettingsPage);
|
|
|
|
|
|
|
|
|
|
TextEditor::CodeStylePreferencesManager::instance()->registerFactory(
|
|
|
|
|
new CppTools::CppCodeStylePreferencesFactory());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Menus
|
2009-01-14 12:39:59 +01:00
|
|
|
Core::ActionContainer *mtools = am->actionContainer(Core::Constants::M_TOOLS);
|
|
|
|
|
Core::ActionContainer *mcpptools = am->createMenu(CppTools::Constants::M_TOOLS_CPP);
|
2008-12-02 12:01:29 +01:00
|
|
|
QMenu *menu = mcpptools->menu();
|
|
|
|
|
menu->setTitle(tr("&C++"));
|
|
|
|
|
menu->setEnabled(true);
|
|
|
|
|
mtools->addMenu(mcpptools);
|
|
|
|
|
|
|
|
|
|
// Actions
|
2010-06-25 17:37:59 +02:00
|
|
|
Core::Context context(CppEditor::Constants::C_CPPEDITOR);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
QAction *switchAction = new QAction(tr("Switch Header/Source"), this);
|
2010-12-20 10:35:30 +01:00
|
|
|
Core::Command *command = am->registerAction(switchAction, Constants::SWITCH_HEADER_SOURCE, context, true);
|
2008-12-02 12:01:29 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(Qt::Key_F4));
|
|
|
|
|
mcpptools->addAction(command);
|
|
|
|
|
connect(switchAction, SIGNAL(triggered()), this, SLOT(switchHeaderSource()));
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppToolsPlugin::extensionsInitialized()
|
|
|
|
|
{
|
2009-04-29 09:04:59 +02:00
|
|
|
// 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");
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-13 13:36:47 +02:00
|
|
|
ExtensionSystem::IPlugin::ShutdownFlag CppToolsPlugin::aboutToShutdown()
|
2008-12-12 16:36:33 +01:00
|
|
|
{
|
2010-07-13 13:36:47 +02:00
|
|
|
return SynchronousShutdown;
|
2008-12-12 16:36:33 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void CppToolsPlugin::switchHeaderSource()
|
|
|
|
|
{
|
2009-01-21 15:52:34 +01:00
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
2009-01-20 11:52:04 +01:00
|
|
|
Core::IEditor *editor = editorManager->currentEditor();
|
2008-12-02 12:01:29 +01:00
|
|
|
QString otherFile = correspondingHeaderOrSource(editor->file()->fileName());
|
2010-09-03 11:57:46 +02:00
|
|
|
if (!otherFile.isEmpty())
|
2009-01-20 11:52:04 +01:00
|
|
|
editorManager->openEditor(otherFile);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFileInfo CppToolsPlugin::findFile(const QDir &dir, const QString &name,
|
|
|
|
|
const ProjectExplorer::Project *project) const
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << dir << name;
|
|
|
|
|
|
2009-01-06 17:37:45 +01:00
|
|
|
QFileInfo fileInSameDir(dir, name);
|
|
|
|
|
if (project && !fileInSameDir.isFile()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
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();
|
|
|
|
|
}
|
2009-01-06 17:37:45 +01:00
|
|
|
return fileInSameDir;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Figure out file type
|
2008-12-12 16:36:33 +01:00
|
|
|
enum FileType {
|
|
|
|
|
HeaderFile,
|
|
|
|
|
C_SourceFile,
|
|
|
|
|
CPP_SourceFile,
|
2009-05-04 17:11:10 +02:00
|
|
|
ObjectiveCPP_SourceFile,
|
2008-12-12 16:36:33 +01:00
|
|
|
UnknownType
|
|
|
|
|
};
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
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;
|
2009-05-04 17:11:10 +02:00
|
|
|
if (typeName == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE))
|
|
|
|
|
return ObjectiveCPP_SourceFile;
|
2008-12-02 12:01:29 +01:00
|
|
|
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
|
2009-05-04 17:11:10 +02:00
|
|
|
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();
|
2008-12-02 12:01:29 +01:00
|
|
|
case C_SourceFile:
|
|
|
|
|
return mimeDatase->findByType(QLatin1String(CppTools::Constants::C_HEADER_MIMETYPE)).suffixes();
|
|
|
|
|
case CPP_SourceFile:
|
2009-05-04 17:11:10 +02:00
|
|
|
case ObjectiveCPP_SourceFile:
|
2008-12-02 12:01:29 +01:00
|
|
|
return mimeDatase->findByType(QLatin1String(CppTools::Constants::CPP_HEADER_MIMETYPE)).suffixes();
|
|
|
|
|
}
|
|
|
|
|
return QStringList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
|
|
|
|
|
{
|
2009-01-20 11:52:04 +01:00
|
|
|
const Core::ICore *core = Core::ICore::instance();
|
2008-12-02 12:01:29 +01:00
|
|
|
const Core::MimeDatabase *mimeDatase = core->mimeDatabase();
|
|
|
|
|
ProjectExplorer::ProjectExplorerPlugin *explorer =
|
2009-03-04 09:38:01 +01:00
|
|
|
ProjectExplorer::ProjectExplorerPlugin::instance();
|
2008-12-02 12:01:29 +01:00
|
|
|
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();
|
2009-03-16 11:17:45 +01:00
|
|
|
const QString baseName = fi.completeBaseName();
|
2008-12-02 12:01:29 +01:00
|
|
|
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)
|