cppeditorplugin.cpp 15.4 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
** 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
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24 25 26
**
** 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
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
hjk's avatar
hjk committed
30

31
#include "cppeditorplugin.h"
32

33
#include "cppautocompleter.h"
34
#include "cppclasswizard.h"
35
#include "cppcodemodelinspectordialog.h"
con's avatar
con committed
36
#include "cppeditorconstants.h"
37
#include "cppeditor.h"
38
#include "cppeditordocument.h"
39
#include "cppeditoroutline.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

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);
hjk's avatar
hjk committed
94 95 96
        setCodeFoldingSupported(true);
        setMarksVisible(true);
        setParenthesesMatchingEnabled(true);
97

98
        setEditorActionHandlers(TextEditorActionHandler::Format
99 100 101 102
                              | TextEditorActionHandler::UnCommentSelection
                              | TextEditorActionHandler::UnCollapseAll
                              | TextEditorActionHandler::FollowSymbolUnderCursor);

103 104
        addHoverHandler(new CppHoverHandler);

105 106 107 108 109
        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);
        }
110
    }
111
};
con's avatar
con committed
112

113
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
114

115
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
116

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

129
CppEditorPlugin::~CppEditorPlugin()
con's avatar
con committed
130 131 132 133
{
    m_instance = 0;
}

134
CppEditorPlugin *CppEditorPlugin::instance()
con's avatar
con committed
135 136 137 138
{
    return m_instance;
}

139
void CppEditorPlugin::setSortedOutline(bool sorted)
140
{
141 142
    m_sortedOutline = sorted;
    emit outlineSortingChanged(sorted);
143 144
}

145
bool CppEditorPlugin::sortedOutline() const
146
{
147
    return m_sortedOutline;
con's avatar
con committed
148 149
}

150
CppQuickFixAssistProvider *CppEditorPlugin::quickFixProvider() const
Leandro Melo's avatar
Leandro Melo committed
151 152 153
{
    return m_quickFixProvider;
}
154

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

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

Leandro Melo's avatar
Leandro Melo committed
166 167
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
168
    registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
169

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

Tobias Hunger's avatar
Tobias Hunger committed
172 173
    IWizardFactory *wizard = new CppClassWizard;
    wizard->setWizardKind(IWizardFactory::ClassWizard);
hjk's avatar
hjk committed
174 175 176 177 178 179
    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
180

181
    Context context(Constants::CPPEDITOR_ID);
con's avatar
con committed
182

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

185 186
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
187

188
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
189 190
    contextMenu->addAction(cmd);

191
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
192
    contextMenu->addAction(cmd);
193
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
194

195
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
196 197 198 199 200 201
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

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

211
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
212 213 214
    cppToolsMenu->addAction(cmd);

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

225
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
226
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
227
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
228
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
229
    contextMenu->addAction(cmd);
230
    cppToolsMenu->addAction(cmd);
231

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

239 240 241 242 243 244 245
    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);

246
    // Refactoring sub-menu
247 248
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
249
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
250
    contextMenu->addSeparator(globalContext);
251 252 253

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

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

270 271
    cppToolsMenu->addSeparator(globalContext);
    QAction *inspectCppCodeModel = new QAction(tr("Inspect C++ Code Model..."), this);
272 273 274
    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()));
275
    cppToolsMenu->addAction(cmd);
276

277
    contextMenu->addSeparator(context);
278

279
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
280
    contextMenu->addAction(cmd);
281

282
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
283
    contextMenu->addAction(cmd);
284

285 286 287 288
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
289

290
    readSettings();
con's avatar
con committed
291 292 293
    return true;
}

294
void CppEditorPlugin::readSettings()
295
{
296
    m_sortedOutline = ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), true).toBool();
297 298
}

299
void CppEditorPlugin::writeSettings()
300
{
301
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
302 303
}

304
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
305 306 307
{
}

308
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
309 310
{
    writeSettings();
311
    return SynchronousShutdown;
312 313
}

314
static CppEditorWidget *currentCppEditorWidget()
315
{
316
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
317 318
}

319
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
320
{
321
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
322
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
323 324
}

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

331
void CppEditorPlugin::renameSymbolUnderCursor()
332
{
333
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
334
        editorWidget->renameSymbolUnderCursor();
335 336
}

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

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

349
void CppEditorPlugin::onTaskStarted(Core::Id type)
350
{
351
    if (type == CppTools::Constants::TASK_INDEX) {
352 353
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
354
        m_reparseExternallyChangedFiles->setEnabled(false);
355
        m_openTypeHierarchyAction->setEnabled(false);
356
        m_openIncludeHierarchyAction->setEnabled(false);
357
    }
358 359
}

360
void CppEditorPlugin::onAllTasksFinished(Core::Id type)
361
{
362
    if (type == CppTools::Constants::TASK_INDEX) {
363 364
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
365
        m_reparseExternallyChangedFiles->setEnabled(true);
366
        m_openTypeHierarchyAction->setEnabled(true);
367
        m_openIncludeHierarchyAction->setEnabled(true);
368
    }
369 370
}

371 372 373 374 375 376 377 378 379 380
void CppEditorPlugin::inspectCppCodeModel()
{
    if (m_cppCodeModelInspectorDialog) {
        ICore::raiseWindow(m_cppCodeModelInspectorDialog);
    } else {
        m_cppCodeModelInspectorDialog = new CppCodeModelInspectorDialog(ICore::mainWindow());
        m_cppCodeModelInspectorDialog->show();
    }
}

381
void CppEditorPlugin::openTypeHierarchy()
382
{
383
    if (currentCppEditorWidget()) {
384 385
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
386 387 388 389
        emit typeHierarchyRequested();
    }
}

390 391
void CppEditorPlugin::openIncludeHierarchy()
{
392
    if (currentCppEditorWidget()) {
393 394 395 396 397
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
        navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
        emit includeHierarchyRequested();
    }
}
398 399 400

} // namespace Internal
} // namespace CppEditor