2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cppeditor.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2013-09-20 10:32:41 +02:00
|
|
|
#include "cppautocompleter.h"
|
2014-06-09 22:02:57 -04:00
|
|
|
#include "cppcanonicalsymbol.h"
|
|
|
|
|
#include "cppdocumentationcommenthelper.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cppeditorconstants.h"
|
2014-05-23 13:55:03 -04:00
|
|
|
#include "cppeditoroutline.h"
|
2013-03-19 13:44:02 +01:00
|
|
|
#include "cppeditorplugin.h"
|
2013-09-20 10:32:41 +02:00
|
|
|
#include "cppfollowsymbolundercursor.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cpphighlighter.h"
|
2014-05-20 12:24:32 -04:00
|
|
|
#include "cpplocalrenaming.h"
|
2013-10-17 11:19:26 +02:00
|
|
|
#include "cpppreprocessordialog.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "cppquickfixassistant.h"
|
2009-08-07 13:02:36 +02:00
|
|
|
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2014-05-20 09:24:48 -04:00
|
|
|
|
2012-02-07 15:09:08 +01:00
|
|
|
#include <cpptools/cppchecksymbols.h>
|
2010-07-05 13:48:53 +02:00
|
|
|
#include <cpptools/cppcodeformatter.h>
|
2013-08-30 12:55:06 +02:00
|
|
|
#include <cpptools/cppcompletionassistprovider.h>
|
2012-02-07 15:09:08 +01:00
|
|
|
#include <cpptools/cpphighlightingsupport.h>
|
2014-05-20 09:34:10 -04:00
|
|
|
#include <cpptools/cppmodelmanagerinterface.h>
|
2014-05-20 09:24:48 -04:00
|
|
|
#include <cpptools/cppsemanticinfo.h>
|
|
|
|
|
#include <cpptools/cpptoolseditorsupport.h>
|
|
|
|
|
#include <cpptools/cpptoolsplugin.h>
|
2011-11-14 13:14:39 +01:00
|
|
|
#include <cpptools/cpptoolsreuse.h>
|
2012-01-20 14:43:21 +01:00
|
|
|
#include <cpptools/symbolfinder.h>
|
2014-05-20 09:24:48 -04:00
|
|
|
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <texteditor/basetextdocument.h>
|
2010-12-10 10:32:46 +01:00
|
|
|
#include <texteditor/basetextdocumentlayout.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <texteditor/codeassist/basicproposalitem.h>
|
|
|
|
|
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
|
|
|
|
|
#include <texteditor/codeassist/genericproposal.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <texteditor/fontsettings.h>
|
2011-08-10 09:50:04 +02:00
|
|
|
#include <texteditor/refactoroverlay.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <cplusplus/ASTPath.h>
|
2014-05-20 09:24:48 -04:00
|
|
|
#include <cplusplus/BackwardsScanner.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
#include <cplusplus/ExpressionUnderCursor.h>
|
|
|
|
|
#include <cplusplus/OverviewModel.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QAction>
|
2014-05-20 09:24:48 -04:00
|
|
|
#include <QFutureWatcher>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QMenu>
|
2014-05-20 09:24:48 -04:00
|
|
|
#include <QPointer>
|
|
|
|
|
#include <QSignalMapper>
|
|
|
|
|
#include <QTextEdit>
|
|
|
|
|
#include <QTimer>
|
2013-08-20 07:46:19 +02:00
|
|
|
#include <QToolButton>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-06-24 16:40:30 +02:00
|
|
|
enum {
|
2011-08-10 09:50:04 +02:00
|
|
|
UPDATE_USES_INTERVAL = 500,
|
|
|
|
|
UPDATE_FUNCTION_DECL_DEF_LINK_INTERVAL = 200
|
2009-06-24 16:40:30 +02:00
|
|
|
};
|
|
|
|
|
|
2009-07-09 12:14:00 +02:00
|
|
|
using namespace CPlusPlus;
|
2012-02-07 15:09:08 +01:00
|
|
|
using namespace CppTools;
|
2009-07-09 12:14:00 +02:00
|
|
|
using namespace CppEditor::Internal;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2014-05-23 13:55:03 -04:00
|
|
|
QTimer *newSingleShotTimer(QObject *parent, int msecInterval)
|
|
|
|
|
{
|
|
|
|
|
QTimer *timer = new QTimer(parent);
|
|
|
|
|
timer->setSingleShot(true);
|
|
|
|
|
timer->setInterval(msecInterval);
|
|
|
|
|
return timer;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
namespace CppEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
CPPEditor::CPPEditor(CppEditorWidget *editor)
|
2011-02-21 16:02:26 +01:00
|
|
|
: BaseTextEditor(editor)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-06-25 17:37:59 +02:00
|
|
|
m_context.add(CppEditor::Constants::C_CPPEDITOR);
|
|
|
|
|
m_context.add(ProjectExplorer::Constants::LANG_CXX);
|
|
|
|
|
m_context.add(TextEditor::Constants::C_TEXTEDITOR);
|
2014-05-08 16:07:24 +02:00
|
|
|
setDuplicateSupported(true);
|
2014-07-30 16:01:34 +02:00
|
|
|
setCommentStyle(Utils::CommentDefinition::CppStyle);
|
2014-07-30 17:02:39 +02:00
|
|
|
setCompletionAssistProvider([this] () -> TextEditor::CompletionAssistProvider * {
|
|
|
|
|
return CppModelManagerInterface::instance()->cppEditorSupport(this)->completionAssistProvider();
|
|
|
|
|
});
|
2014-07-31 14:44:35 +02:00
|
|
|
setAutoCompleter(new CppAutoCompleter);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-24 11:07:26 +01:00
|
|
|
Q_GLOBAL_STATIC(CppTools::SymbolFinder, symbolFinder)
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
class CppEditorWidgetPrivate
|
2014-05-20 09:24:48 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidgetPrivate(CppEditorWidget *q);
|
2014-05-20 09:24:48 -04:00
|
|
|
|
|
|
|
|
public:
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget *q;
|
2014-05-20 09:24:48 -04:00
|
|
|
|
|
|
|
|
QPointer<CppTools::CppModelManagerInterface> m_modelManager;
|
|
|
|
|
|
|
|
|
|
CPPEditorDocument *m_cppEditorDocument;
|
2014-05-23 13:55:03 -04:00
|
|
|
CppEditorOutline *m_cppEditorOutline;
|
|
|
|
|
|
2014-05-26 12:24:23 -04:00
|
|
|
CppDocumentationCommentHelper m_cppDocumentationCommentHelper;
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
QTimer *m_updateUsesTimer;
|
|
|
|
|
QTimer *m_updateFunctionDeclDefLinkTimer;
|
|
|
|
|
QHash<int, QTextCharFormat> m_semanticHighlightFormatMap;
|
|
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
CppLocalRenaming m_localRenaming;
|
2014-05-20 09:24:48 -04:00
|
|
|
|
|
|
|
|
CppTools::SemanticInfo m_lastSemanticInfo;
|
|
|
|
|
QList<TextEditor::QuickFixOperation::Ptr> m_quickFixes;
|
|
|
|
|
|
|
|
|
|
QScopedPointer<QFutureWatcher<TextEditor::HighlightingResult> > m_highlightWatcher;
|
|
|
|
|
unsigned m_highlightRevision; // the editor revision that requested the highlight
|
|
|
|
|
|
|
|
|
|
QScopedPointer<QFutureWatcher<QList<int> > > m_referencesWatcher;
|
|
|
|
|
unsigned m_referencesRevision;
|
|
|
|
|
int m_referencesCursorPosition;
|
|
|
|
|
|
|
|
|
|
FunctionDeclDefLinkFinder *m_declDefLinkFinder;
|
|
|
|
|
QSharedPointer<FunctionDeclDefLink> m_declDefLink;
|
|
|
|
|
|
|
|
|
|
QScopedPointer<FollowSymbolUnderCursor> m_followSymbolUnderCursor;
|
|
|
|
|
QToolButton *m_preprocessorButton;
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidgetPrivate::CppEditorWidgetPrivate(CppEditorWidget *q)
|
2014-05-20 09:24:48 -04:00
|
|
|
: q(q)
|
|
|
|
|
, m_modelManager(CppModelManagerInterface::instance())
|
2014-08-01 23:31:56 +02:00
|
|
|
, m_cppEditorDocument(qobject_cast<CPPEditorDocument *>(q->textDocument()))
|
2014-05-23 13:55:03 -04:00
|
|
|
, m_cppEditorOutline(new CppEditorOutline(q))
|
2014-05-26 12:24:23 -04:00
|
|
|
, m_cppDocumentationCommentHelper(q)
|
2014-05-20 12:24:32 -04:00
|
|
|
, m_localRenaming(q)
|
2014-05-20 09:24:48 -04:00
|
|
|
, m_highlightRevision(0)
|
|
|
|
|
, m_referencesRevision(0)
|
|
|
|
|
, m_referencesCursorPosition(0)
|
|
|
|
|
, m_declDefLinkFinder(new FunctionDeclDefLinkFinder(q))
|
|
|
|
|
, m_followSymbolUnderCursor(new FollowSymbolUnderCursor(q))
|
|
|
|
|
, m_preprocessorButton(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget::CppEditorWidget(QWidget *parent)
|
2014-01-17 17:09:15 +01:00
|
|
|
: TextEditor::BaseTextEditorWidget(new CPPEditorDocument(), parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-01-09 18:04:45 +01:00
|
|
|
ctor();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget::CppEditorWidget(CppEditorWidget *other)
|
2014-01-09 18:04:45 +01:00
|
|
|
: TextEditor::BaseTextEditorWidget(other)
|
|
|
|
|
{
|
|
|
|
|
ctor();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::ctor()
|
2014-01-09 18:04:45 +01:00
|
|
|
{
|
2014-07-30 16:30:31 +02:00
|
|
|
d.reset(new CppEditorWidgetPrivate(this));
|
2014-01-09 18:04:45 +01:00
|
|
|
|
2012-02-07 15:09:08 +01:00
|
|
|
qRegisterMetaType<SemanticInfo>("CppTools::SemanticInfo");
|
2009-07-09 12:14:00 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
setParenthesesMatchingEnabled(true);
|
|
|
|
|
setMarksVisible(true);
|
2009-03-16 17:23:50 +01:00
|
|
|
setCodeFoldingSupported(true);
|
2014-08-01 18:29:38 +02:00
|
|
|
setRevisionsVisible(true);
|
2010-11-10 17:00:07 +01:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_modelManager) {
|
|
|
|
|
CppEditorSupport *editorSupport = d->m_modelManager->cppEditorSupport(editor());
|
2013-04-17 10:58:20 +02:00
|
|
|
connect(editorSupport, SIGNAL(documentUpdated()),
|
|
|
|
|
this, SLOT(onDocumentUpdated()));
|
|
|
|
|
connect(editorSupport, SIGNAL(semanticInfoUpdated(CppTools::SemanticInfo)),
|
|
|
|
|
this, SLOT(updateSemanticInfo(CppTools::SemanticInfo)));
|
2014-02-06 10:22:38 +01:00
|
|
|
connect(editorSupport, SIGNAL(highlighterStarted(QFuture<TextEditor::HighlightingResult>*,uint)),
|
|
|
|
|
this, SLOT(highlighterStarted(QFuture<TextEditor::HighlightingResult>*,uint)));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-07-05 18:52:52 +02:00
|
|
|
|
2011-08-10 09:50:04 +02:00
|
|
|
connect(this, SIGNAL(refactorMarkerClicked(TextEditor::RefactorMarker)),
|
|
|
|
|
this, SLOT(onRefactorMarkerClicked(TextEditor::RefactorMarker)));
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
connect(d->m_declDefLinkFinder, SIGNAL(foundLink(QSharedPointer<FunctionDeclDefLink>)),
|
2011-08-10 09:50:04 +02:00
|
|
|
this, SLOT(onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefLink>)));
|
2011-12-07 15:05:02 +01:00
|
|
|
|
2014-08-01 23:31:56 +02:00
|
|
|
connect(textDocument(), SIGNAL(filePathChanged(QString,QString)),
|
2014-01-10 16:18:17 +01:00
|
|
|
this, SLOT(onFilePathChanged()));
|
2014-05-20 12:24:32 -04:00
|
|
|
|
|
|
|
|
connect(&d->m_localRenaming, SIGNAL(finished()),
|
|
|
|
|
this, SLOT(onLocalRenamingFinished()));
|
|
|
|
|
connect(&d->m_localRenaming, SIGNAL(processKeyPressNormally(QKeyEvent*)),
|
|
|
|
|
this, SLOT(onLocalRenamingProcessKeyPressNormally(QKeyEvent*)));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-08-01 18:01:29 +02:00
|
|
|
// Tool bar creation
|
2014-05-23 13:55:03 -04:00
|
|
|
d->m_updateUsesTimer = newSingleShotTimer(this, UPDATE_USES_INTERVAL);
|
2014-05-20 09:24:48 -04:00
|
|
|
connect(d->m_updateUsesTimer, SIGNAL(timeout()), this, SLOT(updateUsesNow()));
|
|
|
|
|
|
2014-05-23 13:55:03 -04:00
|
|
|
d->m_updateFunctionDeclDefLinkTimer = newSingleShotTimer(this, UPDATE_FUNCTION_DECL_DEF_LINK_INTERVAL);
|
2014-05-20 09:24:48 -04:00
|
|
|
connect(d->m_updateFunctionDeclDefLinkTimer, SIGNAL(timeout()),
|
2013-04-30 14:50:47 +02:00
|
|
|
this, SLOT(updateFunctionDeclDefLinkNow()));
|
2011-08-10 09:50:04 +02:00
|
|
|
|
2014-05-23 13:55:03 -04:00
|
|
|
connect(this, SIGNAL(cursorPositionChanged()),
|
|
|
|
|
d->m_cppEditorOutline, SLOT(updateIndex()));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-06-25 15:47:01 +02:00
|
|
|
// set up slots to document changes
|
2014-01-09 18:04:45 +01:00
|
|
|
connect(document(), SIGNAL(contentsChange(int,int,int)),
|
|
|
|
|
this, SLOT(onContentsChanged(int,int,int)));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-08-10 09:50:04 +02:00
|
|
|
// set up function declaration - definition link
|
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateFunctionDeclDefLink()));
|
|
|
|
|
connect(this, SIGNAL(textChanged()), this, SLOT(updateFunctionDeclDefLink()));
|
2009-07-09 12:14:00 +02:00
|
|
|
|
|
|
|
|
// set up the semantic highlighter
|
|
|
|
|
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateUses()));
|
|
|
|
|
connect(this, SIGNAL(textChanged()), this, SLOT(updateUses()));
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_preprocessorButton = new QToolButton(this);
|
|
|
|
|
d->m_preprocessorButton->setText(QLatin1String("#"));
|
2013-10-23 14:13:06 +02:00
|
|
|
Core::Command *cmd = Core::ActionManager::command(Constants::OPEN_PREPROCESSOR_DIALOG);
|
|
|
|
|
connect(cmd, SIGNAL(keySequenceChanged()), this, SLOT(updatePreprocessorButtonTooltip()));
|
|
|
|
|
updatePreprocessorButtonTooltip();
|
2014-05-20 09:24:48 -04:00
|
|
|
connect(d->m_preprocessorButton, SIGNAL(clicked()), this, SLOT(showPreProcessorWidget()));
|
2014-08-01 16:41:18 +02:00
|
|
|
insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Left, d->m_preprocessorButton);
|
|
|
|
|
insertExtraToolBarWidget(TextEditor::BaseTextEditorWidget::Left, d->m_cppEditorOutline->widget());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-01 18:01:29 +02:00
|
|
|
CppEditorWidget::~CppEditorWidget()
|
|
|
|
|
{
|
|
|
|
|
if (d->m_modelManager)
|
|
|
|
|
d->m_modelManager->deleteCppEditorSupport(editor());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CPPEditorDocument *CppEditorWidget::cppEditorDocument() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_cppEditorDocument;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppEditorOutline *CppEditorWidget::outline() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_cppEditorOutline;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::BaseTextEditor *CppEditorWidget::createEditor()
|
|
|
|
|
{
|
|
|
|
|
return new CPPEditor(this);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::paste()
|
2009-12-03 18:35:36 +01:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
if (d->m_localRenaming.handlePaste())
|
2009-12-03 18:35:36 +01:00
|
|
|
return;
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget::paste();
|
2009-12-03 18:35:36 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::cut()
|
2009-12-03 18:35:36 +01:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
if (d->m_localRenaming.handlePaste())
|
2009-12-03 18:35:36 +01:00
|
|
|
return;
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget::cut();
|
2009-12-03 18:35:36 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::selectAll()
|
2012-01-18 12:09:41 +01:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
if (d->m_localRenaming.handleSelectAll())
|
2009-12-03 18:35:36 +01:00
|
|
|
return;
|
2011-11-14 13:22:44 +01:00
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
BaseTextEditorWidget::selectAll();
|
2009-07-01 18:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
/// \brief Called by \c CppEditorSupport when the document corresponding to the
|
|
|
|
|
/// file in this editor is updated.
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onDocumentUpdated()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-05-23 13:55:03 -04:00
|
|
|
d->m_cppEditorOutline->update();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
const Macro *CppEditorWidget::findCanonicalMacro(const QTextCursor &cursor, Document::Ptr doc) const
|
2009-12-21 14:54:10 +01:00
|
|
|
{
|
2013-07-24 11:52:01 +02:00
|
|
|
if (!doc)
|
2009-12-21 14:54:10 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
int line, col;
|
|
|
|
|
convertPosition(cursor.position(), &line, &col);
|
|
|
|
|
|
2012-03-08 15:53:28 +01:00
|
|
|
if (const Macro *macro = doc->findMacroDefinitionAt(line)) {
|
|
|
|
|
QTextCursor macroCursor = cursor;
|
2014-05-09 10:04:13 -04:00
|
|
|
const QByteArray name = identifierUnderCursor(¯oCursor).toUtf8();
|
2013-12-02 15:47:36 +01:00
|
|
|
if (macro->name() == name)
|
2012-03-08 15:53:28 +01:00
|
|
|
return macro;
|
|
|
|
|
} else if (const Document::MacroUse *use = doc->findMacroUseAt(cursor.position())) {
|
2013-12-02 15:47:36 +01:00
|
|
|
return &use->macro();
|
2012-03-08 15:53:28 +01:00
|
|
|
}
|
2009-12-21 14:54:10 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-10-05 15:17:25 +02:00
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::findUsages()
|
2010-01-29 21:33:57 +01:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_modelManager)
|
2013-10-02 09:26:50 +02:00
|
|
|
return;
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
SemanticInfo info = d->m_lastSemanticInfo;
|
2010-12-03 13:49:35 +01:00
|
|
|
info.snapshot = CppModelManagerInterface::instance()->snapshot();
|
2010-08-05 13:59:09 +02:00
|
|
|
info.snapshot.insert(info.doc);
|
2010-05-31 12:09:28 +02:00
|
|
|
|
2012-03-08 15:53:28 +01:00
|
|
|
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_modelManager->findMacroUsages(*macro);
|
2012-03-08 15:53:28 +01:00
|
|
|
} else {
|
2014-06-09 21:51:14 -04:00
|
|
|
CanonicalSymbol cs(this, info.doc, info.snapshot);
|
2012-03-08 15:53:28 +01:00
|
|
|
Symbol *canonicalSymbol = cs(textCursor());
|
|
|
|
|
if (canonicalSymbol)
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_modelManager->findUsages(canonicalSymbol, cs.context());
|
2009-10-09 11:33:31 +02:00
|
|
|
}
|
2009-10-05 15:17:25 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::renameUsages(const QString &replacement)
|
2010-08-05 13:59:09 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_modelManager)
|
2013-10-02 09:26:50 +02:00
|
|
|
return;
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
SemanticInfo info = d->m_lastSemanticInfo;
|
2010-12-03 13:49:35 +01:00
|
|
|
info.snapshot = CppModelManagerInterface::instance()->snapshot();
|
2010-08-05 13:59:09 +02:00
|
|
|
info.snapshot.insert(info.doc);
|
|
|
|
|
|
2012-03-17 13:26:27 +01:00
|
|
|
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_modelManager->renameMacroUsages(*macro, replacement);
|
2012-03-17 13:26:27 +01:00
|
|
|
} else {
|
2014-06-09 21:51:14 -04:00
|
|
|
CanonicalSymbol cs(this, info.doc, info.snapshot);
|
2012-03-17 13:26:27 +01:00
|
|
|
if (Symbol *canonicalSymbol = cs(textCursor()))
|
|
|
|
|
if (canonicalSymbol->identifier() != 0)
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_modelManager->renameUsages(canonicalSymbol, cs.context(), replacement);
|
2012-03-17 13:26:27 +01:00
|
|
|
}
|
2010-08-05 13:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::markSymbolsNow()
|
2009-10-09 11:33:31 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
QTC_ASSERT(d->m_referencesWatcher, return);
|
|
|
|
|
if (!d->m_referencesWatcher->isCanceled()
|
|
|
|
|
&& d->m_referencesCursorPosition == position()
|
|
|
|
|
&& d->m_referencesRevision == editorRevision()) {
|
|
|
|
|
const SemanticInfo info = d->m_lastSemanticInfo;
|
2013-12-11 15:19:54 +01:00
|
|
|
TranslationUnit *unit = info.doc->translationUnit();
|
2014-05-20 09:24:48 -04:00
|
|
|
const QList<int> result = d->m_referencesWatcher->result();
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-11 15:19:54 +01:00
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-11 15:19:54 +01:00
|
|
|
foreach (int index, result) {
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
unit->getTokenPosition(index, &line, &column);
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-11 15:19:54 +01:00
|
|
|
if (column)
|
|
|
|
|
--column; // adjust the column position.
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-12 21:37:46 +01:00
|
|
|
const int len = unit->tokenAt(index).utf16chars();
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-11 15:19:54 +01:00
|
|
|
QTextCursor cursor(document()->findBlockByNumber(line - 1));
|
|
|
|
|
cursor.setPosition(cursor.position() + column);
|
|
|
|
|
cursor.setPosition(cursor.position() + len, QTextCursor::KeepAnchor);
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-11 15:19:54 +01:00
|
|
|
QTextEdit::ExtraSelection sel;
|
2014-05-20 12:24:32 -04:00
|
|
|
sel.format = textCharFormat(TextEditor::C_OCCURRENCES);
|
2013-12-11 15:19:54 +01:00
|
|
|
sel.cursor = cursor;
|
|
|
|
|
selections.append(sel);
|
|
|
|
|
}
|
2009-09-30 13:25:40 +02:00
|
|
|
|
2013-12-11 15:19:54 +01:00
|
|
|
setExtraSelections(CodeSemanticsSelection, selections);
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_referencesWatcher.reset();
|
2009-08-07 13:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-30 14:50:47 +02:00
|
|
|
static QList<int> lazyFindReferences(Scope *scope, QString code, Document::Ptr doc,
|
|
|
|
|
Snapshot snapshot)
|
2010-07-19 12:17:08 +02:00
|
|
|
{
|
|
|
|
|
TypeOfExpression typeOfExpression;
|
2010-08-10 14:40:27 +02:00
|
|
|
snapshot.insert(doc);
|
|
|
|
|
typeOfExpression.init(doc, snapshot);
|
2013-01-19 13:17:34 +01:00
|
|
|
// make possible to instantiate templates
|
|
|
|
|
typeOfExpression.setExpandTemplates(true);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (Symbol *canonicalSymbol = CanonicalSymbol::canonicalSymbol(scope, code, typeOfExpression))
|
2013-04-30 14:50:47 +02:00
|
|
|
return CppModelManagerInterface::instance()->references(canonicalSymbol,
|
|
|
|
|
typeOfExpression.context());
|
2010-07-19 12:17:08 +02:00
|
|
|
return QList<int>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::markSymbols(const QTextCursor &tc, const SemanticInfo &info)
|
2010-07-19 12:17:08 +02:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
d->m_localRenaming.stop();
|
2010-07-19 12:17:08 +02:00
|
|
|
|
2013-07-24 11:52:01 +02:00
|
|
|
if (!info.doc)
|
2010-07-19 12:17:08 +02:00
|
|
|
return;
|
2014-05-20 12:24:32 -04:00
|
|
|
const QTextCharFormat &occurrencesFormat = textCharFormat(TextEditor::C_OCCURRENCES);
|
2013-01-17 14:44:22 +01:00
|
|
|
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
|
|
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
|
|
|
|
|
|
|
|
|
//Macro definition
|
|
|
|
|
if (macro->fileName() == info.doc->fileName()) {
|
|
|
|
|
QTextCursor cursor(document());
|
2014-05-09 10:04:13 -04:00
|
|
|
cursor.setPosition(macro->utf16CharOffset());
|
2013-04-30 14:50:47 +02:00
|
|
|
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,
|
2014-05-09 10:04:13 -04:00
|
|
|
macro->nameToQString().size());
|
2013-01-17 14:44:22 +01:00
|
|
|
|
|
|
|
|
QTextEdit::ExtraSelection sel;
|
2014-01-21 14:29:25 +01:00
|
|
|
sel.format = occurrencesFormat;
|
2013-01-17 14:44:22 +01:00
|
|
|
sel.cursor = cursor;
|
|
|
|
|
selections.append(sel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Other macro uses
|
2013-03-08 09:55:44 +01:00
|
|
|
foreach (const Document::MacroUse &use, info.doc->macroUses()) {
|
2014-05-05 17:13:36 +03:00
|
|
|
const Macro &useMacro = use.macro();
|
|
|
|
|
if (useMacro.line() != macro->line()
|
2014-05-09 10:04:13 -04:00
|
|
|
|| useMacro.utf16CharOffset() != macro->utf16CharOffset()
|
2014-05-05 17:13:36 +03:00
|
|
|
|| useMacro.length() != macro->length()
|
|
|
|
|
|| useMacro.fileName() != macro->fileName())
|
2013-01-17 14:44:22 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
QTextCursor cursor(document());
|
2014-05-09 10:04:13 -04:00
|
|
|
cursor.setPosition(use.utf16charsBegin());
|
|
|
|
|
cursor.setPosition(use.utf16charsEnd(), QTextCursor::KeepAnchor);
|
2013-01-17 14:44:22 +01:00
|
|
|
|
|
|
|
|
QTextEdit::ExtraSelection sel;
|
2014-01-21 14:29:25 +01:00
|
|
|
sel.format = occurrencesFormat;
|
2013-01-17 14:44:22 +01:00
|
|
|
sel.cursor = cursor;
|
|
|
|
|
selections.append(sel);
|
|
|
|
|
}
|
2010-07-20 14:59:53 +02:00
|
|
|
|
2013-01-17 14:44:22 +01:00
|
|
|
setExtraSelections(CodeSemanticsSelection, selections);
|
|
|
|
|
} else {
|
2014-06-09 21:51:14 -04:00
|
|
|
CanonicalSymbol cs(this, info.doc, info.snapshot);
|
2013-01-17 14:44:22 +01:00
|
|
|
QString expression;
|
2014-06-09 21:51:14 -04:00
|
|
|
if (Scope *scope = cs.getScopeAndExpression(tc, &expression)) {
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_referencesWatcher)
|
|
|
|
|
d->m_referencesWatcher->cancel();
|
|
|
|
|
d->m_referencesWatcher.reset(new QFutureWatcher<QList<int> >);
|
|
|
|
|
connect(d->m_referencesWatcher.data(), SIGNAL(finished()), SLOT(markSymbolsNow()));
|
|
|
|
|
|
|
|
|
|
d->m_referencesRevision = info.revision;
|
|
|
|
|
d->m_referencesCursorPosition = position();
|
|
|
|
|
d->m_referencesWatcher->setFuture(
|
|
|
|
|
QtConcurrent::run(&lazyFindReferences, scope, expression, info.doc, info.snapshot));
|
2013-01-17 14:44:22 +01:00
|
|
|
} else {
|
|
|
|
|
const QList<QTextEdit::ExtraSelection> selections = extraSelections(CodeSemanticsSelection);
|
|
|
|
|
|
2013-07-24 11:52:01 +02:00
|
|
|
if (!selections.isEmpty())
|
2013-01-17 14:44:22 +01:00
|
|
|
setExtraSelections(CodeSemanticsSelection, QList<QTextEdit::ExtraSelection>());
|
|
|
|
|
}
|
2010-07-19 12:17:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::renameSymbolUnderCursor()
|
2009-07-01 17:57:00 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_modelManager)
|
2013-10-02 09:26:50 +02:00
|
|
|
return;
|
|
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
CppEditorSupport *ces = d->m_modelManager->cppEditorSupport(editor());
|
|
|
|
|
updateSemanticInfo(ces->recalculateSemanticInfo());
|
2009-09-29 16:40:32 +02:00
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
if (!d->m_localRenaming.start()) // Rename local symbol
|
|
|
|
|
renameUsages(); // Rename non-local symbol or macro
|
2009-07-01 17:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onContentsChanged(int position, int charsRemoved, int charsAdded)
|
2009-07-01 18:41:04 +02:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
Q_UNUSED(position)
|
|
|
|
|
Q_UNUSED(charsAdded)
|
2009-07-01 18:41:04 +02:00
|
|
|
|
|
|
|
|
if (charsRemoved > 0)
|
|
|
|
|
updateUses();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::updatePreprocessorButtonTooltip()
|
2013-10-23 14:13:06 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
QTC_ASSERT(d->m_preprocessorButton, return);
|
2013-10-23 14:13:06 +02:00
|
|
|
Core::Command *cmd = Core::ActionManager::command(Constants::OPEN_PREPROCESSOR_DIALOG);
|
|
|
|
|
QTC_ASSERT(cmd, return);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_preprocessorButton->setToolTip(cmd->action()->toolTip());
|
2013-10-23 14:13:06 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
QList<QTextEdit::ExtraSelection> CppEditorWidget::createSelectionsFromUses(
|
2014-05-20 12:24:32 -04:00
|
|
|
const QList<SemanticInfo::Use> &uses)
|
2009-06-25 16:11:28 +02:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
QList<QTextEdit::ExtraSelection> result;
|
|
|
|
|
const bool isUnused = uses.size() == 1;
|
2009-06-26 09:48:40 +02:00
|
|
|
|
2009-07-09 12:14:00 +02:00
|
|
|
foreach (const SemanticInfo::Use &use, uses) {
|
2013-02-13 15:01:48 +01:00
|
|
|
if (use.isInvalid())
|
|
|
|
|
continue;
|
2009-06-25 16:11:28 +02:00
|
|
|
|
2013-02-13 15:01:48 +01:00
|
|
|
QTextEdit::ExtraSelection sel;
|
2009-09-23 18:06:59 +02:00
|
|
|
if (isUnused)
|
2014-05-20 12:24:32 -04:00
|
|
|
sel.format = textCharFormat(TextEditor::C_OCCURRENCES_UNUSED);
|
2009-09-23 18:06:59 +02:00
|
|
|
else
|
2014-05-20 12:24:32 -04:00
|
|
|
sel.format = textCharFormat(TextEditor::C_OCCURRENCES);
|
2009-06-26 09:48:40 +02:00
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
const int position = document()->findBlockByNumber(use.line - 1).position() + use.column - 1;
|
|
|
|
|
const int anchor = position + use.length;
|
2009-06-25 16:11:28 +02:00
|
|
|
|
2009-09-24 10:05:33 +02:00
|
|
|
sel.cursor = QTextCursor(document());
|
2009-07-09 17:32:39 +02:00
|
|
|
sel.cursor.setPosition(anchor);
|
|
|
|
|
sel.cursor.setPosition(position, QTextCursor::KeepAnchor);
|
2009-06-25 16:11:28 +02:00
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
result.append(sel);
|
2009-06-25 16:11:28 +02:00
|
|
|
}
|
2014-05-20 12:24:32 -04:00
|
|
|
|
|
|
|
|
return result;
|
2009-06-25 16:11:28 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::updateUses()
|
2009-07-01 17:57:00 +02:00
|
|
|
{
|
2013-05-06 14:59:15 +02:00
|
|
|
// Block premature semantic info calculation when editor is created.
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_modelManager && d->m_modelManager->cppEditorSupport(editor())->initialized())
|
|
|
|
|
d->m_updateUsesTimer->start();
|
2009-07-01 17:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::updateUsesNow()
|
2009-07-01 17:57:00 +02:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
if (d->m_localRenaming.isActive())
|
2009-07-01 17:57:00 +02:00
|
|
|
return;
|
|
|
|
|
|
2009-07-09 12:14:00 +02:00
|
|
|
semanticRehighlight();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::highlightSymbolUsages(int from, int to)
|
2010-07-13 14:37:31 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (editorRevision() != d->m_highlightRevision)
|
2010-07-13 14:37:31 +02:00
|
|
|
return; // outdated
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
else if (!d->m_highlightWatcher || d->m_highlightWatcher->isCanceled())
|
2010-07-13 14:37:31 +02:00
|
|
|
return; // aborted
|
|
|
|
|
|
2014-08-01 23:31:56 +02:00
|
|
|
TextEditor::SyntaxHighlighter *highlighter = textDocument()->syntaxHighlighter();
|
2011-08-16 09:47:54 +02:00
|
|
|
QTC_ASSERT(highlighter, return);
|
2010-07-15 16:03:48 +02:00
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
TextEditor::SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
|
2014-05-20 09:24:48 -04:00
|
|
|
highlighter, d->m_highlightWatcher->future(), from, to, d->m_semanticHighlightFormatMap);
|
2010-07-13 14:37:31 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::finishHighlightSymbolUsages()
|
2010-07-05 18:52:52 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
QTC_ASSERT(d->m_highlightWatcher, return);
|
|
|
|
|
if (!d->m_highlightWatcher->isCanceled()
|
|
|
|
|
&& editorRevision() == d->m_highlightRevision
|
|
|
|
|
&& !d->m_lastSemanticInfo.doc.isNull()) {
|
2014-08-01 23:31:56 +02:00
|
|
|
TextEditor::SyntaxHighlighter *highlighter = textDocument()->syntaxHighlighter();
|
2013-12-11 15:19:54 +01:00
|
|
|
QTC_CHECK(highlighter);
|
|
|
|
|
if (highlighter)
|
|
|
|
|
TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(highlighter,
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_highlightWatcher->future());
|
2013-12-11 15:19:54 +01:00
|
|
|
}
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_highlightWatcher.reset();
|
2010-07-05 18:52:52 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::switchDeclarationDefinition(bool inNextSplit)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_modelManager)
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_lastSemanticInfo.doc)
|
2013-04-26 17:35:55 +02:00
|
|
|
return;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-04-26 17:35:55 +02:00
|
|
|
// Find function declaration or definition under cursor
|
|
|
|
|
Function *functionDefinitionSymbol = 0;
|
|
|
|
|
Symbol *functionDeclarationSymbol = 0;
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
ASTPath astPathFinder(d->m_lastSemanticInfo.doc);
|
2013-04-26 17:35:55 +02:00
|
|
|
const QList<AST *> astPath = astPathFinder(textCursor());
|
|
|
|
|
|
|
|
|
|
for (int i = 0, size = astPath.size(); i < size; ++i) {
|
|
|
|
|
AST *ast = astPath.at(i);
|
|
|
|
|
if (FunctionDefinitionAST *functionDefinitionAST = ast->asFunctionDefinition()) {
|
|
|
|
|
if ((functionDefinitionSymbol = functionDefinitionAST->symbol))
|
|
|
|
|
break; // Function definition found!
|
|
|
|
|
} else if (SimpleDeclarationAST *simpleDeclaration = ast->asSimpleDeclaration()) {
|
|
|
|
|
if (List<Symbol *> *symbols = simpleDeclaration->symbols) {
|
|
|
|
|
if (Symbol *symbol = symbols->value) {
|
|
|
|
|
if (symbol->isDeclaration() && symbol->type()->isFunctionType()) {
|
|
|
|
|
functionDeclarationSymbol = symbol;
|
|
|
|
|
break; // Function declaration found!
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-04-26 17:35:55 +02:00
|
|
|
// Link to function definition/declaration
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget::Link symbolLink;
|
2013-04-26 17:35:55 +02:00
|
|
|
if (functionDeclarationSymbol) {
|
|
|
|
|
symbolLink = linkToSymbol(symbolFinder()
|
2014-05-20 09:24:48 -04:00
|
|
|
->findMatchingDefinition(functionDeclarationSymbol, d->m_modelManager->snapshot()));
|
2013-04-26 17:35:55 +02:00
|
|
|
} else if (functionDefinitionSymbol) {
|
2014-05-20 09:24:48 -04:00
|
|
|
const Snapshot snapshot = d->m_modelManager->snapshot();
|
|
|
|
|
LookupContext context(d->m_lastSemanticInfo.doc, snapshot);
|
2013-04-26 17:35:55 +02:00
|
|
|
ClassOrNamespace *binding = context.lookupType(functionDefinitionSymbol);
|
|
|
|
|
const QList<LookupItem> declarations = context.lookup(functionDefinitionSymbol->name(),
|
|
|
|
|
functionDefinitionSymbol->enclosingScope());
|
|
|
|
|
|
|
|
|
|
QList<Symbol *> best;
|
|
|
|
|
foreach (const LookupItem &r, declarations) {
|
|
|
|
|
if (Symbol *decl = r.declaration()) {
|
|
|
|
|
if (Function *funTy = decl->type()->asFunctionType()) {
|
2014-05-15 12:00:13 -04:00
|
|
|
if (funTy->match(functionDefinitionSymbol)) {
|
2013-04-26 17:35:55 +02:00
|
|
|
if (decl != functionDefinitionSymbol && binding == r.binding())
|
|
|
|
|
best.prepend(decl);
|
|
|
|
|
else
|
|
|
|
|
best.append(decl);
|
2010-12-10 10:47:57 +01:00
|
|
|
}
|
|
|
|
|
}
|
2010-05-12 16:36:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-05 14:14:33 +01:00
|
|
|
|
2013-04-26 17:35:55 +02:00
|
|
|
if (best.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
symbolLink = linkToSymbol(best.first());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2013-04-26 17:35:55 +02:00
|
|
|
|
|
|
|
|
// Open Editor at link position
|
|
|
|
|
if (symbolLink.hasValidTarget())
|
|
|
|
|
openCppEditorAt(symbolLink, inNextSplit != alwaysOpenLinksInNextSplit());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
QString CppEditorWidget::identifierUnderCursor(QTextCursor *macroCursor)
|
2010-12-10 11:50:36 +01:00
|
|
|
{
|
|
|
|
|
macroCursor->movePosition(QTextCursor::StartOfWord);
|
|
|
|
|
macroCursor->movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
|
|
|
|
return macroCursor->selectedText();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget::Link CppEditorWidget::findLinkAt(const QTextCursor &cursor, bool resolveTarget,
|
2013-07-04 20:11:10 +02:00
|
|
|
bool inNextSplit)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_modelManager)
|
2013-09-20 10:32:41 +02:00
|
|
|
return Link();
|
2009-01-28 23:46:43 +01:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
return d->m_followSymbolUnderCursor->findLink(cursor, resolveTarget,
|
|
|
|
|
d->m_modelManager->snapshot(),
|
|
|
|
|
d->m_lastSemanticInfo.doc,
|
|
|
|
|
symbolFinder(),
|
|
|
|
|
inNextSplit);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
unsigned CppEditorWidget::editorRevision() const
|
2009-12-15 15:52:55 +01:00
|
|
|
{
|
|
|
|
|
return document()->revision();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
bool CppEditorWidget::isOutdated() const
|
2009-11-18 15:06:26 +01:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_lastSemanticInfo.revision != editorRevision())
|
2009-11-18 15:06:26 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
SemanticInfo CppEditorWidget::semanticInfo() const
|
2009-07-10 12:09:26 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
return d->m_lastSemanticInfo;
|
2009-07-10 12:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
bool CppEditorWidget::event(QEvent *e)
|
2009-07-01 17:57:00 +02:00
|
|
|
{
|
|
|
|
|
switch (e->type()) {
|
|
|
|
|
case QEvent::ShortcutOverride:
|
2011-09-05 10:33:17 +02:00
|
|
|
// handle escape manually if a rename is active
|
2014-05-20 12:24:32 -04:00
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && d->m_localRenaming.isActive()) {
|
2009-07-01 17:57:00 +02:00
|
|
|
e->accept();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
return BaseTextEditorWidget::event(e);
|
2009-07-01 17:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::performQuickFix(int index)
|
2009-11-23 16:55:04 +01:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
TextEditor::QuickFixOperation::Ptr op = d->m_quickFixes.at(index);
|
2010-06-03 14:45:55 +02:00
|
|
|
op->perform();
|
2009-11-23 16:55:04 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-07-09 12:14:00 +02:00
|
|
|
// ### enable
|
|
|
|
|
// updateSemanticInfo(m_semanticHighlighter->semanticInfo(currentSource()));
|
|
|
|
|
|
2013-02-18 15:12:09 +01:00
|
|
|
QPointer<QMenu> menu(new QMenu(this));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-05-24 13:49:06 +02:00
|
|
|
Core::ActionContainer *mcontext = Core::ActionManager::actionContainer(Constants::M_CONTEXT);
|
2008-12-02 12:01:29 +01:00
|
|
|
QMenu *contextMenu = mcontext->menu();
|
|
|
|
|
|
2010-12-10 15:51:25 +01:00
|
|
|
QMenu *quickFixMenu = new QMenu(tr("&Refactor"), menu);
|
2013-04-30 14:50:47 +02:00
|
|
|
quickFixMenu->addAction(Core::ActionManager::command(
|
|
|
|
|
Constants::RENAME_SYMBOL_UNDER_CURSOR)->action());
|
2009-11-23 16:55:04 +01:00
|
|
|
|
|
|
|
|
QSignalMapper mapper;
|
|
|
|
|
connect(&mapper, SIGNAL(mapped(int)), this, SLOT(performQuickFix(int)));
|
2013-07-24 11:52:01 +02:00
|
|
|
if (!isOutdated()) {
|
2011-04-15 16:19:23 +02:00
|
|
|
TextEditor::IAssistInterface *interface =
|
|
|
|
|
createAssistInterface(TextEditor::QuickFix, TextEditor::ExplicitlyInvoked);
|
|
|
|
|
if (interface) {
|
|
|
|
|
QScopedPointer<TextEditor::IAssistProcessor> processor(
|
2013-03-19 13:44:02 +01:00
|
|
|
CppEditorPlugin::instance()->quickFixProvider()->createProcessor());
|
2011-04-15 16:19:23 +02:00
|
|
|
QScopedPointer<TextEditor::IAssistProposal> proposal(processor->perform(interface));
|
|
|
|
|
if (!proposal.isNull()) {
|
|
|
|
|
TextEditor::BasicProposalItemListModel *model =
|
|
|
|
|
static_cast<TextEditor::BasicProposalItemListModel *>(proposal->model());
|
|
|
|
|
for (int index = 0; index < model->size(); ++index) {
|
|
|
|
|
TextEditor::BasicProposalItem *item =
|
|
|
|
|
static_cast<TextEditor::BasicProposalItem *>(model->proposalItem(index));
|
|
|
|
|
TextEditor::QuickFixOperation::Ptr op =
|
|
|
|
|
item->data().value<TextEditor::QuickFixOperation::Ptr>();
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_quickFixes.append(op);
|
2011-04-15 16:19:23 +02:00
|
|
|
QAction *action = quickFixMenu->addAction(op->description());
|
|
|
|
|
mapper.setMapping(action, index);
|
|
|
|
|
connect(action, SIGNAL(triggered()), &mapper, SLOT(map()));
|
|
|
|
|
}
|
|
|
|
|
delete model;
|
2009-11-23 16:55:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-15 14:33:44 +02:00
|
|
|
foreach (QAction *action, contextMenu->actions()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
menu->addAction(action);
|
2012-01-11 14:26:09 +01:00
|
|
|
if (action->objectName() == QLatin1String(Constants::M_REFACTORING_MENU_INSERTION_POINT))
|
2010-09-15 14:33:44 +02:00
|
|
|
menu->addMenu(quickFixMenu);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-11-02 14:02:18 +01:00
|
|
|
appendStandardContextMenuActions(menu);
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
menu->exec(e->globalPos());
|
2013-02-18 15:12:09 +01:00
|
|
|
if (!menu)
|
|
|
|
|
return;
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_quickFixes.clear();
|
2008-12-02 12:01:29 +01:00
|
|
|
delete menu;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::keyPressEvent(QKeyEvent *e)
|
2009-07-01 17:57:00 +02:00
|
|
|
{
|
2014-05-20 12:24:32 -04:00
|
|
|
if (d->m_localRenaming.handleKeyPressEvent(e))
|
2009-07-01 18:41:04 +02:00
|
|
|
return;
|
|
|
|
|
|
2014-05-26 12:24:23 -04:00
|
|
|
if (d->m_cppDocumentationCommentHelper.handleKeyPressEvent(e))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
TextEditor::BaseTextEditorWidget::keyPressEvent(e);
|
2009-07-01 17:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-07 14:41:57 +01:00
|
|
|
Core::IEditor *CPPEditor::duplicate()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget *newEditor = new CppEditorWidget(
|
|
|
|
|
qobject_cast<CppEditorWidget *>(editorWidget()));
|
2013-03-19 13:44:02 +01:00
|
|
|
CppEditorPlugin::instance()->initializeEditor(newEditor);
|
2011-02-21 16:02:26 +01:00
|
|
|
return newEditor->editor();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-05-10 20:43:03 +02:00
|
|
|
bool CPPEditor::open(QString *errorString, const QString &fileName, const QString &realFileName)
|
2009-10-13 17:17:08 +02:00
|
|
|
{
|
2013-05-24 08:56:20 +02:00
|
|
|
if (!TextEditor::BaseTextEditor::open(errorString, fileName, realFileName))
|
|
|
|
|
return false;
|
2014-08-01 23:31:56 +02:00
|
|
|
textDocument()->setMimeType(Core::MimeDatabase::findByFile(QFileInfo(fileName)).type());
|
2013-05-24 08:56:20 +02:00
|
|
|
return true;
|
2009-10-13 17:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::applyFontSettings()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2014-08-01 23:31:56 +02:00
|
|
|
const TextEditor::FontSettings &fs = textDocument()->fontSettings();
|
2013-04-16 16:48:10 +02:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::TypeUse] =
|
2012-04-26 14:17:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_TYPE);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::LocalUse] =
|
2012-04-26 14:17:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_LOCAL);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::FieldUse] =
|
2012-04-26 14:17:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_FIELD);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::EnumerationUse] =
|
2012-08-27 10:42:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_ENUMERATION);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::VirtualMethodUse] =
|
2012-04-26 14:17:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_VIRTUAL_METHOD);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::LabelUse] =
|
2012-04-26 14:17:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_LABEL);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::MacroUse] =
|
2012-04-26 14:17:42 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_PREPROCESSOR);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::FunctionUse] =
|
2010-10-18 17:45:49 +02:00
|
|
|
fs.toTextCharFormat(TextEditor::C_FUNCTION);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::PseudoKeywordUse] =
|
2012-06-25 23:49:17 +04:00
|
|
|
fs.toTextCharFormat(TextEditor::C_KEYWORD);
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_semanticHighlightFormatMap[CppHighlightingSupport::StringUse] =
|
2013-12-10 14:37:32 +01:00
|
|
|
fs.toTextCharFormat(TextEditor::C_STRING);
|
2010-07-15 17:01:37 +02:00
|
|
|
|
2014-01-22 15:36:06 +01:00
|
|
|
// this also makes the document apply font settings
|
|
|
|
|
TextEditor::BaseTextEditorWidget::applyFontSettings();
|
2014-01-30 12:48:30 +01:00
|
|
|
semanticRehighlight(true);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::slotCodeStyleSettingsChanged(const QVariant &)
|
2011-02-03 15:48:14 +01:00
|
|
|
{
|
|
|
|
|
CppTools::QtStyleCodeFormatter formatter;
|
|
|
|
|
formatter.invalidateCache(document());
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
CppEditorWidget::Link CppEditorWidget::linkToSymbol(CPlusPlus::Symbol *symbol)
|
2008-12-11 10:28:39 +01:00
|
|
|
{
|
2010-08-13 16:38:45 +02:00
|
|
|
if (!symbol)
|
|
|
|
|
return Link();
|
|
|
|
|
|
2013-02-06 14:23:18 +01:00
|
|
|
const QString filename = QString::fromUtf8(symbol->fileName(),
|
2009-03-23 17:20:19 +01:00
|
|
|
symbol->fileNameLength());
|
2013-02-06 14:23:18 +01:00
|
|
|
|
2009-03-23 17:20:19 +01:00
|
|
|
unsigned line = symbol->line();
|
|
|
|
|
unsigned column = symbol->column();
|
2009-03-04 15:47:26 +01:00
|
|
|
|
2009-03-03 13:46:37 +01:00
|
|
|
if (column)
|
|
|
|
|
--column;
|
2009-03-04 15:47:26 +01:00
|
|
|
|
2009-03-23 17:20:19 +01:00
|
|
|
if (symbol->isGenerated())
|
2009-03-05 16:29:18 +01:00
|
|
|
column = 0;
|
2009-03-03 13:46:37 +01:00
|
|
|
|
2013-02-06 14:23:18 +01:00
|
|
|
return Link(filename, line, column);
|
2009-03-23 17:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
bool CppEditorWidget::openCppEditorAt(const Link &link, bool inNextSplit)
|
2009-03-23 17:20:19 +01:00
|
|
|
{
|
2013-02-05 14:14:33 +01:00
|
|
|
if (!link.hasValidTarget())
|
2009-03-23 17:20:19 +01:00
|
|
|
return false;
|
|
|
|
|
|
2013-05-31 12:52:53 +02:00
|
|
|
Core::EditorManager::OpenEditorFlags flags;
|
2013-05-31 15:16:06 +02:00
|
|
|
if (inNextSplit)
|
|
|
|
|
flags |= Core::EditorManager::OpenInOtherSplit;
|
2013-05-30 17:26:51 +02:00
|
|
|
return Core::EditorManager::openEditorAt(link.targetFileName,
|
|
|
|
|
link.targetLine,
|
|
|
|
|
link.targetColumn,
|
2013-05-31 15:16:06 +02:00
|
|
|
Constants::CPPEDITOR_ID,
|
|
|
|
|
flags);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-07-09 12:14:00 +02:00
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::semanticRehighlight(bool force)
|
2009-07-09 12:14:00 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_modelManager) {
|
2014-03-11 14:00:57 -03:00
|
|
|
const CppEditorSupport::ForceReason forceReason = force
|
|
|
|
|
? CppEditorSupport::ForceDueEditorRequest
|
|
|
|
|
: CppEditorSupport::NoForce;
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_modelManager->cppEditorSupport(editor())->recalculateSemanticInfoDetached(forceReason);
|
2014-03-11 14:00:57 -03:00
|
|
|
}
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::highlighterStarted(QFuture<TextEditor::HighlightingResult> *highlighter,
|
2013-04-17 10:58:20 +02:00
|
|
|
unsigned revision)
|
|
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_highlightRevision = revision;
|
2013-12-11 15:19:54 +01:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_highlightWatcher.reset(new QFutureWatcher<TextEditor::HighlightingResult>);
|
|
|
|
|
connect(d->m_highlightWatcher.data(), SIGNAL(resultsReadyAt(int,int)),
|
2013-12-11 15:19:54 +01:00
|
|
|
SLOT(highlightSymbolUsages(int,int)));
|
2014-05-20 09:24:48 -04:00
|
|
|
connect(d->m_highlightWatcher.data(), SIGNAL(finished()),
|
2013-12-11 15:19:54 +01:00
|
|
|
SLOT(finishHighlightSymbolUsages()));
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_highlightWatcher->setFuture(QFuture<TextEditor::HighlightingResult>(*highlighter));
|
2009-07-09 12:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::updateSemanticInfo(const SemanticInfo &semanticInfo)
|
2009-07-09 12:14:00 +02:00
|
|
|
{
|
2009-12-15 15:52:55 +01:00
|
|
|
if (semanticInfo.revision != editorRevision()) {
|
2009-07-10 12:09:26 +02:00
|
|
|
// got outdated semantic info
|
|
|
|
|
semanticRehighlight();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_lastSemanticInfo = semanticInfo; // update the semantic info
|
2009-07-10 12:09:26 +02:00
|
|
|
|
2009-07-09 12:14:00 +02:00
|
|
|
int line = 0, column = 0;
|
|
|
|
|
convertPosition(position(), &line, &column);
|
|
|
|
|
|
2009-11-30 15:21:16 +01:00
|
|
|
QList<QTextEdit::ExtraSelection> unusedSelections;
|
2014-05-20 12:24:32 -04:00
|
|
|
QList<QTextEdit::ExtraSelection> selections;
|
2009-07-09 12:14:00 +02:00
|
|
|
|
2011-11-14 13:14:39 +01:00
|
|
|
// We can use the semanticInfo's snapshot (and avoid locking), but not its
|
|
|
|
|
// document, since it doesn't contain expanded macros.
|
2014-08-01 23:31:56 +02:00
|
|
|
LookupContext context(semanticInfo.snapshot.document(textDocument()->filePath()),
|
2011-11-14 13:14:39 +01:00
|
|
|
semanticInfo.snapshot);
|
|
|
|
|
|
2009-07-09 12:14:00 +02:00
|
|
|
SemanticInfo::LocalUseIterator it(semanticInfo.localUses);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
const QList<SemanticInfo::Use> &uses = it.value();
|
|
|
|
|
|
|
|
|
|
bool good = false;
|
|
|
|
|
foreach (const SemanticInfo::Use &use, uses) {
|
|
|
|
|
unsigned l = line;
|
|
|
|
|
unsigned c = column + 1; // convertCursorPosition() returns a 0-based column number.
|
|
|
|
|
if (l == use.line && c >= use.column && c <= (use.column + use.length)) {
|
|
|
|
|
good = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 13:14:39 +01:00
|
|
|
if (uses.size() == 1) {
|
2014-05-20 12:24:32 -04:00
|
|
|
if (!CppTools::isOwnershipRAIIType(it.key(), context))
|
|
|
|
|
unusedSelections << createSelectionsFromUses(uses); // unused declaration
|
|
|
|
|
} else if (good && selections.isEmpty()) {
|
|
|
|
|
selections << createSelectionsFromUses(uses);
|
2011-11-14 13:14:39 +01:00
|
|
|
}
|
2009-07-09 12:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
2009-11-30 15:21:16 +01:00
|
|
|
setExtraSelections(UnusedSymbolSelection, unusedSelections);
|
2010-05-25 15:55:12 +02:00
|
|
|
|
2014-05-20 12:24:32 -04:00
|
|
|
if (!selections.isEmpty()) {
|
|
|
|
|
setExtraSelections(CodeSemanticsSelection, selections);
|
|
|
|
|
d->m_localRenaming.updateLocalUseSelections(selections);
|
|
|
|
|
} else {
|
2010-07-19 12:17:08 +02:00
|
|
|
markSymbols(textCursor(), semanticInfo);
|
2014-05-20 12:24:32 -04:00
|
|
|
}
|
2010-05-21 15:45:18 +02:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_lastSemanticInfo.forced = false; // clear the forced flag
|
2011-08-10 09:50:04 +02:00
|
|
|
|
|
|
|
|
// schedule a check for a decl/def link
|
|
|
|
|
updateFunctionDeclDefLink();
|
2009-07-09 12:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
TextEditor::IAssistInterface *CppEditorWidget::createAssistInterface(
|
2011-04-15 16:19:23 +02:00
|
|
|
TextEditor::AssistKind kind,
|
|
|
|
|
TextEditor::AssistReason reason) const
|
|
|
|
|
{
|
|
|
|
|
if (kind == TextEditor::Completion) {
|
2013-08-30 13:13:44 +02:00
|
|
|
CppEditorSupport *ces = CppModelManagerInterface::instance()->cppEditorSupport(editor());
|
|
|
|
|
CppCompletionAssistProvider *cap = ces->completionAssistProvider();
|
2013-08-19 16:05:29 +02:00
|
|
|
if (cap) {
|
2013-08-30 13:13:44 +02:00
|
|
|
return cap->createAssistInterface(
|
2013-08-19 16:05:29 +02:00
|
|
|
ProjectExplorer::ProjectExplorerPlugin::currentProject(),
|
2014-05-14 11:34:45 -04:00
|
|
|
editor(), document(), cppEditorDocument()->isObjCEnabled(), position(),
|
|
|
|
|
reason);
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
} else if (kind == TextEditor::QuickFix) {
|
2011-11-14 13:22:44 +01:00
|
|
|
if (!semanticInfo().doc || isOutdated())
|
2011-04-15 16:19:23 +02:00
|
|
|
return 0;
|
2014-07-30 16:30:31 +02:00
|
|
|
return new CppQuickFixAssistInterface(const_cast<CppEditorWidget *>(this), reason);
|
2013-07-04 20:11:10 +02:00
|
|
|
} else {
|
|
|
|
|
return BaseTextEditorWidget::createAssistInterface(kind, reason);
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
QSharedPointer<FunctionDeclDefLink> CppEditorWidget::declDefLink() const
|
2011-09-05 10:33:17 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
return d->m_declDefLink;
|
2011-09-05 10:33:17 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onRefactorMarkerClicked(const TextEditor::RefactorMarker &marker)
|
2011-08-10 09:50:04 +02:00
|
|
|
{
|
|
|
|
|
if (marker.data.canConvert<FunctionDeclDefLink::Marker>())
|
|
|
|
|
applyDeclDefLinkChanges(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::updateFunctionDeclDefLink()
|
2011-08-10 09:50:04 +02:00
|
|
|
{
|
|
|
|
|
const int pos = textCursor().selectionStart();
|
|
|
|
|
|
2011-08-25 13:22:21 +02:00
|
|
|
// if there's already a link, abort it if the cursor is outside or the name changed
|
2012-11-23 13:25:45 +01:00
|
|
|
// (adding a prefix is an exception since the user might type a return type)
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_declDefLink
|
|
|
|
|
&& (pos < d->m_declDefLink->linkSelection.selectionStart()
|
|
|
|
|
|| pos > d->m_declDefLink->linkSelection.selectionEnd()
|
|
|
|
|
|| !d->m_declDefLink->nameSelection.selectedText().trimmed()
|
|
|
|
|
.endsWith(d->m_declDefLink->nameInitial))) {
|
2011-08-10 09:50:04 +02:00
|
|
|
abortDeclDefLink();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// don't start a new scan if there's one active and the cursor is already in the scanned area
|
2014-05-20 09:24:48 -04:00
|
|
|
const QTextCursor scannedSelection = d->m_declDefLinkFinder->scannedSelection();
|
2011-08-10 09:50:04 +02:00
|
|
|
if (!scannedSelection.isNull()
|
|
|
|
|
&& scannedSelection.selectionStart() <= pos
|
|
|
|
|
&& scannedSelection.selectionEnd() >= pos) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_updateFunctionDeclDefLinkTimer->start();
|
2011-08-10 09:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::updateFunctionDeclDefLinkNow()
|
2011-08-10 09:50:04 +02:00
|
|
|
{
|
2012-05-08 09:43:14 +02:00
|
|
|
if (Core::EditorManager::currentEditor() != editor())
|
2011-08-10 09:50:04 +02:00
|
|
|
return;
|
2014-05-20 09:24:48 -04:00
|
|
|
if (d->m_declDefLink) {
|
2011-08-10 09:50:04 +02:00
|
|
|
// update the change marker
|
2014-05-20 09:24:48 -04:00
|
|
|
const Utils::ChangeSet changes = d->m_declDefLink->changes(d->m_lastSemanticInfo.snapshot);
|
2011-08-10 09:50:04 +02:00
|
|
|
if (changes.isEmpty())
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_declDefLink->hideMarker(this);
|
2011-08-10 09:50:04 +02:00
|
|
|
else
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_declDefLink->showMarker(this);
|
2011-08-10 09:50:04 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_lastSemanticInfo.doc || isOutdated())
|
2011-08-10 09:50:04 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Snapshot snapshot = CppModelManagerInterface::instance()->snapshot();
|
2014-05-20 09:24:48 -04:00
|
|
|
snapshot.insert(d->m_lastSemanticInfo.doc);
|
2011-08-10 09:50:04 +02:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_declDefLinkFinder->startFindLinkAt(textCursor(), d->m_lastSemanticInfo.doc, snapshot);
|
2011-08-10 09:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefLink> link)
|
2011-08-10 09:50:04 +02:00
|
|
|
{
|
|
|
|
|
abortDeclDefLink();
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_declDefLink = link;
|
|
|
|
|
Core::IDocument *targetDocument
|
|
|
|
|
= Core::DocumentModel::documentForFilePath( d->m_declDefLink->targetFile->fileName());
|
2014-08-01 23:31:56 +02:00
|
|
|
if (textDocument() != targetDocument) {
|
2014-07-25 14:54:10 +02:00
|
|
|
if (auto textDocument = qobject_cast<TextEditor::BaseTextDocument *>(targetDocument))
|
|
|
|
|
connect(textDocument, SIGNAL(contentsChanged()),
|
2013-07-18 09:40:50 +02:00
|
|
|
this, SLOT(abortDeclDefLink()));
|
2011-08-17 12:21:04 +02:00
|
|
|
}
|
2013-07-18 09:40:50 +02:00
|
|
|
|
2011-08-10 09:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onFilePathChanged()
|
2014-01-10 16:18:17 +01:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
QTC_ASSERT(d->m_modelManager, return);
|
2014-01-10 16:18:17 +01:00
|
|
|
QByteArray additionalDirectives;
|
2014-08-01 23:31:56 +02:00
|
|
|
const QString &filePath = textDocument()->filePath();
|
2014-01-10 16:18:17 +01:00
|
|
|
if (!filePath.isEmpty()) {
|
|
|
|
|
const QString &projectFile = ProjectExplorer::SessionManager::value(
|
|
|
|
|
QLatin1String(Constants::CPP_PREPROCESSOR_PROJECT_PREFIX) + filePath).toString();
|
|
|
|
|
additionalDirectives = ProjectExplorer::SessionManager::value(
|
|
|
|
|
projectFile + QLatin1Char(',') + filePath).toString().toUtf8();
|
|
|
|
|
|
|
|
|
|
QSharedPointer<SnapshotUpdater> updater
|
2014-05-20 09:24:48 -04:00
|
|
|
= d->m_modelManager->cppEditorSupport(editor())->snapshotUpdater();
|
|
|
|
|
updater->setProjectPart(d->m_modelManager->projectPartForProjectFile(projectFile));
|
2014-01-10 16:18:17 +01:00
|
|
|
updater->setEditorDefines(additionalDirectives);
|
|
|
|
|
}
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_preprocessorButton->setProperty("highlightWidget", !additionalDirectives.trimmed().isEmpty());
|
|
|
|
|
d->m_preprocessorButton->update();
|
2014-01-10 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::applyDeclDefLinkChanges(bool jumpToMatch)
|
2011-08-10 09:50:04 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_declDefLink)
|
2011-08-10 09:50:04 +02:00
|
|
|
return;
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_declDefLink->apply(this, jumpToMatch);
|
2011-08-30 10:05:09 +02:00
|
|
|
abortDeclDefLink();
|
2011-08-10 09:50:04 +02:00
|
|
|
updateFunctionDeclDefLink();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
FollowSymbolUnderCursor *CppEditorWidget::followSymbolUnderCursorDelegate()
|
2013-07-04 20:11:10 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
return d->m_followSymbolUnderCursor.data();
|
2013-07-04 20:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::abortDeclDefLink()
|
2011-08-10 09:50:04 +02:00
|
|
|
{
|
2014-05-20 09:24:48 -04:00
|
|
|
if (!d->m_declDefLink)
|
2011-08-10 09:50:04 +02:00
|
|
|
return;
|
2011-08-17 12:21:04 +02:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
Core::IDocument *targetDocument
|
|
|
|
|
= Core::DocumentModel::documentForFilePath(d->m_declDefLink->targetFile->fileName());
|
2014-08-01 23:31:56 +02:00
|
|
|
if (textDocument() != targetDocument) {
|
2014-07-25 14:54:10 +02:00
|
|
|
if (auto textDocument = qobject_cast<TextEditor::BaseTextDocument *>(targetDocument))
|
|
|
|
|
disconnect(textDocument, SIGNAL(contentsChanged()),
|
2013-07-18 09:40:50 +02:00
|
|
|
this, SLOT(abortDeclDefLink()));
|
2011-08-17 12:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_declDefLink->hideMarker(this);
|
|
|
|
|
d->m_declDefLink.clear();
|
2011-08-10 09:50:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onLocalRenamingFinished()
|
2014-05-20 12:24:32 -04:00
|
|
|
{
|
|
|
|
|
semanticRehighlight(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::onLocalRenamingProcessKeyPressNormally(QKeyEvent *e)
|
2014-05-20 12:24:32 -04:00
|
|
|
{
|
|
|
|
|
BaseTextEditorWidget::keyPressEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
QTextCharFormat CppEditorWidget::textCharFormat(TextEditor::TextStyle category)
|
2014-05-20 12:24:32 -04:00
|
|
|
{
|
2014-08-01 23:31:56 +02:00
|
|
|
return textDocument()->fontSettings().toTextCharFormat(category);
|
2014-05-20 12:24:32 -04:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 16:30:31 +02:00
|
|
|
void CppEditorWidget::showPreProcessorWidget()
|
2013-08-20 07:46:19 +02:00
|
|
|
{
|
|
|
|
|
const QString &fileName = editor()->document()->filePath();
|
|
|
|
|
|
2013-10-17 11:19:26 +02:00
|
|
|
// Check if this editor belongs to a project
|
2014-05-20 09:24:48 -04:00
|
|
|
QList<ProjectPart::Ptr> projectParts = d->m_modelManager->projectPart(fileName);
|
2013-08-20 07:46:19 +02:00
|
|
|
if (projectParts.isEmpty())
|
2014-05-20 09:24:48 -04:00
|
|
|
projectParts = d->m_modelManager->projectPartFromDependencies(fileName);
|
2013-08-20 07:46:19 +02:00
|
|
|
if (projectParts.isEmpty())
|
2014-05-20 09:24:48 -04:00
|
|
|
projectParts << d->m_modelManager->fallbackProjectPart();
|
2013-08-20 07:46:19 +02:00
|
|
|
|
2014-08-01 23:31:56 +02:00
|
|
|
CppPreProcessorDialog preProcessorDialog(this, textDocument()->filePath(), projectParts);
|
2013-10-17 11:19:26 +02:00
|
|
|
if (preProcessorDialog.exec() == QDialog::Accepted) {
|
|
|
|
|
QSharedPointer<SnapshotUpdater> updater
|
2014-05-20 09:24:48 -04:00
|
|
|
= d->m_modelManager->cppEditorSupport(editor())->snapshotUpdater();
|
2013-10-17 14:52:26 +02:00
|
|
|
const QString &additionals = preProcessorDialog.additionalPreProcessorDirectives();
|
2013-12-02 15:23:13 +01:00
|
|
|
updater->setProjectPart(preProcessorDialog.projectPart());
|
2013-10-17 14:52:26 +02:00
|
|
|
updater->setEditorDefines(additionals.toUtf8());
|
2014-05-20 09:24:48 -04:00
|
|
|
updater->update(d->m_modelManager->workingCopy());
|
2013-10-17 14:52:26 +02:00
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
d->m_preprocessorButton->setProperty("highlightWidget", !additionals.trimmed().isEmpty());
|
|
|
|
|
d->m_preprocessorButton->update();
|
2013-10-17 11:19:26 +02:00
|
|
|
}
|
2013-08-20 07:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-20 09:24:48 -04:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppEditor
|