cppeditorplugin.cpp 15.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 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"
con's avatar
con committed
33 34 35
#include "cppeditor.h"
#include "cppeditorconstants.h"
#include "cppfilewizard.h"
36
#include "cpphoverhandler.h"
37
#include "cppoutline.h"
38
#include "cpptypehierarchy.h"
39
#include "cppincludehierarchy.h"
40
#include "cppsnippetprovider.h"
Leandro Melo's avatar
Leandro Melo committed
41
#include "cppquickfixassistant.h"
Nikolai Kosjar's avatar
Nikolai Kosjar committed
42
#include "cppquickfixes.h"
43
#include "cpphighlighterfactory.h"
con's avatar
con committed
44

45
#include <coreplugin/actionmanager/actioncontainer.h>
46 47 48 49
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
50
#include <coreplugin/navigationwidget.h>
51 52
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cpptoolsconstants.h>
53
#include <texteditor/texteditoractionhandler.h>
con's avatar
con committed
54
#include <texteditor/texteditorsettings.h>
55

56
#include <utils/hostosinfo.h>
con's avatar
con committed
57

58 59
#include <QCoreApplication>
#include <QStringList>
con's avatar
con committed
60

61
using namespace Core;
62
using namespace CppEditor;
con's avatar
con committed
63 64
using namespace CppEditor::Internal;

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

67 68
enum { QUICKFIX_INTERVAL = 20 };

69
//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed
70

71
CppEditorFactory::CppEditorFactory(CppEditorPlugin *owner) :
con's avatar
con committed
72 73
    m_owner(owner)
{
74 75 76 77 78 79
    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);
80

81
    if (!Utils::HostOsInfo::isMacHost() && !Utils::HostOsInfo::isWindowsHost()) {
82 83 84
        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);
85
    }
con's avatar
con committed
86 87
}

88
IEditor *CppEditorFactory::createEditor(QWidget *parent)
con's avatar
con committed
89
{
90
    CPPEditorWidget *editor = new CPPEditorWidget(parent);
con's avatar
con committed
91 92
    editor->setRevisionsVisible(true);
    m_owner->initializeEditor(editor);
93
    return editor->editor();
con's avatar
con committed
94 95
}

96
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
97

98
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
99

100
CppEditorPlugin::CppEditorPlugin() :
con's avatar
con committed
101
    m_actionHandler(0),
102
    m_sortedOutline(false),
103 104
    m_renameSymbolUnderCursorAction(0),
    m_findUsagesAction(0),
105
    m_reparseExternallyChangedFiles(0),
Leandro Melo's avatar
Leandro Melo committed
106
    m_openTypeHierarchyAction(0),
107
    m_openIncludeHierarchyAction(0),
Leandro Melo's avatar
Leandro Melo committed
108
    m_quickFixProvider(0)
con's avatar
con committed
109 110 111 112
{
    m_instance = this;
}

113
CppEditorPlugin::~CppEditorPlugin()
con's avatar
con committed
114 115 116 117 118
{
    delete m_actionHandler;
    m_instance = 0;
}

119
CppEditorPlugin *CppEditorPlugin::instance()
con's avatar
con committed
120 121 122 123
{
    return m_instance;
}

124
void CppEditorPlugin::initializeEditor(CPPEditorWidget *editor)
con's avatar
con committed
125 126 127
{
    m_actionHandler->setupActions(editor);

Orgad Shaneh's avatar
Orgad Shaneh committed
128
    editor->setLanguageSettingsId(CppTools::Constants::CPP_SETTINGS_ID);
129
    TextEditor::TextEditorSettings::initializeEditor(editor);
con's avatar
con committed
130

131
    // function combo box sorting
132 133
    connect(this, SIGNAL(outlineSortingChanged(bool)),
            editor, SLOT(setSortedOutline(bool)));
134 135
}

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(this));
158
    addAutoReleasedObject(new CppHoverHandler);
159
    addAutoReleasedObject(new CppOutlineWidgetFactory);
160
    addAutoReleasedObject(new CppTypeHierarchyFactory);
161
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
162
    addAutoReleasedObject(new CppSnippetProvider);
163
    addAutoReleasedObject(new CppHighlighterFactory);
164

Leandro Melo's avatar
Leandro Melo committed
165 166
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
167
    CppEditor::Internal::registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
168

hjk's avatar
hjk committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    QString trCat = QCoreApplication::translate(Constants::WIZARD_CATEGORY, Constants::WIZARD_TR_CATEGORY);

    IWizard *wizard = new CppClassWizard;
    wizard->setWizardKind(IWizard::ClassWizard);
    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);
    wizard->setWizardKind(IWizard::FileWizard);
    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);
    wizard->setWizardKind(IWizard::FileWizard);
    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
