CppTools: Fix choosing project part after project open

As long as there are project parts for a source file, always determine
the best project part, instead of trying to stick to the previous one.
This ensures the best project part at all times and simplifies the code.

Change-Id: I25ea3eb43a5a3e6d93688d4b8965f596dc9ae22b
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Nikolai Kosjar
2017-01-19 15:46:40 +01:00
parent bddfe21961
commit 7415964819
14 changed files with 69 additions and 63 deletions

View File

@@ -125,7 +125,7 @@ ProjectPartInfo BaseEditorDocumentParser::determineProjectPart(
const ProjectPartInfo &currentProjectPartInfo,
const ProjectExplorer::Project *activeProject,
Language languagePreference,
bool hasActiveProjectChanged)
bool projectsUpdated)
{
Internal::ProjectPartChooser chooser;
chooser.setFallbackProjectPart([](){
@@ -145,7 +145,7 @@ ProjectPartInfo BaseEditorDocumentParser::determineProjectPart(
preferredProjectPartId,
activeProject,
languagePreference,
hasActiveProjectChanged);
projectsUpdated);
return chooserResult;
}

View File

@@ -56,18 +56,18 @@ public:
UpdateParams(const WorkingCopy &workingCopy,
const ProjectExplorer::Project *activeProject,
Language languagePreference,
bool hasActiveProjectChanged)
bool projectsUpdated)
: workingCopy(workingCopy)
, activeProject(activeProject)
, languagePreference(languagePreference)
, hasActiveProjectChanged(hasActiveProjectChanged)
, projectsUpdated(projectsUpdated)
{
}
WorkingCopy workingCopy;
const ProjectExplorer::Project *activeProject = nullptr;
Language languagePreference = Language::Cxx;
bool hasActiveProjectChanged = false;
bool projectsUpdated = false;
};
public:
@@ -99,7 +99,7 @@ protected:
const ProjectPartInfo &currentProjectPartInfo,
const ProjectExplorer::Project *activeProject,
Language languagePreference,
bool hasActiveProjectChanged);
bool projectsUpdated);
mutable QMutex m_stateAndConfigurationMutex;

View File

@@ -56,7 +56,7 @@ BaseEditorDocumentProcessor::~BaseEditorDocumentProcessor()
{
}
void BaseEditorDocumentProcessor::run(bool hasActiveProjectChanged)
void BaseEditorDocumentProcessor::run(bool projectsUpdated)
{
const Language languagePreference = codeModelSettings()->interpretAmbigiousHeadersAsCHeaders()
? Language::C
@@ -65,7 +65,7 @@ void BaseEditorDocumentProcessor::run(bool hasActiveProjectChanged)
runImpl({CppModelManager::instance()->workingCopy(),
ProjectExplorer::SessionManager::startupProject(),
languagePreference,
hasActiveProjectChanged});
projectsUpdated});
}
TextEditor::QuickFixOperations

View File

@@ -54,7 +54,7 @@ public:
BaseEditorDocumentProcessor(QTextDocument *textDocument, const QString &filePath);
virtual ~BaseEditorDocumentProcessor();
void run(bool hasActiveProjectChanged = false);
void run(bool projectsUpdated = false);
virtual void semanticRehighlight() = 0;
virtual void recalculateSemanticInfoDetached(bool force) = 0;
virtual CppTools::SemanticInfo recalculateSemanticInfo() = 0;

View File

