Commit 74159648 authored by Nikolai Kosjar's avatar Nikolai Kosjar

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's avatarDavid Schulz <david.schulz@qt.io>
parent bddfe219
......@@ -41,7 +41,7 @@ void ClangEditorDocumentParser::updateImpl(const QFutureInterface<void> &,
state_.projectPartInfo,
updateParams.activeProject,
updateParams.languagePreference,
updateParams.hasActiveProjectChanged);
updateParams.projectsUpdated);
emit projectPartInfoUpdated(state_.projectPartInfo);
setState(state_);
}
......
......@@ -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;
}
......
......@@ -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;
......
......@@ -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
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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);
......
......@@ -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);
}
}
}
......
......@@ -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;
......
......@@ -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);
......
......@@ -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};
......
......@@ -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;
......
......@@ -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);
......
......@@ -53,8 +53,8 @@ protected:
ProjectPartInfo::NoHint};
QString preferredProjectPartId;
const ProjectExplorer::Project *activeProject = nullptr;
bool projectHasChanged = false;
Language languagePreference = Language::Cxx;
bool projectsChanged = false;
::ProjectPartChooser chooser;
QList<ProjectPart::Ptr> projectPartsForFile;
......@@ -74,17 +74,6 @@ TEST_F(ProjectPartChooser, ChooseManuallySet)
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)
{
const QList<ProjectPart::Ptr> projectParts = createProjectPartsWithDifferentProjects();
......@@ -131,7 +120,6 @@ TEST_F(ProjectPartChooser, ForMultipleCheckIfActiveProjectChanged)
projectPartsForFile += projectParts;
currentProjectPartInfo.projectPart = firstProjectPart;
activeProject = secondProjectPart->project;
projectHasChanged = true;
const ProjectPart::Ptr chosen = choose().projectPart;
......@@ -213,7 +201,7 @@ TEST_F(ProjectPartChooser, ContinueUsingFallbackFromModelManagerIfProjectDoesNot
ASSERT_THAT(chosen, Eq(fallbackProjectPart));
}
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges)
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges1)
{
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
......@@ -226,6 +214,20 @@ TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges)
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)
{
fallbackProjectPart.reset(new ProjectPart);
......@@ -237,7 +239,7 @@ TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager)
void ProjectPartChooser::SetUp()
{
chooser.setFallbackProjectPart([&](){
chooser.setFallbackProjectPart([&]() {
return fallbackProjectPart;
});
chooser.setProjectPartsForFile([&](const QString &) {
......@@ -255,7 +257,7 @@ const ProjectPartInfo ProjectPartChooser::choose() const
preferredProjectPartId,
activeProject,
languagePreference,
projectHasChanged);
projectsChanged);
}
QList<ProjectPart::Ptr> ProjectPartChooser::createProjectPartsWithDifferentProjects()
......
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