cppeditorplugin.cpp 16.8 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"
33
#include "cppcodemodelinspectordialog.h"
con's avatar
con committed
34
#include "cppeditorconstants.h"
35
#include "cppeditordocument.h"
36 37
#include "cppeditor.h"
#include "cppeditoroutline.h"
con's avatar
con committed
38
#include "cppfilewizard.h"
39
#include "cpphighlighter.h"
40
#include "cpphoverhandler.h"
41
#include "cppincludehierarchy.h"
42
#include "cppoutline.h"
Leandro Melo's avatar
Leandro Melo committed
43
#include "cppquickfixassistant.h"
Nikolai Kosjar's avatar
Nikolai Kosjar committed
44
#include "cppquickfixes.h"
45 46
#include "cppsnippetprovider.h"
#include "cpptypehierarchy.h"
47

48
#include <coreplugin/actionmanager/actioncontainer.h>
49 50 51 52
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
53
#include <coreplugin/navigationwidget.h>
54 55
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cpptoolsconstants.h>
56
#include <texteditor/texteditoractionhandler.h>
57
#include <texteditor/texteditorconstants.h>
58
#include <texteditor/highlighterfactory.h>
59

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

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

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

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

71 72
enum { QUICKFIX_INTERVAL = 20 };

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

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

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

91
    if (!Utils::HostOsInfo::isMacHost() && !Utils::HostOsInfo::isWindowsHost()) {
92 93 94
        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);
95
    }
con's avatar
con committed
96 97
}

98
IEditor *CppEditorFactory::createEditor()
con's avatar
con committed
99
{
100 101 102 103 104
    CPPEditor *editor = new CPPEditor;
    CppEditorWidget *widget = new CppEditorWidget(BaseTextDocumentPtr(new CPPEditorDocument), editor);
    m_owner->initializeEditor(widget);
    editor->configureCodeAssistant();
    return editor;
con's avatar
con committed
105 106
}

107
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
108

109
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
110

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

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

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

133
void CppEditorPlugin::initializeEditor(CppEditorWidget *editor)
con's avatar
con committed
134
{
135
    // function combo box sorting
136
    connect(this, SIGNAL(outlineSortingChanged(bool)),
137
            editor->outline(), SLOT(setSorted(bool)));
138 139
}

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

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

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

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

161
    addAutoReleasedObject(new CppEditorFactory(this));
162
    addAutoReleasedObject(new CppHoverHandler);
163
    addAutoReleasedObject(new CppOutlineWidgetFactory);
164
    addAutoReleasedObject(new CppTypeHierarchyFactory);
165
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
166
    addAutoReleasedObject(new CppSnippetProvider);
167 168 169 170 171 172 173 174 175

    auto hf = new TextEditor::HighlighterFactory;
    hf->setProductType<CppHighlighter>();
    hf->setId(CppEditor::Constants::CPPEDITOR_ID);
    hf->addMimeType(CppEditor::Constants::C_SOURCE_MIMETYPE);
    hf->addMimeType(CppEditor::Constants::C_HEADER_MIMETYPE);
    hf->addMimeType(CppEditor::Constants::CPP_SOURCE_MIMETYPE);
    hf->addMimeType(CppEditor::Constants::CPP_HEADER_MIMETYPE);
    addAutoReleasedObject(hf);
176

Leandro Melo's avatar
Leandro Melo committed
177 178
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
179
    CppEditor::Internal::registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
180

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

Tobias Hunger's avatar
Tobias Hunger committed
183 184
    IWizardFactory *wizard = new CppClassWizard;
    wizard->setWizardKind(IWizardFactory::ClassWizard);
hjk's avatar
hjk committed
185 186 187 188 189 190 191 192
    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
193
    wizard->setWizardKind(IWizardFactory::FileWizard);
hjk's avatar
hjk committed
194 195 196 197 198 199 200 201 202
    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
203
    wizard->setWizardKind(IWizardFactory::FileWizard);
hjk's avatar
hjk committed
204 205 206 207 208 209
    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
210

211
    Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
212

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

215 216
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
217

218
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
219 220
    contextMenu->addAction(cmd);

221
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
222
    contextMenu->addAction(cmd);
223
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
224

225
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
226 227 228 229 230 231
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

232
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Function Declaration/Definition"), this);
233
    cmd = ActionManager::registerAction(switchDeclarationDefinition,
234
        Constants::SWITCH_DECLARATION_DEFINITION, context, true);
235
    cmd->setDefaultKeySequence(QKeySequence(tr("Shift+F2")));
con's avatar
con committed
236 237
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
238
    contextMenu->addAction(cmd);
239
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
240

241
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
242 243 244
    cppToolsMenu->addAction(cmd);

    QAction *openDeclarationDefinitionInNextSplit =
