From bddfe21961f59ee471b302da9bc7a5c7ee4f561d Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Thu, 19 Jan 2017 12:52:16 +0100 Subject: [PATCH] CppTools: Fix using updated project part 1. Open a project consisting of two subprojects referencing the same source file. 2. Open the source file. 3. Click '#' in the editor toolbar and select the second project (part). 4. Update the project file, e.g. add a define ==> Editor does not reflect the added define This is due to comparing project part pointers. Fix by using the project part id that remains stable across project manager updates. Change-Id: Ifd1a113e55ebe2ecf036cd7caafdbfd6e4cdf415 Reviewed-by: David Schulz --- .../clangeditordocumentparser.cpp | 2 +- src/plugins/cppeditor/cppeditordocument.cpp | 2 +- .../cpptools/baseeditordocumentparser.cpp | 8 +++---- .../cpptools/baseeditordocumentparser.h | 4 ++-- .../cpptools/builtineditordocumentparser.cpp | 2 +- .../cpptools/cppprojectpartchooser.cpp | 24 ++++++++++++------- src/plugins/cpptools/cppprojectpartchooser.h | 2 +- .../unittest/cppprojectpartchooser-test.cpp | 11 +++++---- 8 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/plugins/clangcodemodel/clangeditordocumentparser.cpp b/src/plugins/clangcodemodel/clangeditordocumentparser.cpp index 2d1d758272b..9948efcccaf 100644 --- a/src/plugins/clangcodemodel/clangeditordocumentparser.cpp +++ b/src/plugins/clangcodemodel/clangeditordocumentparser.cpp @@ -37,7 +37,7 @@ void ClangEditorDocumentParser::updateImpl(const QFutureInterface &, { State state_ = state(); state_.projectPartInfo = determineProjectPart(filePath(), - configuration(), + configuration().preferredProjectPartId, state_.projectPartInfo, updateParams.activeProject, updateParams.languagePreference, diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index 704478149a2..6640263c967 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -278,7 +278,7 @@ void CppEditorDocument::setPreprocessorSettings(const CppTools::ProjectPart::Ptr if (parser->projectPartInfo().projectPart != projectPart || parser->configuration().editorDefines != defines) { CppTools::BaseEditorDocumentParser::Configuration config = parser->configuration(); - config.manuallySetProjectPart = projectPart; + config.preferredProjectPartId = projectPart->id(); config.editorDefines = defines; parser->setConfiguration(config); diff --git a/src/plugins/cpptools/baseeditordocumentparser.cpp b/src/plugins/cpptools/baseeditordocumentparser.cpp index c0a5945bb5a..cd31bd9a074 100644 --- a/src/plugins/cpptools/baseeditordocumentparser.cpp +++ b/src/plugins/cpptools/baseeditordocumentparser.cpp @@ -121,15 +121,13 @@ BaseEditorDocumentParser::Ptr BaseEditorDocumentParser::get(const QString &fileP ProjectPartInfo BaseEditorDocumentParser::determineProjectPart( const QString &filePath, - const Configuration &config, + const QString &preferredProjectPartId, const ProjectPartInfo ¤tProjectPartInfo, const ProjectExplorer::Project *activeProject, Language languagePreference, bool hasActiveProjectChanged) { - using Internal::ProjectPartChooser; - - ProjectPartChooser chooser; + Internal::ProjectPartChooser chooser; chooser.setFallbackProjectPart([](){ return CppModelManager::instance()->fallbackProjectPart(); }); @@ -144,7 +142,7 @@ ProjectPartInfo BaseEditorDocumentParser::determineProjectPart( const ProjectPartInfo chooserResult = chooser.choose(filePath, currentProjectPartInfo, - config.manuallySetProjectPart, + preferredProjectPartId, activeProject, languagePreference, hasActiveProjectChanged); diff --git a/src/plugins/cpptools/baseeditordocumentparser.h b/src/plugins/cpptools/baseeditordocumentparser.h index 627c8c4655e..320627a8064 100644 --- a/src/plugins/cpptools/baseeditordocumentparser.h +++ b/src/plugins/cpptools/baseeditordocumentparser.h @@ -49,7 +49,7 @@ public: struct Configuration { bool usePrecompiledHeaders = false; QByteArray editorDefines; - ProjectPart::Ptr manuallySetProjectPart; + QString preferredProjectPartId; }; struct UpdateParams { @@ -95,7 +95,7 @@ protected: void setState(const State &state); static ProjectPartInfo determineProjectPart(const QString &filePath, - const Configuration &config, + const QString &preferredProjectPartId, const ProjectPartInfo ¤tProjectPartInfo, const ProjectExplorer::Project *activeProject, Language languagePreference, diff --git a/src/plugins/cpptools/builtineditordocumentparser.cpp b/src/plugins/cpptools/builtineditordocumentparser.cpp index f521a27aafb..ff09a635802 100644 --- a/src/plugins/cpptools/builtineditordocumentparser.cpp +++ b/src/plugins/cpptools/builtineditordocumentparser.cpp @@ -78,7 +78,7 @@ void BuiltinEditorDocumentParser::updateImpl(const QFutureInterface &futur LanguageFeatures features = LanguageFeatures::defaultFeatures(); baseState.projectPartInfo = determineProjectPart(filePath(), - baseConfig, + baseConfig.preferredProjectPartId, baseState.projectPartInfo, updateParams.activeProject, updateParams.languagePreference, diff --git a/src/plugins/cpptools/cppprojectpartchooser.cpp b/src/plugins/cpptools/cppprojectpartchooser.cpp index 85dbf254e86..470fcfd4caf 100644 --- a/src/plugins/cpptools/cppprojectpartchooser.cpp +++ b/src/plugins/cpptools/cppprojectpartchooser.cpp @@ -44,9 +44,11 @@ public: }; ProjectPartPrioritizer(const QList &projectParts, + const QString &preferredProjectPartId, const ProjectExplorer::Project *activeProject, Language languagePreference) : m_projectParts(projectParts) + , m_preferredProjectPartId(preferredProjectPartId) , m_activeProject(activeProject) , m_languagePreference(languagePreference) { @@ -93,6 +95,9 @@ private: { int thePriority = 0; + if (!m_preferredProjectPartId.isEmpty() && projectPart.id() == m_preferredProjectPartId) + thePriority += 1000; + if (projectPart.project == m_activeProject) thePriority += 100; @@ -114,6 +119,7 @@ private: private: const QList m_projectParts; + const QString m_preferredProjectPartId; const ProjectExplorer::Project *m_activeProject = nullptr; Language m_languagePreference = Language::Cxx; @@ -122,10 +128,9 @@ private: bool m_isAmbiguous = false; }; -ProjectPartInfo ProjectPartChooser::choose( - const QString &filePath, +ProjectPartInfo ProjectPartChooser::choose(const QString &filePath, const ProjectPartInfo ¤tProjectPartInfo, - const ProjectPart::Ptr &manuallySetProjectPart, + const QString &preferredProjectPartId, const ProjectExplorer::Project *activeProject, Language languagePreference, bool projectHasChanged) const @@ -134,9 +139,6 @@ ProjectPartInfo ProjectPartChooser::choose( QTC_CHECK(m_projectPartsFromDependenciesForFile); QTC_CHECK(m_fallbackProjectPart); - if (manuallySetProjectPart) - return {manuallySetProjectPart, ProjectPartInfo::NoHint}; - ProjectPart::Ptr projectPart = currentProjectPartInfo.projectPart; ProjectPartInfo::Hint hint = ProjectPartInfo::NoHint; @@ -153,12 +155,18 @@ ProjectPartInfo ProjectPartChooser::choose( projectPart = m_fallbackProjectPart(); hint = ProjectPartInfo::IsFallbackMatch; } else { - ProjectPartPrioritizer prioritizer(projectParts, activeProject, languagePreference); + ProjectPartPrioritizer prioritizer(projectParts, + preferredProjectPartId, + activeProject, + languagePreference); projectPart = prioritizer.projectPart(); } } else { if (projectHasChanged || !projectParts.contains(projectPart)) { - ProjectPartPrioritizer prioritizer(projectParts, activeProject, languagePreference); + ProjectPartPrioritizer prioritizer(projectParts, + preferredProjectPartId, + activeProject, + languagePreference); projectPart = prioritizer.projectPart(); hint = prioritizer.isAmbiguous() ? ProjectPartInfo::IsAmbiguousMatch diff --git a/src/plugins/cpptools/cppprojectpartchooser.h b/src/plugins/cpptools/cppprojectpartchooser.h index 74d4b2dcb58..6cf59544cf4 100644 --- a/src/plugins/cpptools/cppprojectpartchooser.h +++ b/src/plugins/cpptools/cppprojectpartchooser.h @@ -51,7 +51,7 @@ public: ProjectPartInfo choose( const QString &filePath, const ProjectPartInfo ¤tProjectPartInfo, - const ProjectPart::Ptr &manuallySetProjectPart, + const QString &preferredProjectPartId, const ProjectExplorer::Project *activeProject, Language languagePreference, bool projectHasChanged) const; diff --git a/tests/unit/unittest/cppprojectpartchooser-test.cpp b/tests/unit/unittest/cppprojectpartchooser-test.cpp index 2eb0f59c91f..278d43a93fa 100644 --- a/tests/unit/unittest/cppprojectpartchooser-test.cpp +++ b/tests/unit/unittest/cppprojectpartchooser-test.cpp @@ -51,7 +51,7 @@ protected: QString filePath; ProjectPartInfo currentProjectPartInfo{ProjectPart::Ptr(new ProjectPart), ProjectPartInfo::NoHint}; - ProjectPart::Ptr manuallySetProjectPart; + QString preferredProjectPartId; const ProjectExplorer::Project *activeProject = nullptr; bool projectHasChanged = false; Language languagePreference = Language::Cxx; @@ -64,11 +64,14 @@ protected: TEST_F(ProjectPartChooser, ChooseManuallySet) { - manuallySetProjectPart.reset(new ProjectPart); + ProjectPart::Ptr p1(new ProjectPart); + ProjectPart::Ptr p2(new ProjectPart); + p2->projectFile = preferredProjectPartId = "someId"; + projectPartsForFile += {p1, p2}; const ProjectPart::Ptr chosen = choose().projectPart; - ASSERT_THAT(chosen, Eq(manuallySetProjectPart)); + ASSERT_THAT(chosen, Eq(p2)); } TEST_F(ProjectPartChooser, ForMultipleChoosePrevious) @@ -249,7 +252,7 @@ const ProjectPartInfo ProjectPartChooser::choose() const { return chooser.choose(filePath, currentProjectPartInfo, - manuallySetProjectPart, + preferredProjectPartId, activeProject, languagePreference, projectHasChanged);