cppeditorplugin.cpp 16.6 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 67 68

namespace CppEditor {
namespace Internal {
con's avatar
con committed
69

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

72 73
enum { QUICKFIX_INTERVAL = 20 };

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

76
CppEditorFactory::CppEditorFactory(CppEditorPlugin *owner) :
con's avatar
con committed
77 78
    m_owner(owner)
{
79 80 81 82 83 84 85 86
    setId(Constants::CPPEDITOR_ID);
    setDisplayName(qApp->translate("OpenWith::Editors", Constants::CPPEDITOR_DISPLAY_NAME));
    addMimeType(Constants::C_SOURCE_MIMETYPE);
    addMimeType(Constants::C_HEADER_MIMETYPE);
    addMimeType(Constants::CPP_SOURCE_MIMETYPE);
    addMimeType(Constants::CPP_HEADER_MIMETYPE);

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

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

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

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

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

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

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

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

134
void CppEditorPlugin::initializeEditor(CppEditorWidget *editor)
con's avatar
con committed
135
{
136
    // function combo box sorting
137
    connect(this, SIGNAL(outlineSortingChanged(bool)),
138
            editor->outline(), SLOT(setSorted(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 169 170

    auto hf = new TextEditor::HighlighterFactory;
    hf->setProductType<CppHighlighter>();
171 172 173 174 175
    hf->setId(Constants::CPPEDITOR_ID);
    hf->addMimeType(Constants::C_SOURCE_MIMETYPE);
    hf->addMimeType(Constants::C_HEADER_MIMETYPE);
    hf->addMimeType(Constants::CPP_SOURCE_MIMETYPE);
    hf->addMimeType(Constants::CPP_HEADER_MIMETYPE);
176
    addAutoReleasedObject(hf);
177

Leandro Melo's avatar
Leandro Melo committed
178 179
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
180
    registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
181

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
    contextMenu->addSeparator(context);
309

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

} // namespace Internal
} // namespace CppEditor