cppplugin.cpp 14.4 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
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
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
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "cppplugin.h"
31
#include "cppclasswizard.h"
con's avatar
con committed
32 33 34 35
#include "cppeditor.h"
#include "cppeditorconstants.h"
#include "cppeditorenums.h"
#include "cppfilewizard.h"
36
#include "cpphoverhandler.h"
37
#include "cppquickfix.h"
con's avatar
con committed
38

39
#include <coreplugin/icore.h>
con's avatar
con committed
40 41 42 43
#include <coreplugin/coreconstants.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/fileiconprovider.h>
44
#include <coreplugin/actionmanager/actionmanager.h>
45
#include <coreplugin/actionmanager/actioncontainer.h>
con's avatar
con committed
46
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
47
#include <coreplugin/editormanager/editormanager.h>
48
#include <coreplugin/progressmanager/progressmanager.h>
con's avatar
con committed
49 50 51
#include <texteditor/completionsupport.h>
#include <texteditor/fontsettings.h>
#include <texteditor/storagesettings.h>
52
#include <texteditor/texteditoractionhandler.h>
53
#include <texteditor/texteditorplugin.h>
con's avatar
con committed
54
#include <texteditor/texteditorsettings.h>
55
#include <texteditor/texteditorconstants.h>
con's avatar
con committed
56
#include <cpptools/cpptoolsconstants.h>
57
#include <cpptools/cppmodelmanagerinterface.h>
con's avatar
con committed
58 59 60

#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
61
#include <QtCore/QTimer>
62
#include <QtCore/QCoreApplication>
63

con's avatar
con committed
64 65 66 67
#include <QtGui/QMenu>

using namespace CppEditor::Internal;

68 69
enum { QUICKFIX_INTERVAL = 20 };

70
//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed
71

72
CppEditorFactory::CppEditorFactory(CppPlugin *owner) :
con's avatar
con committed
73 74 75
    m_owner(owner)
{
    m_mimeTypes << QLatin1String(CppEditor::Constants::C_SOURCE_MIMETYPE)
76 77 78 79
            << QLatin1String(CppEditor::Constants::C_HEADER_MIMETYPE)
            << QLatin1String(CppEditor::Constants::CPP_SOURCE_MIMETYPE)
            << QLatin1String(CppEditor::Constants::CPP_HEADER_MIMETYPE);

80
#if !defined(Q_WS_MAC) && !defined(Q_WS_WIN)
81 82 83 84 85 86 87 88
    Core::FileIconProvider *iconProvider = Core::FileIconProvider::instance();
    Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase();
    iconProvider->registerIconOverlayForMimeType(QIcon(":/cppeditor/images/qt_cpp.png"),
                                                 mimeDatabase->findByType(QLatin1String(CppEditor::Constants::CPP_SOURCE_MIMETYPE)));
    iconProvider->registerIconOverlayForMimeType(QIcon(":/cppeditor/images/qt_c.png"),
                                                 mimeDatabase->findByType(QLatin1String(CppEditor::Constants::C_SOURCE_MIMETYPE)));
    iconProvider->registerIconOverlayForMimeType(QIcon(":/cppeditor/images/qt_h.png"),
                                                 mimeDatabase->findByType(QLatin1String(CppEditor::Constants::CPP_HEADER_MIMETYPE)));
89
#endif
con's avatar
con committed
90 91
}

92
QString CppEditorFactory::id() const
con's avatar
con committed
93
{
94
    return QLatin1String(CppEditor::Constants::CPPEDITOR_ID);
95 96 97 98 99
}

QString CppEditorFactory::displayName() const
{
    return tr(CppEditor::Constants::CPPEDITOR_DISPLAY_NAME);
con's avatar
con committed
100 101
}

102
Core::IFile *CppEditorFactory::open(const QString &fileName)
con's avatar
con committed
103
{
104
    Core::IEditor *iface = Core::EditorManager::instance()->openEditor(fileName, id());
con's avatar
con committed
105 106 107
    return iface ? iface->file() : 0;
}

