Commit cc6a9bab authored by Erik Verbruggen's avatar Erik Verbruggen Committed by Nikolai Kosjar

C++: Move preprocessor settings handling to the document.

This cleans up the handling of those settings, and makes sure that the
C++ document is setting those settings before parsing. This also
prevents lock contention in the parser, because before this patch the
document would kick of the initial parse (after opening a document),
after which the editor would come in and try to set the preprocessor
settings, resulting in the UI thread locking until the initial parse was
done.

Change-Id: Ia2e18a9667e10279f045c1c5849046ec4cfe85ae
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 914adeab
......@@ -58,8 +58,6 @@
#include <cpptools/cppworkingcopy.h>
#include <cpptools/symbolfinder.h>
#include <projectexplorer/session.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/basetextdocumentlayout.h>
#include <texteditor/codeassist/assistproposalitem.h>
......@@ -172,9 +170,6 @@ void CppEditorWidget::finalizeInitialization()
connect(d->m_declDefLinkFinder, SIGNAL(foundLink(QSharedPointer<FunctionDeclDefLink>)),
this, SLOT(onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefLink>)));
connect(textDocument(), SIGNAL(filePathChanged(QString,QString)),
this, SLOT(onFilePathChanged()));
connect(&d->m_useSelectionsUpdater,
SIGNAL(selectionsForVariableUnderCursorUpdated(QList<QTextEdit::ExtraSelection>)),
&d->m_localRenaming,
......@@ -197,6 +192,12 @@ void CppEditorWidget::finalizeInitialization()
connect(this, SIGNAL(cursorPositionChanged()),
d->m_cppEditorOutline, SLOT(updateIndex()));
connect(cppEditorDocument(), &CppEditorDocument::preprocessorSettingsChanged,
[this](bool customSettings) {
d->m_preprocessorButton->setProperty("highlightWidget", customSettings);
d->m_preprocessorButton->update();
});
// set up function declaration - definition link
d->m_updateFunctionDeclDefLinkTimer.setSingleShot(true);
d->m_updateFunctionDeclDefLinkTimer.setInterval(UPDATE_FUNCTION_DECL_DEF_LINK_INTERVAL);
......@@ -712,26 +713,6 @@ void CppEditorWidget::onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefL
}
void CppEditorWidget::onFilePathChanged()
{
QTC_ASSERT(d->m_modelManager, return);
QByteArray additionalDirectives;
const QString &filePath = textDocument()->filePath();
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();
BaseEditorDocumentParser *parser = BaseEditorDocumentParser::get(filePath);
QTC_ASSERT(parser, return);
parser->setProjectPart(d->m_modelManager->projectPartForProjectFile(projectFile));
parser->setEditorDefines(additionalDirectives);
}
d->m_preprocessorButton->setProperty("highlightWidget", !additionalDirectives.trimmed().isEmpty());
d->m_preprocessorButton->update();
}
void CppEditorWidget::applyDeclDefLinkChanges(bool jumpToMatch)
{
if (!d->m_declDefLink)
......@@ -782,15 +763,10 @@ void CppEditorWidget::showPreProcessorWidget()
CppPreProcessorDialog preProcessorDialog(this, textDocument()->filePath(), projectParts);
if (preProcessorDialog.exec() == QDialog::Accepted) {
BaseEditorDocumentParser *parser = BaseEditorDocumentParser::get(fileName);
QTC_ASSERT(parser, return);
const QString &additionals = preProcessorDialog.additionalPreProcessorDirectives();
parser->setProjectPart(preProcessorDialog.projectPart());
parser->setEditorDefines(additionals.toUtf8());
parser->update(d->m_modelManager->workingCopy());
d->m_preprocessorButton->setProperty("highlightWidget", !additionals.trimmed().isEmpty());
d->m_preprocessorButton->update();
cppEditorDocument()->setPreprocessorSettings(
preProcessorDialog.projectPart(),
preProcessorDialog.additionalPreProcessorDirectives().toUtf8());
cppEditorDocument()->scheduleProcessDocument();
}
}
......
......@@ -122,7 +122,6 @@ private slots:
void abortDeclDefLink();
void onFunctionDeclDefLinkFound(QSharedPointer<FunctionDeclDefLink> link);
void onFilePathChanged();
void onCppDocumentUpdated();
void onCodeWarningsUpdated(unsigned revision,
......
......@@ -32,6 +32,7 @@
#include "cppeditorconstants.h"
#include "cpphighlighter.h"
#include <cpptools/baseeditordocumentparser.h>
#include <cpptools/builtineditordocumentprocessor.h>
#include <cpptools/cppcodeformatter.h>
#include <cpptools/cppcodemodelsettings.h>
......@@ -40,6 +41,8 @@
#include <cpptools/cpptoolsconstants.h>
#include <cpptools/cpptoolsplugin.h>
#include <projectexplorer/session.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
......@@ -206,6 +209,7 @@ void CppEditorDocument::onFilePathChanged(const QString &oldPath, const QString
m_editorDocumentHandle.reset(new CppEditorDocumentHandle(this));
resetProcessor();
updatePreprocessorSettings();
m_processorRevision = document()->revision();
processDocument();
}
......@@ -237,6 +241,34 @@ void CppEditorDocument::resetProcessor()
processor(); // creates a new processor
}
void CppEditorDocument::updatePreprocessorSettings()
{
if (filePath().isEmpty())
return;
const QString prefix = QLatin1String(Constants::CPP_PREPROCESSOR_PROJECT_PREFIX);
const QString &projectFile = ProjectExplorer::SessionManager::value(
prefix + filePath()).toString();
const QString directivesKey = projectFile + QLatin1Char(',') + filePath();
const QByteArray additionalDirectives = ProjectExplorer::SessionManager::value(
directivesKey).toString().toUtf8();
setPreprocessorSettings(mm()->projectPartForProjectFile(projectFile), additionalDirectives);
}
void CppEditorDocument::setPreprocessorSettings(const CppTools::ProjectPart::Ptr &projectPart,
const QByteArray &defines)
{
CppTools::BaseEditorDocumentParser *parser = processor()->parser();
QTC_ASSERT(parser, return);
if (parser->projectPart() != projectPart || parser->editorDefines() != defines) {
parser->setProjectPart(projectPart);
parser->setEditorDefines(defines);
emit preprocessorSettingsChanged(!defines.trimmed().isEmpty());
}
}
unsigned CppEditorDocument::contentsRevision() const
{
return document()->revision();
......
......@@ -60,6 +60,9 @@ public:
void semanticRehighlight();
CppTools::SemanticInfo recalculateSemanticInfo(); // TODO: Remove me
void setPreprocessorSettings(const CppTools::ProjectPart::Ptr &projectPart,
const QByteArray &defines);
signals:
void codeWarningsUpdated(unsigned contentsRevision,
const QList<QTextEdit::ExtraSelection> selections);
......@@ -70,6 +73,11 @@ signals:
void cppDocumentUpdated(const CPlusPlus::Document::Ptr document); // TODO: Remove me
void semanticInfoUpdated(const CppTools::SemanticInfo semanticInfo); // TODO: Remove me
void preprocessorSettingsChanged(bool customSettings);
public slots:
void scheduleProcessDocument();
protected:
void applyFontSettings();
......@@ -81,7 +89,6 @@ private slots:
void onAboutToReload();
void onReloadFinished();
void scheduleProcessDocument();
void processDocument();
private:
......@@ -90,6 +97,7 @@ private:
CppTools::BaseEditorDocumentProcessor *processor();
void resetProcessor();
void updatePreprocessorSettings();
void releaseResources();
private:
......
......@@ -95,7 +95,7 @@ int CppPreProcessorDialog::exec()
ProjectExplorer::SessionManager::setValue(
QLatin1String(Constants::CPP_PREPROCESSOR_PROJECT_PREFIX) + m_filePath,
m_partAdditions[m_ui->projectComboBox->currentIndex()].projectPart->id());
m_partAdditions[m_ui->projectComboBox->currentIndex()].projectPart->projectFile);
foreach (ProjectPartAddition partAddition, m_partAdditions) {
const QString &previousDirectives = ProjectExplorer::SessionManager::value(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment