cppeditorplugin.cpp 16.5 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 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 "cppincludehierarchy.h"
40
#include "cppsnippetprovider.h"
Leandro Melo's avatar
Leandro Melo committed
41
#include "cppquickfixassistant.h"
Nikolai Kosjar's avatar
Nikolai Kosjar committed
42
#include "cppquickfixes.h"
43
#include "cpphighlighterfactory.h"
con's avatar
con committed
44

45 46
#include "cppcodemodelinspectordialog.h"

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

59
#include <utils/hostosinfo.h>
con's avatar
con committed
60

61 62
#include <QCoreApplication>
#include <QStringList>
con's avatar
con committed
63

64
using namespace Core;
65
using namespace CppEditor;
con's avatar
con committed
66 67
using namespace CppEditor::Internal;

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

70 71
enum { QUICKFIX_INTERVAL = 20 };

72
//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed
73

74
CppEditorFactory::CppEditorFactory(CppEditorPlugin *owner) :
con's avatar
con committed
75 76
    m_owner(owner)
{
77 78 79 80 81 82
    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);
83

84 85 86 87 88 89
    new TextEditor::TextEditorActionHandler(this, CppEditor::Constants::C_CPPEDITOR,
        TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
        | TextEditor::TextEditorActionHandler::UnCollapseAll
        | TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor);

90
    if (!Utils::HostOsInfo::isMacHost() && !Utils::HostOsInfo::isWindowsHost()) {
91 92 93
        FileIconProvider::registerIconOverlayForMimeType(":/cppeditor/images/qt_cpp.png", CppEditor::Constants::CPP_SOURCE_MIMETYPE);
        FileIconProvider::registerIconOverlayForMimeType(":/cppeditor/images/qt_c.png", CppEditor::Constants::C_SOURCE_MIMETYPE);
        FileIconProvider::registerIconOverlayForMimeType(":/cppeditor/images/qt_h.png", CppEditor::Constants::CPP_HEADER_MIMETYPE);
94
    }
con's avatar
con committed
95 96
}

97
IEditor *CppEditorFactory::createEditor()
con's avatar
con committed
98
{
99
    CPPEditorWidget *editor = new CPPEditorWidget();
con's avatar
con committed
100 101
    editor->setRevisionsVisible(true);
    m_owner->initializeEditor(editor);
102
    return editor->editor();
con's avatar
con committed
103 104
}

105
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
106

107
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
108

109
CppEditorPlugin::CppEditorPlugin() :
110
    m_sortedOutline(true),
111 112
    m_renameSymbolUnderCursorAction(0),
    m_findUsagesAction(0),
113
    m_reparseExternallyChangedFiles(0),
Leandro Melo's avatar
Leandro Melo committed
114
    m_openTypeHierarchyAction(0),
115
    m_openIncludeHierarchyAction(0),
Leandro Melo's avatar
Leandro Melo committed
116
    m_quickFixProvider(0)
con's avatar
con committed
117 118 119 120
{
    m_instance = this;
}

121
CppEditorPlugin::~CppEditorPlugin()
con's avatar
con committed
122 123 124 125
{
    m_instance = 0;
}

126
CppEditorPlugin *CppEditorPlugin::instance()
con's avatar
con committed
127 128 129 130
{
    return m_instance;
}

131
void CppEditorPlugin::initializeEditor(CPPEditorWidget *editor)
con's avatar
con committed
132
{
Orgad Shaneh's avatar
Orgad Shaneh committed
133
    editor->setLanguageSettingsId(CppTools::Constants::CPP_SETTINGS_ID);
134
    TextEditor::TextEditorSettings::initializeEditor(editor);
con's avatar
con committed
135

136
    // function combo box sorting
137 138
    connect(this, SIGNAL(outlineSortingChanged(bool)),
            editor, SLOT(setSortedOutline(bool)));
139 140
}

141
void CppEditorPlugin::setSortedOutline(bool sorted)
142
{
143 144
    m_sortedOutline = sorted;
    emit outlineSortingChanged(sorted);
145 146
}

147
bool CppEditorPlugin::sortedOutline() const
148
{
149
    return m_sortedOutline;
con's avatar
con committed
150 151
}

152
CppQuickFixAssistProvider *CppEditorPlugin::quickFixProvider() const
Leandro Melo's avatar
Leandro Melo committed
153 154 155
{
    return m_quickFixProvider;
}
156