245
            new QAction(tr("Open Function Declaration/Definition in Next Split"), this);
246
    cmd = ActionManager::registerAction(openDeclarationDefinitionInNextSplit,
247
        Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT, context, true);
248 249 250
    cmd->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                            ? tr("Meta+E, Shift+F2")
                                            : tr("Ctrl+E, Shift+F2")));
251 252 253 254
    connect(openDeclarationDefinitionInNextSplit, SIGNAL(triggered()),
            this, SLOT(openDeclarationDefinitionInNextSplit()));
    cppToolsMenu->addAction(cmd);

255
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
256
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
257
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
258
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
259
    contextMenu->addAction(cmd);
260
    cppToolsMenu->addAction(cmd);
261

262
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
263 264
    cmd = ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
265 266 267 268
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

269 270 271 272 273 274 275
    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);

276
    // Refactoring sub-menu
277 278
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
279
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
280
    contextMenu->addSeparator(globalContext);
281 282 283

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
284
    cmd = ActionManager::registerAction(m_renameSymbolUnderCursorAction,
285 286
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
287
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
288 289 290 291 292
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
293
    cppToolsMenu->addSeparator(globalContext);
294 295
    m_reparseExternallyChangedFiles = new QAction(tr("Reparse Externally Changed Files"), this);
    cmd = ActionManager::registerAction(m_reparseExternallyChangedFiles, Constants::UPDATE_CODEMODEL, globalContext);
296
    CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance();
297
    connect(m_reparseExternallyChangedFiles, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
298
    cppToolsMenu->addAction(cmd);
299

300 301
    cppToolsMenu->addSeparator(globalContext);
    QAction *inspectCppCodeModel = new QAction(tr("Inspect C++ Code Model..."), this);
302 303 304
    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()));
305
    cppToolsMenu->addAction(cmd);
306

307
    contextMenu->addSeparator(context);
308

309
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
310
    contextMenu->addAction(cmd);
311

312
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
313
    contextMenu->addAction(cmd);
314

315 316 317 318
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
319

320
    readSettings();
con's avatar
con committed
321 322 323
    return true;
}

324
void CppEditorPlugin::readSettings()
325
{
326
    m_sortedOutline = ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), true).toBool();
327 328
}

329
void CppEditorPlugin::writeSettings()
330
{
331
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
332 333
}

334
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
335 336 337
{
}

338
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
339 340
{
    writeSettings();
341
    return SynchronousShutdown;
342 343
}

344
static CppEditorWidget *currentCppEditorWidget()
345
{
346
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
347 348
}

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

355
void CppEditorPlugin::openDeclarationDefinitionInNextSplit()
356
{
357
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
358
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ true);
con's avatar
con committed
359 360
}

361
void CppEditorPlugin::renameSymbolUnderCursor()
362
{
363
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
364
        editorWidget->renameSymbolUnderCursor();
365 366
}

367
void CppEditorPlugin::findUsages()
368
{
369
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
370
        editorWidget->findUsages();
371 372
}

373 374
void CppEditorPlugin::showPreProcessorDialog()
{
375
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
376
        editorWidget->showPreProcessorWidget();
377 378
}

379
void CppEditorPlugin::onTaskStarted(Core::Id type)
380
{
381
    if (type == CppTools::Constants::TASK_INDEX) {
382 383
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
384
        m_reparseExternallyChangedFiles->setEnabled(false);
385
        m_openTypeHierarchyAction->setEnabled(false);
386
        m_openIncludeHierarchyAction->setEnabled(false);
387
    }
388 389
}

390
void CppEditorPlugin::onAllTasksFinished(Core::Id type)
391
{
392
    if (type == CppTools::Constants::TASK_INDEX) {
393 394
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
395
        m_reparseExternallyChangedFiles->setEnabled(true);
396
        m_openTypeHierarchyAction->setEnabled(true);
397
        m_openIncludeHierarchyAction->setEnabled(true);
398
    }
399 400
}

401 402 403 404 405 406 407 408 409 410
void CppEditorPlugin::inspectCppCodeModel()
{
    if (m_cppCodeModelInspectorDialog) {
        ICore::raiseWindow(m_cppCodeModelInspectorDialog);
    } else {
        m_cppCodeModelInspectorDialog = new CppCodeModelInspectorDialog(ICore::mainWindow());
        m_cppCodeModelInspectorDialog->show();
    }
}

411
void CppEditorPlugin::openTypeHierarchy()
412
{
413
    if (currentCppEditorWidget()) {
414 415
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
416 417 418 419
        emit typeHierarchyRequested();
    }
}

420 421
void CppEditorPlugin::openIncludeHierarchy()
{
422
    if (currentCppEditorWidget()) {
423 424 425 426 427
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
        navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
        emit includeHierarchyRequested();
    }
}