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

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

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

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

66
using namespace Core;
67
using namespace TextEditor;
68 69 70

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

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

74 75
enum { QUICKFIX_INTERVAL = 20 };

76
//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed
77

78
class CppEditorFactory : public BaseTextEditorFactory
con's avatar
con committed
79
{
80 81 82 83 84 85 86 87 88 89 90 91 92 93
public:
    CppEditorFactory()
    {
        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);

        setDocumentCreator([]() { return new CppEditorDocument; });
        setEditorWidgetCreator([]() { return new CppEditorWidget; });
        setEditorCreator([]() { return new CppEditor; });
        setAutoCompleterCreator([]() { return new CppAutoCompleter; });
94
        setCommentStyle(Utils::CommentDefinition::CppStyle);
95 96 97 98 99 100 101 102 103 104 105 106

        setEditorActionHandlers(Constants::C_CPPEDITOR,
                                TextEditorActionHandler::Format
                              | TextEditorActionHandler::UnCommentSelection
                              | TextEditorActionHandler::UnCollapseAll
                              | TextEditorActionHandler::FollowSymbolUnderCursor);

        if (!Utils::HostOsInfo::isMacHost() && !Utils::HostOsInfo::isWindowsHost()) {
            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);
        }
107
    }
108
};
con's avatar
con committed
109

110
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
111

112
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
113

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

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

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

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

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

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

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

157
    addAutoReleasedObject(new CppEditorFactory);
158
    addAutoReleasedObject(new CppHoverHandler);
159
    addAutoReleasedObject(new CppOutlineWidgetFactory);
160
    addAutoReleasedObject(new CppTypeHierarchyFactory);
161
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
162
    addAutoReleasedObject(new CppSnippetProvider);
163 164 165

    auto hf = new TextEditor::HighlighterFactory;
    hf->setProductType<CppHighlighter>();
166 167 168 169 170
    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);
171
    addAutoReleasedObject(hf);
172

Leandro Melo's avatar
Leandro Melo committed
173 174
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
175
    registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
176

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

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

207
    Context context(Constants::C_CPPEDITOR);
con's avatar
con committed
208

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

211 212
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
213

214
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
215 216
    contextMenu->addAction(cmd);

217
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
218
    contextMenu->addAction(cmd);
219
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
220

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

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

237
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
238 239 240
    cppToolsMenu->addAction(cmd);

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

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

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

265 266 267 268 269 270 271
    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);

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

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

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

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

303
    contextMenu->addSeparator(context);
304

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

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

311 312 313 314
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
315

316
    readSettings();
con's avatar
con committed
317 318 319
    return true;
}

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

325
void CppEditorPlugin::writeSettings()
326
{
327
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
328 329
}

330
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
331 332 333
{
}

334
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
335 336
{
    writeSettings();
337
    return SynchronousShutdown;
338 339
}

340
static CppEditorWidget *currentCppEditorWidget()
341
{
342
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
343 344
}

345
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
346
{
347
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
348
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
349 350
}

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

357
void CppEditorPlugin::renameSymbolUnderCursor()
358
{
359
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
360
        editorWidget->renameSymbolUnderCursor();
361 362
}

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

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

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

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

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

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

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

} // namespace Internal
} // namespace CppEditor