cppeditorplugin.cpp 14.6 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 "cppsnippetprovider.h"
Leandro Melo's avatar
Leandro Melo committed
40
#include "cppquickfixassistant.h"
Nikolai Kosjar's avatar
Nikolai Kosjar committed
41
#include "cppquickfixes.h"
42
#include "cpphighlighterfactory.h"
con's avatar
con committed
43

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

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

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

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

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

66 67
enum { QUICKFIX_INTERVAL = 20 };

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

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

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

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

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

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

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

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

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

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

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

129
    // method combo box sorting
130 131
    connect(this, SIGNAL(outlineSortingChanged(bool)),
            editor, SLOT(setSortedOutline(bool)));
132 133
}

134
void CppEditorPlugin::setSortedOutline(bool sorted)
135
{
136 137
    m_sortedOutline = sorted;
    emit outlineSortingChanged(sorted);
138 139
}

140
bool CppEditorPlugin::sortedOutline() const
141
{
142
    return m_sortedOutline;
con's avatar
con committed
143 144
}

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(this));
156
    addAutoReleasedObject(new CppHoverHandler);
157
    addAutoReleasedObject(new CppOutlineWidgetFactory);
158
    addAutoReleasedObject(new CppTypeHierarchyFactory);
159
    addAutoReleasedObject(new CppSnippetProvider);
160
    addAutoReleasedObject(new CppHighlighterFactory);
161

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

166 167
    QObject *core = ICore::instance();
    CppFileWizard::BaseFileWizardParameters wizardParameters(IWizard::FileWizard);
con's avatar
con committed
168

169
    wizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY));
170 171
    wizardParameters.setDisplayCategory(QCoreApplication::translate(Constants::WIZARD_CATEGORY,
                                                                    Constants::WIZARD_TR_CATEGORY));
172
    wizardParameters.setDisplayName(tr("C++ Class"));
Friedemann Kleint's avatar
Friedemann Kleint committed
173
    wizardParameters.setId(QLatin1String("A.Class"));
174
    wizardParameters.setKind(IWizard::ClassWizard);
175
    wizardParameters.setDescription(tr("Creates a C++ header and a source file for a new class that you can add to a C++ project."));
Friedemann Kleint's avatar
Friedemann Kleint committed
176
    addAutoReleasedObject(new CppClassWizard(wizardParameters, core));
con's avatar
con committed
177

178
    wizardParameters.setKind(IWizard::FileWizard);
179
    wizardParameters.setDescription(tr("Creates a C++ source file that you can add to a C++ project."));
180
    wizardParameters.setDisplayName(tr("C++ Source File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
181
    wizardParameters.setId(QLatin1String("B.Source"));
182
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Source, core));
con's avatar
con committed
183

184
    wizardParameters.setDescription(tr("Creates a C++ header file that you can add to a C++ project."));
185
    wizardParameters.setDisplayName(tr("C++ Header File"));
Friedemann Kleint's avatar
Friedemann Kleint committed
186 187
    wizardParameters.setId(QLatin1String("C.Header"));
    addAutoReleasedObject(new CppFileWizard(wizardParameters, Header, core));
con's avatar
con committed
188

189
    Context context(CppEditor::Constants::C_CPPEDITOR);
con's avatar
con committed
190

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

193 194
    Command *cmd;
    ActionContainer *cppToolsMenu = ActionManager::actionContainer(CppTools::Constants::M_TOOLS_CPP);
con's avatar
con committed
195

196
    cmd = ActionManager::command(CppTools::Constants::SWITCH_HEADER_SOURCE);
197 198
    contextMenu->addAction(cmd);

199
    cmd = ActionManager::command(TextEditor::Constants::FOLLOW_SYMBOL_UNDER_CURSOR);
mae's avatar
mae committed
200
    contextMenu->addAction(cmd);
201
    cppToolsMenu->addAction(cmd);
con's avatar
con committed
202

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

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

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

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

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

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

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

    // Update context in global context
257
    cppToolsMenu->addSeparator(globalContext);
258
    m_updateCodeModelAction = new QAction(tr("Update Code Model"), this);
259
    cmd = ActionManager::registerAction(m_updateCodeModelAction, Constants::UPDATE_CODEMODEL, globalContext);
260
    CppTools::CppModelManagerInterface *cppModelManager = CppTools::CppModelManagerInterface::instance();
261 262
    connect(m_updateCodeModelAction, SIGNAL(triggered()), cppModelManager, SLOT(updateModifiedSourceFiles()));
    cppToolsMenu->addAction(cmd);
263

264
    m_actionHandler = new TextEditor::TextEditorActionHandler(CppEditor::Constants::C_CPPEDITOR,
con's avatar
con committed
265 266
        TextEditor::TextEditorActionHandler::Format
        | TextEditor::TextEditorActionHandler::UnCommentSelection
267 268
        | TextEditor::TextEditorActionHandler::UnCollapseAll
        | TextEditor::TextEditorActionHandler::FollowSymbolUnderCursor);
con's avatar
con committed
269

270
    m_actionHandler->initializeActions();
271

272
    contextMenu->addSeparator(context);
273

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

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

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

285 286
    connect(EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
            SLOT(currentEditorChanged(Core::IEditor*)));
287

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

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

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

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

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

312
void CppEditorPlugin::switchDeclarationDefinition()
con's avatar
con committed
313
{
314
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
315
    if (editor)
316 317 318
        editor->switchDeclarationDefinition(/*inNextSplit*/ false);
}

319
void CppEditorPlugin::openDeclarationDefinitionInNextSplit()
320
{
321
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
322 323
    if (editor)
        editor->switchDeclarationDefinition(/*inNextSplit*/ true);
con's avatar
con committed
324 325
}

326
void CppEditorPlugin::renameSymbolUnderCursor()
327
{
328
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
329 330 331 332
    if (editor)
        editor->renameSymbolUnderCursor();
}

333
void CppEditorPlugin::findUsages()
334
{
335
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
336 337 338 339
    if (editor)
        editor->findUsages();
}

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_updateCodeModelAction->setEnabled(false);
346
        m_openTypeHierarchyAction->setEnabled(false);
347
    }
348 349
}

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

360
void CppEditorPlugin::currentEditorChanged(IEditor *editor)
361
{
362
    if (!editor)
363 364
        return;

365
    if (CPPEditorWidget *textEditor = qobject_cast<CPPEditorWidget *>(editor->widget()))
366
        textEditor->semanticRehighlight(/*force = */ true);
367 368
}

369
void CppEditorPlugin::openTypeHierarchy()
370
{
371
    CPPEditorWidget *editor = qobject_cast<CPPEditorWidget*>(EditorManager::currentEditor()->widget());
372
    if (editor) {
373 374
        NavigationWidget *navigation = NavigationWidget::instance();
        navigation->activateSubWidget(Constants::TYPE_HIERARCHY_ID);
375 376 377 378
        emit typeHierarchyRequested();
    }
}

379
Q_EXPORT_PLUGIN(CppEditorPlugin)