@@ -82,7 +82,7 @@ void BuiltinEditorDocumentParser::updateImpl(const QFutureInterface<void> &futur
baseState.projectPartInfo,
updateParams.activeProject,
updateParams.languagePreference,
updateParams.hasActiveProjectChanged);
updateParams.projectsUpdated);
emit projectPartInfoUpdated(baseState.projectPartInfo);
if (state.forceSnapshotInvalidation) {

View File

@@ -157,8 +157,7 @@ void indexFindErrors(QFutureInterface<void> &future, const ParseParams params)
// Parse the file as precisely as possible
BuiltinEditorDocumentParser parser(file);
parser.setReleaseSourceAndAST(false);
parser.update({CppModelManager::instance()->workingCopy(), nullptr,
Language::Cxx, false});
parser.update({CppModelManager::instance()->workingCopy(), nullptr, Language::Cxx, false});
CPlusPlus::Document::Ptr document = parser.document();
QTC_ASSERT(document, return);

View File

@@ -793,7 +793,7 @@ void CppModelManager::watchForCanceledProjectIndexer(QFuture<void> future,
watcher->setFuture(future);
}
void CppModelManager::updateCppEditorDocuments(bool hasActiveProjectChanged) const
void CppModelManager::updateCppEditorDocuments(bool projectsUpdated) const
{
// Refresh visible documents
QSet<Core::IDocument *> visibleCppEditorDocuments;
@@ -802,7 +802,7 @@ void CppModelManager::updateCppEditorDocuments(bool hasActiveProjectChanged) con
const QString filePath = document->filePath().toString();
if (CppEditorDocumentHandle *theCppEditorDocument = cppEditorDocument(filePath)) {
visibleCppEditorDocuments.insert(document);
theCppEditorDocument->processor()->run(hasActiveProjectChanged);
theCppEditorDocument->processor()->run(projectsUpdated);
}
}
}
@@ -814,9 +814,9 @@ void CppModelManager::updateCppEditorDocuments(bool hasActiveProjectChanged) con
foreach (Core::IDocument *document, invisibleCppEditorDocuments) {
const QString filePath = document->filePath().toString();
if (CppEditorDocumentHandle *theCppEditorDocument = cppEditorDocument(filePath)) {
const CppEditorDocumentHandle::RefreshReason refreshReason = hasActiveProjectChanged
? CppEditorDocumentHandle::ActiveProjectChange
: CppEditorDocumentHandle::Usual;
const CppEditorDocumentHandle::RefreshReason refreshReason = projectsUpdated
? CppEditorDocumentHandle::ProjectUpdate
: CppEditorDocumentHandle::Other;
theCppEditorDocument->setRefreshReason(refreshReason);
}
}
@@ -905,7 +905,7 @@ QFuture<void> CppModelManager::updateProjectInfo(const ProjectInfo &newProjectIn
// However, on e.g. a session restore first the editor documents are created and then the
// project updates come in. That is, there are no reasonable dependency tables based on
// resolved includes that we could rely on.
updateCppEditorDocuments();
updateCppEditorDocuments(/*projectsUpdated = */ true);
// Trigger reindexing
QFuture<void> indexerFuture = updateSourceFiles(filesToReindex, ForcedProgressNotification);
@@ -1044,9 +1044,18 @@ void CppModelManager::onAboutToRemoveProject(ProjectExplorer::Project *project)
delayedGC();
}
void CppModelManager::onActiveProjectChanged(ProjectExplorer::Project *)
void CppModelManager::onActiveProjectChanged(ProjectExplorer::Project *project)
{
updateCppEditorDocuments(true);
if (!project)
return; // Last project closed.
{
QMutexLocker locker(&d->m_projectMutex);
if (!d->m_projectToProjectsInfo.contains(project))
return; // Not yet known to us.
}
updateCppEditorDocuments();
}
void CppModelManager::onSourceFilesRefreshed() const
@@ -1067,10 +1076,9 @@ void CppModelManager::onCurrentEditorChanged(Core::IEditor *editor)
const CppEditorDocumentHandle::RefreshReason refreshReason
= theCppEditorDocument->refreshReason();
if (refreshReason != CppEditorDocumentHandle::None) {
const bool projectsChanged = refreshReason == CppEditorDocumentHandle::ProjectUpdate;
theCppEditorDocument->setRefreshReason(CppEditorDocumentHandle::None);
const bool hasActiveProjectChanged
= refreshReason == CppEditorDocumentHandle::ActiveProjectChange;
theCppEditorDocument->processor()->run(hasActiveProjectChanged);
theCppEditorDocument->processor()->run(projectsChanged);
}
}
}

View File

@@ -88,7 +88,7 @@ public:
QFuture<void> updateSourceFiles(const QSet<QString> &sourceFiles,
ProgressNotificationMode mode = ReservedProgressNotification);
void updateCppEditorDocuments(bool hasActiveProjectChanged = false) const;
void updateCppEditorDocuments(bool projectsUpdated = false) const;
WorkingCopy workingCopy() const;
QByteArray codeModelConfiguration() const;

