2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 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 12:01:29 +01:00
|
|
|
|
2013-09-30 13:36:01 +02:00
|
|
|
#include "cppcodemodelsettings.h"
|
2013-08-30 12:55:06 +02:00
|
|
|
#include "cppcompletionassistprovider.h"
|
2013-03-12 12:06:41 +01:00
|
|
|
#include "cpptoolseditorsupport.h"
|
2013-09-30 13:36:01 +02:00
|
|
|
#include "cpptoolsplugin.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cppmodelmanager.h"
|
2013-04-17 10:58:20 +02:00
|
|
|
#include "cpplocalsymbols.h"
|
|
|
|
|
|
2013-05-07 11:12:52 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
|
2013-06-18 16:26:40 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2013-04-17 10:58:20 +02:00
|
|
|
#include <utils/runextensions.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
#include <QList>
|
|
|
|
|
#include <QMutexLocker>
|
|
|
|
|
#include <QTextBlock>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTimer>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-07 15:09:08 +01:00
|
|
|
using namespace CppTools;
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace CppTools::Internal;
|
2009-06-09 13:52:27 +02:00
|
|
|
using namespace CPlusPlus;
|
2013-04-17 10:58:20 +02:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
class FunctionDefinitionUnderCursor: protected ASTVisitor
|
|
|
|
|
{
|
|
|
|
|
unsigned _line;
|
|
|
|
|
unsigned _column;
|
|
|
|
|
DeclarationAST *_functionDefinition;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
FunctionDefinitionUnderCursor(TranslationUnit *translationUnit)
|
|
|
|
|
: ASTVisitor(translationUnit),
|
|
|
|
|
_line(0), _column(0)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
DeclarationAST *operator()(AST *ast, unsigned line, unsigned column)
|
|
|
|
|
{
|
|
|
|
|
_functionDefinition = 0;
|
|
|
|
|
_line = line;
|
|
|
|
|
_column = column;
|
|
|
|
|
accept(ast);
|
|
|
|
|
return _functionDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual bool preVisit(AST *ast)
|
|
|
|
|
{
|
|
|
|
|
if (_functionDefinition)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-07-17 00:01:45 +03:00
|
|
|
if (FunctionDefinitionAST *def = ast->asFunctionDefinition())
|
2013-04-17 10:58:20 +02:00
|
|
|
return checkDeclaration(def);
|
|
|
|
|
|
2013-07-17 00:01:45 +03:00
|
|
|
if (ObjCMethodDeclarationAST *method = ast->asObjCMethodDeclaration()) {
|
2013-04-17 10:58:20 +02:00
|
|
|
if (method->function_body)
|
|
|
|
|
return checkDeclaration(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool checkDeclaration(DeclarationAST *ast)
|
|
|
|
|
{
|
|
|
|
|
unsigned startLine, startColumn;
|
|
|
|
|
unsigned endLine, endColumn;
|
|
|
|
|
getTokenStartPosition(ast->firstToken(), &startLine, &startColumn);
|
|
|
|
|
getTokenEndPosition(ast->lastToken() - 1, &endLine, &endColumn);
|
2009-06-09 13:52:27 +02:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
if (_line > startLine || (_line == startLine && _column >= startColumn)) {
|
|
|
|
|
if (_line < endLine || (_line == endLine && _column < endColumn)) {
|
|
|
|
|
_functionDefinition = ast;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
CppEditorSupport::CppEditorSupport(CppModelManager *modelManager, BaseTextEditor *textEditor)
|
|
|
|
|
: QObject(modelManager)
|
|
|
|
|
, m_modelManager(modelManager)
|
|
|
|
|
, m_textEditor(textEditor)
|
|
|
|
|
, m_updateDocumentInterval(UpdateDocumentDefaultInterval)
|
|
|
|
|
, m_revision(0)
|
2013-10-10 10:26:39 +02:00
|
|
|
, m_editorVisible(textEditor->widget()->isVisible())
|
2013-04-17 10:58:20 +02:00
|
|
|
, m_cachedContentsEditorRevision(-1)
|
2013-06-18 16:26:40 +02:00
|
|
|
, m_fileIsBeingReloaded(false)
|
2013-04-17 10:58:20 +02:00
|
|
|
, m_initialized(false)
|
|
|
|
|
, m_lastHighlightRevision(0)
|
2013-06-13 16:02:27 +02:00
|
|
|
, m_highlightingSupport(modelManager->highlightingSupport(textEditor))
|
2013-10-02 11:30:43 +02:00
|
|
|
, m_completionAssistProvider(m_modelManager->completionAssistProvider(textEditor))
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-04-17 10:58:20 +02:00
|
|
|
connect(m_modelManager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
|
|
|
|
|
this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
|
|
|
|
|
|
2013-06-13 16:02:27 +02:00
|
|
|
if (m_highlightingSupport && m_highlightingSupport->requiresSemanticInfo()) {
|
|
|
|
|
connect(this, SIGNAL(semanticInfoUpdated(CppTools::SemanticInfo)),
|
|
|
|
|
this, SLOT(startHighlighting()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
m_updateDocumentTimer = new QTimer(this);
|
|
|
|
|
m_updateDocumentTimer->setSingleShot(true);
|
|
|
|
|
m_updateDocumentTimer->setInterval(m_updateDocumentInterval);
|
|
|
|
|
connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
|
2010-03-24 15:53:39 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
m_updateEditorTimer = new QTimer(this);
|
|
|
|
|
m_updateEditorTimer->setInterval(UpdateEditorInterval);
|
|
|
|
|
m_updateEditorTimer->setSingleShot(true);
|
|
|
|
|
connect(m_updateEditorTimer, SIGNAL(timeout()),
|
|
|
|
|
this, SLOT(updateEditorNow()));
|
|
|
|
|
|
|
|
|
|
connect(m_textEditor, SIGNAL(contentsChanged()), this, SLOT(updateDocument()));
|
|
|
|
|
connect(this, SIGNAL(diagnosticsChanged()), this, SLOT(onDiagnosticsChanged()));
|
|
|
|
|
|
2013-05-02 11:42:33 +02:00
|
|
|
connect(m_textEditor->document(), SIGNAL(mimeTypeChanged()),
|
|
|
|
|
this, SLOT(onMimeTypeChanged()));
|
2013-06-13 16:02:27 +02:00
|
|
|
|
2013-06-18 16:26:40 +02:00
|
|
|
connect(m_textEditor->document(), SIGNAL(aboutToReload()),
|
|
|
|
|
this, SLOT(onAboutToReload()));
|
|
|
|
|
connect(m_textEditor->document(), SIGNAL(reloadFinished(bool)),
|
|
|
|
|
this, SLOT(onReloadFinished()));
|
|
|
|
|
|
2013-10-10 10:26:39 +02:00
|
|
|
connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor *)),
|
|
|
|
|
this, SLOT(onCurrentEditorChanged()));
|
|
|
|
|
m_editorGCTimer = new QTimer(this);
|
|
|
|
|
m_editorGCTimer->setSingleShot(true);
|
|
|
|
|
m_editorGCTimer->setInterval(EditorHiddenGCTimeout);
|
|
|
|
|
connect(m_editorGCTimer, SIGNAL(timeout()), this, SLOT(releaseResources()));
|
|
|
|
|
|
2013-06-13 16:02:27 +02:00
|
|
|
updateDocument();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppEditorSupport::~CppEditorSupport()
|
2013-05-06 09:59:56 +02:00
|
|
|
{
|
2013-11-18 14:43:48 +01:00
|
|
|
m_documentParser.cancel();
|
2013-05-06 09:59:56 +02:00
|
|
|
m_highlighter.cancel();
|
|
|
|
|
m_futureSemanticInfo.cancel();
|
|
|
|
|
|
2013-11-19 16:18:27 +01:00
|
|
|
m_documentParser.waitForFinished();
|
2013-05-06 09:59:56 +02:00
|
|
|
m_highlighter.waitForFinished();
|
|
|
|
|
m_futureSemanticInfo.waitForFinished();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
QString CppEditorSupport::fileName() const
|
|
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
return m_textEditor->document()->filePath();
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-08-19 15:47:51 +02:00
|
|
|
QByteArray CppEditorSupport::contents() const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-08-19 16:05:29 +02:00
|
|
|
QMutexLocker locker(&m_cachedContentsLock);
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
const int editorRev = editorRevision();
|
2013-06-18 16:26:40 +02:00
|
|
|
if (m_cachedContentsEditorRevision != editorRev && !m_fileIsBeingReloaded) {
|
2013-04-17 10:58:20 +02:00
|
|
|
m_cachedContentsEditorRevision = editorRev;
|
2013-12-09 16:14:55 +01:00
|
|
|
m_cachedContents = m_textEditor->textDocument()->plainText().toUtf8();
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
return m_cachedContents;
|
|
|
|
|
}
|
2009-06-09 13:52:27 +02:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
unsigned CppEditorSupport::editorRevision() const
|
|
|
|
|
{
|
|
|
|
|
return m_textEditor->editorWidget()->document()->revision();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
void CppEditorSupport::setExtraDiagnostics(const QString &key,
|
|
|
|
|
const QList<Document::DiagnosticMessage> &messages)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-04-17 10:58:20 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_diagnosticsMutex);
|
|
|
|
|
m_allDiagnostics.insert(key, messages);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
emit diagnosticsChanged();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-09 11:43:20 +02:00
|
|
|
void CppEditorSupport::setIfdefedOutBlocks(const QList<BlockRange> &ifdefedOutBlocks)
|
|
|
|
|
{
|
|
|
|
|
m_editorUpdates.ifdefedOutBlocks = ifdefedOutBlocks;
|
|
|
|
|
|
|
|
|
|
emit diagnosticsChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 14:59:15 +02:00
|
|
|
bool CppEditorSupport::initialized()
|
|
|
|
|
{
|
|
|
|
|
return m_initialized;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
SemanticInfo CppEditorSupport::recalculateSemanticInfo(bool emitSignalWhenFinished)
|
2009-12-15 15:52:55 +01:00
|
|
|
{
|
2013-04-17 10:58:20 +02:00
|
|
|
m_futureSemanticInfo.cancel();
|
2009-12-15 15:52:55 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
SemanticInfo::Source source = currentSource(false);
|
|
|
|
|
recalculateSemanticInfoNow(source, emitSignalWhenFinished);
|
|
|
|
|
return m_lastSemanticInfo;
|
2009-12-15 15:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
Document::Ptr CppEditorSupport::lastSemanticInfoDocument() const
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_lastSemanticInfoLock);
|
|
|
|
|
|
|
|
|
|
return m_lastSemanticInfo.doc;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
void CppEditorSupport::recalculateSemanticInfoDetached(bool force)
|
|
|
|
|
{
|
2013-05-06 14:59:15 +02:00
|
|
|
// Block premature calculation caused by CppEditorPlugin::currentEditorChanged
|
|
|
|
|
// when the editor is created.
|
|
|
|
|
if (!m_initialized)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
m_futureSemanticInfo.cancel();
|
|
|
|
|
SemanticInfo::Source source = currentSource(force);
|
|
|
|
|
m_futureSemanticInfo = QtConcurrent::run<CppEditorSupport, void>(
|
|
|
|
|
&CppEditorSupport::recalculateSemanticInfoDetached_helper, this, source);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-05-02 11:28:12 +02:00
|
|
|
if (force && m_highlightingSupport && !m_highlightingSupport->requiresSemanticInfo())
|
2013-04-17 10:58:20 +02:00
|
|
|
startHighlighting();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-08-30 12:55:06 +02:00
|
|
|
CppCompletionAssistProvider *CppEditorSupport::completionAssistProvider() const
|
|
|
|
|
{
|
2013-08-30 13:13:44 +02:00
|
|
|
return m_completionAssistProvider;
|
2013-08-30 12:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-19 16:05:29 +02:00
|
|
|
QSharedPointer<SnapshotUpdater> CppEditorSupport::snapshotUpdater()
|
|
|
|
|
{
|
|
|
|
|
QSharedPointer<SnapshotUpdater> updater = m_snapshotUpdater;
|
|
|
|
|
if (!updater) {
|
2013-10-01 13:31:30 +03:00
|
|
|
updater = QSharedPointer<SnapshotUpdater>(new SnapshotUpdater(fileName()));
|
2013-08-19 16:05:29 +02:00
|
|
|
m_snapshotUpdater = updater;
|
2013-09-30 13:36:01 +02:00
|
|
|
|
|
|
|
|
QSharedPointer<CppCodeModelSettings> cms = CppToolsPlugin::instance()->codeModelSettings();
|
|
|
|
|
updater->setUsePrecompiledHeaders(cms->pchUsage() != CppCodeModelSettings::PchUse_None);
|
2013-08-19 16:05:29 +02:00
|
|
|
}
|
|
|
|
|
return updater;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void CppEditorSupport::updateDocument()
|
2009-02-10 22:56:04 +01:00
|
|
|
{
|
2013-04-17 10:58:20 +02:00
|
|
|
m_revision = editorRevision();
|
2010-03-24 15:53:39 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
if (qobject_cast<BaseTextEditorWidget*>(m_textEditor->widget()) != 0)
|
|
|
|
|
m_updateEditorTimer->stop();
|
2009-02-10 22:56:04 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
m_updateDocumentTimer->start(m_updateDocumentInterval);
|
2009-02-10 22:56:04 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-11-18 14:43:48 +01:00
|
|
|
static void parse(QFutureInterface<void> &future, QSharedPointer<SnapshotUpdater> updater)
|
2013-08-19 16:05:29 +02:00
|
|
|
{
|
|
|
|
|
future.setProgressRange(0, 1);
|
2013-11-18 14:43:48 +01:00
|
|
|
if (future.isCanceled()) {
|
|
|
|
|
future.setProgressValue(1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-08-19 16:05:29 +02:00
|
|
|
|
|
|
|
|
CppModelManager *cmm = qobject_cast<CppModelManager *>(CppModelManager::instance());
|
|
|
|
|
updater->update(cmm->workingCopy());
|
2013-11-14 10:35:54 +01:00
|
|
|
cmm->finishedRefreshingSourceFiles(QStringList(updater->fileInEditor()));
|
2013-08-19 16:05:29 +02:00
|
|
|
|
|
|
|
|
future.setProgressValue(1);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void CppEditorSupport::updateDocumentNow()
|
|
|
|
|
{
|
2013-04-17 10:58:20 +02:00
|
|
|
if (m_documentParser.isRunning() || m_revision != editorRevision()) {
|
|
|
|
|
m_updateDocumentTimer->start(m_updateDocumentInterval);
|
|
|
|
|
} else {
|
|
|
|
|
m_updateDocumentTimer->stop();
|
|
|
|
|
|
2013-11-14 10:35:54 +01:00
|
|
|
if (m_fileIsBeingReloaded || fileName().isEmpty())
|
2013-06-18 16:26:40 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_highlightingSupport && !m_highlightingSupport->requiresSemanticInfo())
|
2013-04-17 10:58:20 +02:00
|
|
|
startHighlighting();
|
|
|
|
|
|
2013-11-18 14:43:48 +01:00
|
|
|
m_documentParser = QtConcurrent::run(&parse, snapshotUpdater());
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-13 14:31:50 +01:00
|
|
|
bool CppEditorSupport::isUpdatingDocument()
|
|
|
|
|
{
|
|
|
|
|
return m_updateDocumentTimer->isActive() || m_documentParser.isRunning();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
void CppEditorSupport::onDocumentUpdated(Document::Ptr doc)
|
|
|
|
|
{
|
|
|
|
|
if (doc.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (doc->fileName() != fileName())
|
|
|
|
|
return; // some other document got updated
|
|
|
|
|
|
|
|
|
|
if (doc->editorRevision() != editorRevision())
|
|
|
|
|
return; // outdated content, wait for a new document to be parsed
|
|
|
|
|
|
|
|
|
|
// Update the ifdeffed-out blocks:
|
2013-08-09 11:43:20 +02:00
|
|
|
if (m_highlightingSupport && !m_highlightingSupport->hightlighterHandlesIfdefedOutBlocks()) {
|
|
|
|
|
QList<Document::Block> skippedBlocks = doc->skippedBlocks();
|
|
|
|
|
QList<BlockRange> ifdefedOutBlocks;
|
|
|
|
|
ifdefedOutBlocks.reserve(skippedBlocks.size());
|
|
|
|
|
foreach (const Document::Block &block, skippedBlocks)
|
|
|
|
|
ifdefedOutBlocks.append(BlockRange(block.begin(), block.end()));
|
|
|
|
|
setIfdefedOutBlocks(ifdefedOutBlocks);
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_highlightingSupport && !m_highlightingSupport->hightlighterHandlesDiagnostics()) {
|
|
|
|
|
// Update the parser errors/warnings:
|
|
|
|
|
static const QString key = QLatin1String("CppTools.ParserDiagnostics");
|
|
|
|
|
setExtraDiagnostics(key, doc->diagnosticMessages());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update semantic info in a future
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!m_initialized ||
|
2013-04-17 10:58:20 +02:00
|
|
|
(m_textEditor->widget()->isVisible()
|
|
|
|
|
&& (m_lastSemanticInfo.doc.isNull()
|
|
|
|
|
|| m_lastSemanticInfo.doc->translationUnit()->ast() == 0
|
|
|
|
|
|| m_lastSemanticInfo.doc->fileName() != fileName()))) {
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
recalculateSemanticInfoDetached(/* force = */ true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// notify the editor that the document is updated
|
|
|
|
|
emit documentUpdated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorSupport::startHighlighting()
|
|
|
|
|
{
|
|
|
|
|
if (!m_highlightingSupport)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-05-07 11:12:52 +02:00
|
|
|
// Start highlighting only if the editor is or would be visible
|
|
|
|
|
// (in case another mode is active) in the edit mode.
|
2013-08-29 15:46:04 +02:00
|
|
|
if (!Core::EditorManager::visibleEditors().contains(m_textEditor))
|
2013-04-17 10:58:20 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (m_highlightingSupport->requiresSemanticInfo()) {
|
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
Document::Ptr doc;
|
|
|
|
|
unsigned revision;
|
|
|
|
|
bool forced;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_lastSemanticInfoLock);
|
|
|
|
|
snapshot = m_lastSemanticInfo.snapshot;
|
|
|
|
|
doc = m_lastSemanticInfo.doc;
|
|
|
|
|
revision = m_lastSemanticInfo.revision;
|
|
|
|
|
forced = m_lastSemanticInfo.forced;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.isNull())
|
|
|
|
|
return;
|
|
|
|
|
if (!forced && m_lastHighlightRevision == revision)
|
|
|
|
|
return;
|
|
|
|
|
m_highlighter.cancel();
|
|
|
|
|
|
|
|
|
|
m_highlighter = m_highlightingSupport->highlightingFuture(doc, snapshot);
|
|
|
|
|
m_lastHighlightRevision = revision;
|
2013-04-28 11:20:02 +03:00
|
|
|
emit highlighterStarted(&m_highlighter, m_lastHighlightRevision);
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
2013-11-27 14:49:48 +01:00
|
|
|
const unsigned revision = currentSource(false).revision;
|
|
|
|
|
if (m_lastHighlightRevision == revision)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_lastHighlightRevision = revision;
|
2013-04-17 10:58:20 +02:00
|
|
|
static const Document::Ptr dummyDoc;
|
|
|
|
|
static const Snapshot dummySnapshot;
|
|
|
|
|
m_highlighter = m_highlightingSupport->highlightingFuture(dummyDoc, dummySnapshot);
|
2013-04-28 11:20:02 +03:00
|
|
|
emit highlighterStarted(&m_highlighter, m_lastHighlightRevision);
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-07 13:34:40 +02:00
|
|
|
/// \brief This slot puts the new diagnostics into the editorUpdates. This function has to be called
|
2013-04-17 10:58:20 +02:00
|
|
|
/// on the UI thread.
|
|
|
|
|
void CppEditorSupport::onDiagnosticsChanged()
|
|
|
|
|
{
|
|
|
|
|
QList<Document::DiagnosticMessage> allDiagnostics;
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_diagnosticsMutex);
|
2013-12-16 15:54:02 +01:00
|
|
|
foreach (const QList<Document::DiagnosticMessage> &msgs, m_allDiagnostics)
|
2013-04-17 10:58:20 +02:00
|
|
|
allDiagnostics.append(msgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_textEditor)
|
|
|
|
|
return;
|
2009-02-10 22:56:04 +01:00
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
// set up the format for the errors
|
|
|
|
|
QTextCharFormat errorFormat;
|
|
|
|
|
errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
|
|
|
|
errorFormat.setUnderlineColor(Qt::red);
|
|
|
|
|
|
|
|
|
|
// set up the format for the warnings.
|
|
|
|
|
QTextCharFormat warningFormat;
|
|
|
|
|
warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
|
|
|
|
warningFormat.setUnderlineColor(Qt::darkYellow);
|
|
|
|
|
|
|
|
|
|
QTextDocument *doc = m_textEditor->editorWidget()->document();
|
|
|
|
|
|
|
|
|
|
m_editorUpdates.selections.clear();
|
|
|
|
|
foreach (const Document::DiagnosticMessage &m, allDiagnostics) {
|
|
|
|
|
QTextEdit::ExtraSelection sel;
|
|
|
|
|
if (m.isWarning())
|
|
|
|
|
sel.format = warningFormat;
|
|
|
|
|
else
|
|
|
|
|
sel.format = errorFormat;
|
|
|
|
|
|
|
|
|
|
QTextCursor c(doc->findBlockByNumber(m.line() - 1));
|
|
|
|
|
const QString text = c.block().text();
|
|
|
|
|
if (m.length() > 0 && m.column() + m.length() < (unsigned)text.size()) {
|
|
|
|
|
int column = m.column() > 0 ? m.column() - 1 : 0;
|
|
|
|
|
c.setPosition(c.position() + column);
|
|
|
|
|
c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, m.length());
|
|
|
|
|
} else {
|
|
|
|
|
for (int i = 0; i < text.size(); ++i) {
|
2013-07-24 11:59:39 +02:00
|
|
|
if (!text.at(i).isSpace()) {
|
2013-04-17 10:58:20 +02:00
|
|
|
c.setPosition(c.position() + i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
c.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
|
|
|
|
|
}
|
|
|
|
|
sel.cursor = c;
|
|
|
|
|
sel.format.setToolTip(m.text());
|
|
|
|
|
m_editorUpdates.selections.append(sel);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2013-04-17 10:58:20 +02:00
|
|
|
|
|
|
|
|
m_editorUpdates.revision = doc->revision();
|
|
|
|
|
|
|
|
|
|
updateEditor();
|
|
|
|
|
}
|
|
|
|
|
void CppEditorSupport::updateEditor()
|
|
|
|
|
{
|
|
|
|
|
m_updateEditorTimer->start(UpdateEditorInterval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorSupport::updateEditorNow()
|
|
|
|
|
{
|
|
|
|
|
if (!m_textEditor)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
BaseTextEditorWidget *editorWidget = m_textEditor->editorWidget();
|
|
|
|
|
if (editorWidget->document()->revision() != m_editorUpdates.revision)
|
|
|
|
|
return; // outdated
|
|
|
|
|
|
|
|
|
|
editorWidget->setExtraSelections(BaseTextEditorWidget::CodeWarningsSelection,
|
|
|
|
|
m_editorUpdates.selections);
|
|
|
|
|
editorWidget->setIfdefedOutBlocks(m_editorUpdates.ifdefedOutBlocks);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-10-10 10:26:39 +02:00
|
|
|
void CppEditorSupport::onCurrentEditorChanged()
|
|
|
|
|
{
|
|
|
|
|
bool editorVisible = m_textEditor->widget()->isVisible();
|
|
|
|
|
|
|
|
|
|
if (m_editorVisible != editorVisible) {
|
|
|
|
|
m_editorVisible = editorVisible;
|
|
|
|
|
if (editorVisible) {
|
|
|
|
|
m_editorGCTimer->stop();
|
|
|
|
|
QMutexLocker locker(&m_lastSemanticInfoLock);
|
|
|
|
|
if (!m_lastSemanticInfo.doc)
|
|
|
|
|
updateDocumentNow();
|
|
|
|
|
} else {
|
|
|
|
|
m_editorGCTimer->start(EditorHiddenGCTimeout);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorSupport::releaseResources()
|
|
|
|
|
{
|
|
|
|
|
snapshotUpdater()->releaseSnapshot();
|
|
|
|
|
QMutexLocker semanticLocker(&m_lastSemanticInfoLock);
|
|
|
|
|
m_lastSemanticInfo = SemanticInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 10:58:20 +02:00
|
|
|
SemanticInfo::Source CppEditorSupport::currentSource(bool force)
|
|
|
|
|
{
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
m_textEditor->convertPosition(m_textEditor->editorWidget()->position(), &line, &column);
|
|
|
|
|
|
2014-01-07 10:43:59 +01:00
|
|
|
const Snapshot snapshot = snapshotUpdater()->snapshot();
|
2013-04-17 10:58:20 +02:00
|
|
|
|
2013-08-19 15:47:51 +02:00
|
|
|
QByteArray code;
|
2013-04-17 10:58:20 +02:00
|
|
|
if (force || m_lastSemanticInfo.revision != editorRevision())
|
|
|
|
|
code = contents(); // get the source code only when needed.
|
|
|
|
|
|
|
|
|
|
const unsigned revision = editorRevision();
|
|
|
|
|
SemanticInfo::Source source(snapshot, fileName(), code, line, column, revision, force);
|
|
|
|
|
return source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorSupport::recalculateSemanticInfoNow(const SemanticInfo::Source &source,
|
|
|
|
|
bool emitSignalWhenFinished,
|
|
|
|
|
TopLevelDeclarationProcessor *processor)
|
|
|
|
|
{
|
|
|
|
|
SemanticInfo semanticInfo;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_lastSemanticInfoLock);
|
|
|
|
|
semanticInfo.revision = m_lastSemanticInfo.revision;
|
|
|
|
|
semanticInfo.forced = source.force;
|
|
|
|
|
|
|
|
|
|
if (!source.force
|
|
|
|
|
&& m_lastSemanticInfo.revision == source.revision
|
|
|
|
|
&& m_lastSemanticInfo.doc
|
|
|
|
|
&& m_lastSemanticInfo.doc->translationUnit()->ast()
|
|
|
|
|
&& m_lastSemanticInfo.doc->fileName() == source.fileName) {
|
|
|
|
|
semanticInfo.snapshot = m_lastSemanticInfo.snapshot; // ### TODO: use the new snapshot.
|
|
|
|
|
semanticInfo.doc = m_lastSemanticInfo.doc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (semanticInfo.doc.isNull()) {
|
|
|
|
|
semanticInfo.snapshot = source.snapshot;
|
|
|
|
|
if (source.snapshot.contains(source.fileName)) {
|
|
|
|
|
Document::Ptr doc = source.snapshot.preprocessedDocument(source.code, source.fileName);
|
|
|
|
|
if (processor)
|
|
|
|
|
doc->control()->setTopLevelDeclarationProcessor(processor);
|
|
|
|
|
doc->check();
|
|
|
|
|
semanticInfo.doc = doc;
|
2013-05-03 16:04:56 +02:00
|
|
|
} else {
|
|
|
|
|
return;
|
2013-04-17 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (semanticInfo.doc) {
|
|
|
|
|
TranslationUnit *translationUnit = semanticInfo.doc->translationUnit();
|
|
|
|
|
AST * ast = translationUnit->ast();
|
|
|
|
|
|
|
|
|
|
FunctionDefinitionUnderCursor functionDefinitionUnderCursor(semanticInfo.doc->translationUnit());
|
|
|
|
|
DeclarationAST *currentFunctionDefinition = functionDefinitionUnderCursor(ast, source.line, source.column);
|
|
|
|
|
|
|
|
|
|
const LocalSymbols useTable(semanticInfo.doc, currentFunctionDefinition);
|
|
|
|
|
semanticInfo.revision = source.revision;
|
|
|
|
|
semanticInfo.localUses = useTable.uses;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_lastSemanticInfoLock);
|
|
|
|
|
m_lastSemanticInfo = semanticInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (emitSignalWhenFinished)
|
|
|
|
|
emit semanticInfoUpdated(semanticInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorSupport::recalculateSemanticInfoDetached_helper(QFutureInterface<void> &future, SemanticInfo::Source source)
|
|
|
|
|
{
|
|
|
|
|
class TLDProc: public TopLevelDeclarationProcessor
|
|
|
|
|
{
|
|
|
|
|
QFutureInterface<void> m_theFuture;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TLDProc(QFutureInterface<void> &aFuture): m_theFuture(aFuture) {}
|
|
|
|
|
virtual ~TLDProc() {}
|
|
|
|
|
virtual bool processDeclaration(DeclarationAST *ast) {
|
|
|
|
|
Q_UNUSED(ast);
|
|
|
|
|
return m_theFuture.isCanceled();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TLDProc tldProc(future);
|
|
|
|
|
recalculateSemanticInfoNow(source, true, &tldProc);
|
|
|
|
|
}
|
2013-05-02 11:42:33 +02:00
|
|
|
|
|
|
|
|
void CppEditorSupport::onMimeTypeChanged()
|
|
|
|
|
{
|
|
|
|
|
m_highlighter.cancel();
|
|
|
|
|
m_highlighter.waitForFinished();
|
|
|
|
|
|
|
|
|
|
m_highlightingSupport.reset(m_modelManager->highlightingSupport(m_textEditor));
|
|
|
|
|
|
|
|
|
|
disconnect(this, SIGNAL(semanticInfoUpdated(CppTools::SemanticInfo)),
|
|
|
|
|
this, SLOT(startHighlighting()));
|
|
|
|
|
if (m_highlightingSupport && m_highlightingSupport->requiresSemanticInfo())
|
|
|
|
|
connect(this, SIGNAL(semanticInfoUpdated(CppTools::SemanticInfo)),
|
|
|
|
|
this, SLOT(startHighlighting()));
|
|
|
|
|
|
2013-08-30 13:13:44 +02:00
|
|
|
m_completionAssistProvider = m_modelManager->completionAssistProvider(m_textEditor);
|
|
|
|
|
|
2013-05-02 11:42:33 +02:00
|
|
|
updateDocumentNow();
|
|
|
|
|
}
|
2013-06-18 16:26:40 +02:00
|
|
|
|
|
|
|
|
void CppEditorSupport::onAboutToReload()
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(!m_fileIsBeingReloaded);
|
|
|
|
|
m_fileIsBeingReloaded = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorSupport::onReloadFinished()
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(m_fileIsBeingReloaded);
|
|
|
|
|
m_fileIsBeingReloaded = false;
|
|
|
|
|
updateDocument();
|
|
|
|
|
}
|