108
Core::IEditor *CppEditorFactory::createEditor(QWidget *parent)
con's avatar
con committed
109 110 111 112 113 114 115
{
    CPPEditor *editor = new CPPEditor(parent);
    editor->setRevisionsVisible(true);
    m_owner->initializeEditor(editor);
    return editor->editableInterface();
}

116
QStringList CppEditorFactory::mimeTypes() const
con's avatar
con committed
117 118 119 120 121 122
{
    return m_mimeTypes;
}

///////////////////////////////// CppPlugin //////////////////////////////////

123 124 125 126 127 128 129 130 131 132 133
static inline
        Core::Command *createSeparator(Core::ActionManager *am,
                                       QObject *parent,
                                       const QList<int> &context,
                                       const char *id)
{
    QAction *separator = new QAction(parent);
    separator->setSeparator(true);
    return am->registerAction(separator, QLatin1String(id), context);
}

con's avatar
con committed
134 135 136 137
CppPlugin *CppPlugin::m_instance = 0;

CppPlugin::CppPlugin() :
    m_actionHandler(0),
138 139 140 141 142
    m_sortedMethodOverview(false),
    m_renameSymbolUnderCursorAction(0),
    m_findUsagesAction(0),
    m_updateCodeModelAction(0)

con's avatar
con committed
143 144
{
    m_instance = this;
145

146
    m_quickFixCollector = 0;
147 148 149 150
    m_quickFixTimer = new QTimer(this);
    m_quickFixTimer->setInterval(20);
    m_quickFixTimer->setSingleShot(true);
    connect(m_quickFixTimer, SIGNAL(timeout()), this, SLOT(quickFixNow()));
con's avatar
con committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

CppPlugin::~CppPlugin()
{
    delete m_actionHandler;
    m_instance = 0;
}

CppPlugin *CppPlugin::instance()
{
    return m_instance;
}

void CppPlugin::initializeEditor(CPPEditor *editor)
{
    m_actionHandler->setupActions(editor);

168
    TextEditor::TextEditorSettings::instance()->initializeEditor(editor);
con's avatar
con committed
169 170

    // auto completion
171 172
    connect(editor, SIGNAL(requestAutoCompletion(TextEditor::ITextEditable*, bool)),
            TextEditor::Internal::CompletionSupport::instance(), SLOT(autoComplete(TextEditor::ITextEditable*, bool)));
Roberto Raggi's avatar
Roberto Raggi committed
173 174

    // quick fix
175 176
    connect(editor, SIGNAL(requestQuickFix(TextEditor::ITextEditable*)),
            this, SLOT(quickFix(TextEditor::ITextEditable*)));
Roberto Raggi's avatar
Roberto Raggi committed
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191
    // method combo box sorting
    connect(this, SIGNAL(methodOverviewSortingChanged(bool)),
            editor, SLOT(setSortedMethodOverview(bool)));
}

void CppPlugin::setSortedMethodOverview(bool sorted)
{
    m_sortedMethodOverview = sorted;
    emit methodOverviewSortingChanged(sorted);
}

bool CppPlugin::sortedMethodOverview() const
{
    return m_sortedMethodOverview;
con's avatar
con committed
192 193
}

194 195 196
CPPQuickFixCollector *CppPlugin::quickFixCollector() const
{ return m_quickFixCollector; }

con's avatar
con committed
197 198
bool CppPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
{
199
    Core::ICore *core = Core::ICore::instance();
200

201
    if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/cppeditor/CppEditor.mimetypes.xml"), errorMessage))
con's avatar
con committed
202 203
        return false;

204
    addAutoReleasedObject(new CppEditorFactory(this));
205
    addAutoReleasedObject(new CppHoverHandler);
206 207 208

    m_quickFixCollector = new CPPQuickFixCollector;
    addAutoReleasedObject(m_quickFixCollector);
209

con's avatar
con committed
210 211
    CppFileWizard::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);

212
    wizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY));
