2014-05-19 16:33:14 -04:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-05-19 16:33:14 -04:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2014-05-19 16:33:14 -04:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2014-05-19 16:33:14 -04:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppeditordocument.h"
|
|
|
|
|
|
|
|
|
|
#include "cppeditorconstants.h"
|
2016-03-16 13:04:05 +01:00
|
|
|
#include "cppeditorplugin.h"
|
2014-05-19 16:33:14 -04:00
|
|
|
#include "cpphighlighter.h"
|
2016-03-16 13:04:05 +01:00
|
|
|
#include "cppquickfixassistant.h"
|
2014-05-19 16:33:14 -04:00
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
#include <coreplugin/infobar.h>
|
|
|
|
|
|
2014-09-09 16:02:31 +02:00
|
|
|
#include <cpptools/baseeditordocumentparser.h>
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <cpptools/builtineditordocumentprocessor.h>
|
2014-05-19 16:33:14 -04:00
|
|
|
#include <cpptools/cppcodeformatter.h>
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <cpptools/cppcodemodelsettings.h>
|
2014-09-15 00:12:27 +02:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2014-08-01 18:16:24 +02:00
|
|
|
#include <cpptools/cppqtstyleindenter.h>
|
2014-05-19 16:33:14 -04:00
|
|
|
#include <cpptools/cpptoolsconstants.h>
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <cpptools/cpptoolsplugin.h>
|
|
|
|
|
|
2014-09-09 16:02:31 +02:00
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
|
2018-11-20 11:23:30 +01:00
|
|
|
#include <texteditor/icodestylepreferencesfactory.h>
|
2019-01-28 12:25:36 +01:00
|
|
|
#include <texteditor/textdocumentlayout.h>
|
2018-11-20 11:23:30 +01:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2015-02-04 09:32:46 +01:00
|
|
|
#include <utils/mimetypes/mimedatabase.h>
|
2014-08-19 15:59:29 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/runextensions.h>
|
2014-05-19 16:33:14 -04:00
|
|
|
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
namespace {
|
|
|
|
|
|
2014-09-15 00:12:27 +02:00
|
|
|
CppTools::CppModelManager *mm()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2014-09-15 00:12:27 +02:00
|
|
|
return CppTools::CppModelManager::instance();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
2018-11-20 11:23:30 +01:00
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2014-05-19 16:33:14 -04:00
|
|
|
namespace CppEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2015-12-02 13:15:26 +01:00
|
|
|
enum { processDocumentIntervalInMs = 150 };
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2015-02-05 12:10:43 +01:00
|
|
|
class CppEditorDocumentHandleImpl : public CppTools::CppEditorDocumentHandle
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2015-02-05 12:10:43 +01:00
|
|
|
CppEditorDocumentHandleImpl(CppEditorDocument *cppEditorDocument)
|
2014-08-19 15:59:29 +02:00
|
|
|
: m_cppEditorDocument(cppEditorDocument)
|
2014-12-21 21:54:30 +02:00
|
|
|
, m_registrationFilePath(cppEditorDocument->filePath().toString())
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2014-11-24 11:19:41 +01:00
|
|
|
mm()->registerCppEditorDocument(this);
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-07 11:04:13 +01:00
|
|
|
~CppEditorDocumentHandleImpl() override
|
|
|
|
|
{
|
|
|
|
|
mm()->unregisterCppEditorDocument(m_registrationFilePath);
|
|
|
|
|
}
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
QString filePath() const override { return m_cppEditorDocument->filePath().toString(); }
|
|
|
|
|
QByteArray contents() const override { return m_cppEditorDocument->contentsText(); }
|
|
|
|
|
unsigned revision() const override { return m_cppEditorDocument->contentsRevision(); }
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
CppTools::BaseEditorDocumentProcessor *processor() const override
|
2014-08-19 15:59:29 +02:00
|
|
|
{ return m_cppEditorDocument->processor(); }
|
|
|
|
|
|
2015-08-20 13:15:04 +02:00
|
|
|
void resetProcessor() override
|
2015-05-08 15:48:17 +02:00
|
|
|
{ m_cppEditorDocument->resetProcessor(); }
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
private:
|
2014-08-27 10:01:27 +02:00
|
|
|
CppEditor::Internal::CppEditorDocument * const m_cppEditorDocument;
|
2014-08-19 15:59:29 +02:00
|
|
|
// The file path of the editor document can change (e.g. by "Save As..."), so make sure
|
|
|
|
|
// that un-registration happens with the path the document was registered.
|
|
|
|
|
const QString m_registrationFilePath;
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
CppEditorDocument::CppEditorDocument()
|
2019-02-07 11:04:13 +01:00
|
|
|
: m_minimizableInfoBars(*infoBar())
|
2014-05-19 16:33:14 -04:00
|
|
|
{
|
|
|
|
|
setId(CppEditor::Constants::CPPEDITOR_ID);
|
|
|
|
|
setSyntaxHighlighter(new CppHighlighter);
|
2018-11-20 11:23:30 +01:00
|
|
|
|
|
|
|
|
ICodeStylePreferencesFactory *factory
|
|
|
|
|
= TextEditorSettings::codeStyleFactory(CppTools::Constants::CPP_SETTINGS_ID);
|
2019-01-16 09:37:54 +01:00
|
|
|
setIndenter(factory->createIndenter(document()));
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2016-05-28 23:47:48 +03:00
|
|
|
connect(this, &TextEditor::TextDocument::tabSettingsChanged,
|
|
|
|
|
this, &CppEditorDocument::invalidateFormatterCache);
|
|
|
|
|
connect(this, &Core::IDocument::mimeTypeChanged,
|
|
|
|
|
this, &CppEditorDocument::onMimeTypeChanged);
|
|
|
|
|
|
|
|
|
|
connect(this, &Core::IDocument::aboutToReload,
|
|
|
|
|
this, &CppEditorDocument::onAboutToReload);
|
|
|
|
|
connect(this, &Core::IDocument::reloadFinished,
|
|
|
|
|
this, &CppEditorDocument::onReloadFinished);
|
2014-12-21 21:54:30 +02:00
|
|
|
connect(this, &IDocument::filePathChanged,
|
|
|
|
|
this, &CppEditorDocument::onFilePathChanged);
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
connect(&m_parseContextModel, &ParseContextModel::preferredParseContextChanged,
|
|
|
|
|
this, &CppEditorDocument::reparseWithPreferredParseContext);
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
// See also onFilePathChanged() for more initialization
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
bool CppEditorDocument::isObjCEnabled() const
|
2014-05-19 16:33:14 -04:00
|
|
|
{
|
|
|
|
|
return m_isObjCEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 22:24:47 +02:00
|
|
|
CppTools::CppCompletionAssistProvider *CppEditorDocument::completionAssistProvider() const
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
return m_completionAssistProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-15 09:46:29 +01:00
|
|
|
TextEditor::IAssistProvider *CppEditorDocument::quickFixAssistProvider() const
|
2016-03-16 13:04:05 +01:00
|
|
|
{
|
|
|
|
|
return CppEditorPlugin::instance()->quickFixProvider();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 14:37:26 +02:00
|
|
|
void CppEditorDocument::recalculateSemanticInfoDetached()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
CppTools::BaseEditorDocumentProcessor *p = processor();
|
|
|
|
|
QTC_ASSERT(p, return);
|
2015-06-19 14:37:26 +02:00
|
|
|
p->recalculateSemanticInfoDetached(true);
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
CppTools::SemanticInfo CppEditorDocument::recalculateSemanticInfo()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
CppTools::BaseEditorDocumentProcessor *p = processor();
|
|
|
|
|
QTC_ASSERT(p, CppTools::SemanticInfo());
|
|
|
|
|
return p->recalculateSemanticInfo();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
QByteArray CppEditorDocument::contentsText() const
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
QMutexLocker locker(&m_cachedContentsLock);
|
|
|
|
|
|
|
|
|
|
const int currentRevision = document()->revision();
|
|
|
|
|
if (m_cachedContentsRevision != currentRevision && !m_fileIsBeingReloaded) {
|
|
|
|
|
m_cachedContentsRevision = currentRevision;
|
|
|
|
|
m_cachedContents = plainText().toUtf8();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_cachedContents;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::applyFontSettings()
|
2014-05-19 16:33:14 -04:00
|
|
|
{
|
|
|
|
|
if (TextEditor::SyntaxHighlighter *highlighter = syntaxHighlighter()) {
|
|
|
|
|
// Clear all additional formats since they may have changed
|
|
|
|
|
QTextBlock b = document()->firstBlock();
|
|
|
|
|
while (b.isValid()) {
|
2016-08-03 23:09:08 +03:00
|
|
|
QVector<QTextLayout::FormatRange> noFormats;
|
|
|
|
|
highlighter->setExtraFormats(b, noFormats);
|
2014-05-19 16:33:14 -04:00
|
|
|
b = b.next();
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-22 18:43:31 +02:00
|
|
|
TextDocument::applyFontSettings(); // rehighlights and updates additional formats
|
2015-06-24 07:18:49 +02:00
|
|
|
if (m_processor)
|
|
|
|
|
m_processor->semanticRehighlight();
|
2014-05-19 16:33:14 -04:00
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::invalidateFormatterCache()
|
2014-05-19 16:33:14 -04:00
|
|
|
{
|
|
|
|
|
CppTools::QtStyleCodeFormatter formatter;
|
|
|
|
|
formatter.invalidateCache(document());
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::onMimeTypeChanged()
|
2014-05-19 16:33:14 -04:00
|
|
|
{
|
|
|
|
|
const QString &mt = mimeType();
|
|
|
|
|
m_isObjCEnabled = (mt == QLatin1String(CppTools::Constants::OBJECTIVE_C_SOURCE_MIMETYPE)
|
2014-08-19 15:59:29 +02:00
|
|
|
|| mt == QLatin1String(CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE));
|
2015-11-11 10:47:27 +01:00
|
|
|
m_completionAssistProvider = mm()->completionAssistProvider();
|
2015-08-31 16:08:12 +02:00
|
|
|
|
|
|
|
|
initializeTimer();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::onAboutToReload()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(!m_fileIsBeingReloaded);
|
|
|
|
|
m_fileIsBeingReloaded = true;
|
2017-07-06 12:04:19 +02:00
|
|
|
|
|
|
|
|
processor()->invalidateDiagnostics();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::onReloadFinished()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(m_fileIsBeingReloaded);
|
|
|
|
|
m_fileIsBeingReloaded = false;
|
2017-07-06 12:04:19 +02:00
|
|
|
|
|
|
|
|
m_processorRevision = document()->revision();
|
|
|
|
|
processDocument();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
void CppEditorDocument::reparseWithPreferredParseContext(const QString &parseContextId)
|
|
|
|
|
{
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
|
|
|
|
|
// Update parser
|
|
|
|
|
setPreferredParseContext(parseContextId);
|
|
|
|
|
|
|
|
|
|
// Remember the setting
|
|
|
|
|
const QString key = Constants::PREFERRED_PARSE_CONTEXT + filePath().toString();
|
|
|
|
|
ProjectExplorer::SessionManager::setValue(key, parseContextId);
|
|
|
|
|
|
|
|
|
|
// Reprocess
|
|
|
|
|
scheduleProcessDocument();
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-21 21:54:30 +02:00
|
|
|
void CppEditorDocument::onFilePathChanged(const Utils::FileName &oldPath,
|
|
|
|
|
const Utils::FileName &newPath)
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(oldPath);
|
|
|
|
|
|
|
|
|
|
if (!newPath.isEmpty()) {
|
2019-01-16 09:37:54 +01:00
|
|
|
indenter()->setFileName(newPath);
|
2017-03-02 12:07:11 +01:00
|
|
|
setMimeType(Utils::mimeTypeForFile(newPath.toFileInfo()).name());
|
2014-08-19 15:59:29 +02:00
|
|
|
|
2017-01-24 18:13:53 +01:00
|
|
|
connect(this, &Core::IDocument::contentsChanged,
|
|
|
|
|
this, &CppEditorDocument::scheduleProcessDocument,
|
|
|
|
|
Qt::UniqueConnection);
|
2014-08-19 15:59:29 +02:00
|
|
|
|
|
|
|
|
// Un-Register/Register in ModelManager
|
2014-11-24 15:07:17 +01:00
|
|
|
m_editorDocumentHandle.reset();
|
2015-02-05 12:10:43 +01:00
|
|
|
m_editorDocumentHandle.reset(new CppEditorDocumentHandleImpl(this));
|
2014-08-19 15:59:29 +02:00
|
|
|
|
|
|
|
|
resetProcessor();
|
2017-01-18 15:05:46 +01:00
|
|
|
applyPreferredParseContextFromSettings();
|
|
|
|
|
applyExtraPreprocessorDirectivesFromSettings();
|
2014-08-19 15:59:29 +02:00
|
|
|
m_processorRevision = document()->revision();
|
|
|
|
|
processDocument();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::scheduleProcessDocument()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2017-07-06 12:04:19 +02:00
|
|
|
if (m_fileIsBeingReloaded)
|
|
|
|
|
return;
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
m_processorRevision = document()->revision();
|
2015-08-31 16:08:12 +02:00
|
|
|
m_processorTimer.start();
|
2016-09-14 16:16:10 +02:00
|
|
|
processor()->editorDocumentTimerRestarted();
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::processDocument()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2017-07-03 13:29:30 +02:00
|
|
|
processor()->invalidateDiagnostics();
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
if (processor()->isParserRunning() || m_processorRevision != contentsRevision()) {
|
|
|
|
|
m_processorTimer.start();
|
2016-09-14 16:16:10 +02:00
|
|
|
processor()->editorDocumentTimerRestarted();
|
2014-08-19 15:59:29 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_processorTimer.stop();
|
|
|
|
|
if (m_fileIsBeingReloaded || filePath().isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
processor()->run();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::resetProcessor()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
releaseResources();
|
|
|
|
|
processor(); // creates a new processor
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
void CppEditorDocument::applyPreferredParseContextFromSettings()
|
|
|
|
|
{
|
|
|
|
|
if (filePath().isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QString key = Constants::PREFERRED_PARSE_CONTEXT + filePath().toString();
|
|
|
|
|
const QString parseContextId = ProjectExplorer::SessionManager::value(key).toString();
|
|
|
|
|
|
|
|
|
|
setPreferredParseContext(parseContextId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorDocument::applyExtraPreprocessorDirectivesFromSettings()
|
2014-09-09 16:02:31 +02:00
|
|
|
{
|
|
|
|
|
if (filePath().isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
const QString key = Constants::EXTRA_PREPROCESSOR_DIRECTIVES + filePath().toString();
|
|
|
|
|
const QByteArray directives = ProjectExplorer::SessionManager::value(key).toString().toUtf8();
|
2014-09-09 16:02:31 +02:00
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
setExtraPreprocessorDirectives(directives);
|
2014-09-09 16:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
void CppEditorDocument::setExtraPreprocessorDirectives(const QByteArray &directives)
|
2014-09-09 16:02:31 +02:00
|
|
|
{
|
2015-09-01 17:34:07 +02:00
|
|
|
const auto parser = processor()->parser();
|
2014-09-09 16:02:31 +02:00
|
|
|
QTC_ASSERT(parser, return);
|
2017-01-18 15:05:46 +01:00
|
|
|
|
|
|
|
|
CppTools::BaseEditorDocumentParser::Configuration config = parser->configuration();
|
|
|
|
|
if (config.editorDefines != directives) {
|
|
|
|
|
config.editorDefines = directives;
|
|
|
|
|
processor()->setParserConfig(config);
|
|
|
|
|
|
|
|
|
|
emit preprocessorSettingsChanged(!directives.trimmed().isEmpty());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorDocument::setPreferredParseContext(const QString &parseContextId)
|
|
|
|
|
{
|
|
|
|
|
const CppTools::BaseEditorDocumentParser::Ptr parser = processor()->parser();
|
|
|
|
|
QTC_ASSERT(parser, return);
|
|
|
|
|
|
|
|
|
|
CppTools::BaseEditorDocumentParser::Configuration config = parser->configuration();
|
|
|
|
|
if (config.preferredProjectPartId != parseContextId) {
|
|
|
|
|
config.preferredProjectPartId = parseContextId;
|
|
|
|
|
processor()->setParserConfig(config);
|
2014-09-09 16:02:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
unsigned CppEditorDocument::contentsRevision() const
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
return document()->revision();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
void CppEditorDocument::releaseResources()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
if (m_processor)
|
2019-02-07 11:04:13 +01:00
|
|
|
disconnect(m_processor.data(), nullptr, this, nullptr);
|
2014-08-19 15:59:29 +02:00
|
|
|
m_processor.reset();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
void CppEditorDocument::showHideInfoBarAboutMultipleParseContexts(bool show)
|
|
|
|
|
{
|
|
|
|
|
const Core::Id id = Constants::MULTIPLE_PARSE_CONTEXTS_AVAILABLE;
|
|
|
|
|
|
|
|
|
|
if (show) {
|
|
|
|
|
Core::InfoBarEntry info(id,
|
|
|
|
|
tr("Note: Multiple parse contexts are available for this file. "
|
|
|
|
|
"Choose the preferred one from the editor toolbar."),
|
|
|
|
|
Core::InfoBarEntry::GlobalSuppressionEnabled);
|
|
|
|
|
info.removeCancelButton();
|
|
|
|
|
if (infoBar()->canInfoBeAdded(id))
|
|
|
|
|
infoBar()->addInfo(info);
|
|
|
|
|
} else {
|
|
|
|
|
infoBar()->removeInfo(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:08:12 +02:00
|
|
|
void CppEditorDocument::initializeTimer()
|
|
|
|
|
{
|
|
|
|
|
m_processorTimer.setSingleShot(true);
|
2015-12-02 13:15:26 +01:00
|
|
|
m_processorTimer.setInterval(processDocumentIntervalInMs);
|
2015-08-31 16:08:12 +02:00
|
|
|
|
|
|
|
|
connect(&m_processorTimer,
|
|
|
|
|
&QTimer::timeout,
|
|
|
|
|
this,
|
|
|
|
|
&CppEditorDocument::processDocument,
|
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 15:05:46 +01:00
|
|
|
ParseContextModel &CppEditorDocument::parseContextModel()
|
|
|
|
|
{
|
|
|
|
|
return m_parseContextModel;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 13:23:01 +02:00
|
|
|
QFuture<CppTools::CursorInfo>
|
|
|
|
|
CppEditorDocument::cursorInfo(const CppTools::CursorInfoParams ¶ms)
|
|
|
|
|
{
|
|
|
|
|
return processor()->cursorInfo(params);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 12:33:55 +01:00
|
|
|
const MinimizableInfoBars &CppEditorDocument::minimizableInfoBars() const
|
|
|
|
|
{
|
|
|
|
|
return m_minimizableInfoBars;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 10:01:27 +02:00
|
|
|
CppTools::BaseEditorDocumentProcessor *CppEditorDocument::processor()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
|
|
|
|
if (!m_processor) {
|
2018-01-11 16:46:45 +01:00
|
|
|
m_processor.reset(mm()->createEditorDocumentProcessor(this));
|
2017-01-17 12:33:55 +01:00
|
|
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::projectPartInfoUpdated,
|
2017-01-18 15:05:46 +01:00
|
|
|
[this] (const CppTools::ProjectPartInfo &info)
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
2017-01-18 15:05:46 +01:00
|
|
|
using namespace CppTools;
|
|
|
|
|
const bool hasProjectPart = !(info.hints & ProjectPartInfo::IsFallbackMatch);
|
2017-01-17 12:33:55 +01:00
|
|
|
m_minimizableInfoBars.processHasProjectPart(hasProjectPart);
|
2017-01-18 15:05:46 +01:00
|
|
|
m_parseContextModel.update(info);
|
|
|
|
|
const bool isAmbiguous = info.hints & ProjectPartInfo::IsAmbiguousMatch;
|
|
|
|
|
const bool isProjectFile = info.hints & ProjectPartInfo::IsFromProjectMatch;
|
|
|
|
|
showHideInfoBarAboutMultipleParseContexts(isAmbiguous && isProjectFile);
|
2017-01-17 12:33:55 +01:00
|
|
|
});
|
2014-08-19 15:59:29 +02:00
|
|
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::codeWarningsUpdated,
|
2017-01-17 12:33:55 +01:00
|
|
|
[this] (unsigned revision,
|
|
|
|
|
const QList<QTextEdit::ExtraSelection> selections,
|
|
|
|
|
const std::function<QWidget*()> &creator,
|
|
|
|
|
const TextEditor::RefactorMarkers &refactorMarkers) {
|
|
|
|
|
emit codeWarningsUpdated(revision, selections, refactorMarkers);
|
|
|
|
|
m_minimizableInfoBars.processHeaderDiagnostics(creator);
|
|
|
|
|
});
|
2014-08-19 15:59:29 +02:00
|
|
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::ifdefedOutBlocksUpdated,
|
2014-08-27 10:01:27 +02:00
|
|
|
this, &CppEditorDocument::ifdefedOutBlocksUpdated);
|
2014-08-19 15:59:29 +02:00
|
|
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::cppDocumentUpdated,
|
2017-01-16 10:51:13 +01:00
|
|
|
[this](const CPlusPlus::Document::Ptr document) {
|
|
|
|
|
// Update syntax highlighter
|
|
|
|
|
auto *highlighter = qobject_cast<CppHighlighter *>(syntaxHighlighter());
|
|
|
|
|
highlighter->setLanguageFeatures(document->languageFeatures());
|
|
|
|
|
|
|
|
|
|
// Forward signal
|
|
|
|
|
emit cppDocumentUpdated(document);
|
|
|
|
|
|
|
|
|
|
});
|
2014-08-19 15:59:29 +02:00
|
|
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::semanticInfoUpdated,
|
2014-08-27 10:01:27 +02:00
|
|
|
this, &CppEditorDocument::semanticInfoUpdated);
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_processor.data();
|
2014-05-19 16:33:14 -04:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 15:58:13 +02:00
|
|
|
TextEditor::TabSettings CppEditorDocument::tabSettings() const
|
|
|
|
|
{
|
2019-01-16 09:37:54 +01:00
|
|
|
return indenter()->tabSettings().value_or(TextEditor::TextDocument::tabSettings());
|
2018-08-29 15:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-28 12:25:36 +01:00
|
|
|
static int formatRange(QTextDocument *doc,
|
|
|
|
|
TextEditor::Indenter *indenter,
|
|
|
|
|
std::pair<int, int> editedRange,
|
|
|
|
|
const TextEditor::TabSettings &tabSettings)
|
|
|
|
|
{
|
|
|
|
|
QTextCursor cursor(doc);
|
|
|
|
|
cursor.setPosition(editedRange.first);
|
|
|
|
|
cursor.setPosition(editedRange.second, QTextCursor::KeepAnchor);
|
|
|
|
|
const int oldBlockCount = doc->blockCount();
|
|
|
|
|
indenter->format(cursor, tabSettings);
|
|
|
|
|
return doc->blockCount() - oldBlockCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppEditorDocument::save(QString *errorString, const QString &fileName, bool autoSave)
|
|
|
|
|
{
|
|
|
|
|
if (indenter()->formatOnSave()) {
|
2019-02-08 08:59:06 +01:00
|
|
|
QTextCursor cursor(document());
|
|
|
|
|
cursor.joinPreviousEditBlock();
|
2019-01-28 12:25:36 +01:00
|
|
|
auto *layout = qobject_cast<TextEditor::TextDocumentLayout *>(document()->documentLayout());
|
|
|
|
|
const int documentRevision = layout->lastSaveRevision;
|
|
|
|
|
|
|
|
|
|
std::pair<int, int> editedRange;
|
|
|
|
|
for (int i = 0; i < document()->blockCount(); ++i) {
|
|
|
|
|
const QTextBlock block = document()->findBlockByNumber(i);
|
|
|
|
|
if (block.revision() == documentRevision) {
|
|
|
|
|
if (editedRange.first != -1)
|
|
|
|
|
i += formatRange(document(), indenter(), editedRange, tabSettings());
|
|
|
|
|
|
|
|
|
|
editedRange = std::make_pair(-1, -1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// block.revision() != documentRevision
|
|
|
|
|
if (editedRange.first == -1)
|
|
|
|
|
editedRange.first = block.position();
|
|
|
|
|
editedRange.second = block.position() + block.length();
|
|
|
|
|
}
|
|
|
|
|
if (editedRange.first != -1)
|
|
|
|
|
formatRange(document(), indenter(), editedRange, tabSettings());
|
2019-02-08 08:59:06 +01:00
|
|
|
cursor.endEditBlock();
|
2019-01-28 12:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TextEditor::TextDocument::save(errorString, fileName, autoSave);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 16:33:14 -04:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppEditor
|