View File

@@ -879,8 +879,7 @@ void CppToolsPlugin::test_modelmanager_precompiled_headers()
BaseEditorDocumentParser::Configuration config = parser->configuration();
config.usePrecompiledHeaders = true;
parser->setConfiguration(config);
parser->update({CppModelManager::instance()->workingCopy(), nullptr,
Language::Cxx, false});
parser->update({CppModelManager::instance()->workingCopy(), nullptr, Language::Cxx, false});
// Check if defines from pch are considered
Document::Ptr document = mm->document(fileName);
@@ -958,8 +957,7 @@ void CppToolsPlugin::test_modelmanager_defines_per_editor()
BaseEditorDocumentParser::Configuration config = parser->configuration();
config.editorDefines = editorDefines.toUtf8();
parser->setConfiguration(config);
parser->update({CppModelManager::instance()->workingCopy(), nullptr,
Language::Cxx, false});
parser->update({CppModelManager::instance()->workingCopy(), nullptr, Language::Cxx, false});
Document::Ptr doc = mm->document(main1File);
QCOMPARE(nameOfFirstDeclaration(doc), firstDeclarationName);

View File

@@ -128,12 +128,13 @@ private:
bool m_isAmbiguous = false;
};
ProjectPartInfo ProjectPartChooser::choose(const QString &filePath,
ProjectPartInfo ProjectPartChooser::choose(
const QString &filePath,
const ProjectPartInfo &currentProjectPartInfo,
const QString &preferredProjectPartId,
const ProjectExplorer::Project *activeProject,
Language languagePreference,
bool projectHasChanged) const
bool projectsUpdated) const
{
QTC_CHECK(m_projectPartsForFile);
QTC_CHECK(m_projectPartsFromDependenciesForFile);
@@ -144,7 +145,8 @@ ProjectPartInfo ProjectPartChooser::choose(const QString &filePath,
QList<ProjectPart::Ptr> projectParts = m_projectPartsForFile(filePath);
if (projectParts.isEmpty()) {
if (projectPart && currentProjectPartInfo.hint == ProjectPartInfo::IsFallbackMatch)
if (!projectsUpdated && projectPart
&& currentProjectPartInfo.hint == ProjectPartInfo::IsFallbackMatch)
// Avoid re-calculating the expensive dependency table for non-project files.
return {projectPart, ProjectPartInfo::IsFallbackMatch};
@@ -162,16 +164,14 @@ ProjectPartInfo ProjectPartChooser::choose(const QString &filePath,
projectPart = prioritizer.projectPart();
}
} else {
if (projectHasChanged || !projectParts.contains(projectPart)) {
ProjectPartPrioritizer prioritizer(projectParts,
preferredProjectPartId,
activeProject,
languagePreference);
projectPart = prioritizer.projectPart();
hint = prioritizer.isAmbiguous()
? ProjectPartInfo::IsAmbiguousMatch
: ProjectPartInfo::NoHint;
}
ProjectPartPrioritizer prioritizer(projectParts,
preferredProjectPartId,
activeProject,
languagePreference);
projectPart = prioritizer.projectPart();
hint = prioritizer.isAmbiguous()
? ProjectPartInfo::IsAmbiguousMatch
: ProjectPartInfo::NoHint;
}
return {projectPart, hint};

View File

@@ -48,13 +48,12 @@ public:
void setProjectPartsForFile(const ProjectPartsForFile &getter);
void setProjectPartsFromDependenciesForFile(const ProjectPartsFromDependenciesForFile &getter);
ProjectPartInfo choose(
const QString &filePath,
ProjectPartInfo choose(const QString &filePath,
const ProjectPartInfo &currentProjectPartInfo,
const QString &preferredProjectPartId,
const ProjectExplorer::Project *activeProject,
Language languagePreference,
bool projectHasChanged) const;
bool projectsUpdated) const;
private:
FallBackProjectPart m_fallbackProjectPart;

View File

@@ -40,8 +40,8 @@ public:
enum RefreshReason {
None,
Usual, // e.g. project configuration change or change of editor contents
ActiveProjectChange
ProjectUpdate,
Other,
};
RefreshReason refreshReason() const;
void setRefreshReason(const RefreshReason &refreshReason);