213 214
    wizardParameters.setDisplayCategory(QCoreApplication::translate(Constants::WIZARD_CATEGORY,
                                                                    Constants::WIZARD_TR_CATEGORY));
215
    wizardParameters.setDisplayName(tr("C++ Class"));
Friedemann Kleint's avatar
Friedemann Kleint committed
216 217 218 219
    wizardParameters.setId(QLatin1String("A.Class"));
    wizardParameters.setKind(Core::IWizard::ClassWizard);
    wizardParameters.setDescription(tr("Creates a header and a source file for a new class."));
    addAutoReleasedObject(new CppClassWizard(wizardParameters, core));
con's avatar
con committed
220

Friedemann Kleint's avatar
Friedemann Kleint committed
221
    wizardParameters.setKind(Core::IWizard::FileWizard);
222
    wizardParameters.setDescription(tr("Creates a C++ source file."));
223
    wizardParameters.setDisplayName(tr("C++ Source File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
224
    wizardParameters.setId(QLatin1String("B.Source"));
225
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Source, core));
con's avatar
con committed
226

Friedemann Kleint's avatar
Friedemann Kleint committed
227
    wizardParameters.setDescription(tr("Creates a C++ header file."));
228
    wizardParameters.setDisplayName(tr("C++ Header File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
229 230
    wizardParameters.setId(QLatin1String("C.Header"));
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Header, core));
con's avatar
con committed
231 232

    QList<int> context;
233
    context << core->uniqueIDManager()->uniqueIdentifier(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
234

235
    Core::ActionManager *am = core->actionManager();
mae's avatar
mae committed
236
    Core::ActionContainer *contextMenu= am->createMenu(CppEditor::Constants::M_CONTEXT);
con's avatar
con committed
237

con's avatar
con committed
238
    Core::Command *cmd;
239
    Core::ActionContainer *cppToolsMenu = am->actionContainer(QLatin1String(CppTools::Constants::M_TOOLS_CPP));
con's avatar
con committed
240

hjk's avatar
hjk committed
241
    QAction *jumpToDefinition = new QAction(tr("Follow Symbol Under Cursor"), this);
con's avatar
con committed
242 243 244 245 246
    cmd = am->registerAction(jumpToDefinition,
        Constants::JUMP_TO_DEFINITION, context);
    cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
    connect(jumpToDefinition, SIGNAL(triggered()),
            this, SLOT(jumpToDefinition()));
mae's avatar
mae committed
247
    contextMenu->addAction(cmd);
248
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
249

hjk's avatar
hjk committed
250
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Method Declaration/Definition"), this);
con's avatar
con committed
251 252 253 254 255
    cmd = am->registerAction(switchDeclarationDefinition,
        Constants::SWITCH_DECLARATION_DEFINITION, context);
    cmd->setDefaultKeySequence(QKeySequence("Shift+F2"));
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
256
    contextMenu->addAction(cmd);
257
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
258

259 260
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
    cmd = am->registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
261
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
262
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
263
    contextMenu->addAction(cmd);
264
    cppToolsMenu->addAction(cmd);
265

hjk's avatar
hjk committed
266
    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"), this);
267
    cmd = am->registerAction(m_renameSymbolUnderCursorAction,
268 269
        Constants::RENAME_SYMBOL_UNDER_CURSOR, context);
    cmd->setDefaultKeySequence(QKeySequence("CTRL+SHIFT+R"));
270
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()), this, SLOT(renameSymbolUnderCursor()));
mae's avatar
mae committed
271
    contextMenu->addAction(cmd);
