cppeditorplugin.cpp 14.7 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 "cpphighlighter.h"
39
#include "cpphoverhandler.h"
40
#include "cppincludehierarchy.h"
41
#include "cppoutline.h"
Leandro Melo's avatar
Leandro Melo committed
42
#include "cppquickfixassistant.h"
Nikolai Kosjar's avatar
Nikolai Kosjar committed
43
#include "cppquickfixes.h"
44 45
#include "cppsnippetprovider.h"
#include "cpptypehierarchy.h"
46

47
#include <coreplugin/actionmanager/actioncontainer.h>
48 49 50 51
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/fileiconprovider.h>
#include <coreplugin/icore.h>
52
#include <coreplugin/navigationwidget.h>
53 54
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cpptoolsconstants.h>
55
#include <texteditor/texteditoractionhandler.h>
56
#include <texteditor/texteditorconstants.h>
57

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

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

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

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

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

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

74 75
enum { QUICKFIX_INTERVAL = 20 };

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

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

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

104 105
        addHoverHandler(new CppHoverHandler);

106
        if (!Utils::HostOsInfo::isMacHost() && !Utils::HostOsInfo::isWindowsHost()) {
Alessandro Portale's avatar
Alessandro Portale committed
107 108 109 110 111 112 113 114 115
            FileIconProvider::registerIconOverlayForMimeType(
                        QIcon(creatorTheme()->imageFile(Theme::IconOverlayCppSource, QLatin1String(":/cppeditor/images/qt_cpp.png"))),
                        Constants::CPP_SOURCE_MIMETYPE);
            FileIconProvider::registerIconOverlayForMimeType(
                        QIcon(creatorTheme()->imageFile(Theme::IconOverlayCSource, QLatin1String(":/cppeditor/images/qt_c.png"))),
                        Constants::C_SOURCE_MIMETYPE);
            FileIconProvider::registerIconOverlayForMimeType(
                        QIcon(creatorTheme()->imageFile(Theme::IconOverlayCppHeader, QLatin1String(":/cppeditor/images/qt_h.png"))),
                        Constants::CPP_HEADER_MIMETYPE);
116
        }
117
    }
118
};
con's avatar
con committed
119

120
///////////////////////////////// CppEditorPlugin //////////////////////////////////
con's avatar
con committed
121

122
CppEditorPlugin *CppEditorPlugin::m_instance = 0;
con's avatar
con committed
123

124
CppEditorPlugin::CppEditorPlugin() :
125 126
    m_renameSymbolUnderCursorAction(0),
    m_findUsagesAction(0),
127
    m_reparseExternallyChangedFiles(0),
Leandro Melo's avatar
Leandro Melo committed
128
    m_openTypeHierarchyAction(0),
129
    m_openIncludeHierarchyAction(0),
Leandro Melo's avatar
Leandro Melo committed
130
    m_quickFixProvider(0)
con's avatar
con committed
131 132 133 134
{
    m_instance = this;
}

135
CppEditorPlugin::~CppEditorPlugin()
con's avatar
con committed
136 137 138 139
{
    m_instance = 0;
}

140
CppEditorPlugin *CppEditorPlugin::instance()
con's avatar
con committed
141 142 143 144
{
    return m_instance;
}

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 CppOutlineWidgetFactory);
157
    addAutoReleasedObject(new CppTypeHierarchyFactory);
158
    addAutoReleasedObject(new CppIncludeHierarchyFactory);
159
    addAutoReleasedObject(new CppSnippetProvider);
160

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

165
    Context context(Constants::CPPEDITOR_ID);
con's avatar
con committed
166

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

169 170
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
171

172
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
173 174
    contextMenu->addAction(cmd);

175
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
176
    contextMenu->addAction(cmd);
177
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
178

179
    QAction *openPreprocessorDialog = new QAction(tr("Additional Preprocessor Directives..."), this);
180 181 182 183 184 185
    cmd = ActionManager::registerAction(openPreprocessorDialog,
                                        Constants::OPEN_PREPROCESSOR_DIALOG, context);
    cmd->setDefaultKeySequence(QKeySequence());
    connect(openPreprocessorDialog, SIGNAL(triggered()), this, SLOT(showPreProcessorDialog()));
    cppToolsMenu->addAction(cmd);

