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 "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
        setEditorActionHandlers(TextEditorActionHandler::Format
97 98 99 100 101 102 103 104 105
                              | 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);
        }
106
    }
107
};
con's avatar
con committed
108

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

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

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

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

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

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

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

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

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

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

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

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

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

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

206
    Context context(Constants::CPPEDITOR_ID);
con's avatar
con committed
207

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

302
    contextMenu->addSeparator(context);
303

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

} // namespace Internal
} // namespace CppEditor