272 273 274 275 276 277 278 279 280 281 282
    cppToolsMenu->addAction(cmd);

    // Update context in global context
    QList<int> globalContext;
    globalContext.append(Core::Constants::C_GLOBAL_ID);
    cppToolsMenu->addAction(createSeparator(am, this, globalContext, CppEditor::Constants::SEPARATOR2));
    m_updateCodeModelAction = new QAction(tr("Update code model"), this);
    cmd = am->registerAction(m_updateCodeModelAction, QLatin1String(Constants::UPDATE_CODEMODEL), globalContext);
    CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance();
    connect(m_updateCodeModelAction, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
    cppToolsMenu->addAction(cmd);
283

284
    m_actionHandler = new TextEditor::TextEditorActionHandler(CppEditor::Constants::C_CPPEDITOR,
con's avatar
con committed
285 286 287 288
        TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
        | TextEditor::TextEditorActionHandler::UnCollapseAll);

289
    m_actionHandler->initializeActions();
290

291
    contextMenu->addAction(createSeparator(am, this, context, CppEditor::Constants::SEPARATOR));
292 293

    cmd = am->command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
294
    contextMenu->addAction(cmd);
295 296

    cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
297
    contextMenu->addAction(cmd);
298

299 300 301 302
    connect(core->progressManager(), SIGNAL(taskStarted(QString)),
            this, SLOT(onTaskStarted(QString)));
    connect(core->progressManager(), SIGNAL(allTasksFinished(QString)),
            this, SLOT(onAllTasksFinished(QString)));
303
    readSettings();
con's avatar
con committed
304 305 306
    return true;
}

307 308 309 310 311 312 313 314 315 316
void CppPlugin::readSettings()
{
    m_sortedMethodOverview = Core::ICore::instance()->settings()->value("CppTools/SortedMethodOverview", false).toBool();
}

void CppPlugin::writeSettings()
{
    Core::ICore::instance()->settings()->setValue("CppTools/SortedMethodOverview", m_sortedMethodOverview);
}

con's avatar
con committed
317 318 319 320
void CppPlugin::extensionsInitialized()
{
}

321
void CppPlugin::aboutToShutdown()
322 323 324 325
{
    writeSettings();
}

con's avatar
con committed
326 327
void CppPlugin::switchDeclarationDefinition()
{
328 329 330
    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
    if (editor)
con's avatar
con committed
331 332 333 334 335
        editor->switchDeclarationDefinition();
}

void CppPlugin::jumpToDefinition()
{
336 337 338
    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
    if (editor)
con's avatar
con committed
339 340 341
        editor->jumpToDefinition();
}

342 343 344 345 346 347 348 349
void CppPlugin::renameSymbolUnderCursor()
{
    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
    if (editor)
        editor->renameSymbolUnderCursor();
}

350 351 352 353 354 355 356 357
void CppPlugin::findUsages()
{
    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *editor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());
    if (editor)
        editor->findUsages();
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
void CppPlugin::quickFix(TextEditor::ITextEditable *editable)
{
    m_currentTextEditable = editable;
    quickFixNow();
}

void CppPlugin::quickFixNow()
{
    if (! m_currentTextEditable)
        return;

    Core::EditorManager *em = Core::EditorManager::instance();
    CPPEditor *currentEditor = qobject_cast<CPPEditor*>(em->currentEditor()->widget());

    if (CPPEditor *editor = qobject_cast<CPPEditor*>(m_currentTextEditable->widget())) {
        if (currentEditor == editor) {
            if (editor->isOutdated())
                m_quickFixTimer->start(QUICKFIX_INTERVAL);
            else
                TextEditor::Internal::CompletionSupport::instance()->quickFix(m_currentTextEditable);
        }
    }
}

382
void CppPlugin::onTaskStarted(const QString &type)
383
{
384 385 386
    if (type == CppTools::Constants::TASK_INDEX) {
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
387
        m_updateCodeModelAction->setEnabled(false);
388
    }
389 390
}

391
void CppPlugin::onAllTasksFinished(const QString &type)
392
{
393 394 395
    if (type == CppTools::Constants::TASK_INDEX) {
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
396
        m_updateCodeModelAction->setEnabled(true);
397
    }
398 399
}

con's avatar
con committed
400
Q_EXPORT_PLUGIN(CppPlugin)