157
bool CppEditorPlugin::initialize(const QStringList & /*arguments*/, QString *errorMessage)
con's avatar
con committed
158
{
hjk's avatar
hjk committed
159
    if (!Core::MimeDatabase::addMimeTypes(QLatin1String(":/cppeditor/CppEditor.mimetypes.xml"), errorMessage))
con's avatar
con committed
160 161
        return false;

162
    addAutoReleasedObject(new CppEditorFactory(this));
163
    addAutoReleasedObject(new CppHoverHandler);
164
    addAutoReleasedObject(new CppOutlineWidgetFactory);
165
    addAutoReleasedObject(new CppTypeHierarchyFactory);
166
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
167
    addAutoReleasedObject(new CppSnippetProvider);
168
    addAutoReleasedObject(new CppHighlighterFactory);
169

Leandro Melo's avatar
Leandro Melo committed
170 171
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
172
    CppEditor::Internal::registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
173

hjk's avatar
hjk committed
174 175
    QString trCat = QCoreApplication::translate(Constants::WIZARD_CATEGORY, Constants::WIZARD_TR_CATEGORY);

Tobias Hunger's avatar
Tobias Hunger committed
176 177
    IWizardFactory *wizard = new CppClassWizard;
    wizard->setWizardKind(IWizardFactory::ClassWizard);
hjk's avatar
hjk committed
178 179 180 181 182 183 184 185
    wizard->setCategory(QLatin1String(Constants::WIZARD_CATEGORY));
    wizard->setDisplayCategory(trCat);
    wizard->setDisplayName(tr("C++ Class"));
    wizard->setId(QLatin1String("A.Class"));
    wizard->setDescription(tr("Creates a C++ header and a source file for a new class that you can add to a C++ project."));
    addAutoReleasedObject(wizard);

    wizard = new CppFileWizard(Source);
Tobias Hunger's avatar
Tobias Hunger committed
186
    wizard->setWizardKind(IWizardFactory::FileWizard);
hjk's avatar
hjk committed
187 188 189 190 191 192 193 194 195
    wizard->setCategory(QLatin1String(Constants::WIZARD_CATEGORY));
    wizard->setDisplayCategory(trCat);
    wizard->setDisplayName(tr("C++ Class"));
    wizard->setDescription(tr("Creates a C++ source file that you can add to a C++ project."));
    wizard->setDisplayName(tr("C++ Source File"));
    wizard->setId(QLatin1String("B.Source"));
    addAutoReleasedObject(wizard);

    wizard = new CppFileWizard(Header);
Tobias Hunger's avatar
Tobias Hunger committed
196
    wizard->setWizardKind(IWizardFactory::FileWizard);
hjk's avatar
hjk committed
197 198 199 200 201 202
    wizard->setCategory(QLatin1String(Constants::WIZARD_CATEGORY));
    wizard->setDisplayCategory(trCat);
    wizard->setDescription(tr("Creates a C++ header file that you can add to a C++ project."));
    wizard->setDisplayName(tr("C++ Header File"));
    wizard->setId(QLatin1String("C.Header"));
    addAutoReleasedObject(wizard);
con's avatar
con committed
203

204
    Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
205

206
    ActionContainer *contextMenu = ActionManager::createMenu(CppEditor::Constants::M_CONTEXT);
con's avatar
con committed
207

208 209
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
210

211
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
212 213
    contextMenu->addAction(cmd);

214
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
215
    contextMenu->addAction(cmd);
216
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
217

218
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
219 220 221 222 223 224
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

225
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Function Declaration/Definition"), this);
226
    cmd = ActionManager::registerAction(switchDeclarationDefinition,
227
        Constants::SWITCH_DECLARATION_DEFINITION, context, true);
228
    cmd->setDefaultKeySequence(QKeySequence(tr("Shift+F2")));
con's avatar
con committed
229 230
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
231
    contextMenu->addAction(cmd);
232
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
233

234
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
235 236 237
    cppToolsMenu->addAction(cmd);

    QAction *openDeclarationDefinitionInNextSplit =
238
            new QAction(tr("Open Function Declaration/Definition in Next Split"), this);
239
    cmd = ActionManager::registerAction(openDeclarationDefinitionInNextSplit,
240
        Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT, context, true);
241 242 243
    cmd->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                            ? tr("Meta+E, Shift+F2")
                                            : tr("Ctrl+E, Shift+F2")));
244 245 246 247
    connect(openDeclarationDefinitionInNextSplit, SIGNAL(triggered()),
            this, SLOT(openDeclarationDefinitionInNextSplit()));
    cppToolsMenu->addAction(cmd);

248
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
249
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
250
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
251
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
252
    contextMenu->addAction(cmd);
253
    cppToolsMenu->addAction(cmd);
254

255
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
256 257
    cmd = ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
258 259 260 261
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

