cppeditorplugin.cpp 15.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"
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 TextEditor;
67 68 69

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

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

73 74
enum { QUICKFIX_INTERVAL = 20 };

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

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

95
        setEditorActionHandlers(TextEditorActionHandler::Format
96 97 98 99 100 101 102 103 104
                              | 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);
        }
105
    }
106
};
con's avatar
con committed
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::setSortedOutline(bool sorted)
135
{
136 137
    m_sortedOutline = sorted;
    emit outlineSortingChanged(sorted);
138 139
}

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

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

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

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

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

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

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

Tobias Hunger's avatar
Tobias Hunger committed
177 178
    IWizardFactory *wizard = new CppClassWizard;
    wizard->setWizardKind(IWizardFactory::ClassWizard);
hjk's avatar
hjk committed
179 180 181 182 183 184
    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);
con's avatar
con committed
185

186
    Context context(Constants::CPPEDITOR_ID);
con's avatar
con committed
187

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

190 191
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
192

193
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
194 195
    contextMenu->addAction(cmd);

196
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
197
    contextMenu->addAction(cmd);
198
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
199

200
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
201 202 203 204 205 206
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

207
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Function Declaration/Definition"), this);
208
    cmd = ActionManager::registerAction(switchDeclarationDefinition,
209
        Constants::SWITCH_DECLARATION_DEFINITION, context, true);
210
    cmd->setDefaultKeySequence(QKeySequence(tr("Shift+F2")));
con's avatar
con committed
211 212
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
213
    contextMenu->addAction(cmd);
214
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
215

216
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
217 218 219
    cppToolsMenu->addAction(cmd);

    QAction *openDeclarationDefinitionInNextSplit =
220
            new QAction(tr("Open Function Declaration/Definition in Next Split"), this);
221
    cmd = ActionManager::registerAction(openDeclarationDefinitionInNextSplit,
222
        Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT, context, true);
223 224 225
    cmd->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                            ? tr("Meta+E, Shift+F2")
                                            : tr("Ctrl+E, Shift+F2")));
226 227 228 229
    connect(openDeclarationDefinitionInNextSplit, SIGNAL(triggered()),
            this, SLOT(openDeclarationDefinitionInNextSplit()));
    cppToolsMenu->addAction(cmd);

230
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
231
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
232
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
233
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
234
    contextMenu->addAction(cmd);
235
    cppToolsMenu->addAction(cmd);
236

237
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
238 239
    cmd = ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
240 241 242 243
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

244 245 246 247 248 249 250
    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);

251
    // Refactoring sub-menu
252 253
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
254
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
255
    contextMenu->addSeparator(globalContext);
256 257 258

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
259
    cmd = ActionManager::registerAction(m_renameSymbolUnderCursorAction,
260 261
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
262
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
263 264 265 266 267
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
268
    cppToolsMenu->addSeparator(globalContext);
269 270
    m_reparseExternallyChangedFiles = new QAction(tr("Reparse Externally Changed Files"), this);
    cmd = ActionManager::registerAction(m_reparseExternallyChangedFiles, Constants::UPDATE_CODEMODEL, globalContext);
271
    CppTools::CppModelManager *cppModelManager = CppTools::CppModelManager::instance();
272
    connect(m_reparseExternallyChangedFiles, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
273
    cppToolsMenu->addAction(cmd);
274

275 276
    cppToolsMenu->addSeparator(globalContext);
    QAction *inspectCppCodeModel = new QAction(tr("Inspect C++ Code Model..."), this);
277 278 279
    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()));
280
    cppToolsMenu->addAction(cmd);
281

282
    contextMenu->addSeparator(context);
283

284
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
285
    contextMenu->addAction(cmd);
286

287
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
288
    contextMenu->addAction(cmd);
289

290 291 292 293
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
294

295
    readSettings();
con's avatar
con committed
296 297 298
    return true;
}

299
void CppEditorPlugin::readSettings()
300
{
301
    m_sortedOutline = ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), true).toBool();
302 303
}

304
void CppEditorPlugin::writeSettings()
305
{
306
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
307 308
}

309
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
310 311 312
{
}

313
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
314 315
{
    writeSettings();
316
    return SynchronousShutdown;
317 318
}

319
static CppEditorWidget *currentCppEditorWidget()
320
{
321
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
322 323
}

324
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
325
{
326
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
327
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
328 329
}

330
void CppEditorPlugin::openDeclarationDefinitionInNextSplit()
331
{
332
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
333
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ true);
con's avatar
con committed
334 335
}

336
void CppEditorPlugin::renameSymbolUnderCursor()
337
{
338
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
339
        editorWidget->renameSymbolUnderCursor();
340 341
}

342
void CppEditorPlugin::findUsages()
343
{
344
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
345
        editorWidget->findUsages();
346 347
}

348 349
void CppEditorPlugin::showPreProcessorDialog()
{
350
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
351
        editorWidget->showPreProcessorWidget();
352 353
}

354
void CppEditorPlugin::onTaskStarted(Core::Id type)
355
{
356
    if (type == CppTools::Constants::TASK_INDEX) {
357 358
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
359
        m_reparseExternallyChangedFiles->setEnabled(false);
360
        m_openTypeHierarchyAction->setEnabled(false);
361
        m_openIncludeHierarchyAction->setEnabled(false);
362
    }
363 364
}

365
void CppEditorPlugin::onAllTasksFinished(Core::Id type)
366
{
367
    if (type == CppTools::Constants::TASK_INDEX) {
368 369
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
370
        m_reparseExternallyChangedFiles->setEnabled(true);
371
        m_openTypeHierarchyAction->setEnabled(true);
372
        m_openIncludeHierarchyAction->setEnabled(true);
373
    }
374 375
}

376 377 378 379 380 381 382 383 384 385
void CppEditorPlugin::inspectCppCodeModel()
{
    if (m_cppCodeModelInspectorDialog) {
        ICore::raiseWindow(m_cppCodeModelInspectorDialog);
    } else {
        m_cppCodeModelInspectorDialog = new CppCodeModelInspectorDialog(ICore::mainWindow());
        m_cppCodeModelInspectorDialog->show();
    }
}

386
void CppEditorPlugin::openTypeHierarchy()
387
{
388
    if (currentCppEditorWidget()) {
389 390
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
391 392 393 394
        emit typeHierarchyRequested();
    }
}

395 396
void CppEditorPlugin::openIncludeHierarchy()
{
397
    if (currentCppEditorWidget()) {
398 399 400 401 402
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
        navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
        emit includeHierarchyRequested();
    }
}
403 404 405

} // namespace Internal
} // namespace CppEditor