forked from qt-creator/qt-creator
		
	CppTools: Prioritize project parts from active project
...when selecting one for the editor document. Change-Id: I85066aaa0862870cb2db2fb2cb40c2b2c23b2cac Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
		@@ -77,17 +77,19 @@ void BaseEditorDocumentParser::setConfiguration(const Configuration &configurati
 | 
			
		||||
    m_configuration = configuration;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BaseEditorDocumentParser::update(const WorkingCopy &workingCopy)
 | 
			
		||||
void BaseEditorDocumentParser::update(const WorkingCopy &workingCopy,
 | 
			
		||||
                                      const ProjectExplorer::Project *activeProject)
 | 
			
		||||
{
 | 
			
		||||
    QFutureInterface<void> dummy;
 | 
			
		||||
    update(dummy, workingCopy);
 | 
			
		||||
    update(dummy, workingCopy, activeProject);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BaseEditorDocumentParser::update(const QFutureInterface<void> &future,
 | 
			
		||||
                                      const WorkingCopy &workingCopy)
 | 
			
		||||
                                      const WorkingCopy &workingCopy,
 | 
			
		||||
                                      const ProjectExplorer::Project *activeProject)
 | 
			
		||||
{
 | 
			
		||||
    QMutexLocker locker(&m_updateIsRunning);
 | 
			
		||||
    updateHelper(future, workingCopy);
 | 
			
		||||
    updateHelper(future, workingCopy, activeProject);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BaseEditorDocumentParser::State BaseEditorDocumentParser::state() const
 | 
			
		||||
@@ -117,9 +119,11 @@ BaseEditorDocumentParser::Ptr BaseEditorDocumentParser::get(const QString &fileP
 | 
			
		||||
    return BaseEditorDocumentParser::Ptr();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart(const QString &filePath,
 | 
			
		||||
                                                                const Configuration &config,
 | 
			
		||||
                                                                const State &state)
 | 
			
		||||
ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart(
 | 
			
		||||
        const QString &filePath,
 | 
			
		||||
        const Configuration &config,
 | 
			
		||||
        const State &state,
 | 
			
		||||
        const ProjectExplorer::Project *activeProject)
 | 
			
		||||
{
 | 
			
		||||
    Internal::ProjectPartChooser chooser;
 | 
			
		||||
    chooser.setFallbackProjectPart([](){
 | 
			
		||||
@@ -136,7 +140,8 @@ ProjectPart::Ptr BaseEditorDocumentParser::determineProjectPart(const QString &f
 | 
			
		||||
    return chooser.choose(filePath,
 | 
			
		||||
                          state.projectPart,
 | 
			
		||||
                          config.manuallySetProjectPart,
 | 
			
		||||
                          config.stickToPreviousProjectPart);
 | 
			
		||||
                          config.stickToPreviousProjectPart,
 | 
			
		||||
                          activeProject);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace CppTools
 | 
			
		||||
 
 | 
			
		||||
@@ -33,6 +33,8 @@
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QMutex>
 | 
			
		||||
 | 
			
		||||
namespace ProjectExplorer { class Project; }
 | 
			
		||||
 | 
			
		||||
namespace CppTools {
 | 
			
		||||
 | 
			
		||||
class CPPTOOLS_EXPORT BaseEditorDocumentParser : public QObject
 | 
			
		||||
@@ -58,8 +60,11 @@ public:
 | 
			
		||||
    Configuration configuration() const;
 | 
			
		||||
    void setConfiguration(const Configuration &configuration);
 | 
			
		||||
 | 
			
		||||
    void update(const WorkingCopy &workingCopy);
 | 
			
		||||
    void update(const QFutureInterface<void> &future, const WorkingCopy &workingCopy);
 | 
			
		||||
    void update(const WorkingCopy &workingCopy,
 | 
			
		||||
                const ProjectExplorer::Project *activeProject);
 | 
			
		||||
    void update(const QFutureInterface<void> &future,
 | 
			
		||||
                const WorkingCopy &workingCopy,
 | 
			
		||||
                const ProjectExplorer::Project *activeProject);
 | 
			
		||||
 | 
			
		||||
    ProjectPart::Ptr projectPart() const;
 | 
			
		||||
 | 
			
		||||
@@ -73,13 +78,15 @@ protected:
 | 
			
		||||
 | 
			
		||||
    static ProjectPart::Ptr determineProjectPart(const QString &filePath,
 | 
			
		||||
                                                 const Configuration &config,
 | 
			
		||||
                                                 const State &state);
 | 
			
		||||
                                                 const State &state,
 | 
			
		||||
                                                 const ProjectExplorer::Project *activeProject);
 | 
			
		||||
 | 
			
		||||
    mutable QMutex m_stateAndConfigurationMutex;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    virtual void updateHelper(const QFutureInterface<void> &future,
 | 
			
		||||
                              const WorkingCopy &workingCopy) = 0;
 | 
			
		||||
                              const WorkingCopy &workingCopy,
 | 
			
		||||
                              const ProjectExplorer::Project *activeProject) = 0;
 | 
			
		||||
 | 
			
		||||
    const QString m_filePath;
 | 
			
		||||
    Configuration m_configuration;
 | 
			
		||||
 
 | 
			
		||||
@@ -73,7 +73,8 @@ void BaseEditorDocumentProcessor::editorDocumentTimerRestarted()
 | 
			
		||||
 | 
			
		||||
void BaseEditorDocumentProcessor::runParser(QFutureInterface<void> &future,
 | 
			
		||||
                                            BaseEditorDocumentParser::Ptr parser,
 | 
			
		||||
                                            const WorkingCopy workingCopy)
 | 
			
		||||
                                            const WorkingCopy workingCopy,
 | 
			
		||||
                                            const ProjectExplorer::Project *activeProject)
 | 
			
		||||
{
 | 
			
		||||
    future.setProgressRange(0, 1);
 | 
			
		||||
    if (future.isCanceled()) {
 | 
			
		||||
@@ -81,7 +82,7 @@ void BaseEditorDocumentProcessor::runParser(QFutureInterface<void> &future,
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    parser->update(future, workingCopy);
 | 
			
		||||
    parser->update(future, workingCopy, activeProject);
 | 
			
		||||
    CppToolsBridge::finishedRefreshingSourceFiles({parser->filePath()});
 | 
			
		||||
 | 
			
		||||
    future.setProgressValue(1);
 | 
			
		||||
 
 | 
			
		||||
@@ -91,7 +91,8 @@ signals:
 | 
			
		||||
protected:
 | 
			
		||||
    static void runParser(QFutureInterface<void> &future,
 | 
			
		||||
                          BaseEditorDocumentParser::Ptr parser,
 | 
			
		||||
                          const CppTools::WorkingCopy workingCopy);
 | 
			
		||||
                          const CppTools::WorkingCopy workingCopy,
 | 
			
		||||
                          const ProjectExplorer::Project *activeProject);
 | 
			
		||||
 | 
			
		||||
    // Convenience
 | 
			
		||||
    QString filePath() const { return m_filePath; }
 | 
			
		||||
 
 | 
			
		||||
@@ -56,7 +56,8 @@ BuiltinEditorDocumentParser::BuiltinEditorDocumentParser(const QString &filePath
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BuiltinEditorDocumentParser::updateHelper(const QFutureInterface<void> &future,
 | 
			
		||||
                                               const WorkingCopy &theWorkingCopy)
 | 
			
		||||
                                               const WorkingCopy &theWorkingCopy,
 | 
			
		||||
                                               const ProjectExplorer::Project *activeProject)
 | 
			
		||||
{
 | 
			
		||||
    if (filePath().isEmpty())
 | 
			
		||||
        return;
 | 
			
		||||
@@ -77,7 +78,7 @@ void BuiltinEditorDocumentParser::updateHelper(const QFutureInterface<void> &fut
 | 
			
		||||
    QString projectConfigFile;
 | 
			
		||||
    LanguageFeatures features = LanguageFeatures::defaultFeatures();
 | 
			
		||||
 | 
			
		||||
    baseState.projectPart = determineProjectPart(filePath(), baseConfig, baseState);
 | 
			
		||||
    baseState.projectPart = determineProjectPart(filePath(), baseConfig, baseState, activeProject);
 | 
			
		||||
 | 
			
		||||
    if (state.forceSnapshotInvalidation) {
 | 
			
		||||
        invalidateSnapshot = true;
 | 
			
		||||
 
 | 
			
		||||
@@ -59,7 +59,8 @@ public:
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    void updateHelper(const QFutureInterface<void> &future,
 | 
			
		||||
                      const WorkingCopy &workingCopy) override;
 | 
			
		||||
                      const WorkingCopy &workingCopy,
 | 
			
		||||
                      const ProjectExplorer::Project *activeProject) override;
 | 
			
		||||
    void addFileAndDependencies(CPlusPlus::Snapshot *snapshot,
 | 
			
		||||
                                QSet<Utils::FileName> *toRemove,
 | 
			
		||||
                                const Utils::FileName &fileName) const;
 | 
			
		||||
 
 | 
			
		||||
@@ -32,6 +32,8 @@
 | 
			
		||||
#include "cpptoolsreuse.h"
 | 
			
		||||
#include "cppworkingcopy.h"
 | 
			
		||||
 | 
			
		||||
#include <projectexplorer/session.h>
 | 
			
		||||
 | 
			
		||||
#include <texteditor/convenience.h>
 | 
			
		||||
#include <texteditor/fontsettings.h>
 | 
			
		||||
#include <texteditor/refactoroverlay.h>
 | 
			
		||||
@@ -205,10 +207,13 @@ BuiltinEditorDocumentProcessor::~BuiltinEditorDocumentProcessor()
 | 
			
		||||
void BuiltinEditorDocumentProcessor::run()
 | 
			
		||||
{
 | 
			
		||||
    CppModelManager *mgr = CppModelManager::instance();
 | 
			
		||||
    const ProjectExplorer::Project *activeProject
 | 
			
		||||
            = ProjectExplorer::SessionManager::startupProject();
 | 
			
		||||
    m_parserFuture = Utils::runAsync(mgr->sharedThreadPool(),
 | 
			
		||||
                                     runParser,
 | 
			
		||||
                                     parser(),
 | 
			
		||||
                                     mgr->workingCopy());
 | 
			
		||||
                                     mgr->workingCopy(),
 | 
			
		||||
                                     activeProject);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BaseEditorDocumentParser::Ptr BuiltinEditorDocumentProcessor::parser()
 | 
			
		||||
 
 | 
			
		||||
@@ -158,7 +158,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());
 | 
			
		||||
        parser.update(CppModelManager::instance()->workingCopy(), nullptr);
 | 
			
		||||
        CPlusPlus::Document::Ptr document = parser.document();
 | 
			
		||||
        QTC_ASSERT(document, return);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -430,8 +430,7 @@ IAssistProcessor *InternalCompletionAssistProvider::createProcessor() const
 | 
			
		||||
    return new InternalCppCompletionAssistProcessor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
AssistInterface *InternalCompletionAssistProvider::createAssistInterface(
 | 
			
		||||
        const QString &filePath,
 | 
			
		||||
AssistInterface *InternalCompletionAssistProvider::createAssistInterface(const QString &filePath,
 | 
			
		||||
        const TextEditorWidget *textEditorWidget,
 | 
			
		||||
        const LanguageFeatures &languageFeatures,
 | 
			
		||||
        int position,
 | 
			
		||||
@@ -2123,7 +2122,7 @@ void CppCompletionAssistInterface::getCppSpecifics() const
 | 
			
		||||
    m_gotCppSpecifics = true;
 | 
			
		||||
 | 
			
		||||
    if (m_parser) {
 | 
			
		||||
        m_parser->update(CppTools::CppModelManager::instance()->workingCopy());
 | 
			
		||||
        m_parser->update(CppTools::CppModelManager::instance()->workingCopy(), nullptr);
 | 
			
		||||
        m_snapshot = m_parser->snapshot();
 | 
			
		||||
        m_headerPaths = m_parser->headerPaths();
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -877,7 +877,7 @@ void CppToolsPlugin::test_modelmanager_precompiled_headers()
 | 
			
		||||
        BaseEditorDocumentParser::Configuration config = parser->configuration();
 | 
			
		||||
        config.usePrecompiledHeaders = true;
 | 
			
		||||
        parser->setConfiguration(config);
 | 
			
		||||
        parser->update(CppModelManager::instance()->workingCopy());
 | 
			
		||||
        parser->update(CppModelManager::instance()->workingCopy(), nullptr);
 | 
			
		||||
 | 
			
		||||
        // Check if defines from pch are considered
 | 
			
		||||
        Document::Ptr document = mm->document(fileName);
 | 
			
		||||
@@ -955,7 +955,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());
 | 
			
		||||
        parser->update(CppModelManager::instance()->workingCopy(), nullptr);
 | 
			
		||||
 | 
			
		||||
        Document::Ptr doc = mm->document(main1File);
 | 
			
		||||
        QCOMPARE(nameOfFirstDeclaration(doc), firstDeclarationName);
 | 
			
		||||
 
 | 
			
		||||
@@ -25,15 +25,25 @@
 | 
			
		||||
 | 
			
		||||
#include "cppprojectpartchooser.h"
 | 
			
		||||
 | 
			
		||||
#include <utils/algorithm.h>
 | 
			
		||||
#include <utils/qtcassert.h>
 | 
			
		||||
 | 
			
		||||
namespace CppTools {
 | 
			
		||||
namespace Internal {
 | 
			
		||||
 | 
			
		||||
static ProjectPart::Ptr selectFromActiveProject(const QList<ProjectPart::Ptr> &projectParts,
 | 
			
		||||
                                                const ProjectExplorer::Project *activeProject)
 | 
			
		||||
{
 | 
			
		||||
    return Utils::findOr(projectParts, projectParts.first(), [&](const ProjectPart::Ptr &projectPart){
 | 
			
		||||
        return projectPart->project == activeProject;
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ProjectPart::Ptr ProjectPartChooser::choose(const QString &filePath,
 | 
			
		||||
                                            const ProjectPart::Ptr ¤tProjectPart,
 | 
			
		||||
                                            const ProjectPart::Ptr &manuallySetProjectPart,
 | 
			
		||||
                                            bool stickToPreviousProjectPart) const
 | 
			
		||||
                                            bool stickToPreviousProjectPart,
 | 
			
		||||
                                            const ProjectExplorer::Project *activeProject) const
 | 
			
		||||
{
 | 
			
		||||
    QTC_CHECK(m_projectPartsForFile);
 | 
			
		||||
    QTC_CHECK(m_projectPartsFromDependenciesForFile);
 | 
			
		||||
@@ -57,11 +67,11 @@ ProjectPart::Ptr ProjectPartChooser::choose(const QString &filePath,
 | 
			
		||||
            // Fall-back step 2: Use fall-back part from the model manager:
 | 
			
		||||
            projectPart = m_fallbackProjectPart();
 | 
			
		||||
        else
 | 
			
		||||
            projectPart = projectParts.first();
 | 
			
		||||
            projectPart = selectFromActiveProject(projectParts, activeProject);
 | 
			
		||||
    } else {
 | 
			
		||||
        if (!projectParts.contains(projectPart))
 | 
			
		||||
            // Apparently the project file changed, so update our project part.
 | 
			
		||||
            projectPart = projectParts.first();
 | 
			
		||||
            projectPart = selectFromActiveProject(projectParts, activeProject);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return projectPart;
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,8 @@
 | 
			
		||||
 | 
			
		||||
#include <functional>
 | 
			
		||||
 | 
			
		||||
namespace ProjectExplorer { class Project; }
 | 
			
		||||
 | 
			
		||||
namespace CppTools {
 | 
			
		||||
namespace Internal {
 | 
			
		||||
 | 
			
		||||
@@ -48,7 +50,8 @@ public:
 | 
			
		||||
    ProjectPart::Ptr choose(const QString &filePath,
 | 
			
		||||
                            const ProjectPart::Ptr ¤tProjectPart,
 | 
			
		||||
                            const ProjectPart::Ptr &manuallySetProjectPart,
 | 
			
		||||
                            bool stickToPreviousProjectPart) const;
 | 
			
		||||
                            bool stickToPreviousProjectPart,
 | 
			
		||||
                            const ProjectExplorer::Project *activeProject) const;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    FallBackProjectPart m_fallbackProjectPart;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user