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

@@ -41,7 +41,7 @@ void ClangEditorDocumentParser::updateImpl(const QFutureInterface<void> &,
state_.projectPartInfo, state_.projectPartInfo,
updateParams.activeProject, updateParams.activeProject,
updateParams.languagePreference, updateParams.languagePreference,
updateParams.hasActiveProjectChanged); updateParams.projectsUpdated);
emit projectPartInfoUpdated(state_.projectPartInfo); emit projectPartInfoUpdated(state_.projectPartInfo);
setState(state_); setState(state_);
} }

View File

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

View File

@@ -56,18 +56,18 @@ public:
UpdateParams(const WorkingCopy &workingCopy, UpdateParams(const WorkingCopy &workingCopy,
const ProjectExplorer::Project *activeProject, const ProjectExplorer::Project *activeProject,
Language languagePreference, Language languagePreference,
bool hasActiveProjectChanged) bool projectsUpdated)
: workingCopy(workingCopy) : workingCopy(workingCopy)
, activeProject(activeProject) , activeProject(activeProject)
, languagePreference(languagePreference) , languagePreference(languagePreference)
, hasActiveProjectChanged(hasActiveProjectChanged) , projectsUpdated(projectsUpdated)
{ {
} }
WorkingCopy workingCopy; WorkingCopy workingCopy;
const ProjectExplorer::Project *activeProject = nullptr; const ProjectExplorer::Project *activeProject = nullptr;
Language languagePreference = Language::Cxx; Language languagePreference = Language::Cxx;
bool hasActiveProjectChanged = false; bool projectsUpdated = false;
}; };
public: public:
@@ -99,7 +99,7 @@ protected:
const ProjectPartInfo &currentProjectPartInfo, const ProjectPartInfo &currentProjectPartInfo,
const ProjectExplorer::Project *activeProject, const ProjectExplorer::Project *activeProject,
Language languagePreference, Language languagePreference,
bool hasActiveProjectChanged); bool projectsUpdated);
mutable QMutex m_stateAndConfigurationMutex; 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() const Language languagePreference = codeModelSettings()->interpretAmbigiousHeadersAsCHeaders()
? Language::C ? Language::C
@@ -65,7 +65,7 @@ void BaseEditorDocumentProcessor::run(bool hasActiveProjectChanged)
runImpl({CppModelManager::instance()->workingCopy(), runImpl({CppModelManager::instance()->workingCopy(),
ProjectExplorer::SessionManager::startupProject(), ProjectExplorer::SessionManager::startupProject(),
languagePreference, languagePreference,
hasActiveProjectChanged}); projectsUpdated});
} }
TextEditor::QuickFixOperations TextEditor::QuickFixOperations

View File

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

View File

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

View File

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

View File