198

199
    Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
200

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

203 204
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
205

206
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
207 208
    contextMenu->addAction(cmd);

209
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
210
    contextMenu->addAction(cmd);
211
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
212

213
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
214 215 216 217 218 219
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

220
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Function Declaration/Definition"), this);
221
    cmd = ActionManager::registerAction(switchDeclarationDefinition,
222
        Constants::SWITCH_DECLARATION_DEFINITION, context, true);
223
    cmd->setDefaultKeySequence(QKeySequence(tr("Shift+F2")));
con's avatar
con committed
224 225
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
226
    contextMenu->addAction(cmd);
227
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
228

229
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
230 231 232
    cppToolsMenu->addAction(cmd);

    QAction *openDeclarationDefinitionInNextSplit =
233
            new QAction(tr("Open Function Declaration/Definition in Next Split"), this);
234
    cmd = ActionManager::registerAction(openDeclarationDefinitionInNextSplit,
235
        Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT, context, true);
236 237 238
    cmd->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                            ? tr("Meta+E, Shift+F2")
                                            : tr("Ctrl+E, Shift+F2")));
239 240 241 242
    connect(openDeclarationDefinitionInNextSplit, SIGNAL(triggered()),
            this, SLOT(openDeclarationDefinitionInNextSplit()));
    cppToolsMenu->addAction(cmd);

243
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
244
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
245
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
246
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
247
    contextMenu->addAction(cmd);
248
    cppToolsMenu->addAction(cmd);
249

250
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
251 252
    cmd = ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
253 254 255 256
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

257 258 259 260 261 262 263
    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);

264
    // Refactoring sub-menu
265 266
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
267
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
268
    contextMenu->addSeparator(globalContext);
269 270 271

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
272
    cmd = ActionManager::registerAction(m_renameSymbolUnderCursorAction,
273 274
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
275
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
276 277 278 279 280
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
281
    cppToolsMenu->addSeparator(globalContext);
282 283
    m_reparseExternallyChangedFiles = new QAction(tr("Reparse Externally Changed Files"), this);
    cmd = ActionManager::registerAction(m_reparseExternallyChangedFiles, Constants::UPDATE_CODEMODEL, globalContext);
284
    CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance();
285
    connect(m_reparseExternallyChangedFiles, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
286
    cppToolsMenu->addAction(cmd);
287

288
    m_actionHandler = new TextEditor::TextEditorActionHandler(CppEditor::Constants::C_CPPEDITOR,
con's avatar
con committed
289 290
        TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
291 292
        | TextEditor::TextEditorActionHandler::UnCollapseAll
        | TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor);
con's avatar
con committed
293

294
    m_actionHandler->initializeActions();
295

296
    contextMenu->addSeparator(context);
297

298
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
299
    contextMenu->addAction(cmd);
300

301
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
302
    contextMenu->addAction(cmd);
303

304 305 306 307
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
308

309
    readSettings();
con's avatar
con committed
310 311 312
    return true;
}

313
void CppEditorPlugin::readSettings()
314
{
315
    m_sortedOutline = ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), false).toBool();
316 317
}

318
void CppEditorPlugin::writeSettings()
319
{
320
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
321 322
}

323
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
324 325 326
{
}

327
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
328 329
{
    writeSettings();
330
    return SynchronousShutdown;
331 332
}

333 334 335 336 337
static CPPEditorWidget *currentCppEditorWidget()
{
    return qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
}

338
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
339
{
340 341
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
342 343
}

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

350
void CppEditorPlugin::renameSymbolUnderCursor()
351
{
352 353
    if (CPPEditorWidget *editorWidget = currentCppEditorWidget())
        editorWidget->renameSymbolUnderCursor();
354 355
}

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

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

368
void CppEditorPlugin::onTaskStarted(Core::Id type)
369
{
370
    if (type == CppTools::Constants::TASK_INDEX) {
371 372
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
373
        m_reparseExternallyChangedFiles->setEnabled(false);
374
        m_openTypeHierarchyAction->setEnabled(false);
375
        m_openIncludeHierarchyAction->setEnabled(false);
376
    }
377 378
}

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

390
void CppEditorPlugin::openTypeHierarchy()
391
{
392
    if (currentCppEditorWidget()) {
393 394
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
395 396 397 398
        emit typeHierarchyRequested();
    }
}

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

408
Q_EXPORT_PLUGIN(CppEditorPlugin)