cppeditorplugin.cpp 15.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

30
#include "cppeditorplugin.h"
31

32
#include "cppclasswizard.h"
con's avatar
con committed
33 34 35
#include "cppeditor.h"
#include "cppeditorconstants.h"
#include "cppfilewizard.h"
36
#include "cpphoverhandler.h"
37
#include "cppoutline.h"
38
#include "cpptypehierarchy.h"
39
#include "cppsnippetprovider.h"
Leandro Melo's avatar
Leandro Melo committed
40
#include "cppquickfixassistant.h"
Nikolai Kosjar's avatar
Nikolai Kosjar committed
41
#include "cppquickfixes.h"
42
#include "cpphighlighterfactory.h"
con's avatar
con committed
43

44
#include <coreplugin/actionmanager/actioncontainer.h>
45 46 47 48
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
49
#include <coreplugin/navigationwidget.h>
50 51
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cpptoolsconstants.h>
52
#include <texteditor/texteditoractionhandler.h>
con's avatar
con committed
53
#include <texteditor/texteditorsettings.h>
54

55
#include <utils/hostosinfo.h>
con's avatar
con committed
56

57 58
#include <QCoreApplication>
#include <QStringList>
con's avatar
con committed
59

60
using namespace CppEditor;
con's avatar
con committed
61 62
using namespace CppEditor::Internal;

Leandro Melo's avatar
Leandro Melo committed
63 64
void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);

65 66
enum { QUICKFIX_INTERVAL = 20 };

67
//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed
68

69
CppEditorFactory::CppEditorFactory(CppEditorPlugin *owner) :
con's avatar
con committed
70 71
    m_owner(owner)
{
72 73 74 75 76 77
    setId(CppEditor::Constants::CPPEDITOR_ID);
    setDisplayName(qApp->translate("OpenWith::Editors", CppEditor::Constants::CPPEDITOR_DISPLAY_NAME));
    addMimeType(CppEditor::Constants::C_SOURCE_MIMETYPE);
    addMimeType(CppEditor::Constants::C_HEADER_MIMETYPE);
    addMimeType(CppEditor::Constants::CPP_SOURCE_MIMETYPE);
    addMimeType(CppEditor::Constants::CPP_HEADER_MIMETYPE);
78

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

91
Core::IEditor *CppEditorFactory::createEditor(QWidget *parent)
con's avatar
con committed
92
{
93
    CPPEditorWidget *editor = new CPPEditorWidget(parent);
con's avatar
con committed
94 95
    editor->setRevisionsVisible(true);
    m_owner->initializeEditor(editor);
96
    return editor->editor();
con's avatar
con committed
97 98
}

99
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
100

101
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
102

103
CppEditorPlugin::CppEditorPlugin() :
con's avatar
con committed
104
    m_actionHandler(0),
105
    m_sortedOutline(false),
106 107
    m_renameSymbolUnderCursorAction(0),
    m_findUsagesAction(0),
108
    m_updateCodeModelAction(0),
Leandro Melo's avatar
Leandro Melo committed
109 110
    m_openTypeHierarchyAction(0),
    m_quickFixProvider(0)
con's avatar
con committed
111 112 113 114
{
    m_instance = this;
}

115
CppEditorPlugin::~CppEditorPlugin()
con's avatar
con committed
116 117 118 119 120
{
    delete m_actionHandler;
    m_instance = 0;
}

121
CppEditorPlugin *CppEditorPlugin::instance()
con's avatar
con committed
122 123 124 125
{
    return m_instance;
}

126
void CppEditorPlugin::initializeEditor(CPPEditorWidget *editor)
con's avatar
con committed
127 128 129
{
    m_actionHandler->setupActions(editor);

Orgad Shaneh's avatar
Orgad Shaneh committed
130
    editor->setLanguageSettingsId(CppTools::Constants::CPP_SETTINGS_ID);
131
    TextEditor::TextEditorSettings::instance()->initializeEditor(editor);
con's avatar
con committed
132

133
    // method combo box sorting
134 135
    connect(this, SIGNAL(outlineSortingChanged(bool)),
            editor, SLOT(setSortedOutline(bool)));
136 137
}

138
void CppEditorPlugin::setSortedOutline(bool sorted)
139
{
140 141
    m_sortedOutline = sorted;
    emit outlineSortingChanged(sorted);
142 143
}

144
bool CppEditorPlugin::sortedOutline() const
145
{
146
    return m_sortedOutline;
con's avatar
con committed
147 148
}

149
CppQuickFixAssistProvider *CppEditorPlugin::quickFixProvider() const
Leandro Melo's avatar
Leandro Melo committed
150 151 152
{
    return m_quickFixProvider;
}
153

154
bool CppEditorPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
con's avatar
con committed
155
{
hjk's avatar
hjk committed
156
    if (!Core::ICore::mimeDatabase()->addMimeTypes(QLatin1String(":/cppeditor/CppEditor.mimetypes.xml"), errorMessage))
con's avatar
con committed
157 158
        return false;

159
    addAutoReleasedObject(new CppEditorFactory(this));
160
    addAutoReleasedObject(new CppHoverHandler);
161
    addAutoReleasedObject(new CppOutlineWidgetFactory);
162
    addAutoReleasedObject(new CppTypeHierarchyFactory);
163
    addAutoReleasedObject(new CppSnippetProvider);
164
    addAutoReleasedObject(new CppHighlighterFactory);
165

Leandro Melo's avatar
Leandro Melo committed
166 167
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
168
    CppEditor::Internal::registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
169

hjk's avatar
hjk committed
170
    QObject *core = Core::ICore::instance();
con's avatar
con committed
171 172
    CppFileWizard::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);

173
    wizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY));
