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

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
#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
    CPPEditorWidget *editor = new CPPEditorWidget();
con's avatar
con committed
101 102
    editor->setRevisionsVisible(true);
    m_owner->initializeEditor(editor);
103
    return editor->editor();
con's avatar
con committed
104 105
}

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

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

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

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

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

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

137
    // function combo box sorting
138
    connect(this, SIGNAL(outlineSortingChanged(bool)),
139
            editor->outline(), SLOT(setSorted(bool)));
140 141
}

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

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

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

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

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

    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);
178

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

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

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

213
    Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
214

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

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

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

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

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

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

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

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

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

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

271 272 273 274 275 276 277
    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);

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

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

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

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

309
    contextMenu->addSeparator(context);
310

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

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

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

322
    readSettings();
con's avatar
con committed
323 324 325
    return true;
}

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

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

336
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
337 338 339
{
}

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

346 347 348 349 350
static CPPEditorWidget *currentCppEditorWidget()
{
    return qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
}

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

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

363
void CppEditorPlugin::renameSymbolUnderCursor()
364
{
365 366
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->renameSymbolUnderCursor();
367 368
}

369
void CppEditorPlugin::findUsages()
370
{
371 372
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->findUsages();
373 374
}

375 376
void CppEditorPlugin::showPreProcessorDialog()
{
377 378
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->showPreProcessorWidget();
379 380
}

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

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

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

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

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

431
Q_EXPORT_PLUGIN(CppEditorPlugin)