262 263 264 265 266 267 268
    m_openIncludeHierarchyAction = new QAction(tr("Open Include Hierarchy"), this);
    cmd = Core::ActionManager::registerAction(m_openIncludeHierarchyAction, Constants::OPEN_INCLUDE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Shift+I") : tr("Ctrl+Shift+I")));
    connect(m_openIncludeHierarchyAction, SIGNAL(triggered()), this, SLOT(openIncludeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

269
    // Refactoring sub-menu
270 271
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
272
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
273
    contextMenu->addSeparator(globalContext);
274 275 276

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
277
    cmd = ActionManager::registerAction(m_renameSymbolUnderCursorAction,
278 279
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
280
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
281 282 283 284 285
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
286
    cppToolsMenu->addSeparator(globalContext);
287 288
    m_reparseExternallyChangedFiles = new QAction(tr("Reparse Externally Changed Files"), this);
    cmd = ActionManager::registerAction(m_reparseExternallyChangedFiles, Constants::UPDATE_CODEMODEL, globalContext);
289
    CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance();
290
    connect(m_reparseExternallyChangedFiles, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
291
    cppToolsMenu->addAction(cmd);
292

293 294
    cppToolsMenu->addSeparator(globalContext);
    QAction *inspectCppCodeModel = new QAction(tr("Inspect C++ Code Model..."), this);
295 296 297
    cmd = ActionManager::registerAction(inspectCppCodeModel, Constants::INSPECT_CPP_CODEMODEL, globalContext);
    cmd->setDefaultKeySequence(QKeySequence(Core::UseMacShortcuts ? tr("Meta+Shift+F12") : tr("Ctrl+Shift+F12")));
    connect(inspectCppCodeModel, SIGNAL(triggered()), this, SLOT(inspectCppCodeModel()));
298
    cppToolsMenu->addAction(cmd);
299

300
    contextMenu->addSeparator(context);
301

302
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
303
    contextMenu->addAction(cmd);
304

305
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
306
    contextMenu->addAction(cmd);
307

308 309 310 311
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
312

313
    readSettings();
con's avatar
con committed
314 315 316
    return true;
}

317
void CppEditorPlugin::readSettings()
318
{
319
    m_sortedOutline = ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), true).toBool();
320 321
}

322
void CppEditorPlugin::writeSettings()
323
{
324
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
325 326
}

327
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
328 329 330
{
}

331
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
332 333
{
    writeSettings();
334
    return SynchronousShutdown;
335 336
}

337 338 339 340 341
static CPPEditorWidget *currentCppEditorWidget()
{
    return qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
}

342
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
343
{
344 345
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
346 347
}

348
void CppEditorPlugin::openDeclarationDefinitionInNextSplit()
349
{
350 351
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ true);
con's avatar
con committed
352 353
}

354
void CppEditorPlugin::renameSymbolUnderCursor()
355
{
356 357
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->renameSymbolUnderCursor();
358 359
}

360
void CppEditorPlugin::findUsages()
361
{
362 363
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->findUsages();
364 365
}

366 367
void CppEditorPlugin::showPreProcessorDialog()
{
368 369
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->showPreProcessorWidget();
370 371
}

372
void CppEditorPlugin::onTaskStarted(Core::Id type)
373
{
374
    if (type == CppTools::Constants::TASK_INDEX) {
375 376
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
377
        m_reparseExternallyChangedFiles->setEnabled(false);
378
        m_openTypeHierarchyAction->setEnabled(false);
379
        m_openIncludeHierarchyAction->setEnabled(false);
380
    }
381 382
}

383
void CppEditorPlugin::onAllTasksFinished(Core::Id type)
384
{
385
    if (type == CppTools::Constants::TASK_INDEX) {
386 387
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
388
        m_reparseExternallyChangedFiles->setEnabled(true);
389
        m_openTypeHierarchyAction->setEnabled(true);
390
        m_openIncludeHierarchyAction->setEnabled(true);
391
    }
392 393
}

394 395 396 397 398 399 400 401 402 403
void CppEditorPlugin::inspectCppCodeModel()
{
    if (m_cppCodeModelInspectorDialog) {
        ICore::raiseWindow(m_cppCodeModelInspectorDialog);
    } else {
        m_cppCodeModelInspectorDialog = new CppCodeModelInspectorDialog(ICore::mainWindow());
        m_cppCodeModelInspectorDialog->show();
    }
}

404
void CppEditorPlugin::openTypeHierarchy()
405
{
406
    if (currentCppEditorWidget()) {
407 408
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
409 410 411 412
        emit typeHierarchyRequested();
    }
}

413 414
void CppEditorPlugin::openIncludeHierarchy()
{
415
    if (currentCppEditorWidget()) {
416 417 418 419 420 421
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
        navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
        emit includeHierarchyRequested();
    }
}

422
Q_EXPORT_PLUGIN(CppEditorPlugin)