cppeditorplugin.cpp 15.3 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);
94

95
        setEditorActionHandlers(TextEditorActionHandler::Format
96 97 98 99
                              | TextEditorActionHandler::UnCommentSelection
                              | TextEditorActionHandler::UnCollapseAll
                              | TextEditorActionHandler::FollowSymbolUnderCursor);

100 101
        addHoverHandler(new CppHoverHandler);

102 103 104 105 106
        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 CppOutlineWidgetFactory);
159
    addAutoReleasedObject(new CppTypeHierarchyFactory);
160
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
161
    addAutoReleasedObject(new CppSnippetProvider);
162

Leandro Melo's avatar
Leandro Melo committed
163 164
    m_quickFixProvider = new CppQuickFixAssistProvider;
    addAutoReleasedObject(m_quickFixProvider);
165
    registerQuickFixes(this);
Roberto Raggi's avatar
Roberto Raggi committed
166

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

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

178
    Context context(Constants::CPPEDITOR_ID);
con's avatar
con committed
179

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

182 183
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
184

185
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
186 187
    contextMenu->addAction(cmd);

188
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
189
    contextMenu->addAction(cmd);
190
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
191

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

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

208
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
209 210 211
    cppToolsMenu->addAction(cmd);

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

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

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

236 237 238 239 240 241 242
    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);

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

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

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

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

274
    contextMenu->addSeparator(context);
275

276
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
277
    contextMenu->addAction(cmd);
278

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

282 283 284 285
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
286

287
    readSettings();
con's avatar
con committed
288 289 290
    return true;
}

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

296
void CppEditorPlugin::writeSettings()
297
{
298
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
299 300
}

301
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
302 303 304
{
}

305
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
306 307
{
    writeSettings();
308
    return SynchronousShutdown;
309 310
}

311
static CppEditorWidget *currentCppEditorWidget()
312
{
313
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
314 315
}

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

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

328
void CppEditorPlugin::renameSymbolUnderCursor()
329
{
330
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
331
        editorWidget->renameSymbolUnderCursor();
332 333
}

334
void CppEditorPlugin::findUsages()
335
{
336
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
337
        editorWidget->findUsages();
338 339
}

340 341
void CppEditorPlugin::showPreProcessorDialog()
{
342
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
343
        editorWidget->showPreProcessorWidget();
344 345
}

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

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

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

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

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

} // namespace Internal
} // namespace CppEditor