@@ -793,7 +793,7 @@ void CppModelManager::watchForCanceledProjectIndexer(QFuture<void> future,
watcher->setFuture(future); watcher->setFuture(future);
} }
void CppModelManager::updateCppEditorDocuments(bool hasActiveProjectChanged) const void CppModelManager::updateCppEditorDocuments(bool projectsUpdated) const
{ {
// Refresh visible documents // Refresh visible documents
QSet<Core::IDocument *> visibleCppEditorDocuments; QSet<Core::IDocument *> visibleCppEditorDocuments;
@@ -802,7 +802,7 @@ void CppModelManager::updateCppEditorDocuments(bool hasActiveProjectChanged) con
const QString filePath = document->filePath().toString(); const QString filePath = document->filePath().toString();
if (CppEditorDocumentHandle *theCppEditorDocument = cppEditorDocument(filePath)) { if (CppEditorDocumentHandle *theCppEditorDocument = cppEditorDocument(filePath)) {
visibleCppEditorDocuments.insert(document); 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) { foreach (Core::IDocument *document, invisibleCppEditorDocuments) {
const QString filePath = document->filePath().toString(); const QString filePath = document->filePath().toString();
if (CppEditorDocumentHandle *theCppEditorDocument = cppEditorDocument(filePath)) { if (CppEditorDocumentHandle *theCppEditorDocument = cppEditorDocument(filePath)) {
const CppEditorDocumentHandle::RefreshReason refreshReason = hasActiveProjectChanged const CppEditorDocumentHandle::RefreshReason refreshReason = projectsUpdated
? CppEditorDocumentHandle::ActiveProjectChange ? CppEditorDocumentHandle::ProjectUpdate
: CppEditorDocumentHandle::Usual; : CppEditorDocumentHandle::Other;
theCppEditorDocument->setRefreshReason(refreshReason); 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 // 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 // project updates come in. That is, there are no reasonable dependency tables based on
// resolved includes that we could rely on. // resolved includes that we could rely on.
updateCppEditorDocuments(); updateCppEditorDocuments(/*projectsUpdated = */ true);
// Trigger reindexing // Trigger reindexing
QFuture<void> indexerFuture = updateSourceFiles(filesToReindex, ForcedProgressNotification); QFuture<void> indexerFuture = updateSourceFiles(filesToReindex, ForcedProgressNotification);
@@ -1044,9 +1044,18 @@ void CppModelManager::onAboutToRemoveProject(ProjectExplorer::Project *project)
delayedGC(); 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 void CppModelManager::onSourceFilesRefreshed() const
@@ -1067,10 +1076,9 @@ void CppModelManager::onCurrentEditorChanged(Core::IEditor *editor)
const CppEditorDocumentHandle::RefreshReason refreshReason const CppEditorDocumentHandle::RefreshReason refreshReason
= theCppEditorDocument->refreshReason(); = theCppEditorDocument->refreshReason();
if (refreshReason != CppEditorDocumentHandle::None) { if (refreshReason != CppEditorDocumentHandle::None) {
const bool projectsChanged = refreshReason == CppEditorDocumentHandle::ProjectUpdate;
theCppEditorDocument->setRefreshReason(CppEditorDocumentHandle::None); theCppEditorDocument->setRefreshReason(CppEditorDocumentHandle::None);
const bool hasActiveProjectChanged theCppEditorDocument->processor()->run(projectsChanged);
= refreshReason == CppEditorDocumentHandle::ActiveProjectChange;
theCppEditorDocument->processor()->run(hasActiveProjectChanged);
} }
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -53,8 +53,8 @@ protected:
ProjectPartInfo::NoHint}; ProjectPartInfo::NoHint};
QString preferredProjectPartId; QString preferredProjectPartId;
const ProjectExplorer::Project *activeProject = nullptr; const ProjectExplorer::Project *activeProject = nullptr;
bool projectHasChanged = false;
Language languagePreference = Language::Cxx; Language languagePreference = Language::Cxx;
bool projectsChanged = false;
::ProjectPartChooser chooser; ::ProjectPartChooser chooser;
QList<ProjectPart::Ptr> projectPartsForFile; QList<ProjectPart::Ptr> projectPartsForFile;
@@ -74,17 +74,6 @@ TEST_F(ProjectPartChooser, ChooseManuallySet)
ASSERT_THAT(chosen, Eq(p2)); ASSERT_THAT(chosen, Eq(p2));
} }
TEST_F(ProjectPartChooser, ForMultipleChoosePrevious)
{
const ProjectPart::Ptr otherProjectPart;
projectPartsForFile += otherProjectPart;
projectPartsForFile += currentProjectPartInfo.projectPart;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(currentProjectPartInfo.projectPart));
}
TEST_F(ProjectPartChooser, ForMultipleChooseFromActiveProject) TEST_F(ProjectPartChooser, ForMultipleChooseFromActiveProject)
{ {
const QList<ProjectPart::Ptr> projectParts = createProjectPartsWithDifferentProjects(); const QList<ProjectPart::Ptr> projectParts = createProjectPartsWithDifferentProjects();
@@ -131,7 +120,6 @@ TEST_F(ProjectPartChooser, ForMultipleCheckIfActiveProjectChanged)
projectPartsForFile += projectParts; projectPartsForFile += projectParts;
currentProjectPartInfo.projectPart = firstProjectPart; currentProjectPartInfo.projectPart = firstProjectPart;
activeProject = secondProjectPart->project; activeProject = secondProjectPart->project;
projectHasChanged = true;
const ProjectPart::Ptr chosen = choose().projectPart; const ProjectPart::Ptr chosen = choose().projectPart;
@@ -213,7 +201,7 @@ TEST_F(ProjectPartChooser, ContinueUsingFallbackFromModelManagerIfProjectDoesNot
ASSERT_THAT(chosen, Eq(fallbackProjectPart)); ASSERT_THAT(chosen, Eq(fallbackProjectPart));
} }
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges) TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges1)
{ {
fallbackProjectPart.reset(new ProjectPart); fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart; currentProjectPartInfo.projectPart = fallbackProjectPart;
@@ -226,6 +214,20 @@ TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges)
ASSERT_THAT(chosen, Eq(addedProject)); ASSERT_THAT(chosen, Eq(addedProject));
} }
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges2)
{
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hint = ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
projectPartsFromDependenciesForFile += addedProject;
projectsChanged = true;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(addedProject));
}
TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager) TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager)
{ {
fallbackProjectPart.reset(new ProjectPart); fallbackProjectPart.reset(new ProjectPart);
@@ -237,7 +239,7 @@ TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager)
void ProjectPartChooser::SetUp() void ProjectPartChooser::SetUp()
{ {
chooser.setFallbackProjectPart([&](){ chooser.setFallbackProjectPart([&]() {
return fallbackProjectPart; return fallbackProjectPart;
}); });
chooser.setProjectPartsForFile([&](const QString &) { chooser.setProjectPartsForFile([&](const QString &) {
@@ -255,7 +257,7 @@ const ProjectPartInfo ProjectPartChooser::choose() const
preferredProjectPartId, preferredProjectPartId,
activeProject, activeProject,
languagePreference, languagePreference,
projectHasChanged); projectsChanged);
} }
QList<ProjectPart::Ptr> ProjectPartChooser::createProjectPartsWithDifferentProjects() QList<ProjectPart::Ptr> ProjectPartChooser::createProjectPartsWithDifferentProjects()