cppeditorplugin.cpp 15 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 "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

59
#include <utils/hostosinfo.h>
con's avatar
con committed
60

61 62
#include <utils/theme/theme.h>

63 64
#include <QCoreApplication>
#include <QStringList>
con's avatar
con committed
65

66
using namespace Core;
67
using namespace TextEditor;
68
using namespace Utils;
69 70 71

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

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

75 76
enum { QUICKFIX_INTERVAL = 20 };

77
//////////////////////////// CppEditorFactory /////////////////////////////
con's avatar
con committed
78

79
class CppEditorFactory : public TextEditorFactory
con's avatar
con committed
80
{
81 82 83 84 85 86 87 88 89 90 91 92 93 94
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; });
95
        setCommentStyle(Utils::CommentDefinition::CppStyle);
hjk's avatar
hjk committed
96 97 98
        setCodeFoldingSupported(true);
        setMarksVisible(true);
        setParenthesesMatchingEnabled(true);
99

100
        setEditorActionHandlers(TextEditorActionHandler::Format
101 102 103 104
                              | TextEditorActionHandler::UnCommentSelection
                              | TextEditorActionHandler::UnCollapseAll
                              | TextEditorActionHandler::FollowSymbolUnderCursor);

105 106
        addHoverHandler(new CppHoverHandler);

107
        if (!Utils::HostOsInfo::isMacHost() && !Utils::HostOsInfo::isWindowsHost()) {
108 109 110
            FileIconProvider::registerIconOverlayForMimeType(creatorTheme()->iconOverlay(Theme::CppSourceMimetype).toLatin1().data(), Constants::CPP_SOURCE_MIMETYPE);
            FileIconProvider::registerIconOverlayForMimeType(creatorTheme()->iconOverlay(Theme::CSourceMimetype).toLatin1().data(),   Constants::C_SOURCE_MIMETYPE);
            FileIconProvider::registerIconOverlayForMimeType(creatorTheme()->iconOverlay(Theme::CppHeaderMimetype).toLatin1().data(), Constants::CPP_HEADER_MIMETYPE);
111
        }
112
    }
113
};
con's avatar
con committed
114

115
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
116

117
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
118

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

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

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

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

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

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

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

162
    addAutoReleasedObject(new CppEditorFactory);
163
    addAutoReleasedObject(new CppOutlineWidgetFactory);
164
    addAutoReleasedObject(new CppTypeHierarchyFactory);
165
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
166
    addAutoReleasedObject(new CppSnippetProvider);
167

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

172
    Context context(Constants::CPPEDITOR_ID);
con's avatar
con committed
173

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

176 177
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
178

179
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
180 181
    contextMenu->addAction(cmd);

182
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
183
    contextMenu->addAction(cmd);
184
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
185

186
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
187 188 189 190 191 192
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

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

202
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
203 204 205
    cppToolsMenu->addAction(cmd);

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

216
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
217
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
218
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
219
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
220
    contextMenu->addAction(cmd);
221
    cppToolsMenu->addAction(cmd);
222

223
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
224 225
    cmd = ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
226 227 228 229
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

230 231 232 233 234 235 236
    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);

237
    // Refactoring sub-menu
238 239
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
240
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
241
    contextMenu->addSeparator(globalContext);
242 243 244

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
245
    cmd = ActionManager::registerAction(m_renameSymbolUnderCursorAction,
246 247
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
248
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
249 250 251 252 253
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
254
    cppToolsMenu->addSeparator(globalContext);
255 256
    m_reparseExternallyChangedFiles = new QAction(tr("Reparse Externally Changed Files"), this);
    cmd = ActionManager::registerAction(m_reparseExternallyChangedFiles, Constants::UPDATE_CODEMODEL, globalContext);
257
    CppTools::CppModelManager *cppModelManager = CppTools::CppModelManager::instance();
258
    connect(m_reparseExternallyChangedFiles, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
259
    cppToolsMenu->addAction(cmd);
260

261 262
    cppToolsMenu->addSeparator(globalContext);
    QAction *inspectCppCodeModel = new QAction(tr("Inspect C++ Code Model..."), this);
263 264 265
    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()));
266
    cppToolsMenu->addAction(cmd);
267

268
    contextMenu->addSeparator(context);
269

270
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
271
    contextMenu->addAction(cmd);
272

273
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
274
    contextMenu->addAction(cmd);
275

276 277 278 279
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
280

281
    readSettings();
con's avatar
con committed
282 283 284
    return true;
}

285
void CppEditorPlugin::readSettings()
286
{
287
    m_sortedOutline = ICore::settings()->value(QLatin1String("CppTools/SortedMethodOverview"), true).toBool();
288 289
}

290
void CppEditorPlugin::writeSettings()
291
{
292
    ICore::settings()->setValue(QLatin1String("CppTools/SortedMethodOverview"), m_sortedOutline);
293 294
}

295
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
296 297 298
{
}

299
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
300 301
{
    writeSettings();
302
    return SynchronousShutdown;
303 304
}

305
static CppEditorWidget *currentCppEditorWidget()
306
{
307
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
308 309
}

310
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
311
{
312
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
313
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
314 315
}

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

322
void CppEditorPlugin::renameSymbolUnderCursor()
323
{
324
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
325
        editorWidget->renameSymbolUnderCursor();
326 327
}

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

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

340
void CppEditorPlugin::onTaskStarted(Core::Id type)
341
{
342
    if (type == CppTools::Constants::TASK_INDEX) {
343 344
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
345
        m_reparseExternallyChangedFiles->setEnabled(false);
346
        m_openTypeHierarchyAction->setEnabled(false);
347
        m_openIncludeHierarchyAction->setEnabled(false);
348
    }
349 350
}

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

362 363 364 365 366 367 368 369 370 371
void CppEditorPlugin::inspectCppCodeModel()
{
    if (m_cppCodeModelInspectorDialog) {
        ICore::raiseWindow(m_cppCodeModelInspectorDialog);
    } else {
        m_cppCodeModelInspectorDialog = new CppCodeModelInspectorDialog(ICore::mainWindow());
        m_cppCodeModelInspectorDialog->show();
    }
}

372
void CppEditorPlugin::openTypeHierarchy()
373
{
374
    if (currentCppEditorWidget()) {
375 376
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
377 378 379 380
        emit typeHierarchyRequested();
    }
}

381 382
void CppEditorPlugin::openIncludeHierarchy()
{
383
    if (currentCppEditorWidget()) {
384 385 386 387 388
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
        navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
        emit includeHierarchyRequested();
    }
}
389 390 391

} // namespace Internal
} // namespace CppEditor