174 175
    wizardParameters.setDisplayCategory(QCoreApplication::translate(Constants::WIZARD_CATEGORY,
                                                                    Constants::WIZARD_TR_CATEGORY));
176
    wizardParameters.setDisplayName(tr("C++ Class"));
Friedemann Kleint's avatar
Friedemann Kleint committed
177 178
    wizardParameters.setId(QLatin1String("A.Class"));
    wizardParameters.setKind(Core::IWizard::ClassWizard);
179
    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
180
    addAutoReleasedObject(new CppClassWizard(wizardParameters, core));
con's avatar
con committed
181

Friedemann Kleint's avatar
Friedemann Kleint committed
182
    wizardParameters.setKind(Core::IWizard::FileWizard);
183
    wizardParameters.setDescription(tr("Creates a C++ source file that you can add to a C++ project."));
184
    wizardParameters.setDisplayName(tr("C++ Source File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
185
    wizardParameters.setId(QLatin1String("B.Source"));
186
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Source, core));
con's avatar
con committed
187

188
    wizardParameters.setDescription(tr("Creates a C++ header file that you can add to a C++ project."));
189
    wizardParameters.setDisplayName(tr("C++ Header File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
190 191
    wizardParameters.setId(QLatin1String("C.Header"));
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Header, core));
con's avatar
con committed
192

193
    Core::Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
194

Eike Ziller's avatar
Eike Ziller committed
195
    Core::ActionContainer *contextMenu= Core::ActionManager::createMenu(CppEditor::Constants::M_CONTEXT);
con's avatar
con committed
196

con's avatar
con committed
197
    Core::Command *cmd;
Eike Ziller's avatar
Eike Ziller committed
198
    Core::ActionContainer *cppToolsMenu = Core::ActionManager::actionContainer(Core::Id(CppTools::Constants::M_TOOLS_CPP));
con's avatar
con committed
199

Eike Ziller's avatar
Eike Ziller committed
200
    cmd = Core::ActionManager::command(Core::Id(CppTools::Constants::SWITCH_HEADER_SOURCE));
201 202
    contextMenu->addAction(cmd);

Eike Ziller's avatar
Eike Ziller committed
203
    cmd = Core::ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
204
    contextMenu->addAction(cmd);
205
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
206

207
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Method Declaration/Definition"), this);
Eike Ziller's avatar
Eike Ziller committed
208
    cmd = Core::ActionManager::registerAction(switchDeclarationDefinition,
209
        Constants::SWITCH_DECLARATION_DEFINITION, context, true);
210
    cmd->setDefaultKeySequence(QKeySequence(tr("Shift+F2")));
con's avatar
con committed
211 212
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
213
    contextMenu->addAction(cmd);
214
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
215

216 217 218 219
    cmd = Core::ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
    cppToolsMenu->addAction(cmd);

    QAction *openDeclarationDefinitionInNextSplit =
220
            new QAction(tr("Open Method Declaration/Definition in Next Split"), this);
221 222
    cmd = Core::ActionManager::registerAction(openDeclarationDefinitionInNextSplit,
        Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT, context, true);
223 224 225
    cmd->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                            ? tr("Meta+E, Shift+F2")
                                            : tr("Ctrl+E, Shift+F2")));
226 227 228 229
    connect(openDeclarationDefinitionInNextSplit, SIGNAL(triggered()),
            this, SLOT(openDeclarationDefinitionInNextSplit()));
    cppToolsMenu->addAction(cmd);

230
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
Eike Ziller's avatar
Eike Ziller committed
231
    cmd = Core::ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
232
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
233
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
234
    contextMenu->addAction(cmd);
235
    cppToolsMenu->addAction(cmd);
236

237
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
Eike Ziller's avatar
Eike Ziller committed
238
    cmd = Core::ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
Eike Ziller's avatar
Eike Ziller committed
239
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
240 241 242 243
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

244
    // Refactoring sub-menu
245
    Core::Context globalContext(Core::Constants::C_GLOBAL);