186
    QAction *switchDeclarationDefinition = new QAction(tr("Switch Between Function Declaration/Definition"), this);
187
    cmd = ActionManager::registerAction(switchDeclarationDefinition,
188
        Constants::SWITCH_DECLARATION_DEFINITION, context, true);
189
    cmd->setDefaultKeySequence(QKeySequence(tr("Shift+F2")));
con's avatar
con committed
190 191
    connect(switchDeclarationDefinition, SIGNAL(triggered()),
            this, SLOT(switchDeclarationDefinition()));
mae's avatar
mae committed
192
    contextMenu->addAction(cmd);
193
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
194

195
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR_IN_NEXT_SPLIT);
196 197 198
    cppToolsMenu->addAction(cmd);

    QAction *openDeclarationDefinitionInNextSplit =
199
            new QAction(tr("Open Function Declaration/Definition in Next Split"), this);
200
    cmd = ActionManager::registerAction(openDeclarationDefinitionInNextSplit,
201
        Constants::OPEN_DECLARATION_DEFINITION_IN_NEXT_SPLIT, context, true);
202 203 204
    cmd->setDefaultKeySequence(QKeySequence(Utils::HostOsInfo::isMacHost()
                                            ? tr("Meta+E, Shift+F2")
                                            : tr("Ctrl+E, Shift+F2")));
205 206 207 208
    connect(openDeclarationDefinitionInNextSplit, SIGNAL(triggered()),
            this, SLOT(openDeclarationDefinitionInNextSplit()));
    cppToolsMenu->addAction(cmd);

209
    m_findUsagesAction = new QAction(tr("Find Usages"), this);
210
    cmd = ActionManager::registerAction(m_findUsagesAction, Constants::FIND_USAGES, context);
211
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+U")));
212
    connect(m_findUsagesAction, SIGNAL(triggered()), this, SLOT(findUsages()));
213
    contextMenu->addAction(cmd);
214
    cppToolsMenu->addAction(cmd);
215

216
    m_openTypeHierarchyAction = new QAction(tr("Open Type Hierarchy"), this);
217 218
    cmd = ActionManager::registerAction(m_openTypeHierarchyAction, Constants::OPEN_TYPE_HIERARCHY, context);
    cmd->setDefaultKeySequence(QKeySequence(UseMacShortcuts ? tr("Meta+Shift+T") : tr("Ctrl+Shift+T")));
219 220 221 222
    connect(m_openTypeHierarchyAction, SIGNAL(triggered()), this, SLOT(openTypeHierarchy()));
    contextMenu->addAction(cmd);
    cppToolsMenu->addAction(cmd);

223 224 225 226 227 228 229
    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);

230
    // Refactoring sub-menu
231 232
    Context globalContext(Core::Constants::C_GLOBAL);
    Command *sep = contextMenu->addSeparator(globalContext);
233
    sep->action()->setObjectName(QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT));
234
    contextMenu->addSeparator(globalContext);
235 236 237

    m_renameSymbolUnderCursorAction = new QAction(tr("Rename Symbol Under Cursor"),
                                                  this);
238
    cmd = ActionManager::registerAction(m_renameSymbolUnderCursorAction,
239 240
                             Constants::RENAME_SYMBOL_UNDER_CURSOR,
                             context);
241
    cmd->setDefaultKeySequence(QKeySequence(tr("CTRL+SHIFT+R")));
242 243 244 245 246
    connect(m_renameSymbolUnderCursorAction, SIGNAL(triggered()),
            this, SLOT(renameSymbolUnderCursor()));
    cppToolsMenu->addAction(cmd);

    // Update context in global context
247
    cppToolsMenu->addSeparator(globalContext);
248 249
    m_reparseExternallyChangedFiles = new QAction(tr("Reparse Externally Changed Files"), this);
    cmd = ActionManager::registerAction(m_reparseExternallyChangedFiles, Constants::UPDATE_CODEMODEL, globalContext);
250
    CppTools::CppModelManager *cppModelManager = CppTools::CppModelManager::instance();
251
    connect(m_reparseExternallyChangedFiles, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
252
    cppToolsMenu->addAction(cmd);
253

254 255
    cppToolsMenu->addSeparator(globalContext);
    QAction *inspectCppCodeModel = new QAction(tr("Inspect C++ Code Model..."), this);
256 257 258
    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()));
