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"
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
#include "baseeditordocumentparser.h"
|
|
|
|
|
#include "builtineditordocumentprocessor.h"
|
|
|
|
|
#include "cppcodeformatter.h"
|
|
|
|
|
#include "cppcodemodelsettings.h"
|
|
|
|
|
#include "cppeditorconstants.h"
|
|
|
|
|
#include "cppeditorplugin.h"
|
|
|
|
|
#include "cppmodelmanager.h"
|
2014-05-19 16:33:14 -04:00
|
|
|
#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"
|
2021-08-30 10:58:08 +02:00
|
|
|
#include "cppqtstyleindenter.h"
|
2016-03-16 13:04:05 +01:00
|
|
|
#include "cppquickfixassistant.h"
|
2014-05-19 16:33:14 -04:00
|
|
|
|
2020-06-17 12:23:44 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2017-01-18 15:05:46 +01:00
|
|
|
|
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-04-02 09:48:30 +02:00
|
|
|
#include <texteditor/storagesettings.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>
|
|
|
|
|
|
2019-04-02 09:48:30 +02:00
|
|
|
#include <utils/executeondestruction.h>
|
2020-06-17 12:23:44 +02:00
|
|
|
#include <utils/infobar.h>
|
2022-02-23 17:11:20 +01:00
|
|
|
#include <utils/mimeutils.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 {
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
CppEditor::CppModelManager *mm()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
return CppEditor::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
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
class CppEditorDocumentHandleImpl : public 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
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
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
|
2021-08-30 10:58:08 +02:00
|
|
|
= TextEditorSettings::codeStyleFactory(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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
void CppEditorDocument::setCompletionAssistProvider(TextEditor::CompletionAssistProvider *provider)
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2021-06-18 16:30:03 +02:00
|
|
|
TextDocument::setCompletionAssistProvider(provider);
|
|
|
|
|
m_completionAssistProvider = nullptr;
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
void CppEditorDocument::setFunctionHintAssistProvider(TextEditor::CompletionAssistProvider *provider)
|
2020-07-31 16:50:03 +02:00
|
|
|
{
|
2021-06-18 16:30:03 +02:00
|
|
|
TextDocument::setFunctionHintAssistProvider(provider);
|
|
|
|
|
m_functionHintAssistProvider = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompletionAssistProvider *CppEditorDocument::completionAssistProvider() const
|
|
|
|
|
{
|
|
|
|
|
return m_completionAssistProvider
|
|
|
|
|
? m_completionAssistProvider : TextDocument::completionAssistProvider();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompletionAssistProvider *CppEditorDocument::functionHintAssistProvider() const
|
|
|
|
|
{
|
|
|
|
|
return m_functionHintAssistProvider
|
|
|
|
|
? m_functionHintAssistProvider : TextDocument::functionHintAssistProvider();
|
2020-07-31 16:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-15 09:46:29 +01:00
|
|
|
TextEditor::IAssistProvider *CppEditorDocument::quickFixAssistProvider() const
|
2016-03-16 13:04:05 +01:00
|
|
|
{
|
2022-01-31 13:10:28 +01:00
|
|
|
if (const auto baseProvider = TextDocument::quickFixAssistProvider())
|
|
|
|
|
return baseProvider;
|
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
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
BaseEditorDocumentProcessor *p = processor();
|
2014-08-19 15:59:29 +02:00
|
|
|
QTC_ASSERT(p, return);
|
2015-06-19 14:37:26 +02:00
|
|
|
p->recalculateSemanticInfoDetached(true);
|
2014-08-19 15:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
SemanticInfo CppEditorDocument::recalculateSemanticInfo()
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
BaseEditorDocumentProcessor *p = processor();
|
|
|
|
|
QTC_ASSERT(p, return SemanticInfo());
|
2014-08-19 15:59:29 +02:00
|
|
|
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
|
|
|
{
|
2019-06-13 09:56:39 +02:00
|
|
|
if (TextEditor::SyntaxHighlighter *highlighter = syntaxHighlighter())
|
|
|
|
|
highlighter->clearAllExtraFormats(); // Clear all additional formats since they may have changed
|
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
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
QtStyleCodeFormatter formatter;
|
2014-05-19 16:33:14 -04:00
|
|
|
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();
|
2021-08-30 10:58:08 +02:00
|
|
|
m_isObjCEnabled = (mt == QLatin1String(Constants::OBJECTIVE_C_SOURCE_MIMETYPE)
|
|
|
|
|
|| mt == QLatin1String(Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE));
|
2015-11-11 10:47:27 +01:00
|
|
|
m_completionAssistProvider = mm()->completionAssistProvider();
|
2020-07-31 16:50:03 +02:00
|
|
|
m_functionHintAssistProvider = mm()->functionHintAssistProvider();
|
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)
|
|
|
|
|
{
|
|
|
|
|
// Update parser
|
|
|
|
|
setPreferredParseContext(parseContextId);
|
|
|
|
|
|
|
|
|
|
// Remember the setting
|
|
|
|
|
const QString key = Constants::PREFERRED_PARSE_CONTEXT + filePath().toString();
|
|
|
|
|
ProjectExplorer::SessionManager::setValue(key, parseContextId);
|
|
|
|
|
|
|
|
|
|
// Reprocess
|
|
|
|
|
scheduleProcessDocument();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
void CppEditorDocument::onFilePathChanged(const Utils::FilePath &oldPath,
|
|
|
|
|
const Utils::FilePath &newPath)
|
2014-08-19 15:59:29 +02:00
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(oldPath)
|
2014-08-19 15:59:29 +02:00
|
|
|
|
|
|
|
|
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();
|
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();
|
|
|
|
|
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
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
BaseEditorDocumentParser::Configuration config = parser->configuration();
|
2017-01-18 15:05:46 +01:00
|
|
|
if (config.editorDefines != directives) {
|
|
|
|
|
config.editorDefines = directives;
|
|
|
|
|
processor()->setParserConfig(config);
|
|
|
|
|
|
|
|
|
|
emit preprocessorSettingsChanged(!directives.trimmed().isEmpty());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppEditorDocument::setPreferredParseContext(const QString &parseContextId)
|
|
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
const BaseEditorDocumentParser::Ptr parser = processor()->parser();
|
2017-01-18 15:05:46 +01:00
|
|
|
QTC_ASSERT(parser, return);
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
BaseEditorDocumentParser::Configuration config = parser->configuration();
|
2017-01-18 15:05:46 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2020-06-26 13:59:38 +02:00
|
|
|
const Utils::Id id = Constants::MULTIPLE_PARSE_CONTEXTS_AVAILABLE;
|
2017-01-18 15:05:46 +01:00
|
|
|
|
|
|
|
|
if (show) {
|
2020-06-17 12:23:44 +02:00
|
|
|
Utils::InfoBarEntry info(id,
|
|
|
|
|
tr("Note: Multiple parse contexts are available for this file. "
|
|
|
|
|
"Choose the preferred one from the editor toolbar."),
|
|
|
|
|
Utils::InfoBarEntry::GlobalSuppression::Enabled);
|
2017-01-18 15:05:46 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
QFuture<CursorInfo> CppEditorDocument::cursorInfo(const CursorInfoParams ¶ms)
|
2017-05-24 13:23:01 +02:00
|
|
|
{
|
|
|
|
|
return processor()->cursorInfo(params);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-17 12:33:55 +01:00
|
|
|
const MinimizableInfoBars &CppEditorDocument::minimizableInfoBars() const
|
|
|
|
|
{
|
|
|
|
|
return m_minimizableInfoBars;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
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));
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(m_processor.data(), &BaseEditorDocumentProcessor::projectPartInfoUpdated,
|
|
|
|
|
[this] (const ProjectPartInfo &info)
|
2017-01-17 12:33:55 +01:00
|
|
|
{
|
2017-01-18 15:05:46 +01:00
|
|
|
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
|
|
|
});
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(m_processor.data(), &BaseEditorDocumentProcessor::codeWarningsUpdated,
|
2017-01-17 12:33:55 +01:00
|
|
|
[this] (unsigned revision,
|
|
|
|
|
const QList<QTextEdit::ExtraSelection> selections,
|
|
|
|
|
const TextEditor::RefactorMarkers &refactorMarkers) {
|
|
|
|
|
emit codeWarningsUpdated(revision, selections, refactorMarkers);
|
|
|
|
|
});
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(m_processor.data(), &BaseEditorDocumentProcessor::ifdefedOutBlocksUpdated,
|
2014-08-27 10:01:27 +02:00
|
|
|
this, &CppEditorDocument::ifdefedOutBlocksUpdated);
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(m_processor.data(), &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);
|
|
|
|
|
|
|
|
|
|
});
|
2021-08-30 10:58:08 +02:00
|
|
|
connect(m_processor.data(), &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
|
|
|
}
|
|
|
|
|
|
2021-05-18 13:55:23 +02:00
|
|
|
bool CppEditorDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
|
2019-01-28 12:25:36 +01:00
|
|
|
{
|
2019-04-02 09:48:30 +02:00
|
|
|
Utils::ExecuteOnDestruction resetSettingsOnScopeExit;
|
|
|
|
|
|
2019-02-18 15:43:34 +01:00
|
|
|
if (indenter()->formatOnSave() && !autoSave) {
|
2019-01-28 12:25:36 +01:00
|
|
|
auto *layout = qobject_cast<TextEditor::TextDocumentLayout *>(document()->documentLayout());
|
|
|
|
|
const int documentRevision = layout->lastSaveRevision;
|
|
|
|
|
|
2019-02-13 14:17:21 +01:00
|
|
|
TextEditor::RangesInLines editedRanges;
|
|
|
|
|
TextEditor::RangeInLines lastRange{-1, -1};
|
2019-01-28 12:25:36 +01:00
|
|
|
for (int i = 0; i < document()->blockCount(); ++i) {
|
|
|
|
|
const QTextBlock block = document()->findBlockByNumber(i);
|
|
|
|
|
if (block.revision() == documentRevision) {
|
2019-02-13 14:17:21 +01:00
|
|
|
if (lastRange.startLine != -1)
|
|
|
|
|
editedRanges.push_back(lastRange);
|
2019-01-28 12:25:36 +01:00
|
|
|
|
2019-02-13 14:17:21 +01:00
|
|
|
lastRange.startLine = lastRange.endLine = -1;
|
2019-01-28 12:25:36 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// block.revision() != documentRevision
|
2019-02-13 14:17:21 +01:00
|
|
|
if (lastRange.startLine == -1)
|
|
|
|
|
lastRange.startLine = block.blockNumber() + 1;
|
|
|
|
|
lastRange.endLine = block.blockNumber() + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastRange.startLine != -1)
|
|
|
|
|
editedRanges.push_back(lastRange);
|
|
|
|
|
|
|
|
|
|
if (!editedRanges.empty()) {
|
|
|
|
|
QTextCursor cursor(document());
|
2019-03-15 13:19:15 +01:00
|
|
|
cursor.beginEditBlock();
|
2019-02-13 14:17:21 +01:00
|
|
|
indenter()->format(editedRanges);
|
|
|
|
|
cursor.endEditBlock();
|
2019-01-28 12:25:36 +01:00
|
|
|
}
|
2019-04-02 09:48:30 +02:00
|
|
|
|
|
|
|
|
TextEditor::StorageSettings settings = storageSettings();
|
|
|
|
|
resetSettingsOnScopeExit.reset(
|
|
|
|
|
[this, defaultSettings = settings]() { setStorageSettings(defaultSettings); });
|
|
|
|
|
settings.m_cleanWhitespace = false;
|
|
|
|
|
setStorageSettings(settings);
|
2019-01-28 12:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 13:55:23 +02:00
|
|
|
return TextEditor::TextDocument::save(errorString, filePath, autoSave);
|
2019-01-28 12:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-19 16:33:14 -04:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppEditor
|