246
    Core::Command *sep = contextMenu->addSeparator(globalContext);
247
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
248
    contextMenu->addSeparator(globalContext);
249 250 251

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
Eike Ziller's avatar
Eike Ziller committed
252
    cmd = Core::ActionManager::registerAction(m_renameSymbolUnderCursorAction,
253 254
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
255
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
256 257 258 259 260
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
261
    cppToolsMenu->addSeparator(globalContext);
262
    m_updateCodeModelAction = new QAction(tr("Update Code Model"), this);
Eike Ziller's avatar
Eike Ziller committed
263
    cmd = Core::ActionManager::registerAction(m_updateCodeModelAction, Core::Id(Constants::UPDATE_CODEMODEL), globalContext);
264
    CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance();
265 266
    connect(m_updateCodeModelAction, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
    cppToolsMenu->addAction(cmd);
267

268
    m_actionHandler = new TextEditor::TextEditorActionHandler(CppEditor::Constants::C_CPPEDITOR,
con's avatar
con committed
269 270
        TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
271 272
        | TextEditor::TextEditorActionHandler::UnCollapseAll
        | TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor);
con's avatar
con committed
273

274
    m_actionHandler->initializeActions();
275

276
    contextMenu->addSeparator(context);
277

Eike Ziller's avatar
Eike Ziller committed
278
    cmd = Core::ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
279
    contextMenu->addAction(cmd);
280

Eike Ziller's avatar
Eike Ziller committed
281
    cmd = Core::ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
282
    contextMenu->addAction(cmd);
283

hjk's avatar
hjk committed
284
    connect(Core::ICore::progressManager(), SIGNAL(taskStarted(QString)),
285
            this, SLOT(onTaskStarted(QString)));
hjk's avatar
hjk committed
286
    connect(Core::ICore::progressManager(), SIGNAL(allTasksFinished(QString)),
287
            this, SLOT(onAllTasksFinished(QString)));
288

289
    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)), SLOT(currentEditorChanged(Core::IEditor*)));
290

291
    readSettings();
con's avatar
con committed
292 293 294
    return true;
}

295
void CppEditorPlugin::readSettings()
296
{
hjk's avatar
hjk committed
297
    m_sortedOutline = Core::ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), false).toBool();
298 299
}

300
void CppEditorPlugin::writeSettings()
301
{
hjk's avatar
hjk committed
302
    Core::ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
303 304
}

305
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
306 307 308
{
}

309
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
310 311
{
    writeSettings();
312
    return SynchronousShutdown;
313 314
}

315
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
316
{
317
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(Core::EditorManager::currentEditor()->widget());
318
    if (editor)
319 320 321
        editor->switchDeclarationDefinition(/*inNextSplit*/ false);
}

322
void CppEditorPlugin::openDeclarationDefinitionInNextSplit()
323 324 325 326
{
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(Core::EditorManager::currentEditor()->widget());
    if (editor)
        editor->switchDeclarationDefinition(/*inNextSplit*/ true);
con's avatar
con committed
327 328
}

329
void CppEditorPlugin::renameSymbolUnderCursor()
330
{
331
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(Core::EditorManager::currentEditor()->widget());
332 333 334 335
    if (editor)
        editor->renameSymbolUnderCursor();
}

336
void CppEditorPlugin::findUsages()
337
{
338
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(Core::EditorManager::currentEditor()->widget());
339 340 341 342
    if (editor)
        editor->findUsages();
}

343
void CppEditorPlugin::onTaskStarted(const QString &type)
344
{
345
    if (type == QLatin1String(CppTools::Constants::TASK_INDEX)) {
346 347
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
348
        m_updateCodeModelAction->setEnabled(false);
349
        m_openTypeHierarchyAction->setEnabled(false);
350
    }
351 352
}

353
void CppEditorPlugin::onAllTasksFinished(const QString &type)
354
{
355
    if (type == QLatin1String(CppTools::Constants::TASK_INDEX)) {
356 357
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
358
        m_updateCodeModelAction->setEnabled(true);
359
        m_openTypeHierarchyAction->setEnabled(true);
360
    }
361 362
}

363
void CppEditorPlugin::currentEditorChanged(Core::IEditor *editor)
364
{
365
    if (!editor)
366 367
        return;

368
    if (CPPEditorWidget *textEditor = qobject_cast<CPPEditorWidget *>(editor->widget()))
369
        textEditor->semanticRehighlight(/*force = */ true);
370 371
}

372
void CppEditorPlugin::openTypeHierarchy()
373
{
374
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(Core::EditorManager::currentEditor()->widget());
375 376
    if (editor) {
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
377
        navigation->activateSubWidget(Core::Id(Constants::TYPE_HIERARCHY_ID));
378 379 380 381
        emit typeHierarchyRequested();
    }
}

382
Q_EXPORT_PLUGIN(CppEditorPlugin)