259
    cppToolsMenu->addAction(cmd);
260

261
    contextMenu->addSeparator(context);
262

263
    cmd = ActionManager::command(TextEditor::Constants::AUTO_INDENT_SELECTION);
mae's avatar
mae committed
264
    contextMenu->addAction(cmd);
265

266
    cmd = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
mae's avatar
mae committed
267
    contextMenu->addAction(cmd);
268

269 270 271 272
    connect(ProgressManager::instance(), SIGNAL(taskStarted(Core::Id)),
            this, SLOT(onTaskStarted(Core::Id)));
    connect(ProgressManager::instance(), SIGNAL(allTasksFinished(Core::Id)),
            this, SLOT(onAllTasksFinished(Core::Id)));
273

con's avatar
con committed
274 275 276
    return true;
}

277
void CppEditorPlugin::extensionsInitialized()
con's avatar
con committed
278 279 280
{
}

281
ExtensionSystem::IPlugin::ShutdownFlag CppEditorPlugin::aboutToShutdown()
282
{
283
    return SynchronousShutdown;
284 285
}

286
static CppEditorWidget *currentCppEditorWidget()
287
{
288
    return qobject_cast<CppEditorWidget*>(EditorManager::currentEditor()->widget());
289 290
}

291
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
292
{
293
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
294
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ false);
295 296
}

297
void CppEditorPlugin::openDeclarationDefinitionInNextSplit()
298
{
299
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
300
        editorWidget->switchDeclarationDefinition(/*inNextSplit*/ true);
con's avatar
con committed
301 302
}

303
void CppEditorPlugin::renameSymbolUnderCursor()
304
{
305
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
306
        editorWidget->renameSymbolUnderCursor();
307 308
}

309
void CppEditorPlugin::findUsages()
310
{
311
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
312
        editorWidget->findUsages();
313 314
}

315 316
void CppEditorPlugin::showPreProcessorDialog()
{
317
    if (CppEditorWidget *editorWidget = currentCppEditorWidget())
318
        editorWidget->showPreProcessorWidget();
319 320
}

321
void CppEditorPlugin::onTaskStarted(Core::Id type)
322
{
323
    if (type == CppTools::Constants::TASK_INDEX) {
324 325
        m_renameSymbolUnderCursorAction->setEnabled(false);
        m_findUsagesAction->setEnabled(false);
326
        m_reparseExternallyChangedFiles->setEnabled(false);
327
        m_openTypeHierarchyAction->setEnabled(false);
328
        m_openIncludeHierarchyAction->setEnabled(false);
329
    }
330 331
}

332
void CppEditorPlugin::onAllTasksFinished(Core::Id type)
333
{
334
    if (type == CppTools::Constants::TASK_INDEX) {
335 336
        m_renameSymbolUnderCursorAction->setEnabled(true);
        m_findUsagesAction->setEnabled(true);
337
        m_reparseExternallyChangedFiles->setEnabled(true);
338
        m_openTypeHierarchyAction->setEnabled(true);
339
        m_openIncludeHierarchyAction->setEnabled(true);
340
    }
341 342
}

343 344 345 346 347 348 349 350 351 352
void CppEditorPlugin::inspectCppCodeModel()
{
    if (m_cppCodeModelInspectorDialog) {
        ICore::raiseWindow(m_cppCodeModelInspectorDialog);
    } else {
        m_cppCodeModelInspectorDialog = new CppCodeModelInspectorDialog(ICore::mainWindow());
        m_cppCodeModelInspectorDialog->show();
    }
}

353
void CppEditorPlugin::openTypeHierarchy()
354
{
355
    if (currentCppEditorWidget()) {
356 357
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
358 359 360 361
        emit typeHierarchyRequested();
    }
}

362 363
void CppEditorPlugin::openIncludeHierarchy()
{
364
    if (currentCppEditorWidget()) {
365 366 367 368 369
        Core::NavigationWidget *navigation = Core::NavigationWidget::instance();
        navigation->activateSubWidget(Core::Id(Constants::INCLUDE_HIERARCHY_ID));
        emit includeHierarchyRequested();
    }
}
370 371 372

} // namespace Internal
} // namespace CppEditor