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
#include <coreplugin/coreconstants.h>
#include <coreplugin/mimedatabase.h>
#include <coreplugin/fileiconprovider.h>
43
#include <coreplugin/actionmanager/actionmanager.h>
44
#include <coreplugin/actionmanager/actioncontainer.h>
con's avatar
con committed
45
#include <coreplugin/actionmanager/command.h>
con's avatar
con committed
46
#include <coreplugin/editormanager/editormanager.h>
47
#include <coreplugin/progressmanager/progressmanager.h>
con's avatar
con committed
48
49
50
#include <texteditor/completionsupport.h>
#include <texteditor/fontsettings.h>
#include <texteditor/storagesettings.h>
51
#include <texteditor/texteditoractionhandler.h>
52
#include <texteditor/texteditorplugin.h>
con's avatar
con committed
53
#include <texteditor/texteditorsettings.h>
54
#include <texteditor/texteditorconstants.h>
con's avatar
con committed
55
#include <cpptools/cpptoolsconstants.h>
56
#include <cpptools/cppmodelmanagerinterface.h>
con's avatar
con committed
57
58
59

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

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

using namespace CppEditor::Internal;

67
68
enum { QUICKFIX_INTERVAL = 20 };

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

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

79
#if !defined(Q_WS_MAC) && !defined(Q_WS_WIN)
80
81
82
83
84
85
86
87
    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)));
88
#endif
con's avatar
con committed
89
90
}

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

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

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

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

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

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

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

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

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

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

145
    m_quickFixCollector = 0;
146
147
148
149
    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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
}

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

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

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

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

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

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

177
178
179
180
181
182
183
184
185
186
187
188
189
190
    // 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
191
192
}

193
CppQuickFixCollector *CppPlugin::quickFixCollector() const
194
195
{ return m_quickFixCollector; }

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

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

203
    addAutoReleasedObject(new CppEditorFactory(this));
204
    addAutoReleasedObject(new CppHoverHandler);
205

206
    m_quickFixCollector = new CppQuickFixCollector;
207
    addAutoReleasedObject(m_quickFixCollector);
208

Roberto Raggi's avatar
Roberto Raggi committed
209
210
    addAutoReleasedObject(new CppQuickFixFactory);

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

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

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

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

233
    Core::Context context(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
    cppToolsMenu->addAction(cmd);

    // Update context in global context
275
    Core::Context globalContext(Core::Constants::C_GLOBAL_ID);
276
    cppToolsMenu->addAction(createSeparator(am, this, globalContext, CppEditor::Constants::SEPARATOR2));
277
    m_updateCodeModelAction = new QAction(tr("Update Code Model"), this);
278
279
280
281
    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);
282

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

288
    m_actionHandler->initializeActions();
289

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

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

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

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

306
307
308
309
310
311
312
313
314
315
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
316
317
318
319
void CppPlugin::extensionsInitialized()
{
}

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

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

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

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

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

357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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);
        }
    }
}

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

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

con's avatar
con committed
399
Q_EXPORT_PLUGIN(CppPlugin)